Home | History | Annotate | Line # | Download | only in dev
ffb.c revision 1.17
      1 /*	$NetBSD: ffb.c,v 1.17 2005/05/31 14:36:17 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.17 2005/05/31 14:36:17 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 		ffb_ras_wait(sc);
    619 		sc->copycols(cookie, row, srccol, dstcol, ncols);
    620 	}
    621 }
    622 
    623 void
    624 ffb_ras_setfg(struct ffb_softc *sc, int32_t fg)
    625 {
    626 	ffb_ras_fifo_wait(sc, 1);
    627 	if (fg == sc->sc_fg_cache)
    628 		return;
    629 	sc->sc_fg_cache = fg;
    630 	FBC_WRITE(sc, FFB_FBC_FG, fg);
    631 	ffb_ras_wait(sc);
    632 }
    633 
    634 /* frame buffer generic driver support functions */
    635 static void
    636 ffbfb_unblank(struct device *dev)
    637 {
    638 	/* u_int on = 1; */
    639 
    640 	if (dev && dev->dv_xname)
    641 		printf("%s: ffbfb_unblank\n", dev->dv_xname);
    642 	else
    643 		printf("ffbfb_unblank(%p)n", dev);
    644 	/* ffb_blank((struct ffb_softc*)dev, WSDISPLAYIO_SVIDEO, &on); */
    645 }
    646 
    647 int
    648 ffbfb_open(dev_t dev, int flags, int mode, struct proc *p)
    649 {
    650 	int unit = minor(dev);
    651 
    652 	if (unit >= ffb_cd.cd_ndevs || ffb_cd.cd_devs[unit] == NULL)
    653 		return ENXIO;
    654 
    655 	return 0;
    656 }
    657 
    658 int
    659 ffbfb_close(dev_t dev, int flags, int mode, struct proc *p)
    660 {
    661 	return 0;
    662 }
    663 
    664 int
    665 ffbfb_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
    666 {
    667 	struct ffb_softc *sc = ffb_cd.cd_devs[minor(dev)];
    668 
    669 	return ffb_ioctl(sc, cmd, data, flags, p);
    670 }
    671 
    672 paddr_t
    673 ffbfb_mmap(dev_t dev, off_t off, int prot)
    674 {
    675 	struct ffb_softc *sc = ffb_cd.cd_devs[minor(dev)];
    676 	uint64_t size;
    677 	int i, reg;
    678 	off_t o;
    679 
    680 	/*
    681 	 * off is a magic cookie (see xfree86/drivers/sunffb/ffb.h),
    682 	 * which we map to an index into the "reg" property, and use
    683 	 * our copy of the firmware data as arguments for the real
    684 	 * mapping.
    685 	 */
    686 	static struct { unsigned long voff; int reg; } map[] = {
    687 		{ 0x00000000, FFB_REG_SFB8R },
    688 		{ 0x00400000, FFB_REG_SFB8G },
    689 		{ 0x00800000, FFB_REG_SFB8B },
    690 		{ 0x00c00000, FFB_REG_SFB8X },
    691 		{ 0x01000000, FFB_REG_SFB32 },
    692 		{ 0x02000000, FFB_REG_SFB64  },
    693 		{ 0x04000000, FFB_REG_FBC },
    694 		{ 0x04004000, FFB_REG_DFB8R },
    695 		{ 0x04404000, FFB_REG_DFB8G },
    696 		{ 0x04804000, FFB_REG_DFB8B },
    697 		{ 0x04c04000, FFB_REG_DFB8X },
    698 		{ 0x05004000, FFB_REG_DFB24 },
    699 		{ 0x06004000, FFB_REG_DFB32 },
    700 		{ 0x07004000, FFB_REG_DFB422A },
    701 		{ 0x0bc06000, FFB_REG_DAC },
    702 		{ 0x0bc08000, FFB_REG_PROM },
    703 		{ 0x0bc18000, 0 }
    704 	};
    705 
    706 	/* special value "FFB_EXP_VOFF" - not backed by any "reg" entry */
    707 	if (off == 0x0bc18000)
    708 		return bus_space_mmap(sc->sc_bt, sc->sc_addrs[FFB_REG_PROM],
    709 		    0x00200000, prot, BUS_SPACE_MAP_LINEAR);
    710 
    711 	/*
    712 	 * FFB_VOFF_FBC_KREGS - used by afbinit to upload firmware. We should
    713 	 * probably mmap them only on afb boards
    714 	 */
    715 	if ((off >= 0x0bc04000) && (off < 0x0bc06000))
    716 		return bus_space_mmap(sc->sc_bt, sc->sc_addrs[FFB_REG_PROM],
    717 		    0x00610000 + (off - 0x0bc04000), prot,
    718 		    BUS_SPACE_MAP_LINEAR);
    719 
    720 #define NELEMS(arr) (sizeof(arr)/sizeof((arr)[0]))
    721 
    722 	/* the map is ordered by voff */
    723 	for (i = 0; i < NELEMS(map)-1; i++) {
    724 		reg = map[i].reg;
    725 		/* the number of entries in reg seems to vary */
    726 		if (reg < sc->sc_nreg) {
    727 			size = min((map[i + 1].voff - map[i].voff),
    728 			    sc->sc_sizes[reg]);
    729 			if ((off >= map[i].voff) &&
    730 			    (off < (map[i].voff + size))) {
    731 				o = off - map[i].voff;
    732 				return bus_space_mmap(sc->sc_bt,
    733 				    sc->sc_addrs[reg], o, prot,
    734 				    BUS_SPACE_MAP_LINEAR);
    735 			}
    736 		}
    737 	}
    738 
    739 	return -1;
    740 }
    741 
    742 void
    743 ffb_clearscreen(struct ffb_softc *sc)
    744 {
    745 	struct rasops_info *ri = &ffb_console_screen.ri;
    746 	ffb_ras_fill(sc);
    747 	ffb_ras_setfg(sc, ri->ri_devcmap[WS_DEFAULT_BG]);
    748 	ffb_ras_fifo_wait(sc, 4);
    749 	FBC_WRITE(sc, FFB_FBC_BY, 0);
    750 	FBC_WRITE(sc, FFB_FBC_BX, 0);
    751 	FBC_WRITE(sc, FFB_FBC_BH, ri->ri_height);
    752 	FBC_WRITE(sc, FFB_FBC_BW, ri->ri_width);
    753 }
    754 
    755 void
    756 ffb_switch_screen(struct ffb_softc *sc)
    757 {
    758 	struct ffb_screen *scr, *oldscr;
    759 
    760 	scr = sc->wanted;
    761 	if (!scr) {
    762 		printf("ffb_switch_screen: disappeared\n");
    763 		(*sc->switchcb)(sc->switchcbarg, EIO, 0);
    764 		return;
    765 	}
    766 	oldscr = sc->active; /* can be NULL! */
    767 #ifdef DIAGNOSTIC
    768 	if (oldscr) {
    769 		if (!oldscr->active)
    770 			panic("ffb_switch_screen: not active");
    771 	}
    772 #endif
    773 	if (scr == oldscr)
    774 		return;
    775 
    776 #ifdef DIAGNOSTIC
    777 	if (scr->active)
    778 		panic("ffb_switch_screen: active");
    779 #endif
    780 
    781 	if (oldscr)
    782 		oldscr->active = 0;
    783 #ifdef notyet
    784 	if (sc->currenttype != type) {
    785 		ffb_set_screentype(sc, type);
    786 		sc->currenttype = type;
    787 	}
    788 #endif
    789 
    790 	/* Clear the entire screen. */
    791 
    792 	scr->active = 1;
    793 	ffb_restore_screen(scr, &ffb_stdscreen, scr->chars);
    794 
    795 	sc->active = scr;
    796 
    797 	scr->ri.ri_ops.cursor(scr, scr->cursoron, scr->cursorrow,
    798 	    scr->cursorcol);
    799 
    800 	sc->wanted = 0;
    801 	if (sc->switchcb)
    802 		(*sc->switchcb)(sc->switchcbarg, 0, 0);
    803 }
    804 
    805 void
    806 ffb_restore_screen(struct ffb_screen *scr,
    807     const struct wsscreen_descr *type, u_int16_t *mem)
    808 {
    809 	int i, j, offset = 0;
    810 	uint16_t *charptr = scr->chars;
    811 	long *attrptr = scr->attrs;
    812 
    813 	ffb_clearscreen(scr->sc);
    814 	ffb_ras_wait(scr->sc);
    815 	for (i = 0; i < scr->ri.ri_rows; i++) {
    816 		for (j = 0; j < scr->ri.ri_cols; j++) {
    817 			scr->sc->putchar(scr, i, j, charptr[offset],
    818 			    attrptr[offset]);
    819 			offset++;
    820 		}
    821 	}
    822 	scr->cursordrawn = 0;
    823 }
    824 
    825 void
    826 ffb_cursor(void *cookie, int on, int row, int col)
    827 {
    828 	struct rasops_info *ri = cookie;
    829 	struct ffb_screen *scr = ri->ri_hw;
    830 	struct ffb_softc *sc = scr->sc;
    831 	int x, y, wi, he, coffset;
    832 
    833 	wi = ri->ri_font->fontwidth;
    834 	he = ri->ri_font->fontheight;
    835 
    836 	if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    837 		x = scr->cursorcol * wi + ri->ri_xorigin;
    838 		y = scr->cursorrow * he + ri->ri_yorigin;
    839 
    840 		if (scr->cursordrawn) {
    841 			/* remove cursor */
    842 			coffset = scr->cursorcol +
    843 			    (scr->cursorrow * ri->ri_cols);
    844 			ffb_ras_wait(sc);
    845 			sc->putchar(cookie, scr->cursorrow, scr->cursorcol,
    846 			    scr->chars[coffset], scr->attrs[coffset]);
    847 			scr->cursordrawn = 0;
    848 		}
    849 		scr->cursorrow = row;
    850 		scr->cursorcol = col;
    851 		if ((scr->cursoron = on) != 0)
    852 		{
    853 			long attr, revattr;
    854 			x = scr->cursorcol * wi + ri->ri_xorigin;
    855 			y = scr->cursorrow * he + ri->ri_yorigin;
    856 			coffset = col + (row * ri->ri_cols);
    857 			attr = scr->attrs[coffset];
    858 #ifdef FFB_CURSOR_SWAP_COLOURS
    859 			revattr=((attr >> 8 ) & 0x000f0000) | ((attr &
    860 			    0x000f0000)<<8) | (attr & 0x0000ffff);
    861 #else
    862 			revattr = attr ^ 0xffff0000;
    863 #endif
    864 			ffb_ras_wait(sc);
    865 			sc->putchar(cookie, scr->cursorrow, scr->cursorcol,
    866 			    scr->chars[coffset], revattr);
    867 			scr->cursordrawn = 1;
    868 		}
    869 	} else {
    870 		scr->cursoron = on;
    871 		scr->cursorrow = row;
    872 		scr->cursorcol = col;
    873 		scr->cursordrawn = 0;
    874 	}
    875 }
    876 
    877 void
    878 ffb_putchar(void *cookie, int row, int col, u_int c, long attr)
    879 {
    880 	struct rasops_info *ri = cookie;
    881 	struct ffb_screen *scr = ri->ri_hw;
    882 	struct ffb_softc *sc = scr->sc;
    883 	int pos;
    884 
    885 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
    886 	     (col < ri->ri_cols)) {
    887 		pos = col + row * ri->ri_cols;
    888 		scr->attrs[pos] = attr;
    889 		scr->chars[pos] = c;
    890 
    891 #if 1
    892 		if ((sc->putchar != NULL) && (	scr->active) &&
    893 		    (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    894 			ffb_ras_wait(sc);
    895 			sc->putchar(cookie, row, col, c, attr);
    896 		}
    897 #else
    898 		if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    899 			int fg, bg, uc, i;
    900 			uint8_t *data;
    901 			int x, y, wi,he;
    902 
    903 			wi = ri->ri_font->fontwidth;
    904 			he = ri->ri_font->fontheight;
    905 
    906 			if (!CHAR_IN_FONT(c, ri->ri_font))
    907 				return;
    908 			bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xff];
    909 			fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xff];
    910 			x = ri->ri_xorigin + col * wi;
    911 			y = ri->ri_yorigin + row * he;
    912 			if (c == 0x20) {
    913 				ffb_rectfill(sc, x, y, wi, he, bg);
    914 			} else {
    915 				uc = c-ri->ri_font->firstchar;
    916 				data = (uint8_t *)ri->ri_font->data + uc *
    917 				    ri->ri_fontscale;
    918 
    919 				ffb_setup_mono(sc, x, y, wi, 1, fg, bg);
    920 				for (i = 0; i < he; i++) {
    921 					ffb_feed_line(sc, ri->ri_font->stride,
    922 					    data);
    923 					data += ri->ri_font->stride;
    924 				}
    925 				/*ffb_ras_wait(sc);*/
    926 			}
    927 		}
    928 #endif
    929 	}
    930 }
    931 
    932 int
    933 ffb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
    934 {
    935 	if ((fg == 0) && (bg == 0))
    936 	{
    937 		fg = WS_DEFAULT_FG;
    938 		bg = WS_DEFAULT_BG;
    939 	}
    940 	if (flags & WSATTR_REVERSE) {
    941 		*attrp = (bg & 0xff) << 24 | (fg & 0xff) << 16 |
    942 		    (flags & 0xff);
    943 	} else
    944 		*attrp = (fg & 0xff) << 24 | (bg & 0xff) << 16 |
    945 		    (flags & 0xff);
    946 	return 0;
    947 }
    948 
    949 void
    950 ffb_init_screen(struct ffb_softc *sc, struct ffb_screen *scr,
    951     int existing, long *defattr)
    952 {
    953 	struct rasops_info *ri = &scr->ri;
    954 	int cnt;
    955 
    956 	scr->sc = sc;
    957 	scr->cursorcol = 0;
    958 	scr->cursorrow = 0;
    959 	scr->cursordrawn=0;
    960 
    961 	ri->ri_depth = 32;
    962 	ri->ri_width = sc->sc_width;
    963 	ri->ri_height = sc->sc_height;
    964 	ri->ri_stride = sc->sc_linebytes;
    965 	ri->ri_flg = RI_CENTER;
    966 
    967 	ri->ri_bits = bus_space_vaddr(sc->sc_bt, sc->sc_pixel_h);
    968 
    969 #ifdef DEBUG_FFB
    970 	printf("addr: %08lx\n",(ulong)ri->ri_bits);
    971 #endif
    972 	rasops_init(ri, sc->sc_height/8, sc->sc_width/8);
    973 	ri->ri_caps = WSSCREEN_WSCOLORS;
    974 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    975 		    sc->sc_width / ri->ri_font->fontwidth);
    976 
    977 	ffb_allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG, 0, defattr);
    978 
    979 	/*
    980 	 * we allocate both chars and attributes in one chunk, attributes first
    981 	 * because they have the (potentially) bigger alignment
    982 	 */
    983 	cnt=ri->ri_rows * ri->ri_cols;
    984 	scr->attrs = (long *)malloc(cnt * (sizeof(long) + sizeof(uint16_t)),
    985 	    M_DEVBUF, M_WAITOK);
    986 	scr->chars = (uint16_t *)&scr->attrs[cnt];
    987 
    988 	/* enable acceleration */
    989 	ri->ri_hw = scr;
    990 	ri->ri_ops.copyrows = ffb_ras_copyrows;
    991 	ri->ri_ops.copycols = ffb_ras_copycols;
    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 
   1001 	if (existing) {
   1002 		scr->active = 1;
   1003 	} else {
   1004 		scr->active = 0;
   1005 	}
   1006 
   1007 	ffb_ras_eraserows(&scr->ri, 0, ri->ri_rows, *defattr);
   1008 
   1009 	LIST_INSERT_HEAD(&sc->screens, scr, next);
   1010 }
   1011 
   1012 int
   1013 ffb_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
   1014     int *curxp, int *curyp, long *defattrp)
   1015 {
   1016 	struct ffb_softc *sc = v;
   1017 	struct ffb_screen *scr;
   1018 
   1019 	scr = malloc(sizeof(struct ffb_screen), M_DEVBUF, M_WAITOK | M_ZERO);
   1020 	ffb_init_screen(sc, scr, 0, defattrp);
   1021 
   1022 	if (sc->active == NULL) {
   1023 		scr->active = 1;
   1024 		sc->active = scr;
   1025 		sc->currenttype = type;
   1026 	}
   1027 
   1028 	*cookiep = scr;
   1029 	*curxp = scr->cursorcol;
   1030 	*curyp = scr->cursorrow;
   1031 	return 0;
   1032 }
   1033 
   1034 void
   1035 ffb_free_screen(void *v, void *cookie)
   1036 {
   1037 	struct ffb_softc *sc = v;
   1038 	struct ffb_screen *scr = cookie;
   1039 
   1040 	LIST_REMOVE(scr, next);
   1041 	if (scr != &ffb_console_screen) {
   1042 		free(scr->attrs, M_DEVBUF);
   1043 		free(scr, M_DEVBUF);
   1044 	} else
   1045 		panic("ffb_free_screen: console");
   1046 
   1047 	if (sc->active == scr)
   1048 		sc->active = 0;
   1049 }
   1050 
   1051 int
   1052 ffb_show_screen(void *v, void *cookie, int waitok,
   1053     void (*cb)(void *, int, int), void *cbarg)
   1054 {
   1055 	struct ffb_softc *sc = v;
   1056 	struct ffb_screen *scr, *oldscr;
   1057 
   1058 	scr = cookie;
   1059 	oldscr = sc->active;
   1060 	if (scr == oldscr)
   1061 		return 0;
   1062 
   1063 	sc->wanted = scr;
   1064 	sc->switchcb = cb;
   1065 	sc->switchcbarg = cbarg;
   1066 	if (cb) {
   1067 		callout_reset(&sc->switch_callout, 0,
   1068 		    (void(*)(void *))ffb_switch_screen, sc);
   1069 		return EAGAIN;
   1070 	}
   1071 
   1072 	ffb_switch_screen(sc);
   1073 	return 0;
   1074 }
   1075 
   1076