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