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