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