Home | History | Annotate | Line # | Download | only in dev
grf_cl.c revision 1.2
      1  1.1  chopps 
      2  1.1  chopps /*
      3  1.1  chopps  * Copyright (c) 1995 Ezra Story
      4  1.1  chopps  * Copyright (c) 1995 Kari Mettinen
      5  1.1  chopps  * Copyright (c) 1994 Markus Wild
      6  1.1  chopps  * Copyright (c) 1994 Lutz Vieweg
      7  1.1  chopps  * All rights reserved.
      8  1.1  chopps  *
      9  1.1  chopps  * Redistribution and use in source and binary forms, with or without
     10  1.1  chopps  * modification, are permitted provided that the following conditions
     11  1.1  chopps  * are met:
     12  1.1  chopps  * 1. Redistributions of source code must retain the above copyright
     13  1.1  chopps  *    notice, this list of conditions and the following disclaimer.
     14  1.1  chopps  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  chopps  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  chopps  *    documentation and/or other materials provided with the distribution.
     17  1.1  chopps  * 3. All advertising materials mentioning features or use of this software
     18  1.1  chopps  *    must display the following acknowledgement:
     19  1.1  chopps  *      This product includes software developed by Lutz Vieweg.
     20  1.1  chopps  * 4. The name of the author may not be used to endorse or promote products
     21  1.1  chopps  *    derived from this software without specific prior written permission
     22  1.1  chopps  *
     23  1.1  chopps  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  1.1  chopps  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  1.1  chopps  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  1.1  chopps  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  1.1  chopps  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  1.1  chopps  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  1.1  chopps  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  1.1  chopps  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  1.1  chopps  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  1.1  chopps  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.1  chopps  */
     34  1.1  chopps 
     35  1.1  chopps #ifdef GRF_CL5426
     36  1.1  chopps /*
     37  1.1  chopps  * Graphics routines for Cirrus CL GD 5426 boards,
     38  1.1  chopps  *
     39  1.1  chopps  * This code offers low-level routines to access Cirrus Cl GD 5426
     40  1.1  chopps  * graphics-boards from within NetBSD for the Amiga.
     41  1.1  chopps  * No warranties for any kind of function at all - this
     42  1.1  chopps  * code may crash your hardware and scratch your harddisk.  Use at your
     43  1.1  chopps  * own risk.  Freely distributable.
     44  1.1  chopps  *
     45  1.1  chopps  * Modified for Cirrus CL GD 5426 from
     46  1.1  chopps  * Lutz Vieweg's retina driver by Kari Mettinen 08/94
     47  1.1  chopps  * Contributions by Ill, ScottE, MiL
     48  1.1  chopps  * Extensively hacked and rewritten by Ezra Story (Ezy) 01/95
     49  1.1  chopps  *
     50  1.1  chopps  * Thanks to Village Tronic Marketing Gmbh for providing me with
     51  1.1  chopps  * a Picasso-II board.
     52  1.1  chopps  * Thanks for Integrated Electronics Oy Ab for providing me with
     53  1.1  chopps  * Cirrus CL GD 542x family documentation.
     54  1.1  chopps  *
     55  1.1  chopps  * TODO:
     56  1.1  chopps  *    Mouse support
     57  1.1  chopps  *    Blitter support
     58  1.1  chopps  *
     59  1.1  chopps  */
     60  1.1  chopps 
     61  1.1  chopps #include <sys/param.h>
     62  1.1  chopps #include <sys/errno.h>
     63  1.1  chopps #include <sys/ioctl.h>
     64  1.1  chopps #include <sys/device.h>
     65  1.1  chopps #include <sys/malloc.h>
     66  1.1  chopps #include <machine/cpu.h>
     67  1.1  chopps #include <dev/cons.h>
     68  1.1  chopps #include <amiga/amiga/device.h>
     69  1.1  chopps #include <amiga/dev/grfioctl.h>
     70  1.1  chopps #include <amiga/dev/grfvar.h>
     71  1.1  chopps #include <amiga/dev/grf_clreg.h>
     72  1.1  chopps #include <amiga/dev/zbusvar.h>
     73  1.1  chopps 
     74  1.1  chopps static int cl_mondefok __P((struct grfvideo_mode *mdp));
     75  1.1  chopps static void cl_boardinit();
     76  1.1  chopps static void CompFQ __P((u_int fq, u_char *num, u_char *denom));
     77  1.1  chopps static int cl_getvmode __P((struct grf_softc *gp, struct grfvideo_mode *vm));
     78  1.1  chopps static int cl_setvmode __P((struct grf_softc *gp, unsigned int mode));
     79  1.1  chopps static int cl_toggle __P((struct grf_softc *gp,unsigned short));
     80  1.1  chopps static int cl_getcmap __P((struct grf_softc *gfp, struct grf_colormap *cmap));
     81  1.1  chopps static int cl_putcmap __P((struct grf_softc *gfp, struct grf_colormap *cmap));
     82  1.1  chopps static void cl_off __P((struct grf_softc *gp));
     83  1.1  chopps static void cl_inittextmode __P((struct grf_softc *gp));
     84  1.1  chopps static int cl_ioctl __P((register struct grf_softc *gp, int cmd, void *data));
     85  1.1  chopps 
     86  1.1  chopps void grfclattach __P((struct device *, struct device *, void *));
     87  1.1  chopps int  grfclprint  __P((void *, char *));
     88  1.1  chopps int  grfclmatch  __P((struct device *, struct cfdata *, void *));
     89  1.1  chopps void memset __P((unsigned char *d, unsigned char c, int l));
     90  1.1  chopps 
     91  1.1  chopps /* Graphics display definitions.
     92  1.1  chopps  * These are filled by 'grfconfig' using GRFIOCSETMON.
     93  1.1  chopps  */
     94  1.1  chopps #define monitor_def_max 8
     95  1.1  chopps static struct grfvideo_mode monitor_def[8] = {
     96  1.1  chopps 	{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}
     97  1.1  chopps };
     98  1.1  chopps static struct grfvideo_mode *monitor_current = &monitor_def[0];
     99  1.1  chopps 
    100  1.1  chopps /* Patchable maximum pixel clock */
    101  1.1  chopps unsigned long cl_maxpixelclock = 115000000;
    102  1.1  chopps 
    103  1.1  chopps /* Console display definition.
    104  1.1  chopps  *   Default hardcoded text mode.  This grf_cl is set up to
    105  1.1  chopps  *   use one text mode only, and this is it.  You may use
    106  1.1  chopps  *   grfconfig to change the mode after boot.
    107  1.1  chopps  */
    108  1.1  chopps /* Console font */
    109  1.2  chopps #ifdef KFONT_8X11
    110  1.2  chopps #define CIRRUSFONT kernel_font_8x11
    111  1.2  chopps #define CIRRUSFONTY 11
    112  1.2  chopps #else
    113  1.1  chopps #define CIRRUSFONT kernel_font_8x8
    114  1.1  chopps #define CIRRUSFONTY 8
    115  1.2  chopps #endif
    116  1.1  chopps extern unsigned char CIRRUSFONT[];
    117  1.1  chopps 
    118  1.1  chopps struct grfcltext_mode clconsole_mode = {
    119  1.1  chopps 	{255, "", 25200000, 640, 480, 4, 80, 94, 99, 100, 100, 481, 490,
    120  1.1  chopps 	498, 522, 522},
    121  1.1  chopps 	8, CIRRUSFONTY, 80, 480/CIRRUSFONTY, CIRRUSFONT, 32, 255
    122  1.1  chopps };
    123  1.1  chopps 
    124  1.1  chopps /* Console colors */
    125  1.1  chopps unsigned char clconscolors[3][3] = {  /* background, foreground, hilite */
    126  1.1  chopps 	{0,0x40,0x50}, {152,152,152},  {255,255,255}
    127  1.1  chopps };
    128  1.1  chopps 
    129  1.1  chopps int             cltype = 0;   /* Picasso, Spectrum or Piccolo */
    130  1.1  chopps unsigned char   pass_toggle;  /* passthru status tracker */
    131  1.1  chopps 
    132  1.1  chopps /* because all 5426-boards have 2 configdev entries, one for
    133  1.1  chopps  * framebuffer mem and the other for regs, we have to hold onto
    134  1.1  chopps  * the pointers globally until we match on both.  This and 'cltype'
    135  1.1  chopps  * are the primary obsticles to multiple board support, but if you
    136  1.1  chopps  * have multiple boards you have bigger problems than grf_cl.
    137  1.1  chopps  */
    138  1.1  chopps static void *cl_fbaddr = 0;     /* framebuffer */
    139  1.1  chopps static void *cl_regaddr = 0;    /* registers */
    140  1.1  chopps static int cl_fbsize;           /* framebuffer size */
    141  1.1  chopps 
    142  1.1  chopps /* standard driver stuff */
    143  1.1  chopps struct cfdriver grfclcd = {
    144  1.1  chopps 	NULL, "grfcl", (cfmatch_t)grfclmatch, grfclattach,
    145  1.1  chopps 	DV_DULL, sizeof(struct grf_softc), NULL, 0
    146  1.1  chopps };
    147  1.1  chopps static struct cfdata *cfdata;
    148  1.1  chopps 
    149  1.1  chopps int
    150  1.1  chopps grfclmatch(pdp, cfp, auxp)
    151  1.1  chopps 	struct device *pdp;
    152  1.1  chopps 	struct cfdata *cfp;
    153  1.1  chopps 	void *auxp;
    154  1.1  chopps {
    155  1.1  chopps 	struct zbus_args *zap;
    156  1.1  chopps 	static int regprod, fbprod;
    157  1.1  chopps 
    158  1.1  chopps 	zap = auxp;
    159  1.1  chopps 
    160  1.1  chopps #ifndef CL5426CONSOLE
    161  1.1  chopps 	if (amiga_realconfig == 0)
    162  1.1  chopps 		 return(0);
    163  1.1  chopps #endif
    164  1.1  chopps 
    165  1.1  chopps 	/* Grab the first board we encounter as the preferred one.
    166  1.1  chopps 	 * This will allow one board to work in a multiple 5426 board
    167  1.1  chopps 	 * system, but not multiple boards at the same time.
    168  1.1  chopps 	 */
    169  1.1  chopps 	if (cltype == 0) {
    170  1.1  chopps 		switch (zap->manid) {
    171  1.1  chopps 		case PICASSO:
    172  1.1  chopps 			regprod = 12;
    173  1.1  chopps 			fbprod = 11;
    174  1.1  chopps 			break;
    175  1.1  chopps 		case SPECTRUM:
    176  1.1  chopps 			regprod = 2;
    177  1.1  chopps 			fbprod = 1;
    178  1.1  chopps 			break;
    179  1.1  chopps 		case PICCOLO:
    180  1.1  chopps 			regprod = 6;
    181  1.1  chopps 			fbprod = 5;
    182  1.1  chopps 			break;
    183  1.1  chopps 		default:
    184  1.1  chopps 			return(0);
    185  1.1  chopps 		}
    186  1.1  chopps 		cltype = zap->manid;
    187  1.1  chopps 	} else {
    188  1.1  chopps 		if (cltype != zap->manid) {
    189  1.1  chopps 			return(0);
    190  1.1  chopps 		}
    191  1.1  chopps 	}
    192  1.1  chopps 
    193  1.1  chopps 	/* Configure either registers or framebuffer in any order
    194  1.1  chopps 	 */
    195  1.1  chopps 	if (zap->prodid == regprod)
    196  1.1  chopps 		cl_regaddr = zap->va;
    197  1.1  chopps 	else if (zap->prodid == fbprod) {
    198  1.1  chopps 		cl_fbaddr = zap->va;
    199  1.1  chopps 		cl_fbsize = zap->size;
    200  1.1  chopps 	} else {
    201  1.1  chopps 		printf("grfcl: Bad product id\n");
    202  1.1  chopps 		return(0);
    203  1.1  chopps 	}
    204  1.1  chopps 
    205  1.1  chopps #ifdef CL5426CONSOLE
    206  1.1  chopps 	if (amiga_realconfig == 0) {
    207  1.1  chopps 		cfdata = cfp;
    208  1.1  chopps 	}
    209  1.1  chopps 
    210  1.1  chopps #endif
    211  1.1  chopps 
    212  1.1  chopps 	return(1);
    213  1.1  chopps }
    214  1.1  chopps 
    215  1.1  chopps void
    216  1.1  chopps grfclattach(pdp, dp, auxp)
    217  1.1  chopps 	struct device *pdp, *dp;
    218  1.1  chopps 	void *auxp;
    219  1.1  chopps {
    220  1.1  chopps 	static struct grf_softc congrf;
    221  1.1  chopps 	struct zbus_args *zap;
    222  1.1  chopps 	struct grf_softc *gp;
    223  1.1  chopps 	int x;
    224  1.1  chopps 	static char attachflag = 0;
    225  1.1  chopps 
    226  1.1  chopps 	zap = auxp;
    227  1.1  chopps 
    228  1.1  chopps 	printf("\n");
    229  1.1  chopps 
    230  1.1  chopps 	/* make sure both halves have matched */
    231  1.1  chopps 	if (!cl_regaddr || !cl_fbaddr)
    232  1.1  chopps 		return;
    233  1.1  chopps 
    234  1.1  chopps 	/* do all that messy console/grf stuff */
    235  1.1  chopps 	if (dp == NULL)
    236  1.1  chopps 		gp = &congrf;
    237  1.1  chopps 	else
    238  1.1  chopps 		gp = (struct grf_softc *)dp;
    239  1.1  chopps 
    240  1.1  chopps 	if (dp != NULL && congrf.g_regkva != 0) {
    241  1.1  chopps 		/*
    242  1.1  chopps 		 * inited earlier, just copy (not device struct)
    243  1.1  chopps 		 */
    244  1.1  chopps 		bcopy(&congrf.g_display, &gp->g_display,
    245  1.1  chopps 		    (char *)&gp[1] - (char *)&gp->g_display);
    246  1.1  chopps 	} else {
    247  1.1  chopps 		gp->g_regkva = (volatile caddr_t)cl_regaddr;
    248  1.1  chopps 		gp->g_fbkva = (volatile caddr_t)cl_fbaddr;
    249  1.1  chopps 
    250  1.1  chopps 		gp->g_unit = GRF_CL5426_UNIT;
    251  1.1  chopps 		gp->g_mode = cl_mode;
    252  1.1  chopps 		gp->g_conpri = grfcl_cnprobe();
    253  1.1  chopps 		gp->g_flags = GF_ALIVE;
    254  1.1  chopps 
    255  1.1  chopps 		/* wakeup the board */
    256  1.1  chopps 		cl_boardinit(gp);
    257  1.1  chopps 
    258  1.1  chopps #ifdef CL5426CONSOLE
    259  1.1  chopps 		grfcl_iteinit(gp);
    260  1.1  chopps 		(void)cl_load_mon(gp, &clconsole_mode);
    261  1.1  chopps #endif
    262  1.1  chopps 
    263  1.1  chopps 	}
    264  1.1  chopps 
    265  1.1  chopps 	/*
    266  1.1  chopps 	 * attach grf (once)
    267  1.1  chopps 	 */
    268  1.1  chopps 	if (amiga_config_found(cfdata, &gp->g_device, gp, grfclprint)) {
    269  1.1  chopps 		attachflag = 1;
    270  1.1  chopps 		printf("grfcl: %dMB ", cl_fbsize/0x100000);
    271  1.1  chopps 		switch (cltype) {
    272  1.1  chopps 		case PICASSO:
    273  1.1  chopps 			printf("Picasso II");
    274  1.1  chopps 			break;
    275  1.1  chopps 		case SPECTRUM:
    276  1.1  chopps 			printf("Spectrum");
    277  1.1  chopps 			break;
    278  1.1  chopps 		case PICCOLO:
    279  1.1  chopps 			printf("Piccolo");
    280  1.1  chopps 			break;
    281  1.1  chopps 		}
    282  1.1  chopps 		printf(" being used\n");
    283  1.1  chopps 	} else {
    284  1.1  chopps 		if (!attachflag)
    285  1.1  chopps 			printf("grfcl unattached!!\n");
    286  1.1  chopps 	}
    287  1.1  chopps }
    288  1.1  chopps 
    289  1.1  chopps int
    290  1.1  chopps grfclprint(auxp, pnp)
    291  1.1  chopps 	void *auxp;
    292  1.1  chopps 	char *pnp;
    293  1.1  chopps {
    294  1.1  chopps 	if (pnp)
    295  1.1  chopps 		printf("ite at %s: ", pnp);
    296  1.1  chopps 	return(UNCONF);
    297  1.1  chopps }
    298  1.1  chopps 
    299  1.1  chopps void
    300  1.1  chopps cl_boardinit(gp)
    301  1.1  chopps struct grf_softc        *gp;
    302  1.1  chopps {
    303  1.1  chopps 	unsigned char *ba = gp->g_regkva;
    304  1.1  chopps 	int x;
    305  1.1  chopps 	void *bah;
    306  1.1  chopps 
    307  1.1  chopps 	/* wakeup board and flip passthru OFF */
    308  1.1  chopps 
    309  1.1  chopps 	RegWakeup(ba);
    310  1.1  chopps 	RegOnpass(ba);
    311  1.1  chopps 
    312  1.1  chopps 	vgaw(ba, 0x46e8, 0x16);
    313  1.1  chopps 	vgaw(ba, 0x102, 1);
    314  1.1  chopps 	vgaw(ba, 0x46e8, 0x0e);
    315  1.1  chopps 	vgaw(ba, 0x3c3, 1);
    316  1.1  chopps 
    317  1.1  chopps 	/* setup initial unchanging parameters */
    318  1.1  chopps 
    319  1.1  chopps 	WSeq(ba, SEQ_ID_CLOCKING_MODE, 0x21);   /* 8 dot - display off */
    320  1.1  chopps 	vgaw(ba, GREG_MISC_OUTPUT_W, 0xe1);     /* mem disable */
    321  1.1  chopps 
    322  1.1  chopps 	WGfx(ba, GCT_ID_OFFSET_1, 0xec);        /* magic cookie */
    323  1.1  chopps 	WSeq(ba, SEQ_ID_UNLOCK_EXT, 0x12);      /* yum! cookies! */
    324  1.1  chopps 
    325  1.1  chopps 	WSeq(ba, SEQ_ID_DRAM_CNTL, 0xb0);
    326  1.1  chopps 	WSeq(ba, SEQ_ID_RESET, 0x03);
    327  1.1  chopps 	WSeq(ba, SEQ_ID_MAP_MASK, 0xff);
    328  1.1  chopps 	WSeq(ba, SEQ_ID_CHAR_MAP_SELECT, 0x00);
    329  1.1  chopps 	WSeq(ba, SEQ_ID_MEMORY_MODE, 0x0e); /* a or 6? */
    330  1.1  chopps 	WSeq(ba, SEQ_ID_EXT_SEQ_MODE, (cltype == PICASSO) ? 0x20 : 0x80);
    331  1.1  chopps 	WSeq(ba, SEQ_ID_EEPROM_CNTL, 0x00);
    332  1.1  chopps 	WSeq(ba, SEQ_ID_PERF_TUNE, 0x0a);
    333  1.1  chopps 	WSeq(ba, SEQ_ID_SIG_CNTL, 0x02);
    334  1.1  chopps 
    335  1.1  chopps 	WSeq(ba, SEQ_ID_MCLK_SELECT, 0x22);
    336  1.1  chopps 
    337  1.1  chopps 	WCrt(ba, CRT_ID_PRESET_ROW_SCAN, 0x00);
    338  1.1  chopps 	WCrt(ba, CRT_ID_CURSOR_START, 0x00);
    339  1.1  chopps 	WCrt(ba, CRT_ID_CURSOR_END, 0x08);
    340  1.1  chopps 	WCrt(ba, CRT_ID_START_ADDR_HIGH, 0x00);
    341  1.1  chopps 	WCrt(ba, CRT_ID_START_ADDR_LOW, 0x00);
    342  1.1  chopps 	WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, 0x00);
    343  1.1  chopps 	WCrt(ba, CRT_ID_CURSOR_LOC_LOW, 0x00);
    344  1.1  chopps 
    345  1.1  chopps 	WCrt(ba, CRT_ID_UNDERLINE_LOC, 0x07);
    346  1.1  chopps 	WCrt(ba, CRT_ID_MODE_CONTROL, 0xa3); /* c3 */
    347  1.1  chopps 	WCrt(ba, CRT_ID_LINE_COMPARE, 0xff); /* ff */
    348  1.1  chopps 	WCrt(ba, CRT_ID_EXT_DISP_CNTL, 0x22);
    349  1.1  chopps 	WSeq(ba, SEQ_ID_CURSOR_STORE, 0x00);
    350  1.1  chopps 
    351  1.1  chopps 	WGfx(ba, GCT_ID_SET_RESET, 0x00);
    352  1.1  chopps 	WGfx(ba, GCT_ID_ENABLE_SET_RESET, 0x00);
    353  1.1  chopps 	WGfx(ba, GCT_ID_DATA_ROTATE, 0x00);
    354  1.1  chopps 	WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x00);
    355  1.1  chopps 	WGfx(ba, GCT_ID_GRAPHICS_MODE, 0x00);
    356  1.1  chopps 	WGfx(ba, GCT_ID_MISC, 0x01);
    357  1.1  chopps 	WGfx(ba, GCT_ID_COLOR_XCARE, 0x0f);
    358  1.1  chopps 	WGfx(ba, GCT_ID_BITMASK, 0xff);
    359  1.1  chopps 	WGfx(ba, GCT_ID_MODE_EXT, 0x28);
    360  1.1  chopps 
    361  1.1  chopps 	for (x=0; x< 0x10; x++)
    362  1.1  chopps 	    WAttr(ba, x, x);
    363  1.1  chopps 	WAttr(ba, ACT_ID_ATTR_MODE_CNTL, 0x01);
    364  1.1  chopps 	WAttr(ba, ACT_ID_OVERSCAN_COLOR, 0x00);
    365  1.1  chopps 	WAttr(ba, ACT_ID_COLOR_PLANE_ENA, 0x0f);
    366  1.1  chopps 	WAttr(ba, ACT_ID_HOR_PEL_PANNING, 0x00);
    367  1.1  chopps 	WAttr(ba, ACT_ID_COLOR_SELECT, 0x00);
    368  1.1  chopps 
    369  1.1  chopps 	delay(200000);
    370  1.1  chopps 	WAttr(ba, 0x34, 0x00);
    371  1.1  chopps 	delay(200000);
    372  1.1  chopps 
    373  1.1  chopps 	vgaw(ba, VDAC_MASK, 0xff);
    374  1.1  chopps 	delay(200000);
    375  1.1  chopps 	vgaw(ba, GREG_MISC_OUTPUT_W, 0xe3); /* c3 */
    376  1.1  chopps 
    377  1.1  chopps 	WGfx(ba, GCT_ID_BLT_STAT_START, 0x40);
    378  1.1  chopps 	WGfx(ba, GCT_ID_BLT_STAT_START, 0x00);
    379  1.1  chopps 
    380  1.1  chopps 	/* colors initially set to greyscale */
    381  1.1  chopps 
    382  1.1  chopps 	vgaw(ba, VDAC_ADDRESS_W, 0);
    383  1.1  chopps 	for (x = 255; x >= 0 ; x--) {
    384  1.1  chopps 		vgaw(ba, VDAC_DATA, x);
    385  1.1  chopps 		vgaw(ba, VDAC_DATA, x);
    386  1.1  chopps 		vgaw(ba, VDAC_DATA, x);
    387  1.1  chopps 	}
    388  1.1  chopps }
    389  1.1  chopps 
    390  1.1  chopps 
    391  1.1  chopps int
    392  1.1  chopps cl_getvmode(gp, vm)
    393  1.1  chopps 	struct grf_softc *gp;
    394  1.1  chopps 	struct grfvideo_mode *vm;
    395  1.1  chopps {
    396  1.1  chopps 	struct grfvideo_mode *gv;
    397  1.1  chopps 
    398  1.1  chopps #ifdef CL5426CONSOLE
    399  1.1  chopps 	/* Handle grabbing console mode */
    400  1.1  chopps 	if (vm->mode_num == 255) {
    401  1.1  chopps 		bcopy(&clconsole_mode, vm, sizeof(struct grfvideo_mode));
    402  1.1  chopps 		/* XXX so grfconfig can tell us the correct text
    403  1.1  chopps 		 * dimensions.
    404  1.1  chopps 		 */
    405  1.1  chopps 		vm->depth = clconsole_mode.fy;
    406  1.1  chopps 		return(0);
    407  1.1  chopps 	}
    408  1.1  chopps #endif
    409  1.1  chopps 	if (vm->mode_num && vm->mode_num > monitor_def_max)
    410  1.1  chopps 		return(EINVAL);
    411  1.1  chopps 
    412  1.1  chopps 	if (!vm->mode_num)
    413  1.1  chopps 		vm->mode_num = (monitor_current - monitor_def) + 1;
    414  1.1  chopps 
    415  1.1  chopps 	gv = monitor_def + (vm->mode_num - 1);
    416  1.1  chopps 	if (gv->mode_num == 0)
    417  1.1  chopps 		return(EINVAL);
    418  1.1  chopps 
    419  1.1  chopps 	bcopy(gv, vm, sizeof(struct grfvideo_mode));
    420  1.1  chopps 	return(0);
    421  1.1  chopps }
    422  1.1  chopps 
    423  1.1  chopps 
    424  1.1  chopps int
    425  1.1  chopps cl_setvmode(gp, mode)
    426  1.1  chopps 	struct grf_softc *gp;
    427  1.1  chopps 	unsigned mode;
    428  1.1  chopps {
    429  1.1  chopps 	struct grfvideo_mode *gv;
    430  1.1  chopps 
    431  1.1  chopps 	if (!mode || (mode > monitor_def_max) ||
    432  1.1  chopps 	    monitor_def[mode-1].mode_num == 0)
    433  1.1  chopps 		return(EINVAL);
    434  1.1  chopps 
    435  1.1  chopps 	monitor_current = monitor_def + (mode - 1);
    436  1.1  chopps 
    437  1.1  chopps 
    438  1.1  chopps 	return(0);
    439  1.1  chopps }
    440  1.1  chopps 
    441  1.1  chopps void
    442  1.1  chopps cl_off(gp)
    443  1.1  chopps struct grf_softc *gp;
    444  1.1  chopps {
    445  1.1  chopps 	char *ba = gp->g_regkva;
    446  1.1  chopps 
    447  1.1  chopps 	/* we'll put the pass-through on for cc ite and set Full Bandwidth
    448  1.1  chopps 	 * bit on just in case it didn't work...but then it doesn't matter
    449  1.1  chopps 	 * does it? =)
    450  1.1  chopps 	 */
    451  1.1  chopps 	RegOnpass(ba);
    452  1.1  chopps 	WSeq(ba, SEQ_ID_CLOCKING_MODE, 0x21);
    453  1.1  chopps }
    454  1.1  chopps 
    455  1.1  chopps /*
    456  1.1  chopps  * Change the mode of the display.
    457  1.1  chopps  * Return a UNIX error number or 0 for success.
    458  1.1  chopps  */
    459  1.1  chopps cl_mode(gp, cmd, arg, a2, a3)
    460  1.1  chopps 	register struct grf_softc *gp;
    461  1.1  chopps 	int cmd;
    462  1.1  chopps 	void *arg;
    463  1.1  chopps 	int a2, a3;
    464  1.1  chopps {
    465  1.1  chopps 	int error;
    466  1.1  chopps 
    467  1.1  chopps 	switch (cmd) {
    468  1.1  chopps 	case GM_GRFON:
    469  1.1  chopps 		error = cl_load_mon (gp,
    470  1.1  chopps 		    (struct grfcltext_mode *) monitor_current) ? 0 : EINVAL;
    471  1.1  chopps 		return(error);
    472  1.1  chopps 
    473  1.1  chopps 	case GM_GRFOFF:
    474  1.1  chopps #ifndef CL5426CONSOLE
    475  1.1  chopps 		cl_off(gp);
    476  1.1  chopps #else
    477  1.1  chopps 		cl_load_mon(gp, &clconsole_mode);
    478  1.1  chopps #endif
    479  1.1  chopps 		return(0);
    480  1.1  chopps 
    481  1.1  chopps 	case GM_GRFCONFIG:
    482  1.1  chopps 		return(0);
    483  1.1  chopps 
    484  1.1  chopps 	case GM_GRFGETVMODE:
    485  1.1  chopps 		return(cl_getvmode (gp, (struct grfvideo_mode *) arg));
    486  1.1  chopps 
    487  1.1  chopps 	case GM_GRFSETVMODE:
    488  1.1  chopps 		error = cl_setvmode (gp, *(unsigned *) arg);
    489  1.1  chopps 		if (!error && (gp->g_flags & GF_GRFON))
    490  1.1  chopps 			cl_load_mon(gp,
    491  1.1  chopps 			    (struct grfcltext_mode *) monitor_current);
    492  1.1  chopps 		return(error);
    493  1.1  chopps 
    494  1.1  chopps 	case GM_GRFGETNUMVM:
    495  1.1  chopps 		*(int *)arg = monitor_def_max;
    496  1.1  chopps 		return(0);
    497  1.1  chopps 
    498  1.1  chopps 	case GM_GRFIOCTL:
    499  1.1  chopps 		return(cl_ioctl (gp, (int) arg, (caddr_t) a2));
    500  1.1  chopps 
    501  1.1  chopps 	default:
    502  1.1  chopps 		break;
    503  1.1  chopps 	}
    504  1.1  chopps 
    505  1.1  chopps 	return(EINVAL);
    506  1.1  chopps }
    507  1.1  chopps 
    508  1.1  chopps int
    509  1.1  chopps cl_ioctl (gp, cmd, data)
    510  1.1  chopps 	register struct grf_softc *gp;
    511  1.1  chopps 	int cmd;
    512  1.1  chopps 	void *data;
    513  1.1  chopps {
    514  1.1  chopps 	switch (cmd) {
    515  1.1  chopps 	case GRFIOCGSPRITEPOS:
    516  1.1  chopps 	case GRFIOCSSPRITEPOS:
    517  1.1  chopps 	case GRFIOCSSPRITEINF:
    518  1.1  chopps 	case GRFIOCGSPRITEINF:
    519  1.1  chopps 	case GRFIOCGSPRITEMAX:
    520  1.1  chopps 		break;
    521  1.1  chopps 
    522  1.1  chopps 	case GRFIOCGETCMAP:
    523  1.1  chopps 		return(cl_getcmap (gp, (struct grf_colormap *) data));
    524  1.1  chopps 
    525  1.1  chopps 	case GRFIOCPUTCMAP:
    526  1.1  chopps 		return(cl_putcmap (gp, (struct grf_colormap *) data));
    527  1.1  chopps 
    528  1.1  chopps 	case GRFIOCBITBLT:
    529  1.1  chopps 		break;
    530  1.1  chopps 
    531  1.1  chopps 	case GRFTOGGLE:
    532  1.1  chopps 		return(cl_toggle (gp,0));
    533  1.1  chopps 
    534  1.1  chopps 	case GRFIOCSETMON:
    535  1.1  chopps 		return(cl_setmonitor (gp, (struct grfvideo_mode *)data));
    536  1.1  chopps 
    537  1.1  chopps 	}
    538  1.1  chopps 	return(EINVAL);
    539  1.1  chopps }
    540  1.1  chopps 
    541  1.1  chopps int
    542  1.1  chopps cl_setmonitor(gp, gv)
    543  1.1  chopps 	struct grf_softc *gp;
    544  1.1  chopps 	struct grfvideo_mode *gv;
    545  1.1  chopps {
    546  1.1  chopps 	struct grfvideo_mode *md;
    547  1.1  chopps         unsigned long maxpix;
    548  1.1  chopps 
    549  1.1  chopps #ifdef CL5426CONSOLE
    550  1.1  chopps 	/* handle interactive setting of console mode */
    551  1.1  chopps 	if (gv->mode_num == 255 && gv->depth == 4) {
    552  1.1  chopps 		bcopy(gv, &clconsole_mode.gv, sizeof(struct grfvideo_mode));
    553  1.1  chopps 		clconsole_mode.rows = gv->disp_height / clconsole_mode.fy;
    554  1.1  chopps 		clconsole_mode.cols = gv->disp_width / clconsole_mode.fx;
    555  1.1  chopps 		if (!(gp->g_flags & GF_GRFON))
    556  1.1  chopps 		    cl_load_mon(gp, &clconsole_mode);
    557  1.1  chopps 		ite_reinit(gp->g_itedev);
    558  1.1  chopps 		return(0);
    559  1.1  chopps 	}
    560  1.1  chopps #endif
    561  1.1  chopps 
    562  1.1  chopps     	/* text mode not valid for other than console */
    563  1.1  chopps     	if (gv->depth == 4)
    564  1.1  chopps     	    	return(EINVAL);
    565  1.1  chopps 
    566  1.1  chopps 	if (!gv->mode_num || gv->mode_num > monitor_def_max)
    567  1.1  chopps 		return(EINVAL);
    568  1.1  chopps 
    569  1.1  chopps 	if ((gv->depth == 24 && (gv->pixel_clock*3) > cl_maxpixelclock) ||
    570  1.1  chopps 	    (gv->pixel_clock > cl_maxpixelclock))
    571  1.1  chopps 		return(EINVAL);
    572  1.1  chopps 
    573  1.1  chopps 	md = monitor_def + (gv->mode_num-1);
    574  1.1  chopps 
    575  1.1  chopps 	bcopy(gv, md, sizeof(struct grfvideo_mode));
    576  1.1  chopps 	return(0);
    577  1.1  chopps }
    578  1.1  chopps 
    579  1.1  chopps int
    580  1.1  chopps cl_getcmap (gfp, cmap)
    581  1.1  chopps 	struct grf_softc *gfp;
    582  1.1  chopps 	struct grf_colormap *cmap;
    583  1.1  chopps {
    584  1.1  chopps 	volatile unsigned char *ba;
    585  1.1  chopps 	u_char red[256], green[256], blue[256], *rp, *gp, *bp;
    586  1.1  chopps 	short x;
    587  1.1  chopps 	int error;
    588  1.1  chopps 
    589  1.1  chopps 	if (cmap->count == 0 || cmap->index >= 256)
    590  1.1  chopps 		return 0;
    591  1.1  chopps 
    592  1.1  chopps 	if (cmap->index + cmap->count > 256)
    593  1.1  chopps 		cmap->count = 256 - cmap->index;
    594  1.1  chopps 
    595  1.1  chopps 	ba = gfp->g_regkva;
    596  1.1  chopps 	/* first read colors out of the chip, then copyout to userspace */
    597  1.1  chopps 	vgaw (ba, VDAC_ADDRESS_W, cmap->index);
    598  1.1  chopps 	x = cmap->count - 1;
    599  1.1  chopps 
    600  1.1  chopps /* Some sort 'o Magic. Spectrum has some changes on the board to speed
    601  1.1  chopps  * up 15 and 16Bit modes. They can access these modes with easy-to-programm
    602  1.1  chopps  * rgbrgbrgb instead of rrrgggbbb. Side effect: when in 8Bit mode, rgb
    603  1.1  chopps  * is swapped to bgr. I wonder if we need to check for 8Bit though, ill
    604  1.1  chopps  */
    605  1.1  chopps 
    606  1.1  chopps 	switch (cltype) {
    607  1.1  chopps 	case SPECTRUM:
    608  1.1  chopps 	case PICCOLO:
    609  1.1  chopps 		rp = blue + cmap->index;
    610  1.1  chopps 		gp = green + cmap->index;
    611  1.1  chopps 		bp = red + cmap->index;
    612  1.1  chopps 		break;
    613  1.1  chopps 	case PICASSO:
    614  1.1  chopps 		rp = red + cmap->index;
    615  1.1  chopps 		gp = green + cmap->index;
    616  1.1  chopps 		bp = blue + cmap->index;
    617  1.1  chopps 		break;
    618  1.1  chopps 	}
    619  1.1  chopps 
    620  1.1  chopps 	do {
    621  1.1  chopps 		*rp++ = vgar (ba, VDAC_DATA) << 2;
    622  1.1  chopps 		*gp++ = vgar (ba, VDAC_DATA) << 2;
    623  1.1  chopps 		*bp++ = vgar (ba, VDAC_DATA) << 2;
    624  1.1  chopps 	} while (x-- > 0);
    625  1.1  chopps 
    626  1.1  chopps 	if (!(error = copyout (red + cmap->index, cmap->red, cmap->count))
    627  1.1  chopps 	    && !(error = copyout (green + cmap->index, cmap->green, cmap->count))
    628  1.1  chopps 	    && !(error = copyout (blue + cmap->index, cmap->blue, cmap->count)))
    629  1.1  chopps 		return(0);
    630  1.1  chopps 
    631  1.1  chopps 	return(error);
    632  1.1  chopps }
    633  1.1  chopps 
    634  1.1  chopps int
    635  1.1  chopps cl_putcmap (gfp, cmap)
    636  1.1  chopps 	struct grf_softc *gfp;
    637  1.1  chopps 	struct grf_colormap *cmap;
    638  1.1  chopps {
    639  1.1  chopps 	volatile unsigned char *ba;
    640  1.1  chopps 	u_char red[256], green[256], blue[256], *rp, *gp, *bp;
    641  1.1  chopps 	short x;
    642  1.1  chopps 	int error;
    643  1.1  chopps 
    644  1.1  chopps 	if (cmap->count == 0 || cmap->index >= 256)
    645  1.1  chopps 		return(0);
    646  1.1  chopps 
    647  1.1  chopps 	if (cmap->index + cmap->count > 256)
    648  1.1  chopps 		cmap->count = 256 - cmap->index;
    649  1.1  chopps 
    650  1.1  chopps 	/* first copy the colors into kernelspace */
    651  1.1  chopps 	if (!(error = copyin (cmap->red, red + cmap->index, cmap->count))
    652  1.1  chopps 	    && !(error = copyin (cmap->green, green + cmap->index, cmap->count))
    653  1.1  chopps 	    && !(error = copyin (cmap->blue, blue + cmap->index, cmap->count))) {
    654  1.1  chopps 		ba = gfp->g_regkva;
    655  1.1  chopps 		vgaw (ba, VDAC_ADDRESS_W, cmap->index);
    656  1.1  chopps 		x = cmap->count - 1;
    657  1.1  chopps 
    658  1.1  chopps 		switch (cltype) {
    659  1.1  chopps 		case SPECTRUM:
    660  1.1  chopps 		case PICCOLO:
    661  1.1  chopps 			rp = blue + cmap->index;
    662  1.1  chopps 			gp = green + cmap->index;
    663  1.1  chopps 			bp = red + cmap->index;
    664  1.1  chopps 			break;
    665  1.1  chopps 		case PICASSO:
    666  1.1  chopps 			rp = red + cmap->index;
    667  1.1  chopps 			gp = green + cmap->index;
    668  1.1  chopps 			bp = blue + cmap->index;
    669  1.1  chopps 			break;
    670  1.1  chopps 		}
    671  1.1  chopps 
    672  1.1  chopps 		do {
    673  1.1  chopps 			vgaw (ba, VDAC_DATA, *rp++ >> 2);
    674  1.1  chopps 			vgaw (ba, VDAC_DATA, *gp++ >> 2);
    675  1.1  chopps 			vgaw (ba, VDAC_DATA, *bp++ >> 2);
    676  1.1  chopps 		} while (x-- > 0);
    677  1.1  chopps 		return(0);
    678  1.1  chopps 	}
    679  1.1  chopps 	else
    680  1.1  chopps 		return(error);
    681  1.1  chopps }
    682  1.1  chopps 
    683  1.1  chopps 
    684  1.1  chopps int
    685  1.1  chopps cl_toggle(gp,wopp)
    686  1.1  chopps 	struct grf_softc *gp;
    687  1.1  chopps 	unsigned short wopp; /* don't need that one yet, ill */
    688  1.1  chopps {
    689  1.1  chopps 	volatile unsigned char *ba;
    690  1.1  chopps 
    691  1.1  chopps 	ba = gp->g_regkva;
    692  1.1  chopps 
    693  1.1  chopps 	if (pass_toggle) {
    694  1.1  chopps 		RegOffpass(ba);
    695  1.1  chopps 	} else {
    696  1.1  chopps 		/* This was in the original.. is it needed? */
    697  1.1  chopps 		if (cltype == PICASSO || cltype == PICCOLO)
    698  1.1  chopps 			RegWakeup(ba);
    699  1.1  chopps 		RegOnpass(ba);
    700  1.1  chopps 	}
    701  1.1  chopps 	return(0);
    702  1.1  chopps }
    703  1.1  chopps 
    704  1.1  chopps static void
    705  1.1  chopps CompFQ(fq,num,denom)
    706  1.1  chopps u_int fq;
    707  1.1  chopps u_char *num;
    708  1.1  chopps u_char *denom;
    709  1.1  chopps {
    710  1.1  chopps #define OSC     14318180
    711  1.1  chopps /* OK, here's what we're doing here:
    712  1.1  chopps  *
    713  1.1  chopps  *             OSC * NUMERATOR
    714  1.1  chopps  *      VCLK = -------------------  Hz
    715  1.1  chopps  *             DENOMINATOR * (1+P)
    716  1.1  chopps  *
    717  1.1  chopps  * so we're given VCLK and we should give out some useful
    718  1.1  chopps  * values....
    719  1.1  chopps  *
    720  1.1  chopps  * NUMERATOR is 6 bits wide
    721  1.1  chopps  * DENOMINATOR is 5 bits wide with bit P in the same char as bit 0.
    722  1.1  chopps  *
    723  1.1  chopps  * We run through all the possible combinations and
    724  1.1  chopps  * return the values which deviate the least from the chosen frequency.
    725  1.1  chopps  *
    726  1.1  chopps  */
    727  1.1  chopps #define OSC     14318180
    728  1.1  chopps #define count(n,d,p)    ((OSC * n)/(d * (1+p)))
    729  1.1  chopps 
    730  1.1  chopps 	unsigned char n, d, p, minn, mind, minp;
    731  1.1  chopps 	unsigned long err, minerr;
    732  1.1  chopps 
    733  1.1  chopps /*
    734  1.1  chopps numer = 0x00 - 0x7f (7 bit!)
    735  1.1  chopps denom = 0x00 - 0x1f (1) 0x20 - 0x3e (even)
    736  1.1  chopps */
    737  1.1  chopps 
    738  1.1  chopps 	/* find lowest error in 6144 iterations.
    739  1.1  chopps 	 */
    740  1.1  chopps 	minerr = fq;
    741  1.1  chopps 	minn = 0;
    742  1.1  chopps 	mind = 0;
    743  1.1  chopps 	p = 0;
    744  1.1  chopps 
    745  1.1  chopps 	for (d=1; d < 0x20; d++) {
    746  1.1  chopps 		for (n=1; n < 0x80; n++) {
    747  1.1  chopps 			err = abs(count(n, d, p) - fq);
    748  1.1  chopps 			if (err < minerr) {
    749  1.1  chopps 				minerr = err;
    750  1.1  chopps 				minn = n;
    751  1.1  chopps 				mind = d;
    752  1.1  chopps 				minp = p;
    753  1.1  chopps 			}
    754  1.1  chopps 		}
    755  1.1  chopps 		if (d == 0x1f && p == 0) {
    756  1.1  chopps 			p = 1;
    757  1.1  chopps 			d = 0x0f;
    758  1.1  chopps 		}
    759  1.1  chopps 	}
    760  1.1  chopps 
    761  1.1  chopps 	*num = minn;
    762  1.1  chopps 	*denom = (mind << 1) | minp;
    763  1.1  chopps 	if (minerr > 500000)
    764  1.1  chopps 	    printf("Warning: CompFQ minimum error = %d\n", minerr);
    765  1.1  chopps 	return;
    766  1.1  chopps }
    767  1.1  chopps 
    768  1.1  chopps int
    769  1.1  chopps cl_mondefok(mdp)
    770  1.1  chopps 	struct grfvideo_mode *mdp;
    771  1.1  chopps {
    772  1.1  chopps 	if (mdp->mode_num == 0 || (mdp->depth == 24 ?
    773  1.1  chopps             mdp->pixel_clock * 3 : mdp->pixel_clock) > cl_maxpixelclock)
    774  1.1  chopps 		return(0);
    775  1.1  chopps 
    776  1.1  chopps 	switch(mdp->depth) {
    777  1.1  chopps 	case 1:
    778  1.1  chopps 	case 4:
    779  1.1  chopps 	case 8:
    780  1.1  chopps 	case 15:
    781  1.1  chopps 	case 16:
    782  1.1  chopps 	case 24:
    783  1.1  chopps 		return(1);
    784  1.1  chopps 	default:
    785  1.1  chopps 		return(0);
    786  1.1  chopps 	}
    787  1.1  chopps }
    788  1.1  chopps 
    789  1.1  chopps int
    790  1.1  chopps cl_load_mon(gp, md)
    791  1.1  chopps 	struct grf_softc *gp;
    792  1.1  chopps 	struct grfcltext_mode *md;
    793  1.1  chopps {
    794  1.1  chopps 	struct grfvideo_mode *gv;
    795  1.1  chopps 	struct grfinfo *gi;
    796  1.1  chopps 	volatile unsigned char *ba;
    797  1.1  chopps 	volatile unsigned char *fb;
    798  1.1  chopps 	unsigned char num0,denom0;
    799  1.1  chopps 	unsigned short HT,HDE,HBS,HBE,HSS,HSE,VDE,VBS,VBE,VSS,VSE,VT;
    800  1.1  chopps 	char LACE, DBLSCAN, TEXT;
    801  1.1  chopps 	unsigned short clkdiv, clksel;
    802  1.1  chopps 	int uplim, lowlim;
    803  1.1  chopps 
    804  1.1  chopps 	/* identity */
    805  1.1  chopps 	gv = &md->gv;
    806  1.1  chopps 	TEXT = (gv->depth == 4);
    807  1.1  chopps 
    808  1.1  chopps 	if (!cl_mondefok(gv)) {
    809  1.1  chopps 		printf("mondef not ok\n");
    810  1.1  chopps 		return(0);
    811  1.1  chopps 	}
    812  1.1  chopps 
    813  1.1  chopps 	ba = gp->g_regkva;
    814  1.1  chopps 	fb = gp->g_fbkva;
    815  1.1  chopps 
    816  1.1  chopps 	/* provide all needed information in grf device-independant
    817  1.1  chopps 	 * locations
    818  1.1  chopps 	 */
    819  1.1  chopps 	gp->g_data              = (caddr_t) gv;
    820  1.1  chopps 	gi = &gp->g_display;
    821  1.1  chopps 	gi->gd_regaddr          = (caddr_t) ztwopa (ba);
    822  1.1  chopps 	gi->gd_regsize          = 64*1024;
    823  1.1  chopps 	gi->gd_fbaddr           = (caddr_t) kvtop (fb);
    824  1.1  chopps 	gi->gd_fbsize           = cl_fbsize;
    825  1.1  chopps 	gi->gd_colors           = 1 << gv->depth;
    826  1.1  chopps 	gi->gd_planes           = gv->depth;
    827  1.1  chopps 	gi->gd_fbwidth          = gv->disp_width;
    828  1.1  chopps 	gi->gd_fbheight         = gv->disp_height;
    829  1.1  chopps 	gi->gd_fbx              = 0;
    830  1.1  chopps 	gi->gd_fby              = 0;
    831  1.1  chopps 	if (TEXT) {
    832  1.1  chopps 		gi->gd_dwidth   = md->fx * md->cols;
    833  1.1  chopps 		gi->gd_dheight  = md->fy * md->rows;
    834  1.1  chopps 	} else {
    835  1.1  chopps 		gi->gd_dwidth   = gv->disp_width;
    836  1.1  chopps 		gi->gd_dheight  = gv->disp_height;
    837  1.1  chopps 	}
    838  1.1  chopps 	gi->gd_dx               = 0;
    839  1.1  chopps 	gi->gd_dy               = 0;
    840  1.1  chopps 
    841  1.1  chopps 	/* get display mode parameters */
    842  1.1  chopps 
    843  1.1  chopps 	HBS = gv->hblank_start;
    844  1.1  chopps 	HBE = gv->hblank_stop;
    845  1.1  chopps 	HSS = gv->hsync_start;
    846  1.1  chopps 	HSE = gv->hsync_stop;
    847  1.1  chopps 	HT  = gv->htotal;
    848  1.1  chopps 	VBS = gv->vblank_start;
    849  1.1  chopps 	VSS = gv->vsync_start;
    850  1.1  chopps 	VSE = gv->vsync_stop;
    851  1.1  chopps 	VBE = gv->vblank_stop;
    852  1.1  chopps 	VT  = gv->vtotal;
    853  1.1  chopps 
    854  1.1  chopps 	if (TEXT)
    855  1.1  chopps 		HDE = ((gv->disp_width+md->fx-1)/md->fx) - 1;
    856  1.1  chopps 	else
    857  1.1  chopps 		HDE = (gv->disp_width+3)/8 - 1; /*HBS;*/
    858  1.1  chopps 	VDE = gv->disp_height-1;
    859  1.1  chopps 
    860  1.1  chopps 	/* figure out whether lace or dblscan is needed */
    861  1.1  chopps 
    862  1.1  chopps 	uplim = gv->disp_height + (gv->disp_height / 4);
    863  1.1  chopps 	lowlim = gv->disp_height - (gv->disp_height / 4);
    864  1.1  chopps 	LACE = (((VT*2) > lowlim) && ((VT*2) < uplim)) ? 1 : 0;
    865  1.1  chopps 	DBLSCAN = (((VT/2) > lowlim) && ((VT/2) < uplim)) ? 1 : 0;
    866  1.1  chopps 
    867  1.1  chopps 	/* adjustments */
    868  1.1  chopps 
    869  1.1  chopps 	if (LACE)
    870  1.1  chopps 		VDE /= 2;
    871  1.1  chopps 
    872  1.1  chopps 	WSeq(ba, SEQ_ID_MEMORY_MODE, (TEXT || (gv->depth == 1)) ? 0x06 : 0x0e);
    873  1.1  chopps 	WSeq(ba, SEQ_ID_DRAM_CNTL, (TEXT || (gv->depth == 1)) ? 0x90 : 0xb0);
    874  1.1  chopps 	WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x00);
    875  1.1  chopps 	WSeq(ba, SEQ_ID_MAP_MASK, (gv->depth == 1) ? 0x01 : 0xff);
    876  1.1  chopps 	WSeq(ba, SEQ_ID_CHAR_MAP_SELECT, 0x00);
    877  1.1  chopps 
    878  1.1  chopps 	/* Set clock */
    879  1.1  chopps 
    880  1.1  chopps 	CompFQ((gv->depth == 24) ? gv->pixel_clock*3 : gv->pixel_clock,
    881  1.1  chopps 	    &num0, &denom0);
    882  1.1  chopps 	WSeq(ba, SEQ_ID_VCLK_0_NUM, num0);
    883  1.1  chopps 	WSeq(ba, SEQ_ID_VCLK_0_DENOM, denom0);
    884  1.1  chopps 
    885  1.1  chopps 	/* load display parameters into board */
    886  1.1  chopps 
    887  1.1  chopps 	WCrt(ba, CRT_ID_HOR_TOTAL, HT);
    888  1.1  chopps 	WCrt(ba, CRT_ID_HOR_DISP_ENA_END, ((HDE >= HBS) ? HBS-1 : HDE));
    889  1.1  chopps 	WCrt(ba, CRT_ID_START_HOR_BLANK, HBS);
    890  1.1  chopps 	WCrt(ba, CRT_ID_END_HOR_BLANK, (HBE & 0x1f) | 0x80); /* | 0x80? */
    891  1.1  chopps 	WCrt(ba, CRT_ID_START_HOR_RETR, HSS);
    892  1.1  chopps 	WCrt(ba, CRT_ID_END_HOR_RETR,
    893  1.1  chopps 	    (HSE & 0x1f) |
    894  1.1  chopps 	    ((HBE & 0x20) ? 0x80 : 0x00) );
    895  1.1  chopps 	WCrt(ba, CRT_ID_VER_TOTAL, VT);
    896  1.1  chopps 	WCrt(ba, CRT_ID_OVERFLOW,
    897  1.1  chopps 	    0x10 |
    898  1.1  chopps 	    ((VT  & 0x100) ? 0x01 : 0x00) |
    899  1.1  chopps 	    ((VDE & 0x100) ? 0x02 : 0x00) |
    900  1.1  chopps 	    ((VSS & 0x100) ? 0x04 : 0x00) |
    901  1.1  chopps 	    ((VBS & 0x100) ? 0x08 : 0x00) |
    902  1.1  chopps 	    ((VT  & 0x200) ? 0x20 : 0x00) |
    903  1.1  chopps 	    ((VDE & 0x200) ? 0x40 : 0x00) |
    904  1.1  chopps 	    ((VSS & 0x200) ? 0x80 : 0x00) );
    905  1.1  chopps 
    906  1.1  chopps 
    907  1.1  chopps 	WCrt(ba, CRT_ID_CHAR_HEIGHT,
    908  1.1  chopps 	    0x40 |  /* TEXT ? 0x00 ??? */
    909  1.1  chopps 	    (DBLSCAN ? 0x80 : 0x00) |
    910  1.1  chopps 	    ((VBS & 0x200) ? 0x20 : 0x00) |
    911  1.1  chopps 	    (TEXT ? ((md->fy-1) & 0x1f) : 0x00));
    912  1.1  chopps 	WCrt(ba, CRT_ID_MODE_CONTROL,
    913  1.1  chopps 	    ((TEXT || (gv->depth == 1)) ? 0xc3 : 0xa3));
    914  1.1  chopps 
    915  1.1  chopps 	/* text cursor */
    916  1.1  chopps 
    917  1.1  chopps 	if (TEXT) {
    918  1.1  chopps #if 0
    919  1.1  chopps 		WCrt(ba, CRT_ID_CURSOR_START, (md->fy & 0x1f) - 2);
    920  1.1  chopps 		WCrt(ba, CRT_ID_CURSOR_END, (md->fy & 0x1f) - 1);
    921  1.1  chopps #else
    922  1.1  chopps 		WCrt(ba, CRT_ID_CURSOR_START, 0x00);
    923  1.1  chopps 		WCrt(ba, CRT_ID_CURSOR_END, md->fy & 0x1f);
    924  1.1  chopps #endif
    925  1.1  chopps 		WCrt(ba, CRT_ID_UNDERLINE_LOC, md->fy-1 & 0x1f);
    926  1.1  chopps 
    927  1.1  chopps 		WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, 0x00);
    928  1.1  chopps 		WCrt(ba, CRT_ID_CURSOR_LOC_LOW, 0x00);
    929  1.1  chopps 	}
    930  1.1  chopps 
    931  1.1  chopps 	WCrt(ba, CRT_ID_START_ADDR_HIGH, 0x00);
    932  1.1  chopps 	WCrt(ba, CRT_ID_START_ADDR_LOW, 0x00);
    933  1.1  chopps 
    934  1.1  chopps 	WCrt(ba, CRT_ID_START_VER_RETR, VSS);
    935  1.1  chopps 	WCrt(ba, CRT_ID_END_VER_RETR, (VSE & 0x0f) | 0x30);
    936  1.1  chopps 	WCrt(ba, CRT_ID_VER_DISP_ENA_END, VDE);
    937  1.1  chopps 	WCrt(ba, CRT_ID_START_VER_BLANK, VBS);
    938  1.1  chopps 	WCrt(ba, CRT_ID_END_VER_BLANK, VBE);
    939  1.1  chopps 
    940  1.1  chopps 	WCrt(ba, CRT_ID_LINE_COMPARE, 0xff);
    941  1.1  chopps 	WCrt(ba, CRT_ID_LACE_END, HT/2); /* MW/16 */
    942  1.1  chopps 	WCrt(ba, CRT_ID_LACE_CNTL,
    943  1.1  chopps 	    (LACE ? 0x01 : 0x00) |
    944  1.1  chopps 	    ((HBE & 0x40) ? 0x10 : 0x00) |
    945  1.1  chopps 	    ((HBE & 0x80) ? 0x20 : 0x00) |
    946  1.1  chopps 	    ((VBE & 0x100) ? 0x40 : 0x00) |
    947  1.1  chopps 	    ((VBE & 0x200) ? 0x80 : 0x00) );
    948  1.1  chopps 
    949  1.1  chopps 	/* depth dependent stuff */
    950  1.1  chopps 
    951  1.1  chopps 	switch (gv->depth) {
    952  1.1  chopps 	case 1:
    953  1.1  chopps 	case 4:
    954  1.1  chopps 	case 8:
    955  1.1  chopps 	    clkdiv = 0;
    956  1.1  chopps 	    break;
    957  1.1  chopps 	case 15:
    958  1.1  chopps 	case 16:
    959  1.1  chopps 	    clkdiv = 3;
    960  1.1  chopps 	    break;
    961  1.1  chopps 	case 24:
    962  1.1  chopps 	    clkdiv = 2;
    963  1.1  chopps 	    break;
    964  1.1  chopps 	}
    965  1.1  chopps 
    966  1.1  chopps 	WGfx(ba, GCT_ID_GRAPHICS_MODE,
    967  1.1  chopps 	    ((TEXT || (gv->depth == 1)) ? 0x00 : 0x40));
    968  1.1  chopps 	WGfx(ba, GCT_ID_MISC, (TEXT ? 0x04 : 0x01));
    969  1.1  chopps 
    970  1.1  chopps 	WSeq(ba, SEQ_ID_EXT_SEQ_MODE,
    971  1.1  chopps 	    ((TEXT || (gv->depth == 1)) ? 0x00 : 0x01) |
    972  1.1  chopps 	    ((cltype == PICASSO) ? 0x20: 0x80) |
    973  1.1  chopps 	    (clkdiv << 1) );
    974  1.1  chopps 
    975  1.1  chopps         delay(200000);
    976  1.1  chopps 	vgaw(ba, VDAC_MASK, 0xff);
    977  1.1  chopps 	delay(200000);
    978  1.1  chopps 	vgar(ba, VDAC_MASK);
    979  1.1  chopps 	delay(200000);
    980  1.1  chopps 	vgar(ba, VDAC_MASK);
    981  1.1  chopps 	delay(200000);
    982  1.1  chopps 	vgar(ba, VDAC_MASK);
    983  1.1  chopps 	delay(200000);
    984  1.1  chopps 	vgar(ba, VDAC_MASK);
    985  1.1  chopps 	delay(200000);
    986  1.1  chopps 	switch (gv->depth) {
    987  1.1  chopps 	case 1:
    988  1.1  chopps 	case 4: /* text */
    989  1.1  chopps 		vgaw(ba, VDAC_MASK, 0);
    990  1.1  chopps 		HDE = gv->disp_width / 16;
    991  1.1  chopps 		break;
    992  1.1  chopps 	case 8:
    993  1.1  chopps 		vgaw(ba, VDAC_MASK, 0);
    994  1.1  chopps 		HDE = gv->disp_width / 8;
    995  1.1  chopps 		break;
    996  1.1  chopps 	case 15:
    997  1.1  chopps 		vgaw(ba, VDAC_MASK, 0xd0);
    998  1.1  chopps 		HDE = gv->disp_width / 4;
    999  1.1  chopps 		break;
   1000  1.1  chopps 	case 16:
   1001  1.1  chopps 		vgaw(ba, VDAC_MASK, 0xc1);
   1002  1.1  chopps 		HDE = gv->disp_width / 4;
   1003  1.1  chopps 		break;
   1004  1.1  chopps 	case 24:
   1005  1.1  chopps 		vgaw(ba, VDAC_MASK, 0xc5);
   1006  1.1  chopps 		HDE = (gv->disp_width / 8) * 3;
   1007  1.1  chopps 		break;
   1008  1.1  chopps 	}
   1009  1.1  chopps 	delay(200000);
   1010  1.1  chopps 
   1011  1.1  chopps 	WCrt(ba, CRT_ID_OFFSET,  HDE);
   1012  1.1  chopps 	WCrt(ba, CRT_ID_EXT_DISP_CNTL,
   1013  1.1  chopps 	    ((TEXT && gv->pixel_clock > 29000000) ? 0x40 : 0x00) |
   1014  1.1  chopps 	    0x22  |
   1015  1.1  chopps 	    ((HDE > 0xff) ? 0x10 : 0x00)); /* text? */
   1016  1.1  chopps 
   1017  1.1  chopps 	delay(200000);
   1018  1.1  chopps 	WAttr(ba, ACT_ID_ATTR_MODE_CNTL, (TEXT ? 0x0a : 0x01));
   1019  1.1  chopps 	delay(200000);
   1020  1.1  chopps 	WAttr(ba, 0x20 | ACT_ID_COLOR_PLANE_ENA,
   1021  1.1  chopps 	    (gv->depth == 1) ? 0x01 : 0x0f);
   1022  1.1  chopps 	delay(200000);
   1023  1.1  chopps 
   1024  1.1  chopps 	/* text initialization */
   1025  1.1  chopps 
   1026  1.1  chopps 	if (TEXT) {
   1027  1.1  chopps 		cl_inittextmode(gp);
   1028  1.1  chopps 	}
   1029  1.1  chopps 
   1030  1.1  chopps 	WSeq(ba, SEQ_ID_CURSOR_ATTR, 0x14);
   1031  1.1  chopps 	WSeq(ba, SEQ_ID_CLOCKING_MODE, 0x01);
   1032  1.1  chopps 
   1033  1.1  chopps 	/* Pass-through */
   1034  1.1  chopps 
   1035  1.1  chopps 	RegOffpass(ba);
   1036  1.1  chopps 
   1037  1.1  chopps 	return(1);
   1038  1.1  chopps }
   1039  1.1  chopps 
   1040  1.1  chopps void
   1041  1.1  chopps cl_inittextmode(gp)
   1042  1.1  chopps 	struct grf_softc *gp;
   1043  1.1  chopps {
   1044  1.1  chopps 	struct grfcltext_mode *tm = (struct grfcltext_mode *)gp->g_data;
   1045  1.1  chopps 	volatile unsigned char *ba = gp->g_regkva;
   1046  1.1  chopps 	unsigned char *fb = gp->g_fbkva;
   1047  1.1  chopps 	unsigned char *c, *f, y;
   1048  1.1  chopps 	unsigned short z;
   1049  1.1  chopps 
   1050  1.1  chopps 
   1051  1.1  chopps 	/* load text font into beginning of display memory.
   1052  1.1  chopps 	 * Each character cell is 32 bytes long (enough for
   1053  1.1  chopps 	 * 4 planes)
   1054  1.1  chopps 	 */
   1055  1.1  chopps 
   1056  1.1  chopps 	SetTextPlane(ba, 0x02);
   1057  1.1  chopps 	c = (unsigned char *)(fb);
   1058  1.1  chopps 	f = tm->fdata;
   1059  1.1  chopps 	for (z = 0; z < tm->fdstart; z++, c+=(32-tm->fy))
   1060  1.1  chopps 		for (y=0; y < tm->fy; y++)
   1061  1.1  chopps 			*c++ = 0;
   1062  1.1  chopps 	for (; z <= tm->fdend; z++, c+=(32-tm->fy))
   1063  1.1  chopps 		for (y=0; y < tm->fy; y++)
   1064  1.1  chopps 			*c++ = *f++;
   1065  1.1  chopps 	for (; z < 256; z++, c+=(32-tm->fy))
   1066  1.1  chopps 		for (y=0; y < tm->fy; y++)
   1067  1.1  chopps 			*c++ = 0;
   1068  1.1  chopps 
   1069  1.1  chopps 	/* clear out text/attr planes (three screens worth) */
   1070  1.1  chopps 
   1071  1.1  chopps 	SetTextPlane(ba, 0x01);
   1072  1.1  chopps 	memset(fb, 0x07, tm->cols*tm->rows*3);
   1073  1.1  chopps 	SetTextPlane(ba, 0x00);
   1074  1.1  chopps 	memset(fb, 0x20, tm->cols*tm->rows*3);
   1075  1.1  chopps 
   1076  1.1  chopps 	/* print out a little init msg */
   1077  1.1  chopps 
   1078  1.1  chopps 	c = (unsigned char *)(fb) + (tm->cols-16);
   1079  1.1  chopps 	strcpy(c, "CIRRUS");
   1080  1.1  chopps 	c[6] = 0x20;
   1081  1.1  chopps 
   1082  1.1  chopps 	/* set colors (B&W) */
   1083  1.1  chopps 
   1084  1.1  chopps 
   1085  1.1  chopps 	vgaw(ba, VDAC_ADDRESS_W, 0);
   1086  1.1  chopps 	for (z=0; z<256; z++) {
   1087  1.1  chopps 		unsigned char r, g, b;
   1088  1.1  chopps 
   1089  1.1  chopps 		y = (z & 1) ? ((z > 7) ? 2 : 1) : 0;
   1090  1.1  chopps 
   1091  1.1  chopps 		if (cltype == PICASSO) {
   1092  1.1  chopps 			r = clconscolors[y][0];
   1093  1.1  chopps 			g = clconscolors[y][1];
   1094  1.1  chopps 			b = clconscolors[y][2];
   1095  1.1  chopps 		} else {
   1096  1.1  chopps 			b = clconscolors[y][0];
   1097  1.1  chopps 			g = clconscolors[y][1];
   1098  1.1  chopps 			r = clconscolors[y][2];
   1099  1.1  chopps 		}
   1100  1.1  chopps 		vgaw(ba, VDAC_DATA, r >> 2);
   1101  1.1  chopps 		vgaw(ba, VDAC_DATA, g >> 2);
   1102  1.1  chopps 		vgaw(ba, VDAC_DATA, b >> 2);
   1103  1.1  chopps 	}
   1104  1.1  chopps }
   1105  1.1  chopps 
   1106  1.1  chopps void
   1107  1.1  chopps memset(d, c, l)
   1108  1.1  chopps 	unsigned char *d;
   1109  1.1  chopps 	unsigned char c;
   1110  1.1  chopps 	int l;
   1111  1.1  chopps {
   1112  1.1  chopps 	for(; l>0; l--)
   1113  1.1  chopps 		*d++ = c;
   1114  1.1  chopps }
   1115  1.1  chopps 
   1116  1.1  chopps #endif  /* GRF_CL5426 */
   1117  1.1  chopps 
   1118