Home | History | Annotate | Line # | Download | only in dev
cgfourteen.c revision 1.35
      1 /*	$NetBSD: cgfourteen.c,v 1.35 2003/07/15 00:04:52 lukem 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/cdefs.h>
     81 __KERNEL_RCSID(0, "$NetBSD: cgfourteen.c,v 1.35 2003/07/15 00:04:52 lukem Exp $");
     82 
     83 #include <sys/param.h>
     84 #include <sys/systm.h>
     85 #include <sys/buf.h>
     86 #include <sys/device.h>
     87 #include <sys/ioctl.h>
     88 #include <sys/malloc.h>
     89 #include <sys/mman.h>
     90 #include <sys/tty.h>
     91 #include <sys/conf.h>
     92 
     93 #include <uvm/uvm_extern.h>
     94 
     95 #include <machine/bus.h>
     96 #include <machine/autoconf.h>
     97 
     98 #include <dev/sbus/sbusvar.h>
     99 
    100 #include <dev/sun/fbio.h>
    101 #include <dev/sun/fbvar.h>
    102 
    103 #include <sparc/dev/cgfourteenreg.h>
    104 #include <sparc/dev/cgfourteenvar.h>
    105 
    106 /* autoconfiguration driver */
    107 static void	cgfourteenattach(struct device *, struct device *, void *);
    108 static int	cgfourteenmatch(struct device *, struct cfdata *, void *);
    109 static void	cgfourteenunblank(struct device *);
    110 
    111 CFATTACH_DECL(cgfourteen, sizeof(struct cgfourteen_softc),
    112     cgfourteenmatch, cgfourteenattach, NULL, NULL);
    113 
    114 extern struct cfdriver cgfourteen_cd;
    115 
    116 dev_type_open(cgfourteenopen);
    117 dev_type_close(cgfourteenclose);
    118 dev_type_ioctl(cgfourteenioctl);
    119 dev_type_mmap(cgfourteenmmap);
    120 
    121 const struct cdevsw cgfourteen_cdevsw = {
    122 	cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
    123 	nostop, notty, nopoll, cgfourteenmmap, nokqfilter,
    124 };
    125 
    126 /* frame buffer generic driver */
    127 static struct fbdriver cgfourteenfbdriver = {
    128 	cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
    129 	nopoll, cgfourteenmmap, nokqfilter
    130 };
    131 
    132 static void cg14_set_video __P((struct cgfourteen_softc *, int));
    133 static int  cg14_get_video __P((struct cgfourteen_softc *));
    134 static int  cg14_get_cmap __P((struct fbcmap *, union cg14cmap *, int));
    135 static int  cg14_put_cmap __P((struct fbcmap *, union cg14cmap *, int));
    136 static void cg14_load_hwcmap __P((struct cgfourteen_softc *, int, int));
    137 static void cg14_init __P((struct cgfourteen_softc *));
    138 static void cg14_reset __P((struct cgfourteen_softc *));
    139 static void cg14_loadomap __P((struct cgfourteen_softc *));/* cursor overlay */
    140 static void cg14_setcursor __P((struct cgfourteen_softc *));/* set position */
    141 static void cg14_loadcursor __P((struct cgfourteen_softc *));/* set shape */
    142 
    143 /*
    144  * We map the display memory with an offset of 256K when emulating the cg3 or
    145  * cg8; the cg3 uses this offset for compatibility with the cg4, and both the
    146  * cg4 and cg8 have a mono overlay plane and an overlay enable plane in the
    147  * first 256K.  Mapping at an offset of 0x04000000 causes only the color
    148  * frame buffer to be mapped, without the overlay planes.
    149  */
    150 #define START		(128*1024 + 128*1024)
    151 #define NOOVERLAY	(0x04000000)
    152 
    153 /*
    154  * Match a cgfourteen.
    155  */
    156 int
    157 cgfourteenmatch(parent, cf, aux)
    158 	struct device *parent;
    159 	struct cfdata *cf;
    160 	void *aux;
    161 {
    162 	union obio_attach_args *uoba = aux;
    163 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    164 
    165 	/*
    166 	 * The cgfourteen is a local-bus video adaptor, accessed directly
    167 	 * via the processor, and not through device space or an external
    168 	 * bus. Thus we look _only_ at the obio bus.
    169 	 * Additionally, these things exist only on the Sun4m.
    170 	 */
    171 
    172 	if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
    173 		return (0);
    174 
    175 	/* Check driver name */
    176 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
    177 }
    178 
    179 /*
    180  * Attach a display.  We need to notice if it is the console, too.
    181  */
    182 void
    183 cgfourteenattach(parent, self, aux)
    184 	struct device *parent, *self;
    185 	void *aux;
    186 {
    187 	union obio_attach_args *uoba = aux;
    188 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    189 	struct cgfourteen_softc *sc = (struct cgfourteen_softc *)self;
    190 	struct fbdevice *fb = &sc->sc_fb;
    191 	bus_space_handle_t bh;
    192 	int node, ramsize;
    193 	u_int32_t *lut;
    194 	int i, isconsole;
    195 
    196 	node = sa->sa_node;
    197 
    198 	/* Remember cookies for cgfourteenmmap() */
    199 	sc->sc_bustag = sa->sa_bustag;
    200 
    201 	fb->fb_driver = &cgfourteenfbdriver;
    202 	fb->fb_device = &sc->sc_dev;
    203 	/* Mask out invalid flags from the user. */
    204 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
    205 
    206 	/*
    207 	 * We're emulating a cg3/8, so represent ourselves as one
    208 	 */
    209 #ifdef CG14_CG8
    210 	fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
    211 	fb->fb_type.fb_depth = 32;
    212 #else
    213 	fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
    214 	fb->fb_type.fb_depth = 8;
    215 #endif
    216 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
    217 #ifdef CG14_CG8
    218 	/*
    219 	 * fb_setsize_obp set fb->fb_linebytes based on the current
    220 	 * depth reported by obp, but that defaults to 8 bits (as
    221 	 * reported by getpropint().  Update the value to reflect
    222 	 * the depth that will be used after open.
    223 	 * The display memory size returned by the cg8 driver includes
    224 	 * the space used by the overlay planes, but the size returned
    225 	 * by the cg3 driver does not; emulate the other drivers.
    226 	 */
    227 	fb->fb_linebytes = (fb->fb_type.fb_width * fb->fb_type.fb_depth) / 8;
    228 	ramsize = roundup(START + (fb->fb_type.fb_height * fb->fb_linebytes),
    229 			PAGE_SIZE);
    230 #else
    231 	ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, PAGE_SIZE);
    232 #endif
    233 	fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
    234 	fb->fb_type.fb_size = ramsize;
    235 
    236 	if (sa->sa_nreg < 2) {
    237 		printf("%s: only %d register sets\n",
    238 			self->dv_xname, sa->sa_nreg);
    239 		return;
    240 	}
    241 	bcopy(sa->sa_reg, sc->sc_physadr,
    242 	      sa->sa_nreg * sizeof(struct openprom_addr));
    243 
    244 	/*
    245 	 * Now map in the 8 useful pages of registers
    246 	 */
    247 	if (sa->sa_size < 0x10000) {
    248 #ifdef DIAGNOSTIC
    249 		printf("warning: can't find all cgfourteen registers...\n");
    250 #endif
    251 		sa->sa_size = 0x10000;
    252 	}
    253 	if (sbus_bus_map(sa->sa_bustag,
    254 			 sa->sa_slot, sa->sa_offset, sa->sa_size,
    255 			 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    256 		printf("%s: cannot map control registers\n", self->dv_xname);
    257 		return;
    258 	}
    259 
    260 	sc->sc_ctl   = (struct cg14ctl  *) (bh);
    261 	sc->sc_hwc   = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
    262 	sc->sc_dac   = (struct cg14dac  *) (bh + CG14_OFFSET_DAC);
    263 	sc->sc_xlut  = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
    264 	sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
    265 	sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
    266 	sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
    267 	sc->sc_clutincr =        (u_int *) (bh + CG14_OFFSET_CLUTINCR);
    268 
    269 	/*
    270 	 * Let the user know that we're here
    271 	 */
    272 #ifdef CG14_CG8
    273 	printf(": cgeight emulated at %dx%dx24bpp",
    274 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    275 #else
    276 	printf(": cgthree emulated at %dx%dx8bpp",
    277 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    278 #endif
    279 	/*
    280 	 * Enable the video, but don't change the pixel depth.
    281 	 */
    282 	cg14_set_video(sc, 1);
    283 
    284 	/*
    285 	 * Grab the initial colormap
    286 	 */
    287 	lut = (u_int32_t *) sc->sc_clut1->clut_lut;
    288 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    289 		sc->sc_cmap.cm_chip[i] = lut[i];
    290 
    291 	/* See if we're the console */
    292 	isconsole = fb_is_console(node);
    293 
    294 	if (isconsole) {
    295 		printf(" (console)\n");
    296 #ifdef notdef
    297 		/*
    298 		 * We don't use the raster console since the cg14 is
    299 		 * fast enough already.
    300 		 */
    301 #ifdef RASTERCONSOLE
    302 		fbrcons_init(fb);
    303 #endif
    304 #endif /* notdef */
    305 	} else
    306 		printf("\n");
    307 
    308 	/* Attach to /dev/fb */
    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 	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 	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 	caddr_t data;
    374 	int flags;
    375 	struct proc *p;
    376 {
    377 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    378 	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 (!uvm_useracc(p->image, count, B_READ) ||
    487 			    !uvm_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 paddr_t
    567 cgfourteenmmap(dev, off, prot)
    568 	dev_t dev;
    569 	off_t off;
    570 	int prot;
    571 {
    572 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    573 
    574 	if (off & PGOFSET)
    575 		panic("cgfourteenmmap");
    576 
    577 	if (off < 0)
    578 		return (-1);
    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 		if (bus_space_mmap(sc->sc_bustag,
    588 			BUS_ADDR(sc->sc_physadr[CG14_CTL_IDX].sbr_slot,
    589 				sc->sc_physadr[CG14_CTL_IDX].sbr_offset),
    590 			off, prot, BUS_SPACE_MAP_LINEAR));
    591 	}
    592 #endif
    593 
    594 	if ((u_int)off >= NOOVERLAY)
    595 		off -= NOOVERLAY;
    596 	else if ((u_int)off >= START)
    597 		off -= START;
    598 	else
    599 		off = 0;
    600 
    601 	/*
    602 	 * fb_size includes the overlay space only for the CG8.
    603 	 */
    604 #ifdef CG14_CG8
    605 	if (off >= sc->sc_fb.fb_type.fb_size - START)
    606 #else
    607 	if (off >= sc->sc_fb.fb_type.fb_size)
    608 #endif
    609 	{
    610 #ifdef DEBUG
    611 		printf("\nmmap request out of bounds: request 0x%x, "
    612 		    "bound 0x%x\n", (unsigned) off,
    613 		    (unsigned)sc->sc_fb.fb_type.fb_size);
    614 #endif
    615 		return (-1);
    616 	}
    617 
    618 	return (bus_space_mmap(sc->sc_bustag,
    619 		BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].oa_space,
    620 			sc->sc_physadr[CG14_PXL_IDX].oa_base),
    621 		off, prot, BUS_SPACE_MAP_LINEAR));
    622 }
    623 
    624 /*
    625  * Miscellaneous helper functions
    626  */
    627 
    628 /* Initialize the framebuffer, storing away useful state for later reset */
    629 static void
    630 cg14_init(sc)
    631 	struct cgfourteen_softc *sc;
    632 {
    633 	u_int32_t *clut;
    634 	u_int8_t  *xlut;
    635 	int i;
    636 
    637 	/*
    638 	 * We stash away the following to restore on close:
    639 	 *
    640 	 * 	color look-up table 1 	(sc->sc_saveclut)
    641 	 *	x look-up table		(sc->sc_savexlut)
    642 	 *	control register	(sc->sc_savectl)
    643 	 *	cursor control register (sc->sc_savehwc)
    644 	 */
    645 	sc->sc_savectl = sc->sc_ctl->ctl_mctl;
    646 	sc->sc_savehwc = sc->sc_hwc->curs_ctl;
    647 
    648 	clut = (u_int32_t *) sc->sc_clut1->clut_lut;
    649 	xlut = (u_int8_t *) sc->sc_xlut->xlut_lut;
    650 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    651 		sc->sc_saveclut.cm_chip[i] = clut[i];
    652 		sc->sc_savexlut[i] = xlut[i];
    653 	}
    654 
    655 #ifdef CG14_CG8
    656 	/*
    657 	 * Enable the video, and put in 24 bit mode.
    658 	 */
    659 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 |
    660 		CG14_MCTL_POWERCTL;
    661 
    662 	/*
    663 	 * Zero the xlut to enable direct-color mode
    664 	 */
    665 	bzero(sc->sc_xlut, CG14_CLUT_SIZE);
    666 #else
    667 	/*
    668 	 * Enable the video and put it in 8 bit mode
    669 	 */
    670 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
    671 		CG14_MCTL_POWERCTL;
    672 #endif
    673 }
    674 
    675 static void
    676 cg14_reset(sc)	/* Restore the state saved on cg14_init */
    677 	struct cgfourteen_softc *sc;
    678 {
    679 	u_int32_t *clut;
    680 	u_int8_t  *xlut;
    681 	int i;
    682 
    683 	/*
    684 	 * We restore the following, saved in cg14_init:
    685 	 *
    686 	 * 	color look-up table 1 	(sc->sc_saveclut)
    687 	 *	x look-up table		(sc->sc_savexlut)
    688 	 *	control register	(sc->sc_savectl)
    689 	 *	cursor control register (sc->sc_savehwc)
    690 	 *
    691 	 * Note that we don't touch the video enable bits in the
    692 	 * control register; otherwise, screenblank wouldn't work.
    693 	 */
    694 	sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
    695 							CG14_MCTL_POWERCTL)) |
    696 				(sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
    697 						    CG14_MCTL_POWERCTL));
    698 	sc->sc_hwc->curs_ctl = sc->sc_savehwc;
    699 
    700 	clut = (u_int32_t *) sc->sc_clut1->clut_lut;
    701 	xlut = (u_int8_t *) sc->sc_xlut->xlut_lut;
    702 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    703 		clut[i] = sc->sc_saveclut.cm_chip[i];
    704 		xlut[i] = sc->sc_savexlut[i];
    705 	}
    706 }
    707 
    708 /* Enable/disable video display; power down monitor if DPMS-capable */
    709 static void
    710 cg14_set_video(sc, enable)
    711 	struct cgfourteen_softc *sc;
    712 	int enable;
    713 {
    714 	/*
    715 	 * We can only use DPMS to power down the display if the chip revision
    716 	 * is greater than 0.
    717 	 */
    718 	if (enable) {
    719 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    720 			sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
    721 						 CG14_MCTL_POWERCTL);
    722 		else
    723 			sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
    724 	} else {
    725 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    726 			sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
    727 						  CG14_MCTL_POWERCTL);
    728 		else
    729 			sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
    730 	}
    731 }
    732 
    733 /* Get status of video display */
    734 static int
    735 cg14_get_video(sc)
    736 	struct cgfourteen_softc *sc;
    737 {
    738 	return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
    739 }
    740 
    741 /* Read the software shadow colormap */
    742 static int
    743 cg14_get_cmap(p, cm, cmsize)
    744 	struct fbcmap *p;
    745 	union cg14cmap *cm;
    746 	int cmsize;
    747 {
    748 	u_int i, start, count;
    749 	u_char *cp;
    750 
    751 	start = p->index;
    752 	count = p->count;
    753 	if (start >= cmsize || count > cmsize - start)
    754 #ifdef DEBUG
    755 	{
    756 		printf("putcmaperror: start %d cmsize %d count %d\n",
    757 		    start,cmsize,count);
    758 #endif
    759 		return (EINVAL);
    760 #ifdef DEBUG
    761 	}
    762 #endif
    763 
    764 	if (!uvm_useracc(p->red, count, B_WRITE) ||
    765 	    !uvm_useracc(p->green, count, B_WRITE) ||
    766 	    !uvm_useracc(p->blue, count, B_WRITE))
    767 		return (EFAULT);
    768 	for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    769 		p->red[i] = cp[3];
    770 		p->green[i] = cp[2];
    771 		p->blue[i] = cp[1];
    772 	}
    773 	return (0);
    774 }
    775 
    776 /* Write the software shadow colormap */
    777 static int
    778 cg14_put_cmap(p, cm, cmsize)
    779 	struct fbcmap *p;
    780 	union cg14cmap *cm;
    781 	int cmsize;
    782 {
    783 	u_int i, start, count;
    784 	u_char *cp;
    785 
    786 	start = p->index;
    787 	count = p->count;
    788 	if (start >= cmsize || count > cmsize - start)
    789 #ifdef DEBUG
    790 	{
    791 		printf("putcmaperror: start %d cmsize %d count %d\n",
    792 		    start,cmsize,count);
    793 #endif
    794 		return (EINVAL);
    795 #ifdef DEBUG
    796 	}
    797 #endif
    798 
    799 	if (!uvm_useracc(p->red, count, B_READ) ||
    800 	    !uvm_useracc(p->green, count, B_READ) ||
    801 	    !uvm_useracc(p->blue, count, B_READ))
    802 		return (EFAULT);
    803 	for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    804 		cp[3] = p->red[i];
    805 		cp[2] = p->green[i];
    806 		cp[1] = p->blue[i];
    807 		cp[0] = 0;	/* no alpha channel */
    808 	}
    809 	return (0);
    810 }
    811 
    812 static void
    813 cg14_load_hwcmap(sc, start, ncolors)
    814 	struct cgfourteen_softc *sc;
    815 	int start, ncolors;
    816 {
    817 	/* XXX switch to auto-increment, and on retrace intr */
    818 
    819 	/* Setup pointers to source and dest */
    820 	u_int32_t *colp = &sc->sc_cmap.cm_chip[start];
    821 	volatile u_int32_t *lutp = &sc->sc_clut1->clut_lut[start];
    822 
    823 	/* Copy by words */
    824 	while (--ncolors >= 0)
    825 		*lutp++ = *colp++;
    826 }
    827 
    828 /*
    829  * Load the cursor (overlay `foreground' and `background') colors.
    830  */
    831 static void
    832 cg14_setcursor(sc)
    833 	struct cgfourteen_softc *sc;
    834 {
    835 	/* we need to subtract the hot-spot value here */
    836 #define COORD(f) (sc->sc_cursor.cc_pos.f - sc->sc_cursor.cc_hot.f)
    837 
    838 	sc->sc_hwc->curs_ctl = (sc->sc_cursor.cc_enable ? CG14_CURS_ENABLE : 0);
    839 	sc->sc_hwc->curs_x = COORD(x);
    840 	sc->sc_hwc->curs_y = COORD(y);
    841 
    842 #undef COORD
    843 }
    844 
    845 static void
    846 cg14_loadcursor(sc)
    847 	struct cgfourteen_softc *sc;
    848 {
    849 	volatile struct cg14curs *hwc;
    850 	u_int edgemask, m;
    851 	int i;
    852 
    853 	/*
    854 	 * Keep the top size.x bits.  Here we *throw out* the top
    855 	 * size.x bits from an all-one-bits word, introducing zeros in
    856 	 * the top size.x bits, then invert all the bits to get what
    857 	 * we really wanted as our mask.  But this fails if size.x is
    858 	 * 32---a sparc uses only the low 5 bits of the shift count---
    859 	 * so we have to special case that.
    860 	 */
    861 	edgemask = ~0;
    862 	if (sc->sc_cursor.cc_size.x < 32)
    863 		edgemask = ~(edgemask >> sc->sc_cursor.cc_size.x);
    864 	hwc = sc->sc_hwc;
    865 	for (i = 0; i < 32; i++) {
    866 		m = sc->sc_cursor.cc_eplane[i] & edgemask;
    867 		hwc->curs_plane0[i] = m;
    868 		hwc->curs_plane1[i] = m & sc->sc_cursor.cc_cplane[i];
    869 	}
    870 }
    871 
    872 static void
    873 cg14_loadomap(sc)
    874 	struct cgfourteen_softc *sc;
    875 {
    876 	/* set background color */
    877 	sc->sc_hwc->curs_color1 = sc->sc_cursor.cc_color.cm_chip[0];
    878 	/* set foreground color */
    879 	sc->sc_hwc->curs_color2 = sc->sc_cursor.cc_color.cm_chip[1];
    880 }
    881