xref: /netbsd-src/sys/dev/sun/cgsix.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: cgsix.c,v 1.5 2001/01/10 13:26:52 tsutsui 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * This software was developed by the Computer Systems Engineering group
44  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
45  * contributed to Berkeley.
46  *
47  * All advertising materials mentioning features or use of this software
48  * must display the following acknowledgement:
49  *	This product includes software developed by the University of
50  *	California, Lawrence Berkeley Laboratory.
51  *
52  * Redistribution and use in source and binary forms, with or without
53  * modification, are permitted provided that the following conditions
54  * are met:
55  * 1. Redistributions of source code must retain the above copyright
56  *    notice, this list of conditions and the following disclaimer.
57  * 2. Redistributions in binary form must reproduce the above copyright
58  *    notice, this list of conditions and the following disclaimer in the
59  *    documentation and/or other materials provided with the distribution.
60  * 3. All advertising materials mentioning features or use of this software
61  *    must display the following acknowledgement:
62  *	This product includes software developed by the University of
63  *	California, Berkeley and its contributors.
64  * 4. Neither the name of the University nor the names of its contributors
65  *    may be used to endorse or promote products derived from this software
66  *    without specific prior written permission.
67  *
68  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78  * SUCH DAMAGE.
79  *
80  *	@(#)cgsix.c	8.4 (Berkeley) 1/21/94
81  */
82 
83 /*
84  * color display (cgsix) driver.
85  *
86  * Does not handle interrupts, even though they can occur.
87  *
88  * XXX should defer colormap updates to vertical retrace interrupts
89  */
90 
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/buf.h>
94 #include <sys/device.h>
95 #include <sys/ioctl.h>
96 #include <sys/malloc.h>
97 #include <sys/mman.h>
98 #include <sys/tty.h>
99 #include <sys/conf.h>
100 
101 #ifdef DEBUG
102 #include <sys/proc.h>
103 #include <sys/syslog.h>
104 #endif
105 
106 #include <uvm/uvm_extern.h>
107 
108 #include <machine/bus.h>
109 
110 #include <dev/sun/fbio.h>
111 #include <dev/sun/fbvar.h>
112 
113 #include <dev/sun/btreg.h>
114 #include <dev/sun/btvar.h>
115 #include <dev/sun/cgsixreg.h>
116 #include <dev/sun/cgsixvar.h>
117 #include <dev/sun/pfourreg.h>
118 
119 #ifdef RASTERCONSOLE
120 #include <dev/rasops/rasops.h>
121 #include <dev/wscons/wsconsio.h>
122 #endif
123 
124 #include <machine/conf.h>
125 
126 static void	cg6_unblank(struct device *);
127 
128 /* cdevsw prototypes */
129 cdev_decl(cgsix);
130 
131 extern struct cfdriver cgsix_cd;
132 
133 /* frame buffer generic driver */
134 static struct fbdriver cg6_fbdriver = {
135 	cg6_unblank, cgsixopen, cgsixclose, cgsixioctl, cgsixpoll, cgsixmmap
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 #ifdef RASTERCONSOLE
145 int cgsix_use_rasterconsole = 1;
146 
147 /*
148  * cg6 accelerated console routines.
149  *
150  * Note that buried in this code in several places is the assumption
151  * that pixels are exactly one byte wide.  Since this is cg6-specific
152  * code, this seems safe.  This assumption resides in things like the
153  * use of ri_emuwidth without messing around with ri_pelbytes, or the
154  * assumption that ri_font->fontwidth is the right thing to multiply
155  * character-cell counts by to get byte counts.
156  */
157 
158 /*
159  * Magic values for blitter
160  */
161 
162 /* Values for the mode register */
163 #define CG6_MODE	(						\
164 	  0x00200000 /* GX_BLIT_SRC */					\
165 	| 0x00020000 /* GX_MODE_COLOR8 */				\
166 	| 0x00008000 /* GX_DRAW_RENDER */				\
167 	| 0x00002000 /* GX_BWRITE0_ENABLE */				\
168 	| 0x00001000 /* GX_BWRITE1_DISABLE */				\
169 	| 0x00000200 /* GX_BREAD_0 */					\
170 	| 0x00000080 /* GX_BDISP_0 */					\
171 )
172 #define CG6_MODE_MASK	(						\
173 	  0x00300000 /* GX_BLIT_ALL */					\
174 	| 0x00060000 /* GX_MODE_ALL */					\
175 	| 0x00018000 /* GX_DRAW_ALL */					\
176 	| 0x00006000 /* GX_BWRITE0_ALL */				\
177 	| 0x00001800 /* GX_BWRITE1_ALL */				\
178 	| 0x00000600 /* GX_BREAD_ALL */					\
179 	| 0x00000180 /* GX_BDISP_ALL */					\
180 )
181 
182 /* Value for the alu register for screen-to-screen copies */
183 #define CG6_ALU_COPY	(						\
184 	  0x80000000 /* GX_PLANE_ONES (ignore planemask register) */	\
185 	| 0x20000000 /* GX_PIXEL_ONES (ignore pixelmask register) */	\
186 	| 0x00800000 /* GX_ATTR_SUPP (function unknown) */		\
187 	| 0x00000000 /* GX_RAST_BOOL (function unknown) */		\
188 	| 0x00000000 /* GX_PLOT_PLOT (function unknown) */		\
189 	| 0x08000000 /* GX_PATTERN_ONES (ignore pattern) */		\
190 	| 0x01000000 /* GX_POLYG_OVERLAP (unsure - handle overlap?) */	\
191 	| 0x0000cccc /* ALU = src */					\
192 )
193 
194 /* Value for the alu register for region fills */
195 #define CG6_ALU_FILL	(						\
196 	  0x80000000 /* GX_PLANE_ONES (ignore planemask register) */	\
197 	| 0x20000000 /* GX_PIXEL_ONES (ignore pixelmask register) */	\
198 	| 0x00800000 /* GX_ATTR_SUPP (function unknown) */		\
199 	| 0x00000000 /* GX_RAST_BOOL (function unknown) */		\
200 	| 0x00000000 /* GX_PLOT_PLOT (function unknown) */		\
201 	| 0x08000000 /* GX_PATTERN_ONES (ignore pattern) */		\
202 	| 0x01000000 /* GX_POLYG_OVERLAP (unsure - handle overlap?) */	\
203 	| 0x0000ff00 /* ALU = fg color */				\
204 )
205 
206 /* Value for the alu register for toggling an area */
207 #define CG6_ALU_FLIP	(						\
208 	  0x80000000 /* GX_PLANE_ONES (ignore planemask register) */	\
209 	| 0x20000000 /* GX_PIXEL_ONES (ignore pixelmask register) */	\
210 	| 0x00800000 /* GX_ATTR_SUPP (function unknown) */		\
211 	| 0x00000000 /* GX_RAST_BOOL (function unknown) */		\
212 	| 0x00000000 /* GX_PLOT_PLOT (function unknown) */		\
213 	| 0x08000000 /* GX_PATTERN_ONES (ignore pattern) */		\
214 	| 0x01000000 /* GX_POLYG_OVERLAP (unsure - handle overlap?) */	\
215 	| 0x00005555 /* ALU = ~dst */					\
216 )
217 
218 /*
219  * Wait for a blit to finish.
220  * 0x8000000 bit: function unknown; 0x20000000 bit: GX_BLT_INPROGRESS
221  */
222 #define CG6_BLIT_WAIT(fbc) do {						\
223 	while (((fbc)->fbc_blit & 0xa0000000) == 0xa0000000)		\
224 		/*EMPTY*/;						\
225 } while (0)
226 
227 /*
228  * Wait for a drawing operation to finish, or at least get queued.
229  * 0x8000000 bit: function unknown; 0x20000000 bit: GX_FULL
230  */
231 #define CG6_DRAW_WAIT(fbc) do {						\
232        	while (((fbc)->fbc_draw & 0xa0000000) == 0xa0000000)		\
233 		/*EMPTY*/;						\
234 } while (0)
235 
236 /*
237  * Wait for the whole engine to go idle.  This may not matter in our case;
238  * I'm not sure whether blits are actually queued or not.  It more likely
239  * is intended for lines and such that do get queued.
240  * 0x10000000 bit: GX_INPROGRESS
241  */
242 #define CG6_DRAIN(fbc) do {						\
243 	while ((fbc)->fbc_s & 0x10000000)				\
244 		/*EMPTY*/;						\
245 } while (0)
246 
247 static void cg6_ras_init(struct cgsix_softc *);
248 static void cg6_ras_copyrows(void *, int, int, int);
249 static void cg6_ras_copycols(void *, int, int, int, int);
250 static void cg6_ras_erasecols(void *, int, int, int, long int);
251 static void cg6_ras_eraserows(void *, int, int, long int);
252 static void cg6_ras_do_cursor(struct rasops_info *);
253 
254 static void
255 cg6_ras_init(struct cgsix_softc *sc)
256 {
257 	volatile struct cg6_fbc *fbc = sc->sc_fbc;
258 
259 	CG6_DRAIN(fbc);
260 	fbc->fbc_mode &= ~CG6_MODE_MASK;
261 	fbc->fbc_mode |= CG6_MODE;
262 }
263 
264 static void
265 cg6_ras_copyrows(void *cookie, int src, int dst, int n)
266 {
267 	struct rasops_info *ri;
268 	volatile struct cg6_fbc *fbc;
269 
270 	ri = cookie;
271 	if (dst == src)
272 		return;
273 	if (src < 0) {
274 		n += src;
275 		src = 0;
276 	}
277 	if (src+n > ri->ri_rows)
278 		n = ri->ri_rows - src;
279 	if (dst < 0) {
280 		n += dst;
281 		dst = 0;
282 	}
283 	if (dst+n > ri->ri_rows)
284 		n = ri->ri_rows - dst;
285 	if (n <= 0)
286 		return;
287 	n *= ri->ri_font->fontheight;
288 	src *= ri->ri_font->fontheight;
289 	dst *= ri->ri_font->fontheight;
290 	fbc = ((struct cgsix_softc *)ri->ri_hw)->sc_fbc;
291 	fbc->fbc_clip = 0;
292 	fbc->fbc_s = 0;
293 	fbc->fbc_offx = 0;
294 	fbc->fbc_offy = 0;
295 	fbc->fbc_clipminx = 0;
296 	fbc->fbc_clipminy = 0;
297 	fbc->fbc_clipmaxx = ri->ri_width - 1;
298 	fbc->fbc_clipmaxy = ri->ri_height - 1;
299 	fbc->fbc_alu = CG6_ALU_COPY;
300 	fbc->fbc_x0 = ri->ri_xorigin;
301 	fbc->fbc_y0 = ri->ri_yorigin + src;
302 	fbc->fbc_x1 = ri->ri_xorigin + ri->ri_emuwidth - 1;
303 	fbc->fbc_y1 = ri->ri_yorigin + src + n - 1;
304 	fbc->fbc_x2 = ri->ri_xorigin;
305 	fbc->fbc_y2 = ri->ri_yorigin + dst;
306 	fbc->fbc_x3 = ri->ri_xorigin + ri->ri_emuwidth - 1;
307 	fbc->fbc_y3 = ri->ri_yorigin + dst + n - 1;
308 	CG6_BLIT_WAIT(fbc);
309 	CG6_DRAIN(fbc);
310 }
311 
312 static void
313 cg6_ras_copycols(void *cookie, int row, int src, int dst, int n)
314 {
315 	struct rasops_info *ri;
316 	volatile struct cg6_fbc *fbc;
317 
318 	ri = cookie;
319 	if (dst == src)
320 		return;
321 	if ((row < 0) || (row >= ri->ri_rows))
322 		return;
323 	if (src < 0) {
324 		n += src;
325 		src = 0;
326 	}
327 	if (src+n > ri->ri_cols)
328 		n = ri->ri_cols - src;
329 	if (dst < 0) {
330 		n += dst;
331 		dst = 0;
332 	}
333 	if (dst+n > ri->ri_cols)
334 		n = ri->ri_cols - dst;
335 	if (n <= 0)
336 		return;
337 	n *= ri->ri_font->fontwidth;
338 	src *= ri->ri_font->fontwidth;
339 	dst *= ri->ri_font->fontwidth;
340 	row *= ri->ri_font->fontheight;
341 	fbc = ((struct cgsix_softc *)ri->ri_hw)->sc_fbc;
342 	fbc->fbc_clip = 0;
343 	fbc->fbc_s = 0;
344 	fbc->fbc_offx = 0;
345 	fbc->fbc_offy = 0;
346 	fbc->fbc_clipminx = 0;
347 	fbc->fbc_clipminy = 0;
348 	fbc->fbc_clipmaxx = ri->ri_width - 1;
349 	fbc->fbc_clipmaxy = ri->ri_height - 1;
350 	fbc->fbc_alu = CG6_ALU_COPY;
351 	fbc->fbc_x0 = ri->ri_xorigin + src;
352 	fbc->fbc_y0 = ri->ri_yorigin + row;
353 	fbc->fbc_x1 = ri->ri_xorigin + src + n - 1;
354 	fbc->fbc_y1 = ri->ri_yorigin + row + ri->ri_font->fontheight - 1;
355 	fbc->fbc_x2 = ri->ri_xorigin + dst;
356 	fbc->fbc_y2 = ri->ri_yorigin + row;
357 	fbc->fbc_x3 = ri->ri_xorigin + dst + n - 1;
358 	fbc->fbc_y3 = ri->ri_yorigin + row + ri->ri_font->fontheight - 1;
359 	CG6_BLIT_WAIT(fbc);
360 	CG6_DRAIN(fbc);
361 }
362 
363 static void
364 cg6_ras_erasecols(void *cookie, int row, int col, int n, long int attr)
365 {
366 	struct rasops_info *ri;
367 	volatile struct cg6_fbc *fbc;
368 
369 	ri = cookie;
370 	if ((row < 0) || (row >= ri->ri_rows))
371 		return;
372 	if (col < 0) {
373 		n += col;
374 		col = 0;
375 	}
376 	if (col+n > ri->ri_cols)
377 		n = ri->ri_cols - col;
378 	if (n <= 0)
379 		return;
380 	n *= ri->ri_font->fontwidth;
381 	col *= ri->ri_font->fontwidth;
382 	row *= ri->ri_font->fontheight;
383 	fbc = ((struct cgsix_softc *)ri->ri_hw)->sc_fbc;
384 	fbc->fbc_clip = 0;
385 	fbc->fbc_s = 0;
386 	fbc->fbc_offx = 0;
387 	fbc->fbc_offy = 0;
388 	fbc->fbc_clipminx = 0;
389 	fbc->fbc_clipminy = 0;
390 	fbc->fbc_clipmaxx = ri->ri_width - 1;
391 	fbc->fbc_clipmaxy = ri->ri_height - 1;
392 	fbc->fbc_alu = CG6_ALU_FILL;
393 	fbc->fbc_fg = ri->ri_devcmap[(attr >> 16) & 0xf];
394 	fbc->fbc_arecty = ri->ri_yorigin + row;
395 	fbc->fbc_arectx = ri->ri_xorigin + col;
396 	fbc->fbc_arecty = ri->ri_yorigin + row + ri->ri_font->fontheight - 1;
397 	fbc->fbc_arectx = ri->ri_xorigin + col + n - 1;
398 	CG6_DRAW_WAIT(fbc);
399 	CG6_DRAIN(fbc);
400 }
401 
402 static void
403 cg6_ras_eraserows(void *cookie, int row, int n, long int attr)
404 {
405 	struct rasops_info *ri;
406 	volatile struct cg6_fbc *fbc;
407 
408 	ri = cookie;
409 	if (row < 0) {
410 		n += row;
411 		row = 0;
412 	}
413 	if (row+n > ri->ri_rows)
414 		n = ri->ri_rows - row;
415 	if (n <= 0)
416 		return;
417 	fbc = ((struct cgsix_softc *)ri->ri_hw)->sc_fbc;
418 	fbc->fbc_clip = 0;
419 	fbc->fbc_s = 0;
420 	fbc->fbc_offx = 0;
421 	fbc->fbc_offy = 0;
422 	fbc->fbc_clipminx = 0;
423 	fbc->fbc_clipminy = 0;
424 	fbc->fbc_clipmaxx = ri->ri_width - 1;
425 	fbc->fbc_clipmaxy = ri->ri_height - 1;
426 	fbc->fbc_alu = CG6_ALU_FILL;
427 	fbc->fbc_fg = ri->ri_devcmap[(attr >> 16) & 0xf];
428 	if ((n == ri->ri_rows) && (ri->ri_flg & RI_FULLCLEAR)) {
429 		fbc->fbc_arecty = 0;
430 		fbc->fbc_arectx = 0;
431 		fbc->fbc_arecty = ri->ri_height - 1;
432 		fbc->fbc_arectx = ri->ri_width - 1;
433 	} else {
434 		row *= ri->ri_font->fontheight;
435 		fbc->fbc_arecty = ri->ri_yorigin + row;
436 		fbc->fbc_arectx = ri->ri_xorigin;
437 		fbc->fbc_arecty = ri->ri_yorigin + row + (n * ri->ri_font->fontheight) - 1;
438 		fbc->fbc_arectx = ri->ri_xorigin + ri->ri_emuwidth - 1;
439 	}
440 	CG6_DRAW_WAIT(fbc);
441 	CG6_DRAIN(fbc);
442 }
443 
444 /*
445  * Really want something more like fg^bg here, but that would be more
446  * or less impossible to migrate to colors.  So we hope there's
447  * something not too inappropriate in the colormap...besides, it's what
448  * the non-accelerated code did. :-)
449  */
450 static void
451 cg6_ras_do_cursor(struct rasops_info *ri)
452 {
453 	volatile struct cg6_fbc *fbc;
454 	int row;
455 	int col;
456 
457 	row = ri->ri_crow * ri->ri_font->fontheight;
458 	col = ri->ri_ccol * ri->ri_font->fontwidth;
459 	fbc = ((struct cgsix_softc *)ri->ri_hw)->sc_fbc;
460 	fbc->fbc_clip = 0;
461 	fbc->fbc_s = 0;
462 	fbc->fbc_offx = 0;
463 	fbc->fbc_offy = 0;
464 	fbc->fbc_clipminx = 0;
465 	fbc->fbc_clipminy = 0;
466 	fbc->fbc_clipmaxx = ri->ri_width - 1;
467 	fbc->fbc_clipmaxy = ri->ri_height - 1;
468 	fbc->fbc_alu = CG6_ALU_FLIP;
469 	fbc->fbc_arecty = ri->ri_yorigin + row;
470 	fbc->fbc_arectx = ri->ri_xorigin + col;
471 	fbc->fbc_arecty = ri->ri_yorigin + row + ri->ri_font->fontheight - 1;
472 	fbc->fbc_arectx = ri->ri_xorigin + col + ri->ri_font->fontwidth - 1;
473 	CG6_DRAW_WAIT(fbc);
474 	CG6_DRAIN(fbc);
475 }
476 #endif /* RASTERCONSOLE */
477 
478 void
479 cg6attach(sc, name, isconsole)
480 	struct cgsix_softc *sc;
481 	char *name;
482 	int isconsole;
483 {
484 	struct fbdevice *fb = &sc->sc_fb;
485 
486 	fb->fb_driver = &cg6_fbdriver;
487 
488 	/* Don't have to map the pfour register on the cgsix. */
489 	fb->fb_pfour = NULL;
490 
491 	fb->fb_type.fb_cmsize = 256;
492 	fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
493 	printf(": %s, %d x %d", name,
494 	       fb->fb_type.fb_width, fb->fb_type.fb_height);
495 
496 	sc->sc_fhcrev = (*sc->sc_fhc >> FHC_REV_SHIFT) &
497 			(FHC_REV_MASK >> FHC_REV_SHIFT);
498 
499 	printf(", rev %d", sc->sc_fhcrev);
500 
501 	/* reset cursor & frame buffer controls */
502 	cg6_reset(sc);
503 
504 	/* enable video */
505 	sc->sc_thc->thc_misc |= THC_MISC_VIDEN;
506 
507 	if (isconsole) {
508 		printf(" (console)");
509 #ifdef RASTERCONSOLE
510 		if (cgsix_use_rasterconsole) {
511 			fbrcons_init(&sc->sc_fb);
512 			sc->sc_fb.fb_rinfo.ri_hw = sc;
513 			sc->sc_fb.fb_rinfo.ri_ops.copyrows = cg6_ras_copyrows;
514 			sc->sc_fb.fb_rinfo.ri_ops.copycols = cg6_ras_copycols;
515 			sc->sc_fb.fb_rinfo.ri_ops.erasecols = cg6_ras_erasecols;
516 			sc->sc_fb.fb_rinfo.ri_ops.eraserows = cg6_ras_eraserows;
517 			sc->sc_fb.fb_rinfo.ri_do_cursor = cg6_ras_do_cursor;
518 			cg6_ras_init(sc);
519 		}
520 #endif
521 	}
522 
523 	printf("\n");
524 	fb_attach(&sc->sc_fb, isconsole);
525 }
526 
527 
528 int
529 cgsixopen(dev, flags, mode, p)
530 	dev_t dev;
531 	int flags, mode;
532 	struct proc *p;
533 {
534 	int unit = minor(dev);
535 
536 	if (unit >= cgsix_cd.cd_ndevs || cgsix_cd.cd_devs[unit] == NULL)
537 		return (ENXIO);
538 	return (0);
539 }
540 
541 int
542 cgsixclose(dev, flags, mode, p)
543 	dev_t dev;
544 	int flags, mode;
545 	struct proc *p;
546 {
547 	struct cgsix_softc *sc = cgsix_cd.cd_devs[minor(dev)];
548 
549 	cg6_reset(sc);
550 
551 	/* (re-)initialize the default color map */
552 	bt_initcmap(&sc->sc_cmap, 256);
553 	cg6_loadcmap(sc, 0, 256);
554 
555 	return (0);
556 }
557 
558 int
559 cgsixioctl(dev, cmd, data, flags, p)
560 	dev_t dev;
561 	u_long cmd;
562 	caddr_t data;
563 	int flags;
564 	struct proc *p;
565 {
566 	struct cgsix_softc *sc = cgsix_cd.cd_devs[minor(dev)];
567 	u_int count;
568 	int v, error;
569 	union cursor_cmap tcm;
570 
571 	switch (cmd) {
572 
573 	case FBIOGTYPE:
574 		*(struct fbtype *)data = sc->sc_fb.fb_type;
575 		break;
576 
577 	case FBIOGATTR:
578 #define fba ((struct fbgattr *)data)
579 		fba->real_type = sc->sc_fb.fb_type.fb_type;
580 		fba->owner = 0;		/* XXX ??? */
581 		fba->fbtype = sc->sc_fb.fb_type;
582 		fba->sattr.flags = 0;
583 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
584 		fba->sattr.dev_specific[0] = -1;
585 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
586 		fba->emu_types[1] = -1;
587 #undef fba
588 		break;
589 
590 	case FBIOGETCMAP:
591 #define	p ((struct fbcmap *)data)
592 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
593 
594 	case FBIOPUTCMAP:
595 		/* copy to software map */
596 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
597 		if (error)
598 			return (error);
599 		/* now blast them into the chip */
600 		/* XXX should use retrace interrupt */
601 		cg6_loadcmap(sc, p->index, p->count);
602 #undef p
603 		break;
604 
605 	case FBIOGVIDEO:
606 		*(int *)data = sc->sc_blanked;
607 		break;
608 
609 	case FBIOSVIDEO:
610 		if (*(int *)data)
611 			cg6_unblank(&sc->sc_dev);
612 		else if (!sc->sc_blanked) {
613 			sc->sc_blanked = 1;
614 			sc->sc_thc->thc_misc &= ~THC_MISC_VIDEN;
615 		}
616 		break;
617 
618 /* these are for both FBIOSCURSOR and FBIOGCURSOR */
619 #define p ((struct fbcursor *)data)
620 #define cc (&sc->sc_cursor)
621 
622 	case FBIOGCURSOR:
623 		/* do not quite want everything here... */
624 		p->set = FB_CUR_SETALL;	/* close enough, anyway */
625 		p->enable = cc->cc_enable;
626 		p->pos = cc->cc_pos;
627 		p->hot = cc->cc_hot;
628 		p->size = cc->cc_size;
629 
630 		/* begin ugh ... can we lose some of this crap?? */
631 		if (p->image != NULL) {
632 			count = cc->cc_size.y * 32 / NBBY;
633 			error = copyout((caddr_t)cc->cc_bits[1],
634 			    (caddr_t)p->image, count);
635 			if (error)
636 				return (error);
637 			error = copyout((caddr_t)cc->cc_bits[0],
638 			    (caddr_t)p->mask, count);
639 			if (error)
640 				return (error);
641 		}
642 		if (p->cmap.red != NULL) {
643 			error = bt_getcmap(&p->cmap,
644 			    (union bt_cmap *)&cc->cc_color, 2, 1);
645 			if (error)
646 				return (error);
647 		} else {
648 			p->cmap.index = 0;
649 			p->cmap.count = 2;
650 		}
651 		/* end ugh */
652 		break;
653 
654 	case FBIOSCURSOR:
655 		/*
656 		 * For setcmap and setshape, verify parameters, so that
657 		 * we do not get halfway through an update and then crap
658 		 * out with the software state screwed up.
659 		 */
660 		v = p->set;
661 		if (v & FB_CUR_SETCMAP) {
662 			/*
663 			 * This use of a temporary copy of the cursor
664 			 * colormap is not terribly efficient, but these
665 			 * copies are small (8 bytes)...
666 			 */
667 			tcm = cc->cc_color;
668 			error = bt_putcmap(&p->cmap, (union bt_cmap *)&tcm, 2, 1);
669 			if (error)
670 				return (error);
671 		}
672 		if (v & FB_CUR_SETSHAPE) {
673 			if ((u_int)p->size.x > 32 || (u_int)p->size.y > 32)
674 				return (EINVAL);
675 			count = p->size.y * 32 / NBBY;
676 			if (!uvm_useracc(p->image, count, B_READ) ||
677 			    !uvm_useracc(p->mask, count, B_READ))
678 				return (EFAULT);
679 		}
680 
681 		/* parameters are OK; do it */
682 		if (v & (FB_CUR_SETCUR | FB_CUR_SETPOS | FB_CUR_SETHOT)) {
683 			if (v & FB_CUR_SETCUR)
684 				cc->cc_enable = p->enable;
685 			if (v & FB_CUR_SETPOS)
686 				cc->cc_pos = p->pos;
687 			if (v & FB_CUR_SETHOT)
688 				cc->cc_hot = p->hot;
689 			cg6_setcursor(sc);
690 		}
691 		if (v & FB_CUR_SETCMAP) {
692 			cc->cc_color = tcm;
693 			cg6_loadomap(sc); /* XXX defer to vertical retrace */
694 		}
695 		if (v & FB_CUR_SETSHAPE) {
696 			cc->cc_size = p->size;
697 			count = p->size.y * 32 / NBBY;
698 			bzero((caddr_t)cc->cc_bits, sizeof cc->cc_bits);
699 			copyin(p->mask, (caddr_t)cc->cc_bits[0], count);
700 			copyin(p->image, (caddr_t)cc->cc_bits[1], count);
701 			cg6_loadcursor(sc);
702 		}
703 		break;
704 
705 #undef p
706 #undef cc
707 
708 	case FBIOGCURPOS:
709 		*(struct fbcurpos *)data = sc->sc_cursor.cc_pos;
710 		break;
711 
712 	case FBIOSCURPOS:
713 		sc->sc_cursor.cc_pos = *(struct fbcurpos *)data;
714 		cg6_setcursor(sc);
715 		break;
716 
717 	case FBIOGCURMAX:
718 		/* max cursor size is 32x32 */
719 		((struct fbcurpos *)data)->x = 32;
720 		((struct fbcurpos *)data)->y = 32;
721 		break;
722 
723 	default:
724 #ifdef DEBUG
725 		log(LOG_NOTICE, "cgsixioctl(0x%lx) (%s[%d])\n", cmd,
726 		    p->p_comm, p->p_pid);
727 #endif
728 		return (ENOTTY);
729 	}
730 	return (0);
731 }
732 
733 int
734 cgsixpoll(dev, events, p)
735 	dev_t dev;
736 	int events;
737 	struct proc *p;
738 {
739 
740 	return (seltrue(dev, events, p));
741 }
742 
743 /*
744  * Clean up hardware state (e.g., after bootup or after X crashes).
745  */
746 static void
747 cg6_reset(sc)
748 	struct cgsix_softc *sc;
749 {
750 	volatile struct cg6_tec_xxx *tec;
751 	int fhc;
752 	volatile struct bt_regs *bt;
753 
754 	/* hide the cursor, just in case */
755 	sc->sc_thc->thc_cursxy = (THC_CURSOFF << 16) | THC_CURSOFF;
756 
757 	/* turn off frobs in transform engine (makes X11 work) */
758 	tec = sc->sc_tec;
759 	tec->tec_mv = 0;
760 	tec->tec_clip = 0;
761 	tec->tec_vdc = 0;
762 
763 	/* take care of hardware bugs in old revisions */
764 	if (sc->sc_fhcrev < 5) {
765 		/*
766 		 * Keep current resolution; set cpu to 68020, set test
767 		 * window (size 1Kx1K), and for rev 1, disable dest cache.
768 		 */
769 		fhc = (*sc->sc_fhc & FHC_RES_MASK) | FHC_CPU_68020 |
770 		    FHC_TEST |
771 		    (11 << FHC_TESTX_SHIFT) | (11 << FHC_TESTY_SHIFT);
772 		if (sc->sc_fhcrev < 2)
773 			fhc |= FHC_DST_DISABLE;
774 		*sc->sc_fhc = fhc;
775 	}
776 
777 	/* Enable cursor in Brooktree DAC. */
778 	bt = sc->sc_bt;
779 	bt->bt_addr = 0x06 << 24;
780 	bt->bt_ctrl |= 0x03 << 24;
781 }
782 
783 static void
784 cg6_setcursor(sc)
785 	struct cgsix_softc *sc;
786 {
787 
788 	/* we need to subtract the hot-spot value here */
789 #define COORD(f) (sc->sc_cursor.cc_pos.f - sc->sc_cursor.cc_hot.f)
790 	sc->sc_thc->thc_cursxy = sc->sc_cursor.cc_enable ?
791 	    ((COORD(x) << 16) | (COORD(y) & 0xffff)) :
792 	    (THC_CURSOFF << 16) | THC_CURSOFF;
793 #undef COORD
794 }
795 
796 static void
797 cg6_loadcursor(sc)
798 	struct cgsix_softc *sc;
799 {
800 	volatile struct cg6_thc *thc;
801 	u_int edgemask, m;
802 	int i;
803 
804 	/*
805 	 * Keep the top size.x bits.  Here we *throw out* the top
806 	 * size.x bits from an all-one-bits word, introducing zeros in
807 	 * the top size.x bits, then invert all the bits to get what
808 	 * we really wanted as our mask.  But this fails if size.x is
809 	 * 32---a sparc uses only the low 5 bits of the shift count---
810 	 * so we have to special case that.
811 	 */
812 	edgemask = ~0;
813 	if (sc->sc_cursor.cc_size.x < 32)
814 		edgemask = ~(edgemask >> sc->sc_cursor.cc_size.x);
815 	thc = sc->sc_thc;
816 	for (i = 0; i < 32; i++) {
817 		m = sc->sc_cursor.cc_bits[0][i] & edgemask;
818 		thc->thc_cursmask[i] = m;
819 		thc->thc_cursbits[i] = m & sc->sc_cursor.cc_bits[1][i];
820 	}
821 }
822 
823 /*
824  * Load a subset of the current (new) colormap into the color DAC.
825  */
826 static void
827 cg6_loadcmap(sc, start, ncolors)
828 	struct cgsix_softc *sc;
829 	int start, ncolors;
830 {
831 	volatile struct bt_regs *bt;
832 	u_int *ip, i;
833 	int count;
834 
835 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
836 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
837 	bt = sc->sc_bt;
838 	bt->bt_addr = BT_D4M4(start) << 24;
839 	while (--count >= 0) {
840 		i = *ip++;
841 		/* hardware that makes one want to pound boards with hammers */
842 		bt->bt_cmap = i;
843 		bt->bt_cmap = i << 8;
844 		bt->bt_cmap = i << 16;
845 		bt->bt_cmap = i << 24;
846 	}
847 }
848 
849 /*
850  * Load the cursor (overlay `foreground' and `background') colors.
851  */
852 static void
853 cg6_loadomap(sc)
854 	struct cgsix_softc *sc;
855 {
856 	volatile struct bt_regs *bt;
857 	u_int i;
858 
859 	bt = sc->sc_bt;
860 	bt->bt_addr = 0x01 << 24;	/* set background color */
861 	i = sc->sc_cursor.cc_color.cm_chip[0];
862 	bt->bt_omap = i;		/* R */
863 	bt->bt_omap = i << 8;		/* G */
864 	bt->bt_omap = i << 16;		/* B */
865 
866 	bt->bt_addr = 0x03 << 24;	/* set foreground color */
867 	bt->bt_omap = i << 24;		/* R */
868 	i = sc->sc_cursor.cc_color.cm_chip[1];
869 	bt->bt_omap = i;		/* G */
870 	bt->bt_omap = i << 8;		/* B */
871 }
872 
873 static void
874 cg6_unblank(dev)
875 	struct device *dev;
876 {
877 	struct cgsix_softc *sc = (struct cgsix_softc *)dev;
878 
879 	if (sc->sc_blanked) {
880 		sc->sc_blanked = 0;
881 		sc->sc_thc->thc_misc |= THC_MISC_VIDEN;
882 	}
883 }
884 
885 /* XXX the following should be moved to a "user interface" header */
886 /*
887  * Base addresses at which users can mmap() the various pieces of a cg6.
888  * Note that although the Brooktree color registers do not occupy 8K,
889  * the X server dies if we do not allow it to map 8K there (it just maps
890  * from 0x70000000 forwards, as a contiguous chunk).
891  */
892 #define	CG6_USER_FBC	0x70000000
893 #define	CG6_USER_TEC	0x70001000
894 #define	CG6_USER_BTREGS	0x70002000
895 #define	CG6_USER_FHC	0x70004000
896 #define	CG6_USER_THC	0x70005000
897 #define	CG6_USER_ROM	0x70006000
898 #define	CG6_USER_RAM	0x70016000
899 #define	CG6_USER_DHC	0x80000000
900 
901 struct mmo {
902 	u_long	mo_uaddr;	/* user (virtual) address */
903 	u_long	mo_size;	/* size, or 0 for video ram size */
904 	u_long	mo_physoff;	/* offset from sc_physadr */
905 };
906 
907 /*
908  * Return the address that would map the given device at the given
909  * offset, allowing for the given protection, or return -1 for error.
910  *
911  * XXX	needs testing against `demanding' applications (e.g., aviator)
912  */
913 paddr_t
914 cgsixmmap(dev, off, prot)
915 	dev_t dev;
916 	off_t off;
917 	int prot;
918 {
919 	struct cgsix_softc *sc = cgsix_cd.cd_devs[minor(dev)];
920 	struct mmo *mo;
921 	u_int u, sz;
922 	static struct mmo mmo[] = {
923 		{ CG6_USER_RAM, 0, CGSIX_RAM_OFFSET },
924 
925 		/* do not actually know how big most of these are! */
926 		{ CG6_USER_FBC, 1, CGSIX_FBC_OFFSET },
927 		{ CG6_USER_TEC, 1, CGSIX_TEC_OFFSET },
928 		{ CG6_USER_BTREGS, 8192 /* XXX */, CGSIX_BT_OFFSET },
929 		{ CG6_USER_FHC, 1, CGSIX_FHC_OFFSET },
930 		{ CG6_USER_THC, sizeof(struct cg6_thc), CGSIX_THC_OFFSET },
931 		{ CG6_USER_ROM, 65536, CGSIX_ROM_OFFSET },
932 		{ CG6_USER_DHC, 1, CGSIX_DHC_OFFSET },
933 	};
934 #define NMMO (sizeof mmo / sizeof *mmo)
935 
936 	if (off & PGOFSET)
937 		panic("cgsixmmap");
938 
939 	/*
940 	 * Entries with size 0 map video RAM (i.e., the size in fb data).
941 	 *
942 	 * Since we work in pages, the fact that the map offset table's
943 	 * sizes are sometimes bizarre (e.g., 1) is effectively ignored:
944 	 * one byte is as good as one page.
945 	 */
946 	for (mo = mmo; mo < &mmo[NMMO]; mo++) {
947 		if ((u_long)off < mo->mo_uaddr)
948 			continue;
949 		u = off - mo->mo_uaddr;
950 		sz = mo->mo_size ? mo->mo_size : sc->sc_fb.fb_type.fb_size;
951 		if (u < sz) {
952 			bus_space_handle_t bh;
953 			if (bus_space_mmap(sc->sc_bustag,
954 					   sc->sc_btype,
955 					   sc->sc_paddr+u+mo->mo_physoff,
956 					   BUS_SPACE_MAP_LINEAR, &bh))
957 				return (-1);
958 
959 			return ((paddr_t)bh);
960 		}
961 	}
962 
963 #ifdef DEBUG
964 	{
965 	  struct proc *p = curproc;	/* XXX */
966 	  log(LOG_NOTICE, "cgsixmmap(0x%llx) (%s[%d])\n",
967 		(long long)off, p->p_comm, p->p_pid);
968 	}
969 #endif
970 	return (-1);	/* not a user-map offset */
971 }
972