Home | History | Annotate | Line # | Download | only in pci
      1 /*	$NetBSD: wcfb.c,v 1.21 2022/07/17 10:28:09 riastradh Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2007, 2008, 2009 Miodrag Vallat.
      5  *               2010 Michael Lorenz
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 /* a driver for (some) 3DLabs Wildcat cards, based on OpenBSD's ifb driver */
     21 
     22 #include <sys/cdefs.h>
     23 __KERNEL_RCSID(0, "$NetBSD: wcfb.c,v 1.21 2022/07/17 10:28:09 riastradh Exp $");
     24 
     25 #include <sys/param.h>
     26 #include <sys/systm.h>
     27 #include <sys/kernel.h>
     28 #include <sys/device.h>
     29 #include <sys/proc.h>
     30 #include <sys/mutex.h>
     31 #include <sys/ioctl.h>
     32 #include <sys/kernel.h>
     33 #include <sys/systm.h>
     34 #include <sys/kauth.h>
     35 #include <sys/kmem.h>
     36 
     37 #include <dev/pci/pcidevs.h>
     38 #include <dev/pci/pcireg.h>
     39 #include <dev/pci/pcivar.h>
     40 #include <dev/pci/pciio.h>
     41 #include <dev/pci/wcfbreg.h>
     42 
     43 #include <dev/wscons/wsdisplayvar.h>
     44 #include <dev/wscons/wsconsio.h>
     45 #include <dev/wsfont/wsfont.h>
     46 #include <dev/rasops/rasops.h>
     47 #include <dev/wscons/wsdisplay_vconsvar.h>
     48 #include <dev/pci/wsdisplay_pci.h>
     49 
     50 #include "opt_wsfb.h"
     51 #include "opt_wsdisplay_compat.h"
     52 
     53 #ifdef WCFB_DEBUG
     54 # define DPRINTF printf
     55 #else
     56 # define DPRINTF while (0) printf
     57 #endif
     58 
     59 static int	wcfb_match(device_t, cfdata_t, void *);
     60 static void	wcfb_attach(device_t, device_t, void *);
     61 static int	wcfb_ioctl(void *, void *, u_long, void *, int,
     62 		    struct lwp *);
     63 static paddr_t	wcfb_mmap(void *, void *, off_t, int);
     64 
     65 struct wcfb_softc {
     66 	device_t sc_dev;
     67 
     68 	pci_chipset_tag_t sc_pc;
     69 	pcitag_t sc_pcitag;
     70 
     71 	bus_space_tag_t sc_memt;
     72 	bus_space_tag_t sc_regt, sc_wtft;
     73 	bus_space_tag_t sc_iot;
     74 
     75 	bus_space_handle_t sc_fbh;
     76 	bus_space_handle_t sc_regh;
     77 	bus_addr_t sc_fb, sc_reg;
     78 	bus_size_t sc_fbsize, sc_regsize;
     79 
     80 	int sc_width, sc_height, sc_stride;
     81 	int sc_locked;
     82 	uint8_t *sc_fbaddr, *sc_fb0, *sc_fb1, *sc_shadow;
     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, sc_dpms;
     89 	u_char sc_cmap_red[256];
     90 	u_char sc_cmap_green[256];
     91 	u_char sc_cmap_blue[256];
     92 	uint32_t sc_fb0off, sc_fb1off, sc_fb8size;
     93 
     94 	void (*copycols)(void *, int, int, int, int);
     95 	void (*erasecols)(void *, int, int, int, long);
     96 	void (*copyrows)(void *, int, int, int);
     97 	void (*eraserows)(void *, int, int, long);
     98 	void (*putchar)(void *, int, int, u_int, long);
     99 	void (*cursor)(void *, int, int, int);
    100 	int sc_is_jfb;
    101 };
    102 
    103 static void	wcfb_init_screen(void *, struct vcons_screen *, int, long *);
    104 
    105 CFATTACH_DECL_NEW(wcfb, sizeof(struct wcfb_softc),
    106     wcfb_match, wcfb_attach, NULL, NULL);
    107 
    108 struct wsdisplay_accessops wcfb_accessops = {
    109 	wcfb_ioctl,
    110 	wcfb_mmap,
    111 	NULL,	/* alloc_screen */
    112 	NULL,	/* free_screen */
    113 	NULL,	/* show_screen */
    114 	NULL, 	/* load_font */
    115 	NULL,	/* pollc */
    116 	NULL	/* scroll */
    117 };
    118 
    119 static void	wcfb_putchar(void *, int, int, u_int, long);
    120 static void	wcfb_cursor(void *, int, int, int);
    121 static void	wcfb_copycols(void *, int, int, int, int);
    122 static void	wcfb_erasecols(void *, int, int, int, long);
    123 static void	wcfb_copyrows(void *, int, int, int);
    124 static void	wcfb_eraserows(void *, int, int, long);
    125 
    126 static void	wcfb_acc_putchar(void *, int, int, u_int, long);
    127 static void	wcfb_acc_cursor(void *, int, int, int);
    128 static void	wcfb_acc_copycols(void *, int, int, int, int);
    129 static void	wcfb_acc_erasecols(void *, int, int, int, long);
    130 static void	wcfb_acc_copyrows(void *, int, int, int);
    131 static void	wcfb_acc_eraserows(void *, int, int, long);
    132 
    133 static void 	wcfb_putpalreg(struct wcfb_softc *, int, int, int, int);
    134 
    135 static void	wcfb_bitblt(struct wcfb_softc *, int, int, int, int, int,
    136 			int, uint32_t);
    137 static void	wcfb_rectfill(struct wcfb_softc *, int, int, int, int, int);
    138 static void	wcfb_rop_common(struct wcfb_softc *, bus_addr_t, int, int, int,
    139 			int, int, int, uint32_t, int32_t);
    140 static void	wcfb_rop_jfb(struct wcfb_softc *, int, int, int, int, int, int,
    141 			uint32_t, int32_t);
    142 static int	wcfb_rop_wait(struct wcfb_softc *);
    143 
    144 static int
    145 wcfb_match(device_t parent, cfdata_t match, void *aux)
    146 {
    147 	struct pci_attach_args *pa = aux;
    148 
    149 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3DLABS &&
    150 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_WILDCAT5110)
    151 		return 100;
    152 
    153 	return 0;
    154 }
    155 
    156 static void
    157 wcfb_attach(device_t parent, device_t self, void *aux)
    158 {
    159 	struct wcfb_softc *sc = device_private(self);
    160 	struct pci_attach_args *pa = aux;
    161 	struct rasops_info	*ri;
    162 	prop_dictionary_t	dict;
    163 	struct wsemuldisplaydev_attach_args aa;
    164 	int 			i, j;
    165 	uint32_t		reg;
    166 	unsigned long		defattr;
    167 	bool			is_console = 0;
    168 	uint32_t		sub;
    169 
    170 	sc->sc_dev = self;
    171 	sc->putchar = NULL;
    172 	pci_aprint_devinfo(pa, NULL);
    173 
    174 	dict = device_properties(self);
    175 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    176 #ifndef WCFB_DEBUG
    177 	if (!is_console) return;
    178 #endif
    179 	sc->sc_memt = pa->pa_memt;
    180 	sc->sc_iot = pa->pa_iot;
    181 	sc->sc_pc = pa->pa_pc;
    182 	sc->sc_pcitag = pa->pa_tag;
    183 
    184 	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM, 0,
    185 	    &sc->sc_regt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    186 		aprint_error("%s: failed to map registers.\n",
    187 		    device_xname(sc->sc_dev));
    188 	}
    189 
    190 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM,
    191 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
    192 	    &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
    193 		aprint_error("%s: failed to map framebuffer.\n",
    194 		    device_xname(sc->sc_dev));
    195 	}
    196 
    197 	sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
    198 #ifdef DEBUG
    199 	memset(sc->sc_fbaddr, 0, sc->sc_fbsize);
    200 #endif
    201 	sc->sc_fb0off =
    202 	    bus_space_read_4(sc->sc_regt, sc->sc_regh,
    203 	        WC_FB8_ADDR0) - sc->sc_fb;
    204 	sc->sc_fb0 = sc->sc_fbaddr + sc->sc_fb0off;
    205 	sc->sc_fb1off =
    206 	    bus_space_read_4(sc->sc_regt, sc->sc_regh,
    207 	        WC_FB8_ADDR1) - sc->sc_fb;
    208 	sc->sc_fb1 = sc->sc_fbaddr + sc->sc_fb1off;
    209 	sc->sc_fb8size = 2 * (sc->sc_fb1off - sc->sc_fb0off);
    210 
    211 	sub = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_SUBSYS_ID_REG);
    212 	aprint_normal("subsys: %08x\n", sub);
    213 	switch (sub) {
    214 		case WC_XVR1200:
    215 			sc->sc_is_jfb = 1;
    216 			break;
    217 		default:
    218 			sc->sc_is_jfb = 0;
    219 	}
    220 
    221 	reg = bus_space_read_4(sc->sc_regt, sc->sc_regh, WC_RESOLUTION);
    222 	sc->sc_height = (reg >> 16) + 1;
    223 #ifdef WCFB_DEBUG
    224 	sc->sc_height -= 200;
    225 #endif
    226 	sc->sc_width = (reg & 0xffff) + 1;
    227 	sc->sc_stride = 1 <<
    228 	    ((bus_space_read_4(sc->sc_regt, sc->sc_regh, WC_CONFIG) &
    229 	      0x00ff0000) >> 16);
    230 	aprint_normal_dev(self, "%d x %d, %d\n",
    231 	    sc->sc_width, sc->sc_height, sc->sc_stride);
    232 
    233 	if (sc->sc_is_jfb == 0) {
    234 		sc->sc_shadow = kmem_alloc(sc->sc_stride * sc->sc_height,
    235 		    KM_SLEEP);
    236 	}
    237 
    238 	for (i = 0x40; i < 0x100; i += 16) {
    239 		aprint_normal("%04x:", i);
    240 		for (j = 0; j < 16; j += 4) {
    241 			aprint_normal(" %08x", bus_space_read_4(sc->sc_regt,
    242 			    sc->sc_regh, 0x8000 + i + j));
    243 		}
    244 		aprint_normal("\n");
    245 	}
    246 
    247 	/* make sure video output is on */
    248 	bus_space_write_4(sc->sc_regt, sc->sc_regh, WC_DPMS_STATE, WC_DPMS_ON);
    249 	sc->sc_dpms = WSDISPLAYIO_VIDEO_ON;
    250 
    251 #if 0
    252 	/* testing & debugging voodoo */
    253 	memset(sc->sc_fb0, 0x01, 0x00100000);
    254 	memset(sc->sc_fb1, 0x00, 0x00100000);
    255 	wcfb_rop_wait(sc);
    256 	wcfb_rop_jfb(sc, 0, 0, 0, 0, 600, 600, WC_ROP_SET, 0xffffffff);
    257 	wcfb_rop_wait(sc);
    258 	delay(4000000);
    259 	bus_space_write_4(sc->sc_regt, sc->sc_regh, WC_FB8_ADDR1,
    260 	    bus_space_read_4(sc->sc_regt, sc->sc_regh, WC_FB8_ADDR0));
    261 	delay(8000000);
    262 #endif
    263 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    264 		"default",
    265 		0, 0,
    266 		NULL,
    267 		8, 16,
    268 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    269 		NULL
    270 	};
    271 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    272 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    273 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    274 	sc->sc_locked = 0;
    275 
    276 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    277 	    &wcfb_accessops);
    278 	sc->vd.init_screen = wcfb_init_screen;
    279 
    280 	/* init engine here */
    281 #if 0
    282 	wcfb_init(sc);
    283 #endif
    284 
    285 	ri = &sc->sc_console_screen.scr_ri;
    286 
    287 	j = 0;
    288 	for (i = 0; i < 256; i++) {
    289 
    290 		sc->sc_cmap_red[i] = rasops_cmap[j];
    291 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    292 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    293 		wcfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    294 		    rasops_cmap[j + 2]);
    295 		j += 3;
    296 	}
    297 
    298 	if (is_console) {
    299 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    300 		    &defattr);
    301 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    302 
    303 		if (sc->sc_is_jfb) {
    304 			wcfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    305 				ri->ri_devcmap[(defattr >> 16) & 0xff]);
    306 		} else {
    307 			memset(sc->sc_fb0,
    308 			    ri->ri_devcmap[(defattr >> 16) & 0xff],
    309 			    sc->sc_stride * sc->sc_height);
    310 			memset(sc->sc_fb1,
    311 			    ri->ri_devcmap[(defattr >> 16) & 0xff],
    312 			    sc->sc_stride * sc->sc_height);
    313 		}
    314 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    315 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    316 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    317 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    318 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    319 		    defattr);
    320 		vcons_replay_msgbuf(&sc->sc_console_screen);
    321 	} else {
    322 		/*
    323 		 * since we're not the console we can postpone the rest
    324 		 * until someone actually allocates a screen for us
    325 		 */
    326 		memset(sc->sc_fb0, WS_DEFAULT_BG,
    327 		    sc->sc_stride * sc->sc_height);
    328 		memset(sc->sc_fb1, WS_DEFAULT_BG,
    329 		    sc->sc_stride * sc->sc_height);
    330 		return;
    331 	}
    332 
    333 	aa.console = is_console;
    334 	aa.scrdata = &sc->sc_screenlist;
    335 	aa.accessops = &wcfb_accessops;
    336 	aa.accesscookie = &sc->vd;
    337 
    338 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE);
    339 }
    340 
    341 static int
    342 wcfb_putcmap(struct wcfb_softc *sc, struct wsdisplay_cmap *cm)
    343 {
    344 	u_char *r, *g, *b;
    345 	u_int index = cm->index;
    346 	u_int count = cm->count;
    347 	int i, error;
    348 	u_char rbuf[256], gbuf[256], bbuf[256];
    349 
    350 	if (cm->index >= 256 || cm->count > 256 ||
    351 	    (cm->index + cm->count) > 256)
    352 		return EINVAL;
    353 	error = copyin(cm->red, &rbuf[index], count);
    354 	if (error)
    355 		return error;
    356 	error = copyin(cm->green, &gbuf[index], count);
    357 	if (error)
    358 		return error;
    359 	error = copyin(cm->blue, &bbuf[index], count);
    360 	if (error)
    361 		return error;
    362 
    363 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    364 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    365 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    366 
    367 	r = &sc->sc_cmap_red[index];
    368 	g = &sc->sc_cmap_green[index];
    369 	b = &sc->sc_cmap_blue[index];
    370 
    371 	for (i = 0; i < count; i++) {
    372 		wcfb_putpalreg(sc, index, *r, *g, *b);
    373 		index++;
    374 		r++, g++, b++;
    375 	}
    376 	return 0;
    377 }
    378 
    379 static int
    380 wcfb_getcmap(struct wcfb_softc *sc, struct wsdisplay_cmap *cm)
    381 {
    382 	u_int index = cm->index;
    383 	u_int count = cm->count;
    384 	int error;
    385 
    386 	if (index >= 255 || count > 256 || index + count > 256)
    387 		return EINVAL;
    388 
    389 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    390 	if (error)
    391 		return error;
    392 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    393 	if (error)
    394 		return error;
    395 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    396 	if (error)
    397 		return error;
    398 
    399 	return 0;
    400 }
    401 
    402 static int
    403 wcfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    404     struct lwp *l)
    405 {
    406 	struct vcons_data *vd = v;
    407 	struct wcfb_softc *sc = vd->cookie;
    408 
    409 	switch (cmd) {
    410 	case WSDISPLAYIO_GTYPE:
    411 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    412 		return 0;
    413 
    414 	/* PCI config read/write passthrough. */
    415 	case PCI_IOC_CFGREAD:
    416 	case PCI_IOC_CFGWRITE:
    417 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    418 		    cmd, data, flag, l);
    419 
    420 	case WSDISPLAYIO_GET_BUSID:
    421 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
    422 		    sc->sc_pcitag, data);
    423 
    424 	case WSDISPLAYIO_SVIDEO: {
    425 		int new_mode = *(int*)data;
    426 		if (new_mode != sc->sc_dpms) {
    427 			sc->sc_dpms = new_mode;
    428 			bus_space_write_4(sc->sc_regt, sc->sc_regh,
    429 			     WC_DPMS_STATE,
    430 			     (new_mode == WSDISPLAYIO_VIDEO_ON) ?
    431 			      WC_DPMS_ON : WC_DPMS_STANDBY);
    432 		}
    433 		}
    434 		return 0;
    435 
    436 	case WSDISPLAYIO_GVIDEO:
    437 		*(int*)data = sc->sc_dpms;
    438 		return 0;
    439 
    440 	case WSDISPLAYIO_GETCMAP:
    441 		return wcfb_getcmap(sc,
    442 		    (struct wsdisplay_cmap *)data);
    443 
    444 	case WSDISPLAYIO_PUTCMAP:
    445 		return wcfb_putcmap(sc,
    446 		    (struct wsdisplay_cmap *)data);
    447 
    448 	case WSDISPLAYIO_GET_FBINFO: {
    449 		struct wsdisplayio_fbinfo *fbi = data;
    450 
    451 		fbi->fbi_fbsize = sc->sc_fb8size;
    452 		fbi->fbi_fboffset = 0;
    453 		fbi->fbi_width = sc->sc_width;
    454 		fbi->fbi_height = sc->sc_height;
    455 		fbi->fbi_bitsperpixel = 8;
    456 		fbi->fbi_stride = sc->sc_stride;
    457 		fbi->fbi_pixeltype = WSFB_CI;
    458 		fbi->fbi_subtype.fbi_cmapinfo.cmap_entries = 256;
    459 		fbi->fbi_flags = WSFB_VRAM_IS_SPLIT;
    460 		return 0;
    461 		}
    462 	}
    463 	return EPASSTHROUGH;
    464 }
    465 
    466 static paddr_t
    467 wcfb_mmap(void *v, void *vs, off_t offset, int prot)
    468 {
    469 	struct vcons_data *vd = v;
    470 	struct wcfb_softc *sc = vd->cookie;
    471 
    472 	/* XXX in theory the order is not fixed... */
    473 
    474 	if (offset < sc->sc_fb8size)
    475 		return bus_space_mmap(sc->sc_memt, sc->sc_fb + sc->sc_fb0off,
    476 		    offset, prot,
    477 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    478 	/*
    479 	 * restrict all other mappings to processes with superuser privileges
    480 	 * or the kernel itself
    481 	 */
    482 	if (kauth_authorize_machdep(kauth_cred_get(),
    483 	    KAUTH_MACHDEP_UNMANAGEDMEM,
    484 	    NULL, NULL, NULL, NULL) != 0) {
    485 		aprint_normal_dev(sc->sc_dev, "mmap() rejected.\n");
    486 		return -1;
    487 	}
    488 
    489 	/* may want to mmap() registers at some point */
    490 
    491 	return -1;
    492 }
    493 
    494 static void
    495 wcfb_init_screen(void *cookie, struct vcons_screen *scr,
    496     int existing, long *defattr)
    497 {
    498 	struct wcfb_softc *sc = cookie;
    499 	struct rasops_info *ri = &scr->scr_ri;
    500 
    501 	ri->ri_depth = 8;
    502 	ri->ri_width = sc->sc_width;
    503 	ri->ri_height = sc->sc_height;
    504 	ri->ri_stride = sc->sc_stride;
    505 	ri->ri_flg = RI_CENTER /*| RI_FULLCLEAR*/;
    506 
    507 	if (sc->sc_is_jfb) {
    508 		ri->ri_bits = sc->sc_fb0;
    509 	} else {
    510 		ri->ri_bits = sc->sc_shadow;
    511 	}
    512 	if (existing) {
    513 		ri->ri_flg |= RI_CLEAR;
    514 	}
    515 
    516 	rasops_init(ri, 0, 0);
    517 	ri->ri_caps = WSSCREEN_WSCOLORS;
    518 
    519 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    520 		    sc->sc_width / ri->ri_font->fontwidth);
    521 
    522 	ri->ri_hw = scr;
    523 	sc->putchar = ri->ri_ops.putchar;
    524 	sc->copyrows = ri->ri_ops.copyrows;
    525 	sc->eraserows = ri->ri_ops.eraserows;
    526 	sc->copycols = ri->ri_ops.copycols;
    527 	sc->erasecols = ri->ri_ops.erasecols;
    528 
    529 	if (sc->sc_is_jfb) {
    530 		ri->ri_ops.copyrows = wcfb_acc_copyrows;
    531 		ri->ri_ops.copycols = wcfb_acc_copycols;
    532 		ri->ri_ops.eraserows = wcfb_acc_eraserows;
    533 		ri->ri_ops.erasecols = wcfb_acc_erasecols;
    534 		ri->ri_ops.putchar = wcfb_acc_putchar;
    535 		ri->ri_ops.cursor = wcfb_acc_cursor;
    536 	} else {
    537 		ri->ri_ops.copyrows = wcfb_copyrows;
    538 		ri->ri_ops.copycols = wcfb_copycols;
    539 		ri->ri_ops.eraserows = wcfb_eraserows;
    540 		ri->ri_ops.erasecols = wcfb_erasecols;
    541 		ri->ri_ops.putchar = wcfb_putchar;
    542 		ri->ri_ops.cursor = wcfb_cursor;
    543 	}
    544 }
    545 
    546 static void
    547 wcfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    548 {
    549 	struct rasops_info *ri = cookie;
    550 	struct vcons_screen *scr = ri->ri_hw;
    551 	struct wcfb_softc *sc = scr->scr_cookie;
    552 	int offset = (ri->ri_yorigin + row * ri->ri_font->fontheight) *
    553 	    sc->sc_stride + ri->ri_xorigin + col * ri->ri_font->fontwidth;
    554 	uint8_t *from, *to0, *to1;
    555 	int i;
    556 
    557 	sc->putchar(ri, row, col, c, attr);
    558 	from = sc->sc_shadow + offset;
    559 	to0 = sc->sc_fb0 + offset;
    560 	to1 = sc->sc_fb1 + offset;
    561 	for (i = 0; i < ri->ri_font->fontheight; i++) {
    562 		memcpy(to0, from, ri->ri_font->fontwidth);
    563 		memcpy(to1, from, ri->ri_font->fontwidth);
    564 		to0 += sc->sc_stride;
    565 		to1 += sc->sc_stride;
    566 		from += sc->sc_stride;
    567 	}
    568 }
    569 
    570 static void
    571 wcfb_putpalreg(struct wcfb_softc *sc, int i, int r, int g, int b)
    572 {
    573 	uint32_t rgb;
    574 
    575 	bus_space_write_4(sc->sc_regt, sc->sc_regh, WC_CMAP_INDEX, i);
    576 	rgb = (b << 22) | (g << 12) | (r << 2);
    577 	bus_space_write_4(sc->sc_regt, sc->sc_regh, WC_CMAP_DATA, rgb);
    578 }
    579 
    580 static void
    581 wcfb_cursor(void *cookie, int on, int row, int col)
    582 {
    583 	struct rasops_info *ri = cookie;
    584 	struct vcons_screen *scr = ri->ri_hw;
    585 	struct wcfb_softc *sc = scr->scr_cookie;
    586 	int coffset;
    587 
    588 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    589 
    590 		if (ri->ri_flg & RI_CURSOR) {
    591 			/* remove cursor */
    592 			coffset = ri->ri_ccol + (ri->ri_crow * ri->ri_cols);
    593 			coffset += vcons_offset_to_zero(scr);
    594 			wcfb_putchar(cookie, ri->ri_crow,
    595 			    ri->ri_ccol, scr->scr_chars[coffset],
    596 			    scr->scr_attrs[coffset]);
    597 			ri->ri_flg &= ~RI_CURSOR;
    598 		}
    599 		ri->ri_crow = row;
    600 		ri->ri_ccol = col;
    601 		if (on) {
    602 			long attr, revattr;
    603 			coffset = col + (row * ri->ri_cols);
    604 #ifdef WSDISPLAY_SCROLLSUPPORT
    605 			coffset += scr->scr_offset_to_zero;
    606 #endif
    607 			attr = scr->scr_attrs[coffset];
    608 			revattr = attr ^ 0xffff0000;
    609 
    610 			wcfb_putchar(cookie, ri->ri_crow, ri->ri_ccol,
    611 			    scr->scr_chars[coffset], revattr);
    612 			ri->ri_flg |= RI_CURSOR;
    613 		}
    614 	} else {
    615 		ri->ri_crow = row;
    616 		ri->ri_ccol = col;
    617 		ri->ri_flg &= ~RI_CURSOR;
    618 	}
    619 }
    620 
    621 static void
    622 wcfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    623 {
    624 	struct rasops_info *ri = cookie;
    625 	struct vcons_screen *scr = ri->ri_hw;
    626 	struct wcfb_softc *sc = scr->scr_cookie;
    627 	int offset = (ri->ri_yorigin + row * ri->ri_font->fontheight) *
    628 	    sc->sc_stride + ri->ri_xorigin + dstcol * ri->ri_font->fontwidth;
    629 	uint8_t *from, *to0, *to1;
    630 	int i;
    631 
    632 	sc->copycols(ri, row, srccol, dstcol, ncols);
    633 	from = sc->sc_shadow + offset;
    634 	to0 = sc->sc_fb0 + offset;
    635 	to1 = sc->sc_fb1 + offset;
    636 	for (i = 0; i < ri->ri_font->fontheight; i++) {
    637 		memcpy(to0, from, ri->ri_font->fontwidth * ncols);
    638 		memcpy(to1, from, ri->ri_font->fontwidth * ncols);
    639 		to0 += sc->sc_stride;
    640 		to1 += sc->sc_stride;
    641 		from += sc->sc_stride;
    642 	}
    643 }
    644 
    645 static void
    646 wcfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    647 {
    648 	struct rasops_info *ri = cookie;
    649 	struct vcons_screen *scr = ri->ri_hw;
    650 	struct wcfb_softc *sc = scr->scr_cookie;
    651 	int offset = (ri->ri_yorigin + row * ri->ri_font->fontheight) *
    652 	    sc->sc_stride + ri->ri_xorigin + startcol * ri->ri_font->fontwidth;
    653 	uint8_t *to0, *to1;
    654 	int i;
    655 
    656 	sc->erasecols(ri, row, startcol, ncols, fillattr);
    657 
    658 	to0 = sc->sc_fb0 + offset;
    659 	to1 = sc->sc_fb1 + offset;
    660 	for (i = 0; i < ri->ri_font->fontheight; i++) {
    661 		memset(to0, ri->ri_devcmap[(fillattr >> 16) & 0xff],
    662 		    ri->ri_font->fontwidth * ncols);
    663 		memset(to1, ri->ri_devcmap[(fillattr >> 16) & 0xff],
    664 		    ri->ri_font->fontwidth * ncols);
    665 		to0 += sc->sc_stride;
    666 		to1 += sc->sc_stride;
    667 	}
    668 }
    669 
    670 static void
    671 wcfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    672 {
    673 	struct rasops_info *ri = cookie;
    674 	struct vcons_screen *scr = ri->ri_hw;
    675 	struct wcfb_softc *sc = scr->scr_cookie;
    676 	int offset = (ri->ri_yorigin + dstrow * ri->ri_font->fontheight) *
    677 	    sc->sc_stride + ri->ri_xorigin;
    678 	uint8_t *from, *to0, *to1;
    679 	int i;
    680 
    681 	sc->copyrows(ri, srcrow, dstrow, nrows);
    682 
    683 	from = sc->sc_shadow + offset;
    684 	to0 = sc->sc_fb0 + offset;
    685 	to1 = sc->sc_fb1 + offset;
    686 	for (i = 0; i < ri->ri_font->fontheight * nrows; i++) {
    687 		memcpy(to0, from, ri->ri_emuwidth);
    688 		memcpy(to1, from, ri->ri_emuwidth);
    689 		to0 += sc->sc_stride;
    690 		to1 += sc->sc_stride;
    691 		from += sc->sc_stride;
    692 	}
    693 }
    694 
    695 static void
    696 wcfb_eraserows(void *cookie, int row, int nrows, long fillattr)
    697 {
    698 	struct rasops_info *ri = cookie;
    699 	struct vcons_screen *scr = ri->ri_hw;
    700 	struct wcfb_softc *sc = scr->scr_cookie;
    701 	int offset = (ri->ri_yorigin + row * ri->ri_font->fontheight) *
    702 	    sc->sc_stride + ri->ri_xorigin;
    703 	uint8_t *to0, *to1;
    704 	int i;
    705 
    706 	sc->eraserows(ri, row, nrows, fillattr);
    707 
    708 	to0 = sc->sc_fb0 + offset;
    709 	to1 = sc->sc_fb1 + offset;
    710 	for (i = 0; i < ri->ri_font->fontheight * nrows; i++) {
    711 		memset(to0, ri->ri_devcmap[(fillattr >> 16) & 0xff],
    712 		    ri->ri_emuwidth);
    713 		memset(to1, ri->ri_devcmap[(fillattr >> 16) & 0xff],
    714 		    ri->ri_emuwidth);
    715 		to0 += sc->sc_stride;
    716 		to1 += sc->sc_stride;
    717 	}
    718 }
    719 
    720 static void
    721 wcfb_bitblt(struct wcfb_softc *sc, int sx, int sy, int dx, int dy, int w,
    722 		 int h, uint32_t rop)
    723 {
    724 	wcfb_rop_wait(sc);
    725 	wcfb_rop_jfb(sc, sx, sy, dx, dy, w, h, rop, 0x0f);
    726 }
    727 
    728 static void
    729 wcfb_rectfill(struct wcfb_softc *sc, int x, int y, int w, int h, int bg)
    730 {
    731 	int32_t mask;
    732 
    733 	/* clear everything just in case... */
    734 	wcfb_rop_wait(sc);
    735 	wcfb_rop_jfb(sc, x, y, x, y, w, h, WC_ROP_CLEAR, 0xffffffff);
    736 
    737 	/* pixels to set... */
    738 	mask = 0x0f & bg;
    739 	if (mask != 0) {
    740 		wcfb_rop_wait(sc);
    741 		wcfb_rop_jfb(sc, x, y, x, y, w, h, WC_ROP_SET, mask);
    742 	}
    743 
    744 }
    745 
    746 void
    747 wcfb_rop_common(struct wcfb_softc *sc, bus_addr_t reg, int sx, int sy,
    748     int dx, int dy, int w, int h, uint32_t rop, int32_t planemask)
    749 {
    750 	int dir = 0;
    751 
    752 	/*
    753 	 * Compute rop direction. This only really matters for
    754 	 * screen-to-screen copies.
    755 	 */
    756 	if (sy < dy /* && sy + h > dy */) {
    757 		sy += h - 1;
    758 		dy += h;
    759 		dir |= WC_BLT_DIR_BACKWARDS_Y;
    760 	}
    761 	if (sx < dx /* && sx + w > dx */) {
    762 		sx += w - 1;
    763 		dx += w;
    764 		dir |= WC_BLT_DIR_BACKWARDS_X;
    765 	}
    766 
    767 	/* Which one of those below is your magic number for today? */
    768 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x61000001);
    769 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0);
    770 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x6301c080);
    771 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x80000000);
    772 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, rop);
    773 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, planemask);
    774 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0);
    775 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x64000303);
    776 	/*
    777 	 * This value is a pixel offset within the destination area. It is
    778 	 * probably used to define complex polygon shapes, with the
    779 	 * last pixel in the list being back to (0,0).
    780 	 */
    781 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, WCFB_COORDS(0, 0));
    782 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0);
    783 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x00030000);
    784 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x2200010d);
    785 
    786 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x33f01000 | dir);
    787 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, WCFB_COORDS(dx, dy));
    788 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, WCFB_COORDS(w, h));
    789 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, WCFB_COORDS(sx, sy));
    790 }
    791 
    792 
    793 static void
    794 wcfb_rop_jfb(struct wcfb_softc *sc, int sx, int sy, int dx, int dy,
    795              int w, int h, uint32_t rop, int32_t planemask)
    796 {
    797 	bus_addr_t reg = WC_JFB_ENGINE;
    798 	uint32_t spr, splr;
    799 
    800 #if 0
    801 	/*
    802 	 * Pick the current spr and splr values from the communication
    803 	 * area if possible.
    804 	 */
    805 	if (sc->sc_comm != NULL) {
    806 		spr = sc->sc_comm[IFB_SHARED_TERM8_SPR >> 2];
    807 		splr = sc->sc_comm[IFB_SHARED_TERM8_SPLR >> 2];
    808 	} else
    809 #endif
    810 	{
    811 		/* supposedly sane defaults */
    812 		spr = 0x54ff0303;
    813 		splr = 0x5c0000ff;
    814 	}
    815 
    816 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x00400016);
    817 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x5b000002);
    818 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x5a000000);
    819 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, spr);
    820 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, splr);
    821 
    822 	wcfb_rop_common(sc, reg, sx, sy, dx, dy, w, h, rop, planemask);
    823 
    824 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x5a000001);
    825 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x5b000001);
    826 }
    827 
    828 static int
    829 wcfb_rop_wait(struct wcfb_softc *sc)
    830 {
    831 	int i;
    832 
    833 	for (i = 1000000; i != 0; i--) {
    834 		if (bus_space_read_4(sc->sc_regt, sc->sc_regh,
    835 		    WC_STATUS) & WC_STATUS_DONE)
    836 			break;
    837 		delay(1);
    838 	}
    839 
    840 	return i;
    841 }
    842 
    843 static void
    844 wcfb_acc_putchar(void *cookie, int row, int col, u_int c, long attr)
    845 {
    846 	struct rasops_info *ri = cookie;
    847 	struct vcons_screen *scr = ri->ri_hw;
    848 	struct wcfb_softc *sc = scr->scr_cookie;
    849 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    850 	int x, y, wi, he;
    851 	uint32_t bg;
    852 
    853 	wi = font->fontwidth;
    854 	he = font->fontheight;
    855 	x = ri->ri_xorigin + col * wi;
    856 	y = ri->ri_yorigin + row * he;
    857 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    858 	if (c == 0x20) {
    859 		wcfb_rectfill(sc, x, y, wi, he, bg);
    860 		return;
    861 	}
    862 	/* we wait until the blitter is idle... */
    863 	wcfb_rop_wait(sc);
    864 	/* ... draw the character into buffer 0 ... */
    865 	sc->putchar(ri, row, col, c, attr);
    866 	/* ... and then blit it into buffer 1 */
    867 	wcfb_bitblt(sc, x, y, x, y, wi, he, WC_ROP_COPY);
    868 }
    869 
    870 static void
    871 wcfb_acc_cursor(void *cookie, int on, int row, int col)
    872 {
    873 	struct rasops_info *ri = cookie;
    874 	struct vcons_screen *scr = ri->ri_hw;
    875 	struct wcfb_softc *sc = scr->scr_cookie;
    876 	int x, y, wi, he;
    877 
    878 	wi = ri->ri_font->fontwidth;
    879 	he = ri->ri_font->fontheight;
    880 
    881 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    882 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    883 		y = ri->ri_crow * he + ri->ri_yorigin;
    884 		if (ri->ri_flg & RI_CURSOR) {
    885 			wcfb_bitblt(sc, x, y, x, y, wi, he, WC_ROP_XOR);
    886 			ri->ri_flg &= ~RI_CURSOR;
    887 		}
    888 		ri->ri_crow = row;
    889 		ri->ri_ccol = col;
    890 		if (on) {
    891 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    892 			y = ri->ri_crow * he + ri->ri_yorigin;
    893 			wcfb_bitblt(sc, x, y, x, y, wi, he, WC_ROP_XOR);
    894 			ri->ri_flg |= RI_CURSOR;
    895 		}
    896 	} else {
    897 		scr->scr_ri.ri_crow = row;
    898 		scr->scr_ri.ri_ccol = col;
    899 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    900 	}
    901 
    902 }
    903 
    904 static void
    905 wcfb_acc_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    906 {
    907 	struct rasops_info *ri = cookie;
    908 	struct vcons_screen *scr = ri->ri_hw;
    909 	struct wcfb_softc *sc = scr->scr_cookie;
    910 	int32_t xs, xd, y, width, height;
    911 
    912 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    913 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    914 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    915 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    916 		width = ri->ri_font->fontwidth * ncols;
    917 		height = ri->ri_font->fontheight;
    918 		wcfb_bitblt(sc, xs, y, xd, y, width, height, WC_ROP_COPY);
    919 	}
    920 }
    921 
    922 static void
    923 wcfb_acc_erasecols(void *cookie, int row, int startcol, int ncols,
    924 		long fillattr)
    925 {
    926 	struct rasops_info *ri = cookie;
    927 	struct vcons_screen *scr = ri->ri_hw;
    928 	struct wcfb_softc *sc = scr->scr_cookie;
    929 	int32_t x, y, width, height, fg, bg, ul;
    930 
    931 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    932 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    933 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    934 		width = ri->ri_font->fontwidth * ncols;
    935 		height = ri->ri_font->fontheight;
    936 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    937 
    938 		wcfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    939 	}
    940 }
    941 
    942 static void
    943 wcfb_acc_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    944 {
    945 	struct rasops_info *ri = cookie;
    946 	struct vcons_screen *scr = ri->ri_hw;
    947 	struct wcfb_softc *sc = scr->scr_cookie;
    948 	int32_t x, ys, yd, width, height;
    949 
    950 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    951 		x = ri->ri_xorigin;
    952 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    953 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    954 		width = ri->ri_emuwidth;
    955 		height = ri->ri_font->fontheight * nrows;
    956 		wcfb_bitblt(sc, x, ys, x, yd, width, height, WC_ROP_COPY);
    957 	}
    958 }
    959 
    960 static void
    961 wcfb_acc_eraserows(void *cookie, int row, int nrows, long fillattr)
    962 {
    963 	struct rasops_info *ri = cookie;
    964 	struct vcons_screen *scr = ri->ri_hw;
    965 	struct wcfb_softc *sc = scr->scr_cookie;
    966 	int32_t x, y, width, height, fg, bg, ul;
    967 
    968 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    969 		x = ri->ri_xorigin;
    970 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    971 		width = ri->ri_emuwidth;
    972 		height = ri->ri_font->fontheight * nrows;
    973 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    974 
    975 		wcfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    976 	}
    977 }
    978