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