Home | History | Annotate | Line # | Download | only in dev
ffb.c revision 1.1
      1 /*	$NetBSD: ffb.c,v 1.1 2003/05/23 06:51:15 petrov 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/types.h>
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/conf.h>
     41 
     42 #include <machine/bus.h>
     43 #include <machine/autoconf.h>
     44 #include <machine/openfirm.h>
     45 
     46 #include <dev/wscons/wsconsio.h>
     47 #include <dev/wscons/wsdisplayvar.h>
     48 #include <dev/wscons/wscons_raster.h>
     49 #include <dev/rasops/rasops.h>
     50 
     51 #include <sparc64/dev/ffbreg.h>
     52 #include <sparc64/dev/ffbvar.h>
     53 
     54 struct wsscreen_descr ffb_stdscreen = {
     55 	"std",
     56 	0, 0,	/* will be filled in -- XXX shouldn't, it's global. */
     57 	0,
     58 	0, 0,
     59 	WSSCREEN_REVERSE | WSSCREEN_WSCOLORS
     60 };
     61 
     62 const struct wsscreen_descr *ffb_scrlist[] = {
     63 	&ffb_stdscreen,
     64 	/* XXX other formats? */
     65 };
     66 
     67 struct wsscreen_list ffb_screenlist = {
     68 	sizeof(ffb_scrlist) / sizeof(struct wsscreen_descr *),
     69 	    ffb_scrlist
     70 };
     71 
     72 int	ffb_ioctl(void *, u_long, caddr_t, int, struct proc *);
     73 int	ffb_alloc_screen(void *, const struct wsscreen_descr *, void **,
     74 	    int *, int *, long *);
     75 void	ffb_free_screen(void *, void *);
     76 int	ffb_show_screen(void *, void *, int, void (*cb)(void *, int, int),
     77 	    void *);
     78 paddr_t ffb_mmap(void *, off_t, int);
     79 static int a2int(char *, int);
     80 void	ffb_ras_fifo_wait(struct ffb_softc *, int);
     81 void	ffb_ras_wait(struct ffb_softc *);
     82 void	ffb_ras_init(struct ffb_softc *);
     83 void	ffb_ras_copyrows(void *, int, int, int);
     84 void	ffb_ras_erasecols(void *, int, int, int, long int);
     85 void	ffb_ras_eraserows(void *, int, int, long int);
     86 void	ffb_ras_do_cursor(struct rasops_info *);
     87 void	ffb_ras_fill(struct ffb_softc *);
     88 void	ffb_ras_setfg(struct ffb_softc *, int32_t);
     89 
     90 struct wsdisplay_accessops ffb_accessops = {
     91 	ffb_ioctl,
     92 	ffb_mmap,
     93 	ffb_alloc_screen,
     94 	ffb_free_screen,
     95 	ffb_show_screen,
     96 	NULL,	/* load font */
     97 	NULL,	/* scrollback */
     98 	NULL,	/* getchar */
     99 	NULL,	/* burner */
    100 };
    101 
    102 void
    103 ffb_attach(struct ffb_softc *sc)
    104 {
    105 	struct wsemuldisplaydev_attach_args waa;
    106 	char *model;
    107 	int btype;
    108 
    109 	printf(":");
    110 
    111 	if (sc->sc_type == FFB_CREATOR) {
    112 		btype = PROM_getpropint(sc->sc_node, "board_type", 0);
    113 		if ((btype & 7) == 3)
    114 			printf(" Creator3D");
    115 		else
    116 			printf(" Creator");
    117 	} else
    118 		printf(" Elite3D");
    119 
    120 	model = PROM_getpropstring(sc->sc_node, "model");
    121 	if (model == NULL || strlen(model) == 0)
    122 		model = "unknown";
    123 
    124 	printf(", model %s\n", model);
    125 
    126 	sc->sc_depth = 24;
    127 	sc->sc_linebytes = 8192;
    128 	sc->sc_height = PROM_getpropint(sc->sc_node, "height", 0);
    129 	sc->sc_width = PROM_getpropint(sc->sc_node, "width", 0);
    130 
    131 	sc->sc_rasops.ri_depth = 32;
    132 	sc->sc_rasops.ri_stride = sc->sc_linebytes;
    133 	sc->sc_rasops.ri_flg = RI_CENTER;
    134 	sc->sc_rasops.ri_bits = (void *)bus_space_vaddr(sc->sc_bt,
    135 	    sc->sc_pixel_h);
    136 
    137 	sc->sc_rasops.ri_width = sc->sc_width;
    138 	sc->sc_rasops.ri_height = sc->sc_height;
    139 	sc->sc_rasops.ri_hw = sc;
    140 
    141 	rasops_init(&sc->sc_rasops,
    142 	    a2int(PROM_getpropstring(optionsnode, "screen-#rows"), 34),
    143 	    a2int(PROM_getpropstring(optionsnode, "screen-#columns"), 80));
    144 
    145 	if ((sc->sc_dv.dv_cfdata->cf_flags & FFB_CFFLAG_NOACCEL) == 0) {
    146 		sc->sc_rasops.ri_hw = sc;
    147 		sc->sc_rasops.ri_ops.eraserows = ffb_ras_eraserows;
    148 		sc->sc_rasops.ri_ops.erasecols = ffb_ras_erasecols;
    149 		sc->sc_rasops.ri_ops.copyrows = ffb_ras_copyrows;
    150 		ffb_ras_init(sc);
    151 	}
    152 
    153 	ffb_stdscreen.nrows = sc->sc_rasops.ri_rows;
    154 	ffb_stdscreen.ncols = sc->sc_rasops.ri_cols;
    155 	ffb_stdscreen.textops = &sc->sc_rasops.ri_ops;
    156 
    157 	if (sc->sc_console) {
    158 		int *ccolp, *crowp;
    159 		long defattr;
    160 
    161 		if (romgetcursoraddr(&crowp, &ccolp))
    162 			ccolp = crowp = NULL;
    163 		if (ccolp != NULL)
    164 			sc->sc_rasops.ri_ccol = *ccolp;
    165 		if (crowp != NULL)
    166 			sc->sc_rasops.ri_crow = *crowp;
    167 
    168 		sc->sc_rasops.ri_ops.allocattr(&sc->sc_rasops,
    169 		    0, 0, 0, &defattr);
    170 
    171 		wsdisplay_cnattach(&ffb_stdscreen, &sc->sc_rasops,
    172 		    sc->sc_rasops.ri_ccol, sc->sc_rasops.ri_crow, defattr);
    173 	}
    174 
    175 	waa.console = sc->sc_console;
    176 	waa.scrdata = &ffb_screenlist;
    177 	waa.accessops = &ffb_accessops;
    178 	waa.accesscookie = sc;
    179 	config_found(&sc->sc_dv, &waa, wsemuldisplaydevprint);
    180 }
    181 
    182 int
    183 ffb_ioctl(v, cmd, data, flags, p)
    184 	void *v;
    185 	u_long cmd;
    186 	caddr_t data;
    187 	int flags;
    188 	struct proc *p;
    189 {
    190 	struct ffb_softc *sc = v;
    191 	struct wsdisplay_fbinfo *wdf;
    192 
    193 	printf("ffb_ioctl: %s cmd _IO%s%s('%c', %lu)\n",
    194 	       sc->sc_dv.dv_xname,
    195 	       (cmd & IOC_IN) ? "W" : "", (cmd & IOC_OUT) ? "R" : "",
    196 	       (char)IOCGROUP(cmd), cmd & 0xff);
    197 
    198 	switch (cmd) {
    199 	case WSDISPLAYIO_GTYPE:
    200 		*(u_int *)data = WSDISPLAY_TYPE_SUN24;
    201 		break;
    202 	case WSDISPLAYIO_SMODE:
    203 		sc->sc_mode = *(u_int *)data;
    204 		break;
    205 	case WSDISPLAYIO_GINFO:
    206 		wdf = (void *)data;
    207 		wdf->height = sc->sc_height;
    208 		wdf->width  = sc->sc_width;
    209 		wdf->depth  = 32;
    210 		wdf->cmsize = 256; /* XXX */
    211 		break;
    212 #ifdef WSDISPLAYIO_LINEBYTES
    213 	case WSDISPLAYIO_LINEBYTES:
    214 		*(u_int *)data = sc->sc_linebytes;
    215 		break;
    216 #endif
    217 	case WSDISPLAYIO_GETCMAP:
    218 		break;/* XXX */
    219 
    220 	case WSDISPLAYIO_PUTCMAP:
    221 		break;/* XXX */
    222 
    223 	case WSDISPLAYIO_SVIDEO:
    224 	case WSDISPLAYIO_GVIDEO:
    225 	case WSDISPLAYIO_GCURPOS:
    226 	case WSDISPLAYIO_SCURPOS:
    227 	case WSDISPLAYIO_GCURMAX:
    228 	case WSDISPLAYIO_GCURSOR:
    229 	case WSDISPLAYIO_SCURSOR:
    230 	default:
    231 		return -1; /* not supported yet */
    232         }
    233 
    234 	return (0);
    235 }
    236 
    237 int
    238 ffb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    239 	void *v;
    240 	const struct wsscreen_descr *type;
    241 	void **cookiep;
    242 	int *curxp, *curyp;
    243 	long *attrp;
    244 {
    245 	struct ffb_softc *sc = v;
    246 
    247 	if (sc->sc_nscreens > 0)
    248 		return (ENOMEM);
    249 
    250 	*cookiep = &sc->sc_rasops;
    251 	*curyp = 0;
    252 	*curxp = 0;
    253 
    254 	sc->sc_rasops.ri_ops.allocattr(&sc->sc_rasops, 0, 0, 0, attrp);
    255 
    256 	sc->sc_nscreens++;
    257 	return (0);
    258 }
    259 
    260 void
    261 ffb_free_screen(v, cookie)
    262 	void *v;
    263 	void *cookie;
    264 {
    265 	struct ffb_softc *sc = v;
    266 
    267 	sc->sc_nscreens--;
    268 }
    269 
    270 int
    271 ffb_show_screen(v, cookie, waitok, cb, cbarg)
    272 	void *v;
    273 	void *cookie;
    274 	int waitok;
    275 	void (*cb)(void *, int, int);
    276 	void *cbarg;
    277 {
    278 	return (0);
    279 }
    280 
    281 paddr_t
    282 ffb_mmap(vsc, off, prot)
    283 	void *vsc;
    284 	off_t off;
    285 	int prot;
    286 {
    287 	struct ffb_softc *sc = vsc;
    288 	int i;
    289 
    290 	switch (sc->sc_mode) {
    291 	case WSDISPLAYIO_MODE_MAPPED:
    292 		for (i = 0; i < sc->sc_nreg; i++) {
    293 			/* Before this set? */
    294 			if (off < sc->sc_addrs[i])
    295 				continue;
    296 			/* After this set? */
    297 			if (off >= (sc->sc_addrs[i] + sc->sc_sizes[i]))
    298 				continue;
    299 
    300 			return (bus_space_mmap(sc->sc_bt, sc->sc_addrs[i],
    301 			    off - sc->sc_addrs[i], prot, BUS_SPACE_MAP_LINEAR));
    302 		}
    303 		break;
    304 #ifdef WSDISPLAYIO_MODE_DUMBFB
    305 	case WSDISPLAYIO_MODE_DUMBFB:
    306 		if (sc->sc_nreg < FFB_REG_DFB24)
    307 			break;
    308 		if (off >= 0 && off < sc->sc_sizes[FFB_REG_DFB24])
    309 			return (bus_space_mmap(sc->sc_bt,
    310 			    sc->sc_addrs[FFB_REG_DFB24], off, prot,
    311 			    BUS_SPACE_MAP_LINEAR));
    312 		break;
    313 #endif
    314 	}
    315 
    316 	return (-1);
    317 }
    318 
    319 static int
    320 a2int(char *cp, int deflt)
    321 {
    322 	int i = 0;
    323 
    324 	if (*cp == '\0')
    325 		return (deflt);
    326 	while (*cp != '\0')
    327 		i = i * 10 + *cp++ - '0';
    328 	return (i);
    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