Home | History | Annotate | Line # | Download | only in dev
cgfourteen.c revision 1.55
      1 /*	$NetBSD: cgfourteen.c,v 1.55 2008/12/12 18:52:40 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  * Provides minimum emulation of a Sun cgthree 8-bit framebuffer to
     57  * allow X to run.
     58  *
     59  * Does not handle interrupts, even though they can occur.
     60  *
     61  * XXX should defer colormap updates to vertical retrace interrupts
     62  */
     63 
     64 /*
     65  * The following is for debugging only; it opens up a security hole
     66  * enabled by allowing any user to map the control registers for the
     67  * cg14 into their space.
     68  */
     69 #undef CG14_MAP_REGS
     70 
     71 /*
     72  * The following enables 24-bit operation: when opened, the framebuffer
     73  * will switch to 24-bit mode (actually 32-bit mode), and provide a
     74  * simple cg8 emulation.
     75  */
     76 #define CG14_CG8
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/buf.h>
     81 #include <sys/device.h>
     82 #include <sys/ioctl.h>
     83 #include <sys/malloc.h>
     84 #include <sys/kmem.h>
     85 #include <sys/mman.h>
     86 #include <sys/tty.h>
     87 #include <sys/conf.h>
     88 #include <dev/pci/pciio.h>
     89 
     90 #include <uvm/uvm_extern.h>
     91 
     92 #include <dev/sun/fbio.h>
     93 #include <machine/autoconf.h>
     94 #include <machine/pmap.h>
     95 #include <dev/sun/fbvar.h>
     96 #include <machine/cpu.h>
     97 #include <dev/sbus/sbusvar.h>
     98 
     99 #include "wsdisplay.h"
    100 #include <dev/wscons/wsconsio.h>
    101 #include <dev/wsfont/wsfont.h>
    102 #include <dev/rasops/rasops.h>
    103 
    104 #include <dev/wscons/wsdisplay_vconsvar.h>
    105 
    106 #include <sparc/dev/cgfourteenreg.h>
    107 #include <sparc/dev/cgfourteenvar.h>
    108 
    109 #include "opt_wsemul.h"
    110 
    111 /* autoconfiguration driver */
    112 static int	cgfourteenmatch(device_t, struct cfdata *, void *);
    113 static void	cgfourteenattach(device_t, device_t, void *);
    114 static void	cgfourteenunblank(device_t);
    115 
    116 CFATTACH_DECL_NEW(cgfourteen, sizeof(struct cgfourteen_softc),
    117     cgfourteenmatch, cgfourteenattach, NULL, NULL);
    118 
    119 extern struct cfdriver cgfourteen_cd;
    120 
    121 dev_type_open(cgfourteenopen);
    122 dev_type_close(cgfourteenclose);
    123 dev_type_ioctl(cgfourteenioctl);
    124 dev_type_mmap(cgfourteenmmap);
    125 dev_type_poll(cgfourteenpoll);
    126 
    127 const struct cdevsw cgfourteen_cdevsw = {
    128         cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
    129         nostop, notty, cgfourteenpoll, cgfourteenmmap, nokqfilter,
    130 };
    131 
    132 /* frame buffer generic driver */
    133 static struct fbdriver cgfourteenfbdriver = {
    134 	cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
    135 	cgfourteenpoll, cgfourteenmmap, nokqfilter
    136 };
    137 
    138 extern struct tty *fbconstty;
    139 
    140 static void cg14_set_video(struct cgfourteen_softc *, int);
    141 static int  cg14_get_video(struct cgfourteen_softc *);
    142 static int  cg14_get_cmap(struct fbcmap *, union cg14cmap *, int);
    143 static int  cg14_put_cmap(struct fbcmap *, union cg14cmap *, int);
    144 static void cg14_load_hwcmap(struct cgfourteen_softc *, int, int);
    145 static void cg14_init(struct cgfourteen_softc *);
    146 static void cg14_reset(struct cgfourteen_softc *);
    147 
    148 #if NWSDISPLAY > 0
    149 static void cg14_setup_wsdisplay(struct cgfourteen_softc *, int);
    150 static void cg14_init_cmap(struct cgfourteen_softc *);
    151 static int  cg14_putcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
    152 static int  cg14_getcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
    153 static void cg14_set_depth(struct cgfourteen_softc *, int);
    154 static void cg14_move_cursor(struct cgfourteen_softc *, int, int);
    155 static int  cg14_do_cursor(struct cgfourteen_softc *,
    156                            struct wsdisplay_cursor *);
    157 #endif
    158 
    159 #if defined(RASTERCONSOLE) && (NWSDISPLAY > 0)
    160 #error You can't have it both ways - either RASTERCONSOLE or wsdisplay
    161 #endif
    162 
    163 /*
    164  * Match a cgfourteen.
    165  */
    166 int
    167 cgfourteenmatch(device_t parent, struct cfdata *cf, void *aux)
    168 {
    169 	union obio_attach_args *uoba = aux;
    170 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    171 
    172 	/*
    173 	 * The cgfourteen is a local-bus video adaptor, accessed directly
    174 	 * via the processor, and not through device space or an external
    175 	 * bus. Thus we look _only_ at the obio bus.
    176 	 * Additionally, these things exist only on the Sun4m.
    177 	 */
    178 
    179 	if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
    180 		return (0);
    181 
    182 	/* Check driver name */
    183 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
    184 }
    185 
    186 /*
    187  * Set COLOUR_OFFSET to the offset of the video RAM.  This is to provide
    188  *  space for faked overlay junk for the cg8 emulation.
    189  *
    190  * As it happens, this value is correct for both cg3 and cg8 emulation!
    191  */
    192 #define COLOUR_OFFSET (256*1024)
    193 
    194 #ifdef RASTERCONSOLE
    195 static void cg14_set_rcons_luts(struct cgfourteen_softc *sc)
    196 {
    197 	int i;
    198 
    199 	for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_xlut->xlut_lut[i] = 0x22;
    200 	for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_clut2->clut_lut[i] = 0x00ffffff;
    201 	sc->sc_clut2->clut_lut[0] = 0x00ffffff;
    202 	sc->sc_clut2->clut_lut[255] = 0;
    203 }
    204 #endif /* RASTERCONSOLE */
    205 
    206 #if NWSDISPLAY > 0
    207 static int	cg14_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    208 static paddr_t	cg14_mmap(void *, void *, off_t, int);
    209 static void	cg14_init_screen(void *, struct vcons_screen *, int, long *);
    210 
    211 
    212 struct wsdisplay_accessops cg14_accessops = {
    213 	cg14_ioctl,
    214 	cg14_mmap,
    215 	NULL,	/* alloc_screen */
    216 	NULL,	/* free_screen */
    217 	NULL,	/* show_screen */
    218 	NULL, 	/* load_font */
    219 	NULL,	/* pollc */
    220 	NULL	/* scroll */
    221 };
    222 #endif
    223 
    224 /*
    225  * Attach a display.  We need to notice if it is the console, too.
    226  */
    227 void
    228 cgfourteenattach(device_t parent, device_t self, void *aux)
    229 {
    230 	union obio_attach_args *uoba = aux;
    231 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    232 	struct cgfourteen_softc *sc = device_private(self);
    233 	struct fbdevice *fb = &sc->sc_fb;
    234 	bus_space_handle_t bh;
    235 	int node, ramsize;
    236 	volatile uint32_t *lut;
    237 	int i, isconsole;
    238 
    239 	sc->sc_dev = self;
    240 	sc->sc_opens = 0;
    241 	node = sa->sa_node;
    242 
    243 	/* Remember cookies for cgfourteenmmap() */
    244 	sc->sc_bustag = sa->sa_bustag;
    245 
    246 	fb->fb_driver = &cgfourteenfbdriver;
    247 	fb->fb_device = sc->sc_dev;
    248 	/* Mask out invalid flags from the user. */
    249 	fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK;
    250 
    251 	/*
    252 	 * We're emulating a cg3/8, so represent ourselves as one
    253 	 */
    254 #ifdef CG14_CG8
    255 	fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
    256 	fb->fb_type.fb_depth = 32;
    257 #else
    258 	fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
    259 	fb->fb_type.fb_depth = 8;
    260 #endif
    261 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
    262 	ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, NBPG);
    263 
    264 	fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
    265 	fb->fb_type.fb_size = ramsize + COLOUR_OFFSET;
    266 
    267 	if (sa->sa_nreg < 2) {
    268 		printf("%s: only %d register sets\n",
    269 			self->dv_xname, sa->sa_nreg);
    270 		return;
    271 	}
    272 	bcopy(sa->sa_reg, sc->sc_physadr,
    273 	      sa->sa_nreg * sizeof(struct sbus_reg));
    274 
    275 	sc->sc_vramsize = sc->sc_physadr[CG14_PXL_IDX].sbr_size;
    276 
    277 	printf(": %d MB VRAM", (uint32_t)(sc->sc_vramsize >> 20));
    278 	/*
    279 	 * Now map in the 8 useful pages of registers
    280 	 */
    281 	if (sa->sa_size < 0x10000) {
    282 #ifdef DIAGNOSTIC
    283 		printf("warning: can't find all cgfourteen registers...\n");
    284 #endif
    285 		sa->sa_size = 0x10000;
    286 	}
    287 	if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    288 			 sa->sa_offset,
    289 			 sa->sa_size,
    290 			 0 /*BUS_SPACE_MAP_LINEAR*/,
    291 			 &bh) != 0) {
    292 		printf("%s: cannot map control registers\n", self->dv_xname);
    293 		return;
    294 	}
    295 	sc->sc_regh = bh;
    296 
    297 	sc->sc_ctl   = (struct cg14ctl  *) (bh);
    298 	sc->sc_hwc   = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
    299 	sc->sc_dac   = (struct cg14dac  *) (bh + CG14_OFFSET_DAC);
    300 	sc->sc_xlut  = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
    301 	sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
    302 	sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
    303 	sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
    304 	sc->sc_clutincr =        (u_int *) (bh + CG14_OFFSET_CLUTINCR);
    305 
    306 	/*
    307 	 * Let the user know that we're here
    308 	 */
    309 #ifdef CG14_CG8
    310 	printf(": cgeight emulated at %dx%dx24bpp",
    311 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    312 #else
    313 	printf(": cgthree emulated at %dx%dx8bpp",
    314 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    315 #endif
    316 	/*
    317 	 * Enable the video.
    318 	 */
    319 	cg14_set_video(sc, 1);
    320 
    321 	/*
    322 	 * Grab the initial colormap
    323 	 */
    324 	lut = sc->sc_clut1->clut_lut;
    325 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    326 		sc->sc_cmap.cm_chip[i] = lut[i];
    327 
    328 	/* See if we're the console */
    329         isconsole = fb_is_console(node);
    330 
    331 #if defined(RASTERCONSOLE)
    332 	if (isconsole) {
    333 		printf(" (console)\n");
    334 		/* *sbus*_bus_map?  but that's how we map the regs... */
    335 		if (sbus_bus_map( sc->sc_bustag,
    336 				  sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    337 				  sc->sc_physadr[CG14_PXL_IDX].sbr_offset +
    338 				    0x03800000,
    339 				  1152 * 900, BUS_SPACE_MAP_LINEAR,
    340 				  &bh) != 0) {
    341 			printf("%s: cannot map pixels\n",
    342 			    device_xname(sc->sc_dev));
    343 			return;
    344 		}
    345 		sc->sc_rcfb = sc->sc_fb;
    346 		sc->sc_rcfb.fb_type.fb_type = FBTYPE_SUN3COLOR;
    347 		sc->sc_rcfb.fb_type.fb_depth = 8;
    348 		sc->sc_rcfb.fb_linebytes = 1152;
    349 		sc->sc_rcfb.fb_type.fb_size = roundup(1152*900,NBPG);
    350 		sc->sc_rcfb.fb_pixels = (void *)bh;
    351 
    352 		printf("vram at %p\n",(void *)bh);
    353 		/* XXX should use actual screen size */
    354 
    355 		for (i = 0; i < ramsize; i++)
    356 		    ((unsigned char *)bh)[i] = 0;
    357 		fbrcons_init(&sc->sc_rcfb);
    358 		cg14_set_rcons_luts(sc);
    359 		sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID |
    360 		    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL;
    361 	} else
    362 		printf("\n");
    363 #endif
    364 
    365 #if NWSDISPLAY > 0
    366 	if (sbus_bus_map( sc->sc_bustag,
    367 	    sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    368 	    sc->sc_physadr[CG14_PXL_IDX].sbr_offset,
    369 	    ramsize, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    370 		printf("%s: cannot map pixels\n", device_xname(sc->sc_dev));
    371 		return;
    372 	}
    373 
    374 	sc->sc_fb.fb_pixels = bus_space_vaddr(sc->sc_bustag, bh);
    375 	sc->sc_shadowfb = kmem_alloc(ramsize, KM_NOSLEEP);
    376 
    377 	if (isconsole)
    378 		printf(" (console)\n");
    379 	else
    380 		printf("\n");
    381 
    382 	sc->sc_depth = 8;
    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  * Keep track of the number of opens made. In the 24-bit driver, we need to
    392  * switch to 24-bit mode on the first open, and switch back to 8-bit on
    393  * the last close. This kind of nonsense is needed to give screenblank
    394  * a fighting chance of working.
    395  */
    396 
    397 int
    398 cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
    399 {
    400 	struct cgfourteen_softc *sc;
    401 	int oldopens;
    402 
    403 if (1) return EINVAL;
    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 
    433 	return (0);
    434 }
    435 
    436 int
    437 cgfourteenioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    438 {
    439 	struct cgfourteen_softc *sc =
    440 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    441 	struct fbgattr *fba;
    442 	int error;
    443 
    444 	switch (cmd) {
    445 
    446 	case FBIOGTYPE:
    447 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    448 		break;
    449 
    450 	case FBIOGATTR:
    451 		fba = (struct fbgattr *)data;
    452 		fba->real_type = FBTYPE_MDICOLOR;
    453 		fba->owner = 0;		/* XXX ??? */
    454 		fba->fbtype = sc->sc_fb.fb_type;
    455 		fba->sattr.flags = 0;
    456 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    457 		fba->sattr.dev_specific[0] = -1;
    458 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    459 		fba->emu_types[1] = -1;
    460 		break;
    461 
    462 	case FBIOGETCMAP:
    463 		return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
    464 				     CG14_CLUT_SIZE));
    465 
    466 	case FBIOPUTCMAP:
    467 		/* copy to software map */
    468 #define p ((struct fbcmap *)data)
    469 #ifdef CG14_CG8
    470 		p->index &= 0xffffff;
    471 #endif
    472 		error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
    473 		if (error)
    474 			return (error);
    475 		/* now blast them into the chip */
    476 		/* XXX should use retrace interrupt */
    477 		cg14_load_hwcmap(sc, p->index, p->count);
    478 #undef p
    479 		break;
    480 
    481 	case FBIOGVIDEO:
    482 		*(int *)data = cg14_get_video(sc);
    483 		break;
    484 
    485 	case FBIOSVIDEO:
    486 		cg14_set_video(sc, *(int *)data);
    487 		break;
    488 
    489 	default:
    490 		return (ENOTTY);
    491 	}
    492 	return (0);
    493 }
    494 
    495 /*
    496  * Undo the effect of an FBIOSVIDEO that turns the video off.
    497  */
    498 static void
    499 cgfourteenunblank(device_t dev)
    500 {
    501 	struct cgfourteen_softc *sc = device_private(dev);
    502 
    503 	cg14_set_video(sc, 1);
    504 #if NWSDISPLAY > 0
    505 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
    506 		cg14_set_depth(sc, 8);
    507 		cg14_init_cmap(sc);
    508 		vcons_redraw_screen(sc->sc_vd.active);
    509 		sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    510 	}
    511 #endif
    512 }
    513 
    514 /*
    515  * Return the address that would map the given device at the given
    516  * offset, allowing for the given protection, or return -1 for error.
    517  *
    518  * Since we're pretending to be a cg8, we put the main video RAM at the
    519  *  same place the cg8 does, at offset 256k.  The cg8 has an enable
    520  *  plane in the 256k space; our "enable" plane works differently.  We
    521  *  can't emulate the enable plane very well, but all X uses it for is
    522  *  to clear it at startup - so we map the first page of video RAM over
    523  *  and over to fill that 256k space.  We also map some other views of
    524  *  the video RAM space.
    525  *
    526  * Our memory map thus looks like
    527  *
    528  *	mmap range		space	base offset
    529  *	00000000-00040000	vram	0 (multi-mapped - see above)
    530  *	00040000-00434800	vram	00000000
    531  *	01000000-01400000	vram	01000000
    532  *	02000000-02200000	vram	02000000
    533  *	02800000-02a00000	vram	02800000
    534  *	03000000-03100000	vram	03000000
    535  *	03400000-03500000	vram	03400000
    536  *	03800000-03900000	vram	03800000
    537  *	03c00000-03d00000	vram	03c00000
    538  *	10000000-10010000	regs	00000000 (only if CG14_MAP_REGS)
    539  */
    540 paddr_t
    541 cgfourteenmmap(dev_t dev, off_t off, int prot)
    542 {
    543 	struct cgfourteen_softc *sc =
    544 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    545 
    546 	if (off & PGOFSET)
    547 		panic("cgfourteenmmap");
    548 
    549 	if (off < 0)
    550 		return (-1);
    551 
    552 #if defined(CG14_MAP_REGS) /* XXX: security hole */
    553 	/*
    554 	 * Map the control registers into user space. Should only be
    555 	 * used for debugging!
    556 	 */
    557 	if ((u_int)off >= 0x10000000 && (u_int)off < 0x10000000 + 16*4096) {
    558 		off -= 0x10000000;
    559 		return (bus_space_mmap(sc->sc_bustag,
    560 			BUS_ADDR(sc->sc_physadr[CG14_CTL_IDX].sbr_slot,
    561 				   sc->sc_physadr[CG14_CTL_IDX].sbr_offset),
    562 			off, prot, BUS_SPACE_MAP_LINEAR));
    563 	}
    564 #endif
    565 
    566 	if (off < COLOUR_OFFSET)
    567 		off = 0;
    568 	else if (off < COLOUR_OFFSET+(1152*900*4))
    569 		off -= COLOUR_OFFSET;
    570 	else {
    571 		switch (off >> 20) {
    572 			case 0x010: case 0x011: case 0x012: case 0x013:
    573 			case 0x020: case 0x021:
    574 			case 0x028: case 0x029:
    575 			case 0x030:
    576 			case 0x034:
    577 			case 0x038:
    578 			case 0x03c:
    579 				break;
    580 			default:
    581 				return(-1);
    582 		}
    583 	}
    584 
    585 	return (bus_space_mmap(sc->sc_bustag,
    586 		BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    587 			   sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
    588 		off, prot, BUS_SPACE_MAP_LINEAR));
    589 }
    590 
    591 int
    592 cgfourteenpoll(dev_t dev, int events, struct lwp *l)
    593 {
    594 
    595 	return (seltrue(dev, events, l));
    596 }
    597 
    598 /*
    599  * Miscellaneous helper functions
    600  */
    601 
    602 /* Initialize the framebuffer, storing away useful state for later reset */
    603 static void
    604 cg14_init(struct cgfourteen_softc *sc)
    605 {
    606 	volatile uint32_t *clut;
    607 	volatile uint8_t  *xlut;
    608 	int i;
    609 
    610 	/*
    611 	 * We stash away the following to restore on close:
    612 	 *
    613 	 * 	color look-up table 1 	(sc->sc_saveclut)
    614 	 *	x look-up table		(sc->sc_savexlut)
    615 	 *	control register	(sc->sc_savectl)
    616 	 *	cursor control register (sc->sc_savehwc)
    617 	 */
    618 	sc->sc_savectl = sc->sc_ctl->ctl_mctl;
    619 	sc->sc_savehwc = sc->sc_hwc->curs_ctl;
    620 
    621 	clut = (volatile uint32_t *) sc->sc_clut1->clut_lut;
    622 	xlut = (volatile uint8_t *) sc->sc_xlut->xlut_lut;
    623 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    624 		sc->sc_saveclut.cm_chip[i] = clut[i];
    625 		sc->sc_savexlut[i] = xlut[i];
    626 	}
    627 
    628 #ifdef CG14_CG8
    629 	/*
    630 	 * Enable the video, and put in 24 bit mode.
    631 	 */
    632 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 |
    633 		CG14_MCTL_POWERCTL;
    634 
    635 	/*
    636 	 * Zero the xlut to enable direct-color mode
    637 	 */
    638 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    639 		sc->sc_xlut->xlut_lut[i] = 0;
    640 #else
    641 	/*
    642 	 * Enable the video and put it in 8 bit mode
    643 	 */
    644 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
    645 		CG14_MCTL_POWERCTL;
    646 #endif
    647 }
    648 
    649 static void
    650 /* Restore the state saved on cg14_init */
    651 cg14_reset(struct cgfourteen_softc *sc)
    652 {
    653 	volatile uint32_t *clut;
    654 	volatile uint8_t  *xlut;
    655 	int i;
    656 
    657 	/*
    658 	 * We restore the following, saved in cg14_init:
    659 	 *
    660 	 * 	color look-up table 1 	(sc->sc_saveclut)
    661 	 *	x look-up table		(sc->sc_savexlut)
    662 	 *	control register	(sc->sc_savectl)
    663 	 *	cursor control register (sc->sc_savehwc)
    664 	 *
    665 	 * Note that we don't touch the video enable bits in the
    666 	 * control register; otherwise, screenblank wouldn't work.
    667 	 */
    668 	sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
    669 							CG14_MCTL_POWERCTL)) |
    670 				(sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
    671 						    CG14_MCTL_POWERCTL));
    672 	sc->sc_hwc->curs_ctl = sc->sc_savehwc;
    673 
    674 	clut = sc->sc_clut1->clut_lut;
    675 	xlut = sc->sc_xlut->xlut_lut;
    676 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    677 		clut[i] = sc->sc_saveclut.cm_chip[i];
    678 		xlut[i] = sc->sc_savexlut[i];
    679 	}
    680 }
    681 
    682 /* Enable/disable video display; power down monitor if DPMS-capable */
    683 static void
    684 cg14_set_video(struct cgfourteen_softc *sc, int enable)
    685 {
    686 	/*
    687 	 * We can only use DPMS to power down the display if the chip revision
    688 	 * is greater than 0.
    689 	 */
    690 	if (enable) {
    691 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    692 			sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
    693 						 CG14_MCTL_POWERCTL);
    694 		else
    695 			sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
    696 	} else {
    697 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    698 			sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
    699 						  CG14_MCTL_POWERCTL);
    700 		else
    701 			sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
    702 	}
    703 }
    704 
    705 /* Get status of video display */
    706 static int
    707 cg14_get_video(struct cgfourteen_softc *sc)
    708 {
    709 	return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
    710 }
    711 
    712 /* Read the software shadow colormap */
    713 static int
    714 cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    715 {
    716         u_int i, start, count;
    717         u_char *cp;
    718         int error;
    719 
    720         start = p->index;
    721         count = p->count;
    722         if (start >= cmsize || count > cmsize - start)
    723                 return (EINVAL);
    724 
    725         for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    726                 error = copyout(&cp[3], &p->red[i], 1);
    727                 if (error)
    728                         return error;
    729                 error = copyout(&cp[2], &p->green[i], 1);
    730                 if (error)
    731                         return error;
    732                 error = copyout(&cp[1], &p->blue[i], 1);
    733                 if (error)
    734                         return error;
    735         }
    736         return (0);
    737 }
    738 
    739 /* Write the software shadow colormap */
    740 static int
    741 cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    742 {
    743         u_int i, start, count;
    744         u_char *cp;
    745         u_char cmap[256][4];
    746         int error;
    747 
    748         start = p->index;
    749         count = p->count;
    750         if (start >= cmsize || count > cmsize - start)
    751                 return (EINVAL);
    752 
    753         memcpy(&cmap, &cm->cm_map, sizeof cmap);
    754         for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
    755                 error = copyin(&p->red[i], &cp[3], 1);
    756                 if (error)
    757                         return error;
    758                 error = copyin(&p->green[i], &cp[2], 1);
    759                 if (error)
    760                         return error;
    761                 error = copyin(&p->blue[i], &cp[1], 1);
    762                 if (error)
    763                         return error;
    764                 cp[0] = 0;      /* no alpha channel */
    765         }
    766         memcpy(&cm->cm_map, &cmap, sizeof cmap);
    767         return (0);
    768 }
    769 
    770 static void
    771 cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
    772 {
    773 	/* XXX switch to auto-increment, and on retrace intr */
    774 
    775 	/* Setup pointers to source and dest */
    776 	uint32_t *colp = &sc->sc_cmap.cm_chip[start];
    777 	volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
    778 
    779 	/* Copy by words */
    780 	while (--ncolors >= 0)
    781 		*lutp++ = *colp++;
    782 }
    783 
    784 #if NWSDISPLAY > 0
    785 static void
    786 cg14_setup_wsdisplay(struct cgfourteen_softc *sc, int is_cons)
    787 {
    788 	struct wsemuldisplaydev_attach_args aa;
    789 	struct rasops_info *ri;
    790 	long defattr;
    791 
    792  	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    793 		"default",
    794 		0, 0,
    795 		NULL,
    796 		8, 16,
    797 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    798 		NULL
    799 	};
    800 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    801 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    802 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    803 	vcons_init(&sc->sc_vd, sc, &sc->sc_defaultscreen_descr,
    804 	    &cg14_accessops);
    805 	sc->sc_vd.init_screen = cg14_init_screen;
    806 
    807 	ri = &sc->sc_console_screen.scr_ri;
    808 
    809 	if (is_cons) {
    810 		vcons_init_screen(&sc->sc_vd, &sc->sc_console_screen, 1,
    811 		    &defattr);
    812 
    813 		/* clear the screen with the default background colour */
    814 		memset(sc->sc_fb.fb_pixels,
    815 		       (defattr >> 16) & 0xff,
    816 		       ri->ri_stride * ri->ri_height);
    817 		if (sc->sc_shadowfb != NULL)
    818 			memset(sc->sc_shadowfb,
    819 			       (defattr >> 16) & 0xff,
    820 			       ri->ri_stride * ri->ri_height);
    821 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    822 
    823 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    824 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    825 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    826 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    827 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    828 		    defattr);
    829 	} else {
    830 		/*
    831 		 * since we're not the console we can postpone the rest
    832 		 * until someone actually allocates a screen for us
    833 		 */
    834 	}
    835 
    836 	cg14_init_cmap(sc);
    837 
    838 	aa.console = is_cons;
    839 	aa.scrdata = &sc->sc_screenlist;
    840 	aa.accessops = &cg14_accessops;
    841 	aa.accesscookie = &sc->sc_vd;
    842 
    843 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    844 }
    845 
    846 static void
    847 cg14_init_cmap(struct cgfourteen_softc *sc)
    848 {
    849 	int i, j = 0;
    850 
    851 	for (i = 0; i < 256; i++) {
    852 
    853 		sc->sc_cmap.cm_map[i][3] = rasops_cmap[j];
    854 		sc->sc_cmap.cm_map[i][2] = rasops_cmap[j + 1];
    855 		sc->sc_cmap.cm_map[i][1] = rasops_cmap[j + 2];
    856 		j += 3;
    857 	}
    858 	cg14_load_hwcmap(sc, 0, 256);
    859 }
    860 
    861 static int
    862 cg14_putcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
    863 {
    864 	u_int index = cm->index;
    865 	u_int count = cm->count;
    866 	int i, error;
    867 	u_char rbuf[256], gbuf[256], bbuf[256];
    868 
    869 	if (cm->index >= 256 || cm->count > 256 ||
    870 	    (cm->index + cm->count) > 256)
    871 		return EINVAL;
    872 	error = copyin(cm->red, &rbuf[index], count);
    873 	if (error)
    874 		return error;
    875 	error = copyin(cm->green, &gbuf[index], count);
    876 	if (error)
    877 		return error;
    878 	error = copyin(cm->blue, &bbuf[index], count);
    879 	if (error)
    880 		return error;
    881 
    882 	for (i = 0; i < count; i++) {
    883 		sc->sc_cmap.cm_map[index][3] = rbuf[index];
    884 		sc->sc_cmap.cm_map[index][2] = gbuf[index];
    885 		sc->sc_cmap.cm_map[index][1] = bbuf[index];
    886 
    887 		index++;
    888 	}
    889 	cg14_load_hwcmap(sc, 0, 256);
    890 	return 0;
    891 }
    892 
    893 static int
    894 cg14_getcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
    895 {
    896 	uint8_t rbuf[256], gbuf[256], bbuf[256];
    897 	u_int index = cm->index;
    898 	u_int count = cm->count;
    899 	int error, i;
    900 
    901 	if (index >= 255 || count > 256 || index + count > 256)
    902 		return EINVAL;
    903 
    904 
    905 	for (i = 0; i < count; i++) {
    906 		rbuf[i] = sc->sc_cmap.cm_map[index][3];
    907 		gbuf[i] = sc->sc_cmap.cm_map[index][2];
    908 		bbuf[i] = sc->sc_cmap.cm_map[index][1];
    909 
    910 		index++;
    911 	}
    912 	error = copyout(rbuf,   cm->red,   count);
    913 	if (error)
    914 		return error;
    915 	error = copyout(gbuf, cm->green, count);
    916 	if (error)
    917 		return error;
    918 	error = copyout(bbuf,  cm->blue,  count);
    919 	if (error)
    920 		return error;
    921 
    922 	return 0;
    923 }
    924 static int
    925 cg14_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    926 	struct lwp *l)
    927 {
    928 	struct vcons_data *vd = v;
    929 	struct cgfourteen_softc *sc = vd->cookie;
    930 	struct wsdisplay_fbinfo *wdf;
    931 	struct vcons_screen *ms = vd->active;
    932 
    933 	switch (cmd) {
    934 
    935 		case WSDISPLAYIO_GTYPE:
    936 			*(uint32_t *)data = WSDISPLAY_TYPE_SUNCG14;
    937 			return 0;
    938 
    939 		case WSDISPLAYIO_GINFO:
    940 			wdf = (void *)data;
    941 			wdf->height = ms->scr_ri.ri_height;
    942 			wdf->width = ms->scr_ri.ri_width;
    943 			wdf->depth = 32;
    944 			wdf->cmsize = 256;
    945 			return 0;
    946 
    947 		case WSDISPLAYIO_GETCMAP:
    948 			return cg14_getcmap(sc,
    949 			    (struct wsdisplay_cmap *)data);
    950 
    951 		case WSDISPLAYIO_PUTCMAP:
    952 			return cg14_putcmap(sc,
    953 			    (struct wsdisplay_cmap *)data);
    954 
    955 		case WSDISPLAYIO_LINEBYTES:
    956 			*(u_int *)data = ms->scr_ri.ri_stride << 2;
    957 			return 0;
    958 
    959 		case WSDISPLAYIO_SMODE:
    960 			{
    961 				int new_mode = *(int*)data;
    962 				if (new_mode != sc->sc_mode) {
    963 					sc->sc_mode = new_mode;
    964 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    965 						bus_space_write_1(sc->sc_bustag,
    966 						    sc->sc_regh,
    967 						    CG14_CURSOR_CONTROL, 0);
    968 						cg14_set_depth(sc, 8);
    969 						cg14_init_cmap(sc);
    970 						vcons_redraw_screen(ms);
    971 					} else {
    972 						cg14_set_depth(sc, 32);
    973 					}
    974 				}
    975 			}
    976 			return 0;
    977 		case WSDISPLAYIO_SVIDEO:
    978 			cg14_set_video(sc, *(int *)data);
    979 			return 0;
    980 		case WSDISPLAYIO_GVIDEO:
    981 			return cg14_get_video(sc) ?
    982 			    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
    983 		case WSDISPLAYIO_GCURPOS:
    984 			{
    985 				struct wsdisplay_curpos *cp = (void *)data;
    986 
    987 				cp->x = sc->sc_cursor.cc_pos.x;
    988 				cp->y = sc->sc_cursor.cc_pos.y;
    989 			}
    990 			return 0;
    991 		case WSDISPLAYIO_SCURPOS:
    992 			{
    993 				struct wsdisplay_curpos *cp = (void *)data;
    994 
    995 				cg14_move_cursor(sc, cp->x, cp->y);
    996 			}
    997 			return 0;
    998 		case WSDISPLAYIO_GCURMAX:
    999 			{
   1000 				struct wsdisplay_curpos *cp = (void *)data;
   1001 
   1002 				cp->x = 32;
   1003 				cp->y = 32;
   1004 			}
   1005 			return 0;
   1006 		case WSDISPLAYIO_SCURSOR:
   1007 			{
   1008 				struct wsdisplay_cursor *cursor = (void *)data;
   1009 
   1010 				return cg14_do_cursor(sc, cursor);
   1011 			}
   1012 		case PCI_IOC_CFGREAD:
   1013 		case PCI_IOC_CFGWRITE:
   1014 			return EINVAL;
   1015 
   1016 	}
   1017 	return EPASSTHROUGH;
   1018 }
   1019 
   1020 static paddr_t
   1021 cg14_mmap(void *v, void *vs, off_t offset, int prot)
   1022 {
   1023 	struct vcons_data *vd = v;
   1024 	struct cgfourteen_softc *sc = vd->cookie;
   1025 
   1026 	/* allow mmap()ing the full framebuffer, not just what we use */
   1027 	if (offset < sc->sc_vramsize)
   1028 		return bus_space_mmap(sc->sc_bustag,
   1029 		    BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
   1030 		      sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
   1031 		    offset + CG14_FB_CBGR, prot, BUS_SPACE_MAP_LINEAR);
   1032 
   1033 	return -1;
   1034 }
   1035 
   1036 static void
   1037 cg14_init_screen(void *cookie, struct vcons_screen *scr,
   1038     int existing, long *defattr)
   1039 {
   1040 	struct cgfourteen_softc *sc = cookie;
   1041 	struct rasops_info *ri = &scr->scr_ri;
   1042 
   1043 	ri->ri_depth = 8;
   1044 	ri->ri_width = sc->sc_fb.fb_type.fb_width;
   1045 	ri->ri_height = sc->sc_fb.fb_type.fb_height;
   1046 	ri->ri_stride = ri->ri_width;
   1047 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
   1048 
   1049 	if (sc->sc_shadowfb != NULL) {
   1050 		ri->ri_bits = sc->sc_shadowfb;
   1051 		ri->ri_hwbits = (char *)sc->sc_fb.fb_pixels;
   1052 	} else
   1053 		ri->ri_bits = (char *)sc->sc_fb.fb_pixels;
   1054 
   1055 	if (existing) {
   1056 		ri->ri_flg |= RI_CLEAR;
   1057 	}
   1058 
   1059 	rasops_init(ri, sc->sc_fb.fb_type.fb_height / 8,
   1060 	     sc->sc_fb.fb_type.fb_width / 8);
   1061 	ri->ri_caps = WSSCREEN_WSCOLORS;
   1062 
   1063 	rasops_reconfig(ri,
   1064 	    sc->sc_fb.fb_type.fb_height / ri->ri_font->fontheight,
   1065 	    sc->sc_fb.fb_type.fb_width / ri->ri_font->fontwidth);
   1066 
   1067 	ri->ri_hw = scr;
   1068 }
   1069 
   1070 static void
   1071 cg14_set_depth(struct cgfourteen_softc *sc, int depth)
   1072 {
   1073 	int i;
   1074 
   1075 	if (sc->sc_depth == depth)
   1076 		return;
   1077 	switch (depth) {
   1078 		case 8:
   1079 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1080 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
   1081 			    CG14_MCTL_PIXMODE_8 | CG14_MCTL_POWERCTL);
   1082 			sc->sc_depth = 8;
   1083 			/* everything is CLUT1 */
   1084 			for (i = 0; i < CG14_CLUT_SIZE; i++)
   1085 			     sc->sc_xlut->xlut_lut[i] = 0;
   1086 			break;
   1087 		case 32:
   1088 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1089 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
   1090 			    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL);
   1091 			sc->sc_depth = 32;
   1092 			for (i = 0; i < CG14_CLUT_SIZE; i++)
   1093 			     sc->sc_xlut->xlut_lut[i] = 0;
   1094 			break;
   1095 		default:
   1096 			printf("%s: can't change to depth %d\n",
   1097 			    device_xname(sc->sc_dev), depth);
   1098 	}
   1099 }
   1100 
   1101 static void
   1102 cg14_move_cursor(struct cgfourteen_softc *sc, int x, int y)
   1103 {
   1104 	uint32_t pos;
   1105 
   1106 	sc->sc_cursor.cc_pos.x = x;
   1107 	sc->sc_cursor.cc_pos.y = y;
   1108 	pos = ((sc->sc_cursor.cc_pos.x - sc->sc_cursor.cc_hot.x ) << 16) |
   1109 	      ((sc->sc_cursor.cc_pos.y - sc->sc_cursor.cc_hot.y ) & 0xffff);
   1110 	bus_space_write_4(sc->sc_bustag, sc->sc_regh, CG14_CURSOR_X, pos);
   1111 }
   1112 
   1113 static int
   1114 cg14_do_cursor(struct cgfourteen_softc *sc, struct wsdisplay_cursor *cur)
   1115 {
   1116 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1117 
   1118 		bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1119 		    CG14_CURSOR_CONTROL, cur->enable ? CG14_CRSR_ENABLE : 0);
   1120 	}
   1121 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1122 
   1123 		sc->sc_cursor.cc_hot.x = cur->hot.x;
   1124 		sc->sc_cursor.cc_hot.y = cur->hot.y;
   1125 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1126 	}
   1127 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1128 
   1129 		cg14_move_cursor(sc, cur->pos.x, cur->pos.y);
   1130 	}
   1131 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1132 		int i;
   1133 		uint32_t val;
   1134 
   1135 		for (i = 0; i < min(cur->cmap.count, 3); i++) {
   1136 			val = (cur->cmap.red[i] ) |
   1137 			      (cur->cmap.green[i] << 8) |
   1138 			      (cur->cmap.blue[i] << 16);
   1139 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1140 			    CG14_CURSOR_COLOR1 + ((i + cur->cmap.index) << 2),
   1141 			    val);
   1142 		}
   1143 	}
   1144 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1145 		uint32_t buffer[32], latch, tmp;
   1146 		int i;
   1147 
   1148 		copyin(cur->mask, buffer, 128);
   1149 		for (i = 0; i < 32; i++) {
   1150 			latch = 0;
   1151 			tmp = buffer[i] & 0x80808080;
   1152 			latch |= tmp >> 7;
   1153 			tmp = buffer[i] & 0x40404040;
   1154 			latch |= tmp >> 5;
   1155 			tmp = buffer[i] & 0x20202020;
   1156 			latch |= tmp >> 3;
   1157 			tmp = buffer[i] & 0x10101010;
   1158 			latch |= tmp >> 1;
   1159 			tmp = buffer[i] & 0x08080808;
   1160 			latch |= tmp << 1;
   1161 			tmp = buffer[i] & 0x04040404;
   1162 			latch |= tmp << 3;
   1163 			tmp = buffer[i] & 0x02020202;
   1164 			latch |= tmp << 5;
   1165 			tmp = buffer[i] & 0x01010101;
   1166 			latch |= tmp << 7;
   1167 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1168 			    CG14_CURSOR_PLANE0 + (i << 2), latch);
   1169 		}
   1170 		copyin(cur->image, buffer, 128);
   1171 		for (i = 0; i < 32; i++) {
   1172 			latch = 0;
   1173 			tmp = buffer[i] & 0x80808080;
   1174 			latch |= tmp >> 7;
   1175 			tmp = buffer[i] & 0x40404040;
   1176 			latch |= tmp >> 5;
   1177 			tmp = buffer[i] & 0x20202020;
   1178 			latch |= tmp >> 3;
   1179 			tmp = buffer[i] & 0x10101010;
   1180 			latch |= tmp >> 1;
   1181 			tmp = buffer[i] & 0x08080808;
   1182 			latch |= tmp << 1;
   1183 			tmp = buffer[i] & 0x04040404;
   1184 			latch |= tmp << 3;
   1185 			tmp = buffer[i] & 0x02020202;
   1186 			latch |= tmp << 5;
   1187 			tmp = buffer[i] & 0x01010101;
   1188 			latch |= tmp << 7;
   1189 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1190 			    CG14_CURSOR_PLANE1 + (i << 2), latch);
   1191 		}
   1192 	}
   1193 	return 0;
   1194 }
   1195 #endif
   1196