ofb.c revision 1.4 1 /* $NetBSD: ofb.c,v 1.4 1998/11/19 15:38:23 mrg 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 <dev/pci/pcidevs.h>
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
42
43 #include <dev/rcons/raster.h>
44 #include <dev/wscons/wsconsio.h>
45 #include <dev/wscons/wscons_raster.h>
46 #include <dev/wscons/wsdisplayvar.h>
47
48 #include <machine/bat.h>
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 wsdisplay_emulops ofb_emulops = {
65 rcons_cursor, /* could use hardware cursor; punt */
66 rcons_mapchar,
67 rcons_putchar,
68 rcons_copycols,
69 rcons_erasecols,
70 rcons_copyrows,
71 rcons_eraserows,
72 rcons_alloc_attr
73 };
74
75 struct wsscreen_descr ofb_stdscreen = {
76 "std",
77 0, 0, /* will be filled in -- XXX shouldn't, it's global */
78 &ofb_emulops,
79 0, 0,
80 WSSCREEN_REVERSE
81 };
82
83 const struct wsscreen_descr *_ofb_scrlist[] = {
84 &ofb_stdscreen,
85 /* XXX other formats, graphics screen? */
86 };
87
88 struct wsscreen_list ofb_screenlist = {
89 sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
90 };
91
92 static int ofb_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
93 static int ofb_mmap __P((void *, off_t, int));
94 static int ofb_alloc_screen __P((void *, const struct wsscreen_descr *,
95 void **, int *, int *, long *));
96 static void ofb_free_screen __P((void *, void *));
97 static void ofb_show_screen __P((void *, void *));
98 static int ofb_load_font __P((void *, void *, int, int, int, void *));
99
100 struct wsdisplay_accessops ofb_accessops = {
101 ofb_ioctl,
102 ofb_mmap,
103 ofb_alloc_screen,
104 ofb_free_screen,
105 ofb_show_screen,
106 ofb_load_font
107 };
108
109 static void ofb_common_init __P((int, struct ofb_devconfig *));
110
111 int
112 ofbmatch(parent, match, aux)
113 struct device *parent;
114 struct cfdata *match;
115 void *aux;
116 {
117 struct pci_attach_args *pa = aux;
118
119 /* /chaos/control */
120 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
121 PCI_PRODUCT(pa->pa_id) == 3)
122 return 1;
123
124 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
125 return 1;
126
127 return 0;
128 }
129
130 void
131 ofbattach(parent, self, aux)
132 struct device *parent, *self;
133 void *aux;
134 {
135 struct ofb_softc *sc = (struct ofb_softc *)self;
136 struct pci_attach_args *pa = aux;
137 struct wsemuldisplaydev_attach_args a;
138 int console;
139 struct ofb_devconfig *dc;
140
141 console = ofb_is_console();
142
143 if (console) {
144 dc = &ofb_console_dc;
145 sc->nscreens = 1;
146 } else {
147 int node;
148
149 dc = malloc(sizeof(struct ofb_devconfig), M_DEVBUF, M_WAITOK);
150 bzero(dc, sizeof(struct ofb_devconfig));
151 node = pcidev_to_ofdev(pa);
152 if (node == 0) {
153 printf(": ofdev not found\n");
154 return;
155 }
156 ofb_common_init(node, dc);
157 }
158 sc->sc_dc = dc;
159
160 if (dc->dc_paddr == 0) {
161 printf(": cannot map framebuffer\n");
162 return;
163 }
164 dc->dc_raster.pixels = mapiodev(dc->dc_paddr,
165 dc->dc_linebytes * dc->dc_height);
166
167 printf(": %d x %d, %dbpp\n",
168 dc->dc_raster.width, dc->dc_raster.height, dc->dc_raster.depth);
169
170 a.console = console;
171 a.scrdata = &ofb_screenlist;
172 a.accessops = &ofb_accessops;
173 a.accesscookie = sc;
174
175 #ifdef DEBUG
176 asm volatile ("mtdbatl 3,%0; mtdbatu 3,%1" :: "r"(0), "r"(0));
177 #endif
178 config_found(self, &a, wsemuldisplaydevprint);
179 }
180
181 void
182 ofb_common_init(node, dc)
183 int node;
184 struct ofb_devconfig *dc;
185 {
186 struct raster *rap;
187 struct rcons *rcp;
188 int i;
189 int addr, width, height, linebytes, depth;
190 u_int regs[5];
191
192 OF_getprop(node, "assigned-addresses", regs, sizeof(regs));
193 addr = regs[2] & 0xf0000000;
194
195 /* Map the framebuffer using BAT3. (va == pa) */
196 asm volatile ("mtdbatl 3,%0; mtdbatu 3,%1; isync"
197 :: "r"(BATL(addr, BAT_I)), "r"(BATU(addr)));
198
199 if (dc->dc_ih == 0) {
200 char name[64];
201
202 bzero(name, 64);
203 OF_package_to_path(node, name, sizeof(name));
204 dc->dc_ih = OF_open(name);
205 }
206
207 /*
208 * /chaos/control don't have "width", "height", ...
209 */
210 width = height = -1;
211 if (OF_getprop(node, "width", &width, sizeof(width)) != 4)
212 OF_interpret("screen-width", 1, &width);
213 if (OF_getprop(node, "height", &height, sizeof(height)) != 4)
214 OF_interpret("screen-height", 1, &height);
215 if (OF_getprop(node, "linebytes", &linebytes, sizeof(linebytes)) != 4)
216 linebytes = width; /* XXX */
217 if (OF_getprop(node, "depth", &depth, sizeof(depth)) != 4)
218 depth = 8; /* XXX */
219 if (width == -1 || height == -1)
220 return;
221
222 OF_interpret("frame-buffer-adr", 1, &addr);
223 if (addr == 0)
224 return;
225 dc->dc_paddr = addr; /* PA of the frame buffer */
226
227 /* Set colormap to black on white. */
228 OF_call_method_1("color!", dc->dc_ih, 4, 0, 0, 0, 0xff);
229 OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 0);
230 for (i = 0; i < height * linebytes; i += sizeof(u_int32_t))
231 *(u_int32_t *)(addr + i) = 0;
232
233 /* initialize the raster */
234 rap = &dc->dc_raster;
235 rap->width = width;
236 rap->height = height;
237 rap->depth = depth;
238 rap->linelongs = linebytes / sizeof(u_int32_t);
239 rap->pixels = (u_int32_t *)addr;
240
241 /* initialize the raster console blitter */
242 rcp = &dc->dc_rcons;
243 rcp->rc_sp = rap;
244 rcp->rc_crow = rcp->rc_ccol = -1;
245 rcp->rc_crowp = &rcp->rc_crow;
246 rcp->rc_ccolp = &rcp->rc_ccol;
247 rcons_init(rcp, 128, 128);
248
249 ofb_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
250 ofb_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
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 = sc->sc_dc->dc_raster.height;
290 wdf->width = sc->sc_dc->dc_raster.width;
291 wdf->depth = sc->sc_dc->dc_raster.depth;
292 wdf->cmsize = 256;
293 return 0;
294
295 case WSDISPLAYIO_PUTCMAP:
296 return putcmap(sc, data);
297
298 /* XXX There are no way to know framebuffer pa from a user program. */
299 case GRFIOCGINFO:
300 gm = (void *)data;
301 bzero(gm, sizeof(struct grfinfo));
302 gm->gd_fbaddr = (caddr_t)sc->sc_dc->dc_paddr;
303 gm->gd_fbrowbytes = sc->sc_dc->dc_linebytes;
304 return 0;
305 }
306 return -1;
307 }
308
309 int
310 ofb_mmap(v, offset, prot)
311 void *v;
312 off_t offset;
313 int prot;
314 {
315 struct ofb_softc *sc = v;
316 struct ofb_devconfig *dc = sc->sc_dc;
317
318 if (offset >= (dc->dc_linebytes * dc->dc_height) || offset < 0)
319 return -1;
320
321 return dc->dc_paddr + offset;
322 }
323
324 int
325 ofb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
326 void *v;
327 const struct wsscreen_descr *type;
328 void **cookiep;
329 int *curxp, *curyp;
330 long *attrp;
331 {
332 struct ofb_softc *sc = v;
333 long defattr;
334
335 if (sc->nscreens > 0)
336 return (ENOMEM);
337
338 *cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
339 *curxp = 0;
340 *curyp = 0;
341 rcons_alloc_attr(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
342 *attrp = defattr;
343 sc->nscreens++;
344 return 0;
345 }
346
347 void
348 ofb_free_screen(v, cookie)
349 void *v;
350 void *cookie;
351 {
352 struct ofb_softc *sc = v;
353
354 if (sc->sc_dc == &ofb_console_dc)
355 panic("ofb_free_screen: console");
356
357 sc->nscreens--;
358 }
359
360 void
361 ofb_show_screen(v, cookie)
362 void *v;
363 void *cookie;
364 {
365 }
366
367 static int
368 ofb_load_font(v, cookie, first, num, stride, data)
369 void *v;
370 void *cookie;
371 int first, num, stride;
372 void *data;
373 {
374 return EINVAL;
375 }
376
377 int
378 ofb_cnattach()
379 {
380 struct ofb_devconfig *dc = &ofb_console_dc;
381 long defattr;
382 int crow;
383 int chosen, stdout, node;
384 char cmd[32];
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 ofb_common_init(node, dc);
392 #if 0
393 /* get current cursor position */
394 OF_interpret("line#", 1, &crow);
395
396 /* move (rom monitor) cursor to the lowest line */
397 sprintf(cmd, "%x to line#", ofb_stdscreen.nrows - 1);
398 OF_interpret(cmd, 0);
399 #endif
400 rcons_alloc_attr(&dc->dc_rcons, 0, 0, 0, &defattr);
401 wsdisplay_cnattach(&ofb_stdscreen, &dc->dc_rcons, 0, 0, defattr);
402
403 return 0;
404 }
405
406 int
407 putcmap(sc, cm)
408 struct ofb_softc *sc;
409 struct wsdisplay_cmap *cm;
410 {
411 struct ofb_devconfig *dc = sc->sc_dc;
412 int index = cm->index;
413 int count = cm->count;
414 int i, s;
415 u_char *r, *g, *b;
416
417 if (cm->index >= 256 || cm->count > 256 ||
418 (cm->index + cm->count) > 256)
419 return EINVAL;
420 #if defined(UVM)
421 if (!uvm_useracc(cm->red, cm->count, B_READ) ||
422 !uvm_useracc(cm->green, cm->count, B_READ) ||
423 !uvm_useracc(cm->blue, cm->count, B_READ))
424 return EFAULT;
425 #else
426 if (!useracc(cm->red, cm->count, B_READ) ||
427 !useracc(cm->green, cm->count, B_READ) ||
428 !useracc(cm->blue, cm->count, B_READ))
429 return EFAULT;
430 #endif
431 copyin(cm->red, &sc->sc_cmap_red[index], count);
432 copyin(cm->green, &sc->sc_cmap_green[index], count);
433 copyin(cm->blue, &sc->sc_cmap_blue[index], count);
434
435 r = &sc->sc_cmap_red[index];
436 g = &sc->sc_cmap_green[index];
437 b = &sc->sc_cmap_blue[index];
438
439 s = splhigh();
440 asm volatile ("mtdbatl 3,%0; mtdbatu 3,%1"
441 :: "r"(BATL(dc->dc_paddr, BAT_I)), "r"(BATU(dc->dc_paddr)));
442
443 for (i = 0; i < count; i++) {
444 OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
445 r++, g++, b++, index++;
446 }
447 splx(s);
448
449 return 0;
450 }
451