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