Home | History | Annotate | Line # | Download | only in pci
r128fb.c revision 1.3.4.6
      1  1.3.4.6      yamt /*	$NetBSD: r128fb.c,v 1.3.4.6 2010/10/09 03:32:22 yamt 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.3.4.6      yamt __KERNEL_RCSID(0, "$NetBSD: r128fb.c,v 1.3.4.6 2010/10/09 03:32:22 yamt 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 <uvm/uvm_extern.h>
     45      1.1  macallan 
     46      1.1  macallan #include <dev/videomode/videomode.h>
     47      1.1  macallan 
     48      1.1  macallan #include <dev/pci/pcivar.h>
     49      1.1  macallan #include <dev/pci/pcireg.h>
     50      1.1  macallan #include <dev/pci/pcidevs.h>
     51      1.1  macallan #include <dev/pci/pciio.h>
     52      1.1  macallan #include <dev/pci/r128fbreg.h>
     53      1.1  macallan 
     54      1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     55      1.1  macallan #include <dev/wscons/wsconsio.h>
     56      1.1  macallan #include <dev/wsfont/wsfont.h>
     57      1.1  macallan #include <dev/rasops/rasops.h>
     58      1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     59      1.1  macallan 
     60      1.1  macallan #include <dev/i2c/i2cvar.h>
     61      1.1  macallan 
     62  1.3.4.6      yamt #include "opt_r128fb.h"
     63  1.3.4.6      yamt 
     64  1.3.4.6      yamt #ifdef R128FB_DEBUG
     65  1.3.4.6      yamt #define DPRINTF printf
     66  1.3.4.6      yamt #else
     67  1.3.4.6      yamt #define DPRINTF while(0) printf
     68  1.3.4.6      yamt #endif
     69  1.3.4.6      yamt 
     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.3.4.6      yamt 	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.1  macallan static void	r128fb_copycols(void *, int, int, int, int);
    127      1.1  macallan static void	r128fb_erasecols(void *, int, int, int, long);
    128      1.1  macallan static void	r128fb_copyrows(void *, int, int, int);
    129      1.1  macallan static void	r128fb_eraserows(void *, int, int, long);
    130      1.1  macallan 
    131  1.3.4.6      yamt static void	r128fb_brightness_up(device_t);
    132  1.3.4.6      yamt static void	r128fb_brightness_down(device_t);
    133  1.3.4.6      yamt /* set backlight level */
    134  1.3.4.6      yamt static void	r128fb_set_backlight(struct r128fb_softc *, int);
    135  1.3.4.6      yamt /* turn backlight on and off without messing with the level */
    136  1.3.4.6      yamt static void	r128fb_switch_backlight(struct r128fb_softc *, int);
    137  1.3.4.6      yamt 
    138      1.1  macallan struct wsdisplay_accessops r128fb_accessops = {
    139      1.1  macallan 	r128fb_ioctl,
    140      1.1  macallan 	r128fb_mmap,
    141      1.1  macallan 	NULL,	/* alloc_screen */
    142      1.1  macallan 	NULL,	/* free_screen */
    143      1.1  macallan 	NULL,	/* show_screen */
    144      1.1  macallan 	NULL, 	/* load_font */
    145      1.1  macallan 	NULL,	/* pollc */
    146      1.1  macallan 	NULL	/* scroll */
    147      1.1  macallan };
    148      1.1  macallan 
    149      1.1  macallan static inline void
    150      1.1  macallan r128fb_wait(struct r128fb_softc *sc, int slots)
    151      1.1  macallan {
    152      1.1  macallan 	uint32_t reg;
    153      1.1  macallan 
    154      1.1  macallan 	do {
    155      1.1  macallan 		reg = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    156      1.1  macallan 		    R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK);
    157      1.1  macallan 	} while (reg <= slots);
    158      1.1  macallan }
    159      1.1  macallan 
    160      1.1  macallan static void
    161      1.1  macallan r128fb_flush_engine(struct r128fb_softc *sc)
    162      1.1  macallan {
    163      1.1  macallan 	uint32_t reg;
    164      1.1  macallan 
    165      1.1  macallan 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT);
    166      1.1  macallan 	reg |= R128_PC_FLUSH_ALL;
    167      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT, reg);
    168      1.1  macallan 	do {
    169      1.1  macallan 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    170      1.1  macallan 		    R128_PC_NGUI_CTLSTAT);
    171      1.1  macallan 	} while (reg & R128_PC_BUSY);
    172      1.1  macallan }
    173      1.1  macallan 
    174      1.1  macallan static int
    175      1.1  macallan r128fb_match(device_t parent, cfdata_t match, void *aux)
    176      1.1  macallan {
    177      1.1  macallan 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    178      1.1  macallan 
    179  1.3.4.2      yamt 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    180      1.1  macallan 		return 0;
    181      1.1  macallan 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI)
    182      1.1  macallan 		return 0;
    183      1.1  macallan 
    184  1.3.4.2      yamt 	/* only cards tested on so far - likely need a list */
    185  1.3.4.2      yamt 	if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE1AGP4XT) ||
    186  1.3.4.2      yamt 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE3AGP4XT) ||
    187  1.3.4.5      yamt 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGEGLPCI) ||
    188  1.3.4.2      yamt 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP))
    189      1.1  macallan 		return 100;
    190      1.1  macallan 	return (0);
    191      1.1  macallan }
    192      1.1  macallan 
    193      1.1  macallan static void
    194      1.1  macallan r128fb_attach(device_t parent, device_t self, void *aux)
    195      1.1  macallan {
    196      1.1  macallan 	struct r128fb_softc	*sc = device_private(self);
    197      1.1  macallan 	struct pci_attach_args	*pa = aux;
    198      1.1  macallan 	struct rasops_info	*ri;
    199  1.3.4.6      yamt 	bus_space_tag_t		tag;
    200      1.1  macallan 	char devinfo[256];
    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.1  macallan 	int i, j;
    206  1.3.4.6      yamt 	uint32_t reg, flags;
    207      1.1  macallan 
    208      1.1  macallan 	sc->sc_pc = pa->pa_pc;
    209      1.1  macallan 	sc->sc_pcitag = pa->pa_tag;
    210      1.1  macallan 	sc->sc_memt = pa->pa_memt;
    211      1.1  macallan 	sc->sc_iot = pa->pa_iot;
    212      1.1  macallan 	sc->sc_dev = self;
    213      1.1  macallan 
    214      1.1  macallan 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    215      1.1  macallan 	aprint_normal(": %s\n", devinfo);
    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.3.4.6      yamt 	if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x10, PCI_MAPREG_TYPE_MEM,
    240  1.3.4.6      yamt 	    &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.3.4.6      yamt 	    &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.3.4.4      yamt 	j = 0;
    277  1.3.4.4      yamt 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    278  1.3.4.4      yamt 
    279  1.3.4.4      yamt 		sc->sc_cmap_red[i] = rasops_cmap[j];
    280  1.3.4.4      yamt 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    281  1.3.4.4      yamt 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    282  1.3.4.4      yamt 		r128fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    283  1.3.4.4      yamt 		    rasops_cmap[j + 2]);
    284  1.3.4.4      yamt 		j += 3;
    285  1.3.4.4      yamt 	}
    286  1.3.4.4      yamt 
    287      1.1  macallan 	if (is_console) {
    288      1.1  macallan 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    289      1.1  macallan 		    &defattr);
    290      1.1  macallan 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    291      1.1  macallan 
    292  1.3.4.4      yamt 		r128fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    293  1.3.4.4      yamt 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    294      1.1  macallan 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    295      1.1  macallan 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    296      1.1  macallan 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    297      1.1  macallan 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    298      1.1  macallan 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    299      1.1  macallan 		    defattr);
    300  1.3.4.4      yamt 		vcons_replay_msgbuf(&sc->sc_console_screen);
    301      1.1  macallan 	} else {
    302      1.1  macallan 		/*
    303      1.1  macallan 		 * since we're not the console we can postpone the rest
    304      1.1  macallan 		 * until someone actually allocates a screen for us
    305      1.1  macallan 		 */
    306      1.1  macallan 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    307      1.1  macallan 	}
    308      1.1  macallan 
    309  1.3.4.6      yamt 	/* no suspend/resume support yet */
    310  1.3.4.6      yamt 	pmf_device_register(sc->sc_dev, NULL, NULL);
    311  1.3.4.6      yamt 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
    312  1.3.4.6      yamt 	DPRINTF("reg: %08x\n", reg);
    313  1.3.4.6      yamt 	if (reg & R128_LVDS_ON) {
    314  1.3.4.6      yamt 		sc->sc_have_backlight = 1;
    315  1.3.4.6      yamt 		sc->sc_bl_on = 1;
    316  1.3.4.6      yamt 		sc->sc_bl_level = 255 -
    317  1.3.4.6      yamt 		    ((reg & R128_LEVEL_MASK) >> R128_LEVEL_SHIFT);
    318  1.3.4.6      yamt 		pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
    319  1.3.4.6      yamt 		    r128fb_brightness_up, TRUE);
    320  1.3.4.6      yamt 		pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
    321  1.3.4.6      yamt 		    r128fb_brightness_down, TRUE);
    322  1.3.4.6      yamt 		aprint_verbose("%s: LVDS output is active, enabling backlight"
    323  1.3.4.6      yamt 			       " control\n", device_xname(self));
    324  1.3.4.6      yamt 	} else
    325  1.3.4.6      yamt 		sc->sc_have_backlight = 0;
    326  1.3.4.6      yamt 
    327      1.1  macallan 	aa.console = is_console;
    328      1.1  macallan 	aa.scrdata = &sc->sc_screenlist;
    329      1.1  macallan 	aa.accessops = &r128fb_accessops;
    330      1.1  macallan 	aa.accesscookie = &sc->vd;
    331      1.1  macallan 
    332      1.1  macallan 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    333      1.1  macallan }
    334      1.1  macallan 
    335      1.1  macallan static int
    336      1.1  macallan r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    337      1.1  macallan 	struct lwp *l)
    338      1.1  macallan {
    339      1.1  macallan 	struct vcons_data *vd = v;
    340      1.1  macallan 	struct r128fb_softc *sc = vd->cookie;
    341      1.1  macallan 	struct wsdisplay_fbinfo *wdf;
    342      1.1  macallan 	struct vcons_screen *ms = vd->active;
    343  1.3.4.6      yamt 	struct wsdisplay_param  *param;
    344      1.1  macallan 
    345      1.1  macallan 	switch (cmd) {
    346      1.1  macallan 
    347      1.1  macallan 		case WSDISPLAYIO_GTYPE:
    348      1.1  macallan 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    349      1.1  macallan 			return 0;
    350      1.1  macallan 
    351      1.1  macallan 		/* PCI config read/write passthrough. */
    352      1.1  macallan 		case PCI_IOC_CFGREAD:
    353      1.1  macallan 		case PCI_IOC_CFGWRITE:
    354      1.1  macallan 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    355      1.1  macallan 			    cmd, data, flag, l));
    356      1.1  macallan 
    357      1.1  macallan 		case WSDISPLAYIO_GINFO:
    358      1.1  macallan 			if (ms == NULL)
    359      1.1  macallan 				return ENODEV;
    360      1.1  macallan 			wdf = (void *)data;
    361      1.1  macallan 			wdf->height = ms->scr_ri.ri_height;
    362      1.1  macallan 			wdf->width = ms->scr_ri.ri_width;
    363      1.1  macallan 			wdf->depth = ms->scr_ri.ri_depth;
    364      1.1  macallan 			wdf->cmsize = 256;
    365      1.1  macallan 			return 0;
    366      1.1  macallan 
    367      1.1  macallan 		case WSDISPLAYIO_GETCMAP:
    368      1.1  macallan 			return r128fb_getcmap(sc,
    369      1.1  macallan 			    (struct wsdisplay_cmap *)data);
    370      1.1  macallan 
    371      1.1  macallan 		case WSDISPLAYIO_PUTCMAP:
    372      1.1  macallan 			return r128fb_putcmap(sc,
    373      1.1  macallan 			    (struct wsdisplay_cmap *)data);
    374      1.1  macallan 
    375      1.1  macallan 		case WSDISPLAYIO_LINEBYTES:
    376      1.1  macallan 			*(u_int *)data = sc->sc_stride;
    377      1.1  macallan 			return 0;
    378      1.1  macallan 
    379      1.1  macallan 		case WSDISPLAYIO_SMODE:
    380      1.1  macallan 			{
    381      1.1  macallan 				int new_mode = *(int*)data;
    382      1.1  macallan 
    383      1.1  macallan 				if (new_mode != sc->sc_mode) {
    384      1.1  macallan 					sc->sc_mode = new_mode;
    385      1.1  macallan 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    386      1.1  macallan 						r128fb_restore_palette(sc);
    387      1.1  macallan 						vcons_redraw_screen(ms);
    388      1.1  macallan 					}
    389      1.1  macallan 				}
    390      1.1  macallan 			}
    391      1.1  macallan 			return 0;
    392  1.3.4.6      yamt 
    393  1.3.4.6      yamt 		case WSDISPLAYIO_GETPARAM:
    394  1.3.4.6      yamt 			param = (struct wsdisplay_param *)data;
    395  1.3.4.6      yamt 			if (sc->sc_have_backlight == 0)
    396  1.3.4.6      yamt 				return EPASSTHROUGH;
    397  1.3.4.6      yamt 			switch (param->param) {
    398  1.3.4.6      yamt 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
    399  1.3.4.6      yamt 				param->min = 0;
    400  1.3.4.6      yamt 				param->max = 255;
    401  1.3.4.6      yamt 				param->curval = sc->sc_bl_level;
    402  1.3.4.6      yamt 				return 0;
    403  1.3.4.6      yamt 			case WSDISPLAYIO_PARAM_BACKLIGHT:
    404  1.3.4.6      yamt 				param->min = 0;
    405  1.3.4.6      yamt 				param->max = 1;
    406  1.3.4.6      yamt 				param->curval = sc->sc_bl_on;
    407  1.3.4.6      yamt 				return 0;
    408  1.3.4.6      yamt 			}
    409  1.3.4.6      yamt 			return EPASSTHROUGH;
    410  1.3.4.6      yamt 
    411  1.3.4.6      yamt 		case WSDISPLAYIO_SETPARAM:
    412  1.3.4.6      yamt 			param = (struct wsdisplay_param *)data;
    413  1.3.4.6      yamt 			if (sc->sc_have_backlight == 0)
    414  1.3.4.6      yamt 				return EPASSTHROUGH;
    415  1.3.4.6      yamt 			switch (param->param) {
    416  1.3.4.6      yamt 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
    417  1.3.4.6      yamt 				r128fb_set_backlight(sc, param->curval);
    418  1.3.4.6      yamt 				return 0;
    419  1.3.4.6      yamt 			case WSDISPLAYIO_PARAM_BACKLIGHT:
    420  1.3.4.6      yamt 				r128fb_switch_backlight(sc,  param->curval);
    421  1.3.4.6      yamt 				return 0;
    422  1.3.4.6      yamt 			}
    423  1.3.4.6      yamt 			return EPASSTHROUGH;
    424      1.1  macallan 	}
    425      1.1  macallan 	return EPASSTHROUGH;
    426      1.1  macallan }
    427      1.1  macallan 
    428      1.1  macallan static paddr_t
    429      1.1  macallan r128fb_mmap(void *v, void *vs, off_t offset, int prot)
    430      1.1  macallan {
    431      1.1  macallan 	struct vcons_data *vd = v;
    432      1.1  macallan 	struct r128fb_softc *sc = vd->cookie;
    433      1.1  macallan 	paddr_t pa;
    434      1.1  macallan 
    435      1.1  macallan 	/* 'regular' framebuffer mmap()ing */
    436      1.1  macallan 	if (offset < sc->sc_fbsize) {
    437      1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    438      1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    439      1.1  macallan 		return pa;
    440      1.1  macallan 	}
    441      1.1  macallan 
    442      1.1  macallan 	/*
    443      1.1  macallan 	 * restrict all other mappings to processes with superuser privileges
    444      1.1  macallan 	 * or the kernel itself
    445      1.1  macallan 	 */
    446  1.3.4.3      yamt 	if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
    447  1.3.4.3      yamt 	    NULL) != 0) {
    448  1.3.4.3      yamt 		aprint_normal("%s: mmap() rejected.\n",
    449  1.3.4.3      yamt 		    device_xname(sc->sc_dev));
    450  1.3.4.3      yamt 		return -1;
    451      1.1  macallan 	}
    452      1.1  macallan 
    453      1.1  macallan 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    454      1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    455      1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    456      1.1  macallan 		return pa;
    457      1.1  macallan 	}
    458      1.1  macallan 
    459      1.1  macallan 	if ((offset >= sc->sc_reg) &&
    460      1.1  macallan 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    461      1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    462      1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    463      1.1  macallan 		return pa;
    464      1.1  macallan 	}
    465      1.1  macallan 
    466      1.3  macallan #ifdef PCI_MAGIC_IO_RANGE
    467      1.1  macallan 	/* allow mapping of IO space */
    468      1.3  macallan 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    469      1.3  macallan 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    470      1.3  macallan 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    471      1.3  macallan 		    0, prot, BUS_SPACE_MAP_LINEAR);
    472      1.1  macallan 		return pa;
    473      1.1  macallan 	}
    474      1.1  macallan #endif
    475      1.1  macallan 
    476      1.1  macallan #ifdef OFB_ALLOW_OTHERS
    477      1.1  macallan 	if (offset >= 0x80000000) {
    478      1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    479      1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    480      1.1  macallan 		return pa;
    481      1.1  macallan 	}
    482      1.1  macallan #endif
    483      1.1  macallan 	return -1;
    484      1.1  macallan }
    485      1.1  macallan 
    486      1.1  macallan static void
    487      1.1  macallan r128fb_init_screen(void *cookie, struct vcons_screen *scr,
    488      1.1  macallan     int existing, long *defattr)
    489      1.1  macallan {
    490      1.1  macallan 	struct r128fb_softc *sc = cookie;
    491      1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    492      1.1  macallan 
    493      1.1  macallan 	ri->ri_depth = sc->sc_depth;
    494      1.1  macallan 	ri->ri_width = sc->sc_width;
    495      1.1  macallan 	ri->ri_height = sc->sc_height;
    496      1.1  macallan 	ri->ri_stride = sc->sc_stride;
    497  1.3.4.6      yamt 	ri->ri_flg = RI_CENTER;
    498      1.1  macallan 
    499      1.1  macallan 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
    500      1.1  macallan 	ri->ri_caps = WSSCREEN_WSCOLORS;
    501      1.1  macallan 
    502      1.1  macallan 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    503      1.1  macallan 		    sc->sc_width / ri->ri_font->fontwidth);
    504      1.1  macallan 
    505      1.1  macallan 	ri->ri_hw = scr;
    506      1.1  macallan 	ri->ri_ops.copyrows = r128fb_copyrows;
    507      1.1  macallan 	ri->ri_ops.copycols = r128fb_copycols;
    508      1.1  macallan 	ri->ri_ops.eraserows = r128fb_eraserows;
    509      1.1  macallan 	ri->ri_ops.erasecols = r128fb_erasecols;
    510      1.1  macallan 	ri->ri_ops.cursor = r128fb_cursor;
    511      1.1  macallan 	ri->ri_ops.putchar = r128fb_putchar;
    512      1.1  macallan }
    513      1.1  macallan 
    514      1.1  macallan static int
    515      1.1  macallan r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
    516      1.1  macallan {
    517      1.1  macallan 	u_char *r, *g, *b;
    518      1.1  macallan 	u_int index = cm->index;
    519      1.1  macallan 	u_int count = cm->count;
    520      1.1  macallan 	int i, error;
    521      1.1  macallan 	u_char rbuf[256], gbuf[256], bbuf[256];
    522      1.1  macallan 
    523      1.1  macallan #ifdef R128FB_DEBUG
    524      1.1  macallan 	aprint_debug("putcmap: %d %d\n",index, count);
    525      1.1  macallan #endif
    526      1.1  macallan 	if (cm->index >= 256 || cm->count > 256 ||
    527      1.1  macallan 	    (cm->index + cm->count) > 256)
    528      1.1  macallan 		return EINVAL;
    529      1.1  macallan 	error = copyin(cm->red, &rbuf[index], count);
    530      1.1  macallan 	if (error)
    531      1.1  macallan 		return error;
    532      1.1  macallan 	error = copyin(cm->green, &gbuf[index], count);
    533      1.1  macallan 	if (error)
    534      1.1  macallan 		return error;
    535      1.1  macallan 	error = copyin(cm->blue, &bbuf[index], count);
    536      1.1  macallan 	if (error)
    537      1.1  macallan 		return error;
    538      1.1  macallan 
    539      1.1  macallan 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    540      1.1  macallan 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    541      1.1  macallan 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    542      1.1  macallan 
    543      1.1  macallan 	r = &sc->sc_cmap_red[index];
    544      1.1  macallan 	g = &sc->sc_cmap_green[index];
    545      1.1  macallan 	b = &sc->sc_cmap_blue[index];
    546      1.1  macallan 
    547      1.1  macallan 	for (i = 0; i < count; i++) {
    548      1.1  macallan 		r128fb_putpalreg(sc, index, *r, *g, *b);
    549      1.1  macallan 		index++;
    550      1.1  macallan 		r++, g++, b++;
    551      1.1  macallan 	}
    552      1.1  macallan 	return 0;
    553      1.1  macallan }
    554      1.1  macallan 
    555      1.1  macallan static int
    556      1.1  macallan r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
    557      1.1  macallan {
    558      1.1  macallan 	u_int index = cm->index;
    559      1.1  macallan 	u_int count = cm->count;
    560      1.1  macallan 	int error;
    561      1.1  macallan 
    562      1.1  macallan 	if (index >= 255 || count > 256 || index + count > 256)
    563      1.1  macallan 		return EINVAL;
    564      1.1  macallan 
    565      1.1  macallan 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    566      1.1  macallan 	if (error)
    567      1.1  macallan 		return error;
    568      1.1  macallan 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    569      1.1  macallan 	if (error)
    570      1.1  macallan 		return error;
    571      1.1  macallan 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    572      1.1  macallan 	if (error)
    573      1.1  macallan 		return error;
    574      1.1  macallan 
    575      1.1  macallan 	return 0;
    576      1.1  macallan }
    577      1.1  macallan 
    578      1.1  macallan static void
    579      1.1  macallan r128fb_restore_palette(struct r128fb_softc *sc)
    580      1.1  macallan {
    581      1.1  macallan 	int i;
    582      1.1  macallan 
    583      1.1  macallan 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    584      1.1  macallan 		r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
    585      1.1  macallan 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    586      1.1  macallan 	}
    587      1.1  macallan }
    588      1.1  macallan 
    589      1.1  macallan static int
    590      1.1  macallan r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    591      1.1  macallan     uint8_t b)
    592      1.1  macallan {
    593      1.1  macallan 	uint32_t reg;
    594      1.1  macallan 
    595      1.1  macallan 	/* whack the DAC */
    596      1.1  macallan 	reg = (r << 16) | (g << 8) | b;
    597      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx);
    598      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg);
    599      1.1  macallan 	return 0;
    600      1.1  macallan }
    601      1.1  macallan 
    602      1.1  macallan static void
    603      1.1  macallan r128fb_init(struct r128fb_softc *sc)
    604      1.1  macallan {
    605      1.1  macallan 	uint32_t datatype;
    606      1.1  macallan 
    607      1.1  macallan 	r128fb_flush_engine(sc);
    608      1.1  macallan 
    609  1.3.4.6      yamt 	r128fb_wait(sc, 9);
    610      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0);
    611      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0);
    612      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH,
    613      1.1  macallan 	    sc->sc_stride >> 3);
    614      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0);
    615      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    616      1.1  macallan 	    R128_DEFAULT_SC_BOTTOM_RIGHT,
    617      1.1  macallan 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
    618      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0);
    619      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT,
    620      1.1  macallan 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
    621  1.3.4.6      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    622  1.3.4.6      yamt 	    R128_DEFAULT_SC_BOTTOM_RIGHT,
    623  1.3.4.6      yamt 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
    624  1.3.4.6      yamt 
    625  1.3.4.6      yamt #if BYTE_ORDER == BIG_ENDIAN
    626      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE,
    627      1.1  macallan 	    R128_HOST_BIG_ENDIAN_EN);
    628  1.3.4.6      yamt #else
    629  1.3.4.6      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE,
    630  1.3.4.6      yamt 	    R128_HOST_LITTLE_ENDIAN_EN);
    631  1.3.4.6      yamt #endif
    632      1.1  macallan 
    633      1.1  macallan 	r128fb_wait(sc, 5);
    634      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH,
    635      1.1  macallan 	    sc->sc_stride >> 3);
    636      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH,
    637      1.1  macallan 	    sc->sc_stride >> 3);
    638      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0);
    639      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0);
    640      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK,
    641      1.1  macallan 	    0xffffffff);
    642      1.1  macallan 
    643      1.1  macallan 	switch (sc->sc_depth) {
    644      1.1  macallan 		case 8:
    645      1.1  macallan 			datatype = R128_GMC_DST_8BPP_CI;
    646      1.1  macallan 			break;
    647      1.1  macallan 		case 15:
    648      1.1  macallan 			datatype = R128_GMC_DST_15BPP;
    649      1.1  macallan 			break;
    650      1.1  macallan 		case 16:
    651      1.1  macallan 			datatype = R128_GMC_DST_16BPP;
    652      1.1  macallan 			break;
    653      1.1  macallan 		case 24:
    654      1.1  macallan 			datatype = R128_GMC_DST_24BPP;
    655      1.1  macallan 			break;
    656      1.1  macallan 		case 32:
    657      1.1  macallan 			datatype = R128_GMC_DST_32BPP;
    658      1.1  macallan 			break;
    659      1.1  macallan 		default:
    660      1.1  macallan 			aprint_error("%s: unsupported depth %d\n",
    661      1.1  macallan 			    device_xname(sc->sc_dev), sc->sc_depth);
    662      1.1  macallan 			return;
    663      1.1  macallan 	}
    664      1.1  macallan 	sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS |
    665      1.1  macallan 	    R128_GMC_AUX_CLIP_DIS | datatype;
    666      1.1  macallan 
    667      1.1  macallan 	r128fb_flush_engine(sc);
    668      1.1  macallan }
    669      1.1  macallan 
    670      1.1  macallan static void
    671      1.1  macallan r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he,
    672      1.1  macallan      uint32_t colour)
    673      1.1  macallan {
    674      1.1  macallan 
    675  1.3.4.6      yamt 	r128fb_wait(sc, 5);
    676      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
    677      1.1  macallan 	    R128_GMC_BRUSH_SOLID_COLOR |
    678      1.1  macallan 	    R128_GMC_SRC_DATATYPE_COLOR |
    679      1.1  macallan 	    R128_ROP3_P |
    680      1.1  macallan 	    sc->sc_master_cntl);
    681      1.1  macallan 
    682      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR,
    683      1.1  macallan 	    colour);
    684      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL,
    685      1.1  macallan 	    R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
    686      1.1  macallan 	/* now feed it coordinates */
    687      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
    688      1.1  macallan 	    (x << 16) | y);
    689      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
    690      1.1  macallan 	    (wi << 16) | he);
    691      1.1  macallan }
    692      1.1  macallan 
    693      1.1  macallan static void
    694      1.1  macallan r128fb_bitblt(struct r128fb_softc *sc, int xs, int ys, int xd, int yd,
    695      1.1  macallan     int wi, int he, int rop)
    696      1.1  macallan {
    697      1.1  macallan 	uint32_t dp_cntl = 0;
    698      1.1  macallan 
    699      1.1  macallan 	r128fb_wait(sc, 5);
    700      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
    701      1.1  macallan 	    R128_GMC_BRUSH_SOLID_COLOR |
    702      1.1  macallan 	    R128_GMC_SRC_DATATYPE_COLOR |
    703      1.1  macallan 	    rop |
    704      1.1  macallan 	    R128_DP_SRC_SOURCE_MEMORY |
    705      1.1  macallan 	    sc->sc_master_cntl);
    706      1.1  macallan 
    707      1.1  macallan 	if (yd <= ys) {
    708      1.1  macallan 		dp_cntl = R128_DST_Y_TOP_TO_BOTTOM;
    709      1.1  macallan 	} else {
    710      1.1  macallan 		ys += he - 1;
    711      1.1  macallan 		yd += he - 1;
    712      1.1  macallan 	}
    713      1.1  macallan 	if (xd <= xs) {
    714      1.1  macallan 		dp_cntl |= R128_DST_X_LEFT_TO_RIGHT;
    715      1.1  macallan 	} else {
    716      1.1  macallan 		xs += wi - 1;
    717      1.1  macallan 		xd += wi - 1;
    718      1.1  macallan 	}
    719      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl);
    720      1.1  macallan 
    721      1.1  macallan 	/* now feed it coordinates */
    722      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y,
    723      1.1  macallan 	    (xs << 16) | ys);
    724      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
    725      1.1  macallan 	    (xd << 16) | yd);
    726      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
    727      1.1  macallan 	    (wi << 16) | he);
    728      1.1  macallan }
    729      1.1  macallan 
    730      1.1  macallan static void
    731      1.1  macallan r128fb_cursor(void *cookie, int on, int row, int col)
    732      1.1  macallan {
    733      1.1  macallan 	struct rasops_info *ri = cookie;
    734      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    735      1.1  macallan 	struct r128fb_softc *sc = scr->scr_cookie;
    736      1.1  macallan 	int x, y, wi, he;
    737      1.1  macallan 
    738      1.1  macallan 	wi = ri->ri_font->fontwidth;
    739      1.1  macallan 	he = ri->ri_font->fontheight;
    740      1.1  macallan 
    741      1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    742      1.1  macallan 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    743      1.1  macallan 		y = ri->ri_crow * he + ri->ri_yorigin;
    744      1.1  macallan 		if (ri->ri_flg & RI_CURSOR) {
    745      1.1  macallan 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
    746      1.1  macallan 			ri->ri_flg &= ~RI_CURSOR;
    747      1.1  macallan 		}
    748      1.1  macallan 		ri->ri_crow = row;
    749      1.1  macallan 		ri->ri_ccol = col;
    750      1.1  macallan 		if (on) {
    751      1.1  macallan 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    752      1.1  macallan 			y = ri->ri_crow * he + ri->ri_yorigin;
    753      1.1  macallan 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
    754  1.3.4.2      yamt 			ri->ri_flg |= RI_CURSOR;
    755      1.1  macallan 		}
    756      1.1  macallan 	} else {
    757      1.1  macallan 		scr->scr_ri.ri_crow = row;
    758      1.1  macallan 		scr->scr_ri.ri_ccol = col;
    759      1.1  macallan 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    760      1.1  macallan 	}
    761      1.1  macallan 
    762      1.1  macallan }
    763      1.1  macallan 
    764      1.1  macallan static void
    765      1.1  macallan r128fb_putchar(void *cookie, int row, int col, u_int c, long attr)
    766      1.1  macallan {
    767  1.3.4.6      yamt 	struct rasops_info *ri = cookie;
    768  1.3.4.6      yamt 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    769  1.3.4.6      yamt 	struct vcons_screen *scr = ri->ri_hw;
    770  1.3.4.6      yamt 	struct r128fb_softc *sc = scr->scr_cookie;
    771  1.3.4.6      yamt 
    772  1.3.4.6      yamt 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    773  1.3.4.6      yamt 		void *data;
    774  1.3.4.6      yamt 		uint32_t fg, bg;
    775  1.3.4.6      yamt 		int uc, i;
    776  1.3.4.6      yamt 		int x, y, wi, he, offset;
    777  1.3.4.6      yamt 
    778  1.3.4.6      yamt 		wi = font->fontwidth;
    779  1.3.4.6      yamt 		he = font->fontheight;
    780  1.3.4.6      yamt 
    781  1.3.4.6      yamt 		if (!CHAR_IN_FONT(c, font))
    782  1.3.4.6      yamt 			return;
    783  1.3.4.6      yamt 		bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    784  1.3.4.6      yamt 		fg = ri->ri_devcmap[(attr >> 24) & 0xf];
    785  1.3.4.6      yamt 		x = ri->ri_xorigin + col * wi;
    786  1.3.4.6      yamt 		y = ri->ri_yorigin + row * he;
    787  1.3.4.6      yamt 		if (c == 0x20) {
    788  1.3.4.6      yamt 			r128fb_rectfill(sc, x, y, wi, he, bg);
    789  1.3.4.6      yamt 		} else {
    790  1.3.4.6      yamt 			uc = c - font->firstchar;
    791  1.3.4.6      yamt 			data = (uint8_t *)font->data + uc * ri->ri_fontscale;
    792  1.3.4.6      yamt 
    793  1.3.4.6      yamt 			r128fb_wait(sc, 8);
    794  1.3.4.6      yamt 
    795  1.3.4.6      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    796  1.3.4.6      yamt 			    R128_DP_GUI_MASTER_CNTL,
    797  1.3.4.6      yamt 			    R128_GMC_BRUSH_SOLID_COLOR |
    798  1.3.4.6      yamt 			    R128_GMC_SRC_DATATYPE_MONO_FG_BG |
    799  1.3.4.6      yamt 			    R128_ROP3_S |
    800  1.3.4.6      yamt 			    R128_DP_SRC_SOURCE_HOST_DATA |
    801  1.3.4.6      yamt 			    R128_GMC_DST_CLIPPING |
    802  1.3.4.6      yamt 			    sc->sc_master_cntl);
    803  1.3.4.6      yamt 
    804  1.3.4.6      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    805  1.3.4.6      yamt 			    R128_DP_CNTL,
    806  1.3.4.6      yamt 			    R128_DST_Y_TOP_TO_BOTTOM |
    807  1.3.4.6      yamt 			    R128_DST_X_LEFT_TO_RIGHT);
    808  1.3.4.6      yamt 
    809  1.3.4.6      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    810  1.3.4.6      yamt 			    R128_DP_SRC_FRGD_CLR, fg);
    811  1.3.4.6      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    812  1.3.4.6      yamt 			    R128_DP_SRC_BKGD_CLR, bg);
    813  1.3.4.6      yamt 
    814  1.3.4.6      yamt 			/*
    815  1.3.4.6      yamt 			 * The Rage 128 doesn't have anything to skip pixels
    816  1.3.4.6      yamt 			 * when colour expanding but all coordinates
    817  1.3.4.6      yamt 			 * are signed so we just clip the leading bytes and
    818  1.3.4.6      yamt 			 * trailing bits away
    819  1.3.4.6      yamt 			 */
    820  1.3.4.6      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    821  1.3.4.6      yamt 			    R128_SC_RIGHT, x + wi - 1);
    822  1.3.4.6      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    823  1.3.4.6      yamt 			    R128_SC_LEFT, x);
    824  1.3.4.6      yamt 
    825  1.3.4.6      yamt 			/* needed? */
    826  1.3.4.6      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    827  1.3.4.6      yamt 			    R128_SRC_X_Y, 0);
    828  1.3.4.6      yamt 
    829  1.3.4.6      yamt 			offset = 32 - (ri->ri_font->stride << 3);
    830  1.3.4.6      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    831  1.3.4.6      yamt 			    R128_DST_X_Y, ((x - offset) << 16) | y);
    832  1.3.4.6      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    833  1.3.4.6      yamt 			    R128_DST_WIDTH_HEIGHT, (32 << 16) | he);
    834  1.3.4.6      yamt 
    835  1.3.4.6      yamt 			r128fb_wait(sc, he);
    836  1.3.4.6      yamt 			switch (ri->ri_font->stride) {
    837  1.3.4.6      yamt 			case 1: {
    838  1.3.4.6      yamt 				uint8_t *data8 = data;
    839  1.3.4.6      yamt 				uint32_t reg;
    840  1.3.4.6      yamt 				for (i = 0; i < he; i++) {
    841  1.3.4.6      yamt 					reg = *data8;
    842  1.3.4.6      yamt 					bus_space_write_stream_4(sc->sc_memt,
    843  1.3.4.6      yamt 					    sc->sc_regh,
    844  1.3.4.6      yamt 					    R128_HOST_DATA0, reg);
    845  1.3.4.6      yamt 					data8++;
    846  1.3.4.6      yamt 				}
    847  1.3.4.6      yamt 			break;
    848  1.3.4.6      yamt 			}
    849  1.3.4.6      yamt 			case 2: {
    850  1.3.4.6      yamt 				uint16_t *data16 = data;
    851  1.3.4.6      yamt 				uint32_t reg;
    852  1.3.4.6      yamt 				for (i = 0; i < he; i++) {
    853  1.3.4.6      yamt 					reg = *data16;
    854  1.3.4.6      yamt 					bus_space_write_stream_4(sc->sc_memt,
    855  1.3.4.6      yamt 					    sc->sc_regh,
    856  1.3.4.6      yamt 					    R128_HOST_DATA0, reg);
    857  1.3.4.6      yamt 					data16++;
    858  1.3.4.6      yamt 				}
    859  1.3.4.6      yamt 				break;
    860  1.3.4.6      yamt 			}
    861  1.3.4.6      yamt 			}
    862  1.3.4.6      yamt 		}
    863  1.3.4.6      yamt 	}
    864      1.1  macallan }
    865      1.1  macallan 
    866      1.1  macallan static void
    867      1.1  macallan r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    868      1.1  macallan {
    869      1.1  macallan 	struct rasops_info *ri = cookie;
    870      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    871      1.1  macallan 	struct r128fb_softc *sc = scr->scr_cookie;
    872      1.1  macallan 	int32_t xs, xd, y, width, height;
    873      1.1  macallan 
    874      1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    875      1.1  macallan 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    876      1.1  macallan 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    877      1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    878      1.1  macallan 		width = ri->ri_font->fontwidth * ncols;
    879      1.1  macallan 		height = ri->ri_font->fontheight;
    880      1.1  macallan 		r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S);
    881      1.1  macallan 	}
    882      1.1  macallan }
    883      1.1  macallan 
    884      1.1  macallan static void
    885      1.1  macallan r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    886      1.1  macallan {
    887      1.1  macallan 	struct rasops_info *ri = cookie;
    888      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    889      1.1  macallan 	struct r128fb_softc *sc = scr->scr_cookie;
    890      1.1  macallan 	int32_t x, y, width, height, fg, bg, ul;
    891      1.1  macallan 
    892      1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    893      1.1  macallan 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    894      1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    895      1.1  macallan 		width = ri->ri_font->fontwidth * ncols;
    896      1.1  macallan 		height = ri->ri_font->fontheight;
    897      1.1  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    898      1.1  macallan 
    899      1.1  macallan 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    900      1.1  macallan 	}
    901      1.1  macallan }
    902      1.1  macallan 
    903      1.1  macallan static void
    904      1.1  macallan r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    905      1.1  macallan {
    906      1.1  macallan 	struct rasops_info *ri = cookie;
    907      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    908      1.1  macallan 	struct r128fb_softc *sc = scr->scr_cookie;
    909      1.1  macallan 	int32_t x, ys, yd, width, height;
    910      1.1  macallan 
    911      1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    912      1.1  macallan 		x = ri->ri_xorigin;
    913      1.1  macallan 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    914      1.1  macallan 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    915      1.1  macallan 		width = ri->ri_emuwidth;
    916  1.3.4.6      yamt 		height = ri->ri_font->fontheight * nrows;
    917      1.1  macallan 		r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S);
    918      1.1  macallan 	}
    919      1.1  macallan }
    920      1.1  macallan 
    921      1.1  macallan static void
    922      1.1  macallan r128fb_eraserows(void *cookie, int row, int nrows, long fillattr)
    923      1.1  macallan {
    924      1.1  macallan 	struct rasops_info *ri = cookie;
    925      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    926      1.1  macallan 	struct r128fb_softc *sc = scr->scr_cookie;
    927      1.1  macallan 	int32_t x, y, width, height, fg, bg, ul;
    928      1.1  macallan 
    929      1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    930      1.1  macallan 		x = ri->ri_xorigin;
    931      1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    932      1.1  macallan 		width = ri->ri_emuwidth;
    933      1.1  macallan 		height = ri->ri_font->fontheight * nrows;
    934      1.1  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    935      1.1  macallan 
    936      1.1  macallan 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    937      1.1  macallan 	}
    938      1.1  macallan }
    939      1.1  macallan 
    940  1.3.4.6      yamt static void
    941  1.3.4.6      yamt r128fb_set_backlight(struct r128fb_softc *sc, int level)
    942  1.3.4.6      yamt {
    943  1.3.4.6      yamt 	uint32_t reg;
    944  1.3.4.6      yamt 
    945  1.3.4.6      yamt 	/*
    946  1.3.4.6      yamt 	 * should we do nothing when backlight is off, should we just store the
    947  1.3.4.6      yamt 	 * level and use it when turning back on or should we just flip sc_bl_on
    948  1.3.4.6      yamt 	 * and turn the backlight on?
    949  1.3.4.6      yamt 	 * For now turn it on so a crashed screensaver can't get the user stuck
    950  1.3.4.6      yamt 	 * with a dark screen as long as hotkeys work
    951  1.3.4.6      yamt 	 */
    952  1.3.4.6      yamt 	if (level > 255) level = 255;
    953  1.3.4.6      yamt 	if (level < 0) level = 0;
    954  1.3.4.6      yamt 	if (level == sc->sc_bl_level)
    955  1.3.4.6      yamt 		return;
    956  1.3.4.6      yamt 	sc->sc_bl_level = level;
    957  1.3.4.6      yamt 	if (sc->sc_bl_on == 0)
    958  1.3.4.6      yamt 		sc->sc_bl_on = 1;
    959  1.3.4.6      yamt 	level = 255 - level;
    960  1.3.4.6      yamt 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
    961  1.3.4.6      yamt 	reg &= ~R128_LEVEL_MASK;
    962  1.3.4.6      yamt 	reg |= level << R128_LEVEL_SHIFT;
    963  1.3.4.6      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
    964  1.3.4.6      yamt 	DPRINTF("backlight level: %d reg %08x\n", level, reg);
    965  1.3.4.6      yamt }
    966  1.3.4.6      yamt 
    967  1.3.4.6      yamt static void
    968  1.3.4.6      yamt r128fb_switch_backlight(struct r128fb_softc *sc, int on)
    969  1.3.4.6      yamt {
    970  1.3.4.6      yamt 	uint32_t reg;
    971  1.3.4.6      yamt 	int level;
    972  1.3.4.6      yamt 
    973  1.3.4.6      yamt 	if (on == sc->sc_bl_on)
    974  1.3.4.6      yamt 		return;
    975  1.3.4.6      yamt 	sc->sc_bl_on = on;
    976  1.3.4.6      yamt 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
    977  1.3.4.6      yamt 	reg &= ~R128_LEVEL_MASK;
    978  1.3.4.6      yamt 	level = on ? 255 - sc->sc_bl_level : 255;
    979  1.3.4.6      yamt 	reg |= level << R128_LEVEL_SHIFT;
    980  1.3.4.6      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
    981  1.3.4.6      yamt 	DPRINTF("backlight state: %d reg %08x\n", on, reg);
    982  1.3.4.6      yamt }
    983  1.3.4.6      yamt 
    984  1.3.4.6      yamt 
    985  1.3.4.6      yamt static void
    986  1.3.4.6      yamt r128fb_brightness_up(device_t dev)
    987  1.3.4.6      yamt {
    988  1.3.4.6      yamt 	struct r128fb_softc *sc = device_private(dev);
    989  1.3.4.6      yamt 
    990  1.3.4.6      yamt 	r128fb_set_backlight(sc, sc->sc_bl_level + 8);
    991  1.3.4.6      yamt }
    992  1.3.4.6      yamt 
    993  1.3.4.6      yamt static void
    994  1.3.4.6      yamt r128fb_brightness_down(device_t dev)
    995  1.3.4.6      yamt {
    996  1.3.4.6      yamt 	struct r128fb_softc *sc = device_private(dev);
    997  1.3.4.6      yamt 
    998  1.3.4.6      yamt 	r128fb_set_backlight(sc, sc->sc_bl_level - 8);
    999  1.3.4.6      yamt }
   1000