Home | History | Annotate | Line # | Download | only in pci
r128fb.c revision 1.22.6.2
      1  1.22.6.2       mrg /*	$NetBSD: r128fb.c,v 1.22.6.2 2012/03/11 01:52:28 mrg Exp $	*/
      2       1.1  macallan 
      3       1.1  macallan /*
      4       1.1  macallan  * Copyright (c) 2007 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 ATI Rage 128 graphics controllers
     30       1.1  macallan  * tested on macppc only so far
     31       1.1  macallan  */
     32       1.1  macallan 
     33       1.1  macallan #include <sys/cdefs.h>
     34  1.22.6.2       mrg __KERNEL_RCSID(0, "$NetBSD: r128fb.c,v 1.22.6.2 2012/03/11 01:52:28 mrg 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/pci/r128fbreg.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.20    cegger #include <dev/pci/wsdisplay_pci.h>
     58  1.22.6.1       mrg #include <dev/wscons/wsdisplay_glyphcachevar.h>
     59       1.1  macallan 
     60       1.1  macallan #include <dev/i2c/i2cvar.h>
     61       1.1  macallan 
     62      1.11  macallan #include "opt_r128fb.h"
     63      1.22  macallan #include "opt_vcons.h"
     64      1.11  macallan 
     65      1.11  macallan #ifdef R128FB_DEBUG
     66      1.11  macallan #define DPRINTF printf
     67      1.11  macallan #else
     68      1.11  macallan #define DPRINTF while(0) printf
     69      1.11  macallan #endif
     70      1.11  macallan 
     71       1.1  macallan struct r128fb_softc {
     72       1.1  macallan 	device_t sc_dev;
     73       1.1  macallan 
     74       1.1  macallan 	pci_chipset_tag_t sc_pc;
     75       1.1  macallan 	pcitag_t sc_pcitag;
     76       1.1  macallan 
     77       1.1  macallan 	bus_space_tag_t sc_memt;
     78       1.1  macallan 	bus_space_tag_t sc_iot;
     79       1.1  macallan 
     80       1.1  macallan 	bus_space_handle_t sc_regh;
     81       1.1  macallan 	bus_addr_t sc_fb, sc_reg;
     82       1.1  macallan 	bus_size_t sc_fbsize, sc_regsize;
     83       1.1  macallan 
     84       1.1  macallan 	int sc_width, sc_height, sc_depth, sc_stride;
     85      1.14  macallan 	int sc_locked, sc_have_backlight, sc_bl_level, sc_bl_on;
     86       1.1  macallan 	struct vcons_screen sc_console_screen;
     87       1.1  macallan 	struct wsscreen_descr sc_defaultscreen_descr;
     88       1.1  macallan 	const struct wsscreen_descr *sc_screens[1];
     89       1.1  macallan 	struct wsscreen_list sc_screenlist;
     90       1.1  macallan 	struct vcons_data vd;
     91       1.1  macallan 	int sc_mode;
     92       1.1  macallan 	u_char sc_cmap_red[256];
     93       1.1  macallan 	u_char sc_cmap_green[256];
     94       1.1  macallan 	u_char sc_cmap_blue[256];
     95       1.1  macallan 	/* engine stuff */
     96       1.1  macallan 	uint32_t sc_master_cntl;
     97  1.22.6.1       mrg 	glyphcache sc_gc;
     98       1.1  macallan };
     99       1.1  macallan 
    100       1.1  macallan static int	r128fb_match(device_t, cfdata_t, void *);
    101       1.1  macallan static void	r128fb_attach(device_t, device_t, void *);
    102       1.1  macallan 
    103       1.1  macallan CFATTACH_DECL_NEW(r128fb, sizeof(struct r128fb_softc),
    104       1.1  macallan     r128fb_match, r128fb_attach, NULL, NULL);
    105       1.1  macallan 
    106       1.1  macallan extern const u_char rasops_cmap[768];
    107       1.1  macallan 
    108       1.1  macallan static int	r128fb_ioctl(void *, void *, u_long, void *, int,
    109       1.1  macallan 			     struct lwp *);
    110       1.1  macallan static paddr_t	r128fb_mmap(void *, void *, off_t, int);
    111       1.1  macallan static void	r128fb_init_screen(void *, struct vcons_screen *, int, long *);
    112       1.1  macallan 
    113       1.1  macallan static int	r128fb_putcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
    114       1.1  macallan static int 	r128fb_getcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
    115       1.1  macallan static void	r128fb_restore_palette(struct r128fb_softc *);
    116       1.1  macallan static int 	r128fb_putpalreg(struct r128fb_softc *, uint8_t, uint8_t,
    117       1.1  macallan 			    uint8_t, uint8_t);
    118       1.1  macallan 
    119       1.1  macallan static void	r128fb_init(struct r128fb_softc *);
    120       1.1  macallan static void	r128fb_flush_engine(struct r128fb_softc *);
    121       1.1  macallan static void	r128fb_rectfill(struct r128fb_softc *, int, int, int, int,
    122       1.1  macallan 			    uint32_t);
    123  1.22.6.1       mrg static void	r128fb_bitblt(void *, int, int, int, int, int,
    124       1.1  macallan 			    int, int);
    125       1.1  macallan 
    126       1.1  macallan static void	r128fb_cursor(void *, int, int, int);
    127       1.1  macallan static void	r128fb_putchar(void *, int, int, u_int, long);
    128  1.22.6.1       mrg static void	r128fb_putchar_aa(void *, int, int, u_int, long);
    129       1.1  macallan static void	r128fb_copycols(void *, int, int, int, int);
    130       1.1  macallan static void	r128fb_erasecols(void *, int, int, int, long);
    131       1.1  macallan static void	r128fb_copyrows(void *, int, int, int);
    132       1.1  macallan static void	r128fb_eraserows(void *, int, int, long);
    133       1.1  macallan 
    134      1.11  macallan static void	r128fb_brightness_up(device_t);
    135      1.11  macallan static void	r128fb_brightness_down(device_t);
    136      1.14  macallan /* set backlight level */
    137      1.11  macallan static void	r128fb_set_backlight(struct r128fb_softc *, int);
    138      1.14  macallan /* turn backlight on and off without messing with the level */
    139      1.14  macallan static void	r128fb_switch_backlight(struct r128fb_softc *, int);
    140      1.11  macallan 
    141       1.1  macallan struct wsdisplay_accessops r128fb_accessops = {
    142       1.1  macallan 	r128fb_ioctl,
    143       1.1  macallan 	r128fb_mmap,
    144       1.1  macallan 	NULL,	/* alloc_screen */
    145       1.1  macallan 	NULL,	/* free_screen */
    146       1.1  macallan 	NULL,	/* show_screen */
    147       1.1  macallan 	NULL, 	/* load_font */
    148       1.1  macallan 	NULL,	/* pollc */
    149       1.1  macallan 	NULL	/* scroll */
    150       1.1  macallan };
    151       1.1  macallan 
    152       1.1  macallan static inline void
    153       1.1  macallan r128fb_wait(struct r128fb_softc *sc, int slots)
    154       1.1  macallan {
    155       1.1  macallan 	uint32_t reg;
    156       1.1  macallan 
    157       1.1  macallan 	do {
    158       1.1  macallan 		reg = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    159       1.1  macallan 		    R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK);
    160       1.1  macallan 	} while (reg <= slots);
    161       1.1  macallan }
    162       1.1  macallan 
    163       1.1  macallan static void
    164       1.1  macallan r128fb_flush_engine(struct r128fb_softc *sc)
    165       1.1  macallan {
    166       1.1  macallan 	uint32_t reg;
    167       1.1  macallan 
    168       1.1  macallan 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT);
    169       1.1  macallan 	reg |= R128_PC_FLUSH_ALL;
    170       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT, reg);
    171       1.1  macallan 	do {
    172       1.1  macallan 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    173       1.1  macallan 		    R128_PC_NGUI_CTLSTAT);
    174       1.1  macallan 	} while (reg & R128_PC_BUSY);
    175       1.1  macallan }
    176       1.1  macallan 
    177       1.1  macallan static int
    178       1.1  macallan r128fb_match(device_t parent, cfdata_t match, void *aux)
    179       1.1  macallan {
    180       1.1  macallan 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    181       1.1  macallan 
    182       1.5  macallan 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    183       1.1  macallan 		return 0;
    184       1.1  macallan 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI)
    185       1.1  macallan 		return 0;
    186       1.1  macallan 
    187       1.5  macallan 	/* only cards tested on so far - likely need a list */
    188       1.5  macallan 	if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE1AGP4XT) ||
    189       1.6  jmcneill 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE3AGP4XT) ||
    190      1.10      jmmv 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGEGLPCI) ||
    191       1.6  jmcneill 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP))
    192       1.1  macallan 		return 100;
    193       1.1  macallan 	return (0);
    194       1.1  macallan }
    195       1.1  macallan 
    196       1.1  macallan static void
    197       1.1  macallan r128fb_attach(device_t parent, device_t self, void *aux)
    198       1.1  macallan {
    199       1.1  macallan 	struct r128fb_softc	*sc = device_private(self);
    200       1.1  macallan 	struct pci_attach_args	*pa = aux;
    201       1.1  macallan 	struct rasops_info	*ri;
    202      1.14  macallan 	bus_space_tag_t		tag;
    203       1.1  macallan 	struct wsemuldisplaydev_attach_args aa;
    204       1.1  macallan 	prop_dictionary_t	dict;
    205       1.1  macallan 	unsigned long		defattr;
    206       1.1  macallan 	bool			is_console;
    207      1.22  macallan 	int			i, j;
    208      1.22  macallan 	uint32_t		reg, flags;
    209  1.22.6.1       mrg 	uint8_t			tmp;
    210       1.1  macallan 
    211       1.1  macallan 	sc->sc_pc = pa->pa_pc;
    212       1.1  macallan 	sc->sc_pcitag = pa->pa_tag;
    213       1.1  macallan 	sc->sc_memt = pa->pa_memt;
    214       1.1  macallan 	sc->sc_iot = pa->pa_iot;
    215       1.1  macallan 	sc->sc_dev = self;
    216       1.1  macallan 
    217  1.22.6.1       mrg 	pci_aprint_devinfo(pa, NULL);
    218       1.1  macallan 
    219       1.1  macallan 	/* fill in parameters from properties */
    220       1.1  macallan 	dict = device_properties(self);
    221       1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    222       1.1  macallan 		aprint_error("%s: no width property\n", device_xname(self));
    223       1.1  macallan 		return;
    224       1.1  macallan 	}
    225       1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    226       1.1  macallan 		aprint_error("%s: no height property\n", device_xname(self));
    227       1.1  macallan 		return;
    228       1.1  macallan 	}
    229       1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    230       1.1  macallan 		aprint_error("%s: no depth property\n", device_xname(self));
    231       1.1  macallan 		return;
    232       1.1  macallan 	}
    233       1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) {
    234       1.1  macallan 		aprint_error("%s: no linebytes property\n",
    235       1.1  macallan 		    device_xname(self));
    236       1.1  macallan 		return;
    237       1.1  macallan 	}
    238       1.1  macallan 
    239       1.1  macallan 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    240       1.1  macallan 
    241      1.13  macallan 	if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x10, PCI_MAPREG_TYPE_MEM,
    242      1.13  macallan 	    &sc->sc_fb, &sc->sc_fbsize, &flags)) {
    243       1.1  macallan 		aprint_error("%s: failed to map the frame buffer.\n",
    244       1.1  macallan 		    device_xname(sc->sc_dev));
    245       1.1  macallan 	}
    246       1.1  macallan 
    247       1.1  macallan 	if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, 0,
    248      1.14  macallan 	    &tag, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    249       1.1  macallan 		aprint_error("%s: failed to map registers.\n",
    250       1.1  macallan 		    device_xname(sc->sc_dev));
    251       1.1  macallan 	}
    252       1.1  macallan 
    253       1.1  macallan 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    254       1.2  macallan 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    255       1.1  macallan 
    256       1.1  macallan 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    257       1.1  macallan 		"default",
    258       1.1  macallan 		0, 0,
    259       1.1  macallan 		NULL,
    260       1.1  macallan 		8, 16,
    261       1.1  macallan 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    262       1.1  macallan 		NULL
    263       1.1  macallan 	};
    264       1.1  macallan 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    265       1.1  macallan 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    266       1.1  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    267       1.1  macallan 	sc->sc_locked = 0;
    268       1.1  macallan 
    269       1.1  macallan 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    270       1.1  macallan 	    &r128fb_accessops);
    271       1.1  macallan 	sc->vd.init_screen = r128fb_init_screen;
    272       1.1  macallan 
    273       1.1  macallan 	/* init engine here */
    274       1.1  macallan 	r128fb_init(sc);
    275       1.1  macallan 
    276       1.1  macallan 	ri = &sc->sc_console_screen.scr_ri;
    277       1.1  macallan 
    278       1.9  macallan 	j = 0;
    279  1.22.6.1       mrg 	if (sc->sc_depth == 8) {
    280  1.22.6.1       mrg 		/* generate an r3g3b2 colour map */
    281  1.22.6.1       mrg 		for (i = 0; i < 256; i++) {
    282  1.22.6.1       mrg 			tmp = i & 0xe0;
    283  1.22.6.1       mrg 			/*
    284  1.22.6.1       mrg 			 * replicate bits so 0xe0 maps to a red value of 0xff
    285  1.22.6.1       mrg 			 * in order to make white look actually white
    286  1.22.6.1       mrg 			 */
    287  1.22.6.1       mrg 			tmp |= (tmp >> 3) | (tmp >> 6);
    288  1.22.6.1       mrg 			sc->sc_cmap_red[i] = tmp;
    289       1.9  macallan 
    290  1.22.6.1       mrg 			tmp = (i & 0x1c) << 3;
    291  1.22.6.1       mrg 			tmp |= (tmp >> 3) | (tmp >> 6);
    292  1.22.6.1       mrg 			sc->sc_cmap_green[i] = tmp;
    293  1.22.6.1       mrg 
    294  1.22.6.1       mrg 			tmp = (i & 0x03) << 6;
    295  1.22.6.1       mrg 			tmp |= tmp >> 2;
    296  1.22.6.1       mrg 			tmp |= tmp >> 4;
    297  1.22.6.1       mrg 			sc->sc_cmap_blue[i] = tmp;
    298  1.22.6.1       mrg 
    299  1.22.6.1       mrg 			r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
    300  1.22.6.1       mrg 				       sc->sc_cmap_green[i],
    301  1.22.6.1       mrg 				       sc->sc_cmap_blue[i]);
    302  1.22.6.1       mrg 		}
    303  1.22.6.1       mrg 	} else {
    304  1.22.6.1       mrg 		/* steal rasops' ANSI cmap */
    305  1.22.6.1       mrg 		for (i = 0; i < 256; i++) {
    306  1.22.6.1       mrg 			sc->sc_cmap_red[i] = i;
    307  1.22.6.1       mrg 			sc->sc_cmap_green[i] = i;
    308  1.22.6.1       mrg 			sc->sc_cmap_blue[i] = i;
    309  1.22.6.1       mrg 			r128fb_putpalreg(sc, i, i, i, i);
    310  1.22.6.1       mrg 			j += 3;
    311  1.22.6.1       mrg 		}
    312       1.9  macallan 	}
    313       1.9  macallan 
    314  1.22.6.1       mrg 	sc->sc_gc.gc_bitblt = r128fb_bitblt;
    315  1.22.6.1       mrg 	sc->sc_gc.gc_blitcookie = sc;
    316  1.22.6.1       mrg 	sc->sc_gc.gc_rop = R128_ROP3_S;
    317       1.1  macallan 	if (is_console) {
    318       1.1  macallan 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    319       1.1  macallan 		    &defattr);
    320       1.1  macallan 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    321       1.1  macallan 
    322       1.9  macallan 		r128fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    323       1.9  macallan 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    324       1.1  macallan 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    325       1.1  macallan 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    326       1.1  macallan 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    327       1.1  macallan 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    328  1.22.6.2       mrg 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    329  1.22.6.1       mrg 				sc->sc_width,
    330  1.22.6.2       mrg 				(0x800000 / sc->sc_stride) - sc->sc_height - 5,
    331  1.22.6.1       mrg 				ri->ri_font->fontwidth,
    332  1.22.6.1       mrg 				ri->ri_font->fontheight,
    333  1.22.6.1       mrg 				defattr);
    334       1.1  macallan 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    335       1.1  macallan 		    defattr);
    336       1.9  macallan 		vcons_replay_msgbuf(&sc->sc_console_screen);
    337       1.1  macallan 	} else {
    338       1.1  macallan 		/*
    339       1.1  macallan 		 * since we're not the console we can postpone the rest
    340       1.1  macallan 		 * until someone actually allocates a screen for us
    341       1.1  macallan 		 */
    342       1.1  macallan 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    343  1.22.6.1       mrg 		glyphcache_init(&sc->sc_gc, sc->sc_height,
    344  1.22.6.1       mrg 				sc->sc_width,
    345  1.22.6.1       mrg 				(0x800000 / sc->sc_stride) - sc->sc_height,
    346  1.22.6.1       mrg 				ri->ri_font->fontwidth,
    347  1.22.6.1       mrg 				ri->ri_font->fontheight,
    348  1.22.6.1       mrg 				defattr);
    349       1.1  macallan 	}
    350       1.1  macallan 
    351      1.11  macallan 	/* no suspend/resume support yet */
    352      1.11  macallan 	pmf_device_register(sc->sc_dev, NULL, NULL);
    353      1.16  macallan 
    354      1.11  macallan 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
    355      1.16  macallan 	DPRINTF("R128_LVDS_GEN_CNTL: %08x\n", reg);
    356      1.11  macallan 	if (reg & R128_LVDS_ON) {
    357      1.11  macallan 		sc->sc_have_backlight = 1;
    358      1.14  macallan 		sc->sc_bl_on = 1;
    359      1.11  macallan 		sc->sc_bl_level = 255 -
    360      1.11  macallan 		    ((reg & R128_LEVEL_MASK) >> R128_LEVEL_SHIFT);
    361      1.11  macallan 		pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
    362      1.11  macallan 		    r128fb_brightness_up, TRUE);
    363      1.11  macallan 		pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
    364      1.11  macallan 		    r128fb_brightness_down, TRUE);
    365      1.12  macallan 		aprint_verbose("%s: LVDS output is active, enabling backlight"
    366      1.12  macallan 			       " control\n", device_xname(self));
    367      1.11  macallan 	} else
    368      1.11  macallan 		sc->sc_have_backlight = 0;
    369      1.12  macallan 
    370      1.12  macallan 	aa.console = is_console;
    371      1.12  macallan 	aa.scrdata = &sc->sc_screenlist;
    372      1.12  macallan 	aa.accessops = &r128fb_accessops;
    373      1.12  macallan 	aa.accesscookie = &sc->vd;
    374      1.12  macallan 
    375      1.12  macallan 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    376       1.1  macallan }
    377       1.1  macallan 
    378       1.1  macallan static int
    379       1.1  macallan r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    380       1.1  macallan 	struct lwp *l)
    381       1.1  macallan {
    382       1.1  macallan 	struct vcons_data *vd = v;
    383       1.1  macallan 	struct r128fb_softc *sc = vd->cookie;
    384       1.1  macallan 	struct wsdisplay_fbinfo *wdf;
    385       1.1  macallan 	struct vcons_screen *ms = vd->active;
    386      1.12  macallan 	struct wsdisplay_param  *param;
    387       1.1  macallan 
    388       1.1  macallan 	switch (cmd) {
    389      1.17    cegger 	case WSDISPLAYIO_GTYPE:
    390      1.17    cegger 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    391      1.17    cegger 		return 0;
    392       1.1  macallan 
    393      1.17    cegger 	/* PCI config read/write passthrough. */
    394      1.17    cegger 	case PCI_IOC_CFGREAD:
    395      1.17    cegger 	case PCI_IOC_CFGWRITE:
    396      1.20    cegger 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    397      1.20    cegger 		    cmd, data, flag, l);
    398      1.20    cegger 
    399      1.20    cegger 	case WSDISPLAYIO_GET_BUSID:
    400      1.20    cegger 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc, sc->sc_pcitag, data);
    401      1.17    cegger 
    402      1.17    cegger 	case WSDISPLAYIO_GINFO:
    403      1.17    cegger 		if (ms == NULL)
    404      1.17    cegger 			return ENODEV;
    405      1.17    cegger 		wdf = (void *)data;
    406      1.17    cegger 		wdf->height = ms->scr_ri.ri_height;
    407      1.17    cegger 		wdf->width = ms->scr_ri.ri_width;
    408      1.17    cegger 		wdf->depth = ms->scr_ri.ri_depth;
    409      1.17    cegger 		wdf->cmsize = 256;
    410      1.17    cegger 		return 0;
    411       1.1  macallan 
    412      1.17    cegger 	case WSDISPLAYIO_GETCMAP:
    413      1.17    cegger 		return r128fb_getcmap(sc,
    414      1.17    cegger 		    (struct wsdisplay_cmap *)data);
    415      1.17    cegger 
    416      1.17    cegger 	case WSDISPLAYIO_PUTCMAP:
    417      1.17    cegger 		return r128fb_putcmap(sc,
    418      1.17    cegger 		    (struct wsdisplay_cmap *)data);
    419       1.1  macallan 
    420      1.17    cegger 	case WSDISPLAYIO_LINEBYTES:
    421      1.17    cegger 		*(u_int *)data = sc->sc_stride;
    422      1.17    cegger 		return 0;
    423       1.1  macallan 
    424      1.17    cegger 	case WSDISPLAYIO_SMODE: {
    425      1.17    cegger 		int new_mode = *(int*)data;
    426      1.17    cegger 		if (new_mode != sc->sc_mode) {
    427      1.17    cegger 			sc->sc_mode = new_mode;
    428      1.17    cegger 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    429      1.17    cegger 				r128fb_init(sc);
    430      1.17    cegger 				r128fb_restore_palette(sc);
    431  1.22.6.1       mrg 				glyphcache_wipe(&sc->sc_gc);
    432      1.19  macallan 				r128fb_rectfill(sc, 0, 0, sc->sc_width,
    433      1.19  macallan 				    sc->sc_height, ms->scr_ri.ri_devcmap[
    434      1.19  macallan 				    (ms->scr_defattr >> 16) & 0xff]);
    435      1.17    cegger 				vcons_redraw_screen(ms);
    436       1.1  macallan 			}
    437      1.17    cegger 		}
    438      1.17    cegger 		}
    439      1.17    cegger 		return 0;
    440      1.12  macallan 
    441      1.17    cegger 	case WSDISPLAYIO_GETPARAM:
    442      1.17    cegger 		param = (struct wsdisplay_param *)data;
    443      1.17    cegger 		if (sc->sc_have_backlight == 0)
    444      1.13  macallan 			return EPASSTHROUGH;
    445      1.17    cegger 		switch (param->param) {
    446      1.17    cegger 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    447      1.17    cegger 			param->min = 0;
    448      1.17    cegger 			param->max = 255;
    449      1.17    cegger 			param->curval = sc->sc_bl_level;
    450      1.17    cegger 			return 0;
    451      1.17    cegger 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    452      1.17    cegger 			param->min = 0;
    453      1.17    cegger 			param->max = 1;
    454      1.17    cegger 			param->curval = sc->sc_bl_on;
    455      1.17    cegger 			return 0;
    456      1.17    cegger 		}
    457      1.17    cegger 		return EPASSTHROUGH;
    458      1.12  macallan 
    459      1.17    cegger 	case WSDISPLAYIO_SETPARAM:
    460      1.17    cegger 		param = (struct wsdisplay_param *)data;
    461      1.17    cegger 		if (sc->sc_have_backlight == 0)
    462      1.13  macallan 			return EPASSTHROUGH;
    463      1.17    cegger 		switch (param->param) {
    464      1.17    cegger 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    465      1.17    cegger 			r128fb_set_backlight(sc, param->curval);
    466      1.17    cegger 			return 0;
    467      1.17    cegger 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    468      1.17    cegger 			r128fb_switch_backlight(sc,  param->curval);
    469      1.17    cegger 			return 0;
    470      1.17    cegger 		}
    471      1.17    cegger 		return EPASSTHROUGH;
    472      1.22  macallan 	case WSDISPLAYIO_GET_EDID: {
    473      1.22  macallan 		struct wsdisplayio_edid_info *d = data;
    474      1.22  macallan 		return wsdisplayio_get_edid(sc->sc_dev, d);
    475      1.22  macallan 	}
    476       1.1  macallan 	}
    477       1.1  macallan 	return EPASSTHROUGH;
    478       1.1  macallan }
    479       1.1  macallan 
    480       1.1  macallan static paddr_t
    481       1.1  macallan r128fb_mmap(void *v, void *vs, off_t offset, int prot)
    482       1.1  macallan {
    483       1.1  macallan 	struct vcons_data *vd = v;
    484       1.1  macallan 	struct r128fb_softc *sc = vd->cookie;
    485       1.1  macallan 	paddr_t pa;
    486       1.1  macallan 
    487       1.1  macallan 	/* 'regular' framebuffer mmap()ing */
    488       1.1  macallan 	if (offset < sc->sc_fbsize) {
    489       1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    490       1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    491       1.1  macallan 		return pa;
    492       1.1  macallan 	}
    493       1.1  macallan 
    494       1.1  macallan 	/*
    495       1.1  macallan 	 * restrict all other mappings to processes with superuser privileges
    496       1.1  macallan 	 * or the kernel itself
    497       1.1  macallan 	 */
    498       1.8      elad 	if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
    499       1.8      elad 	    NULL) != 0) {
    500       1.8      elad 		aprint_normal("%s: mmap() rejected.\n",
    501       1.8      elad 		    device_xname(sc->sc_dev));
    502       1.8      elad 		return -1;
    503       1.1  macallan 	}
    504       1.1  macallan 
    505       1.1  macallan 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    506       1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    507       1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    508       1.1  macallan 		return pa;
    509       1.1  macallan 	}
    510       1.1  macallan 
    511       1.1  macallan 	if ((offset >= sc->sc_reg) &&
    512       1.1  macallan 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    513       1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    514       1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    515       1.1  macallan 		return pa;
    516       1.1  macallan 	}
    517       1.1  macallan 
    518       1.3  macallan #ifdef PCI_MAGIC_IO_RANGE
    519       1.1  macallan 	/* allow mapping of IO space */
    520       1.3  macallan 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    521       1.3  macallan 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    522       1.3  macallan 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    523       1.3  macallan 		    0, prot, BUS_SPACE_MAP_LINEAR);
    524       1.1  macallan 		return pa;
    525       1.1  macallan 	}
    526       1.1  macallan #endif
    527       1.1  macallan 
    528       1.1  macallan #ifdef OFB_ALLOW_OTHERS
    529       1.1  macallan 	if (offset >= 0x80000000) {
    530       1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    531       1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    532       1.1  macallan 		return pa;
    533       1.1  macallan 	}
    534       1.1  macallan #endif
    535       1.1  macallan 	return -1;
    536       1.1  macallan }
    537       1.1  macallan 
    538       1.1  macallan static void
    539       1.1  macallan r128fb_init_screen(void *cookie, struct vcons_screen *scr,
    540       1.1  macallan     int existing, long *defattr)
    541       1.1  macallan {
    542       1.1  macallan 	struct r128fb_softc *sc = cookie;
    543       1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    544       1.1  macallan 
    545       1.1  macallan 	ri->ri_depth = sc->sc_depth;
    546       1.1  macallan 	ri->ri_width = sc->sc_width;
    547       1.1  macallan 	ri->ri_height = sc->sc_height;
    548       1.1  macallan 	ri->ri_stride = sc->sc_stride;
    549      1.13  macallan 	ri->ri_flg = RI_CENTER;
    550  1.22.6.1       mrg 	if (sc->sc_depth == 8)
    551  1.22.6.1       mrg 		ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
    552       1.1  macallan 
    553  1.22.6.1       mrg 	rasops_init(ri, 0, 0);
    554       1.1  macallan 	ri->ri_caps = WSSCREEN_WSCOLORS;
    555      1.22  macallan #ifdef VCONS_DRAW_INTR
    556      1.22  macallan 	scr->scr_flags |= VCONS_DONT_READ;
    557      1.22  macallan #endif
    558       1.1  macallan 
    559       1.1  macallan 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    560       1.1  macallan 		    sc->sc_width / ri->ri_font->fontwidth);
    561       1.1  macallan 
    562       1.1  macallan 	ri->ri_hw = scr;
    563       1.1  macallan 	ri->ri_ops.copyrows = r128fb_copyrows;
    564       1.1  macallan 	ri->ri_ops.copycols = r128fb_copycols;
    565       1.1  macallan 	ri->ri_ops.eraserows = r128fb_eraserows;
    566       1.1  macallan 	ri->ri_ops.erasecols = r128fb_erasecols;
    567       1.1  macallan 	ri->ri_ops.cursor = r128fb_cursor;
    568  1.22.6.1       mrg 	if (FONT_IS_ALPHA(ri->ri_font)) {
    569  1.22.6.1       mrg 		ri->ri_ops.putchar = r128fb_putchar_aa;
    570  1.22.6.1       mrg 		ri->ri_ops.allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG,
    571  1.22.6.1       mrg 		     0, &sc->sc_gc.gc_attr);
    572  1.22.6.1       mrg 	} else
    573  1.22.6.1       mrg 		ri->ri_ops.putchar = r128fb_putchar;
    574       1.1  macallan }
    575       1.1  macallan 
    576       1.1  macallan static int
    577       1.1  macallan r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
    578       1.1  macallan {
    579       1.1  macallan 	u_char *r, *g, *b;
    580       1.1  macallan 	u_int index = cm->index;
    581       1.1  macallan 	u_int count = cm->count;
    582       1.1  macallan 	int i, error;
    583       1.1  macallan 	u_char rbuf[256], gbuf[256], bbuf[256];
    584       1.1  macallan 
    585       1.1  macallan #ifdef R128FB_DEBUG
    586       1.1  macallan 	aprint_debug("putcmap: %d %d\n",index, count);
    587       1.1  macallan #endif
    588       1.1  macallan 	if (cm->index >= 256 || cm->count > 256 ||
    589       1.1  macallan 	    (cm->index + cm->count) > 256)
    590       1.1  macallan 		return EINVAL;
    591       1.1  macallan 	error = copyin(cm->red, &rbuf[index], count);
    592       1.1  macallan 	if (error)
    593       1.1  macallan 		return error;
    594       1.1  macallan 	error = copyin(cm->green, &gbuf[index], count);
    595       1.1  macallan 	if (error)
    596       1.1  macallan 		return error;
    597       1.1  macallan 	error = copyin(cm->blue, &bbuf[index], count);
    598       1.1  macallan 	if (error)
    599       1.1  macallan 		return error;
    600       1.1  macallan 
    601       1.1  macallan 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    602       1.1  macallan 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    603       1.1  macallan 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    604       1.1  macallan 
    605       1.1  macallan 	r = &sc->sc_cmap_red[index];
    606       1.1  macallan 	g = &sc->sc_cmap_green[index];
    607       1.1  macallan 	b = &sc->sc_cmap_blue[index];
    608       1.1  macallan 
    609       1.1  macallan 	for (i = 0; i < count; i++) {
    610       1.1  macallan 		r128fb_putpalreg(sc, index, *r, *g, *b);
    611       1.1  macallan 		index++;
    612       1.1  macallan 		r++, g++, b++;
    613       1.1  macallan 	}
    614       1.1  macallan 	return 0;
    615       1.1  macallan }
    616       1.1  macallan 
    617       1.1  macallan static int
    618       1.1  macallan r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
    619       1.1  macallan {
    620       1.1  macallan 	u_int index = cm->index;
    621       1.1  macallan 	u_int count = cm->count;
    622       1.1  macallan 	int error;
    623       1.1  macallan 
    624       1.1  macallan 	if (index >= 255 || count > 256 || index + count > 256)
    625       1.1  macallan 		return EINVAL;
    626       1.1  macallan 
    627       1.1  macallan 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    628       1.1  macallan 	if (error)
    629       1.1  macallan 		return error;
    630       1.1  macallan 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    631       1.1  macallan 	if (error)
    632       1.1  macallan 		return error;
    633       1.1  macallan 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    634       1.1  macallan 	if (error)
    635       1.1  macallan 		return error;
    636       1.1  macallan 
    637       1.1  macallan 	return 0;
    638       1.1  macallan }
    639       1.1  macallan 
    640       1.1  macallan static void
    641       1.1  macallan r128fb_restore_palette(struct r128fb_softc *sc)
    642       1.1  macallan {
    643       1.1  macallan 	int i;
    644       1.1  macallan 
    645       1.1  macallan 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    646       1.1  macallan 		r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
    647       1.1  macallan 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    648       1.1  macallan 	}
    649       1.1  macallan }
    650       1.1  macallan 
    651       1.1  macallan static int
    652       1.1  macallan r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    653       1.1  macallan     uint8_t b)
    654       1.1  macallan {
    655       1.1  macallan 	uint32_t reg;
    656       1.1  macallan 
    657       1.1  macallan 	/* whack the DAC */
    658       1.1  macallan 	reg = (r << 16) | (g << 8) | b;
    659       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx);
    660       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg);
    661       1.1  macallan 	return 0;
    662       1.1  macallan }
    663       1.1  macallan 
    664       1.1  macallan static void
    665       1.1  macallan r128fb_init(struct r128fb_softc *sc)
    666       1.1  macallan {
    667  1.22.6.1       mrg 	uint32_t datatype, d, reg;
    668       1.1  macallan 
    669       1.1  macallan 	r128fb_flush_engine(sc);
    670       1.1  macallan 
    671      1.13  macallan 	r128fb_wait(sc, 9);
    672       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0);
    673       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0);
    674  1.22.6.1       mrg 	/* pitch is in units of 8 pixels */
    675       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH,
    676  1.22.6.1       mrg 	    sc->sc_width >> 3);
    677       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0);
    678       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    679       1.1  macallan 	    R128_DEFAULT_SC_BOTTOM_RIGHT,
    680       1.1  macallan 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
    681       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0);
    682       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT,
    683       1.1  macallan 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
    684      1.13  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    685      1.13  macallan 	    R128_DEFAULT_SC_BOTTOM_RIGHT,
    686      1.13  macallan 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
    687      1.13  macallan 
    688  1.22.6.1       mrg #if 0
    689      1.13  macallan #if BYTE_ORDER == BIG_ENDIAN
    690       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE,
    691       1.1  macallan 	    R128_HOST_BIG_ENDIAN_EN);
    692      1.13  macallan #else
    693      1.18  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE, 0);
    694      1.13  macallan #endif
    695  1.22.6.1       mrg #endif
    696  1.22.6.1       mrg 	r128fb_wait(sc, 7);
    697       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH,
    698  1.22.6.1       mrg 	    sc->sc_width >> 3);
    699       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH,
    700  1.22.6.1       mrg 	    sc->sc_width >> 3);
    701       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0);
    702       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0);
    703       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK,
    704       1.1  macallan 	    0xffffffff);
    705       1.1  macallan 
    706       1.1  macallan 	switch (sc->sc_depth) {
    707       1.1  macallan 		case 8:
    708       1.1  macallan 			datatype = R128_GMC_DST_8BPP_CI;
    709  1.22.6.1       mrg 			d = R128_CRTC_COLOR_8BIT;
    710       1.1  macallan 			break;
    711       1.1  macallan 		case 15:
    712       1.1  macallan 			datatype = R128_GMC_DST_15BPP;
    713  1.22.6.1       mrg 			d = R128_CRTC_COLOR_15BIT;
    714       1.1  macallan 			break;
    715       1.1  macallan 		case 16:
    716       1.1  macallan 			datatype = R128_GMC_DST_16BPP;
    717  1.22.6.1       mrg 			d = R128_CRTC_COLOR_16BIT;
    718       1.1  macallan 			break;
    719       1.1  macallan 		case 24:
    720       1.1  macallan 			datatype = R128_GMC_DST_24BPP;
    721  1.22.6.1       mrg 			d = R128_CRTC_COLOR_24BIT;
    722       1.1  macallan 			break;
    723       1.1  macallan 		case 32:
    724       1.1  macallan 			datatype = R128_GMC_DST_32BPP;
    725  1.22.6.1       mrg 			d = R128_CRTC_COLOR_32BIT;
    726       1.1  macallan 			break;
    727       1.1  macallan 		default:
    728       1.1  macallan 			aprint_error("%s: unsupported depth %d\n",
    729       1.1  macallan 			    device_xname(sc->sc_dev), sc->sc_depth);
    730       1.1  macallan 			return;
    731       1.1  macallan 	}
    732       1.1  macallan 	sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS |
    733       1.1  macallan 	    R128_GMC_AUX_CLIP_DIS | datatype;
    734  1.22.6.1       mrg 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_CRTC_GEN_CNTL);
    735  1.22.6.1       mrg 	DPRINTF("depth: %d\n", reg & R128_CRTC_PIX_WIDTH);
    736  1.22.6.1       mrg 	reg &= ~R128_CRTC_PIX_WIDTH;
    737  1.22.6.1       mrg 	reg |= d;
    738  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_GEN_CNTL, reg);
    739  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_PITCH, sc->sc_width >> 3);
    740       1.1  macallan 	r128fb_flush_engine(sc);
    741       1.1  macallan }
    742       1.1  macallan 
    743       1.1  macallan static void
    744       1.1  macallan r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he,
    745       1.1  macallan      uint32_t colour)
    746       1.1  macallan {
    747       1.1  macallan 
    748      1.13  macallan 	r128fb_wait(sc, 5);
    749       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
    750       1.1  macallan 	    R128_GMC_BRUSH_SOLID_COLOR |
    751       1.1  macallan 	    R128_GMC_SRC_DATATYPE_COLOR |
    752       1.1  macallan 	    R128_ROP3_P |
    753       1.1  macallan 	    sc->sc_master_cntl);
    754       1.1  macallan 
    755       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR,
    756       1.1  macallan 	    colour);
    757       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL,
    758       1.1  macallan 	    R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
    759       1.1  macallan 	/* now feed it coordinates */
    760       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
    761       1.1  macallan 	    (x << 16) | y);
    762       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
    763       1.1  macallan 	    (wi << 16) | he);
    764       1.1  macallan }
    765       1.1  macallan 
    766       1.1  macallan static void
    767  1.22.6.1       mrg r128fb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
    768       1.1  macallan     int wi, int he, int rop)
    769       1.1  macallan {
    770  1.22.6.1       mrg 	struct r128fb_softc *sc = cookie;
    771       1.1  macallan 	uint32_t dp_cntl = 0;
    772       1.1  macallan 
    773       1.1  macallan 	r128fb_wait(sc, 5);
    774       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
    775       1.1  macallan 	    R128_GMC_BRUSH_SOLID_COLOR |
    776       1.1  macallan 	    R128_GMC_SRC_DATATYPE_COLOR |
    777       1.1  macallan 	    rop |
    778       1.1  macallan 	    R128_DP_SRC_SOURCE_MEMORY |
    779       1.1  macallan 	    sc->sc_master_cntl);
    780       1.1  macallan 
    781       1.1  macallan 	if (yd <= ys) {
    782       1.1  macallan 		dp_cntl = R128_DST_Y_TOP_TO_BOTTOM;
    783       1.1  macallan 	} else {
    784       1.1  macallan 		ys += he - 1;
    785       1.1  macallan 		yd += he - 1;
    786       1.1  macallan 	}
    787       1.1  macallan 	if (xd <= xs) {
    788       1.1  macallan 		dp_cntl |= R128_DST_X_LEFT_TO_RIGHT;
    789       1.1  macallan 	} else {
    790       1.1  macallan 		xs += wi - 1;
    791       1.1  macallan 		xd += wi - 1;
    792       1.1  macallan 	}
    793       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl);
    794       1.1  macallan 
    795       1.1  macallan 	/* now feed it coordinates */
    796       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y,
    797       1.1  macallan 	    (xs << 16) | ys);
    798       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
    799       1.1  macallan 	    (xd << 16) | yd);
    800       1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
    801       1.1  macallan 	    (wi << 16) | he);
    802       1.1  macallan }
    803       1.1  macallan 
    804       1.1  macallan static void
    805       1.1  macallan r128fb_cursor(void *cookie, int on, int row, int col)
    806       1.1  macallan {
    807       1.1  macallan 	struct rasops_info *ri = cookie;
    808       1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    809       1.1  macallan 	struct r128fb_softc *sc = scr->scr_cookie;
    810       1.1  macallan 	int x, y, wi, he;
    811       1.1  macallan 
    812       1.1  macallan 	wi = ri->ri_font->fontwidth;
    813       1.1  macallan 	he = ri->ri_font->fontheight;
    814       1.1  macallan 
    815       1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    816       1.1  macallan 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    817       1.1  macallan 		y = ri->ri_crow * he + ri->ri_yorigin;
    818       1.1  macallan 		if (ri->ri_flg & RI_CURSOR) {
    819       1.1  macallan 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
    820       1.1  macallan 			ri->ri_flg &= ~RI_CURSOR;
    821       1.1  macallan 		}
    822       1.1  macallan 		ri->ri_crow = row;
    823       1.1  macallan 		ri->ri_ccol = col;
    824       1.1  macallan 		if (on) {
    825       1.1  macallan 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    826       1.1  macallan 			y = ri->ri_crow * he + ri->ri_yorigin;
    827       1.1  macallan 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
    828       1.7      yamt 			ri->ri_flg |= RI_CURSOR;
    829       1.1  macallan 		}
    830       1.1  macallan 	} else {
    831       1.1  macallan 		scr->scr_ri.ri_crow = row;
    832       1.1  macallan 		scr->scr_ri.ri_ccol = col;
    833       1.1  macallan 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    834       1.1  macallan 	}
    835       1.1  macallan 
    836       1.1  macallan }
    837       1.1  macallan 
    838       1.1  macallan static void
    839       1.1  macallan r128fb_putchar(void *cookie, int row, int col, u_int c, long attr)
    840       1.1  macallan {
    841      1.13  macallan 	struct rasops_info *ri = cookie;
    842      1.13  macallan 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    843      1.13  macallan 	struct vcons_screen *scr = ri->ri_hw;
    844      1.13  macallan 	struct r128fb_softc *sc = scr->scr_cookie;
    845  1.22.6.1       mrg 	void *data;
    846  1.22.6.1       mrg 	uint32_t fg, bg;
    847  1.22.6.1       mrg 	int i, x, y, wi, he, offset;
    848      1.13  macallan 
    849  1.22.6.1       mrg 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
    850  1.22.6.1       mrg 		return;
    851      1.13  macallan 
    852  1.22.6.1       mrg 	if (!CHAR_IN_FONT(c, font))
    853  1.22.6.1       mrg 		return;
    854      1.13  macallan 
    855  1.22.6.1       mrg 	wi = font->fontwidth;
    856  1.22.6.1       mrg 	he = font->fontheight;
    857      1.13  macallan 
    858  1.22.6.1       mrg 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    859  1.22.6.1       mrg 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
    860  1.22.6.1       mrg 	x = ri->ri_xorigin + col * wi;
    861  1.22.6.1       mrg 	y = ri->ri_yorigin + row * he;
    862      1.13  macallan 
    863  1.22.6.1       mrg 	if (c == 0x20) {
    864  1.22.6.1       mrg 		r128fb_rectfill(sc, x, y, wi, he, bg);
    865  1.22.6.1       mrg 		return;
    866  1.22.6.1       mrg 	}
    867      1.13  macallan 
    868  1.22.6.1       mrg 	data = WSFONT_GLYPH(c, font);
    869  1.22.6.1       mrg 
    870  1.22.6.1       mrg 	r128fb_wait(sc, 8);
    871  1.22.6.1       mrg 
    872  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    873  1.22.6.1       mrg 	    R128_DP_GUI_MASTER_CNTL,
    874  1.22.6.1       mrg 	    R128_GMC_BRUSH_SOLID_COLOR |
    875  1.22.6.1       mrg 	    R128_GMC_SRC_DATATYPE_MONO_FG_BG |
    876  1.22.6.1       mrg 	    R128_ROP3_S |
    877  1.22.6.1       mrg 	    R128_DP_SRC_SOURCE_HOST_DATA |
    878  1.22.6.1       mrg 	    R128_GMC_DST_CLIPPING |
    879  1.22.6.1       mrg 	    sc->sc_master_cntl);
    880  1.22.6.1       mrg 
    881  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    882  1.22.6.1       mrg 	    R128_DP_CNTL,
    883  1.22.6.1       mrg 	    R128_DST_Y_TOP_TO_BOTTOM |
    884  1.22.6.1       mrg 	    R128_DST_X_LEFT_TO_RIGHT);
    885  1.22.6.1       mrg 
    886  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    887  1.22.6.1       mrg 	    R128_DP_SRC_FRGD_CLR, fg);
    888  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    889  1.22.6.1       mrg 	    R128_DP_SRC_BKGD_CLR, bg);
    890  1.22.6.1       mrg 
    891  1.22.6.1       mrg 	/*
    892  1.22.6.1       mrg 	 * The Rage 128 doesn't have anything to skip pixels
    893  1.22.6.1       mrg 	 * when colour expanding but all coordinates
    894  1.22.6.1       mrg 	 * are signed so we just clip the leading bytes and
    895  1.22.6.1       mrg 	 * trailing bits away
    896  1.22.6.1       mrg 	 */
    897  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    898  1.22.6.1       mrg 	    R128_SC_RIGHT, x + wi - 1);
    899  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    900  1.22.6.1       mrg 	    R128_SC_LEFT, x);
    901  1.22.6.1       mrg 
    902  1.22.6.1       mrg 	/* needed? */
    903  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0);
    904  1.22.6.1       mrg 
    905  1.22.6.1       mrg 	offset = 32 - (font->stride << 3);
    906  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
    907  1.22.6.1       mrg 	    ((x - offset) << 16) | y);
    908  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    909  1.22.6.1       mrg 	    R128_DST_WIDTH_HEIGHT, (32 << 16) | he);
    910  1.22.6.1       mrg 
    911  1.22.6.1       mrg 	r128fb_wait(sc, he);
    912  1.22.6.1       mrg 	switch (font->stride) {
    913  1.22.6.1       mrg 		case 1: {
    914  1.22.6.1       mrg 			uint8_t *data8 = data;
    915  1.22.6.1       mrg 			uint32_t reg;
    916  1.22.6.1       mrg 			for (i = 0; i < he; i++) {
    917  1.22.6.1       mrg 				reg = *data8;
    918  1.22.6.1       mrg 				bus_space_write_stream_4(sc->sc_memt,
    919  1.22.6.1       mrg 				    sc->sc_regh, R128_HOST_DATA0, reg);
    920  1.22.6.1       mrg 				data8++;
    921      1.13  macallan 			}
    922  1.22.6.1       mrg 			break;
    923  1.22.6.1       mrg 		}
    924  1.22.6.1       mrg 		case 2: {
    925  1.22.6.1       mrg 			uint16_t *data16 = data;
    926  1.22.6.1       mrg 			uint32_t reg;
    927  1.22.6.1       mrg 			for (i = 0; i < he; i++) {
    928  1.22.6.1       mrg 				reg = *data16;
    929  1.22.6.1       mrg 				bus_space_write_stream_4(sc->sc_memt,
    930  1.22.6.1       mrg 				    sc->sc_regh, R128_HOST_DATA0, reg);
    931  1.22.6.1       mrg 				data16++;
    932      1.13  macallan 			}
    933  1.22.6.1       mrg 			break;
    934      1.13  macallan 		}
    935      1.13  macallan 	}
    936       1.1  macallan }
    937       1.1  macallan 
    938       1.1  macallan static void
    939  1.22.6.1       mrg r128fb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
    940  1.22.6.1       mrg {
    941  1.22.6.1       mrg 	struct rasops_info *ri = cookie;
    942  1.22.6.1       mrg 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    943  1.22.6.1       mrg 	struct vcons_screen *scr = ri->ri_hw;
    944  1.22.6.1       mrg 	struct r128fb_softc *sc = scr->scr_cookie;
    945  1.22.6.1       mrg 	uint32_t bg, latch = 0, bg8, fg8, pixel;
    946  1.22.6.1       mrg 	int i, x, y, wi, he, r, g, b, aval;
    947  1.22.6.1       mrg 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
    948  1.22.6.1       mrg 	uint8_t *data8;
    949  1.22.6.2       mrg 	int rv, cnt = 0;
    950  1.22.6.1       mrg 
    951  1.22.6.1       mrg 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
    952  1.22.6.1       mrg 		return;
    953  1.22.6.1       mrg 
    954  1.22.6.1       mrg 	if (!CHAR_IN_FONT(c, font))
    955  1.22.6.1       mrg 		return;
    956  1.22.6.1       mrg 
    957  1.22.6.1       mrg 	wi = font->fontwidth;
    958  1.22.6.1       mrg 	he = font->fontheight;
    959  1.22.6.1       mrg 
    960  1.22.6.1       mrg 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    961  1.22.6.1       mrg 	x = ri->ri_xorigin + col * wi;
    962  1.22.6.1       mrg 	y = ri->ri_yorigin + row * he;
    963  1.22.6.1       mrg 	if (c == 0x20) {
    964  1.22.6.1       mrg 		r128fb_rectfill(sc, x, y, wi, he, bg);
    965  1.22.6.1       mrg 		return;
    966  1.22.6.1       mrg 	}
    967  1.22.6.1       mrg 
    968  1.22.6.1       mrg 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
    969  1.22.6.1       mrg 	if (rv == GC_OK)
    970  1.22.6.1       mrg 		return;
    971  1.22.6.1       mrg 
    972  1.22.6.1       mrg 	data8 = WSFONT_GLYPH(c, font);
    973  1.22.6.1       mrg 
    974  1.22.6.1       mrg 	r128fb_wait(sc, 5);
    975  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    976  1.22.6.1       mrg 	    R128_DP_GUI_MASTER_CNTL,
    977  1.22.6.1       mrg 	    R128_GMC_BRUSH_SOLID_COLOR |
    978  1.22.6.1       mrg 	    R128_GMC_SRC_DATATYPE_COLOR |
    979  1.22.6.1       mrg 	    R128_ROP3_S |
    980  1.22.6.1       mrg 	    R128_DP_SRC_SOURCE_HOST_DATA |
    981  1.22.6.1       mrg 	    sc->sc_master_cntl);
    982  1.22.6.1       mrg 
    983  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    984  1.22.6.1       mrg 	    R128_DP_CNTL,
    985  1.22.6.1       mrg 	    R128_DST_Y_TOP_TO_BOTTOM |
    986  1.22.6.1       mrg 	    R128_DST_X_LEFT_TO_RIGHT);
    987  1.22.6.1       mrg 
    988  1.22.6.1       mrg 	/* needed? */
    989  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0);
    990  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
    991  1.22.6.1       mrg 	    (x << 16) | y);
    992  1.22.6.1       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
    993  1.22.6.1       mrg 	    (wi << 16) | he);
    994  1.22.6.1       mrg 
    995  1.22.6.1       mrg 	/*
    996  1.22.6.1       mrg 	 * we need the RGB colours here, so get offsets into rasops_cmap
    997  1.22.6.1       mrg 	 */
    998  1.22.6.1       mrg 	fgo = ((attr >> 24) & 0xf) * 3;
    999  1.22.6.1       mrg 	bgo = ((attr >> 16) & 0xf) * 3;
   1000  1.22.6.1       mrg 
   1001  1.22.6.1       mrg 	r0 = rasops_cmap[bgo];
   1002  1.22.6.1       mrg 	r1 = rasops_cmap[fgo];
   1003  1.22.6.1       mrg 	g0 = rasops_cmap[bgo + 1];
   1004  1.22.6.1       mrg 	g1 = rasops_cmap[fgo + 1];
   1005  1.22.6.1       mrg 	b0 = rasops_cmap[bgo + 2];
   1006  1.22.6.1       mrg 	b1 = rasops_cmap[fgo + 2];
   1007  1.22.6.1       mrg #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
   1008  1.22.6.1       mrg 	bg8 = R3G3B2(r0, g0, b0);
   1009  1.22.6.1       mrg 	fg8 = R3G3B2(r1, g1, b1);
   1010  1.22.6.2       mrg 
   1011  1.22.6.2       mrg 	r128fb_wait(sc, 16);
   1012  1.22.6.2       mrg 
   1013  1.22.6.1       mrg 	for (i = 0; i < ri->ri_fontscale; i++) {
   1014  1.22.6.1       mrg 		aval = *data8;
   1015  1.22.6.1       mrg 		if (aval == 0) {
   1016  1.22.6.1       mrg 			pixel = bg8;
   1017  1.22.6.1       mrg 		} else if (aval == 255) {
   1018  1.22.6.1       mrg 			pixel = fg8;
   1019  1.22.6.1       mrg 		} else {
   1020  1.22.6.1       mrg 			r = aval * r1 + (255 - aval) * r0;
   1021  1.22.6.1       mrg 			g = aval * g1 + (255 - aval) * g0;
   1022  1.22.6.1       mrg 			b = aval * b1 + (255 - aval) * b0;
   1023  1.22.6.1       mrg 			pixel = ((r & 0xe000) >> 8) |
   1024  1.22.6.1       mrg 				((g & 0xe000) >> 11) |
   1025  1.22.6.1       mrg 				((b & 0xc000) >> 14);
   1026  1.22.6.1       mrg 		}
   1027  1.22.6.1       mrg 		latch = (latch << 8) | pixel;
   1028  1.22.6.1       mrg 		/* write in 32bit chunks */
   1029  1.22.6.1       mrg 		if ((i & 3) == 3) {
   1030  1.22.6.1       mrg 			bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
   1031  1.22.6.1       mrg 			    R128_HOST_DATA0, latch);
   1032  1.22.6.1       mrg 			/*
   1033  1.22.6.1       mrg 			 * not strictly necessary, old data should be shifted
   1034  1.22.6.1       mrg 			 * out
   1035  1.22.6.1       mrg 			 */
   1036  1.22.6.1       mrg 			latch = 0;
   1037  1.22.6.2       mrg 			cnt++;
   1038  1.22.6.2       mrg 			if (cnt > 15) {
   1039  1.22.6.2       mrg 				r128fb_wait(sc, 16);
   1040  1.22.6.2       mrg 				cnt = 0;
   1041  1.22.6.2       mrg 			}
   1042  1.22.6.1       mrg 		}
   1043  1.22.6.1       mrg 		data8++;
   1044  1.22.6.1       mrg 	}
   1045  1.22.6.1       mrg 	/* if we have pixels left in latch write them out */
   1046  1.22.6.1       mrg 	if ((i & 3) != 0) {
   1047  1.22.6.1       mrg 		latch = latch << ((4 - (i & 3)) << 3);
   1048  1.22.6.1       mrg 		bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
   1049  1.22.6.1       mrg 				    R128_HOST_DATA0, latch);
   1050  1.22.6.1       mrg 	}
   1051  1.22.6.1       mrg 	if (rv == GC_ADD) {
   1052  1.22.6.1       mrg 		glyphcache_add(&sc->sc_gc, c, x, y);
   1053  1.22.6.1       mrg 	}
   1054  1.22.6.1       mrg }
   1055  1.22.6.1       mrg 
   1056  1.22.6.1       mrg static void
   1057       1.1  macallan r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1058       1.1  macallan {
   1059       1.1  macallan 	struct rasops_info *ri = cookie;
   1060       1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1061       1.1  macallan 	struct r128fb_softc *sc = scr->scr_cookie;
   1062       1.1  macallan 	int32_t xs, xd, y, width, height;
   1063       1.1  macallan 
   1064       1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1065       1.1  macallan 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1066       1.1  macallan 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1067       1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1068       1.1  macallan 		width = ri->ri_font->fontwidth * ncols;
   1069       1.1  macallan 		height = ri->ri_font->fontheight;
   1070       1.1  macallan 		r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S);
   1071       1.1  macallan 	}
   1072       1.1  macallan }
   1073       1.1  macallan 
   1074       1.1  macallan static void
   1075       1.1  macallan r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1076       1.1  macallan {
   1077       1.1  macallan 	struct rasops_info *ri = cookie;
   1078       1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1079       1.1  macallan 	struct r128fb_softc *sc = scr->scr_cookie;
   1080       1.1  macallan 	int32_t x, y, width, height, fg, bg, ul;
   1081       1.1  macallan 
   1082       1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1083       1.1  macallan 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1084       1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1085       1.1  macallan 		width = ri->ri_font->fontwidth * ncols;
   1086       1.1  macallan 		height = ri->ri_font->fontheight;
   1087       1.1  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1088       1.1  macallan 
   1089       1.1  macallan 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1090       1.1  macallan 	}
   1091       1.1  macallan }
   1092       1.1  macallan 
   1093       1.1  macallan static void
   1094       1.1  macallan r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1095       1.1  macallan {
   1096       1.1  macallan 	struct rasops_info *ri = cookie;
   1097       1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1098       1.1  macallan 	struct r128fb_softc *sc = scr->scr_cookie;
   1099       1.1  macallan 	int32_t x, ys, yd, width, height;
   1100       1.1  macallan 
   1101       1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1102       1.1  macallan 		x = ri->ri_xorigin;
   1103       1.1  macallan 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1104       1.1  macallan 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1105       1.1  macallan 		width = ri->ri_emuwidth;
   1106      1.13  macallan 		height = ri->ri_font->fontheight * nrows;
   1107       1.1  macallan 		r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S);
   1108       1.1  macallan 	}
   1109       1.1  macallan }
   1110       1.1  macallan 
   1111       1.1  macallan static void
   1112       1.1  macallan r128fb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1113       1.1  macallan {
   1114       1.1  macallan 	struct rasops_info *ri = cookie;
   1115       1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1116       1.1  macallan 	struct r128fb_softc *sc = scr->scr_cookie;
   1117       1.1  macallan 	int32_t x, y, width, height, fg, bg, ul;
   1118       1.1  macallan 
   1119       1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1120       1.1  macallan 		x = ri->ri_xorigin;
   1121       1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1122       1.1  macallan 		width = ri->ri_emuwidth;
   1123       1.1  macallan 		height = ri->ri_font->fontheight * nrows;
   1124       1.1  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1125       1.1  macallan 
   1126       1.1  macallan 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1127       1.1  macallan 	}
   1128       1.1  macallan }
   1129       1.1  macallan 
   1130      1.11  macallan static void
   1131      1.11  macallan r128fb_set_backlight(struct r128fb_softc *sc, int level)
   1132      1.11  macallan {
   1133      1.11  macallan 	uint32_t reg;
   1134      1.11  macallan 
   1135      1.14  macallan 	/*
   1136      1.14  macallan 	 * should we do nothing when backlight is off, should we just store the
   1137      1.14  macallan 	 * level and use it when turning back on or should we just flip sc_bl_on
   1138      1.14  macallan 	 * and turn the backlight on?
   1139      1.14  macallan 	 * For now turn it on so a crashed screensaver can't get the user stuck
   1140      1.14  macallan 	 * with a dark screen as long as hotkeys work
   1141      1.14  macallan 	 */
   1142      1.11  macallan 	if (level > 255) level = 255;
   1143      1.11  macallan 	if (level < 0) level = 0;
   1144      1.11  macallan 	if (level == sc->sc_bl_level)
   1145      1.11  macallan 		return;
   1146      1.11  macallan 	sc->sc_bl_level = level;
   1147      1.14  macallan 	if (sc->sc_bl_on == 0)
   1148      1.14  macallan 		sc->sc_bl_on = 1;
   1149      1.11  macallan 	level = 255 - level;
   1150      1.11  macallan 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
   1151      1.11  macallan 	reg &= ~R128_LEVEL_MASK;
   1152      1.21  macallan 	reg |= (level << R128_LEVEL_SHIFT);
   1153      1.11  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
   1154      1.14  macallan 	DPRINTF("backlight level: %d reg %08x\n", level, reg);
   1155      1.14  macallan }
   1156      1.14  macallan 
   1157      1.14  macallan static void
   1158      1.14  macallan r128fb_switch_backlight(struct r128fb_softc *sc, int on)
   1159      1.14  macallan {
   1160      1.14  macallan 	uint32_t reg;
   1161      1.14  macallan 	int level;
   1162      1.14  macallan 
   1163      1.14  macallan 	if (on == sc->sc_bl_on)
   1164      1.14  macallan 		return;
   1165      1.14  macallan 	sc->sc_bl_on = on;
   1166      1.14  macallan 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
   1167      1.14  macallan 	reg &= ~R128_LEVEL_MASK;
   1168      1.14  macallan 	level = on ? 255 - sc->sc_bl_level : 255;
   1169      1.14  macallan 	reg |= level << R128_LEVEL_SHIFT;
   1170      1.14  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
   1171      1.14  macallan 	DPRINTF("backlight state: %d reg %08x\n", on, reg);
   1172      1.11  macallan }
   1173      1.11  macallan 
   1174      1.11  macallan 
   1175      1.11  macallan static void
   1176      1.11  macallan r128fb_brightness_up(device_t dev)
   1177      1.11  macallan {
   1178      1.11  macallan 	struct r128fb_softc *sc = device_private(dev);
   1179      1.11  macallan 
   1180      1.11  macallan 	r128fb_set_backlight(sc, sc->sc_bl_level + 8);
   1181      1.11  macallan }
   1182      1.11  macallan 
   1183      1.11  macallan static void
   1184      1.11  macallan r128fb_brightness_down(device_t dev)
   1185      1.11  macallan {
   1186      1.11  macallan 	struct r128fb_softc *sc = device_private(dev);
   1187      1.11  macallan 
   1188      1.11  macallan 	r128fb_set_backlight(sc, sc->sc_bl_level - 8);
   1189      1.11  macallan }
   1190