ofb.c revision 1.10 1 /* $NetBSD: ofb.c,v 1.10 1999/12/06 19:25:58 drochner Exp $ */
2
3 /*
4 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 #include <sys/param.h>
31 #include <sys/buf.h>
32 #include <sys/conf.h>
33 #include <sys/device.h>
34 #include <sys/ioctl.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/systm.h>
38
39 #include <dev/pci/pcidevs.h>
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
42
43 #include <dev/rcons/raster.h>
44 #include <dev/wscons/wsconsio.h>
45 #include <dev/wscons/wscons_raster.h>
46 #include <dev/wscons/wsdisplayvar.h>
47
48 #include <machine/bus.h>
49 #include <machine/grfioctl.h>
50
51 #include <macppc/dev/ofbvar.h>
52
53 int ofbmatch __P((struct device *, struct cfdata *, void *));
54 void ofbattach __P((struct device *, struct device *, void *));
55 int ofbprint __P((void *, const char *));
56
57 struct cfattach ofb_ca = {
58 sizeof(struct ofb_softc), ofbmatch, ofbattach,
59 };
60
61 struct ofb_devconfig ofb_console_dc;
62
63 struct wsdisplay_emulops ofb_emulops = {
64 rcons_cursor, /* could use hardware cursor; punt */
65 rcons_mapchar,
66 rcons_putchar,
67 rcons_copycols,
68 rcons_erasecols,
69 rcons_copyrows,
70 rcons_eraserows,
71 rcons_alloc_attr
72 };
73
74 struct wsscreen_descr ofb_stdscreen = {
75 "std",
76 0, 0, /* will be filled in -- XXX shouldn't, it's global */
77 &ofb_emulops,
78 0, 0,
79 WSSCREEN_REVERSE
80 };
81
82 const struct wsscreen_descr *_ofb_scrlist[] = {
83 &ofb_stdscreen,
84 /* XXX other formats, graphics screen? */
85 };
86
87 struct wsscreen_list ofb_screenlist = {
88 sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
89 };
90
91 static int ofb_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
92 static int ofb_mmap __P((void *, off_t, int));
93 static int ofb_alloc_screen __P((void *, const struct wsscreen_descr *,
94 void **, int *, int *, long *));
95 static void ofb_free_screen __P((void *, void *));
96 static int ofb_show_screen __P((void *, void *, int,
97 void (*) (void *, int, int), void *));
98
99 struct wsdisplay_accessops ofb_accessops = {
100 ofb_ioctl,
101 ofb_mmap,
102 ofb_alloc_screen,
103 ofb_free_screen,
104 ofb_show_screen,
105 0 /* load_font */
106 };
107
108 static void ofb_common_init __P((int, struct ofb_devconfig *));
109
110 int
111 ofbmatch(parent, match, aux)
112 struct device *parent;
113 struct cfdata *match;
114 void *aux;
115 {
116 struct pci_attach_args *pa = aux;
117
118 /* /chaos/control */
119 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
120 PCI_PRODUCT(pa->pa_id) == 3)
121 return 1;
122
123 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
124 return 1;
125
126 return 0;
127 }
128
129 void
130 ofbattach(parent, self, aux)
131 struct device *parent, *self;
132 void *aux;
133 {
134 struct ofb_softc *sc = (struct ofb_softc *)self;
135 struct pci_attach_args *pa = aux;
136 struct wsemuldisplaydev_attach_args a;
137 int console;
138 struct ofb_devconfig *dc;
139 char devinfo[256];
140
141 console = ofb_is_console();
142
143 if (console) {
144 dc = &ofb_console_dc;
145 sc->nscreens = 1;
146 } else {
147 int node, i, screenbytes;
148
149 dc = malloc(sizeof(struct ofb_devconfig), M_DEVBUF, M_WAITOK);
150 bzero(dc, sizeof(struct ofb_devconfig));
151 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
152 if (node == 0) {
153 printf(": ofdev not found\n");
154 return;
155 }
156 ofb_common_init(node, dc);
157
158 /* Set colormap to black on white. */
159 OF_call_method_1("color!", dc->dc_ih, 4, 0, 0, 0, 0xff);
160 OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 0);
161 screenbytes = dc->dc_height * dc->dc_linebytes;
162 for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
163 *(u_int32_t *)(dc->dc_paddr + i) = 0;
164 }
165 sc->sc_dc = dc;
166
167 if (dc->dc_paddr == 0) {
168 printf(": cannot map framebuffer\n");
169 return;
170 }
171
172 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
173 printf(": %s\n", devinfo);
174 printf("%s: %d x %d, %dbpp\n", self->dv_xname,
175 dc->dc_raster.width, dc->dc_raster.height, dc->dc_raster.depth);
176
177 a.console = console;
178 a.scrdata = &ofb_screenlist;
179 a.accessops = &ofb_accessops;
180 a.accesscookie = sc;
181
182 config_found(self, &a, wsemuldisplaydevprint);
183 }
184
185 void
186 ofb_common_init(node, dc)
187 int node;
188 struct ofb_devconfig *dc;
189 {
190 struct raster *rap;
191 struct rcons *rcp;
192 int i;
193 int addr, width, height, linebytes, depth;
194
195 if (dc->dc_ih == 0) {
196 char name[64];
197
198 bzero(name, 64);
199 OF_package_to_path(node, name, sizeof(name));
200 dc->dc_ih = OF_open(name);
201 }
202
203 /* XXX /chaos/control doesn't have "width", "height", ... */
204 width = height = -1;
205 if (OF_getprop(node, "width", &width, sizeof(width)) != 4)
206 OF_interpret("screen-width", 1, &width);
207 if (OF_getprop(node, "height", &height, sizeof(height)) != 4)
208 OF_interpret("screen-height", 1, &height);
209 if (OF_getprop(node, "linebytes", &linebytes, sizeof(linebytes)) != 4)
210 linebytes = width; /* XXX */
211 if (OF_getprop(node, "depth", &depth, sizeof(depth)) != 4)
212 depth = 8; /* XXX */
213 if (width == -1 || height == -1)
214 return;
215
216 OF_interpret("frame-buffer-adr", 1, &addr);
217 if (addr == 0 || addr == -1)
218 return;
219 dc->dc_paddr = addr; /* PA of the frame buffer */
220
221 /* initialize the raster */
222 rap = &dc->dc_raster;
223 rap->width = width;
224 rap->height = height;
225 rap->depth = depth;
226 rap->linelongs = linebytes / sizeof(u_int32_t);
227 rap->pixels = (u_int32_t *)addr;
228
229 /* initialize the raster console blitter */
230 rcp = &dc->dc_rcons;
231 rcp->rc_sp = rap;
232 rcp->rc_crow = rcp->rc_ccol = -1;
233 rcp->rc_crowp = &rcp->rc_crow;
234 rcp->rc_ccolp = &rcp->rc_ccol;
235 rcons_init(rcp, 128, 128);
236
237 /* If screen is smaller than 1024x768, use small font. */
238 if ((width < 1024 || height < 768) && copy_rom_font() == 0) {
239 rcp->rc_xorigin = 2;
240 rcp->rc_yorigin = 4;
241
242 OF_interpret("#lines", 1, &dc->dc_rcons.rc_maxrow);
243 OF_interpret("#columns", 1, &dc->dc_rcons.rc_maxcol);
244 }
245
246 ofb_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
247 ofb_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
248 }
249
250 int
251 ofb_is_console()
252 {
253 int chosen, stdout, node;
254 char type[16];
255
256 chosen = OF_finddevice("/chosen");
257 OF_getprop(chosen, "stdout", &stdout, 4);
258 node = OF_instance_to_package(stdout);
259 OF_getprop(node, "device_type", type, sizeof(type));
260 if (strcmp(type, "display") == 0)
261 return 1;
262 else
263 return 0;
264 }
265
266 int
267 ofb_ioctl(v, cmd, data, flag, p)
268 void *v;
269 u_long cmd;
270 caddr_t data;
271 int flag;
272 struct proc *p;
273 {
274 struct ofb_softc *sc = v;
275 struct ofb_devconfig *dc = sc->sc_dc;
276 struct wsdisplay_fbinfo *wdf;
277 struct grfinfo *gm;
278
279 switch (cmd) {
280 case WSDISPLAYIO_GTYPE:
281 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */
282 return 0;
283
284 case WSDISPLAYIO_GINFO:
285 wdf = (void *)data;
286 wdf->height = sc->sc_dc->dc_raster.height;
287 wdf->width = sc->sc_dc->dc_raster.width;
288 wdf->depth = sc->sc_dc->dc_raster.depth;
289 wdf->cmsize = 256;
290 return 0;
291
292 case WSDISPLAYIO_PUTCMAP:
293 return putcmap(sc, data);
294
295 /* XXX There are no way to know framebuffer pa from a user program. */
296 case GRFIOCGINFO:
297 gm = (void *)data;
298 bzero(gm, sizeof(struct grfinfo));
299 gm->gd_fbaddr = (caddr_t)sc->sc_dc->dc_paddr;
300 gm->gd_fbrowbytes = sc->sc_dc->dc_linebytes;
301 return 0;
302 }
303 return -1;
304 }
305
306 int
307 ofb_mmap(v, offset, prot)
308 void *v;
309 off_t offset;
310 int prot;
311 {
312 struct ofb_softc *sc = v;
313 struct ofb_devconfig *dc = sc->sc_dc;
314
315 if (offset >= (dc->dc_linebytes * dc->dc_height) || offset < 0)
316 return -1;
317
318 return dc->dc_paddr + offset;
319 }
320
321 int
322 ofb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
323 void *v;
324 const struct wsscreen_descr *type;
325 void **cookiep;
326 int *curxp, *curyp;
327 long *attrp;
328 {
329 struct ofb_softc *sc = v;
330 long defattr;
331
332 if (sc->nscreens > 0)
333 return (ENOMEM);
334
335 *cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
336 *curxp = 0;
337 *curyp = 0;
338 rcons_alloc_attr(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
339 *attrp = defattr;
340 sc->nscreens++;
341 return 0;
342 }
343
344 void
345 ofb_free_screen(v, cookie)
346 void *v;
347 void *cookie;
348 {
349 struct ofb_softc *sc = v;
350
351 if (sc->sc_dc == &ofb_console_dc)
352 panic("ofb_free_screen: console");
353
354 sc->nscreens--;
355 }
356
357 int
358 ofb_show_screen(v, cookie, waitok, cb, cbarg)
359 void *v;
360 void *cookie;
361 int waitok;
362 void (*cb) __P((void *, int, int));
363 void *cbarg;
364 {
365
366 return (0);
367 }
368
369 int
370 ofb_cnattach()
371 {
372 struct ofb_devconfig *dc = &ofb_console_dc;
373 long defattr;
374 int crow = 0, i, screenbytes;
375 int chosen, stdout, node;
376 char cmd[32];
377
378 chosen = OF_finddevice("/chosen");
379 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
380 node = OF_instance_to_package(stdout);
381 dc->dc_ih = stdout;
382
383 ofb_common_init(node, dc);
384 screenbytes = dc->dc_height * dc->dc_linebytes;
385
386 /* Set colormap to black on white. */
387 OF_call_method_1("color!", dc->dc_ih, 4, 0, 0, 0, 0xff);
388 OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 0xf0);
389 for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
390 *(u_int32_t *)(dc->dc_paddr + i) ^= 0xffffffff;
391 OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 0);
392
393 /* get current cursor position */
394 OF_interpret("line#", 1, &crow);
395
396 /* move (rom monitor) cursor to the lowest line */
397 sprintf(cmd, "%x to line#", ofb_stdscreen.nrows - 1);
398 OF_interpret(cmd, 0);
399
400 if (dc->dc_width >= 1024 && dc->dc_height >= 768) {
401 for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
402 *(u_int32_t *)(dc->dc_paddr + i) = 0;
403 crow = 0;
404 }
405
406 rcons_alloc_attr(&dc->dc_rcons, 0, 0, 0, &defattr);
407 wsdisplay_cnattach(&ofb_stdscreen, &dc->dc_rcons, 0, crow, defattr);
408
409 return 0;
410 }
411
412 int
413 copy_rom_font()
414 {
415 int i, j;
416 u_char *romfont;
417 int char_width, char_height;
418 int chosen, mmu, m, e;
419
420 extern struct raster_font gallant19; /* XXX */
421
422 /* Get ROM FONT address. */
423 OF_interpret("font-adr", 1, &romfont);
424 if (romfont == NULL)
425 return -1;
426
427 chosen = OF_finddevice("/chosen");
428 OF_getprop(chosen, "mmu", &mmu, 4);
429
430 /*
431 * Convert to physcal address. We cannot access to Open Firmware's
432 * virtual address space.
433 */
434 OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
435
436 /* Get character size */
437 OF_interpret("char-width", 1, &char_width);
438 OF_interpret("char-height", 1, &char_height);
439
440 /* XXX but we cannot use malloc here... */
441 gallant19.width = char_width;
442 gallant19.height = char_height;
443 gallant19.ascent = 0;
444
445 for (i = 32; i < 128; i++) {
446 u_int *p;
447
448 if (gallant19.chars[i].r == NULL)
449 continue;
450
451 gallant19.chars[i].r->width = char_width;
452 gallant19.chars[i].r->height = char_height;
453 p = gallant19.chars[i].r->pixels;
454
455 for (j = 0; j < char_height; j++)
456 *p++ = romfont[(i - 32) * char_height + j] << 24;
457 }
458 return 0;
459 }
460
461 int
462 putcmap(sc, cm)
463 struct ofb_softc *sc;
464 struct wsdisplay_cmap *cm;
465 {
466 struct ofb_devconfig *dc = sc->sc_dc;
467 int index = cm->index;
468 int count = cm->count;
469 int i;
470 u_char *r, *g, *b;
471
472 if (cm->index >= 256 || cm->count > 256 ||
473 (cm->index + cm->count) > 256)
474 return EINVAL;
475 if (!uvm_useracc(cm->red, cm->count, B_READ) ||
476 !uvm_useracc(cm->green, cm->count, B_READ) ||
477 !uvm_useracc(cm->blue, cm->count, B_READ))
478 return EFAULT;
479 copyin(cm->red, &sc->sc_cmap_red[index], count);
480 copyin(cm->green, &sc->sc_cmap_green[index], count);
481 copyin(cm->blue, &sc->sc_cmap_blue[index], count);
482
483 r = &sc->sc_cmap_red[index];
484 g = &sc->sc_cmap_green[index];
485 b = &sc->sc_cmap_blue[index];
486
487 for (i = 0; i < count; i++) {
488 OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
489 r++, g++, b++, index++;
490 }
491
492 return 0;
493 }
494