Home | History | Annotate | Line # | Download | only in dev
cgfourteen.c revision 1.38
      1 /*	$NetBSD: cgfourteen.c,v 1.38 2003/11/13 03:09:28 chs 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.38 2003/11/13 03:09:28 chs 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 	u_int eplane[32], cplane[32];
    383 
    384 	switch (cmd) {
    385 
    386 	case FBIOGTYPE:
    387 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    388 		break;
    389 
    390 	case FBIOGATTR:
    391 		fba = (struct fbgattr *)data;
    392 		fba->real_type = FBTYPE_MDICOLOR;
    393 		fba->owner = 0;		/* XXX ??? */
    394 		fba->fbtype = sc->sc_fb.fb_type;
    395 		fba->sattr.flags = 0;
    396 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    397 		fba->sattr.dev_specific[0] = -1;
    398 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    399 		fba->emu_types[1] = -1;
    400 		break;
    401 
    402 	case FBIOGETCMAP:
    403 		return (cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
    404 				     CG14_CLUT_SIZE));
    405 
    406 	case FBIOPUTCMAP:
    407 		/* copy to software map */
    408 #define p ((struct fbcmap *)data)
    409 #ifdef CG14_CG8
    410 		p->index &= 0xffffff;
    411 #endif
    412 		error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
    413 		if (error)
    414 			return (error);
    415 		/* now blast them into the chip */
    416 		/* XXX should use retrace interrupt */
    417 		cg14_load_hwcmap(sc, p->index, p->count);
    418 #undef p
    419 		break;
    420 
    421 	case FBIOGVIDEO:
    422 		*(int *)data = cg14_get_video(sc);
    423 		break;
    424 
    425 	case FBIOSVIDEO:
    426 		cg14_set_video(sc, *(int *)data);
    427 		break;
    428 
    429 /* these are for both FBIOSCURSOR and FBIOGCURSOR */
    430 #define p ((struct fbcursor *)data)
    431 #define cc (&sc->sc_cursor)
    432 	case FBIOGCURSOR:
    433 		/* do not quite want everything here... */
    434 		p->set = FB_CUR_SETALL;	/* close enough, anyway */
    435 		p->enable = cc->cc_enable;
    436 		p->pos = cc->cc_pos;
    437 		p->hot = cc->cc_hot;
    438 		p->size = cc->cc_size;
    439 
    440 		/* begin ugh ... can we lose some of this crap?? */
    441 		if (p->image != NULL) {
    442 			count = cc->cc_size.y * 32 / NBBY;
    443 			error = copyout(cc->cc_cplane, p->image, count);
    444 			if (error)
    445 				return (error);
    446 			error = copyout(cc->cc_eplane, p->mask, count);
    447 			if (error)
    448 				return (error);
    449 		}
    450 		if (p->cmap.red != NULL) {
    451 			error = cg14_get_cmap(&p->cmap,
    452 			    (union cg14cmap *)&cc->cc_color, 2);
    453 			if (error)
    454 				return (error);
    455 		} else {
    456 			p->cmap.index = 0;
    457 			p->cmap.count = 2;
    458 		}
    459 		/* end ugh */
    460 		break;
    461 
    462 	case FBIOSCURSOR:
    463 		/*
    464 		 * For setcmap and setshape, verify parameters, so that
    465 		 * we do not get halfway through an update and then crap
    466 		 * out with the software state screwed up.
    467 		 */
    468 		v = p->set;
    469 		if (v & FB_CUR_SETCMAP) {
    470 			/*
    471 			 * This use of a temporary copy of the cursor
    472 			 * colormap is not terribly efficient, but these
    473 			 * copies are small (8 bytes)...
    474 			 */
    475 			tcm = cc->cc_color;
    476 			error = cg14_put_cmap(&p->cmap, (union cg14cmap *)&tcm,
    477 					      2);
    478 			if (error)
    479 				return (error);
    480 		}
    481 		if (v & FB_CUR_SETSHAPE) {
    482 			if ((u_int)p->size.x > 32 || (u_int)p->size.y > 32)
    483 				return (EINVAL);
    484 			count = p->size.y * 32 / NBBY;
    485 			error = copyin(p->mask, eplane, count);
    486 			if (error)
    487 				return error;
    488 			error = copyin(p->image, cplane, count);
    489 			if (error)
    490 				return error;
    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 			memset(cc->cc_eplane, 0, sizeof cc->cc_eplane);
    511 			memcpy(cc->cc_eplane, eplane, count);
    512 			memset(cc->cc_cplane, 0, sizeof cc->cc_cplane);
    513 			memcpy(cc->cc_cplane, 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 paddr_t
    569 cgfourteenmmap(dev, off, prot)
    570 	dev_t dev;
    571 	off_t off;
    572 	int prot;
    573 {
    574 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
    575 
    576 	if (off & PGOFSET)
    577 		panic("cgfourteenmmap");
    578 
    579 	if (off < 0)
    580 		return (-1);
    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 		if (bus_space_mmap(sc->sc_bustag,
    590 			BUS_ADDR(sc->sc_physadr[CG14_CTL_IDX].sbr_slot,
    591 				sc->sc_physadr[CG14_CTL_IDX].sbr_offset),
    592 			off, prot, BUS_SPACE_MAP_LINEAR));
    593 	}
    594 #endif
    595 
    596 	if ((u_int)off >= NOOVERLAY)
    597 		off -= NOOVERLAY;
    598 	else if ((u_int)off >= START)
    599 		off -= START;
    600 	else
    601 		off = 0;
    602 
    603 	/*
    604 	 * fb_size includes the overlay space only for the CG8.
    605 	 */
    606 #ifdef CG14_CG8
    607 	if (off >= sc->sc_fb.fb_type.fb_size - START)
    608 #else
    609 	if (off >= sc->sc_fb.fb_type.fb_size)
    610 #endif
    611 	{
    612 #ifdef DEBUG
    613 		printf("\nmmap request out of bounds: request 0x%x, "
    614 		    "bound 0x%x\n", (unsigned) off,
    615 		    (unsigned)sc->sc_fb.fb_type.fb_size);
    616 #endif
    617 		return (-1);
    618 	}
    619 
    620 	return (bus_space_mmap(sc->sc_bustag,
    621 		BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].oa_space,
    622 			sc->sc_physadr[CG14_PXL_IDX].oa_base),
    623 		off, prot, BUS_SPACE_MAP_LINEAR));
    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 	memset(sc->sc_xlut, 0, 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 	int error;
    753 
    754 	start = p->index;
    755 	count = p->count;
    756 	if (start >= cmsize || count > cmsize - start)
    757 		return (EINVAL);
    758 
    759 	for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    760 		error = copyout(&cp[3], &p->red[i], 1);
    761 		if (error)
    762 			return error;
    763 		error = copyout(&cp[2], &p->green[i], 1);
    764 		if (error)
    765 			return error;
    766 		error = copyout(&cp[1], &p->blue[i], 1);
    767 		if (error)
    768 			return error;
    769 	}
    770 	return (0);
    771 }
    772 
    773 /* Write the software shadow colormap */
    774 static int
    775 cg14_put_cmap(p, cm, cmsize)
    776 	struct fbcmap *p;
    777 	union cg14cmap *cm;
    778 	int cmsize;
    779 {
    780 	u_int i, start, count;
    781 	u_char *cp;
    782 	u_char cmap[256][4];
    783 	int error;
    784 
    785 	start = p->index;
    786 	count = p->count;
    787 	if (start >= cmsize || count > cmsize - start)
    788 		return (EINVAL);
    789 
    790 	memcpy(&cmap, &cm->cm_map, sizeof cmap);
    791 	for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
    792 		error = copyin(&p->red[i], &cp[3], 1);
    793 		if (error)
    794 			return error;
    795 		error = copyin(&p->green[i], &cp[2], 1);
    796 		if (error)
    797 			return error;
    798 		error = copyin(&p->blue[i], &cp[1], 1);
    799 		if (error)
    800 			return error;
    801 		cp[0] = 0;	/* no alpha channel */
    802 	}
    803 	memcpy(&cm->cm_map, &cmap, sizeof cmap);
    804 	return (0);
    805 }
    806 
    807 static void
    808 cg14_load_hwcmap(sc, start, ncolors)
    809 	struct cgfourteen_softc *sc;
    810 	int start, ncolors;
    811 {
    812 	/* XXX switch to auto-increment, and on retrace intr */
    813 
    814 	/* Setup pointers to source and dest */
    815 	u_int32_t *colp = &sc->sc_cmap.cm_chip[start];
    816 	volatile u_int32_t *lutp = &sc->sc_clut1->clut_lut[start];
    817 
    818 	/* Copy by words */
    819 	while (--ncolors >= 0)
    820 		*lutp++ = *colp++;
    821 }
    822 
    823 /*
    824  * Load the cursor (overlay `foreground' and `background') colors.
    825  */
    826 static void
    827 cg14_setcursor(sc)
    828 	struct cgfourteen_softc *sc;
    829 {
    830 	/* we need to subtract the hot-spot value here */
    831 #define COORD(f) (sc->sc_cursor.cc_pos.f - sc->sc_cursor.cc_hot.f)
    832 
    833 	sc->sc_hwc->curs_ctl = (sc->sc_cursor.cc_enable ? CG14_CURS_ENABLE : 0);
    834 	sc->sc_hwc->curs_x = COORD(x);
    835 	sc->sc_hwc->curs_y = COORD(y);
    836 
    837 #undef COORD
    838 }
    839 
    840 static void
    841 cg14_loadcursor(sc)
    842 	struct cgfourteen_softc *sc;
    843 {
    844 	volatile struct cg14curs *hwc;
    845 	u_int edgemask, m;
    846 	int i;
    847 
    848 	/*
    849 	 * Keep the top size.x bits.  Here we *throw out* the top
    850 	 * size.x bits from an all-one-bits word, introducing zeros in
    851 	 * the top size.x bits, then invert all the bits to get what
    852 	 * we really wanted as our mask.  But this fails if size.x is
    853 	 * 32---a sparc uses only the low 5 bits of the shift count---
    854 	 * so we have to special case that.
    855 	 */
    856 	edgemask = ~0;
    857 	if (sc->sc_cursor.cc_size.x < 32)
    858 		edgemask = ~(edgemask >> sc->sc_cursor.cc_size.x);
    859 	hwc = sc->sc_hwc;
    860 	for (i = 0; i < 32; i++) {
    861 		m = sc->sc_cursor.cc_eplane[i] & edgemask;
    862 		hwc->curs_plane0[i] = m;
    863 		hwc->curs_plane1[i] = m & sc->sc_cursor.cc_cplane[i];
    864 	}
    865 }
    866 
    867 static void
    868 cg14_loadomap(sc)
    869 	struct cgfourteen_softc *sc;
    870 {
    871 	/* set background color */
    872 	sc->sc_hwc->curs_color1 = sc->sc_cursor.cc_color.cm_chip[0];
    873 	/* set foreground color */
    874 	sc->sc_hwc->curs_color2 = sc->sc_cursor.cc_color.cm_chip[1];
    875 }
    876