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