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