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