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