Home | History | Annotate | Line # | Download | only in voyager
voyagerfb.c revision 1.20
      1 /*	$NetBSD: voyagerfb.c,v 1.20 2012/04/19 09:03:01 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.20 2012/04/19 09:03:01 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_fbsize / sc->sc_stride) - sc->sc_height,
    301 			sc->sc_width,
    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 
    552 	if (sc->sc_depth == 32) {
    553 #ifdef VOYAGERFB_ANTIALIAS
    554 		ri->ri_flg |= RI_ENABLE_ALPHA;
    555 #endif
    556 		/* we always run in RGB */
    557 		ri->ri_rnum = 8;
    558 		ri->ri_gnum = 8;
    559 		ri->ri_bnum = 8;
    560 		ri->ri_rpos = 16;
    561 		ri->ri_gpos = 8;
    562 		ri->ri_bpos = 0;
    563 	}
    564 
    565 	rasops_init(ri, 0, 0);
    566 	ri->ri_caps = WSSCREEN_WSCOLORS;
    567 
    568 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    569 		    sc->sc_width / ri->ri_font->fontwidth);
    570 
    571 	ri->ri_hw = scr;
    572 	ri->ri_ops.copyrows = voyagerfb_copyrows;
    573 	ri->ri_ops.copycols = voyagerfb_copycols;
    574 	ri->ri_ops.eraserows = voyagerfb_eraserows;
    575 	ri->ri_ops.erasecols = voyagerfb_erasecols;
    576 	ri->ri_ops.cursor = voyagerfb_cursor;
    577 	if (FONT_IS_ALPHA(ri->ri_font)) {
    578 		ri->ri_ops.putchar = voyagerfb_putchar_aa32;
    579 	} else
    580 		ri->ri_ops.putchar = voyagerfb_putchar_mono;
    581 }
    582 
    583 static int
    584 voyagerfb_putcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
    585 {
    586 	u_char *r, *g, *b;
    587 	u_int index = cm->index;
    588 	u_int count = cm->count;
    589 	int i, error;
    590 	u_char rbuf[256], gbuf[256], bbuf[256];
    591 
    592 #ifdef VOYAGERFB_DEBUG
    593 	aprint_debug("putcmap: %d %d\n",index, count);
    594 #endif
    595 	if (cm->index >= 256 || cm->count > 256 ||
    596 	    (cm->index + cm->count) > 256)
    597 		return EINVAL;
    598 	error = copyin(cm->red, &rbuf[index], count);
    599 	if (error)
    600 		return error;
    601 	error = copyin(cm->green, &gbuf[index], count);
    602 	if (error)
    603 		return error;
    604 	error = copyin(cm->blue, &bbuf[index], count);
    605 	if (error)
    606 		return error;
    607 
    608 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    609 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    610 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    611 
    612 	r = &sc->sc_cmap_red[index];
    613 	g = &sc->sc_cmap_green[index];
    614 	b = &sc->sc_cmap_blue[index];
    615 
    616 	for (i = 0; i < count; i++) {
    617 		voyagerfb_putpalreg(sc, index, *r, *g, *b);
    618 		index++;
    619 		r++, g++, b++;
    620 	}
    621 	return 0;
    622 }
    623 
    624 static int
    625 voyagerfb_getcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
    626 {
    627 	u_int index = cm->index;
    628 	u_int count = cm->count;
    629 	int error;
    630 
    631 	if (index >= 255 || count > 256 || index + count > 256)
    632 		return EINVAL;
    633 
    634 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    635 	if (error)
    636 		return error;
    637 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    638 	if (error)
    639 		return error;
    640 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    641 	if (error)
    642 		return error;
    643 
    644 	return 0;
    645 }
    646 
    647 static void
    648 voyagerfb_restore_palette(struct voyagerfb_softc *sc)
    649 {
    650 	int i;
    651 
    652 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    653 		voyagerfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    654 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    655 	}
    656 }
    657 
    658 static int
    659 voyagerfb_putpalreg(struct voyagerfb_softc *sc, int idx, uint8_t r,
    660     uint8_t g, uint8_t b)
    661 {
    662 	uint32_t reg;
    663 
    664 	reg = (r << 16) | (g << 8) | b;
    665 	/* XXX we should probably write the CRT palette too */
    666 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    667 	    SM502_PALETTE_PANEL + (idx << 2), reg);
    668 	return 0;
    669 }
    670 
    671 static void
    672 voyagerfb_init(struct voyagerfb_softc *sc)
    673 {
    674 	int reg;
    675 
    676 	voyagerfb_wait(sc);
    677 	/* disable colour compare */
    678 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_COLOR_COMP_MASK, 0);
    679 	/* allow writes to all planes */
    680 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PLANEMASK,
    681 	    0xffffffff);
    682 	/* disable clipping */
    683 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CLIP_TOP_LEFT, 0);
    684 	/* source and destination in local memory, no offset */
    685 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC_BASE, 0);
    686 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST_BASE, 0);
    687 	/* pitch is screen stride */
    688 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PITCH,
    689 	    sc->sc_width | (sc->sc_width << 16));
    690 	/* window is screen width */
    691 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_WINDOW_WIDTH,
    692 	    sc->sc_width | (sc->sc_width << 16));
    693 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL);
    694 	reg &= ~SM502_PDC_DEPTH_MASK;
    695 
    696 	switch (sc->sc_depth) {
    697 		case 8:
    698 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    699 			    SM502_STRETCH, SM502_STRETCH_8BIT);
    700 			sc->sc_stride = sc->sc_width;
    701 			reg |= SM502_PDC_8BIT;
    702 			break;
    703 		case 16:
    704 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    705 			    SM502_STRETCH, SM502_STRETCH_16BIT);
    706 			sc->sc_stride = sc->sc_width << 1;
    707 			reg |= SM502_PDC_16BIT;
    708 			break;
    709 		case 24:
    710 		case 32:
    711 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    712 			    SM502_STRETCH, SM502_STRETCH_32BIT);
    713 			sc->sc_stride = sc->sc_width << 2;
    714 			reg |= SM502_PDC_32BIT;
    715 			break;
    716 	}
    717 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_FB_OFFSET,
    718 	    (sc->sc_stride << 16) | sc->sc_stride);
    719 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL,
    720 	    reg);
    721 
    722 	/* put the cursor at the end of video memory */
    723 	sc->sc_cursor_addr = 16 * 1024 * 1024 - 16 * 64;	/* XXX */
    724 	DPRINTF("%s: %08x\n", __func__, sc->sc_cursor_addr);
    725 	sc->sc_cursor = (uint32_t *)((uint8_t *)bus_space_vaddr(sc->sc_memt, sc->sc_fbh)
    726 			 + sc->sc_cursor_addr);
    727 #ifdef VOYAGERFB_DEBUG
    728 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_XY, 0x00100010);
    729 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_COL12, 0x0000ffff);
    730 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_COL3, 0x0000f800);
    731 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_ADDR,
    732 	    SM502_CRSR_ENABLE | sc->sc_cursor_addr);
    733 	sc->sc_cursor[0] = 0x00000000;
    734 	sc->sc_cursor[1] = 0x00000000;
    735 	sc->sc_cursor[2] = 0xffffffff;
    736 	sc->sc_cursor[3] = 0xffffffff;
    737 	sc->sc_cursor[4] = 0xaaaaaaaa;
    738 	sc->sc_cursor[5] = 0xaaaaaaaa;
    739 	sc->sc_cursor[6] = 0xffffffff;
    740 	sc->sc_cursor[7] = 0x00000000;
    741 #else
    742 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_ADDR,
    743 	    sc->sc_cursor_addr);
    744 #endif
    745 }
    746 
    747 static void
    748 voyagerfb_rectfill(struct voyagerfb_softc *sc, int x, int y, int wi, int he,
    749      uint32_t colour)
    750 {
    751 
    752 	voyagerfb_ready(sc);
    753 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL,
    754 	    ROP_COPY |
    755 	    SM502_CTRL_USE_ROP2 |
    756 	    SM502_CTRL_CMD_RECTFILL |
    757 	    SM502_CTRL_QUICKSTART_E);
    758 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND,
    759 	    colour);
    760 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
    761 	    (x << 16) | y);
    762 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
    763 	    (wi << 16) | he);
    764 }
    765 
    766 static void
    767 voyagerfb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
    768     int wi, int he, int rop)
    769 {
    770 	struct voyagerfb_softc *sc = cookie;
    771 	uint32_t cmd;
    772 
    773 	cmd = (rop & 0xf) | SM502_CTRL_USE_ROP2 | SM502_CTRL_CMD_BITBLT |
    774 	      SM502_CTRL_QUICKSTART_E;
    775 
    776 	voyagerfb_ready(sc);
    777 
    778 	if (xd <= xs) {
    779 		/* left to right */
    780 	} else {
    781 		/*
    782 		 * According to the manual this flag reverses only the blitter's
    783 		 * X direction. At least on my Gdium it also reverses the Y
    784 		 * direction
    785 		 */
    786 		cmd |= SM502_CTRL_R_TO_L;
    787 		xs += wi - 1;
    788 		xd += wi - 1;
    789 		ys += he - 1;
    790 		yd += he - 1;
    791 	}
    792 
    793 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
    794 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC,
    795 	    (xs << 16) | ys);
    796 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
    797 	    (xd << 16) | yd);
    798 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
    799 	    (wi << 16) | he);
    800 }
    801 
    802 static void
    803 voyagerfb_cursor(void *cookie, int on, int row, int col)
    804 {
    805 	struct rasops_info *ri = cookie;
    806 	struct vcons_screen *scr = ri->ri_hw;
    807 	struct voyagerfb_softc *sc = scr->scr_cookie;
    808 	int x, y, wi, he;
    809 
    810 	wi = ri->ri_font->fontwidth;
    811 	he = ri->ri_font->fontheight;
    812 
    813 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    814 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    815 		y = ri->ri_crow * he + ri->ri_yorigin;
    816 		if (ri->ri_flg & RI_CURSOR) {
    817 			voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
    818 			ri->ri_flg &= ~RI_CURSOR;
    819 		}
    820 		ri->ri_crow = row;
    821 		ri->ri_ccol = col;
    822 		if (on) {
    823 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    824 			y = ri->ri_crow * he + ri->ri_yorigin;
    825 			voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
    826 			ri->ri_flg |= RI_CURSOR;
    827 		}
    828 	} else {
    829 		scr->scr_ri.ri_crow = row;
    830 		scr->scr_ri.ri_ccol = col;
    831 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    832 	}
    833 
    834 }
    835 
    836 static inline void
    837 voyagerfb_feed8(struct voyagerfb_softc *sc, uint8_t *data, int len)
    838 {
    839 	uint32_t *port = (uint32_t *)sc->sc_dataport;
    840 	int i;
    841 
    842 	for (i = 0; i < ((len + 3) & 0xfffc); i++) {
    843 		*port = *data;
    844 		data++;
    845 	}
    846 }
    847 
    848 static inline void
    849 voyagerfb_feed16(struct voyagerfb_softc *sc, uint16_t *data, int len)
    850 {
    851 	uint32_t *port = (uint32_t *)sc->sc_dataport;
    852 	int i;
    853 
    854 	len = len << 1;
    855 	for (i = 0; i < ((len + 1) & 0xfffe); i++) {
    856 		*port = *data;
    857 		data++;
    858 	}
    859 }
    860 
    861 static void
    862 voyagerfb_putchar_mono(void *cookie, int row, int col, u_int c, long attr)
    863 {
    864 	struct rasops_info *ri = cookie;
    865 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    866 	struct vcons_screen *scr = ri->ri_hw;
    867 	struct voyagerfb_softc *sc = scr->scr_cookie;
    868 	uint32_t cmd;
    869 	int fg, bg;
    870 	uint8_t *data;
    871 	int x, y, wi, he;
    872 
    873 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
    874 		return;
    875 
    876 	if (!CHAR_IN_FONT(c, font))
    877 		return;
    878 
    879 	wi = font->fontwidth;
    880 	he = font->fontheight;
    881 
    882 	bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
    883 	fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
    884 	x = ri->ri_xorigin + col * wi;
    885 	y = ri->ri_yorigin + row * he;
    886 	if (c == 0x20) {
    887 		voyagerfb_rectfill(sc, x, y, wi, he, bg);
    888 		return;
    889 	}
    890 
    891 	data = WSFONT_GLYPH(c, font);
    892 
    893 	cmd = ROP_COPY |
    894 	      SM502_CTRL_USE_ROP2 |
    895 	      SM502_CTRL_CMD_HOSTWRT |
    896 	      SM502_CTRL_HOSTBLT_MONO |
    897 	      SM502_CTRL_QUICKSTART_E |
    898 	      SM502_CTRL_MONO_PACK_32BIT;
    899 	voyagerfb_ready(sc);
    900 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
    901 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND, fg);
    902 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_BACKGROUND, bg);
    903 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, 0);
    904 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, (x << 16) | y);
    905 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    906 	    SM502_DIMENSION, (wi << 16) | he);
    907 	/* now feed the data, padded to 32bit */
    908 	switch (ri->ri_font->stride) {
    909 		case 1:
    910 			voyagerfb_feed8(sc, data, ri->ri_fontscale);
    911 			break;
    912 		case 2:
    913 			voyagerfb_feed16(sc, (uint16_t *)data,
    914 			    ri->ri_fontscale);
    915 			break;
    916 	}
    917 }
    918 
    919 static void
    920 voyagerfb_putchar_aa32(void *cookie, int row, int col, u_int c, long attr)
    921 {
    922 	struct rasops_info *ri = cookie;
    923 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    924 	struct vcons_screen *scr = ri->ri_hw;
    925 	struct voyagerfb_softc *sc = scr->scr_cookie;
    926 	uint32_t cmd;
    927 	int fg, bg;
    928 	uint8_t *data;
    929 	int x, y, wi, he;
    930 	int i, j, r, g, b, aval, pad;
    931 	int rf, gf, bf, rb, gb, bb;
    932 	uint32_t pixel;
    933 	int rv;
    934 
    935 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
    936 		return;
    937 
    938 	if (!CHAR_IN_FONT(c, font))
    939 		return;
    940 
    941 	wi = font->fontwidth;
    942 	he = font->fontheight;
    943 
    944 	bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
    945 	fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
    946 	x = ri->ri_xorigin + col * wi;
    947 	y = ri->ri_yorigin + row * he;
    948 	if (c == 0x20) {
    949 		voyagerfb_rectfill(sc, x, y, wi, he, bg);
    950 		return;
    951 	}
    952 
    953 	data = WSFONT_GLYPH(c, font);
    954 	/*
    955 	 * we can't accelerate the actual alpha blending but
    956 	 * we can at least use a host blit to go through the
    957 	 * pipeline instead of having to sync the engine
    958 	 */
    959 
    960 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
    961 	if (rv == GC_OK)
    962 		return;
    963 
    964 	cmd = ROP_COPY |
    965 	      SM502_CTRL_USE_ROP2 |
    966 	      SM502_CTRL_CMD_HOSTWRT |
    967 	      SM502_CTRL_QUICKSTART_E;
    968 	voyagerfb_ready(sc);
    969 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
    970 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, 0);
    971 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, (x << 16) | y);
    972 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION, (wi << 16) | he);
    973 	rf = (fg >> 16) & 0xff;
    974 	rb = (bg >> 16) & 0xff;
    975 	gf = (fg >> 8) & 0xff;
    976 	gb = (bg >> 8) & 0xff;
    977 	bf =  fg & 0xff;
    978 	bb =  bg & 0xff;
    979 	pad = wi & 1;
    980 	for (i = 0; i < he; i++) {
    981 		for (j = 0; j < wi; j++) {
    982 			aval = *data;
    983 			data++;
    984 			if (aval == 0) {
    985 				pixel = bg;
    986 			} else if (aval == 255) {
    987 				pixel = fg;
    988 			} else {
    989 				r = aval * rf + (255 - aval) * rb;
    990 				g = aval * gf + (255 - aval) * gb;
    991 				b = aval * bf + (255 - aval) * bb;
    992 				pixel = (r & 0xff00) << 8 |
    993 				        (g & 0xff00) |
    994 				        (b & 0xff00) >> 8;
    995 			}
    996 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    997 			    SM502_DATAPORT, pixel);
    998 		}
    999 		if (pad)
   1000 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1001 			    SM502_DATAPORT, 0);
   1002 	}
   1003 	if (rv == GC_ADD) {
   1004 		glyphcache_add(&sc->sc_gc, c, x, y);
   1005 	}
   1006 }
   1007 
   1008 static void
   1009 voyagerfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1010 {
   1011 	struct rasops_info *ri = cookie;
   1012 	struct vcons_screen *scr = ri->ri_hw;
   1013 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1014 	int32_t xs, xd, y, width, height;
   1015 
   1016 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1017 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1018 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1019 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1020 		width = ri->ri_font->fontwidth * ncols;
   1021 		height = ri->ri_font->fontheight;
   1022 		voyagerfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
   1023 	}
   1024 }
   1025 
   1026 static void
   1027 voyagerfb_erasecols(void *cookie, int row, int startcol, int ncols,
   1028      long fillattr)
   1029 {
   1030 	struct rasops_info *ri = cookie;
   1031 	struct vcons_screen *scr = ri->ri_hw;
   1032 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1033 	int32_t x, y, width, height, fg, bg, ul;
   1034 
   1035 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1036 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1037 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1038 		width = ri->ri_font->fontwidth * ncols;
   1039 		height = ri->ri_font->fontheight;
   1040 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1041 
   1042 		voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1043 	}
   1044 }
   1045 
   1046 static void
   1047 voyagerfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1048 {
   1049 	struct rasops_info *ri = cookie;
   1050 	struct vcons_screen *scr = ri->ri_hw;
   1051 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1052 	int32_t x, ys, yd, width, height;
   1053 	int i;
   1054 
   1055 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1056 		x = ri->ri_xorigin;
   1057 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1058 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1059 		width = ri->ri_emuwidth;
   1060 		height = ri->ri_font->fontheight * nrows;
   1061 		if ((nrows > 1) && (dstrow > srcrow)) {
   1062 			/*
   1063 			 * the blitter can't do bottom-up copies so we have
   1064 			 * to copy line by line here
   1065 			 * should probably use a command sequence
   1066 			 */
   1067 			ys += (height - ri->ri_font->fontheight);
   1068 			yd += (height - ri->ri_font->fontheight);
   1069 			for (i = 0; i < nrows; i++) {
   1070 				voyagerfb_bitblt(sc, x, ys, x, yd, width,
   1071 				    ri->ri_font->fontheight, ROP_COPY);
   1072 				ys -= ri->ri_font->fontheight;
   1073 				yd -= ri->ri_font->fontheight;
   1074 			}
   1075 		} else
   1076 			voyagerfb_bitblt(sc, x, ys, x, yd, width, height,
   1077 			    ROP_COPY);
   1078 	}
   1079 }
   1080 
   1081 static void
   1082 voyagerfb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1083 {
   1084 	struct rasops_info *ri = cookie;
   1085 	struct vcons_screen *scr = ri->ri_hw;
   1086 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1087 	int32_t x, y, width, height, fg, bg, ul;
   1088 
   1089 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1090 		x = ri->ri_xorigin;
   1091 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1092 		width = ri->ri_emuwidth;
   1093 		height = ri->ri_font->fontheight * nrows;
   1094 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1095 
   1096 		voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1097 	}
   1098 }
   1099 
   1100 /* backlight control */
   1101 static void
   1102 voyagerfb_setup_backlight(struct voyagerfb_softc *sc)
   1103 {
   1104 	/* switch the pin to gpio mode if it isn't already */
   1105 	voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1106 	/* turn it on */
   1107 	voyager_write_gpio(sc->sc_gpio_cookie, 0xffffffff, GPIO_BACKLIGHT);
   1108 	sc->sc_bl_on = 1;
   1109 	sc->sc_bl_level = 255;
   1110 	pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
   1111 	    voyagerfb_brightness_up, TRUE);
   1112 	pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
   1113 	    voyagerfb_brightness_down, TRUE);
   1114 }
   1115 
   1116 static void
   1117 voyagerfb_set_backlight(struct voyagerfb_softc *sc, int level)
   1118 {
   1119 
   1120 	/*
   1121 	 * should we do nothing when backlight is off, should we just store the
   1122 	 * level and use it when turning back on or should we just flip sc_bl_on
   1123 	 * and turn the backlight on?
   1124 	 * For now turn it on so a crashed screensaver can't get the user stuck
   1125 	 * with a dark screen as long as hotkeys work
   1126 	 */
   1127 	if (level > 255) level = 255;
   1128 	if (level < 0) level = 0;
   1129 	if (level == sc->sc_bl_level)
   1130 		return;
   1131 	sc->sc_bl_level = level;
   1132 	if (sc->sc_bl_on == 0)
   1133 		sc->sc_bl_on = 1;
   1134 	/* and here we would actually muck with the hardware */
   1135 	if ((level == 0) || (level == 255)) {
   1136 		/* in these cases bypass the PWM and use the gpio */
   1137 		voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1138 		if (level == 0) {
   1139 			voyager_write_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1140 		} else {
   1141 			voyager_write_gpio(sc->sc_gpio_cookie, 0xffffffff, GPIO_BACKLIGHT);
   1142 		}
   1143 	} else {
   1144 		uint32_t pwm;
   1145 
   1146 		pwm = voyager_set_pwm(20000, level * 1000 / 256);
   1147 		pwm |= SM502_PWM_ENABLE;
   1148 		bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PWM0, pwm);
   1149 
   1150 		/* let the PWM take over */
   1151 		voyager_control_gpio(sc->sc_gpio_cookie, 0xffffffff, GPIO_BACKLIGHT);
   1152 	}
   1153 }
   1154 
   1155 static void
   1156 voyagerfb_switch_backlight(struct voyagerfb_softc *sc, int on)
   1157 {
   1158 
   1159 	if (on == sc->sc_bl_on)
   1160 		return;
   1161 	sc->sc_bl_on = on;
   1162 	if (on) {
   1163 		int level = sc->sc_bl_level;
   1164 
   1165 		sc->sc_bl_level = -1;
   1166 		voyagerfb_set_backlight(sc, level);
   1167 	} else {
   1168 		voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1169 		voyager_write_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1170 	}
   1171 }
   1172 
   1173 
   1174 static void
   1175 voyagerfb_brightness_up(device_t dev)
   1176 {
   1177 	struct voyagerfb_softc *sc = device_private(dev);
   1178 
   1179 	voyagerfb_set_backlight(sc, sc->sc_bl_level + 8);
   1180 }
   1181 
   1182 static void
   1183 voyagerfb_brightness_down(device_t dev)
   1184 {
   1185 	struct voyagerfb_softc *sc = device_private(dev);
   1186 
   1187 	voyagerfb_set_backlight(sc, sc->sc_bl_level - 8);
   1188 }
   1189 
   1190 static int
   1191 voyagerfb_set_curpos(struct voyagerfb_softc *sc, int x, int y)
   1192 {
   1193 	uint32_t val;
   1194 	int xx, yy;
   1195 
   1196 	sc->sc_cur_x = x;
   1197 	sc->sc_cur_y = y;
   1198 
   1199 	xx = x - sc->sc_hot_x;
   1200 	yy = y - sc->sc_hot_y;
   1201 
   1202 	if (xx < 0) xx = abs(xx) | 0x800;
   1203 	if (yy < 0) yy = abs(yy) | 0x800;
   1204 
   1205 	val = (xx & 0xffff) | (yy << 16);
   1206 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_XY, val);
   1207 
   1208 	return 0;
   1209 }
   1210 
   1211 static int
   1212 voyagerfb_gcursor(struct voyagerfb_softc *sc, struct wsdisplay_cursor *cur)
   1213 {
   1214 	/* do nothing for now */
   1215 	return 0;
   1216 }
   1217 
   1218 static int
   1219 voyagerfb_scursor(struct voyagerfb_softc *sc, struct wsdisplay_cursor *cur)
   1220 {
   1221 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1222 
   1223 		bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1224 		    SM502_PANEL_CRSR_ADDR,
   1225 		    sc->sc_cursor_addr | (cur->enable ? SM502_CRSR_ENABLE : 0));
   1226 		DPRINTF("%s: %08x\n", __func__, sc->sc_cursor_addr);
   1227 	}
   1228 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1229 
   1230 		sc->sc_hot_x = cur->hot.x;
   1231 		sc->sc_hot_y = cur->hot.y;
   1232 	}
   1233 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1234 
   1235 		voyagerfb_set_curpos(sc, cur->pos.x, cur->pos.y);
   1236 	}
   1237 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1238 		int i, idx;
   1239 		uint32_t val;
   1240 
   1241 		for (i = 0; i < cur->cmap.count; i++) {
   1242 			val = ((cur->cmap.red[i] & 0xf8) << 8) |
   1243 			      ((cur->cmap.green[i] & 0xfc) << 3) |
   1244 			      ((cur->cmap.blue[i] & 0xf8) >> 3);
   1245 			idx = i + cur->cmap.index;
   1246 			bus_space_write_2(sc->sc_memt, sc->sc_regh,
   1247 			    SM502_PANEL_CRSR_COL12 + (idx << 1),
   1248 			    val);
   1249 			/*
   1250 			 * if userland doesn't try to set the 3rd colour we
   1251 			 * assume it expects an X11-style 2 colour cursor
   1252 			 * X should be our main user anyway
   1253 			 */
   1254 			if ((idx == 1) &&
   1255 			   ((cur->cmap.count + cur->cmap.index) < 3)) {
   1256 				bus_space_write_2(sc->sc_memt, sc->sc_regh,
   1257 				    SM502_PANEL_CRSR_COL3,
   1258 				    val);
   1259 			}
   1260 			DPRINTF("%s: %d %04x\n", __func__, i + cur->cmap.index, val);
   1261 		}
   1262 	}
   1263 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1264 
   1265 		int i, j, cnt = 0;
   1266 		uint32_t latch = 0, omask;
   1267 		uint8_t imask;
   1268 		DPRINTF("%s: %d %d\n", __func__, cur->size.x, cur->size.y);
   1269 		for (i = 0; i < 256; i++) {
   1270 			omask = 0x00000001;
   1271 			imask = 0x01;
   1272 			cur->image[cnt] &= cur->mask[cnt];
   1273 			for (j = 0; j < 8; j++) {
   1274 				if (cur->mask[cnt] & imask)
   1275 					latch |= omask;
   1276 				omask <<= 1;
   1277 				if (cur->image[cnt] & imask)
   1278 					latch |= omask;
   1279 				omask <<= 1;
   1280 				imask <<= 1;
   1281 			}
   1282 			cnt++;
   1283 			imask = 0x01;
   1284 			cur->image[cnt] &= cur->mask[cnt];
   1285 			for (j = 0; j < 8; j++) {
   1286 				if (cur->mask[cnt] & imask)
   1287 					latch |= omask;
   1288 				omask <<= 1;
   1289 				if (cur->image[cnt] & imask)
   1290 					latch |= omask;
   1291 				omask <<= 1;
   1292 				imask <<= 1;
   1293 			}
   1294 			cnt++;
   1295 			sc->sc_cursor[i] = latch;
   1296 			latch = 0;
   1297 		}
   1298 	}
   1299 	return 0;
   1300 }
   1301