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