ffb.c revision 1.3 1 /* $NetBSD: ffb.c,v 1.3 2003/07/15 03:36:05 lukem Exp $ */
2 /* $OpenBSD: creator.c,v 1.20 2002/07/30 19:48:15 jason Exp $ */
3
4 /*
5 * Copyright (c) 2002 Jason L. Wright (jason (at) thought.net)
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Jason L. Wright
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: ffb.c,v 1.3 2003/07/15 03:36:05 lukem Exp $");
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/conf.h>
44
45 #include <machine/bus.h>
46 #include <machine/autoconf.h>
47 #include <machine/openfirm.h>
48
49 #include <dev/wscons/wsconsio.h>
50 #include <dev/wscons/wsdisplayvar.h>
51 #include <dev/wscons/wscons_raster.h>
52 #include <dev/rasops/rasops.h>
53
54 #include <sparc64/dev/ffbreg.h>
55 #include <sparc64/dev/ffbvar.h>
56
57 struct wsscreen_descr ffb_stdscreen = {
58 "std",
59 0, 0, /* will be filled in -- XXX shouldn't, it's global. */
60 0,
61 0, 0,
62 WSSCREEN_REVERSE | WSSCREEN_WSCOLORS
63 };
64
65 const struct wsscreen_descr *ffb_scrlist[] = {
66 &ffb_stdscreen,
67 /* XXX other formats? */
68 };
69
70 struct wsscreen_list ffb_screenlist = {
71 sizeof(ffb_scrlist) / sizeof(struct wsscreen_descr *),
72 ffb_scrlist
73 };
74
75 int ffb_ioctl(void *, u_long, caddr_t, int, struct proc *);
76 int ffb_alloc_screen(void *, const struct wsscreen_descr *, void **,
77 int *, int *, long *);
78 void ffb_free_screen(void *, void *);
79 int ffb_show_screen(void *, void *, int, void (*cb)(void *, int, int),
80 void *);
81 paddr_t ffb_mmap(void *, off_t, int);
82 static int a2int(char *, int);
83 void ffb_ras_fifo_wait(struct ffb_softc *, int);
84 void ffb_ras_wait(struct ffb_softc *);
85 void ffb_ras_init(struct ffb_softc *);
86 void ffb_ras_copyrows(void *, int, int, int);
87 void ffb_ras_erasecols(void *, int, int, int, long int);
88 void ffb_ras_eraserows(void *, int, int, long int);
89 void ffb_ras_do_cursor(struct rasops_info *);
90 void ffb_ras_fill(struct ffb_softc *);
91 void ffb_ras_setfg(struct ffb_softc *, int32_t);
92
93 struct wsdisplay_accessops ffb_accessops = {
94 ffb_ioctl,
95 ffb_mmap,
96 ffb_alloc_screen,
97 ffb_free_screen,
98 ffb_show_screen,
99 NULL, /* load font */
100 NULL, /* scrollback */
101 NULL, /* getchar */
102 NULL, /* burner */
103 };
104
105 void
106 ffb_attach(struct ffb_softc *sc)
107 {
108 struct wsemuldisplaydev_attach_args waa;
109 char *model;
110 int btype;
111
112 printf(":");
113
114 if (sc->sc_type == FFB_CREATOR) {
115 btype = PROM_getpropint(sc->sc_node, "board_type", 0);
116 if ((btype & 7) == 3)
117 printf(" Creator3D");
118 else
119 printf(" Creator");
120 } else
121 printf(" Elite3D");
122
123 model = PROM_getpropstring(sc->sc_node, "model");
124 if (model == NULL || strlen(model) == 0)
125 model = "unknown";
126
127 printf(", model %s\n", model);
128
129 sc->sc_depth = 24;
130 sc->sc_linebytes = 8192;
131 sc->sc_height = PROM_getpropint(sc->sc_node, "height", 0);
132 sc->sc_width = PROM_getpropint(sc->sc_node, "width", 0);
133
134 sc->sc_rasops.ri_depth = 32;
135 sc->sc_rasops.ri_stride = sc->sc_linebytes;
136 sc->sc_rasops.ri_flg = RI_CENTER;
137 sc->sc_rasops.ri_bits = (void *)bus_space_vaddr(sc->sc_bt,
138 sc->sc_pixel_h);
139
140 sc->sc_rasops.ri_width = sc->sc_width;
141 sc->sc_rasops.ri_height = sc->sc_height;
142 sc->sc_rasops.ri_hw = sc;
143
144 rasops_init(&sc->sc_rasops,
145 a2int(PROM_getpropstring(optionsnode, "screen-#rows"), 34),
146 a2int(PROM_getpropstring(optionsnode, "screen-#columns"), 80));
147
148 if ((sc->sc_dv.dv_cfdata->cf_flags & FFB_CFFLAG_NOACCEL) == 0) {
149 sc->sc_rasops.ri_hw = sc;
150 sc->sc_rasops.ri_ops.eraserows = ffb_ras_eraserows;
151 sc->sc_rasops.ri_ops.erasecols = ffb_ras_erasecols;
152 sc->sc_rasops.ri_ops.copyrows = ffb_ras_copyrows;
153 ffb_ras_init(sc);
154 }
155
156 ffb_stdscreen.nrows = sc->sc_rasops.ri_rows;
157 ffb_stdscreen.ncols = sc->sc_rasops.ri_cols;
158 ffb_stdscreen.textops = &sc->sc_rasops.ri_ops;
159
160 if (sc->sc_console) {
161 int *ccolp, *crowp;
162 long defattr;
163
164 if (romgetcursoraddr(&crowp, &ccolp))
165 ccolp = crowp = NULL;
166 if (ccolp != NULL)
167 sc->sc_rasops.ri_ccol = *ccolp;
168 if (crowp != NULL)
169 sc->sc_rasops.ri_crow = *crowp;
170
171 sc->sc_rasops.ri_ops.allocattr(&sc->sc_rasops,
172 0, 0, 0, &defattr);
173
174 wsdisplay_cnattach(&ffb_stdscreen, &sc->sc_rasops,
175 sc->sc_rasops.ri_ccol, sc->sc_rasops.ri_crow, defattr);
176 }
177
178 waa.console = sc->sc_console;
179 waa.scrdata = &ffb_screenlist;
180 waa.accessops = &ffb_accessops;
181 waa.accesscookie = sc;
182 config_found(&sc->sc_dv, &waa, wsemuldisplaydevprint);
183 }
184
185 int
186 ffb_ioctl(v, cmd, data, flags, p)
187 void *v;
188 u_long cmd;
189 caddr_t data;
190 int flags;
191 struct proc *p;
192 {
193 struct ffb_softc *sc = v;
194 struct wsdisplay_fbinfo *wdf;
195
196 #ifdef FFBDEBUG
197 printf("ffb_ioctl: %s cmd _IO%s%s('%c', %lu)\n",
198 sc->sc_dv.dv_xname,
199 (cmd & IOC_IN) ? "W" : "", (cmd & IOC_OUT) ? "R" : "",
200 (char)IOCGROUP(cmd), cmd & 0xff);
201 #endif
202
203 switch (cmd) {
204 case WSDISPLAYIO_GTYPE:
205 *(u_int *)data = WSDISPLAY_TYPE_SUN24;
206 break;
207 case WSDISPLAYIO_SMODE:
208 sc->sc_mode = *(u_int *)data;
209 break;
210 case WSDISPLAYIO_GINFO:
211 wdf = (void *)data;
212 wdf->height = sc->sc_height;
213 wdf->width = sc->sc_width;
214 wdf->depth = 32;
215 wdf->cmsize = 256; /* XXX */
216 break;
217 #ifdef WSDISPLAYIO_LINEBYTES
218 case WSDISPLAYIO_LINEBYTES:
219 *(u_int *)data = sc->sc_linebytes;
220 break;
221 #endif
222 case WSDISPLAYIO_GETCMAP:
223 break;/* XXX */
224
225 case WSDISPLAYIO_PUTCMAP:
226 break;/* XXX */
227
228 case WSDISPLAYIO_SVIDEO:
229 case WSDISPLAYIO_GVIDEO:
230 case WSDISPLAYIO_GCURPOS:
231 case WSDISPLAYIO_SCURPOS:
232 case WSDISPLAYIO_GCURMAX:
233 case WSDISPLAYIO_GCURSOR:
234 case WSDISPLAYIO_SCURSOR:
235 default:
236 return -1; /* not supported yet */
237 }
238
239 return (0);
240 }
241
242 int
243 ffb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
244 void *v;
245 const struct wsscreen_descr *type;
246 void **cookiep;
247 int *curxp, *curyp;
248 long *attrp;
249 {
250 struct ffb_softc *sc = v;
251
252 if (sc->sc_nscreens > 0)
253 return (ENOMEM);
254
255 *cookiep = &sc->sc_rasops;
256 *curyp = 0;
257 *curxp = 0;
258
259 sc->sc_rasops.ri_ops.allocattr(&sc->sc_rasops, 0, 0, 0, attrp);
260
261 sc->sc_nscreens++;
262 return (0);
263 }
264
265 void
266 ffb_free_screen(v, cookie)
267 void *v;
268 void *cookie;
269 {
270 struct ffb_softc *sc = v;
271
272 sc->sc_nscreens--;
273 }
274
275 int
276 ffb_show_screen(v, cookie, waitok, cb, cbarg)
277 void *v;
278 void *cookie;
279 int waitok;
280 void (*cb)(void *, int, int);
281 void *cbarg;
282 {
283 return (0);
284 }
285
286 paddr_t
287 ffb_mmap(vsc, off, prot)
288 void *vsc;
289 off_t off;
290 int prot;
291 {
292 struct ffb_softc *sc = vsc;
293 int i;
294
295 switch (sc->sc_mode) {
296 case WSDISPLAYIO_MODE_MAPPED:
297 for (i = 0; i < sc->sc_nreg; i++) {
298 /* Before this set? */
299 if (off < sc->sc_addrs[i])
300 continue;
301 /* After this set? */
302 if (off >= (sc->sc_addrs[i] + sc->sc_sizes[i]))
303 continue;
304
305 return (bus_space_mmap(sc->sc_bt, sc->sc_addrs[i],
306 off - sc->sc_addrs[i], prot, BUS_SPACE_MAP_LINEAR));
307 }
308 break;
309 #ifdef WSDISPLAYIO_MODE_DUMBFB
310 case WSDISPLAYIO_MODE_DUMBFB:
311 if (sc->sc_nreg < FFB_REG_DFB24)
312 break;
313 if (off >= 0 && off < sc->sc_sizes[FFB_REG_DFB24])
314 return (bus_space_mmap(sc->sc_bt,
315 sc->sc_addrs[FFB_REG_DFB24], off, prot,
316 BUS_SPACE_MAP_LINEAR));
317 break;
318 #endif
319 }
320
321 return (-1);
322 }
323
324 static int
325 a2int(char *cp, int deflt)
326 {
327 int i = 0;
328
329 if (*cp == '\0')
330 return (deflt);
331 while (*cp != '\0')
332 i = i * 10 + *cp++ - '0';
333 return (i);
334 }
335
336 void
337 ffb_ras_fifo_wait(sc, n)
338 struct ffb_softc *sc;
339 int n;
340 {
341 int32_t cache = sc->sc_fifo_cache;
342
343 if (cache < n) {
344 do {
345 cache = FBC_READ(sc, FFB_FBC_UCSR);
346 cache = (cache & FBC_UCSR_FIFO_MASK) - 8;
347 } while (cache < n);
348 }
349 sc->sc_fifo_cache = cache - n;
350 }
351
352 void
353 ffb_ras_wait(sc)
354 struct ffb_softc *sc;
355 {
356 u_int32_t ucsr, r;
357
358 while (1) {
359 ucsr = FBC_READ(sc, FFB_FBC_UCSR);
360 if ((ucsr & (FBC_UCSR_FB_BUSY|FBC_UCSR_RP_BUSY)) == 0)
361 break;
362 r = ucsr & (FBC_UCSR_READ_ERR | FBC_UCSR_FIFO_OVFL);
363 if (r != 0)
364 FBC_WRITE(sc, FFB_FBC_UCSR, r);
365 }
366 }
367
368 void
369 ffb_ras_init(sc)
370 struct ffb_softc *sc;
371 {
372 ffb_ras_fifo_wait(sc, 7);
373 FBC_WRITE(sc, FFB_FBC_PPC,
374 FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE |
375 FBC_PPC_APE_DIS | FBC_PPC_CS_CONST);
376 FBC_WRITE(sc, FFB_FBC_FBC,
377 FFB_FBC_WB_A | FFB_FBC_RB_A | FFB_FBC_SB_BOTH |
378 FFB_FBC_XE_OFF | FFB_FBC_RGBE_MASK);
379 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW);
380 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE);
381 FBC_WRITE(sc, FFB_FBC_PMASK, 0xffffffff);
382 FBC_WRITE(sc, FFB_FBC_FONTINC, 0x10000);
383 sc->sc_fg_cache = 0;
384 FBC_WRITE(sc, FFB_FBC_FG, sc->sc_fg_cache);
385 ffb_ras_wait(sc);
386 }
387
388 void
389 ffb_ras_eraserows(cookie, row, n, attr)
390 void *cookie;
391 int row, n;
392 long int attr;
393 {
394 struct rasops_info *ri = cookie;
395 struct ffb_softc *sc = ri->ri_hw;
396
397 if (row < 0) {
398 n += row;
399 row = 0;
400 }
401 if (row + n > ri->ri_rows)
402 n = ri->ri_rows - row;
403 if (n <= 0)
404 return;
405
406 ffb_ras_fill(sc);
407 ffb_ras_setfg(sc, ri->ri_devcmap[(attr >> 16) & 0xf]);
408 ffb_ras_fifo_wait(sc, 4);
409 if ((n == ri->ri_rows) && (ri->ri_flg & RI_FULLCLEAR)) {
410 FBC_WRITE(sc, FFB_FBC_BY, 0);
411 FBC_WRITE(sc, FFB_FBC_BX, 0);
412 FBC_WRITE(sc, FFB_FBC_BH, ri->ri_height);
413 FBC_WRITE(sc, FFB_FBC_BW, ri->ri_width);
414 } else {
415 row *= ri->ri_font->fontheight;
416 FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + row);
417 FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin);
418 FBC_WRITE(sc, FFB_FBC_BH, n * ri->ri_font->fontheight);
419 FBC_WRITE(sc, FFB_FBC_BW, ri->ri_emuwidth);
420 }
421 ffb_ras_wait(sc);
422 }
423
424 void
425 ffb_ras_erasecols(cookie, row, col, n, attr)
426 void *cookie;
427 int row, col, n;
428 long int attr;
429 {
430 struct rasops_info *ri = cookie;
431 struct ffb_softc *sc = ri->ri_hw;
432
433 if ((row < 0) || (row >= ri->ri_rows))
434 return;
435 if (col < 0) {
436 n += col;
437 col = 0;
438 }
439 if (col + n > ri->ri_cols)
440 n = ri->ri_cols - col;
441 if (n <= 0)
442 return;
443 n *= ri->ri_font->fontwidth;
444 col *= ri->ri_font->fontwidth;
445 row *= ri->ri_font->fontheight;
446
447 ffb_ras_fill(sc);
448 ffb_ras_setfg(sc, ri->ri_devcmap[(attr >> 16) & 0xf]);
449 ffb_ras_fifo_wait(sc, 4);
450 FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + row);
451 FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin + col);
452 FBC_WRITE(sc, FFB_FBC_BH, ri->ri_font->fontheight);
453 FBC_WRITE(sc, FFB_FBC_BW, n - 1);
454 ffb_ras_wait(sc);
455 }
456
457 void
458 ffb_ras_fill(sc)
459 struct ffb_softc *sc;
460 {
461 ffb_ras_fifo_wait(sc, 2);
462 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW);
463 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE);
464 ffb_ras_wait(sc);
465 }
466
467 void
468 ffb_ras_copyrows(cookie, src, dst, n)
469 void *cookie;
470 int src, dst, n;
471 {
472 struct rasops_info *ri = cookie;
473 struct ffb_softc *sc = ri->ri_hw;
474
475 if (dst == src)
476 return;
477 if (src < 0) {
478 n += src;
479 src = 0;
480 }
481 if ((src + n) > ri->ri_rows)
482 n = ri->ri_rows - src;
483 if (dst < 0) {
484 n += dst;
485 dst = 0;
486 }
487 if ((dst + n) > ri->ri_rows)
488 n = ri->ri_rows - dst;
489 if (n <= 0)
490 return;
491 n *= ri->ri_font->fontheight;
492 src *= ri->ri_font->fontheight;
493 dst *= ri->ri_font->fontheight;
494
495 ffb_ras_fifo_wait(sc, 8);
496 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_OLD | (FBC_ROP_OLD << 8));
497 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_VSCROLL);
498 FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + src);
499 FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin);
500 FBC_WRITE(sc, FFB_FBC_DY, ri->ri_yorigin + dst);
501 FBC_WRITE(sc, FFB_FBC_DX, ri->ri_xorigin);
502 FBC_WRITE(sc, FFB_FBC_BH, n);
503 FBC_WRITE(sc, FFB_FBC_BW, ri->ri_emuwidth);
504 ffb_ras_wait(sc);
505 }
506
507 void
508 ffb_ras_setfg(sc, fg)
509 struct ffb_softc *sc;
510 int32_t fg;
511 {
512 ffb_ras_fifo_wait(sc, 1);
513 if (fg == sc->sc_fg_cache)
514 return;
515 sc->sc_fg_cache = fg;
516 FBC_WRITE(sc, FFB_FBC_FG, fg);
517 ffb_ras_wait(sc);
518 }
519