Home | History | Annotate | Line # | Download | only in dev
cgfourteen.c revision 1.72
      1 /*	$NetBSD: cgfourteen.c,v 1.72 2013/02/12 22:24:47 macallan Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996
      5  *	The President and Fellows of Harvard College. All rights reserved.
      6  * Copyright (c) 1992, 1993
      7  *	The Regents of the University of California.  All rights reserved.
      8  *
      9  * This software was developed by the Computer Systems Engineering group
     10  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     11  * contributed to Berkeley.
     12  *
     13  * All advertising materials mentioning features or use of this software
     14  * must display the following acknowledgement:
     15  *	This product includes software developed by Harvard University.
     16  *	This product includes software developed by the University of
     17  *	California, Lawrence Berkeley Laboratory.
     18  *
     19  * Redistribution and use in source and binary forms, with or without
     20  * modification, are permitted provided that the following conditions
     21  * are met:
     22  * 1. Redistributions of source code must retain the above copyright
     23  *    notice, this list of conditions and the following disclaimer.
     24  * 2. Redistributions in binary form must reproduce the above copyright
     25  *    notice, this list of conditions and the following disclaimer in the
     26  *    documentation and/or other materials provided with the distribution.
     27  * 3. All advertising materials mentioning features or use of this software
     28  *    must display the following acknowledgement:
     29  *	This product includes software developed by the University of
     30  *	California, Berkeley and its contributors.
     31  *	This product includes software developed by Harvard University and
     32  *	its contributors.
     33  * 4. Neither the name of the University nor the names of its contributors
     34  *    may be used to endorse or promote products derived from this software
     35  *    without specific prior written permission.
     36  *
     37  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     38  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     40  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     42  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     43  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     45  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     46  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     47  * SUCH DAMAGE.
     48  *
     49  *   Based on:
     50  *	NetBSD: cgthree.c,v 1.28 1996/05/31 09:59:22 pk Exp
     51  *	NetBSD: cgsix.c,v 1.25 1996/04/01 17:30:00 christos Exp
     52  */
     53 
     54 /*
     55  * Driver for Campus-II on-board mbus-based video (cgfourteen).
     56  *
     57  * Does not handle interrupts, even though they can occur.
     58  *
     59  * XXX should defer colormap updates to vertical retrace interrupts
     60  */
     61 
     62 /*
     63  * The following is for debugging only; it opens up a security hole
     64  * enabled by allowing any user to map the control registers for the
     65  * cg14 into their space.
     66  */
     67 #undef CG14_MAP_REGS
     68 
     69 #include "opt_wsemul.h"
     70 #include "sx.h"
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/buf.h>
     75 #include <sys/device.h>
     76 #include <sys/ioctl.h>
     77 #include <sys/malloc.h>
     78 #include <sys/kmem.h>
     79 #include <sys/mman.h>
     80 #include <sys/tty.h>
     81 #include <sys/conf.h>
     82 #include <dev/pci/pciio.h>
     83 
     84 #include <uvm/uvm_extern.h>
     85 
     86 #include <dev/sun/fbio.h>
     87 #include <machine/autoconf.h>
     88 #include <machine/pmap.h>
     89 #include <dev/sun/fbvar.h>
     90 #include <machine/cpu.h>
     91 #include <dev/sbus/sbusvar.h>
     92 
     93 #include "wsdisplay.h"
     94 #include <dev/wscons/wsconsio.h>
     95 #include <dev/wsfont/wsfont.h>
     96 #include <dev/rasops/rasops.h>
     97 
     98 #include <dev/wscons/wsdisplay_vconsvar.h>
     99 
    100 #include <sparc/sparc/asm.h>
    101 #include <sparc/dev/cgfourteenreg.h>
    102 #include <sparc/dev/cgfourteenvar.h>
    103 #include <sparc/dev/sxreg.h>
    104 #include <sparc/dev/sxvar.h>
    105 
    106 /* autoconfiguration driver */
    107 static int	cgfourteenmatch(device_t, struct cfdata *, void *);
    108 static void	cgfourteenattach(device_t, device_t, void *);
    109 static void	cgfourteenunblank(device_t);
    110 
    111 CFATTACH_DECL_NEW(cgfourteen, sizeof(struct cgfourteen_softc),
    112     cgfourteenmatch, cgfourteenattach, NULL, NULL);
    113 
    114 extern struct cfdriver cgfourteen_cd;
    115 
    116 dev_type_open(cgfourteenopen);
    117 dev_type_close(cgfourteenclose);
    118 dev_type_ioctl(cgfourteenioctl);
    119 dev_type_mmap(cgfourteenmmap);
    120 dev_type_poll(cgfourteenpoll);
    121 
    122 const struct cdevsw cgfourteen_cdevsw = {
    123         cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
    124         nostop, notty, cgfourteenpoll, cgfourteenmmap, nokqfilter,
    125 };
    126 
    127 /* frame buffer generic driver */
    128 static struct fbdriver cgfourteenfbdriver = {
    129 	cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
    130 	cgfourteenpoll, cgfourteenmmap, nokqfilter
    131 };
    132 
    133 static void cg14_set_video(struct cgfourteen_softc *, int);
    134 static int  cg14_get_video(struct cgfourteen_softc *);
    135 static int  cg14_get_cmap(struct fbcmap *, union cg14cmap *, int);
    136 static int  cg14_put_cmap(struct fbcmap *, union cg14cmap *, int);
    137 static void cg14_load_hwcmap(struct cgfourteen_softc *, int, int);
    138 static void cg14_init(struct cgfourteen_softc *);
    139 static void cg14_reset(struct cgfourteen_softc *);
    140 
    141 #if NWSDISPLAY > 0
    142 static void cg14_setup_wsdisplay(struct cgfourteen_softc *, int);
    143 static void cg14_init_cmap(struct cgfourteen_softc *);
    144 static int  cg14_putcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
    145 static int  cg14_getcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
    146 static void cg14_set_depth(struct cgfourteen_softc *, int);
    147 static void cg14_move_cursor(struct cgfourteen_softc *, int, int);
    148 static int  cg14_do_cursor(struct cgfourteen_softc *,
    149                            struct wsdisplay_cursor *);
    150 
    151 #if NSX > 0
    152 static void cg14_wait_idle(struct cgfourteen_softc *);
    153 static void cg14_rectfill(struct cgfourteen_softc *, int, int, int, int,
    154     uint32_t);
    155 static void cg14_invert(struct cgfourteen_softc *, int, int, int, int);
    156 static void cg14_bitblt(void *, int, int, int, int, int, int, int);
    157 
    158 #if 0
    159 static void cg14_putchar_aa(void *, int, int, u_int, long);
    160 #endif
    161 static void cg14_cursor(void *, int, int, int);
    162 static void cg14_putchar(void *, int, int, u_int, long);
    163 static void cg14_copycols(void *, int, int, int, int);
    164 static void cg14_erasecols(void *, int, int, int, long);
    165 static void cg14_copyrows(void *, int, int, int);
    166 static void cg14_eraserows(void *, int, int, long);
    167 #endif /* NSX > 0 */
    168 
    169 #endif
    170 
    171 /*
    172  * Match a cgfourteen.
    173  */
    174 int
    175 cgfourteenmatch(device_t parent, struct cfdata *cf, void *aux)
    176 {
    177 	union obio_attach_args *uoba = aux;
    178 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    179 
    180 	/*
    181 	 * The cgfourteen is a local-bus video adaptor, accessed directly
    182 	 * via the processor, and not through device space or an external
    183 	 * bus. Thus we look _only_ at the obio bus.
    184 	 * Additionally, these things exist only on the Sun4m.
    185 	 */
    186 
    187 	if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
    188 		return (0);
    189 
    190 	/* Check driver name */
    191 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
    192 }
    193 
    194 /*
    195  * Set COLOUR_OFFSET to the offset of the video RAM.  This is to provide
    196  *  space for faked overlay junk for the cg8 emulation.
    197  *
    198  * As it happens, this value is correct for both cg3 and cg8 emulation!
    199  */
    200 #define COLOUR_OFFSET (256*1024)
    201 
    202 #if NWSDISPLAY > 0
    203 static int	cg14_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    204 static paddr_t	cg14_mmap(void *, void *, off_t, int);
    205 static void	cg14_init_screen(void *, struct vcons_screen *, int, long *);
    206 
    207 
    208 struct wsdisplay_accessops cg14_accessops = {
    209 	cg14_ioctl,
    210 	cg14_mmap,
    211 	NULL,	/* alloc_screen */
    212 	NULL,	/* free_screen */
    213 	NULL,	/* show_screen */
    214 	NULL, 	/* load_font */
    215 	NULL,	/* pollc */
    216 	NULL	/* scroll */
    217 };
    218 #endif
    219 
    220 /*
    221  * Attach a display.  We need to notice if it is the console, too.
    222  */
    223 void
    224 cgfourteenattach(device_t parent, device_t self, void *aux)
    225 {
    226 	union obio_attach_args *uoba = aux;
    227 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    228 	struct cgfourteen_softc *sc = device_private(self);
    229 	struct fbdevice *fb = &sc->sc_fb;
    230 	bus_space_handle_t bh;
    231 	int node, ramsize;
    232 	volatile uint32_t *lut;
    233 	int i, isconsole, items;
    234 	uint32_t fbva[2] = {0, 0};
    235 	uint32_t *ptr = fbva;
    236 #if NSX > 0
    237 	device_t dv;
    238 	deviter_t di;
    239 #endif
    240 
    241 	sc->sc_dev = self;
    242 	sc->sc_opens = 0;
    243 	node = sa->sa_node;
    244 
    245 	/* Remember cookies for cgfourteenmmap() */
    246 	sc->sc_bustag = sa->sa_bustag;
    247 
    248 	fb->fb_driver = &cgfourteenfbdriver;
    249 	fb->fb_device = sc->sc_dev;
    250 	/* Mask out invalid flags from the user. */
    251 	fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK;
    252 
    253 	fb->fb_type.fb_type = FBTYPE_MDICOLOR;
    254 	fb->fb_type.fb_depth = 32;
    255 
    256 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
    257 	ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, NBPG);
    258 
    259 	fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
    260 	fb->fb_type.fb_size = ramsize + COLOUR_OFFSET;
    261 
    262 	if (sa->sa_nreg < 2) {
    263 		printf("%s: only %d register sets\n",
    264 			device_xname(self), sa->sa_nreg);
    265 		return;
    266 	}
    267 	memcpy(sc->sc_physadr, sa->sa_reg,
    268 	      sa->sa_nreg * sizeof(struct sbus_reg));
    269 
    270 	sc->sc_vramsize = sc->sc_physadr[CG14_PXL_IDX].sbr_size;
    271 
    272 	printf(": %d MB VRAM", (uint32_t)(sc->sc_vramsize >> 20));
    273 	/*
    274 	 * Now map in the 8 useful pages of registers
    275 	 */
    276 	if (sa->sa_size < 0x10000) {
    277 #ifdef DIAGNOSTIC
    278 		printf("warning: can't find all cgfourteen registers...\n");
    279 #endif
    280 		sa->sa_size = 0x10000;
    281 	}
    282 	if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    283 			 sa->sa_offset,
    284 			 sa->sa_size,
    285 			 0 /*BUS_SPACE_MAP_LINEAR*/,
    286 			 &bh) != 0) {
    287 		printf("%s: cannot map control registers\n",
    288 		    device_xname(self));
    289 		return;
    290 	}
    291 	sc->sc_regh = bh;
    292 	sc->sc_regaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
    293 	sc->sc_fbaddr = BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    294 				sc->sc_physadr[CG14_PXL_IDX].sbr_offset);
    295 
    296 	sc->sc_ctl   = (struct cg14ctl  *) (bh);
    297 	sc->sc_hwc   = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
    298 	sc->sc_dac   = (struct cg14dac  *) (bh + CG14_OFFSET_DAC);
    299 	sc->sc_xlut  = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
    300 	sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
    301 	sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
    302 	sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
    303 	sc->sc_clutincr =        (u_int *) (bh + CG14_OFFSET_CLUTINCR);
    304 
    305 	/*
    306 	 * Let the user know that we're here
    307 	 */
    308 	printf(": %dx%d",
    309 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    310 
    311 	/*
    312 	 * Enable the video.
    313 	 */
    314 	cg14_set_video(sc, 1);
    315 
    316 	/*
    317 	 * Grab the initial colormap
    318 	 */
    319 	lut = sc->sc_clut1->clut_lut;
    320 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    321 		sc->sc_cmap.cm_chip[i] = lut[i];
    322 
    323 	/* See if we're the console */
    324         isconsole = fb_is_console(node);
    325 
    326 #if NWSDISPLAY > 0
    327 	prom_getprop(sa->sa_node, "address", 4, &items, &ptr);
    328 	if (fbva[1] == 0) {
    329 		if (sbus_bus_map( sc->sc_bustag,
    330 		    sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    331 		    sc->sc_physadr[CG14_PXL_IDX].sbr_offset,
    332 		    ramsize, BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
    333 		    &bh) != 0) {
    334 			printf("%s: cannot map pixels\n",
    335 				device_xname(sc->sc_dev));
    336 			return;
    337 		}
    338 		sc->sc_fb.fb_pixels = bus_space_vaddr(sc->sc_bustag, bh);
    339 	} else {
    340 		sc->sc_fb.fb_pixels = (void *)fbva[1];
    341 	}
    342 
    343 	if (isconsole)
    344 		printf(" (console)\n");
    345 	else
    346 		printf("\n");
    347 
    348 	sc->sc_depth = 8;
    349 
    350 #if NSX > 0
    351 	/* see if we've got an SX to help us */
    352 	sc->sc_sx = NULL;
    353 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    354 	    dv != NULL;
    355 	    dv = deviter_next(&di)) {
    356 		if (device_is_a(dv, "sx")) {
    357 			sc->sc_sx = device_private(dv);
    358 		}
    359 	}
    360 	deviter_release(&di);
    361 	if (sc->sc_sx != NULL) {
    362 		sc->sc_fb_paddr = bus_space_mmap(sc->sc_bustag,
    363 		    sc->sc_fbaddr, 0, 0, 0) & 0xfffff000;
    364 		aprint_normal_dev(sc->sc_dev, "using %s\n",
    365 		    device_xname(sc->sc_sx->sc_dev));
    366 		aprint_debug_dev(sc->sc_dev, "fb paddr: %08x\n",
    367 		    sc->sc_fb_paddr);
    368 		sx_write(sc->sc_sx, SX_PAGE_BOUND_LOWER, sc->sc_fb_paddr);
    369 		sx_write(sc->sc_sx, SX_PAGE_BOUND_UPPER,
    370 		    sc->sc_fb_paddr + 0x03ffffff);
    371 	}
    372 	cg14_wait_idle(sc);
    373 #endif
    374 	cg14_setup_wsdisplay(sc, isconsole);
    375 #endif
    376 
    377 	/* Attach to /dev/fb */
    378 	fb_attach(&sc->sc_fb, isconsole);
    379 }
    380 
    381 /*
    382  * Keep track of the number of opens made. In the 24-bit driver, we need to
    383  * switch to 24-bit mode on the first open, and switch back to 8-bit on
    384  * the last close. This kind of nonsense is needed to give screenblank
    385  * a fighting chance of working.
    386  */
    387 
    388 int
    389 cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
    390 {
    391 	struct cgfourteen_softc *sc;
    392 	int oldopens;
    393 
    394 	sc = device_lookup_private(&cgfourteen_cd, minor(dev));
    395 	if (sc == NULL)
    396 		return(ENXIO);
    397 	oldopens = sc->sc_opens++;
    398 
    399 	/* Setup the cg14 as we want it, and save the original PROM state */
    400 	if (oldopens == 0)	/* first open only, to make screenblank work */
    401 		cg14_init(sc);
    402 
    403 	return (0);
    404 }
    405 
    406 int
    407 cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
    408 {
    409 	struct cgfourteen_softc *sc =
    410 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    411 	int opens;
    412 
    413 	opens = --sc->sc_opens;
    414 	if (sc->sc_opens < 0)
    415 		opens = sc->sc_opens = 0;
    416 
    417 	/*
    418 	 * Restore video state to make the PROM happy, on last close.
    419 	 */
    420 	if (opens == 0)
    421 		cg14_reset(sc);
    422 
    423 	return (0);
    424 }
    425 
    426 int
    427 cgfourteenioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    428 {
    429 	struct cgfourteen_softc *sc =
    430 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    431 	struct fbgattr *fba;
    432 	int error;
    433 
    434 	switch (cmd) {
    435 
    436 	case FBIOGTYPE:
    437 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    438 		break;
    439 
    440 	case FBIOGATTR:
    441 		fba = (struct fbgattr *)data;
    442 		fba->real_type = FBTYPE_MDICOLOR;
    443 		fba->owner = 0;		/* XXX ??? */
    444 		fba->fbtype = sc->sc_fb.fb_type;
    445 		fba->sattr.flags = 0;
    446 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    447 		fba->sattr.dev_specific[0] = -1;
    448 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    449 		fba->emu_types[1] = -1;
    450 		break;
    451 
    452 	case FBIOGETCMAP:
    453 		return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
    454 				     CG14_CLUT_SIZE));
    455 
    456 	case FBIOPUTCMAP:
    457 		/* copy to software map */
    458 #define p ((struct fbcmap *)data)
    459 		error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
    460 		if (error)
    461 			return (error);
    462 		/* now blast them into the chip */
    463 		/* XXX should use retrace interrupt */
    464 		cg14_load_hwcmap(sc, p->index, p->count);
    465 #undef p
    466 		break;
    467 
    468 	case FBIOGVIDEO:
    469 		*(int *)data = cg14_get_video(sc);
    470 		break;
    471 
    472 	case FBIOSVIDEO:
    473 		cg14_set_video(sc, *(int *)data);
    474 		break;
    475 
    476 	case CG14_SET_PIXELMODE: {
    477 		int depth = *(int *)data;
    478 
    479 		if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
    480 			return EINVAL;
    481 
    482 		cg14_set_depth(sc, depth);
    483 		}
    484 		break;
    485 	default:
    486 		return (ENOTTY);
    487 	}
    488 	return (0);
    489 }
    490 
    491 /*
    492  * Undo the effect of an FBIOSVIDEO that turns the video off.
    493  */
    494 static void
    495 cgfourteenunblank(device_t dev)
    496 {
    497 	struct cgfourteen_softc *sc = device_private(dev);
    498 
    499 	cg14_set_video(sc, 1);
    500 #if NWSDISPLAY > 0
    501 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
    502 		cg14_set_depth(sc, 8);
    503 		cg14_init_cmap(sc);
    504 		vcons_redraw_screen(sc->sc_vd.active);
    505 		sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    506 	}
    507 #endif
    508 }
    509 
    510 /*
    511  * Return the address that would map the given device at the given
    512  * offset, allowing for the given protection, or return -1 for error.
    513   */
    514 paddr_t
    515 cgfourteenmmap(dev_t dev, off_t off, int prot)
    516 {
    517 	struct cgfourteen_softc *sc =
    518 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    519 	off_t offset = -1;
    520 
    521 	if (off & PGOFSET)
    522 		panic("cgfourteenmmap");
    523 
    524 	if (off < 0)
    525 		return (-1);
    526 
    527 	if (off >= 0 && off < 0x10000) {
    528 		offset = sc->sc_regaddr;
    529 	} else if (off >= CG14_CURSOR_VOFF &&
    530 		   off < (CG14_CURSOR_VOFF + 0x1000)) {
    531 		offset = sc->sc_regaddr + CG14_OFFSET_CURS;
    532 		off -= CG14_CURSOR_VOFF;
    533 	} else if (off >= CG14_DIRECT_VOFF &&
    534 		   off < (CG14_DIRECT_VOFF + sc->sc_vramsize)) {
    535 		offset = sc->sc_fbaddr + CG14_FB_VRAM;
    536 		off -= CG14_DIRECT_VOFF;
    537 	} else if (off >= CG14_BGR_VOFF &&
    538 		   off < (CG14_BGR_VOFF + sc->sc_vramsize)) {
    539 		offset = sc->sc_fbaddr + CG14_FB_CBGR;
    540 		off -= CG14_BGR_VOFF;
    541 	} else if (off >= CG14_X32_VOFF &&
    542 		   off < (CG14_X32_VOFF + (sc->sc_vramsize >> 2))) {
    543 		offset = sc->sc_fbaddr + CG14_FB_PX32;
    544 		off -= CG14_X32_VOFF;
    545 	} else if (off >= CG14_B32_VOFF &&
    546 		   off < (CG14_B32_VOFF + (sc->sc_vramsize >> 2))) {
    547 		offset = sc->sc_fbaddr + CG14_FB_PB32;
    548 		off -= CG14_B32_VOFF;
    549 	} else if (off >= CG14_G32_VOFF &&
    550 		   off < (CG14_G32_VOFF + (sc->sc_vramsize >> 2))) {
    551 		offset = sc->sc_fbaddr + CG14_FB_PG32;
    552 		off -= CG14_G32_VOFF;
    553 	} else if (off >= CG14_R32_VOFF &&
    554 		   off < CG14_R32_VOFF + (sc->sc_vramsize >> 2)) {
    555 		offset = sc->sc_fbaddr + CG14_FB_PR32;
    556 		off -= CG14_R32_VOFF;
    557 	} else
    558 		return -1;
    559 	return (bus_space_mmap(sc->sc_bustag, offset, off, prot,
    560 		    BUS_SPACE_MAP_LINEAR));
    561 }
    562 
    563 int
    564 cgfourteenpoll(dev_t dev, int events, struct lwp *l)
    565 {
    566 
    567 	return (seltrue(dev, events, l));
    568 }
    569 
    570 /*
    571  * Miscellaneous helper functions
    572  */
    573 
    574 /* Initialize the framebuffer, storing away useful state for later reset */
    575 static void
    576 cg14_init(struct cgfourteen_softc *sc)
    577 {
    578 #if 0
    579 	volatile uint32_t *clut;
    580 	volatile uint8_t  *xlut;
    581 	int i;
    582 
    583 	/*
    584 	 * We stash away the following to restore on close:
    585 	 *
    586 	 * 	color look-up table 1 	(sc->sc_saveclut)
    587 	 *	x look-up table		(sc->sc_savexlut)
    588 	 *	control register	(sc->sc_savectl)
    589 	 *	cursor control register (sc->sc_savehwc)
    590 	 */
    591 	sc->sc_savectl = sc->sc_ctl->ctl_mctl;
    592 	sc->sc_savehwc = sc->sc_hwc->curs_ctl;
    593 
    594 	clut = (volatile uint32_t *) sc->sc_clut1->clut_lut;
    595 	xlut = (volatile uint8_t *) sc->sc_xlut->xlut_lut;
    596 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    597 		sc->sc_saveclut.cm_chip[i] = clut[i];
    598 		sc->sc_savexlut[i] = xlut[i];
    599 	}
    600 
    601 	/*
    602 	 * Enable the video and put it in 8 bit mode
    603 	 */
    604 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
    605 		CG14_MCTL_POWERCTL;
    606 #else
    607 	cg14_set_depth(sc, 32);
    608 #endif
    609 }
    610 
    611 static void
    612 /* Restore the state saved on cg14_init */
    613 cg14_reset(struct cgfourteen_softc *sc)
    614 {
    615 #if 0
    616 	volatile uint32_t *clut;
    617 	volatile uint8_t  *xlut;
    618 	int i;
    619 
    620 	/*
    621 	 * We restore the following, saved in cg14_init:
    622 	 *
    623 	 * 	color look-up table 1 	(sc->sc_saveclut)
    624 	 *	x look-up table		(sc->sc_savexlut)
    625 	 *	control register	(sc->sc_savectl)
    626 	 *	cursor control register (sc->sc_savehwc)
    627 	 *
    628 	 * Note that we don't touch the video enable bits in the
    629 	 * control register; otherwise, screenblank wouldn't work.
    630 	 */
    631 	sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
    632 							CG14_MCTL_POWERCTL)) |
    633 				(sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
    634 						    CG14_MCTL_POWERCTL));
    635 	sc->sc_hwc->curs_ctl = sc->sc_savehwc;
    636 
    637 	clut = sc->sc_clut1->clut_lut;
    638 	xlut = sc->sc_xlut->xlut_lut;
    639 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    640 		clut[i] = sc->sc_saveclut.cm_chip[i];
    641 		xlut[i] = sc->sc_savexlut[i];
    642 	}
    643 #else
    644 	cg14_set_depth(sc, 8);
    645 #endif
    646 }
    647 
    648 /* Enable/disable video display; power down monitor if DPMS-capable */
    649 static void
    650 cg14_set_video(struct cgfourteen_softc *sc, int enable)
    651 {
    652 	/*
    653 	 * We can only use DPMS to power down the display if the chip revision
    654 	 * is greater than 0.
    655 	 */
    656 	if (enable) {
    657 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    658 			sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
    659 						 CG14_MCTL_POWERCTL);
    660 		else
    661 			sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
    662 	} else {
    663 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    664 			sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
    665 						  CG14_MCTL_POWERCTL);
    666 		else
    667 			sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
    668 	}
    669 }
    670 
    671 /* Get status of video display */
    672 static int
    673 cg14_get_video(struct cgfourteen_softc *sc)
    674 {
    675 	return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
    676 }
    677 
    678 /* Read the software shadow colormap */
    679 static int
    680 cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    681 {
    682         u_int i, start, count;
    683         u_char *cp;
    684         int error;
    685 
    686         start = p->index;
    687         count = p->count;
    688         if (start >= cmsize || count > cmsize - start)
    689                 return (EINVAL);
    690 
    691         for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    692                 error = copyout(&cp[3], &p->red[i], 1);
    693                 if (error)
    694                         return error;
    695                 error = copyout(&cp[2], &p->green[i], 1);
    696                 if (error)
    697                         return error;
    698                 error = copyout(&cp[1], &p->blue[i], 1);
    699                 if (error)
    700                         return error;
    701         }
    702         return (0);
    703 }
    704 
    705 /* Write the software shadow colormap */
    706 static int
    707 cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    708 {
    709         u_int i, start, count;
    710         u_char *cp;
    711         u_char cmap[256][4];
    712         int error;
    713 
    714         start = p->index;
    715         count = p->count;
    716         if (start >= cmsize || count > cmsize - start)
    717                 return (EINVAL);
    718 
    719         memcpy(&cmap, &cm->cm_map, sizeof cmap);
    720         for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
    721                 error = copyin(&p->red[i], &cp[3], 1);
    722                 if (error)
    723                         return error;
    724                 error = copyin(&p->green[i], &cp[2], 1);
    725                 if (error)
    726                         return error;
    727                 error = copyin(&p->blue[i], &cp[1], 1);
    728                 if (error)
    729                         return error;
    730                 cp[0] = 0;      /* no alpha channel */
    731         }
    732         memcpy(&cm->cm_map, &cmap, sizeof cmap);
    733         return (0);
    734 }
    735 
    736 static void
    737 cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
    738 {
    739 	/* XXX switch to auto-increment, and on retrace intr */
    740 
    741 	/* Setup pointers to source and dest */
    742 	uint32_t *colp = &sc->sc_cmap.cm_chip[start];
    743 	volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
    744 
    745 	/* Copy by words */
    746 	while (--ncolors >= 0)
    747 		*lutp++ = *colp++;
    748 }
    749 
    750 #if NWSDISPLAY > 0
    751 static void
    752 cg14_setup_wsdisplay(struct cgfourteen_softc *sc, int is_cons)
    753 {
    754 	struct wsemuldisplaydev_attach_args aa;
    755 	struct rasops_info *ri;
    756 	long defattr;
    757 
    758  	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    759 		"default",
    760 		0, 0,
    761 		NULL,
    762 		8, 16,
    763 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    764 		NULL
    765 	};
    766 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    767 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    768 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    769 	vcons_init(&sc->sc_vd, sc, &sc->sc_defaultscreen_descr,
    770 	    &cg14_accessops);
    771 	sc->sc_vd.init_screen = cg14_init_screen;
    772 
    773 	ri = &sc->sc_console_screen.scr_ri;
    774 
    775 	if (is_cons) {
    776 		vcons_init_screen(&sc->sc_vd, &sc->sc_console_screen, 1,
    777 		    &defattr);
    778 
    779 		/* clear the screen with the default background colour */
    780 		memset(sc->sc_fb.fb_pixels,
    781 		       (defattr >> 16) & 0xff,
    782 		       ri->ri_stride * ri->ri_height);
    783 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    784 
    785 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    786 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    787 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    788 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    789 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    790 		    defattr);
    791 		vcons_replay_msgbuf(&sc->sc_console_screen);
    792 	} else {
    793 		/*
    794 		 * since we're not the console we can postpone the rest
    795 		 * until someone actually allocates a screen for us
    796 		 */
    797 	}
    798 
    799 	cg14_init_cmap(sc);
    800 
    801 	aa.console = is_cons;
    802 	aa.scrdata = &sc->sc_screenlist;
    803 	aa.accessops = &cg14_accessops;
    804 	aa.accesscookie = &sc->sc_vd;
    805 
    806 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    807 }
    808 
    809 static void
    810 cg14_init_cmap(struct cgfourteen_softc *sc)
    811 {
    812 	int i, j = 0;
    813 
    814 	for (i = 0; i < 256; i++) {
    815 
    816 		sc->sc_cmap.cm_map[i][3] = rasops_cmap[j];
    817 		sc->sc_cmap.cm_map[i][2] = rasops_cmap[j + 1];
    818 		sc->sc_cmap.cm_map[i][1] = rasops_cmap[j + 2];
    819 		j += 3;
    820 	}
    821 	cg14_load_hwcmap(sc, 0, 256);
    822 }
    823 
    824 static int
    825 cg14_putcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
    826 {
    827 	u_int index = cm->index;
    828 	u_int count = cm->count;
    829 	int i, error;
    830 	u_char rbuf[256], gbuf[256], bbuf[256];
    831 
    832 	if (cm->index >= 256 || cm->count > 256 ||
    833 	    (cm->index + cm->count) > 256)
    834 		return EINVAL;
    835 	error = copyin(cm->red, &rbuf[index], count);
    836 	if (error)
    837 		return error;
    838 	error = copyin(cm->green, &gbuf[index], count);
    839 	if (error)
    840 		return error;
    841 	error = copyin(cm->blue, &bbuf[index], count);
    842 	if (error)
    843 		return error;
    844 
    845 	for (i = 0; i < count; i++) {
    846 		sc->sc_cmap.cm_map[index][3] = rbuf[index];
    847 		sc->sc_cmap.cm_map[index][2] = gbuf[index];
    848 		sc->sc_cmap.cm_map[index][1] = bbuf[index];
    849 
    850 		index++;
    851 	}
    852 	cg14_load_hwcmap(sc, 0, 256);
    853 	return 0;
    854 }
    855 
    856 static int
    857 cg14_getcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
    858 {
    859 	uint8_t rbuf[256], gbuf[256], bbuf[256];
    860 	u_int index = cm->index;
    861 	u_int count = cm->count;
    862 	int error, i;
    863 
    864 	if (index >= 255 || count > 256 || index + count > 256)
    865 		return EINVAL;
    866 
    867 
    868 	for (i = 0; i < count; i++) {
    869 		rbuf[i] = sc->sc_cmap.cm_map[index][3];
    870 		gbuf[i] = sc->sc_cmap.cm_map[index][2];
    871 		bbuf[i] = sc->sc_cmap.cm_map[index][1];
    872 
    873 		index++;
    874 	}
    875 	error = copyout(rbuf,   cm->red,   count);
    876 	if (error)
    877 		return error;
    878 	error = copyout(gbuf, cm->green, count);
    879 	if (error)
    880 		return error;
    881 	error = copyout(bbuf,  cm->blue,  count);
    882 	if (error)
    883 		return error;
    884 
    885 	return 0;
    886 }
    887 
    888 static int
    889 cg14_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    890 	struct lwp *l)
    891 {
    892 	struct vcons_data *vd = v;
    893 	struct cgfourteen_softc *sc = vd->cookie;
    894 	struct wsdisplay_fbinfo *wdf;
    895 	struct vcons_screen *ms = vd->active;
    896 
    897 	switch (cmd) {
    898 
    899 		case WSDISPLAYIO_GTYPE:
    900 			*(uint32_t *)data = WSDISPLAY_TYPE_SUNCG14;
    901 			return 0;
    902 
    903 		case WSDISPLAYIO_GINFO:
    904 			wdf = (void *)data;
    905 			wdf->height = ms->scr_ri.ri_height;
    906 			wdf->width = ms->scr_ri.ri_width;
    907 			wdf->depth = 32;
    908 			wdf->cmsize = 256;
    909 			return 0;
    910 
    911 		case WSDISPLAYIO_GETCMAP:
    912 			return cg14_getcmap(sc,
    913 			    (struct wsdisplay_cmap *)data);
    914 
    915 		case WSDISPLAYIO_PUTCMAP:
    916 			return cg14_putcmap(sc,
    917 			    (struct wsdisplay_cmap *)data);
    918 
    919 		case WSDISPLAYIO_LINEBYTES:
    920 			*(u_int *)data = ms->scr_ri.ri_stride << 2;
    921 			return 0;
    922 
    923 		case WSDISPLAYIO_SMODE:
    924 			{
    925 				int new_mode = *(int*)data;
    926 				if (new_mode != sc->sc_mode) {
    927 					sc->sc_mode = new_mode;
    928 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    929 						bus_space_write_1(sc->sc_bustag,
    930 						    sc->sc_regh,
    931 						    CG14_CURSOR_CONTROL, 0);
    932 
    933 						cg14_set_depth(sc, 8);
    934 						cg14_init_cmap(sc);
    935 						vcons_redraw_screen(ms);
    936 					} else {
    937 
    938 						cg14_set_depth(sc, 32);
    939 					}
    940 				}
    941 			}
    942 			return 0;
    943 		case WSDISPLAYIO_SVIDEO:
    944 			cg14_set_video(sc, *(int *)data);
    945 			return 0;
    946 		case WSDISPLAYIO_GVIDEO:
    947 			return cg14_get_video(sc) ?
    948 			    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
    949 		case WSDISPLAYIO_GCURPOS:
    950 			{
    951 				struct wsdisplay_curpos *cp = (void *)data;
    952 
    953 				cp->x = sc->sc_cursor.cc_pos.x;
    954 				cp->y = sc->sc_cursor.cc_pos.y;
    955 			}
    956 			return 0;
    957 		case WSDISPLAYIO_SCURPOS:
    958 			{
    959 				struct wsdisplay_curpos *cp = (void *)data;
    960 
    961 				cg14_move_cursor(sc, cp->x, cp->y);
    962 			}
    963 			return 0;
    964 		case WSDISPLAYIO_GCURMAX:
    965 			{
    966 				struct wsdisplay_curpos *cp = (void *)data;
    967 
    968 				cp->x = 32;
    969 				cp->y = 32;
    970 			}
    971 			return 0;
    972 		case WSDISPLAYIO_SCURSOR:
    973 			{
    974 				struct wsdisplay_cursor *cursor = (void *)data;
    975 
    976 				return cg14_do_cursor(sc, cursor);
    977 			}
    978 		case PCI_IOC_CFGREAD:
    979 		case PCI_IOC_CFGWRITE:
    980 			return EINVAL;
    981 
    982 	}
    983 	return EPASSTHROUGH;
    984 }
    985 
    986 static paddr_t
    987 cg14_mmap(void *v, void *vs, off_t offset, int prot)
    988 {
    989 	struct vcons_data *vd = v;
    990 	struct cgfourteen_softc *sc = vd->cookie;
    991 
    992 	/* allow mmap()ing the full framebuffer, not just what we use */
    993 	if (offset < sc->sc_vramsize)
    994 		return bus_space_mmap(sc->sc_bustag,
    995 		    BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    996 		      sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
    997 		    offset + CG14_FB_CBGR, prot, BUS_SPACE_MAP_LINEAR);
    998 
    999 	return -1;
   1000 }
   1001 
   1002 static void
   1003 cg14_init_screen(void *cookie, struct vcons_screen *scr,
   1004     int existing, long *defattr)
   1005 {
   1006 	struct cgfourteen_softc *sc = cookie;
   1007 	struct rasops_info *ri = &scr->scr_ri;
   1008 
   1009 	ri->ri_depth = 8;
   1010 	ri->ri_width = sc->sc_fb.fb_type.fb_width;
   1011 	ri->ri_height = sc->sc_fb.fb_type.fb_height;
   1012 	ri->ri_stride = ri->ri_width;
   1013 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
   1014 
   1015 	ri->ri_bits = (char *)sc->sc_fb.fb_pixels;
   1016 #if NSX > 0
   1017 	/*
   1018 	 * unaligned copies with horizontal overlap are slow, so don't bother
   1019 	 * handling them in cg14_bitblt() and use putchar() instead
   1020 	 */
   1021 	if (sc->sc_sx != NULL) {
   1022 		scr->scr_flags |= VCONS_NO_COPYCOLS;
   1023 	} else
   1024 #endif
   1025 	scr->scr_flags |= VCONS_DONT_READ;
   1026 
   1027 	if (existing) {
   1028 		ri->ri_flg |= RI_CLEAR;
   1029 	}
   1030 
   1031 	rasops_init(ri, 0, 0);
   1032 	ri->ri_caps = WSSCREEN_WSCOLORS;
   1033 
   1034 	rasops_reconfig(ri,
   1035 	    sc->sc_fb.fb_type.fb_height / ri->ri_font->fontheight,
   1036 	    sc->sc_fb.fb_type.fb_width / ri->ri_font->fontwidth);
   1037 
   1038 	ri->ri_hw = scr;
   1039 #if NSX > 0
   1040 	if (sc->sc_sx != NULL) {
   1041 		ri->ri_ops.copyrows = cg14_copyrows;
   1042 		ri->ri_ops.copycols = cg14_copycols;
   1043 		ri->ri_ops.eraserows = cg14_eraserows;
   1044 		ri->ri_ops.erasecols = cg14_erasecols;
   1045 		ri->ri_ops.cursor = cg14_cursor;
   1046 #if 0
   1047 		if (FONT_IS_ALPHA(ri->ri_font)) {
   1048 			ri->ri_ops.putchar = cg14_putchar_aa;
   1049 		} else
   1050 #endif
   1051 			ri->ri_ops.putchar = cg14_putchar;
   1052 	}
   1053 #endif /* NSX > 0 */
   1054 }
   1055 
   1056 static void
   1057 cg14_set_depth(struct cgfourteen_softc *sc, int depth)
   1058 {
   1059 	int i;
   1060 
   1061 	if (sc->sc_depth == depth)
   1062 		return;
   1063 
   1064 	switch (depth) {
   1065 		case 8:
   1066 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1067 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
   1068 			    CG14_MCTL_PIXMODE_8 | CG14_MCTL_POWERCTL);
   1069 			sc->sc_depth = 8;
   1070 			/* everything is CLUT1 */
   1071 			for (i = 0; i < CG14_CLUT_SIZE; i++)
   1072 			     sc->sc_xlut->xlut_lut[i] = 0;
   1073 			break;
   1074 		case 32:
   1075 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1076 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
   1077 			    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL);
   1078 			sc->sc_depth = 32;
   1079 			for (i = 0; i < CG14_CLUT_SIZE; i++)
   1080 			     sc->sc_xlut->xlut_lut[i] = 0;
   1081 			break;
   1082 		default:
   1083 			printf("%s: can't change to depth %d\n",
   1084 			    device_xname(sc->sc_dev), depth);
   1085 	}
   1086 }
   1087 
   1088 static void
   1089 cg14_move_cursor(struct cgfourteen_softc *sc, int x, int y)
   1090 {
   1091 	uint32_t pos;
   1092 
   1093 	sc->sc_cursor.cc_pos.x = x;
   1094 	sc->sc_cursor.cc_pos.y = y;
   1095 	pos = ((sc->sc_cursor.cc_pos.x - sc->sc_cursor.cc_hot.x ) << 16) |
   1096 	      ((sc->sc_cursor.cc_pos.y - sc->sc_cursor.cc_hot.y ) & 0xffff);
   1097 	bus_space_write_4(sc->sc_bustag, sc->sc_regh, CG14_CURSOR_X, pos);
   1098 }
   1099 
   1100 static int
   1101 cg14_do_cursor(struct cgfourteen_softc *sc, struct wsdisplay_cursor *cur)
   1102 {
   1103 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1104 
   1105 		bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1106 		    CG14_CURSOR_CONTROL, cur->enable ? CG14_CRSR_ENABLE : 0);
   1107 	}
   1108 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1109 
   1110 		sc->sc_cursor.cc_hot.x = cur->hot.x;
   1111 		sc->sc_cursor.cc_hot.y = cur->hot.y;
   1112 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1113 	}
   1114 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1115 
   1116 		cg14_move_cursor(sc, cur->pos.x, cur->pos.y);
   1117 	}
   1118 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1119 		int i;
   1120 		uint32_t val;
   1121 
   1122 		for (i = 0; i < min(cur->cmap.count, 3); i++) {
   1123 			val = (cur->cmap.red[i] ) |
   1124 			      (cur->cmap.green[i] << 8) |
   1125 			      (cur->cmap.blue[i] << 16);
   1126 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1127 			    CG14_CURSOR_COLOR1 + ((i + cur->cmap.index) << 2),
   1128 			    val);
   1129 		}
   1130 	}
   1131 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1132 		uint32_t buffer[32], latch, tmp;
   1133 		int i;
   1134 
   1135 		copyin(cur->mask, buffer, 128);
   1136 		for (i = 0; i < 32; i++) {
   1137 			latch = 0;
   1138 			tmp = buffer[i] & 0x80808080;
   1139 			latch |= tmp >> 7;
   1140 			tmp = buffer[i] & 0x40404040;
   1141 			latch |= tmp >> 5;
   1142 			tmp = buffer[i] & 0x20202020;
   1143 			latch |= tmp >> 3;
   1144 			tmp = buffer[i] & 0x10101010;
   1145 			latch |= tmp >> 1;
   1146 			tmp = buffer[i] & 0x08080808;
   1147 			latch |= tmp << 1;
   1148 			tmp = buffer[i] & 0x04040404;
   1149 			latch |= tmp << 3;
   1150 			tmp = buffer[i] & 0x02020202;
   1151 			latch |= tmp << 5;
   1152 			tmp = buffer[i] & 0x01010101;
   1153 			latch |= tmp << 7;
   1154 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1155 			    CG14_CURSOR_PLANE0 + (i << 2), latch);
   1156 		}
   1157 		copyin(cur->image, buffer, 128);
   1158 		for (i = 0; i < 32; i++) {
   1159 			latch = 0;
   1160 			tmp = buffer[i] & 0x80808080;
   1161 			latch |= tmp >> 7;
   1162 			tmp = buffer[i] & 0x40404040;
   1163 			latch |= tmp >> 5;
   1164 			tmp = buffer[i] & 0x20202020;
   1165 			latch |= tmp >> 3;
   1166 			tmp = buffer[i] & 0x10101010;
   1167 			latch |= tmp >> 1;
   1168 			tmp = buffer[i] & 0x08080808;
   1169 			latch |= tmp << 1;
   1170 			tmp = buffer[i] & 0x04040404;
   1171 			latch |= tmp << 3;
   1172 			tmp = buffer[i] & 0x02020202;
   1173 			latch |= tmp << 5;
   1174 			tmp = buffer[i] & 0x01010101;
   1175 			latch |= tmp << 7;
   1176 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1177 			    CG14_CURSOR_PLANE1 + (i << 2), latch);
   1178 		}
   1179 	}
   1180 	return 0;
   1181 }
   1182 
   1183 #if NSX > 0
   1184 
   1185 static void
   1186 cg14_wait_idle(struct cgfourteen_softc *sc)
   1187 {
   1188 }
   1189 
   1190 static void
   1191 cg14_rectfill(struct cgfourteen_softc *sc, int x, int y, int wi, int he,
   1192      uint32_t colour)
   1193 {
   1194 	uint32_t addr, pptr;
   1195 	int line, cnt, pre, words;
   1196 	int stride = sc->sc_fb.fb_type.fb_width;
   1197 
   1198 	addr = sc->sc_fb_paddr + x + stride * y;
   1199 	sx_write(sc->sc_sx, SX_QUEUED(8), colour);
   1200 	sx_write(sc->sc_sx, SX_QUEUED(9), colour);
   1201 	/*
   1202 	 * Calculate the number of pixels we need to do one by one
   1203 	 * until we're 32bit aligned, then do the rest in 32bit
   1204 	 * mode. Assumes that stride is always a multiple of 4.
   1205 	 */
   1206 	pre = addr & 3;
   1207 	if (pre != 0) pre = 4 - pre;
   1208 	for (line = 0; line < he; line++) {
   1209 		pptr = addr;
   1210 		cnt = wi;
   1211 		if (pre) {
   1212 			sta(pptr, ASI_SX, SX_STBS(8, pre - 1, pptr & 7));
   1213 			pptr += pre;
   1214 			cnt -= pre;
   1215 		}
   1216 		/* now do the aligned pixels in 32bit chunks */
   1217 		while(cnt > 31) {
   1218 			words = min(32, cnt >> 2);
   1219 			sta(pptr, ASI_SX, SX_STS(8, words - 1, pptr & 7));
   1220 			pptr += words << 2;
   1221 			cnt -= words << 2;
   1222 		}
   1223 		/* do any remaining pixels byte-wise again */
   1224 		if (cnt > 0)
   1225 			sta(pptr, ASI_SX, SX_STBS(8, cnt - 1, pptr & 7));
   1226 		addr += stride;
   1227 	}
   1228 }
   1229 
   1230 static void
   1231 cg14_invert(struct cgfourteen_softc *sc, int x, int y, int wi, int he)
   1232 {
   1233 	uint32_t addr, pptr;
   1234 	int line, cnt, pre, words;
   1235 	int stride = sc->sc_fb.fb_type.fb_width;
   1236 
   1237 	addr = sc->sc_fb_paddr + x + stride * y;
   1238 	sx_write(sc->sc_sx, SX_ROP_CONTROL, 0x33); /* ~src a */
   1239 	/*
   1240 	 * Calculate the number of pixels we need to do one by one
   1241 	 * until we're 32bit aligned, then do the rest in 32bit
   1242 	 * mode. Assumes that stride is always a multiple of 4.
   1243 	 */
   1244 	pre = addr & 3;
   1245 	if (pre != 0) pre = 4 - pre;
   1246 	for (line = 0; line < he; line++) {
   1247 		pptr = addr;
   1248 		cnt = wi;
   1249 		if (pre) {
   1250 			sta(pptr, ASI_SX, SX_LDB(8, pre - 1, pptr & 7));
   1251 			sx_write(sc->sc_sx, SX_INSTRUCTIONS,
   1252 			    SX_ROP(8, 8, 32, pre - 1));
   1253 			sta(pptr, ASI_SX, SX_STB(32, pre - 1, pptr & 7));
   1254 			pptr += pre;
   1255 			cnt -= pre;
   1256 		}
   1257 		/* now do the aligned pixels in 32bit chunks */
   1258 		while(cnt > 15) {
   1259 			words = min(16, cnt >> 2);
   1260 			sta(pptr, ASI_SX, SX_LD(8, words - 1, pptr & 7));
   1261 			sx_write(sc->sc_sx, SX_INSTRUCTIONS,
   1262 			    SX_ROP(8, 8, 32, words - 1));
   1263 			sta(pptr, ASI_SX, SX_ST(32, words - 1, pptr & 7));
   1264 			pptr += words << 2;
   1265 			cnt -= words << 2;
   1266 		}
   1267 		/* do any remaining pixels byte-wise again */
   1268 		if (cnt > 0)
   1269 			sta(pptr, ASI_SX, SX_LDB(8, cnt - 1, pptr & 7));
   1270 			sx_write(sc->sc_sx, SX_INSTRUCTIONS,
   1271 			    SX_ROP(8, 8, 32, cnt - 1));
   1272 			sta(pptr, ASI_SX, SX_STB(32, cnt - 1, pptr & 7));
   1273 		addr += stride;
   1274 	}
   1275 }
   1276 
   1277 static inline void
   1278 cg14_slurp(int reg, uint32_t addr, int cnt)
   1279 {
   1280 	int num;
   1281 	while (cnt > 0) {
   1282 		num = min(32, cnt);
   1283 		sta(addr, ASI_SX, SX_LD(reg, num - 1, addr & 7));
   1284 		cnt -= num;
   1285 		reg += num;
   1286 		addr += (num << 2);
   1287 	}
   1288 }
   1289 
   1290 static inline void
   1291 cg14_spit(int reg, uint32_t addr, int cnt)
   1292 {
   1293 	int num;
   1294 	while (cnt > 0) {
   1295 		num = min(32, cnt);
   1296 		sta(addr, ASI_SX, SX_ST(reg, num - 1, addr & 7));
   1297 		cnt -= num;
   1298 		reg += num;
   1299 		addr += (num << 2);
   1300 	}
   1301 }
   1302 
   1303 static void
   1304 cg14_bitblt(void *cookie, int xs, int ys, int xd, int yd,
   1305     int wi, int he, int rop)
   1306 {
   1307 	struct cgfourteen_softc *sc = cookie;
   1308 	uint32_t saddr, daddr, sptr, dptr;
   1309 	int line, cnt, stride = sc->sc_fb.fb_type.fb_width;
   1310 	int num, words, skip;
   1311 
   1312 	if (ys < yd) {
   1313 		/* need to go bottom-up */
   1314 		saddr = sc->sc_fb_paddr + xs + stride * (ys + he - 1);
   1315 		daddr = sc->sc_fb_paddr + xd + stride * (yd + he - 1);
   1316 		skip = -stride;
   1317 	} else {
   1318 		saddr = sc->sc_fb_paddr + xs + stride * ys;
   1319 		daddr = sc->sc_fb_paddr + xd + stride * yd;
   1320 		skip = stride;
   1321 	}
   1322 
   1323 	if ((saddr & 3) == (daddr & 3)) {
   1324 		int pre = saddr & 3;	/* pixels to copy byte-wise */
   1325 		if (pre != 0) pre = 4 - pre;
   1326 		for (line = 0; line < he; line++) {
   1327 			sptr = saddr;
   1328 			dptr = daddr;
   1329 			cnt = wi;
   1330 			if (pre > 0) {
   1331 				sta(sptr, ASI_SX,
   1332 				    SX_LDB(32, pre - 1, sptr & 7));
   1333 				sta(dptr, ASI_SX,
   1334 				    SX_STB(32, pre - 1, dptr & 7));
   1335 				cnt -= pre;
   1336 				sptr += pre;
   1337 				dptr += pre;
   1338 			}
   1339 			words = cnt >> 2;
   1340 			while(cnt > 3) {
   1341 				num = min(120, words);
   1342 				cg14_slurp(8, sptr, num);
   1343 				cg14_spit(8, dptr, num);
   1344 				sptr += num << 2;
   1345 				dptr += num << 2;
   1346 				cnt -= num << 2;
   1347 			}
   1348 			if (cnt > 0) {
   1349 				sta(sptr, ASI_SX,
   1350 				    SX_LDB(32, cnt - 1, sptr & 7));
   1351 				sta(dptr, ASI_SX,
   1352 				    SX_STB(32, cnt - 1, dptr & 7));
   1353 			}
   1354 			saddr += skip;
   1355 			daddr += skip;
   1356 		}
   1357 	} else {
   1358 		/* unaligned, have to use byte mode */
   1359 		for (line = 0; line < he; line++) {
   1360 			sptr = saddr;
   1361 			dptr = daddr;
   1362 			cnt = wi;
   1363 			while(cnt > 31) {
   1364 				sta(sptr, ASI_SX, SX_LDB(32, 31, sptr & 7));
   1365 				sta(dptr, ASI_SX, SX_STB(32, 31, dptr & 7));
   1366 				sptr += 32;
   1367 				dptr += 32;
   1368 				cnt -= 32;
   1369 			}
   1370 			if (cnt > 0) {
   1371 				sta(sptr, ASI_SX,
   1372 				    SX_LDB(32, cnt - 1, sptr & 7));
   1373 				sta(dptr, ASI_SX,
   1374 				    SX_STB(32, cnt - 1, dptr & 7));
   1375 			}
   1376 			saddr += skip;
   1377 			daddr += skip;
   1378 		}
   1379 	}
   1380 }
   1381 
   1382 
   1383 static void
   1384 cg14_putchar(void *cookie, int row, int col, u_int c, long attr)
   1385 {
   1386 	struct rasops_info *ri = cookie;
   1387 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1388 	struct vcons_screen *scr = ri->ri_hw;
   1389 	struct cgfourteen_softc *sc = scr->scr_cookie;
   1390 	void *data;
   1391 	uint32_t fg, bg;
   1392 	int i, x, y, wi, he;
   1393 	uint32_t addr;
   1394 	int stride = sc->sc_fb.fb_type.fb_width;
   1395 
   1396 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1397 		return;
   1398 
   1399 	if (!CHAR_IN_FONT(c, font))
   1400 		return;
   1401 
   1402 	wi = font->fontwidth;
   1403 	he = font->fontheight;
   1404 
   1405 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1406 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
   1407 	sx_write(sc->sc_sx, SX_QUEUED(8), bg);
   1408 	sx_write(sc->sc_sx, SX_QUEUED(9), fg);
   1409 
   1410 	x = ri->ri_xorigin + col * wi;
   1411 	y = ri->ri_yorigin + row * he;
   1412 
   1413 	if (c == 0x20) {
   1414 		cg14_rectfill(sc, x, y, wi, he, bg);
   1415 		return;
   1416 	}
   1417 
   1418 	data = WSFONT_GLYPH(c, font);
   1419 	addr = sc->sc_fb_paddr + x + stride * y;
   1420 
   1421 	switch (font->stride) {
   1422 		case 1: {
   1423 			uint8_t *data8 = data;
   1424 			uint32_t reg;
   1425 			for (i = 0; i < he; i++) {
   1426 				reg = *data8;
   1427 				sx_write(sc->sc_sx, SX_QUEUED(R_MASK),
   1428 				    reg << 24);
   1429 				sta(addr, ASI_SX, SX_STBS(8, wi - 1, addr & 7));
   1430 				data8++;
   1431 				addr += stride;
   1432 			}
   1433 			break;
   1434 		}
   1435 		case 2: {
   1436 			uint16_t *data16 = data;
   1437 			uint32_t reg;
   1438 			for (i = 0; i < he; i++) {
   1439 				reg = *data16;
   1440 				sx_write(sc->sc_sx, SX_QUEUED(R_MASK),
   1441 				    reg << 16);
   1442 				sta(addr, ASI_SX, SX_STBS(8, wi - 1, addr & 7));
   1443 				data16++;
   1444 				addr += stride;
   1445 			}
   1446 			break;
   1447 		}
   1448 	}
   1449 }
   1450 
   1451 static void
   1452 cg14_cursor(void *cookie, int on, int row, int col)
   1453 {
   1454 	struct rasops_info *ri = cookie;
   1455 	struct vcons_screen *scr = ri->ri_hw;
   1456 	struct cgfourteen_softc *sc = scr->scr_cookie;
   1457 	int x, y, wi, he;
   1458 
   1459 	wi = ri->ri_font->fontwidth;
   1460 	he = ri->ri_font->fontheight;
   1461 
   1462 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1463 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1464 		y = ri->ri_crow * he + ri->ri_yorigin;
   1465 		if (ri->ri_flg & RI_CURSOR) {
   1466 			cg14_invert(sc, x, y, wi, he);
   1467 			ri->ri_flg &= ~RI_CURSOR;
   1468 		}
   1469 		ri->ri_crow = row;
   1470 		ri->ri_ccol = col;
   1471 		if (on) {
   1472 			x = ri->ri_ccol * wi + ri->ri_xorigin;
   1473 			y = ri->ri_crow * he + ri->ri_yorigin;
   1474 			cg14_invert(sc, x, y, wi, he);
   1475 			ri->ri_flg |= RI_CURSOR;
   1476 		}
   1477 	} else {
   1478 		scr->scr_ri.ri_crow = row;
   1479 		scr->scr_ri.ri_ccol = col;
   1480 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
   1481 	}
   1482 
   1483 }
   1484 
   1485 #if 0
   1486 static void
   1487 r128fb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
   1488 {
   1489 	struct rasops_info *ri = cookie;
   1490 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1491 	struct vcons_screen *scr = ri->ri_hw;
   1492 	struct r128fb_softc *sc = scr->scr_cookie;
   1493 	uint32_t bg, latch = 0, bg8, fg8, pixel;
   1494 	int i, x, y, wi, he, r, g, b, aval;
   1495 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
   1496 	uint8_t *data8;
   1497 	int rv, cnt = 0;
   1498 
   1499 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1500 		return;
   1501 
   1502 	if (!CHAR_IN_FONT(c, font))
   1503 		return;
   1504 
   1505 	wi = font->fontwidth;
   1506 	he = font->fontheight;
   1507 
   1508 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1509 	x = ri->ri_xorigin + col * wi;
   1510 	y = ri->ri_yorigin + row * he;
   1511 	if (c == 0x20) {
   1512 		r128fb_rectfill(sc, x, y, wi, he, bg);
   1513 		return;
   1514 	}
   1515 
   1516 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1517 	if (rv == GC_OK)
   1518 		return;
   1519 
   1520 	data8 = WSFONT_GLYPH(c, font);
   1521 
   1522 	r128fb_wait(sc, 5);
   1523 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1524 	    R128_DP_GUI_MASTER_CNTL,
   1525 	    R128_GMC_BRUSH_SOLID_COLOR |
   1526 	    R128_GMC_SRC_DATATYPE_COLOR |
   1527 	    R128_ROP3_S |
   1528 	    R128_DP_SRC_SOURCE_HOST_DATA |
   1529 	    sc->sc_master_cntl);
   1530 
   1531 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1532 	    R128_DP_CNTL,
   1533 	    R128_DST_Y_TOP_TO_BOTTOM |
   1534 	    R128_DST_X_LEFT_TO_RIGHT);
   1535 
   1536 	/* needed? */
   1537 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0);
   1538 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
   1539 	    (x << 16) | y);
   1540 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
   1541 	    (wi << 16) | he);
   1542 
   1543 	/*
   1544 	 * we need the RGB colours here, so get offsets into rasops_cmap
   1545 	 */
   1546 	fgo = ((attr >> 24) & 0xf) * 3;
   1547 	bgo = ((attr >> 16) & 0xf) * 3;
   1548 
   1549 	r0 = rasops_cmap[bgo];
   1550 	r1 = rasops_cmap[fgo];
   1551 	g0 = rasops_cmap[bgo + 1];
   1552 	g1 = rasops_cmap[fgo + 1];
   1553 	b0 = rasops_cmap[bgo + 2];
   1554 	b1 = rasops_cmap[fgo + 2];
   1555 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
   1556 	bg8 = R3G3B2(r0, g0, b0);
   1557 	fg8 = R3G3B2(r1, g1, b1);
   1558 
   1559 	r128fb_wait(sc, 16);
   1560 
   1561 	for (i = 0; i < ri->ri_fontscale; i++) {
   1562 		aval = *data8;
   1563 		if (aval == 0) {
   1564 			pixel = bg8;
   1565 		} else if (aval == 255) {
   1566 			pixel = fg8;
   1567 		} else {
   1568 			r = aval * r1 + (255 - aval) * r0;
   1569 			g = aval * g1 + (255 - aval) * g0;
   1570 			b = aval * b1 + (255 - aval) * b0;
   1571 			pixel = ((r & 0xe000) >> 8) |
   1572 				((g & 0xe000) >> 11) |
   1573 				((b & 0xc000) >> 14);
   1574 		}
   1575 		latch = (latch << 8) | pixel;
   1576 		/* write in 32bit chunks */
   1577 		if ((i & 3) == 3) {
   1578 			bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
   1579 			    R128_HOST_DATA0, latch);
   1580 			/*
   1581 			 * not strictly necessary, old data should be shifted
   1582 			 * out
   1583 			 */
   1584 			latch = 0;
   1585 			cnt++;
   1586 			if (cnt > 15) {
   1587 				r128fb_wait(sc, 16);
   1588 				cnt = 0;
   1589 			}
   1590 		}
   1591 		data8++;
   1592 	}
   1593 	/* if we have pixels left in latch write them out */
   1594 	if ((i & 3) != 0) {
   1595 		latch = latch << ((4 - (i & 3)) << 3);
   1596 		bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
   1597 				    R128_HOST_DATA0, latch);
   1598 	}
   1599 	if (rv == GC_ADD) {
   1600 		glyphcache_add(&sc->sc_gc, c, x, y);
   1601 	}
   1602 }
   1603 #endif
   1604 
   1605 static void
   1606 cg14_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1607 {
   1608 	struct rasops_info *ri = cookie;
   1609 	struct vcons_screen *scr = ri->ri_hw;
   1610 	struct cgfourteen_softc *sc = scr->scr_cookie;
   1611 	int32_t xs, xd, y, width, height;
   1612 
   1613 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1614 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1615 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1616 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1617 		width = ri->ri_font->fontwidth * ncols;
   1618 		height = ri->ri_font->fontheight;
   1619 		cg14_bitblt(sc, xs, y, xd, y, width, height, 0x0c);
   1620 	}
   1621 }
   1622 
   1623 static void
   1624 cg14_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1625 {
   1626 	struct rasops_info *ri = cookie;
   1627 	struct vcons_screen *scr = ri->ri_hw;
   1628 	struct cgfourteen_softc *sc = scr->scr_cookie;
   1629 	int32_t x, y, width, height, fg, bg, ul;
   1630 
   1631 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1632 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1633 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1634 		width = ri->ri_font->fontwidth * ncols;
   1635 		height = ri->ri_font->fontheight;
   1636 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1637 
   1638 		cg14_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1639 	}
   1640 }
   1641 
   1642 static void
   1643 cg14_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1644 {
   1645 	struct rasops_info *ri = cookie;
   1646 	struct vcons_screen *scr = ri->ri_hw;
   1647 	struct cgfourteen_softc *sc = scr->scr_cookie;
   1648 	int32_t x, ys, yd, width, height;
   1649 
   1650 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1651 		x = ri->ri_xorigin;
   1652 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1653 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1654 		width = ri->ri_emuwidth;
   1655 		height = ri->ri_font->fontheight * nrows;
   1656 		cg14_bitblt(sc, x, ys, x, yd, width, height, 0x0c);
   1657 	}
   1658 }
   1659 
   1660 static void
   1661 cg14_eraserows(void *cookie, int row, int nrows, long fillattr)
   1662 {
   1663 	struct rasops_info *ri = cookie;
   1664 	struct vcons_screen *scr = ri->ri_hw;
   1665 	struct cgfourteen_softc *sc = scr->scr_cookie;
   1666 	int32_t x, y, width, height, fg, bg, ul;
   1667 
   1668 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1669 		x = ri->ri_xorigin;
   1670 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1671 		width = ri->ri_emuwidth;
   1672 		height = ri->ri_font->fontheight * nrows;
   1673 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1674 
   1675 		cg14_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1676 	}
   1677 }
   1678 
   1679 #endif /* NSX > 0 */
   1680 
   1681 #endif
   1682