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