Home | History | Annotate | Line # | Download | only in voyager
voyagerfb.c revision 1.5
      1 /*	$NetBSD: voyagerfb.c,v 1.5 2011/09/22 06:16:13 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.5 2011/09/22 06:16:13 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 /* there are probably gdium-specific */
     63 #define GPIO_BACKLIGHT	0x20000000
     64 
     65 struct voyagerfb_softc {
     66 	device_t sc_dev;
     67 
     68 	pci_chipset_tag_t sc_pc;
     69 	pcitag_t sc_pcitag;
     70 	bus_space_tag_t sc_memt;
     71 
     72 	bus_space_handle_t sc_fbh;
     73 	bus_space_handle_t sc_regh;
     74 	bus_addr_t sc_fb, sc_reg;
     75 	bus_size_t sc_fbsize, sc_regsize;
     76 
     77 	int sc_width, sc_height, sc_depth, sc_stride;
     78 	int sc_locked;
     79 	void *sc_fbaddr;
     80 	struct vcons_screen sc_console_screen;
     81 	struct wsscreen_descr sc_defaultscreen_descr;
     82 	const struct wsscreen_descr *sc_screens[1];
     83 	struct wsscreen_list sc_screenlist;
     84 	struct vcons_data vd;
     85 	uint8_t *sc_dataport;
     86 	int sc_mode;
     87 	int sc_bl_on, sc_bl_level;
     88 	void *sc_gpio_cookie;
     89 	u_char sc_cmap_red[256];
     90 	u_char sc_cmap_green[256];
     91 	u_char sc_cmap_blue[256];
     92 };
     93 
     94 static int	voyagerfb_match(device_t, cfdata_t, void *);
     95 static void	voyagerfb_attach(device_t, device_t, void *);
     96 
     97 CFATTACH_DECL_NEW(voyagerfb, sizeof(struct voyagerfb_softc),
     98     voyagerfb_match, voyagerfb_attach, NULL, NULL);
     99 
    100 extern const u_char rasops_cmap[768];
    101 
    102 static int	voyagerfb_ioctl(void *, void *, u_long, void *, int,
    103 			     struct lwp *);
    104 static paddr_t	voyagerfb_mmap(void *, void *, off_t, int);
    105 static void	voyagerfb_init_screen(void *, struct vcons_screen *, int,
    106 		 long *);
    107 
    108 static int	voyagerfb_putcmap(struct voyagerfb_softc *,
    109 		 struct wsdisplay_cmap *);
    110 static int 	voyagerfb_getcmap(struct voyagerfb_softc *,
    111 		 struct wsdisplay_cmap *);
    112 static void	voyagerfb_restore_palette(struct voyagerfb_softc *);
    113 static int 	voyagerfb_putpalreg(struct voyagerfb_softc *, int, uint8_t,
    114 			    uint8_t, uint8_t);
    115 
    116 static void	voyagerfb_init(struct voyagerfb_softc *);
    117 
    118 static void	voyagerfb_rectfill(struct voyagerfb_softc *, int, int, int, int,
    119 			    uint32_t);
    120 static void	voyagerfb_bitblt(struct voyagerfb_softc *, int, int, int, int,
    121 			    int, int, int);
    122 
    123 static void	voyagerfb_cursor(void *, int, int, int);
    124 static void	voyagerfb_putchar(void *, int, int, u_int, long);
    125 static void	voyagerfb_copycols(void *, int, int, int, int);
    126 static void	voyagerfb_erasecols(void *, int, int, int, long);
    127 static void	voyagerfb_copyrows(void *, int, int, int);
    128 static void	voyagerfb_eraserows(void *, int, int, long);
    129 
    130 struct wsdisplay_accessops voyagerfb_accessops = {
    131 	voyagerfb_ioctl,
    132 	voyagerfb_mmap,
    133 	NULL,	/* alloc_screen */
    134 	NULL,	/* free_screen */
    135 	NULL,	/* show_screen */
    136 	NULL, 	/* load_font */
    137 	NULL,	/* pollc */
    138 	NULL	/* scroll */
    139 };
    140 
    141 static void	voyagerfb_brightness_up(device_t);
    142 static void	voyagerfb_brightness_down(device_t);
    143 /* set backlight level */
    144 static void	voyagerfb_set_backlight(struct voyagerfb_softc *, int);
    145 /* turn backlight on and off without messing with the level */
    146 static void	voyagerfb_switch_backlight(struct voyagerfb_softc *, int);
    147 
    148 /* wait for FIFO empty so we can feed it another command */
    149 static inline void
    150 voyagerfb_ready(struct voyagerfb_softc *sc)
    151 {
    152 	do {} while ((bus_space_read_4(sc->sc_memt, sc->sc_regh,
    153 	    SM502_SYSTEM_CTRL) & SM502_SYSCTL_FIFO_EMPTY) == 0);
    154 }
    155 
    156 /* wait for the drawing engine to be idle */
    157 static inline void
    158 voyagerfb_wait(struct voyagerfb_softc *sc)
    159 {
    160 	do {} while ((bus_space_read_4(sc->sc_memt, sc->sc_regh,
    161 	    SM502_SYSTEM_CTRL) & SM502_SYSCTL_ENGINE_BUSY) != 0);
    162 }
    163 
    164 static int
    165 voyagerfb_match(device_t parent, cfdata_t match, void *aux)
    166 {
    167 	struct voyager_attach_args *vaa = (struct voyager_attach_args *)aux;
    168 
    169 	if (strcmp(vaa->vaa_name, "voyagerfb") == 0) return 100;
    170 	return 0;
    171 }
    172 
    173 static void
    174 voyagerfb_attach(device_t parent, device_t self, void *aux)
    175 {
    176 	struct voyagerfb_softc	*sc = device_private(self);
    177 	struct voyager_attach_args	*vaa = aux;
    178 	struct rasops_info	*ri;
    179 	struct wsemuldisplaydev_attach_args aa;
    180 	prop_dictionary_t	dict;
    181 	unsigned long		defattr;
    182 	uint32_t		reg;
    183 	bool			is_console;
    184 	int i, j;
    185 
    186 	sc->sc_pc = vaa->vaa_pc;
    187 	sc->sc_pcitag = vaa->vaa_pcitag;
    188 	sc->sc_memt = vaa->vaa_tag;
    189 	sc->sc_dev = self;
    190 
    191 	aprint_normal("\n");
    192 
    193 	dict = device_properties(self);
    194 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    195 
    196 	sc->sc_fb = vaa->vaa_mem_pa;
    197 	sc->sc_fbh = vaa->vaa_memh;
    198 	sc->sc_fbsize = 16 * 1024 * 1024;
    199 	sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
    200 
    201 	sc->sc_reg = vaa->vaa_reg_pa;
    202 	sc->sc_regh = vaa->vaa_regh;
    203 	sc->sc_regsize = 2 * 1024 * 1024;
    204 	sc->sc_dataport = bus_space_vaddr(sc->sc_memt, sc->sc_regh);
    205 	sc->sc_dataport += SM502_DATAPORT;
    206 
    207 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CRTL);
    208 	switch (reg & SM502_PDC_DEPTH_MASK) {
    209 		case SM502_PDC_8BIT:
    210 			sc->sc_depth = 8;
    211 			break;
    212 		case SM502_PDC_16BIT:
    213 			sc->sc_depth = 16;
    214 			break;
    215 		case SM502_PDC_32BIT:
    216 			sc->sc_depth = 24;
    217 			break;
    218 		default:
    219 			panic("%s: unsupported depth", device_xname(self));
    220 	}
    221 	sc->sc_stride = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    222 		SM502_PANEL_FB_OFFSET) & SM502_FBA_WIN_STRIDE_MASK) >> 16;
    223 	sc->sc_width = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    224 		SM502_PANEL_FB_WIDTH) & SM502_FBW_WIN_WIDTH_MASK) >> 16;
    225 	sc->sc_height = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    226 		SM502_PANEL_FB_HEIGHT) & SM502_FBH_WIN_HEIGHT_MASK) >> 16;
    227 
    228 	printf("%s: %d x %d, %d bit, stride %d\n", device_xname(self),
    229 		sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
    230 	/*
    231 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
    232 	 * but as far as I know there are no SM502 with 64bit BARs
    233 	 */
    234 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    235 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    236 
    237 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    238 		"default",
    239 		0, 0,
    240 		NULL,
    241 		8, 16,
    242 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    243 		NULL
    244 	};
    245 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    246 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    247 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    248 	sc->sc_locked = 0;
    249 
    250 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    251 	    &voyagerfb_accessops);
    252 	sc->vd.init_screen = voyagerfb_init_screen;
    253 
    254 	/* backlight control */
    255 	sc->sc_gpio_cookie = device_private(parent);
    256 	voyager_write_gpio(sc->sc_gpio_cookie, 0xffffffff, GPIO_BACKLIGHT);
    257 	sc->sc_bl_on = 1;
    258 	sc->sc_bl_level = 255;
    259 	pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
    260 	    voyagerfb_brightness_up, TRUE);
    261 	pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
    262 	    voyagerfb_brightness_down, TRUE);
    263 
    264 	/* init engine here */
    265 	voyagerfb_init(sc);
    266 
    267 	ri = &sc->sc_console_screen.scr_ri;
    268 
    269 	if (is_console) {
    270 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    271 		    &defattr);
    272 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    273 
    274 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    275 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    276 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    277 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    278 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    279 		    defattr);
    280 	} else {
    281 		/*
    282 		 * since we're not the console we can postpone the rest
    283 		 * until someone actually allocates a screen for us
    284 		 */
    285 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    286 	}
    287 
    288 	j = 0;
    289 	if (sc->sc_depth <= 8) {
    290 		for (i = 0; i < (1 << sc->sc_depth); i++) {
    291 
    292 			sc->sc_cmap_red[i] = rasops_cmap[j];
    293 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    294 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    295 			voyagerfb_putpalreg(sc, i, rasops_cmap[j],
    296 			    rasops_cmap[j + 1], rasops_cmap[j + 2]);
    297 			j += 3;
    298 		}
    299 	}
    300 
    301 	voyagerfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    302 	    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    303 
    304 	if (is_console)
    305 		vcons_replay_msgbuf(&sc->sc_console_screen);
    306 
    307 	aa.console = is_console;
    308 	aa.scrdata = &sc->sc_screenlist;
    309 	aa.accessops = &voyagerfb_accessops;
    310 	aa.accesscookie = &sc->vd;
    311 
    312 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    313 }
    314 
    315 static int
    316 voyagerfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    317 	struct lwp *l)
    318 {
    319 	struct vcons_data *vd = v;
    320 	struct voyagerfb_softc *sc = vd->cookie;
    321 	struct wsdisplay_fbinfo *wdf;
    322 	struct vcons_screen *ms = vd->active;
    323 	struct wsdisplay_param  *param;
    324 
    325 	switch (cmd) {
    326 	case WSDISPLAYIO_GTYPE:
    327 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    328 		return 0;
    329 
    330 	/* PCI config read/write passthrough. */
    331 	case PCI_IOC_CFGREAD:
    332 	case PCI_IOC_CFGWRITE:
    333 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    334 		    cmd, data, flag, l);
    335 
    336 	case WSDISPLAYIO_GET_BUSID:
    337 		return wsdisplayio_busid_pci(device_parent(sc->sc_dev),
    338 		    sc->sc_pc, sc->sc_pcitag, data);
    339 
    340 	case WSDISPLAYIO_GINFO:
    341 		if (ms == NULL)
    342 			return ENODEV;
    343 		wdf = (void *)data;
    344 		wdf->height = ms->scr_ri.ri_height;
    345 		wdf->width = ms->scr_ri.ri_width;
    346 		wdf->depth = ms->scr_ri.ri_depth;
    347 		wdf->cmsize = 256;
    348 		return 0;
    349 
    350 	case WSDISPLAYIO_GETCMAP:
    351 		return voyagerfb_getcmap(sc,
    352 		    (struct wsdisplay_cmap *)data);
    353 
    354 	case WSDISPLAYIO_PUTCMAP:
    355 		return voyagerfb_putcmap(sc,
    356 		    (struct wsdisplay_cmap *)data);
    357 
    358 	case WSDISPLAYIO_LINEBYTES:
    359 		*(u_int *)data = sc->sc_stride;
    360 		return 0;
    361 
    362 	case WSDISPLAYIO_SMODE: {
    363 		int new_mode = *(int*)data;
    364 		if (new_mode != sc->sc_mode) {
    365 			sc->sc_mode = new_mode;
    366 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    367 				voyagerfb_restore_palette(sc);
    368 				vcons_redraw_screen(ms);
    369 			}
    370 		}
    371 		}
    372 		return 0;
    373 
    374 	case WSDISPLAYIO_GVIDEO:
    375 		*(int *)data = sc->sc_bl_on ? WSDISPLAYIO_VIDEO_ON :
    376 					      WSDISPLAYIO_VIDEO_OFF;
    377 		return 0;
    378 
    379 	case WSDISPLAYIO_SVIDEO: {
    380 			int new_bl = *(int *)data;
    381 
    382 			voyagerfb_switch_backlight(sc,  new_bl);
    383 		}
    384 		return 0;
    385 
    386 	case WSDISPLAYIO_GETPARAM:
    387 		param = (struct wsdisplay_param *)data;
    388 		switch (param->param) {
    389 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    390 			param->min = 0;
    391 			param->max = 255;
    392 			param->curval = sc->sc_bl_level;
    393 			return 0;
    394 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    395 			param->min = 0;
    396 			param->max = 1;
    397 			param->curval = sc->sc_bl_on;
    398 			return 0;
    399 		}
    400 		return EPASSTHROUGH;
    401 
    402 	case WSDISPLAYIO_SETPARAM:
    403 		param = (struct wsdisplay_param *)data;
    404 		switch (param->param) {
    405 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    406 			voyagerfb_set_backlight(sc, param->curval);
    407 			return 0;
    408 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    409 			voyagerfb_switch_backlight(sc,  param->curval);
    410 			return 0;
    411 		}
    412 		return EPASSTHROUGH;
    413 	}
    414 	return EPASSTHROUGH;
    415 }
    416 
    417 static paddr_t
    418 voyagerfb_mmap(void *v, void *vs, off_t offset, int prot)
    419 {
    420 	struct vcons_data *vd = v;
    421 	struct voyagerfb_softc *sc = vd->cookie;
    422 	paddr_t pa;
    423 
    424 	/* 'regular' framebuffer mmap()ing */
    425 	if (offset < sc->sc_fbsize) {
    426 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    427 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    428 		return pa;
    429 	}
    430 
    431 	/*
    432 	 * restrict all other mappings to processes with superuser privileges
    433 	 * or the kernel itself
    434 	 */
    435 	if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
    436 	    NULL) != 0) {
    437 		aprint_normal("%s: mmap() rejected.\n",
    438 		    device_xname(sc->sc_dev));
    439 		return -1;
    440 	}
    441 
    442 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    443 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    444 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    445 		return pa;
    446 	}
    447 
    448 	if ((offset >= sc->sc_reg) &&
    449 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    450 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 0);
    451 		return pa;
    452 	}
    453 
    454 	return -1;
    455 }
    456 
    457 static void
    458 voyagerfb_init_screen(void *cookie, struct vcons_screen *scr,
    459     int existing, long *defattr)
    460 {
    461 	struct voyagerfb_softc *sc = cookie;
    462 	struct rasops_info *ri = &scr->scr_ri;
    463 
    464 	ri->ri_depth = sc->sc_depth;
    465 	ri->ri_width = sc->sc_width;
    466 	ri->ri_height = sc->sc_height;
    467 	ri->ri_stride = sc->sc_stride;
    468 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    469 
    470 	ri->ri_bits = (char *)sc->sc_fbaddr;
    471 
    472 	if (existing) {
    473 		ri->ri_flg |= RI_CLEAR;
    474 	}
    475 
    476 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
    477 	ri->ri_caps = WSSCREEN_WSCOLORS;
    478 
    479 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    480 		    sc->sc_width / ri->ri_font->fontwidth);
    481 
    482 	ri->ri_hw = scr;
    483 	ri->ri_ops.copyrows = voyagerfb_copyrows;
    484 	ri->ri_ops.copycols = voyagerfb_copycols;
    485 	ri->ri_ops.eraserows = voyagerfb_eraserows;
    486 	ri->ri_ops.erasecols = voyagerfb_erasecols;
    487 	ri->ri_ops.cursor = voyagerfb_cursor;
    488 	ri->ri_ops.putchar = voyagerfb_putchar;
    489 }
    490 
    491 static int
    492 voyagerfb_putcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
    493 {
    494 	u_char *r, *g, *b;
    495 	u_int index = cm->index;
    496 	u_int count = cm->count;
    497 	int i, error;
    498 	u_char rbuf[256], gbuf[256], bbuf[256];
    499 
    500 #ifdef VOYAGERFB_DEBUG
    501 	aprint_debug("putcmap: %d %d\n",index, count);
    502 #endif
    503 	if (cm->index >= 256 || cm->count > 256 ||
    504 	    (cm->index + cm->count) > 256)
    505 		return EINVAL;
    506 	error = copyin(cm->red, &rbuf[index], count);
    507 	if (error)
    508 		return error;
    509 	error = copyin(cm->green, &gbuf[index], count);
    510 	if (error)
    511 		return error;
    512 	error = copyin(cm->blue, &bbuf[index], count);
    513 	if (error)
    514 		return error;
    515 
    516 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    517 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    518 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    519 
    520 	r = &sc->sc_cmap_red[index];
    521 	g = &sc->sc_cmap_green[index];
    522 	b = &sc->sc_cmap_blue[index];
    523 
    524 	for (i = 0; i < count; i++) {
    525 		voyagerfb_putpalreg(sc, index, *r, *g, *b);
    526 		index++;
    527 		r++, g++, b++;
    528 	}
    529 	return 0;
    530 }
    531 
    532 static int
    533 voyagerfb_getcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
    534 {
    535 	u_int index = cm->index;
    536 	u_int count = cm->count;
    537 	int error;
    538 
    539 	if (index >= 255 || count > 256 || index + count > 256)
    540 		return EINVAL;
    541 
    542 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    543 	if (error)
    544 		return error;
    545 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    546 	if (error)
    547 		return error;
    548 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    549 	if (error)
    550 		return error;
    551 
    552 	return 0;
    553 }
    554 
    555 static void
    556 voyagerfb_restore_palette(struct voyagerfb_softc *sc)
    557 {
    558 	int i;
    559 
    560 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    561 		voyagerfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    562 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    563 	}
    564 }
    565 
    566 static int
    567 voyagerfb_putpalreg(struct voyagerfb_softc *sc, int idx, uint8_t r,
    568     uint8_t g, uint8_t b)
    569 {
    570 	uint32_t reg;
    571 
    572 	reg = (r << 16) | (g << 8) | b;
    573 	/* XXX we should probably write the CRT palette too */
    574 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    575 	    SM502_PALETTE_PANEL + (idx << 2), reg);
    576 	return 0;
    577 }
    578 
    579 static void
    580 voyagerfb_init(struct voyagerfb_softc *sc)
    581 {
    582 
    583 	voyagerfb_wait(sc);
    584 	/* disable colour compare */
    585 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_COLOR_COMP_MASK, 0);
    586 	/* allow writes to all planes */
    587 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PLANEMASK,
    588 	    0xffffffff);
    589 	/* disable clipping */
    590 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CLIP_TOP_LEFT, 0);
    591 	/* source and destination in local memory, no offset */
    592 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC_BASE, 0);
    593 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST_BASE, 0);
    594 	/* pitch is screen stride */
    595 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PITCH,
    596 	    sc->sc_width | (sc->sc_width << 16));
    597 	/* window is screen width */
    598 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_WINDOW_WIDTH,
    599 	    sc->sc_width | (sc->sc_width << 16));
    600 	switch (sc->sc_depth) {
    601 		case 8:
    602 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    603 			    SM502_STRETCH, SM502_STRETCH_8BIT);
    604 			break;
    605 		case 16:
    606 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    607 			    SM502_STRETCH, SM502_STRETCH_16BIT);
    608 			break;
    609 		case 24:
    610 		case 32:
    611 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    612 			    SM502_STRETCH, SM502_STRETCH_32BIT);
    613 			break;
    614 	}
    615 }
    616 
    617 static void
    618 voyagerfb_rectfill(struct voyagerfb_softc *sc, int x, int y, int wi, int he,
    619      uint32_t colour)
    620 {
    621 
    622 	voyagerfb_ready(sc);
    623 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL,
    624 	    ROP_COPY |
    625 	    SM502_CTRL_USE_ROP2 |
    626 	    SM502_CTRL_CMD_RECTFILL |
    627 	    SM502_CTRL_QUICKSTART_E);
    628 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND,
    629 	    colour);
    630 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
    631 	    (x << 16) | y);
    632 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
    633 	    (wi << 16) | he);
    634 }
    635 
    636 static void
    637 voyagerfb_bitblt(struct voyagerfb_softc *sc, int xs, int ys, int xd, int yd,
    638     int wi, int he, int rop)
    639 {
    640 	uint32_t cmd;
    641 
    642 	cmd = (rop & 0xf) | SM502_CTRL_USE_ROP2 | SM502_CTRL_CMD_BITBLT |
    643 	      SM502_CTRL_QUICKSTART_E;
    644 
    645 	voyagerfb_ready(sc);
    646 
    647 	if (xd <= xs) {
    648 		/* left to right */
    649 	} else {
    650 		/*
    651 		 * According to the manual this flag reverses only the blitter's
    652 		 * X direction. At least on my Gdium it also reverses the Y
    653 		 * direction
    654 		 */
    655 		cmd |= SM502_CTRL_R_TO_L;
    656 		xs += wi - 1;
    657 		xd += wi - 1;
    658 		ys += he - 1;
    659 		yd += he - 1;
    660 	}
    661 
    662 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
    663 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC,
    664 	    (xs << 16) | ys);
    665 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
    666 	    (xd << 16) | yd);
    667 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
    668 	    (wi << 16) | he);
    669 }
    670 
    671 static void
    672 voyagerfb_cursor(void *cookie, int on, int row, int col)
    673 {
    674 	struct rasops_info *ri = cookie;
    675 	struct vcons_screen *scr = ri->ri_hw;
    676 	struct voyagerfb_softc *sc = scr->scr_cookie;
    677 	int x, y, wi, he;
    678 
    679 	wi = ri->ri_font->fontwidth;
    680 	he = ri->ri_font->fontheight;
    681 
    682 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    683 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    684 		y = ri->ri_crow * he + ri->ri_yorigin;
    685 		if (ri->ri_flg & RI_CURSOR) {
    686 			voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
    687 			ri->ri_flg &= ~RI_CURSOR;
    688 		}
    689 		ri->ri_crow = row;
    690 		ri->ri_ccol = col;
    691 		if (on) {
    692 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    693 			y = ri->ri_crow * he + ri->ri_yorigin;
    694 			voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
    695 			ri->ri_flg |= RI_CURSOR;
    696 		}
    697 	} else {
    698 		scr->scr_ri.ri_crow = row;
    699 		scr->scr_ri.ri_ccol = col;
    700 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    701 	}
    702 
    703 }
    704 
    705 static inline void
    706 voyagerfb_feed8(struct voyagerfb_softc *sc, uint8_t *data, int len)
    707 {
    708 	uint32_t *port = (uint32_t *)sc->sc_dataport;
    709 	int i;
    710 
    711 	for (i = 0; i < ((len + 3) & 0xfffc); i++) {
    712 		*port = *data;
    713 		data++;
    714 	}
    715 }
    716 
    717 static inline void
    718 voyagerfb_feed16(struct voyagerfb_softc *sc, uint16_t *data, int len)
    719 {
    720 	uint32_t *port = (uint32_t *)sc->sc_dataport;
    721 	int i;
    722 
    723 	len = len << 1;
    724 	for (i = 0; i < ((len + 1) & 0xfffe); i++) {
    725 		*port = *data;
    726 		data++;
    727 	}
    728 }
    729 
    730 
    731 static void
    732 voyagerfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    733 {
    734 	struct rasops_info *ri = cookie;
    735 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    736 	struct vcons_screen *scr = ri->ri_hw;
    737 	struct voyagerfb_softc *sc = scr->scr_cookie;
    738 	uint32_t cmd;
    739 
    740 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    741 		int fg, bg, uc;
    742 		uint8_t *data;
    743 		int x, y, wi, he;
    744 		wi = font->fontwidth;
    745 		he = font->fontheight;
    746 
    747 		if (!CHAR_IN_FONT(c, font))
    748 			return;
    749 		bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
    750 		fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
    751 		x = ri->ri_xorigin + col * wi;
    752 		y = ri->ri_yorigin + row * he;
    753 		if (c == 0x20) {
    754 			voyagerfb_rectfill(sc, x, y, wi, he, bg);
    755 		} else {
    756 			uc = c - font->firstchar;
    757 			data = (uint8_t *)font->data + uc * ri->ri_fontscale;
    758 			cmd = ROP_COPY |
    759 			      SM502_CTRL_USE_ROP2 |
    760 			      SM502_CTRL_CMD_HOSTWRT |
    761 			      SM502_CTRL_HOSTBLT_MONO |
    762 			      SM502_CTRL_QUICKSTART_E |
    763 			      SM502_CTRL_MONO_PACK_32BIT;
    764 			voyagerfb_ready(sc);
    765 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    766 			    SM502_CONTROL, cmd);
    767 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    768 			    SM502_FOREGROUND, fg);
    769 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    770 			    SM502_BACKGROUND, bg);
    771 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    772 			    SM502_SRC, 0);
    773 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    774 			    SM502_DST, (x << 16) | y);
    775 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    776 			    SM502_DIMENSION, (wi << 16) | he);
    777 			/* now feed the data, padded to 32bit */
    778 			switch (ri->ri_font->stride) {
    779 			case 1:
    780 				voyagerfb_feed8(sc, data, ri->ri_fontscale);
    781 				break;
    782 			case 2:
    783 				voyagerfb_feed16(sc, (uint16_t *)data,
    784 				    ri->ri_fontscale);
    785 				break;
    786 
    787 			}
    788 		}
    789 	}
    790 }
    791 
    792 static void
    793 voyagerfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    794 {
    795 	struct rasops_info *ri = cookie;
    796 	struct vcons_screen *scr = ri->ri_hw;
    797 	struct voyagerfb_softc *sc = scr->scr_cookie;
    798 	int32_t xs, xd, y, width, height;
    799 
    800 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    801 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    802 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    803 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    804 		width = ri->ri_font->fontwidth * ncols;
    805 		height = ri->ri_font->fontheight;
    806 		voyagerfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
    807 	}
    808 }
    809 
    810 static void
    811 voyagerfb_erasecols(void *cookie, int row, int startcol, int ncols,
    812      long fillattr)
    813 {
    814 	struct rasops_info *ri = cookie;
    815 	struct vcons_screen *scr = ri->ri_hw;
    816 	struct voyagerfb_softc *sc = scr->scr_cookie;
    817 	int32_t x, y, width, height, fg, bg, ul;
    818 
    819 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    820 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    821 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    822 		width = ri->ri_font->fontwidth * ncols;
    823 		height = ri->ri_font->fontheight;
    824 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    825 
    826 		voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    827 	}
    828 }
    829 
    830 static void
    831 voyagerfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    832 {
    833 	struct rasops_info *ri = cookie;
    834 	struct vcons_screen *scr = ri->ri_hw;
    835 	struct voyagerfb_softc *sc = scr->scr_cookie;
    836 	int32_t x, ys, yd, width, height;
    837 	int i;
    838 
    839 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    840 		x = ri->ri_xorigin;
    841 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    842 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    843 		width = ri->ri_emuwidth;
    844 		height = ri->ri_font->fontheight * nrows;
    845 		if ((nrows > 1) && (dstrow > srcrow)) {
    846 			/*
    847 			 * the blitter can't do bottom-up copies so we have
    848 			 * to copy line by line here
    849 			 * should probably use a command sequence
    850 			 */
    851 			ys += (height - ri->ri_font->fontheight);
    852 			yd += (height - ri->ri_font->fontheight);
    853 			for (i = 0; i < nrows; i++) {
    854 				voyagerfb_bitblt(sc, x, ys, x, yd, width,
    855 				    ri->ri_font->fontheight, ROP_COPY);
    856 				ys -= ri->ri_font->fontheight;
    857 				yd -= ri->ri_font->fontheight;
    858 			}
    859 		} else
    860 			voyagerfb_bitblt(sc, x, ys, x, yd, width, height,
    861 			    ROP_COPY);
    862 	}
    863 }
    864 
    865 static void
    866 voyagerfb_eraserows(void *cookie, int row, int nrows, long fillattr)
    867 {
    868 	struct rasops_info *ri = cookie;
    869 	struct vcons_screen *scr = ri->ri_hw;
    870 	struct voyagerfb_softc *sc = scr->scr_cookie;
    871 	int32_t x, y, width, height, fg, bg, ul;
    872 
    873 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    874 		x = ri->ri_xorigin;
    875 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    876 		width = ri->ri_emuwidth;
    877 		height = ri->ri_font->fontheight * nrows;
    878 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    879 
    880 		voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    881 	}
    882 }
    883 
    884 /* backlight control */
    885 static void
    886 voyagerfb_set_backlight(struct voyagerfb_softc *sc, int level)
    887 {
    888 
    889 	/*
    890 	 * should we do nothing when backlight is off, should we just store the
    891 	 * level and use it when turning back on or should we just flip sc_bl_on
    892 	 * and turn the backlight on?
    893 	 * For now turn it on so a crashed screensaver can't get the user stuck
    894 	 * with a dark screen as long as hotkeys work
    895 	 */
    896 	if (level > 255) level = 255;
    897 	if (level < 0) level = 0;
    898 	if (level == sc->sc_bl_level)
    899 		return;
    900 	sc->sc_bl_level = level;
    901 	if (sc->sc_bl_on == 0)
    902 		sc->sc_bl_on = 1;
    903 	level = 255 - level;
    904 	/* and here we would actually muck with the hardware */
    905 }
    906 
    907 static void
    908 voyagerfb_switch_backlight(struct voyagerfb_softc *sc, int on)
    909 {
    910 
    911 	if (on == sc->sc_bl_on)
    912 		return;
    913 	sc->sc_bl_on = on;
    914 	voyager_write_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, on ? GPIO_BACKLIGHT : 0);
    915 }
    916 
    917 
    918 static void
    919 voyagerfb_brightness_up(device_t dev)
    920 {
    921 	struct voyagerfb_softc *sc = device_private(dev);
    922 
    923 	voyagerfb_set_backlight(sc, sc->sc_bl_level + 8);
    924 }
    925 
    926 static void
    927 voyagerfb_brightness_down(device_t dev)
    928 {
    929 	struct voyagerfb_softc *sc = device_private(dev);
    930 
    931 	voyagerfb_set_backlight(sc, sc->sc_bl_level - 8);
    932 }
    933