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