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