Home | History | Annotate | Line # | Download | only in voyager
voyagerfb.c revision 1.32
      1 /*	$NetBSD: voyagerfb.c,v 1.32 2021/05/21 20:23:35 macallan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009, 2011 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * A console driver for Silicon Motion SM502 / Voyager GX  graphics controllers
     30  * tested on GDIUM only so far
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: voyagerfb.c,v 1.32 2021/05/21 20:23:35 macallan Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #include <sys/lwp.h>
     42 #include <sys/kauth.h>
     43 
     44 #include <dev/videomode/videomode.h>
     45 
     46 #include <dev/pci/pcivar.h>
     47 #include <dev/pci/pcireg.h>
     48 #include <dev/pci/pcidevs.h>
     49 #include <dev/pci/pciio.h>
     50 #include <dev/ic/sm502reg.h>
     51 
     52 #include <dev/wscons/wsdisplayvar.h>
     53 #include <dev/wscons/wsconsio.h>
     54 #include <dev/wsfont/wsfont.h>
     55 #include <dev/rasops/rasops.h>
     56 #include <dev/wscons/wsdisplay_vconsvar.h>
     57 #include <dev/pci/wsdisplay_pci.h>
     58 
     59 #include <dev/i2c/i2cvar.h>
     60 #include <dev/pci/voyagervar.h>
     61 #include <dev/wscons/wsdisplay_glyphcachevar.h>
     62 
     63 #include "opt_voyagerfb.h"
     64 
     65 #ifdef VOYAGERFB_DEBUG
     66 #define DPRINTF aprint_error
     67 #else
     68 #define DPRINTF while (0) printf
     69 #endif
     70 
     71 /* XXX these are gdium-specific */
     72 #define GPIO_BACKLIGHT	0x20000000
     73 
     74 struct voyagerfb_softc {
     75 	device_t sc_dev;
     76 
     77 	pci_chipset_tag_t sc_pc;
     78 	pcitag_t sc_pcitag;
     79 	bus_space_tag_t sc_memt;
     80 
     81 	bus_space_handle_t sc_fbh;
     82 	bus_space_handle_t sc_regh;
     83 	bus_addr_t sc_fb, sc_reg;
     84 	bus_size_t sc_fbsize, sc_regsize;
     85 
     86 	int sc_width, sc_height, sc_depth, sc_stride;
     87 	int sc_locked;
     88 	void *sc_fbaddr;
     89 	struct vcons_screen sc_console_screen;
     90 	struct wsscreen_descr sc_defaultscreen_descr;
     91 	const struct wsscreen_descr *sc_screens[1];
     92 	struct wsscreen_list sc_screenlist;
     93 	struct vcons_data vd;
     94 	uint8_t *sc_dataport;
     95 	int sc_mode;
     96 	int sc_bl_on, sc_bl_level;
     97 	void *sc_gpio_cookie;
     98 
     99 	/* cursor stuff */
    100 	int sc_cur_x;
    101 	int sc_cur_y;
    102 	int sc_hot_x;
    103 	int sc_hot_y;
    104 	uint32_t sc_cursor_addr;
    105 	uint32_t *sc_cursor;
    106 
    107 	/* colour map */
    108 	u_char sc_cmap_red[256];
    109 	u_char sc_cmap_green[256];
    110 	u_char sc_cmap_blue[256];
    111 
    112 	glyphcache sc_gc;
    113 };
    114 
    115 static int	voyagerfb_match(device_t, cfdata_t, void *);
    116 static void	voyagerfb_attach(device_t, device_t, void *);
    117 
    118 CFATTACH_DECL_NEW(voyagerfb, sizeof(struct voyagerfb_softc),
    119     voyagerfb_match, voyagerfb_attach, NULL, NULL);
    120 
    121 extern const u_char rasops_cmap[768];
    122 
    123 static int	voyagerfb_ioctl(void *, void *, u_long, void *, int,
    124 			     struct lwp *);
    125 static paddr_t	voyagerfb_mmap(void *, void *, off_t, int);
    126 static void	voyagerfb_init_screen(void *, struct vcons_screen *, int,
    127 		 long *);
    128 
    129 static int	voyagerfb_putcmap(struct voyagerfb_softc *,
    130 		 struct wsdisplay_cmap *);
    131 static int 	voyagerfb_getcmap(struct voyagerfb_softc *,
    132 		 struct wsdisplay_cmap *);
    133 static void	voyagerfb_restore_palette(struct voyagerfb_softc *);
    134 static int 	voyagerfb_putpalreg(struct voyagerfb_softc *, int, uint8_t,
    135 			    uint8_t, uint8_t);
    136 
    137 static void	voyagerfb_init(struct voyagerfb_softc *);
    138 
    139 static void	voyagerfb_rectfill(struct voyagerfb_softc *, int, int, int, int,
    140 			    uint32_t);
    141 static void	voyagerfb_bitblt(void *, int, int, int, int,
    142 			    int, int, int);
    143 
    144 static void	voyagerfb_cursor(void *, int, int, int);
    145 static void	voyagerfb_putchar_mono(void *, int, int, u_int, long);
    146 static void	voyagerfb_putchar_aa32(void *, int, int, u_int, long);
    147 static void	voyagerfb_putchar_aa8(void *, int, int, u_int, long);
    148 static void	voyagerfb_copycols(void *, int, int, int, int);
    149 static void	voyagerfb_erasecols(void *, int, int, int, long);
    150 static void	voyagerfb_copyrows(void *, int, int, int);
    151 static void	voyagerfb_eraserows(void *, int, int, long);
    152 
    153 static int	voyagerfb_set_curpos(struct voyagerfb_softc *, int, int);
    154 static int	voyagerfb_gcursor(struct voyagerfb_softc *,
    155 		 struct wsdisplay_cursor *);
    156 static int	voyagerfb_scursor(struct voyagerfb_softc *,
    157 		 struct wsdisplay_cursor *);
    158 
    159 struct wsdisplay_accessops voyagerfb_accessops = {
    160 	voyagerfb_ioctl,
    161 	voyagerfb_mmap,
    162 	NULL,	/* alloc_screen */
    163 	NULL,	/* free_screen */
    164 	NULL,	/* show_screen */
    165 	NULL, 	/* load_font */
    166 	NULL,	/* pollc */
    167 	NULL	/* scroll */
    168 };
    169 
    170 static void	voyagerfb_setup_backlight(struct voyagerfb_softc *);
    171 static void	voyagerfb_brightness_up(device_t);
    172 static void	voyagerfb_brightness_down(device_t);
    173 /* set backlight level */
    174 static void	voyagerfb_set_backlight(struct voyagerfb_softc *, int);
    175 /* turn backlight on and off without messing with the level */
    176 static void	voyagerfb_switch_backlight(struct voyagerfb_softc *, int);
    177 
    178 /* wait for FIFO empty so we can feed it another command */
    179 static inline void
    180 voyagerfb_ready(struct voyagerfb_softc *sc)
    181 {
    182 	do {} while ((bus_space_read_4(sc->sc_memt, sc->sc_regh,
    183 	    SM502_SYSTEM_CTRL) & SM502_SYSCTL_FIFO_EMPTY) == 0);
    184 }
    185 
    186 /* wait for the drawing engine to be idle */
    187 static inline void
    188 voyagerfb_wait(struct voyagerfb_softc *sc)
    189 {
    190 	do {} while ((bus_space_read_4(sc->sc_memt, sc->sc_regh,
    191 	    SM502_SYSTEM_CTRL) & SM502_SYSCTL_ENGINE_BUSY) != 0);
    192 }
    193 
    194 static int
    195 voyagerfb_match(device_t parent, cfdata_t match, void *aux)
    196 {
    197 	struct voyager_attach_args *vaa = (struct voyager_attach_args *)aux;
    198 
    199 	if (strcmp(vaa->vaa_name, "voyagerfb") == 0) return 100;
    200 	return 0;
    201 }
    202 
    203 static void
    204 voyagerfb_attach(device_t parent, device_t self, void *aux)
    205 {
    206 	struct voyagerfb_softc	*sc = device_private(self);
    207 	struct voyager_attach_args	*vaa = aux;
    208 	struct rasops_info	*ri;
    209 	struct wsemuldisplaydev_attach_args aa;
    210 	prop_dictionary_t	dict;
    211 	unsigned long		defattr;
    212 	uint32_t		reg;
    213 	bool			is_console;
    214 	int i, j;
    215 	uint8_t			cmap[768];
    216 
    217 	sc->sc_pc = vaa->vaa_pc;
    218 	sc->sc_pcitag = vaa->vaa_pcitag;
    219 	sc->sc_memt = vaa->vaa_tag;
    220 	sc->sc_dev = self;
    221 
    222 	aprint_normal("\n");
    223 
    224 	dict = device_properties(self);
    225 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    226 
    227 	sc->sc_fb = vaa->vaa_mem_pa;
    228 	sc->sc_fbh = vaa->vaa_memh;
    229 	sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
    230 
    231 	sc->sc_reg = vaa->vaa_reg_pa;
    232 	sc->sc_regh = vaa->vaa_regh;
    233 	sc->sc_regsize = 2 * 1024 * 1024;
    234 	sc->sc_dataport = bus_space_vaddr(sc->sc_memt, sc->sc_regh);
    235 	sc->sc_dataport += SM502_DATAPORT;
    236 
    237 	sc->sc_gpio_cookie = device_private(parent);
    238 
    239 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_DRAM_CONTROL);
    240 	switch(reg & 0x0000e000) {
    241 		case SM502_MEM_2M:
    242 			sc->sc_fbsize = 2 * 1024 * 1024;
    243 			break;
    244 		case SM502_MEM_4M:
    245 			sc->sc_fbsize = 4 * 1024 * 1024;
    246 			break;
    247 		case SM502_MEM_8M:
    248 			sc->sc_fbsize = 8 * 1024 * 1024;
    249 			break;
    250 		case SM502_MEM_16M:
    251 			sc->sc_fbsize = 16 * 1024 * 1024;
    252 			break;
    253 		case SM502_MEM_32M:
    254 			sc->sc_fbsize = 32 * 1024 * 1024;
    255 			break;
    256 		case SM502_MEM_64M:
    257 			sc->sc_fbsize = 64 * 1024 * 1024;
    258 			break;
    259 	}
    260 
    261 	sc->sc_width = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    262 		SM502_PANEL_FB_WIDTH) & SM502_FBW_WIN_WIDTH_MASK) >> 16;
    263 	sc->sc_height = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    264 		SM502_PANEL_FB_HEIGHT) & SM502_FBH_WIN_HEIGHT_MASK) >> 16;
    265 
    266 #ifdef VOYAGERFB_DEPTH_32
    267 	sc->sc_depth = 32;
    268 #else
    269 	sc->sc_depth = 8;
    270 #endif
    271 
    272 	/*
    273 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
    274 	 * but as far as I know there are no SM502 with 64bit BARs
    275 	 */
    276 	aprint_normal_dev(self, "%d MB video memory at 0x%08x\n",
    277 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    278 
    279 	/* init engine here */
    280 	voyagerfb_init(sc);
    281 
    282 	aprint_normal_dev(self, "%d x %d, %d bit, stride %d\n",
    283 		sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
    284 
    285 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    286 		"default",
    287 		0, 0,
    288 		NULL,
    289 		8, 16,
    290 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    291 		WSSCREEN_RESIZE,
    292 		NULL
    293 	};
    294 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    295 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    296 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    297 	sc->sc_locked = 0;
    298 
    299 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    300 	    &voyagerfb_accessops);
    301 	sc->vd.init_screen = voyagerfb_init_screen;
    302 	sc->vd.show_screen_cookie = &sc->sc_gc;
    303 	sc->vd.show_screen_cb = glyphcache_adapt;
    304 
    305 	/* backlight control */
    306 	voyagerfb_setup_backlight(sc);
    307 
    308 	ri = &sc->sc_console_screen.scr_ri;
    309 
    310 	sc->sc_gc.gc_bitblt = voyagerfb_bitblt;
    311 	sc->sc_gc.gc_blitcookie = sc;
    312 	sc->sc_gc.gc_rop = ROP_COPY;
    313 	if (is_console) {
    314 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    315 		    &defattr);
    316 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    317 
    318 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    319 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    320 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    321 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    322 	} else {
    323 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
    324 			/* do some minimal setup to avoid weirdness later */
    325 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    326 			    &defattr);
    327 		} else
    328 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    329 	}
    330 	glyphcache_init(&sc->sc_gc, sc->sc_height,
    331 			((sc->sc_fbsize - 16 * 64) / sc->sc_stride) -
    332 			    sc->sc_height,
    333 			sc->sc_width,
    334 			ri->ri_font->fontwidth,
    335 			ri->ri_font->fontheight,
    336 			defattr);
    337 	if (is_console)
    338 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    339 		    defattr);
    340 
    341 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    342 	j = 0;
    343 	if (sc->sc_depth <= 8) {
    344 		for (i = 0; i < 256; i++) {
    345 
    346 			sc->sc_cmap_red[i] = cmap[j];
    347 			sc->sc_cmap_green[i] = cmap[j + 1];
    348 			sc->sc_cmap_blue[i] = cmap[j + 2];
    349 			voyagerfb_putpalreg(sc, i, cmap[j], cmap[j + 1],
    350 			    cmap[j + 2]);
    351 			j += 3;
    352 		}
    353 	}
    354 
    355 	voyagerfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    356 	    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    357 
    358 	if (is_console)
    359 		vcons_replay_msgbuf(&sc->sc_console_screen);
    360 
    361 	aa.console = is_console;
    362 	aa.scrdata = &sc->sc_screenlist;
    363 	aa.accessops = &voyagerfb_accessops;
    364 	aa.accesscookie = &sc->vd;
    365 
    366 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint,
    367 	    CFARG_IATTR, "wsemuldisplaydev",
    368 	    CFARG_EOL);
    369 }
    370 
    371 static int
    372 voyagerfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    373 	struct lwp *l)
    374 {
    375 	struct vcons_data *vd = v;
    376 	struct voyagerfb_softc *sc = vd->cookie;
    377 	struct wsdisplay_fbinfo *wdf;
    378 	struct vcons_screen *ms = vd->active;
    379 	struct wsdisplay_param  *param;
    380 
    381 	switch (cmd) {
    382 	case WSDISPLAYIO_GTYPE:
    383 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    384 		return 0;
    385 
    386 	/* PCI config read/write pass through. */
    387 	case PCI_IOC_CFGREAD:
    388 	case PCI_IOC_CFGWRITE:
    389 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    390 		    cmd, data, flag, l);
    391 
    392 	case WSDISPLAYIO_GET_BUSID:
    393 		return wsdisplayio_busid_pci(device_parent(sc->sc_dev),
    394 		    sc->sc_pc, sc->sc_pcitag, data);
    395 
    396 	case WSDISPLAYIO_GINFO:
    397 		if (ms == NULL)
    398 			return ENODEV;
    399 		wdf = (void *)data;
    400 		wdf->height = ms->scr_ri.ri_height;
    401 		wdf->width = ms->scr_ri.ri_width;
    402 		wdf->depth = 32;
    403 		wdf->cmsize = 256;
    404 		return 0;
    405 
    406 	case WSDISPLAYIO_GETCMAP:
    407 		return voyagerfb_getcmap(sc,
    408 		    (struct wsdisplay_cmap *)data);
    409 
    410 	case WSDISPLAYIO_PUTCMAP:
    411 		return voyagerfb_putcmap(sc,
    412 		    (struct wsdisplay_cmap *)data);
    413 
    414 	case WSDISPLAYIO_LINEBYTES:
    415 		*(u_int *)data = sc->sc_stride;
    416 		return 0;
    417 
    418 	case WSDISPLAYIO_SMODE: {
    419 		int new_mode = *(int*)data;
    420 		if (new_mode != sc->sc_mode) {
    421 			sc->sc_mode = new_mode;
    422 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    423 #ifdef VOYAGERFB_DEPTH_32
    424 				sc->sc_depth = 32;
    425 #else
    426 				sc->sc_depth = 8;
    427 #endif
    428 				glyphcache_wipe(&sc->sc_gc);
    429 				voyagerfb_init(sc);
    430 				voyagerfb_restore_palette(sc);
    431 				vcons_redraw_screen(ms);
    432 			} else {
    433 				sc->sc_depth = 32;
    434 				voyagerfb_init(sc);
    435 			}
    436 		}
    437 		}
    438 		return 0;
    439 
    440 	case WSDISPLAYIO_GVIDEO:
    441 		*(int *)data = sc->sc_bl_on ? WSDISPLAYIO_VIDEO_ON :
    442 					      WSDISPLAYIO_VIDEO_OFF;
    443 		return 0;
    444 
    445 	case WSDISPLAYIO_SVIDEO: {
    446 			int new_bl = *(int *)data;
    447 
    448 			voyagerfb_switch_backlight(sc,  new_bl);
    449 		}
    450 		return 0;
    451 
    452 	case WSDISPLAYIO_GETPARAM:
    453 		param = (struct wsdisplay_param *)data;
    454 		switch (param->param) {
    455 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    456 			param->min = 0;
    457 			param->max = 255;
    458 			param->curval = sc->sc_bl_level;
    459 			return 0;
    460 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    461 			param->min = 0;
    462 			param->max = 1;
    463 			param->curval = sc->sc_bl_on;
    464 			return 0;
    465 		}
    466 		return EPASSTHROUGH;
    467 
    468 	case WSDISPLAYIO_SETPARAM:
    469 		param = (struct wsdisplay_param *)data;
    470 		switch (param->param) {
    471 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    472 			voyagerfb_set_backlight(sc, param->curval);
    473 			return 0;
    474 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    475 			voyagerfb_switch_backlight(sc,  param->curval);
    476 			return 0;
    477 		}
    478 		return EPASSTHROUGH;
    479 
    480 	case WSDISPLAYIO_GCURPOS:
    481 		{
    482 			struct wsdisplay_curpos *pos;
    483 
    484 			pos = (struct wsdisplay_curpos *)data;
    485 			pos->x = sc->sc_cur_x;
    486 			pos->y = sc->sc_cur_y;
    487 		}
    488 		return 0;
    489 
    490 	case WSDISPLAYIO_SCURPOS:
    491 		{
    492 			struct wsdisplay_curpos *pos;
    493 
    494 			pos = (struct wsdisplay_curpos *)data;
    495 			voyagerfb_set_curpos(sc, pos->x, pos->y);
    496 		}
    497 		return 0;
    498 
    499 	case WSDISPLAYIO_GCURMAX:
    500 		{
    501 			struct wsdisplay_curpos *pos;
    502 
    503 			pos = (struct wsdisplay_curpos *)data;
    504 			pos->x = 64;
    505 			pos->y = 64;
    506 		}
    507 		return 0;
    508 
    509 	case WSDISPLAYIO_GCURSOR:
    510 		{
    511 			struct wsdisplay_cursor *cu;
    512 
    513 			cu = (struct wsdisplay_cursor *)data;
    514 			return voyagerfb_gcursor(sc, cu);
    515 		}
    516 
    517 	case WSDISPLAYIO_SCURSOR:
    518 		{
    519 			struct wsdisplay_cursor *cu;
    520 
    521 			cu = (struct wsdisplay_cursor *)data;
    522 			return voyagerfb_scursor(sc, cu);
    523 		}
    524 
    525 	case WSDISPLAYIO_GET_FBINFO:
    526 		{
    527 			struct wsdisplayio_fbinfo *fbi = data;
    528 			return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    529 		}
    530 	}
    531 	return EPASSTHROUGH;
    532 }
    533 
    534 static paddr_t
    535 voyagerfb_mmap(void *v, void *vs, off_t offset, int prot)
    536 {
    537 	struct vcons_data *vd = v;
    538 	struct voyagerfb_softc *sc = vd->cookie;
    539 	paddr_t pa;
    540 
    541 	/* 'regular' framebuffer mmap()ing */
    542 	if (offset < sc->sc_fbsize) {
    543 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    544 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    545 		return pa;
    546 	}
    547 
    548 	/*
    549 	 * restrict all other mappings to processes with privileges
    550 	 */
    551 	if (kauth_authorize_machdep(kauth_cred_get(),
    552 	    KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0) {
    553 		aprint_normal("%s: mmap() rejected.\n",
    554 		    device_xname(sc->sc_dev));
    555 		return -1;
    556 	}
    557 
    558 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    559 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    560 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    561 		return pa;
    562 	}
    563 
    564 	if ((offset >= sc->sc_reg) &&
    565 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    566 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 0);
    567 		return pa;
    568 	}
    569 
    570 	return -1;
    571 }
    572 
    573 static void
    574 voyagerfb_init_screen(void *cookie, struct vcons_screen *scr,
    575     int existing, long *defattr)
    576 {
    577 	struct voyagerfb_softc *sc = cookie;
    578 	struct rasops_info *ri = &scr->scr_ri;
    579 
    580 	ri->ri_depth = sc->sc_depth;
    581 	ri->ri_width = sc->sc_width;
    582 	ri->ri_height = sc->sc_height;
    583 	ri->ri_stride = sc->sc_stride;
    584 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    585 
    586 	ri->ri_bits = (char *)sc->sc_fbaddr;
    587 
    588 	if (existing) {
    589 		ri->ri_flg |= RI_CLEAR;
    590 	}
    591 
    592 	if (sc->sc_depth == 8) {
    593 		ri->ri_flg |= RI_8BIT_IS_RGB;
    594 #ifdef VOYAGERFB_ANTIALIAS
    595 		ri->ri_flg |= RI_ENABLE_ALPHA | RI_PREFER_ALPHA;
    596 #endif
    597 	}
    598 	if (sc->sc_depth == 32) {
    599 #ifdef VOYAGERFB_ANTIALIAS
    600 		ri->ri_flg |= RI_ENABLE_ALPHA;
    601 #endif
    602 		/* we always run in RGB */
    603 		ri->ri_rnum = 8;
    604 		ri->ri_gnum = 8;
    605 		ri->ri_bnum = 8;
    606 		ri->ri_rpos = 16;
    607 		ri->ri_gpos = 8;
    608 		ri->ri_bpos = 0;
    609 	}
    610 
    611 	scr->scr_flags |= VCONS_LOADFONT;
    612 
    613 	rasops_init(ri, 0, 0);
    614 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    615 		      WSSCREEN_RESIZE;
    616 
    617 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    618 		    sc->sc_width / ri->ri_font->fontwidth);
    619 
    620 	ri->ri_hw = scr;
    621 	ri->ri_ops.copyrows = voyagerfb_copyrows;
    622 	ri->ri_ops.copycols = voyagerfb_copycols;
    623 	ri->ri_ops.eraserows = voyagerfb_eraserows;
    624 	ri->ri_ops.erasecols = voyagerfb_erasecols;
    625 	ri->ri_ops.cursor = voyagerfb_cursor;
    626 	if (FONT_IS_ALPHA(ri->ri_font)) {
    627 	        switch (sc->sc_depth) {
    628 	                case 32:
    629                 		ri->ri_ops.putchar = voyagerfb_putchar_aa32;
    630                 		break;
    631                         case 8:
    632                                 ri->ri_ops.putchar = voyagerfb_putchar_aa8;
    633                                 break;
    634                         default:
    635                                 printf("alpha font at %d!?\n", sc->sc_depth);
    636                 }
    637 	} else
    638 		ri->ri_ops.putchar = voyagerfb_putchar_mono;
    639 }
    640 
    641 static int
    642 voyagerfb_putcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
    643 {
    644 	u_char *r, *g, *b;
    645 	u_int index = cm->index;
    646 	u_int count = cm->count;
    647 	int i, error;
    648 	u_char rbuf[256], gbuf[256], bbuf[256];
    649 
    650 #ifdef VOYAGERFB_DEBUG
    651 	aprint_debug("putcmap: %d %d\n",index, count);
    652 #endif
    653 	if (cm->index >= 256 || cm->count > 256 ||
    654 	    (cm->index + cm->count) > 256)
    655 		return EINVAL;
    656 	error = copyin(cm->red, &rbuf[index], count);
    657 	if (error)
    658 		return error;
    659 	error = copyin(cm->green, &gbuf[index], count);
    660 	if (error)
    661 		return error;
    662 	error = copyin(cm->blue, &bbuf[index], count);
    663 	if (error)
    664 		return error;
    665 
    666 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    667 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    668 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    669 
    670 	r = &sc->sc_cmap_red[index];
    671 	g = &sc->sc_cmap_green[index];
    672 	b = &sc->sc_cmap_blue[index];
    673 
    674 	for (i = 0; i < count; i++) {
    675 		voyagerfb_putpalreg(sc, index, *r, *g, *b);
    676 		index++;
    677 		r++, g++, b++;
    678 	}
    679 	return 0;
    680 }
    681 
    682 static int
    683 voyagerfb_getcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
    684 {
    685 	u_int index = cm->index;
    686 	u_int count = cm->count;
    687 	int error;
    688 
    689 	if (index >= 255 || count > 256 || index + count > 256)
    690 		return EINVAL;
    691 
    692 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    693 	if (error)
    694 		return error;
    695 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    696 	if (error)
    697 		return error;
    698 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    699 	if (error)
    700 		return error;
    701 
    702 	return 0;
    703 }
    704 
    705 static void
    706 voyagerfb_restore_palette(struct voyagerfb_softc *sc)
    707 {
    708 	int i;
    709 
    710 	for (i = 0; i < 256; i++) {
    711 		voyagerfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    712 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    713 	}
    714 }
    715 
    716 static int
    717 voyagerfb_putpalreg(struct voyagerfb_softc *sc, int idx, uint8_t r,
    718     uint8_t g, uint8_t b)
    719 {
    720 	uint32_t reg;
    721 
    722 	reg = (r << 16) | (g << 8) | b;
    723 	/* XXX we should probably write the CRT palette too */
    724 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    725 	    SM502_PALETTE_PANEL + (idx << 2), reg);
    726 	return 0;
    727 }
    728 
    729 static void
    730 voyagerfb_init(struct voyagerfb_softc *sc)
    731 {
    732 	int reg;
    733 
    734 	voyagerfb_wait(sc);
    735 	/* disable colour compare */
    736 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_COLOR_COMP_MASK, 0);
    737 	/* allow writes to all planes */
    738 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PLANEMASK,
    739 	    0xffffffff);
    740 	/* disable clipping */
    741 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CLIP_TOP_LEFT, 0);
    742 	/* source and destination in local memory, no offset */
    743 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC_BASE, 0);
    744 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST_BASE, 0);
    745 	/* pitch is screen stride */
    746 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PITCH,
    747 	    sc->sc_width | (sc->sc_width << 16));
    748 	/* window is screen width */
    749 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_WINDOW_WIDTH,
    750 	    sc->sc_width | (sc->sc_width << 16));
    751 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL);
    752 	reg &= ~SM502_PDC_DEPTH_MASK;
    753 
    754 	switch (sc->sc_depth) {
    755 		case 8:
    756 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    757 			    SM502_STRETCH, SM502_STRETCH_8BIT);
    758 			sc->sc_stride = sc->sc_width;
    759 			reg |= SM502_PDC_8BIT;
    760 			break;
    761 		case 16:
    762 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    763 			    SM502_STRETCH, SM502_STRETCH_16BIT);
    764 			sc->sc_stride = sc->sc_width << 1;
    765 			reg |= SM502_PDC_16BIT;
    766 			break;
    767 		case 24:
    768 		case 32:
    769 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    770 			    SM502_STRETCH, SM502_STRETCH_32BIT);
    771 			sc->sc_stride = sc->sc_width << 2;
    772 			reg |= SM502_PDC_32BIT;
    773 			break;
    774 	}
    775 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_FB_OFFSET,
    776 	    (sc->sc_stride << 16) | sc->sc_stride);
    777 
    778 	/* clear the screen... */
    779 	voyagerfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, 0);
    780 
    781 	/* ...and then switch colour depth. For aesthetic reasons. */
    782 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL,
    783 	    reg);
    784 
    785 	/* put the cursor at the end of video memory */
    786 	sc->sc_cursor_addr = sc->sc_fbsize - 16 * 64;	/* XXX */
    787 	DPRINTF("%s: %08x\n", __func__, sc->sc_cursor_addr);
    788 	sc->sc_cursor = (uint32_t *)((uint8_t *)bus_space_vaddr(sc->sc_memt,
    789 			 sc->sc_fbh) + sc->sc_cursor_addr);
    790 #ifdef VOYAGERFB_DEBUG
    791 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_XY,
    792 							 0x00100010);
    793 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_COL12,
    794 							 0x0000ffff);
    795 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_COL3,
    796 							 0x0000f800);
    797 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_ADDR,
    798 	    SM502_CRSR_ENABLE | sc->sc_cursor_addr);
    799 	sc->sc_cursor[0] = 0x00000000;
    800 	sc->sc_cursor[1] = 0x00000000;
    801 	sc->sc_cursor[2] = 0xffffffff;
    802 	sc->sc_cursor[3] = 0xffffffff;
    803 	sc->sc_cursor[4] = 0xaaaaaaaa;
    804 	sc->sc_cursor[5] = 0xaaaaaaaa;
    805 	sc->sc_cursor[6] = 0xffffffff;
    806 	sc->sc_cursor[7] = 0x00000000;
    807 #else
    808 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_ADDR,
    809 	    sc->sc_cursor_addr);
    810 #endif
    811 }
    812 
    813 static void
    814 voyagerfb_rectfill(struct voyagerfb_softc *sc, int x, int y, int wi, int he,
    815      uint32_t colour)
    816 {
    817 
    818 	voyagerfb_ready(sc);
    819 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL,
    820 	    ROP_COPY |
    821 	    SM502_CTRL_USE_ROP2 |
    822 	    SM502_CTRL_CMD_RECTFILL |
    823 	    SM502_CTRL_QUICKSTART_E);
    824 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND,
    825 	    colour);
    826 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
    827 	    (x << 16) | y);
    828 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
    829 	    (wi << 16) | he);
    830 }
    831 
    832 static void
    833 voyagerfb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
    834     int wi, int he, int rop)
    835 {
    836 	struct voyagerfb_softc *sc = cookie;
    837 	uint32_t cmd;
    838 
    839 	cmd = (rop & 0xf) | SM502_CTRL_USE_ROP2 | SM502_CTRL_CMD_BITBLT |
    840 	      SM502_CTRL_QUICKSTART_E;
    841 
    842 	voyagerfb_ready(sc);
    843 
    844 	if (xd <= xs) {
    845 		/* left to right */
    846 	} else {
    847 		/*
    848 		 * According to the manual this flag reverses only the blitter's
    849 		 * X direction. At least on my Gdium it also reverses the Y
    850 		 * direction
    851 		 */
    852 		cmd |= SM502_CTRL_R_TO_L;
    853 		xs += wi - 1;
    854 		xd += wi - 1;
    855 		ys += he - 1;
    856 		yd += he - 1;
    857 	}
    858 
    859 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
    860 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC,
    861 	    (xs << 16) | ys);
    862 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
    863 	    (xd << 16) | yd);
    864 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
    865 	    (wi << 16) | he);
    866 }
    867 
    868 static void
    869 voyagerfb_cursor(void *cookie, int on, int row, int col)
    870 {
    871 	struct rasops_info *ri = cookie;
    872 	struct vcons_screen *scr = ri->ri_hw;
    873 	struct voyagerfb_softc *sc = scr->scr_cookie;
    874 	int x, y, wi, he;
    875 
    876 	wi = ri->ri_font->fontwidth;
    877 	he = ri->ri_font->fontheight;
    878 
    879 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    880 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    881 		y = ri->ri_crow * he + ri->ri_yorigin;
    882 		if (ri->ri_flg & RI_CURSOR) {
    883 			voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
    884 			ri->ri_flg &= ~RI_CURSOR;
    885 		}
    886 		ri->ri_crow = row;
    887 		ri->ri_ccol = col;
    888 		if (on) {
    889 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    890 			y = ri->ri_crow * he + ri->ri_yorigin;
    891 			voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
    892 			ri->ri_flg |= RI_CURSOR;
    893 		}
    894 	} else {
    895 		scr->scr_ri.ri_crow = row;
    896 		scr->scr_ri.ri_ccol = col;
    897 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    898 	}
    899 
    900 }
    901 
    902 static inline void
    903 voyagerfb_feed8(struct voyagerfb_softc *sc, uint8_t *data, int len)
    904 {
    905 	uint32_t *port = (uint32_t *)sc->sc_dataport;
    906 	int i;
    907 
    908 	for (i = 0; i < ((len + 3) & 0xfffc); i++) {
    909 		*port = *data;
    910 		data++;
    911 	}
    912 }
    913 
    914 static inline void
    915 voyagerfb_feed16(struct voyagerfb_softc *sc, uint16_t *data, int len)
    916 {
    917 	uint32_t *port = (uint32_t *)sc->sc_dataport;
    918 	int i;
    919 
    920 	len = len << 1;
    921 	for (i = 0; i < ((len + 1) & 0xfffe); i++) {
    922 		*port = *data;
    923 		data++;
    924 	}
    925 }
    926 
    927 static void
    928 voyagerfb_putchar_mono(void *cookie, int row, int col, u_int c, long attr)
    929 {
    930 	struct rasops_info *ri = cookie;
    931 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    932 	struct vcons_screen *scr = ri->ri_hw;
    933 	struct voyagerfb_softc *sc = scr->scr_cookie;
    934 	uint32_t cmd;
    935 	int fg, bg;
    936 	uint8_t *data;
    937 	int x, y, wi, he;
    938 
    939 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
    940 		return;
    941 
    942 	if (!CHAR_IN_FONT(c, font))
    943 		return;
    944 
    945 	wi = font->fontwidth;
    946 	he = font->fontheight;
    947 
    948 	bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
    949 	fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
    950 	x = ri->ri_xorigin + col * wi;
    951 	y = ri->ri_yorigin + row * he;
    952 	if (c == 0x20) {
    953 		voyagerfb_rectfill(sc, x, y, wi, he, bg);
    954 		return;
    955 	}
    956 
    957 	data = WSFONT_GLYPH(c, font);
    958 
    959 	cmd = ROP_COPY |
    960 	      SM502_CTRL_USE_ROP2 |
    961 	      SM502_CTRL_CMD_HOSTWRT |
    962 	      SM502_CTRL_HOSTBLT_MONO |
    963 	      SM502_CTRL_QUICKSTART_E |
    964 	      SM502_CTRL_MONO_PACK_32BIT;
    965 	voyagerfb_ready(sc);
    966 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
    967 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND, fg);
    968 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_BACKGROUND, bg);
    969 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, 0);
    970 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, (x << 16) | y);
    971 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    972 	    SM502_DIMENSION, (wi << 16) | he);
    973 	/* now feed the data, padded to 32bit */
    974 	switch (ri->ri_font->stride) {
    975 		case 1:
    976 			voyagerfb_feed8(sc, data, ri->ri_fontscale);
    977 			break;
    978 		case 2:
    979 			voyagerfb_feed16(sc, (uint16_t *)data,
    980 			    ri->ri_fontscale);
    981 			break;
    982 	}
    983 }
    984 
    985 static void
    986 voyagerfb_putchar_aa32(void *cookie, int row, int col, u_int c, long attr)
    987 {
    988 	struct rasops_info *ri = cookie;
    989 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    990 	struct vcons_screen *scr = ri->ri_hw;
    991 	struct voyagerfb_softc *sc = scr->scr_cookie;
    992 	uint32_t cmd;
    993 	int fg, bg;
    994 	uint8_t *data;
    995 	int x, y, wi, he;
    996 	int i, j, r, g, b, aval, pad;
    997 	int rf, gf, bf, rb, gb, bb;
    998 	uint32_t pixel;
    999 	int rv;
   1000 
   1001 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1002 		return;
   1003 
   1004 	if (!CHAR_IN_FONT(c, font))
   1005 		return;
   1006 
   1007 	wi = font->fontwidth;
   1008 	he = font->fontheight;
   1009 
   1010 	bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
   1011 	fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
   1012 	x = ri->ri_xorigin + col * wi;
   1013 	y = ri->ri_yorigin + row * he;
   1014 	if (c == 0x20) {
   1015 		voyagerfb_rectfill(sc, x, y, wi, he, bg);
   1016 		return;
   1017 	}
   1018 
   1019 	data = WSFONT_GLYPH(c, font);
   1020 	/*
   1021 	 * we can't accelerate the actual alpha blending but
   1022 	 * we can at least use a host blit to go through the
   1023 	 * pipeline instead of having to sync the engine
   1024 	 */
   1025 
   1026 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1027 	if (rv == GC_OK)
   1028 		return;
   1029 
   1030 	cmd = ROP_COPY |
   1031 	      SM502_CTRL_USE_ROP2 |
   1032 	      SM502_CTRL_CMD_HOSTWRT |
   1033 	      SM502_CTRL_QUICKSTART_E;
   1034 	voyagerfb_ready(sc);
   1035 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
   1036 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, 0);
   1037 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, (x << 16) | y);
   1038 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
   1039 	    (wi << 16) | he);
   1040 	rf = (fg >> 16) & 0xff;
   1041 	rb = (bg >> 16) & 0xff;
   1042 	gf = (fg >> 8) & 0xff;
   1043 	gb = (bg >> 8) & 0xff;
   1044 	bf =  fg & 0xff;
   1045 	bb =  bg & 0xff;
   1046 	pad = wi & 1;
   1047 	for (i = 0; i < he; i++) {
   1048 		for (j = 0; j < wi; j++) {
   1049 			aval = *data;
   1050 			data++;
   1051 			if (aval == 0) {
   1052 				pixel = bg;
   1053 			} else if (aval == 255) {
   1054 				pixel = fg;
   1055 			} else {
   1056 				r = aval * rf + (255 - aval) * rb;
   1057 				g = aval * gf + (255 - aval) * gb;
   1058 				b = aval * bf + (255 - aval) * bb;
   1059 				pixel = (r & 0xff00) << 8 |
   1060 				        (g & 0xff00) |
   1061 				        (b & 0xff00) >> 8;
   1062 			}
   1063 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1064 			    SM502_DATAPORT, pixel);
   1065 		}
   1066 		if (pad)
   1067 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1068 			    SM502_DATAPORT, 0);
   1069 	}
   1070 	if (rv == GC_ADD) {
   1071 		glyphcache_add(&sc->sc_gc, c, x, y);
   1072 	}
   1073 }
   1074 
   1075 static void
   1076 voyagerfb_putchar_aa8(void *cookie, int row, int col, u_int c, long attr)
   1077 {
   1078 	struct rasops_info *ri = cookie;
   1079 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1080 	struct vcons_screen *scr = ri->ri_hw;
   1081 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1082 	uint32_t cmd;
   1083 	int bg;
   1084 	uint8_t *data;
   1085 	int x, y, wi, he;
   1086 	int i, j, r, g, b, aval, pad;
   1087 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
   1088 	uint32_t pixel = 0, latch = 0, bg8, fg8;
   1089 	int rv;
   1090 
   1091 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1092 		return;
   1093 
   1094 	if (!CHAR_IN_FONT(c, font))
   1095 		return;
   1096 
   1097 	wi = font->fontwidth;
   1098 	he = font->fontheight;
   1099 
   1100 	bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
   1101 	x = ri->ri_xorigin + col * wi;
   1102 	y = ri->ri_yorigin + row * he;
   1103 	if (c == 0x20) {
   1104 		voyagerfb_rectfill(sc, x, y, wi, he, bg);
   1105 		return;
   1106 	}
   1107 
   1108 	data = WSFONT_GLYPH(c, font);
   1109 	/*
   1110 	 * we can't accelerate the actual alpha blending but
   1111 	 * we can at least use a host blit to go through the
   1112 	 * pipeline instead of having to sync the engine
   1113 	 */
   1114 
   1115 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1116 	if (rv == GC_OK)
   1117 		return;
   1118 
   1119 	cmd = ROP_COPY |
   1120 	      SM502_CTRL_USE_ROP2 |
   1121 	      SM502_CTRL_CMD_HOSTWRT |
   1122 	      SM502_CTRL_QUICKSTART_E;
   1123 	voyagerfb_ready(sc);
   1124 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
   1125 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, 0);
   1126 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, (x << 16) | y);
   1127 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
   1128 	    (wi << 16) | he);
   1129 
   1130 	/*
   1131 	 * we need the RGB colours here, so get offsets into rasops_cmap
   1132 	 */
   1133 	fgo = ((attr >> 24) & 0xf) * 3;
   1134 	bgo = ((attr >> 16) & 0xf) * 3;
   1135 
   1136 	r0 = rasops_cmap[bgo];
   1137 	r1 = rasops_cmap[fgo];
   1138 	g0 = rasops_cmap[bgo + 1];
   1139 	g1 = rasops_cmap[fgo + 1];
   1140 	b0 = rasops_cmap[bgo + 2];
   1141 	b1 = rasops_cmap[fgo + 2];
   1142 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
   1143 	bg8 = R3G3B2(r0, g0, b0);
   1144 	fg8 = R3G3B2(r1, g1, b1);
   1145 
   1146 	pad = wi & 4;
   1147 	for (i = 0; i < he; i++) {
   1148 		for (j = 0; j < wi; j++) {
   1149 			aval = *data;
   1150 			data++;
   1151 			if (aval == 0) {
   1152 				pixel = bg8;
   1153 			} else if (aval == 255) {
   1154 				pixel = fg8;
   1155 			} else {
   1156 				r = aval * r1 + (255 - aval) * r0;
   1157 				g = aval * g1 + (255 - aval) * g0;
   1158 				b = aval * b1 + (255 - aval) * b0;
   1159 				pixel = ((r & 0xe000) >> 8) |
   1160 					((g & 0xe000) >> 11) |
   1161 					((b & 0xc000) >> 14);
   1162 			}
   1163 			latch = (latch << 8) | pixel;
   1164 			/* write in 32bit chunks */
   1165 			if ((j & 3) == 3) {
   1166 				bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1167 				    SM502_DATAPORT, be32toh(latch));
   1168 				latch = 0;
   1169 			}
   1170 		}
   1171 		/* if we have pixels left in latch write them out */
   1172 		if ((j & 3) != 0) {
   1173 			latch = latch << ((4 - (i & 3)) << 3);
   1174 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1175 			    SM502_DATAPORT, be32toh(latch));
   1176 		}
   1177 		if (pad)
   1178 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1179 			    SM502_DATAPORT, 0);
   1180 	}
   1181 	if (rv == GC_ADD) {
   1182 		glyphcache_add(&sc->sc_gc, c, x, y);
   1183 	}
   1184 }
   1185 
   1186 static void
   1187 voyagerfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1188 {
   1189 	struct rasops_info *ri = cookie;
   1190 	struct vcons_screen *scr = ri->ri_hw;
   1191 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1192 	int32_t xs, xd, y, width, height;
   1193 
   1194 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1195 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1196 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1197 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1198 		width = ri->ri_font->fontwidth * ncols;
   1199 		height = ri->ri_font->fontheight;
   1200 		voyagerfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
   1201 	}
   1202 }
   1203 
   1204 static void
   1205 voyagerfb_erasecols(void *cookie, int row, int startcol, int ncols,
   1206      long fillattr)
   1207 {
   1208 	struct rasops_info *ri = cookie;
   1209 	struct vcons_screen *scr = ri->ri_hw;
   1210 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1211 	int32_t x, y, width, height, fg, bg, ul;
   1212 
   1213 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1214 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1215 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1216 		width = ri->ri_font->fontwidth * ncols;
   1217 		height = ri->ri_font->fontheight;
   1218 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1219 
   1220 		voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1221 	}
   1222 }
   1223 
   1224 static void
   1225 voyagerfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1226 {
   1227 	struct rasops_info *ri = cookie;
   1228 	struct vcons_screen *scr = ri->ri_hw;
   1229 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1230 	int32_t x, ys, yd, width, height;
   1231 	int i;
   1232 
   1233 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1234 		x = ri->ri_xorigin;
   1235 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1236 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1237 		width = ri->ri_emuwidth;
   1238 		height = ri->ri_font->fontheight * nrows;
   1239 		if ((nrows > 1) && (dstrow > srcrow)) {
   1240 			/*
   1241 			 * the blitter can't do bottom-up copies so we have
   1242 			 * to copy line by line here
   1243 			 * should probably use a command sequence
   1244 			 */
   1245 			ys += (height - ri->ri_font->fontheight);
   1246 			yd += (height - ri->ri_font->fontheight);
   1247 			for (i = 0; i < nrows; i++) {
   1248 				voyagerfb_bitblt(sc, x, ys, x, yd, width,
   1249 				    ri->ri_font->fontheight, ROP_COPY);
   1250 				ys -= ri->ri_font->fontheight;
   1251 				yd -= ri->ri_font->fontheight;
   1252 			}
   1253 		} else
   1254 			voyagerfb_bitblt(sc, x, ys, x, yd, width, height,
   1255 			    ROP_COPY);
   1256 	}
   1257 }
   1258 
   1259 static void
   1260 voyagerfb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1261 {
   1262 	struct rasops_info *ri = cookie;
   1263 	struct vcons_screen *scr = ri->ri_hw;
   1264 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1265 	int32_t x, y, width, height, fg, bg, ul;
   1266 
   1267 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1268 		x = ri->ri_xorigin;
   1269 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1270 		width = ri->ri_emuwidth;
   1271 		height = ri->ri_font->fontheight * nrows;
   1272 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1273 
   1274 		voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1275 	}
   1276 }
   1277 
   1278 /* backlight control */
   1279 static void
   1280 voyagerfb_setup_backlight(struct voyagerfb_softc *sc)
   1281 {
   1282 	/* switch the pin to gpio mode if it isn't already */
   1283 	voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1284 	/* turn it on */
   1285 	voyager_write_gpio(sc->sc_gpio_cookie, 0xffffffff, GPIO_BACKLIGHT);
   1286 	sc->sc_bl_on = 1;
   1287 	sc->sc_bl_level = 255;
   1288 	pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
   1289 	    voyagerfb_brightness_up, TRUE);
   1290 	pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
   1291 	    voyagerfb_brightness_down, TRUE);
   1292 }
   1293 
   1294 static void
   1295 voyagerfb_set_backlight(struct voyagerfb_softc *sc, int level)
   1296 {
   1297 
   1298 	/*
   1299 	 * should we do nothing when backlight is off, should we just store the
   1300 	 * level and use it when turning back on or should we just flip sc_bl_on
   1301 	 * and turn the backlight on?
   1302 	 * For now turn it on so a crashed screensaver can't get the user stuck
   1303 	 * with a dark screen as long as hotkeys work
   1304 	 */
   1305 	if (level > 255) level = 255;
   1306 	if (level < 0) level = 0;
   1307 	if (level == sc->sc_bl_level)
   1308 		return;
   1309 	sc->sc_bl_level = level;
   1310 	if (sc->sc_bl_on == 0)
   1311 		sc->sc_bl_on = 1;
   1312 	/* and here we would actually muck with the hardware */
   1313 	if ((level == 0) || (level == 255)) {
   1314 		/* in these cases bypass the PWM and use the gpio */
   1315 		voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1316 		if (level == 0) {
   1317 			voyager_write_gpio(sc->sc_gpio_cookie,
   1318 			    ~GPIO_BACKLIGHT, 0);
   1319 		} else {
   1320 			voyager_write_gpio(sc->sc_gpio_cookie,
   1321 			    0xffffffff, GPIO_BACKLIGHT);
   1322 		}
   1323 	} else {
   1324 		uint32_t pwm;
   1325 
   1326 		pwm = voyager_set_pwm(20000, level * 1000 / 256);
   1327 		pwm |= SM502_PWM_ENABLE;
   1328 		bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PWM0, pwm);
   1329 
   1330 		/* let the PWM take over */
   1331 		voyager_control_gpio(sc->sc_gpio_cookie,
   1332 		    0xffffffff, GPIO_BACKLIGHT);
   1333 	}
   1334 }
   1335 
   1336 static void
   1337 voyagerfb_switch_backlight(struct voyagerfb_softc *sc, int on)
   1338 {
   1339 
   1340 	if (on == sc->sc_bl_on)
   1341 		return;
   1342 	sc->sc_bl_on = on;
   1343 	if (on) {
   1344 		int level = sc->sc_bl_level;
   1345 
   1346 		sc->sc_bl_level = -1;
   1347 		voyagerfb_set_backlight(sc, level);
   1348 	} else {
   1349 		voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1350 		voyager_write_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1351 	}
   1352 }
   1353 
   1354 
   1355 static void
   1356 voyagerfb_brightness_up(device_t dev)
   1357 {
   1358 	struct voyagerfb_softc *sc = device_private(dev);
   1359 
   1360 	voyagerfb_set_backlight(sc, sc->sc_bl_level + 8);
   1361 }
   1362 
   1363 static void
   1364 voyagerfb_brightness_down(device_t dev)
   1365 {
   1366 	struct voyagerfb_softc *sc = device_private(dev);
   1367 
   1368 	voyagerfb_set_backlight(sc, sc->sc_bl_level - 8);
   1369 }
   1370 
   1371 static int
   1372 voyagerfb_set_curpos(struct voyagerfb_softc *sc, int x, int y)
   1373 {
   1374 	uint32_t val;
   1375 	int xx, yy;
   1376 
   1377 	sc->sc_cur_x = x;
   1378 	sc->sc_cur_y = y;
   1379 
   1380 	xx = x - sc->sc_hot_x;
   1381 	yy = y - sc->sc_hot_y;
   1382 
   1383 	if (xx < 0) xx = abs(xx) | 0x800;
   1384 	if (yy < 0) yy = abs(yy) | 0x800;
   1385 
   1386 	val = (xx & 0xffff) | (yy << 16);
   1387 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_XY, val);
   1388 
   1389 	return 0;
   1390 }
   1391 
   1392 static int
   1393 voyagerfb_gcursor(struct voyagerfb_softc *sc, struct wsdisplay_cursor *cur)
   1394 {
   1395 	/* do nothing for now */
   1396 	return 0;
   1397 }
   1398 
   1399 static int
   1400 voyagerfb_scursor(struct voyagerfb_softc *sc, struct wsdisplay_cursor *cur)
   1401 {
   1402 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1403 
   1404 		bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1405 		    SM502_PANEL_CRSR_ADDR,
   1406 		    sc->sc_cursor_addr | (cur->enable ? SM502_CRSR_ENABLE : 0));
   1407 		DPRINTF("%s: %08x\n", __func__, sc->sc_cursor_addr);
   1408 	}
   1409 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1410 
   1411 		sc->sc_hot_x = cur->hot.x;
   1412 		sc->sc_hot_y = cur->hot.y;
   1413 	}
   1414 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1415 
   1416 		voyagerfb_set_curpos(sc, cur->pos.x, cur->pos.y);
   1417 	}
   1418 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1419 		int i, idx;
   1420 		uint32_t val;
   1421 
   1422 		for (i = 0; i < cur->cmap.count; i++) {
   1423 			val = ((cur->cmap.red[i] & 0xf8) << 8) |
   1424 			      ((cur->cmap.green[i] & 0xfc) << 3) |
   1425 			      ((cur->cmap.blue[i] & 0xf8) >> 3);
   1426 			idx = i + cur->cmap.index;
   1427 			bus_space_write_2(sc->sc_memt, sc->sc_regh,
   1428 			    SM502_PANEL_CRSR_COL12 + (idx << 1),
   1429 			    val);
   1430 			/*
   1431 			 * if userland doesn't try to set the 3rd colour we
   1432 			 * assume it expects an X11-style 2 colour cursor
   1433 			 * X should be our main user anyway
   1434 			 */
   1435 			if ((idx == 1) &&
   1436 			   ((cur->cmap.count + cur->cmap.index) < 3)) {
   1437 				bus_space_write_2(sc->sc_memt, sc->sc_regh,
   1438 				    SM502_PANEL_CRSR_COL3,
   1439 				    val);
   1440 			}
   1441 			DPRINTF("%s: %d %04x\n", __func__, i + cur->cmap.index,
   1442 			    val);
   1443 		}
   1444 	}
   1445 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1446 
   1447 		int i, j, cnt = 0;
   1448 		uint32_t latch = 0, omask;
   1449 		uint8_t imask;
   1450 		DPRINTF("%s: %d %d\n", __func__, cur->size.x, cur->size.y);
   1451 		for (i = 0; i < 256; i++) {
   1452 			omask = 0x00000001;
   1453 			imask = 0x01;
   1454 			cur->image[cnt] &= cur->mask[cnt];
   1455 			for (j = 0; j < 8; j++) {
   1456 				if (cur->mask[cnt] & imask)
   1457 					latch |= omask;
   1458 				omask <<= 1;
   1459 				if (cur->image[cnt] & imask)
   1460 					latch |= omask;
   1461 				omask <<= 1;
   1462 				imask <<= 1;
   1463 			}
   1464 			cnt++;
   1465 			imask = 0x01;
   1466 			cur->image[cnt] &= cur->mask[cnt];
   1467 			for (j = 0; j < 8; j++) {
   1468 				if (cur->mask[cnt] & imask)
   1469 					latch |= omask;
   1470 				omask <<= 1;
   1471 				if (cur->image[cnt] & imask)
   1472 					latch |= omask;
   1473 				omask <<= 1;
   1474 				imask <<= 1;
   1475 			}
   1476 			cnt++;
   1477 			sc->sc_cursor[i] = latch;
   1478 			latch = 0;
   1479 		}
   1480 	}
   1481 	return 0;
   1482 }
   1483