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