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