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