Home | History | Annotate | Line # | Download | only in dev
ffb.c revision 1.22.2.2
      1 /*	$NetBSD: ffb.c,v 1.22.2.2 2006/05/24 10:57:14 yamt 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.22.2.2 2006/05/24 10:57:14 yamt 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 #include <sys/ioctl.h>
     45 #include <sys/malloc.h>
     46 #include <sys/mman.h>
     47 
     48 #include <machine/bus.h>
     49 #include <machine/autoconf.h>
     50 #include <machine/openfirm.h>
     51 #include <machine/vmparam.h>
     52 
     53 #include <dev/wscons/wsconsio.h>
     54 #include <dev/sun/fbio.h>
     55 #include <dev/sun/fbvar.h>
     56 
     57 #include <sparc64/dev/ffbreg.h>
     58 #include <sparc64/dev/ffbvar.h>
     59 
     60 #include <dev/wsfont/wsfont.h>
     61 
     62 #ifndef WS_DEFAULT_BG
     63 /* Sun -> background should be white */
     64 #define WS_DEFAULT_BG 0xf
     65 #endif
     66 
     67 extern struct cfdriver ffb_cd;
     68 
     69 struct wsscreen_descr ffb_stdscreen = {
     70 	"sunffb",
     71 	0, 0,	/* will be filled in -- XXX shouldn't, it's global. */
     72 	0,
     73 	0, 0,
     74 	WSSCREEN_REVERSE | WSSCREEN_WSCOLORS
     75 };
     76 
     77 const struct wsscreen_descr *ffb_scrlist[] = {
     78 	&ffb_stdscreen,
     79 	/* XXX other formats? */
     80 };
     81 
     82 struct wsscreen_list ffb_screenlist = {
     83 	sizeof(ffb_scrlist) / sizeof(struct wsscreen_descr *),
     84 	    ffb_scrlist
     85 };
     86 
     87 static struct ffb_screen ffb_console_screen;
     88 
     89 int	ffb_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
     90 static int ffb_blank(struct ffb_softc *, u_long, u_int *);
     91 paddr_t ffb_mmap(void *, void *, off_t, int);
     92 void	ffb_ras_fifo_wait(struct ffb_softc *, int);
     93 void	ffb_ras_wait(struct ffb_softc *);
     94 void	ffb_ras_init(struct ffb_softc *);
     95 void	ffb_ras_copyrows(void *, int, int, int);
     96 void	ffb_ras_copycols(void *, int, int, int, int);
     97 void	ffb_ras_erasecols(void *, int, int, int, long int);
     98 void	ffb_ras_eraserows(void *, int, int, long int);
     99 void	ffb_ras_do_cursor(struct rasops_info *);
    100 void	ffb_ras_fill(struct ffb_softc *);
    101 void	ffb_ras_setfg(struct ffb_softc *, int32_t);
    102 
    103 int	ffb_alloc_screen(void *, const struct wsscreen_descr *, void **,
    104 	    int *, int *, long *);
    105 void	ffb_free_screen(void *, void *);
    106 int	ffb_show_screen(void *, void *, int, void (*)(void *, int, int),
    107 	    void *);
    108 void	ffb_switch_screen(struct ffb_softc *);
    109 void	ffb_restore_screen(struct ffb_screen *,
    110 	    const struct wsscreen_descr *, uint16_t *);
    111 void	ffb_clearscreen(struct ffb_softc *);
    112 int	ffb_load_font(void *, void *, struct wsdisplay_font *);
    113 void	ffb_init_screen(struct ffb_softc *, struct ffb_screen *, int,
    114 	    long *);
    115 int	ffb_allocattr(void *, int, int, int, long *);
    116 void	ffb_putchar(void *, int, int, u_int, long);
    117 void	ffb_cursor(void *, int, int, int);
    118 
    119 /* frame buffer generic driver */
    120 static void ffbfb_unblank(struct device*);
    121 dev_type_open(ffbfb_open);
    122 dev_type_close(ffbfb_close);
    123 dev_type_ioctl(ffbfb_ioctl);
    124 dev_type_mmap(ffbfb_mmap);
    125 
    126 static struct fbdriver ffb_fbdriver = {
    127         ffbfb_unblank, ffbfb_open, ffbfb_close, ffbfb_ioctl, nopoll,
    128 	ffbfb_mmap, nokqfilter
    129 };
    130 
    131 struct wsdisplay_accessops ffb_accessops = {
    132 	ffb_ioctl,
    133 	ffb_mmap,
    134 	ffb_alloc_screen,
    135 	ffb_free_screen,
    136 	ffb_show_screen,
    137 	NULL,	/* load font */
    138 	NULL,	/* pollc */
    139 	NULL,	/* scroll */
    140 };
    141 
    142 void
    143 ffb_attach(struct ffb_softc *sc)
    144 {
    145 	struct wsemuldisplaydev_attach_args waa;
    146 	struct rasops_info *ri;
    147 	long defattr;
    148 	const char *model;
    149 	int btype;
    150 	int maxrow, maxcol;
    151 	u_int blank = WSDISPLAYIO_VIDEO_ON;
    152 	char buf[6+1];
    153 
    154 	printf(":");
    155 
    156 	sc->putchar = NULL;
    157 	sc->copycols = NULL;
    158 
    159 	if (sc->sc_type == FFB_CREATOR) {
    160 		btype = prom_getpropint(sc->sc_node, "board_type", 0);
    161 		if ((btype & 7) == 3)
    162 			printf(" Creator3D");
    163 		else
    164 			printf(" Creator");
    165 	} else
    166 		printf(" Elite3D");
    167 
    168 	model = prom_getpropstring(sc->sc_node, "model");
    169 	if (model == NULL || strlen(model) == 0)
    170 		model = "unknown";
    171 
    172 	sc->sc_depth = 24;
    173 	sc->sc_linebytes = 8192;
    174 	sc->sc_height = prom_getpropint(sc->sc_node, "height", 0);
    175 	sc->sc_width = prom_getpropint(sc->sc_node, "width", 0);
    176 
    177 	maxcol = (prom_getoption("screen-#columns", buf, sizeof buf) == 0)
    178 		? strtoul(buf, NULL, 10)
    179 		: 80;
    180 
    181 	maxrow = (prom_getoption("screen-#rows", buf, sizeof buf) != 0)
    182 		? strtoul(buf, NULL, 10)
    183 		: 34;
    184 
    185 	ffb_ras_init(sc);
    186 
    187 	/* collect DAC version, as Elite3D cursor enable bit is reversed */
    188 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_GVERS);
    189 	sc->sc_dacrev = DAC_READ(sc, FFB_DAC_VALUE) >> 28;
    190 
    191 	if (sc->sc_type == FFB_AFB)
    192 		sc->sc_dacrev = 10;
    193 	printf(", model %s, dac %u\n", model, sc->sc_dacrev);
    194 
    195 	ffb_blank(sc, WSDISPLAYIO_SVIDEO, &blank);
    196 
    197 	sc->sc_accel = ((device_cfdata(&sc->sc_dv)->cf_flags &
    198 	    FFB_CFFLAG_NOACCEL) == 0);
    199 
    200 	wsfont_init();
    201 
    202 	/* we mess with ffb_console_screen only once */
    203 	if (sc->sc_console) {
    204 		ffb_init_screen(sc, &ffb_console_screen, 1, &defattr);
    205 		ffb_console_screen.active = 1;
    206 		sc->active = &ffb_console_screen;
    207 	}
    208 	ri = &ffb_console_screen.ri;
    209 
    210 	ffb_stdscreen.nrows = ri->ri_rows;
    211 	ffb_stdscreen.ncols = ri->ri_cols;
    212 	ffb_stdscreen.textops = &ri->ri_ops;
    213 	ffb_stdscreen.capabilities = ri->ri_caps;
    214 
    215 	sc->sc_fb.fb_driver = &ffb_fbdriver;
    216 	sc->sc_fb.fb_type.fb_cmsize = 0;
    217 	sc->sc_fb.fb_type.fb_size = maxrow * sc->sc_linebytes;
    218 	sc->sc_fb.fb_type.fb_type = FBTYPE_CREATOR;
    219 	sc->sc_fb.fb_type.fb_width = sc->sc_width;
    220 	sc->sc_fb.fb_type.fb_depth = sc->sc_depth;
    221 	sc->sc_fb.fb_type.fb_height = sc->sc_height;
    222 	sc->sc_fb.fb_device = &sc->sc_dv;
    223 	fb_attach(&sc->sc_fb, sc->sc_console);
    224 
    225 	if (sc->sc_console) {
    226 		wsdisplay_cnattach(&ffb_stdscreen, ri, 0, 0, defattr);
    227 	}
    228 
    229 	ffb_clearscreen(sc);
    230 
    231 	waa.console = sc->sc_console;
    232 	waa.scrdata = &ffb_screenlist;
    233 	waa.accessops = &ffb_accessops;
    234 	waa.accesscookie = sc;
    235 	config_found(&sc->sc_dv, &waa, wsemuldisplaydevprint);
    236 }
    237 
    238 int
    239 ffb_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flags, struct lwp *l)
    240 {
    241 	struct ffb_softc *sc = v;
    242 	struct wsdisplay_fbinfo *wdf;
    243 	struct ffb_screen *ms = sc->active;
    244 
    245 #ifdef FFBDEBUG
    246 	printf("ffb_ioctl: %s cmd _IO%s%s('%c', %lu)\n",
    247 	       sc->sc_dv.dv_xname,
    248 	       (cmd & IOC_IN) ? "W" : "", (cmd & IOC_OUT) ? "R" : "",
    249 	       (char)IOCGROUP(cmd), cmd & 0xff);
    250 #endif
    251 
    252 	switch (cmd) {
    253 	case FBIOGTYPE:
    254 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    255 		break;
    256 	case FBIOGATTR:
    257 #define fba ((struct fbgattr *)data)
    258 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    259 		fba->owner = 0; 	/* XXX ??? */
    260 		fba->fbtype = sc->sc_fb.fb_type;
    261 		fba->sattr.flags = 0;
    262 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    263 		fba->sattr.dev_specific[0] = -1;
    264 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    265 		fba->emu_types[1] = -1;
    266 #undef fba
    267 		break;
    268 
    269 	case FBIOGETCMAP:
    270 	case FBIOPUTCMAP:
    271 		return EIO;
    272 
    273 	case FBIOGVIDEO:
    274 	case FBIOSVIDEO:
    275 		return ffb_blank(sc, cmd == FBIOGVIDEO?
    276 		    WSDISPLAYIO_GVIDEO : WSDISPLAYIO_SVIDEO,
    277 		    (u_int *)data);
    278 		break;
    279 	case FBIOGCURSOR:
    280 	case FBIOSCURSOR:
    281 		/* the console driver is not using the hardware cursor */
    282 		break;
    283 	case FBIOGCURPOS:
    284 		printf("%s: FBIOGCURPOS not implemented\n", sc->sc_dv.dv_xname);
    285 		return EIO;
    286 	case FBIOSCURPOS:
    287 		printf("%s: FBIOSCURPOS not implemented\n", sc->sc_dv.dv_xname);
    288 		return EIO;
    289 	case FBIOGCURMAX:
    290 		printf("%s: FBIOGCURMAX not implemented\n", sc->sc_dv.dv_xname);
    291 		return EIO;
    292 
    293 	case WSDISPLAYIO_GTYPE:
    294 		*(u_int *)data = WSDISPLAY_TYPE_SUNFFB;
    295 		break;
    296 	case WSDISPLAYIO_SMODE:
    297 		{
    298 			if (sc->sc_mode != *(u_int *)data) {
    299 				sc->sc_mode = *(u_int *)data;
    300 				if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    301 					ffb_ras_init(sc);
    302 					ffb_restore_screen(ms, ms->type,
    303 					    ms->chars);
    304 					ffb_cursor(ms, ms->cursoron,
    305 					    ms->cursorrow,
    306 					    ms->cursorcol);
    307 				}
    308 			}
    309 		}
    310 		break;
    311 	case WSDISPLAYIO_GINFO:
    312 		wdf = (void *)data;
    313 		wdf->height = sc->sc_height;
    314 		wdf->width  = sc->sc_width;
    315 		wdf->depth  = 32;
    316 		wdf->cmsize = 256; /* XXX */
    317 		break;
    318 #ifdef WSDISPLAYIO_LINEBYTES
    319 	case WSDISPLAYIO_LINEBYTES:
    320 		*(u_int *)data = sc->sc_linebytes;
    321 		break;
    322 #endif
    323 	case WSDISPLAYIO_GETCMAP:
    324 		break;/* XXX */
    325 
    326 	case WSDISPLAYIO_PUTCMAP:
    327 		break;/* XXX */
    328 
    329 	case WSDISPLAYIO_SVIDEO:
    330 	case WSDISPLAYIO_GVIDEO:
    331 		return(ffb_blank(sc, cmd, (u_int *)data));
    332 		break;
    333 	case WSDISPLAYIO_GCURPOS:
    334 	case WSDISPLAYIO_SCURPOS:
    335 	case WSDISPLAYIO_GCURMAX:
    336 	case WSDISPLAYIO_GCURSOR:
    337 	case WSDISPLAYIO_SCURSOR:
    338 		return EIO; /* not supported yet */
    339 	default:
    340 		return EPASSTHROUGH;
    341         }
    342 
    343 	return (0);
    344 }
    345 
    346 /* blank/unblank the screen */
    347 static int
    348 ffb_blank(struct ffb_softc *sc, u_long cmd, u_int *data)
    349 {
    350 	u_int val;
    351 
    352 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_GSBLANK);
    353 	val = DAC_READ(sc, FFB_DAC_VALUE);
    354 
    355 	switch (cmd) {
    356 	case WSDISPLAYIO_GVIDEO:
    357 		*data = val & 1;
    358 		return(0);
    359 		break;
    360 	case WSDISPLAYIO_SVIDEO:
    361 		if (*data == WSDISPLAYIO_VIDEO_OFF)
    362 			val &= ~1;
    363 		else if (*data == WSDISPLAYIO_VIDEO_ON)
    364 			val |= 1;
    365 		else
    366 			return(EINVAL);
    367 		break;
    368 	default:
    369 		return(EINVAL);
    370 	}
    371 
    372 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_GSBLANK);
    373 	DAC_WRITE(sc, FFB_DAC_VALUE, val);
    374 
    375 	return(0);
    376 }
    377 
    378 paddr_t
    379 ffb_mmap(void *vsc, void *vs, off_t off, int prot)
    380 {
    381 	struct ffb_softc *sc = vsc;
    382 	int i;
    383 
    384 	switch (sc->sc_mode) {
    385 	case WSDISPLAYIO_MODE_MAPPED:
    386 		for (i = 0; i < sc->sc_nreg; i++) {
    387 			/* Before this set? */
    388 			if (off < sc->sc_addrs[i])
    389 				continue;
    390 			/* After this set? */
    391 			if (off >= (sc->sc_addrs[i] + sc->sc_sizes[i]))
    392 				continue;
    393 
    394 			return (bus_space_mmap(sc->sc_bt, sc->sc_addrs[i],
    395 			    off - sc->sc_addrs[i], prot, BUS_SPACE_MAP_LINEAR));
    396 		}
    397 		break;
    398 #ifdef WSDISPLAYIO_MODE_DUMBFB
    399 	case WSDISPLAYIO_MODE_DUMBFB:
    400 		if (sc->sc_nreg < FFB_REG_DFB24)
    401 			break;
    402 		if (off >= 0 && off < sc->sc_sizes[FFB_REG_DFB24])
    403 			return (bus_space_mmap(sc->sc_bt,
    404 			    sc->sc_addrs[FFB_REG_DFB24], off, prot,
    405 			    BUS_SPACE_MAP_LINEAR));
    406 		break;
    407 #endif
    408 	}
    409 
    410 	return (-1);
    411 }
    412 
    413 void
    414 ffb_ras_fifo_wait(struct ffb_softc *sc, int n)
    415 {
    416 	int32_t cache = sc->sc_fifo_cache;
    417 
    418 	if (cache < n) {
    419 		do {
    420 			cache = FBC_READ(sc, FFB_FBC_UCSR);
    421 			cache = (cache & FBC_UCSR_FIFO_MASK) - 8;
    422 		} while (cache < n);
    423 	}
    424 	sc->sc_fifo_cache = cache - n;
    425 }
    426 
    427 void
    428 ffb_ras_wait(struct ffb_softc *sc)
    429 {
    430 	uint32_t ucsr, r;
    431 
    432 	while (1) {
    433 		ucsr = FBC_READ(sc, FFB_FBC_UCSR);
    434 		if ((ucsr & (FBC_UCSR_FB_BUSY|FBC_UCSR_RP_BUSY)) == 0)
    435 			break;
    436 		r = ucsr & (FBC_UCSR_READ_ERR | FBC_UCSR_FIFO_OVFL);
    437 		if (r != 0)
    438 			FBC_WRITE(sc, FFB_FBC_UCSR, r);
    439 	}
    440 }
    441 
    442 void
    443 ffb_ras_init(struct ffb_softc *sc)
    444 {
    445 	ffb_ras_fifo_wait(sc, 7);
    446 	FBC_WRITE(sc, FFB_FBC_PPC,
    447 	    FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE |
    448 	    FBC_PPC_APE_DIS | FBC_PPC_CS_CONST);
    449 	FBC_WRITE(sc, FFB_FBC_FBC,
    450 	    FFB_FBC_WB_A | FFB_FBC_RB_A | FFB_FBC_SB_BOTH |
    451 	    FFB_FBC_XE_OFF | FFB_FBC_RGBE_MASK);
    452 	FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW);
    453 	FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE);
    454 	FBC_WRITE(sc, FFB_FBC_PMASK, 0xffffffff);
    455 	FBC_WRITE(sc, FFB_FBC_FONTINC, 0x10000);
    456 	sc->sc_fg_cache = 0;
    457 	FBC_WRITE(sc, FFB_FBC_FG, sc->sc_fg_cache);
    458 	ffb_ras_wait(sc);
    459 }
    460 
    461 void
    462 ffb_ras_eraserows(void *cookie, int row, int n, long attr)
    463 {
    464 	struct rasops_info *ri = cookie;
    465 	struct ffb_screen *scr = ri->ri_hw;
    466 	struct ffb_softc *sc = scr->sc;;
    467 
    468 	int start, end, i;
    469 
    470 	start = ri->ri_cols * row;
    471 	end = ri->ri_cols * (row + n);
    472 
    473 	for (i = start; i < end; i++) {
    474 		scr->attrs[i] = attr;
    475 		scr->chars[i] = 0x20;
    476 	}
    477 
    478 	if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    479 		if (row < 0) {
    480 			n += row;
    481 			row = 0;
    482 		}
    483 		if (row + n > ri->ri_rows)
    484 			n = ri->ri_rows - row;
    485 		if (n <= 0)
    486 			return;
    487 
    488 		ffb_ras_fill(sc);
    489 		ffb_ras_setfg(sc, ri->ri_devcmap[(attr >> 16) & 0xf]);
    490 		ffb_ras_fifo_wait(sc, 4);
    491 		if ((n == ri->ri_rows) && (ri->ri_flg & RI_FULLCLEAR)) {
    492 			FBC_WRITE(sc, FFB_FBC_BY, 0);
    493 			FBC_WRITE(sc, FFB_FBC_BX, 0);
    494 			FBC_WRITE(sc, FFB_FBC_BH, ri->ri_height);
    495 			FBC_WRITE(sc, FFB_FBC_BW, ri->ri_width);
    496 		} else {
    497 			row *= ri->ri_font->fontheight;
    498 			FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + row);
    499 			FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin);
    500 			FBC_WRITE(sc, FFB_FBC_BH, n * ri->ri_font->fontheight);
    501 			FBC_WRITE(sc, FFB_FBC_BW, ri->ri_emuwidth);
    502 		}
    503 		ffb_ras_wait(sc);
    504 	}
    505 }
    506 
    507 void
    508 ffb_ras_erasecols(void *cookie, int row, int col, int n, long attr)
    509 {
    510 	struct rasops_info *ri = cookie;
    511 	struct ffb_screen *scr = ri->ri_hw;
    512 	struct ffb_softc *sc = scr->sc;;
    513 
    514 	int start = col + row * ri->ri_cols;
    515 	int end = start + n, i;
    516 
    517 	for (i = start; i < end; i++) {
    518 		scr->attrs[i] = attr;
    519 		scr->chars[i] = 0x20;
    520 	}
    521 	if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    522 
    523 		if ((row < 0) || (row >= ri->ri_rows))
    524 			return;
    525 		if (col < 0) {
    526 			n += col;
    527 			col = 0;
    528 		}
    529 		if (col + n > ri->ri_cols)
    530 			n = ri->ri_cols - col;
    531 		if (n <= 0)
    532 			return;
    533 		n *= ri->ri_font->fontwidth;
    534 		col *= ri->ri_font->fontwidth;
    535 		row *= ri->ri_font->fontheight;
    536 
    537 		ffb_ras_fill(sc);
    538 		ffb_ras_setfg(sc, ri->ri_devcmap[(attr >> 16) & 0xf]);
    539 		ffb_ras_fifo_wait(sc, 4);
    540 		FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + row);
    541 		FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin + col);
    542 		FBC_WRITE(sc, FFB_FBC_BH, ri->ri_font->fontheight);
    543 		FBC_WRITE(sc, FFB_FBC_BW, n - 1);
    544 		ffb_ras_wait(sc);
    545 	}
    546 }
    547 
    548 void
    549 ffb_ras_fill(struct ffb_softc *sc)
    550 {
    551 	ffb_ras_fifo_wait(sc, 2);
    552 	FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW);
    553 	FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE);
    554 	ffb_ras_wait(sc);
    555 }
    556 
    557 void
    558 ffb_ras_copyrows(void *cookie, int src, int dst, int n)
    559 {
    560 	struct rasops_info *ri = cookie;
    561 	struct ffb_screen *scr = ri->ri_hw;
    562 	struct ffb_softc *sc = scr->sc;
    563 
    564 	int from, to, len;
    565 
    566 	from = ri->ri_cols * src;
    567 	to = ri->ri_cols * dst;
    568 	len = ri->ri_cols * n;
    569 
    570 	memmove(&scr->attrs[to], &scr->attrs[from], len * sizeof(long));
    571 	memmove(&scr->chars[to], &scr->chars[from], len * sizeof(uint16_t));
    572 
    573 	if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    574 
    575 		if (dst == src)
    576 			return;
    577 		if (src < 0) {
    578 			n += src;
    579 			src = 0;
    580 		}
    581 		if ((src + n) > ri->ri_rows)
    582 			n = ri->ri_rows - src;
    583 		if (dst < 0) {
    584 			n += dst;
    585 			dst = 0;
    586 		}
    587 		if ((dst + n) > ri->ri_rows)
    588 			n = ri->ri_rows - dst;
    589 		if (n <= 0)
    590 			return;
    591 		n *= ri->ri_font->fontheight;
    592 		src *= ri->ri_font->fontheight;
    593 		dst *= ri->ri_font->fontheight;
    594 
    595 		ffb_ras_fifo_wait(sc, 8);
    596 		FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_OLD | (FBC_ROP_OLD << 8));
    597 		FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_VSCROLL);
    598 		FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + src);
    599 		FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin);
    600 		FBC_WRITE(sc, FFB_FBC_DY, ri->ri_yorigin + dst);
    601 		FBC_WRITE(sc, FFB_FBC_DX, ri->ri_xorigin);
    602 		FBC_WRITE(sc, FFB_FBC_BH, n);
    603 		FBC_WRITE(sc, FFB_FBC_BW, ri->ri_emuwidth);
    604 		ffb_ras_wait(sc);
    605 	}
    606 }
    607 
    608 void
    609 ffb_ras_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    610 {
    611 	struct rasops_info *ri = cookie;
    612 	struct ffb_screen *scr = ri->ri_hw;
    613 	struct ffb_softc *sc = scr->sc;
    614 	int from = srccol + row * ri->ri_cols;
    615 	int to = dstcol + row * ri->ri_cols;
    616 
    617 	memmove(&scr->attrs[to], &scr->attrs[from], ncols * sizeof(long));
    618 	memmove(&scr->chars[to], &scr->chars[from], ncols * sizeof(uint16_t));
    619 
    620 	if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    621 		ffb_ras_wait(sc);
    622 		sc->copycols(cookie, row, srccol, dstcol, ncols);
    623 	}
    624 }
    625 
    626 void
    627 ffb_ras_setfg(struct ffb_softc *sc, int32_t fg)
    628 {
    629 	ffb_ras_fifo_wait(sc, 1);
    630 	if (fg == sc->sc_fg_cache)
    631 		return;
    632 	sc->sc_fg_cache = fg;
    633 	FBC_WRITE(sc, FFB_FBC_FG, fg);
    634 	ffb_ras_wait(sc);
    635 }
    636 
    637 /* frame buffer generic driver support functions */
    638 static void
    639 ffbfb_unblank(struct device *dev)
    640 {
    641 	/* u_int on = 1; */
    642 
    643 	if (dev && dev->dv_xname)
    644 		printf("%s: ffbfb_unblank\n", dev->dv_xname);
    645 	else
    646 		printf("ffbfb_unblank(%p)n", dev);
    647 	/* ffb_blank((struct ffb_softc*)dev, WSDISPLAYIO_SVIDEO, &on); */
    648 }
    649 
    650 int
    651 ffbfb_open(dev_t dev, int flags, int mode, struct lwp *l)
    652 {
    653 	int unit = minor(dev);
    654 
    655 	if (unit >= ffb_cd.cd_ndevs || ffb_cd.cd_devs[unit] == NULL)
    656 		return ENXIO;
    657 
    658 	return 0;
    659 }
    660 
    661 int
    662 ffbfb_close(dev_t dev, int flags, int mode, struct lwp *l)
    663 {
    664 	return 0;
    665 }
    666 
    667 int
    668 ffbfb_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct lwp *l)
    669 {
    670 	struct ffb_softc *sc = ffb_cd.cd_devs[minor(dev)];
    671 
    672 	return ffb_ioctl(sc, NULL, cmd, data, flags, l);
    673 }
    674 
    675 paddr_t
    676 ffbfb_mmap(dev_t dev, off_t off, int prot)
    677 {
    678 	struct ffb_softc *sc = ffb_cd.cd_devs[minor(dev)];
    679 	uint64_t size;
    680 	int i, reg;
    681 	off_t o;
    682 
    683 	/*
    684 	 * off is a magic cookie (see xfree86/drivers/sunffb/ffb.h),
    685 	 * which we map to an index into the "reg" property, and use
    686 	 * our copy of the firmware data as arguments for the real
    687 	 * mapping.
    688 	 */
    689 	static struct { unsigned long voff; int reg; } map[] = {
    690 		{ 0x00000000, FFB_REG_SFB8R },
    691 		{ 0x00400000, FFB_REG_SFB8G },
    692 		{ 0x00800000, FFB_REG_SFB8B },
    693 		{ 0x00c00000, FFB_REG_SFB8X },
    694 		{ 0x01000000, FFB_REG_SFB32 },
    695 		{ 0x02000000, FFB_REG_SFB64  },
    696 		{ 0x04000000, FFB_REG_FBC },
    697 		{ 0x04004000, FFB_REG_DFB8R },
    698 		{ 0x04404000, FFB_REG_DFB8G },
    699 		{ 0x04804000, FFB_REG_DFB8B },
    700 		{ 0x04c04000, FFB_REG_DFB8X },
    701 		{ 0x05004000, FFB_REG_DFB24 },
    702 		{ 0x06004000, FFB_REG_DFB32 },
    703 		{ 0x07004000, FFB_REG_DFB422A },
    704 		{ 0x0bc06000, FFB_REG_DAC },
    705 		{ 0x0bc08000, FFB_REG_PROM },
    706 		{ 0x0bc18000, 0 }
    707 	};
    708 
    709 	/* special value "FFB_EXP_VOFF" - not backed by any "reg" entry */
    710 	if (off == 0x0bc18000)
    711 		return bus_space_mmap(sc->sc_bt, sc->sc_addrs[FFB_REG_PROM],
    712 		    0x00200000, prot, BUS_SPACE_MAP_LINEAR);
    713 
    714 	/*
    715 	 * FFB_VOFF_FBC_KREGS - used by afbinit to upload firmware. We should
    716 	 * probably mmap them only on afb boards
    717 	 */
    718 	if ((off >= 0x0bc04000) && (off < 0x0bc06000))
    719 		return bus_space_mmap(sc->sc_bt, sc->sc_addrs[FFB_REG_PROM],
    720 		    0x00610000 + (off - 0x0bc04000), prot,
    721 		    BUS_SPACE_MAP_LINEAR);
    722 
    723 #define NELEMS(arr) (sizeof(arr)/sizeof((arr)[0]))
    724 
    725 	/* the map is ordered by voff */
    726 	for (i = 0; i < NELEMS(map)-1; i++) {
    727 		reg = map[i].reg;
    728 		/* the number of entries in reg seems to vary */
    729 		if (reg < sc->sc_nreg) {
    730 			size = min((map[i + 1].voff - map[i].voff),
    731 			    sc->sc_sizes[reg]);
    732 			if ((off >= map[i].voff) &&
    733 			    (off < (map[i].voff + size))) {
    734 				o = off - map[i].voff;
    735 				return bus_space_mmap(sc->sc_bt,
    736 				    sc->sc_addrs[reg], o, prot,
    737 				    BUS_SPACE_MAP_LINEAR);
    738 			}
    739 		}
    740 	}
    741 
    742 	return -1;
    743 }
    744 
    745 void
    746 ffb_clearscreen(struct ffb_softc *sc)
    747 {
    748 	struct rasops_info *ri = &ffb_console_screen.ri;
    749 	ffb_ras_fill(sc);
    750 	ffb_ras_setfg(sc, ri->ri_devcmap[WS_DEFAULT_BG]);
    751 	ffb_ras_fifo_wait(sc, 4);
    752 	FBC_WRITE(sc, FFB_FBC_BY, 0);
    753 	FBC_WRITE(sc, FFB_FBC_BX, 0);
    754 	FBC_WRITE(sc, FFB_FBC_BH, ri->ri_height);
    755 	FBC_WRITE(sc, FFB_FBC_BW, ri->ri_width);
    756 }
    757 
    758 void
    759 ffb_switch_screen(struct ffb_softc *sc)
    760 {
    761 	struct ffb_screen *scr, *oldscr;
    762 
    763 	scr = sc->wanted;
    764 	if (!scr) {
    765 		printf("ffb_switch_screen: disappeared\n");
    766 		(*sc->switchcb)(sc->switchcbarg, EIO, 0);
    767 		return;
    768 	}
    769 	oldscr = sc->active; /* can be NULL! */
    770 #ifdef DIAGNOSTIC
    771 	if (oldscr) {
    772 		if (!oldscr->active)
    773 			panic("ffb_switch_screen: not active");
    774 	}
    775 #endif
    776 	if (scr == oldscr)
    777 		return;
    778 
    779 #ifdef DIAGNOSTIC
    780 	if (scr->active)
    781 		panic("ffb_switch_screen: active");
    782 #endif
    783 
    784 	if (oldscr)
    785 		oldscr->active = 0;
    786 #ifdef notyet
    787 	if (sc->currenttype != type) {
    788 		ffb_set_screentype(sc, type);
    789 		sc->currenttype = type;
    790 	}
    791 #endif
    792 
    793 	/* Clear the entire screen. */
    794 
    795 	scr->active = 1;
    796 	ffb_restore_screen(scr, &ffb_stdscreen, scr->chars);
    797 
    798 	sc->active = scr;
    799 
    800 	scr->ri.ri_ops.cursor(scr, scr->cursoron, scr->cursorrow,
    801 	    scr->cursorcol);
    802 
    803 	sc->wanted = 0;
    804 	if (sc->switchcb)
    805 		(*sc->switchcb)(sc->switchcbarg, 0, 0);
    806 }
    807 
    808 void
    809 ffb_restore_screen(struct ffb_screen *scr,
    810     const struct wsscreen_descr *type, uint16_t *mem)
    811 {
    812 	int i, j, offset = 0;
    813 	uint16_t *charptr = scr->chars;
    814 	long *attrptr = scr->attrs;
    815 
    816 	ffb_clearscreen(scr->sc);
    817 	ffb_ras_wait(scr->sc);
    818 	for (i = 0; i < scr->ri.ri_rows; i++) {
    819 		for (j = 0; j < scr->ri.ri_cols; j++) {
    820 			scr->sc->putchar(scr, i, j, charptr[offset],
    821 			    attrptr[offset]);
    822 			offset++;
    823 		}
    824 	}
    825 	scr->cursordrawn = 0;
    826 }
    827 
    828 void
    829 ffb_cursor(void *cookie, int on, int row, int col)
    830 {
    831 	struct rasops_info *ri = cookie;
    832 	struct ffb_screen *scr = ri->ri_hw;
    833 	struct ffb_softc *sc = scr->sc;
    834 	int x, y, wi, he, coffset;
    835 
    836 	wi = ri->ri_font->fontwidth;
    837 	he = ri->ri_font->fontheight;
    838 
    839 	if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    840 		x = scr->cursorcol * wi + ri->ri_xorigin;
    841 		y = scr->cursorrow * he + ri->ri_yorigin;
    842 
    843 		if (scr->cursordrawn) {
    844 			/* remove cursor */
    845 			coffset = scr->cursorcol +
    846 			    (scr->cursorrow * ri->ri_cols);
    847 			ffb_ras_wait(sc);
    848 			sc->putchar(cookie, scr->cursorrow, scr->cursorcol,
    849 			    scr->chars[coffset], scr->attrs[coffset]);
    850 			scr->cursordrawn = 0;
    851 		}
    852 		scr->cursorrow = row;
    853 		scr->cursorcol = col;
    854 		if ((scr->cursoron = on) != 0)
    855 		{
    856 			long attr, revattr;
    857 			x = scr->cursorcol * wi + ri->ri_xorigin;
    858 			y = scr->cursorrow * he + ri->ri_yorigin;
    859 			coffset = col + (row * ri->ri_cols);
    860 			attr = scr->attrs[coffset];
    861 #ifdef FFB_CURSOR_SWAP_COLOURS
    862 			revattr=((attr >> 8 ) & 0x000f0000) | ((attr &
    863 			    0x000f0000)<<8) | (attr & 0x0000ffff);
    864 #else
    865 			revattr = attr ^ 0xffff0000;
    866 #endif
    867 			ffb_ras_wait(sc);
    868 			sc->putchar(cookie, scr->cursorrow, scr->cursorcol,
    869 			    scr->chars[coffset], revattr);
    870 			scr->cursordrawn = 1;
    871 		}
    872 	} else {
    873 		scr->cursoron = on;
    874 		scr->cursorrow = row;
    875 		scr->cursorcol = col;
    876 		scr->cursordrawn = 0;
    877 	}
    878 }
    879 
    880 void
    881 ffb_putchar(void *cookie, int row, int col, u_int c, long attr)
    882 {
    883 	struct rasops_info *ri = cookie;
    884 	struct ffb_screen *scr = ri->ri_hw;
    885 	struct ffb_softc *sc = scr->sc;
    886 	int pos;
    887 
    888 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
    889 	     (col < ri->ri_cols)) {
    890 		pos = col + row * ri->ri_cols;
    891 		scr->attrs[pos] = attr;
    892 		scr->chars[pos] = c;
    893 
    894 #if 1
    895 		if ((sc->putchar != NULL) && (	scr->active) &&
    896 		    (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    897 			ffb_ras_wait(sc);
    898 			sc->putchar(cookie, row, col, c, attr);
    899 		}
    900 #else
    901 		if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    902 			int fg, bg, uc, i;
    903 			uint8_t *data;
    904 			int x, y, wi,he;
    905 
    906 			wi = ri->ri_font->fontwidth;
    907 			he = ri->ri_font->fontheight;
    908 
    909 			if (!CHAR_IN_FONT(c, ri->ri_font))
    910 				return;
    911 			bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xff];
    912 			fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xff];
    913 			x = ri->ri_xorigin + col * wi;
    914 			y = ri->ri_yorigin + row * he;
    915 			if (c == 0x20) {
    916 				ffb_rectfill(sc, x, y, wi, he, bg);
    917 			} else {
    918 				uc = c-ri->ri_font->firstchar;
    919 				data = (uint8_t *)ri->ri_font->data + uc *
    920 				    ri->ri_fontscale;
    921 
    922 				ffb_setup_mono(sc, x, y, wi, 1, fg, bg);
    923 				for (i = 0; i < he; i++) {
    924 					ffb_feed_line(sc, ri->ri_font->stride,
    925 					    data);
    926 					data += ri->ri_font->stride;
    927 				}
    928 				/*ffb_ras_wait(sc);*/
    929 			}
    930 		}
    931 #endif
    932 	}
    933 }
    934 
    935 int
    936 ffb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
    937 {
    938 	if ((fg == 0) && (bg == 0))
    939 	{
    940 		fg = WS_DEFAULT_FG;
    941 		bg = WS_DEFAULT_BG;
    942 	}
    943 	if (flags & WSATTR_REVERSE) {
    944 		*attrp = (bg & 0xff) << 24 | (fg & 0xff) << 16 |
    945 		    (flags & 0xff);
    946 	} else
    947 		*attrp = (fg & 0xff) << 24 | (bg & 0xff) << 16 |
    948 		    (flags & 0xff);
    949 	return 0;
    950 }
    951 
    952 void
    953 ffb_init_screen(struct ffb_softc *sc, struct ffb_screen *scr,
    954     int existing, long *defattr)
    955 {
    956 	struct rasops_info *ri = &scr->ri;
    957 	int cnt;
    958 
    959 	scr->sc = sc;
    960 	scr->cursorcol = 0;
    961 	scr->cursorrow = 0;
    962 	scr->cursordrawn=0;
    963 
    964 	ri->ri_depth = 32;
    965 	ri->ri_width = sc->sc_width;
    966 	ri->ri_height = sc->sc_height;
    967 	ri->ri_stride = sc->sc_linebytes;
    968 	ri->ri_flg = RI_CENTER;
    969 
    970 	ri->ri_bits = bus_space_vaddr(sc->sc_bt, sc->sc_pixel_h);
    971 
    972 #ifdef DEBUG_FFB
    973 	printf("addr: %08lx\n",(ulong)ri->ri_bits);
    974 #endif
    975 	rasops_init(ri, sc->sc_height/8, sc->sc_width/8);
    976 	ri->ri_caps = WSSCREEN_WSCOLORS;
    977 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    978 		    sc->sc_width / ri->ri_font->fontwidth);
    979 
    980 	ffb_allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG, 0, defattr);
    981 
    982 	/*
    983 	 * we allocate both chars and attributes in one chunk, attributes first
    984 	 * because they have the (potentially) bigger alignment
    985 	 */
    986 	cnt=ri->ri_rows * ri->ri_cols;
    987 	scr->attrs = (long *)malloc(cnt * (sizeof(long) + sizeof(uint16_t)),
    988 	    M_DEVBUF, M_WAITOK);
    989 	scr->chars = (uint16_t *)&scr->attrs[cnt];
    990 
    991 	/* enable acceleration */
    992 	ri->ri_hw = scr;
    993 	ri->ri_ops.copyrows = ffb_ras_copyrows;
    994 	ri->ri_ops.eraserows = ffb_ras_eraserows;
    995 	ri->ri_ops.erasecols = ffb_ras_erasecols;
    996 	ri->ri_ops.cursor = ffb_cursor;
    997 	ri->ri_ops.allocattr = ffb_allocattr;
    998 	if (sc->putchar == NULL)
    999 		sc->putchar = ri->ri_ops.putchar;
   1000 		sc->copycols = ri->ri_ops.copycols;
   1001 	ri->ri_ops.putchar = ffb_putchar;
   1002 	ri->ri_ops.copycols = ffb_ras_copycols;
   1003 
   1004 
   1005 	if (existing) {
   1006 		scr->active = 1;
   1007 	} else {
   1008 		scr->active = 0;
   1009 	}
   1010 
   1011 	ffb_ras_eraserows(&scr->ri, 0, ri->ri_rows, *defattr);
   1012 
   1013 	LIST_INSERT_HEAD(&sc->screens, scr, next);
   1014 }
   1015 
   1016 int
   1017 ffb_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
   1018     int *curxp, int *curyp, long *defattrp)
   1019 {
   1020 	struct ffb_softc *sc = v;
   1021 	struct ffb_screen *scr;
   1022 
   1023 	scr = malloc(sizeof(struct ffb_screen), M_DEVBUF, M_WAITOK | M_ZERO);
   1024 	ffb_init_screen(sc, scr, 0, defattrp);
   1025 
   1026 	if (sc->active == NULL) {
   1027 		scr->active = 1;
   1028 		sc->active = scr;
   1029 		sc->currenttype = type;
   1030 	}
   1031 
   1032 	*cookiep = scr;
   1033 	*curxp = scr->cursorcol;
   1034 	*curyp = scr->cursorrow;
   1035 	return 0;
   1036 }
   1037 
   1038 void
   1039 ffb_free_screen(void *v, void *cookie)
   1040 {
   1041 	struct ffb_softc *sc = v;
   1042 	struct ffb_screen *scr = cookie;
   1043 
   1044 	LIST_REMOVE(scr, next);
   1045 	if (scr != &ffb_console_screen) {
   1046 		free(scr->attrs, M_DEVBUF);
   1047 		free(scr, M_DEVBUF);
   1048 	} else
   1049 		panic("ffb_free_screen: console");
   1050 
   1051 	if (sc->active == scr)
   1052 		sc->active = 0;
   1053 }
   1054 
   1055 int
   1056 ffb_show_screen(void *v, void *cookie, int waitok,
   1057     void (*cb)(void *, int, int), void *cbarg)
   1058 {
   1059 	struct ffb_softc *sc = v;
   1060 	struct ffb_screen *scr, *oldscr;
   1061 
   1062 	scr = cookie;
   1063 	oldscr = sc->active;
   1064 	if (scr == oldscr)
   1065 		return 0;
   1066 
   1067 	sc->wanted = scr;
   1068 	sc->switchcb = cb;
   1069 	sc->switchcbarg = cbarg;
   1070 	if (cb) {
   1071 		callout_reset(&sc->switch_callout, 0,
   1072 		    (void(*)(void *))ffb_switch_screen, sc);
   1073 		return EAGAIN;
   1074 	}
   1075 
   1076 	ffb_switch_screen(sc);
   1077 	return 0;
   1078 }
   1079 
   1080