ofb.c revision 1.41 1 /* $NetBSD: ofb.c,v 1.41 2004/12/17 05:44:12 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.41 2004/12/17 05:44:12 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 dc->dc_ih = OF_open(name);
239 }
240
241 /* XXX /chaos/control doesn't have "width", "height", ... */
242 width = height = -1;
243 if (OF_getprop(node, "width", &width, 4) != 4)
244 OF_interpret("screen-width", 1, &width);
245 if (OF_getprop(node, "height", &height, 4) != 4)
246 OF_interpret("screen-height", 1, &height);
247 if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
248 linebytes = width; /* XXX */
249 if (OF_getprop(node, "depth", &depth, 4) != 4)
250 depth = 8; /* XXX */
251 if (OF_getprop(node, "address", &addr, 4) != 4)
252 OF_interpret("frame-buffer-adr", 1, &addr);
253
254 if (width == -1 || height == -1 || addr == 0 || addr == -1)
255 return;
256
257 dc->dc_paddr = addr; /* PA of the frame buffer */
258
259 /* Make sure 0/0/0 is black and 255/255/255 is white. */
260 OF_call_method_1("color!", dc->dc_ih, 4, 0, 0, 0, 0);
261 OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 255);
262
263 /* Enable write-through cache. */
264 if (ofb_enable_cache) {
265 vaddr_t va;
266 /*
267 * Let's try to find an empty BAT to use
268 */
269 for (va = SEGMENT_LENGTH; va < (USER_SR << ADDR_SR_SHFT);
270 va += SEGMENT_LENGTH) {
271 if (battable[va >> ADDR_SR_SHFT].batu == 0) {
272 battable[va >> ADDR_SR_SHFT].batl =
273 BATL(addr & 0xf0000000,
274 BAT_G | BAT_W | BAT_M, BAT_PP_RW);
275 battable[va >> ADDR_SR_SHFT].batu =
276 BATL(va, BAT_BL_256M, BAT_Vs);
277 addr &= 0x0fffffff;
278 addr |= va;
279 break;
280 }
281 }
282 }
283
284 /* initialize rasops */
285 ri->ri_width = width;
286 ri->ri_height = height;
287 ri->ri_depth = depth;
288 ri->ri_stride = linebytes;
289 ri->ri_bits = (char *)addr;
290 ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR | RI_CENTER;
291
292 /* If screen is smaller than 1024x768, use small font. */
293 if ((width < 1024 || height < 768) && copy_rom_font() == 0) {
294 int cols, rows;
295
296 OF_interpret("#lines", 1, &rows);
297 OF_interpret("#columns", 1, &cols);
298
299 ri->ri_font = &openfirm6x11;
300 ri->ri_wsfcookie = -1; /* not using wsfont */
301 rasops_init(ri, rows, cols);
302
303 ri->ri_xorigin = 2;
304 ri->ri_yorigin = 3;
305 ri->ri_bits = (char *)addr + ri->ri_xorigin +
306 ri->ri_stride * ri->ri_yorigin;
307 } else {
308 rasops_init(ri, 24, 80);
309 rasops_reconfig(ri, (height - 2) / ri->ri_font->fontheight,
310 ((width - 2) / ri->ri_font->fontwidth) & ~7);
311 }
312
313 /* black on white */
314 ri->ri_devcmap[0] = 0xffffffff; /* bg */
315 ri->ri_devcmap[1] = 0; /* fg */
316
317 ofb_stdscreen.nrows = ri->ri_rows;
318 ofb_stdscreen.ncols = ri->ri_cols;
319 ofb_stdscreen.textops = &ri->ri_ops;
320 ofb_stdscreen.capabilities = ri->ri_caps;
321 }
322
323 int
324 ofb_is_console()
325 {
326 int chosen, stdout, node;
327 char type[16];
328
329 chosen = OF_finddevice("/chosen");
330 OF_getprop(chosen, "stdout", &stdout, 4);
331 node = OF_instance_to_package(stdout);
332 OF_getprop(node, "device_type", type, sizeof(type));
333 if (strcmp(type, "display") == 0)
334 return 1;
335 else
336 return 0;
337 }
338
339 int
340 ofb_ioctl(v, cmd, data, flag, p)
341 void *v;
342 u_long cmd;
343 caddr_t data;
344 int flag;
345 struct proc *p;
346 {
347 struct ofb_softc *sc = v;
348 struct ofb_devconfig *dc = sc->sc_dc;
349 struct wsdisplay_fbinfo *wdf;
350 struct grfinfo *gm;
351
352 switch (cmd) {
353 case WSDISPLAYIO_GTYPE:
354 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */
355 return 0;
356
357 case WSDISPLAYIO_GINFO:
358 wdf = (void *)data;
359 wdf->height = dc->dc_ri.ri_height;
360 wdf->width = dc->dc_ri.ri_width;
361 wdf->depth = dc->dc_ri.ri_depth;
362 wdf->cmsize = 256;
363 return 0;
364
365 case WSDISPLAYIO_GETCMAP:
366 return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
367
368 case WSDISPLAYIO_PUTCMAP:
369 return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
370
371 /* XXX There are no way to know framebuffer pa from a user program. */
372 case GRFIOCGINFO:
373 gm = (void *)data;
374 memset(gm, 0, sizeof(struct grfinfo));
375 gm->gd_fbaddr = (caddr_t)dc->dc_paddr;
376 gm->gd_fbrowbytes = dc->dc_ri.ri_stride;
377 return 0;
378 /* PCI config read/write passthrough. */
379 case PCI_IOC_CFGREAD:
380 case PCI_IOC_CFGWRITE:
381 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
382 cmd, data, flag, p));
383 }
384 return EPASSTHROUGH;
385 }
386
387 paddr_t
388 ofb_mmap(v, offset, prot)
389 void *v;
390 off_t offset;
391 int prot;
392 {
393 struct ofb_softc *sc = v;
394 struct ofb_devconfig *dc = sc->sc_dc;
395 struct rasops_info *ri = &dc->dc_ri;
396 u_int32_t *ap = sc->sc_addrs;
397 paddr_t pa;
398 int i;
399
400 if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
401 return dc->dc_paddr + offset;
402
403 pa = offset;
404 for (i = 0; i < 6; i++) {
405 switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
406 case OFW_PCI_PHYS_HI_SPACE_MEM32:
407 if (pa >= ap[2] && pa < ap[2] + ap[4])
408 return pa;
409 /* XXX I/O space? */
410 }
411 ap += 5;
412 }
413
414 return -1;
415 }
416
417 int
418 ofb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
419 void *v;
420 const struct wsscreen_descr *type;
421 void **cookiep;
422 int *curxp, *curyp;
423 long *attrp;
424 {
425 struct ofb_softc *sc = v;
426 struct rasops_info *ri = &sc->sc_dc->dc_ri;
427 long defattr;
428
429 if (sc->nscreens > 0)
430 return (ENOMEM);
431
432 *cookiep = ri; /* one and only for now */
433 *curxp = 0;
434 *curyp = 0;
435 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
436 *attrp = defattr;
437 sc->nscreens++;
438 return 0;
439 }
440
441 void
442 ofb_free_screen(v, cookie)
443 void *v;
444 void *cookie;
445 {
446 struct ofb_softc *sc = v;
447
448 if (sc->sc_dc == &ofb_console_dc)
449 panic("ofb_free_screen: console");
450
451 sc->nscreens--;
452 }
453
454 int
455 ofb_show_screen(v, cookie, waitok, cb, cbarg)
456 void *v;
457 void *cookie;
458 int waitok;
459 void (*cb) __P((void *, int, int));
460 void *cbarg;
461 {
462
463 return (0);
464 }
465
466 int
467 ofb_cnattach()
468 {
469 struct ofb_devconfig *dc = &ofb_console_dc;
470 struct rasops_info *ri = &dc->dc_ri;
471 long defattr;
472 int crow = 0;
473 int chosen, stdout, node;
474
475 chosen = OF_finddevice("/chosen");
476 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
477 node = OF_instance_to_package(stdout);
478 dc->dc_ih = stdout;
479
480 /* get current cursor position */
481 OF_interpret("line#", 1, &crow);
482
483 /* move (rom monitor) cursor to the lowest line - 1 */
484 OF_interpret("#lines 2 - to line#", 0);
485
486 ofb_common_init(node, dc);
487
488 if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
489 int i, screenbytes = ri->ri_stride * ri->ri_height;
490
491 for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
492 *(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
493 crow = 0;
494 }
495
496 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
497 wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
498
499 return 0;
500 }
501
502 int
503 copy_rom_font()
504 {
505 u_char *romfont;
506 int char_width, char_height;
507 int chosen, mmu, m, e;
508
509 /* Get ROM FONT address. */
510 OF_interpret("font-adr", 1, &romfont);
511 if (romfont == NULL)
512 return -1;
513
514 chosen = OF_finddevice("/chosen");
515 OF_getprop(chosen, "mmu", &mmu, 4);
516
517 /*
518 * Convert to physcal address. We cannot access to Open Firmware's
519 * virtual address space.
520 */
521 OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
522
523 /* Get character size */
524 OF_interpret("char-width", 1, &char_width);
525 OF_interpret("char-height", 1, &char_height);
526
527 openfirm6x11.name = "Open Firmware";
528 openfirm6x11.firstchar = 32;
529 openfirm6x11.numchars = 96;
530 openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
531 openfirm6x11.fontwidth = char_width;
532 openfirm6x11.fontheight = char_height;
533 openfirm6x11.stride = 1;
534 openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
535 openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
536 openfirm6x11.data = romfont;
537
538 return 0;
539 }
540
541 int
542 ofb_getcmap(sc, cm)
543 struct ofb_softc *sc;
544 struct wsdisplay_cmap *cm;
545 {
546 u_int index = cm->index;
547 u_int count = cm->count;
548 int error;
549
550 if (index >= 256 || count > 256 || index + count > 256)
551 return EINVAL;
552
553 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
554 if (error)
555 return error;
556 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
557 if (error)
558 return error;
559 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
560 if (error)
561 return error;
562
563 return 0;
564 }
565
566 int
567 ofb_putcmap(sc, cm)
568 struct ofb_softc *sc;
569 struct wsdisplay_cmap *cm;
570 {
571 struct ofb_devconfig *dc = sc->sc_dc;
572 u_int index = cm->index;
573 u_int count = cm->count;
574 int i, error;
575 u_char rbuf[256], gbuf[256], bbuf[256];
576 u_char *r, *g, *b;
577
578 if (cm->index >= 256 || cm->count > 256 ||
579 (cm->index + cm->count) > 256)
580 return EINVAL;
581 error = copyin(cm->red, &rbuf[index], count);
582 if (error)
583 return error;
584 error = copyin(cm->green, &gbuf[index], count);
585 if (error)
586 return error;
587 error = copyin(cm->blue, &bbuf[index], count);
588 if (error)
589 return error;
590
591 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
592 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
593 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
594
595 r = &sc->sc_cmap_red[index];
596 g = &sc->sc_cmap_green[index];
597 b = &sc->sc_cmap_blue[index];
598 for (i = 0; i < count; i++) {
599 OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
600 r++, g++, b++, index++;
601 }
602 return 0;
603 }
604