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