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