Home | History | Annotate | Line # | Download | only in voyager
voyagerfb.c revision 1.29
      1 /*	$NetBSD: voyagerfb.c,v 1.29 2018/01/19 23:37:36 macallan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009, 2011 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * A console driver for Silicon Motion SM502 / Voyager GX  graphics controllers
     30  * tested on GDIUM only so far
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: voyagerfb.c,v 1.29 2018/01/19 23:37:36 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 #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 	/*
    273 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
    274 	 * but as far as I know there are no SM502 with 64bit BARs
    275 	 */
    276 	aprint_normal_dev(self, "%d MB video memory at 0x%08x\n",
    277 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    278 
    279 	/* init engine here */
    280 	voyagerfb_init(sc);
    281 
    282 	aprint_normal_dev(self, "%d x %d, %d bit, stride %d\n",
    283 		sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
    284 
    285 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    286 		"default",
    287 		0, 0,
    288 		NULL,
    289 		8, 16,
    290 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    291 		WSSCREEN_RESIZE,
    292 		NULL
    293 	};
    294 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    295 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    296 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    297 	sc->sc_locked = 0;
    298 
    299 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    300 	    &voyagerfb_accessops);
    301 	sc->vd.init_screen = voyagerfb_init_screen;
    302 	sc->vd.show_screen_cookie = &sc->sc_gc;
    303 	sc->vd.show_screen_cb = glyphcache_adapt;
    304 
    305 	/* backlight control */
    306 	voyagerfb_setup_backlight(sc);
    307 
    308 	ri = &sc->sc_console_screen.scr_ri;
    309 
    310 	sc->sc_gc.gc_bitblt = voyagerfb_bitblt;
    311 	sc->sc_gc.gc_blitcookie = sc;
    312 	sc->sc_gc.gc_rop = ROP_COPY;
    313 	if (is_console) {
    314 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    315 		    &defattr);
    316 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    317 
    318 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    319 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    320 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    321 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    322 	} else {
    323 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
    324 			/* do some minimal setup to avoid weirdness later */
    325 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    326 			    &defattr);
    327 		} else
    328 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    329 	}
    330 	glyphcache_init(&sc->sc_gc, sc->sc_height,
    331 			(sc->sc_fbsize / sc->sc_stride) - sc->sc_height,
    332 			sc->sc_width,
    333 			ri->ri_font->fontwidth,
    334 			ri->ri_font->fontheight,
    335 			defattr);
    336 	if (is_console)
    337 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    338 		    defattr);
    339 
    340 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    341 	j = 0;
    342 	if (sc->sc_depth <= 8) {
    343 		for (i = 0; i < 256; i++) {
    344 
    345 			sc->sc_cmap_red[i] = cmap[j];
    346 			sc->sc_cmap_green[i] = cmap[j + 1];
    347 			sc->sc_cmap_blue[i] = cmap[j + 2];
    348 			voyagerfb_putpalreg(sc, i, cmap[j], cmap[j + 1],
    349 			    cmap[j + 2]);
    350 			j += 3;
    351 		}
    352 	}
    353 
    354 	voyagerfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    355 	    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    356 
    357 	if (is_console)
    358 		vcons_replay_msgbuf(&sc->sc_console_screen);
    359 
    360 	aa.console = is_console;
    361 	aa.scrdata = &sc->sc_screenlist;
    362 	aa.accessops = &voyagerfb_accessops;
    363 	aa.accesscookie = &sc->vd;
    364 
    365 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    366 }
    367 
    368 static int
    369 voyagerfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    370 	struct lwp *l)
    371 {
    372 	struct vcons_data *vd = v;
    373 	struct voyagerfb_softc *sc = vd->cookie;
    374 	struct wsdisplay_fbinfo *wdf;
    375 	struct vcons_screen *ms = vd->active;
    376 	struct wsdisplay_param  *param;
    377 
    378 	switch (cmd) {
    379 	case WSDISPLAYIO_GTYPE:
    380 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    381 		return 0;
    382 
    383 	/* PCI config read/write pass through. */
    384 	case PCI_IOC_CFGREAD:
    385 	case PCI_IOC_CFGWRITE:
    386 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    387 		    cmd, data, flag, l);
    388 
    389 	case WSDISPLAYIO_GET_BUSID:
    390 		return wsdisplayio_busid_pci(device_parent(sc->sc_dev),
    391 		    sc->sc_pc, sc->sc_pcitag, data);
    392 
    393 	case WSDISPLAYIO_GINFO:
    394 		if (ms == NULL)
    395 			return ENODEV;
    396 		wdf = (void *)data;
    397 		wdf->height = ms->scr_ri.ri_height;
    398 		wdf->width = ms->scr_ri.ri_width;
    399 		wdf->depth = 32;
    400 		wdf->cmsize = 256;
    401 		return 0;
    402 
    403 	case WSDISPLAYIO_GETCMAP:
    404 		return voyagerfb_getcmap(sc,
    405 		    (struct wsdisplay_cmap *)data);
    406 
    407 	case WSDISPLAYIO_PUTCMAP:
    408 		return voyagerfb_putcmap(sc,
    409 		    (struct wsdisplay_cmap *)data);
    410 
    411 	case WSDISPLAYIO_LINEBYTES:
    412 		*(u_int *)data = sc->sc_stride;
    413 		return 0;
    414 
    415 	case WSDISPLAYIO_SMODE: {
    416 		int new_mode = *(int*)data;
    417 		if (new_mode != sc->sc_mode) {
    418 			sc->sc_mode = new_mode;
    419 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    420 #ifdef VOYAGERFB_DEPTH_32
    421 				sc->sc_depth = 32;
    422 #else
    423 				sc->sc_depth = 8;
    424 #endif
    425 				glyphcache_wipe(&sc->sc_gc);
    426 				voyagerfb_init(sc);
    427 				voyagerfb_restore_palette(sc);
    428 				vcons_redraw_screen(ms);
    429 			} else {
    430 				sc->sc_depth = 32;
    431 				voyagerfb_init(sc);
    432 			}
    433 		}
    434 		}
    435 		return 0;
    436 
    437 	case WSDISPLAYIO_GVIDEO:
    438 		*(int *)data = sc->sc_bl_on ? WSDISPLAYIO_VIDEO_ON :
    439 					      WSDISPLAYIO_VIDEO_OFF;
    440 		return 0;
    441 
    442 	case WSDISPLAYIO_SVIDEO: {
    443 			int new_bl = *(int *)data;
    444 
    445 			voyagerfb_switch_backlight(sc,  new_bl);
    446 		}
    447 		return 0;
    448 
    449 	case WSDISPLAYIO_GETPARAM:
    450 		param = (struct wsdisplay_param *)data;
    451 		switch (param->param) {
    452 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    453 			param->min = 0;
    454 			param->max = 255;
    455 			param->curval = sc->sc_bl_level;
    456 			return 0;
    457 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    458 			param->min = 0;
    459 			param->max = 1;
    460 			param->curval = sc->sc_bl_on;
    461 			return 0;
    462 		}
    463 		return EPASSTHROUGH;
    464 
    465 	case WSDISPLAYIO_SETPARAM:
    466 		param = (struct wsdisplay_param *)data;
    467 		switch (param->param) {
    468 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    469 			voyagerfb_set_backlight(sc, param->curval);
    470 			return 0;
    471 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    472 			voyagerfb_switch_backlight(sc,  param->curval);
    473 			return 0;
    474 		}
    475 		return EPASSTHROUGH;
    476 
    477 	case WSDISPLAYIO_GCURPOS:
    478 		{
    479 			struct wsdisplay_curpos *pos;
    480 
    481 			pos = (struct wsdisplay_curpos *)data;
    482 			pos->x = sc->sc_cur_x;
    483 			pos->y = sc->sc_cur_y;
    484 		}
    485 		return 0;
    486 
    487 	case WSDISPLAYIO_SCURPOS:
    488 		{
    489 			struct wsdisplay_curpos *pos;
    490 
    491 			pos = (struct wsdisplay_curpos *)data;
    492 			voyagerfb_set_curpos(sc, pos->x, pos->y);
    493 		}
    494 		return 0;
    495 
    496 	case WSDISPLAYIO_GCURMAX:
    497 		{
    498 			struct wsdisplay_curpos *pos;
    499 
    500 			pos = (struct wsdisplay_curpos *)data;
    501 			pos->x = 64;
    502 			pos->y = 64;
    503 		}
    504 		return 0;
    505 
    506 	case WSDISPLAYIO_GCURSOR:
    507 		{
    508 			struct wsdisplay_cursor *cu;
    509 
    510 			cu = (struct wsdisplay_cursor *)data;
    511 			return voyagerfb_gcursor(sc, cu);
    512 		}
    513 
    514 	case WSDISPLAYIO_SCURSOR:
    515 		{
    516 			struct wsdisplay_cursor *cu;
    517 
    518 			cu = (struct wsdisplay_cursor *)data;
    519 			return voyagerfb_scursor(sc, cu);
    520 		}
    521 
    522 	case WSDISPLAYIO_GET_FBINFO:
    523 		{
    524 			struct wsdisplayio_fbinfo *fbi = data;
    525 			return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    526 		}
    527 	}
    528 	return EPASSTHROUGH;
    529 }
    530 
    531 static paddr_t
    532 voyagerfb_mmap(void *v, void *vs, off_t offset, int prot)
    533 {
    534 	struct vcons_data *vd = v;
    535 	struct voyagerfb_softc *sc = vd->cookie;
    536 	paddr_t pa;
    537 
    538 	/* 'regular' framebuffer mmap()ing */
    539 	if (offset < sc->sc_fbsize) {
    540 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    541 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    542 		return pa;
    543 	}
    544 
    545 	/*
    546 	 * restrict all other mappings to processes with privileges
    547 	 */
    548 	if (kauth_authorize_machdep(kauth_cred_get(),
    549 	    KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0) {
    550 		aprint_normal("%s: mmap() rejected.\n",
    551 		    device_xname(sc->sc_dev));
    552 		return -1;
    553 	}
    554 
    555 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    556 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    557 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    558 		return pa;
    559 	}
    560 
    561 	if ((offset >= sc->sc_reg) &&
    562 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    563 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 0);
    564 		return pa;
    565 	}
    566 
    567 	return -1;
    568 }
    569 
    570 static void
    571 voyagerfb_init_screen(void *cookie, struct vcons_screen *scr,
    572     int existing, long *defattr)
    573 {
    574 	struct voyagerfb_softc *sc = cookie;
    575 	struct rasops_info *ri = &scr->scr_ri;
    576 
    577 	ri->ri_depth = sc->sc_depth;
    578 	ri->ri_width = sc->sc_width;
    579 	ri->ri_height = sc->sc_height;
    580 	ri->ri_stride = sc->sc_stride;
    581 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    582 
    583 	ri->ri_bits = (char *)sc->sc_fbaddr;
    584 
    585 	if (existing) {
    586 		ri->ri_flg |= RI_CLEAR;
    587 	}
    588 
    589 	if (sc->sc_depth == 8) {
    590 		ri->ri_flg |= RI_8BIT_IS_RGB;
    591 #ifdef VOYAGERFB_ANTIALIAS
    592 		ri->ri_flg |= RI_ENABLE_ALPHA | RI_PREFER_ALPHA;
    593 #endif
    594 	}
    595 	if (sc->sc_depth == 32) {
    596 #ifdef VOYAGERFB_ANTIALIAS
    597 		ri->ri_flg |= RI_ENABLE_ALPHA;
    598 #endif
    599 		/* we always run in RGB */
    600 		ri->ri_rnum = 8;
    601 		ri->ri_gnum = 8;
    602 		ri->ri_bnum = 8;
    603 		ri->ri_rpos = 16;
    604 		ri->ri_gpos = 8;
    605 		ri->ri_bpos = 0;
    606 	}
    607 
    608 	scr->scr_flags |= VCONS_LOADFONT;
    609 
    610 	rasops_init(ri, 0, 0);
    611 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    612 		      WSSCREEN_RESIZE;
    613 
    614 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    615 		    sc->sc_width / ri->ri_font->fontwidth);
    616 
    617 	ri->ri_hw = scr;
    618 	ri->ri_ops.copyrows = voyagerfb_copyrows;
    619 	ri->ri_ops.copycols = voyagerfb_copycols;
    620 	ri->ri_ops.eraserows = voyagerfb_eraserows;
    621 	ri->ri_ops.erasecols = voyagerfb_erasecols;
    622 	ri->ri_ops.cursor = voyagerfb_cursor;
    623 	if (FONT_IS_ALPHA(ri->ri_font)) {
    624 	        switch (sc->sc_depth) {
    625 	                case 32:
    626                 		ri->ri_ops.putchar = voyagerfb_putchar_aa32;
    627                 		break;
    628                         case 8:
    629                                 ri->ri_ops.putchar = voyagerfb_putchar_aa8;
    630                                 break;
    631                         default:
    632                                 printf("alpha font at %d!?\n", sc->sc_depth);
    633                 }
    634 	} else
    635 		ri->ri_ops.putchar = voyagerfb_putchar_mono;
    636 }
    637 
    638 static int
    639 voyagerfb_putcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
    640 {
    641 	u_char *r, *g, *b;
    642 	u_int index = cm->index;
    643 	u_int count = cm->count;
    644 	int i, error;
    645 	u_char rbuf[256], gbuf[256], bbuf[256];
    646 
    647 #ifdef VOYAGERFB_DEBUG
    648 	aprint_debug("putcmap: %d %d\n",index, count);
    649 #endif
    650 	if (cm->index >= 256 || cm->count > 256 ||
    651 	    (cm->index + cm->count) > 256)
    652 		return EINVAL;
    653 	error = copyin(cm->red, &rbuf[index], count);
    654 	if (error)
    655 		return error;
    656 	error = copyin(cm->green, &gbuf[index], count);
    657 	if (error)
    658 		return error;
    659 	error = copyin(cm->blue, &bbuf[index], count);
    660 	if (error)
    661 		return error;
    662 
    663 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    664 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    665 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    666 
    667 	r = &sc->sc_cmap_red[index];
    668 	g = &sc->sc_cmap_green[index];
    669 	b = &sc->sc_cmap_blue[index];
    670 
    671 	for (i = 0; i < count; i++) {
    672 		voyagerfb_putpalreg(sc, index, *r, *g, *b);
    673 		index++;
    674 		r++, g++, b++;
    675 	}
    676 	return 0;
    677 }
    678 
    679 static int
    680 voyagerfb_getcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
    681 {
    682 	u_int index = cm->index;
    683 	u_int count = cm->count;
    684 	int error;
    685 
    686 	if (index >= 255 || count > 256 || index + count > 256)
    687 		return EINVAL;
    688 
    689 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    690 	if (error)
    691 		return error;
    692 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    693 	if (error)
    694 		return error;
    695 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    696 	if (error)
    697 		return error;
    698 
    699 	return 0;
    700 }
    701 
    702 static void
    703 voyagerfb_restore_palette(struct voyagerfb_softc *sc)
    704 {
    705 	int i;
    706 
    707 	for (i = 0; i < 256; i++) {
    708 		voyagerfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    709 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    710 	}
    711 }
    712 
    713 static int
    714 voyagerfb_putpalreg(struct voyagerfb_softc *sc, int idx, uint8_t r,
    715     uint8_t g, uint8_t b)
    716 {
    717 	uint32_t reg;
    718 
    719 	reg = (r << 16) | (g << 8) | b;
    720 	/* XXX we should probably write the CRT palette too */
    721 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    722 	    SM502_PALETTE_PANEL + (idx << 2), reg);
    723 	return 0;
    724 }
    725 
    726 static void
    727 voyagerfb_init(struct voyagerfb_softc *sc)
    728 {
    729 	int reg;
    730 
    731 	voyagerfb_wait(sc);
    732 	/* disable colour compare */
    733 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_COLOR_COMP_MASK, 0);
    734 	/* allow writes to all planes */
    735 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PLANEMASK,
    736 	    0xffffffff);
    737 	/* disable clipping */
    738 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CLIP_TOP_LEFT, 0);
    739 	/* source and destination in local memory, no offset */
    740 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC_BASE, 0);
    741 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST_BASE, 0);
    742 	/* pitch is screen stride */
    743 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PITCH,
    744 	    sc->sc_width | (sc->sc_width << 16));
    745 	/* window is screen width */
    746 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_WINDOW_WIDTH,
    747 	    sc->sc_width | (sc->sc_width << 16));
    748 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL);
    749 	reg &= ~SM502_PDC_DEPTH_MASK;
    750 
    751 	switch (sc->sc_depth) {
    752 		case 8:
    753 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    754 			    SM502_STRETCH, SM502_STRETCH_8BIT);
    755 			sc->sc_stride = sc->sc_width;
    756 			reg |= SM502_PDC_8BIT;
    757 			break;
    758 		case 16:
    759 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    760 			    SM502_STRETCH, SM502_STRETCH_16BIT);
    761 			sc->sc_stride = sc->sc_width << 1;
    762 			reg |= SM502_PDC_16BIT;
    763 			break;
    764 		case 24:
    765 		case 32:
    766 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    767 			    SM502_STRETCH, SM502_STRETCH_32BIT);
    768 			sc->sc_stride = sc->sc_width << 2;
    769 			reg |= SM502_PDC_32BIT;
    770 			break;
    771 	}
    772 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_FB_OFFSET,
    773 	    (sc->sc_stride << 16) | sc->sc_stride);
    774 
    775 	/* clear the screen... */
    776 	voyagerfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, 0);
    777 
    778 	/* ...and then switch colour depth. For aesthetic reasons. */
    779 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL,
    780 	    reg);
    781 
    782 	/* put the cursor at the end of video memory */
    783 	sc->sc_cursor_addr = 16 * 1024 * 1024 - 16 * 64;	/* XXX */
    784 	DPRINTF("%s: %08x\n", __func__, sc->sc_cursor_addr);
    785 	sc->sc_cursor = (uint32_t *)((uint8_t *)bus_space_vaddr(sc->sc_memt,
    786 			 sc->sc_fbh) + sc->sc_cursor_addr);
    787 #ifdef VOYAGERFB_DEBUG
    788 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_XY,
    789 							 0x00100010);
    790 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_COL12,
    791 							 0x0000ffff);
    792 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_COL3,
    793 							 0x0000f800);
    794 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_ADDR,
    795 	    SM502_CRSR_ENABLE | sc->sc_cursor_addr);
    796 	sc->sc_cursor[0] = 0x00000000;
    797 	sc->sc_cursor[1] = 0x00000000;
    798 	sc->sc_cursor[2] = 0xffffffff;
    799 	sc->sc_cursor[3] = 0xffffffff;
    800 	sc->sc_cursor[4] = 0xaaaaaaaa;
    801 	sc->sc_cursor[5] = 0xaaaaaaaa;
    802 	sc->sc_cursor[6] = 0xffffffff;
    803 	sc->sc_cursor[7] = 0x00000000;
    804 #else
    805 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_ADDR,
    806 	    sc->sc_cursor_addr);
    807 #endif
    808 }
    809 
    810 static void
    811 voyagerfb_rectfill(struct voyagerfb_softc *sc, int x, int y, int wi, int he,
    812      uint32_t colour)
    813 {
    814 
    815 	voyagerfb_ready(sc);
    816 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL,
    817 	    ROP_COPY |
    818 	    SM502_CTRL_USE_ROP2 |
    819 	    SM502_CTRL_CMD_RECTFILL |
    820 	    SM502_CTRL_QUICKSTART_E);
    821 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND,
    822 	    colour);
    823 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
    824 	    (x << 16) | y);
    825 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
    826 	    (wi << 16) | he);
    827 }
    828 
    829 static void
    830 voyagerfb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
    831     int wi, int he, int rop)
    832 {
    833 	struct voyagerfb_softc *sc = cookie;
    834 	uint32_t cmd;
    835 
    836 	cmd = (rop & 0xf) | SM502_CTRL_USE_ROP2 | SM502_CTRL_CMD_BITBLT |
    837 	      SM502_CTRL_QUICKSTART_E;
    838 
    839 	voyagerfb_ready(sc);
    840 
    841 	if (xd <= xs) {
    842 		/* left to right */
    843 	} else {
    844 		/*
    845 		 * According to the manual this flag reverses only the blitter's
    846 		 * X direction. At least on my Gdium it also reverses the Y
    847 		 * direction
    848 		 */
    849 		cmd |= SM502_CTRL_R_TO_L;
    850 		xs += wi - 1;
    851 		xd += wi - 1;
    852 		ys += he - 1;
    853 		yd += he - 1;
    854 	}
    855 
    856 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
    857 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC,
    858 	    (xs << 16) | ys);
    859 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
    860 	    (xd << 16) | yd);
    861 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
    862 	    (wi << 16) | he);
    863 }
    864 
    865 static void
    866 voyagerfb_cursor(void *cookie, int on, int row, int col)
    867 {
    868 	struct rasops_info *ri = cookie;
    869 	struct vcons_screen *scr = ri->ri_hw;
    870 	struct voyagerfb_softc *sc = scr->scr_cookie;
    871 	int x, y, wi, he;
    872 
    873 	wi = ri->ri_font->fontwidth;
    874 	he = ri->ri_font->fontheight;
    875 
    876 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    877 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    878 		y = ri->ri_crow * he + ri->ri_yorigin;
    879 		if (ri->ri_flg & RI_CURSOR) {
    880 			voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
    881 			ri->ri_flg &= ~RI_CURSOR;
    882 		}
    883 		ri->ri_crow = row;
    884 		ri->ri_ccol = col;
    885 		if (on) {
    886 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    887 			y = ri->ri_crow * he + ri->ri_yorigin;
    888 			voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
    889 			ri->ri_flg |= RI_CURSOR;
    890 		}
    891 	} else {
    892 		scr->scr_ri.ri_crow = row;
    893 		scr->scr_ri.ri_ccol = col;
    894 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    895 	}
    896 
    897 }
    898 
    899 static inline void
    900 voyagerfb_feed8(struct voyagerfb_softc *sc, uint8_t *data, int len)
    901 {
    902 	uint32_t *port = (uint32_t *)sc->sc_dataport;
    903 	int i;
    904 
    905 	for (i = 0; i < ((len + 3) & 0xfffc); i++) {
    906 		*port = *data;
    907 		data++;
    908 	}
    909 }
    910 
    911 static inline void
    912 voyagerfb_feed16(struct voyagerfb_softc *sc, uint16_t *data, int len)
    913 {
    914 	uint32_t *port = (uint32_t *)sc->sc_dataport;
    915 	int i;
    916 
    917 	len = len << 1;
    918 	for (i = 0; i < ((len + 1) & 0xfffe); i++) {
    919 		*port = *data;
    920 		data++;
    921 	}
    922 }
    923 
    924 static void
    925 voyagerfb_putchar_mono(void *cookie, int row, int col, u_int c, long attr)
    926 {
    927 	struct rasops_info *ri = cookie;
    928 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    929 	struct vcons_screen *scr = ri->ri_hw;
    930 	struct voyagerfb_softc *sc = scr->scr_cookie;
    931 	uint32_t cmd;
    932 	int fg, bg;
    933 	uint8_t *data;
    934 	int x, y, wi, he;
    935 
    936 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
    937 		return;
    938 
    939 	if (!CHAR_IN_FONT(c, font))
    940 		return;
    941 
    942 	wi = font->fontwidth;
    943 	he = font->fontheight;
    944 
    945 	bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
    946 	fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
    947 	x = ri->ri_xorigin + col * wi;
    948 	y = ri->ri_yorigin + row * he;
    949 	if (c == 0x20) {
    950 		voyagerfb_rectfill(sc, x, y, wi, he, bg);
    951 		return;
    952 	}
    953 
    954 	data = WSFONT_GLYPH(c, font);
    955 
    956 	cmd = ROP_COPY |
    957 	      SM502_CTRL_USE_ROP2 |
    958 	      SM502_CTRL_CMD_HOSTWRT |
    959 	      SM502_CTRL_HOSTBLT_MONO |
    960 	      SM502_CTRL_QUICKSTART_E |
    961 	      SM502_CTRL_MONO_PACK_32BIT;
    962 	voyagerfb_ready(sc);
    963 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
    964 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND, fg);
    965 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_BACKGROUND, bg);
    966 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, 0);
    967 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, (x << 16) | y);
    968 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    969 	    SM502_DIMENSION, (wi << 16) | he);
    970 	/* now feed the data, padded to 32bit */
    971 	switch (ri->ri_font->stride) {
    972 		case 1:
    973 			voyagerfb_feed8(sc, data, ri->ri_fontscale);
    974 			break;
    975 		case 2:
    976 			voyagerfb_feed16(sc, (uint16_t *)data,
    977 			    ri->ri_fontscale);
    978 			break;
    979 	}
    980 }
    981 
    982 static void
    983 voyagerfb_putchar_aa32(void *cookie, int row, int col, u_int c, long attr)
    984 {
    985 	struct rasops_info *ri = cookie;
    986 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    987 	struct vcons_screen *scr = ri->ri_hw;
    988 	struct voyagerfb_softc *sc = scr->scr_cookie;
    989 	uint32_t cmd;
    990 	int fg, bg;
    991 	uint8_t *data;
    992 	int x, y, wi, he;
    993 	int i, j, r, g, b, aval, pad;
    994 	int rf, gf, bf, rb, gb, bb;
    995 	uint32_t pixel;
    996 	int rv;
    997 
    998 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
    999 		return;
   1000 
   1001 	if (!CHAR_IN_FONT(c, font))
   1002 		return;
   1003 
   1004 	wi = font->fontwidth;
   1005 	he = font->fontheight;
   1006 
   1007 	bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
   1008 	fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
   1009 	x = ri->ri_xorigin + col * wi;
   1010 	y = ri->ri_yorigin + row * he;
   1011 	if (c == 0x20) {
   1012 		voyagerfb_rectfill(sc, x, y, wi, he, bg);
   1013 		return;
   1014 	}
   1015 
   1016 	data = WSFONT_GLYPH(c, font);
   1017 	/*
   1018 	 * we can't accelerate the actual alpha blending but
   1019 	 * we can at least use a host blit to go through the
   1020 	 * pipeline instead of having to sync the engine
   1021 	 */
   1022 
   1023 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1024 	if (rv == GC_OK)
   1025 		return;
   1026 
   1027 	cmd = ROP_COPY |
   1028 	      SM502_CTRL_USE_ROP2 |
   1029 	      SM502_CTRL_CMD_HOSTWRT |
   1030 	      SM502_CTRL_QUICKSTART_E;
   1031 	voyagerfb_ready(sc);
   1032 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
   1033 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, 0);
   1034 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, (x << 16) | y);
   1035 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
   1036 	    (wi << 16) | he);
   1037 	rf = (fg >> 16) & 0xff;
   1038 	rb = (bg >> 16) & 0xff;
   1039 	gf = (fg >> 8) & 0xff;
   1040 	gb = (bg >> 8) & 0xff;
   1041 	bf =  fg & 0xff;
   1042 	bb =  bg & 0xff;
   1043 	pad = wi & 1;
   1044 	for (i = 0; i < he; i++) {
   1045 		for (j = 0; j < wi; j++) {
   1046 			aval = *data;
   1047 			data++;
   1048 			if (aval == 0) {
   1049 				pixel = bg;
   1050 			} else if (aval == 255) {
   1051 				pixel = fg;
   1052 			} else {
   1053 				r = aval * rf + (255 - aval) * rb;
   1054 				g = aval * gf + (255 - aval) * gb;
   1055 				b = aval * bf + (255 - aval) * bb;
   1056 				pixel = (r & 0xff00) << 8 |
   1057 				        (g & 0xff00) |
   1058 				        (b & 0xff00) >> 8;
   1059 			}
   1060 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1061 			    SM502_DATAPORT, pixel);
   1062 		}
   1063 		if (pad)
   1064 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1065 			    SM502_DATAPORT, 0);
   1066 	}
   1067 	if (rv == GC_ADD) {
   1068 		glyphcache_add(&sc->sc_gc, c, x, y);
   1069 	}
   1070 }
   1071 
   1072 static void
   1073 voyagerfb_putchar_aa8(void *cookie, int row, int col, u_int c, long attr)
   1074 {
   1075 	struct rasops_info *ri = cookie;
   1076 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1077 	struct vcons_screen *scr = ri->ri_hw;
   1078 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1079 	uint32_t cmd;
   1080 	int bg;
   1081 	uint8_t *data;
   1082 	int x, y, wi, he;
   1083 	int i, j, r, g, b, aval, pad;
   1084 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
   1085 	uint32_t pixel = 0, latch = 0, bg8, fg8;
   1086 	int rv;
   1087 
   1088 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1089 		return;
   1090 
   1091 	if (!CHAR_IN_FONT(c, font))
   1092 		return;
   1093 
   1094 	wi = font->fontwidth;
   1095 	he = font->fontheight;
   1096 
   1097 	bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
   1098 	x = ri->ri_xorigin + col * wi;
   1099 	y = ri->ri_yorigin + row * he;
   1100 	if (c == 0x20) {
   1101 		voyagerfb_rectfill(sc, x, y, wi, he, bg);
   1102 		return;
   1103 	}
   1104 
   1105 	data = WSFONT_GLYPH(c, font);
   1106 	/*
   1107 	 * we can't accelerate the actual alpha blending but
   1108 	 * we can at least use a host blit to go through the
   1109 	 * pipeline instead of having to sync the engine
   1110 	 */
   1111 
   1112 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1113 	if (rv == GC_OK)
   1114 		return;
   1115 
   1116 	cmd = ROP_COPY |
   1117 	      SM502_CTRL_USE_ROP2 |
   1118 	      SM502_CTRL_CMD_HOSTWRT |
   1119 	      SM502_CTRL_QUICKSTART_E;
   1120 	voyagerfb_ready(sc);
   1121 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
   1122 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, 0);
   1123 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, (x << 16) | y);
   1124 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
   1125 	    (wi << 16) | he);
   1126 
   1127 	/*
   1128 	 * we need the RGB colours here, so get offsets into rasops_cmap
   1129 	 */
   1130 	fgo = ((attr >> 24) & 0xf) * 3;
   1131 	bgo = ((attr >> 16) & 0xf) * 3;
   1132 
   1133 	r0 = rasops_cmap[bgo];
   1134 	r1 = rasops_cmap[fgo];
   1135 	g0 = rasops_cmap[bgo + 1];
   1136 	g1 = rasops_cmap[fgo + 1];
   1137 	b0 = rasops_cmap[bgo + 2];
   1138 	b1 = rasops_cmap[fgo + 2];
   1139 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
   1140 	bg8 = R3G3B2(r0, g0, b0);
   1141 	fg8 = R3G3B2(r1, g1, b1);
   1142 
   1143 	pad = wi & 4;
   1144 	for (i = 0; i < he; i++) {
   1145 		for (j = 0; j < wi; j++) {
   1146 			aval = *data;
   1147 			data++;
   1148 			if (aval == 0) {
   1149 				pixel = bg8;
   1150 			} else if (aval == 255) {
   1151 				pixel = fg8;
   1152 			} else {
   1153 				r = aval * r1 + (255 - aval) * r0;
   1154 				g = aval * g1 + (255 - aval) * g0;
   1155 				b = aval * b1 + (255 - aval) * b0;
   1156 				pixel = ((r & 0xe000) >> 8) |
   1157 					((g & 0xe000) >> 11) |
   1158 					((b & 0xc000) >> 14);
   1159 			}
   1160 			latch = (latch << 8) | pixel;
   1161 			/* write in 32bit chunks */
   1162 			if ((j & 3) == 3) {
   1163 				bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1164 				    SM502_DATAPORT, be32toh(latch));
   1165 				latch = 0;
   1166 			}
   1167 		}
   1168 		/* if we have pixels left in latch write them out */
   1169 		if ((j & 3) != 0) {
   1170 			latch = latch << ((4 - (i & 3)) << 3);
   1171 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1172 			    SM502_DATAPORT, be32toh(latch));
   1173 		}
   1174 		if (pad)
   1175 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1176 			    SM502_DATAPORT, 0);
   1177 	}
   1178 	if (rv == GC_ADD) {
   1179 		glyphcache_add(&sc->sc_gc, c, x, y);
   1180 	}
   1181 }
   1182 
   1183 static void
   1184 voyagerfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1185 {
   1186 	struct rasops_info *ri = cookie;
   1187 	struct vcons_screen *scr = ri->ri_hw;
   1188 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1189 	int32_t xs, xd, y, width, height;
   1190 
   1191 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1192 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1193 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1194 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1195 		width = ri->ri_font->fontwidth * ncols;
   1196 		height = ri->ri_font->fontheight;
   1197 		voyagerfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
   1198 	}
   1199 }
   1200 
   1201 static void
   1202 voyagerfb_erasecols(void *cookie, int row, int startcol, int ncols,
   1203      long fillattr)
   1204 {
   1205 	struct rasops_info *ri = cookie;
   1206 	struct vcons_screen *scr = ri->ri_hw;
   1207 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1208 	int32_t x, y, width, height, fg, bg, ul;
   1209 
   1210 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1211 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1212 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1213 		width = ri->ri_font->fontwidth * ncols;
   1214 		height = ri->ri_font->fontheight;
   1215 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1216 
   1217 		voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1218 	}
   1219 }
   1220 
   1221 static void
   1222 voyagerfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1223 {
   1224 	struct rasops_info *ri = cookie;
   1225 	struct vcons_screen *scr = ri->ri_hw;
   1226 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1227 	int32_t x, ys, yd, width, height;
   1228 	int i;
   1229 
   1230 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1231 		x = ri->ri_xorigin;
   1232 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1233 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1234 		width = ri->ri_emuwidth;
   1235 		height = ri->ri_font->fontheight * nrows;
   1236 		if ((nrows > 1) && (dstrow > srcrow)) {
   1237 			/*
   1238 			 * the blitter can't do bottom-up copies so we have
   1239 			 * to copy line by line here
   1240 			 * should probably use a command sequence
   1241 			 */
   1242 			ys += (height - ri->ri_font->fontheight);
   1243 			yd += (height - ri->ri_font->fontheight);
   1244 			for (i = 0; i < nrows; i++) {
   1245 				voyagerfb_bitblt(sc, x, ys, x, yd, width,
   1246 				    ri->ri_font->fontheight, ROP_COPY);
   1247 				ys -= ri->ri_font->fontheight;
   1248 				yd -= ri->ri_font->fontheight;
   1249 			}
   1250 		} else
   1251 			voyagerfb_bitblt(sc, x, ys, x, yd, width, height,
   1252 			    ROP_COPY);
   1253 	}
   1254 }
   1255 
   1256 static void
   1257 voyagerfb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1258 {
   1259 	struct rasops_info *ri = cookie;
   1260 	struct vcons_screen *scr = ri->ri_hw;
   1261 	struct voyagerfb_softc *sc = scr->scr_cookie;
   1262 	int32_t x, y, width, height, fg, bg, ul;
   1263 
   1264 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1265 		x = ri->ri_xorigin;
   1266 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1267 		width = ri->ri_emuwidth;
   1268 		height = ri->ri_font->fontheight * nrows;
   1269 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1270 
   1271 		voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1272 	}
   1273 }
   1274 
   1275 /* backlight control */
   1276 static void
   1277 voyagerfb_setup_backlight(struct voyagerfb_softc *sc)
   1278 {
   1279 	/* switch the pin to gpio mode if it isn't already */
   1280 	voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1281 	/* turn it on */
   1282 	voyager_write_gpio(sc->sc_gpio_cookie, 0xffffffff, GPIO_BACKLIGHT);
   1283 	sc->sc_bl_on = 1;
   1284 	sc->sc_bl_level = 255;
   1285 	pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
   1286 	    voyagerfb_brightness_up, TRUE);
   1287 	pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
   1288 	    voyagerfb_brightness_down, TRUE);
   1289 }
   1290 
   1291 static void
   1292 voyagerfb_set_backlight(struct voyagerfb_softc *sc, int level)
   1293 {
   1294 
   1295 	/*
   1296 	 * should we do nothing when backlight is off, should we just store the
   1297 	 * level and use it when turning back on or should we just flip sc_bl_on
   1298 	 * and turn the backlight on?
   1299 	 * For now turn it on so a crashed screensaver can't get the user stuck
   1300 	 * with a dark screen as long as hotkeys work
   1301 	 */
   1302 	if (level > 255) level = 255;
   1303 	if (level < 0) level = 0;
   1304 	if (level == sc->sc_bl_level)
   1305 		return;
   1306 	sc->sc_bl_level = level;
   1307 	if (sc->sc_bl_on == 0)
   1308 		sc->sc_bl_on = 1;
   1309 	/* and here we would actually muck with the hardware */
   1310 	if ((level == 0) || (level == 255)) {
   1311 		/* in these cases bypass the PWM and use the gpio */
   1312 		voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1313 		if (level == 0) {
   1314 			voyager_write_gpio(sc->sc_gpio_cookie,
   1315 			    ~GPIO_BACKLIGHT, 0);
   1316 		} else {
   1317 			voyager_write_gpio(sc->sc_gpio_cookie,
   1318 			    0xffffffff, GPIO_BACKLIGHT);
   1319 		}
   1320 	} else {
   1321 		uint32_t pwm;
   1322 
   1323 		pwm = voyager_set_pwm(20000, level * 1000 / 256);
   1324 		pwm |= SM502_PWM_ENABLE;
   1325 		bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PWM0, pwm);
   1326 
   1327 		/* let the PWM take over */
   1328 		voyager_control_gpio(sc->sc_gpio_cookie,
   1329 		    0xffffffff, GPIO_BACKLIGHT);
   1330 	}
   1331 }
   1332 
   1333 static void
   1334 voyagerfb_switch_backlight(struct voyagerfb_softc *sc, int on)
   1335 {
   1336 
   1337 	if (on == sc->sc_bl_on)
   1338 		return;
   1339 	sc->sc_bl_on = on;
   1340 	if (on) {
   1341 		int level = sc->sc_bl_level;
   1342 
   1343 		sc->sc_bl_level = -1;
   1344 		voyagerfb_set_backlight(sc, level);
   1345 	} else {
   1346 		voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1347 		voyager_write_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
   1348 	}
   1349 }
   1350 
   1351 
   1352 static void
   1353 voyagerfb_brightness_up(device_t dev)
   1354 {
   1355 	struct voyagerfb_softc *sc = device_private(dev);
   1356 
   1357 	voyagerfb_set_backlight(sc, sc->sc_bl_level + 8);
   1358 }
   1359 
   1360 static void
   1361 voyagerfb_brightness_down(device_t dev)
   1362 {
   1363 	struct voyagerfb_softc *sc = device_private(dev);
   1364 
   1365 	voyagerfb_set_backlight(sc, sc->sc_bl_level - 8);
   1366 }
   1367 
   1368 static int
   1369 voyagerfb_set_curpos(struct voyagerfb_softc *sc, int x, int y)
   1370 {
   1371 	uint32_t val;
   1372 	int xx, yy;
   1373 
   1374 	sc->sc_cur_x = x;
   1375 	sc->sc_cur_y = y;
   1376 
   1377 	xx = x - sc->sc_hot_x;
   1378 	yy = y - sc->sc_hot_y;
   1379 
   1380 	if (xx < 0) xx = abs(xx) | 0x800;
   1381 	if (yy < 0) yy = abs(yy) | 0x800;
   1382 
   1383 	val = (xx & 0xffff) | (yy << 16);
   1384 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_XY, val);
   1385 
   1386 	return 0;
   1387 }
   1388 
   1389 static int
   1390 voyagerfb_gcursor(struct voyagerfb_softc *sc, struct wsdisplay_cursor *cur)
   1391 {
   1392 	/* do nothing for now */
   1393 	return 0;
   1394 }
   1395 
   1396 static int
   1397 voyagerfb_scursor(struct voyagerfb_softc *sc, struct wsdisplay_cursor *cur)
   1398 {
   1399 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1400 
   1401 		bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1402 		    SM502_PANEL_CRSR_ADDR,
   1403 		    sc->sc_cursor_addr | (cur->enable ? SM502_CRSR_ENABLE : 0));
   1404 		DPRINTF("%s: %08x\n", __func__, sc->sc_cursor_addr);
   1405 	}
   1406 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1407 
   1408 		sc->sc_hot_x = cur->hot.x;
   1409 		sc->sc_hot_y = cur->hot.y;
   1410 	}
   1411 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1412 
   1413 		voyagerfb_set_curpos(sc, cur->pos.x, cur->pos.y);
   1414 	}
   1415 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1416 		int i, idx;
   1417 		uint32_t val;
   1418 
   1419 		for (i = 0; i < cur->cmap.count; i++) {
   1420 			val = ((cur->cmap.red[i] & 0xf8) << 8) |
   1421 			      ((cur->cmap.green[i] & 0xfc) << 3) |
   1422 			      ((cur->cmap.blue[i] & 0xf8) >> 3);
   1423 			idx = i + cur->cmap.index;
   1424 			bus_space_write_2(sc->sc_memt, sc->sc_regh,
   1425 			    SM502_PANEL_CRSR_COL12 + (idx << 1),
   1426 			    val);
   1427 			/*
   1428 			 * if userland doesn't try to set the 3rd colour we
   1429 			 * assume it expects an X11-style 2 colour cursor
   1430 			 * X should be our main user anyway
   1431 			 */
   1432 			if ((idx == 1) &&
   1433 			   ((cur->cmap.count + cur->cmap.index) < 3)) {
   1434 				bus_space_write_2(sc->sc_memt, sc->sc_regh,
   1435 				    SM502_PANEL_CRSR_COL3,
   1436 				    val);
   1437 			}
   1438 			DPRINTF("%s: %d %04x\n", __func__, i + cur->cmap.index,
   1439 			    val);
   1440 		}
   1441 	}
   1442 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1443 
   1444 		int i, j, cnt = 0;
   1445 		uint32_t latch = 0, omask;
   1446 		uint8_t imask;
   1447 		DPRINTF("%s: %d %d\n", __func__, cur->size.x, cur->size.y);
   1448 		for (i = 0; i < 256; i++) {
   1449 			omask = 0x00000001;
   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 			imask = 0x01;
   1463 			cur->image[cnt] &= cur->mask[cnt];
   1464 			for (j = 0; j < 8; j++) {
   1465 				if (cur->mask[cnt] & imask)
   1466 					latch |= omask;
   1467 				omask <<= 1;
   1468 				if (cur->image[cnt] & imask)
   1469 					latch |= omask;
   1470 				omask <<= 1;
   1471 				imask <<= 1;
   1472 			}
   1473 			cnt++;
   1474 			sc->sc_cursor[i] = latch;
   1475 			latch = 0;
   1476 		}
   1477 	}
   1478 	return 0;
   1479 }
   1480