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