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