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