xref: /netbsd-src/sys/arch/amiga/dev/grf_cl.c (revision 4f16a5aa24d60efb639f4ed717d9770c6c1e5811)
1 /*	$NetBSD: grf_cl.c,v 1.57 2023/08/27 19:48:19 andvar Exp $ */
2 
3 /*
4  * Copyright (c) 1997 Klaus Burkert
5  * Copyright (c) 1995 Ezra Story
6  * Copyright (c) 1995 Kari Mettinen
7  * Copyright (c) 1994 Markus Wild
8  * Copyright (c) 1994 Lutz Vieweg
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by Lutz Vieweg.
22  * 4. The name of the author may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 #include "opt_amigacons.h"
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: grf_cl.c,v 1.57 2023/08/27 19:48:19 andvar Exp $");
40 
41 #include "grfcl.h"
42 #include "ite.h"
43 #include "wsdisplay.h"
44 #if NGRFCL > 0
45 
46 /*
47  * Graphics routines for Cirrus CL GD 5426 boards,
48  *
49  * This code offers low-level routines to access Cirrus Cl GD 5426
50  * graphics-boards from within NetBSD for the Amiga.
51  * No warranties for any kind of function at all - this
52  * code may crash your hardware and scratch your harddisk.  Use at your
53  * own risk.  Freely distributable.
54  *
55  * Modified for Cirrus CL GD 5426 from
56  * Lutz Vieweg's retina driver by Kari Mettinen 08/94
57  * Contributions by Ill, ScottE, MiL
58  * Extensively hacked and rewritten by Ezra Story (Ezy) 01/95
59  * Picasso/040 patches (wee!) by crest 01/96
60  *
61  * PicassoIV support bz Klaus "crest" Burkert.
62  * Fixed interlace and doublescan, added clockdoubling and
63  * HiColor&TrueColor support by crest 01/97
64  *
65  * Thanks to Village Tronic Marketing Gmbh for providing me with
66  * a Picasso-II board.
67  * Thanks for Integrated Electronics Oy Ab for providing me with
68  * Cirrus CL GD 542x family documentation.
69  *
70  * TODO:
71  *    Mouse support (almost there! :-))
72  *    Blitter support
73  *
74  */
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/errno.h>
79 #include <sys/ioctl.h>
80 #include <sys/device.h>
81 #include <sys/device_impl.h>	/* XXX autoconf abuse */
82 #include <sys/malloc.h>
83 
84 #include <machine/cpu.h>
85 #include <dev/cons.h>
86 #if NWSDISPLAY > 0
87 #include <dev/wscons/wsconsio.h>
88 #include <dev/wscons/wsdisplayvar.h>
89 #include <dev/rasops/rasops.h>
90 #include <dev/wscons/wsdisplay_vconsvar.h>
91 #endif
92 #include <amiga/dev/itevar.h>
93 #include <amiga/amiga/device.h>
94 #include <amiga/dev/grfioctl.h>
95 #include <amiga/dev/grfvar.h>
96 #include <amiga/dev/grf_clreg.h>
97 #include <amiga/dev/zbusvar.h>
98 
99 int	cl_mondefok(struct grfvideo_mode *);
100 void	cl_boardinit(struct grf_softc *);
101 static void cl_CompFQ(u_int, u_char *, u_char *, u_char *);
102 int	cl_getvmode(struct grf_softc *, struct grfvideo_mode *);
103 int	cl_setvmode(struct grf_softc *, unsigned int);
104 int	cl_toggle(struct grf_softc *, unsigned short);
105 int	cl_getcmap(struct grf_softc *, struct grf_colormap *);
106 int	cl_putcmap(struct grf_softc *, struct grf_colormap *);
107 #ifndef CL5426CONSOLE
108 void	cl_off(struct grf_softc *);
109 #endif
110 void	cl_inittextmode(struct grf_softc *);
111 int	cl_ioctl(register struct grf_softc *, u_long, void *);
112 int	cl_getmousepos(struct grf_softc *, struct grf_position *);
113 int	cl_setmousepos(struct grf_softc *, struct grf_position *);
114 static int cl_setspriteinfo(struct grf_softc *, struct grf_spriteinfo *);
115 int	cl_getspriteinfo(struct grf_softc *, struct grf_spriteinfo *);
116 static int cl_getspritemax(struct grf_softc *, struct grf_position *);
117 int	cl_blank(struct grf_softc *, int);
118 int	cl_isblank(struct grf_softc *);
119 int	cl_setmonitor(struct grf_softc *, struct grfvideo_mode *);
120 void	cl_writesprpos(volatile char *, short, short);
121 void	writeshifted(volatile char *, signed char, signed char);
122 
123 static void	RegWakeup(volatile void *);
124 static void	RegOnpass(volatile void *);
125 static void	RegOffpass(volatile void *);
126 
127 void	grfclattach(device_t, device_t, void *);
128 int	grfclprint(void *, const char *);
129 int	grfclmatch(device_t, cfdata_t, void *);
130 void	cl_memset(unsigned char *, unsigned char, int);
131 
132 #if NWSDISPLAY > 0
133 /* wsdisplay acessops, emulops */
134 static int	cl_wsioctl(void *, void *, u_long, void *, int, struct lwp *);
135 static int	cl_get_fbinfo(struct grf_softc *, struct wsdisplayio_fbinfo *);
136 
137 static void	cl_wscursor(void *, int, int, int);
138 static void	cl_wsputchar(void *, int, int, u_int, long);
139 static void	cl_wscopycols(void *, int, int, int, int);
140 static void	cl_wserasecols(void *, int, int, int, long);
141 static void	cl_wscopyrows(void *, int, int, int);
142 static void	cl_wseraserows(void *, int, int, long);
143 static int	cl_wsallocattr(void *, int, int, int, long *);
144 static int	cl_wsmapchar(void *, int, unsigned int *);
145 #endif  /* NWSDISPLAY > 0 */
146 
147 /* Graphics display definitions.
148  * These are filled by 'grfconfig' using GRFIOCSETMON.
149  */
150 #define monitor_def_max 24
151 static struct grfvideo_mode monitor_def[24] = {
152 	{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0},
153 	{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0},
154 	{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}
155 };
156 static struct grfvideo_mode *monitor_current = &monitor_def[0];
157 
158 /* Patchable maximum pixel clock */
159 unsigned long cl_maxpixelclock = 86000000;
160 
161 /* Console display definition.
162  *   Default hardcoded text mode.  This grf_cl is set up to
163  *   use one text mode only, and this is it.  You may use
164  *   grfconfig to change the mode after boot.
165  */
166 /* Console font */
167 #ifdef KFONT_8X11
168 #define CIRRUSFONT kernel_font_8x11
169 #define CIRRUSFONTY 11
170 #else
171 #define CIRRUSFONT kernel_font_8x8
172 #define CIRRUSFONTY 8
173 #endif
174 extern unsigned char CIRRUSFONT[];
175 
176 struct grfcltext_mode clconsole_mode = {
177 	{255, "", 25000000, 640, 480, 4, 640/8, 680/8, 768/8, 800/8,
178 	 481, 490, 498, 522, 0},
179 	8, CIRRUSFONTY, 80, 480 / CIRRUSFONTY, CIRRUSFONT, 32, 255
180 };
181 /* Console colors */
182 unsigned char clconscolors[3][3] = {	/* background, foreground, hilite */
183 	{0, 0x40, 0x50}, {152, 152, 152}, {255, 255, 255}
184 };
185 
186 int	cltype = 0;		/* Picasso, Spectrum or Piccolo */
187 int	cl_64bit = 0;		/* PiccoloSD64 or PicassoIV */
188 unsigned char cl_pass_toggle;	/* passthru status tracker */
189 static int cl_blanked;		/* true when video is currently blanked out */
190 
191 /*
192  * because all 542x-boards have 2 configdev entries, one for
193  * framebuffer mem and the other for regs, we have to hold onto
194  * the pointers globally until we match on both.  This and 'cltype'
195  * are the primary obsticles to multiple board support, but if you
196  * have multiple boards you have bigger problems than grf_cl.
197  */
198 static void *cl_fbaddr = 0;	/* framebuffer */
199 static void *cl_regaddr = 0;	/* registers */
200 static int cl_fbsize;		/* framebuffer size */
201 static int cl_fbautosize;	/* framebuffer autoconfig size */
202 
203 
204 /*
205  * current sprite info, if you add support for multiple boards
206  * make this an array or something
207  */
208 struct grf_spriteinfo cl_cursprite;
209 
210 /* sprite bitmaps in kernel stack, you'll need to arrayize these too if
211  * you add multiple board support
212  */
213 static unsigned char cl_imageptr[8 * 64], cl_maskptr[8 * 64];
214 static unsigned char cl_sprred[2], cl_sprgreen[2], cl_sprblue[2];
215 
216 #if NWSDISPLAY > 0
217 static struct wsdisplay_accessops cl_accessops = {
218 	.ioctl		= cl_wsioctl,
219 	.mmap		= grf_wsmmap
220 };
221 
222 static struct wsdisplay_emulops cl_textops = {
223 	.cursor		= cl_wscursor,
224 	.mapchar	= cl_wsmapchar,
225 	.putchar	= cl_wsputchar,
226 	.copycols	= cl_wscopycols,
227 	.erasecols	= cl_wserasecols,
228 	.copyrows	= cl_wscopyrows,
229 	.eraserows	= cl_wseraserows,
230 	.allocattr	= cl_wsallocattr
231 };
232 
233 static struct wsscreen_descr cl_defaultscreen = {
234 	.name		= "default",
235 	.textops	= &cl_textops,
236 	.fontwidth	= 8,
237 	.fontheight	= CIRRUSFONTY,
238 	.capabilities	= WSSCREEN_HILIT | WSSCREEN_BLINK |
239 			  WSSCREEN_REVERSE | WSSCREEN_UNDERLINE
240 };
241 
242 static const struct wsscreen_descr *cl_screens[] = {
243 	&cl_defaultscreen,
244 };
245 
246 static struct wsscreen_list cl_screenlist = {
247 	sizeof(cl_screens) / sizeof(struct wsscreen_descr *), cl_screens
248 };
249 #endif  /* NWSDISPLAY > 0 */
250 
251 /* standard driver stuff */
252 CFATTACH_DECL_NEW(grfcl, sizeof(struct grf_softc),
253     grfclmatch, grfclattach, NULL, NULL);
254 
255 static struct cfdata *cfdata;
256 
257 int
258 grfclmatch(device_t parent, cfdata_t cf, void *aux)
259 {
260 	struct zbus_args *zap;
261 	static int regprod, fbprod;
262 	int error;
263 
264 	zap = aux;
265 
266 #ifndef CL5426CONSOLE
267 	if (amiga_realconfig == 0)
268 		return (0);
269 #endif
270 
271 	/* Grab the first board we encounter as the preferred one.  This will
272 	 * allow one board to work in a multiple 5426 board system, but not
273 	 * multiple boards at the same time.  */
274 	if (cltype == 0) {
275 		switch (zap->manid) {
276 		    case PICASSO:
277 			switch (zap->prodid) {
278 			    case 11:
279 			    case 12:
280 				regprod = 12;
281 				fbprod = 11;
282 				error = 0;
283 				break;
284 			    case 22:
285 				error = 0;
286 				break;
287 			    case 21:
288 			    case 23:
289 				regprod = 23;
290 				fbprod = 21;
291 				cl_64bit = 1;
292 				error = 0;
293 				break;
294 			    case 24:
295 				regprod = 24;
296 				fbprod = 24;
297 				cl_64bit = 1;
298 				error = 0;
299 				break;
300 		    	    default:
301 				error = 1;
302 				break;
303 			}
304 			if (error == 1)
305 			    return (0);
306 			else
307 			    break;
308 		    case SPECTRUM:
309 			if (zap->prodid != 2 && zap->prodid != 1)
310 				return (0);
311 			regprod = 2;
312 			fbprod = 1;
313 			break;
314 		    case PICCOLO:
315 			switch (zap->prodid) {
316 			    case 5:
317 			    case 6:
318 				regprod = 6;
319 				fbprod = 5;
320 				error = 0;
321 				break;
322 			    case 10:
323 			    case 11:
324 				regprod = 11;
325 				fbprod = 10;
326 				cl_64bit = 1;
327 				error = 0;
328 				break;
329 		    	    default:
330 				error = 1;
331 				break;
332 			}
333 			if (error == 1)
334 			    return (0);
335 			else
336 			    break;
337 		    default:
338 			return (0);
339 		}
340 		cltype = zap->manid;
341 	} else {
342 		if (cltype != zap->manid) {
343 			return (0);
344 		}
345 	}
346 
347 	/* Configure either registers or framebuffer in any order */
348 	if ((cltype == PICASSO) && (cl_64bit == 1)) {
349 		switch (zap->prodid) {
350 		    case 21:
351 			cl_fbaddr = zap->va;
352 			cl_fbautosize = zap->size;
353 			break;
354 		    case 22:
355 			cl_fbautosize += zap->size;
356 			break;
357 		    case 23:
358 			cl_regaddr = (void *)((unsigned long)(zap->va) + 0x10000);
359 			break;
360 		    case 24:
361 			cl_regaddr = (void *)((unsigned long)(zap->va) + 0x600000);
362 			/* check for PicassoIV with 64MB config and handle it */
363 			if (zap->size == 0x04000000) {
364 			    cl_fbaddr = (void *)((unsigned long)(zap->va) + 0x02000000);
365 			} else {
366 			    cl_fbaddr = (void *)((unsigned long)(zap->va) + 0x01000000);
367 			}
368 			cl_fbautosize = 0x400000;
369 			break;
370 		    default:
371 			return (0);
372 		}
373 	}
374 	else {
375 		if (zap->prodid == regprod)
376 			cl_regaddr = zap->va;
377 		else
378 			if (zap->prodid == fbprod) {
379 				cl_fbaddr = zap->va;
380 				cl_fbautosize = zap->size;
381 			} else
382 				return (0);
383 	}
384 
385 #ifdef CL5426CONSOLE
386 		if (amiga_realconfig == 0) {
387 			cfdata = cf;
388 		}
389 #endif
390 
391 	return (1);
392 }
393 
394 void
395 grfclattach(device_t parent, device_t self, void *aux)
396 {
397 	static struct grf_softc congrf;
398 	struct zbus_args *zap;
399 	struct grf_softc *gp;
400 	struct device temp;
401 	static char attachflag = 0;
402 
403 	zap = aux;
404 
405 	printf("\n");
406 
407 	/* make sure both halves have matched */
408 	if (!cl_regaddr || !cl_fbaddr)
409 		return;
410 
411 	/* do all that messy console/grf stuff */
412 	if (self == NULL) {
413 		gp = &congrf;
414 		gp->g_device = &temp;
415 		temp.dv_private = gp;
416 	} else {
417 		gp = device_private(self);
418 		gp->g_device = self;
419 	}
420 
421 	if (self != NULL && congrf.g_regkva != 0) {
422 		/*
423 		 * inited earlier, just copy (not device struct)
424 		 */
425 		memcpy(&gp->g_display, &congrf.g_display,
426 		    (char *) &gp[1] - (char *) &gp->g_display);
427 	} else {
428 		gp->g_regkva = (volatile void *) cl_regaddr;
429 		gp->g_fbkva = (volatile void *) cl_fbaddr;
430 
431 		gp->g_unit = GRF_CL5426_UNIT;
432 		gp->g_mode = cl_mode;
433 #if NITE > 0
434 		gp->g_conpri = grfcl_cnprobe();
435 #endif
436 		gp->g_flags = GF_ALIVE;
437 
438 		/* wakeup the board */
439 		cl_boardinit(gp);
440 
441 #ifdef CL5426CONSOLE
442 #if NWSDISPLAY > 0
443 		gp->g_accessops = &cl_accessops;
444 		gp->g_emulops = &cl_textops;
445 		gp->g_defaultscr = &cl_defaultscreen;
446 		gp->g_scrlist = &cl_screenlist;
447 #else
448 #if NITE > 0
449 		grfcl_iteinit(gp);
450 #endif
451 #endif  /* NWSDISPLAY > 0 */
452 		(void) cl_load_mon(gp, &clconsole_mode);
453 #endif
454 	}
455 
456 	/*
457 	 * attach grf (once)
458 	 */
459 	if (amiga_config_found(cfdata, gp->g_device, gp, grfclprint,
460 			       CFARGS_NONE)) {
461 		attachflag = 1;
462 		printf("grfcl: %dMB ", cl_fbsize / 0x100000);
463 		switch (cltype) {
464 		    case PICASSO:
465 			if (cl_64bit == 1) {
466 				printf("Picasso IV");
467 				/* 135MHz will be supported if we
468 				 * have a palette doubling mode.
469 				 */
470 				cl_maxpixelclock = 86000000;
471 			}
472 			else {
473 				printf("Picasso II");
474 
475 				/* check for PicassoII+ (crest) */
476 				if(zap->serno == 0x00100000)
477 				    printf("+");
478 
479 				/* determine used Gfx/chipset (crest) */
480 				vgaw(gp->g_regkva, CRT_ADDRESS, 0x27); /* Chip ID */
481 				switch(vgar(gp->g_regkva, CRT_ADDRESS_R)>>2) {
482 				    case 0x24:
483 					printf(" (with CL-GD5426)");
484 					break;
485 				    case 0x26:
486 					printf(" (with CL-GD5428)");
487 					break;
488 				    case 0x27:
489 					printf(" (with CL-GD5429)");
490 					break;
491 				}
492 	                        cl_maxpixelclock = 86000000;
493 			}
494 			break;
495 		    case SPECTRUM:
496 			printf("Spectrum");
497                         cl_maxpixelclock = 90000000;
498 			break;
499 		    case PICCOLO:
500 			if (cl_64bit == 1) {
501 				printf("Piccolo SD64");
502 				/* 110MHz will be supported if we
503 				 * have a palette doubling mode.
504 				 */
505 				cl_maxpixelclock = 90000000;
506 			} else {
507 				printf("Piccolo");
508 				cl_maxpixelclock = 90000000;
509 			}
510 			break;
511 		}
512 		printf(" being used\n");
513 #ifdef CL_OVERCLOCK
514                 cl_maxpixelclock = 115000000;
515 #endif
516 	} else {
517 		if (!attachflag)
518 			printf("grfcl unattached!!\n");
519 	}
520 }
521 
522 int
523 grfclprint(void *aux, const char *pnp)
524 {
525 	if (pnp)
526 		aprint_normal("ite at %s: ", pnp);
527 	return (UNCONF);
528 }
529 
530 void
531 cl_boardinit(struct grf_softc *gp)
532 {
533 	volatile unsigned char *ba = gp->g_regkva;
534 	int     x;
535 
536 	if ((cltype == PICASSO) && (cl_64bit == 1)) { /* PicassoIV */
537 		WCrt(ba, 0x51, 0x00);		/* disable capture (FlickerFixer) */
538 		delay(200000);		/* wait some time (two frames as of now) */
539 		WGfx(ba, 0x2f, 0x00);			/* get Blitter into 542x  */
540 		WGfx(ba, GCT_ID_RESERVED, 0x00);	/* compatibility mode     */
541 		WGfx(ba, GCT_ID_BLT_STAT_START, 0x00);	/* or at least, try so... */
542 		cl_fbsize = cl_fbautosize;
543 	} else {
544 
545 		/* wakeup board and flip passthru OFF */
546 		RegWakeup(ba);
547 		RegOnpass(ba);
548 
549 		vgaw(ba, 0x46e8, 0x16);
550 		vgaw(ba, 0x102, 1);
551 		vgaw(ba, 0x46e8, 0x0e);
552 		if (cl_64bit != 1)
553 			vgaw(ba, 0x3c3, 1);
554 
555 		cl_fbsize = cl_fbautosize;
556 
557 		/* setup initial unchanging parameters */
558 
559 		cl_blanked = 1;
560 		WSeq(ba, SEQ_ID_CLOCKING_MODE, 0x21);	/* 8 dot - display off */
561 		vgaw(ba, GREG_MISC_OUTPUT_W, 0xed);	/* mem disable */
562 
563 		WGfx(ba, GCT_ID_OFFSET_1, 0xec);	/* magic cookie */
564 		WSeq(ba, SEQ_ID_UNLOCK_EXT, 0x12);	/* yum! cookies! */
565 
566 		if (cl_64bit == 1) {
567 			WSeq(ba, SEQ_ID_CONF_RBACK, 0x00);
568 			WSeq(ba, SEQ_ID_DRAM_CNTL, (cl_fbsize / 0x100000 == 2) ? 0x38 : 0xb8);
569 		} else {
570 			WSeq(ba, SEQ_ID_DRAM_CNTL, 0xb0);
571 		}
572 		WSeq(ba, SEQ_ID_RESET, 0x03);
573 		WSeq(ba, SEQ_ID_MAP_MASK, 0xff);
574 		WSeq(ba, SEQ_ID_CHAR_MAP_SELECT, 0x00);
575 		WSeq(ba, SEQ_ID_MEMORY_MODE, 0x0e);	/* a or 6? */
576 		WSeq(ba, SEQ_ID_EXT_SEQ_MODE, (cltype == PICASSO) ? 0x21 : 0x81);
577 		WSeq(ba, SEQ_ID_EEPROM_CNTL, 0x00);
578 		if (cl_64bit == 1)
579 			WSeq(ba, SEQ_ID_PERF_TUNE, 0x5a);
580 		else
581 			WSeq(ba, SEQ_ID_PERF_TUNE, 0x0a);	/* mouse 0a fa */
582 		WSeq(ba, SEQ_ID_SIG_CNTL, 0x02);
583 		WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x04);
584 
585 		if (cl_64bit == 1)
586 			WSeq(ba, SEQ_ID_MCLK_SELECT, 0x1c);
587 		else
588 		WSeq(ba, SEQ_ID_MCLK_SELECT, 0x22);
589 
590 		WCrt(ba, CRT_ID_PRESET_ROW_SCAN, 0x00);
591 		WCrt(ba, CRT_ID_CURSOR_START, 0x00);
592 		WCrt(ba, CRT_ID_CURSOR_END, 0x08);
593 		WCrt(ba, CRT_ID_START_ADDR_HIGH, 0x00);
594 		WCrt(ba, CRT_ID_START_ADDR_LOW, 0x00);
595 		WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, 0x00);
596 		WCrt(ba, CRT_ID_CURSOR_LOC_LOW, 0x00);
597 
598 		WCrt(ba, CRT_ID_UNDERLINE_LOC, 0x07);
599 		WCrt(ba, CRT_ID_MODE_CONTROL, 0xe3);
600 		WCrt(ba, CRT_ID_LINE_COMPARE, 0xff);	/* ff */
601 		WCrt(ba, CRT_ID_EXT_DISP_CNTL, 0x22);
602 		if (cl_64bit == 1) {
603 			WCrt(ba, CRT_ID_SYNC_ADJ_GENLOCK, 0x00);
604 			WCrt(ba, CRT_ID_OVERLAY_EXT_CTRL_REG, 0x40);
605 		}
606 		WSeq(ba, SEQ_ID_CURSOR_STORE, 0x3c);	/* mouse 0x00 */
607 
608 		WGfx(ba, GCT_ID_SET_RESET, 0x00);
609 		WGfx(ba, GCT_ID_ENABLE_SET_RESET, 0x00);
610 		WGfx(ba, GCT_ID_DATA_ROTATE, 0x00);
611 		WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x00);
612 		WGfx(ba, GCT_ID_GRAPHICS_MODE, 0x00);
613 		WGfx(ba, GCT_ID_MISC, 0x01);
614 		WGfx(ba, GCT_ID_COLOR_XCARE, 0x0f);
615 		WGfx(ba, GCT_ID_BITMASK, 0xff);
616 		WGfx(ba, GCT_ID_MODE_EXT, 0x28);
617 
618 		for (x = 0; x < 0x10; x++)
619 			WAttr(ba, x, x);
620 		WAttr(ba, ACT_ID_ATTR_MODE_CNTL, 0x01);
621 		WAttr(ba, ACT_ID_OVERSCAN_COLOR, 0x00);
622 		WAttr(ba, ACT_ID_COLOR_PLANE_ENA, 0x0f);
623 		WAttr(ba, ACT_ID_HOR_PEL_PANNING, 0x00);
624 		WAttr(ba, ACT_ID_COLOR_SELECT, 0x00);
625 		WAttr(ba, 0x34, 0x00);
626 
627 		vgaw(ba, VDAC_MASK, 0xff);
628 		vgaw(ba, GREG_MISC_OUTPUT_W, 0xef);
629 
630 		WGfx(ba, GCT_ID_BLT_STAT_START, 0x04);
631 		WGfx(ba, GCT_ID_BLT_STAT_START, 0x00);
632 	}
633 
634 	/* colors initially set to greyscale */
635 	vgaw(ba, VDAC_ADDRESS_W, 0);
636 	for (x = 255; x >= 0; x--) {
637 		vgaw(ba, VDAC_DATA, x);
638 		vgaw(ba, VDAC_DATA, x);
639 		vgaw(ba, VDAC_DATA, x);
640 	}
641 	/* set sprite bitmap pointers */
642 	cl_cursprite.image = cl_imageptr;
643 	cl_cursprite.mask = cl_maskptr;
644 	cl_cursprite.cmap.red = cl_sprred;
645 	cl_cursprite.cmap.green = cl_sprgreen;
646 	cl_cursprite.cmap.blue = cl_sprblue;
647 
648 	if (cl_64bit == 0) {
649 
650 		/* check for 1MB or 2MB board (crest) */
651 		volatile unsigned long *cl_fbtestaddr;
652 		cl_fbtestaddr = (volatile unsigned long *)gp->g_fbkva;
653 
654 		WGfx(ba, GCT_ID_OFFSET_0, 0x40);
655 		*cl_fbtestaddr = 0x12345678;
656 
657 		if (*cl_fbtestaddr != 0x12345678) {
658 			WSeq(ba, SEQ_ID_DRAM_CNTL, 0x30);
659 			cl_fbsize = 0x100000;
660 		}
661 		else
662 		{
663 			cl_fbsize = 0x200000;
664 		}
665 	}
666 	WGfx(ba, GCT_ID_OFFSET_0, 0x00);
667 }
668 
669 
670 int
671 cl_getvmode(struct grf_softc *gp, struct grfvideo_mode *vm)
672 {
673 	struct grfvideo_mode *gv;
674 
675 #ifdef CL5426CONSOLE
676 	/* Handle grabbing console mode */
677 	if (vm->mode_num == 255) {
678 		memcpy(vm, &clconsole_mode, sizeof(struct grfvideo_mode));
679 		/* XXX so grfconfig can tell us the correct text dimensions. */
680 		vm->depth = clconsole_mode.fy;
681 	} else
682 #endif
683         {
684                 if (vm->mode_num == 0)
685                         vm->mode_num = (monitor_current - monitor_def) + 1;
686                 if (vm->mode_num < 1 || vm->mode_num > monitor_def_max)
687                         return (EINVAL);
688                 gv = monitor_def + (vm->mode_num - 1);
689                 if (gv->mode_num == 0)
690                         return (EINVAL);
691 
692                 memcpy(vm, gv, sizeof(struct grfvideo_mode));
693         }
694 
695         /* adjust internal values to pixel values */
696 
697         vm->hblank_start *= 8;
698         vm->hsync_start *= 8;
699         vm->hsync_stop *= 8;
700         vm->htotal *= 8;
701 
702 	return (0);
703 }
704 
705 
706 int
707 cl_setvmode(struct grf_softc *gp, unsigned mode)
708 {
709 	if (!mode || (mode > monitor_def_max) ||
710 	    monitor_def[mode - 1].mode_num == 0)
711 		return (EINVAL);
712 
713 	monitor_current = monitor_def + (mode - 1);
714 
715 	return (0);
716 }
717 
718 #ifndef CL5426CONSOLE
719 void
720 cl_off(struct grf_softc *gp)
721 {
722 	char   *ba = gp->g_regkva;
723 
724 	/*
725 	 * we'll put the pass-through on for cc ite and set Full Bandwidth bit
726 	 * on just in case it didn't work...but then it doesn't matter does
727 	 * it? =)
728 	 */
729 	RegOnpass(ba);
730 	vgaw(ba, SEQ_ADDRESS, SEQ_ID_CLOCKING_MODE);
731 	vgaw(ba, SEQ_ADDRESS_W, vgar(ba, SEQ_ADDRESS_W) | 0x20);
732 	cl_blanked = 1;
733 }
734 #endif
735 
736 int
737 cl_blank(struct grf_softc *gp, int on)
738 {
739 
740 	WSeq(gp->g_regkva, SEQ_ID_CLOCKING_MODE, on ? 0x01 : 0x21);
741 	cl_blanked = !on;
742 	return 0;
743 }
744 
745 int
746 cl_isblank(struct grf_softc *gp)
747 {
748 
749 	return cl_blanked;
750 }
751 
752 /*
753  * Change the mode of the display.
754  * Return a UNIX error number or 0 for success.
755  */
756 int
757 cl_mode(register struct grf_softc *gp, u_long cmd, void *arg, u_long a2, int a3)
758 {
759 	int     error;
760 
761 	switch (cmd) {
762 	    case GM_GRFON:
763 		error = cl_load_mon(gp,
764 		    (struct grfcltext_mode *) monitor_current) ? 0 : EINVAL;
765 		return (error);
766 
767 	    case GM_GRFOFF:
768 #ifndef CL5426CONSOLE
769 		cl_off(gp);
770 #else
771 		cl_load_mon(gp, &clconsole_mode);
772 #endif
773 		return (0);
774 
775 	    case GM_GRFCONFIG:
776 		return (0);
777 
778 	    case GM_GRFGETVMODE:
779 		return (cl_getvmode(gp, (struct grfvideo_mode *) arg));
780 
781 	    case GM_GRFSETVMODE:
782 		error = cl_setvmode(gp, *(unsigned *) arg);
783 		if (!error && (gp->g_flags & GF_GRFON))
784 			cl_load_mon(gp,
785 			    (struct grfcltext_mode *) monitor_current);
786 		return (error);
787 
788 	    case GM_GRFGETNUMVM:
789 		*(int *) arg = monitor_def_max;
790 		return (0);
791 
792 	    case GM_GRFIOCTL:
793 		return (cl_ioctl(gp, a2, arg));
794 
795 	    default:
796 		break;
797 	}
798 
799 	return (EPASSTHROUGH);
800 }
801 
802 int
803 cl_ioctl(register struct grf_softc *gp, u_long cmd, void *data)
804 {
805 	switch (cmd) {
806 	    case GRFIOCGSPRITEPOS:
807 		return (cl_getmousepos(gp, (struct grf_position *) data));
808 
809 	    case GRFIOCSSPRITEPOS:
810 		return (cl_setmousepos(gp, (struct grf_position *) data));
811 
812 	    case GRFIOCSSPRITEINF:
813 		return (cl_setspriteinfo(gp, (struct grf_spriteinfo *) data));
814 
815 	    case GRFIOCGSPRITEINF:
816 		return (cl_getspriteinfo(gp, (struct grf_spriteinfo *) data));
817 
818 	    case GRFIOCGSPRITEMAX:
819 		return (cl_getspritemax(gp, (struct grf_position *) data));
820 
821 	    case GRFIOCGETCMAP:
822 		return (cl_getcmap(gp, (struct grf_colormap *) data));
823 
824 	    case GRFIOCPUTCMAP:
825 		return (cl_putcmap(gp, (struct grf_colormap *) data));
826 
827 	    case GRFIOCBITBLT:
828 		break;
829 
830 	    case GRFTOGGLE:
831 		return (cl_toggle(gp, 0));
832 
833 	    case GRFIOCSETMON:
834 		return (cl_setmonitor(gp, (struct grfvideo_mode *) data));
835 
836             case GRFIOCBLANK:
837                 return (cl_blank(gp, *(int *)data));
838 
839 	}
840 	return (EPASSTHROUGH);
841 }
842 
843 int
844 cl_getmousepos(struct grf_softc *gp, struct grf_position *data)
845 {
846 	data->x = cl_cursprite.pos.x;
847 	data->y = cl_cursprite.pos.y;
848 	return (0);
849 }
850 
851 void
852 cl_writesprpos(volatile char *ba, short x, short y)
853 {
854 	/* we want to use a 16-bit write to 3c4 so no macros used */
855 	volatile unsigned char *cwp;
856         volatile unsigned short *wp;
857 
858 	cwp = ba + 0x3c4;
859         wp = (volatile unsigned short *)cwp;
860 
861 	/*
862 	 * don't ask me why, but apparently you can't do a 16-bit write with
863 	 * x-position like with y-position below (dagge)
864 	 */
865         cwp[0] = 0x10 | ((x << 5) & 0xff);
866         cwp[1] = (x >> 3) & 0xff;
867 
868         *wp = 0x1100 | ((y & 7) << 13) | ((y >> 3) & 0xff);
869 }
870 
871 void
872 writeshifted(volatile char *to, signed char shiftx, signed char shifty)
873 {
874 	int y;
875 	unsigned long long *tptr, *iptr, *mptr, line;
876 
877 	tptr = (unsigned long long *) __UNVOLATILE(to);
878         iptr = (unsigned long long *) cl_cursprite.image;
879         mptr = (unsigned long long *) cl_cursprite.mask;
880 
881         shiftx = shiftx < 0 ? 0 : shiftx;
882         shifty = shifty < 0 ? 0 : shifty;
883 
884         /* start reading shifty lines down, and
885          * shift each line in by shiftx
886          */
887         for (y = shifty; y < 64; y++) {
888 
889                 /* image */
890                 line = iptr[y];
891 		*tptr++ = line << shiftx;
892 
893                 /* mask */
894                 line = mptr[y];
895 		*tptr++ = line << shiftx;
896 	}
897 
898         /* clear the remainder */
899         for (y = shifty; y > 0; y--) {
900                 *tptr++ = 0;
901                 *tptr++ = 0;
902         }
903 }
904 
905 int
906 cl_setmousepos(struct grf_softc *gp, struct grf_position *data)
907 {
908 	volatile char *ba = gp->g_regkva;
909         short rx, ry;
910 #ifdef CL_SHIFTSPRITE
911 	short prx, pry;
912 	volatile char *fb = gp->g_fbkva;
913         volatile char *sprite = fb + (cl_fbsize - 1024);
914 #endif
915 
916         /* no movement */
917 	if (cl_cursprite.pos.x == data->x && cl_cursprite.pos.y == data->y)
918 		return (0);
919 
920         /* current and previous real coordinates */
921 	rx = data->x - cl_cursprite.hot.x;
922 	ry = data->y - cl_cursprite.hot.y;
923 
924         /*
925 	 * if we are/were on an edge, create (un)shifted bitmap --
926          * ripped out optimization (not extremely worthwhile,
927          * and kind of buggy anyhow).
928          */
929 #ifdef CL_SHIFTSPRITE
930 	prx = cl_cursprite.pos.x - cl_cursprite.hot.x;
931 	pry = cl_cursprite.pos.y - cl_cursprite.hot.y;
932         if (rx < 0 || ry < 0 || prx < 0 || pry < 0) {
933                 writeshifted(sprite, rx < 0 ? -rx : 0, ry < 0 ? -ry : 0);
934         }
935 #endif
936 
937         /* do movement, save position */
938         cl_writesprpos(ba, rx < 0 ? 0 : rx, ry < 0 ? 0 : ry);
939 	cl_cursprite.pos.x = data->x;
940 	cl_cursprite.pos.y = data->y;
941 
942 	return (0);
943 }
944 
945 int
946 cl_getspriteinfo(struct grf_softc *gp, struct grf_spriteinfo *data)
947 {
948 	copyout(&cl_cursprite, data, sizeof(struct grf_spriteinfo));
949 	copyout(cl_cursprite.image, data->image, 64 * 8);
950 	copyout(cl_cursprite.mask, data->mask, 64 * 8);
951 	return (0);
952 }
953 
954 static int
955 cl_setspriteinfo(struct grf_softc *gp, struct grf_spriteinfo *data)
956 {
957 	volatile unsigned char *ba = gp->g_regkva, *fb = gp->g_fbkva;
958         volatile char *sprite = fb + (cl_fbsize - 1024);
959 
960 	if (data->set & GRFSPRSET_SHAPE) {
961 
962                 unsigned short dsx, dsy, i;
963                 unsigned long *di, *dm, *si, *sm;
964                 unsigned long ssi[128], ssm[128];
965                 struct grf_position gpos;
966 
967 
968                 /* check for a too large sprite (no clipping!) */
969                 dsy = data->size.y;
970                 dsx = data->size.x;
971                 if (dsy > 64 || dsx > 64)
972                         return(EINVAL);
973 
974                 /* prepare destination */
975                 di = (unsigned long *)cl_cursprite.image;
976                 dm = (unsigned long *)cl_cursprite.mask;
977                 cl_memset((unsigned char *)di, 0, 8*64);
978                 cl_memset((unsigned char *)dm, 0, 8*64);
979 
980                 /* two alternatives:  64 across, then it's
981                  * the same format we use, just copy.  Otherwise,
982                  * copy into tmp buf and recopy skipping the
983                  * unused 32 bits.
984                  */
985                 if ((dsx - 1) / 32) {
986                         copyin(data->image, di, 8 * dsy);
987                         copyin(data->mask, dm, 8 * dsy);
988                 } else {
989                         si = ssi; sm = ssm;
990                         copyin(data->image, si, 4 * dsy);
991                         copyin(data->mask, sm, 4 * dsy);
992                         for (i = 0; i < dsy; i++) {
993                                 *di = *si++;
994                                 *dm = *sm++;
995                                 di += 2;
996                                 dm += 2;
997                         }
998                 }
999 
1000                 /* set size */
1001 		cl_cursprite.size.x = data->size.x;
1002 		cl_cursprite.size.y = data->size.y;
1003 
1004                 /* forcably load into board */
1005                 gpos.x = cl_cursprite.pos.x;
1006                 gpos.y = cl_cursprite.pos.y;
1007                 cl_cursprite.pos.x = -1;
1008                 cl_cursprite.pos.y = -1;
1009                 writeshifted(sprite, 0, 0);
1010                 cl_setmousepos(gp, &gpos);
1011 
1012 	}
1013 	if (data->set & GRFSPRSET_HOT) {
1014 
1015 		cl_cursprite.hot = data->hot;
1016 
1017 	}
1018 	if (data->set & GRFSPRSET_CMAP) {
1019 
1020 		u_char  red[2], green[2], blue[2];
1021 
1022 		copyin(data->cmap.red, red, 2);
1023 		copyin(data->cmap.green, green, 2);
1024 		copyin(data->cmap.blue, blue, 2);
1025 		memcpy(cl_cursprite.cmap.red, red, 2);
1026 		memcpy(cl_cursprite.cmap.green, green, 2);
1027 		memcpy(cl_cursprite.cmap.blue, blue, 2);
1028 
1029                 /* enable and load colors 256 & 257 */
1030 		WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x06);
1031 
1032                 /* 256 */
1033 		vgaw(ba, VDAC_ADDRESS_W, 0x00);
1034 		if (cltype == PICASSO) {
1035 			vgaw(ba, VDAC_DATA, (u_char) (red[0] >> 2));
1036 			vgaw(ba, VDAC_DATA, (u_char) (green[0] >> 2));
1037 			vgaw(ba, VDAC_DATA, (u_char) (blue[0] >> 2));
1038 		} else {
1039 			vgaw(ba, VDAC_DATA, (u_char) (blue[0] >> 2));
1040 			vgaw(ba, VDAC_DATA, (u_char) (green[0] >> 2));
1041 			vgaw(ba, VDAC_DATA, (u_char) (red[0] >> 2));
1042 		}
1043 
1044                 /* 257 */
1045 		vgaw(ba, VDAC_ADDRESS_W, 0x0f);
1046 		if (cltype == PICASSO) {
1047 			vgaw(ba, VDAC_DATA, (u_char) (red[1] >> 2));
1048 			vgaw(ba, VDAC_DATA, (u_char) (green[1] >> 2));
1049 			vgaw(ba, VDAC_DATA, (u_char) (blue[1] >> 2));
1050 		} else {
1051 			vgaw(ba, VDAC_DATA, (u_char) (blue[1] >> 2));
1052 			vgaw(ba, VDAC_DATA, (u_char) (green[1] >> 2));
1053 			vgaw(ba, VDAC_DATA, (u_char) (red[1] >> 2));
1054 		}
1055 
1056                 /* turn on/off sprite */
1057 		if (cl_cursprite.enable) {
1058 			WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x05);
1059 		} else {
1060 			WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x04);
1061 		}
1062 
1063 	}
1064 	if (data->set & GRFSPRSET_ENABLE) {
1065 
1066 		if (data->enable == 1) {
1067 			WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x05);
1068 			cl_cursprite.enable = 1;
1069 		} else {
1070 			WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x04);
1071 			cl_cursprite.enable = 0;
1072 		}
1073 
1074 	}
1075 	if (data->set & GRFSPRSET_POS) {
1076 
1077                 /* force placement */
1078                 cl_cursprite.pos.x = -1;
1079                 cl_cursprite.pos.y = -1;
1080 
1081                 /* do it */
1082                 cl_setmousepos(gp, &data->pos);
1083 
1084 	}
1085 	return (0);
1086 }
1087 
1088 static int
1089 cl_getspritemax(struct grf_softc *gp, struct grf_position *data)
1090 {
1091 	if (gp->g_display.gd_planes == 24)
1092 		return (EINVAL);
1093 	data->x = 64;
1094 	data->y = 64;
1095 	return (0);
1096 }
1097 
1098 int
1099 cl_setmonitor(struct grf_softc *gp, struct grfvideo_mode *gv)
1100 {
1101 	struct grfvideo_mode *md;
1102 
1103         if (!cl_mondefok(gv))
1104                 return(EINVAL);
1105 
1106 #ifdef CL5426CONSOLE
1107 	/* handle interactive setting of console mode */
1108 	if (gv->mode_num == 255) {
1109 		memcpy(&clconsole_mode.gv, gv, sizeof(struct grfvideo_mode));
1110 		clconsole_mode.gv.hblank_start /= 8;
1111 		clconsole_mode.gv.hsync_start /= 8;
1112 		clconsole_mode.gv.hsync_stop /= 8;
1113 		clconsole_mode.gv.htotal /= 8;
1114 		clconsole_mode.rows = gv->disp_height / clconsole_mode.fy;
1115 		clconsole_mode.cols = gv->disp_width / clconsole_mode.fx;
1116 		if (!(gp->g_flags & GF_GRFON))
1117 			cl_load_mon(gp, &clconsole_mode);
1118 #if NITE > 0
1119 		ite_reinit(gp->g_itedev);
1120 #endif
1121 		return (0);
1122 	}
1123 #endif
1124 
1125 	md = monitor_def + (gv->mode_num - 1);
1126 	memcpy(md, gv, sizeof(struct grfvideo_mode));
1127 
1128 	/* adjust pixel oriented values to internal rep. */
1129 
1130 	md->hblank_start /= 8;
1131 	md->hsync_start /= 8;
1132 	md->hsync_stop /= 8;
1133 	md->htotal /= 8;
1134 
1135 	return (0);
1136 }
1137 
1138 int
1139 cl_getcmap(struct grf_softc *gfp, struct grf_colormap *cmap)
1140 {
1141 	volatile unsigned char *ba;
1142 	u_char  red[256], green[256], blue[256], *rp, *gp, *bp;
1143 	short   x;
1144 	int     error;
1145 
1146 	if (cmap->count == 0 || cmap->index >= 256)
1147 		return 0;
1148 
1149 	if (cmap->count > 256 - cmap->index)
1150 		cmap->count = 256 - cmap->index;
1151 
1152 	ba = gfp->g_regkva;
1153 	/* first read colors out of the chip, then copyout to userspace */
1154 	vgaw(ba, VDAC_ADDRESS_R, cmap->index);
1155 	x = cmap->count - 1;
1156 
1157 /*
1158  * Some sort 'o Magic. Spectrum has some changes on the board to speed
1159  * up 15 and 16Bit modes. They can access these modes with easy-to-program
1160  * rgbrgbrgb instead of rrrgggbbb. Side effect: when in 8Bit mode, rgb
1161  * is swapped to bgr. I wonder if we need to check for 8Bit though, ill
1162  */
1163 
1164 /*
1165  * The source for the above comment is somewhat unknown to me.
1166  * The Spectrum, Piccolo and PiccoloSD64 have the analog Red and Blue
1167  * lines swapped. In 24BPP this provides RGB instead of BGR as it would
1168  * be native to the chipset. This requires special programming for the
1169  * CLUT in 8BPP to compensate and avoid false colors.
1170  * I didn't find any special stuff for 15 and 16BPP though, crest.
1171  */
1172 
1173 	switch (cltype) {
1174 	    case SPECTRUM:
1175 	    case PICCOLO:
1176 		rp = blue + cmap->index;
1177 		gp = green + cmap->index;
1178 		bp = red + cmap->index;
1179 		break;
1180 	    case PICASSO:
1181 		rp = red + cmap->index;
1182 		gp = green + cmap->index;
1183 		bp = blue + cmap->index;
1184 		break;
1185 	    default:
1186 		rp = gp = bp = 0;
1187 		break;
1188 	}
1189 
1190 	do {
1191 		*rp++ = vgar(ba, VDAC_DATA) << 2;
1192 		*gp++ = vgar(ba, VDAC_DATA) << 2;
1193 		*bp++ = vgar(ba, VDAC_DATA) << 2;
1194 	} while (x-- > 0);
1195 
1196 	if (!(error = copyout(red + cmap->index, cmap->red, cmap->count))
1197 	    && !(error = copyout(green + cmap->index, cmap->green, cmap->count))
1198 	    && !(error = copyout(blue + cmap->index, cmap->blue, cmap->count)))
1199 		return (0);
1200 
1201 	return (error);
1202 }
1203 
1204 int
1205 cl_putcmap(struct grf_softc *gfp, struct grf_colormap *cmap)
1206 {
1207 	volatile unsigned char *ba;
1208 	u_char  red[256], green[256], blue[256], *rp, *gp, *bp;
1209 	short   x;
1210 	int     error;
1211 
1212 	if (cmap->count == 0 || cmap->index >= 256)
1213 		return (0);
1214 
1215 	if (cmap->count > 256 - cmap->index)
1216 		cmap->count = 256 - cmap->index;
1217 
1218 	/* first copy the colors into kernelspace */
1219 	if (!(error = copyin(cmap->red, red + cmap->index, cmap->count))
1220 	    && !(error = copyin(cmap->green, green + cmap->index, cmap->count))
1221 	    && !(error = copyin(cmap->blue, blue + cmap->index, cmap->count))) {
1222 		ba = gfp->g_regkva;
1223 		vgaw(ba, VDAC_ADDRESS_W, cmap->index);
1224 		x = cmap->count - 1;
1225 
1226 		switch (cltype) {
1227 		    case SPECTRUM:
1228 		    case PICCOLO:
1229 			rp = blue + cmap->index;
1230 			gp = green + cmap->index;
1231 			bp = red + cmap->index;
1232 			break;
1233 		    case PICASSO:
1234 			rp = red + cmap->index;
1235 			gp = green + cmap->index;
1236 			bp = blue + cmap->index;
1237 			break;
1238 		    default:
1239 			rp = gp = bp = 0;
1240 			break;
1241 		}
1242 
1243 		do {
1244 			vgaw(ba, VDAC_DATA, *rp++ >> 2);
1245 			vgaw(ba, VDAC_DATA, *gp++ >> 2);
1246 			vgaw(ba, VDAC_DATA, *bp++ >> 2);
1247 		} while (x-- > 0);
1248 		return (0);
1249 	} else
1250 		return (error);
1251 }
1252 
1253 
1254 int
1255 cl_toggle(struct grf_softc *gp, unsigned short wopp)
1256 	/* wopp:	 don't need that one yet, ill */
1257 {
1258 	volatile void *ba;
1259 
1260 	ba = gp->g_regkva;
1261 
1262 	if (cl_pass_toggle) {
1263 		RegOffpass(ba);
1264 	} else {
1265 		RegOnpass(ba);
1266 	}
1267 	return (0);
1268 }
1269 
1270 static void
1271 cl_CompFQ(u_int fq, u_char *num, u_char *denom, u_char *clkdoub)
1272 {
1273 #define OSC     14318180
1274 /* OK, here's what we're doing here:
1275  *
1276  *             OSC * NUMERATOR
1277  *      VCLK = -------------------  Hz
1278  *             DENOMINATOR * (1+P)
1279  *
1280  * so we're given VCLK and we should give out some useful
1281  * values....
1282  *
1283  * NUMERATOR is 7 bits wide
1284  * DENOMINATOR is 5 bits wide with bit P in the same char as bit 0.
1285  *
1286  * We run through all the possible combinations and
1287  * return the values which deviate the least from the chosen frequency.
1288  *
1289  */
1290 #define OSC     14318180
1291 #define count(n,d,p)    ((OSC * n)/(d * (1+p)))
1292 
1293 	unsigned char n, d, p, minn, mind, minp = 0;
1294 	unsigned long err, minerr;
1295 
1296 /*
1297 numer = 0x00 - 0x7f
1298 denom = 0x00 - 0x1f (1) 0x20 - 0x3e (even)
1299 */
1300 
1301 	/* find lowest error in 6144 iterations. */
1302 	minerr = fq;
1303 	minn = 0;
1304 	mind = 0;
1305 	p = 0;
1306 
1307 	if ((cl_64bit == 1) && (fq >= 86000000))
1308 	{
1309 		for (d = 1; d < 0x20; d++) {
1310 			for (n = 1; n < 0x80; n++) {
1311 				err = abs(count(n, d, 0) - fq);
1312 				if (err < minerr) {
1313 					minerr = err;
1314 					minn = n;
1315 					mind = d;
1316 					minp = 1;
1317 				}
1318 			}
1319 		}
1320 		*clkdoub = 1;
1321 	}
1322 	else {
1323 		for (d = 1; d < 0x20; d++) {
1324 			for (n = 1; n < 0x80; n++) {
1325 				err = abs(count(n, d, p) - fq);
1326 				if (err < minerr) {
1327 					minerr = err;
1328 					minn = n;
1329 					mind = d;
1330 					minp = p;
1331 				}
1332 			}
1333 			if (d == 0x1f && p == 0) {
1334 				p = 1;
1335 				d = 0x0f;
1336 			}
1337 		}
1338 		*clkdoub = 0;
1339 	}
1340 
1341 	*num = minn;
1342 	*denom = (mind << 1) | minp;
1343 	if (minerr > 500000)
1344 		printf("Warning: CompFQ minimum error = %ld\n", minerr);
1345 	return;
1346 }
1347 
1348 int
1349 cl_mondefok(struct grfvideo_mode *gv)
1350 {
1351         unsigned long maxpix;
1352 
1353 	if (gv->mode_num < 1 || gv->mode_num > monitor_def_max)
1354                 if (gv->mode_num != 255 || gv->depth != 4)
1355                         return(0);
1356 
1357 	switch (gv->depth) {
1358 	    case 4:
1359                 if (gv->mode_num != 255)
1360                         return(0);
1361 	    case 1:
1362 	    case 8:
1363 		maxpix = cl_maxpixelclock;
1364 		if (cl_64bit == 1)
1365 		{
1366 			if (cltype == PICASSO) /* Picasso IV */
1367 				maxpix = 135000000;
1368 			else                   /* Piccolo SD64 */
1369 				maxpix = 110000000;
1370 		}
1371                 break;
1372 	    case 15:
1373 	    case 16:
1374 		if (cl_64bit == 1)
1375 	                maxpix = 85000000;
1376 		else
1377 	                maxpix = cl_maxpixelclock - (cl_maxpixelclock / 3);
1378                 break;
1379 	    case 24:
1380 		if ((cltype == PICASSO) && (cl_64bit == 1))
1381 	                maxpix = 85000000;
1382 		else
1383 	                maxpix = cl_maxpixelclock / 3;
1384                 break;
1385 	    case 32:
1386 		if ((cltype == PICCOLO) && (cl_64bit == 1))
1387 	                maxpix = 50000000;
1388 		else
1389 	                maxpix = 0;
1390                 break;
1391 	default:
1392 		printf("grfcl: Illegal depth in mode %d\n",
1393 			(int) gv->mode_num);
1394 		return (0);
1395 	}
1396 
1397         if (gv->pixel_clock > maxpix) {
1398 		printf("grfcl: Pixelclock too high in mode %d\n",
1399 			(int) gv->mode_num);
1400                 return (0);
1401 	}
1402 
1403 	if (gv->disp_flags & GRF_FLAGS_SYNC_ON_GREEN) {
1404 		printf("grfcl: sync-on-green is not supported\n");
1405 		return (0);
1406 	}
1407 
1408         return (1);
1409 }
1410 
1411 int
1412 cl_load_mon(struct grf_softc *gp, struct grfcltext_mode *md)
1413 {
1414 	struct grfvideo_mode *gv;
1415 	struct grfinfo *gi;
1416 	volatile void *ba, *fb;
1417 	unsigned char num0, denom0, clkdoub;
1418 	unsigned short HT, HDE, HBS, HBE, HSS, HSE, VDE, VBS, VBE, VSS,
1419 	        VSE, VT;
1420 	int	clkmul, clkmode;
1421 	int	vmul;
1422 	int	sr15;
1423 	unsigned char hvsync_pulse;
1424 	char    TEXT;
1425 
1426 	/* identity */
1427 	gv = &md->gv;
1428 	TEXT = (gv->depth == 4);
1429 
1430 	if (!cl_mondefok(gv)) {
1431 		printf("grfcl: Monitor definition not ok\n");
1432 		return (0);
1433 	}
1434 
1435 	ba = gp->g_regkva;
1436 	fb = gp->g_fbkva;
1437 
1438 	/* provide all needed information in grf device-independent locations */
1439 	gp->g_data = (void *) gv;
1440 	gi = &gp->g_display;
1441 	gi->gd_regaddr = (void *) kvtop(__UNVOLATILE(ba));
1442 	gi->gd_regsize = 64 * 1024;
1443 	gi->gd_fbaddr = (void *) kvtop(__UNVOLATILE(fb));
1444 	gi->gd_fbsize = cl_fbsize;
1445 	gi->gd_colors = 1 << gv->depth;
1446 	gi->gd_planes = gv->depth;
1447 	gi->gd_fbwidth = gv->disp_width;
1448 	gi->gd_fbheight = gv->disp_height;
1449 	gi->gd_fbx = 0;
1450 	gi->gd_fby = 0;
1451 	if (TEXT) {
1452 		gi->gd_dwidth = md->fx * md->cols;
1453 		gi->gd_dheight = md->fy * md->rows;
1454 	} else {
1455 		gi->gd_dwidth = gv->disp_width;
1456 		gi->gd_dheight = gv->disp_height;
1457 	}
1458 	gi->gd_dx = 0;
1459 	gi->gd_dy = 0;
1460 
1461 	/* get display mode parameters */
1462 
1463 	HBS = gv->hblank_start;
1464 	HSS = gv->hsync_start;
1465 	HSE = gv->hsync_stop;
1466 	HBE = gv->htotal - 1;
1467 	HT = gv->htotal;
1468 	VBS = gv->vblank_start;
1469 	VSS = gv->vsync_start;
1470 	VSE = gv->vsync_stop;
1471 	VBE = gv->vtotal - 1;
1472 	VT = gv->vtotal;
1473 
1474 	if (TEXT)
1475 		HDE = ((gv->disp_width + md->fx - 1) / md->fx) - 1;
1476 	else
1477 		HDE = (gv->disp_width + 3) / 8 - 1;	/* HBS; */
1478 	VDE = gv->disp_height - 1;
1479 
1480 	/* adjustments */
1481 	switch (gv->depth) {
1482 	    case 8:
1483 		clkmul = 1;
1484 		clkmode = 0x0;
1485 		break;
1486 	    case 15:
1487 	    case 16:
1488 		clkmul = 1;
1489 		clkmode = 0x6;
1490 		break;
1491 	    case 24:
1492 		if ((cltype == PICASSO) && (cl_64bit == 1))	/* Picasso IV */
1493 			clkmul = 1;
1494 		else
1495 			clkmul = 3;
1496 		clkmode = 0x4;
1497 		break;
1498 	    case 32:
1499 		clkmul = 1;
1500 		clkmode = 0x8;
1501 		break;
1502 	    default:
1503 		clkmul = 1;
1504 		clkmode = 0x0;
1505 		break;
1506 	}
1507 
1508 	if ((VT > 1023) && (!(gv->disp_flags & GRF_FLAGS_LACE))) {
1509 		WCrt(ba, CRT_ID_MODE_CONTROL, 0xe7);
1510 	} else
1511 		WCrt(ba, CRT_ID_MODE_CONTROL, 0xe3);
1512 
1513 	vmul = 2;
1514 	if ((VT > 1023) || (gv->disp_flags & GRF_FLAGS_LACE))
1515 		vmul = 1;
1516 	if (gv->disp_flags & GRF_FLAGS_DBLSCAN)
1517 		vmul = 4;
1518 
1519 	VDE = VDE * vmul / 2;
1520 	VBS = VBS * vmul / 2;
1521 	VSS = VSS * vmul / 2;
1522 	VSE = VSE * vmul / 2;
1523 	VBE = VBE * vmul / 2;
1524 	VT  = VT * vmul / 2;
1525 
1526 	WSeq(ba, SEQ_ID_MEMORY_MODE, (TEXT || (gv->depth == 1)) ? 0x06 : 0x0e);
1527 	if (cl_64bit == 1) {
1528 	    if (TEXT || (gv->depth == 1))
1529 		sr15 = 0xd0;
1530 	    else
1531 		sr15 = ((cl_fbsize / 0x100000 == 2) ? 0x38 : 0xb8);
1532 	    WSeq(ba, SEQ_ID_CONF_RBACK, 0x00);
1533 	} else {
1534 		sr15 = (TEXT || (gv->depth == 1)) ? 0xd0 : 0xb0;
1535 		sr15 &= ((cl_fbsize / 0x100000) == 2) ? 0xff : 0x7f;
1536 	}
1537 	WSeq(ba, SEQ_ID_DRAM_CNTL, sr15);
1538 	WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x00);
1539 	WSeq(ba, SEQ_ID_MAP_MASK, (gv->depth == 1) ? 0x01 : 0xff);
1540 	WSeq(ba, SEQ_ID_CHAR_MAP_SELECT, 0x00);
1541 
1542 	/* Set clock */
1543 
1544 	cl_CompFQ(gv->pixel_clock * clkmul, &num0, &denom0, &clkdoub);
1545 
1546 	/* Horizontal/Vertical Sync Pulse */
1547 	hvsync_pulse = vgar(ba, GREG_MISC_OUTPUT_R);
1548 	if (gv->disp_flags & GRF_FLAGS_PHSYNC)
1549 		hvsync_pulse &= ~0x40;
1550 	else
1551 		hvsync_pulse |= 0x40;
1552 	if (gv->disp_flags & GRF_FLAGS_PVSYNC)
1553 		hvsync_pulse &= ~0x80;
1554 	else
1555 		hvsync_pulse |= 0x80;
1556 	vgaw(ba, GREG_MISC_OUTPUT_W, hvsync_pulse);
1557 
1558 	if (clkdoub) {
1559 		HDE /= 2;
1560 		HBS /= 2;
1561 		HSS /= 2;
1562 		HSE /= 2;
1563 		HBE /= 2;
1564 		HT  /= 2;
1565 		clkmode = 0x6;
1566 	}
1567 
1568 	WSeq(ba, SEQ_ID_VCLK_3_NUM, num0);
1569 	WSeq(ba, SEQ_ID_VCLK_3_DENOM, denom0);
1570 
1571 	/* load display parameters into board */
1572 
1573 	WCrt(ba, CRT_ID_HOR_TOTAL, HT);
1574 	WCrt(ba, CRT_ID_HOR_DISP_ENA_END, ((HDE >= HBS) ? HBS - 1 : HDE));
1575 	WCrt(ba, CRT_ID_START_HOR_BLANK, HBS);
1576 	WCrt(ba, CRT_ID_END_HOR_BLANK, (HBE & 0x1f) | 0x80);	/* | 0x80? */
1577 	WCrt(ba, CRT_ID_START_HOR_RETR, HSS);
1578 	WCrt(ba, CRT_ID_END_HOR_RETR,
1579 	    (HSE & 0x1f) |
1580 	    ((HBE & 0x20) ? 0x80 : 0x00));
1581 	WCrt(ba, CRT_ID_VER_TOTAL, VT);
1582 	WCrt(ba, CRT_ID_OVERFLOW,
1583 	    0x10 |
1584 	    ((VT & 0x100) ? 0x01 : 0x00) |
1585 	    ((VDE & 0x100) ? 0x02 : 0x00) |
1586 	    ((VSS & 0x100) ? 0x04 : 0x00) |
1587 	    ((VBS & 0x100) ? 0x08 : 0x00) |
1588 	    ((VT & 0x200) ? 0x20 : 0x00) |
1589 	    ((VDE & 0x200) ? 0x40 : 0x00) |
1590 	    ((VSS & 0x200) ? 0x80 : 0x00));
1591 
1592 	WCrt(ba, CRT_ID_CHAR_HEIGHT,
1593 	    0x40 |		/* TEXT ? 0x00 ??? */
1594 	    ((gv->disp_flags & GRF_FLAGS_DBLSCAN) ? 0x80 : 0x00) |
1595 	    ((VBS & 0x200) ? 0x20 : 0x00) |
1596 	    (TEXT ? ((md->fy - 1) & 0x1f) : 0x00));
1597 
1598 	/* text cursor */
1599 
1600 	if (TEXT) {
1601 #if CL_ULCURSOR
1602 		WCrt(ba, CRT_ID_CURSOR_START, (md->fy & 0x1f) - 2);
1603 		WCrt(ba, CRT_ID_CURSOR_END, (md->fy & 0x1f) - 1);
1604 #else
1605 		WCrt(ba, CRT_ID_CURSOR_START, 0x00);
1606 		WCrt(ba, CRT_ID_CURSOR_END, md->fy & 0x1f);
1607 #endif
1608 		WCrt(ba, CRT_ID_UNDERLINE_LOC, (md->fy - 1) & 0x1f);
1609 
1610 		WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, 0x00);
1611 		WCrt(ba, CRT_ID_CURSOR_LOC_LOW, 0x00);
1612 	}
1613 	WCrt(ba, CRT_ID_START_ADDR_HIGH, 0x00);
1614 	WCrt(ba, CRT_ID_START_ADDR_LOW, 0x00);
1615 
1616 	WCrt(ba, CRT_ID_START_VER_RETR, VSS);
1617 	WCrt(ba, CRT_ID_END_VER_RETR, (VSE & 0x0f) | 0x20);
1618 	WCrt(ba, CRT_ID_VER_DISP_ENA_END, VDE);
1619 	WCrt(ba, CRT_ID_START_VER_BLANK, VBS);
1620 	WCrt(ba, CRT_ID_END_VER_BLANK, VBE);
1621 
1622 	WCrt(ba, CRT_ID_LINE_COMPARE, 0xff);
1623 	WCrt(ba, CRT_ID_LACE_END, HT / 2);	/* MW/16 */
1624 	WCrt(ba, CRT_ID_LACE_CNTL,
1625 	    ((gv->disp_flags & GRF_FLAGS_LACE) ? 0x01 : 0x00) |
1626 	    ((HBE & 0x40) ? 0x10 : 0x00) |
1627 	    ((HBE & 0x80) ? 0x20 : 0x00) |
1628 	    ((VBE & 0x100) ? 0x40 : 0x00) |
1629 	    ((VBE & 0x200) ? 0x80 : 0x00));
1630 
1631 	WGfx(ba, GCT_ID_GRAPHICS_MODE,
1632 	    ((TEXT || (gv->depth == 1)) ? 0x00 : 0x40));
1633 	WGfx(ba, GCT_ID_MISC, (TEXT ? 0x04 : 0x01));
1634 
1635 	WSeq(ba, SEQ_ID_EXT_SEQ_MODE,
1636 	    ((TEXT || (gv->depth == 1)) ? 0x00 : 0x01) |
1637 	    ((cltype == PICASSO) ? 0x20 : 0x80) | clkmode);
1638 
1639 	/* write 0x00 to VDAC_MASK before accessing HDR this helps
1640 	   sometimes, out of "secret" application note (crest) */
1641 	vgaw(ba, VDAC_MASK, 0);
1642 	/* reset HDR "magic" access counter (crest) */
1643 	vgar(ba, VDAC_ADDRESS);
1644 
1645 	delay(200000);
1646 	vgar(ba, VDAC_MASK);
1647 	delay(200000);
1648 	vgar(ba, VDAC_MASK);
1649 	delay(200000);
1650 	vgar(ba, VDAC_MASK);
1651 	delay(200000);
1652 	vgar(ba, VDAC_MASK);
1653 	delay(200000);
1654 	switch (gv->depth) {
1655 	    case 1:
1656 	    case 4:		/* text */
1657 		vgaw(ba, VDAC_MASK, 0);
1658 		HDE = gv->disp_width / 16;
1659 		break;
1660 	    case 8:
1661 		if (clkdoub)
1662 			vgaw(ba, VDAC_MASK, 0x4a); /* Clockdouble Magic */
1663 		else
1664 			vgaw(ba, VDAC_MASK, 0);
1665 		HDE = gv->disp_width / 8;
1666 		break;
1667 	    case 15:
1668 		vgaw(ba, VDAC_MASK, 0xd0);
1669 		HDE = gv->disp_width / 4;
1670 		break;
1671 	    case 16:
1672 		vgaw(ba, VDAC_MASK, 0xc1);
1673 		HDE = gv->disp_width / 4;
1674 		break;
1675 	    case 24:
1676 		vgaw(ba, VDAC_MASK, 0xc5);
1677 		HDE = (gv->disp_width / 8) * 3;
1678 		break;
1679 	    case 32:
1680 		vgaw(ba, VDAC_MASK, 0xc5);
1681 		HDE = (gv->disp_width / 4);
1682 		break;
1683 	}
1684 
1685 	/* reset HDR "magic" access counter (crest) */
1686 	vgar(ba, VDAC_ADDRESS);
1687 	/* then enable all bit in VDAC_MASK afterwards (crest) */
1688 	vgaw(ba, VDAC_MASK, 0xff);
1689 
1690 	WCrt(ba, CRT_ID_OFFSET, HDE);
1691 	if (cl_64bit == 1) {
1692 		WCrt(ba, CRT_ID_SYNC_ADJ_GENLOCK, 0x00);
1693 		WCrt(ba, CRT_ID_OVERLAY_EXT_CTRL_REG, 0x40);
1694 	}
1695 	WCrt(ba, CRT_ID_EXT_DISP_CNTL,
1696 	    ((TEXT && gv->pixel_clock > 29000000) ? 0x40 : 0x00) |
1697 	    0x22 |
1698 	    ((HDE > 0xff) ? 0x10 : 0x00));
1699 
1700 	WAttr(ba, ACT_ID_ATTR_MODE_CNTL, (TEXT ? 0x0a : 0x01));
1701 	WAttr(ba, 0x20 | ACT_ID_COLOR_PLANE_ENA,
1702 	    (gv->depth == 1) ? 0x01 : 0x0f);
1703 
1704 	/* text initialization */
1705 
1706 	if (TEXT) {
1707 		cl_inittextmode(gp);
1708 	}
1709 	WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x14);
1710 	WSeq(ba, SEQ_ID_CLOCKING_MODE, 0x01);
1711 	cl_blanked = 0;
1712 
1713 	/* Pass-through */
1714 
1715 	RegOffpass(ba);
1716 
1717 	return (1);
1718 }
1719 
1720 void
1721 cl_inittextmode(struct grf_softc *gp)
1722 {
1723 	struct grfcltext_mode *tm = (struct grfcltext_mode *) gp->g_data;
1724 	volatile unsigned char *ba = gp->g_regkva;
1725 	unsigned char *fb = __UNVOLATILE(gp->g_fbkva);
1726 	unsigned char *c, *f, y;
1727 	unsigned short z;
1728 
1729 
1730 	/* load text font into beginning of display memory. Each character
1731 	 * cell is 32 bytes long (enough for 4 planes) */
1732 
1733 	SetTextPlane(ba, 0x02);
1734         cl_memset(fb, 0, 256 * 32);
1735 	c = (unsigned char *) (fb) + (32 * tm->fdstart);
1736 	f = tm->fdata;
1737 	for (z = tm->fdstart; z <= tm->fdend; z++, c += (32 - tm->fy))
1738 		for (y = 0; y < tm->fy; y++)
1739 			*c++ = *f++;
1740 
1741 	/* clear out text/attr planes (three screens worth) */
1742 
1743 	SetTextPlane(ba, 0x01);
1744 	cl_memset(fb, 0x07, tm->cols * tm->rows * 3);
1745 	SetTextPlane(ba, 0x00);
1746 	cl_memset(fb, 0x20, tm->cols * tm->rows * 3);
1747 
1748 	/* print out a little init msg */
1749 
1750 	c = (unsigned char *) (fb) + (tm->cols - 16);
1751 	strcpy(c, "CIRRUS");
1752 	c[6] = 0x20;
1753 
1754 	/* set colors (B&W) */
1755 
1756 	vgaw(ba, VDAC_ADDRESS_W, 0);
1757 	for (z = 0; z < 256; z++) {
1758 		unsigned char r, g, b;
1759 
1760 		y = (z & 1) ? ((z > 7) ? 2 : 1) : 0;
1761 
1762 		if (cltype == PICASSO) {
1763 			r = clconscolors[y][0];
1764 			g = clconscolors[y][1];
1765 			b = clconscolors[y][2];
1766 		} else {
1767 			b = clconscolors[y][0];
1768 			g = clconscolors[y][1];
1769 			r = clconscolors[y][2];
1770 		}
1771 		vgaw(ba, VDAC_DATA, r >> 2);
1772 		vgaw(ba, VDAC_DATA, g >> 2);
1773 		vgaw(ba, VDAC_DATA, b >> 2);
1774 	}
1775 }
1776 
1777 void
1778 cl_memset(unsigned char *d, unsigned char c, int l)
1779 {
1780 	for (; l > 0; l--)
1781 		*d++ = c;
1782 }
1783 
1784 /*
1785  * Special wakeup/passthrough registers on graphics boards
1786  *
1787  * The methods have diverged a bit for each board, so
1788  * WPass(P) has been converted into a set of specific
1789  * inline functions.
1790  */
1791 static void
1792 RegWakeup(volatile void *ba)
1793 {
1794 
1795 	switch (cltype) {
1796 	    case SPECTRUM:
1797 		vgaw(ba, PASS_ADDRESS_W, 0x1f);
1798 		break;
1799 	    case PICASSO:
1800 		/* Picasso needs no wakeup */
1801 		break;
1802 	    case PICCOLO:
1803 		if (cl_64bit == 1)
1804 			vgaw(ba, PASS_ADDRESS_W, 0x1f);
1805 		else
1806 			vgaw(ba, PASS_ADDRESS_W, vgar(ba, PASS_ADDRESS) | 0x10);
1807 		break;
1808 	}
1809 	delay(200000);
1810 }
1811 
1812 static void
1813 RegOnpass(volatile void *ba)
1814 {
1815 
1816 	switch (cltype) {
1817 	    case SPECTRUM:
1818 		vgaw(ba, PASS_ADDRESS_W, 0x4f);
1819 		break;
1820 	    case PICASSO:
1821 		if (cl_64bit == 0)
1822 			vgaw(ba, PASS_ADDRESS_WP, 0x01);
1823 		break;
1824 	    case PICCOLO:
1825 		if (cl_64bit == 1)
1826 			vgaw(ba, PASS_ADDRESS_W, 0x4f);
1827 		else
1828 			vgaw(ba, PASS_ADDRESS_W, vgar(ba, PASS_ADDRESS) & 0xdf);
1829 		break;
1830 	}
1831 	cl_pass_toggle = 1;
1832 	delay(200000);
1833 }
1834 
1835 static void
1836 RegOffpass(volatile void *ba)
1837 {
1838 
1839 	switch (cltype) {
1840 	    case SPECTRUM:
1841 		vgaw(ba, PASS_ADDRESS_W, 0x6f);
1842 		break;
1843 	    case PICASSO:
1844 		if (cl_64bit == 0)
1845 			vgaw(ba, PASS_ADDRESS_W, 0xff);
1846 		break;
1847 	    case PICCOLO:
1848 		if (cl_64bit == 1)
1849 			vgaw(ba, PASS_ADDRESS_W, 0x6f);
1850 		else
1851 			vgaw(ba, PASS_ADDRESS_W, vgar(ba, PASS_ADDRESS) | 0x20);
1852 		break;
1853 	}
1854 	cl_pass_toggle = 0;
1855 	delay(200000);
1856 }
1857 
1858 #if NWSDISPLAY > 0
1859 static void
1860 cl_wscursor(void *c, int on, int row, int col)
1861 {
1862 	struct rasops_info *ri;
1863 	struct vcons_screen *scr;
1864 	struct grf_softc *gp;
1865 	volatile void *ba;
1866 	int offs;
1867 
1868 	ri = c;
1869 	scr = ri->ri_hw;
1870 	gp = scr->scr_cookie;
1871 	ba = gp->g_regkva;
1872 
1873 	if ((ri->ri_flg & RI_CURSOR) && !on) {
1874 		/* cursor was visible, but we want to remove it */
1875 		/*WCrt(ba, CRT_ID_CURSOR_START, | 0x20);*/
1876 		ri->ri_flg &= ~RI_CURSOR;
1877 	}
1878 
1879 	ri->ri_crow = row;
1880 	ri->ri_ccol = col;
1881 
1882 	if (on) {
1883 		/* move cursor to new location */
1884 		if (!(ri->ri_flg & RI_CURSOR)) {
1885 			/*WCrt(ba, CRT_ID_CURSOR_START, | 0x20);*/
1886 			ri->ri_flg |= RI_CURSOR;
1887 		}
1888 		offs = gp->g_rowoffset[row] + col;
1889 		WCrt(ba, CRT_ID_CURSOR_LOC_LOW, offs & 0xff);
1890 		WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, offs >> 8);
1891 	}
1892 }
1893 
1894 static void
1895 cl_wsputchar(void *c, int row, int col, u_int ch, long attr)
1896 {
1897 	struct rasops_info *ri;
1898 	struct vcons_screen *scr;
1899 	struct grf_softc *gp;
1900 	volatile unsigned char *ba, *cp;
1901 
1902 	ri = c;
1903 	scr = ri->ri_hw;
1904 	gp = scr->scr_cookie;
1905 	ba = gp->g_regkva;
1906 	cp = gp->g_fbkva;
1907 
1908 	cp += gp->g_rowoffset[row] + col;
1909 	SetTextPlane(ba, 0x00);
1910 	*cp = ch;
1911 	SetTextPlane(ba, 0x01);
1912 	*cp = attr;
1913 }
1914 
1915 static void
1916 cl_wscopycols(void *c, int row, int srccol, int dstcol, int ncols)
1917 {
1918 	volatile unsigned char *ba, *dst, *src;
1919 	struct rasops_info *ri;
1920 	struct vcons_screen *scr;
1921 	struct grf_softc *gp;
1922 	int i;
1923 
1924 	KASSERT(ncols > 0);
1925 	ri = c;
1926 	scr = ri->ri_hw;
1927 	gp = scr->scr_cookie;
1928 	ba = gp->g_regkva;
1929 	src = gp->g_fbkva;
1930 
1931 	src += gp->g_rowoffset[row];
1932 	dst = src;
1933 	src += srccol;
1934 	dst += dstcol;
1935 	if (srccol < dstcol) {
1936 		/* need to copy backwards */
1937 		src += ncols;
1938 		dst += ncols;
1939 		SetTextPlane(ba, 0x00);
1940 		for (i = 0; i < ncols; i++)
1941 			*(--dst) = *(--src);
1942 		src += ncols;
1943 		dst += ncols;
1944 		SetTextPlane(ba, 0x01);
1945 		for (i = 0; i < ncols; i++)
1946 			*(--dst) = *(--src);
1947 	} else {
1948 		SetTextPlane(ba, 0x00);
1949 		for (i = 0; i < ncols; i++)
1950 			*dst++ = *src++;
1951 		src -= ncols;
1952 		dst -= ncols;
1953 		SetTextPlane(ba, 0x01);
1954 		for (i = 0; i < ncols; i++)
1955 			*dst++ = *src++;
1956 	}
1957 }
1958 
1959 static void
1960 cl_wserasecols(void *c, int row, int startcol, int ncols, long fillattr)
1961 {
1962 	volatile unsigned char *ba, *cp;
1963 	struct rasops_info *ri;
1964 	struct vcons_screen *scr;
1965 	struct grf_softc *gp;
1966 	int i;
1967 
1968 	ri = c;
1969 	scr = ri->ri_hw;
1970 	gp = scr->scr_cookie;
1971 	ba = gp->g_regkva;
1972 	cp = gp->g_fbkva;
1973 
1974 	cp += gp->g_rowoffset[row] + startcol;
1975 	SetTextPlane(ba, 0x00);
1976 	for (i = 0; i < ncols; i++)
1977 		*cp++ = 0x20;
1978 	cp -= ncols;
1979 	SetTextPlane(ba, 0x01);
1980 	for (i = 0; i < ncols; i++)
1981 		*cp++ = 0x07;
1982 }
1983 
1984 static void
1985 cl_wscopyrows(void *c, int srcrow, int dstrow, int nrows)
1986 {
1987 	volatile unsigned char *ba, *dst, *src;
1988 	struct rasops_info *ri;
1989 	struct vcons_screen *scr;
1990 	struct grf_softc *gp;
1991 	int i, n;
1992 
1993 	KASSERT(nrows > 0);
1994 	ri = c;
1995 	scr = ri->ri_hw;
1996 	gp = scr->scr_cookie;
1997 	ba = gp->g_regkva;
1998 	src = dst = gp->g_fbkva;
1999 	n = ri->ri_cols * nrows;
2000 
2001 	if (srcrow < dstrow) {
2002 		/* need to copy backwards */
2003 		src += gp->g_rowoffset[srcrow + nrows];
2004 		dst += gp->g_rowoffset[dstrow + nrows];
2005 		SetTextPlane(ba, 0x00);
2006 		for (i = 0; i < n; i++)
2007 			*(--dst) = *(--src);
2008 		src += n;
2009 		dst += n;
2010 		SetTextPlane(ba, 0x01);
2011 		for (i = 0; i < n; i++)
2012 			*(--dst) = *(--src);
2013 	} else {
2014 		src += gp->g_rowoffset[srcrow];
2015 		dst += gp->g_rowoffset[dstrow];
2016 		SetTextPlane(ba, 0x00);
2017 		for (i = 0; i < n; i++)
2018 			*dst++ = *src++;
2019 		src -= n;
2020 		dst -= n;
2021 		SetTextPlane(ba, 0x01);
2022 		for (i = 0; i < n; i++)
2023 			*dst++ = *src++;
2024 	}
2025 }
2026 
2027 static void
2028 cl_wseraserows(void *c, int row, int nrows, long fillattr)
2029 {
2030 	volatile unsigned char *ba, *cp;
2031 	struct rasops_info *ri;
2032 	struct vcons_screen *scr;
2033 	struct grf_softc *gp;
2034 	int i, n;
2035 
2036 	ri = c;
2037 	scr = ri->ri_hw;
2038 	gp = scr->scr_cookie;
2039 	ba = gp->g_regkva;
2040 	cp = gp->g_fbkva;
2041 
2042 	cp += gp->g_rowoffset[row];
2043 	n = ri->ri_cols * nrows;
2044 	SetTextPlane(ba, 0x00);
2045 	for (i = 0; i < n; i++)
2046 		*cp++ = 0x20;
2047 	cp -= n;
2048 	SetTextPlane(ba, 0x01);
2049 	for (i = 0; i < n; i++)
2050 		*cp++ = 0x07;
2051 }
2052 
2053 static int
2054 cl_wsallocattr(void *c, int fg, int bg, int flg, long *attr)
2055 {
2056 
2057 	/* XXX color support? */
2058 	*attr = (flg & WSATTR_REVERSE) ? 0x70 : 0x07;
2059 	if (flg & WSATTR_UNDERLINE)	*attr = 0x01;
2060 	if (flg & WSATTR_HILIT)		*attr |= 0x08;
2061 	if (flg & WSATTR_BLINK)		*attr |= 0x80;
2062 	return 0;
2063 }
2064 
2065 /* our font does not support unicode extensions */
2066 static int
2067 cl_wsmapchar(void *c, int ch, unsigned int *cp)
2068 {
2069 
2070 	if (ch > 0 && ch < 256) {
2071 		*cp = ch;
2072 		return 5;
2073 	}
2074 	*cp = ' ';
2075 	return 0;
2076 }
2077 
2078 static int
2079 cl_wsioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
2080 {
2081 	struct vcons_data *vd;
2082 	struct grf_softc *gp;
2083 
2084 	vd = v;
2085 	gp = vd->cookie;
2086 
2087 	switch (cmd) {
2088 	case WSDISPLAYIO_GETCMAP:
2089 		/* Note: wsdisplay_cmap and grf_colormap have same format */
2090 		if (gp->g_display.gd_planes == 8)
2091 			return cl_getcmap(gp, (struct grf_colormap *)data);
2092 		return EINVAL;
2093 
2094 	case WSDISPLAYIO_PUTCMAP:
2095 		/* Note: wsdisplay_cmap and grf_colormap have same format */
2096 		if (gp->g_display.gd_planes == 8)
2097 			return cl_putcmap(gp, (struct grf_colormap *)data);
2098 		return EINVAL;
2099 
2100 	case WSDISPLAYIO_GVIDEO:
2101 		if (cl_isblank(gp))
2102 			*(u_int *)data = WSDISPLAYIO_VIDEO_OFF;
2103 		else
2104 			*(u_int *)data = WSDISPLAYIO_VIDEO_ON;
2105 		return 0;
2106 
2107 	case WSDISPLAYIO_SVIDEO:
2108 		return cl_blank(gp, *(u_int *)data == WSDISPLAYIO_VIDEO_ON);
2109 
2110 	case WSDISPLAYIO_SMODE:
2111 		if ((*(int *)data) != gp->g_wsmode) {
2112 			if (*(int *)data == WSDISPLAYIO_MODE_EMUL) {
2113 				/* load console text mode, redraw screen */
2114 				(void)cl_load_mon(gp, &clconsole_mode);
2115 				if (vd->active != NULL)
2116 					vcons_redraw_screen(vd->active);
2117 			} else {
2118 				/* switch to current graphics mode */
2119 				if (!cl_load_mon(gp,
2120 				    (struct grfcltext_mode *)monitor_current))
2121 					return EINVAL;
2122 			}
2123 			gp->g_wsmode = *(int *)data;
2124 		}
2125 		return 0;
2126 
2127 	case WSDISPLAYIO_GET_FBINFO:
2128 		return cl_get_fbinfo(gp, data);
2129 	}
2130 
2131 	/* handle this command hw-independent in grf(4) */
2132 	return grf_wsioctl(v, vs, cmd, data, flag, l);
2133 }
2134 
2135 /*
2136  * Fill the wsdisplayio_fbinfo structure with information from the current
2137  * graphics mode. Even when text mode is active.
2138  */
2139 static int
2140 cl_get_fbinfo(struct grf_softc *gp, struct wsdisplayio_fbinfo *fbi)
2141 {
2142 	struct grfvideo_mode *md;
2143 	uint32_t rbits, gbits, bbits;
2144 
2145 	md = monitor_current;
2146 
2147 	switch (md->depth) {
2148 	case 8:
2149 		fbi->fbi_bitsperpixel = 8;
2150 		rbits = gbits = bbits = 6;  /* keep gcc happy */
2151 		break;
2152 	case 15:
2153 		fbi->fbi_bitsperpixel = 16;
2154 		rbits = gbits = bbits = 5;
2155 		break;
2156 	case 16:
2157 		fbi->fbi_bitsperpixel = 16;
2158 		rbits = bbits = 5;
2159 		gbits = 6;
2160 		break;
2161 	case 24:
2162 		fbi->fbi_bitsperpixel = 24;
2163 		rbits = gbits = bbits = 8;
2164 		break;
2165 	default:
2166 		return EINVAL;
2167 	}
2168 
2169 	fbi->fbi_stride = (fbi->fbi_bitsperpixel / 8) * md->disp_width;
2170 	fbi->fbi_width = md->disp_width;
2171 	fbi->fbi_height = md->disp_height;
2172 
2173 	if (md->depth > 8) {
2174 		fbi->fbi_pixeltype = WSFB_RGB;
2175 		fbi->fbi_subtype.fbi_rgbmasks.red_offset = bbits + gbits;
2176 		fbi->fbi_subtype.fbi_rgbmasks.red_size = rbits;
2177 		fbi->fbi_subtype.fbi_rgbmasks.green_offset = bbits;
2178 		fbi->fbi_subtype.fbi_rgbmasks.green_size = gbits;
2179 		fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 0;
2180 		fbi->fbi_subtype.fbi_rgbmasks.blue_size = bbits;
2181 		fbi->fbi_subtype.fbi_rgbmasks.alpha_offset = 0;
2182 		fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 0;
2183 	} else {
2184 		fbi->fbi_pixeltype = WSFB_CI;
2185 		fbi->fbi_subtype.fbi_cmapinfo.cmap_entries = 1 << md->depth;
2186 	}
2187 
2188 	fbi->fbi_flags = 0;
2189 	fbi->fbi_fbsize = fbi->fbi_stride * fbi->fbi_height;
2190 	fbi->fbi_fboffset = 0;
2191 	return 0;
2192 }
2193 #endif	/* NWSDISPLAY > 0 */
2194 
2195 #endif /* NGRFCL */
2196