ffb.c revision 1.7 1 /* $NetBSD: ffb.c,v 1.7 2004/05/21 21:45:04 heas 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.7 2004/05/21 21:45:04 heas 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 "sunffb",
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 static int ffb_blank(struct ffb_softc *, u_long, u_int *);
79 void ffb_free_screen(void *, void *);
80 int ffb_show_screen(void *, void *, int, void (*cb)(void *, int, int),
81 void *);
82 paddr_t ffb_mmap(void *, off_t, 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 int maxrow, maxcol;
112 u_int blank = WSDISPLAYIO_VIDEO_ON;
113 char buf[6+1];
114
115 printf(":");
116
117 if (sc->sc_type == FFB_CREATOR) {
118 btype = prom_getpropint(sc->sc_node, "board_type", 0);
119 if ((btype & 7) == 3)
120 printf(" Creator3D");
121 else
122 printf(" Creator");
123 } else
124 printf(" Elite3D");
125
126 model = prom_getpropstring(sc->sc_node, "model");
127 if (model == NULL || strlen(model) == 0)
128 model = "unknown";
129
130 printf(", model %s\n", model);
131
132 sc->sc_depth = 24;
133 sc->sc_linebytes = 8192;
134 sc->sc_height = prom_getpropint(sc->sc_node, "height", 0);
135 sc->sc_width = prom_getpropint(sc->sc_node, "width", 0);
136
137 sc->sc_rasops.ri_depth = 32;
138 sc->sc_rasops.ri_stride = sc->sc_linebytes;
139 sc->sc_rasops.ri_flg = RI_CENTER;
140 sc->sc_rasops.ri_bits = (void *)bus_space_vaddr(sc->sc_bt,
141 sc->sc_pixel_h);
142
143 sc->sc_rasops.ri_width = sc->sc_width;
144 sc->sc_rasops.ri_height = sc->sc_height;
145 sc->sc_rasops.ri_hw = sc;
146
147 maxcol = (prom_getoption("screen-#columns", buf, sizeof buf) == 0)
148 ? strtoul(buf, NULL, 10)
149 : 80;
150
151 maxrow = (prom_getoption("screen-#rows", buf, sizeof buf) != 0)
152 ? strtoul(buf, NULL, 10)
153 : 34;
154
155 rasops_init(&sc->sc_rasops, maxrow, maxcol);
156
157 if ((sc->sc_dv.dv_cfdata->cf_flags & FFB_CFFLAG_NOACCEL) == 0) {
158 sc->sc_rasops.ri_hw = sc;
159 sc->sc_rasops.ri_ops.eraserows = ffb_ras_eraserows;
160 sc->sc_rasops.ri_ops.erasecols = ffb_ras_erasecols;
161 sc->sc_rasops.ri_ops.copyrows = ffb_ras_copyrows;
162 ffb_ras_init(sc);
163 }
164
165 ffb_stdscreen.nrows = sc->sc_rasops.ri_rows;
166 ffb_stdscreen.ncols = sc->sc_rasops.ri_cols;
167 ffb_stdscreen.textops = &sc->sc_rasops.ri_ops;
168
169 ffb_blank(sc, WSDISPLAYIO_SVIDEO, &blank);
170
171 if (sc->sc_console) {
172 int *ccolp, *crowp;
173 long defattr;
174
175 if (romgetcursoraddr(&crowp, &ccolp))
176 ccolp = crowp = NULL;
177 if (ccolp != NULL)
178 sc->sc_rasops.ri_ccol = *ccolp;
179 if (crowp != NULL)
180 sc->sc_rasops.ri_crow = *crowp;
181
182 sc->sc_rasops.ri_ops.allocattr(&sc->sc_rasops,
183 0, 0, 0, &defattr);
184
185 wsdisplay_cnattach(&ffb_stdscreen, &sc->sc_rasops,
186 sc->sc_rasops.ri_ccol, sc->sc_rasops.ri_crow, defattr);
187 }
188
189 waa.console = sc->sc_console;
190 waa.scrdata = &ffb_screenlist;
191 waa.accessops = &ffb_accessops;
192 waa.accesscookie = sc;
193 config_found(&sc->sc_dv, &waa, wsemuldisplaydevprint);
194 }
195
196 int
197 ffb_ioctl(v, cmd, data, flags, p)
198 void *v;
199 u_long cmd;
200 caddr_t data;
201 int flags;
202 struct proc *p;
203 {
204 struct ffb_softc *sc = v;
205 struct wsdisplay_fbinfo *wdf;
206
207 #ifdef FFBDEBUG
208 printf("ffb_ioctl: %s cmd _IO%s%s('%c', %lu)\n",
209 sc->sc_dv.dv_xname,
210 (cmd & IOC_IN) ? "W" : "", (cmd & IOC_OUT) ? "R" : "",
211 (char)IOCGROUP(cmd), cmd & 0xff);
212 #endif
213
214 switch (cmd) {
215 case WSDISPLAYIO_GTYPE:
216 *(u_int *)data = WSDISPLAY_TYPE_SUNFFB;
217 break;
218 case WSDISPLAYIO_SMODE:
219 sc->sc_mode = *(u_int *)data;
220 break;
221 case WSDISPLAYIO_GINFO:
222 wdf = (void *)data;
223 wdf->height = sc->sc_height;
224 wdf->width = sc->sc_width;
225 wdf->depth = 32;
226 wdf->cmsize = 256; /* XXX */
227 break;
228 #ifdef WSDISPLAYIO_LINEBYTES
229 case WSDISPLAYIO_LINEBYTES:
230 *(u_int *)data = sc->sc_linebytes;
231 break;
232 #endif
233 case WSDISPLAYIO_GETCMAP:
234 break;/* XXX */
235
236 case WSDISPLAYIO_PUTCMAP:
237 break;/* XXX */
238
239 case WSDISPLAYIO_SVIDEO:
240 case WSDISPLAYIO_GVIDEO:
241 return(ffb_blank(sc, cmd, (u_int *)data));
242 break;
243 case WSDISPLAYIO_GCURPOS:
244 case WSDISPLAYIO_SCURPOS:
245 case WSDISPLAYIO_GCURMAX:
246 case WSDISPLAYIO_GCURSOR:
247 case WSDISPLAYIO_SCURSOR:
248 default:
249 return -1; /* not supported yet */
250 }
251
252 return (0);
253 }
254
255 int
256 ffb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
257 void *v;
258 const struct wsscreen_descr *type;
259 void **cookiep;
260 int *curxp, *curyp;
261 long *attrp;
262 {
263 struct ffb_softc *sc = v;
264
265 if (sc->sc_nscreens > 0)
266 return (ENOMEM);
267
268 *cookiep = &sc->sc_rasops;
269 *curyp = 0;
270 *curxp = 0;
271
272 sc->sc_rasops.ri_ops.allocattr(&sc->sc_rasops, 0, 0, 0, attrp);
273
274 sc->sc_nscreens++;
275 return (0);
276 }
277
278 /* blank/unblank the screen */
279 static int
280 ffb_blank(struct ffb_softc *sc, u_long cmd, u_int *data)
281 {
282 u_int val;
283
284 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_GSBLANK);
285 val = DAC_READ(sc, FFB_DAC_VALUE);
286
287 switch (cmd) {
288 case WSDISPLAYIO_GVIDEO:
289 *data = val & 1;
290 return(0);
291 break;
292 case WSDISPLAYIO_SVIDEO:
293 if (*data == WSDISPLAYIO_VIDEO_OFF)
294 val &= ~1;
295 else if (*data == WSDISPLAYIO_VIDEO_ON)
296 val |= 1;
297 else
298 return(EINVAL);
299 break;
300 default:
301 return(EINVAL);
302 }
303
304 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_GSBLANK);
305 DAC_WRITE(sc, FFB_DAC_VALUE, val);
306
307 return(0);
308 }
309
310 void
311 ffb_free_screen(v, cookie)
312 void *v;
313 void *cookie;
314 {
315 struct ffb_softc *sc = v;
316
317 sc->sc_nscreens--;
318 }
319
320 int
321 ffb_show_screen(v, cookie, waitok, cb, cbarg)
322 void *v;
323 void *cookie;
324 int waitok;
325 void (*cb)(void *, int, int);
326 void *cbarg;
327 {
328 return (0);
329 }
330
331 paddr_t
332 ffb_mmap(vsc, off, prot)
333 void *vsc;
334 off_t off;
335 int prot;
336 {
337 struct ffb_softc *sc = vsc;
338 int i;
339
340 switch (sc->sc_mode) {
341 case WSDISPLAYIO_MODE_MAPPED:
342 for (i = 0; i < sc->sc_nreg; i++) {
343 /* Before this set? */
344 if (off < sc->sc_addrs[i])
345 continue;
346 /* After this set? */
347 if (off >= (sc->sc_addrs[i] + sc->sc_sizes[i]))
348 continue;
349
350 return (bus_space_mmap(sc->sc_bt, sc->sc_addrs[i],
351 off - sc->sc_addrs[i], prot, BUS_SPACE_MAP_LINEAR));
352 }
353 break;
354 #ifdef WSDISPLAYIO_MODE_DUMBFB
355 case WSDISPLAYIO_MODE_DUMBFB:
356 if (sc->sc_nreg < FFB_REG_DFB24)
357 break;
358 if (off >= 0 && off < sc->sc_sizes[FFB_REG_DFB24])
359 return (bus_space_mmap(sc->sc_bt,
360 sc->sc_addrs[FFB_REG_DFB24], off, prot,
361 BUS_SPACE_MAP_LINEAR));
362 break;
363 #endif
364 }
365
366 return (-1);
367 }
368
369 void
370 ffb_ras_fifo_wait(sc, n)
371 struct ffb_softc *sc;
372 int n;
373 {
374 int32_t cache = sc->sc_fifo_cache;
375
376 if (cache < n) {
377 do {
378 cache = FBC_READ(sc, FFB_FBC_UCSR);
379 cache = (cache & FBC_UCSR_FIFO_MASK) - 8;
380 } while (cache < n);
381 }
382 sc->sc_fifo_cache = cache - n;
383 }
384
385 void
386 ffb_ras_wait(sc)
387 struct ffb_softc *sc;
388 {
389 u_int32_t ucsr, r;
390
391 while (1) {
392 ucsr = FBC_READ(sc, FFB_FBC_UCSR);
393 if ((ucsr & (FBC_UCSR_FB_BUSY|FBC_UCSR_RP_BUSY)) == 0)
394 break;
395 r = ucsr & (FBC_UCSR_READ_ERR | FBC_UCSR_FIFO_OVFL);
396 if (r != 0)
397 FBC_WRITE(sc, FFB_FBC_UCSR, r);
398 }
399 }
400
401 void
402 ffb_ras_init(sc)
403 struct ffb_softc *sc;
404 {
405 ffb_ras_fifo_wait(sc, 7);
406 FBC_WRITE(sc, FFB_FBC_PPC,
407 FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE |
408 FBC_PPC_APE_DIS | FBC_PPC_CS_CONST);
409 FBC_WRITE(sc, FFB_FBC_FBC,
410 FFB_FBC_WB_A | FFB_FBC_RB_A | FFB_FBC_SB_BOTH |
411 FFB_FBC_XE_OFF | FFB_FBC_RGBE_MASK);
412 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW);
413 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE);
414 FBC_WRITE(sc, FFB_FBC_PMASK, 0xffffffff);
415 FBC_WRITE(sc, FFB_FBC_FONTINC, 0x10000);
416 sc->sc_fg_cache = 0;
417 FBC_WRITE(sc, FFB_FBC_FG, sc->sc_fg_cache);
418 ffb_ras_wait(sc);
419 }
420
421 void
422 ffb_ras_eraserows(cookie, row, n, attr)
423 void *cookie;
424 int row, n;
425 long int attr;
426 {
427 struct rasops_info *ri = cookie;
428 struct ffb_softc *sc = ri->ri_hw;
429
430 if (row < 0) {
431 n += row;
432 row = 0;
433 }
434 if (row + n > ri->ri_rows)
435 n = ri->ri_rows - row;
436 if (n <= 0)
437 return;
438
439 ffb_ras_fill(sc);
440 ffb_ras_setfg(sc, ri->ri_devcmap[(attr >> 16) & 0xf]);
441 ffb_ras_fifo_wait(sc, 4);
442 if ((n == ri->ri_rows) && (ri->ri_flg & RI_FULLCLEAR)) {
443 FBC_WRITE(sc, FFB_FBC_BY, 0);
444 FBC_WRITE(sc, FFB_FBC_BX, 0);
445 FBC_WRITE(sc, FFB_FBC_BH, ri->ri_height);
446 FBC_WRITE(sc, FFB_FBC_BW, ri->ri_width);
447 } else {
448 row *= ri->ri_font->fontheight;
449 FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + row);
450 FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin);
451 FBC_WRITE(sc, FFB_FBC_BH, n * ri->ri_font->fontheight);
452 FBC_WRITE(sc, FFB_FBC_BW, ri->ri_emuwidth);
453 }
454 ffb_ras_wait(sc);
455 }
456
457 void
458 ffb_ras_erasecols(cookie, row, col, n, attr)
459 void *cookie;
460 int row, col, n;
461 long int attr;
462 {
463 struct rasops_info *ri = cookie;
464 struct ffb_softc *sc = ri->ri_hw;
465
466 if ((row < 0) || (row >= ri->ri_rows))
467 return;
468 if (col < 0) {
469 n += col;
470 col = 0;
471 }
472 if (col + n > ri->ri_cols)
473 n = ri->ri_cols - col;
474 if (n <= 0)
475 return;
476 n *= ri->ri_font->fontwidth;
477 col *= ri->ri_font->fontwidth;
478 row *= ri->ri_font->fontheight;
479
480 ffb_ras_fill(sc);
481 ffb_ras_setfg(sc, ri->ri_devcmap[(attr >> 16) & 0xf]);
482 ffb_ras_fifo_wait(sc, 4);
483 FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + row);
484 FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin + col);
485 FBC_WRITE(sc, FFB_FBC_BH, ri->ri_font->fontheight);
486 FBC_WRITE(sc, FFB_FBC_BW, n - 1);
487 ffb_ras_wait(sc);
488 }
489
490 void
491 ffb_ras_fill(sc)
492 struct ffb_softc *sc;
493 {
494 ffb_ras_fifo_wait(sc, 2);
495 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW);
496 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE);
497 ffb_ras_wait(sc);
498 }
499
500 void
501 ffb_ras_copyrows(cookie, src, dst, n)
502 void *cookie;
503 int src, dst, n;
504 {
505 struct rasops_info *ri = cookie;
506 struct ffb_softc *sc = ri->ri_hw;
507
508 if (dst == src)
509 return;
510 if (src < 0) {
511 n += src;
512 src = 0;
513 }
514 if ((src + n) > ri->ri_rows)
515 n = ri->ri_rows - src;
516 if (dst < 0) {
517 n += dst;
518 dst = 0;
519 }
520 if ((dst + n) > ri->ri_rows)
521 n = ri->ri_rows - dst;
522 if (n <= 0)
523 return;
524 n *= ri->ri_font->fontheight;
525 src *= ri->ri_font->fontheight;
526 dst *= ri->ri_font->fontheight;
527
528 ffb_ras_fifo_wait(sc, 8);
529 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_OLD | (FBC_ROP_OLD << 8));
530 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_VSCROLL);
531 FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + src);
532 FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin);
533 FBC_WRITE(sc, FFB_FBC_DY, ri->ri_yorigin + dst);
534 FBC_WRITE(sc, FFB_FBC_DX, ri->ri_xorigin);
535 FBC_WRITE(sc, FFB_FBC_BH, n);
536 FBC_WRITE(sc, FFB_FBC_BW, ri->ri_emuwidth);
537 ffb_ras_wait(sc);
538 }
539
540 void
541 ffb_ras_setfg(sc, fg)
542 struct ffb_softc *sc;
543 int32_t fg;
544 {
545 ffb_ras_fifo_wait(sc, 1);
546 if (fg == sc->sc_fg_cache)
547 return;
548 sc->sc_fg_cache = fg;
549 FBC_WRITE(sc, FFB_FBC_FG, fg);
550 ffb_ras_wait(sc);
551 }
552