ofb.c revision 1.36 1 /* $NetBSD: ofb.c,v 1.36 2003/07/15 02:43:30 lukem 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/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: ofb.c,v 1.36 2003/07/15 02:43:30 lukem Exp $");
32
33 #include <sys/param.h>
34 #include <sys/buf.h>
35 #include <sys/conf.h>
36 #include <sys/device.h>
37 #include <sys/ioctl.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/systm.h>
41
42 #include <uvm/uvm_extern.h>
43
44 #include <dev/pci/pcidevs.h>
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pciio.h>
48
49 #include <dev/wscons/wsconsio.h>
50 #include <dev/wscons/wsdisplayvar.h>
51 #include <dev/rasops/rasops.h>
52
53 #include <dev/ofw/openfirm.h>
54 #include <dev/ofw/ofw_pci.h>
55
56 #include <machine/bat.h>
57 #include <machine/bus.h>
58 #include <machine/autoconf.h>
59 #include <machine/grfioctl.h>
60
61 #include <macppc/dev/ofbvar.h>
62
63 #if OFB_ENABLE_CACHE
64 int ofb_enable_cache = 1;
65 #else
66 int ofb_enable_cache = 0;
67 #endif
68
69 int ofbmatch __P((struct device *, struct cfdata *, void *));
70 void ofbattach __P((struct device *, struct device *, void *));
71 int ofbprint __P((void *, const char *));
72
73 CFATTACH_DECL(ofb, sizeof(struct ofb_softc),
74 ofbmatch, ofbattach, NULL, NULL);
75
76 struct ofb_devconfig ofb_console_dc;
77
78 struct wsscreen_descr ofb_stdscreen = {
79 "std",
80 0, 0, /* will be filled in -- XXX shouldn't, it's global */
81 0,
82 0, 0,
83 WSSCREEN_REVERSE
84 };
85
86 const struct wsscreen_descr *_ofb_scrlist[] = {
87 &ofb_stdscreen,
88 /* XXX other formats, graphics screen? */
89 };
90
91 struct wsscreen_list ofb_screenlist = {
92 sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
93 };
94
95 static int ofb_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
96 static paddr_t ofb_mmap __P((void *, off_t, int));
97 static int ofb_alloc_screen __P((void *, const struct wsscreen_descr *,
98 void **, int *, int *, long *));
99 static void ofb_free_screen __P((void *, void *));
100 static int ofb_show_screen __P((void *, void *, int,
101 void (*) (void *, int, int), void *));
102 static int copy_rom_font __P((void));
103
104 struct wsdisplay_accessops ofb_accessops = {
105 ofb_ioctl,
106 ofb_mmap,
107 ofb_alloc_screen,
108 ofb_free_screen,
109 ofb_show_screen,
110 0 /* load_font */
111 };
112
113 static struct wsdisplay_font openfirm6x11;
114
115 static void ofb_common_init __P((int, struct ofb_devconfig *));
116 static int ofb_getcmap __P((struct ofb_softc *, struct wsdisplay_cmap *));
117 static int ofb_putcmap __P((struct ofb_softc *, struct wsdisplay_cmap *));
118
119 int
120 ofbmatch(parent, match, aux)
121 struct device *parent;
122 struct cfdata *match;
123 void *aux;
124 {
125 struct pci_attach_args *pa = aux;
126
127 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
128 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
129 return 1;
130
131 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
132 return 1;
133
134 return 0;
135 }
136
137 void
138 ofbattach(parent, self, aux)
139 struct device *parent, *self;
140 void *aux;
141 {
142 struct ofb_softc *sc = (struct ofb_softc *)self;
143 struct pci_attach_args *pa = aux;
144 struct wsemuldisplaydev_attach_args a;
145 int console, node;
146 struct ofb_devconfig *dc;
147 char devinfo[256];
148
149 console = ofb_is_console();
150
151 if (console) {
152 dc = &ofb_console_dc;
153 node = dc->dc_node;
154 sc->nscreens = 1;
155 } else {
156 int i, len, screenbytes;
157
158 dc = malloc(sizeof(struct ofb_devconfig), M_DEVBUF, M_WAITOK);
159 memset(dc, 0, sizeof(struct ofb_devconfig));
160 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
161 if (node == 0) {
162 printf(": ofdev not found\n");
163 return;
164 }
165
166 /* XXX There are two child screens on PowerBook. */
167 memset(devinfo, 0, sizeof(devinfo));
168 OF_getprop(node, "device_type", devinfo, sizeof(devinfo));
169 len = strlen(devinfo);
170 if (strcmp(devinfo + len - 7, "-parent") == 0)
171 node = OF_child(node);
172
173 ofb_common_init(node, dc);
174
175 screenbytes = dc->dc_ri.ri_stride * dc->dc_ri.ri_height;
176 for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
177 *(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
178 }
179 sc->sc_dc = dc;
180
181 sc->sc_pc = pa->pa_pc;
182 sc->sc_pcitag = pa->pa_tag;
183
184 /* XXX */
185 if (OF_getprop(node, "assigned-addresses", sc->sc_addrs,
186 sizeof(sc->sc_addrs)) == -1) {
187 node = OF_parent(node);
188 OF_getprop(node, "assigned-addresses", sc->sc_addrs,
189 sizeof(sc->sc_addrs));
190 }
191
192 if (dc->dc_paddr == 0) {
193 printf(": cannot map framebuffer\n");
194 return;
195 }
196
197 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
198 printf(": %s\n", devinfo);
199 printf("%s: %d x %d, %dbpp\n", self->dv_xname,
200 dc->dc_ri.ri_width, dc->dc_ri.ri_height, dc->dc_ri.ri_depth);
201
202 sc->sc_cmap_red[0] = sc->sc_cmap_green[0] = sc->sc_cmap_blue[0] = 0;
203 sc->sc_cmap_red[15] = sc->sc_cmap_red[255] = 0xff;
204 sc->sc_cmap_green[15] = sc->sc_cmap_green[255] = 0xff;
205 sc->sc_cmap_blue[15] = sc->sc_cmap_blue[255] = 0xff;
206
207 a.console = console;
208 a.scrdata = &ofb_screenlist;
209 a.accessops = &ofb_accessops;
210 a.accesscookie = sc;
211
212 config_found(self, &a, wsemuldisplaydevprint);
213 }
214
215 void
216 ofb_common_init(node, dc)
217 int node;
218 struct ofb_devconfig *dc;
219 {
220 struct rasops_info *ri = &dc->dc_ri;
221 int32_t addr, width, height, linebytes, depth;
222
223 dc->dc_node = node;
224 if (dc->dc_ih == 0) {
225 char name[64];
226
227 memset(name, 0, 64);
228 OF_package_to_path(node, name, sizeof(name));
229 dc->dc_ih = OF_open(name);
230 }
231
232 /* XXX /chaos/control doesn't have "width", "height", ... */
233 width = height = -1;
234 if (OF_getprop(node, "width", &width, 4) != 4)
235 OF_interpret("screen-width", 1, &width);
236 if (OF_getprop(node, "height", &height, 4) != 4)
237 OF_interpret("screen-height", 1, &height);
238 if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
239 linebytes = width; /* XXX */
240 if (OF_getprop(node, "depth", &depth, 4) != 4)
241 depth = 8; /* XXX */
242 if (OF_getprop(node, "address", &addr, 4) != 4)
243 OF_interpret("frame-buffer-adr", 1, &addr);
244
245 if (width == -1 || height == -1 || addr == 0 || addr == -1)
246 return;
247
248 dc->dc_paddr = addr; /* PA of the frame buffer */
249
250 /* Make sure 0/0/0 is black and 255/255/255 is white. */
251 OF_call_method_1("color!", dc->dc_ih, 4, 0, 0, 0, 0);
252 OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 255);
253
254 /* Enable write-through cache. */
255 if (ofb_enable_cache) {
256 vaddr_t va;
257 /*
258 * Let's try to find an empty BAT to use
259 */
260 for (va = SEGMENT_LENGTH; va < (USER_SR << ADDR_SR_SHFT);
261 va += SEGMENT_LENGTH) {
262 if (battable[va >> ADDR_SR_SHFT].batu == 0) {
263 battable[va >> ADDR_SR_SHFT].batl =
264 BATL(addr & 0xf0000000,
265 BAT_G | BAT_W | BAT_M, BAT_PP_RW);
266 battable[va >> ADDR_SR_SHFT].batu =
267 BATL(va, BAT_BL_256M, BAT_Vs);
268 addr &= 0x0fffffff;
269 addr |= va;
270 break;
271 }
272 }
273 }
274
275 /* initialize rasops */
276 ri->ri_width = width;
277 ri->ri_height = height;
278 ri->ri_depth = depth;
279 ri->ri_stride = linebytes;
280 ri->ri_bits = (char *)addr;
281 ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR | RI_CENTER;
282
283 /* If screen is smaller than 1024x768, use small font. */
284 if ((width < 1024 || height < 768) && copy_rom_font() == 0) {
285 int cols, rows;
286
287 OF_interpret("#lines", 1, &rows);
288 OF_interpret("#columns", 1, &cols);
289
290 ri->ri_font = &openfirm6x11;
291 ri->ri_wsfcookie = -1; /* not using wsfont */
292 rasops_init(ri, rows, cols);
293
294 ri->ri_xorigin = 2;
295 ri->ri_yorigin = 3;
296 ri->ri_bits = (char *)addr + ri->ri_xorigin +
297 ri->ri_stride * ri->ri_yorigin;
298 } else {
299 rasops_init(ri, 24, 80);
300 rasops_reconfig(ri, (height - 2) / ri->ri_font->fontheight,
301 ((width - 2) / ri->ri_font->fontwidth) & ~7);
302 }
303
304 /* black on white */
305 ri->ri_devcmap[0] = 0xffffffff; /* bg */
306 ri->ri_devcmap[1] = 0; /* fg */
307
308 ofb_stdscreen.nrows = ri->ri_rows;
309 ofb_stdscreen.ncols = ri->ri_cols;
310 ofb_stdscreen.textops = &ri->ri_ops;
311 ofb_stdscreen.capabilities = ri->ri_caps;
312 }
313
314 int
315 ofb_is_console()
316 {
317 int chosen, stdout, node;
318 char type[16];
319
320 chosen = OF_finddevice("/chosen");
321 OF_getprop(chosen, "stdout", &stdout, 4);
322 node = OF_instance_to_package(stdout);
323 OF_getprop(node, "device_type", type, sizeof(type));
324 if (strcmp(type, "display") == 0)
325 return 1;
326 else
327 return 0;
328 }
329
330 int
331 ofb_ioctl(v, cmd, data, flag, p)
332 void *v;
333 u_long cmd;
334 caddr_t data;
335 int flag;
336 struct proc *p;
337 {
338 struct ofb_softc *sc = v;
339 struct ofb_devconfig *dc = sc->sc_dc;
340 struct wsdisplay_fbinfo *wdf;
341 struct grfinfo *gm;
342
343 switch (cmd) {
344 case WSDISPLAYIO_GTYPE:
345 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */
346 return 0;
347
348 case WSDISPLAYIO_GINFO:
349 wdf = (void *)data;
350 wdf->height = dc->dc_ri.ri_height;
351 wdf->width = dc->dc_ri.ri_width;
352 wdf->depth = dc->dc_ri.ri_depth;
353 wdf->cmsize = 256;
354 return 0;
355
356 case WSDISPLAYIO_GETCMAP:
357 return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
358
359 case WSDISPLAYIO_PUTCMAP:
360 return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
361
362 /* XXX There are no way to know framebuffer pa from a user program. */
363 case GRFIOCGINFO:
364 gm = (void *)data;
365 memset(gm, 0, sizeof(struct grfinfo));
366 gm->gd_fbaddr = (caddr_t)dc->dc_paddr;
367 gm->gd_fbrowbytes = dc->dc_ri.ri_stride;
368 return 0;
369 /* PCI config read/write passthrough. */
370 case PCI_IOC_CFGREAD:
371 case PCI_IOC_CFGWRITE:
372 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
373 cmd, data, flag, p));
374 }
375 return EPASSTHROUGH;
376 }
377
378 paddr_t
379 ofb_mmap(v, offset, prot)
380 void *v;
381 off_t offset;
382 int prot;
383 {
384 struct ofb_softc *sc = v;
385 struct ofb_devconfig *dc = sc->sc_dc;
386 struct rasops_info *ri = &dc->dc_ri;
387 u_int32_t *ap = sc->sc_addrs;
388 paddr_t pa;
389 int i;
390
391 if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
392 return dc->dc_paddr + offset;
393
394 pa = offset;
395 for (i = 0; i < 6; i++) {
396 switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
397 case OFW_PCI_PHYS_HI_SPACE_MEM32:
398 if (pa >= ap[2] && pa < ap[2] + ap[4])
399 return pa;
400 /* XXX I/O space? */
401 }
402 ap += 5;
403 }
404
405 return -1;
406 }
407
408 int
409 ofb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
410 void *v;
411 const struct wsscreen_descr *type;
412 void **cookiep;
413 int *curxp, *curyp;
414 long *attrp;
415 {
416 struct ofb_softc *sc = v;
417 struct rasops_info *ri = &sc->sc_dc->dc_ri;
418 long defattr;
419
420 if (sc->nscreens > 0)
421 return (ENOMEM);
422
423 *cookiep = ri; /* one and only for now */
424 *curxp = 0;
425 *curyp = 0;
426 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
427 *attrp = defattr;
428 sc->nscreens++;
429 return 0;
430 }
431
432 void
433 ofb_free_screen(v, cookie)
434 void *v;
435 void *cookie;
436 {
437 struct ofb_softc *sc = v;
438
439 if (sc->sc_dc == &ofb_console_dc)
440 panic("ofb_free_screen: console");
441
442 sc->nscreens--;
443 }
444
445 int
446 ofb_show_screen(v, cookie, waitok, cb, cbarg)
447 void *v;
448 void *cookie;
449 int waitok;
450 void (*cb) __P((void *, int, int));
451 void *cbarg;
452 {
453
454 return (0);
455 }
456
457 int
458 ofb_cnattach()
459 {
460 struct ofb_devconfig *dc = &ofb_console_dc;
461 struct rasops_info *ri = &dc->dc_ri;
462 long defattr;
463 int crow = 0;
464 int chosen, stdout, node;
465
466 chosen = OF_finddevice("/chosen");
467 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
468 node = OF_instance_to_package(stdout);
469 dc->dc_ih = stdout;
470
471 /* get current cursor position */
472 OF_interpret("line#", 1, &crow);
473
474 /* move (rom monitor) cursor to the lowest line - 1 */
475 OF_interpret("#lines 2 - to line#", 0);
476
477 ofb_common_init(node, dc);
478
479 if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
480 int i, screenbytes = ri->ri_stride * ri->ri_height;
481
482 for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
483 *(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
484 crow = 0;
485 }
486
487 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
488 wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
489
490 return 0;
491 }
492
493 int
494 copy_rom_font()
495 {
496 u_char *romfont;
497 int char_width, char_height;
498 int chosen, mmu, m, e;
499
500 /* Get ROM FONT address. */
501 OF_interpret("font-adr", 1, &romfont);
502 if (romfont == NULL)
503 return -1;
504
505 chosen = OF_finddevice("/chosen");
506 OF_getprop(chosen, "mmu", &mmu, 4);
507
508 /*
509 * Convert to physcal address. We cannot access to Open Firmware's
510 * virtual address space.
511 */
512 OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
513
514 /* Get character size */
515 OF_interpret("char-width", 1, &char_width);
516 OF_interpret("char-height", 1, &char_height);
517
518 openfirm6x11.name = "Open Firmware";
519 openfirm6x11.firstchar = 32;
520 openfirm6x11.numchars = 96;
521 openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
522 openfirm6x11.fontwidth = char_width;
523 openfirm6x11.fontheight = char_height;
524 openfirm6x11.stride = 1;
525 openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
526 openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
527 openfirm6x11.data = romfont;
528
529 return 0;
530 }
531
532 int
533 ofb_getcmap(sc, cm)
534 struct ofb_softc *sc;
535 struct wsdisplay_cmap *cm;
536 {
537 u_int index = cm->index;
538 u_int count = cm->count;
539 int error;
540
541 if (index >= 256 || count > 256 || index + count > 256)
542 return EINVAL;
543
544 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
545 if (error)
546 return error;
547 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
548 if (error)
549 return error;
550 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
551 if (error)
552 return error;
553
554 return 0;
555 }
556
557 int
558 ofb_putcmap(sc, cm)
559 struct ofb_softc *sc;
560 struct wsdisplay_cmap *cm;
561 {
562 struct ofb_devconfig *dc = sc->sc_dc;
563 u_int index = cm->index;
564 u_int count = cm->count;
565 int i;
566 u_char *r, *g, *b;
567
568 if (cm->index >= 256 || cm->count > 256 ||
569 (cm->index + cm->count) > 256)
570 return EINVAL;
571 if (!uvm_useracc(cm->red, cm->count, B_READ) ||
572 !uvm_useracc(cm->green, cm->count, B_READ) ||
573 !uvm_useracc(cm->blue, cm->count, B_READ))
574 return EFAULT;
575 copyin(cm->red, &sc->sc_cmap_red[index], count);
576 copyin(cm->green, &sc->sc_cmap_green[index], count);
577 copyin(cm->blue, &sc->sc_cmap_blue[index], count);
578
579 r = &sc->sc_cmap_red[index];
580 g = &sc->sc_cmap_green[index];
581 b = &sc->sc_cmap_blue[index];
582
583 for (i = 0; i < count; i++) {
584 OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
585 r++, g++, b++, index++;
586 }
587
588 return 0;
589 }
590