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