xref: /netbsd-src/sys/dev/pci/voyager/voyagerfb.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: voyagerfb.c,v 1.27 2014/03/11 08:19:45 mrg 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.27 2014/03/11 08:19:45 mrg 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 		} else
325 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
326 	}
327 	glyphcache_init(&sc->sc_gc, sc->sc_height,
328 			(sc->sc_fbsize / sc->sc_stride) - sc->sc_height,
329 			sc->sc_width,
330 			ri->ri_font->fontwidth,
331 			ri->ri_font->fontheight,
332 			defattr);
333 	if (is_console)
334 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
335 		    defattr);
336 
337 	rasops_get_cmap(ri, cmap, sizeof(cmap));
338 	j = 0;
339 	if (sc->sc_depth <= 8) {
340 		for (i = 0; i < 256; i++) {
341 
342 			sc->sc_cmap_red[i] = cmap[j];
343 			sc->sc_cmap_green[i] = cmap[j + 1];
344 			sc->sc_cmap_blue[i] = cmap[j + 2];
345 			voyagerfb_putpalreg(sc, i, cmap[j], cmap[j + 1],
346 			    cmap[j + 2]);
347 			j += 3;
348 		}
349 	}
350 
351 	voyagerfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
352 	    ri->ri_devcmap[(defattr >> 16) & 0xff]);
353 
354 	if (is_console)
355 		vcons_replay_msgbuf(&sc->sc_console_screen);
356 
357 	aa.console = is_console;
358 	aa.scrdata = &sc->sc_screenlist;
359 	aa.accessops = &voyagerfb_accessops;
360 	aa.accesscookie = &sc->vd;
361 
362 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
363 }
364 
365 static int
366 voyagerfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
367 	struct lwp *l)
368 {
369 	struct vcons_data *vd = v;
370 	struct voyagerfb_softc *sc = vd->cookie;
371 	struct wsdisplay_fbinfo *wdf;
372 	struct vcons_screen *ms = vd->active;
373 	struct wsdisplay_param  *param;
374 
375 	switch (cmd) {
376 	case WSDISPLAYIO_GTYPE:
377 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
378 		return 0;
379 
380 	/* PCI config read/write pass through. */
381 	case PCI_IOC_CFGREAD:
382 	case PCI_IOC_CFGWRITE:
383 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
384 		    cmd, data, flag, l);
385 
386 	case WSDISPLAYIO_GET_BUSID:
387 		return wsdisplayio_busid_pci(device_parent(sc->sc_dev),
388 		    sc->sc_pc, sc->sc_pcitag, data);
389 
390 	case WSDISPLAYIO_GINFO:
391 		if (ms == NULL)
392 			return ENODEV;
393 		wdf = (void *)data;
394 		wdf->height = ms->scr_ri.ri_height;
395 		wdf->width = ms->scr_ri.ri_width;
396 		wdf->depth = 32;
397 		wdf->cmsize = 256;
398 		return 0;
399 
400 	case WSDISPLAYIO_GETCMAP:
401 		return voyagerfb_getcmap(sc,
402 		    (struct wsdisplay_cmap *)data);
403 
404 	case WSDISPLAYIO_PUTCMAP:
405 		return voyagerfb_putcmap(sc,
406 		    (struct wsdisplay_cmap *)data);
407 
408 	case WSDISPLAYIO_LINEBYTES:
409 		*(u_int *)data = sc->sc_stride;
410 		return 0;
411 
412 	case WSDISPLAYIO_SMODE: {
413 		int new_mode = *(int*)data;
414 		if (new_mode != sc->sc_mode) {
415 			sc->sc_mode = new_mode;
416 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
417 #ifdef VOYAGERFB_DEPTH_32
418 				sc->sc_depth = 32;
419 #else
420 				sc->sc_depth = 8;
421 #endif
422 				glyphcache_wipe(&sc->sc_gc);
423 				voyagerfb_init(sc);
424 				voyagerfb_restore_palette(sc);
425 				vcons_redraw_screen(ms);
426 			} else {
427 				sc->sc_depth = 32;
428 				voyagerfb_init(sc);
429 			}
430 		}
431 		}
432 		return 0;
433 
434 	case WSDISPLAYIO_GVIDEO:
435 		*(int *)data = sc->sc_bl_on ? WSDISPLAYIO_VIDEO_ON :
436 					      WSDISPLAYIO_VIDEO_OFF;
437 		return 0;
438 
439 	case WSDISPLAYIO_SVIDEO: {
440 			int new_bl = *(int *)data;
441 
442 			voyagerfb_switch_backlight(sc,  new_bl);
443 		}
444 		return 0;
445 
446 	case WSDISPLAYIO_GETPARAM:
447 		param = (struct wsdisplay_param *)data;
448 		switch (param->param) {
449 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
450 			param->min = 0;
451 			param->max = 255;
452 			param->curval = sc->sc_bl_level;
453 			return 0;
454 		case WSDISPLAYIO_PARAM_BACKLIGHT:
455 			param->min = 0;
456 			param->max = 1;
457 			param->curval = sc->sc_bl_on;
458 			return 0;
459 		}
460 		return EPASSTHROUGH;
461 
462 	case WSDISPLAYIO_SETPARAM:
463 		param = (struct wsdisplay_param *)data;
464 		switch (param->param) {
465 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
466 			voyagerfb_set_backlight(sc, param->curval);
467 			return 0;
468 		case WSDISPLAYIO_PARAM_BACKLIGHT:
469 			voyagerfb_switch_backlight(sc,  param->curval);
470 			return 0;
471 		}
472 		return EPASSTHROUGH;
473 
474 	case WSDISPLAYIO_GCURPOS:
475 		{
476 			struct wsdisplay_curpos *pos;
477 
478 			pos = (struct wsdisplay_curpos *)data;
479 			pos->x = sc->sc_cur_x;
480 			pos->y = sc->sc_cur_y;
481 		}
482 		return 0;
483 
484 	case WSDISPLAYIO_SCURPOS:
485 		{
486 			struct wsdisplay_curpos *pos;
487 
488 			pos = (struct wsdisplay_curpos *)data;
489 			voyagerfb_set_curpos(sc, pos->x, pos->y);
490 		}
491 		return 0;
492 
493 	case WSDISPLAYIO_GCURMAX:
494 		{
495 			struct wsdisplay_curpos *pos;
496 
497 			pos = (struct wsdisplay_curpos *)data;
498 			pos->x = 64;
499 			pos->y = 64;
500 		}
501 		return 0;
502 
503 	case WSDISPLAYIO_GCURSOR:
504 		{
505 			struct wsdisplay_cursor *cu;
506 
507 			cu = (struct wsdisplay_cursor *)data;
508 			return voyagerfb_gcursor(sc, cu);
509 		}
510 
511 	case WSDISPLAYIO_SCURSOR:
512 		{
513 			struct wsdisplay_cursor *cu;
514 
515 			cu = (struct wsdisplay_cursor *)data;
516 			return voyagerfb_scursor(sc, cu);
517 		}
518 	}
519 	return EPASSTHROUGH;
520 }
521 
522 static paddr_t
523 voyagerfb_mmap(void *v, void *vs, off_t offset, int prot)
524 {
525 	struct vcons_data *vd = v;
526 	struct voyagerfb_softc *sc = vd->cookie;
527 	paddr_t pa;
528 
529 	/* 'regular' framebuffer mmap()ing */
530 	if (offset < sc->sc_fbsize) {
531 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
532 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
533 		return pa;
534 	}
535 
536 	/*
537 	 * restrict all other mappings to processes with privileges
538 	 */
539 	if (kauth_authorize_machdep(kauth_cred_get(),
540 	    KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0) {
541 		aprint_normal("%s: mmap() rejected.\n",
542 		    device_xname(sc->sc_dev));
543 		return -1;
544 	}
545 
546 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
547 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
548 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
549 		return pa;
550 	}
551 
552 	if ((offset >= sc->sc_reg) &&
553 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
554 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 0);
555 		return pa;
556 	}
557 
558 	return -1;
559 }
560 
561 static void
562 voyagerfb_init_screen(void *cookie, struct vcons_screen *scr,
563     int existing, long *defattr)
564 {
565 	struct voyagerfb_softc *sc = cookie;
566 	struct rasops_info *ri = &scr->scr_ri;
567 
568 	ri->ri_depth = sc->sc_depth;
569 	ri->ri_width = sc->sc_width;
570 	ri->ri_height = sc->sc_height;
571 	ri->ri_stride = sc->sc_stride;
572 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
573 
574 	ri->ri_bits = (char *)sc->sc_fbaddr;
575 
576 	if (existing) {
577 		ri->ri_flg |= RI_CLEAR;
578 	}
579 
580 	if (sc->sc_depth == 8) {
581 		ri->ri_flg |= RI_8BIT_IS_RGB;
582 #ifdef VOYAGERFB_ANTIALIAS
583 		ri->ri_flg |= RI_ENABLE_ALPHA;
584 #endif
585 	}
586 	if (sc->sc_depth == 32) {
587 #ifdef VOYAGERFB_ANTIALIAS
588 		ri->ri_flg |= RI_ENABLE_ALPHA;
589 #endif
590 		/* we always run in RGB */
591 		ri->ri_rnum = 8;
592 		ri->ri_gnum = 8;
593 		ri->ri_bnum = 8;
594 		ri->ri_rpos = 16;
595 		ri->ri_gpos = 8;
596 		ri->ri_bpos = 0;
597 	}
598 
599 	rasops_init(ri, 0, 0);
600 	ri->ri_caps = WSSCREEN_WSCOLORS;
601 
602 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
603 		    sc->sc_width / ri->ri_font->fontwidth);
604 
605 	ri->ri_hw = scr;
606 	ri->ri_ops.copyrows = voyagerfb_copyrows;
607 	ri->ri_ops.copycols = voyagerfb_copycols;
608 	ri->ri_ops.eraserows = voyagerfb_eraserows;
609 	ri->ri_ops.erasecols = voyagerfb_erasecols;
610 	ri->ri_ops.cursor = voyagerfb_cursor;
611 	if (FONT_IS_ALPHA(ri->ri_font)) {
612 	        switch (sc->sc_depth) {
613 	                case 32:
614                 		ri->ri_ops.putchar = voyagerfb_putchar_aa32;
615                 		break;
616                         case 8:
617                                 ri->ri_ops.putchar = voyagerfb_putchar_aa8;
618                                 break;
619                         default:
620                                 printf("alpha font at %d!?\n", sc->sc_depth);
621                 }
622 	} else
623 		ri->ri_ops.putchar = voyagerfb_putchar_mono;
624 }
625 
626 static int
627 voyagerfb_putcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
628 {
629 	u_char *r, *g, *b;
630 	u_int index = cm->index;
631 	u_int count = cm->count;
632 	int i, error;
633 	u_char rbuf[256], gbuf[256], bbuf[256];
634 
635 #ifdef VOYAGERFB_DEBUG
636 	aprint_debug("putcmap: %d %d\n",index, count);
637 #endif
638 	if (cm->index >= 256 || cm->count > 256 ||
639 	    (cm->index + cm->count) > 256)
640 		return EINVAL;
641 	error = copyin(cm->red, &rbuf[index], count);
642 	if (error)
643 		return error;
644 	error = copyin(cm->green, &gbuf[index], count);
645 	if (error)
646 		return error;
647 	error = copyin(cm->blue, &bbuf[index], count);
648 	if (error)
649 		return error;
650 
651 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
652 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
653 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
654 
655 	r = &sc->sc_cmap_red[index];
656 	g = &sc->sc_cmap_green[index];
657 	b = &sc->sc_cmap_blue[index];
658 
659 	for (i = 0; i < count; i++) {
660 		voyagerfb_putpalreg(sc, index, *r, *g, *b);
661 		index++;
662 		r++, g++, b++;
663 	}
664 	return 0;
665 }
666 
667 static int
668 voyagerfb_getcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
669 {
670 	u_int index = cm->index;
671 	u_int count = cm->count;
672 	int error;
673 
674 	if (index >= 255 || count > 256 || index + count > 256)
675 		return EINVAL;
676 
677 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
678 	if (error)
679 		return error;
680 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
681 	if (error)
682 		return error;
683 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
684 	if (error)
685 		return error;
686 
687 	return 0;
688 }
689 
690 static void
691 voyagerfb_restore_palette(struct voyagerfb_softc *sc)
692 {
693 	int i;
694 
695 	for (i = 0; i < 256; i++) {
696 		voyagerfb_putpalreg(sc, i, sc->sc_cmap_red[i],
697 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
698 	}
699 }
700 
701 static int
702 voyagerfb_putpalreg(struct voyagerfb_softc *sc, int idx, uint8_t r,
703     uint8_t g, uint8_t b)
704 {
705 	uint32_t reg;
706 
707 	reg = (r << 16) | (g << 8) | b;
708 	/* XXX we should probably write the CRT palette too */
709 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
710 	    SM502_PALETTE_PANEL + (idx << 2), reg);
711 	return 0;
712 }
713 
714 static void
715 voyagerfb_init(struct voyagerfb_softc *sc)
716 {
717 	int reg;
718 
719 	voyagerfb_wait(sc);
720 	/* disable colour compare */
721 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_COLOR_COMP_MASK, 0);
722 	/* allow writes to all planes */
723 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PLANEMASK,
724 	    0xffffffff);
725 	/* disable clipping */
726 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CLIP_TOP_LEFT, 0);
727 	/* source and destination in local memory, no offset */
728 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC_BASE, 0);
729 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST_BASE, 0);
730 	/* pitch is screen stride */
731 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PITCH,
732 	    sc->sc_width | (sc->sc_width << 16));
733 	/* window is screen width */
734 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_WINDOW_WIDTH,
735 	    sc->sc_width | (sc->sc_width << 16));
736 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL);
737 	reg &= ~SM502_PDC_DEPTH_MASK;
738 
739 	switch (sc->sc_depth) {
740 		case 8:
741 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
742 			    SM502_STRETCH, SM502_STRETCH_8BIT);
743 			sc->sc_stride = sc->sc_width;
744 			reg |= SM502_PDC_8BIT;
745 			break;
746 		case 16:
747 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
748 			    SM502_STRETCH, SM502_STRETCH_16BIT);
749 			sc->sc_stride = sc->sc_width << 1;
750 			reg |= SM502_PDC_16BIT;
751 			break;
752 		case 24:
753 		case 32:
754 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
755 			    SM502_STRETCH, SM502_STRETCH_32BIT);
756 			sc->sc_stride = sc->sc_width << 2;
757 			reg |= SM502_PDC_32BIT;
758 			break;
759 	}
760 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_FB_OFFSET,
761 	    (sc->sc_stride << 16) | sc->sc_stride);
762 
763 	/* clear the screen... */
764 	voyagerfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, 0);
765 
766 	/* ...and then switch colour depth. For aesthetic reasons. */
767 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL,
768 	    reg);
769 
770 	/* put the cursor at the end of video memory */
771 	sc->sc_cursor_addr = 16 * 1024 * 1024 - 16 * 64;	/* XXX */
772 	DPRINTF("%s: %08x\n", __func__, sc->sc_cursor_addr);
773 	sc->sc_cursor = (uint32_t *)((uint8_t *)bus_space_vaddr(sc->sc_memt,
774 			 sc->sc_fbh) + sc->sc_cursor_addr);
775 #ifdef VOYAGERFB_DEBUG
776 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_XY,
777 							 0x00100010);
778 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_COL12,
779 							 0x0000ffff);
780 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_COL3,
781 							 0x0000f800);
782 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_ADDR,
783 	    SM502_CRSR_ENABLE | sc->sc_cursor_addr);
784 	sc->sc_cursor[0] = 0x00000000;
785 	sc->sc_cursor[1] = 0x00000000;
786 	sc->sc_cursor[2] = 0xffffffff;
787 	sc->sc_cursor[3] = 0xffffffff;
788 	sc->sc_cursor[4] = 0xaaaaaaaa;
789 	sc->sc_cursor[5] = 0xaaaaaaaa;
790 	sc->sc_cursor[6] = 0xffffffff;
791 	sc->sc_cursor[7] = 0x00000000;
792 #else
793 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_ADDR,
794 	    sc->sc_cursor_addr);
795 #endif
796 }
797 
798 static void
799 voyagerfb_rectfill(struct voyagerfb_softc *sc, int x, int y, int wi, int he,
800      uint32_t colour)
801 {
802 
803 	voyagerfb_ready(sc);
804 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL,
805 	    ROP_COPY |
806 	    SM502_CTRL_USE_ROP2 |
807 	    SM502_CTRL_CMD_RECTFILL |
808 	    SM502_CTRL_QUICKSTART_E);
809 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND,
810 	    colour);
811 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
812 	    (x << 16) | y);
813 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
814 	    (wi << 16) | he);
815 }
816 
817 static void
818 voyagerfb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
819     int wi, int he, int rop)
820 {
821 	struct voyagerfb_softc *sc = cookie;
822 	uint32_t cmd;
823 
824 	cmd = (rop & 0xf) | SM502_CTRL_USE_ROP2 | SM502_CTRL_CMD_BITBLT |
825 	      SM502_CTRL_QUICKSTART_E;
826 
827 	voyagerfb_ready(sc);
828 
829 	if (xd <= xs) {
830 		/* left to right */
831 	} else {
832 		/*
833 		 * According to the manual this flag reverses only the blitter's
834 		 * X direction. At least on my Gdium it also reverses the Y
835 		 * direction
836 		 */
837 		cmd |= SM502_CTRL_R_TO_L;
838 		xs += wi - 1;
839 		xd += wi - 1;
840 		ys += he - 1;
841 		yd += he - 1;
842 	}
843 
844 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
845 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC,
846 	    (xs << 16) | ys);
847 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
848 	    (xd << 16) | yd);
849 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
850 	    (wi << 16) | he);
851 }
852 
853 static void
854 voyagerfb_cursor(void *cookie, int on, int row, int col)
855 {
856 	struct rasops_info *ri = cookie;
857 	struct vcons_screen *scr = ri->ri_hw;
858 	struct voyagerfb_softc *sc = scr->scr_cookie;
859 	int x, y, wi, he;
860 
861 	wi = ri->ri_font->fontwidth;
862 	he = ri->ri_font->fontheight;
863 
864 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
865 		x = ri->ri_ccol * wi + ri->ri_xorigin;
866 		y = ri->ri_crow * he + ri->ri_yorigin;
867 		if (ri->ri_flg & RI_CURSOR) {
868 			voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
869 			ri->ri_flg &= ~RI_CURSOR;
870 		}
871 		ri->ri_crow = row;
872 		ri->ri_ccol = col;
873 		if (on) {
874 			x = ri->ri_ccol * wi + ri->ri_xorigin;
875 			y = ri->ri_crow * he + ri->ri_yorigin;
876 			voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
877 			ri->ri_flg |= RI_CURSOR;
878 		}
879 	} else {
880 		scr->scr_ri.ri_crow = row;
881 		scr->scr_ri.ri_ccol = col;
882 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
883 	}
884 
885 }
886 
887 static inline void
888 voyagerfb_feed8(struct voyagerfb_softc *sc, uint8_t *data, int len)
889 {
890 	uint32_t *port = (uint32_t *)sc->sc_dataport;
891 	int i;
892 
893 	for (i = 0; i < ((len + 3) & 0xfffc); i++) {
894 		*port = *data;
895 		data++;
896 	}
897 }
898 
899 static inline void
900 voyagerfb_feed16(struct voyagerfb_softc *sc, uint16_t *data, int len)
901 {
902 	uint32_t *port = (uint32_t *)sc->sc_dataport;
903 	int i;
904 
905 	len = len << 1;
906 	for (i = 0; i < ((len + 1) & 0xfffe); i++) {
907 		*port = *data;
908 		data++;
909 	}
910 }
911 
912 static void
913 voyagerfb_putchar_mono(void *cookie, int row, int col, u_int c, long attr)
914 {
915 	struct rasops_info *ri = cookie;
916 	struct wsdisplay_font *font = PICK_FONT(ri, c);
917 	struct vcons_screen *scr = ri->ri_hw;
918 	struct voyagerfb_softc *sc = scr->scr_cookie;
919 	uint32_t cmd;
920 	int fg, bg;
921 	uint8_t *data;
922 	int x, y, wi, he;
923 
924 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
925 		return;
926 
927 	if (!CHAR_IN_FONT(c, font))
928 		return;
929 
930 	wi = font->fontwidth;
931 	he = font->fontheight;
932 
933 	bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
934 	fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
935 	x = ri->ri_xorigin + col * wi;
936 	y = ri->ri_yorigin + row * he;
937 	if (c == 0x20) {
938 		voyagerfb_rectfill(sc, x, y, wi, he, bg);
939 		return;
940 	}
941 
942 	data = WSFONT_GLYPH(c, font);
943 
944 	cmd = ROP_COPY |
945 	      SM502_CTRL_USE_ROP2 |
946 	      SM502_CTRL_CMD_HOSTWRT |
947 	      SM502_CTRL_HOSTBLT_MONO |
948 	      SM502_CTRL_QUICKSTART_E |
949 	      SM502_CTRL_MONO_PACK_32BIT;
950 	voyagerfb_ready(sc);
951 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
952 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND, fg);
953 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_BACKGROUND, bg);
954 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, 0);
955 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, (x << 16) | y);
956 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
957 	    SM502_DIMENSION, (wi << 16) | he);
958 	/* now feed the data, padded to 32bit */
959 	switch (ri->ri_font->stride) {
960 		case 1:
961 			voyagerfb_feed8(sc, data, ri->ri_fontscale);
962 			break;
963 		case 2:
964 			voyagerfb_feed16(sc, (uint16_t *)data,
965 			    ri->ri_fontscale);
966 			break;
967 	}
968 }
969 
970 static void
971 voyagerfb_putchar_aa32(void *cookie, int row, int col, u_int c, long attr)
972 {
973 	struct rasops_info *ri = cookie;
974 	struct wsdisplay_font *font = PICK_FONT(ri, c);
975 	struct vcons_screen *scr = ri->ri_hw;
976 	struct voyagerfb_softc *sc = scr->scr_cookie;
977 	uint32_t cmd;
978 	int fg, bg;
979 	uint8_t *data;
980 	int x, y, wi, he;
981 	int i, j, r, g, b, aval, pad;
982 	int rf, gf, bf, rb, gb, bb;
983 	uint32_t pixel;
984 	int rv;
985 
986 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
987 		return;
988 
989 	if (!CHAR_IN_FONT(c, font))
990 		return;
991 
992 	wi = font->fontwidth;
993 	he = font->fontheight;
994 
995 	bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
996 	fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
997 	x = ri->ri_xorigin + col * wi;
998 	y = ri->ri_yorigin + row * he;
999 	if (c == 0x20) {
1000 		voyagerfb_rectfill(sc, x, y, wi, he, bg);
1001 		return;
1002 	}
1003 
1004 	data = WSFONT_GLYPH(c, font);
1005 	/*
1006 	 * we can't accelerate the actual alpha blending but
1007 	 * we can at least use a host blit to go through the
1008 	 * pipeline instead of having to sync the engine
1009 	 */
1010 
1011 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1012 	if (rv == GC_OK)
1013 		return;
1014 
1015 	cmd = ROP_COPY |
1016 	      SM502_CTRL_USE_ROP2 |
1017 	      SM502_CTRL_CMD_HOSTWRT |
1018 	      SM502_CTRL_QUICKSTART_E;
1019 	voyagerfb_ready(sc);
1020 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
1021 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, 0);
1022 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, (x << 16) | y);
1023 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
1024 	    (wi << 16) | he);
1025 	rf = (fg >> 16) & 0xff;
1026 	rb = (bg >> 16) & 0xff;
1027 	gf = (fg >> 8) & 0xff;
1028 	gb = (bg >> 8) & 0xff;
1029 	bf =  fg & 0xff;
1030 	bb =  bg & 0xff;
1031 	pad = wi & 1;
1032 	for (i = 0; i < he; i++) {
1033 		for (j = 0; j < wi; j++) {
1034 			aval = *data;
1035 			data++;
1036 			if (aval == 0) {
1037 				pixel = bg;
1038 			} else if (aval == 255) {
1039 				pixel = fg;
1040 			} else {
1041 				r = aval * rf + (255 - aval) * rb;
1042 				g = aval * gf + (255 - aval) * gb;
1043 				b = aval * bf + (255 - aval) * bb;
1044 				pixel = (r & 0xff00) << 8 |
1045 				        (g & 0xff00) |
1046 				        (b & 0xff00) >> 8;
1047 			}
1048 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
1049 			    SM502_DATAPORT, pixel);
1050 		}
1051 		if (pad)
1052 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
1053 			    SM502_DATAPORT, 0);
1054 	}
1055 	if (rv == GC_ADD) {
1056 		glyphcache_add(&sc->sc_gc, c, x, y);
1057 	}
1058 }
1059 
1060 static void
1061 voyagerfb_putchar_aa8(void *cookie, int row, int col, u_int c, long attr)
1062 {
1063 	struct rasops_info *ri = cookie;
1064 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1065 	struct vcons_screen *scr = ri->ri_hw;
1066 	struct voyagerfb_softc *sc = scr->scr_cookie;
1067 	uint32_t cmd;
1068 	int bg;
1069 	uint8_t *data;
1070 	int x, y, wi, he;
1071 	int i, j, r, g, b, aval, pad;
1072 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
1073 	uint32_t pixel = 0, latch = 0, bg8, fg8;
1074 	int rv;
1075 
1076 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1077 		return;
1078 
1079 	if (!CHAR_IN_FONT(c, font))
1080 		return;
1081 
1082 	wi = font->fontwidth;
1083 	he = font->fontheight;
1084 
1085 	bg = ri->ri_devcmap[(attr >> 16) & 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