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