ofb.c revision 1.4 1 1.4 mrg /* $NetBSD: ofb.c,v 1.4 1998/11/19 15:38:23 mrg Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*
4 1.1 tsubai * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 1.1 tsubai * All rights reserved.
6 1.1 tsubai *
7 1.1 tsubai * Author: Chris G. Demetriou
8 1.1 tsubai *
9 1.1 tsubai * Permission to use, copy, modify and distribute this software and
10 1.1 tsubai * its documentation is hereby granted, provided that both the copyright
11 1.1 tsubai * notice and this permission notice appear in all copies of the
12 1.1 tsubai * software, derivative works or modified versions, and any portions
13 1.1 tsubai * thereof, and that both notices appear in supporting documentation.
14 1.1 tsubai *
15 1.1 tsubai * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 1.1 tsubai * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 1.1 tsubai * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 1.1 tsubai *
19 1.1 tsubai * Carnegie Mellon requests users of this software to return to
20 1.1 tsubai *
21 1.1 tsubai * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 1.1 tsubai * School of Computer Science
23 1.1 tsubai * Carnegie Mellon University
24 1.1 tsubai * Pittsburgh PA 15213-3890
25 1.1 tsubai *
26 1.1 tsubai * any improvements or extensions that they make and grant Carnegie the
27 1.1 tsubai * rights to redistribute these changes.
28 1.1 tsubai */
29 1.1 tsubai
30 1.1 tsubai #include <sys/param.h>
31 1.1 tsubai #include <sys/buf.h>
32 1.1 tsubai #include <sys/conf.h>
33 1.1 tsubai #include <sys/device.h>
34 1.1 tsubai #include <sys/ioctl.h>
35 1.1 tsubai #include <sys/kernel.h>
36 1.1 tsubai #include <sys/malloc.h>
37 1.1 tsubai #include <sys/systm.h>
38 1.1 tsubai
39 1.3 tsubai #include <dev/pci/pcidevs.h>
40 1.1 tsubai #include <dev/pci/pcireg.h>
41 1.1 tsubai #include <dev/pci/pcivar.h>
42 1.1 tsubai
43 1.1 tsubai #include <dev/rcons/raster.h>
44 1.1 tsubai #include <dev/wscons/wsconsio.h>
45 1.1 tsubai #include <dev/wscons/wscons_raster.h>
46 1.1 tsubai #include <dev/wscons/wsdisplayvar.h>
47 1.1 tsubai
48 1.1 tsubai #include <machine/bat.h>
49 1.1 tsubai #include <machine/bus.h>
50 1.1 tsubai #include <machine/grfioctl.h>
51 1.1 tsubai
52 1.1 tsubai #include <macppc/dev/ofbvar.h>
53 1.1 tsubai
54 1.1 tsubai int ofbmatch __P((struct device *, struct cfdata *, void *));
55 1.1 tsubai void ofbattach __P((struct device *, struct device *, void *));
56 1.1 tsubai int ofbprint __P((void *, const char *));
57 1.1 tsubai
58 1.1 tsubai struct cfattach ofb_ca = {
59 1.1 tsubai sizeof(struct ofb_softc), ofbmatch, ofbattach,
60 1.1 tsubai };
61 1.1 tsubai
62 1.1 tsubai struct ofb_devconfig ofb_console_dc;
63 1.1 tsubai
64 1.1 tsubai struct wsdisplay_emulops ofb_emulops = {
65 1.1 tsubai rcons_cursor, /* could use hardware cursor; punt */
66 1.1 tsubai rcons_mapchar,
67 1.1 tsubai rcons_putchar,
68 1.1 tsubai rcons_copycols,
69 1.1 tsubai rcons_erasecols,
70 1.1 tsubai rcons_copyrows,
71 1.1 tsubai rcons_eraserows,
72 1.1 tsubai rcons_alloc_attr
73 1.1 tsubai };
74 1.1 tsubai
75 1.1 tsubai struct wsscreen_descr ofb_stdscreen = {
76 1.1 tsubai "std",
77 1.1 tsubai 0, 0, /* will be filled in -- XXX shouldn't, it's global */
78 1.1 tsubai &ofb_emulops,
79 1.1 tsubai 0, 0,
80 1.1 tsubai WSSCREEN_REVERSE
81 1.1 tsubai };
82 1.1 tsubai
83 1.1 tsubai const struct wsscreen_descr *_ofb_scrlist[] = {
84 1.1 tsubai &ofb_stdscreen,
85 1.1 tsubai /* XXX other formats, graphics screen? */
86 1.1 tsubai };
87 1.1 tsubai
88 1.1 tsubai struct wsscreen_list ofb_screenlist = {
89 1.1 tsubai sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
90 1.1 tsubai };
91 1.1 tsubai
92 1.1 tsubai static int ofb_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
93 1.1 tsubai static int ofb_mmap __P((void *, off_t, int));
94 1.1 tsubai static int ofb_alloc_screen __P((void *, const struct wsscreen_descr *,
95 1.1 tsubai void **, int *, int *, long *));
96 1.1 tsubai static void ofb_free_screen __P((void *, void *));
97 1.1 tsubai static void ofb_show_screen __P((void *, void *));
98 1.1 tsubai static int ofb_load_font __P((void *, void *, int, int, int, void *));
99 1.1 tsubai
100 1.1 tsubai struct wsdisplay_accessops ofb_accessops = {
101 1.1 tsubai ofb_ioctl,
102 1.1 tsubai ofb_mmap,
103 1.1 tsubai ofb_alloc_screen,
104 1.1 tsubai ofb_free_screen,
105 1.1 tsubai ofb_show_screen,
106 1.1 tsubai ofb_load_font
107 1.1 tsubai };
108 1.1 tsubai
109 1.1 tsubai static void ofb_common_init __P((int, struct ofb_devconfig *));
110 1.1 tsubai
111 1.1 tsubai int
112 1.1 tsubai ofbmatch(parent, match, aux)
113 1.1 tsubai struct device *parent;
114 1.1 tsubai struct cfdata *match;
115 1.1 tsubai void *aux;
116 1.1 tsubai {
117 1.1 tsubai struct pci_attach_args *pa = aux;
118 1.1 tsubai
119 1.3 tsubai /* /chaos/control */
120 1.3 tsubai if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
121 1.3 tsubai PCI_PRODUCT(pa->pa_id) == 3)
122 1.3 tsubai return 1;
123 1.1 tsubai
124 1.3 tsubai if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
125 1.3 tsubai return 1;
126 1.3 tsubai
127 1.3 tsubai return 0;
128 1.1 tsubai }
129 1.1 tsubai
130 1.1 tsubai void
131 1.1 tsubai ofbattach(parent, self, aux)
132 1.1 tsubai struct device *parent, *self;
133 1.1 tsubai void *aux;
134 1.1 tsubai {
135 1.1 tsubai struct ofb_softc *sc = (struct ofb_softc *)self;
136 1.1 tsubai struct pci_attach_args *pa = aux;
137 1.1 tsubai struct wsemuldisplaydev_attach_args a;
138 1.1 tsubai int console;
139 1.1 tsubai struct ofb_devconfig *dc;
140 1.1 tsubai
141 1.1 tsubai console = ofb_is_console();
142 1.1 tsubai
143 1.1 tsubai if (console) {
144 1.1 tsubai dc = &ofb_console_dc;
145 1.1 tsubai sc->nscreens = 1;
146 1.1 tsubai } else {
147 1.1 tsubai int node;
148 1.1 tsubai
149 1.1 tsubai dc = malloc(sizeof(struct ofb_devconfig), M_DEVBUF, M_WAITOK);
150 1.1 tsubai bzero(dc, sizeof(struct ofb_devconfig));
151 1.1 tsubai node = pcidev_to_ofdev(pa);
152 1.3 tsubai if (node == 0) {
153 1.3 tsubai printf(": ofdev not found\n");
154 1.3 tsubai return;
155 1.3 tsubai }
156 1.1 tsubai ofb_common_init(node, dc);
157 1.1 tsubai }
158 1.1 tsubai sc->sc_dc = dc;
159 1.1 tsubai
160 1.1 tsubai if (dc->dc_paddr == 0) {
161 1.1 tsubai printf(": cannot map framebuffer\n");
162 1.1 tsubai return;
163 1.1 tsubai }
164 1.1 tsubai dc->dc_raster.pixels = mapiodev(dc->dc_paddr,
165 1.1 tsubai dc->dc_linebytes * dc->dc_height);
166 1.1 tsubai
167 1.2 tsubai printf(": %d x %d, %dbpp\n",
168 1.1 tsubai dc->dc_raster.width, dc->dc_raster.height, dc->dc_raster.depth);
169 1.1 tsubai
170 1.1 tsubai a.console = console;
171 1.1 tsubai a.scrdata = &ofb_screenlist;
172 1.1 tsubai a.accessops = &ofb_accessops;
173 1.1 tsubai a.accesscookie = sc;
174 1.1 tsubai
175 1.1 tsubai #ifdef DEBUG
176 1.1 tsubai asm volatile ("mtdbatl 3,%0; mtdbatu 3,%1" :: "r"(0), "r"(0));
177 1.1 tsubai #endif
178 1.1 tsubai config_found(self, &a, wsemuldisplaydevprint);
179 1.1 tsubai }
180 1.1 tsubai
181 1.1 tsubai void
182 1.1 tsubai ofb_common_init(node, dc)
183 1.1 tsubai int node;
184 1.1 tsubai struct ofb_devconfig *dc;
185 1.1 tsubai {
186 1.1 tsubai struct raster *rap;
187 1.1 tsubai struct rcons *rcp;
188 1.1 tsubai int i;
189 1.1 tsubai int addr, width, height, linebytes, depth;
190 1.1 tsubai u_int regs[5];
191 1.1 tsubai
192 1.1 tsubai OF_getprop(node, "assigned-addresses", regs, sizeof(regs));
193 1.1 tsubai addr = regs[2] & 0xf0000000;
194 1.1 tsubai
195 1.1 tsubai /* Map the framebuffer using BAT3. (va == pa) */
196 1.1 tsubai asm volatile ("mtdbatl 3,%0; mtdbatu 3,%1; isync"
197 1.1 tsubai :: "r"(BATL(addr, BAT_I)), "r"(BATU(addr)));
198 1.1 tsubai
199 1.1 tsubai if (dc->dc_ih == 0) {
200 1.1 tsubai char name[64];
201 1.1 tsubai
202 1.1 tsubai bzero(name, 64);
203 1.1 tsubai OF_package_to_path(node, name, sizeof(name));
204 1.1 tsubai dc->dc_ih = OF_open(name);
205 1.1 tsubai }
206 1.3 tsubai
207 1.3 tsubai /*
208 1.3 tsubai * /chaos/control don't have "width", "height", ...
209 1.3 tsubai */
210 1.3 tsubai width = height = -1;
211 1.3 tsubai if (OF_getprop(node, "width", &width, sizeof(width)) != 4)
212 1.3 tsubai OF_interpret("screen-width", 1, &width);
213 1.3 tsubai if (OF_getprop(node, "height", &height, sizeof(height)) != 4)
214 1.3 tsubai OF_interpret("screen-height", 1, &height);
215 1.3 tsubai if (OF_getprop(node, "linebytes", &linebytes, sizeof(linebytes)) != 4)
216 1.3 tsubai linebytes = width; /* XXX */
217 1.3 tsubai if (OF_getprop(node, "depth", &depth, sizeof(depth)) != 4)
218 1.3 tsubai depth = 8; /* XXX */
219 1.3 tsubai if (width == -1 || height == -1)
220 1.3 tsubai return;
221 1.1 tsubai
222 1.1 tsubai OF_interpret("frame-buffer-adr", 1, &addr);
223 1.1 tsubai if (addr == 0)
224 1.1 tsubai return;
225 1.1 tsubai dc->dc_paddr = addr; /* PA of the frame buffer */
226 1.1 tsubai
227 1.1 tsubai /* Set colormap to black on white. */
228 1.1 tsubai OF_call_method_1("color!", dc->dc_ih, 4, 0, 0, 0, 0xff);
229 1.1 tsubai OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 0);
230 1.1 tsubai for (i = 0; i < height * linebytes; i += sizeof(u_int32_t))
231 1.1 tsubai *(u_int32_t *)(addr + i) = 0;
232 1.1 tsubai
233 1.1 tsubai /* initialize the raster */
234 1.1 tsubai rap = &dc->dc_raster;
235 1.1 tsubai rap->width = width;
236 1.1 tsubai rap->height = height;
237 1.1 tsubai rap->depth = depth;
238 1.1 tsubai rap->linelongs = linebytes / sizeof(u_int32_t);
239 1.1 tsubai rap->pixels = (u_int32_t *)addr;
240 1.1 tsubai
241 1.1 tsubai /* initialize the raster console blitter */
242 1.1 tsubai rcp = &dc->dc_rcons;
243 1.1 tsubai rcp->rc_sp = rap;
244 1.1 tsubai rcp->rc_crow = rcp->rc_ccol = -1;
245 1.1 tsubai rcp->rc_crowp = &rcp->rc_crow;
246 1.1 tsubai rcp->rc_ccolp = &rcp->rc_ccol;
247 1.1 tsubai rcons_init(rcp, 128, 128);
248 1.1 tsubai
249 1.1 tsubai ofb_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
250 1.1 tsubai ofb_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
251 1.1 tsubai }
252 1.1 tsubai
253 1.1 tsubai int
254 1.1 tsubai ofb_is_console()
255 1.1 tsubai {
256 1.1 tsubai int chosen, stdout, node;
257 1.1 tsubai char type[16];
258 1.1 tsubai
259 1.1 tsubai chosen = OF_finddevice("/chosen");
260 1.1 tsubai OF_getprop(chosen, "stdout", &stdout, 4);
261 1.1 tsubai node = OF_instance_to_package(stdout);
262 1.1 tsubai OF_getprop(node, "device_type", type, sizeof(type));
263 1.1 tsubai if (strcmp(type, "display") == 0)
264 1.1 tsubai return 1;
265 1.1 tsubai else
266 1.1 tsubai return 0;
267 1.1 tsubai }
268 1.1 tsubai
269 1.1 tsubai int
270 1.1 tsubai ofb_ioctl(v, cmd, data, flag, p)
271 1.1 tsubai void *v;
272 1.1 tsubai u_long cmd;
273 1.1 tsubai caddr_t data;
274 1.1 tsubai int flag;
275 1.1 tsubai struct proc *p;
276 1.1 tsubai {
277 1.1 tsubai struct ofb_softc *sc = v;
278 1.1 tsubai struct ofb_devconfig *dc = sc->sc_dc;
279 1.1 tsubai struct wsdisplay_fbinfo *wdf;
280 1.1 tsubai struct grfinfo *gm;
281 1.1 tsubai
282 1.1 tsubai switch (cmd) {
283 1.1 tsubai case WSDISPLAYIO_GTYPE:
284 1.1 tsubai *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */
285 1.1 tsubai return 0;
286 1.1 tsubai
287 1.1 tsubai case WSDISPLAYIO_GINFO:
288 1.1 tsubai wdf = (void *)data;
289 1.1 tsubai wdf->height = sc->sc_dc->dc_raster.height;
290 1.1 tsubai wdf->width = sc->sc_dc->dc_raster.width;
291 1.1 tsubai wdf->depth = sc->sc_dc->dc_raster.depth;
292 1.1 tsubai wdf->cmsize = 256;
293 1.1 tsubai return 0;
294 1.1 tsubai
295 1.1 tsubai case WSDISPLAYIO_PUTCMAP:
296 1.1 tsubai return putcmap(sc, data);
297 1.1 tsubai
298 1.1 tsubai /* XXX There are no way to know framebuffer pa from a user program. */
299 1.1 tsubai case GRFIOCGINFO:
300 1.1 tsubai gm = (void *)data;
301 1.1 tsubai bzero(gm, sizeof(struct grfinfo));
302 1.1 tsubai gm->gd_fbaddr = (caddr_t)sc->sc_dc->dc_paddr;
303 1.1 tsubai gm->gd_fbrowbytes = sc->sc_dc->dc_linebytes;
304 1.1 tsubai return 0;
305 1.1 tsubai }
306 1.1 tsubai return -1;
307 1.1 tsubai }
308 1.1 tsubai
309 1.1 tsubai int
310 1.1 tsubai ofb_mmap(v, offset, prot)
311 1.1 tsubai void *v;
312 1.1 tsubai off_t offset;
313 1.1 tsubai int prot;
314 1.1 tsubai {
315 1.1 tsubai struct ofb_softc *sc = v;
316 1.1 tsubai struct ofb_devconfig *dc = sc->sc_dc;
317 1.1 tsubai
318 1.4 mrg if (offset >= (dc->dc_linebytes * dc->dc_height) || offset < 0)
319 1.1 tsubai return -1;
320 1.1 tsubai
321 1.1 tsubai return dc->dc_paddr + offset;
322 1.1 tsubai }
323 1.1 tsubai
324 1.1 tsubai int
325 1.1 tsubai ofb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
326 1.1 tsubai void *v;
327 1.1 tsubai const struct wsscreen_descr *type;
328 1.1 tsubai void **cookiep;
329 1.1 tsubai int *curxp, *curyp;
330 1.1 tsubai long *attrp;
331 1.1 tsubai {
332 1.1 tsubai struct ofb_softc *sc = v;
333 1.1 tsubai long defattr;
334 1.1 tsubai
335 1.1 tsubai if (sc->nscreens > 0)
336 1.1 tsubai return (ENOMEM);
337 1.1 tsubai
338 1.1 tsubai *cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
339 1.1 tsubai *curxp = 0;
340 1.1 tsubai *curyp = 0;
341 1.1 tsubai rcons_alloc_attr(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
342 1.1 tsubai *attrp = defattr;
343 1.1 tsubai sc->nscreens++;
344 1.1 tsubai return 0;
345 1.1 tsubai }
346 1.1 tsubai
347 1.1 tsubai void
348 1.1 tsubai ofb_free_screen(v, cookie)
349 1.1 tsubai void *v;
350 1.1 tsubai void *cookie;
351 1.1 tsubai {
352 1.1 tsubai struct ofb_softc *sc = v;
353 1.1 tsubai
354 1.1 tsubai if (sc->sc_dc == &ofb_console_dc)
355 1.1 tsubai panic("ofb_free_screen: console");
356 1.1 tsubai
357 1.1 tsubai sc->nscreens--;
358 1.1 tsubai }
359 1.1 tsubai
360 1.1 tsubai void
361 1.1 tsubai ofb_show_screen(v, cookie)
362 1.1 tsubai void *v;
363 1.1 tsubai void *cookie;
364 1.1 tsubai {
365 1.1 tsubai }
366 1.1 tsubai
367 1.1 tsubai static int
368 1.1 tsubai ofb_load_font(v, cookie, first, num, stride, data)
369 1.1 tsubai void *v;
370 1.1 tsubai void *cookie;
371 1.1 tsubai int first, num, stride;
372 1.1 tsubai void *data;
373 1.1 tsubai {
374 1.1 tsubai return EINVAL;
375 1.1 tsubai }
376 1.1 tsubai
377 1.1 tsubai int
378 1.1 tsubai ofb_cnattach()
379 1.1 tsubai {
380 1.1 tsubai struct ofb_devconfig *dc = &ofb_console_dc;
381 1.1 tsubai long defattr;
382 1.1 tsubai int crow;
383 1.1 tsubai int chosen, stdout, node;
384 1.1 tsubai char cmd[32];
385 1.1 tsubai
386 1.1 tsubai chosen = OF_finddevice("/chosen");
387 1.1 tsubai OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
388 1.1 tsubai node = OF_instance_to_package(stdout);
389 1.1 tsubai dc->dc_ih = stdout;
390 1.1 tsubai
391 1.1 tsubai ofb_common_init(node, dc);
392 1.1 tsubai #if 0
393 1.1 tsubai /* get current cursor position */
394 1.1 tsubai OF_interpret("line#", 1, &crow);
395 1.1 tsubai
396 1.1 tsubai /* move (rom monitor) cursor to the lowest line */
397 1.1 tsubai sprintf(cmd, "%x to line#", ofb_stdscreen.nrows - 1);
398 1.1 tsubai OF_interpret(cmd, 0);
399 1.1 tsubai #endif
400 1.1 tsubai rcons_alloc_attr(&dc->dc_rcons, 0, 0, 0, &defattr);
401 1.1 tsubai wsdisplay_cnattach(&ofb_stdscreen, &dc->dc_rcons, 0, 0, defattr);
402 1.1 tsubai
403 1.1 tsubai return 0;
404 1.1 tsubai }
405 1.1 tsubai
406 1.1 tsubai int
407 1.1 tsubai putcmap(sc, cm)
408 1.1 tsubai struct ofb_softc *sc;
409 1.1 tsubai struct wsdisplay_cmap *cm;
410 1.1 tsubai {
411 1.1 tsubai struct ofb_devconfig *dc = sc->sc_dc;
412 1.1 tsubai int index = cm->index;
413 1.1 tsubai int count = cm->count;
414 1.1 tsubai int i, s;
415 1.1 tsubai u_char *r, *g, *b;
416 1.1 tsubai
417 1.1 tsubai if (cm->index >= 256 || cm->count > 256 ||
418 1.1 tsubai (cm->index + cm->count) > 256)
419 1.1 tsubai return EINVAL;
420 1.1 tsubai #if defined(UVM)
421 1.1 tsubai if (!uvm_useracc(cm->red, cm->count, B_READ) ||
422 1.1 tsubai !uvm_useracc(cm->green, cm->count, B_READ) ||
423 1.1 tsubai !uvm_useracc(cm->blue, cm->count, B_READ))
424 1.1 tsubai return EFAULT;
425 1.1 tsubai #else
426 1.1 tsubai if (!useracc(cm->red, cm->count, B_READ) ||
427 1.1 tsubai !useracc(cm->green, cm->count, B_READ) ||
428 1.1 tsubai !useracc(cm->blue, cm->count, B_READ))
429 1.1 tsubai return EFAULT;
430 1.1 tsubai #endif
431 1.1 tsubai copyin(cm->red, &sc->sc_cmap_red[index], count);
432 1.1 tsubai copyin(cm->green, &sc->sc_cmap_green[index], count);
433 1.1 tsubai copyin(cm->blue, &sc->sc_cmap_blue[index], count);
434 1.1 tsubai
435 1.1 tsubai r = &sc->sc_cmap_red[index];
436 1.1 tsubai g = &sc->sc_cmap_green[index];
437 1.1 tsubai b = &sc->sc_cmap_blue[index];
438 1.1 tsubai
439 1.1 tsubai s = splhigh();
440 1.1 tsubai asm volatile ("mtdbatl 3,%0; mtdbatu 3,%1"
441 1.1 tsubai :: "r"(BATL(dc->dc_paddr, BAT_I)), "r"(BATU(dc->dc_paddr)));
442 1.1 tsubai
443 1.1 tsubai for (i = 0; i < count; i++) {
444 1.1 tsubai OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
445 1.1 tsubai r++, g++, b++, index++;
446 1.1 tsubai }
447 1.1 tsubai splx(s);
448 1.1 tsubai
449 1.1 tsubai return 0;
450 1.1 tsubai }
451