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