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