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