Home | History | Annotate | Line # | Download | only in dev
cgfourteen.c revision 1.43
      1 /*	$NetBSD: cgfourteen.c,v 1.43 2006/01/19 09:32:37 he 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 #define 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/mman.h>
     85 #include <sys/tty.h>
     86 #include <sys/conf.h>
     87 
     88 #include <uvm/uvm_extern.h>
     89 
     90 #include <dev/sun/fbio.h>
     91 #include <machine/autoconf.h>
     92 #include <machine/pmap.h>
     93 #include <dev/sun/fbvar.h>
     94 #include <machine/cpu.h>
     95 #include <dev/sbus/sbusvar.h>
     96 
     97 #include <sparc/dev/cgfourteenreg.h>
     98 #include <sparc/dev/cgfourteenvar.h>
     99 
    100 /* autoconfiguration driver */
    101 static int	cgfourteenmatch(struct device *, struct cfdata *, void *);
    102 static void	cgfourteenattach(struct device *, struct device *, void *);
    103 static void	cgfourteenunblank(struct device *);
    104 
    105 CFATTACH_DECL(cgfourteen, sizeof(struct cgfourteen_softc),
    106     cgfourteenmatch, cgfourteenattach, NULL, NULL);
    107 
    108 extern struct cfdriver cgfourteen_cd;
    109 
    110 dev_type_open(cgfourteenopen);
    111 dev_type_close(cgfourteenclose);
    112 dev_type_ioctl(cgfourteenioctl);
    113 dev_type_mmap(cgfourteenmmap);
    114 dev_type_poll(cgfourteenpoll);
    115 
    116 const struct cdevsw cgfourteen_cdevsw = {
    117         cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
    118         nostop, notty, cgfourteenpoll, cgfourteenmmap, nokqfilter,
    119 };
    120 
    121 /* frame buffer generic driver */
    122 static struct fbdriver cgfourteenfbdriver = {
    123 	cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
    124 	cgfourteenpoll, cgfourteenmmap, nokqfilter
    125 };
    126 
    127 extern struct tty *fbconstty;
    128 
    129 static void cg14_set_video(struct cgfourteen_softc *, int);
    130 static int  cg14_get_video(struct cgfourteen_softc *);
    131 static int  cg14_get_cmap(struct fbcmap *, union cg14cmap *, int);
    132 static int  cg14_put_cmap(struct fbcmap *, union cg14cmap *, int);
    133 static void cg14_load_hwcmap(struct cgfourteen_softc *, int, int);
    134 static void cg14_init(struct cgfourteen_softc *);
    135 static void cg14_reset(struct cgfourteen_softc *);
    136 
    137 /*
    138  * Match a cgfourteen.
    139  */
    140 int
    141 cgfourteenmatch(struct device *parent, struct cfdata *cf, void *aux)
    142 {
    143 	union obio_attach_args *uoba = aux;
    144 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    145 
    146 	/*
    147 	 * The cgfourteen is a local-bus video adaptor, accessed directly
    148 	 * via the processor, and not through device space or an external
    149 	 * bus. Thus we look _only_ at the obio bus.
    150 	 * Additionally, these things exist only on the Sun4m.
    151 	 */
    152 
    153 	if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
    154 		return (0);
    155 
    156 	/* Check driver name */
    157 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
    158 }
    159 
    160 /*
    161  * Set COLOUR_OFFSET to the offset of the video RAM.  This is to provide
    162  *  space for faked overlay junk for the cg8 emulation.
    163  *
    164  * As it happens, this value is correct for both cg3 and cg8 emulation!
    165  */
    166 #define COLOUR_OFFSET (256*1024)
    167 
    168 static void cg14_set_rcons_luts(struct cgfourteen_softc *sc)
    169 {
    170 	int i;
    171 
    172 	for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_xlut->xlut_lut[i] = 0x22;
    173 	for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_clut2->clut_lut[i] = 0x00ffffff;
    174 	sc->sc_clut2->clut_lut[0] = 0x00ffffff;
    175 	sc->sc_clut2->clut_lut[255] = 0;
    176 }
    177 
    178 /*
    179  * Attach a display.  We need to notice if it is the console, too.
    180  */
    181 void
    182 cgfourteenattach(struct device *parent, struct device *self, void *aux)
    183 {
    184 	union obio_attach_args *uoba = aux;
    185 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    186 	struct cgfourteen_softc *sc = (struct cgfourteen_softc *)self;
    187 	struct fbdevice *fb = &sc->sc_fb;
    188 	bus_space_handle_t bh;
    189 	int node, ramsize;
    190 	volatile uint32_t *lut;
    191 	int i, isconsole;
    192 
    193 	node = sa->sa_node;
    194 
    195 	/* Remember cookies for cgfourteenmmap() */
    196 	sc->sc_bustag = sa->sa_bustag;
    197 
    198 	fb->fb_driver = &cgfourteenfbdriver;
    199 	fb->fb_device = &sc->sc_dev;
    200 	/* Mask out invalid flags from the user. */
    201 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
    202 
    203 	/*
    204 	 * We're emulating a cg3/8, so represent ourselves as one
    205 	 */
    206 #ifdef CG14_CG8
    207 	fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
    208 	fb->fb_type.fb_depth = 32;
    209 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
    210 	ramsize = roundup(fb->fb_type.fb_height * 1152 * 4, NBPG);
    211 #else
    212 	fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
    213 	fb->fb_type.fb_depth = 8;
    214 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
    215 	ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, NBPG);
    216 #endif
    217 	fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
    218 	fb->fb_type.fb_size = ramsize + COLOUR_OFFSET;
    219 
    220 	if (sa->sa_nreg < 2) {
    221 		printf("%s: only %d register sets\n",
    222 			self->dv_xname, sa->sa_nreg);
    223 		return;
    224 	}
    225 	bcopy(sa->sa_reg, sc->sc_physadr,
    226 	      sa->sa_nreg * sizeof(struct sbus_reg));
    227 
    228 	/*
    229 	 * Now map in the 8 useful pages of registers
    230 	 */
    231 	if (sa->sa_size < 0x10000) {
    232 #ifdef DIAGNOSTIC
    233 		printf("warning: can't find all cgfourteen registers...\n");
    234 #endif
    235 		sa->sa_size = 0x10000;
    236 	}
    237 	if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    238 			 sa->sa_offset,
    239 			 sa->sa_size,
    240 			 BUS_SPACE_MAP_LINEAR,
    241 			 &bh) != 0) {
    242 		printf("%s: cannot map control registers\n", self->dv_xname);
    243 		return;
    244 	}
    245 
    246 	sc->sc_ctl   = (struct cg14ctl  *) (bh);
    247 	sc->sc_hwc   = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
    248 	sc->sc_dac   = (struct cg14dac  *) (bh + CG14_OFFSET_DAC);
    249 	sc->sc_xlut  = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
    250 	sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
    251 	sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
    252 	sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
    253 	sc->sc_clutincr =        (u_int *) (bh + CG14_OFFSET_CLUTINCR);
    254 
    255 	/*
    256 	 * Let the user know that we're here
    257 	 */
    258 #ifdef CG14_CG8
    259 	printf(": cgeight emulated at %dx%dx24bpp",
    260 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    261 #else
    262 	printf(": cgthree emulated at %dx%dx8bpp",
    263 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    264 #endif
    265 	/*
    266 	 * Enable the video.
    267 	 */
    268 	cg14_set_video(sc, 1);
    269 
    270 	/*
    271 	 * Grab the initial colormap
    272 	 */
    273 	lut = sc->sc_clut1->clut_lut;
    274 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    275 		sc->sc_cmap.cm_chip[i] = lut[i];
    276 
    277 	/* See if we're the console */
    278         isconsole = fb_is_console(node);
    279 
    280 	if (isconsole) {
    281 		printf(" (console)\n");
    282 #ifdef RASTERCONSOLE
    283 		/* *sbus*_bus_map?  but that's how we map the regs... */
    284 		if (sbus_bus_map( sc->sc_bustag,
    285 				  sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    286 				  sc->sc_physadr[CG14_PXL_IDX].sbr_offset+0x03800000,
    287 				  1152*900, BUS_SPACE_MAP_LINEAR,
    288 				  &bh) != 0) {
    289 			printf("%s: cannot map pixels\n",&sc->sc_dev.dv_xname[0]);
    290 		} else {
    291 			sc->sc_rcfb = sc->sc_fb;
    292 			sc->sc_rcfb.fb_type.fb_type = FBTYPE_SUN3COLOR;
    293 			sc->sc_rcfb.fb_type.fb_depth = 8;
    294 			sc->sc_rcfb.fb_linebytes = 1152;
    295 			sc->sc_rcfb.fb_type.fb_size = roundup(1152*900,NBPG);
    296 			sc->sc_rcfb.fb_pixels = (void *)bh;
    297 			printf("vram at %p\n",(void *)bh);
    298 			for (i=0;i<1152*900;i++) ((unsigned char *)bh)[i] = 0;
    299 			fbrcons_init(&sc->sc_rcfb);
    300 			cg14_set_rcons_luts(sc);
    301 			sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL;
    302 		}
    303 #endif
    304 	} else
    305 		printf("\n");
    306 
    307 	/* Attach to /dev/fb */
    308 	fb_attach(&sc->sc_fb, isconsole);
    309 }
    310 
    311 /*
    312  * Keep track of the number of opens made. In the 24-bit driver, we need to
    313  * switch to 24-bit mode on the first open, and switch back to 8-bit on
    314  * the last close. This kind of nonsense is needed to give screenblank
    315  * a fighting chance of working.
    316  */
    317 static int cg14_opens = 0;
    318 
    319 int
    320 cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
    321 {
    322 	struct cgfourteen_softc *sc;
    323 	int unit;
    324 	int s, oldopens;
    325 
    326 	unit = minor(dev);
    327 	if (unit >= cgfourteen_cd.cd_ndevs)
    328 		return(ENXIO);
    329 	sc = cgfourteen_cd.cd_devs[minor(dev)];
    330 	if (sc == NULL)
    331 		return(ENXIO);
    332 	s = splhigh();
    333 	oldopens = cg14_opens++;
    334 	splx(s);
    335 
    336 	/* Setup the cg14 as we want it, and save the original PROM state */
    337 	if (oldopens == 0)	/* first open only, to make screenblank work */
    338 		cg14_init(sc);
    339 
    340 	return (0);
    341 }
    342 
    343 int
    344 cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
    345 {
    346 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    347 	int s, opens;
    348 
    349 	s = splhigh();
    350 	opens = --cg14_opens;
    351 	if (cg14_opens < 0)
    352 		opens = cg14_opens = 0;
    353 	splx(s);
    354 
    355 	/*
    356 	 * Restore video state to make the PROM happy, on last close.
    357 	 */
    358 	if (opens == 0)
    359 		cg14_reset(sc);
    360 
    361 	return (0);
    362 }
    363 
    364 int
    365 cgfourteenioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct lwp *l)
    366 {
    367 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    368 	struct fbgattr *fba;
    369 	int error;
    370 
    371 	switch (cmd) {
    372 
    373 	case FBIOGTYPE:
    374 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    375 		break;
    376 
    377 	case FBIOGATTR:
    378 		fba = (struct fbgattr *)data;
    379 		fba->real_type = FBTYPE_MDICOLOR;
    380 		fba->owner = 0;		/* XXX ??? */
    381 		fba->fbtype = sc->sc_fb.fb_type;
    382 		fba->sattr.flags = 0;
    383 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    384 		fba->sattr.dev_specific[0] = -1;
    385 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    386 		fba->emu_types[1] = -1;
    387 		break;
    388 
    389 	case FBIOGETCMAP:
    390 		return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
    391 				     CG14_CLUT_SIZE));
    392 
    393 	case FBIOPUTCMAP:
    394 		/* copy to software map */
    395 #define p ((struct fbcmap *)data)
    396 #ifdef CG14_CG8
    397 		p->index &= 0xffffff;
    398 #endif
    399 		error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
    400 		if (error)
    401 			return (error);
    402 		/* now blast them into the chip */
    403 		/* XXX should use retrace interrupt */
    404 		cg14_load_hwcmap(sc, p->index, p->count);
    405 #undef p
    406 		break;
    407 
    408 	case FBIOGVIDEO:
    409 		*(int *)data = cg14_get_video(sc);
    410 		break;
    411 
    412 	case FBIOSVIDEO:
    413 		cg14_set_video(sc, *(int *)data);
    414 		break;
    415 
    416 	default:
    417 		return (ENOTTY);
    418 	}
    419 	return (0);
    420 }
    421 
    422 /*
    423  * Undo the effect of an FBIOSVIDEO that turns the video off.
    424  */
    425 static void
    426 cgfourteenunblank(struct device *dev)
    427 {
    428 
    429 	cg14_set_video((struct cgfourteen_softc *)dev, 1);
    430 }
    431 
    432 /*
    433  * Return the address that would map the given device at the given
    434  * offset, allowing for the given protection, or return -1 for error.
    435  *
    436  * Since we're pretending to be a cg8, we put the main video RAM at the
    437  *  same place the cg8 does, at offset 256k.  The cg8 has an enable
    438  *  plane in the 256k space; our "enable" plane works differently.  We
    439  *  can't emulate the enable plane very well, but all X uses it for is
    440  *  to clear it at startup - so we map the first page of video RAM over
    441  *  and over to fill that 256k space.  We also map some other views of
    442  *  the video RAM space.
    443  *
    444  * Our memory map thus looks like
    445  *
    446  *	mmap range		space	base offset
    447  *	00000000-00040000	vram	0 (multi-mapped - see above)
    448  *	00040000-00434800	vram	00000000
    449  *	01000000-01400000	vram	01000000
    450  *	02000000-02200000	vram	02000000
    451  *	02800000-02a00000	vram	02800000
    452  *	03000000-03100000	vram	03000000
    453  *	03400000-03500000	vram	03400000
    454  *	03800000-03900000	vram	03800000
    455  *	03c00000-03d00000	vram	03c00000
    456  *	10000000-10010000	regs	00000000 (only if CG14_MAP_REGS)
    457  */
    458 paddr_t
    459 cgfourteenmmap(dev_t dev, off_t off, int prot)
    460 {
    461 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    462 
    463 	if (off & PGOFSET)
    464 		panic("cgfourteenmmap");
    465 
    466 	if (off < 0)
    467 		return (-1);
    468 
    469 #if defined(CG14_MAP_REGS) /* XXX: security hole */
    470 	/*
    471 	 * Map the control registers into user space. Should only be
    472 	 * used for debugging!
    473 	 */
    474 	if ((u_int)off >= 0x10000000 && (u_int)off < 0x10000000 + 16*4096) {
    475 		off -= 0x10000000;
    476 		return (bus_space_mmap(sc->sc_bustag,
    477 			BUS_ADDR(sc->sc_physadr[CG14_CTL_IDX].sbr_slot,
    478 				   sc->sc_physadr[CG14_CTL_IDX].sbr_offset),
    479 			off, prot, BUS_SPACE_MAP_LINEAR));
    480 	}
    481 #endif
    482 
    483 	if (off < COLOUR_OFFSET)
    484 		off = 0;
    485 	else if (off < COLOUR_OFFSET+(1152*900*4))
    486 		off -= COLOUR_OFFSET;
    487 	else {
    488 		switch (off >> 20) {
    489 			case 0x010: case 0x011: case 0x012: case 0x013:
    490 			case 0x020: case 0x021:
    491 			case 0x028: case 0x029:
    492 			case 0x030:
    493 			case 0x034:
    494 			case 0x038:
    495 			case 0x03c:
    496 				break;
    497 			default:
    498 				return(-1);
    499 		}
    500 	}
    501 
    502 	return (bus_space_mmap(sc->sc_bustag,
    503 		BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    504 			   sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
    505 		off, prot, BUS_SPACE_MAP_LINEAR));
    506 }
    507 
    508 int
    509 cgfourteenpoll(dev_t dev, int events, struct lwp *l)
    510 {
    511 
    512 	return (seltrue(dev, events, l));
    513 }
    514 
    515 /*
    516  * Miscellaneous helper functions
    517  */
    518 
    519 /* Initialize the framebuffer, storing away useful state for later reset */
    520 static void
    521 cg14_init(struct cgfourteen_softc *sc)
    522 {
    523 	volatile u_int32_t *clut;
    524 	volatile u_int8_t  *xlut;
    525 	int i;
    526 
    527 	/*
    528 	 * We stash away the following to restore on close:
    529 	 *
    530 	 * 	color look-up table 1 	(sc->sc_saveclut)
    531 	 *	x look-up table		(sc->sc_savexlut)
    532 	 *	control register	(sc->sc_savectl)
    533 	 *	cursor control register (sc->sc_savehwc)
    534 	 */
    535 	sc->sc_savectl = sc->sc_ctl->ctl_mctl;
    536 	sc->sc_savehwc = sc->sc_hwc->curs_ctl;
    537 
    538 	clut = (volatile u_int32_t *) sc->sc_clut1->clut_lut;
    539 	xlut = (volatile u_int8_t *) sc->sc_xlut->xlut_lut;
    540 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    541 		sc->sc_saveclut.cm_chip[i] = clut[i];
    542 		sc->sc_savexlut[i] = xlut[i];
    543 	}
    544 
    545 #ifdef CG14_CG8
    546 	/*
    547 	 * Enable the video, and put in 24 bit mode.
    548 	 */
    549 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 |
    550 		CG14_MCTL_POWERCTL;
    551 
    552 	/*
    553 	 * Zero the xlut to enable direct-color mode
    554 	 */
    555 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    556 		sc->sc_xlut->xlut_lut[i] = 0;
    557 #else
    558 	/*
    559 	 * Enable the video and put it in 8 bit mode
    560 	 */
    561 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
    562 		CG14_MCTL_POWERCTL;
    563 #endif
    564 }
    565 
    566 static void
    567 /* Restore the state saved on cg14_init */
    568 cg14_reset(struct cgfourteen_softc *sc)
    569 {
    570 	volatile uint32_t *clut;
    571 	volatile uint8_t  *xlut;
    572 	int i;
    573 
    574 	/*
    575 	 * We restore the following, saved in cg14_init:
    576 	 *
    577 	 * 	color look-up table 1 	(sc->sc_saveclut)
    578 	 *	x look-up table		(sc->sc_savexlut)
    579 	 *	control register	(sc->sc_savectl)
    580 	 *	cursor control register (sc->sc_savehwc)
    581 	 *
    582 	 * Note that we don't touch the video enable bits in the
    583 	 * control register; otherwise, screenblank wouldn't work.
    584 	 */
    585 	sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
    586 							CG14_MCTL_POWERCTL)) |
    587 				(sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
    588 						    CG14_MCTL_POWERCTL));
    589 	sc->sc_hwc->curs_ctl = sc->sc_savehwc;
    590 
    591 	clut = sc->sc_clut1->clut_lut;
    592 	xlut = sc->sc_xlut->xlut_lut;
    593 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    594 		clut[i] = sc->sc_saveclut.cm_chip[i];
    595 		xlut[i] = sc->sc_savexlut[i];
    596 	}
    597 }
    598 
    599 /* Enable/disable video display; power down monitor if DPMS-capable */
    600 static void
    601 cg14_set_video(struct cgfourteen_softc *sc, int enable)
    602 {
    603 	/*
    604 	 * We can only use DPMS to power down the display if the chip revision
    605 	 * is greater than 0.
    606 	 */
    607 	if (enable) {
    608 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    609 			sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
    610 						 CG14_MCTL_POWERCTL);
    611 		else
    612 			sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
    613 	} else {
    614 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    615 			sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
    616 						  CG14_MCTL_POWERCTL);
    617 		else
    618 			sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
    619 	}
    620 }
    621 
    622 /* Get status of video display */
    623 static int
    624 cg14_get_video(struct cgfourteen_softc *sc)
    625 {
    626 	return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
    627 }
    628 
    629 /* Read the software shadow colormap */
    630 static int
    631 cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    632 {
    633         u_int i, start, count;
    634         u_char *cp;
    635         int error;
    636 
    637         start = p->index;
    638         count = p->count;
    639         if (start >= cmsize || count > cmsize - start)
    640                 return (EINVAL);
    641 
    642         for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    643                 error = copyout(&cp[3], &p->red[i], 1);
    644                 if (error)
    645                         return error;
    646                 error = copyout(&cp[2], &p->green[i], 1);
    647                 if (error)
    648                         return error;
    649                 error = copyout(&cp[1], &p->blue[i], 1);
    650                 if (error)
    651                         return error;
    652         }
    653         return (0);
    654 }
    655 
    656 /* Write the software shadow colormap */
    657 static int
    658 cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    659 {
    660         u_int i, start, count;
    661         u_char *cp;
    662         u_char cmap[256][4];
    663         int error;
    664 
    665         start = p->index;
    666         count = p->count;
    667         if (start >= cmsize || count > cmsize - start)
    668                 return (EINVAL);
    669 
    670         memcpy(&cmap, &cm->cm_map, sizeof cmap);
    671         for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
    672                 error = copyin(&p->red[i], &cp[3], 1);
    673                 if (error)
    674                         return error;
    675                 error = copyin(&p->green[i], &cp[2], 1);
    676                 if (error)
    677                         return error;
    678                 error = copyin(&p->blue[i], &cp[1], 1);
    679                 if (error)
    680                         return error;
    681                 cp[0] = 0;      /* no alpha channel */
    682         }
    683         memcpy(&cm->cm_map, &cmap, sizeof cmap);
    684         return (0);
    685 }
    686 
    687 static void
    688 cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
    689 {
    690 	/* XXX switch to auto-increment, and on retrace intr */
    691 
    692 	/* Setup pointers to source and dest */
    693 	uint32_t *colp = &sc->sc_cmap.cm_chip[start];
    694 	volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
    695 
    696 	/* Copy by words */
    697 	while (--ncolors >= 0)
    698 		*lutp++ = *colp++;
    699 }
    700