Home | History | Annotate | Line # | Download | only in pci
r128fb.c revision 1.14
      1 /*	$NetBSD: r128fb.c,v 1.14 2010/10/07 20:39:54 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.14 2010/10/07 20:39:54 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 <uvm/uvm_extern.h>
     45 
     46 #include <dev/videomode/videomode.h>
     47 
     48 #include <dev/pci/pcivar.h>
     49 #include <dev/pci/pcireg.h>
     50 #include <dev/pci/pcidevs.h>
     51 #include <dev/pci/pciio.h>
     52 #include <dev/pci/r128fbreg.h>
     53 
     54 #include <dev/wscons/wsdisplayvar.h>
     55 #include <dev/wscons/wsconsio.h>
     56 #include <dev/wsfont/wsfont.h>
     57 #include <dev/rasops/rasops.h>
     58 #include <dev/wscons/wsdisplay_vconsvar.h>
     59 
     60 #include <dev/i2c/i2cvar.h>
     61 
     62 #include "opt_r128fb.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_copycols(void *, int, int, int, int);
    127 static void	r128fb_erasecols(void *, int, int, int, long);
    128 static void	r128fb_copyrows(void *, int, int, int);
    129 static void	r128fb_eraserows(void *, int, int, long);
    130 
    131 static void	r128fb_brightness_up(device_t);
    132 static void	r128fb_brightness_down(device_t);
    133 /* set backlight level */
    134 static void	r128fb_set_backlight(struct r128fb_softc *, int);
    135 /* turn backlight on and off without messing with the level */
    136 static void	r128fb_switch_backlight(struct r128fb_softc *, int);
    137 
    138 struct wsdisplay_accessops r128fb_accessops = {
    139 	r128fb_ioctl,
    140 	r128fb_mmap,
    141 	NULL,	/* alloc_screen */
    142 	NULL,	/* free_screen */
    143 	NULL,	/* show_screen */
    144 	NULL, 	/* load_font */
    145 	NULL,	/* pollc */
    146 	NULL	/* scroll */
    147 };
    148 
    149 static inline void
    150 r128fb_wait(struct r128fb_softc *sc, int slots)
    151 {
    152 	uint32_t reg;
    153 
    154 	do {
    155 		reg = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    156 		    R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK);
    157 	} while (reg <= slots);
    158 }
    159 
    160 static void
    161 r128fb_flush_engine(struct r128fb_softc *sc)
    162 {
    163 	uint32_t reg;
    164 
    165 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT);
    166 	reg |= R128_PC_FLUSH_ALL;
    167 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT, reg);
    168 	do {
    169 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    170 		    R128_PC_NGUI_CTLSTAT);
    171 	} while (reg & R128_PC_BUSY);
    172 }
    173 
    174 static int
    175 r128fb_match(device_t parent, cfdata_t match, void *aux)
    176 {
    177 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    178 
    179 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    180 		return 0;
    181 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI)
    182 		return 0;
    183 
    184 	/* only cards tested on so far - likely need a list */
    185 	if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE1AGP4XT) ||
    186 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE3AGP4XT) ||
    187 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGEGLPCI) ||
    188 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP))
    189 		return 100;
    190 	return (0);
    191 }
    192 
    193 static void
    194 r128fb_attach(device_t parent, device_t self, void *aux)
    195 {
    196 	struct r128fb_softc	*sc = device_private(self);
    197 	struct pci_attach_args	*pa = aux;
    198 	struct rasops_info	*ri;
    199 	bus_space_tag_t		tag;
    200 	char devinfo[256];
    201 	struct wsemuldisplaydev_attach_args aa;
    202 	prop_dictionary_t	dict;
    203 	unsigned long		defattr;
    204 	bool			is_console;
    205 	int i, j;
    206 	uint32_t reg, flags;
    207 
    208 	sc->sc_pc = pa->pa_pc;
    209 	sc->sc_pcitag = pa->pa_tag;
    210 	sc->sc_memt = pa->pa_memt;
    211 	sc->sc_iot = pa->pa_iot;
    212 	sc->sc_dev = self;
    213 
    214 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    215 	aprint_normal(": %s\n", devinfo);
    216 
    217 	/* fill in parameters from properties */
    218 	dict = device_properties(self);
    219 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    220 		aprint_error("%s: no width property\n", device_xname(self));
    221 		return;
    222 	}
    223 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    224 		aprint_error("%s: no height property\n", device_xname(self));
    225 		return;
    226 	}
    227 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    228 		aprint_error("%s: no depth property\n", device_xname(self));
    229 		return;
    230 	}
    231 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) {
    232 		aprint_error("%s: no linebytes property\n",
    233 		    device_xname(self));
    234 		return;
    235 	}
    236 
    237 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    238 
    239 	if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x10, PCI_MAPREG_TYPE_MEM,
    240 	    &sc->sc_fb, &sc->sc_fbsize, &flags)) {
    241 		aprint_error("%s: failed to map the frame buffer.\n",
    242 		    device_xname(sc->sc_dev));
    243 	}
    244 
    245 	if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, 0,
    246 	    &tag, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    247 		aprint_error("%s: failed to map registers.\n",
    248 		    device_xname(sc->sc_dev));
    249 	}
    250 
    251 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    252 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    253 
    254 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    255 		"default",
    256 		0, 0,
    257 		NULL,
    258 		8, 16,
    259 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    260 		NULL
    261 	};
    262 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    263 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    264 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    265 	sc->sc_locked = 0;
    266 
    267 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    268 	    &r128fb_accessops);
    269 	sc->vd.init_screen = r128fb_init_screen;
    270 
    271 	/* init engine here */
    272 	r128fb_init(sc);
    273 
    274 	ri = &sc->sc_console_screen.scr_ri;
    275 
    276 	j = 0;
    277 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    278 
    279 		sc->sc_cmap_red[i] = rasops_cmap[j];
    280 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    281 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    282 		r128fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    283 		    rasops_cmap[j + 2]);
    284 		j += 3;
    285 	}
    286 
    287 	if (is_console) {
    288 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    289 		    &defattr);
    290 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    291 
    292 		r128fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    293 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    294 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    295 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    296 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    297 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    298 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    299 		    defattr);
    300 		vcons_replay_msgbuf(&sc->sc_console_screen);
    301 	} else {
    302 		/*
    303 		 * since we're not the console we can postpone the rest
    304 		 * until someone actually allocates a screen for us
    305 		 */
    306 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    307 	}
    308 
    309 	/* no suspend/resume support yet */
    310 	pmf_device_register(sc->sc_dev, NULL, NULL);
    311 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
    312 	DPRINTF("reg: %08x\n", reg);
    313 	if (reg & R128_LVDS_ON) {
    314 		sc->sc_have_backlight = 1;
    315 		sc->sc_bl_on = 1;
    316 		sc->sc_bl_level = 255 -
    317 		    ((reg & R128_LEVEL_MASK) >> R128_LEVEL_SHIFT);
    318 		pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
    319 		    r128fb_brightness_up, TRUE);
    320 		pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
    321 		    r128fb_brightness_down, TRUE);
    322 		aprint_verbose("%s: LVDS output is active, enabling backlight"
    323 			       " control\n", device_xname(self));
    324 	} else
    325 		sc->sc_have_backlight = 0;
    326 
    327 	aa.console = is_console;
    328 	aa.scrdata = &sc->sc_screenlist;
    329 	aa.accessops = &r128fb_accessops;
    330 	aa.accesscookie = &sc->vd;
    331 
    332 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    333 }
    334 
    335 static int
    336 r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    337 	struct lwp *l)
    338 {
    339 	struct vcons_data *vd = v;
    340 	struct r128fb_softc *sc = vd->cookie;
    341 	struct wsdisplay_fbinfo *wdf;
    342 	struct vcons_screen *ms = vd->active;
    343 	struct wsdisplay_param  *param;
    344 
    345 	switch (cmd) {
    346 
    347 		case WSDISPLAYIO_GTYPE:
    348 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    349 			return 0;
    350 
    351 		/* PCI config read/write passthrough. */
    352 		case PCI_IOC_CFGREAD:
    353 		case PCI_IOC_CFGWRITE:
    354 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    355 			    cmd, data, flag, l));
    356 
    357 		case WSDISPLAYIO_GINFO:
    358 			if (ms == NULL)
    359 				return ENODEV;
    360 			wdf = (void *)data;
    361 			wdf->height = ms->scr_ri.ri_height;
    362 			wdf->width = ms->scr_ri.ri_width;
    363 			wdf->depth = ms->scr_ri.ri_depth;
    364 			wdf->cmsize = 256;
    365 			return 0;
    366 
    367 		case WSDISPLAYIO_GETCMAP:
    368 			return r128fb_getcmap(sc,
    369 			    (struct wsdisplay_cmap *)data);
    370 
    371 		case WSDISPLAYIO_PUTCMAP:
    372 			return r128fb_putcmap(sc,
    373 			    (struct wsdisplay_cmap *)data);
    374 
    375 		case WSDISPLAYIO_LINEBYTES:
    376 			*(u_int *)data = sc->sc_stride;
    377 			return 0;
    378 
    379 		case WSDISPLAYIO_SMODE:
    380 			{
    381 				int new_mode = *(int*)data;
    382 
    383 				if (new_mode != sc->sc_mode) {
    384 					sc->sc_mode = new_mode;
    385 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    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 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
    964 	DPRINTF("backlight level: %d reg %08x\n", level, reg);
    965 }
    966 
    967 static void
    968 r128fb_switch_backlight(struct r128fb_softc *sc, int on)
    969 {
    970 	uint32_t reg;
    971 	int level;
    972 
    973 	if (on == sc->sc_bl_on)
    974 		return;
    975 	sc->sc_bl_on = on;
    976 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
    977 	reg &= ~R128_LEVEL_MASK;
    978 	level = on ? 255 - sc->sc_bl_level : 255;
    979 	reg |= level << R128_LEVEL_SHIFT;
    980 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
    981 	DPRINTF("backlight state: %d reg %08x\n", on, reg);
    982 }
    983 
    984 
    985 static void
    986 r128fb_brightness_up(device_t dev)
    987 {
    988 	struct r128fb_softc *sc = device_private(dev);
    989 
    990 	r128fb_set_backlight(sc, sc->sc_bl_level + 8);
    991 }
    992 
    993 static void
    994 r128fb_brightness_down(device_t dev)
    995 {
    996 	struct r128fb_softc *sc = device_private(dev);
    997 
    998 	r128fb_set_backlight(sc, sc->sc_bl_level - 8);
    999 }
   1000