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