xref: /netbsd-src/sys/dev/sun/cgsix.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	$NetBSD: cgsix.c,v 1.60 2012/11/13 20:47:58 macallan Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * This software was developed by the Computer Systems Engineering group
37  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
38  * contributed to Berkeley.
39  *
40  * All advertising materials mentioning features or use of this software
41  * must display the following acknowledgement:
42  *	This product includes software developed by the University of
43  *	California, Lawrence Berkeley Laboratory.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  *	@(#)cgsix.c	8.4 (Berkeley) 1/21/94
70  */
71 
72 /*
73  * color display (cgsix) driver.
74  *
75  * Does not handle interrupts, even though they can occur.
76  *
77  * XXX should defer colormap updates to vertical retrace interrupts
78  */
79 
80 #include <sys/cdefs.h>
81 __KERNEL_RCSID(0, "$NetBSD: cgsix.c,v 1.60 2012/11/13 20:47:58 macallan Exp $");
82 
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/buf.h>
86 #include <sys/device.h>
87 #include <sys/ioctl.h>
88 #include <sys/malloc.h>
89 #include <sys/mman.h>
90 #include <sys/tty.h>
91 #include <sys/conf.h>
92 
93 #ifdef DEBUG
94 #include <sys/proc.h>
95 #include <sys/syslog.h>
96 #endif
97 
98 #include <sys/bus.h>
99 
100 #include <dev/sun/fbio.h>
101 #include <dev/sun/fbvar.h>
102 
103 #include <dev/sun/btreg.h>
104 #include <dev/sun/btvar.h>
105 #include <dev/sun/pfourreg.h>
106 
107 #include <dev/wscons/wsconsio.h>
108 #include <dev/wsfont/wsfont.h>
109 #include <dev/rasops/rasops.h>
110 
111 #include "opt_wsemul.h"
112 #include "rasops_glue.h"
113 
114 #include <dev/sun/cgsixreg.h>
115 #include <dev/sun/cgsixvar.h>
116 
117 #include "ioconf.h"
118 
119 static void	cg6_unblank(device_t);
120 static void	cg6_blank(struct cgsix_softc *, int);
121 
122 dev_type_open(cgsixopen);
123 dev_type_close(cgsixclose);
124 dev_type_ioctl(cgsixioctl);
125 dev_type_mmap(cgsixmmap);
126 
127 const struct cdevsw cgsix_cdevsw = {
128 	cgsixopen, cgsixclose, noread, nowrite, cgsixioctl,
129 	nostop, notty, nopoll, cgsixmmap, nokqfilter, D_OTHER
130 };
131 
132 /* frame buffer generic driver */
133 static struct fbdriver cg6_fbdriver = {
134 	cg6_unblank, cgsixopen, cgsixclose, cgsixioctl, nopoll, cgsixmmap,
135 	nokqfilter
136 };
137 
138 static void cg6_reset (struct cgsix_softc *);
139 static void cg6_loadcmap (struct cgsix_softc *, int, int);
140 static void cg6_loadomap (struct cgsix_softc *);
141 static void cg6_setcursor (struct cgsix_softc *);/* set position */
142 static void cg6_loadcursor (struct cgsix_softc *);/* set shape */
143 
144 #if NWSDISPLAY > 0
145 #ifdef RASTERCONSOLE
146 #error RASTERCONSOLE and wsdisplay are mutually exclusive
147 #endif
148 
149 static void cg6_setup_palette(struct cgsix_softc *);
150 
151 struct wsscreen_descr cgsix_defaultscreen = {
152 	"std",
153 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
154 	NULL,		/* textops */
155 	8, 16,	/* font width/height */
156 	WSSCREEN_WSCOLORS,	/* capabilities */
157 	NULL	/* modecookie */
158 };
159 
160 static int 	cgsix_ioctl(void *, void *, u_long, void *, int, struct lwp *);
161 static paddr_t	cgsix_mmap(void *, void *, off_t, int);
162 static void	cgsix_init_screen(void *, struct vcons_screen *, int, long *);
163 
164 static void	cgsix_clearscreen(struct cgsix_softc *);
165 
166 void 	cgsix_setup_mono(struct cgsix_softc *, int, int, int, int, uint32_t,
167 		uint32_t);
168 void 	cgsix_feed_line(struct cgsix_softc *, int, uint8_t *);
169 void 	cgsix_rectfill(struct cgsix_softc *, int, int, int, int, uint32_t);
170 void	cgsix_bitblt(void *, int, int, int, int, int, int, int);
171 
172 int	cgsix_putcmap(struct cgsix_softc *, struct wsdisplay_cmap *);
173 int	cgsix_getcmap(struct cgsix_softc *, struct wsdisplay_cmap *);
174 void	cgsix_putchar(void *, int, int, u_int, long);
175 void	cgsix_putchar_aa(void *, int, int, u_int, long);
176 void	cgsix_cursor(void *, int, int, int);
177 
178 struct wsdisplay_accessops cgsix_accessops = {
179 	cgsix_ioctl,
180 	cgsix_mmap,
181 	NULL,	/* alloc_screen */
182 	NULL,	/* free_screen */
183 	NULL,	/* show_screen */
184 	NULL, 	/* load_font */
185 	NULL,	/* pollc */
186 	NULL	/* scroll */
187 };
188 
189 const struct wsscreen_descr *_cgsix_scrlist[] = {
190 	&cgsix_defaultscreen
191 };
192 
193 struct wsscreen_list cgsix_screenlist = {
194 	sizeof(_cgsix_scrlist) / sizeof(struct wsscreen_descr *),
195 	_cgsix_scrlist
196 };
197 
198 
199 extern const u_char rasops_cmap[768];
200 
201 #endif /* NWSDISPLAY > 0 */
202 
203 #if (NWSDISPLAY > 0) || defined(RASTERCONSOLE)
204 void	cg6_invert(struct cgsix_softc *, int, int, int, int);
205 
206 /* need this for both cases because ri_hw points to it */
207 static struct vcons_screen cg6_console_screen;
208 #endif
209 
210 #ifdef RASTERCONSOLE
211 int cgsix_use_rasterconsole = 1;
212 #endif
213 
214 /*
215  * cg6 accelerated console routines.
216  *
217  * Note that buried in this code in several places is the assumption
218  * that pixels are exactly one byte wide.  Since this is cg6-specific
219  * code, this seems safe.  This assumption resides in things like the
220  * use of ri_emuwidth without messing around with ri_pelbytes, or the
221  * assumption that ri_font->fontwidth is the right thing to multiply
222  * character-cell counts by to get byte counts.
223  */
224 
225 /*
226  * Magic values for blitter
227  */
228 
229 /* Values for the mode register */
230 #define CG6_MODE	(						\
231 	  0x00200000 /* GX_BLIT_SRC */					\
232 	| 0x00020000 /* GX_MODE_COLOR8 */				\
233 	| 0x00008000 /* GX_DRAW_RENDER */				\
234 	| 0x00002000 /* GX_BWRITE0_ENABLE */				\
235 	| 0x00001000 /* GX_BWRITE1_DISABLE */				\
236 	| 0x00000200 /* GX_BREAD_0 */					\
237 	| 0x00000080 /* GX_BDISP_0 */					\
238 )
239 #define CG6_MODE_MASK	(						\
240 	  0x00300000 /* GX_BLIT_ALL */					\
241 	| 0x00060000 /* GX_MODE_ALL */					\
242 	| 0x00018000 /* GX_DRAW_ALL */					\
243 	| 0x00006000 /* GX_BWRITE0_ALL */				\
244 	| 0x00001800 /* GX_BWRITE1_ALL */				\
245 	| 0x00000600 /* GX_BREAD_ALL */					\
246 	| 0x00000180 /* GX_BDISP_ALL */					\
247 )
248 
249 /* Value for the alu register for screen-to-screen copies */
250 #define CG6_ALU_COPY	(						\
251 	  0x80000000 /* GX_PLANE_ONES (ignore planemask register) */	\
252 	| 0x20000000 /* GX_PIXEL_ONES (ignore pixelmask register) */	\
253 	| 0x00800000 /* GX_ATTR_SUPP (function unknown) */		\
254 	| 0x00000000 /* GX_RAST_BOOL (function unknown) */		\
255 	| 0x00000000 /* GX_PLOT_PLOT (function unknown) */		\
256 	| 0x08000000 /* GX_PATTERN_ONES (ignore pattern) */		\
257 	| 0x01000000 /* GX_POLYG_OVERLAP (unsure - handle overlap?) */	\
258 	| 0x0000cccc /* ALU = src */					\
259 )
260 
261 /* Value for the alu register for region fills */
262 #define CG6_ALU_FILL	(						\
263 	  0x80000000 /* GX_PLANE_ONES (ignore planemask register) */	\
264 	| 0x20000000 /* GX_PIXEL_ONES (ignore pixelmask register) */	\
265 	| 0x00800000 /* GX_ATTR_SUPP (function unknown) */		\
266 	| 0x00000000 /* GX_RAST_BOOL (function unknown) */		\
267 	| 0x00000000 /* GX_PLOT_PLOT (function unknown) */		\
268 	| 0x08000000 /* GX_PATTERN_ONES (ignore pattern) */		\
269 	| 0x01000000 /* GX_POLYG_OVERLAP (unsure - handle overlap?) */	\
270 	| 0x0000ff00 /* ALU = fg color */				\
271 )
272 
273 /* Value for the alu register for toggling an area */
274 #define CG6_ALU_FLIP	(						\
275 	  0x80000000 /* GX_PLANE_ONES (ignore planemask register) */	\
276 	| 0x20000000 /* GX_PIXEL_ONES (ignore pixelmask register) */	\
277 	| 0x00800000 /* GX_ATTR_SUPP (function unknown) */		\
278 	| 0x00000000 /* GX_RAST_BOOL (function unknown) */		\
279 	| 0x00000000 /* GX_PLOT_PLOT (function unknown) */		\
280 	| 0x08000000 /* GX_PATTERN_ONES (ignore pattern) */		\
281 	| 0x01000000 /* GX_POLYG_OVERLAP (unsure - handle overlap?) */	\
282 	| 0x00005555 /* ALU = ~dst */					\
283 )
284 
285 /*
286  * Run a blitter command
287  */
288 #define CG6_BLIT(f) { volatile uint32_t foo; foo = f->fbc_blit; }
289 
290 /*
291  * Run a drawing command
292  */
293 #define CG6_DRAW(f) { volatile uint32_t foo; foo = f->fbc_draw; }
294 
295 /*
296  * Wait for the whole engine to go idle.  This may not matter in our case;
297  * I'm not sure whether blits are actually queued or not.  It more likely
298  * is intended for lines and such that do get queued.
299  * 0x10000000 bit: GX_INPROGRESS
300  */
301 #define CG6_DRAIN(fbc) do {						\
302 	while ((fbc)->fbc_s & GX_INPROGRESS)				\
303 		/*EMPTY*/;						\
304 } while (0)
305 
306 /*
307  * something is missing here
308  * Waiting for GX_FULL to clear should be enough to send another command
309  * but some CG6 ( LX onboard for example ) lock up if we do that while
310  * it works fine on others ( a 4MB TGX+ I've got here )
311  * So, until I figure out what's going on we wait for the blitter to go
312  * fully idle.
313  */
314 #define CG6_WAIT_READY(fbc) do {					\
315        	while (((fbc)->fbc_s & GX_INPROGRESS/*GX_FULL*/) != 0)		\
316 		/*EMPTY*/;						\
317 } while (0)
318 
319 #if (NWSDISPLAY > 0) || defined(RASTERCONSOLE)
320 static void cg6_ras_init(struct cgsix_softc *);
321 static void cg6_ras_copyrows(void *, int, int, int);
322 static void cg6_ras_copycols(void *, int, int, int, int);
323 static void cg6_ras_erasecols(void *, int, int, int, long int);
324 static void cg6_ras_eraserows(void *, int, int, long int);
325 #if defined(RASTERCONSOLE) && defined(CG6_BLIT_CURSOR)
326 static void cg6_ras_do_cursor(struct rasops_info *);
327 #endif
328 
329 static void
330 cg6_ras_init(struct cgsix_softc *sc)
331 {
332 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
333 
334 	CG6_DRAIN(fbc);
335 	fbc->fbc_mode &= ~CG6_MODE_MASK;
336 	fbc->fbc_mode |= CG6_MODE;
337 
338 	/* set some common drawing engine parameters */
339 	fbc->fbc_clip = 0;
340 	fbc->fbc_s = 0;
341 	fbc->fbc_offx = 0;
342 	fbc->fbc_offy = 0;
343 	fbc->fbc_clipminx = 0;
344 	fbc->fbc_clipminy = 0;
345 	fbc->fbc_clipmaxx = 0x3fff;
346 	fbc->fbc_clipmaxy = 0x3fff;
347 }
348 
349 static void
350 cg6_ras_copyrows(void *cookie, int src, int dst, int n)
351 {
352 	struct rasops_info *ri = cookie;
353 	struct vcons_screen *scr = ri->ri_hw;
354 	struct cgsix_softc *sc = scr->scr_cookie;
355 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
356 
357 	if (dst == src)
358 		return;
359 	if (src < 0) {
360 		n += src;
361 		src = 0;
362 	}
363 	if (src+n > ri->ri_rows)
364 		n = ri->ri_rows - src;
365 	if (dst < 0) {
366 		n += dst;
367 		dst = 0;
368 	}
369 	if (dst+n > ri->ri_rows)
370 		n = ri->ri_rows - dst;
371 	if (n <= 0)
372 		return;
373 	n *= ri->ri_font->fontheight;
374 	src *= ri->ri_font->fontheight;
375 	dst *= ri->ri_font->fontheight;
376 
377 	CG6_WAIT_READY(fbc);
378 
379 	fbc->fbc_alu = CG6_ALU_COPY;
380 	fbc->fbc_mode = GX_BLIT_SRC | GX_MODE_COLOR8;
381 
382 	fbc->fbc_x0 = ri->ri_xorigin;
383 	fbc->fbc_y0 = ri->ri_yorigin + src;
384 	fbc->fbc_x1 = ri->ri_xorigin + ri->ri_emuwidth - 1;
385 	fbc->fbc_y1 = ri->ri_yorigin + src + n - 1;
386 	fbc->fbc_x2 = ri->ri_xorigin;
387 	fbc->fbc_y2 = ri->ri_yorigin + dst;
388 	fbc->fbc_x3 = ri->ri_xorigin + ri->ri_emuwidth - 1;
389 	fbc->fbc_y3 = ri->ri_yorigin + dst + n - 1;
390 	CG6_BLIT(fbc);
391 }
392 
393 static void
394 cg6_ras_copycols(void *cookie, int row, int src, int dst, int n)
395 {
396 	struct rasops_info *ri = cookie;
397 	struct vcons_screen *scr = ri->ri_hw;
398 	struct cgsix_softc *sc = scr->scr_cookie;
399 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
400 
401 	if (dst == src)
402 		return;
403 	if ((row < 0) || (row >= ri->ri_rows))
404 		return;
405 	if (src < 0) {
406 		n += src;
407 		src = 0;
408 	}
409 	if (src+n > ri->ri_cols)
410 		n = ri->ri_cols - src;
411 	if (dst < 0) {
412 		n += dst;
413 		dst = 0;
414 	}
415 	if (dst+n > ri->ri_cols)
416 		n = ri->ri_cols - dst;
417 	if (n <= 0)
418 		return;
419 	n *= ri->ri_font->fontwidth;
420 	src *= ri->ri_font->fontwidth;
421 	dst *= ri->ri_font->fontwidth;
422 	row *= ri->ri_font->fontheight;
423 
424 	CG6_WAIT_READY(fbc);
425 
426 	fbc->fbc_alu = CG6_ALU_COPY;
427 	fbc->fbc_mode = GX_BLIT_SRC | GX_MODE_COLOR8;
428 
429 	fbc->fbc_x0 = ri->ri_xorigin + src;
430 	fbc->fbc_y0 = ri->ri_yorigin + row;
431 	fbc->fbc_x1 = ri->ri_xorigin + src + n - 1;
432 	fbc->fbc_y1 = ri->ri_yorigin + row +
433 	    ri->ri_font->fontheight - 1;
434 	fbc->fbc_x2 = ri->ri_xorigin + dst;
435 	fbc->fbc_y2 = ri->ri_yorigin + row;
436 	fbc->fbc_x3 = ri->ri_xorigin + dst + n - 1;
437 	fbc->fbc_y3 = ri->ri_yorigin + row +
438 	    ri->ri_font->fontheight - 1;
439 	CG6_BLIT(fbc);
440 }
441 
442 static void
443 cg6_ras_erasecols(void *cookie, int row, int col, int n, long int attr)
444 {
445 	struct rasops_info *ri = cookie;
446 	struct vcons_screen *scr = ri->ri_hw;
447 	struct cgsix_softc *sc = scr->scr_cookie;
448 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
449 
450 	if ((row < 0) || (row >= ri->ri_rows))
451 		return;
452 	if (col < 0) {
453 		n += col;
454 		col = 0;
455 	}
456 	if (col+n > ri->ri_cols)
457 		n = ri->ri_cols - col;
458 	if (n <= 0)
459 		return;
460 	n *= ri->ri_font->fontwidth;
461 	col *= ri->ri_font->fontwidth;
462 	row *= ri->ri_font->fontheight;
463 
464 	CG6_WAIT_READY(fbc);
465 	fbc->fbc_alu = CG6_ALU_FILL;
466 	fbc->fbc_mode = GX_BLIT_SRC | GX_MODE_COLOR8;
467 
468 	fbc->fbc_fg = ri->ri_devcmap[(attr >> 16) & 0xff];
469 	fbc->fbc_arecty = ri->ri_yorigin + row;
470 	fbc->fbc_arectx = ri->ri_xorigin + col;
471 	fbc->fbc_arecty = ri->ri_yorigin + row +
472 	    ri->ri_font->fontheight - 1;
473 	fbc->fbc_arectx = ri->ri_xorigin + col + n - 1;
474 	CG6_DRAW(fbc);
475 }
476 
477 static void
478 cg6_ras_eraserows(void *cookie, int row, int n, long int attr)
479 {
480 	struct rasops_info *ri = cookie;
481 	struct vcons_screen *scr = ri->ri_hw;
482 	struct cgsix_softc *sc = scr->scr_cookie;
483 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
484 
485 	if (row < 0) {
486 		n += row;
487 		row = 0;
488 	}
489 	if (row+n > ri->ri_rows)
490 		n = ri->ri_rows - row;
491 	if (n <= 0)
492 		return;
493 
494 	CG6_WAIT_READY(fbc);
495 	fbc->fbc_alu = CG6_ALU_FILL;
496 	fbc->fbc_mode = GX_BLIT_SRC | GX_MODE_COLOR8;
497 
498 	fbc->fbc_fg = ri->ri_devcmap[(attr >> 16) & 0xff];
499 	if ((n == ri->ri_rows) && (ri->ri_flg & RI_FULLCLEAR)) {
500 		fbc->fbc_arecty = 0;
501 		fbc->fbc_arectx = 0;
502 		fbc->fbc_arecty = ri->ri_height - 1;
503 		fbc->fbc_arectx = ri->ri_width - 1;
504 	} else {
505 		row *= ri->ri_font->fontheight;
506 		fbc->fbc_arecty = ri->ri_yorigin + row;
507 		fbc->fbc_arectx = ri->ri_xorigin;
508 		fbc->fbc_arecty = ri->ri_yorigin + row +
509 		    (n * ri->ri_font->fontheight) - 1;
510 		fbc->fbc_arectx = ri->ri_xorigin + ri->ri_emuwidth - 1;
511 	}
512 	CG6_DRAW(fbc);
513 }
514 
515 #if defined(RASTERCONSOLE) && defined(CG6_BLIT_CURSOR)
516 /*
517  * Really want something more like fg^bg here, but that would be more
518  * or less impossible to migrate to colors.  So we hope there's
519  * something not too inappropriate in the colormap...besides, it's what
520  * the non-accelerated code did. :-)
521  */
522 static void
523 cg6_ras_do_cursor(struct rasops_info *ri)
524 {
525 	struct vcons_screen *scr = ri->ri_hw;
526 	struct cgsix_softc *sc = scr->cookie;
527 	int row, col;
528 
529 	row = ri->ri_crow * ri->ri_font->fontheight;
530 	col = ri->ri_ccol * ri->ri_font->fontwidth;
531 	cg6_invert(sc, ri->ri_xorigin + col,ri->ri_yorigin +
532 	    row, ri->ri_font->fontwidth, ri->ri_font->fontheight);
533 }
534 #endif	/* RASTERCONSOLE */
535 
536 #endif	/* (NWSDISPLAY > 0) || defined(RASTERCONSOLE) */
537 
538 void
539 cg6attach(struct cgsix_softc *sc, const char *name, int isconsole)
540 {
541 	struct fbdevice *fb = &sc->sc_fb;
542 #if NWSDISPLAY > 0
543 	struct wsemuldisplaydev_attach_args aa;
544 	struct rasops_info *ri = &cg6_console_screen.scr_ri;
545 	unsigned long defattr;
546 #endif
547 
548 	fb->fb_driver = &cg6_fbdriver;
549 
550 	/* Don't have to map the pfour register on the cgsix. */
551 	fb->fb_pfour = NULL;
552 
553 	fb->fb_type.fb_cmsize = 256;
554 	fb->fb_type.fb_size = sc->sc_ramsize;
555 
556 	printf(": %s, %d x %d", name,
557 	       fb->fb_type.fb_width, fb->fb_type.fb_height);
558 	if(sc->sc_fhc) {
559 		sc->sc_fhcrev = (*sc->sc_fhc >> FHC_REV_SHIFT) &
560 			(FHC_REV_MASK >> FHC_REV_SHIFT);
561 	} else
562 		sc->sc_fhcrev=-1;
563 	printf(", rev %d", sc->sc_fhcrev);
564 
565 	/* reset cursor & frame buffer controls */
566 	cg6_reset(sc);
567 
568 	/* enable video */
569 	sc->sc_thc->thc_misc |= THC_MISC_VIDEN;
570 
571 	if (isconsole) {
572 		printf(" (console)");
573 
574 /* this is the old console attachment stuff - sparc still needs it */
575 #ifdef RASTERCONSOLE
576 		if (cgsix_use_rasterconsole) {
577 			fbrcons_init(&sc->sc_fb);
578 			/*
579 			 * we don't use the screen struct but keep it here to
580 			 * avoid ugliness in the cg6_ras_* functions
581 			 */
582 			cg6_console_screen.scr_cookie = sc;
583 			sc->sc_fb.fb_rinfo.ri_hw = &cg6_console_screen;
584 			sc->sc_fb.fb_rinfo.ri_ops.copyrows = cg6_ras_copyrows;
585 			sc->sc_fb.fb_rinfo.ri_ops.copycols = cg6_ras_copycols;
586 			sc->sc_fb.fb_rinfo.ri_ops.erasecols = cg6_ras_erasecols;
587 			sc->sc_fb.fb_rinfo.ri_ops.eraserows = cg6_ras_eraserows;
588 #ifdef CG6_BLIT_CURSOR
589 			sc->sc_fb.fb_rinfo.ri_do_cursor = cg6_ras_do_cursor;
590 #endif
591 			cg6_ras_init(sc);
592 		}
593 #endif
594 	}
595 	printf("\n");
596 
597 	fb_attach(&sc->sc_fb, isconsole);
598 	sc->sc_width = fb->fb_type.fb_width;
599 	sc->sc_stride = fb->fb_type.fb_width;
600 	sc->sc_height = fb->fb_type.fb_height;
601 
602 	printf("%s: framebuffer size: %d MB\n", device_xname(sc->sc_dev),
603 	    sc->sc_ramsize >> 20);
604 
605 #if NWSDISPLAY
606 	/* setup rasops and so on for wsdisplay */
607 	memcpy(sc->sc_default_cmap, rasops_cmap, 768);
608 	wsfont_init();
609 	cg6_ras_init(sc);
610 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
611 	sc->sc_bg = WS_DEFAULT_BG;
612 	sc->sc_fb_is_open = FALSE;
613 
614 	vcons_init(&sc->vd, sc, &cgsix_defaultscreen, &cgsix_accessops);
615 	sc->vd.init_screen = cgsix_init_screen;
616 
617 	cg6_setup_palette(sc);
618 	cgsix_clearscreen(sc);
619 
620 	sc->sc_gc.gc_bitblt = cgsix_bitblt;
621 	sc->sc_gc.gc_blitcookie = sc;
622 	sc->sc_gc.gc_rop = CG6_ALU_COPY;
623 
624 	if(isconsole) {
625 		/* we mess with cg6_console_screen only once */
626 		vcons_init_screen(&sc->vd, &cg6_console_screen, 1,
627 		    &defattr);
628 		cg6_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
629 
630 		cgsix_defaultscreen.textops = &ri->ri_ops;
631 		cgsix_defaultscreen.capabilities = ri->ri_caps;
632 		cgsix_defaultscreen.nrows = ri->ri_rows;
633 		cgsix_defaultscreen.ncols = ri->ri_cols;
634 		SCREEN_VISIBLE(&cg6_console_screen);
635 		sc->vd.active = &cg6_console_screen;
636 		wsdisplay_cnattach(&cgsix_defaultscreen, ri, 0, 0, defattr);
637 		if (ri->ri_flg & RI_ENABLE_ALPHA) {
638 			glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
639 				(sc->sc_ramsize / sc->sc_stride) -
640 				  sc->sc_height - 5,
641 				sc->sc_width,
642 				ri->ri_font->fontwidth,
643 				ri->ri_font->fontheight,
644 				defattr);
645 		}
646 		vcons_replay_msgbuf(&cg6_console_screen);
647 	} else {
648 		/*
649 		 * since we're not the console we can postpone the rest
650 		 * until someone actually allocates a screen for us
651 		 */
652 		if (cg6_console_screen.scr_ri.ri_rows == 0) {
653 			/* do some minimal setup to avoid weirdnesses later */
654 			vcons_init_screen(&sc->vd, &cg6_console_screen, 1,
655 			    &defattr);
656 		}
657 		if (ri->ri_flg & RI_ENABLE_ALPHA) {
658 			glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
659 				(sc->sc_ramsize / sc->sc_stride) -
660 				  sc->sc_height - 5,
661 				sc->sc_width,
662 				ri->ri_font->fontwidth,
663 				ri->ri_font->fontheight,
664 				defattr);
665 		}
666 	}
667 	cg6_setup_palette(sc);
668 
669 	aa.scrdata = &cgsix_screenlist;
670 	aa.console = isconsole;
671 	aa.accessops = &cgsix_accessops;
672 	aa.accesscookie = &sc->vd;
673 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
674 #else
675 	bt_initcmap(&sc->sc_cmap, 256);
676 	cg6_loadcmap(sc, 0, 256);
677 
678 #endif
679 }
680 
681 
682 int
683 cgsixopen(dev_t dev, int flags, int mode, struct lwp *l)
684 {
685 	device_t dv = device_lookup(&cgsix_cd, minor(dev));
686 	struct cgsix_softc *sc = device_private(dv);
687 
688 	if (dv == NULL)
689 		return ENXIO;
690 	sc->sc_fb_is_open = TRUE;
691 
692 	return 0;
693 }
694 
695 int
696 cgsixclose(dev_t dev, int flags, int mode, struct lwp *l)
697 {
698 	device_t dv = device_lookup(&cgsix_cd, minor(dev));
699 	struct cgsix_softc *sc = device_private(dv);
700 
701 	cg6_reset(sc);
702 	sc->sc_fb_is_open = FALSE;
703 
704 #if NWSDISPLAY > 0
705 	if (IS_IN_EMUL_MODE(sc)) {
706 		struct vcons_screen *ms = sc->vd.active;
707 
708 		cg6_ras_init(sc);
709 		cg6_setup_palette(sc);
710 		glyphcache_wipe(&sc->sc_gc);
711 
712 		/* we don't know if the screen exists */
713 		if (ms != NULL)
714 			vcons_redraw_screen(ms);
715 	}
716 #else
717 	/* (re-)initialize the default color map */
718 	bt_initcmap(&sc->sc_cmap, 256);
719 
720 	cg6_loadcmap(sc, 0, 256);
721 #endif
722 	return 0;
723 }
724 
725 int
726 cgsixioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
727 {
728 	struct cgsix_softc *sc = device_lookup_private(&cgsix_cd, minor(dev));
729 	union cursor_cmap tcm;
730 	uint32_t image[32], mask[32];
731 	u_int count;
732 	int v, error;
733 
734 #ifdef CGSIX_DEBUG
735 	printf("cgsixioctl(%lx)\n",cmd);
736 #endif
737 
738 	switch (cmd) {
739 
740 	case FBIOGTYPE:
741 		*(struct fbtype *)data = sc->sc_fb.fb_type;
742 		break;
743 
744 	case FBIOGATTR:
745 #define fba ((struct fbgattr *)data)
746 		fba->real_type = sc->sc_fb.fb_type.fb_type;
747 		fba->owner = 0;		/* XXX ??? */
748 		fba->fbtype = sc->sc_fb.fb_type;
749 		fba->sattr.flags = 0;
750 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
751 		fba->sattr.dev_specific[0] = -1;
752 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
753 		fba->emu_types[1] = -1;
754 #undef fba
755 		break;
756 
757 	case FBIOGETCMAP:
758 #define	p ((struct fbcmap *)data)
759 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
760 
761 	case FBIOPUTCMAP:
762 		/* copy to software map */
763 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
764 		if (error)
765 			return error;
766 		/* now blast them into the chip */
767 		/* XXX should use retrace interrupt */
768 		cg6_loadcmap(sc, p->index, p->count);
769 #undef p
770 		break;
771 
772 	case FBIOGVIDEO:
773 		*(int *)data = sc->sc_blanked;
774 		break;
775 
776 	case FBIOSVIDEO:
777 		cg6_blank(sc, !(*(int *)data));
778 		break;
779 
780 /* these are for both FBIOSCURSOR and FBIOGCURSOR */
781 #define p ((struct fbcursor *)data)
782 #define cc (&sc->sc_cursor)
783 
784 	case FBIOGCURSOR:
785 		/* do not quite want everything here... */
786 		p->set = FB_CUR_SETALL;	/* close enough, anyway */
787 		p->enable = cc->cc_enable;
788 		p->pos = cc->cc_pos;
789 		p->hot = cc->cc_hot;
790 		p->size = cc->cc_size;
791 
792 		/* begin ugh ... can we lose some of this crap?? */
793 		if (p->image != NULL) {
794 			count = cc->cc_size.y * 32 / NBBY;
795 			error = copyout(cc->cc_bits[1], p->image, count);
796 			if (error)
797 				return error;
798 			error = copyout(cc->cc_bits[0], p->mask, count);
799 			if (error)
800 				return error;
801 		}
802 		if (p->cmap.red != NULL) {
803 			error = bt_getcmap(&p->cmap,
804 			    (union bt_cmap *)&cc->cc_color, 2, 1);
805 			if (error)
806 				return error;
807 		} else {
808 			p->cmap.index = 0;
809 			p->cmap.count = 2;
810 		}
811 		/* end ugh */
812 		break;
813 
814 	case FBIOSCURSOR:
815 		/*
816 		 * For setcmap and setshape, verify parameters, so that
817 		 * we do not get halfway through an update and then crap
818 		 * out with the software state screwed up.
819 		 */
820 		v = p->set;
821 		if (v & FB_CUR_SETCMAP) {
822 			/*
823 			 * This use of a temporary copy of the cursor
824 			 * colormap is not terribly efficient, but these
825 			 * copies are small (8 bytes)...
826 			 */
827 			tcm = cc->cc_color;
828 			error = bt_putcmap(&p->cmap, (union bt_cmap *)&tcm, 2,
829 			    1);
830 			if (error)
831 				return error;
832 		}
833 		if (v & FB_CUR_SETSHAPE) {
834 			if ((u_int)p->size.x > 32 || (u_int)p->size.y > 32)
835 				return EINVAL;
836 			count = p->size.y * 32 / NBBY;
837 			error = copyin(p->image, image, count);
838 			if (error)
839 				return error;
840 			error = copyin(p->mask, mask, count);
841 			if (error)
842 				return error;
843 		}
844 
845 		/* parameters are OK; do it */
846 		if (v & (FB_CUR_SETCUR | FB_CUR_SETPOS | FB_CUR_SETHOT)) {
847 			if (v & FB_CUR_SETCUR)
848 				cc->cc_enable = p->enable;
849 			if (v & FB_CUR_SETPOS)
850 				cc->cc_pos = p->pos;
851 			if (v & FB_CUR_SETHOT)
852 				cc->cc_hot = p->hot;
853 			cg6_setcursor(sc);
854 		}
855 		if (v & FB_CUR_SETCMAP) {
856 			cc->cc_color = tcm;
857 			cg6_loadomap(sc); /* XXX defer to vertical retrace */
858 		}
859 		if (v & FB_CUR_SETSHAPE) {
860 			cc->cc_size = p->size;
861 			count = p->size.y * 32 / NBBY;
862 			memset(cc->cc_bits, 0, sizeof cc->cc_bits);
863 			memcpy(cc->cc_bits[1], image, count);
864 			memcpy(cc->cc_bits[0], mask, count);
865 			cg6_loadcursor(sc);
866 		}
867 		break;
868 
869 #undef p
870 #undef cc
871 
872 	case FBIOGCURPOS:
873 		*(struct fbcurpos *)data = sc->sc_cursor.cc_pos;
874 		break;
875 
876 	case FBIOSCURPOS:
877 		sc->sc_cursor.cc_pos = *(struct fbcurpos *)data;
878 		cg6_setcursor(sc);
879 		break;
880 
881 	case FBIOGCURMAX:
882 		/* max cursor size is 32x32 */
883 		((struct fbcurpos *)data)->x = 32;
884 		((struct fbcurpos *)data)->y = 32;
885 		break;
886 
887 	default:
888 #ifdef DEBUG
889 		log(LOG_NOTICE, "cgsixioctl(0x%lx) (%s[%d])\n", cmd,
890 		    l->l_proc->p_comm, l->l_proc->p_pid);
891 #endif
892 		return ENOTTY;
893 	}
894 	return 0;
895 }
896 
897 /*
898  * Clean up hardware state (e.g., after bootup or after X crashes).
899  */
900 static void
901 cg6_reset(struct cgsix_softc *sc)
902 {
903 	volatile struct cg6_tec_xxx *tec;
904 	int fhc;
905 	volatile struct bt_regs *bt;
906 
907 	/* hide the cursor, just in case */
908 	sc->sc_thc->thc_cursxy = (THC_CURSOFF << 16) | THC_CURSOFF;
909 
910 	/* turn off frobs in transform engine (makes X11 work) */
911 	tec = sc->sc_tec;
912 	tec->tec_mv = 0;
913 	tec->tec_clip = 0;
914 	tec->tec_vdc = 0;
915 
916 	/* take care of hardware bugs in old revisions */
917 	if (sc->sc_fhcrev < 5) {
918 		/*
919 		 * Keep current resolution; set CPU to 68020, set test
920 		 * window (size 1Kx1K), and for rev 1, disable dest cache.
921 		 */
922 		fhc = (*sc->sc_fhc & FHC_RES_MASK) | FHC_CPU_68020 |
923 		    FHC_TEST |
924 		    (11 << FHC_TESTX_SHIFT) | (11 << FHC_TESTY_SHIFT);
925 		if (sc->sc_fhcrev < 2)
926 			fhc |= FHC_DST_DISABLE;
927 		*sc->sc_fhc = fhc;
928 	}
929 
930 	/* Enable cursor in Brooktree DAC. */
931 	bt = sc->sc_bt;
932 	bt->bt_addr = 0x06 << 24;
933 	bt->bt_ctrl |= 0x03 << 24;
934 }
935 
936 static void
937 cg6_setcursor(struct cgsix_softc *sc)
938 {
939 
940 	/* we need to subtract the hot-spot value here */
941 #define COORD(f) (sc->sc_cursor.cc_pos.f - sc->sc_cursor.cc_hot.f)
942 	sc->sc_thc->thc_cursxy = sc->sc_cursor.cc_enable ?
943 	    ((COORD(x) << 16) | (COORD(y) & 0xffff)) :
944 	    (THC_CURSOFF << 16) | THC_CURSOFF;
945 #undef COORD
946 }
947 
948 static void
949 cg6_loadcursor(struct cgsix_softc *sc)
950 {
951 	volatile struct cg6_thc *thc;
952 	u_int edgemask, m;
953 	int i;
954 
955 	/*
956 	 * Keep the top size.x bits.  Here we *throw out* the top
957 	 * size.x bits from an all-one-bits word, introducing zeros in
958 	 * the top size.x bits, then invert all the bits to get what
959 	 * we really wanted as our mask.  But this fails if size.x is
960 	 * 32---a sparc uses only the low 5 bits of the shift count---
961 	 * so we have to special case that.
962 	 */
963 	edgemask = ~0;
964 	if (sc->sc_cursor.cc_size.x < 32)
965 		edgemask = ~(edgemask >> sc->sc_cursor.cc_size.x);
966 	thc = sc->sc_thc;
967 	for (i = 0; i < 32; i++) {
968 		m = sc->sc_cursor.cc_bits[0][i] & edgemask;
969 		thc->thc_cursmask[i] = m;
970 		thc->thc_cursbits[i] = m & sc->sc_cursor.cc_bits[1][i];
971 	}
972 }
973 
974 /*
975  * Load a subset of the current (new) colormap into the color DAC.
976  */
977 static void
978 cg6_loadcmap(struct cgsix_softc *sc, int start, int ncolors)
979 {
980 	volatile struct bt_regs *bt;
981 	u_int *ip, i;
982 	int count;
983 
984 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
985 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
986 	bt = sc->sc_bt;
987 	bt->bt_addr = BT_D4M4(start) << 24;
988 	while (--count >= 0) {
989 		i = *ip++;
990 		/* hardware that makes one want to pound boards with hammers */
991 		bt->bt_cmap = i;
992 		bt->bt_cmap = i << 8;
993 		bt->bt_cmap = i << 16;
994 		bt->bt_cmap = i << 24;
995 	}
996 }
997 
998 /*
999  * Load the cursor (overlay `foreground' and `background') colors.
1000  */
1001 static void
1002 cg6_loadomap(struct cgsix_softc *sc)
1003 {
1004 	volatile struct bt_regs *bt;
1005 	u_int i;
1006 
1007 	bt = sc->sc_bt;
1008 	bt->bt_addr = 0x01 << 24;	/* set background color */
1009 	i = sc->sc_cursor.cc_color.cm_chip[0];
1010 	bt->bt_omap = i;		/* R */
1011 	bt->bt_omap = i << 8;		/* G */
1012 	bt->bt_omap = i << 16;		/* B */
1013 
1014 	bt->bt_addr = 0x03 << 24;	/* set foreground color */
1015 	bt->bt_omap = i << 24;		/* R */
1016 	i = sc->sc_cursor.cc_color.cm_chip[1];
1017 	bt->bt_omap = i;		/* G */
1018 	bt->bt_omap = i << 8;		/* B */
1019 }
1020 
1021 /* blank or unblank the screen */
1022 static void
1023 cg6_blank(struct cgsix_softc *sc, int flag)
1024 {
1025 
1026 	if (sc->sc_blanked != flag) {
1027 		sc->sc_blanked = flag;
1028 		if (flag) {
1029 			sc->sc_thc->thc_misc &= ~THC_MISC_VIDEN;
1030 		} else {
1031 			sc->sc_thc->thc_misc |= THC_MISC_VIDEN;
1032 		}
1033 	}
1034 }
1035 
1036 /*
1037  * this is called on panic or ddb entry - force the console to the front, reset
1038  * the colour map and enable drawing so we actually see the message even when X
1039  * is running
1040  */
1041 static void
1042 cg6_unblank(device_t dev)
1043 {
1044 	struct cgsix_softc *sc = device_private(dev);
1045 
1046 	cg6_blank(sc, 0);
1047 }
1048 
1049 /* XXX the following should be moved to a "user interface" header */
1050 /*
1051  * Base addresses at which users can mmap() the various pieces of a cg6.
1052  * Note that although the Brooktree color registers do not occupy 8K,
1053  * the X server dies if we do not allow it to map 8K there (it just maps
1054  * from 0x70000000 forwards, as a contiguous chunk).
1055  */
1056 #define	CG6_USER_FBC	0x70000000
1057 #define	CG6_USER_TEC	0x70001000
1058 #define	CG6_USER_BTREGS	0x70002000
1059 #define	CG6_USER_FHC	0x70004000
1060 #define	CG6_USER_THC	0x70005000
1061 #define	CG6_USER_ROM	0x70006000
1062 #define	CG6_USER_RAM	0x70016000
1063 #define	CG6_USER_DHC	0x80000000
1064 
1065 struct mmo {
1066 	u_long	mo_uaddr;	/* user (virtual) address */
1067 	u_long	mo_size;	/* size, or 0 for video ram size */
1068 	u_long	mo_physoff;	/* offset from sc_physadr */
1069 };
1070 
1071 /*
1072  * Return the address that would map the given device at the given
1073  * offset, allowing for the given protection, or return -1 for error.
1074  *
1075  * XXX	needs testing against `demanding' applications (e.g., aviator)
1076  */
1077 paddr_t
1078 cgsixmmap(dev_t dev, off_t off, int prot)
1079 {
1080 	struct cgsix_softc *sc = device_lookup_private(&cgsix_cd, minor(dev));
1081 	struct mmo *mo;
1082 	u_int u, sz;
1083 	static struct mmo mmo[] = {
1084 		{ CG6_USER_RAM, 0, CGSIX_RAM_OFFSET },
1085 
1086 		/* do not actually know how big most of these are! */
1087 		{ CG6_USER_FBC, 1, CGSIX_FBC_OFFSET },
1088 		{ CG6_USER_TEC, 1, CGSIX_TEC_OFFSET },
1089 		{ CG6_USER_BTREGS, 8192 /* XXX */, CGSIX_BT_OFFSET },
1090 		{ CG6_USER_FHC, 1, CGSIX_FHC_OFFSET },
1091 		{ CG6_USER_THC, sizeof(struct cg6_thc), CGSIX_THC_OFFSET },
1092 		{ CG6_USER_ROM, 65536, CGSIX_ROM_OFFSET },
1093 		{ CG6_USER_DHC, 1, CGSIX_DHC_OFFSET },
1094 	};
1095 #define NMMO (sizeof mmo / sizeof *mmo)
1096 
1097 	if (off & PGOFSET)
1098 		panic("cgsixmmap");
1099 
1100 	/*
1101 	 * Entries with size 0 map video RAM (i.e., the size in fb data).
1102 	 *
1103 	 * Since we work in pages, the fact that the map offset table's
1104 	 * sizes are sometimes bizarre (e.g., 1) is effectively ignored:
1105 	 * one byte is as good as one page.
1106 	 */
1107 	for (mo = mmo; mo < &mmo[NMMO]; mo++) {
1108 		if ((u_long)off < mo->mo_uaddr)
1109 			continue;
1110 		u = off - mo->mo_uaddr;
1111 		sz = mo->mo_size ? mo->mo_size :
1112 		    sc->sc_ramsize;
1113 		if (u < sz) {
1114 			return (bus_space_mmap(sc->sc_bustag,
1115 				sc->sc_paddr, u+mo->mo_physoff,
1116 				prot, BUS_SPACE_MAP_LINEAR));
1117 		}
1118 	}
1119 
1120 #ifdef DEBUG
1121 	{
1122 	  struct proc *p = curlwp->l_proc;	/* XXX */
1123 	  log(LOG_NOTICE, "cgsixmmap(0x%llx) (%s[%d])\n",
1124 		(long long)off, p->p_comm, p->p_pid);
1125 	}
1126 #endif
1127 	return -1;	/* not a user-map offset */
1128 }
1129 
1130 #if NWSDISPLAY > 0
1131 
1132 static void
1133 cg6_setup_palette(struct cgsix_softc *sc)
1134 {
1135 	int i, j;
1136 
1137 	rasops_get_cmap(&cg6_console_screen.scr_ri, sc->sc_default_cmap,
1138 	    sizeof(sc->sc_default_cmap));
1139 	j = 0;
1140 	for (i = 0; i < 256; i++) {
1141 		sc->sc_cmap.cm_map[i][0] = sc->sc_default_cmap[j];
1142 		j++;
1143 		sc->sc_cmap.cm_map[i][1] = sc->sc_default_cmap[j];
1144 		j++;
1145 		sc->sc_cmap.cm_map[i][2] = sc->sc_default_cmap[j];
1146 		j++;
1147 	}
1148 	cg6_loadcmap(sc, 0, 256);
1149 }
1150 
1151 int
1152 cgsix_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
1153 	struct lwp *l)
1154 {
1155 	/* we'll probably need to add more stuff here */
1156 	struct vcons_data *vd = v;
1157 	struct cgsix_softc *sc = vd->cookie;
1158 	struct wsdisplay_fbinfo *wdf;
1159 	struct rasops_info *ri = &sc->sc_fb.fb_rinfo;
1160 	struct vcons_screen *ms = sc->vd.active;
1161 
1162 #ifdef CGSIX_DEBUG
1163 	printf("cgsix_ioctl(%lx)\n",cmd);
1164 #endif
1165 	switch (cmd) {
1166 		case WSDISPLAYIO_GTYPE:
1167 			*(u_int *)data = WSDISPLAY_TYPE_SUNTCX;
1168 			return 0;
1169 		case WSDISPLAYIO_GINFO:
1170 			wdf = (void *)data;
1171 			wdf->height = ri->ri_height;
1172 			wdf->width = ri->ri_width;
1173 			wdf->depth = ri->ri_depth;
1174 			wdf->cmsize = 256;
1175 			return 0;
1176 
1177 		case WSDISPLAYIO_GETCMAP:
1178 			return cgsix_getcmap(sc,
1179 			    (struct wsdisplay_cmap *)data);
1180 		case WSDISPLAYIO_PUTCMAP:
1181 			return cgsix_putcmap(sc,
1182 			    (struct wsdisplay_cmap *)data);
1183 
1184 		case WSDISPLAYIO_LINEBYTES:
1185 			*(u_int *)data = sc->sc_stride;
1186 			return 0;
1187 
1188 		case WSDISPLAYIO_SMODE:
1189 			{
1190 				int new_mode = *(int*)data;
1191 
1192 				if (new_mode != sc->sc_mode) {
1193 					sc->sc_mode = new_mode;
1194 					if (IS_IN_EMUL_MODE(sc)) {
1195 						cg6_reset(sc);
1196 						cg6_ras_init(sc);
1197 						cg6_setup_palette(sc);
1198 						glyphcache_wipe(&sc->sc_gc);
1199 						vcons_redraw_screen(ms);
1200 					}
1201 				}
1202 			}
1203 	}
1204 	return EPASSTHROUGH;
1205 }
1206 
1207 paddr_t
1208 cgsix_mmap(void *v, void *vs, off_t offset, int prot)
1209 {
1210 	struct vcons_data *vd = v;
1211 	struct cgsix_softc *sc = vd->cookie;
1212 
1213 	if (offset < sc->sc_ramsize) {
1214 		return bus_space_mmap(sc->sc_bustag, sc->sc_paddr,
1215 		    CGSIX_RAM_OFFSET + offset, prot, BUS_SPACE_MAP_LINEAR);
1216 	}
1217 	return -1;
1218 }
1219 
1220 int
1221 cgsix_putcmap(struct cgsix_softc *sc, struct wsdisplay_cmap *cm)
1222 {
1223 	u_int index = cm->index;
1224 	u_int count = cm->count;
1225 	int error, i;
1226 
1227 	if (index >= 256 || count > 256 || index + count > 256)
1228 		return EINVAL;
1229 
1230 	for (i = 0; i < count; i++)
1231 	{
1232 		error = copyin(&cm->red[i],
1233 		    &sc->sc_cmap.cm_map[index + i][0], 1);
1234 		if (error)
1235 			return error;
1236 		error = copyin(&cm->green[i],
1237 		    &sc->sc_cmap.cm_map[index + i][1],
1238 		    1);
1239 		if (error)
1240 			return error;
1241 		error = copyin(&cm->blue[i],
1242 		    &sc->sc_cmap.cm_map[index + i][2], 1);
1243 		if (error)
1244 			return error;
1245 	}
1246 	cg6_loadcmap(sc, index, count);
1247 
1248 	return 0;
1249 }
1250 
1251 int
1252 cgsix_getcmap(struct cgsix_softc *sc, struct wsdisplay_cmap *cm)
1253 {
1254 	u_int index = cm->index;
1255 	u_int count = cm->count;
1256 	int error,i;
1257 
1258 	if (index >= 256 || count > 256 || index + count > 256)
1259 		return EINVAL;
1260 
1261 	for (i = 0; i < count; i++)
1262 	{
1263 		error = copyout(&sc->sc_cmap.cm_map[index + i][0],
1264 		    &cm->red[i], 1);
1265 		if (error)
1266 			return error;
1267 		error = copyout(&sc->sc_cmap.cm_map[index + i][1],
1268 		    &cm->green[i], 1);
1269 		if (error)
1270 			return error;
1271 		error = copyout(&sc->sc_cmap.cm_map[index + i][2],
1272 		    &cm->blue[i], 1);
1273 		if (error)
1274 			return error;
1275 	}
1276 
1277 	return 0;
1278 }
1279 
1280 void
1281 cgsix_init_screen(void *cookie, struct vcons_screen *scr,
1282     int existing, long *defattr)
1283 {
1284 	struct cgsix_softc *sc = cookie;
1285 	struct rasops_info *ri = &scr->scr_ri;
1286 	int av;
1287 
1288 	ri->ri_depth = 8;
1289 	ri->ri_width = sc->sc_width;
1290 	ri->ri_height = sc->sc_height;
1291 	ri->ri_stride = sc->sc_stride;
1292 	av = sc->sc_ramsize - (sc->sc_height * sc->sc_stride);
1293 	ri->ri_flg = RI_CENTER  | RI_8BIT_IS_RGB;
1294 	if (av > (128 * 1024)) {
1295 		ri->ri_flg |= RI_ENABLE_ALPHA;
1296 	}
1297 	ri->ri_bits = sc->sc_fb.fb_pixels;
1298 
1299 	/* We need unaccelerated initial screen clear on old revisions */
1300 	if (sc->sc_fhcrev < 2)
1301 		memset(sc->sc_fb.fb_pixels, (*defattr >> 16) & 0xff,
1302 		    sc->sc_stride * sc->sc_height);
1303 	rasops_init(ri, 0, 0);
1304 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
1305 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
1306 		    sc->sc_width / ri->ri_font->fontwidth);
1307 
1308 	/* enable acceleration */
1309 	ri->ri_hw = scr;
1310 	ri->ri_ops.copyrows = cg6_ras_copyrows;
1311 	ri->ri_ops.copycols = cg6_ras_copycols;
1312 	ri->ri_ops.eraserows = cg6_ras_eraserows;
1313 	ri->ri_ops.erasecols = cg6_ras_erasecols;
1314 	ri->ri_ops.cursor = cgsix_cursor;
1315 	if (FONT_IS_ALPHA(ri->ri_font)) {
1316 		ri->ri_ops.putchar = cgsix_putchar_aa;
1317 	} else
1318 		ri->ri_ops.putchar = cgsix_putchar;
1319 }
1320 
1321 void
1322 cgsix_rectfill(struct cgsix_softc *sc, int xs, int ys, int wi, int he,
1323     uint32_t col)
1324 {
1325 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
1326 
1327 	CG6_WAIT_READY(fbc);
1328 
1329 	fbc->fbc_alu = CG6_ALU_FILL;
1330 	fbc->fbc_mode = GX_BLIT_SRC | GX_MODE_COLOR8;
1331 
1332 	fbc->fbc_fg = col;
1333 	fbc->fbc_arecty = ys;
1334 	fbc->fbc_arectx = xs;
1335 	fbc->fbc_arecty = ys + he - 1;
1336 	fbc->fbc_arectx = xs + wi - 1;
1337 	CG6_DRAW(fbc);
1338 }
1339 
1340 void
1341 cgsix_bitblt(void *cookie, int xs, int ys, int xd, int yd,
1342     int wi, int he, int rop)
1343 {
1344 	struct cgsix_softc *sc = cookie;
1345 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
1346 	CG6_WAIT_READY(fbc);
1347 
1348 	fbc->fbc_alu = rop;
1349 	fbc->fbc_mode = GX_BLIT_SRC | GX_MODE_COLOR8;
1350 
1351 	fbc->fbc_x0 = xs;
1352 	fbc->fbc_y0 = ys;
1353 	fbc->fbc_x1 = xs + wi - 1;
1354 	fbc->fbc_y1 = ys + he - 1;
1355 	fbc->fbc_x2 = xd;
1356 	fbc->fbc_y2 = yd;
1357 	fbc->fbc_x3 = xd + wi - 1;
1358 	fbc->fbc_y3 = yd + he - 1;
1359 	CG6_BLIT(fbc);
1360 }
1361 
1362 void
1363 cgsix_setup_mono(struct cgsix_softc *sc, int x, int y, int wi, int he,
1364     uint32_t fg, uint32_t bg)
1365 {
1366 	volatile struct cg6_fbc *fbc=sc->sc_fbc;
1367 
1368 	CG6_WAIT_READY(fbc);
1369 
1370 	fbc->fbc_x0 = x;
1371 	fbc->fbc_x1 = x + wi - 1;
1372 	fbc->fbc_y0 = y;
1373 	fbc->fbc_incx = 0;
1374 	fbc->fbc_incy = 1;
1375 	fbc->fbc_fg = fg;
1376 	fbc->fbc_bg = bg;
1377 	fbc->fbc_mode = GX_BLIT_NOSRC | GX_MODE_COLOR1;
1378 	fbc->fbc_alu = GX_PATTERN_ONES | ROP_OSTP(GX_ROP_CLEAR, GX_ROP_SET);
1379 	sc->sc_mono_width = wi;
1380 	/* now feed the data into the chip */
1381 }
1382 
1383 void
1384 cgsix_feed_line(struct cgsix_softc *sc, int count, uint8_t *data)
1385 {
1386 	int i;
1387 	uint32_t latch, res = 0, shift;
1388 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
1389 
1390 	if (sc->sc_mono_width > 32) {
1391 		/* ARGH! */
1392 	} else
1393 	{
1394 		shift = 24;
1395 		for (i = 0; i < count; i++) {
1396 			latch = data[i];
1397 			res |= latch << shift;
1398 			shift -= 8;
1399 		}
1400 		fbc->fbc_font = res;
1401 	}
1402 }
1403 
1404 void
1405 cgsix_putchar(void *cookie, int row, int col, u_int c, long attr)
1406 {
1407 	struct rasops_info *ri = cookie;
1408 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1409 	struct vcons_screen *scr = ri->ri_hw;
1410 	struct cgsix_softc *sc = scr->scr_cookie;
1411 	int inv;
1412 
1413 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
1414 	    (col < ri->ri_cols)) {
1415 
1416 		if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1417 
1418 			int fg, bg, uc, i;
1419 			uint8_t *data;
1420 			int x, y, wi, he;
1421 			volatile struct cg6_fbc *fbc = sc->sc_fbc;
1422 
1423 			wi = font->fontwidth;
1424 			he = font->fontheight;
1425 
1426 			if (!CHAR_IN_FONT(c, font))
1427 				return;
1428 			inv = ((attr >> 8) & WSATTR_REVERSE);
1429 			if (inv) {
1430 				fg = (u_char)ri->ri_devcmap[(attr >> 16) &
1431 				    0xff];
1432 				bg = (u_char)ri->ri_devcmap[(attr >> 24) &
1433 				    0xff];
1434 			} else {
1435 				bg = (u_char)ri->ri_devcmap[(attr >> 16) &
1436 				    0xff];
1437 				fg = (u_char)ri->ri_devcmap[(attr >> 24) &
1438 				    0xff];
1439 			}
1440 
1441 			x = ri->ri_xorigin + col * wi;
1442 			y = ri->ri_yorigin + row * he;
1443 
1444 			if (c == 0x20) {
1445 				cgsix_rectfill(sc, x, y, wi, he, bg);
1446 			} else {
1447 				uc = c - font->firstchar;
1448 				data = (uint8_t *)font->data + uc *
1449 				    ri->ri_fontscale;
1450 
1451 				cgsix_setup_mono(sc, x, y, wi, 1, fg, bg);
1452 				for (i = 0; i < he; i++) {
1453 					cgsix_feed_line(sc, font->stride,
1454 					    data);
1455 					data += font->stride;
1456 				}
1457 				/* put the chip back to normal */
1458 				fbc->fbc_incy = 0;
1459 			}
1460 		}
1461 	}
1462 }
1463 
1464 void
1465 cgsix_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
1466 {
1467 	struct rasops_info *ri = cookie;
1468 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1469 	struct vcons_screen *scr = ri->ri_hw;
1470 	struct cgsix_softc *sc = scr->scr_cookie;
1471 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
1472 
1473 	uint32_t bg, latch = 0, bg8, fg8, pixel;
1474 	int i, j, shift, x, y, wi, he, r, g, b, aval;
1475 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
1476 	uint8_t *data8;
1477 	int rv;
1478 
1479 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1480 		return;
1481 
1482 	if (!CHAR_IN_FONT(c, font))
1483 		return;
1484 
1485 	wi = font->fontwidth;
1486 	he = font->fontheight;
1487 
1488 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1489 	x = ri->ri_xorigin + col * wi;
1490 	y = ri->ri_yorigin + row * he;
1491 	if (c == 0x20) {
1492 		cgsix_rectfill(sc, x, y, wi, he, bg);
1493 		return;
1494 	}
1495 
1496 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1497 	if (rv == GC_OK)
1498 		return;
1499 
1500 	data8 = WSFONT_GLYPH(c, font);
1501 
1502 	CG6_WAIT_READY(sc->sc_fbc);
1503 	fbc->fbc_incx = 4;
1504 	fbc->fbc_incy = 0;
1505 	fbc->fbc_mode = GX_BLIT_NOSRC | GX_MODE_COLOR8;
1506 	fbc->fbc_alu = CG6_ALU_COPY;
1507 	fbc->fbc_clipmaxx = x + wi - 1;
1508 
1509 	/*
1510 	 * we need the RGB colours here, so get offsets into rasops_cmap
1511 	 */
1512 	fgo = ((attr >> 24) & 0xf) * 3;
1513 	bgo = ((attr >> 16) & 0xf) * 3;
1514 
1515 	r0 = rasops_cmap[bgo];
1516 	r1 = rasops_cmap[fgo];
1517 	g0 = rasops_cmap[bgo + 1];
1518 	g1 = rasops_cmap[fgo + 1];
1519 	b0 = rasops_cmap[bgo + 2];
1520 	b1 = rasops_cmap[fgo + 2];
1521 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
1522 	bg8 = R3G3B2(r0, g0, b0);
1523 	fg8 = R3G3B2(r1, g1, b1);
1524 
1525 	for (i = 0; i < he; i++) {
1526 
1527 		CG6_WAIT_READY(fbc);
1528 		fbc->fbc_x0 = x;
1529 		fbc->fbc_x1 = x + 3;
1530 		fbc->fbc_y0 = y + i;
1531 
1532 		shift = 24;
1533 		for (j = 0; j < wi; j++) {
1534 			aval = *data8;
1535 			if (aval == 0) {
1536 				pixel = bg8;
1537 			} else if (aval == 255) {
1538 				pixel = fg8;
1539 			} else {
1540 				r = aval * r1 + (255 - aval) * r0;
1541 				g = aval * g1 + (255 - aval) * g0;
1542 				b = aval * b1 + (255 - aval) * b0;
1543 				pixel = ((r & 0xe000) >> 8) |
1544 					((g & 0xe000) >> 11) |
1545 					((b & 0xc000) >> 14);
1546 			}
1547 			data8++;
1548 
1549 			latch |= pixel << shift;
1550 			if (shift == 0) {
1551 				fbc->fbc_font = latch;
1552 				latch = 0;
1553 				shift = 24;
1554 			} else
1555 				shift -= 8;
1556 		}
1557 		if (shift != 24)
1558 			fbc->fbc_font = latch;
1559 	}
1560 	fbc->fbc_clipmaxx = 0x3fff;
1561 
1562 	if (rv == GC_ADD) {
1563 		glyphcache_add(&sc->sc_gc, c, x, y);
1564 	}
1565 }
1566 
1567 void
1568 cgsix_cursor(void *cookie, int on, int row, int col)
1569 {
1570 	struct rasops_info *ri = cookie;
1571 	struct vcons_screen *scr = ri->ri_hw;
1572 	struct cgsix_softc *sc = scr->scr_cookie;
1573 	int x, y, wi, he;
1574 
1575 	wi = ri->ri_font->fontwidth;
1576 	he = ri->ri_font->fontheight;
1577 
1578 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1579 		x = ri->ri_ccol * wi + ri->ri_xorigin;
1580 		y = ri->ri_crow * he + ri->ri_yorigin;
1581 		if (ri->ri_flg & RI_CURSOR) {
1582 			cg6_invert(sc, x, y, wi, he);
1583 			ri->ri_flg &= ~RI_CURSOR;
1584 		}
1585 		ri->ri_crow = row;
1586 		ri->ri_ccol = col;
1587 		if (on)
1588 		{
1589 			x = ri->ri_ccol * wi + ri->ri_xorigin;
1590 			y = ri->ri_crow * he + ri->ri_yorigin;
1591 			cg6_invert(sc, x, y, wi, he);
1592 			ri->ri_flg |= RI_CURSOR;
1593 		}
1594 	} else
1595 	{
1596 		ri->ri_crow = row;
1597 		ri->ri_ccol = col;
1598 		ri->ri_flg &= ~RI_CURSOR;
1599 	}
1600 }
1601 
1602 void
1603 cgsix_clearscreen(struct cgsix_softc *sc)
1604 {
1605 	struct rasops_info *ri = &cg6_console_screen.scr_ri;
1606 
1607 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1608 		volatile struct cg6_fbc *fbc = sc->sc_fbc;
1609 
1610 		CG6_WAIT_READY(fbc);
1611 
1612 		fbc->fbc_alu = CG6_ALU_FILL;
1613 		fbc->fbc_mode = GX_BLIT_SRC | GX_MODE_COLOR8;
1614 
1615 		fbc->fbc_fg = ri->ri_devcmap[sc->sc_bg];
1616 		fbc->fbc_arectx = 0;
1617 		fbc->fbc_arecty = 0;
1618 		fbc->fbc_arectx = ri->ri_width - 1;
1619 		fbc->fbc_arecty = ri->ri_height - 1;
1620 		CG6_DRAW(fbc);
1621 	}
1622 }
1623 
1624 #endif /* NWSDISPLAY > 0 */
1625 
1626 #if (NWSDISPLAY > 0) || defined(RASTERCONSOLE)
1627 void
1628 cg6_invert(struct cgsix_softc *sc, int x, int y, int wi, int he)
1629 {
1630 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
1631 
1632 	CG6_WAIT_READY(fbc);
1633 
1634 	fbc->fbc_alu = CG6_ALU_FLIP;
1635 	fbc->fbc_mode = GX_BLIT_SRC | GX_MODE_COLOR8;
1636 	fbc->fbc_arecty = y;
1637 	fbc->fbc_arectx = x;
1638 	fbc->fbc_arecty = y + he - 1;
1639 	fbc->fbc_arectx = x + wi - 1;
1640 	CG6_DRAW(fbc);
1641 }
1642 
1643 #endif
1644 
1645