Home | History | Annotate | Line # | Download | only in dev
cgfourteen.c revision 1.3
      1 /*	$NetBSD: cgfourteen.c,v 1.3 1996/10/04 20:34:35 thorpej 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  * XXX Note that the code enabled by this define is currently untested/broken.
     77  */
     78 #undef CG14_CG8
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/buf.h>
     83 #include <sys/device.h>
     84 #include <sys/ioctl.h>
     85 #include <sys/malloc.h>
     86 #include <sys/mman.h>
     87 #include <sys/tty.h>
     88 #include <sys/conf.h>
     89 
     90 #include <vm/vm.h>
     91 
     92 #include <machine/fbio.h>
     93 #include <machine/autoconf.h>
     94 #include <machine/pmap.h>
     95 #include <machine/fbvar.h>
     96 #include <machine/cpu.h>
     97 #include <machine/conf.h>
     98 
     99 #include <sparc/dev/cgfourteenreg.h>
    100 #include <sparc/dev/cgfourteenvar.h>
    101 
    102 /* autoconfiguration driver */
    103 static void	cgfourteenattach(struct device *, struct device *, void *);
    104 static int	cgfourteenmatch(struct device *, void *, void *);
    105 static void	cgfourteenunblank(struct device *);
    106 
    107 /* cdevsw prototypes */
    108 cdev_decl(cgfourteen);
    109 
    110 struct cfattach cgfourteen_ca = {
    111 	sizeof(struct cgfourteen_softc), cgfourteenmatch, cgfourteenattach
    112 };
    113 
    114 struct cfdriver cgfourteen_cd = {
    115 	NULL, "cgfourteen", DV_DULL
    116 };
    117 
    118 /* frame buffer generic driver */
    119 static struct fbdriver cgfourteenfbdriver = {
    120 	cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
    121 	cgfourteenpoll, cgfourteenmmap
    122 };
    123 
    124 extern int fbnode;
    125 extern struct tty *fbconstty;
    126 
    127 static void cg14_set_video __P((struct cgfourteen_softc *, int));
    128 static int  cg14_get_video __P((struct cgfourteen_softc *));
    129 static int  cg14_get_cmap __P((struct fbcmap *, union cg14cmap *, int));
    130 static int  cg14_put_cmap __P((struct fbcmap *, union cg14cmap *, int));
    131 static void cg14_load_hwcmap __P((struct cgfourteen_softc *, int, int));
    132 static void cg14_init __P((struct cgfourteen_softc *));
    133 static void cg14_reset __P((struct cgfourteen_softc *));
    134 static void cg14_loadomap __P((struct cgfourteen_softc *));/* cursor overlay */
    135 static void cg14_setcursor __P((struct cgfourteen_softc *));/* set position */
    136 static void cg14_loadcursor __P((struct cgfourteen_softc *));/* set shape */
    137 
    138 /*
    139  * Match a cgfourteen.
    140  */
    141 int
    142 cgfourteenmatch(parent, vcf, aux)
    143 	struct device *parent;
    144 	void *vcf, *aux;
    145 {
    146 	struct cfdata *cf = vcf;
    147 	struct confargs *ca = aux;
    148 	struct romaux *ra = &ca->ca_ra;
    149 
    150 	/*
    151 	 * Mask out invalid flags from the user.
    152 	 */
    153 	cf->cf_flags &= FB_USERMASK;
    154 
    155 	/* Check driver name */
    156 	if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
    157 		return (0);
    158 
    159 	/*
    160 	 * The cgfourteen is a local-bus video adaptor, accessed directly
    161 	 * via the processor, and not through device space or an external
    162 	 * bus. Thus we look _only_ at the obio bus.
    163 	 * Additionally, these things exist only on the Sun4m.
    164 	 */
    165 	if (CPU_ISSUN4M && ca->ca_bustype == BUS_OBIO)
    166 		return(1);
    167 	return (0);
    168 }
    169 
    170 /*
    171  * Attach a display.  We need to notice if it is the console, too.
    172  */
    173 void
    174 cgfourteenattach(parent, self, args)
    175 	struct device *parent, *self;
    176 	void *args;
    177 {
    178 	register struct cgfourteen_softc *sc = (struct cgfourteen_softc *)self;
    179 	register struct confargs *ca = args;
    180 	register int node = 0, ramsize;
    181 	register u_int32_t *lut;
    182 	int i, isconsole;
    183 
    184 	sc->sc_fb.fb_driver = &cgfourteenfbdriver;
    185 	sc->sc_fb.fb_device = &sc->sc_dev;
    186 	sc->sc_fb.fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
    187 
    188 	/*
    189 	 * We're emulating a cg3/8, so represent ourselves as one
    190 	 */
    191 #ifdef CG14_CG8
    192 	sc->sc_fb.fb_type.fb_type = FBTYPE_MEMCOLOR;
    193 #else
    194 	sc->sc_fb.fb_type.fb_type = FBTYPE_SUN3COLOR;
    195 #endif
    196 
    197 	node = ca->ca_ra.ra_node;
    198 #ifdef CG14_CG8
    199 	sc->sc_fb.fb_type.fb_depth = 32;
    200 #else
    201 	sc->sc_fb.fb_type.fb_depth = 8;
    202 #endif
    203 	fb_setsize(&sc->sc_fb, sc->sc_fb.fb_type.fb_depth,
    204 	    1152, 900, node, ca->ca_bustype);
    205 
    206 	ramsize = roundup(sc->sc_fb.fb_type.fb_height * sc->sc_fb.fb_linebytes,
    207 		NBPG);
    208 
    209 	sc->sc_fb.fb_type.fb_cmsize = CG14_CLUT_SIZE;
    210 	sc->sc_fb.fb_type.fb_size = ramsize;
    211 
    212 	/*
    213 	 * Now map in the 8 useful pages of registers
    214 	 */
    215 	if (ca->ca_ra.ra_len < 0x10000) {
    216 #ifdef DIAGNOSTIC
    217 		printf("warning: can't find all cgfourteen registers...\n");
    218 #endif
    219 		ca->ca_ra.ra_len = 0x10000;
    220 	}
    221 	sc->sc_ctl = (struct cg14ctl *) mapiodev(ca->ca_ra.ra_reg, 0,
    222 						 ca->ca_ra.ra_len,
    223 						 ca->ca_bustype);
    224 
    225 	sc->sc_hwc = (struct cg14curs *) ((u_int)sc->sc_ctl +
    226 					  CG14_OFFSET_CURS);
    227 	sc->sc_dac = (struct cg14dac *) ((u_int)sc->sc_ctl +
    228 					 CG14_OFFSET_DAC);
    229 	sc->sc_xlut = (struct cg14xlut *) ((u_int)sc->sc_ctl +
    230 					   CG14_OFFSET_XLUT);
    231 	sc->sc_clut1 = (struct cg14clut *) ((u_int)sc->sc_ctl +
    232 					    CG14_OFFSET_CLUT1);
    233 	sc->sc_clut2 = (struct cg14clut *) ((u_int)sc->sc_ctl +
    234 					    CG14_OFFSET_CLUT2);
    235 	sc->sc_clut3 = (struct cg14clut *) ((u_int)sc->sc_ctl +
    236 					    CG14_OFFSET_CLUT3);
    237 	sc->sc_clutincr = (u_int *) ((u_int)sc->sc_ctl +
    238 				     CG14_OFFSET_CLUTINCR);
    239 
    240 	/*
    241 	 * Stash the physical address of the framebuffer for use by mmap
    242 	 */
    243 	if (ca->ca_ra.ra_nreg < 2)
    244 		panic("cgfourteen with only one register set; can't find"
    245 		      " framebuffer");
    246 	sc->sc_phys = ca->ca_ra.ra_reg[1];
    247 
    248 #if defined(DEBUG) && defined(CG14_MAP_REGS)
    249 	/* Store the physical address of the control registers */
    250 	sc->sc_regphys = ca->ca_ra.ra_reg[0];
    251 #endif
    252 
    253 	/*
    254 	 * Let the user know that we're here
    255 	 */
    256 #ifdef CG14_CG8
    257 	printf(": cgeight emulated at %dx%dx24bpp",
    258 	       sc->sc_fb.fb_type.fb_width, sc->sc_fb.fb_type.fb_height);
    259 #else
    260 	printf(": cgthree emulated at %dx%dx8bpp",
    261 	       sc->sc_fb.fb_type.fb_width, sc->sc_fb.fb_type.fb_height);
    262 #endif
    263 	/*
    264 	 * Enable the video, but don't change the pixel depth.
    265 	 */
    266 	cg14_set_video(sc, 1);
    267 
    268 	/*
    269 	 * Grab the initial colormap
    270 	 */
    271 	lut = (u_int32_t *) sc->sc_clut1->clut_lut;
    272 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    273 		sc->sc_cmap.cm_chip[i] = lut[i];
    274 
    275 	/* See if we're the console */
    276 	isconsole = node == fbnode && fbconstty != NULL;
    277 
    278 	/*
    279 	 * We don't use the raster console since the cg14 is fast enough
    280 	 * already.
    281 	 */
    282 #ifdef notdef
    283 	/*
    284 	 * When the ROM has mapped in a cgfourteen display, the address
    285 	 * maps only the video RAM, so in any case we have to map the
    286 	 * registers ourselves.  We only need the video RAM if we are
    287 	 * going to print characters via rconsole.
    288 	 */
    289 	if ((sc->sc_fb.fb_pixels = ca->ca_ra.ra_vaddr) == NULL && isconsole) {
    290 		/* this probably cannot happen, but what the heck */
    291 		sc->sc_fb.fb_pixels = mapiodev(ca->ca_ra.ra_reg, CG3REG_MEM,
    292 						ramsize, ca->ca_bustype);
    293 	}
    294 #endif /* notdef */
    295 
    296 
    297 	if (isconsole) {
    298 		printf(" (console)\n");
    299 #ifdef notdef
    300 #ifdef RASTERCONSOLE
    301 		fbrcons_init(&sc->sc_fb);
    302 #endif
    303 #endif /* notdef */
    304 	} else
    305 		printf("\n");
    306 
    307 	/* Attach to /dev/fb */
    308 	if (node == fbnode)
    309 		fb_attach(&sc->sc_fb, isconsole);
    310 }
    311 
    312 /*
    313  * Keep track of the number of opens made. In the 24-bit driver, we need to
    314  * switch to 24-bit mode on the first open, and switch back to 8-bit on
    315  * the last close. This kind of nonsense is needed to give screenblank
    316  * a fighting chance of working.
    317  */
    318 static int cg14_opens = 0;
    319 
    320 int
    321 cgfourteenopen(dev, flags, mode, p)
    322 	dev_t dev;
    323 	int flags, mode;
    324 	struct proc *p;
    325 {
    326 	register struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    327 	int unit = minor(dev);
    328 	int s, oldopens;
    329 
    330 	if (unit >= cgfourteen_cd.cd_ndevs ||
    331 	    cgfourteen_cd.cd_devs[unit] == NULL)
    332 		return (ENXIO);
    333 
    334 	s = splhigh();
    335 	oldopens = cg14_opens++;
    336 	splx(s);
    337 
    338 	/* Setup the cg14 as we want it, and save the original PROM state */
    339 	if (oldopens == 0)	/* first open only, to make screenblank work */
    340 		cg14_init(sc);
    341 
    342 	return (0);
    343 }
    344 
    345 int
    346 cgfourteenclose(dev, flags, mode, p)
    347 	dev_t dev;
    348 	int flags, mode;
    349 	struct proc *p;
    350 {
    351 	register struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    352 	int s, opens;
    353 
    354 	s = splhigh();
    355 	opens = --cg14_opens;
    356 	if (cg14_opens < 0)
    357 		opens = cg14_opens = 0;
    358 	splx(s);
    359 
    360 	/*
    361 	 * Restore video state to make the PROM happy, on last close.
    362 	 */
    363 	if (opens == 0)
    364 		cg14_reset(sc);
    365 
    366 	return (0);
    367 }
    368 
    369 int
    370 cgfourteenioctl(dev, cmd, data, flags, p)
    371 	dev_t dev;
    372 	u_long cmd;
    373 	register caddr_t data;
    374 	int flags;
    375 	struct proc *p;
    376 {
    377 	register struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    378 	register struct fbgattr *fba;
    379 	union cg14cursor_cmap tcm;
    380 	int v, error;
    381 	u_int count;
    382 
    383 	switch (cmd) {
    384 
    385 	case FBIOGTYPE:
    386 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    387 		break;
    388 
    389 	case FBIOGATTR:
    390 		fba = (struct fbgattr *)data;
    391 		fba->real_type = FBTYPE_MDICOLOR;
    392 		fba->owner = 0;		/* XXX ??? */
    393 		fba->fbtype = sc->sc_fb.fb_type;
    394 		fba->sattr.flags = 0;
    395 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    396 		fba->sattr.dev_specific[0] = -1;
    397 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    398 		fba->emu_types[1] = -1;
    399 		break;
    400 
    401 	case FBIOGETCMAP:
    402 		return (cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
    403 				     CG14_CLUT_SIZE));
    404 
    405 	case FBIOPUTCMAP:
    406 		/* copy to software map */
    407 #define p ((struct fbcmap *)data)
    408 #ifdef CG14_CG8
    409 		p->index &= 0xffffff;
    410 #endif
    411 		error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
    412 		if (error)
    413 			return (error);
    414 		/* now blast them into the chip */
    415 		/* XXX should use retrace interrupt */
    416 		cg14_load_hwcmap(sc, p->index, p->count);
    417 #undef p
    418 		break;
    419 
    420 	case FBIOGVIDEO:
    421 		*(int *)data = cg14_get_video(sc);
    422 		break;
    423 
    424 	case FBIOSVIDEO:
    425 		cg14_set_video(sc, *(int *)data);
    426 		break;
    427 
    428 /* these are for both FBIOSCURSOR and FBIOGCURSOR */
    429 #define p ((struct fbcursor *)data)
    430 #define cc (&sc->sc_cursor)
    431 	case FBIOGCURSOR:
    432 		/* do not quite want everything here... */
    433 		p->set = FB_CUR_SETALL;	/* close enough, anyway */
    434 		p->enable = cc->cc_enable;
    435 		p->pos = cc->cc_pos;
    436 		p->hot = cc->cc_hot;
    437 		p->size = cc->cc_size;
    438 
    439 		/* begin ugh ... can we lose some of this crap?? */
    440 		if (p->image != NULL) {
    441 			count = cc->cc_size.y * 32 / NBBY;
    442 			error = copyout((caddr_t)cc->cc_cplane,
    443 			    (caddr_t)p->image, count);
    444 			if (error)
    445 				return (error);
    446 			error = copyout((caddr_t)cc->cc_eplane,
    447 			    (caddr_t)p->mask, count);
    448 			if (error)
    449 				return (error);
    450 		}
    451 		if (p->cmap.red != NULL) {
    452 			error = cg14_get_cmap(&p->cmap,
    453 			    (union cg14cmap *)&cc->cc_color, 2);
    454 			if (error)
    455 				return (error);
    456 		} else {
    457 			p->cmap.index = 0;
    458 			p->cmap.count = 2;
    459 		}
    460 		/* end ugh */
    461 		break;
    462 
    463 	case FBIOSCURSOR:
    464 		/*
    465 		 * For setcmap and setshape, verify parameters, so that
    466 		 * we do not get halfway through an update and then crap
    467 		 * out with the software state screwed up.
    468 		 */
    469 		v = p->set;
    470 		if (v & FB_CUR_SETCMAP) {
    471 			/*
    472 			 * This use of a temporary copy of the cursor
    473 			 * colormap is not terribly efficient, but these
    474 			 * copies are small (8 bytes)...
    475 			 */
    476 			tcm = cc->cc_color;
    477 			error = cg14_put_cmap(&p->cmap, (union cg14cmap *)&tcm,
    478 					      2);
    479 			if (error)
    480 				return (error);
    481 		}
    482 		if (v & FB_CUR_SETSHAPE) {
    483 			if ((u_int)p->size.x > 32 || (u_int)p->size.y > 32)
    484 				return (EINVAL);
    485 			count = p->size.y * 32 / NBBY;
    486 			if (!useracc(p->image, count, B_READ) ||
    487 			    !useracc(p->mask, count, B_READ))
    488 				return (EFAULT);
    489 		}
    490 
    491 		/* parameters are OK; do it */
    492 		if (v & (FB_CUR_SETCUR | FB_CUR_SETPOS | FB_CUR_SETHOT)) {
    493 			if (v & FB_CUR_SETCUR)
    494 				cc->cc_enable = p->enable;
    495 			if (v & FB_CUR_SETPOS)
    496 				cc->cc_pos = p->pos;
    497 			if (v & FB_CUR_SETHOT)
    498 				cc->cc_hot = p->hot;
    499 			cg14_setcursor(sc);
    500 		}
    501 		if (v & FB_CUR_SETCMAP) {
    502 			cc->cc_color = tcm;
    503 			cg14_loadomap(sc); /* XXX defer to vertical retrace */
    504 		}
    505 		if (v & FB_CUR_SETSHAPE) {
    506 			cc->cc_size = p->size;
    507 			count = p->size.y * 32 / NBBY;
    508 			bzero((caddr_t)cc->cc_eplane, sizeof cc->cc_eplane);
    509 			bzero((caddr_t)cc->cc_cplane, sizeof cc->cc_cplane);
    510 			bcopy(p->mask, (caddr_t)cc->cc_eplane, count);
    511 			bcopy(p->image, (caddr_t)cc->cc_cplane, count);
    512 			cg14_loadcursor(sc);
    513 		}
    514 		break;
    515 
    516 #undef cc
    517 #undef p
    518 	case FBIOGCURPOS:
    519 		*(struct fbcurpos *)data = sc->sc_cursor.cc_pos;
    520 		break;
    521 
    522 	case FBIOSCURPOS:
    523 		sc->sc_cursor.cc_pos = *(struct fbcurpos *)data;
    524 		cg14_setcursor(sc);
    525 		break;
    526 
    527 	case FBIOGCURMAX:
    528 		/* max cursor size is 32x32 */
    529 		((struct fbcurpos *)data)->x = 32;
    530 		((struct fbcurpos *)data)->y = 32;
    531 		break;
    532 
    533 	default:
    534 		return (ENOTTY);
    535 	}
    536 	return (0);
    537 }
    538 
    539 /*
    540  * Undo the effect of an FBIOSVIDEO that turns the video off.
    541  */
    542 static void
    543 cgfourteenunblank(dev)
    544 	struct device *dev;
    545 {
    546 
    547 	cg14_set_video((struct cgfourteen_softc *)dev, 1);
    548 }
    549 
    550 /*
    551  * Return the address that would map the given device at the given
    552  * offset, allowing for the given protection, or return -1 for error.
    553  *
    554  * The cg14 frame buffer can be mapped in either 8-bit or 32-bit mode
    555  * starting at the address stored in the PROM. In 8-bit mode, the X
    556  * channel is not present, and can be ignored. In 32-bit mode, mapping
    557  * at 0K delivers a 32-bpp buffer where the upper 8 bits select the X
    558  * channel information. We hardwire the Xlut to all zeroes to insure
    559  * that, regardless of this value, direct 24-bit color access will be
    560  * used.
    561  *
    562  * Alternatively, mapping the frame buffer at an offset of 16M seems to
    563  * tell the chip to ignore the X channel. XXX where does it get the X value
    564  * to use?
    565  */
    566 int
    567 cgfourteenmmap(dev, off, prot)
    568 	dev_t dev;
    569 	int off, prot;
    570 {
    571 	register struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    572 
    573 #define CG3START		(128*1024 + 128*1024)
    574 #define CG8START		(256*1024)
    575 #define NOOVERLAY	(0x04000000)
    576 
    577 	if (off & PGOFSET)
    578 		panic("cgfourteenmmap");
    579 
    580 #if defined(DEBUG) && defined(CG14_MAP_REGS) /* XXX: security hole */
    581 	/*
    582 	 * Map the control registers into user space. Should only be
    583 	 * used for debugging!
    584 	 */
    585 	if ((u_int)off >= 0x10000000 && (u_int)off < 0x10000000 + 16*4096) {
    586 		off -= 0x10000000;
    587 		return (REG2PHYS(&sc->sc_regphys, off, 0) | PMAP_NC);
    588 	}
    589 #endif
    590 
    591 	if ((u_int)off >= NOOVERLAY)
    592 		off -= NOOVERLAY;
    593 #ifdef CG14_CG8
    594 	else if ((u_int)off >= CG8START) {
    595 		off -= CG8START;
    596 	}
    597 #else
    598 	else if ((u_int)off >= CG3START)
    599 		off -= CG3START;
    600 #endif
    601 	else
    602 		off = 0;
    603 
    604 	if ((unsigned)off >= sc->sc_fb.fb_type.fb_size *
    605 		sc->sc_fb.fb_type.fb_depth/8) {
    606 #ifdef DEBUG
    607 		printf("\nmmap request out of bounds: request 0x%x, "
    608 		       "bound 0x%x\n", (unsigned) off,
    609 		       (unsigned)sc->sc_fb.fb_type.fb_size);
    610 #endif
    611 		return (-1);
    612 	}
    613 
    614 	/*
    615 	 * Use PMAP_NC to disable the cache, since otherwise refresh is
    616 	 * very confused.
    617 	 */
    618 	return (REG2PHYS(&sc->sc_phys, off, 0) | PMAP_NC);
    619 }
    620 
    621 int
    622 cgfourteenpoll(dev, events, p)
    623 	dev_t dev;
    624 	int events;
    625 	struct proc *p;
    626 {
    627 
    628 	return (seltrue(dev, events, p));
    629 }
    630 
    631 /*
    632  * Miscellaneous helper functions
    633  */
    634 
    635 /* Initialize the framebuffer, storing away useful state for later reset */
    636 static void
    637 cg14_init(sc)
    638 	struct cgfourteen_softc *sc;
    639 {
    640 	register u_int32_t *clut;
    641 	register u_int8_t  *xlut;
    642 	register int i;
    643 
    644 	/*
    645 	 * We stash away the following to restore on close:
    646 	 *
    647 	 * 	color look-up table 1 	(sc->sc_saveclut)
    648 	 *	x look-up table		(sc->sc_savexlut)
    649 	 *	control register	(sc->sc_savectl)
    650 	 *	cursor control register (sc->sc_savehwc)
    651 	 */
    652 	sc->sc_savectl = sc->sc_ctl->ctl_mctl;
    653 	sc->sc_savehwc = sc->sc_hwc->curs_ctl;
    654 
    655 	clut = (u_int32_t *) sc->sc_clut1->clut_lut;
    656 	xlut = (u_int8_t *) sc->sc_xlut->xlut_lut;
    657 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    658 		sc->sc_saveclut.cm_chip[i] = clut[i];
    659 		sc->sc_savexlut[i] = xlut[i];
    660 	}
    661 
    662 #ifdef CG14_CG8
    663 	/*
    664 	 * Enable the video, and put in 24 bit mode.
    665 	 */
    666 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 |
    667 		CG14_MCTL_POWERCTL;
    668 
    669 	/*
    670 	 * Zero the xlut to enable direct-color mode
    671 	 */
    672 	bzero(sc->sc_xlut, CG14_CLUT_SIZE);
    673 #else
    674 	/*
    675 	 * Enable the video and put it in 8 bit mode
    676 	 */
    677 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
    678 		CG14_MCTL_POWERCTL;
    679 #endif
    680 }
    681 
    682 static void
    683 cg14_reset(sc)	/* Restore the state saved on cg14_init */
    684 	struct cgfourteen_softc *sc;
    685 {
    686 	register u_int32_t *clut;
    687 	register u_int8_t  *xlut;
    688 	register int i;
    689 
    690 	/*
    691 	 * We restore the following, saved in cg14_init:
    692 	 *
    693 	 * 	color look-up table 1 	(sc->sc_saveclut)
    694 	 *	x look-up table		(sc->sc_savexlut)
    695 	 *	control register	(sc->sc_savectl)
    696 	 *	cursor control register (sc->sc_savehwc)
    697 	 *
    698 	 * Note that we don't touch the video enable bits in the
    699 	 * control register; otherwise, screenblank wouldn't work.
    700 	 */
    701 	sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
    702 							CG14_MCTL_POWERCTL)) |
    703 				(sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
    704 						    CG14_MCTL_POWERCTL));
    705 	sc->sc_hwc->curs_ctl = sc->sc_savehwc;
    706 
    707 	clut = (u_int32_t *) sc->sc_clut1->clut_lut;
    708 	xlut = (u_int8_t *) sc->sc_xlut->xlut_lut;
    709 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    710 		clut[i] = sc->sc_saveclut.cm_chip[i];
    711 		xlut[i] = sc->sc_savexlut[i];
    712 	}
    713 }
    714 
    715 /* Enable/disable video display; power down monitor if DPMS-capable */
    716 static void
    717 cg14_set_video(sc, enable)
    718 	struct cgfourteen_softc *sc;
    719 	int enable;
    720 {
    721 	/*
    722 	 * We can only use DPMS to power down the display if the chip revision
    723 	 * is greater than 0.
    724 	 */
    725 	if (enable) {
    726 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    727 			sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
    728 						 CG14_MCTL_POWERCTL);
    729 		else
    730 			sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
    731 	} else {
    732 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    733 			sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
    734 						  CG14_MCTL_POWERCTL);
    735 		else
    736 			sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
    737 	}
    738 }
    739 
    740 /* Get status of video display */
    741 static int
    742 cg14_get_video(sc)
    743 	struct cgfourteen_softc *sc;
    744 {
    745 	return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
    746 }
    747 
    748 /* Read the software shadow colormap */
    749 static int
    750 cg14_get_cmap(p, cm, cmsize)
    751 	register struct fbcmap *p;
    752 	union cg14cmap *cm;
    753 	int cmsize;
    754 {
    755         register u_int i, start, count;
    756         register u_char *cp;
    757 
    758         start = p->index;
    759         count = p->count;
    760         if (start >= cmsize || start + count > cmsize)
    761 #ifdef DEBUG
    762 	{
    763 		printf("putcmaperror: start %d cmsize %d count %d\n",
    764 		       start,cmsize,count);
    765 #endif
    766                 return (EINVAL);
    767 #ifdef DEBUG
    768 	}
    769 #endif
    770 
    771         if (!useracc(p->red, count, B_WRITE) ||
    772             !useracc(p->green, count, B_WRITE) ||
    773             !useracc(p->blue, count, B_WRITE))
    774                 return (EFAULT);
    775         for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    776                 p->red[i] = cp[3];
    777                 p->green[i] = cp[2];
    778                 p->blue[i] = cp[1];
    779         }
    780         return (0);
    781 }
    782 
    783 /* Write the software shadow colormap */
    784 static int
    785 cg14_put_cmap(p, cm, cmsize)
    786         register struct fbcmap *p;
    787         union cg14cmap *cm;
    788         int cmsize;
    789 {
    790         register u_int i, start, count;
    791         register u_char *cp;
    792 
    793         start = p->index;
    794         count = p->count;
    795         if (start >= cmsize || start + count > cmsize)
    796 #ifdef DEBUG
    797 	{
    798 		printf("putcmaperror: start %d cmsize %d count %d\n",
    799 		       start,cmsize,count);
    800 #endif
    801                 return (EINVAL);
    802 #ifdef DEBUG
    803 	}
    804 #endif
    805 
    806         if (!useracc(p->red, count, B_READ) ||
    807             !useracc(p->green, count, B_READ) ||
    808             !useracc(p->blue, count, B_READ))
    809                 return (EFAULT);
    810         for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    811                 cp[3] = p->red[i];
    812                 cp[2] = p->green[i];
    813                 cp[1] = p->blue[i];
    814 		cp[0] = 0;	/* no alpha channel */
    815         }
    816         return (0);
    817 }
    818 
    819 static void
    820 cg14_load_hwcmap(sc, start, ncolors)
    821 	register struct cgfourteen_softc *sc;
    822 	register int start, ncolors;
    823 {
    824 	/* XXX switch to auto-increment, and on retrace intr */
    825 
    826 	/* Setup pointers to source and dest */
    827 	register u_int32_t *colp = &sc->sc_cmap.cm_chip[start];
    828 	volatile register u_int32_t *lutp = &sc->sc_clut1->clut_lut[start];
    829 
    830 	/* Copy by words */
    831 	while (--ncolors >= 0)
    832 		*lutp++ = *colp++;
    833 }
    834 
    835 /*
    836  * Load the cursor (overlay `foreground' and `background') colors.
    837  */
    838 static void
    839 cg14_setcursor(sc)
    840 	register struct cgfourteen_softc *sc;
    841 {
    842 	/* we need to subtract the hot-spot value here */
    843 #define COORD(f) (sc->sc_cursor.cc_pos.f - sc->sc_cursor.cc_hot.f)
    844 
    845 	sc->sc_hwc->curs_ctl = (sc->sc_cursor.cc_enable ? CG14_CURS_ENABLE : 0);
    846 	sc->sc_hwc->curs_x = COORD(x);
    847 	sc->sc_hwc->curs_y = COORD(y);
    848 
    849 #undef COORD
    850 }
    851 
    852 static void
    853 cg14_loadcursor(sc)
    854 	register struct cgfourteen_softc *sc;
    855 {
    856 	register volatile struct cg14curs *hwc;
    857 	register u_int edgemask, m;
    858 	register int i;
    859 
    860 	/*
    861 	 * Keep the top size.x bits.  Here we *throw out* the top
    862 	 * size.x bits from an all-one-bits word, introducing zeros in
    863 	 * the top size.x bits, then invert all the bits to get what
    864 	 * we really wanted as our mask.  But this fails if size.x is
    865 	 * 32---a sparc uses only the low 5 bits of the shift count---
    866 	 * so we have to special case that.
    867 	 */
    868 	edgemask = ~0;
    869 	if (sc->sc_cursor.cc_size.x < 32)
    870 		edgemask = ~(edgemask >> sc->sc_cursor.cc_size.x);
    871 	hwc = sc->sc_hwc;
    872 	for (i = 0; i < 32; i++) {
    873 		m = sc->sc_cursor.cc_eplane[i] & edgemask;
    874 		hwc->curs_plane0[i] = m;
    875 		hwc->curs_plane1[i] = m & sc->sc_cursor.cc_cplane[i];
    876 	}
    877 }
    878 
    879 static void
    880 cg14_loadomap(sc)
    881 	register struct cgfourteen_softc *sc;
    882 {
    883 	/* set background color */
    884 	sc->sc_hwc->curs_color1 = sc->sc_cursor.cc_color.cm_chip[0];
    885 	/* set foreground color */
    886 	sc->sc_hwc->curs_color2 = sc->sc_cursor.cc_color.cm_chip[1];
    887 }
    888