Home | History | Annotate | Line # | Download | only in voyager
voyagerfb.c revision 1.19
      1 /*	$NetBSD: voyagerfb.c,v 1.19 2012/03/20 14:59:36 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.19 2012/03/20 14:59:36 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 /* there are probably 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_copycols(void *, int, int, int, int);
    148 static void	voyagerfb_erasecols(void *, int, int, int, long);
    149 static void	voyagerfb_copyrows(void *, int, int, int);
    150 static void	voyagerfb_eraserows(void *, int, int, long);
    151 
    152 static int	voyagerfb_set_curpos(struct voyagerfb_softc *, int, int);
    153 static int	voyagerfb_gcursor(struct voyagerfb_softc *, struct wsdisplay_cursor *);
    154 static int	voyagerfb_scursor(struct voyagerfb_softc *, struct wsdisplay_cursor *);
    155 
    156 struct wsdisplay_accessops voyagerfb_accessops = {
    157 	voyagerfb_ioctl,
    158 	voyagerfb_mmap,
    159 	NULL,	/* alloc_screen */
    160 	NULL,	/* free_screen */
    161 	NULL,	/* show_screen */
    162 	NULL, 	/* load_font */
    163 	NULL,	/* pollc */
    164 	NULL	/* scroll */
    165 };
    166 
    167 static void	voyagerfb_setup_backlight(struct voyagerfb_softc *);
    168 static void	voyagerfb_brightness_up(device_t);
    169 static void	voyagerfb_brightness_down(device_t);
    170 /* set backlight level */
    171 static void	voyagerfb_set_backlight(struct voyagerfb_softc *, int);
    172 /* turn backlight on and off without messing with the level */
    173 static void	voyagerfb_switch_backlight(struct voyagerfb_softc *, int);
    174 
    175 /* wait for FIFO empty so we can feed it another command */
    176 static inline void
    177 voyagerfb_ready(struct voyagerfb_softc *sc)
    178 {
    179 	do {} while ((bus_space_read_4(sc->sc_memt, sc->sc_regh,
    180 	    SM502_SYSTEM_CTRL) & SM502_SYSCTL_FIFO_EMPTY) == 0);
    181 }
    182 
    183 /* wait for the drawing engine to be idle */
    184 static inline void
    185 voyagerfb_wait(struct voyagerfb_softc *sc)
    186 {
    187 	do {} while ((bus_space_read_4(sc->sc_memt, sc->sc_regh,
    188 	    SM502_SYSTEM_CTRL) & SM502_SYSCTL_ENGINE_BUSY) != 0);
    189 }
    190 
    191 static int
    192 voyagerfb_match(device_t parent, cfdata_t match, void *aux)
    193 {
    194 	struct voyager_attach_args *vaa = (struct voyager_attach_args *)aux;
    195 
    196 	if (strcmp(vaa->vaa_name, "voyagerfb") == 0) return 100;
    197 	return 0;
    198 }
    199 
    200 static void
    201 voyagerfb_attach(device_t parent, device_t self, void *aux)
    202 {
    203 	struct voyagerfb_softc	*sc = device_private(self);
    204 	struct voyager_attach_args	*vaa = aux;
    205 	struct rasops_info	*ri;
    206 	struct wsemuldisplaydev_attach_args aa;
    207 	prop_dictionary_t	dict;
    208 	unsigned long		defattr;
    209 	bool			is_console;
    210 	int i, j;
    211 
    212 	sc->sc_pc = vaa->vaa_pc;
    213 	sc->sc_pcitag = vaa->vaa_pcitag;
    214 	sc->sc_memt = vaa->vaa_tag;
    215 	sc->sc_dev = self;
    216 
    217 	aprint_normal("\n");
    218 
    219 	dict = device_properties(self);
    220 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    221 
    222 	sc->sc_fb = vaa->vaa_mem_pa;
    223 	sc->sc_fbh = vaa->vaa_memh;
    224 	sc->sc_fbsize = 16 * 1024 * 1024;
    225 	sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
    226 
    227 	sc->sc_reg = vaa->vaa_reg_pa;
    228 	sc->sc_regh = vaa->vaa_regh;
    229 	sc->sc_regsize = 2 * 1024 * 1024;
    230 	sc->sc_dataport = bus_space_vaddr(sc->sc_memt, sc->sc_regh);
    231 	sc->sc_dataport += SM502_DATAPORT;
    232 
    233 	sc->sc_width = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    234 		SM502_PANEL_FB_WIDTH) & SM502_FBW_WIN_WIDTH_MASK) >> 16;
    235 	sc->sc_height = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    236 		SM502_PANEL_FB_HEIGHT) & SM502_FBH_WIN_HEIGHT_MASK) >> 16;
    237 
    238 #ifdef VOYAGERFB_ANTIALIAS
    239 	sc->sc_depth = 32;
    240 #else
    241 	sc->sc_depth = 8;
    242 #endif
    243 
    244 	/* init engine here */
    245 	voyagerfb_init(sc);
    246 
    247 	printf("%s: %d x %d, %d bit, stride %d\n", device_xname(self),
    248 		sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
    249 
    250 	/*
    251 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
    252 	 * but as far as I know there are no SM502 with 64bit BARs
    253 	 */
    254 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    255 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    256 
    257 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    258 		"default",
    259 		0, 0,
    260 		NULL,
    261 		8, 16,
    262 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    263 		NULL
    264 	};
    265 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    266 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    267 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    268 	sc->sc_locked = 0;
    269 
    270 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    271 	    &voyagerfb_accessops);
    272 	sc->vd.init_screen = voyagerfb_init_screen;
    273 
    274 	/* backlight control */
    275 	sc->sc_gpio_cookie = device_private(parent);
    276 	voyagerfb_setup_backlight(sc);
    277 
    278 	ri = &sc->sc_console_screen.scr_ri;
    279 
    280 	sc->sc_gc.gc_bitblt = voyagerfb_bitblt;
    281 	sc->sc_gc.gc_blitcookie = sc;
    282 	sc->sc_gc.gc_rop = ROP_COPY;
    283 	if (is_console) {
    284 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    285 		    &defattr);
    286 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    287 
    288 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    289 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    290 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    291 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    292 	} else {
    293 		/*
    294 		 * since we're not the console we can postpone the rest
    295 		 * until someone actually allocates a screen for us
    296 		 */
    297 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    298 	}
    299 	glyphcache_init(&sc->sc_gc, sc->sc_height,
    300 			sc->sc_width,
    301 			(sc->sc_fbsize / sc->sc_stride) - sc->sc_height,
    302 			ri->ri_font->fontwidth,
    303 			ri->ri_font->fontheight,
    304 			defattr);
    305 	if (is_console)
    306 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    307 		    defattr);
    308 
    309 	j = 0;
    310 	if (sc->sc_depth <= 8) {
    311 		for (i = 0; i < (1 << sc->sc_depth); i++) {
    312 
    313 			sc->sc_cmap_red[i] = rasops_cmap[j];
    314 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    315 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    316 			voyagerfb_putpalreg(sc, i, rasops_cmap[j],
    317 			    rasops_cmap[j + 1], rasops_cmap[j + 2]);
    318 			j += 3;
    319 		}
    320 	}
    321 
    322 	voyagerfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    323 	    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    324 
    325 	if (is_console)
    326 		vcons_replay_msgbuf(&sc->sc_console_screen);
    327 
    328 	aa.console = is_console;
    329 	aa.scrdata = &sc->sc_screenlist;
    330 	aa.accessops = &voyagerfb_accessops;
    331 	aa.accesscookie = &sc->vd;
    332 
    333 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    334 }
    335 
    336 static int
    337 voyagerfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    338 	struct lwp *l)
    339 {
    340 	struct vcons_data *vd = v;
    341 	struct voyagerfb_softc *sc = vd->cookie;
    342 	struct wsdisplay_fbinfo *wdf;
    343 	struct vcons_screen *ms = vd->active;
    344 	struct wsdisplay_param  *param;
    345 
    346 	switch (cmd) {
    347 	case WSDISPLAYIO_GTYPE:
    348 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    349 		return 0;
    350 
    351 	/* PCI config read/write passthrough. */
    352 	case PCI_IOC_CFGREAD:
    353 	case PCI_IOC_CFGWRITE:
    354 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    355 		    cmd, data, flag, l);
    356 
    357 	case WSDISPLAYIO_GET_BUSID:
    358 		return wsdisplayio_busid_pci(device_parent(sc->sc_dev),
    359 		    sc->sc_pc, sc->sc_pcitag, data);
    360 
    361 	case WSDISPLAYIO_GINFO:
    362 		if (ms == NULL)
    363 			return ENODEV;
    364 		wdf = (void *)data;
    365 		wdf->height = ms->scr_ri.ri_height;
    366 		wdf->width = ms->scr_ri.ri_width;
    367 		wdf->depth = 32;
    368 		wdf->cmsize = 256;
    369 		return 0;
    370 
    371 	case WSDISPLAYIO_GETCMAP:
    372 		return voyagerfb_getcmap(sc,
    373 		    (struct wsdisplay_cmap *)data);
    374 
    375 	case WSDISPLAYIO_PUTCMAP:
    376 		return voyagerfb_putcmap(sc,
    377 		    (struct wsdisplay_cmap *)data);
    378 
    379 	case WSDISPLAYIO_LINEBYTES:
    380 		*(u_int *)data = sc->sc_stride;
    381 		return 0;
    382 
    383 	case WSDISPLAYIO_SMODE: {
    384 		int new_mode = *(int*)data;
    385 		if (new_mode != sc->sc_mode) {
    386 			sc->sc_mode = new_mode;
    387 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    388 #ifdef VOYAGERFB_ANTIALIAS
    389 				sc->sc_depth = 32;
    390 #else
    391 				sc->sc_depth = 8;
    392 #endif
    393 				glyphcache_wipe(&sc->sc_gc);
    394 				voyagerfb_init(sc);
    395 				voyagerfb_restore_palette(sc);
    396 				vcons_redraw_screen(ms);
    397 			} else {
    398 				sc->sc_depth = 32;
    399 				voyagerfb_init(sc);
    400 			}
    401 		}
    402 		}
    403 		return 0;
    404 
    405 	case WSDISPLAYIO_GVIDEO:
    406 		*(int *)data = sc->sc_bl_on ? WSDISPLAYIO_VIDEO_ON :
    407 					      WSDISPLAYIO_VIDEO_OFF;
    408 		return 0;
    409 
    410 	case WSDISPLAYIO_SVIDEO: {
    411 			int new_bl = *(int *)data;
    412 
    413 			voyagerfb_switch_backlight(sc,  new_bl);
    414 		}
    415 		return 0;
    416 
    417 	case WSDISPLAYIO_GETPARAM:
    418 		param = (struct wsdisplay_param *)data;
    419 		switch (param->param) {
    420 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    421 			param->min = 0;
    422 			param->max = 255;
    423 			param->curval = sc->sc_bl_level;
    424 			return 0;
    425 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    426 			param->min = 0;
    427 			param->max = 1;
    428 			param->curval = sc->sc_bl_on;
    429 			return 0;
    430 		}
    431 		return EPASSTHROUGH;
    432 
    433 	case WSDISPLAYIO_SETPARAM:
    434 		param = (struct wsdisplay_param *)data;
    435 		switch (param->param) {
    436 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    437 			voyagerfb_set_backlight(sc, param->curval);
    438 			return 0;
    439 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    440 			voyagerfb_switch_backlight(sc,  param->curval);
    441 			return 0;
    442 		}
    443 		return EPASSTHROUGH;
    444 
    445 	case WSDISPLAYIO_GCURPOS:
    446 		{
    447 			struct wsdisplay_curpos *pos;
    448 
    449 			pos = (struct wsdisplay_curpos *)data;
    450 			pos->x = sc->sc_cur_x;
    451 			pos->y = sc->sc_cur_y;
    452 		}
    453 		return 0;
    454 
    455 	case WSDISPLAYIO_SCURPOS:
    456 		{
    457 			struct wsdisplay_curpos *pos;
    458 
    459 			pos = (struct wsdisplay_curpos *)data;
    460 			voyagerfb_set_curpos(sc, pos->x, pos->y);
    461 		}
    462 		return 0;
    463 
    464 	case WSDISPLAYIO_GCURMAX:
    465 		{
    466 			struct wsdisplay_curpos *pos;
    467 
    468 			pos = (struct wsdisplay_curpos *)data;
    469 			pos->x = 64;
    470 			pos->y = 64;
    471 		}
    472 		return 0;
    473 
    474 	case WSDISPLAYIO_GCURSOR:
    475 		{
    476 			struct wsdisplay_cursor *cu;
    477 
    478 			cu = (struct wsdisplay_cursor *)data;
    479 			return voyagerfb_gcursor(sc, cu);
    480 		}
    481 
    482 	case WSDISPLAYIO_SCURSOR:
    483 		{
    484 			struct wsdisplay_cursor *cu;
    485 
    486 			cu = (struct wsdisplay_cursor *)data;
    487 			return voyagerfb_scursor(sc, cu);
    488 		}
    489 	}
    490 	return EPASSTHROUGH;
    491 }
    492 
    493 static paddr_t
    494 voyagerfb_mmap(void *v, void *vs, off_t offset, int prot)
    495 {
    496 	struct vcons_data *vd = v;
    497 	struct voyagerfb_softc *sc = vd->cookie;
    498 	paddr_t pa;
    499 
    500 	/* 'regular' framebuffer mmap()ing */
    501 	if (offset < sc->sc_fbsize) {
    502 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    503 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    504 		return pa;
    505 	}
    506 
    507 	/*
    508 	 * restrict all other mappings to processes with superuser privileges
    509 	 * or the kernel itself
    510 	 */
    511 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
    512 	    NULL, NULL, NULL, NULL) != 0) {
    513 		aprint_normal("%s: mmap() rejected.\n",
    514 		    device_xname(sc->sc_dev));
    515 		return -1;
    516 	}
    517 
    518 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    519 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    520 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    521 		return pa;
    522 	}
    523 
    524 	if ((offset >= sc->sc_reg) &&
    525 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    526 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 0);
    527 		return pa;
    528 	}
    529 
    530 	return -1;
    531 }
    532 
    533 static void
    534 voyagerfb_init_screen(void *cookie, struct vcons_screen *scr,
    535     int existing, long *defattr)
    536 {
    537 	struct voyagerfb_softc *sc = cookie;
    538 	struct rasops_info *ri = &scr->scr_ri;
    539 
    540 	ri->ri_depth = sc->sc_depth;
    541 	ri->ri_width = sc->sc_width;
    542 	ri->ri_height = sc->sc_height;
    543 	ri->ri_stride = sc->sc_stride;
    544 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    545 
    546 	ri->ri_bits = (char *)sc->sc_fbaddr;
    547 
    548 	if (existing) {
    549 		ri->ri_flg |= RI_CLEAR;
    550 	}
    551 #ifdef VOYAGERFB_ANTIALIAS
    552 	if (sc->sc_depth == 32)
    553 		ri->ri_flg |= RI_ENABLE_ALPHA;
    554 #endif
    555 
    556 	rasops_init(ri, 0, 0);
    557 	ri->ri_caps = WSSCREEN_WSCOLORS;
    558 
    559 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    560 		    sc->sc_width / ri->ri_font->fontwidth);
    561 
    562 	ri->ri_hw = scr;
    563 	ri->ri_ops.copyrows = voyagerfb_copyrows;
    564 	ri->ri_ops.copycols = voyagerfb_copycols;
    565 	ri->ri_ops.eraserows = voyagerfb_eraserows;
    566 	ri->ri_ops.erasecols = voyagerfb_erasecols;
    567 	ri->ri_ops.cursor = voyagerfb_cursor;
    568 	if (FONT_IS_ALPHA(ri->ri_font)) {
    569 		ri->ri_ops.putchar = voyagerfb_putchar_aa32;
    570 	} else
    571 		ri->ri_ops.putchar = voyagerfb_putchar_mono;
    572 }
    573 
    574 static int
    575 voyagerfb_putcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
    576 {
    577 	u_char *r, *g, *b;
    578 	u_int index = cm->index;
    579 	u_int count = cm->count;
    580 	int i, error;
    581 	u_char rbuf[256], gbuf[256], bbuf[256];
    582 
    583 #ifdef VOYAGERFB_DEBUG
    584 	aprint_debug("putcmap: %d %d\n",index, count);
    585 #endif
    586 	if (cm->index >= 256 || cm->count > 256 ||
    587 	    (cm->index + cm->count) > 256)
    588 		return EINVAL;
    589 	error = copyin(cm->red, &rbuf[index], count);
    590 	if (error)
    591 		return error;
    592 	error = copyin(cm->green, &gbuf[index], count);
    593 	if (error)
    594 		return error;
    595 	error = copyin(cm->blue, &bbuf[index], count);
    596 	if (error)
    597 		return error;
    598 
    599 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    600 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    601 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    602 
    603 	r = &sc->sc_cmap_red[index];
    604 	g = &sc->sc_cmap_green[index];
    605 	b = &sc->sc_cmap_blue[index];
    606 
    607 	for (i = 0; i < count; i++) {
    608 		voyagerfb_putpalreg(sc, index, *r, *g, *b);
    609 		index++;
    610 		r++, g++, b++;
    611 	}
    612 	return 0;
    613 }
    614 
    615 static int
    616 voyagerfb_getcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
    617 {
    618 	u_int index = cm->index;
    619 	u_int count = cm->count;
    620 	int error;
    621 
    622 	if (index >= 255 || count > 256 || index + count > 256)
    623 		return EINVAL;
    624 
    625 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    626 	if (error)
    627 		return error;
    628 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    629 	if (error)
    630 		return error;
    631 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    632 	if (error)
    633 		return error;
    634 
    635 	return 0;
    636 }
    637 
    638 static void
    639 voyagerfb_restore_palette(struct voyagerfb_softc *sc)
    640 {
    641 	int i;
    642 
    643 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    644 		voyagerfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    645 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    646 	}
    647 }
    648 
    649 static int
    650 voyagerfb_putpalreg(struct voyagerfb_softc *sc, int idx, uint8_t r,
    651     uint8_t g, uint8_t b)
    652 {
    653 	uint32_t reg;
    654 
    655 	reg = (r << 16) | (g << 8) | b;
    656 	/* XXX we should probably write the CRT palette too */
    657 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    658 	    SM502_PALETTE_PANEL + (idx << 2), reg);
    659 	return 0;
    660 }
    661 
    662 static void
    663 voyagerfb_init(struct voyagerfb_softc *sc)
    664 {
    665 	int reg;
    666 
    667 	voyagerfb_wait(sc);
    668 	/* disable colour compare */
    669 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_COLOR_COMP_MASK, 0);
    670 	/* allow writes to all planes */
    671 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PLANEMASK,
    672 	    0xffffffff);
    673 	/* disable clipping */
    674 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CLIP_TOP_LEFT, 0);
    675 	/* source and destination in local memory, no offset */
    676 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC_BASE, 0);
    677 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST_BASE, 0);
    678 	/* pitch is screen stride */
    679 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PITCH,
    680 	    sc->sc_width | (sc->sc_width << 16));
    681 	/* window is screen width */
    682 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_WINDOW_WIDTH,
    683 	    sc->sc_width | (sc->sc_width << 16));
    684 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL);
    685 	reg &= ~SM502_PDC_DEPTH_MASK;
    686 
    687 	switch (sc->sc_depth) {
    688 		case 8:
    689 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    690 			    SM502_STRETCH, SM502_STRETCH_8BIT);
    691 			sc->sc_stride = sc->sc_width;
    692 			reg |= SM502_PDC_8BIT;
    693 			break;
    694 		case 16:
    695 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    696 			    SM502_STRETCH, SM502_STRETCH_16BIT);
    697 			sc->sc_stride = sc->sc_width << 1;
    698 			reg |= SM502_PDC_16BIT;
    699 			break;
    700 		case 24:
    701 		case 32:
    702 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    703 			    SM502_STRETCH, SM502_STRETCH_32BIT);
    704 			sc->sc_stride = sc->sc_width << 2;
    705 			reg |= SM502_PDC_32BIT;
    706 			break;
    707 	}
    708 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_FB_OFFSET,
    709 	    (sc->sc_stride << 16) | sc->sc_stride);
    710 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL,
    711 	    reg);
    712 
    713 	/* put the cursor at the end of video memory */
    714 	sc->sc_cursor_addr = 16 * 1024 * 1024 - 16 * 64;	/* XXX */
    715 	DPRINTF("%s: %08x\n", __func__, sc->sc_cursor_addr);
    716 	sc->sc_cursor = (uint32_t *)((uint8_t *)bus_space_vaddr(sc->sc_memt, sc->sc_fbh)
    717 			 + sc->sc_cursor_addr);
    718 #ifdef VOYAGERFB_DEBUG
    719 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_XY, 0x00100010);
    720 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_COL12, 0x0000ffff);
    721 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_COL3, 0x0000f800);
    722 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_ADDR,
    723 	    SM502_CRSR_ENABLE | sc->sc_cursor_addr);
    724 	sc->sc_cursor[0] = 0x00000000;
    725 	sc->sc_cursor[1] = 0x00000000;
    726 	sc->sc_cursor[2] = 0xffffffff;
    727 	sc->sc_cursor[3] = 0xffffffff;
    728 	sc->sc_cursor[4] = 0xaaaaaaaa;
    729 	sc->sc_cursor[5] = 0xaaaaaaaa;
    730 	sc->sc_cursor[6] = 0xffffffff;
    731 	sc->sc_cursor[7] = 0x00000000;
    732 #else
    733 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_ADDR,
    734 	    sc->sc_cursor_addr);
    735 #endif
    736 }
    737 
    738 static void
    739 voyagerfb_rectfill(struct voyagerfb_softc *sc, int x, int y, int wi, int he,
    740      uint32_t colour)
    741 {
    742 
    743 	voyagerfb_ready(sc);
    744 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL,
    745 	    ROP_COPY |
    746 	    SM502_CTRL_USE_ROP2 |
    747 	    SM502_CTRL_CMD_RECTFILL |
    748 	    SM502_CTRL_QUICKSTART_E);
    749 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND,
    750 	    colour);
    751 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
    752 	    (x << 16) | y);
    753 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
    754 	    (wi << 16) | he);
    755 }
    756 
    757 static void
    758 voyagerfb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
    759     int wi, int he, int rop)
    760 {
    761 	struct voyagerfb_softc *sc = cookie;
    762 	uint32_t cmd;
    763 
    764 	cmd = (rop & 0xf) | SM502_CTRL_USE_ROP2 | SM502_CTRL_CMD_BITBLT |
    765 	      SM502_CTRL_QUICKSTART_E;
    766 
    767 	voyagerfb_ready(sc);
    768 
    769 	if (xd <= xs) {
    770 		/* left to right */
    771 	} else {
    772 		/*
    773 		 * According to the manual this flag reverses only the blitter's
    774 		 * X direction. At least on my Gdium it also reverses the Y
    775 		 * direction
    776 		 */
    777 		cmd |= SM502_CTRL_R_TO_L;
    778 		xs += wi - 1;
    779 		xd += wi - 1;
    780 		ys += he - 1;
    781 		yd += he - 1;
    782 	}
    783 
    784 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
    785 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC,
    786 	    (xs << 16) | ys);
    787 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
    788 	    (xd << 16) | yd);
    789 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
    790 	    (wi << 16) | he);
    791 }
    792 
    793 static void
    794 voyagerfb_cursor(void *cookie, int on, int row, int col)
    795 {
    796 	struct rasops_info *ri = cookie;
    797 	struct vcons_screen *scr = ri->ri_hw;
    798 	struct voyagerfb_softc *sc = scr->scr_cookie;
    799 	int x, y, wi, he;
    800 
    801 	wi = ri->ri_font->fontwidth;
    802 	he = ri->ri_font->fontheight;
    803 
    804 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    805 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    806 		y = ri->ri_crow * he + ri->ri_yorigin;
    807 		if (ri->ri_flg & RI_CURSOR) {
    808 			voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
    809 			ri->ri_flg &= ~RI_CURSOR;
    810 		}
    811 		ri->ri_crow = row;
    812 		ri->ri_ccol = col;
    813 		if (on) {
    814 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    815 			y = ri->ri_crow * he + ri->ri_yorigin;
    816 			voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
    817 			ri->ri_flg |= RI_CURSOR;
    818 		}
    819 	} else {
    820 		scr->scr_ri.ri_crow = row;
    821 		scr->scr_ri.ri_ccol = col;
    822 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    823 	}
    824 
    825 }
    826 
    827 static inline void
    828 voyagerfb_feed8(struct voyagerfb_softc *sc, uint8_t *data, int len)
    829 {
    830 	uint32_t *port = (uint32_t *)sc->sc_dataport;
    831 	int i;
    832 
    833 	for (i = 0; i < ((len + 3) & 0xfffc); i++) {
    834 		*port = *data;
    835 		data++;
    836 	}
    837 }
    838 
    839 static inline void
    840 voyagerfb_feed16(struct voyagerfb_softc *sc, uint16_t *data, int len)
    841 {
    842 	uint32_t *port = (uint32_t *)sc->sc_dataport;
    843 	int i;
    844 
    845 	len = len << 1;
    846 	for (i = 0; i < ((len + 1) & 0xfffe); i++) {
    847 		*port = *data;
    848 		data++;
    849 	}
    850 }
    851 
    852 static void
    853 voyagerfb_putchar_mono(void *cookie, int row, int col, u_int c, long attr)
    854 {
    855 	struct rasops_info *ri = cookie;
    856 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    857 	struct vcons_screen *scr = ri->ri_hw;
    858 	struct voyagerfb_softc *sc = scr->scr_cookie;
    859 	uint32_t cmd;
    860 	int fg, bg;
    861 	uint8_t *data;
    862 	int x, y, wi, he;
    863 
    864 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
    865 		return;
    866 
    867 	if (!CHAR_IN_FONT(c, font))
    868 		return;
    869 
    870 	wi = font->fontwidth;
    871 	he = font->fontheight;
    872 
    873 	bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
    874 	fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
    875 	x = ri->ri_xorigin + col * wi;
    876 	y = ri->ri_yorigin + row * he;
    877 	if (c == 0x20) {
    878 		voyagerfb_rectfill(sc, x, y, wi, he, bg);
    879 		return;
    880 	}
    881 
    882 	data = WSFONT_GLYPH(c, font);
    883 
    884 	cmd = ROP_COPY |
    885 	      SM502_CTRL_USE_ROP2 |
    886 	      SM502_CTRL_CMD_HOSTWRT |
    887 	      SM502_CTRL_HOSTBLT_MONO |
    888 	      SM502_CTRL_QUICKSTART_E |
    889 	      SM502_CTRL_MONO_PACK_32BIT;
    890 	voyagerfb_ready(sc);
    891 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
    892 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND, fg);
    893 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_BACKGROUND, bg);
    894 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, 0);
    895 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, (x << 16) | y);
    896 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    897 	    SM502_DIMENSION, (wi << 16) | he);
    898 	/* now feed the data, padded to 32bit */
    899 	switch (ri->ri_font->stride) {
    900 		case 1:
    901 			voyagerfb_feed8(sc, data, ri->ri_fontscale);
    902 			break;
    903 		case 2:
    904 			voyagerfb_feed16(sc, (uint16_t *)data,
    905 			    ri->ri_fontscale);
    906 			break;
    907 	}
    908 }
    909 
    910 static void
    911 voyagerfb_putchar_aa32(void *cookie, int row, int col, u_int c, long attr)
    912 {
    913 	struct rasops_info *ri = cookie;
    914 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    915 	struct vcons_screen *scr = ri->ri_hw;
    916 	struct voyagerfb_softc *sc = scr->scr_cookie;
    917 	uint32_t cmd;
    918 	int fg, bg;
    919 	uint8_t *data;
    920 	int x, y, wi, he;
    921 	int i, j, r, g, b, aval, pad;
    922 	int rf, gf, bf, rb, gb, bb;
    923 	uint32_t pixel;
    924 	int rv;
    925 
    926 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
    927 		return;
    928 
    929 	if (!CHAR_IN_FONT(c, font))
    930 		return;
    931 
    932 	wi = font->fontwidth;
    933 	he = font->fontheight;
    934 
    935 	bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
    936 	fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
    937 	x = ri->ri_xorigin + col * wi;
    938 	y = ri->ri_yorigin + row * he;
    939 	if (c == 0x20) {
    940 		voyagerfb_rectfill(sc, x, y, wi, he, bg);
    941 		return;
    942 	}
    943 
    944 	data = WSFONT_GLYPH(c, font);
    945 	/*
    946 	 * we can't accelerate the actual alpha blending but
    947 	 * we can at least use a host blit to go through the
    948 	 * pipeline instead of having to sync the engine
    949 	 */
    950 
    951 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
    952 	if (rv == GC_OK)
    953 		return;
    954 
    955 	cmd = ROP_COPY |
    956 	      SM502_CTRL_USE_ROP2 |
    957 	      SM502_CTRL_CMD_HOSTWRT |
    958 	      SM502_CTRL_QUICKSTART_E;
    959 	voyagerfb_ready(sc);
    960 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
    961 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, 0);
    962 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, (x << 16) | y);
    963 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION, (wi << 16) | he);
    964 	rf = (fg >> 16) & 0xff;
    965 	rb = (bg >> 16) & 0xff;
    966 	gf = (fg >> 8) & 0xff;
    967 	gb = (bg >> 8) & 0xff;
    968 	bf =  fg & 0xff;
    969 	bb =  bg & 0xff;
    970 	pad = wi & 1;
    971 	for (i = 0; i < he; i++) {
    972 		for (j = 0; j < wi; j++) {
    973 			aval = *data;
    974 			data++;
    975 			if (aval == 0) {
    976 				pixel = bg;
    977 			} else if (aval == 255) {
    978 				pixel = fg;
    979 			} else {
    980 				r = aval * rf + (255 - aval) * rb;
    981 				g = aval * gf + (255 - aval) * gb;
    982 				b = aval * bf + (255 - aval) * bb;
    983 				pixel = (r & 0xff00) << 8 |
    984 				        (g & 0xff00) |
    985 				        (b & 0xff00) >> 8;
    986 			}
    987 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    988 			    SM502_DATAPORT, pixel);
    989 		}
    990 		if (pad)
    991 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    992 			    SM502_DATAPORT, 0);
    993 	}
    994 	if (rv == GC_ADD) {
    995 		glyphcache_add(&sc->sc_gc, c, x, y);
    996 	}
    997 }
    998 
    999 static void
   1000 voyagerfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1001 {
   1002 	struct rasops_info *ri = cookie;
   1003 	struct vcons_screen *scr = ri->ri_hw;
   1004 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1005 	int32_t xs, xd, y, width, height;
   1006 
   1007 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1008 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1009 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1010 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1011 		width = ri->ri_font->fontwidth * ncols;
   1012 		height = ri->ri_font->fontheight;
   1013 		voyagerfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
   1014 	}
   1015 }
   1016 
   1017 static void
   1018 voyagerfb_erasecols(void *cookie, int row, int startcol, int ncols,
   1019      long fillattr)
   1020 {
   1021 	struct rasops_info *ri = cookie;
   1022 	struct vcons_screen *scr = ri->ri_hw;
   1023 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1024 	int32_t x, y, width, height, fg, bg, ul;
   1025 
   1026 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1027 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1028 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1029 		width = ri->ri_font->fontwidth * ncols;
   1030 		height = ri->ri_font->fontheight;
   1031 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1032 
   1033 		voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1034 	}
   1035 }
   1036 
   1037 static void
   1038 voyagerfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1039 {
   1040 	struct rasops_info *ri = cookie;
   1041 	struct vcons_screen *scr = ri->ri_hw;
   1042 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1043 	int32_t x, ys, yd, width, height;
   1044 	int i;
   1045 
   1046 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1047 		x = ri->ri_xorigin;
   1048 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1049 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1050 		width = ri->ri_emuwidth;
   1051 		height = ri->ri_font->fontheight * nrows;
   1052 		if ((nrows > 1) && (dstrow > srcrow)) {
   1053 			/*
   1054 			 * the blitter can't do bottom-up copies so we have
   1055 			 * to copy line by line here
   1056 			 * should probably use a command sequence
   1057 			 */
   1058 			ys += (height - ri->ri_font->fontheight);
   1059 			yd += (height - ri->ri_font->fontheight);
   1060 			for (i = 0; i < nrows; i++) {
   1061 				voyagerfb_bitblt(sc, x, ys, x, yd, width,
   1062 				    ri->ri_font->fontheight, ROP_COPY);
   1063 				ys -= ri->ri_font->fontheight;
   1064 				yd -= ri->ri_font->fontheight;
   1065 			}
   1066 		} else
   1067 			voyagerfb_bitblt(sc, x, ys, x, yd, width, height,
   1068 			    ROP_COPY);
   1069 	}
   1070 }
   1071 
   1072 static void
   1073 voyagerfb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1074 {
   1075 	struct rasops_info *ri = cookie;
   1076 	struct vcons_screen *scr = ri->ri_hw;
   1077 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1078 	int32_t x, y, width, height, fg, bg, ul;
   1079 
   1080 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1081 		x = ri->ri_xorigin;
   1082 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1083 		width = ri->ri_emuwidth;
   1084 		height = ri->ri_font->fontheight * nrows;
   1085 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1086 
   1087 		voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1088 	}
   1089 }
   1090 
   1091 /* backlight control */
   1092 static void
   1093 voyagerfb_setup_backlight(struct voyagerfb_softc *sc)
   1094 {
   1095 	/* switch the pin to gpio mode if it isn't already */
   1096 	voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1097 	/* turn it on */
   1098 	voyager_write_gpio(sc->sc_gpio_cookie, 0xffffffff, GPIO_BACKLIGHT);
   1099 	sc->sc_bl_on = 1;
   1100 	sc->sc_bl_level = 255;
   1101 	pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
   1102 	    voyagerfb_brightness_up, TRUE);
   1103 	pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
   1104 	    voyagerfb_brightness_down, TRUE);
   1105 }
   1106 
   1107 static void
   1108 voyagerfb_set_backlight(struct voyagerfb_softc *sc, int level)
   1109 {
   1110 
   1111 	/*
   1112 	 * should we do nothing when backlight is off, should we just store the
   1113 	 * level and use it when turning back on or should we just flip sc_bl_on
   1114 	 * and turn the backlight on?
   1115 	 * For now turn it on so a crashed screensaver can't get the user stuck
   1116 	 * with a dark screen as long as hotkeys work
   1117 	 */
   1118 	if (level > 255) level = 255;
   1119 	if (level < 0) level = 0;
   1120 	if (level == sc->sc_bl_level)
   1121 		return;
   1122 	sc->sc_bl_level = level;
   1123 	if (sc->sc_bl_on == 0)
   1124 		sc->sc_bl_on = 1;
   1125 	/* and here we would actually muck with the hardware */
   1126 	if ((level == 0) || (level == 255)) {
   1127 		/* in these cases bypass the PWM and use the gpio */
   1128 		voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1129 		if (level == 0) {
   1130 			voyager_write_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1131 		} else {
   1132 			voyager_write_gpio(sc->sc_gpio_cookie, 0xffffffff, GPIO_BACKLIGHT);
   1133 		}
   1134 	} else {
   1135 		uint32_t pwm;
   1136 
   1137 		pwm = voyager_set_pwm(20000, level * 1000 / 256);
   1138 		pwm |= SM502_PWM_ENABLE;
   1139 		bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PWM0, pwm);
   1140 
   1141 		/* let the PWM take over */
   1142 		voyager_control_gpio(sc->sc_gpio_cookie, 0xffffffff, GPIO_BACKLIGHT);
   1143 	}
   1144 }
   1145 
   1146 static void
   1147 voyagerfb_switch_backlight(struct voyagerfb_softc *sc, int on)
   1148 {
   1149 
   1150 	if (on == sc->sc_bl_on)
   1151 		return;
   1152 	sc->sc_bl_on = on;
   1153 	if (on) {
   1154 		int level = sc->sc_bl_level;
   1155 
   1156 		sc->sc_bl_level = -1;
   1157 		voyagerfb_set_backlight(sc, level);
   1158 	} else {
   1159 		voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1160 		voyager_write_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1161 	}
   1162 }
   1163 
   1164 
   1165 static void
   1166 voyagerfb_brightness_up(device_t dev)
   1167 {
   1168 	struct voyagerfb_softc *sc = device_private(dev);
   1169 
   1170 	voyagerfb_set_backlight(sc, sc->sc_bl_level + 8);
   1171 }
   1172 
   1173 static void
   1174 voyagerfb_brightness_down(device_t dev)
   1175 {
   1176 	struct voyagerfb_softc *sc = device_private(dev);
   1177 
   1178 	voyagerfb_set_backlight(sc, sc->sc_bl_level - 8);
   1179 }
   1180 
   1181 static int
   1182 voyagerfb_set_curpos(struct voyagerfb_softc *sc, int x, int y)
   1183 {
   1184 	uint32_t val;
   1185 	int xx, yy;
   1186 
   1187 	sc->sc_cur_x = x;
   1188 	sc->sc_cur_y = y;
   1189 
   1190 	xx = x - sc->sc_hot_x;
   1191 	yy = y - sc->sc_hot_y;
   1192 
   1193 	if (xx < 0) xx = abs(xx) | 0x800;
   1194 	if (yy < 0) yy = abs(yy) | 0x800;
   1195 
   1196 	val = (xx & 0xffff) | (yy << 16);
   1197 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_XY, val);
   1198 
   1199 	return 0;
   1200 }
   1201 
   1202 static int
   1203 voyagerfb_gcursor(struct voyagerfb_softc *sc, struct wsdisplay_cursor *cur)
   1204 {
   1205 	/* do nothing for now */
   1206 	return 0;
   1207 }
   1208 
   1209 static int
   1210 voyagerfb_scursor(struct voyagerfb_softc *sc, struct wsdisplay_cursor *cur)
   1211 {
   1212 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1213 
   1214 		bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1215 		    SM502_PANEL_CRSR_ADDR,
   1216 		    sc->sc_cursor_addr | (cur->enable ? SM502_CRSR_ENABLE : 0));
   1217 		DPRINTF("%s: %08x\n", __func__, sc->sc_cursor_addr);
   1218 	}
   1219 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1220 
   1221 		sc->sc_hot_x = cur->hot.x;
   1222 		sc->sc_hot_y = cur->hot.y;
   1223 	}
   1224 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1225 
   1226 		voyagerfb_set_curpos(sc, cur->pos.x, cur->pos.y);
   1227 	}
   1228 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1229 		int i, idx;
   1230 		uint32_t val;
   1231 
   1232 		for (i = 0; i < cur->cmap.count; i++) {
   1233 			val = ((cur->cmap.red[i] & 0xf8) << 8) |
   1234 			      ((cur->cmap.green[i] & 0xfc) << 3) |
   1235 			      ((cur->cmap.blue[i] & 0xf8) >> 3);
   1236 			idx = i + cur->cmap.index;
   1237 			bus_space_write_2(sc->sc_memt, sc->sc_regh,
   1238 			    SM502_PANEL_CRSR_COL12 + (idx << 1),
   1239 			    val);
   1240 			/*
   1241 			 * if userland doesn't try to set the 3rd colour we
   1242 			 * assume it expects an X11-style 2 colour cursor
   1243 			 * X should be our main user anyway
   1244 			 */
   1245 			if ((idx == 1) &&
   1246 			   ((cur->cmap.count + cur->cmap.index) < 3)) {
   1247 				bus_space_write_2(sc->sc_memt, sc->sc_regh,
   1248 				    SM502_PANEL_CRSR_COL3,
   1249 				    val);
   1250 			}
   1251 			DPRINTF("%s: %d %04x\n", __func__, i + cur->cmap.index, val);
   1252 		}
   1253 	}
   1254 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1255 
   1256 		int i, j, cnt = 0;
   1257 		uint32_t latch = 0, omask;
   1258 		uint8_t imask;
   1259 		DPRINTF("%s: %d %d\n", __func__, cur->size.x, cur->size.y);
   1260 		for (i = 0; i < 256; i++) {
   1261 			omask = 0x00000001;
   1262 			imask = 0x01;
   1263 			cur->image[cnt] &= cur->mask[cnt];
   1264 			for (j = 0; j < 8; j++) {
   1265 				if (cur->mask[cnt] & imask)
   1266 					latch |= omask;
   1267 				omask <<= 1;
   1268 				if (cur->image[cnt] & imask)
   1269 					latch |= omask;
   1270 				omask <<= 1;
   1271 				imask <<= 1;
   1272 			}
   1273 			cnt++;
   1274 			imask = 0x01;
   1275 			cur->image[cnt] &= cur->mask[cnt];
   1276 			for (j = 0; j < 8; j++) {
   1277 				if (cur->mask[cnt] & imask)
   1278 					latch |= omask;
   1279 				omask <<= 1;
   1280 				if (cur->image[cnt] & imask)
   1281 					latch |= omask;
   1282 				omask <<= 1;
   1283 				imask <<= 1;
   1284 			}
   1285 			cnt++;
   1286 			sc->sc_cursor[i] = latch;
   1287 			latch = 0;
   1288 		}
   1289 	}
   1290 	return 0;
   1291 }
   1292