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