Home | History | Annotate | Line # | Download | only in pci
r128fb.c revision 1.18
      1 /*	$NetBSD: r128fb.c,v 1.18 2010/12/25 03:29:08 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.18 2010/12/25 03:29:08 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 	case WSDISPLAYIO_GTYPE:
    346 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    347 		return 0;
    348 
    349 	/* PCI config read/write passthrough. */
    350 	case PCI_IOC_CFGREAD:
    351 	case PCI_IOC_CFGWRITE:
    352 		return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    353 		    cmd, data, flag, l));
    354 
    355 	case WSDISPLAYIO_GINFO:
    356 		if (ms == NULL)
    357 			return ENODEV;
    358 		wdf = (void *)data;
    359 		wdf->height = ms->scr_ri.ri_height;
    360 		wdf->width = ms->scr_ri.ri_width;
    361 		wdf->depth = ms->scr_ri.ri_depth;
    362 		wdf->cmsize = 256;
    363 		return 0;
    364 
    365 	case WSDISPLAYIO_GETCMAP:
    366 		return r128fb_getcmap(sc,
    367 		    (struct wsdisplay_cmap *)data);
    368 
    369 	case WSDISPLAYIO_PUTCMAP:
    370 		return r128fb_putcmap(sc,
    371 		    (struct wsdisplay_cmap *)data);
    372 
    373 	case WSDISPLAYIO_LINEBYTES:
    374 		*(u_int *)data = sc->sc_stride;
    375 		return 0;
    376 
    377 	case WSDISPLAYIO_SMODE: {
    378 		int new_mode = *(int*)data;
    379 		if (new_mode != sc->sc_mode) {
    380 			sc->sc_mode = new_mode;
    381 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    382 				r128fb_init(sc);
    383 				r128fb_restore_palette(sc);
    384 				vcons_redraw_screen(ms);
    385 			}
    386 		}
    387 		}
    388 		return 0;
    389 
    390 	case WSDISPLAYIO_GETPARAM:
    391 		param = (struct wsdisplay_param *)data;
    392 		if (sc->sc_have_backlight == 0)
    393 			return EPASSTHROUGH;
    394 		switch (param->param) {
    395 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    396 			param->min = 0;
    397 			param->max = 255;
    398 			param->curval = sc->sc_bl_level;
    399 			return 0;
    400 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    401 			param->min = 0;
    402 			param->max = 1;
    403 			param->curval = sc->sc_bl_on;
    404 			return 0;
    405 		}
    406 		return EPASSTHROUGH;
    407 
    408 	case WSDISPLAYIO_SETPARAM:
    409 		param = (struct wsdisplay_param *)data;
    410 		if (sc->sc_have_backlight == 0)
    411 			return EPASSTHROUGH;
    412 		switch (param->param) {
    413 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    414 			r128fb_set_backlight(sc, param->curval);
    415 			return 0;
    416 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    417 			r128fb_switch_backlight(sc,  param->curval);
    418 			return 0;
    419 		}
    420 		return EPASSTHROUGH;
    421 	}
    422 	return EPASSTHROUGH;
    423 }
    424 
    425 static paddr_t
    426 r128fb_mmap(void *v, void *vs, off_t offset, int prot)
    427 {
    428 	struct vcons_data *vd = v;
    429 	struct r128fb_softc *sc = vd->cookie;
    430 	paddr_t pa;
    431 
    432 	/* 'regular' framebuffer mmap()ing */
    433 	if (offset < sc->sc_fbsize) {
    434 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    435 		    BUS_SPACE_MAP_LINEAR);
    436 		return pa;
    437 	}
    438 
    439 	/*
    440 	 * restrict all other mappings to processes with superuser privileges
    441 	 * or the kernel itself
    442 	 */
    443 	if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
    444 	    NULL) != 0) {
    445 		aprint_normal("%s: mmap() rejected.\n",
    446 		    device_xname(sc->sc_dev));
    447 		return -1;
    448 	}
    449 
    450 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    451 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    452 		    BUS_SPACE_MAP_LINEAR);
    453 		return pa;
    454 	}
    455 
    456 	if ((offset >= sc->sc_reg) &&
    457 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    458 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    459 		    BUS_SPACE_MAP_LINEAR);
    460 		return pa;
    461 	}
    462 
    463 #ifdef PCI_MAGIC_IO_RANGE
    464 	/* allow mapping of IO space */
    465 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    466 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    467 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    468 		    0, prot, BUS_SPACE_MAP_LINEAR);
    469 		return pa;
    470 	}
    471 #endif
    472 
    473 #ifdef OFB_ALLOW_OTHERS
    474 	if (offset >= 0x80000000) {
    475 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    476 		    BUS_SPACE_MAP_LINEAR);
    477 		return pa;
    478 	}
    479 #endif
    480 	return -1;
    481 }
    482 
    483 static void
    484 r128fb_init_screen(void *cookie, struct vcons_screen *scr,
    485     int existing, long *defattr)
    486 {
    487 	struct r128fb_softc *sc = cookie;
    488 	struct rasops_info *ri = &scr->scr_ri;
    489 
    490 	ri->ri_depth = sc->sc_depth;
    491 	ri->ri_width = sc->sc_width;
    492 	ri->ri_height = sc->sc_height;
    493 	ri->ri_stride = sc->sc_stride;
    494 	ri->ri_flg = RI_CENTER;
    495 
    496 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
    497 	ri->ri_caps = WSSCREEN_WSCOLORS;
    498 
    499 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    500 		    sc->sc_width / ri->ri_font->fontwidth);
    501 
    502 	ri->ri_hw = scr;
    503 	ri->ri_ops.copyrows = r128fb_copyrows;
    504 	ri->ri_ops.copycols = r128fb_copycols;
    505 	ri->ri_ops.eraserows = r128fb_eraserows;
    506 	ri->ri_ops.erasecols = r128fb_erasecols;
    507 	ri->ri_ops.cursor = r128fb_cursor;
    508 	ri->ri_ops.putchar = r128fb_putchar;
    509 }
    510 
    511 static int
    512 r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
    513 {
    514 	u_char *r, *g, *b;
    515 	u_int index = cm->index;
    516 	u_int count = cm->count;
    517 	int i, error;
    518 	u_char rbuf[256], gbuf[256], bbuf[256];
    519 
    520 #ifdef R128FB_DEBUG
    521 	aprint_debug("putcmap: %d %d\n",index, count);
    522 #endif
    523 	if (cm->index >= 256 || cm->count > 256 ||
    524 	    (cm->index + cm->count) > 256)
    525 		return EINVAL;
    526 	error = copyin(cm->red, &rbuf[index], count);
    527 	if (error)
    528 		return error;
    529 	error = copyin(cm->green, &gbuf[index], count);
    530 	if (error)
    531 		return error;
    532 	error = copyin(cm->blue, &bbuf[index], count);
    533 	if (error)
    534 		return error;
    535 
    536 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    537 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    538 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    539 
    540 	r = &sc->sc_cmap_red[index];
    541 	g = &sc->sc_cmap_green[index];
    542 	b = &sc->sc_cmap_blue[index];
    543 
    544 	for (i = 0; i < count; i++) {
    545 		r128fb_putpalreg(sc, index, *r, *g, *b);
    546 		index++;
    547 		r++, g++, b++;
    548 	}
    549 	return 0;
    550 }
    551 
    552 static int
    553 r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
    554 {
    555 	u_int index = cm->index;
    556 	u_int count = cm->count;
    557 	int error;
    558 
    559 	if (index >= 255 || count > 256 || index + count > 256)
    560 		return EINVAL;
    561 
    562 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    563 	if (error)
    564 		return error;
    565 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    566 	if (error)
    567 		return error;
    568 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    569 	if (error)
    570 		return error;
    571 
    572 	return 0;
    573 }
    574 
    575 static void
    576 r128fb_restore_palette(struct r128fb_softc *sc)
    577 {
    578 	int i;
    579 
    580 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    581 		r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
    582 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    583 	}
    584 }
    585 
    586 static int
    587 r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    588     uint8_t b)
    589 {
    590 	uint32_t reg;
    591 
    592 	/* whack the DAC */
    593 	reg = (r << 16) | (g << 8) | b;
    594 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx);
    595 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg);
    596 	return 0;
    597 }
    598 
    599 static void
    600 r128fb_init(struct r128fb_softc *sc)
    601 {
    602 	uint32_t datatype;
    603 
    604 	r128fb_flush_engine(sc);
    605 
    606 	r128fb_wait(sc, 9);
    607 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0);
    608 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0);
    609 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH,
    610 	    sc->sc_stride >> 3);
    611 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0);
    612 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    613 	    R128_DEFAULT_SC_BOTTOM_RIGHT,
    614 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
    615 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0);
    616 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_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,
    619 	    R128_DEFAULT_SC_BOTTOM_RIGHT,
    620 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
    621 
    622 #if BYTE_ORDER == BIG_ENDIAN
    623 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE,
    624 	    R128_HOST_BIG_ENDIAN_EN);
    625 #else
    626 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE, 0);
    627 #endif
    628 
    629 	r128fb_wait(sc, 5);
    630 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH,
    631 	    sc->sc_stride >> 3);
    632 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH,
    633 	    sc->sc_stride >> 3);
    634 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0);
    635 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0);
    636 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK,
    637 	    0xffffffff);
    638 
    639 	switch (sc->sc_depth) {
    640 		case 8:
    641 			datatype = R128_GMC_DST_8BPP_CI;
    642 			break;
    643 		case 15:
    644 			datatype = R128_GMC_DST_15BPP;
    645 			break;
    646 		case 16:
    647 			datatype = R128_GMC_DST_16BPP;
    648 			break;
    649 		case 24:
    650 			datatype = R128_GMC_DST_24BPP;
    651 			break;
    652 		case 32:
    653 			datatype = R128_GMC_DST_32BPP;
    654 			break;
    655 		default:
    656 			aprint_error("%s: unsupported depth %d\n",
    657 			    device_xname(sc->sc_dev), sc->sc_depth);
    658 			return;
    659 	}
    660 	sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS |
    661 	    R128_GMC_AUX_CLIP_DIS | datatype;
    662 
    663 	r128fb_flush_engine(sc);
    664 }
    665 
    666 static void
    667 r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he,
    668      uint32_t colour)
    669 {
    670 
    671 	r128fb_wait(sc, 5);
    672 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
    673 	    R128_GMC_BRUSH_SOLID_COLOR |
    674 	    R128_GMC_SRC_DATATYPE_COLOR |
    675 	    R128_ROP3_P |
    676 	    sc->sc_master_cntl);
    677 
    678 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR,
    679 	    colour);
    680 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL,
    681 	    R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
    682 	/* now feed it coordinates */
    683 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
    684 	    (x << 16) | y);
    685 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
    686 	    (wi << 16) | he);
    687 }
    688 
    689 static void
    690 r128fb_bitblt(struct r128fb_softc *sc, int xs, int ys, int xd, int yd,
    691     int wi, int he, int rop)
    692 {
    693 	uint32_t dp_cntl = 0;
    694 
    695 	r128fb_wait(sc, 5);
    696 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
    697 	    R128_GMC_BRUSH_SOLID_COLOR |
    698 	    R128_GMC_SRC_DATATYPE_COLOR |
    699 	    rop |
    700 	    R128_DP_SRC_SOURCE_MEMORY |
    701 	    sc->sc_master_cntl);
    702 
    703 	if (yd <= ys) {
    704 		dp_cntl = R128_DST_Y_TOP_TO_BOTTOM;
    705 	} else {
    706 		ys += he - 1;
    707 		yd += he - 1;
    708 	}
    709 	if (xd <= xs) {
    710 		dp_cntl |= R128_DST_X_LEFT_TO_RIGHT;
    711 	} else {
    712 		xs += wi - 1;
    713 		xd += wi - 1;
    714 	}
    715 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl);
    716 
    717 	/* now feed it coordinates */
    718 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y,
    719 	    (xs << 16) | ys);
    720 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
    721 	    (xd << 16) | yd);
    722 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
    723 	    (wi << 16) | he);
    724 }
    725 
    726 static void
    727 r128fb_cursor(void *cookie, int on, int row, int col)
    728 {
    729 	struct rasops_info *ri = cookie;
    730 	struct vcons_screen *scr = ri->ri_hw;
    731 	struct r128fb_softc *sc = scr->scr_cookie;
    732 	int x, y, wi, he;
    733 
    734 	wi = ri->ri_font->fontwidth;
    735 	he = ri->ri_font->fontheight;
    736 
    737 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    738 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    739 		y = ri->ri_crow * he + ri->ri_yorigin;
    740 		if (ri->ri_flg & RI_CURSOR) {
    741 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
    742 			ri->ri_flg &= ~RI_CURSOR;
    743 		}
    744 		ri->ri_crow = row;
    745 		ri->ri_ccol = col;
    746 		if (on) {
    747 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    748 			y = ri->ri_crow * he + ri->ri_yorigin;
    749 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
    750 			ri->ri_flg |= RI_CURSOR;
    751 		}
    752 	} else {
    753 		scr->scr_ri.ri_crow = row;
    754 		scr->scr_ri.ri_ccol = col;
    755 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    756 	}
    757 
    758 }
    759 
    760 static void
    761 r128fb_putchar(void *cookie, int row, int col, u_int c, long attr)
    762 {
    763 	struct rasops_info *ri = cookie;
    764 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    765 	struct vcons_screen *scr = ri->ri_hw;
    766 	struct r128fb_softc *sc = scr->scr_cookie;
    767 
    768 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    769 		void *data;
    770 		uint32_t fg, bg;
    771 		int uc, i;
    772 		int x, y, wi, he, offset;
    773 
    774 		wi = font->fontwidth;
    775 		he = font->fontheight;
    776 
    777 		if (!CHAR_IN_FONT(c, font))
    778 			return;
    779 		bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    780 		fg = ri->ri_devcmap[(attr >> 24) & 0xf];
    781 		x = ri->ri_xorigin + col * wi;
    782 		y = ri->ri_yorigin + row * he;
    783 		if (c == 0x20) {
    784 			r128fb_rectfill(sc, x, y, wi, he, bg);
    785 		} else {
    786 			uc = c - font->firstchar;
    787 			data = (uint8_t *)font->data + uc * ri->ri_fontscale;
    788 
    789 			r128fb_wait(sc, 8);
    790 
    791 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    792 			    R128_DP_GUI_MASTER_CNTL,
    793 			    R128_GMC_BRUSH_SOLID_COLOR |
    794 			    R128_GMC_SRC_DATATYPE_MONO_FG_BG |
    795 			    R128_ROP3_S |
    796 			    R128_DP_SRC_SOURCE_HOST_DATA |
    797 			    R128_GMC_DST_CLIPPING |
    798 			    sc->sc_master_cntl);
    799 
    800 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    801 			    R128_DP_CNTL,
    802 			    R128_DST_Y_TOP_TO_BOTTOM |
    803 			    R128_DST_X_LEFT_TO_RIGHT);
    804 
    805 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    806 			    R128_DP_SRC_FRGD_CLR, fg);
    807 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    808 			    R128_DP_SRC_BKGD_CLR, bg);
    809 
    810 			/*
    811 			 * The Rage 128 doesn't have anything to skip pixels
    812 			 * when colour expanding but all coordinates
    813 			 * are signed so we just clip the leading bytes and
    814 			 * trailing bits away
    815 			 */
    816 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    817 			    R128_SC_RIGHT, x + wi - 1);
    818 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    819 			    R128_SC_LEFT, x);
    820 
    821 			/* needed? */
    822 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    823 			    R128_SRC_X_Y, 0);
    824 
    825 			offset = 32 - (ri->ri_font->stride << 3);
    826 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    827 			    R128_DST_X_Y, ((x - offset) << 16) | y);
    828 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    829 			    R128_DST_WIDTH_HEIGHT, (32 << 16) | he);
    830 
    831 			r128fb_wait(sc, he);
    832 			switch (ri->ri_font->stride) {
    833 			case 1: {
    834 				uint8_t *data8 = data;
    835 				uint32_t reg;
    836 				for (i = 0; i < he; i++) {
    837 					reg = *data8;
    838 					bus_space_write_stream_4(sc->sc_memt,
    839 					    sc->sc_regh,
    840 					    R128_HOST_DATA0, reg);
    841 					data8++;
    842 				}
    843 			break;
    844 			}
    845 			case 2: {
    846 				uint16_t *data16 = data;
    847 				uint32_t reg;
    848 				for (i = 0; i < he; i++) {
    849 					reg = *data16;
    850 					bus_space_write_stream_4(sc->sc_memt,
    851 					    sc->sc_regh,
    852 					    R128_HOST_DATA0, reg);
    853 					data16++;
    854 				}
    855 				break;
    856 			}
    857 			}
    858 		}
    859 	}
    860 }
    861 
    862 static void
    863 r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    864 {
    865 	struct rasops_info *ri = cookie;
    866 	struct vcons_screen *scr = ri->ri_hw;
    867 	struct r128fb_softc *sc = scr->scr_cookie;
    868 	int32_t xs, xd, y, width, height;
    869 
    870 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    871 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    872 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    873 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    874 		width = ri->ri_font->fontwidth * ncols;
    875 		height = ri->ri_font->fontheight;
    876 		r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S);
    877 	}
    878 }
    879 
    880 static void
    881 r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    882 {
    883 	struct rasops_info *ri = cookie;
    884 	struct vcons_screen *scr = ri->ri_hw;
    885 	struct r128fb_softc *sc = scr->scr_cookie;
    886 	int32_t x, y, width, height, fg, bg, ul;
    887 
    888 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    889 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    890 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    891 		width = ri->ri_font->fontwidth * ncols;
    892 		height = ri->ri_font->fontheight;
    893 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    894 
    895 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    896 	}
    897 }
    898 
    899 static void
    900 r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    901 {
    902 	struct rasops_info *ri = cookie;
    903 	struct vcons_screen *scr = ri->ri_hw;
    904 	struct r128fb_softc *sc = scr->scr_cookie;
    905 	int32_t x, ys, yd, width, height;
    906 
    907 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    908 		x = ri->ri_xorigin;
    909 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    910 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    911 		width = ri->ri_emuwidth;
    912 		height = ri->ri_font->fontheight * nrows;
    913 		r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S);
    914 	}
    915 }
    916 
    917 static void
    918 r128fb_eraserows(void *cookie, int row, int nrows, long fillattr)
    919 {
    920 	struct rasops_info *ri = cookie;
    921 	struct vcons_screen *scr = ri->ri_hw;
    922 	struct r128fb_softc *sc = scr->scr_cookie;
    923 	int32_t x, y, width, height, fg, bg, ul;
    924 
    925 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    926 		x = ri->ri_xorigin;
    927 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    928 		width = ri->ri_emuwidth;
    929 		height = ri->ri_font->fontheight * nrows;
    930 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    931 
    932 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    933 	}
    934 }
    935 
    936 static void
    937 r128fb_set_backlight(struct r128fb_softc *sc, int level)
    938 {
    939 	uint32_t reg;
    940 
    941 	/*
    942 	 * should we do nothing when backlight is off, should we just store the
    943 	 * level and use it when turning back on or should we just flip sc_bl_on
    944 	 * and turn the backlight on?
    945 	 * For now turn it on so a crashed screensaver can't get the user stuck
    946 	 * with a dark screen as long as hotkeys work
    947 	 */
    948 	if (level > 255) level = 255;
    949 	if (level < 0) level = 0;
    950 	if (level == sc->sc_bl_level)
    951 		return;
    952 	sc->sc_bl_level = level;
    953 	if (sc->sc_bl_on == 0)
    954 		sc->sc_bl_on = 1;
    955 	level = 255 - level;
    956 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
    957 	reg &= ~R128_LEVEL_MASK;
    958 	reg |= (level << R128_LEVEL_SHIFT) |
    959 	       (level != 255 ? R128_LVDS_BLON : 0);
    960 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
    961 	DPRINTF("backlight level: %d reg %08x\n", level, reg);
    962 }
    963 
    964 static void
    965 r128fb_switch_backlight(struct r128fb_softc *sc, int on)
    966 {
    967 	uint32_t reg;
    968 	int level;
    969 
    970 	if (on == sc->sc_bl_on)
    971 		return;
    972 	sc->sc_bl_on = on;
    973 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
    974 	reg &= ~R128_LEVEL_MASK;
    975 	if (on) {
    976 		reg |= R128_LVDS_BLON;
    977 	} else
    978 		reg &= ~R128_LVDS_BLON;
    979 	level = on ? 255 - sc->sc_bl_level : 255;
    980 	reg |= level << R128_LEVEL_SHIFT;
    981 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
    982 	DPRINTF("backlight state: %d reg %08x\n", on, reg);
    983 }
    984 
    985 
    986 static void
    987 r128fb_brightness_up(device_t dev)
    988 {
    989 	struct r128fb_softc *sc = device_private(dev);
    990 
    991 	r128fb_set_backlight(sc, sc->sc_bl_level + 8);
    992 }
    993 
    994 static void
    995 r128fb_brightness_down(device_t dev)
    996 {
    997 	struct r128fb_softc *sc = device_private(dev);
    998 
    999 	r128fb_set_backlight(sc, sc->sc_bl_level - 8);
   1000 }
   1001