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