ofb.c revision 1.12 1 /* $NetBSD: ofb.c,v 1.12 2000/04/02 12:53:05 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 /* Make sure 0/0/0 is black and 255/255/255 is white. */
215 OF_call_method_1("color!", dc->dc_ih, 4, 0, 0, 0, 0);
216 OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 255);
217
218 /* initialize rasops */
219 ri->ri_width = width;
220 ri->ri_height = height;
221 ri->ri_depth = depth;
222 ri->ri_stride = linebytes;
223 ri->ri_bits = (char *)addr;
224 ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR | RI_CENTER;
225
226 /* If screen is smaller than 1024x768, use small font. */
227 if ((width < 1024 || height < 768) && copy_rom_font() == 0) {
228 int cols, rows;
229
230 OF_interpret("#lines", 1, &rows);
231 OF_interpret("#columns", 1, &cols);
232
233 ri->ri_font = &openfirm6x11;
234 ri->ri_wsfcookie = -1; /* not using wsfont */
235 rasops_init(ri, rows, cols);
236
237 ri->ri_xorigin = 2;
238 ri->ri_yorigin = 3;
239 ri->ri_bits = (char *)addr + ri->ri_xorigin +
240 ri->ri_stride * ri->ri_yorigin;
241 } else
242 rasops_init(ri, height/22, (width/12) & ~7); /* XXX 12x22 */
243
244 /* black on white */
245 ri->ri_devcmap[0] = 0xffffffff; /* bg */
246 ri->ri_devcmap[1] = 0; /* fg */
247
248 ofb_stdscreen.nrows = ri->ri_rows;
249 ofb_stdscreen.ncols = ri->ri_cols;
250 ofb_stdscreen.textops = &ri->ri_ops;
251 ofb_stdscreen.capabilities = ri->ri_caps;
252 }
253
254 int
255 ofb_is_console()
256 {
257 int chosen, stdout, node;
258 char type[16];
259
260 chosen = OF_finddevice("/chosen");
261 OF_getprop(chosen, "stdout", &stdout, 4);
262 node = OF_instance_to_package(stdout);
263 OF_getprop(node, "device_type", type, sizeof(type));
264 if (strcmp(type, "display") == 0)
265 return 1;
266 else
267 return 0;
268 }
269
270 int
271 ofb_ioctl(v, cmd, data, flag, p)
272 void *v;
273 u_long cmd;
274 caddr_t data;
275 int flag;
276 struct proc *p;
277 {
278 struct ofb_softc *sc = v;
279 struct ofb_devconfig *dc = sc->sc_dc;
280 struct wsdisplay_fbinfo *wdf;
281 struct grfinfo *gm;
282
283 switch (cmd) {
284 case WSDISPLAYIO_GTYPE:
285 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */
286 return 0;
287
288 case WSDISPLAYIO_GINFO:
289 wdf = (void *)data;
290 wdf->height = dc->dc_ri.ri_height;
291 wdf->width = dc->dc_ri.ri_width;
292 wdf->depth = dc->dc_ri.ri_depth;
293 wdf->cmsize = 256;
294 return 0;
295
296 case WSDISPLAYIO_GETCMAP:
297 return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
298
299 case WSDISPLAYIO_PUTCMAP:
300 return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
301
302 /* XXX There are no way to know framebuffer pa from a user program. */
303 case GRFIOCGINFO:
304 gm = (void *)data;
305 bzero(gm, sizeof(struct grfinfo));
306 gm->gd_fbaddr = (caddr_t)dc->dc_paddr;
307 gm->gd_fbrowbytes = dc->dc_ri.ri_stride;
308 return 0;
309 }
310 return -1;
311 }
312
313 int
314 ofb_mmap(v, offset, prot)
315 void *v;
316 off_t offset;
317 int prot;
318 {
319 struct ofb_softc *sc = v;
320 struct ofb_devconfig *dc = sc->sc_dc;
321 struct rasops_info *ri = &dc->dc_ri;
322
323 if (offset >= (ri->ri_stride * ri->ri_height) || offset < 0)
324 return -1;
325
326 return dc->dc_paddr + offset;
327 }
328
329 int
330 ofb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
331 void *v;
332 const struct wsscreen_descr *type;
333 void **cookiep;
334 int *curxp, *curyp;
335 long *attrp;
336 {
337 struct ofb_softc *sc = v;
338 struct rasops_info *ri = &sc->sc_dc->dc_ri;
339 long defattr;
340
341 if (sc->nscreens > 0)
342 return (ENOMEM);
343
344 *cookiep = ri; /* one and only for now */
345 *curxp = 0;
346 *curyp = 0;
347 (*ri->ri_ops.alloc_attr)(ri, 0, 0, 0, &defattr);
348 *attrp = defattr;
349 sc->nscreens++;
350 return 0;
351 }
352
353 void
354 ofb_free_screen(v, cookie)
355 void *v;
356 void *cookie;
357 {
358 struct ofb_softc *sc = v;
359
360 if (sc->sc_dc == &ofb_console_dc)
361 panic("ofb_free_screen: console");
362
363 sc->nscreens--;
364 }
365
366 int
367 ofb_show_screen(v, cookie, waitok, cb, cbarg)
368 void *v;
369 void *cookie;
370 int waitok;
371 void (*cb) __P((void *, int, int));
372 void *cbarg;
373 {
374
375 return (0);
376 }
377
378 int
379 ofb_cnattach()
380 {
381 struct ofb_devconfig *dc = &ofb_console_dc;
382 struct rasops_info *ri = &dc->dc_ri;
383 long defattr;
384 int crow = 0;
385 int chosen, stdout, node;
386
387 chosen = OF_finddevice("/chosen");
388 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
389 node = OF_instance_to_package(stdout);
390 dc->dc_ih = stdout;
391
392 /* get current cursor position */
393 OF_interpret("line#", 1, &crow);
394
395 /* move (rom monitor) cursor to the lowest line - 1 */
396 OF_interpret("#lines 2 - to line#", 0);
397
398 ofb_common_init(node, dc);
399
400 if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
401 int i, screenbytes = ri->ri_stride * ri->ri_height;
402
403 for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
404 *(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
405 crow = 0;
406 }
407
408 (*ri->ri_ops.alloc_attr)(ri, 0, 0, 0, &defattr);
409 wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
410
411 return 0;
412 }
413
414 int
415 copy_rom_font()
416 {
417 u_char *romfont;
418 int char_width, char_height;
419 int chosen, mmu, m, e;
420
421 /* Get ROM FONT address. */
422 OF_interpret("font-adr", 1, &romfont);
423 if (romfont == NULL)
424 return -1;
425
426 chosen = OF_finddevice("/chosen");
427 OF_getprop(chosen, "mmu", &mmu, 4);
428
429 /*
430 * Convert to physcal address. We cannot access to Open Firmware's
431 * virtual address space.
432 */
433 OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
434
435 /* Get character size */
436 OF_interpret("char-width", 1, &char_width);
437 OF_interpret("char-height", 1, &char_height);
438
439 openfirm6x11.name = "Open Firmware";
440 openfirm6x11.firstchar = 32;
441 openfirm6x11.numchars = 96;
442 openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
443 openfirm6x11.fontwidth = char_width;
444 openfirm6x11.fontheight = char_height;
445 openfirm6x11.stride = 1;
446 openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
447 openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
448 openfirm6x11.data = romfont;
449
450 return 0;
451 }
452
453 int
454 ofb_getcmap(sc, cm)
455 struct ofb_softc *sc;
456 struct wsdisplay_cmap *cm;
457 {
458 u_int index = cm->index;
459 u_int count = cm->count;
460 int error;
461
462 if (index >= 256 || count > 256 || index + count > 256)
463 return EINVAL;
464
465 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
466 if (error)
467 return error;
468 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
469 if (error)
470 return error;
471 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
472 if (error)
473 return error;
474
475 return 0;
476 }
477
478 int
479 ofb_putcmap(sc, cm)
480 struct ofb_softc *sc;
481 struct wsdisplay_cmap *cm;
482 {
483 struct ofb_devconfig *dc = sc->sc_dc;
484 int index = cm->index;
485 int count = cm->count;
486 int i;
487 u_char *r, *g, *b;
488
489 if (cm->index >= 256 || cm->count > 256 ||
490 (cm->index + cm->count) > 256)
491 return EINVAL;
492 if (!uvm_useracc(cm->red, cm->count, B_READ) ||
493 !uvm_useracc(cm->green, cm->count, B_READ) ||
494 !uvm_useracc(cm->blue, cm->count, B_READ))
495 return EFAULT;
496 copyin(cm->red, &sc->sc_cmap_red[index], count);
497 copyin(cm->green, &sc->sc_cmap_green[index], count);
498 copyin(cm->blue, &sc->sc_cmap_blue[index], count);
499
500 r = &sc->sc_cmap_red[index];
501 g = &sc->sc_cmap_green[index];
502 b = &sc->sc_cmap_blue[index];
503
504 for (i = 0; i < count; i++) {
505 OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
506 r++, g++, b++, index++;
507 }
508
509 return 0;
510 }
511