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