xref: /netbsd-src/sys/dev/sun/cgsix.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: cgsix.c,v 1.59 2012/07/18 02:31:46 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.59 2012/07/18 02:31:46 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 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
638 				(sc->sc_ramsize / sc->sc_stride) -
639 				  sc->sc_height - 5,
640 				sc->sc_width,
641 				ri->ri_font->fontwidth,
642 				ri->ri_font->fontheight,
643 				defattr);
644 
645 		vcons_replay_msgbuf(&cg6_console_screen);
646 	} else {
647 		/*
648 		 * since we're not the console we can postpone the rest
649 		 * until someone actually allocates a screen for us
650 		 */
651 		if (cg6_console_screen.scr_ri.ri_rows == 0) {
652 			/* do some minimal setup to avoid weirdnesses later */
653 			vcons_init_screen(&sc->vd, &cg6_console_screen, 1,
654 			    &defattr);
655 		}
656 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
657 				(sc->sc_ramsize / sc->sc_stride) -
658 				  sc->sc_height - 5,
659 				sc->sc_width,
660 				ri->ri_font->fontwidth,
661 				ri->ri_font->fontheight,
662 				defattr);
663 	}
664 	cg6_setup_palette(sc);
665 
666 	aa.scrdata = &cgsix_screenlist;
667 	aa.console = isconsole;
668 	aa.accessops = &cgsix_accessops;
669 	aa.accesscookie = &sc->vd;
670 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
671 #else
672 	bt_initcmap(&sc->sc_cmap, 256);
673 	cg6_loadcmap(sc, 0, 256);
674 
675 #endif
676 }
677 
678 
679 int
680 cgsixopen(dev_t dev, int flags, int mode, struct lwp *l)
681 {
682 	device_t dv = device_lookup(&cgsix_cd, minor(dev));
683 	struct cgsix_softc *sc = device_private(dv);
684 
685 	if (dv == NULL)
686 		return ENXIO;
687 	sc->sc_fb_is_open = TRUE;
688 
689 	return 0;
690 }
691 
692 int
693 cgsixclose(dev_t dev, int flags, int mode, struct lwp *l)
694 {
695 	device_t dv = device_lookup(&cgsix_cd, minor(dev));
696 	struct cgsix_softc *sc = device_private(dv);
697 
698 	cg6_reset(sc);
699 	sc->sc_fb_is_open = FALSE;
700 
701 #if NWSDISPLAY > 0
702 	if (IS_IN_EMUL_MODE(sc)) {
703 		struct vcons_screen *ms = sc->vd.active;
704 
705 		cg6_ras_init(sc);
706 		cg6_setup_palette(sc);
707 		glyphcache_wipe(&sc->sc_gc);
708 
709 		/* we don't know if the screen exists */
710 		if (ms != NULL)
711 			vcons_redraw_screen(ms);
712 	}
713 #else
714 	/* (re-)initialize the default color map */
715 	bt_initcmap(&sc->sc_cmap, 256);
716 
717 	cg6_loadcmap(sc, 0, 256);
718 #endif
719 	return 0;
720 }
721 
722 int
723 cgsixioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
724 {
725 	struct cgsix_softc *sc = device_lookup_private(&cgsix_cd, minor(dev));
726 	union cursor_cmap tcm;
727 	uint32_t image[32], mask[32];
728 	u_int count;
729 	int v, error;
730 
731 #ifdef CGSIX_DEBUG
732 	printf("cgsixioctl(%lx)\n",cmd);
733 #endif
734 
735 	switch (cmd) {
736 
737 	case FBIOGTYPE:
738 		*(struct fbtype *)data = sc->sc_fb.fb_type;
739 		break;
740 
741 	case FBIOGATTR:
742 #define fba ((struct fbgattr *)data)
743 		fba->real_type = sc->sc_fb.fb_type.fb_type;
744 		fba->owner = 0;		/* XXX ??? */
745 		fba->fbtype = sc->sc_fb.fb_type;
746 		fba->sattr.flags = 0;
747 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
748 		fba->sattr.dev_specific[0] = -1;
749 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
750 		fba->emu_types[1] = -1;
751 #undef fba
752 		break;
753 
754 	case FBIOGETCMAP:
755 #define	p ((struct fbcmap *)data)
756 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
757 
758 	case FBIOPUTCMAP:
759 		/* copy to software map */
760 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
761 		if (error)
762 			return error;
763 		/* now blast them into the chip */
764 		/* XXX should use retrace interrupt */
765 		cg6_loadcmap(sc, p->index, p->count);
766 #undef p
767 		break;
768 
769 	case FBIOGVIDEO:
770 		*(int *)data = sc->sc_blanked;
771 		break;
772 
773 	case FBIOSVIDEO:
774 		cg6_blank(sc, !(*(int *)data));
775 		break;
776 
777 /* these are for both FBIOSCURSOR and FBIOGCURSOR */
778 #define p ((struct fbcursor *)data)
779 #define cc (&sc->sc_cursor)
780 
781 	case FBIOGCURSOR:
782 		/* do not quite want everything here... */
783 		p->set = FB_CUR_SETALL;	/* close enough, anyway */
784 		p->enable = cc->cc_enable;
785 		p->pos = cc->cc_pos;
786 		p->hot = cc->cc_hot;
787 		p->size = cc->cc_size;
788 
789 		/* begin ugh ... can we lose some of this crap?? */
790 		if (p->image != NULL) {
791 			count = cc->cc_size.y * 32 / NBBY;
792 			error = copyout(cc->cc_bits[1], p->image, count);
793 			if (error)
794 				return error;
795 			error = copyout(cc->cc_bits[0], p->mask, count);
796 			if (error)
797 				return error;
798 		}
799 		if (p->cmap.red != NULL) {
800 			error = bt_getcmap(&p->cmap,
801 			    (union bt_cmap *)&cc->cc_color, 2, 1);
802 			if (error)
803 				return error;
804 		} else {
805 			p->cmap.index = 0;
806 			p->cmap.count = 2;
807 		}
808 		/* end ugh */
809 		break;
810 
811 	case FBIOSCURSOR:
812 		/*
813 		 * For setcmap and setshape, verify parameters, so that
814 		 * we do not get halfway through an update and then crap
815 		 * out with the software state screwed up.
816 		 */
817 		v = p->set;
818 		if (v & FB_CUR_SETCMAP) {
819 			/*
820 			 * This use of a temporary copy of the cursor
821 			 * colormap is not terribly efficient, but these
822 			 * copies are small (8 bytes)...
823 			 */
824 			tcm = cc->cc_color;
825 			error = bt_putcmap(&p->cmap, (union bt_cmap *)&tcm, 2,
826 			    1);
827 			if (error)
828 				return error;
829 		}
830 		if (v & FB_CUR_SETSHAPE) {
831 			if ((u_int)p->size.x > 32 || (u_int)p->size.y > 32)
832 				return EINVAL;
833 			count = p->size.y * 32 / NBBY;
834 			error = copyin(p->image, image, count);
835 			if (error)
836 				return error;
837 			error = copyin(p->mask, mask, count);
838 			if (error)
839 				return error;
840 		}
841 
842 		/* parameters are OK; do it */
843 		if (v & (FB_CUR_SETCUR | FB_CUR_SETPOS | FB_CUR_SETHOT)) {
844 			if (v & FB_CUR_SETCUR)
845 				cc->cc_enable = p->enable;
846 			if (v & FB_CUR_SETPOS)
847 				cc->cc_pos = p->pos;
848 			if (v & FB_CUR_SETHOT)
849 				cc->cc_hot = p->hot;
850 			cg6_setcursor(sc);
851 		}
852 		if (v & FB_CUR_SETCMAP) {
853 			cc->cc_color = tcm;
854 			cg6_loadomap(sc); /* XXX defer to vertical retrace */
855 		}
856 		if (v & FB_CUR_SETSHAPE) {
857 			cc->cc_size = p->size;
858 			count = p->size.y * 32 / NBBY;
859 			memset(cc->cc_bits, 0, sizeof cc->cc_bits);
860 			memcpy(cc->cc_bits[1], image, count);
861 			memcpy(cc->cc_bits[0], mask, count);
862 			cg6_loadcursor(sc);
863 		}
864 		break;
865 
866 #undef p
867 #undef cc
868 
869 	case FBIOGCURPOS:
870 		*(struct fbcurpos *)data = sc->sc_cursor.cc_pos;
871 		break;
872 
873 	case FBIOSCURPOS:
874 		sc->sc_cursor.cc_pos = *(struct fbcurpos *)data;
875 		cg6_setcursor(sc);
876 		break;
877 
878 	case FBIOGCURMAX:
879 		/* max cursor size is 32x32 */
880 		((struct fbcurpos *)data)->x = 32;
881 		((struct fbcurpos *)data)->y = 32;
882 		break;
883 
884 	default:
885 #ifdef DEBUG
886 		log(LOG_NOTICE, "cgsixioctl(0x%lx) (%s[%d])\n", cmd,
887 		    l->l_proc->p_comm, l->l_proc->p_pid);
888 #endif
889 		return ENOTTY;
890 	}
891 	return 0;
892 }
893 
894 /*
895  * Clean up hardware state (e.g., after bootup or after X crashes).
896  */
897 static void
898 cg6_reset(struct cgsix_softc *sc)
899 {
900 	volatile struct cg6_tec_xxx *tec;
901 	int fhc;
902 	volatile struct bt_regs *bt;
903 
904 	/* hide the cursor, just in case */
905 	sc->sc_thc->thc_cursxy = (THC_CURSOFF << 16) | THC_CURSOFF;
906 
907 	/* turn off frobs in transform engine (makes X11 work) */
908 	tec = sc->sc_tec;
909 	tec->tec_mv = 0;
910 	tec->tec_clip = 0;
911 	tec->tec_vdc = 0;
912 
913 	/* take care of hardware bugs in old revisions */
914 	if (sc->sc_fhcrev < 5) {
915 		/*
916 		 * Keep current resolution; set CPU to 68020, set test
917 		 * window (size 1Kx1K), and for rev 1, disable dest cache.
918 		 */
919 		fhc = (*sc->sc_fhc & FHC_RES_MASK) | FHC_CPU_68020 |
920 		    FHC_TEST |
921 		    (11 << FHC_TESTX_SHIFT) | (11 << FHC_TESTY_SHIFT);
922 		if (sc->sc_fhcrev < 2)
923 			fhc |= FHC_DST_DISABLE;
924 		*sc->sc_fhc = fhc;
925 	}
926 
927 	/* Enable cursor in Brooktree DAC. */
928 	bt = sc->sc_bt;
929 	bt->bt_addr = 0x06 << 24;
930 	bt->bt_ctrl |= 0x03 << 24;
931 }
932 
933 static void
934 cg6_setcursor(struct cgsix_softc *sc)
935 {
936 
937 	/* we need to subtract the hot-spot value here */
938 #define COORD(f) (sc->sc_cursor.cc_pos.f - sc->sc_cursor.cc_hot.f)
939 	sc->sc_thc->thc_cursxy = sc->sc_cursor.cc_enable ?
940 	    ((COORD(x) << 16) | (COORD(y) & 0xffff)) :
941 	    (THC_CURSOFF << 16) | THC_CURSOFF;
942 #undef COORD
943 }
944 
945 static void
946 cg6_loadcursor(struct cgsix_softc *sc)
947 {
948 	volatile struct cg6_thc *thc;
949 	u_int edgemask, m;
950 	int i;
951 
952 	/*
953 	 * Keep the top size.x bits.  Here we *throw out* the top
954 	 * size.x bits from an all-one-bits word, introducing zeros in
955 	 * the top size.x bits, then invert all the bits to get what
956 	 * we really wanted as our mask.  But this fails if size.x is
957 	 * 32---a sparc uses only the low 5 bits of the shift count---
958 	 * so we have to special case that.
959 	 */
960 	edgemask = ~0;
961 	if (sc->sc_cursor.cc_size.x < 32)
962 		edgemask = ~(edgemask >> sc->sc_cursor.cc_size.x);
963 	thc = sc->sc_thc;
964 	for (i = 0; i < 32; i++) {
965 		m = sc->sc_cursor.cc_bits[0][i] & edgemask;
966 		thc->thc_cursmask[i] = m;
967 		thc->thc_cursbits[i] = m & sc->sc_cursor.cc_bits[1][i];
968 	}
969 }
970 
971 /*
972  * Load a subset of the current (new) colormap into the color DAC.
973  */
974 static void
975 cg6_loadcmap(struct cgsix_softc *sc, int start, int ncolors)
976 {
977 	volatile struct bt_regs *bt;
978 	u_int *ip, i;
979 	int count;
980 
981 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
982 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
983 	bt = sc->sc_bt;
984 	bt->bt_addr = BT_D4M4(start) << 24;
985 	while (--count >= 0) {
986 		i = *ip++;
987 		/* hardware that makes one want to pound boards with hammers */
988 		bt->bt_cmap = i;
989 		bt->bt_cmap = i << 8;
990 		bt->bt_cmap = i << 16;
991 		bt->bt_cmap = i << 24;
992 	}
993 }
994 
995 /*
996  * Load the cursor (overlay `foreground' and `background') colors.
997  */
998 static void
999 cg6_loadomap(struct cgsix_softc *sc)
1000 {
1001 	volatile struct bt_regs *bt;
1002 	u_int i;
1003 
1004 	bt = sc->sc_bt;
1005 	bt->bt_addr = 0x01 << 24;	/* set background color */
1006 	i = sc->sc_cursor.cc_color.cm_chip[0];
1007 	bt->bt_omap = i;		/* R */
1008 	bt->bt_omap = i << 8;		/* G */
1009 	bt->bt_omap = i << 16;		/* B */
1010 
1011 	bt->bt_addr = 0x03 << 24;	/* set foreground color */
1012 	bt->bt_omap = i << 24;		/* R */
1013 	i = sc->sc_cursor.cc_color.cm_chip[1];
1014 	bt->bt_omap = i;		/* G */
1015 	bt->bt_omap = i << 8;		/* B */
1016 }
1017 
1018 /* blank or unblank the screen */
1019 static void
1020 cg6_blank(struct cgsix_softc *sc, int flag)
1021 {
1022 
1023 	if (sc->sc_blanked != flag) {
1024 		sc->sc_blanked = flag;
1025 		if (flag) {
1026 			sc->sc_thc->thc_misc &= ~THC_MISC_VIDEN;
1027 		} else {
1028 			sc->sc_thc->thc_misc |= THC_MISC_VIDEN;
1029 		}
1030 	}
1031 }
1032 
1033 /*
1034  * this is called on panic or ddb entry - force the console to the front, reset
1035  * the colour map and enable drawing so we actually see the message even when X
1036  * is running
1037  */
1038 static void
1039 cg6_unblank(device_t dev)
1040 {
1041 	struct cgsix_softc *sc = device_private(dev);
1042 
1043 	cg6_blank(sc, 0);
1044 }
1045 
1046 /* XXX the following should be moved to a "user interface" header */
1047 /*
1048  * Base addresses at which users can mmap() the various pieces of a cg6.
1049  * Note that although the Brooktree color registers do not occupy 8K,
1050  * the X server dies if we do not allow it to map 8K there (it just maps
1051  * from 0x70000000 forwards, as a contiguous chunk).
1052  */
1053 #define	CG6_USER_FBC	0x70000000
1054 #define	CG6_USER_TEC	0x70001000
1055 #define	CG6_USER_BTREGS	0x70002000
1056 #define	CG6_USER_FHC	0x70004000
1057 #define	CG6_USER_THC	0x70005000
1058 #define	CG6_USER_ROM	0x70006000
1059 #define	CG6_USER_RAM	0x70016000
1060 #define	CG6_USER_DHC	0x80000000
1061 
1062 struct mmo {
1063 	u_long	mo_uaddr;	/* user (virtual) address */
1064 	u_long	mo_size;	/* size, or 0 for video ram size */
1065 	u_long	mo_physoff;	/* offset from sc_physadr */
1066 };
1067 
1068 /*
1069  * Return the address that would map the given device at the given
1070  * offset, allowing for the given protection, or return -1 for error.
1071  *
1072  * XXX	needs testing against `demanding' applications (e.g., aviator)
1073  */
1074 paddr_t
1075 cgsixmmap(dev_t dev, off_t off, int prot)
1076 {
1077 	struct cgsix_softc *sc = device_lookup_private(&cgsix_cd, minor(dev));
1078 	struct mmo *mo;
1079 	u_int u, sz;
1080 	static struct mmo mmo[] = {
1081 		{ CG6_USER_RAM, 0, CGSIX_RAM_OFFSET },
1082 
1083 		/* do not actually know how big most of these are! */
1084 		{ CG6_USER_FBC, 1, CGSIX_FBC_OFFSET },
1085 		{ CG6_USER_TEC, 1, CGSIX_TEC_OFFSET },
1086 		{ CG6_USER_BTREGS, 8192 /* XXX */, CGSIX_BT_OFFSET },
1087 		{ CG6_USER_FHC, 1, CGSIX_FHC_OFFSET },
1088 		{ CG6_USER_THC, sizeof(struct cg6_thc), CGSIX_THC_OFFSET },
1089 		{ CG6_USER_ROM, 65536, CGSIX_ROM_OFFSET },
1090 		{ CG6_USER_DHC, 1, CGSIX_DHC_OFFSET },
1091 	};
1092 #define NMMO (sizeof mmo / sizeof *mmo)
1093 
1094 	if (off & PGOFSET)
1095 		panic("cgsixmmap");
1096 
1097 	/*
1098 	 * Entries with size 0 map video RAM (i.e., the size in fb data).
1099 	 *
1100 	 * Since we work in pages, the fact that the map offset table's
1101 	 * sizes are sometimes bizarre (e.g., 1) is effectively ignored:
1102 	 * one byte is as good as one page.
1103 	 */
1104 	for (mo = mmo; mo < &mmo[NMMO]; mo++) {
1105 		if ((u_long)off < mo->mo_uaddr)
1106 			continue;
1107 		u = off - mo->mo_uaddr;
1108 		sz = mo->mo_size ? mo->mo_size :
1109 		    sc->sc_ramsize;
1110 		if (u < sz) {
1111 			return (bus_space_mmap(sc->sc_bustag,
1112 				sc->sc_paddr, u+mo->mo_physoff,
1113 				prot, BUS_SPACE_MAP_LINEAR));
1114 		}
1115 	}
1116 
1117 #ifdef DEBUG
1118 	{
1119 	  struct proc *p = curlwp->l_proc;	/* XXX */
1120 	  log(LOG_NOTICE, "cgsixmmap(0x%llx) (%s[%d])\n",
1121 		(long long)off, p->p_comm, p->p_pid);
1122 	}
1123 #endif
1124 	return -1;	/* not a user-map offset */
1125 }
1126 
1127 #if NWSDISPLAY > 0
1128 
1129 static void
1130 cg6_setup_palette(struct cgsix_softc *sc)
1131 {
1132 	int i, j;
1133 
1134 	rasops_get_cmap(&cg6_console_screen.scr_ri, sc->sc_default_cmap,
1135 	    sizeof(sc->sc_default_cmap));
1136 	j = 0;
1137 	for (i = 0; i < 256; i++) {
1138 		sc->sc_cmap.cm_map[i][0] = sc->sc_default_cmap[j];
1139 		j++;
1140 		sc->sc_cmap.cm_map[i][1] = sc->sc_default_cmap[j];
1141 		j++;
1142 		sc->sc_cmap.cm_map[i][2] = sc->sc_default_cmap[j];
1143 		j++;
1144 	}
1145 	cg6_loadcmap(sc, 0, 256);
1146 }
1147 
1148 int
1149 cgsix_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
1150 	struct lwp *l)
1151 {
1152 	/* we'll probably need to add more stuff here */
1153 	struct vcons_data *vd = v;
1154 	struct cgsix_softc *sc = vd->cookie;
1155 	struct wsdisplay_fbinfo *wdf;
1156 	struct rasops_info *ri = &sc->sc_fb.fb_rinfo;
1157 	struct vcons_screen *ms = sc->vd.active;
1158 
1159 #ifdef CGSIX_DEBUG
1160 	printf("cgsix_ioctl(%lx)\n",cmd);
1161 #endif
1162 	switch (cmd) {
1163 		case WSDISPLAYIO_GTYPE:
1164 			*(u_int *)data = WSDISPLAY_TYPE_SUNTCX;
1165 			return 0;
1166 		case WSDISPLAYIO_GINFO:
1167 			wdf = (void *)data;
1168 			wdf->height = ri->ri_height;
1169 			wdf->width = ri->ri_width;
1170 			wdf->depth = ri->ri_depth;
1171 			wdf->cmsize = 256;
1172 			return 0;
1173 
1174 		case WSDISPLAYIO_GETCMAP:
1175 			return cgsix_getcmap(sc,
1176 			    (struct wsdisplay_cmap *)data);
1177 		case WSDISPLAYIO_PUTCMAP:
1178 			return cgsix_putcmap(sc,
1179 			    (struct wsdisplay_cmap *)data);
1180 
1181 		case WSDISPLAYIO_LINEBYTES:
1182 			*(u_int *)data = sc->sc_stride;
1183 			return 0;
1184 
1185 		case WSDISPLAYIO_SMODE:
1186 			{
1187 				int new_mode = *(int*)data;
1188 
1189 				if (new_mode != sc->sc_mode) {
1190 					sc->sc_mode = new_mode;
1191 					if (IS_IN_EMUL_MODE(sc)) {
1192 						cg6_reset(sc);
1193 						cg6_ras_init(sc);
1194 						cg6_setup_palette(sc);
1195 						glyphcache_wipe(&sc->sc_gc);
1196 						vcons_redraw_screen(ms);
1197 					}
1198 				}
1199 			}
1200 	}
1201 	return EPASSTHROUGH;
1202 }
1203 
1204 paddr_t
1205 cgsix_mmap(void *v, void *vs, off_t offset, int prot)
1206 {
1207 	struct vcons_data *vd = v;
1208 	struct cgsix_softc *sc = vd->cookie;
1209 
1210 	if (offset < sc->sc_ramsize) {
1211 		return bus_space_mmap(sc->sc_bustag, sc->sc_paddr,
1212 		    CGSIX_RAM_OFFSET + offset, prot, BUS_SPACE_MAP_LINEAR);
1213 	}
1214 	return -1;
1215 }
1216 
1217 int
1218 cgsix_putcmap(struct cgsix_softc *sc, struct wsdisplay_cmap *cm)
1219 {
1220 	u_int index = cm->index;
1221 	u_int count = cm->count;
1222 	int error, i;
1223 
1224 	if (index >= 256 || count > 256 || index + count > 256)
1225 		return EINVAL;
1226 
1227 	for (i = 0; i < count; i++)
1228 	{
1229 		error = copyin(&cm->red[i],
1230 		    &sc->sc_cmap.cm_map[index + i][0], 1);
1231 		if (error)
1232 			return error;
1233 		error = copyin(&cm->green[i],
1234 		    &sc->sc_cmap.cm_map[index + i][1],
1235 		    1);
1236 		if (error)
1237 			return error;
1238 		error = copyin(&cm->blue[i],
1239 		    &sc->sc_cmap.cm_map[index + i][2], 1);
1240 		if (error)
1241 			return error;
1242 	}
1243 	cg6_loadcmap(sc, index, count);
1244 
1245 	return 0;
1246 }
1247 
1248 int
1249 cgsix_getcmap(struct cgsix_softc *sc, struct wsdisplay_cmap *cm)
1250 {
1251 	u_int index = cm->index;
1252 	u_int count = cm->count;
1253 	int error,i;
1254 
1255 	if (index >= 256 || count > 256 || index + count > 256)
1256 		return EINVAL;
1257 
1258 	for (i = 0; i < count; i++)
1259 	{
1260 		error = copyout(&sc->sc_cmap.cm_map[index + i][0],
1261 		    &cm->red[i], 1);
1262 		if (error)
1263 			return error;
1264 		error = copyout(&sc->sc_cmap.cm_map[index + i][1],
1265 		    &cm->green[i], 1);
1266 		if (error)
1267 			return error;
1268 		error = copyout(&sc->sc_cmap.cm_map[index + i][2],
1269 		    &cm->blue[i], 1);
1270 		if (error)
1271 			return error;
1272 	}
1273 
1274 	return 0;
1275 }
1276 
1277 void
1278 cgsix_init_screen(void *cookie, struct vcons_screen *scr,
1279     int existing, long *defattr)
1280 {
1281 	struct cgsix_softc *sc = cookie;
1282 	struct rasops_info *ri = &scr->scr_ri;
1283 
1284 	ri->ri_depth = 8;
1285 	ri->ri_width = sc->sc_width;
1286 	ri->ri_height = sc->sc_height;
1287 	ri->ri_stride = sc->sc_stride;
1288 	ri->ri_flg = RI_CENTER | RI_ENABLE_ALPHA | RI_8BIT_IS_RGB;
1289 
1290 	ri->ri_bits = sc->sc_fb.fb_pixels;
1291 
1292 	/* We need unaccelerated initial screen clear on old revisions */
1293 	if (sc->sc_fhcrev < 2)
1294 		memset(sc->sc_fb.fb_pixels, (*defattr >> 16) & 0xff,
1295 		    sc->sc_stride * sc->sc_height);
1296 	rasops_init(ri, 0, 0);
1297 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
1298 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
1299 		    sc->sc_width / ri->ri_font->fontwidth);
1300 
1301 	/* enable acceleration */
1302 	ri->ri_hw = scr;
1303 	ri->ri_ops.copyrows = cg6_ras_copyrows;
1304 	ri->ri_ops.copycols = cg6_ras_copycols;
1305 	ri->ri_ops.eraserows = cg6_ras_eraserows;
1306 	ri->ri_ops.erasecols = cg6_ras_erasecols;
1307 	ri->ri_ops.cursor = cgsix_cursor;
1308 	if (FONT_IS_ALPHA(ri->ri_font)) {
1309 		ri->ri_ops.putchar = cgsix_putchar_aa;
1310 	} else
1311 		ri->ri_ops.putchar = cgsix_putchar;
1312 }
1313 
1314 void
1315 cgsix_rectfill(struct cgsix_softc *sc, int xs, int ys, int wi, int he,
1316     uint32_t col)
1317 {
1318 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
1319 
1320 	CG6_WAIT_READY(fbc);
1321 
1322 	fbc->fbc_alu = CG6_ALU_FILL;
1323 	fbc->fbc_mode = GX_BLIT_SRC | GX_MODE_COLOR8;
1324 
1325 	fbc->fbc_fg = col;
1326 	fbc->fbc_arecty = ys;
1327 	fbc->fbc_arectx = xs;
1328 	fbc->fbc_arecty = ys + he - 1;
1329 	fbc->fbc_arectx = xs + wi - 1;
1330 	CG6_DRAW(fbc);
1331 }
1332 
1333 void
1334 cgsix_bitblt(void *cookie, int xs, int ys, int xd, int yd,
1335     int wi, int he, int rop)
1336 {
1337 	struct cgsix_softc *sc = cookie;
1338 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
1339 	CG6_WAIT_READY(fbc);
1340 
1341 	fbc->fbc_alu = rop;
1342 	fbc->fbc_mode = GX_BLIT_SRC | GX_MODE_COLOR8;
1343 
1344 	fbc->fbc_x0 = xs;
1345 	fbc->fbc_y0 = ys;
1346 	fbc->fbc_x1 = xs + wi - 1;
1347 	fbc->fbc_y1 = ys + he - 1;
1348 	fbc->fbc_x2 = xd;
1349 	fbc->fbc_y2 = yd;
1350 	fbc->fbc_x3 = xd + wi - 1;
1351 	fbc->fbc_y3 = yd + he - 1;
1352 	CG6_BLIT(fbc);
1353 }
1354 
1355 void
1356 cgsix_setup_mono(struct cgsix_softc *sc, int x, int y, int wi, int he,
1357     uint32_t fg, uint32_t bg)
1358 {
1359 	volatile struct cg6_fbc *fbc=sc->sc_fbc;
1360 
1361 	CG6_WAIT_READY(fbc);
1362 
1363 	fbc->fbc_x0 = x;
1364 	fbc->fbc_x1 = x + wi - 1;
1365 	fbc->fbc_y0 = y;
1366 	fbc->fbc_incx = 0;
1367 	fbc->fbc_incy = 1;
1368 	fbc->fbc_fg = fg;
1369 	fbc->fbc_bg = bg;
1370 	fbc->fbc_mode = GX_BLIT_NOSRC | GX_MODE_COLOR1;
1371 	fbc->fbc_alu = GX_PATTERN_ONES | ROP_OSTP(GX_ROP_CLEAR, GX_ROP_SET);
1372 	sc->sc_mono_width = wi;
1373 	/* now feed the data into the chip */
1374 }
1375 
1376 void
1377 cgsix_feed_line(struct cgsix_softc *sc, int count, uint8_t *data)
1378 {
1379 	int i;
1380 	uint32_t latch, res = 0, shift;
1381 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
1382 
1383 	if (sc->sc_mono_width > 32) {
1384 		/* ARGH! */
1385 	} else
1386 	{
1387 		shift = 24;
1388 		for (i = 0; i < count; i++) {
1389 			latch = data[i];
1390 			res |= latch << shift;
1391 			shift -= 8;
1392 		}
1393 		fbc->fbc_font = res;
1394 	}
1395 }
1396 
1397 void
1398 cgsix_putchar(void *cookie, int row, int col, u_int c, long attr)
1399 {
1400 	struct rasops_info *ri = cookie;
1401 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1402 	struct vcons_screen *scr = ri->ri_hw;
1403 	struct cgsix_softc *sc = scr->scr_cookie;
1404 	int inv;
1405 
1406 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
1407 	    (col < ri->ri_cols)) {
1408 
1409 		if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1410 
1411 			int fg, bg, uc, i;
1412 			uint8_t *data;
1413 			int x, y, wi, he;
1414 			volatile struct cg6_fbc *fbc = sc->sc_fbc;
1415 
1416 			wi = font->fontwidth;
1417 			he = font->fontheight;
1418 
1419 			if (!CHAR_IN_FONT(c, font))
1420 				return;
1421 			inv = ((attr >> 8) & WSATTR_REVERSE);
1422 			if (inv) {
1423 				fg = (u_char)ri->ri_devcmap[(attr >> 16) &
1424 				    0xff];
1425 				bg = (u_char)ri->ri_devcmap[(attr >> 24) &
1426 				    0xff];
1427 			} else {
1428 				bg = (u_char)ri->ri_devcmap[(attr >> 16) &
1429 				    0xff];
1430 				fg = (u_char)ri->ri_devcmap[(attr >> 24) &
1431 				    0xff];
1432 			}
1433 
1434 			x = ri->ri_xorigin + col * wi;
1435 			y = ri->ri_yorigin + row * he;
1436 
1437 			if (c == 0x20) {
1438 				cgsix_rectfill(sc, x, y, wi, he, bg);
1439 			} else {
1440 				uc = c - font->firstchar;
1441 				data = (uint8_t *)font->data + uc *
1442 				    ri->ri_fontscale;
1443 
1444 				cgsix_setup_mono(sc, x, y, wi, 1, fg, bg);
1445 				for (i = 0; i < he; i++) {
1446 					cgsix_feed_line(sc, font->stride,
1447 					    data);
1448 					data += font->stride;
1449 				}
1450 				/* put the chip back to normal */
1451 				fbc->fbc_incy = 0;
1452 			}
1453 		}
1454 	}
1455 }
1456 
1457 void
1458 cgsix_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
1459 {
1460 	struct rasops_info *ri = cookie;
1461 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1462 	struct vcons_screen *scr = ri->ri_hw;
1463 	struct cgsix_softc *sc = scr->scr_cookie;
1464 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
1465 
1466 	uint32_t bg, latch = 0, bg8, fg8, pixel;
1467 	int i, j, shift, x, y, wi, he, r, g, b, aval;
1468 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
1469 	uint8_t *data8;
1470 	int rv;
1471 
1472 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1473 		return;
1474 
1475 	if (!CHAR_IN_FONT(c, font))
1476 		return;
1477 
1478 	wi = font->fontwidth;
1479 	he = font->fontheight;
1480 
1481 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1482 	x = ri->ri_xorigin + col * wi;
1483 	y = ri->ri_yorigin + row * he;
1484 	if (c == 0x20) {
1485 		cgsix_rectfill(sc, x, y, wi, he, bg);
1486 		return;
1487 	}
1488 
1489 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1490 	if (rv == GC_OK)
1491 		return;
1492 
1493 	data8 = WSFONT_GLYPH(c, font);
1494 
1495 	CG6_WAIT_READY(sc->sc_fbc);
1496 	fbc->fbc_incx = 4;
1497 	fbc->fbc_incy = 0;
1498 	fbc->fbc_mode = GX_BLIT_NOSRC | GX_MODE_COLOR8;
1499 	fbc->fbc_alu = CG6_ALU_COPY;
1500 	fbc->fbc_clipmaxx = x + wi - 1;
1501 
1502 	/*
1503 	 * we need the RGB colours here, so get offsets into rasops_cmap
1504 	 */
1505 	fgo = ((attr >> 24) & 0xf) * 3;
1506 	bgo = ((attr >> 16) & 0xf) * 3;
1507 
1508 	r0 = rasops_cmap[bgo];
1509 	r1 = rasops_cmap[fgo];
1510 	g0 = rasops_cmap[bgo + 1];
1511 	g1 = rasops_cmap[fgo + 1];
1512 	b0 = rasops_cmap[bgo + 2];
1513 	b1 = rasops_cmap[fgo + 2];
1514 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
1515 	bg8 = R3G3B2(r0, g0, b0);
1516 	fg8 = R3G3B2(r1, g1, b1);
1517 
1518 	for (i = 0; i < he; i++) {
1519 
1520 		CG6_WAIT_READY(fbc);
1521 		fbc->fbc_x0 = x;
1522 		fbc->fbc_x1 = x + 3;
1523 		fbc->fbc_y0 = y + i;
1524 
1525 		shift = 24;
1526 		for (j = 0; j < wi; j++) {
1527 			aval = *data8;
1528 			if (aval == 0) {
1529 				pixel = bg8;
1530 			} else if (aval == 255) {
1531 				pixel = fg8;
1532 			} else {
1533 				r = aval * r1 + (255 - aval) * r0;
1534 				g = aval * g1 + (255 - aval) * g0;
1535 				b = aval * b1 + (255 - aval) * b0;
1536 				pixel = ((r & 0xe000) >> 8) |
1537 					((g & 0xe000) >> 11) |
1538 					((b & 0xc000) >> 14);
1539 			}
1540 			data8++;
1541 
1542 			latch |= pixel << shift;
1543 			if (shift == 0) {
1544 				fbc->fbc_font = latch;
1545 				latch = 0;
1546 				shift = 24;
1547 			} else
1548 				shift -= 8;
1549 		}
1550 		if (shift != 24)
1551 			fbc->fbc_font = latch;
1552 	}
1553 	fbc->fbc_clipmaxx = 0x3fff;
1554 
1555 	if (rv == GC_ADD) {
1556 		glyphcache_add(&sc->sc_gc, c, x, y);
1557 	}
1558 }
1559 
1560 void
1561 cgsix_cursor(void *cookie, int on, int row, int col)
1562 {
1563 	struct rasops_info *ri = cookie;
1564 	struct vcons_screen *scr = ri->ri_hw;
1565 	struct cgsix_softc *sc = scr->scr_cookie;
1566 	int x, y, wi, he;
1567 
1568 	wi = ri->ri_font->fontwidth;
1569 	he = ri->ri_font->fontheight;
1570 
1571 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1572 		x = ri->ri_ccol * wi + ri->ri_xorigin;
1573 		y = ri->ri_crow * he + ri->ri_yorigin;
1574 		if (ri->ri_flg & RI_CURSOR) {
1575 			cg6_invert(sc, x, y, wi, he);
1576 			ri->ri_flg &= ~RI_CURSOR;
1577 		}
1578 		ri->ri_crow = row;
1579 		ri->ri_ccol = col;
1580 		if (on)
1581 		{
1582 			x = ri->ri_ccol * wi + ri->ri_xorigin;
1583 			y = ri->ri_crow * he + ri->ri_yorigin;
1584 			cg6_invert(sc, x, y, wi, he);
1585 			ri->ri_flg |= RI_CURSOR;
1586 		}
1587 	} else
1588 	{
1589 		ri->ri_crow = row;
1590 		ri->ri_ccol = col;
1591 		ri->ri_flg &= ~RI_CURSOR;
1592 	}
1593 }
1594 
1595 void
1596 cgsix_clearscreen(struct cgsix_softc *sc)
1597 {
1598 	struct rasops_info *ri = &cg6_console_screen.scr_ri;
1599 
1600 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1601 		volatile struct cg6_fbc *fbc = sc->sc_fbc;
1602 
1603 		CG6_WAIT_READY(fbc);
1604 
1605 		fbc->fbc_alu = CG6_ALU_FILL;
1606 		fbc->fbc_mode = GX_BLIT_SRC | GX_MODE_COLOR8;
1607 
1608 		fbc->fbc_fg = ri->ri_devcmap[sc->sc_bg];
1609 		fbc->fbc_arectx = 0;
1610 		fbc->fbc_arecty = 0;
1611 		fbc->fbc_arectx = ri->ri_width - 1;
1612 		fbc->fbc_arecty = ri->ri_height - 1;
1613 		CG6_DRAW(fbc);
1614 	}
1615 }
1616 
1617 #endif /* NWSDISPLAY > 0 */
1618 
1619 #if (NWSDISPLAY > 0) || defined(RASTERCONSOLE)
1620 void
1621 cg6_invert(struct cgsix_softc *sc, int x, int y, int wi, int he)
1622 {
1623 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
1624 
1625 	CG6_WAIT_READY(fbc);
1626 
1627 	fbc->fbc_alu = CG6_ALU_FLIP;
1628 	fbc->fbc_mode = GX_BLIT_SRC | GX_MODE_COLOR8;
1629 	fbc->fbc_arecty = y;
1630 	fbc->fbc_arectx = x;
1631 	fbc->fbc_arecty = y + he - 1;
1632 	fbc->fbc_arectx = x + wi - 1;
1633 	CG6_DRAW(fbc);
1634 }
1635 
1636 #endif
1637 
1638