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