xref: /netbsd/sys/arch/amiga/dev/grf_cl.c (revision d99cf440)
1 /*	$NetBSD: grf_cl.c,v 1.56 2023/02/14 20:27:17 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.56 2023/02/14 20:27:17 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
grfclmatch(device_t parent,cfdata_t cf,void * aux)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
grfclattach(device_t parent,device_t self,void * aux)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
grfclprint(void * aux,const char * pnp)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
cl_boardinit(struct grf_softc * gp)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
cl_getvmode(struct grf_softc * gp,struct grfvideo_mode * vm)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
cl_setvmode(struct grf_softc * gp,unsigned mode)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
cl_off(struct grf_softc * gp)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
cl_blank(struct grf_softc * gp,int on)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
cl_isblank(struct grf_softc * gp)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
cl_mode(register struct grf_softc * gp,u_long cmd,void * arg,u_long a2,int a3)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
cl_ioctl(register struct grf_softc * gp,u_long cmd,void * data)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
cl_getmousepos(struct grf_softc * gp,struct grf_position * data)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
cl_writesprpos(volatile char * ba,short x,short y)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
writeshifted(volatile char * to,signed char shiftx,signed char shifty)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
cl_setmousepos(struct grf_softc * gp,struct grf_position * data)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 	volatile char *fb = gp->g_fbkva;
912         volatile char *sprite = fb + (cl_fbsize - 1024);
913 #endif
914 
915         /* no movement */
916 	if (cl_cursprite.pos.x == data->x && cl_cursprite.pos.y == data->y)
917 		return (0);
918 
919         /* current and previous real coordinates */
920 	rx = data->x - cl_cursprite.hot.x;
921 	ry = data->y - cl_cursprite.hot.y;
922 
923         /*
924 	 * if we are/were on an edge, create (un)shifted bitmap --
925          * ripped out optimization (not extremely worthwhile,
926          * and kind of buggy anyhow).
927          */
928 #ifdef CL_SHIFTSPRITE
929         if (rx < 0 || ry < 0 || prx < 0 || pry < 0) {
930                 writeshifted(sprite, rx < 0 ? -rx : 0, ry < 0 ? -ry : 0);
931         }
932 #endif
933 
934         /* do movement, save position */
935         cl_writesprpos(ba, rx < 0 ? 0 : rx, ry < 0 ? 0 : ry);
936 	cl_cursprite.pos.x = data->x;
937 	cl_cursprite.pos.y = data->y;
938 
939 	return (0);
940 }
941 
942 int
cl_getspriteinfo(struct grf_softc * gp,struct grf_spriteinfo * data)943 cl_getspriteinfo(struct grf_softc *gp, struct grf_spriteinfo *data)
944 {
945 	copyout(&cl_cursprite, data, sizeof(struct grf_spriteinfo));
946 	copyout(cl_cursprite.image, data->image, 64 * 8);
947 	copyout(cl_cursprite.mask, data->mask, 64 * 8);
948 	return (0);
949 }
950 
951 static int
cl_setspriteinfo(struct grf_softc * gp,struct grf_spriteinfo * data)952 cl_setspriteinfo(struct grf_softc *gp, struct grf_spriteinfo *data)
953 {
954 	volatile unsigned char *ba = gp->g_regkva, *fb = gp->g_fbkva;
955         volatile char *sprite = fb + (cl_fbsize - 1024);
956 
957 	if (data->set & GRFSPRSET_SHAPE) {
958 
959                 unsigned short dsx, dsy, i;
960                 unsigned long *di, *dm, *si, *sm;
961                 unsigned long ssi[128], ssm[128];
962                 struct grf_position gpos;
963 
964 
965                 /* check for a too large sprite (no clipping!) */
966                 dsy = data->size.y;
967                 dsx = data->size.x;
968                 if (dsy > 64 || dsx > 64)
969                         return(EINVAL);
970 
971                 /* prepare destination */
972                 di = (unsigned long *)cl_cursprite.image;
973                 dm = (unsigned long *)cl_cursprite.mask;
974                 cl_memset((unsigned char *)di, 0, 8*64);
975                 cl_memset((unsigned char *)dm, 0, 8*64);
976 
977                 /* two alternatives:  64 across, then it's
978                  * the same format we use, just copy.  Otherwise,
979                  * copy into tmp buf and recopy skipping the
980                  * unused 32 bits.
981                  */
982                 if ((dsx - 1) / 32) {
983                         copyin(data->image, di, 8 * dsy);
984                         copyin(data->mask, dm, 8 * dsy);
985                 } else {
986                         si = ssi; sm = ssm;
987                         copyin(data->image, si, 4 * dsy);
988                         copyin(data->mask, sm, 4 * dsy);
989                         for (i = 0; i < dsy; i++) {
990                                 *di = *si++;
991                                 *dm = *sm++;
992                                 di += 2;
993                                 dm += 2;
994                         }
995                 }
996 
997                 /* set size */
998 		cl_cursprite.size.x = data->size.x;
999 		cl_cursprite.size.y = data->size.y;
1000 
1001                 /* forcably load into board */
1002                 gpos.x = cl_cursprite.pos.x;
1003                 gpos.y = cl_cursprite.pos.y;
1004                 cl_cursprite.pos.x = -1;
1005                 cl_cursprite.pos.y = -1;
1006                 writeshifted(sprite, 0, 0);
1007                 cl_setmousepos(gp, &gpos);
1008 
1009 	}
1010 	if (data->set & GRFSPRSET_HOT) {
1011 
1012 		cl_cursprite.hot = data->hot;
1013 
1014 	}
1015 	if (data->set & GRFSPRSET_CMAP) {
1016 
1017 		u_char  red[2], green[2], blue[2];
1018 
1019 		copyin(data->cmap.red, red, 2);
1020 		copyin(data->cmap.green, green, 2);
1021 		copyin(data->cmap.blue, blue, 2);
1022 		memcpy(cl_cursprite.cmap.red, red, 2);
1023 		memcpy(cl_cursprite.cmap.green, green, 2);
1024 		memcpy(cl_cursprite.cmap.blue, blue, 2);
1025 
1026                 /* enable and load colors 256 & 257 */
1027 		WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x06);
1028 
1029                 /* 256 */
1030 		vgaw(ba, VDAC_ADDRESS_W, 0x00);
1031 		if (cltype == PICASSO) {
1032 			vgaw(ba, VDAC_DATA, (u_char) (red[0] >> 2));
1033 			vgaw(ba, VDAC_DATA, (u_char) (green[0] >> 2));
1034 			vgaw(ba, VDAC_DATA, (u_char) (blue[0] >> 2));
1035 		} else {
1036 			vgaw(ba, VDAC_DATA, (u_char) (blue[0] >> 2));
1037 			vgaw(ba, VDAC_DATA, (u_char) (green[0] >> 2));
1038 			vgaw(ba, VDAC_DATA, (u_char) (red[0] >> 2));
1039 		}
1040 
1041                 /* 257 */
1042 		vgaw(ba, VDAC_ADDRESS_W, 0x0f);
1043 		if (cltype == PICASSO) {
1044 			vgaw(ba, VDAC_DATA, (u_char) (red[1] >> 2));
1045 			vgaw(ba, VDAC_DATA, (u_char) (green[1] >> 2));
1046 			vgaw(ba, VDAC_DATA, (u_char) (blue[1] >> 2));
1047 		} else {
1048 			vgaw(ba, VDAC_DATA, (u_char) (blue[1] >> 2));
1049 			vgaw(ba, VDAC_DATA, (u_char) (green[1] >> 2));
1050 			vgaw(ba, VDAC_DATA, (u_char) (red[1] >> 2));
1051 		}
1052 
1053                 /* turn on/off sprite */
1054 		if (cl_cursprite.enable) {
1055 			WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x05);
1056 		} else {
1057 			WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x04);
1058 		}
1059 
1060 	}
1061 	if (data->set & GRFSPRSET_ENABLE) {
1062 
1063 		if (data->enable == 1) {
1064 			WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x05);
1065 			cl_cursprite.enable = 1;
1066 		} else {
1067 			WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x04);
1068 			cl_cursprite.enable = 0;
1069 		}
1070 
1071 	}
1072 	if (data->set & GRFSPRSET_POS) {
1073 
1074                 /* force placement */
1075                 cl_cursprite.pos.x = -1;
1076                 cl_cursprite.pos.y = -1;
1077 
1078                 /* do it */
1079                 cl_setmousepos(gp, &data->pos);
1080 
1081 	}
1082 	return (0);
1083 }
1084 
1085 static int
cl_getspritemax(struct grf_softc * gp,struct grf_position * data)1086 cl_getspritemax(struct grf_softc *gp, struct grf_position *data)
1087 {
1088 	if (gp->g_display.gd_planes == 24)
1089 		return (EINVAL);
1090 	data->x = 64;
1091 	data->y = 64;
1092 	return (0);
1093 }
1094 
1095 int
cl_setmonitor(struct grf_softc * gp,struct grfvideo_mode * gv)1096 cl_setmonitor(struct grf_softc *gp, struct grfvideo_mode *gv)
1097 {
1098 	struct grfvideo_mode *md;
1099 
1100         if (!cl_mondefok(gv))
1101                 return(EINVAL);
1102 
1103 #ifdef CL5426CONSOLE
1104 	/* handle interactive setting of console mode */
1105 	if (gv->mode_num == 255) {
1106 		memcpy(&clconsole_mode.gv, gv, sizeof(struct grfvideo_mode));
1107 		clconsole_mode.gv.hblank_start /= 8;
1108 		clconsole_mode.gv.hsync_start /= 8;
1109 		clconsole_mode.gv.hsync_stop /= 8;
1110 		clconsole_mode.gv.htotal /= 8;
1111 		clconsole_mode.rows = gv->disp_height / clconsole_mode.fy;
1112 		clconsole_mode.cols = gv->disp_width / clconsole_mode.fx;
1113 		if (!(gp->g_flags & GF_GRFON))
1114 			cl_load_mon(gp, &clconsole_mode);
1115 #if NITE > 0
1116 		ite_reinit(gp->g_itedev);
1117 #endif
1118 		return (0);
1119 	}
1120 #endif
1121 
1122 	md = monitor_def + (gv->mode_num - 1);
1123 	memcpy(md, gv, sizeof(struct grfvideo_mode));
1124 
1125 	/* adjust pixel oriented values to internal rep. */
1126 
1127 	md->hblank_start /= 8;
1128 	md->hsync_start /= 8;
1129 	md->hsync_stop /= 8;
1130 	md->htotal /= 8;
1131 
1132 	return (0);
1133 }
1134 
1135 int
cl_getcmap(struct grf_softc * gfp,struct grf_colormap * cmap)1136 cl_getcmap(struct grf_softc *gfp, struct grf_colormap *cmap)
1137 {
1138 	volatile unsigned char *ba;
1139 	u_char  red[256], green[256], blue[256], *rp, *gp, *bp;
1140 	short   x;
1141 	int     error;
1142 
1143 	if (cmap->count == 0 || cmap->index >= 256)
1144 		return 0;
1145 
1146 	if (cmap->count > 256 - cmap->index)
1147 		cmap->count = 256 - cmap->index;
1148 
1149 	ba = gfp->g_regkva;
1150 	/* first read colors out of the chip, then copyout to userspace */
1151 	vgaw(ba, VDAC_ADDRESS_R, cmap->index);
1152 	x = cmap->count - 1;
1153 
1154 /*
1155  * Some sort 'o Magic. Spectrum has some changes on the board to speed
1156  * up 15 and 16Bit modes. They can access these modes with easy-to-program
1157  * rgbrgbrgb instead of rrrgggbbb. Side effect: when in 8Bit mode, rgb
1158  * is swapped to bgr. I wonder if we need to check for 8Bit though, ill
1159  */
1160 
1161 /*
1162  * The source for the above comment is somewhat unknown to me.
1163  * The Spectrum, Piccolo and PiccoloSD64 have the analog Red and Blue
1164  * lines swapped. In 24BPP this provides RGB instead of BGR as it would
1165  * be native to the chipset. This requires special programming for the
1166  * CLUT in 8BPP to compensate and avoid false colors.
1167  * I didn't find any special stuff for 15 and 16BPP though, crest.
1168  */
1169 
1170 	switch (cltype) {
1171 	    case SPECTRUM:
1172 	    case PICCOLO:
1173 		rp = blue + cmap->index;
1174 		gp = green + cmap->index;
1175 		bp = red + cmap->index;
1176 		break;
1177 	    case PICASSO:
1178 		rp = red + cmap->index;
1179 		gp = green + cmap->index;
1180 		bp = blue + cmap->index;
1181 		break;
1182 	    default:
1183 		rp = gp = bp = 0;
1184 		break;
1185 	}
1186 
1187 	do {
1188 		*rp++ = vgar(ba, VDAC_DATA) << 2;
1189 		*gp++ = vgar(ba, VDAC_DATA) << 2;
1190 		*bp++ = vgar(ba, VDAC_DATA) << 2;
1191 	} while (x-- > 0);
1192 
1193 	if (!(error = copyout(red + cmap->index, cmap->red, cmap->count))
1194 	    && !(error = copyout(green + cmap->index, cmap->green, cmap->count))
1195 	    && !(error = copyout(blue + cmap->index, cmap->blue, cmap->count)))
1196 		return (0);
1197 
1198 	return (error);
1199 }
1200 
1201 int
cl_putcmap(struct grf_softc * gfp,struct grf_colormap * cmap)1202 cl_putcmap(struct grf_softc *gfp, struct grf_colormap *cmap)
1203 {
1204 	volatile unsigned char *ba;
1205 	u_char  red[256], green[256], blue[256], *rp, *gp, *bp;
1206 	short   x;
1207 	int     error;
1208 
1209 	if (cmap->count == 0 || cmap->index >= 256)
1210 		return (0);
1211 
1212 	if (cmap->count > 256 - cmap->index)
1213 		cmap->count = 256 - cmap->index;
1214 
1215 	/* first copy the colors into kernelspace */
1216 	if (!(error = copyin(cmap->red, red + cmap->index, cmap->count))
1217 	    && !(error = copyin(cmap->green, green + cmap->index, cmap->count))
1218 	    && !(error = copyin(cmap->blue, blue + cmap->index, cmap->count))) {
1219 		ba = gfp->g_regkva;
1220 		vgaw(ba, VDAC_ADDRESS_W, cmap->index);
1221 		x = cmap->count - 1;
1222 
1223 		switch (cltype) {
1224 		    case SPECTRUM:
1225 		    case PICCOLO:
1226 			rp = blue + cmap->index;
1227 			gp = green + cmap->index;
1228 			bp = red + cmap->index;
1229 			break;
1230 		    case PICASSO:
1231 			rp = red + cmap->index;
1232 			gp = green + cmap->index;
1233 			bp = blue + cmap->index;
1234 			break;
1235 		    default:
1236 			rp = gp = bp = 0;
1237 			break;
1238 		}
1239 
1240 		do {
1241 			vgaw(ba, VDAC_DATA, *rp++ >> 2);
1242 			vgaw(ba, VDAC_DATA, *gp++ >> 2);
1243 			vgaw(ba, VDAC_DATA, *bp++ >> 2);
1244 		} while (x-- > 0);
1245 		return (0);
1246 	} else
1247 		return (error);
1248 }
1249 
1250 
1251 int
cl_toggle(struct grf_softc * gp,unsigned short wopp)1252 cl_toggle(struct grf_softc *gp, unsigned short wopp)
1253 	/* wopp:	 don't need that one yet, ill */
1254 {
1255 	volatile void *ba;
1256 
1257 	ba = gp->g_regkva;
1258 
1259 	if (cl_pass_toggle) {
1260 		RegOffpass(ba);
1261 	} else {
1262 		RegOnpass(ba);
1263 	}
1264 	return (0);
1265 }
1266 
1267 static void
cl_CompFQ(u_int fq,u_char * num,u_char * denom,u_char * clkdoub)1268 cl_CompFQ(u_int fq, u_char *num, u_char *denom, u_char *clkdoub)
1269 {
1270 #define OSC     14318180
1271 /* OK, here's what we're doing here:
1272  *
1273  *             OSC * NUMERATOR
1274  *      VCLK = -------------------  Hz
1275  *             DENOMINATOR * (1+P)
1276  *
1277  * so we're given VCLK and we should give out some useful
1278  * values....
1279  *
1280  * NUMERATOR is 7 bits wide
1281  * DENOMINATOR is 5 bits wide with bit P in the same char as bit 0.
1282  *
1283  * We run through all the possible combinations and
1284  * return the values which deviate the least from the chosen frequency.
1285  *
1286  */
1287 #define OSC     14318180
1288 #define count(n,d,p)    ((OSC * n)/(d * (1+p)))
1289 
1290 	unsigned char n, d, p, minn, mind, minp = 0;
1291 	unsigned long err, minerr;
1292 
1293 /*
1294 numer = 0x00 - 0x7f
1295 denom = 0x00 - 0x1f (1) 0x20 - 0x3e (even)
1296 */
1297 
1298 	/* find lowest error in 6144 iterations. */
1299 	minerr = fq;
1300 	minn = 0;
1301 	mind = 0;
1302 	p = 0;
1303 
1304 	if ((cl_64bit == 1) && (fq >= 86000000))
1305 	{
1306 		for (d = 1; d < 0x20; d++) {
1307 			for (n = 1; n < 0x80; n++) {
1308 				err = abs(count(n, d, 0) - fq);
1309 				if (err < minerr) {
1310 					minerr = err;
1311 					minn = n;
1312 					mind = d;
1313 					minp = 1;
1314 				}
1315 			}
1316 		}
1317 		*clkdoub = 1;
1318 	}
1319 	else {
1320 		for (d = 1; d < 0x20; d++) {
1321 			for (n = 1; n < 0x80; n++) {
1322 				err = abs(count(n, d, p) - fq);
1323 				if (err < minerr) {
1324 					minerr = err;
1325 					minn = n;
1326 					mind = d;
1327 					minp = p;
1328 				}
1329 			}
1330 			if (d == 0x1f && p == 0) {
1331 				p = 1;
1332 				d = 0x0f;
1333 			}
1334 		}
1335 		*clkdoub = 0;
1336 	}
1337 
1338 	*num = minn;
1339 	*denom = (mind << 1) | minp;
1340 	if (minerr > 500000)
1341 		printf("Warning: CompFQ minimum error = %ld\n", minerr);
1342 	return;
1343 }
1344 
1345 int
cl_mondefok(struct grfvideo_mode * gv)1346 cl_mondefok(struct grfvideo_mode *gv)
1347 {
1348         unsigned long maxpix;
1349 
1350 	if (gv->mode_num < 1 || gv->mode_num > monitor_def_max)
1351                 if (gv->mode_num != 255 || gv->depth != 4)
1352                         return(0);
1353 
1354 	switch (gv->depth) {
1355 	    case 4:
1356                 if (gv->mode_num != 255)
1357                         return(0);
1358 	    case 1:
1359 	    case 8:
1360 		maxpix = cl_maxpixelclock;
1361 		if (cl_64bit == 1)
1362 		{
1363 			if (cltype == PICASSO) /* Picasso IV */
1364 				maxpix = 135000000;
1365 			else                   /* Piccolo SD64 */
1366 				maxpix = 110000000;
1367 		}
1368                 break;
1369 	    case 15:
1370 	    case 16:
1371 		if (cl_64bit == 1)
1372 	                maxpix = 85000000;
1373 		else
1374 	                maxpix = cl_maxpixelclock - (cl_maxpixelclock / 3);
1375                 break;
1376 	    case 24:
1377 		if ((cltype == PICASSO) && (cl_64bit == 1))
1378 	                maxpix = 85000000;
1379 		else
1380 	                maxpix = cl_maxpixelclock / 3;
1381                 break;
1382 	    case 32:
1383 		if ((cltype == PICCOLO) && (cl_64bit == 1))
1384 	                maxpix = 50000000;
1385 		else
1386 	                maxpix = 0;
1387                 break;
1388 	default:
1389 		printf("grfcl: Illegal depth in mode %d\n",
1390 			(int) gv->mode_num);
1391 		return (0);
1392 	}
1393 
1394         if (gv->pixel_clock > maxpix) {
1395 		printf("grfcl: Pixelclock too high in mode %d\n",
1396 			(int) gv->mode_num);
1397                 return (0);
1398 	}
1399 
1400 	if (gv->disp_flags & GRF_FLAGS_SYNC_ON_GREEN) {
1401 		printf("grfcl: sync-on-green is not supported\n");
1402 		return (0);
1403 	}
1404 
1405         return (1);
1406 }
1407 
1408 int
cl_load_mon(struct grf_softc * gp,struct grfcltext_mode * md)1409 cl_load_mon(struct grf_softc *gp, struct grfcltext_mode *md)
1410 {
1411 	struct grfvideo_mode *gv;
1412 	struct grfinfo *gi;
1413 	volatile void *ba, *fb;
1414 	unsigned char num0, denom0, clkdoub;
1415 	unsigned short HT, HDE, HBS, HBE, HSS, HSE, VDE, VBS, VBE, VSS,
1416 	        VSE, VT;
1417 	int	clkmul, clkmode;
1418 	int	vmul;
1419 	int	sr15;
1420 	unsigned char hvsync_pulse;
1421 	char    TEXT;
1422 
1423 	/* identity */
1424 	gv = &md->gv;
1425 	TEXT = (gv->depth == 4);
1426 
1427 	if (!cl_mondefok(gv)) {
1428 		printf("grfcl: Monitor definition not ok\n");
1429 		return (0);
1430 	}
1431 
1432 	ba = gp->g_regkva;
1433 	fb = gp->g_fbkva;
1434 
1435 	/* provide all needed information in grf device-independent locations */
1436 	gp->g_data = (void *) gv;
1437 	gi = &gp->g_display;
1438 	gi->gd_regaddr = (void *) kvtop(__UNVOLATILE(ba));
1439 	gi->gd_regsize = 64 * 1024;
1440 	gi->gd_fbaddr = (void *) kvtop(__UNVOLATILE(fb));
1441 	gi->gd_fbsize = cl_fbsize;
1442 	gi->gd_colors = 1 << gv->depth;
1443 	gi->gd_planes = gv->depth;
1444 	gi->gd_fbwidth = gv->disp_width;
1445 	gi->gd_fbheight = gv->disp_height;
1446 	gi->gd_fbx = 0;
1447 	gi->gd_fby = 0;
1448 	if (TEXT) {
1449 		gi->gd_dwidth = md->fx * md->cols;
1450 		gi->gd_dheight = md->fy * md->rows;
1451 	} else {
1452 		gi->gd_dwidth = gv->disp_width;
1453 		gi->gd_dheight = gv->disp_height;
1454 	}
1455 	gi->gd_dx = 0;
1456 	gi->gd_dy = 0;
1457 
1458 	/* get display mode parameters */
1459 
1460 	HBS = gv->hblank_start;
1461 	HSS = gv->hsync_start;
1462 	HSE = gv->hsync_stop;
1463 	HBE = gv->htotal - 1;
1464 	HT = gv->htotal;
1465 	VBS = gv->vblank_start;
1466 	VSS = gv->vsync_start;
1467 	VSE = gv->vsync_stop;
1468 	VBE = gv->vtotal - 1;
1469 	VT = gv->vtotal;
1470 
1471 	if (TEXT)
1472 		HDE = ((gv->disp_width + md->fx - 1) / md->fx) - 1;
1473 	else
1474 		HDE = (gv->disp_width + 3) / 8 - 1;	/* HBS; */
1475 	VDE = gv->disp_height - 1;
1476 
1477 	/* adjustments */
1478 	switch (gv->depth) {
1479 	    case 8:
1480 		clkmul = 1;
1481 		clkmode = 0x0;
1482 		break;
1483 	    case 15:
1484 	    case 16:
1485 		clkmul = 1;
1486 		clkmode = 0x6;
1487 		break;
1488 	    case 24:
1489 		if ((cltype == PICASSO) && (cl_64bit == 1))	/* Picasso IV */
1490 			clkmul = 1;
1491 		else
1492 			clkmul = 3;
1493 		clkmode = 0x4;
1494 		break;
1495 	    case 32:
1496 		clkmul = 1;
1497 		clkmode = 0x8;
1498 		break;
1499 	    default:
1500 		clkmul = 1;
1501 		clkmode = 0x0;
1502 		break;
1503 	}
1504 
1505 	if ((VT > 1023) && (!(gv->disp_flags & GRF_FLAGS_LACE))) {
1506 		WCrt(ba, CRT_ID_MODE_CONTROL, 0xe7);
1507 	} else
1508 		WCrt(ba, CRT_ID_MODE_CONTROL, 0xe3);
1509 
1510 	vmul = 2;
1511 	if ((VT > 1023) || (gv->disp_flags & GRF_FLAGS_LACE))
1512 		vmul = 1;
1513 	if (gv->disp_flags & GRF_FLAGS_DBLSCAN)
1514 		vmul = 4;
1515 
1516 	VDE = VDE * vmul / 2;
1517 	VBS = VBS * vmul / 2;
1518 	VSS = VSS * vmul / 2;
1519 	VSE = VSE * vmul / 2;
1520 	VBE = VBE * vmul / 2;
1521 	VT  = VT * vmul / 2;
1522 
1523 	WSeq(ba, SEQ_ID_MEMORY_MODE, (TEXT || (gv->depth == 1)) ? 0x06 : 0x0e);
1524 	if (cl_64bit == 1) {
1525 	    if (TEXT || (gv->depth == 1))
1526 		sr15 = 0xd0;
1527 	    else
1528 		sr15 = ((cl_fbsize / 0x100000 == 2) ? 0x38 : 0xb8);
1529 	    WSeq(ba, SEQ_ID_CONF_RBACK, 0x00);
1530 	} else {
1531 		sr15 = (TEXT || (gv->depth == 1)) ? 0xd0 : 0xb0;
1532 		sr15 &= ((cl_fbsize / 0x100000) == 2) ? 0xff : 0x7f;
1533 	}
1534 	WSeq(ba, SEQ_ID_DRAM_CNTL, sr15);
1535 	WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x00);
1536 	WSeq(ba, SEQ_ID_MAP_MASK, (gv->depth == 1) ? 0x01 : 0xff);
1537 	WSeq(ba, SEQ_ID_CHAR_MAP_SELECT, 0x00);
1538 
1539 	/* Set clock */
1540 
1541 	cl_CompFQ(gv->pixel_clock * clkmul, &num0, &denom0, &clkdoub);
1542 
1543 	/* Horizontal/Vertical Sync Pulse */
1544 	hvsync_pulse = vgar(ba, GREG_MISC_OUTPUT_R);
1545 	if (gv->disp_flags & GRF_FLAGS_PHSYNC)
1546 		hvsync_pulse &= ~0x40;
1547 	else
1548 		hvsync_pulse |= 0x40;
1549 	if (gv->disp_flags & GRF_FLAGS_PVSYNC)
1550 		hvsync_pulse &= ~0x80;
1551 	else
1552 		hvsync_pulse |= 0x80;
1553 	vgaw(ba, GREG_MISC_OUTPUT_W, hvsync_pulse);
1554 
1555 	if (clkdoub) {
1556 		HDE /= 2;
1557 		HBS /= 2;
1558 		HSS /= 2;
1559 		HSE /= 2;
1560 		HBE /= 2;
1561 		HT  /= 2;
1562 		clkmode = 0x6;
1563 	}
1564 
1565 	WSeq(ba, SEQ_ID_VCLK_3_NUM, num0);
1566 	WSeq(ba, SEQ_ID_VCLK_3_DENOM, denom0);
1567 
1568 	/* load display parameters into board */
1569 
1570 	WCrt(ba, CRT_ID_HOR_TOTAL, HT);
1571 	WCrt(ba, CRT_ID_HOR_DISP_ENA_END, ((HDE >= HBS) ? HBS - 1 : HDE));
1572 	WCrt(ba, CRT_ID_START_HOR_BLANK, HBS);
1573 	WCrt(ba, CRT_ID_END_HOR_BLANK, (HBE & 0x1f) | 0x80);	/* | 0x80? */
1574 	WCrt(ba, CRT_ID_START_HOR_RETR, HSS);
1575 	WCrt(ba, CRT_ID_END_HOR_RETR,
1576 	    (HSE & 0x1f) |
1577 	    ((HBE & 0x20) ? 0x80 : 0x00));
1578 	WCrt(ba, CRT_ID_VER_TOTAL, VT);
1579 	WCrt(ba, CRT_ID_OVERFLOW,
1580 	    0x10 |
1581 	    ((VT & 0x100) ? 0x01 : 0x00) |
1582 	    ((VDE & 0x100) ? 0x02 : 0x00) |
1583 	    ((VSS & 0x100) ? 0x04 : 0x00) |
1584 	    ((VBS & 0x100) ? 0x08 : 0x00) |
1585 	    ((VT & 0x200) ? 0x20 : 0x00) |
1586 	    ((VDE & 0x200) ? 0x40 : 0x00) |
1587 	    ((VSS & 0x200) ? 0x80 : 0x00));
1588 
1589 	WCrt(ba, CRT_ID_CHAR_HEIGHT,
1590 	    0x40 |		/* TEXT ? 0x00 ??? */
1591 	    ((gv->disp_flags & GRF_FLAGS_DBLSCAN) ? 0x80 : 0x00) |
1592 	    ((VBS & 0x200) ? 0x20 : 0x00) |
1593 	    (TEXT ? ((md->fy - 1) & 0x1f) : 0x00));
1594 
1595 	/* text cursor */
1596 
1597 	if (TEXT) {
1598 #if CL_ULCURSOR
1599 		WCrt(ba, CRT_ID_CURSOR_START, (md->fy & 0x1f) - 2);
1600 		WCrt(ba, CRT_ID_CURSOR_END, (md->fy & 0x1f) - 1);
1601 #else
1602 		WCrt(ba, CRT_ID_CURSOR_START, 0x00);
1603 		WCrt(ba, CRT_ID_CURSOR_END, md->fy & 0x1f);
1604 #endif
1605 		WCrt(ba, CRT_ID_UNDERLINE_LOC, (md->fy - 1) & 0x1f);
1606 
1607 		WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, 0x00);
1608 		WCrt(ba, CRT_ID_CURSOR_LOC_LOW, 0x00);
1609 	}
1610 	WCrt(ba, CRT_ID_START_ADDR_HIGH, 0x00);
1611 	WCrt(ba, CRT_ID_START_ADDR_LOW, 0x00);
1612 
1613 	WCrt(ba, CRT_ID_START_VER_RETR, VSS);
1614 	WCrt(ba, CRT_ID_END_VER_RETR, (VSE & 0x0f) | 0x20);
1615 	WCrt(ba, CRT_ID_VER_DISP_ENA_END, VDE);
1616 	WCrt(ba, CRT_ID_START_VER_BLANK, VBS);
1617 	WCrt(ba, CRT_ID_END_VER_BLANK, VBE);
1618 
1619 	WCrt(ba, CRT_ID_LINE_COMPARE, 0xff);
1620 	WCrt(ba, CRT_ID_LACE_END, HT / 2);	/* MW/16 */
1621 	WCrt(ba, CRT_ID_LACE_CNTL,
1622 	    ((gv->disp_flags & GRF_FLAGS_LACE) ? 0x01 : 0x00) |
1623 	    ((HBE & 0x40) ? 0x10 : 0x00) |
1624 	    ((HBE & 0x80) ? 0x20 : 0x00) |
1625 	    ((VBE & 0x100) ? 0x40 : 0x00) |
1626 	    ((VBE & 0x200) ? 0x80 : 0x00));
1627 
1628 	WGfx(ba, GCT_ID_GRAPHICS_MODE,
1629 	    ((TEXT || (gv->depth == 1)) ? 0x00 : 0x40));
1630 	WGfx(ba, GCT_ID_MISC, (TEXT ? 0x04 : 0x01));
1631 
1632 	WSeq(ba, SEQ_ID_EXT_SEQ_MODE,
1633 	    ((TEXT || (gv->depth == 1)) ? 0x00 : 0x01) |
1634 	    ((cltype == PICASSO) ? 0x20 : 0x80) | clkmode);
1635 
1636 	/* write 0x00 to VDAC_MASK before accessing HDR this helps
1637 	   sometimes, out of "secret" application note (crest) */
1638 	vgaw(ba, VDAC_MASK, 0);
1639 	/* reset HDR "magic" access counter (crest) */
1640 	vgar(ba, VDAC_ADDRESS);
1641 
1642 	delay(200000);
1643 	vgar(ba, VDAC_MASK);
1644 	delay(200000);
1645 	vgar(ba, VDAC_MASK);
1646 	delay(200000);
1647 	vgar(ba, VDAC_MASK);
1648 	delay(200000);
1649 	vgar(ba, VDAC_MASK);
1650 	delay(200000);
1651 	switch (gv->depth) {
1652 	    case 1:
1653 	    case 4:		/* text */
1654 		vgaw(ba, VDAC_MASK, 0);
1655 		HDE = gv->disp_width / 16;
1656 		break;
1657 	    case 8:
1658 		if (clkdoub)
1659 			vgaw(ba, VDAC_MASK, 0x4a); /* Clockdouble Magic */
1660 		else
1661 			vgaw(ba, VDAC_MASK, 0);
1662 		HDE = gv->disp_width / 8;
1663 		break;
1664 	    case 15:
1665 		vgaw(ba, VDAC_MASK, 0xd0);
1666 		HDE = gv->disp_width / 4;
1667 		break;
1668 	    case 16:
1669 		vgaw(ba, VDAC_MASK, 0xc1);
1670 		HDE = gv->disp_width / 4;
1671 		break;
1672 	    case 24:
1673 		vgaw(ba, VDAC_MASK, 0xc5);
1674 		HDE = (gv->disp_width / 8) * 3;
1675 		break;
1676 	    case 32:
1677 		vgaw(ba, VDAC_MASK, 0xc5);
1678 		HDE = (gv->disp_width / 4);
1679 		break;
1680 	}
1681 
1682 	/* reset HDR "magic" access counter (crest) */
1683 	vgar(ba, VDAC_ADDRESS);
1684 	/* then enable all bit in VDAC_MASK afterwards (crest) */
1685 	vgaw(ba, VDAC_MASK, 0xff);
1686 
1687 	WCrt(ba, CRT_ID_OFFSET, HDE);
1688 	if (cl_64bit == 1) {
1689 		WCrt(ba, CRT_ID_SYNC_ADJ_GENLOCK, 0x00);
1690 		WCrt(ba, CRT_ID_OVERLAY_EXT_CTRL_REG, 0x40);
1691 	}
1692 	WCrt(ba, CRT_ID_EXT_DISP_CNTL,
1693 	    ((TEXT && gv->pixel_clock > 29000000) ? 0x40 : 0x00) |
1694 	    0x22 |
1695 	    ((HDE > 0xff) ? 0x10 : 0x00));
1696 
1697 	WAttr(ba, ACT_ID_ATTR_MODE_CNTL, (TEXT ? 0x0a : 0x01));
1698 	WAttr(ba, 0x20 | ACT_ID_COLOR_PLANE_ENA,
1699 	    (gv->depth == 1) ? 0x01 : 0x0f);
1700 
1701 	/* text initialization */
1702 
1703 	if (TEXT) {
1704 		cl_inittextmode(gp);
1705 	}
1706 	WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x14);
1707 	WSeq(ba, SEQ_ID_CLOCKING_MODE, 0x01);
1708 	cl_blanked = 0;
1709 
1710 	/* Pass-through */
1711 
1712 	RegOffpass(ba);
1713 
1714 	return (1);
1715 }
1716 
1717 void
cl_inittextmode(struct grf_softc * gp)1718 cl_inittextmode(struct grf_softc *gp)
1719 {
1720 	struct grfcltext_mode *tm = (struct grfcltext_mode *) gp->g_data;
1721 	volatile unsigned char *ba = gp->g_regkva;
1722 	unsigned char *fb = __UNVOLATILE(gp->g_fbkva);
1723 	unsigned char *c, *f, y;
1724 	unsigned short z;
1725 
1726 
1727 	/* load text font into beginning of display memory. Each character
1728 	 * cell is 32 bytes long (enough for 4 planes) */
1729 
1730 	SetTextPlane(ba, 0x02);
1731         cl_memset(fb, 0, 256 * 32);
1732 	c = (unsigned char *) (fb) + (32 * tm->fdstart);
1733 	f = tm->fdata;
1734 	for (z = tm->fdstart; z <= tm->fdend; z++, c += (32 - tm->fy))
1735 		for (y = 0; y < tm->fy; y++)
1736 			*c++ = *f++;
1737 
1738 	/* clear out text/attr planes (three screens worth) */
1739 
1740 	SetTextPlane(ba, 0x01);
1741 	cl_memset(fb, 0x07, tm->cols * tm->rows * 3);
1742 	SetTextPlane(ba, 0x00);
1743 	cl_memset(fb, 0x20, tm->cols * tm->rows * 3);
1744 
1745 	/* print out a little init msg */
1746 
1747 	c = (unsigned char *) (fb) + (tm->cols - 16);
1748 	strcpy(c, "CIRRUS");
1749 	c[6] = 0x20;
1750 
1751 	/* set colors (B&W) */
1752 
1753 	vgaw(ba, VDAC_ADDRESS_W, 0);
1754 	for (z = 0; z < 256; z++) {
1755 		unsigned char r, g, b;
1756 
1757 		y = (z & 1) ? ((z > 7) ? 2 : 1) : 0;
1758 
1759 		if (cltype == PICASSO) {
1760 			r = clconscolors[y][0];
1761 			g = clconscolors[y][1];
1762 			b = clconscolors[y][2];
1763 		} else {
1764 			b = clconscolors[y][0];
1765 			g = clconscolors[y][1];
1766 			r = clconscolors[y][2];
1767 		}
1768 		vgaw(ba, VDAC_DATA, r >> 2);
1769 		vgaw(ba, VDAC_DATA, g >> 2);
1770 		vgaw(ba, VDAC_DATA, b >> 2);
1771 	}
1772 }
1773 
1774 void
cl_memset(unsigned char * d,unsigned char c,int l)1775 cl_memset(unsigned char *d, unsigned char c, int l)
1776 {
1777 	for (; l > 0; l--)
1778 		*d++ = c;
1779 }
1780 
1781 /*
1782  * Special wakeup/passthrough registers on graphics boards
1783  *
1784  * The methods have diverged a bit for each board, so
1785  * WPass(P) has been converted into a set of specific
1786  * inline functions.
1787  */
1788 static void
RegWakeup(volatile void * ba)1789 RegWakeup(volatile void *ba)
1790 {
1791 
1792 	switch (cltype) {
1793 	    case SPECTRUM:
1794 		vgaw(ba, PASS_ADDRESS_W, 0x1f);
1795 		break;
1796 	    case PICASSO:
1797 		/* Picasso needs no wakeup */
1798 		break;
1799 	    case PICCOLO:
1800 		if (cl_64bit == 1)
1801 			vgaw(ba, PASS_ADDRESS_W, 0x1f);
1802 		else
1803 			vgaw(ba, PASS_ADDRESS_W, vgar(ba, PASS_ADDRESS) | 0x10);
1804 		break;
1805 	}
1806 	delay(200000);
1807 }
1808 
1809 static void
RegOnpass(volatile void * ba)1810 RegOnpass(volatile void *ba)
1811 {
1812 
1813 	switch (cltype) {
1814 	    case SPECTRUM:
1815 		vgaw(ba, PASS_ADDRESS_W, 0x4f);
1816 		break;
1817 	    case PICASSO:
1818 		if (cl_64bit == 0)
1819 			vgaw(ba, PASS_ADDRESS_WP, 0x01);
1820 		break;
1821 	    case PICCOLO:
1822 		if (cl_64bit == 1)
1823 			vgaw(ba, PASS_ADDRESS_W, 0x4f);
1824 		else
1825 			vgaw(ba, PASS_ADDRESS_W, vgar(ba, PASS_ADDRESS) & 0xdf);
1826 		break;
1827 	}
1828 	cl_pass_toggle = 1;
1829 	delay(200000);
1830 }
1831 
1832 static void
RegOffpass(volatile void * ba)1833 RegOffpass(volatile void *ba)
1834 {
1835 
1836 	switch (cltype) {
1837 	    case SPECTRUM:
1838 		vgaw(ba, PASS_ADDRESS_W, 0x6f);
1839 		break;
1840 	    case PICASSO:
1841 		if (cl_64bit == 0)
1842 			vgaw(ba, PASS_ADDRESS_W, 0xff);
1843 		break;
1844 	    case PICCOLO:
1845 		if (cl_64bit == 1)
1846 			vgaw(ba, PASS_ADDRESS_W, 0x6f);
1847 		else
1848 			vgaw(ba, PASS_ADDRESS_W, vgar(ba, PASS_ADDRESS) | 0x20);
1849 		break;
1850 	}
1851 	cl_pass_toggle = 0;
1852 	delay(200000);
1853 }
1854 
1855 #if NWSDISPLAY > 0
1856 static void
cl_wscursor(void * c,int on,int row,int col)1857 cl_wscursor(void *c, int on, int row, int col)
1858 {
1859 	struct rasops_info *ri;
1860 	struct vcons_screen *scr;
1861 	struct grf_softc *gp;
1862 	volatile void *ba;
1863 	int offs;
1864 
1865 	ri = c;
1866 	scr = ri->ri_hw;
1867 	gp = scr->scr_cookie;
1868 	ba = gp->g_regkva;
1869 
1870 	if ((ri->ri_flg & RI_CURSOR) && !on) {
1871 		/* cursor was visible, but we want to remove it */
1872 		/*WCrt(ba, CRT_ID_CURSOR_START, | 0x20);*/
1873 		ri->ri_flg &= ~RI_CURSOR;
1874 	}
1875 
1876 	ri->ri_crow = row;
1877 	ri->ri_ccol = col;
1878 
1879 	if (on) {
1880 		/* move cursor to new location */
1881 		if (!(ri->ri_flg & RI_CURSOR)) {
1882 			/*WCrt(ba, CRT_ID_CURSOR_START, | 0x20);*/
1883 			ri->ri_flg |= RI_CURSOR;
1884 		}
1885 		offs = gp->g_rowoffset[row] + col;
1886 		WCrt(ba, CRT_ID_CURSOR_LOC_LOW, offs & 0xff);
1887 		WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, offs >> 8);
1888 	}
1889 }
1890 
1891 static void
cl_wsputchar(void * c,int row,int col,u_int ch,long attr)1892 cl_wsputchar(void *c, int row, int col, u_int ch, long attr)
1893 {
1894 	struct rasops_info *ri;
1895 	struct vcons_screen *scr;
1896 	struct grf_softc *gp;
1897 	volatile unsigned char *ba, *cp;
1898 
1899 	ri = c;
1900 	scr = ri->ri_hw;
1901 	gp = scr->scr_cookie;
1902 	ba = gp->g_regkva;
1903 	cp = gp->g_fbkva;
1904 
1905 	cp += gp->g_rowoffset[row] + col;
1906 	SetTextPlane(ba, 0x00);
1907 	*cp = ch;
1908 	SetTextPlane(ba, 0x01);
1909 	*cp = attr;
1910 }
1911 
1912 static void
cl_wscopycols(void * c,int row,int srccol,int dstcol,int ncols)1913 cl_wscopycols(void *c, int row, int srccol, int dstcol, int ncols)
1914 {
1915 	volatile unsigned char *ba, *dst, *src;
1916 	struct rasops_info *ri;
1917 	struct vcons_screen *scr;
1918 	struct grf_softc *gp;
1919 	int i;
1920 
1921 	KASSERT(ncols > 0);
1922 	ri = c;
1923 	scr = ri->ri_hw;
1924 	gp = scr->scr_cookie;
1925 	ba = gp->g_regkva;
1926 	src = gp->g_fbkva;
1927 
1928 	src += gp->g_rowoffset[row];
1929 	dst = src;
1930 	src += srccol;
1931 	dst += dstcol;
1932 	if (srccol < dstcol) {
1933 		/* need to copy backwards */
1934 		src += ncols;
1935 		dst += ncols;
1936 		SetTextPlane(ba, 0x00);
1937 		for (i = 0; i < ncols; i++)
1938 			*(--dst) = *(--src);
1939 		src += ncols;
1940 		dst += ncols;
1941 		SetTextPlane(ba, 0x01);
1942 		for (i = 0; i < ncols; i++)
1943 			*(--dst) = *(--src);
1944 	} else {
1945 		SetTextPlane(ba, 0x00);
1946 		for (i = 0; i < ncols; i++)
1947 			*dst++ = *src++;
1948 		src -= ncols;
1949 		dst -= ncols;
1950 		SetTextPlane(ba, 0x01);
1951 		for (i = 0; i < ncols; i++)
1952 			*dst++ = *src++;
1953 	}
1954 }
1955 
1956 static void
cl_wserasecols(void * c,int row,int startcol,int ncols,long fillattr)1957 cl_wserasecols(void *c, int row, int startcol, int ncols, long fillattr)
1958 {
1959 	volatile unsigned char *ba, *cp;
1960 	struct rasops_info *ri;
1961 	struct vcons_screen *scr;
1962 	struct grf_softc *gp;
1963 	int i;
1964 
1965 	ri = c;
1966 	scr = ri->ri_hw;
1967 	gp = scr->scr_cookie;
1968 	ba = gp->g_regkva;
1969 	cp = gp->g_fbkva;
1970 
1971 	cp += gp->g_rowoffset[row] + startcol;
1972 	SetTextPlane(ba, 0x00);
1973 	for (i = 0; i < ncols; i++)
1974 		*cp++ = 0x20;
1975 	cp -= ncols;
1976 	SetTextPlane(ba, 0x01);
1977 	for (i = 0; i < ncols; i++)
1978 		*cp++ = 0x07;
1979 }
1980 
1981 static void
cl_wscopyrows(void * c,int srcrow,int dstrow,int nrows)1982 cl_wscopyrows(void *c, int srcrow, int dstrow, int nrows)
1983 {
1984 	volatile unsigned char *ba, *dst, *src;
1985 	struct rasops_info *ri;
1986 	struct vcons_screen *scr;
1987 	struct grf_softc *gp;
1988 	int i, n;
1989 
1990 	KASSERT(nrows > 0);
1991 	ri = c;
1992 	scr = ri->ri_hw;
1993 	gp = scr->scr_cookie;
1994 	ba = gp->g_regkva;
1995 	src = dst = gp->g_fbkva;
1996 	n = ri->ri_cols * nrows;
1997 
1998 	if (srcrow < dstrow) {
1999 		/* need to copy backwards */
2000 		src += gp->g_rowoffset[srcrow + nrows];
2001 		dst += gp->g_rowoffset[dstrow + nrows];
2002 		SetTextPlane(ba, 0x00);
2003 		for (i = 0; i < n; i++)
2004 			*(--dst) = *(--src);
2005 		src += n;
2006 		dst += n;
2007 		SetTextPlane(ba, 0x01);
2008 		for (i = 0; i < n; i++)
2009 			*(--dst) = *(--src);
2010 	} else {
2011 		src += gp->g_rowoffset[srcrow];
2012 		dst += gp->g_rowoffset[dstrow];
2013 		SetTextPlane(ba, 0x00);
2014 		for (i = 0; i < n; i++)
2015 			*dst++ = *src++;
2016 		src -= n;
2017 		dst -= n;
2018 		SetTextPlane(ba, 0x01);
2019 		for (i = 0; i < n; i++)
2020 			*dst++ = *src++;
2021 	}
2022 }
2023 
2024 static void
cl_wseraserows(void * c,int row,int nrows,long fillattr)2025 cl_wseraserows(void *c, int row, int nrows, long fillattr)
2026 {
2027 	volatile unsigned char *ba, *cp;
2028 	struct rasops_info *ri;
2029 	struct vcons_screen *scr;
2030 	struct grf_softc *gp;
2031 	int i, n;
2032 
2033 	ri = c;
2034 	scr = ri->ri_hw;
2035 	gp = scr->scr_cookie;
2036 	ba = gp->g_regkva;
2037 	cp = gp->g_fbkva;
2038 
2039 	cp += gp->g_rowoffset[row];
2040 	n = ri->ri_cols * nrows;
2041 	SetTextPlane(ba, 0x00);
2042 	for (i = 0; i < n; i++)
2043 		*cp++ = 0x20;
2044 	cp -= n;
2045 	SetTextPlane(ba, 0x01);
2046 	for (i = 0; i < n; i++)
2047 		*cp++ = 0x07;
2048 }
2049 
2050 static int
cl_wsallocattr(void * c,int fg,int bg,int flg,long * attr)2051 cl_wsallocattr(void *c, int fg, int bg, int flg, long *attr)
2052 {
2053 
2054 	/* XXX color support? */
2055 	*attr = (flg & WSATTR_REVERSE) ? 0x70 : 0x07;
2056 	if (flg & WSATTR_UNDERLINE)	*attr = 0x01;
2057 	if (flg & WSATTR_HILIT)		*attr |= 0x08;
2058 	if (flg & WSATTR_BLINK)		*attr |= 0x80;
2059 	return 0;
2060 }
2061 
2062 /* our font does not support unicode extensions */
2063 static int
cl_wsmapchar(void * c,int ch,unsigned int * cp)2064 cl_wsmapchar(void *c, int ch, unsigned int *cp)
2065 {
2066 
2067 	if (ch > 0 && ch < 256) {
2068 		*cp = ch;
2069 		return 5;
2070 	}
2071 	*cp = ' ';
2072 	return 0;
2073 }
2074 
2075 static int
cl_wsioctl(void * v,void * vs,u_long cmd,void * data,int flag,struct lwp * l)2076 cl_wsioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
2077 {
2078 	struct vcons_data *vd;
2079 	struct grf_softc *gp;
2080 
2081 	vd = v;
2082 	gp = vd->cookie;
2083 
2084 	switch (cmd) {
2085 	case WSDISPLAYIO_GETCMAP:
2086 		/* Note: wsdisplay_cmap and grf_colormap have same format */
2087 		if (gp->g_display.gd_planes == 8)
2088 			return cl_getcmap(gp, (struct grf_colormap *)data);
2089 		return EINVAL;
2090 
2091 	case WSDISPLAYIO_PUTCMAP:
2092 		/* Note: wsdisplay_cmap and grf_colormap have same format */
2093 		if (gp->g_display.gd_planes == 8)
2094 			return cl_putcmap(gp, (struct grf_colormap *)data);
2095 		return EINVAL;
2096 
2097 	case WSDISPLAYIO_GVIDEO:
2098 		if (cl_isblank(gp))
2099 			*(u_int *)data = WSDISPLAYIO_VIDEO_OFF;
2100 		else
2101 			*(u_int *)data = WSDISPLAYIO_VIDEO_ON;
2102 		return 0;
2103 
2104 	case WSDISPLAYIO_SVIDEO:
2105 		return cl_blank(gp, *(u_int *)data == WSDISPLAYIO_VIDEO_ON);
2106 
2107 	case WSDISPLAYIO_SMODE:
2108 		if ((*(int *)data) != gp->g_wsmode) {
2109 			if (*(int *)data == WSDISPLAYIO_MODE_EMUL) {
2110 				/* load console text mode, redraw screen */
2111 				(void)cl_load_mon(gp, &clconsole_mode);
2112 				if (vd->active != NULL)
2113 					vcons_redraw_screen(vd->active);
2114 			} else {
2115 				/* switch to current graphics mode */
2116 				if (!cl_load_mon(gp,
2117 				    (struct grfcltext_mode *)monitor_current))
2118 					return EINVAL;
2119 			}
2120 			gp->g_wsmode = *(int *)data;
2121 		}
2122 		return 0;
2123 
2124 	case WSDISPLAYIO_GET_FBINFO:
2125 		return cl_get_fbinfo(gp, data);
2126 	}
2127 
2128 	/* handle this command hw-independent in grf(4) */
2129 	return grf_wsioctl(v, vs, cmd, data, flag, l);
2130 }
2131 
2132 /*
2133  * Fill the wsdisplayio_fbinfo structure with information from the current
2134  * graphics mode. Even when text mode is active.
2135  */
2136 static int
cl_get_fbinfo(struct grf_softc * gp,struct wsdisplayio_fbinfo * fbi)2137 cl_get_fbinfo(struct grf_softc *gp, struct wsdisplayio_fbinfo *fbi)
2138 {
2139 	struct grfvideo_mode *md;
2140 	uint32_t rbits, gbits, bbits;
2141 
2142 	md = monitor_current;
2143 
2144 	switch (md->depth) {
2145 	case 8:
2146 		fbi->fbi_bitsperpixel = 8;
2147 		rbits = gbits = bbits = 6;  /* keep gcc happy */
2148 		break;
2149 	case 15:
2150 		fbi->fbi_bitsperpixel = 16;
2151 		rbits = gbits = bbits = 5;
2152 		break;
2153 	case 16:
2154 		fbi->fbi_bitsperpixel = 16;
2155 		rbits = bbits = 5;
2156 		gbits = 6;
2157 		break;
2158 	case 24:
2159 		fbi->fbi_bitsperpixel = 24;
2160 		rbits = gbits = bbits = 8;
2161 		break;
2162 	default:
2163 		return EINVAL;
2164 	}
2165 
2166 	fbi->fbi_stride = (fbi->fbi_bitsperpixel / 8) * md->disp_width;
2167 	fbi->fbi_width = md->disp_width;
2168 	fbi->fbi_height = md->disp_height;
2169 
2170 	if (md->depth > 8) {
2171 		fbi->fbi_pixeltype = WSFB_RGB;
2172 		fbi->fbi_subtype.fbi_rgbmasks.red_offset = bbits + gbits;
2173 		fbi->fbi_subtype.fbi_rgbmasks.red_size = rbits;
2174 		fbi->fbi_subtype.fbi_rgbmasks.green_offset = bbits;
2175 		fbi->fbi_subtype.fbi_rgbmasks.green_size = gbits;
2176 		fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 0;
2177 		fbi->fbi_subtype.fbi_rgbmasks.blue_size = bbits;
2178 		fbi->fbi_subtype.fbi_rgbmasks.alpha_offset = 0;
2179 		fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 0;
2180 	} else {
2181 		fbi->fbi_pixeltype = WSFB_CI;
2182 		fbi->fbi_subtype.fbi_cmapinfo.cmap_entries = 1 << md->depth;
2183 	}
2184 
2185 	fbi->fbi_flags = 0;
2186 	fbi->fbi_fbsize = fbi->fbi_stride * fbi->fbi_height;
2187 	fbi->fbi_fboffset = 0;
2188 	return 0;
2189 }
2190 #endif	/* NWSDISPLAY > 0 */
2191 
2192 #endif /* NGRFCL */
2193