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