xref: /netbsd-src/sys/arch/sparc/dev/cgfourteen.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: cgfourteen.c,v 1.52 2007/10/17 19:57:11 garbled 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 #define CG14_CG8
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/buf.h>
81 #include <sys/device.h>
82 #include <sys/ioctl.h>
83 #include <sys/malloc.h>
84 #include <sys/mman.h>
85 #include <sys/tty.h>
86 #include <sys/conf.h>
87 
88 #include <uvm/uvm_extern.h>
89 
90 #include <dev/sun/fbio.h>
91 #include <machine/autoconf.h>
92 #include <machine/pmap.h>
93 #include <dev/sun/fbvar.h>
94 #include <machine/cpu.h>
95 #include <dev/sbus/sbusvar.h>
96 
97 #include "wsdisplay.h"
98 #include <dev/wscons/wsconsio.h>
99 #include <dev/wsfont/wsfont.h>
100 #include <dev/rasops/rasops.h>
101 
102 #include <dev/wscons/wsdisplay_vconsvar.h>
103 
104 #include <sparc/dev/cgfourteenreg.h>
105 #include <sparc/dev/cgfourteenvar.h>
106 
107 #include "opt_wsemul.h"
108 
109 /* autoconfiguration driver */
110 static int	cgfourteenmatch(struct device *, struct cfdata *, void *);
111 static void	cgfourteenattach(struct device *, struct device *, void *);
112 static void	cgfourteenunblank(struct device *);
113 
114 CFATTACH_DECL(cgfourteen, sizeof(struct cgfourteen_softc),
115     cgfourteenmatch, cgfourteenattach, NULL, NULL);
116 
117 extern struct cfdriver cgfourteen_cd;
118 
119 dev_type_open(cgfourteenopen);
120 dev_type_close(cgfourteenclose);
121 dev_type_ioctl(cgfourteenioctl);
122 dev_type_mmap(cgfourteenmmap);
123 dev_type_poll(cgfourteenpoll);
124 
125 const struct cdevsw cgfourteen_cdevsw = {
126         cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
127         nostop, notty, cgfourteenpoll, cgfourteenmmap, nokqfilter,
128 };
129 
130 /* frame buffer generic driver */
131 static struct fbdriver cgfourteenfbdriver = {
132 	cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
133 	cgfourteenpoll, cgfourteenmmap, nokqfilter
134 };
135 
136 extern struct tty *fbconstty;
137 
138 static void cg14_set_video(struct cgfourteen_softc *, int);
139 static int  cg14_get_video(struct cgfourteen_softc *);
140 static int  cg14_get_cmap(struct fbcmap *, union cg14cmap *, int);
141 static int  cg14_put_cmap(struct fbcmap *, union cg14cmap *, int);
142 static void cg14_load_hwcmap(struct cgfourteen_softc *, int, int);
143 static void cg14_init(struct cgfourteen_softc *);
144 static void cg14_reset(struct cgfourteen_softc *);
145 
146 #if NWSDISPLAY > 0
147 static void cg14_setup_wsdisplay(struct cgfourteen_softc *, int);
148 static void cg14_init_cmap(struct cgfourteen_softc *);
149 static int  cg14_putcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
150 static int  cg14_getcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
151 static void cg14_set_depth(struct cgfourteen_softc *, int);
152 static void cg14_move_cursor(struct cgfourteen_softc *, int, int);
153 static int  cg14_do_cursor(struct cgfourteen_softc *,
154                            struct wsdisplay_cursor *);
155 #endif
156 
157 #if defined(RASTERCONSOLE) && (NWSDISPLAY > 0)
158 #error You can't have it both ways - either RASTERCONSOLE or wsdisplay
159 #endif
160 
161 /*
162  * Match a cgfourteen.
163  */
164 int
165 cgfourteenmatch(struct device *parent, struct cfdata *cf, void *aux)
166 {
167 	union obio_attach_args *uoba = aux;
168 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
169 
170 	/*
171 	 * The cgfourteen is a local-bus video adaptor, accessed directly
172 	 * via the processor, and not through device space or an external
173 	 * bus. Thus we look _only_ at the obio bus.
174 	 * Additionally, these things exist only on the Sun4m.
175 	 */
176 
177 	if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
178 		return (0);
179 
180 	/* Check driver name */
181 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
182 }
183 
184 /*
185  * Set COLOUR_OFFSET to the offset of the video RAM.  This is to provide
186  *  space for faked overlay junk for the cg8 emulation.
187  *
188  * As it happens, this value is correct for both cg3 and cg8 emulation!
189  */
190 #define COLOUR_OFFSET (256*1024)
191 
192 #ifdef RASTERCONSOLE
193 static void cg14_set_rcons_luts(struct cgfourteen_softc *sc)
194 {
195 	int i;
196 
197 	for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_xlut->xlut_lut[i] = 0x22;
198 	for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_clut2->clut_lut[i] = 0x00ffffff;
199 	sc->sc_clut2->clut_lut[0] = 0x00ffffff;
200 	sc->sc_clut2->clut_lut[255] = 0;
201 }
202 #endif /* RASTERCONSOLE */
203 
204 #if NWSDISPLAY > 0
205 static int	cg14_ioctl(void *, void *, u_long, void *, int, struct lwp *);
206 static paddr_t	cg14_mmap(void *, void *, off_t, int);
207 static void	cg14_init_screen(void *, struct vcons_screen *, int, long *);
208 
209 
210 struct wsdisplay_accessops cg14_accessops = {
211 	cg14_ioctl,
212 	cg14_mmap,
213 	NULL,	/* alloc_screen */
214 	NULL,	/* free_screen */
215 	NULL,	/* show_screen */
216 	NULL, 	/* load_font */
217 	NULL,	/* pollc */
218 	NULL	/* scroll */
219 };
220 #endif
221 
222 /*
223  * Attach a display.  We need to notice if it is the console, too.
224  */
225 void
226 cgfourteenattach(struct device *parent, struct device *self, void *aux)
227 {
228 	union obio_attach_args *uoba = aux;
229 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
230 	struct cgfourteen_softc *sc = (struct cgfourteen_softc *)self;
231 	struct fbdevice *fb = &sc->sc_fb;
232 	bus_space_handle_t bh;
233 	int node, ramsize;
234 	volatile uint32_t *lut;
235 	int i, isconsole;
236 
237 	node = sa->sa_node;
238 
239 	/* Remember cookies for cgfourteenmmap() */
240 	sc->sc_bustag = sa->sa_bustag;
241 
242 	fb->fb_driver = &cgfourteenfbdriver;
243 	fb->fb_device = &sc->sc_dev;
244 	/* Mask out invalid flags from the user. */
245 	fb->fb_flags = device_cfdata(&sc->sc_dev)->cf_flags & FB_USERMASK;
246 
247 	/*
248 	 * We're emulating a cg3/8, so represent ourselves as one
249 	 */
250 #ifdef CG14_CG8
251 	fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
252 	fb->fb_type.fb_depth = 32;
253 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
254 	ramsize = roundup(fb->fb_type.fb_height * 1152 * 4, NBPG);
255 #else
256 	fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
257 	fb->fb_type.fb_depth = 8;
258 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
259 	ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, NBPG);
260 #endif
261 	fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
262 	fb->fb_type.fb_size = ramsize + COLOUR_OFFSET;
263 
264 	if (sa->sa_nreg < 2) {
265 		printf("%s: only %d register sets\n",
266 			self->dv_xname, sa->sa_nreg);
267 		return;
268 	}
269 	bcopy(sa->sa_reg, sc->sc_physadr,
270 	      sa->sa_nreg * sizeof(struct sbus_reg));
271 
272 	sc->sc_vramsize = sc->sc_physadr[CG14_PXL_IDX].sbr_size;
273 
274 	printf(": %d MB VRAM", (uint32_t)(sc->sc_vramsize >> 20));
275 	/*
276 	 * Now map in the 8 useful pages of registers
277 	 */
278 	if (sa->sa_size < 0x10000) {
279 #ifdef DIAGNOSTIC
280 		printf("warning: can't find all cgfourteen registers...\n");
281 #endif
282 		sa->sa_size = 0x10000;
283 	}
284 	if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
285 			 sa->sa_offset,
286 			 sa->sa_size,
287 			 BUS_SPACE_MAP_LINEAR,
288 			 &bh) != 0) {
289 		printf("%s: cannot map control registers\n", self->dv_xname);
290 		return;
291 	}
292 	sc->sc_regh = bh;
293 
294 	sc->sc_ctl   = (struct cg14ctl  *) (bh);
295 	sc->sc_hwc   = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
296 	sc->sc_dac   = (struct cg14dac  *) (bh + CG14_OFFSET_DAC);
297 	sc->sc_xlut  = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
298 	sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
299 	sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
300 	sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
301 	sc->sc_clutincr =        (u_int *) (bh + CG14_OFFSET_CLUTINCR);
302 
303 	/*
304 	 * Let the user know that we're here
305 	 */
306 #ifdef CG14_CG8
307 	printf(": cgeight emulated at %dx%dx24bpp",
308 		fb->fb_type.fb_width, fb->fb_type.fb_height);
309 #else
310 	printf(": cgthree emulated at %dx%dx8bpp",
311 		fb->fb_type.fb_width, fb->fb_type.fb_height);
312 #endif
313 	/*
314 	 * Enable the video.
315 	 */
316 	cg14_set_video(sc, 1);
317 
318 	/*
319 	 * Grab the initial colormap
320 	 */
321 	lut = sc->sc_clut1->clut_lut;
322 	for (i = 0; i < CG14_CLUT_SIZE; i++)
323 		sc->sc_cmap.cm_chip[i] = lut[i];
324 
325 	/* See if we're the console */
326         isconsole = fb_is_console(node);
327 
328 #if defined(RASTERCONSOLE)
329 	if (isconsole) {
330 		printf(" (console)\n");
331 		/* *sbus*_bus_map?  but that's how we map the regs... */
332 		if (sbus_bus_map( sc->sc_bustag,
333 				  sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
334 				  sc->sc_physadr[CG14_PXL_IDX].sbr_offset +
335 				    0x03800000,
336 				  1152 * 900, BUS_SPACE_MAP_LINEAR,
337 				  &bh) != 0) {
338 			printf("%s: cannot map pixels\n",&sc->sc_dev.dv_xname[0]);
339 			return;
340 		}
341 		sc->sc_rcfb = sc->sc_fb;
342 		sc->sc_rcfb.fb_type.fb_type = FBTYPE_SUN3COLOR;
343 		sc->sc_rcfb.fb_type.fb_depth = 8;
344 		sc->sc_rcfb.fb_linebytes = 1152;
345 		sc->sc_rcfb.fb_type.fb_size = roundup(1152*900,NBPG);
346 		sc->sc_rcfb.fb_pixels = (void *)bh;
347 
348 		printf("vram at %p\n",(void *)bh);
349 		/* XXX should use actual screen size */
350 
351 		for (i = 0; i < 1152 * 900; i++)
352 		    ((unsigned char *)bh)[i] = 0;
353 		fbrcons_init(&sc->sc_rcfb);
354 		cg14_set_rcons_luts(sc);
355 		sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID |
356 		    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL;
357 	} else
358 		printf("\n");
359 #endif
360 
361 #if NWSDISPLAY > 0
362 	if (sbus_bus_map( sc->sc_bustag,
363 	    sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
364 	    sc->sc_physadr[CG14_PXL_IDX].sbr_offset,
365 	    ramsize, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
366 		printf("%s: cannot map pixels\n",&sc->sc_dev.dv_xname[0]);
367 		return;
368 	}
369 
370 	sc->sc_fb.fb_pixels = bus_space_vaddr(sc->sc_bustag, bh);
371 	if (isconsole)
372 		printf(" (console)\n");
373 	else
374 		printf("\n");
375 
376 	sc->sc_depth = 8;
377 	cg14_setup_wsdisplay(sc, isconsole);
378 #endif
379 
380 	/* Attach to /dev/fb */
381 	fb_attach(&sc->sc_fb, isconsole);
382 }
383 
384 /*
385  * Keep track of the number of opens made. In the 24-bit driver, we need to
386  * switch to 24-bit mode on the first open, and switch back to 8-bit on
387  * the last close. This kind of nonsense is needed to give screenblank
388  * a fighting chance of working.
389  */
390 static int cg14_opens = 0;
391 
392 int
393 cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
394 {
395 	struct cgfourteen_softc *sc;
396 	int unit;
397 	int s, oldopens;
398 
399 	unit = minor(dev);
400 	if (unit >= cgfourteen_cd.cd_ndevs)
401 		return(ENXIO);
402 	sc = cgfourteen_cd.cd_devs[minor(dev)];
403 	if (sc == NULL)
404 		return(ENXIO);
405 	s = splhigh();
406 	oldopens = cg14_opens++;
407 	splx(s);
408 
409 	/* Setup the cg14 as we want it, and save the original PROM state */
410 	if (oldopens == 0)	/* first open only, to make screenblank work */
411 		cg14_init(sc);
412 
413 	return (0);
414 }
415 
416 int
417 cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
418 {
419 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
420 	int s, opens;
421 
422 	s = splhigh();
423 	opens = --cg14_opens;
424 	if (cg14_opens < 0)
425 		opens = cg14_opens = 0;
426 	splx(s);
427 
428 	/*
429 	 * Restore video state to make the PROM happy, on last close.
430 	 */
431 	if (opens == 0)
432 		cg14_reset(sc);
433 
434 	return (0);
435 }
436 
437 int
438 cgfourteenioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
439 {
440 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
441 	struct fbgattr *fba;
442 	int error;
443 
444 	switch (cmd) {
445 
446 	case FBIOGTYPE:
447 		*(struct fbtype *)data = sc->sc_fb.fb_type;
448 		break;
449 
450 	case FBIOGATTR:
451 		fba = (struct fbgattr *)data;
452 		fba->real_type = FBTYPE_MDICOLOR;
453 		fba->owner = 0;		/* XXX ??? */
454 		fba->fbtype = sc->sc_fb.fb_type;
455 		fba->sattr.flags = 0;
456 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
457 		fba->sattr.dev_specific[0] = -1;
458 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
459 		fba->emu_types[1] = -1;
460 		break;
461 
462 	case FBIOGETCMAP:
463 		return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
464 				     CG14_CLUT_SIZE));
465 
466 	case FBIOPUTCMAP:
467 		/* copy to software map */
468 #define p ((struct fbcmap *)data)
469 #ifdef CG14_CG8
470 		p->index &= 0xffffff;
471 #endif
472 		error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
473 		if (error)
474 			return (error);
475 		/* now blast them into the chip */
476 		/* XXX should use retrace interrupt */
477 		cg14_load_hwcmap(sc, p->index, p->count);
478 #undef p
479 		break;
480 
481 	case FBIOGVIDEO:
482 		*(int *)data = cg14_get_video(sc);
483 		break;
484 
485 	case FBIOSVIDEO:
486 		cg14_set_video(sc, *(int *)data);
487 		break;
488 
489 	default:
490 		return (ENOTTY);
491 	}
492 	return (0);
493 }
494 
495 /*
496  * Undo the effect of an FBIOSVIDEO that turns the video off.
497  */
498 static void
499 cgfourteenunblank(struct device *dev)
500 {
501 	struct cgfourteen_softc *sc = (struct cgfourteen_softc *)dev;
502 
503 	cg14_set_video(sc, 1);
504 #if NWSDISPLAY > 0
505 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
506 		cg14_set_depth(sc, 8);
507 		cg14_init_cmap(sc);
508 		vcons_redraw_screen(sc->sc_vd.active);
509 		sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
510 	}
511 #endif
512 }
513 
514 /*
515  * Return the address that would map the given device at the given
516  * offset, allowing for the given protection, or return -1 for error.
517  *
518  * Since we're pretending to be a cg8, we put the main video RAM at the
519  *  same place the cg8 does, at offset 256k.  The cg8 has an enable
520  *  plane in the 256k space; our "enable" plane works differently.  We
521  *  can't emulate the enable plane very well, but all X uses it for is
522  *  to clear it at startup - so we map the first page of video RAM over
523  *  and over to fill that 256k space.  We also map some other views of
524  *  the video RAM space.
525  *
526  * Our memory map thus looks like
527  *
528  *	mmap range		space	base offset
529  *	00000000-00040000	vram	0 (multi-mapped - see above)
530  *	00040000-00434800	vram	00000000
531  *	01000000-01400000	vram	01000000
532  *	02000000-02200000	vram	02000000
533  *	02800000-02a00000	vram	02800000
534  *	03000000-03100000	vram	03000000
535  *	03400000-03500000	vram	03400000
536  *	03800000-03900000	vram	03800000
537  *	03c00000-03d00000	vram	03c00000
538  *	10000000-10010000	regs	00000000 (only if CG14_MAP_REGS)
539  */
540 paddr_t
541 cgfourteenmmap(dev_t dev, off_t off, int prot)
542 {
543 	struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
544 
545 	if (off & PGOFSET)
546 		panic("cgfourteenmmap");
547 
548 	if (off < 0)
549 		return (-1);
550 
551 #if defined(CG14_MAP_REGS) /* XXX: security hole */
552 	/*
553 	 * Map the control registers into user space. Should only be
554 	 * used for debugging!
555 	 */
556 	if ((u_int)off >= 0x10000000 && (u_int)off < 0x10000000 + 16*4096) {
557 		off -= 0x10000000;
558 		return (bus_space_mmap(sc->sc_bustag,
559 			BUS_ADDR(sc->sc_physadr[CG14_CTL_IDX].sbr_slot,
560 				   sc->sc_physadr[CG14_CTL_IDX].sbr_offset),
561 			off, prot, BUS_SPACE_MAP_LINEAR));
562 	}
563 #endif
564 
565 	if (off < COLOUR_OFFSET)
566 		off = 0;
567 	else if (off < COLOUR_OFFSET+(1152*900*4))
568 		off -= COLOUR_OFFSET;
569 	else {
570 		switch (off >> 20) {
571 			case 0x010: case 0x011: case 0x012: case 0x013:
572 			case 0x020: case 0x021:
573 			case 0x028: case 0x029:
574 			case 0x030:
575 			case 0x034:
576 			case 0x038:
577 			case 0x03c:
578 				break;
579 			default:
580 				return(-1);
581 		}
582 	}
583 
584 	return (bus_space_mmap(sc->sc_bustag,
585 		BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
586 			   sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
587 		off, prot, BUS_SPACE_MAP_LINEAR));
588 }
589 
590 int
591 cgfourteenpoll(dev_t dev, int events, struct lwp *l)
592 {
593 
594 	return (seltrue(dev, events, l));
595 }
596 
597 /*
598  * Miscellaneous helper functions
599  */
600 
601 /* Initialize the framebuffer, storing away useful state for later reset */
602 static void
603 cg14_init(struct cgfourteen_softc *sc)
604 {
605 	volatile uint32_t *clut;
606 	volatile uint8_t  *xlut;
607 	int i;
608 
609 	/*
610 	 * We stash away the following to restore on close:
611 	 *
612 	 * 	color look-up table 1 	(sc->sc_saveclut)
613 	 *	x look-up table		(sc->sc_savexlut)
614 	 *	control register	(sc->sc_savectl)
615 	 *	cursor control register (sc->sc_savehwc)
616 	 */
617 	sc->sc_savectl = sc->sc_ctl->ctl_mctl;
618 	sc->sc_savehwc = sc->sc_hwc->curs_ctl;
619 
620 	clut = (volatile uint32_t *) sc->sc_clut1->clut_lut;
621 	xlut = (volatile uint8_t *) sc->sc_xlut->xlut_lut;
622 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
623 		sc->sc_saveclut.cm_chip[i] = clut[i];
624 		sc->sc_savexlut[i] = xlut[i];
625 	}
626 
627 #ifdef CG14_CG8
628 	/*
629 	 * Enable the video, and put in 24 bit mode.
630 	 */
631 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 |
632 		CG14_MCTL_POWERCTL;
633 
634 	/*
635 	 * Zero the xlut to enable direct-color mode
636 	 */
637 	for (i = 0; i < CG14_CLUT_SIZE; i++)
638 		sc->sc_xlut->xlut_lut[i] = 0;
639 #else
640 	/*
641 	 * Enable the video and put it in 8 bit mode
642 	 */
643 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
644 		CG14_MCTL_POWERCTL;
645 #endif
646 }
647 
648 static void
649 /* Restore the state saved on cg14_init */
650 cg14_reset(struct cgfourteen_softc *sc)
651 {
652 	volatile uint32_t *clut;
653 	volatile uint8_t  *xlut;
654 	int i;
655 
656 	/*
657 	 * We restore the following, saved in cg14_init:
658 	 *
659 	 * 	color look-up table 1 	(sc->sc_saveclut)
660 	 *	x look-up table		(sc->sc_savexlut)
661 	 *	control register	(sc->sc_savectl)
662 	 *	cursor control register (sc->sc_savehwc)
663 	 *
664 	 * Note that we don't touch the video enable bits in the
665 	 * control register; otherwise, screenblank wouldn't work.
666 	 */
667 	sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
668 							CG14_MCTL_POWERCTL)) |
669 				(sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
670 						    CG14_MCTL_POWERCTL));
671 	sc->sc_hwc->curs_ctl = sc->sc_savehwc;
672 
673 	clut = sc->sc_clut1->clut_lut;
674 	xlut = sc->sc_xlut->xlut_lut;
675 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
676 		clut[i] = sc->sc_saveclut.cm_chip[i];
677 		xlut[i] = sc->sc_savexlut[i];
678 	}
679 }
680 
681 /* Enable/disable video display; power down monitor if DPMS-capable */
682 static void
683 cg14_set_video(struct cgfourteen_softc *sc, int enable)
684 {
685 	/*
686 	 * We can only use DPMS to power down the display if the chip revision
687 	 * is greater than 0.
688 	 */
689 	if (enable) {
690 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
691 			sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
692 						 CG14_MCTL_POWERCTL);
693 		else
694 			sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
695 	} else {
696 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
697 			sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
698 						  CG14_MCTL_POWERCTL);
699 		else
700 			sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
701 	}
702 }
703 
704 /* Get status of video display */
705 static int
706 cg14_get_video(struct cgfourteen_softc *sc)
707 {
708 	return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
709 }
710 
711 /* Read the software shadow colormap */
712 static int
713 cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
714 {
715         u_int i, start, count;
716         u_char *cp;
717         int error;
718 
719         start = p->index;
720         count = p->count;
721         if (start >= cmsize || count > cmsize - start)
722                 return (EINVAL);
723 
724         for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
725                 error = copyout(&cp[3], &p->red[i], 1);
726                 if (error)
727                         return error;
728                 error = copyout(&cp[2], &p->green[i], 1);
729                 if (error)
730                         return error;
731                 error = copyout(&cp[1], &p->blue[i], 1);
732                 if (error)
733                         return error;
734         }
735         return (0);
736 }
737 
738 /* Write the software shadow colormap */
739 static int
740 cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
741 {
742         u_int i, start, count;
743         u_char *cp;
744         u_char cmap[256][4];
745         int error;
746 
747         start = p->index;
748         count = p->count;
749         if (start >= cmsize || count > cmsize - start)
750                 return (EINVAL);
751 
752         memcpy(&cmap, &cm->cm_map, sizeof cmap);
753         for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
754                 error = copyin(&p->red[i], &cp[3], 1);
755                 if (error)
756                         return error;
757                 error = copyin(&p->green[i], &cp[2], 1);
758                 if (error)
759                         return error;
760                 error = copyin(&p->blue[i], &cp[1], 1);
761                 if (error)
762                         return error;
763                 cp[0] = 0;      /* no alpha channel */
764         }
765         memcpy(&cm->cm_map, &cmap, sizeof cmap);
766         return (0);
767 }
768 
769 static void
770 cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
771 {
772 	/* XXX switch to auto-increment, and on retrace intr */
773 
774 	/* Setup pointers to source and dest */
775 	uint32_t *colp = &sc->sc_cmap.cm_chip[start];
776 	volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
777 
778 	/* Copy by words */
779 	while (--ncolors >= 0)
780 		*lutp++ = *colp++;
781 }
782 
783 #if NWSDISPLAY > 0
784 static void
785 cg14_setup_wsdisplay(struct cgfourteen_softc *sc, int is_cons)
786 {
787 	struct wsemuldisplaydev_attach_args aa;
788 	struct rasops_info *ri;
789 	long defattr;
790 
791  	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
792 		"default",
793 		0, 0,
794 		NULL,
795 		8, 16,
796 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
797 		NULL
798 	};
799 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
800 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
801 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
802 	vcons_init(&sc->sc_vd, sc, &sc->sc_defaultscreen_descr,
803 	    &cg14_accessops);
804 	sc->sc_vd.init_screen = cg14_init_screen;
805 
806 	ri = &sc->sc_console_screen.scr_ri;
807 
808 	if (is_cons) {
809 		vcons_init_screen(&sc->sc_vd, &sc->sc_console_screen, 1,
810 		    &defattr);
811 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
812 
813 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
814 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
815 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
816 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
817 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
818 		    defattr);
819 	} else {
820 		/*
821 		 * since we're not the console we can postpone the rest
822 		 * until someone actually allocates a screen for us
823 		 */
824 	}
825 
826 	cg14_init_cmap(sc);
827 
828 	aa.console = is_cons;
829 	aa.scrdata = &sc->sc_screenlist;
830 	aa.accessops = &cg14_accessops;
831 	aa.accesscookie = &sc->sc_vd;
832 
833 	config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
834 }
835 
836 static void
837 cg14_init_cmap(struct cgfourteen_softc *sc)
838 {
839 	int i, j = 0;
840 
841 	for (i = 0; i < 256; i++) {
842 
843 		sc->sc_cmap.cm_map[i][3] = rasops_cmap[j];
844 		sc->sc_cmap.cm_map[i][2] = rasops_cmap[j + 1];
845 		sc->sc_cmap.cm_map[i][1] = rasops_cmap[j + 2];
846 		j += 3;
847 	}
848 	cg14_load_hwcmap(sc, 0, 256);
849 }
850 
851 static int
852 cg14_putcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
853 {
854 	u_int index = cm->index;
855 	u_int count = cm->count;
856 	int i, error;
857 	u_char rbuf[256], gbuf[256], bbuf[256];
858 
859 	if (cm->index >= 256 || cm->count > 256 ||
860 	    (cm->index + cm->count) > 256)
861 		return EINVAL;
862 	error = copyin(cm->red, &rbuf[index], count);
863 	if (error)
864 		return error;
865 	error = copyin(cm->green, &gbuf[index], count);
866 	if (error)
867 		return error;
868 	error = copyin(cm->blue, &bbuf[index], count);
869 	if (error)
870 		return error;
871 
872 	for (i = 0; i < count; i++) {
873 		sc->sc_cmap.cm_map[index][3] = rbuf[index];
874 		sc->sc_cmap.cm_map[index][2] = gbuf[index];
875 		sc->sc_cmap.cm_map[index][1] = bbuf[index];
876 
877 		index++;
878 	}
879 	cg14_load_hwcmap(sc, 0, 256);
880 	return 0;
881 }
882 
883 static int
884 cg14_getcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
885 {
886 	uint8_t rbuf[256], gbuf[256], bbuf[256];
887 	u_int index = cm->index;
888 	u_int count = cm->count;
889 	int error, i;
890 
891 	if (index >= 255 || count > 256 || index + count > 256)
892 		return EINVAL;
893 
894 
895 	for (i = 0; i < count; i++) {
896 		rbuf[i] = sc->sc_cmap.cm_map[index][3];
897 		gbuf[i] = sc->sc_cmap.cm_map[index][2];
898 		bbuf[i] = sc->sc_cmap.cm_map[index][1];
899 
900 		index++;
901 	}
902 	error = copyout(rbuf,   cm->red,   count);
903 	if (error)
904 		return error;
905 	error = copyout(gbuf, cm->green, count);
906 	if (error)
907 		return error;
908 	error = copyout(bbuf,  cm->blue,  count);
909 	if (error)
910 		return error;
911 
912 	return 0;
913 }
914 static int
915 cg14_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
916 	struct lwp *l)
917 {
918 	struct vcons_data *vd = v;
919 	struct cgfourteen_softc *sc = vd->cookie;
920 	struct wsdisplay_fbinfo *wdf;
921 	struct vcons_screen *ms = vd->active;
922 
923 	switch (cmd) {
924 
925 		case WSDISPLAYIO_GTYPE:
926 			*(uint32_t *)data = WSDISPLAY_TYPE_SUNCG14;
927 			return 0;
928 
929 		case WSDISPLAYIO_GINFO:
930 			wdf = (void *)data;
931 			wdf->height = ms->scr_ri.ri_height;
932 			wdf->width = ms->scr_ri.ri_width;
933 			wdf->depth = 32;
934 			wdf->cmsize = 256;
935 			return 0;
936 
937 		case WSDISPLAYIO_GETCMAP:
938 			return cg14_getcmap(sc,
939 			    (struct wsdisplay_cmap *)data);
940 
941 		case WSDISPLAYIO_PUTCMAP:
942 			return cg14_putcmap(sc,
943 			    (struct wsdisplay_cmap *)data);
944 
945 		case WSDISPLAYIO_LINEBYTES:
946 			*(u_int *)data = ms->scr_ri.ri_width << 2;
947 			return 0;
948 
949 		case WSDISPLAYIO_SMODE:
950 			{
951 				int new_mode = *(int*)data;
952 				if (new_mode != sc->sc_mode) {
953 					sc->sc_mode = new_mode;
954 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
955 						bus_space_write_1(sc->sc_bustag,
956 						    sc->sc_regh,
957 						    CG14_CURSOR_CONTROL, 0);
958 						cg14_set_depth(sc, 8);
959 						cg14_init_cmap(sc);
960 						vcons_redraw_screen(ms);
961 					} else
962 						cg14_set_depth(sc, 32);
963 				}
964 			}
965 			return 0;
966 		case WSDISPLAYIO_SVIDEO:
967 			cg14_set_video(sc, *(int *)data);
968 			return 0;
969 		case WSDISPLAYIO_GVIDEO:
970 			return cg14_get_video(sc) ?
971 			    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
972 		case WSDISPLAYIO_GCURPOS:
973 			{
974 				struct wsdisplay_curpos *cp = (void *)data;
975 
976 				cp->x = sc->sc_cursor.cc_pos.x;
977 				cp->y = sc->sc_cursor.cc_pos.y;
978 			}
979 			return 0;
980 		case WSDISPLAYIO_SCURPOS:
981 			{
982 				struct wsdisplay_curpos *cp = (void *)data;
983 
984 				cg14_move_cursor(sc, cp->x, cp->y);
985 			}
986 			return 0;
987 		case WSDISPLAYIO_GCURMAX:
988 			{
989 				struct wsdisplay_curpos *cp = (void *)data;
990 
991 				cp->x = 32;
992 				cp->y = 32;
993 			}
994 			return 0;
995 		case WSDISPLAYIO_SCURSOR:
996 			{
997 				struct wsdisplay_cursor *cursor = (void *)data;
998 
999 				return cg14_do_cursor(sc, cursor);
1000 			}
1001 	}
1002 	return EPASSTHROUGH;
1003 }
1004 
1005 static paddr_t
1006 cg14_mmap(void *v, void *vs, off_t offset, int prot)
1007 {
1008 	struct vcons_data *vd = v;
1009 	struct cgfourteen_softc *sc = vd->cookie;
1010 
1011 	/* allow mmap()ing the full framebuffer, not just what we use */
1012 	if (offset < sc->sc_vramsize)
1013 		return bus_space_mmap(sc->sc_bustag,
1014 		    BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
1015 		      sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
1016 		    offset + CG14_FB_CBGR, prot, BUS_SPACE_MAP_LINEAR);
1017 
1018 	return -1;
1019 }
1020 
1021 static void
1022 cg14_init_screen(void *cookie, struct vcons_screen *scr,
1023     int existing, long *defattr)
1024 {
1025 	struct cgfourteen_softc *sc = cookie;
1026 	struct rasops_info *ri = &scr->scr_ri;
1027 
1028 	ri->ri_depth = 8;
1029 	ri->ri_width = sc->sc_fb.fb_type.fb_width;
1030 	ri->ri_height = sc->sc_fb.fb_type.fb_height;
1031 	ri->ri_stride = ri->ri_width;
1032 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
1033 
1034 	ri->ri_bits = (char *)sc->sc_fb.fb_pixels;
1035 
1036 	if (existing) {
1037 		ri->ri_flg |= RI_CLEAR;
1038 	}
1039 
1040 	rasops_init(ri, sc->sc_fb.fb_type.fb_height / 8,
1041 	     sc->sc_fb.fb_type.fb_width / 8);
1042 	ri->ri_caps = WSSCREEN_WSCOLORS;
1043 
1044 	rasops_reconfig(ri,
1045 	    sc->sc_fb.fb_type.fb_height / ri->ri_font->fontheight,
1046 	    sc->sc_fb.fb_type.fb_width / ri->ri_font->fontwidth);
1047 
1048 	ri->ri_hw = scr;
1049 }
1050 
1051 static void
1052 cg14_set_depth(struct cgfourteen_softc *sc, int depth)
1053 {
1054 	int i;
1055 
1056 	if (sc->sc_depth == depth)
1057 		return;
1058 	switch (depth) {
1059 		case 8:
1060 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
1061 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
1062 			    CG14_MCTL_PIXMODE_8 | CG14_MCTL_POWERCTL);
1063 			sc->sc_depth = 8;
1064 			/* everything is CLUT1 */
1065 			for (i = 0; i < CG14_CLUT_SIZE; i++)
1066 			     sc->sc_xlut->xlut_lut[i] = 0;
1067 			break;
1068 		case 32:
1069 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
1070 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
1071 			    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL);
1072 			sc->sc_depth = 32;
1073 			for (i = 0; i < CG14_CLUT_SIZE; i++)
1074 			     sc->sc_xlut->xlut_lut[i] = 0;
1075 			break;
1076 		default:
1077 			printf("%s: can't change to depth %d\n",
1078 			    sc->sc_dev.dv_xname, depth);
1079 	}
1080 }
1081 
1082 static void
1083 cg14_move_cursor(struct cgfourteen_softc *sc, int x, int y)
1084 {
1085 	uint32_t pos;
1086 
1087 	sc->sc_cursor.cc_pos.x = x;
1088 	sc->sc_cursor.cc_pos.y = y;
1089 	pos = ((sc->sc_cursor.cc_pos.x - sc->sc_cursor.cc_hot.x ) << 16) |
1090 	      ((sc->sc_cursor.cc_pos.y - sc->sc_cursor.cc_hot.y ) & 0xffff);
1091 	bus_space_write_4(sc->sc_bustag, sc->sc_regh, CG14_CURSOR_X, pos);
1092 }
1093 
1094 static int
1095 cg14_do_cursor(struct cgfourteen_softc *sc, struct wsdisplay_cursor *cur)
1096 {
1097 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
1098 
1099 		bus_space_write_1(sc->sc_bustag, sc->sc_regh,
1100 		    CG14_CURSOR_CONTROL, cur->enable ? CG14_CRSR_ENABLE : 0);
1101 	}
1102 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
1103 
1104 		sc->sc_cursor.cc_hot.x = cur->hot.x;
1105 		sc->sc_cursor.cc_hot.y = cur->hot.y;
1106 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
1107 	}
1108 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
1109 
1110 		cg14_move_cursor(sc, cur->pos.x, cur->pos.y);
1111 	}
1112 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
1113 		int i;
1114 		uint32_t val;
1115 
1116 		for (i = 0; i < cur->cmap.count; i++) {
1117 			val = (cur->cmap.red[i] ) |
1118 			      (cur->cmap.green[i] << 8) |
1119 			      (cur->cmap.blue[i] << 16);
1120 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
1121 			    CG14_CURSOR_COLOR1 + ((i + cur->cmap.index) << 2),
1122 			    val);
1123 		}
1124 	}
1125 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
1126 		uint32_t buffer[32], latch, tmp;
1127 		int i;
1128 
1129 		memcpy(buffer, cur->mask, 128);
1130 		for (i = 0; i < 32; i++) {
1131 			latch = 0;
1132 			tmp = buffer[i] & 0x80808080;
1133 			latch |= tmp >> 7;
1134 			tmp = buffer[i] & 0x40404040;
1135 			latch |= tmp >> 5;
1136 			tmp = buffer[i] & 0x20202020;
1137 			latch |= tmp >> 3;
1138 			tmp = buffer[i] & 0x10101010;
1139 			latch |= tmp >> 1;
1140 			tmp = buffer[i] & 0x08080808;
1141 			latch |= tmp << 1;
1142 			tmp = buffer[i] & 0x04040404;
1143 			latch |= tmp << 3;
1144 			tmp = buffer[i] & 0x02020202;
1145 			latch |= tmp << 5;
1146 			tmp = buffer[i] & 0x01010101;
1147 			latch |= tmp << 7;
1148 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
1149 			    CG14_CURSOR_PLANE0 + (i << 2), latch);
1150 		}
1151 		memcpy(buffer, cur->image, 128);
1152 		for (i = 0; i < 32; i++) {
1153 			latch = 0;
1154 			tmp = buffer[i] & 0x80808080;
1155 			latch |= tmp >> 7;
1156 			tmp = buffer[i] & 0x40404040;
1157 			latch |= tmp >> 5;
1158 			tmp = buffer[i] & 0x20202020;
1159 			latch |= tmp >> 3;
1160 			tmp = buffer[i] & 0x10101010;
1161 			latch |= tmp >> 1;
1162 			tmp = buffer[i] & 0x08080808;
1163 			latch |= tmp << 1;
1164 			tmp = buffer[i] & 0x04040404;
1165 			latch |= tmp << 3;
1166 			tmp = buffer[i] & 0x02020202;
1167 			latch |= tmp << 5;
1168 			tmp = buffer[i] & 0x01010101;
1169 			latch |= tmp << 7;
1170 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
1171 			    CG14_CURSOR_PLANE1 + (i << 2), latch);
1172 		}
1173 	}
1174 	return 0;
1175 }
1176 #endif
1177