Home | History | Annotate | Line # | Download | only in dev
grf_cl.c revision 1.18
      1 /*      $NetBSD: grf_cl.c,v 1.18 1996/12/23 09:10:04 veego Exp $        */
      2 
      3 /*
      4  * Copyright (c) 1995 Ezra Story
      5  * Copyright (c) 1995 Kari Mettinen
      6  * Copyright (c) 1994 Markus Wild
      7  * Copyright (c) 1994 Lutz Vieweg
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by Lutz Vieweg.
     21  * 4. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 #include "grfcl.h"
     36 #if NGRFCL > 0
     37 
     38 /*
     39  * Graphics routines for Cirrus CL GD 5426 boards,
     40  *
     41  * This code offers low-level routines to access Cirrus Cl GD 5426
     42  * graphics-boards from within NetBSD for the Amiga.
     43  * No warranties for any kind of function at all - this
     44  * code may crash your hardware and scratch your harddisk.  Use at your
     45  * own risk.  Freely distributable.
     46  *
     47  * Modified for Cirrus CL GD 5426 from
     48  * Lutz Vieweg's retina driver by Kari Mettinen 08/94
     49  * Contributions by Ill, ScottE, MiL
     50  * Extensively hacked and rewritten by Ezra Story (Ezy) 01/95
     51  * Picasso/040 patches (wee!) by crest 01/96
     52  *
     53  * Thanks to Village Tronic Marketing Gmbh for providing me with
     54  * a Picasso-II board.
     55  * Thanks for Integrated Electronics Oy Ab for providing me with
     56  * Cirrus CL GD 542x family documentation.
     57  *
     58  * TODO:
     59  *    Mouse support (almost there! :-))
     60  *    Blitter support
     61  *
     62  */
     63 
     64 #include <sys/param.h>
     65 #include <sys/systm.h>
     66 #include <sys/errno.h>
     67 #include <sys/ioctl.h>
     68 #include <sys/device.h>
     69 #include <sys/malloc.h>
     70 
     71 #include <machine/cpu.h>
     72 #include <dev/cons.h>
     73 #include <amiga/dev/itevar.h>
     74 #include <amiga/amiga/device.h>
     75 #include <amiga/dev/grfioctl.h>
     76 #include <amiga/dev/grfvar.h>
     77 #include <amiga/dev/grf_clreg.h>
     78 #include <amiga/dev/zbusvar.h>
     79 
     80 int cl_mondefok __P((struct grfvideo_mode *));
     81 void cl_boardinit __P((struct grf_softc *));
     82 static void cl_CompFQ __P((u_int, u_char *, u_char *));
     83 int cl_getvmode __P((struct grf_softc *, struct grfvideo_mode *));
     84 int cl_setvmode __P((struct grf_softc *, unsigned int));
     85 int cl_toggle __P((struct grf_softc *, unsigned short));
     86 int cl_getcmap __P((struct grf_softc *, struct grf_colormap *));
     87 int cl_putcmap __P((struct grf_softc *, struct grf_colormap *));
     88 #ifndef CL5426CONSOLE
     89 void cl_off __P((struct grf_softc *));
     90 #endif
     91 void cl_inittextmode __P((struct grf_softc *));
     92 int cl_ioctl __P((register struct grf_softc *, u_long, void *));
     93 int cl_getmousepos __P((struct grf_softc *, struct grf_position *));
     94 int cl_setmousepos __P((struct grf_softc *, struct grf_position *));
     95 static int cl_setspriteinfo __P((struct grf_softc *, struct grf_spriteinfo *));
     96 int cl_getspriteinfo __P((struct grf_softc *, struct grf_spriteinfo *));
     97 static int cl_getspritemax __P((struct grf_softc *, struct grf_position *));
     98 int cl_blank __P((struct grf_softc *, int *));
     99 int cl_setmonitor __P((struct grf_softc *, struct grfvideo_mode *));
    100 void cl_writesprpos __P((volatile char *, short, short));
    101 void writeshifted __P((volatile char *, char, char));
    102 
    103 static void RegWakeup __P((volatile caddr_t));
    104 static void RegOnpass __P((volatile caddr_t));
    105 static void RegOffpass __P((volatile caddr_t));
    106 
    107 void grfclattach __P((struct device *, struct device *, void *));
    108 int grfclprint __P((void *, const char *));
    109 int grfclmatch __P((struct device *, struct cfdata *, void *));
    110 void cl_memset __P((unsigned char *, unsigned char, int));
    111 
    112 /* Graphics display definitions.
    113  * These are filled by 'grfconfig' using GRFIOCSETMON.
    114  */
    115 #define monitor_def_max 8
    116 static struct grfvideo_mode monitor_def[8] = {
    117 	{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}
    118 };
    119 static struct grfvideo_mode *monitor_current = &monitor_def[0];
    120 
    121 /* Patchable maximum pixel clock */
    122 unsigned long cl_maxpixelclock = 86000000;
    123 
    124 /* Console display definition.
    125  *   Default hardcoded text mode.  This grf_cl is set up to
    126  *   use one text mode only, and this is it.  You may use
    127  *   grfconfig to change the mode after boot.
    128  */
    129 /* Console font */
    130 #ifdef KFONT_8X11
    131 #define CIRRUSFONT kernel_font_8x11
    132 #define CIRRUSFONTY 11
    133 #else
    134 #define CIRRUSFONT kernel_font_8x8
    135 #define CIRRUSFONTY 8
    136 #endif
    137 extern unsigned char CIRRUSFONT[];
    138 
    139 struct grfcltext_mode clconsole_mode = {
    140 	{255, "", 25200000, 640, 480, 4, 80, 100, 94, 99, 100, 481, 522, 490,
    141 	498, 522},
    142 	8, CIRRUSFONTY, 80, 480 / CIRRUSFONTY, CIRRUSFONT, 32, 255
    143 };
    144 /* Console colors */
    145 unsigned char clconscolors[3][3] = {	/* background, foreground, hilite */
    146 	{0, 0x40, 0x50}, {152, 152, 152}, {255, 255, 255}
    147 };
    148 
    149 int	cltype = 0;		/* Picasso, Spectrum or Piccolo */
    150 int	cl_sd64 = 0;
    151 unsigned char pass_toggle;	/* passthru status tracker */
    152 
    153 /* because all 5426-boards have 2 configdev entries, one for
    154  * framebuffer mem and the other for regs, we have to hold onto
    155  * the pointers globally until we match on both.  This and 'cltype'
    156  * are the primary obsticles to multiple board support, but if you
    157  * have multiple boards you have bigger problems than grf_cl.
    158  */
    159 static void *cl_fbaddr = 0;	/* framebuffer */
    160 static void *cl_regaddr = 0;	/* registers */
    161 static int cl_fbsize;		/* framebuffer size */
    162 
    163 /* current sprite info, if you add support for multiple boards
    164  * make this an array or something
    165  */
    166 struct grf_spriteinfo cl_cursprite;
    167 
    168 /* sprite bitmaps in kernel stack, you'll need to arrayize these too if
    169  * you add multiple board support
    170  */
    171 static unsigned char cl_imageptr[8 * 64], cl_maskptr[8 * 64];
    172 static unsigned char cl_sprred[2], cl_sprgreen[2], cl_sprblue[2];
    173 
    174 /* standard driver stuff */
    175 struct cfattach grfcl_ca = {
    176 	sizeof(struct grf_softc), grfclmatch, grfclattach
    177 };
    178 
    179 struct cfdriver grfcl_cd = {
    180 	NULL, "grfcl", DV_DULL, NULL, 0
    181 };
    182 static struct cfdata *cfdata;
    183 
    184 int
    185 grfclmatch(pdp, cfp, auxp)
    186 	struct device *pdp;
    187 	struct cfdata *cfp;
    188 	void   *auxp;
    189 {
    190 	struct zbus_args *zap;
    191 	static int regprod, fbprod;
    192 	int error;
    193 
    194 	zap = auxp;
    195 
    196 #ifndef CL5426CONSOLE
    197 	if (amiga_realconfig == 0)
    198 		return (0);
    199 #endif
    200 
    201 	/* Grab the first board we encounter as the preferred one.  This will
    202 	 * allow one board to work in a multiple 5426 board system, but not
    203 	 * multiple boards at the same time.  */
    204 	if (cltype == 0) {
    205 		switch (zap->manid) {
    206 		    case PICASSO:
    207 			if (zap->prodid != 12 && zap->prodid != 11)
    208 				return (0);
    209 			regprod = 12;
    210 			fbprod = 11;
    211 			break;
    212 		    case SPECTRUM:
    213 			if (zap->prodid != 2 && zap->prodid != 1)
    214 				return (0);
    215 			regprod = 2;
    216 			fbprod = 1;
    217 			break;
    218 		    case PICCOLO:
    219 			switch (zap->prodid) {
    220 			    case 5:
    221 			    case 6:
    222 				regprod = 6;
    223 				fbprod = 5;
    224 				error = 0;
    225 				break;
    226 			    case 10:
    227 			    case 11:
    228 				regprod = 11;
    229 				fbprod = 10;
    230 				cl_sd64 = 1;
    231 				error = 0;
    232 				break;
    233 		    	    default:
    234 				error = 1;
    235 				break;
    236 			}
    237 			if (error == 1)
    238 			    return (0);
    239 			else
    240 			    break;
    241 		    default:
    242 			return (0);
    243 		}
    244 		cltype = zap->manid;
    245 	} else {
    246 		if (cltype != zap->manid) {
    247 			return (0);
    248 		}
    249 	}
    250 
    251 	/* Configure either registers or framebuffer in any order */
    252 	if (zap->prodid == regprod)
    253 		cl_regaddr = zap->va;
    254 	else
    255 		if (zap->prodid == fbprod) {
    256 			cl_fbaddr = zap->va;
    257 			cl_fbsize = zap->size;
    258 		} else
    259 			return (0);
    260 
    261 #ifdef CL5426CONSOLE
    262 	if (amiga_realconfig == 0) {
    263 		cfdata = cfp;
    264 	}
    265 #endif
    266 
    267 	return (1);
    268 }
    269 
    270 void
    271 grfclattach(pdp, dp, auxp)
    272 	struct device *pdp, *dp;
    273 	void   *auxp;
    274 {
    275 	static struct grf_softc congrf;
    276 	struct zbus_args *zap;
    277 	struct grf_softc *gp;
    278 	static char attachflag = 0;
    279 
    280 	zap = auxp;
    281 
    282 	printf("\n");
    283 
    284 	/* make sure both halves have matched */
    285 	if (!cl_regaddr || !cl_fbaddr)
    286 		return;
    287 
    288 	/* do all that messy console/grf stuff */
    289 	if (dp == NULL)
    290 		gp = &congrf;
    291 	else
    292 		gp = (struct grf_softc *) dp;
    293 
    294 	if (dp != NULL && congrf.g_regkva != 0) {
    295 		/*
    296 		 * inited earlier, just copy (not device struct)
    297 		 */
    298 		bcopy(&congrf.g_display, &gp->g_display,
    299 		    (char *) &gp[1] - (char *) &gp->g_display);
    300 	} else {
    301 		gp->g_regkva = (volatile caddr_t) cl_regaddr;
    302 		gp->g_fbkva = (volatile caddr_t) cl_fbaddr;
    303 
    304 		gp->g_unit = GRF_CL5426_UNIT;
    305 		gp->g_mode = cl_mode;
    306 		gp->g_conpri = grfcl_cnprobe();
    307 		gp->g_flags = GF_ALIVE;
    308 
    309 		/* wakeup the board */
    310 		cl_boardinit(gp);
    311 #ifdef CL5426CONSOLE
    312 		grfcl_iteinit(gp);
    313 		(void) cl_load_mon(gp, &clconsole_mode);
    314 #endif
    315 
    316 	}
    317 
    318 	/*
    319 	 * attach grf (once)
    320 	 */
    321 	if (amiga_config_found(cfdata, &gp->g_device, gp, grfclprint)) {
    322 		attachflag = 1;
    323 		printf("grfcl: %dMB ", cl_fbsize / 0x100000);
    324 		switch (cltype) {
    325 		    case PICASSO:
    326 			printf("Picasso II");
    327                         cl_maxpixelclock = 86000000;
    328 			break;
    329 		    case SPECTRUM:
    330 			printf("Spectrum");
    331                         cl_maxpixelclock = 90000000;
    332 			break;
    333 		    case PICCOLO:
    334 			if (cl_sd64 == 1) {
    335 				printf("Piccolo SD64");
    336 				/* 110MHz will be supported if we
    337 				 * have a palette doubling mode.
    338 				 */
    339 				cl_maxpixelclock = 90000000;
    340 			} else {
    341 				printf("Piccolo");
    342 				cl_maxpixelclock = 90000000;
    343 			}
    344 			break;
    345 		}
    346 		printf(" being used\n");
    347 #ifdef CL_OVERCLOCK
    348                 cl_maxpixelclock = 115000000;
    349 #endif
    350 	} else {
    351 		if (!attachflag)
    352 			printf("grfcl unattached!!\n");
    353 	}
    354 }
    355 
    356 int
    357 grfclprint(auxp, pnp)
    358 	void   *auxp;
    359 	const char *pnp;
    360 {
    361 	if (pnp)
    362 		printf("ite at %s: ", pnp);
    363 	return (UNCONF);
    364 }
    365 
    366 void
    367 cl_boardinit(gp)
    368 	struct grf_softc *gp;
    369 {
    370 	unsigned char *ba = gp->g_regkva;
    371 	int     x;
    372 
    373 	/* wakeup board and flip passthru OFF */
    374 
    375 	RegWakeup(ba);
    376 	RegOnpass(ba);
    377 
    378 	vgaw(ba, 0x46e8, 0x16);
    379 	vgaw(ba, 0x102, 1);
    380 	vgaw(ba, 0x46e8, 0x0e);
    381 	if (cl_sd64 != 1)
    382 		vgaw(ba, 0x3c3, 1);
    383 
    384 	/* setup initial unchanging parameters */
    385 
    386 	WSeq(ba, SEQ_ID_CLOCKING_MODE, 0x21);	/* 8 dot - display off */
    387 	vgaw(ba, GREG_MISC_OUTPUT_W, 0xed);	/* mem disable */
    388 
    389 	WGfx(ba, GCT_ID_OFFSET_1, 0xec);	/* magic cookie */
    390 	WSeq(ba, SEQ_ID_UNLOCK_EXT, 0x12);	/* yum! cookies! */
    391 
    392 	if (cl_sd64 == 1) {
    393 		WSeq(ba, SEQ_ID_CONF_RBACK, 0x00);
    394 		WSeq(ba, SEQ_ID_DRAM_CNTL, (cl_fbsize / 0x100000 == 2) ? 0x38 : 0xb8);
    395 	} else {
    396 		WSeq(ba, SEQ_ID_DRAM_CNTL, 0xb0);
    397 	}
    398 	WSeq(ba, SEQ_ID_RESET, 0x03);
    399 	WSeq(ba, SEQ_ID_MAP_MASK, 0xff);
    400 	WSeq(ba, SEQ_ID_CHAR_MAP_SELECT, 0x00);
    401 	WSeq(ba, SEQ_ID_MEMORY_MODE, 0x0e);	/* a or 6? */
    402 	WSeq(ba, SEQ_ID_EXT_SEQ_MODE, (cltype == PICASSO) ? 0x20 : 0x80);
    403 	WSeq(ba, SEQ_ID_EEPROM_CNTL, 0x00);
    404 	if (cl_sd64 == 1)
    405 		WSeq(ba, SEQ_ID_PERF_TUNE, 0x5a);
    406 	else
    407 		WSeq(ba, SEQ_ID_PERF_TUNE, 0x0a);	/* mouse 0a fa */
    408 	WSeq(ba, SEQ_ID_SIG_CNTL, 0x02);
    409 	WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x04);
    410 
    411 	if (cl_sd64 == 1)
    412 		WSeq(ba, SEQ_ID_MCLK_SELECT, 0x1c);
    413 	else
    414 		WSeq(ba, SEQ_ID_MCLK_SELECT, 0x22);
    415 
    416 	WCrt(ba, CRT_ID_PRESET_ROW_SCAN, 0x00);
    417 	WCrt(ba, CRT_ID_CURSOR_START, 0x00);
    418 	WCrt(ba, CRT_ID_CURSOR_END, 0x08);
    419 	WCrt(ba, CRT_ID_START_ADDR_HIGH, 0x00);
    420 	WCrt(ba, CRT_ID_START_ADDR_LOW, 0x00);
    421 	WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, 0x00);
    422 	WCrt(ba, CRT_ID_CURSOR_LOC_LOW, 0x00);
    423 
    424 	WCrt(ba, CRT_ID_UNDERLINE_LOC, 0x07);
    425 	WCrt(ba, CRT_ID_MODE_CONTROL, 0xa3);	/* c3 */
    426 	WCrt(ba, CRT_ID_LINE_COMPARE, 0xff);	/* ff */
    427 	WCrt(ba, CRT_ID_EXT_DISP_CNTL, 0x22);
    428 	if (cl_sd64 == 1) {
    429 		WCrt(ba, CRT_ID_SYNC_ADJ_GENLOCK, 0x00);
    430 		WCrt(ba, CRT_ID_OVERLAY_EXT_CTRL_REG, 0x40);
    431 	}
    432 	WSeq(ba, SEQ_ID_CURSOR_STORE, 0x3c);	/* mouse 0x00 */
    433 
    434 	WGfx(ba, GCT_ID_SET_RESET, 0x00);
    435 	WGfx(ba, GCT_ID_ENABLE_SET_RESET, 0x00);
    436 	WGfx(ba, GCT_ID_DATA_ROTATE, 0x00);
    437 	WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x00);
    438 	WGfx(ba, GCT_ID_GRAPHICS_MODE, 0x00);
    439 	WGfx(ba, GCT_ID_MISC, 0x01);
    440 	WGfx(ba, GCT_ID_COLOR_XCARE, 0x0f);
    441 	WGfx(ba, GCT_ID_BITMASK, 0xff);
    442 	WGfx(ba, GCT_ID_MODE_EXT, 0x28);
    443 
    444 	for (x = 0; x < 0x10; x++)
    445 		WAttr(ba, x, x);
    446 	WAttr(ba, ACT_ID_ATTR_MODE_CNTL, 0x01);
    447 	WAttr(ba, ACT_ID_OVERSCAN_COLOR, 0x00);
    448 	WAttr(ba, ACT_ID_COLOR_PLANE_ENA, 0x0f);
    449 	WAttr(ba, ACT_ID_HOR_PEL_PANNING, 0x00);
    450 	WAttr(ba, ACT_ID_COLOR_SELECT, 0x00);
    451 
    452 	delay(200000);
    453 	WAttr(ba, 0x34, 0x00);
    454 	delay(200000);
    455 
    456 	vgaw(ba, VDAC_MASK, 0xff);
    457 	delay(200000);
    458 	vgaw(ba, GREG_MISC_OUTPUT_W, 0xef);
    459 
    460 	WGfx(ba, GCT_ID_BLT_STAT_START, 0x04);
    461 	WGfx(ba, GCT_ID_BLT_STAT_START, 0x00);
    462 
    463 	/* colors initially set to greyscale */
    464 
    465 	vgaw(ba, VDAC_ADDRESS_W, 0);
    466 	for (x = 255; x >= 0; x--) {
    467 		vgaw(ba, VDAC_DATA, x);
    468 		vgaw(ba, VDAC_DATA, x);
    469 		vgaw(ba, VDAC_DATA, x);
    470 	}
    471 	/* set sprite bitmap pointers */
    472 	cl_cursprite.image = cl_imageptr;
    473 	cl_cursprite.mask = cl_maskptr;
    474 	cl_cursprite.cmap.red = cl_sprred;
    475 	cl_cursprite.cmap.green = cl_sprgreen;
    476 	cl_cursprite.cmap.blue = cl_sprblue;
    477 }
    478 
    479 
    480 int
    481 cl_getvmode(gp, vm)
    482 	struct grf_softc *gp;
    483 	struct grfvideo_mode *vm;
    484 {
    485 	struct grfvideo_mode *gv;
    486 
    487 #ifdef CL5426CONSOLE
    488 	/* Handle grabbing console mode */
    489 	if (vm->mode_num == 255) {
    490 		bcopy(&clconsole_mode, vm, sizeof(struct grfvideo_mode));
    491 		/* XXX so grfconfig can tell us the correct text dimensions. */
    492 		vm->depth = clconsole_mode.fy;
    493 	} else
    494 #endif
    495         {
    496                 if (vm->mode_num == 0)
    497                         vm->mode_num = (monitor_current - monitor_def) + 1;
    498                 if (vm->mode_num < 1 || vm->mode_num > monitor_def_max)
    499                         return (EINVAL);
    500                 gv = monitor_def + (vm->mode_num - 1);
    501                 if (gv->mode_num == 0)
    502                         return (EINVAL);
    503 
    504                 bcopy(gv, vm, sizeof(struct grfvideo_mode));
    505         }
    506 
    507         /* adjust internal values to pixel values */
    508 
    509         vm->hblank_start *= 8;
    510         vm->hblank_stop *= 8;
    511         vm->hsync_start *= 8;
    512         vm->hsync_stop *= 8;
    513         vm->htotal *= 8;
    514 
    515 	return (0);
    516 }
    517 
    518 
    519 int
    520 cl_setvmode(gp, mode)
    521 	struct grf_softc *gp;
    522 	unsigned mode;
    523 {
    524 	if (!mode || (mode > monitor_def_max) ||
    525 	    monitor_def[mode - 1].mode_num == 0)
    526 		return (EINVAL);
    527 
    528 	monitor_current = monitor_def + (mode - 1);
    529 
    530 	return (0);
    531 }
    532 
    533 #ifndef CL5426CONSOLE
    534 void
    535 cl_off(gp)
    536 	struct grf_softc *gp;
    537 {
    538 	char   *ba = gp->g_regkva;
    539 
    540 	/* we'll put the pass-through on for cc ite and set Full Bandwidth bit
    541 	 * on just in case it didn't work...but then it doesn't matter does
    542 	 * it? =) */
    543 	RegOnpass(ba);
    544 	WSeq(ba, SEQ_ID_CLOCKING_MODE, 0x21);
    545 }
    546 #endif
    547 
    548 int
    549 cl_blank(gp, on)
    550         struct grf_softc *gp;
    551         int *on;
    552 {
    553         WSeq(gp->g_regkva, SEQ_ID_CLOCKING_MODE, *on > 0 ? 0x01 : 0x21);
    554         return(0);
    555 }
    556 
    557 /*
    558  * Change the mode of the display.
    559  * Return a UNIX error number or 0 for success.
    560  */
    561 int
    562 cl_mode(gp, cmd, arg, a2, a3)
    563 	register struct grf_softc *gp;
    564 	u_long cmd;
    565 	void *arg;
    566 	u_long a2;
    567 	int a3;
    568 {
    569 	int     error;
    570 
    571 	switch (cmd) {
    572 	    case GM_GRFON:
    573 		error = cl_load_mon(gp,
    574 		    (struct grfcltext_mode *) monitor_current) ? 0 : EINVAL;
    575 		return (error);
    576 
    577 	    case GM_GRFOFF:
    578 #ifndef CL5426CONSOLE
    579 		cl_off(gp);
    580 #else
    581 		cl_load_mon(gp, &clconsole_mode);
    582 #endif
    583 		return (0);
    584 
    585 	    case GM_GRFCONFIG:
    586 		return (0);
    587 
    588 	    case GM_GRFGETVMODE:
    589 		return (cl_getvmode(gp, (struct grfvideo_mode *) arg));
    590 
    591 	    case GM_GRFSETVMODE:
    592 		error = cl_setvmode(gp, *(unsigned *) arg);
    593 		if (!error && (gp->g_flags & GF_GRFON))
    594 			cl_load_mon(gp,
    595 			    (struct grfcltext_mode *) monitor_current);
    596 		return (error);
    597 
    598 	    case GM_GRFGETNUMVM:
    599 		*(int *) arg = monitor_def_max;
    600 		return (0);
    601 
    602 	    case GM_GRFIOCTL:
    603 		return (cl_ioctl(gp, a2, arg));
    604 
    605 	    default:
    606 		break;
    607 	}
    608 
    609 	return (EINVAL);
    610 }
    611 
    612 int
    613 cl_ioctl(gp, cmd, data)
    614 	register struct grf_softc *gp;
    615 	u_long cmd;
    616 	void   *data;
    617 {
    618 	switch (cmd) {
    619 	    case GRFIOCGSPRITEPOS:
    620 		return (cl_getmousepos(gp, (struct grf_position *) data));
    621 
    622 	    case GRFIOCSSPRITEPOS:
    623 		return (cl_setmousepos(gp, (struct grf_position *) data));
    624 
    625 	    case GRFIOCSSPRITEINF:
    626 		return (cl_setspriteinfo(gp, (struct grf_spriteinfo *) data));
    627 
    628 	    case GRFIOCGSPRITEINF:
    629 		return (cl_getspriteinfo(gp, (struct grf_spriteinfo *) data));
    630 
    631 	    case GRFIOCGSPRITEMAX:
    632 		return (cl_getspritemax(gp, (struct grf_position *) data));
    633 
    634 	    case GRFIOCGETCMAP:
    635 		return (cl_getcmap(gp, (struct grf_colormap *) data));
    636 
    637 	    case GRFIOCPUTCMAP:
    638 		return (cl_putcmap(gp, (struct grf_colormap *) data));
    639 
    640 	    case GRFIOCBITBLT:
    641 		break;
    642 
    643 	    case GRFTOGGLE:
    644 		return (cl_toggle(gp, 0));
    645 
    646 	    case GRFIOCSETMON:
    647 		return (cl_setmonitor(gp, (struct grfvideo_mode *) data));
    648 
    649             case GRFIOCBLANK:
    650                 return (cl_blank(gp, (int *)data));
    651 
    652 	}
    653 	return (EINVAL);
    654 }
    655 
    656 int
    657 cl_getmousepos(gp, data)
    658 	struct grf_softc *gp;
    659 	struct grf_position *data;
    660 {
    661 	data->x = cl_cursprite.pos.x;
    662 	data->y = cl_cursprite.pos.y;
    663 	return (0);
    664 }
    665 
    666 void
    667 cl_writesprpos(ba, x, y)
    668 	volatile char *ba;
    669 	short   x;
    670 	short   y;
    671 {
    672 	/* we want to use a 16-bit write to 3c4 so no macros used */
    673 	volatile unsigned char *cwp;
    674         volatile unsigned short *wp;
    675 
    676 	cwp = ba + 0x3c4;
    677         wp = (unsigned short *)cwp;
    678 
    679 	/* don't ask me why, but apparently you can't do a 16-bit write with
    680 	 * x-position like with y-position below (dagge) */
    681         cwp[0] = 0x10 | ((x << 5) & 0xff);
    682         cwp[1] = (x >> 3) & 0xff;
    683 
    684         *wp = 0x1100 | ((y & 7) << 13) | ((y >> 3) & 0xff);
    685 
    686 }
    687 
    688 void
    689 writeshifted(to, shiftx, shifty)
    690 	volatile char *to;
    691 	char    shiftx;
    692 	char    shifty;
    693 {
    694 	register char y;
    695 	unsigned long long *tptr, *iptr, *mptr, line;
    696 
    697 	tptr = (unsigned long long *) to;
    698         iptr = (unsigned long long *) cl_cursprite.image;
    699         mptr = (unsigned long long *) cl_cursprite.mask;
    700 
    701         shiftx = shiftx < 0 ? 0 : shiftx;
    702         shifty = shifty < 0 ? 0 : shifty;
    703 
    704         /* start reading shifty lines down, and
    705          * shift each line in by shiftx
    706          */
    707         for (y = shifty; y < 64; y++) {
    708 
    709                 /* image */
    710                 line = iptr[y];
    711 		*tptr++ = line << shiftx;
    712 
    713                 /* mask */
    714                 line = mptr[y];
    715 		*tptr++ = line << shiftx;
    716 	}
    717 
    718         /* clear the remainder */
    719         for (y = shifty; y > 0; y--) {
    720                 *tptr++ = 0;
    721                 *tptr++ = 0;
    722         }
    723 }
    724 
    725 int
    726 cl_setmousepos(gp, data)
    727 	struct grf_softc *gp;
    728 	struct grf_position *data;
    729 {
    730 	volatile char *ba = gp->g_regkva;
    731         short rx, ry, prx, pry;
    732 #ifdef CL_SHIFTSPRITE
    733 	volatile char *fb = gp->g_fbkva;
    734         volatile char *sprite = fb + (cl_fbsize - 1024);
    735 #endif
    736 
    737         /* no movement */
    738 	if (cl_cursprite.pos.x == data->x && cl_cursprite.pos.y == data->y)
    739 		return (0);
    740 
    741         /* current and previous real coordinates */
    742 	rx = data->x - cl_cursprite.hot.x;
    743 	ry = data->y - cl_cursprite.hot.y;
    744 	prx = cl_cursprite.pos.x - cl_cursprite.hot.x;
    745 	pry = cl_cursprite.pos.y - cl_cursprite.hot.y;
    746 
    747         /* if we are/were on an edge, create (un)shifted bitmap --
    748          * ripped out optimization (not extremely worthwhile,
    749          * and kind of buggy anyhow).
    750          */
    751 #ifdef CL_SHIFTSPRITE
    752         if (rx < 0 || ry < 0 || prx < 0 || pry < 0) {
    753                 writeshifted(sprite, rx < 0 ? -rx : 0, ry < 0 ? -ry : 0);
    754         }
    755 #endif
    756 
    757         /* do movement, save position */
    758         cl_writesprpos(ba, rx < 0 ? 0 : rx, ry < 0 ? 0 : ry);
    759 	cl_cursprite.pos.x = data->x;
    760 	cl_cursprite.pos.y = data->y;
    761 
    762 	return (0);
    763 }
    764 
    765 int
    766 cl_getspriteinfo(gp, data)
    767 	struct grf_softc *gp;
    768 	struct grf_spriteinfo *data;
    769 {
    770 	copyout(&cl_cursprite, data, sizeof(struct grf_spriteinfo));
    771 	copyout(cl_cursprite.image, data->image, 64 * 8);
    772 	copyout(cl_cursprite.mask, data->mask, 64 * 8);
    773 	return (0);
    774 }
    775 
    776 static int
    777 cl_setspriteinfo(gp, data)
    778 	struct grf_softc *gp;
    779 	struct grf_spriteinfo *data;
    780 {
    781 	volatile unsigned char *ba = gp->g_regkva, *fb = gp->g_fbkva;
    782         volatile char *sprite = fb + (cl_fbsize - 1024);
    783 
    784 	if (data->set & GRFSPRSET_SHAPE) {
    785 
    786                 short dsx, dsy, i;
    787                 unsigned long *di, *dm, *si, *sm;
    788                 unsigned long ssi[128], ssm[128];
    789                 struct grf_position gpos;
    790 
    791 
    792                 /* check for a too large sprite (no clipping!) */
    793                 dsy = data->size.y;
    794                 dsx = data->size.x;
    795                 if (dsy > 64 || dsx > 64)
    796                         return(EINVAL);
    797 
    798                 /* prepare destination */
    799                 di = (unsigned long *)cl_cursprite.image;
    800                 dm = (unsigned long *)cl_cursprite.mask;
    801                 cl_memset((unsigned char *)di, 0, 8*64);
    802                 cl_memset((unsigned char *)dm, 0, 8*64);
    803 
    804                 /* two alternatives:  64 across, then it's
    805                  * the same format we use, just copy.  Otherwise,
    806                  * copy into tmp buf and recopy skipping the
    807                  * unused 32 bits.
    808                  */
    809                 if ((dsx - 1) / 32) {
    810                         copyin(data->image, di, 8 * dsy);
    811                         copyin(data->mask, dm, 8 * dsy);
    812                 } else {
    813                         si = ssi; sm = ssm;
    814                         copyin(data->image, si, 4 * dsy);
    815                         copyin(data->mask, sm, 4 * dsy);
    816                         for (i = 0; i < dsy; i++) {
    817                                 *di = *si++;
    818                                 *dm = *sm++;
    819                                 di += 2;
    820                                 dm += 2;
    821                         }
    822                 }
    823 
    824                 /* set size */
    825 		cl_cursprite.size.x = data->size.x;
    826 		cl_cursprite.size.y = data->size.y;
    827 
    828                 /* forcably load into board */
    829                 gpos.x = cl_cursprite.pos.x;
    830                 gpos.y = cl_cursprite.pos.y;
    831                 cl_cursprite.pos.x = -1;
    832                 cl_cursprite.pos.y = -1;
    833                 writeshifted(sprite, 0, 0);
    834                 cl_setmousepos(gp, &gpos);
    835 
    836 	}
    837 	if (data->set & GRFSPRSET_HOT) {
    838 
    839 		cl_cursprite.hot = data->hot;
    840 
    841 	}
    842 	if (data->set & GRFSPRSET_CMAP) {
    843 
    844 		u_char  red[2], green[2], blue[2];
    845 
    846 		copyin(data->cmap.red, red, 2);
    847 		copyin(data->cmap.green, green, 2);
    848 		copyin(data->cmap.blue, blue, 2);
    849 		bcopy(red, cl_cursprite.cmap.red, 2);
    850 		bcopy(green, cl_cursprite.cmap.green, 2);
    851 		bcopy(blue, cl_cursprite.cmap.blue, 2);
    852 
    853                 /* enable and load colors 256 & 257 */
    854 		WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x06);
    855 
    856                 /* 256 */
    857 		vgaw(ba, VDAC_ADDRESS_W, 0x00);
    858 		if (cltype == PICASSO) {
    859 			vgaw(ba, VDAC_DATA, (u_char) (red[0] >> 2));
    860 			vgaw(ba, VDAC_DATA, (u_char) (green[0] >> 2));
    861 			vgaw(ba, VDAC_DATA, (u_char) (blue[0] >> 2));
    862 		} else {
    863 			vgaw(ba, VDAC_DATA, (u_char) (blue[0] >> 2));
    864 			vgaw(ba, VDAC_DATA, (u_char) (green[0] >> 2));
    865 			vgaw(ba, VDAC_DATA, (u_char) (red[0] >> 2));
    866 		}
    867 
    868                 /* 257 */
    869 		vgaw(ba, VDAC_ADDRESS_W, 0x0f);
    870 		if (cltype == PICASSO) {
    871 			vgaw(ba, VDAC_DATA, (u_char) (red[1] >> 2));
    872 			vgaw(ba, VDAC_DATA, (u_char) (green[1] >> 2));
    873 			vgaw(ba, VDAC_DATA, (u_char) (blue[1] >> 2));
    874 		} else {
    875 			vgaw(ba, VDAC_DATA, (u_char) (blue[1] >> 2));
    876 			vgaw(ba, VDAC_DATA, (u_char) (green[1] >> 2));
    877 			vgaw(ba, VDAC_DATA, (u_char) (red[1] >> 2));
    878 		}
    879 
    880                 /* turn on/off sprite */
    881 		if (cl_cursprite.enable) {
    882 			WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x05);
    883 		} else {
    884 			WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x04);
    885 		}
    886 
    887 	}
    888 	if (data->set & GRFSPRSET_ENABLE) {
    889 
    890 		if (data->enable == 1) {
    891 			WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x05);
    892 			cl_cursprite.enable = 1;
    893 		} else {
    894 			WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x04);
    895 			cl_cursprite.enable = 0;
    896 		}
    897 
    898 	}
    899 	if (data->set & GRFSPRSET_POS) {
    900 
    901                 /* force placement */
    902                 cl_cursprite.pos.x = -1;
    903                 cl_cursprite.pos.y = -1;
    904 
    905                 /* do it */
    906                 cl_setmousepos(gp, &data->pos);
    907 
    908 	}
    909 	return (0);
    910 }
    911 
    912 static int
    913 cl_getspritemax(gp, data)
    914 	struct grf_softc *gp;
    915 	struct grf_position *data;
    916 {
    917 	if (gp->g_display.gd_planes == 24)
    918 		return (EINVAL);
    919 	data->x = 64;
    920 	data->y = 64;
    921 	return (0);
    922 }
    923 
    924 int
    925 cl_setmonitor(gp, gv)
    926 	struct grf_softc *gp;
    927 	struct grfvideo_mode *gv;
    928 {
    929 	struct grfvideo_mode *md;
    930 
    931         if (!cl_mondefok(gv))
    932                 return(EINVAL);
    933 
    934 #ifdef CL5426CONSOLE
    935 	/* handle interactive setting of console mode */
    936 	if (gv->mode_num == 255) {
    937 		bcopy(gv, &clconsole_mode.gv, sizeof(struct grfvideo_mode));
    938                 clconsole_mode.gv.hblank_start /= 8;
    939                 clconsole_mode.gv.hblank_stop /= 8;
    940                 clconsole_mode.gv.hsync_start /= 8;
    941                 clconsole_mode.gv.hsync_stop /= 8;
    942                 clconsole_mode.gv.htotal /= 8;
    943 		clconsole_mode.rows = gv->disp_height / clconsole_mode.fy;
    944 		clconsole_mode.cols = gv->disp_width / clconsole_mode.fx;
    945 		if (!(gp->g_flags & GF_GRFON))
    946 			cl_load_mon(gp, &clconsole_mode);
    947 		ite_reinit(gp->g_itedev);
    948 		return (0);
    949 	}
    950 #endif
    951 
    952 	md = monitor_def + (gv->mode_num - 1);
    953 	bcopy(gv, md, sizeof(struct grfvideo_mode));
    954 
    955         /* adjust pixel oriented values to internal rep. */
    956 
    957         md->hblank_start /= 8;
    958         md->hblank_stop /= 8;
    959         md->hsync_start /= 8;
    960         md->hsync_stop /= 8;
    961         md->htotal /= 8;
    962 
    963 	return (0);
    964 }
    965 
    966 int
    967 cl_getcmap(gfp, cmap)
    968 	struct grf_softc *gfp;
    969 	struct grf_colormap *cmap;
    970 {
    971 	volatile unsigned char *ba;
    972 	u_char  red[256], green[256], blue[256], *rp, *gp, *bp;
    973 	short   x;
    974 	int     error;
    975 
    976 	if (cmap->count == 0 || cmap->index >= 256)
    977 		return 0;
    978 
    979 	if (cmap->index + cmap->count > 256)
    980 		cmap->count = 256 - cmap->index;
    981 
    982 	ba = gfp->g_regkva;
    983 	/* first read colors out of the chip, then copyout to userspace */
    984 	vgaw(ba, VDAC_ADDRESS_R, cmap->index);
    985 	x = cmap->count - 1;
    986 
    987 /* Some sort 'o Magic. Spectrum has some changes on the board to speed
    988  * up 15 and 16Bit modes. They can access these modes with easy-to-programm
    989  * rgbrgbrgb instead of rrrgggbbb. Side effect: when in 8Bit mode, rgb
    990  * is swapped to bgr. I wonder if we need to check for 8Bit though, ill
    991  */
    992 
    993 	switch (cltype) {
    994 	    case SPECTRUM:
    995 	    case PICCOLO:
    996 		rp = blue + cmap->index;
    997 		gp = green + cmap->index;
    998 		bp = red + cmap->index;
    999 		break;
   1000 	    case PICASSO:
   1001 		rp = red + cmap->index;
   1002 		gp = green + cmap->index;
   1003 		bp = blue + cmap->index;
   1004 		break;
   1005 	    default:
   1006 		rp = gp = bp = 0;
   1007 		break;
   1008 	}
   1009 
   1010 	do {
   1011 		*rp++ = vgar(ba, VDAC_DATA) << 2;
   1012 		*gp++ = vgar(ba, VDAC_DATA) << 2;
   1013 		*bp++ = vgar(ba, VDAC_DATA) << 2;
   1014 	} while (x-- > 0);
   1015 
   1016 	if (!(error = copyout(red + cmap->index, cmap->red, cmap->count))
   1017 	    && !(error = copyout(green + cmap->index, cmap->green, cmap->count))
   1018 	    && !(error = copyout(blue + cmap->index, cmap->blue, cmap->count)))
   1019 		return (0);
   1020 
   1021 	return (error);
   1022 }
   1023 
   1024 int
   1025 cl_putcmap(gfp, cmap)
   1026 	struct grf_softc *gfp;
   1027 	struct grf_colormap *cmap;
   1028 {
   1029 	volatile unsigned char *ba;
   1030 	u_char  red[256], green[256], blue[256], *rp, *gp, *bp;
   1031 	short   x;
   1032 	int     error;
   1033 
   1034 	if (cmap->count == 0 || cmap->index >= 256)
   1035 		return (0);
   1036 
   1037 	if (cmap->index + cmap->count > 256)
   1038 		cmap->count = 256 - cmap->index;
   1039 
   1040 	/* first copy the colors into kernelspace */
   1041 	if (!(error = copyin(cmap->red, red + cmap->index, cmap->count))
   1042 	    && !(error = copyin(cmap->green, green + cmap->index, cmap->count))
   1043 	    && !(error = copyin(cmap->blue, blue + cmap->index, cmap->count))) {
   1044 		ba = gfp->g_regkva;
   1045 		vgaw(ba, VDAC_ADDRESS_W, cmap->index);
   1046 		x = cmap->count - 1;
   1047 
   1048 		switch (cltype) {
   1049 		    case SPECTRUM:
   1050 		    case PICCOLO:
   1051 			rp = blue + cmap->index;
   1052 			gp = green + cmap->index;
   1053 			bp = red + cmap->index;
   1054 			break;
   1055 		    case PICASSO:
   1056 			rp = red + cmap->index;
   1057 			gp = green + cmap->index;
   1058 			bp = blue + cmap->index;
   1059 			break;
   1060 		    default:
   1061 			rp = gp = bp = 0;
   1062 			break;
   1063 		}
   1064 
   1065 		do {
   1066 			vgaw(ba, VDAC_DATA, *rp++ >> 2);
   1067 			vgaw(ba, VDAC_DATA, *gp++ >> 2);
   1068 			vgaw(ba, VDAC_DATA, *bp++ >> 2);
   1069 		} while (x-- > 0);
   1070 		return (0);
   1071 	} else
   1072 		return (error);
   1073 }
   1074 
   1075 
   1076 int
   1077 cl_toggle(gp, wopp)
   1078 	struct grf_softc *gp;
   1079 	unsigned short wopp;	/* don't need that one yet, ill */
   1080 {
   1081 	volatile caddr_t ba;
   1082 
   1083 	ba = gp->g_regkva;
   1084 
   1085 	if (pass_toggle) {
   1086 		RegOffpass(ba);
   1087 	} else {
   1088 		/* This was in the original.. is it needed? */
   1089 		if (cltype == PICASSO || cltype == PICCOLO)
   1090 			RegWakeup(ba);
   1091 		RegOnpass(ba);
   1092 	}
   1093 	return (0);
   1094 }
   1095 
   1096 static void
   1097 cl_CompFQ(fq, num, denom)
   1098 	u_int   fq;
   1099 	u_char *num;
   1100 	u_char *denom;
   1101 {
   1102 #define OSC     14318180
   1103 /* OK, here's what we're doing here:
   1104  *
   1105  *             OSC * NUMERATOR
   1106  *      VCLK = -------------------  Hz
   1107  *             DENOMINATOR * (1+P)
   1108  *
   1109  * so we're given VCLK and we should give out some useful
   1110  * values....
   1111  *
   1112  * NUMERATOR is 7 bits wide
   1113  * DENOMINATOR is 5 bits wide with bit P in the same char as bit 0.
   1114  *
   1115  * We run through all the possible combinations and
   1116  * return the values which deviate the least from the chosen frequency.
   1117  *
   1118  */
   1119 #define OSC     14318180
   1120 #define count(n,d,p)    ((OSC * n)/(d * (1+p)))
   1121 
   1122 	unsigned char n, d, p, minn, mind, minp = 0;
   1123 	unsigned long err, minerr;
   1124 
   1125 /*
   1126 numer = 0x00 - 0x7f
   1127 denom = 0x00 - 0x1f (1) 0x20 - 0x3e (even)
   1128 */
   1129 
   1130 	/* find lowest error in 6144 iterations. */
   1131 	minerr = fq;
   1132 	minn = 0;
   1133 	mind = 0;
   1134 	p = 0;
   1135 
   1136 	for (d = 1; d < 0x20; d++) {
   1137 		for (n = 1; n < 0x80; n++) {
   1138 			err = abs(count(n, d, p) - fq);
   1139 			if (err < minerr) {
   1140 				minerr = err;
   1141 				minn = n;
   1142 				mind = d;
   1143 				minp = p;
   1144 			}
   1145 		}
   1146 		if (d == 0x1f && p == 0) {
   1147 			p = 1;
   1148 			d = 0x0f;
   1149 		}
   1150 	}
   1151 
   1152 	*num = minn;
   1153 	*denom = (mind << 1) | minp;
   1154 	if (minerr > 500000)
   1155 		printf("Warning: CompFQ minimum error = %ld\n", minerr);
   1156 	return;
   1157 }
   1158 
   1159 int
   1160 cl_mondefok(gv)
   1161 	struct grfvideo_mode *gv;
   1162 {
   1163         unsigned long maxpix;
   1164 
   1165 	if (gv->mode_num < 1 || gv->mode_num > monitor_def_max)
   1166                 if (gv->mode_num != 255 || gv->depth != 4)
   1167                         return(0);
   1168 
   1169 	switch (gv->depth) {
   1170 	    case 4:
   1171                 if (gv->mode_num != 255)
   1172                         return(0);
   1173 	    case 1:
   1174 	    case 8:
   1175                 maxpix = cl_maxpixelclock;
   1176                 break;
   1177 	    case 15:
   1178 	    case 16:
   1179                 maxpix = cl_maxpixelclock - (cl_maxpixelclock / 3);
   1180                 break;
   1181 	    case 24:
   1182                 maxpix = cl_maxpixelclock / 3;
   1183                 break;
   1184 	default:
   1185 		return (0);
   1186 	}
   1187         if (gv->pixel_clock > maxpix)
   1188                 return (0);
   1189         return (1);
   1190 }
   1191 
   1192 int
   1193 cl_load_mon(gp, md)
   1194 	struct grf_softc *gp;
   1195 	struct grfcltext_mode *md;
   1196 {
   1197 	struct grfvideo_mode *gv;
   1198 	struct grfinfo *gi;
   1199 	volatile caddr_t ba, fb;
   1200 	unsigned char num0, denom0;
   1201 	unsigned short HT, HDE, HBS, HBE, HSS, HSE, VDE, VBS, VBE, VSS,
   1202 	        VSE, VT;
   1203 	char    LACE, DBLSCAN, TEXT;
   1204 	unsigned short clkdiv;
   1205 	int     uplim, lowlim;
   1206 	int	sr15;
   1207 
   1208 	/* identity */
   1209 	gv = &md->gv;
   1210 	TEXT = (gv->depth == 4);
   1211 
   1212 	if (!cl_mondefok(gv)) {
   1213 		printf("mondef not ok\n");
   1214 		return (0);
   1215 	}
   1216 	ba = gp->g_regkva;
   1217 	fb = gp->g_fbkva;
   1218 
   1219 	/* provide all needed information in grf device-independant locations */
   1220 	gp->g_data = (caddr_t) gv;
   1221 	gi = &gp->g_display;
   1222 	gi->gd_regaddr = (caddr_t) ztwopa(ba);
   1223 	gi->gd_regsize = 64 * 1024;
   1224 	gi->gd_fbaddr = (caddr_t) kvtop(fb);
   1225 	gi->gd_fbsize = cl_fbsize;
   1226 	gi->gd_colors = 1 << gv->depth;
   1227 	gi->gd_planes = gv->depth;
   1228 	gi->gd_fbwidth = gv->disp_width;
   1229 	gi->gd_fbheight = gv->disp_height;
   1230 	gi->gd_fbx = 0;
   1231 	gi->gd_fby = 0;
   1232 	if (TEXT) {
   1233 		gi->gd_dwidth = md->fx * md->cols;
   1234 		gi->gd_dheight = md->fy * md->rows;
   1235 	} else {
   1236 		gi->gd_dwidth = gv->disp_width;
   1237 		gi->gd_dheight = gv->disp_height;
   1238 	}
   1239 	gi->gd_dx = 0;
   1240 	gi->gd_dy = 0;
   1241 
   1242 	/* get display mode parameters */
   1243 
   1244 	HBS = gv->hblank_start;
   1245 	HBE = gv->hblank_stop;
   1246 	HSS = gv->hsync_start;
   1247 	HSE = gv->hsync_stop;
   1248 	HT = gv->htotal;
   1249 	VBS = gv->vblank_start;
   1250 	VSS = gv->vsync_start;
   1251 	VSE = gv->vsync_stop;
   1252 	VBE = gv->vblank_stop;
   1253 	VT = gv->vtotal;
   1254 
   1255 	if (TEXT)
   1256 		HDE = ((gv->disp_width + md->fx - 1) / md->fx) - 1;
   1257 	else
   1258 		HDE = (gv->disp_width + 3) / 8 - 1;	/* HBS; */
   1259 	VDE = gv->disp_height - 1;
   1260 
   1261 	/* figure out whether lace or dblscan is needed */
   1262 
   1263 	uplim = gv->disp_height + (gv->disp_height / 4);
   1264 	lowlim = gv->disp_height - (gv->disp_height / 4);
   1265 	LACE = (((VT * 2) > lowlim) && ((VT * 2) < uplim)) ? 1 : 0;
   1266 	DBLSCAN = (((VT / 2) > lowlim) && ((VT / 2) < uplim)) ? 1 : 0;
   1267 
   1268 	/* adjustments */
   1269 
   1270 	if (LACE)
   1271 		VDE /= 2;
   1272 
   1273 	WSeq(ba, SEQ_ID_MEMORY_MODE, (TEXT || (gv->depth == 1)) ? 0x06 : 0x0e);
   1274 	if (cl_sd64 == 1) {
   1275 	    if (TEXT || (gv->depth == 1))
   1276 		sr15 = 0x90;
   1277 	    else
   1278 		sr15 = ((cl_fbsize / 0x100000 == 2) ? 0x38 : 0xb8);
   1279 	    WSeq(ba, SEQ_ID_CONF_RBACK, 0x00);
   1280 	} else {
   1281 		sr15 = (TEXT || (gv->depth == 1)) ? 0xd0 : 0xb0;
   1282 	}
   1283 	WSeq(ba, SEQ_ID_DRAM_CNTL, sr15);
   1284 	WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x00);
   1285 	WSeq(ba, SEQ_ID_MAP_MASK, (gv->depth == 1) ? 0x01 : 0xff);
   1286 	WSeq(ba, SEQ_ID_CHAR_MAP_SELECT, 0x00);
   1287 
   1288 	/* Set clock */
   1289 
   1290 	cl_CompFQ((gv->depth == 24) ? gv->pixel_clock * 3 : gv->pixel_clock,
   1291 	    &num0, &denom0);
   1292 	WSeq(ba, SEQ_ID_VCLK_3_NUM, num0);
   1293 	WSeq(ba, SEQ_ID_VCLK_3_DENOM, denom0);
   1294 
   1295 	/* load display parameters into board */
   1296 
   1297 	WCrt(ba, CRT_ID_HOR_TOTAL, HT);
   1298 	WCrt(ba, CRT_ID_HOR_DISP_ENA_END, ((HDE >= HBS) ? HBS - 1 : HDE));
   1299 	WCrt(ba, CRT_ID_START_HOR_BLANK, HBS);
   1300 	WCrt(ba, CRT_ID_END_HOR_BLANK, (HBE & 0x1f) | 0x80);	/* | 0x80? */
   1301 	WCrt(ba, CRT_ID_START_HOR_RETR, HSS);
   1302 	WCrt(ba, CRT_ID_END_HOR_RETR,
   1303 	    (HSE & 0x1f) |
   1304 	    ((HBE & 0x20) ? 0x80 : 0x00));
   1305 	WCrt(ba, CRT_ID_VER_TOTAL, VT);
   1306 	WCrt(ba, CRT_ID_OVERFLOW,
   1307 	    0x10 |
   1308 	    ((VT & 0x100) ? 0x01 : 0x00) |
   1309 	    ((VDE & 0x100) ? 0x02 : 0x00) |
   1310 	    ((VSS & 0x100) ? 0x04 : 0x00) |
   1311 	    ((VBS & 0x100) ? 0x08 : 0x00) |
   1312 	    ((VT & 0x200) ? 0x20 : 0x00) |
   1313 	    ((VDE & 0x200) ? 0x40 : 0x00) |
   1314 	    ((VSS & 0x200) ? 0x80 : 0x00));
   1315 
   1316 
   1317 	WCrt(ba, CRT_ID_CHAR_HEIGHT,
   1318 	    0x40 |		/* TEXT ? 0x00 ??? */
   1319 	    (DBLSCAN ? 0x80 : 0x00) |
   1320 	    ((VBS & 0x200) ? 0x20 : 0x00) |
   1321 	    (TEXT ? ((md->fy - 1) & 0x1f) : 0x00));
   1322 	WCrt(ba, CRT_ID_MODE_CONTROL, 0xe3);
   1323 
   1324 	/* text cursor */
   1325 
   1326 	if (TEXT) {
   1327 #if CL_ULCURSOR
   1328 		WCrt(ba, CRT_ID_CURSOR_START, (md->fy & 0x1f) - 2);
   1329 		WCrt(ba, CRT_ID_CURSOR_END, (md->fy & 0x1f) - 1);
   1330 #else
   1331 		WCrt(ba, CRT_ID_CURSOR_START, 0x00);
   1332 		WCrt(ba, CRT_ID_CURSOR_END, md->fy & 0x1f);
   1333 #endif
   1334 		WCrt(ba, CRT_ID_UNDERLINE_LOC, (md->fy - 1) & 0x1f);
   1335 
   1336 		WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, 0x00);
   1337 		WCrt(ba, CRT_ID_CURSOR_LOC_LOW, 0x00);
   1338 	}
   1339 	WCrt(ba, CRT_ID_START_ADDR_HIGH, 0x00);
   1340 	WCrt(ba, CRT_ID_START_ADDR_LOW, 0x00);
   1341 
   1342 	WCrt(ba, CRT_ID_START_VER_RETR, VSS);
   1343 	WCrt(ba, CRT_ID_END_VER_RETR, (VSE & 0x0f) | 0x20);
   1344 	WCrt(ba, CRT_ID_VER_DISP_ENA_END, VDE);
   1345 	WCrt(ba, CRT_ID_START_VER_BLANK, VBS);
   1346 	WCrt(ba, CRT_ID_END_VER_BLANK, VBE);
   1347 
   1348 	WCrt(ba, CRT_ID_LINE_COMPARE, 0xff);
   1349 	WCrt(ba, CRT_ID_LACE_END, HT / 2);	/* MW/16 */
   1350 	WCrt(ba, CRT_ID_LACE_CNTL,
   1351 	    (LACE ? 0x01 : 0x00) |
   1352 	    ((HBE & 0x40) ? 0x10 : 0x00) |
   1353 	    ((HBE & 0x80) ? 0x20 : 0x00) |
   1354 	    ((VBE & 0x100) ? 0x40 : 0x00) |
   1355 	    ((VBE & 0x200) ? 0x80 : 0x00));
   1356 
   1357 	/* depth dependent stuff */
   1358 
   1359 	switch (gv->depth) {
   1360 	    case 1:
   1361 	    case 4:
   1362 	    case 8:
   1363 		clkdiv = 0;
   1364 		break;
   1365 	    case 15:
   1366 	    case 16:
   1367 		clkdiv = 3;
   1368 		break;
   1369 	    case 24:
   1370 		clkdiv = 2;
   1371 		break;
   1372 	    default:
   1373 		clkdiv = 0;
   1374 		panic("grfcl: Unsuported depth: %i", gv->depth);
   1375 		break;
   1376 	}
   1377 
   1378 	WGfx(ba, GCT_ID_GRAPHICS_MODE,
   1379 	    ((TEXT || (gv->depth == 1)) ? 0x00 : 0x40));
   1380 	WGfx(ba, GCT_ID_MISC, (TEXT ? 0x04 : 0x01));
   1381 
   1382 	WSeq(ba, SEQ_ID_EXT_SEQ_MODE,
   1383 	    ((TEXT || (gv->depth == 1)) ? 0x00 : 0x01) |
   1384 	    ((cltype == PICASSO) ? 0x20 : 0x80) |
   1385 	    (clkdiv << 1));
   1386 
   1387 	delay(200000);
   1388 
   1389 	/* write 0x00 to VDAC_MASK before accessing HDR this helps
   1390 	   sometimes, out of "secret" application note (crest) */
   1391 	vgaw(ba, VDAC_MASK, 0);
   1392 	delay(200000);
   1393 	/* reset HDR "magic" access counter (crest) */
   1394 	vgar(ba, VDAC_ADDRESS);
   1395 
   1396 	delay(200000);
   1397 	vgar(ba, VDAC_MASK);
   1398 	delay(200000);
   1399 	vgar(ba, VDAC_MASK);
   1400 	delay(200000);
   1401 	vgar(ba, VDAC_MASK);
   1402 	delay(200000);
   1403 	vgar(ba, VDAC_MASK);
   1404 	delay(200000);
   1405 	switch (gv->depth) {
   1406 	    case 1:
   1407 	    case 4:		/* text */
   1408 		vgaw(ba, VDAC_MASK, 0);
   1409 		HDE = gv->disp_width / 16;
   1410 		break;
   1411 	    case 8:
   1412 		vgaw(ba, VDAC_MASK, 0);
   1413 		HDE = gv->disp_width / 8;
   1414 		break;
   1415 	    case 15:
   1416 		vgaw(ba, VDAC_MASK, 0xd0);
   1417 		HDE = gv->disp_width / 4;
   1418 		break;
   1419 	    case 16:
   1420 		vgaw(ba, VDAC_MASK, 0xc1);
   1421 		HDE = gv->disp_width / 4;
   1422 		break;
   1423 	    case 24:
   1424 		vgaw(ba, VDAC_MASK, 0xc5);
   1425 		HDE = (gv->disp_width / 8) * 3;
   1426 		break;
   1427 	}
   1428 	delay(20000);
   1429 
   1430 	/* reset HDR "magic" access counter (crest) */
   1431 	vgar(ba, VDAC_ADDRESS);
   1432 	delay(200000);
   1433 	/* then enable all bit in VDAC_MASK afterwards (crest) */
   1434 	vgaw(ba, VDAC_MASK, 0xff);
   1435 	delay(20000);
   1436 
   1437 	WCrt(ba, CRT_ID_OFFSET, HDE);
   1438 	if (cl_sd64 == 1) {
   1439 		WCrt(ba, CRT_ID_SYNC_ADJ_GENLOCK, 0x00);
   1440 		WCrt(ba, CRT_ID_OVERLAY_EXT_CTRL_REG, 0x40);
   1441 	}
   1442 	WCrt(ba, CRT_ID_EXT_DISP_CNTL,
   1443 	    ((TEXT && gv->pixel_clock > 29000000) ? 0x40 : 0x00) |
   1444 	    0x22 |
   1445 	    ((HDE > 0xff) ? 0x10 : 0x00));	/* text? */
   1446 
   1447 	delay(200000);
   1448 	WAttr(ba, ACT_ID_ATTR_MODE_CNTL, (TEXT ? 0x0a : 0x01));
   1449 	delay(200000);
   1450 	WAttr(ba, 0x20 | ACT_ID_COLOR_PLANE_ENA,
   1451 	    (gv->depth == 1) ? 0x01 : 0x0f);
   1452 	delay(200000);
   1453 
   1454 	/* text initialization */
   1455 
   1456 	if (TEXT) {
   1457 		cl_inittextmode(gp);
   1458 	}
   1459 	WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x14);
   1460 	WSeq(ba, SEQ_ID_CLOCKING_MODE, 0x01);
   1461 
   1462 	/* Pass-through */
   1463 
   1464 	RegOffpass(ba);
   1465 
   1466 	return (1);
   1467 }
   1468 
   1469 void
   1470 cl_inittextmode(gp)
   1471 	struct grf_softc *gp;
   1472 {
   1473 	struct grfcltext_mode *tm = (struct grfcltext_mode *) gp->g_data;
   1474 	volatile unsigned char *ba = gp->g_regkva;
   1475 	unsigned char *fb = gp->g_fbkva;
   1476 	unsigned char *c, *f, y;
   1477 	unsigned short z;
   1478 
   1479 
   1480 	/* load text font into beginning of display memory. Each character
   1481 	 * cell is 32 bytes long (enough for 4 planes) */
   1482 
   1483 	SetTextPlane(ba, 0x02);
   1484         cl_memset(fb, 0, 256 * 32);
   1485 	c = (unsigned char *) (fb) + (32 * tm->fdstart);
   1486 	f = tm->fdata;
   1487 	for (z = tm->fdstart; z <= tm->fdend; z++, c += (32 - tm->fy))
   1488 		for (y = 0; y < tm->fy; y++)
   1489 			*c++ = *f++;
   1490 
   1491 	/* clear out text/attr planes (three screens worth) */
   1492 
   1493 	SetTextPlane(ba, 0x01);
   1494 	cl_memset(fb, 0x07, tm->cols * tm->rows * 3);
   1495 	SetTextPlane(ba, 0x00);
   1496 	cl_memset(fb, 0x20, tm->cols * tm->rows * 3);
   1497 
   1498 	/* print out a little init msg */
   1499 
   1500 	c = (unsigned char *) (fb) + (tm->cols - 16);
   1501 	strcpy(c, "CIRRUS");
   1502 	c[6] = 0x20;
   1503 
   1504 	/* set colors (B&W) */
   1505 
   1506 
   1507 	vgaw(ba, VDAC_ADDRESS_W, 0);
   1508 	for (z = 0; z < 256; z++) {
   1509 		unsigned char r, g, b;
   1510 
   1511 		y = (z & 1) ? ((z > 7) ? 2 : 1) : 0;
   1512 
   1513 		if (cltype == PICASSO) {
   1514 			r = clconscolors[y][0];
   1515 			g = clconscolors[y][1];
   1516 			b = clconscolors[y][2];
   1517 		} else {
   1518 			b = clconscolors[y][0];
   1519 			g = clconscolors[y][1];
   1520 			r = clconscolors[y][2];
   1521 		}
   1522 		vgaw(ba, VDAC_DATA, r >> 2);
   1523 		vgaw(ba, VDAC_DATA, g >> 2);
   1524 		vgaw(ba, VDAC_DATA, b >> 2);
   1525 	}
   1526 }
   1527 
   1528 void
   1529 cl_memset(d, c, l)
   1530 	unsigned char *d;
   1531 	unsigned char c;
   1532 	int     l;
   1533 {
   1534 	for (; l > 0; l--)
   1535 		*d++ = c;
   1536 }
   1537 
   1538 /* Special wakeup/passthrough registers on graphics boards
   1539  *
   1540  * The methods have diverged a bit for each board, so
   1541  * WPass(P) has been converted into a set of specific
   1542  * inline functions.
   1543  */
   1544 static void
   1545 RegWakeup(ba)
   1546 	volatile caddr_t ba;
   1547 {
   1548 
   1549 	switch (cltype) {
   1550 	    case SPECTRUM:
   1551 		vgaw(ba, PASS_ADDRESS_W, 0x1f);
   1552 		break;
   1553 	    case PICASSO:
   1554 		vgaw(ba, PASS_ADDRESS_W, 0xff);
   1555 		break;
   1556 	    case PICCOLO:
   1557 		if (cl_sd64 == 1)
   1558 			vgaw(ba, PASS_ADDRESS_W, 0x1f);
   1559 		else
   1560 			vgaw(ba, PASS_ADDRESS_W, vgar(ba, PASS_ADDRESS) | 0x10);
   1561 		break;
   1562 	}
   1563 	delay(200000);
   1564 }
   1565 
   1566 static void
   1567 RegOnpass(ba)
   1568 	volatile caddr_t ba;
   1569 {
   1570 
   1571 	switch (cltype) {
   1572 	    case SPECTRUM:
   1573 		vgaw(ba, PASS_ADDRESS_W, 0x4f);
   1574 		break;
   1575 	    case PICASSO:
   1576 		vgaw(ba, PASS_ADDRESS_WP, 0x01);
   1577 		break;
   1578 	    case PICCOLO:
   1579 		if (cl_sd64 == 1)
   1580 			vgaw(ba, PASS_ADDRESS_W, 0x4f);
   1581 		else
   1582 			vgaw(ba, PASS_ADDRESS_W, vgar(ba, PASS_ADDRESS) & 0xdf);
   1583 		break;
   1584 	}
   1585 	pass_toggle = 1;
   1586 	delay(200000);
   1587 }
   1588 
   1589 static void
   1590 RegOffpass(ba)
   1591 	volatile caddr_t ba;
   1592 {
   1593 
   1594 	switch (cltype) {
   1595 	    case SPECTRUM:
   1596 		vgaw(ba, PASS_ADDRESS_W, 0x6f);
   1597 		break;
   1598 	    case PICASSO:
   1599 		vgaw(ba, PASS_ADDRESS_W, 0xff);
   1600 		delay(200000);
   1601 		vgaw(ba, PASS_ADDRESS_W, 0xff);
   1602 		break;
   1603 	    case PICCOLO:
   1604 		if (cl_sd64 == 1)
   1605 			vgaw(ba, PASS_ADDRESS_W, 0x6f);
   1606 		else
   1607 			vgaw(ba, PASS_ADDRESS_W, vgar(ba, PASS_ADDRESS) | 0x20);
   1608 		break;
   1609 	}
   1610 	pass_toggle = 0;
   1611 	delay(200000);
   1612 }
   1613 
   1614 #endif /* NGRFCL */
   1615