Home | History | Annotate | Line # | Download | only in pci
r128fb.c revision 1.2
      1 /*	$NetBSD: r128fb.c,v 1.2 2007/11/23 20:12: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  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * A console driver for ATI Rage 128 graphics controllers
     32  * tested on macppc only so far
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: r128fb.c,v 1.2 2007/11/23 20:12:54 macallan Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/device.h>
     42 #include <sys/malloc.h>
     43 #include <sys/lwp.h>
     44 #include <sys/kauth.h>
     45 
     46 #include <uvm/uvm_extern.h>
     47 
     48 #include <dev/videomode/videomode.h>
     49 
     50 #include <dev/pci/pcivar.h>
     51 #include <dev/pci/pcireg.h>
     52 #include <dev/pci/pcidevs.h>
     53 #include <dev/pci/pciio.h>
     54 #include <dev/pci/r128fbreg.h>
     55 
     56 #include <dev/wscons/wsdisplayvar.h>
     57 #include <dev/wscons/wsconsio.h>
     58 #include <dev/wsfont/wsfont.h>
     59 #include <dev/rasops/rasops.h>
     60 #include <dev/wscons/wsdisplay_vconsvar.h>
     61 
     62 #include <dev/i2c/i2cvar.h>
     63 
     64 struct r128fb_softc {
     65 	device_t sc_dev;
     66 
     67 	pci_chipset_tag_t sc_pc;
     68 	pcitag_t sc_pcitag;
     69 
     70 	bus_space_tag_t sc_memt;
     71 	bus_space_tag_t sc_iot;
     72 
     73 	bus_space_handle_t sc_fbh;
     74 	bus_space_handle_t sc_regh;
     75 	bus_addr_t sc_fb, sc_reg;
     76 	bus_size_t sc_fbsize, sc_regsize;
     77 
     78 	int sc_width, sc_height, sc_depth, sc_stride;
     79 	int sc_locked;
     80 	void *sc_fbaddr;
     81 	struct vcons_screen sc_console_screen;
     82 	struct wsscreen_descr sc_defaultscreen_descr;
     83 	const struct wsscreen_descr *sc_screens[1];
     84 	struct wsscreen_list sc_screenlist;
     85 	struct vcons_data vd;
     86 	int sc_mode;
     87 	u_char sc_cmap_red[256];
     88 	u_char sc_cmap_green[256];
     89 	u_char sc_cmap_blue[256];
     90 	/* engine stuff */
     91 	uint32_t sc_master_cntl;
     92 };
     93 
     94 static int	r128fb_match(device_t, cfdata_t, void *);
     95 static void	r128fb_attach(device_t, device_t, void *);
     96 
     97 CFATTACH_DECL_NEW(r128fb, sizeof(struct r128fb_softc),
     98     r128fb_match, r128fb_attach, NULL, NULL);
     99 
    100 extern const u_char rasops_cmap[768];
    101 
    102 static int	r128fb_ioctl(void *, void *, u_long, void *, int,
    103 			     struct lwp *);
    104 static paddr_t	r128fb_mmap(void *, void *, off_t, int);
    105 static void	r128fb_init_screen(void *, struct vcons_screen *, int, long *);
    106 
    107 static int	r128fb_putcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
    108 static int 	r128fb_getcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
    109 static void	r128fb_restore_palette(struct r128fb_softc *);
    110 static int 	r128fb_putpalreg(struct r128fb_softc *, uint8_t, uint8_t,
    111 			    uint8_t, uint8_t);
    112 
    113 static void	r128fb_init(struct r128fb_softc *);
    114 static void	r128fb_flush_engine(struct r128fb_softc *);
    115 static void	r128fb_rectfill(struct r128fb_softc *, int, int, int, int,
    116 			    uint32_t);
    117 static void	r128fb_bitblt(struct r128fb_softc *, int, int, int, int, int,
    118 			    int, int);
    119 
    120 static void	r128fb_cursor(void *, int, int, int);
    121 #if 0
    122 static void	r128fb_putchar(void *, int, int, u_int, long);
    123 #endif
    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 struct wsdisplay_accessops r128fb_accessops = {
    130 	r128fb_ioctl,
    131 	r128fb_mmap,
    132 	NULL,	/* alloc_screen */
    133 	NULL,	/* free_screen */
    134 	NULL,	/* show_screen */
    135 	NULL, 	/* load_font */
    136 	NULL,	/* pollc */
    137 	NULL	/* scroll */
    138 };
    139 
    140 static inline void
    141 r128fb_wait(struct r128fb_softc *sc, int slots)
    142 {
    143 	uint32_t reg;
    144 
    145 	do {
    146 		reg = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    147 		    R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK);
    148 	} while (reg <= slots);
    149 }
    150 
    151 static void
    152 r128fb_flush_engine(struct r128fb_softc *sc)
    153 {
    154 	uint32_t reg;
    155 
    156 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT);
    157 	reg |= R128_PC_FLUSH_ALL;
    158 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT, reg);
    159 	do {
    160 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    161 		    R128_PC_NGUI_CTLSTAT);
    162 	} while (reg & R128_PC_BUSY);
    163 }
    164 
    165 static int
    166 r128fb_match(device_t parent, cfdata_t match, void *aux)
    167 {
    168 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    169 
    170 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    171 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    172 		return 0;
    173 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI)
    174 		return 0;
    175 
    176 	/* only card tested on so far - likely need a list */
    177 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE1AGP4XT)
    178 		return 100;
    179 	return (0);
    180 }
    181 
    182 static void
    183 r128fb_attach(device_t parent, device_t self, void *aux)
    184 {
    185 	struct r128fb_softc	*sc = device_private(self);
    186 	struct pci_attach_args	*pa = aux;
    187 	struct rasops_info	*ri;
    188 	char devinfo[256];
    189 	struct wsemuldisplaydev_attach_args aa;
    190 	prop_dictionary_t	dict;
    191 	unsigned long		defattr;
    192 	bool			is_console;
    193 	int i, j;
    194 
    195 	sc->sc_pc = pa->pa_pc;
    196 	sc->sc_pcitag = pa->pa_tag;
    197 	sc->sc_memt = pa->pa_memt;
    198 	sc->sc_iot = pa->pa_iot;
    199 	sc->sc_dev = self;
    200 
    201 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    202 	aprint_normal(": %s\n", devinfo);
    203 
    204 	/* fill in parameters from properties */
    205 	dict = device_properties(self);
    206 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    207 		aprint_error("%s: no width property\n", device_xname(self));
    208 		return;
    209 	}
    210 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    211 		aprint_error("%s: no height property\n", device_xname(self));
    212 		return;
    213 	}
    214 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    215 		aprint_error("%s: no depth property\n", device_xname(self));
    216 		return;
    217 	}
    218 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) {
    219 		aprint_error("%s: no linebytes property\n",
    220 		    device_xname(self));
    221 		return;
    222 	}
    223 
    224 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    225 
    226 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM,
    227 	    BUS_SPACE_MAP_LINEAR,
    228 	    &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
    229 		aprint_error("%s: failed to map the frame buffer.\n",
    230 		    device_xname(sc->sc_dev));
    231 	}
    232 	sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
    233 
    234 	if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, 0,
    235 	    &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    236 		aprint_error("%s: failed to map registers.\n",
    237 		    device_xname(sc->sc_dev));
    238 	}
    239 
    240 	/*
    241 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
    242 	 * but as far as I know there are no mach64 with 64bit BARs
    243 	 */
    244 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    245 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    246 
    247 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    248 		"default",
    249 		0, 0,
    250 		NULL,
    251 		8, 16,
    252 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    253 		NULL
    254 	};
    255 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    256 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    257 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    258 	sc->sc_locked = 0;
    259 
    260 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    261 	    &r128fb_accessops);
    262 	sc->vd.init_screen = r128fb_init_screen;
    263 
    264 	/* init engine here */
    265 	r128fb_init(sc);
    266 
    267 	ri = &sc->sc_console_screen.scr_ri;
    268 
    269 	if (is_console) {
    270 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    271 		    &defattr);
    272 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    273 
    274 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    275 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    276 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    277 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    278 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    279 		    defattr);
    280 	} else {
    281 		/*
    282 		 * since we're not the console we can postpone the rest
    283 		 * until someone actually allocates a screen for us
    284 		 */
    285 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    286 	}
    287 
    288 	j = 0;
    289 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    290 
    291 		sc->sc_cmap_red[i] = rasops_cmap[j];
    292 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    293 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    294 		r128fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    295 		    rasops_cmap[j + 2]);
    296 		j += 3;
    297 	}
    298 
    299 	r128fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    300 	    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    301 
    302 	aa.console = is_console;
    303 	aa.scrdata = &sc->sc_screenlist;
    304 	aa.accessops = &r128fb_accessops;
    305 	aa.accesscookie = &sc->vd;
    306 
    307 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    308 
    309 }
    310 
    311 static int
    312 r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    313 	struct lwp *l)
    314 {
    315 	struct vcons_data *vd = v;
    316 	struct r128fb_softc *sc = vd->cookie;
    317 	struct wsdisplay_fbinfo *wdf;
    318 	struct vcons_screen *ms = vd->active;
    319 
    320 	switch (cmd) {
    321 
    322 		case WSDISPLAYIO_GTYPE:
    323 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    324 			return 0;
    325 
    326 		/* PCI config read/write passthrough. */
    327 		case PCI_IOC_CFGREAD:
    328 		case PCI_IOC_CFGWRITE:
    329 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    330 			    cmd, data, flag, l));
    331 
    332 		case WSDISPLAYIO_GINFO:
    333 			if (ms == NULL)
    334 				return ENODEV;
    335 			wdf = (void *)data;
    336 			wdf->height = ms->scr_ri.ri_height;
    337 			wdf->width = ms->scr_ri.ri_width;
    338 			wdf->depth = ms->scr_ri.ri_depth;
    339 			wdf->cmsize = 256;
    340 			return 0;
    341 
    342 		case WSDISPLAYIO_GETCMAP:
    343 			return r128fb_getcmap(sc,
    344 			    (struct wsdisplay_cmap *)data);
    345 
    346 		case WSDISPLAYIO_PUTCMAP:
    347 			return r128fb_putcmap(sc,
    348 			    (struct wsdisplay_cmap *)data);
    349 
    350 		case WSDISPLAYIO_LINEBYTES:
    351 			*(u_int *)data = sc->sc_stride;
    352 			return 0;
    353 
    354 		case WSDISPLAYIO_SMODE:
    355 			{
    356 				int new_mode = *(int*)data;
    357 
    358 				/* notify the bus backend */
    359 				if (new_mode != sc->sc_mode) {
    360 					sc->sc_mode = new_mode;
    361 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    362 						r128fb_restore_palette(sc);
    363 						vcons_redraw_screen(ms);
    364 					}
    365 				}
    366 			}
    367 			return 0;
    368 	}
    369 	return EPASSTHROUGH;
    370 }
    371 
    372 static paddr_t
    373 r128fb_mmap(void *v, void *vs, off_t offset, int prot)
    374 {
    375 	struct vcons_data *vd = v;
    376 	struct r128fb_softc *sc = vd->cookie;
    377 	struct lwp *me;
    378 	paddr_t pa;
    379 
    380 	/* 'regular' framebuffer mmap()ing */
    381 	if (offset < sc->sc_fbsize) {
    382 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    383 		    BUS_SPACE_MAP_LINEAR);
    384 		return pa;
    385 	}
    386 
    387 	/*
    388 	 * restrict all other mappings to processes with superuser privileges
    389 	 * or the kernel itself
    390 	 */
    391 	me = curlwp;
    392 	if (me != NULL) {
    393 		if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
    394 		    NULL) != 0) {
    395 			aprint_normal("%s: mmap() rejected.\n",
    396 			    device_xname(sc->sc_dev));
    397 			return -1;
    398 		}
    399 	}
    400 
    401 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    402 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    403 		    BUS_SPACE_MAP_LINEAR);
    404 		return pa;
    405 	}
    406 
    407 	if ((offset >= sc->sc_reg) &&
    408 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    409 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    410 		    BUS_SPACE_MAP_LINEAR);
    411 		return pa;
    412 	}
    413 
    414 #ifdef macppc
    415 	/* allow mapping of IO space */
    416 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
    417 		pa = bus_space_mmap(sc->sc_iot, offset - 0xf2000000, 0, prot,
    418 		    BUS_SPACE_MAP_LINEAR);
    419 		return pa;
    420 	}
    421 #endif
    422 
    423 #ifdef OFB_ALLOW_OTHERS
    424 	if (offset >= 0x80000000) {
    425 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    426 		    BUS_SPACE_MAP_LINEAR);
    427 		return pa;
    428 	}
    429 #endif
    430 	return -1;
    431 }
    432 
    433 static void
    434 r128fb_init_screen(void *cookie, struct vcons_screen *scr,
    435     int existing, long *defattr)
    436 {
    437 	struct r128fb_softc *sc = cookie;
    438 	struct rasops_info *ri = &scr->scr_ri;
    439 
    440 	ri->ri_depth = sc->sc_depth;
    441 	ri->ri_width = sc->sc_width;
    442 	ri->ri_height = sc->sc_height;
    443 	ri->ri_stride = sc->sc_stride;
    444 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    445 
    446 	ri->ri_bits = (char *)sc->sc_fbaddr;
    447 
    448 	if (existing) {
    449 		ri->ri_flg |= RI_CLEAR;
    450 	}
    451 
    452 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
    453 	ri->ri_caps = WSSCREEN_WSCOLORS;
    454 
    455 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    456 		    sc->sc_width / ri->ri_font->fontwidth);
    457 
    458 	ri->ri_hw = scr;
    459 	ri->ri_ops.copyrows = r128fb_copyrows;
    460 	ri->ri_ops.copycols = r128fb_copycols;
    461 	ri->ri_ops.eraserows = r128fb_eraserows;
    462 	ri->ri_ops.erasecols = r128fb_erasecols;
    463 	ri->ri_ops.cursor = r128fb_cursor;
    464 #if 0
    465 	ri->ri_ops.putchar = r128fb_putchar;
    466 #endif
    467 }
    468 
    469 static int
    470 r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
    471 {
    472 	u_char *r, *g, *b;
    473 	u_int index = cm->index;
    474 	u_int count = cm->count;
    475 	int i, error;
    476 	u_char rbuf[256], gbuf[256], bbuf[256];
    477 
    478 #ifdef R128FB_DEBUG
    479 	aprint_debug("putcmap: %d %d\n",index, count);
    480 #endif
    481 	if (cm->index >= 256 || cm->count > 256 ||
    482 	    (cm->index + cm->count) > 256)
    483 		return EINVAL;
    484 	error = copyin(cm->red, &rbuf[index], count);
    485 	if (error)
    486 		return error;
    487 	error = copyin(cm->green, &gbuf[index], count);
    488 	if (error)
    489 		return error;
    490 	error = copyin(cm->blue, &bbuf[index], count);
    491 	if (error)
    492 		return error;
    493 
    494 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    495 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    496 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    497 
    498 	r = &sc->sc_cmap_red[index];
    499 	g = &sc->sc_cmap_green[index];
    500 	b = &sc->sc_cmap_blue[index];
    501 
    502 	for (i = 0; i < count; i++) {
    503 		r128fb_putpalreg(sc, index, *r, *g, *b);
    504 		index++;
    505 		r++, g++, b++;
    506 	}
    507 	return 0;
    508 }
    509 
    510 static int
    511 r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
    512 {
    513 	u_int index = cm->index;
    514 	u_int count = cm->count;
    515 	int error;
    516 
    517 	if (index >= 255 || count > 256 || index + count > 256)
    518 		return EINVAL;
    519 
    520 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    521 	if (error)
    522 		return error;
    523 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    524 	if (error)
    525 		return error;
    526 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    527 	if (error)
    528 		return error;
    529 
    530 	return 0;
    531 }
    532 
    533 static void
    534 r128fb_restore_palette(struct r128fb_softc *sc)
    535 {
    536 	int i;
    537 
    538 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    539 		r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
    540 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    541 	}
    542 }
    543 
    544 static int
    545 r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    546     uint8_t b)
    547 {
    548 	uint32_t reg;
    549 
    550 	/* whack the DAC */
    551 	reg = (r << 16) | (g << 8) | b;
    552 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx);
    553 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg);
    554 	return 0;
    555 }
    556 
    557 static void
    558 r128fb_init(struct r128fb_softc *sc)
    559 {
    560 	uint32_t datatype;
    561 
    562 	r128fb_flush_engine(sc);
    563 
    564 	r128fb_wait(sc, 8);
    565 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0);
    566 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0);
    567 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH,
    568 	    sc->sc_stride >> 3);
    569 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0);
    570 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
    571 	    R128_DEFAULT_SC_BOTTOM_RIGHT,
    572 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
    573 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0);
    574 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT,
    575 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
    576 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE,
    577 	    R128_HOST_BIG_ENDIAN_EN);
    578 
    579 	r128fb_wait(sc, 5);
    580 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH,
    581 	    sc->sc_stride >> 3);
    582 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH,
    583 	    sc->sc_stride >> 3);
    584 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0);
    585 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0);
    586 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK,
    587 	    0xffffffff);
    588 
    589 	switch (sc->sc_depth) {
    590 		case 8:
    591 			datatype = R128_GMC_DST_8BPP_CI;
    592 			break;
    593 		case 15:
    594 			datatype = R128_GMC_DST_15BPP;
    595 			break;
    596 		case 16:
    597 			datatype = R128_GMC_DST_16BPP;
    598 			break;
    599 		case 24:
    600 			datatype = R128_GMC_DST_24BPP;
    601 			break;
    602 		case 32:
    603 			datatype = R128_GMC_DST_32BPP;
    604 			break;
    605 		default:
    606 			aprint_error("%s: unsupported depth %d\n",
    607 			    device_xname(sc->sc_dev), sc->sc_depth);
    608 			return;
    609 	}
    610 	sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS |
    611 	    R128_GMC_AUX_CLIP_DIS | datatype;
    612 
    613 	r128fb_flush_engine(sc);
    614 }
    615 
    616 static void
    617 r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he,
    618      uint32_t colour)
    619 {
    620 
    621 	r128fb_wait(sc, 6);
    622 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
    623 	    R128_GMC_BRUSH_SOLID_COLOR |
    624 	    R128_GMC_SRC_DATATYPE_COLOR |
    625 	    R128_ROP3_P |
    626 	    sc->sc_master_cntl);
    627 
    628 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR,
    629 	    colour);
    630 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL,
    631 	    R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
    632 	/* now feed it coordinates */
    633 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
    634 	    (x << 16) | y);
    635 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
    636 	    (wi << 16) | he);
    637 	r128fb_flush_engine(sc);
    638 }
    639 
    640 static void
    641 r128fb_bitblt(struct r128fb_softc *sc, int xs, int ys, int xd, int yd,
    642     int wi, int he, int rop)
    643 {
    644 	uint32_t dp_cntl = 0;
    645 
    646 	r128fb_wait(sc, 5);
    647 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
    648 	    R128_GMC_BRUSH_SOLID_COLOR |
    649 	    R128_GMC_SRC_DATATYPE_COLOR |
    650 	    rop |
    651 	    R128_DP_SRC_SOURCE_MEMORY |
    652 	    sc->sc_master_cntl);
    653 
    654 	if (yd <= ys) {
    655 		dp_cntl = R128_DST_Y_TOP_TO_BOTTOM;
    656 	} else {
    657 		ys += he - 1;
    658 		yd += he - 1;
    659 	}
    660 	if (xd <= xs) {
    661 		dp_cntl |= R128_DST_X_LEFT_TO_RIGHT;
    662 	} else {
    663 		xs += wi - 1;
    664 		xd += wi - 1;
    665 	}
    666 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl);
    667 
    668 	/* now feed it coordinates */
    669 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y,
    670 	    (xs << 16) | ys);
    671 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
    672 	    (xd << 16) | yd);
    673 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
    674 	    (wi << 16) | he);
    675 	r128fb_flush_engine(sc);
    676 }
    677 
    678 static void
    679 r128fb_cursor(void *cookie, int on, int row, int col)
    680 {
    681 	struct rasops_info *ri = cookie;
    682 	struct vcons_screen *scr = ri->ri_hw;
    683 	struct r128fb_softc *sc = scr->scr_cookie;
    684 	int x, y, wi, he;
    685 
    686 	wi = ri->ri_font->fontwidth;
    687 	he = ri->ri_font->fontheight;
    688 
    689 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    690 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    691 		y = ri->ri_crow * he + ri->ri_yorigin;
    692 		if (ri->ri_flg & RI_CURSOR) {
    693 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
    694 			ri->ri_flg &= ~RI_CURSOR;
    695 		}
    696 		ri->ri_crow = row;
    697 		ri->ri_ccol = col;
    698 		if (on) {
    699 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    700 			y = ri->ri_crow * he + ri->ri_yorigin;
    701 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
    702 			ri->ri_flg |= RI_CURSOR;;
    703 		}
    704 	} else {
    705 		scr->scr_ri.ri_crow = row;
    706 		scr->scr_ri.ri_ccol = col;
    707 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    708 	}
    709 
    710 }
    711 
    712 #if 0
    713 static void
    714 r128fb_putchar(void *cookie, int row, int col, u_int c, long attr)
    715 {
    716 }
    717 #endif
    718 
    719 static void
    720 r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    721 {
    722 	struct rasops_info *ri = cookie;
    723 	struct vcons_screen *scr = ri->ri_hw;
    724 	struct r128fb_softc *sc = scr->scr_cookie;
    725 	int32_t xs, xd, y, width, height;
    726 
    727 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    728 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    729 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    730 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    731 		width = ri->ri_font->fontwidth * ncols;
    732 		height = ri->ri_font->fontheight;
    733 		r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S);
    734 	}
    735 }
    736 
    737 static void
    738 r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    739 {
    740 	struct rasops_info *ri = cookie;
    741 	struct vcons_screen *scr = ri->ri_hw;
    742 	struct r128fb_softc *sc = scr->scr_cookie;
    743 	int32_t x, y, width, height, fg, bg, ul;
    744 
    745 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    746 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    747 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    748 		width = ri->ri_font->fontwidth * ncols;
    749 		height = ri->ri_font->fontheight;
    750 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    751 
    752 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    753 	}
    754 }
    755 
    756 static void
    757 r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    758 {
    759 	struct rasops_info *ri = cookie;
    760 	struct vcons_screen *scr = ri->ri_hw;
    761 	struct r128fb_softc *sc = scr->scr_cookie;
    762 	int32_t x, ys, yd, width, height;
    763 
    764 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    765 		x = ri->ri_xorigin;
    766 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    767 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    768 		width = ri->ri_emuwidth;
    769 		height = ri->ri_font->fontheight*nrows;
    770 		r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S);
    771 	}
    772 }
    773 
    774 static void
    775 r128fb_eraserows(void *cookie, int row, int nrows, long fillattr)
    776 {
    777 	struct rasops_info *ri = cookie;
    778 	struct vcons_screen *scr = ri->ri_hw;
    779 	struct r128fb_softc *sc = scr->scr_cookie;
    780 	int32_t x, y, width, height, fg, bg, ul;
    781 
    782 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    783 		x = ri->ri_xorigin;
    784 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    785 		width = ri->ri_emuwidth;
    786 		height = ri->ri_font->fontheight * nrows;
    787 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    788 
    789 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    790 	}
    791 }
    792 
    793