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