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