Home | History | Annotate | Line # | Download | only in dev
grfabs_et.c revision 1.6
      1  1.6       leo /*	$NetBSD: grfabs_et.c,v 1.6 1997/01/10 20:59:27 leo Exp $	*/
      2  1.1       leo 
      3  1.1       leo /*
      4  1.1       leo  * Copyright (c) 1996 Leo Weppelman.
      5  1.1       leo  * All rights reserved.
      6  1.1       leo  *
      7  1.1       leo  * Redistribution and use in source and binary forms, with or without
      8  1.1       leo  * modification, are permitted provided that the following conditions
      9  1.1       leo  * are met:
     10  1.1       leo  * 1. Redistributions of source code must retain the above copyright
     11  1.1       leo  *    notice, this list of conditions and the following disclaimer.
     12  1.1       leo  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       leo  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       leo  *    documentation and/or other materials provided with the distribution.
     15  1.1       leo  * 3. All advertising materials mentioning features or use of this software
     16  1.1       leo  *    must display the following acknowledgement:
     17  1.1       leo  *      This product includes software developed by Leo Weppelman.
     18  1.1       leo  * 4. The name of the author may not be used to endorse or promote products
     19  1.1       leo  *    derived from this software without specific prior written permission
     20  1.1       leo  *
     21  1.1       leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1       leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1       leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1       leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1       leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1       leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1       leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1       leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1       leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1       leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1       leo  */
     32  1.1       leo 
     33  1.1       leo /*
     34  1.1       leo  * Most of the lower-level et4000 stuff was derived from:
     35  1.1       leo  *	.../amiga/dev/grf_et.c
     36  1.1       leo  *
     37  1.1       leo  * Which was copyrighted by:
     38  1.1       leo  *	Copyright (c) 1996 Tobias Abt
     39  1.1       leo  *	Copyright (c) 1995 Ezra Story
     40  1.1       leo  *	Copyright (c) 1995 Kari Mettinen
     41  1.1       leo  *	Copyright (c) 1994 Markus Wild
     42  1.1       leo  *	Copyright (c) 1994 Lutz Vieweg
     43  1.1       leo  *
     44  1.1       leo  * Thanks guys!
     45  1.1       leo  *
     46  1.1       leo  */
     47  1.1       leo #include <sys/param.h>
     48  1.1       leo #include <sys/queue.h>
     49  1.1       leo #include <sys/malloc.h>
     50  1.1       leo #include <sys/device.h>
     51  1.1       leo #include <sys/systm.h>
     52  1.1       leo 
     53  1.1       leo /*
     54  1.1       leo  * For PCI probing...
     55  1.1       leo  */
     56  1.1       leo #include <dev/pci/pcireg.h>
     57  1.1       leo #include <dev/pci/pcivar.h>
     58  1.1       leo 
     59  1.1       leo #include <machine/iomap.h>
     60  1.1       leo #include <machine/video.h>
     61  1.1       leo #include <machine/mfp.h>
     62  1.6       leo #include <machine/cpu.h>
     63  1.1       leo #include <atari/atari/device.h>
     64  1.1       leo #include <atari/dev/grfioctl.h>
     65  1.1       leo #include <atari/dev/grfabs_reg.h>
     66  1.3       leo #include <atari/dev/grfabs_et.h>
     67  1.1       leo #include <atari/dev/grf_etreg.h>
     68  1.1       leo 
     69  1.3       leo #define	SAVEBUF_SIZE	(32*1024 + sizeof(save_area_t))
     70  1.1       leo 
     71  1.1       leo /*
     72  1.1       leo  * Function decls
     73  1.1       leo  */
     74  1.1       leo static void       init_view __P((view_t *, bmap_t *, dmode_t *, box_t *));
     75  1.1       leo static colormap_t *alloc_colormap __P((dmode_t *));
     76  1.1       leo static void	  et_display_view __P((view_t *));
     77  1.1       leo static view_t	  *et_alloc_view __P((dmode_t *, dimen_t *, u_char));
     78  1.1       leo static void	  et_boardinit __P((void));
     79  1.1       leo static void	  et_free_view __P((view_t *));
     80  1.3       leo static void	  et_loadmode __P((struct grfvideo_mode *, et_sv_reg_t *));
     81  1.1       leo static void	  et_remove_view __P((view_t *));
     82  1.1       leo static void	  et_save_view __P((view_t *));
     83  1.1       leo static int	  et_use_colormap __P((view_t *, colormap_t *));
     84  1.1       leo 
     85  1.1       leo /*
     86  1.1       leo  * Our function switch table
     87  1.1       leo  */
     88  1.1       leo struct grfabs_sw et_vid_sw = {
     89  1.1       leo 	et_display_view,
     90  1.1       leo 	et_alloc_view,
     91  1.1       leo 	et_free_view,
     92  1.1       leo 	et_remove_view,
     93  1.1       leo 	et_save_view,
     94  1.1       leo 	et_use_colormap
     95  1.1       leo };
     96  1.1       leo 
     97  1.1       leo static struct grfvideo_mode hw_modes[] = {
     98  1.1       leo     {
     99  1.1       leo 	0, "", 25175000,		/* num, descr, pix-clock	*/
    100  1.3       leo 	640, 400, 4,			/* width, height, depth		*/
    101  1.5       leo 	632/8, 672/8, 688/8, 808/8, 768/8,/* HBS, HBE, HSS, HSE, HT	*/
    102  1.3       leo 	399, 450, 408, 413, 449		/* VBS, VBE, VSS, VSE, VT	*/
    103  1.3       leo     },
    104  1.1       leo     {
    105  1.1       leo 	0, "", 25175000,		/* num, descr, pix-clock	*/
    106  1.3       leo 	640, 480, 4,			/* width, height, depth		*/
    107  1.5       leo 	632/8, 672/8, 688/8, 752/8, 752/8,/* HBS, HBE, HSS, HSE, HT	*/
    108  1.3       leo 	481, 522, 490, 498, 522		/* VBS, VBE, VSS, VSE, VT	*/
    109  1.1       leo     }
    110  1.1       leo };
    111  1.1       leo 
    112  1.1       leo static dmode_t vid_modes[] = {
    113  1.1       leo     { { NULL, NULL },
    114  1.3       leo 	"640x400", { 640, 400 }, 1, (void*)&hw_modes[0], &et_vid_sw },
    115  1.3       leo     { { NULL, NULL },
    116  1.3       leo 	"640x480", { 640, 480 }, 1, (void*)&hw_modes[1], &et_vid_sw },
    117  1.1       leo     { { NULL, NULL }, NULL,  }
    118  1.1       leo };
    119  1.1       leo 
    120  1.1       leo #define	ET_NUMCLOCKS	32
    121  1.1       leo 
    122  1.1       leo static u_int et_clockfreqs[ET_NUMCLOCKS] = {
    123  1.1       leo 	 6293750,  7080500,  7875000,  8125000,
    124  1.1       leo 	 9000000,  9375000, 10000000, 11225000,
    125  1.1       leo 	12587500, 14161000, 15750000, 16250000,
    126  1.1       leo 	18000000, 18750000, 20000000, 22450000,
    127  1.1       leo 	25175000, 28322000, 31500000, 32500000,
    128  1.1       leo 	36000000, 37500000, 40000000, 44900000,
    129  1.1       leo 	50350000, 56644000, 63000000, 65000000,
    130  1.1       leo 	72000000, 75000000, 80000000, 89800000
    131  1.1       leo };
    132  1.1       leo 
    133  1.1       leo static bmap_t	con_bm; /* XXX */
    134  1.1       leo 
    135  1.1       leo struct grfabs_et_priv {
    136  1.1       leo 	pcitag_t		pci_tag;
    137  1.1       leo 	volatile caddr_t	regkva;
    138  1.1       leo 	volatile caddr_t	memkva;
    139  1.1       leo 	int			regsz;
    140  1.1       leo 	int			memsz;
    141  1.1       leo } et_priv;
    142  1.1       leo 
    143  1.1       leo /*
    144  1.1       leo  * XXX: called from ite console init routine.
    145  1.1       leo  * Initialize list of posible video modes.
    146  1.1       leo  */
    147  1.1       leo void
    148  1.1       leo et_probe_video(modelp)
    149  1.1       leo MODES	*modelp;
    150  1.1       leo {
    151  1.1       leo 	dmode_t	*dm;
    152  1.1       leo 	int	i;
    153  1.1       leo 
    154  1.1       leo 	for (i = 0; (dm = &vid_modes[i])->name != NULL; i++) {
    155  1.1       leo 		LIST_INSERT_HEAD(modelp, dm, link);
    156  1.1       leo 	}
    157  1.1       leo }
    158  1.1       leo 
    159  1.1       leo static void
    160  1.1       leo et_display_view(v)
    161  1.1       leo view_t *v;
    162  1.1       leo {
    163  1.1       leo 	dmode_t		*dm = v->mode;
    164  1.1       leo 	bmap_t		*bm = v->bitmap;
    165  1.1       leo 	int		sv_size;
    166  1.1       leo 	u_short		*src, *dst;
    167  1.3       leo 	save_area_t	*sa;
    168  1.1       leo 
    169  1.1       leo 	if (dm->current_view && (dm->current_view != v)) {
    170  1.1       leo 		/*
    171  1.1       leo 		 * Mark current view for this mode as no longer displayed
    172  1.1       leo 		 */
    173  1.1       leo 		dm->current_view->flags &= ~VF_DISPLAY;
    174  1.1       leo 	}
    175  1.1       leo 	dm->current_view = v;
    176  1.1       leo 	v->flags |= VF_DISPLAY;
    177  1.1       leo 
    178  1.3       leo 	if ((sa = (save_area_t*)v->save_area) == NULL)
    179  1.3       leo 		return; /* XXX: Can't happen.... */
    180  1.3       leo 
    181  1.3       leo 	/*
    182  1.3       leo 	 * Restore register settings and turn the plane pointer
    183  1.3       leo 	 * to the card-memory
    184  1.3       leo 	 */
    185  1.3       leo 	et_hwrest(&sa->sv_regs);
    186  1.1       leo 	bm->plane = et_priv.memkva;
    187  1.1       leo 
    188  1.3       leo 	et_use_colormap(v, v->colormap);
    189  1.1       leo 
    190  1.1       leo 	/*
    191  1.3       leo 	 * Copy the backing store to card-memory
    192  1.1       leo 	 */
    193  1.3       leo 	sv_size = sa->fb_size;
    194  1.3       leo 	src     = sa->sv_fb;
    195  1.3       leo 	dst     = (u_short *)bm->plane;
    196  1.1       leo 	while (sv_size--)
    197  1.1       leo 		*dst++ = *src++;
    198  1.1       leo }
    199  1.1       leo 
    200  1.1       leo void
    201  1.1       leo et_remove_view(v)
    202  1.1       leo view_t *v;
    203  1.1       leo {
    204  1.1       leo 	dmode_t *mode = v->mode;
    205  1.1       leo 
    206  1.1       leo 	if (mode->current_view == v) {
    207  1.1       leo #if 0
    208  1.1       leo 		if (v->flags & VF_DISPLAY)
    209  1.1       leo 			panic("Cannot shutdown display\n"); /* XXX */
    210  1.1       leo #endif
    211  1.1       leo 		mode->current_view = NULL;
    212  1.1       leo 	}
    213  1.1       leo 	v->flags &= ~VF_DISPLAY;
    214  1.1       leo }
    215  1.1       leo 
    216  1.1       leo void
    217  1.1       leo et_save_view(v)
    218  1.1       leo view_t *v;
    219  1.1       leo {
    220  1.3       leo 	bmap_t		*bm = v->bitmap;
    221  1.3       leo 	u_char		font_height;
    222  1.3       leo 	int		sv_size;
    223  1.3       leo 	u_short		*src, *dst;
    224  1.3       leo 	save_area_t	*sa;
    225  1.1       leo 
    226  1.1       leo 	if (!atari_realconfig)
    227  1.1       leo 		return;
    228  1.3       leo 
    229  1.1       leo 	if (RGfx(et_priv.regkva, GCT_ID_MISC) & 1) {
    230  1.4  christos #if 0 /* XXX: Can't use printf here.... */
    231  1.4  christos 		printf("et_save_view: Don't know how to save"
    232  1.1       leo 			" a graphics mode\n");
    233  1.3       leo #endif
    234  1.1       leo 		return;
    235  1.1       leo 	}
    236  1.1       leo 	if (v->save_area == NULL)
    237  1.1       leo 		v->save_area = malloc(SAVEBUF_SIZE, M_DEVBUF, M_WAITOK);
    238  1.1       leo 
    239  1.1       leo 	/*
    240  1.1       leo 	 * Calculate the size of the copy
    241  1.1       leo 	 */
    242  1.1       leo 	font_height = RCrt(et_priv.regkva, CRT_ID_MAX_ROW_ADDRESS) & 0x1f;
    243  1.1       leo 	sv_size = bm->bytes_per_row * (bm->rows / (font_height + 1));
    244  1.3       leo 	sv_size = min(SAVEBUF_SIZE, sv_size);
    245  1.1       leo 
    246  1.3       leo 	/*
    247  1.3       leo 	 * Save all we need to know....
    248  1.3       leo 	 */
    249  1.3       leo 	sa  = (save_area_t *)v->save_area;
    250  1.3       leo 	et_hwsave(&sa->sv_regs);
    251  1.3       leo 	sa->fb_size = sv_size;
    252  1.1       leo 	src = (u_short *)bm->plane;
    253  1.3       leo 	dst = sa->sv_fb;
    254  1.1       leo 	while (sv_size--)
    255  1.1       leo 		*dst++ = *src++;
    256  1.3       leo 	bm->plane = (u_char *)sa->sv_fb;
    257  1.1       leo }
    258  1.1       leo 
    259  1.1       leo void
    260  1.1       leo et_free_view(v)
    261  1.1       leo view_t *v;
    262  1.1       leo {
    263  1.1       leo 	if(v) {
    264  1.1       leo 		et_remove_view(v);
    265  1.1       leo 		if (v->colormap != &gra_con_cmap)
    266  1.1       leo 			free(v->colormap, M_DEVBUF);
    267  1.1       leo 		if (v->save_area != NULL)
    268  1.1       leo 			free(v->save_area, M_DEVBUF);
    269  1.1       leo 		if (v != &gra_con_view) {
    270  1.1       leo 			free(v->bitmap, M_DEVBUF);
    271  1.1       leo 			free(v, M_DEVBUF);
    272  1.1       leo 		}
    273  1.1       leo 	}
    274  1.1       leo }
    275  1.1       leo 
    276  1.1       leo static int
    277  1.1       leo et_use_colormap(v, cm)
    278  1.1       leo view_t		*v;
    279  1.1       leo colormap_t	*cm;
    280  1.1       leo {
    281  1.1       leo 	return (0); /* XXX: Nothing here for now... */
    282  1.1       leo }
    283  1.1       leo 
    284  1.1       leo static view_t *
    285  1.1       leo et_alloc_view(mode, dim, depth)
    286  1.1       leo dmode_t	*mode;
    287  1.1       leo dimen_t	*dim;
    288  1.1       leo u_char   depth;
    289  1.1       leo {
    290  1.3       leo 	view_t		*v;
    291  1.3       leo 	bmap_t		*bm;
    292  1.3       leo 	box_t		box;
    293  1.3       leo 	save_area_t	*sa;
    294  1.1       leo 
    295  1.1       leo 	if (!atari_realconfig) {
    296  1.1       leo 		v  = &gra_con_view;
    297  1.1       leo 		bm = &con_bm;
    298  1.1       leo 	}
    299  1.1       leo 	else {
    300  1.1       leo 		v  = malloc(sizeof(*v), M_DEVBUF, M_WAITOK);
    301  1.1       leo 		bm = malloc(sizeof(*bm), M_DEVBUF, M_WAITOK);
    302  1.1       leo 	}
    303  1.1       leo 	v->bitmap = bm;
    304  1.1       leo 
    305  1.1       leo 	/*
    306  1.1       leo 	 * Initialize the bitmap
    307  1.1       leo 	 */
    308  1.1       leo 	bm->plane         = et_priv.memkva;
    309  1.6       leo 	bm->hw_address    = (caddr_t)kvtop(et_priv.memkva);
    310  1.1       leo 	bm->regs          = et_priv.regkva;
    311  1.6       leo 	bm->hw_regs       = (caddr_t)kvtop(et_priv.regkva);
    312  1.1       leo 	bm->reg_size      = et_priv.regsz;
    313  1.1       leo 
    314  1.1       leo 	bm->bytes_per_row = (mode->size.width * depth) / NBBY;
    315  1.1       leo 	bm->rows          = mode->size.height;
    316  1.1       leo 	bm->depth         = depth;
    317  1.1       leo 
    318  1.1       leo 	/*
    319  1.1       leo 	 * Allocate a save_area.
    320  1.1       leo 	 * Note: If atari_realconfig is false, no save area is (can be)
    321  1.1       leo 	 * allocated. This means that the plane is the video memory,
    322  1.3       leo 	 * which is what's wanted in this case.
    323  1.1       leo 	 */
    324  1.1       leo 	if (atari_realconfig) {
    325  1.1       leo 		v->save_area = malloc(SAVEBUF_SIZE, M_DEVBUF, M_WAITOK);
    326  1.3       leo 		sa           = (save_area_t*)v->save_area;
    327  1.3       leo 		sa->fb_size  = 0;
    328  1.3       leo 		bm->plane    = (u_char *)sa->sv_fb;
    329  1.3       leo 		et_loadmode(mode->data, &sa->sv_regs);
    330  1.1       leo 	}
    331  1.1       leo 	else v->save_area = NULL;
    332  1.1       leo 
    333  1.1       leo 	v->colormap = alloc_colormap(mode);
    334  1.1       leo 	if (v->colormap) {
    335  1.1       leo 		INIT_BOX(&box,0,0,mode->size.width,mode->size.height);
    336  1.1       leo 		init_view(v, bm, mode, &box);
    337  1.1       leo 		return (v);
    338  1.1       leo 	}
    339  1.1       leo 	if (v != &gra_con_view) {
    340  1.1       leo 		free(v, M_DEVBUF);
    341  1.1       leo 		free(bm, M_DEVBUF);
    342  1.1       leo 	}
    343  1.1       leo 	return (NULL);
    344  1.1       leo }
    345  1.1       leo 
    346  1.1       leo static void
    347  1.1       leo init_view(v, bm, mode, dbox)
    348  1.1       leo view_t	*v;
    349  1.1       leo bmap_t	*bm;
    350  1.1       leo dmode_t	*mode;
    351  1.1       leo box_t	*dbox;
    352  1.1       leo {
    353  1.1       leo 	v->bitmap    = bm;
    354  1.1       leo 	v->mode      = mode;
    355  1.1       leo 	v->flags     = 0;
    356  1.1       leo 	bcopy(dbox, &v->display, sizeof(box_t));
    357  1.1       leo }
    358  1.1       leo 
    359  1.1       leo /* XXX: No more than a stub... */
    360  1.1       leo static colormap_t *
    361  1.1       leo alloc_colormap(dm)
    362  1.1       leo dmode_t		*dm;
    363  1.1       leo {
    364  1.1       leo 	colormap_t	*cm;
    365  1.1       leo 	int		i;
    366  1.1       leo 
    367  1.1       leo 	cm = &gra_con_cmap;
    368  1.1       leo 	cm->entry = gra_con_colors;
    369  1.1       leo 
    370  1.1       leo 	cm->first = 0;
    371  1.1       leo 	cm->size  = 2;
    372  1.1       leo 
    373  1.1       leo 	for (i = 0; i < 2; i++)
    374  1.1       leo 		cm->entry[i] = gra_def_color16[i % 16];
    375  1.1       leo 	return (cm);
    376  1.1       leo }
    377  1.1       leo 
    378  1.1       leo /*
    379  1.1       leo  * Go look for a VGA card on the PCI-bus. This search is a
    380  1.1       leo  * stripped down version of the PCI-probe. It only looks on
    381  1.1       leo  * bus0 for simple cards.
    382  1.1       leo  */
    383  1.1       leo int
    384  1.1       leo et_probe_card()
    385  1.1       leo {
    386  1.1       leo 	pci_chipset_tag_t	pc = NULL; /* XXX */
    387  1.1       leo 	pcitag_t		tag;
    388  1.1       leo 	int			class, device, found, id, maxndevs;
    389  1.1       leo 
    390  1.1       leo 	found    = 0;
    391  1.1       leo 	tag      = 0;
    392  1.1       leo 	maxndevs = pci_bus_maxdevs(pc, 0);
    393  1.1       leo 
    394  1.1       leo 	for (device = 0; device < maxndevs; device++) {
    395  1.1       leo 
    396  1.1       leo 		tag = pci_make_tag(pc, 0, device, 0);
    397  1.1       leo 		id  = pci_conf_read(pc, tag, PCI_ID_REG);
    398  1.1       leo 		if (id == 0 || id == 0xffffffff)
    399  1.1       leo 			continue;
    400  1.1       leo 		class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    401  1.1       leo 		if (PCI_CLASS(class) == PCI_CLASS_PREHISTORIC
    402  1.1       leo 		    && PCI_SUBCLASS(class) == PCI_SUBCLASS_PREHISTORIC_VGA) {
    403  1.1       leo 			found = 1;
    404  1.1       leo 			break;
    405  1.1       leo 		}
    406  1.1       leo 		if (PCI_CLASS(class) == PCI_CLASS_DISPLAY
    407  1.1       leo 		    && PCI_SUBCLASS(class) == PCI_SUBCLASS_DISPLAY_VGA) {
    408  1.1       leo 			found = 1;
    409  1.1       leo 			break;
    410  1.1       leo 		}
    411  1.1       leo 	}
    412  1.1       leo 	if (!found)
    413  1.1       leo 		return (0);
    414  1.1       leo 
    415  1.1       leo 	et_priv.pci_tag = tag;
    416  1.1       leo 
    417  1.1       leo 	/*
    418  1.1       leo 	 * The things below are setup in atari_init.c
    419  1.1       leo 	 */
    420  1.1       leo 	et_priv.regkva  = (volatile caddr_t)pci_io_addr;
    421  1.1       leo 	et_priv.memkva  = (volatile caddr_t)pci_mem_addr;
    422  1.1       leo 	et_priv.memsz   = 4 * NBPG;
    423  1.1       leo 	et_priv.regsz   = NBPG;
    424  1.1       leo 
    425  1.1       leo 	if (found && !atari_realconfig) {
    426  1.1       leo 		et_boardinit();
    427  1.3       leo 		et_loadmode(&hw_modes[0], NULL);
    428  1.1       leo 		return (1);
    429  1.1       leo 	}
    430  1.1       leo 
    431  1.1       leo 	return (1);
    432  1.1       leo }
    433  1.1       leo 
    434  1.1       leo static void
    435  1.3       leo et_loadmode(mode, regs)
    436  1.3       leo struct grfvideo_mode	*mode;
    437  1.3       leo et_sv_reg_t		*regs;
    438  1.1       leo {
    439  1.1       leo 	unsigned short	HDE, VDE;
    440  1.1       leo 	int	    	lace, dblscan;
    441  1.1       leo 	int     	uplim, lowlim;
    442  1.3       leo 	int		i;
    443  1.1       leo 	unsigned char	clock, tmp;
    444  1.1       leo 	volatile u_char	*ba;
    445  1.3       leo 	et_sv_reg_t	loc_regs;
    446  1.3       leo 
    447  1.3       leo 	if (regs == NULL)
    448  1.3       leo 		regs = &loc_regs;
    449  1.1       leo 
    450  1.1       leo 	ba  = et_priv.regkva;
    451  1.1       leo 	HDE = mode->disp_width / 8 - 1;
    452  1.1       leo 	VDE = mode->disp_height - 1;
    453  1.1       leo 
    454  1.1       leo 	/* figure out whether lace or dblscan is needed */
    455  1.1       leo 
    456  1.1       leo 	uplim   = mode->disp_height + (mode->disp_height / 4);
    457  1.1       leo 	lowlim  = mode->disp_height - (mode->disp_height / 4);
    458  1.1       leo 	lace    = (((mode->vtotal * 2) > lowlim)
    459  1.1       leo 		   && ((mode->vtotal * 2) < uplim)) ? 1 : 0;
    460  1.1       leo 	dblscan = (((mode->vtotal / 2) > lowlim)
    461  1.1       leo 		   && ((mode->vtotal / 2) < uplim)) ? 1 : 0;
    462  1.1       leo 
    463  1.1       leo 	/* adjustments */
    464  1.1       leo 	if (lace)
    465  1.1       leo 		VDE /= 2;
    466  1.1       leo 
    467  1.3       leo 	regs->misc_output = 0x23; /* Page 0, Color mode */
    468  1.3       leo 	regs->seg_sel     = 0x00;
    469  1.3       leo 	regs->state_ctl   = 0x00;
    470  1.3       leo 
    471  1.3       leo 	regs->seq[SEQ_ID_RESET]           = 0x03; /* reset off		*/
    472  1.3       leo 	regs->seq[SEQ_ID_CLOCKING_MODE]   = 0x21; /* Turn off screen	*/
    473  1.3       leo 	regs->seq[SEQ_ID_MAP_MASK]        = 0xff; /* Cpu writes all planes*/
    474  1.3       leo 	regs->seq[SEQ_ID_CHAR_MAP_SELECT] = 0x00; /* Char. generator 0	*/
    475  1.3       leo 	regs->seq[SEQ_ID_MEMORY_MODE]     = 0x0e; /* Seq. Memory mode	*/
    476  1.1       leo 
    477  1.1       leo 	/*
    478  1.1       leo 	 * Set the clock...
    479  1.1       leo 	 */
    480  1.1       leo 	for(clock = ET_NUMCLOCKS-1; clock > 0; clock--) {
    481  1.1       leo 		if (et_clockfreqs[clock] <= mode->pixel_clock)
    482  1.1       leo 			break;
    483  1.1       leo 	}
    484  1.3       leo 	regs->misc_output |= (clock & 3) << 2;
    485  1.3       leo 	regs->aux_mode     = 0xb4 | ((clock & 8) << 3);
    486  1.3       leo 	regs->compat_6845  = (clock & 4) ? 0x0a : 0x08;
    487  1.3       leo 
    488  1.3       leo 	/*
    489  1.3       leo 	 * The display parameters...
    490  1.3       leo 	 */
    491  1.3       leo 	regs->crt[CRT_ID_HOR_TOTAL]        =  mode->htotal;
    492  1.3       leo 	regs->crt[CRT_ID_HOR_DISP_ENA_END] = ((HDE >= mode->hblank_start)
    493  1.3       leo 						? mode->hblank_stop - 1
    494  1.3       leo 						: HDE);
    495  1.3       leo 	regs->crt[CRT_ID_START_HOR_BLANK]  = mode->hblank_start;
    496  1.3       leo 	regs->crt[CRT_ID_END_HOR_BLANK]    = (mode->hblank_stop & 0x1f) | 0x80;
    497  1.3       leo 	regs->crt[CRT_ID_START_HOR_RETR]   = mode->hsync_start;
    498  1.3       leo 	regs->crt[CRT_ID_END_HOR_RETR]     = (mode->hsync_stop & 0x1f)
    499  1.3       leo 						| ((mode->hblank_stop & 0x20)
    500  1.3       leo 							? 0x80 : 0x00);
    501  1.3       leo 	regs->crt[CRT_ID_VER_TOTAL]        = mode->vtotal;
    502  1.3       leo 	regs->crt[CRT_ID_START_VER_RETR]   = mode->vsync_start;
    503  1.3       leo 	regs->crt[CRT_ID_END_VER_RETR]     = (mode->vsync_stop & 0x0f) | 0x30;
    504  1.3       leo 	regs->crt[CRT_ID_VER_DISP_ENA_END] = VDE;
    505  1.3       leo 	regs->crt[CRT_ID_START_VER_BLANK]  = mode->vblank_start;
    506  1.3       leo 	regs->crt[CRT_ID_END_VER_BLANK]    = mode->vblank_stop;
    507  1.3       leo 	regs->crt[CRT_ID_MODE_CONTROL]     = 0xab;
    508  1.3       leo 	regs->crt[CRT_ID_START_ADDR_HIGH]  = 0x00;
    509  1.3       leo 	regs->crt[CRT_ID_START_ADDR_LOW]   = 0x00;
    510  1.3       leo 	regs->crt[CRT_ID_LINE_COMPARE]     = 0xff;
    511  1.3       leo 	regs->crt[CRT_ID_UNDERLINE_LOC]    = 0x00;
    512  1.3       leo 	regs->crt[CRT_ID_OFFSET]           = mode->disp_width/16;
    513  1.3       leo 	regs->crt[CRT_ID_MAX_ROW_ADDRESS]  =
    514  1.3       leo 		0x40 |
    515  1.3       leo 		(dblscan ? 0x80 : 0x00) |
    516  1.3       leo 		((mode->vblank_start & 0x200) ? 0x20 : 0x00);
    517  1.3       leo 	regs->crt[CRT_ID_OVERFLOW] =
    518  1.1       leo 		0x10 |
    519  1.1       leo 		((mode->vtotal       & 0x100) ? 0x01 : 0x00) |
    520  1.1       leo 		((VDE                & 0x100) ? 0x02 : 0x00) |
    521  1.1       leo 		((mode->vsync_start  & 0x100) ? 0x04 : 0x00) |
    522  1.1       leo 		((mode->vblank_start & 0x100) ? 0x08 : 0x00) |
    523  1.1       leo 		((mode->vtotal       & 0x200) ? 0x20 : 0x00) |
    524  1.1       leo 		((VDE                & 0x200) ? 0x40 : 0x00) |
    525  1.3       leo 		((mode->vsync_start  & 0x200) ? 0x80 : 0x00);
    526  1.3       leo 	regs->overfl_high =
    527  1.1       leo 		0x10 |
    528  1.1       leo 		((mode->vblank_start & 0x400) ? 0x01 : 0x00) |
    529  1.1       leo 		((mode->vtotal       & 0x400) ? 0x02 : 0x00) |
    530  1.1       leo 		((VDE                & 0x400) ? 0x04 : 0x00) |
    531  1.1       leo 		((mode->vsync_start  & 0x400) ? 0x08 : 0x00) |
    532  1.3       leo 		(lace ? 0x80 : 0x00);
    533  1.3       leo 	regs->hor_overfl =
    534  1.1       leo 		((mode->htotal       & 0x100) ? 0x01 : 0x00) |
    535  1.1       leo 		((mode->hblank_start & 0x100) ? 0x04 : 0x00) |
    536  1.3       leo 		((mode->hsync_start  & 0x100) ? 0x10 : 0x00);
    537  1.3       leo 
    538  1.3       leo 	regs->grf[GCT_ID_SET_RESET]        = 0x00;
    539  1.3       leo 	regs->grf[GCT_ID_ENABLE_SET_RESET] = 0x00;
    540  1.3       leo 	regs->grf[GCT_ID_COLOR_COMPARE]    = 0x00;
    541  1.3       leo 	regs->grf[GCT_ID_DATA_ROTATE]      = 0x00;
    542  1.3       leo 	regs->grf[GCT_ID_READ_MAP_SELECT]  = 0x00;
    543  1.3       leo 	regs->grf[GCT_ID_GRAPHICS_MODE]    = mode->depth == 1 ? 0x00: 0x40;
    544  1.3       leo 	regs->grf[GCT_ID_MISC]             = 0x01;
    545  1.3       leo 	regs->grf[GCT_ID_COLOR_XCARE]      = 0x0f;
    546  1.3       leo 	regs->grf[GCT_ID_BITMASK]          = 0xff;
    547  1.3       leo 
    548  1.3       leo 	for (i = 0; i < 0x10; i++)
    549  1.3       leo 		regs->attr[i] = i;
    550  1.3       leo 	regs->attr[ACT_ID_ATTR_MODE_CNTL]  = 0x01;
    551  1.3       leo 	regs->attr[ACT_ID_OVERSCAN_COLOR]  = 0x00;
    552  1.3       leo 	regs->attr[ACT_ID_COLOR_PLANE_ENA] = 0x0f;
    553  1.3       leo 	regs->attr[ACT_ID_HOR_PEL_PANNING] = 0x00;
    554  1.3       leo 	regs->attr[ACT_ID_COLOR_SELECT]    = 0x00;
    555  1.3       leo 	regs->attr[ACT_ID_MISCELLANEOUS]   = 0x00;
    556  1.1       leo 
    557  1.1       leo 	/*
    558  1.1       leo 	 * XXX: This works for depth == 4. I need some better docs
    559  1.1       leo 	 * to fix the other modes....
    560  1.1       leo 	 */
    561  1.1       leo 	vgaw(ba, VDAC_MASK, 0xff);
    562  1.1       leo 	vgar(ba, VDAC_MASK);
    563  1.1       leo 	vgar(ba, VDAC_MASK);
    564  1.1       leo 	vgar(ba, VDAC_MASK);
    565  1.1       leo 	vgar(ba, VDAC_MASK);
    566  1.1       leo 
    567  1.1       leo 	vgaw(ba, VDAC_MASK, 0);
    568  1.1       leo 	/*
    569  1.1       leo 	 * End of depth stuff
    570  1.1       leo 	 */
    571  1.1       leo 
    572  1.1       leo 	/*
    573  1.1       leo 	 * Compute Hsync & Vsync polarity
    574  1.1       leo 	 * Note: This seems to be some kind of a black art :-(
    575  1.1       leo 	 */
    576  1.3       leo 	tmp = regs->misc_output & 0x3f;
    577  1.5       leo #if 1 /* This is according to my BW monitor & Xfree... */
    578  1.1       leo 	if (VDE < 400)
    579  1.1       leo 		tmp |= 0x40;	/* -hsync +vsync */
    580  1.1       leo 	else if (VDE < 480)
    581  1.1       leo 		tmp |= 0xc0;	/* -hsync -vsync */
    582  1.1       leo #else /* This is according to my color monitor.... */
    583  1.1       leo 	if (VDE < 400)
    584  1.1       leo 		tmp |= 0x00;	/* +hsync +vsync */
    585  1.1       leo 	else if (VDE < 480)
    586  1.1       leo 		tmp |= 0x80;	/* +hsync -vsync */
    587  1.1       leo #endif
    588  1.1       leo 	/* I'm unable to try the rest.... */
    589  1.3       leo 	regs->misc_output = tmp;
    590  1.1       leo 
    591  1.3       leo 	if(regs == &loc_regs)
    592  1.3       leo 		et_hwrest(regs);
    593  1.1       leo }
    594  1.1       leo 
    595  1.1       leo static void
    596  1.1       leo et_boardinit()
    597  1.1       leo {
    598  1.1       leo 	volatile u_char *ba;
    599  1.1       leo 	int		i, j;
    600  1.1       leo 
    601  1.1       leo 	ba = et_priv.regkva;
    602  1.1       leo 
    603  1.1       leo 	vgaw(ba, GREG_HERCULESCOMPAT,     0x03);
    604  1.1       leo 	vgaw(ba, GREG_DISPMODECONTROL,    0xa0);
    605  1.1       leo 	vgaw(ba, GREG_MISC_OUTPUT_W,      0x23);
    606  1.1       leo 
    607  1.1       leo 	WSeq(ba, SEQ_ID_RESET,            0x03);
    608  1.1       leo 	WSeq(ba, SEQ_ID_CLOCKING_MODE,    0x21);	/* 8 dot, Display off */
    609  1.1       leo 	WSeq(ba, SEQ_ID_MAP_MASK,         0x0f);
    610  1.1       leo 	WSeq(ba, SEQ_ID_CHAR_MAP_SELECT,  0x00);
    611  1.1       leo 	WSeq(ba, SEQ_ID_MEMORY_MODE,      0x0e);
    612  1.1       leo 	WSeq(ba, SEQ_ID_AUXILIARY_MODE,   0xf4);
    613  1.1       leo 
    614  1.1       leo 	WCrt(ba, CRT_ID_PRESET_ROW_SCAN,  0x00);
    615  1.1       leo 	WCrt(ba, CRT_ID_CURSOR_START,     0x00);
    616  1.1       leo 	WCrt(ba, CRT_ID_CURSOR_END,       0x08);
    617  1.1       leo 	WCrt(ba, CRT_ID_START_ADDR_HIGH,  0x00);
    618  1.1       leo 	WCrt(ba, CRT_ID_START_ADDR_LOW,   0x00);
    619  1.1       leo 	WCrt(ba, CRT_ID_CURSOR_LOC_HIGH,  0x00);
    620  1.1       leo 	WCrt(ba, CRT_ID_CURSOR_LOC_LOW,   0x00);
    621  1.1       leo 
    622  1.1       leo 	WCrt(ba, CRT_ID_UNDERLINE_LOC,    0x07);
    623  1.1       leo 	WCrt(ba, CRT_ID_MODE_CONTROL,     0xa3);
    624  1.1       leo 	WCrt(ba, CRT_ID_LINE_COMPARE,     0xff);
    625  1.1       leo 	/*
    626  1.1       leo 	 * ET4000 special
    627  1.1       leo 	 */
    628  1.1       leo 	WCrt(ba, CRT_ID_RASCAS_CONFIG,    0x28);
    629  1.1       leo 	WCrt(ba, CTR_ID_EXT_START,        0x00);
    630  1.1       leo 	WCrt(ba, CRT_ID_6845_COMPAT,      0x08);
    631  1.1       leo 	WCrt(ba, CRT_ID_VIDEO_CONFIG1,    0x73);
    632  1.1       leo 	WCrt(ba, CRT_ID_VIDEO_CONFIG2,    0x09);
    633  1.1       leo 
    634  1.1       leo 	WCrt(ba, CRT_ID_HOR_OVERFLOW,     0x00);
    635  1.1       leo 
    636  1.1       leo 	WGfx(ba, GCT_ID_SET_RESET,        0x00);
    637  1.1       leo 	WGfx(ba, GCT_ID_ENABLE_SET_RESET, 0x00);
    638  1.1       leo 	WGfx(ba, GCT_ID_COLOR_COMPARE,    0x00);
    639  1.1       leo 	WGfx(ba, GCT_ID_DATA_ROTATE,      0x00);
    640  1.1       leo 	WGfx(ba, GCT_ID_READ_MAP_SELECT,  0x00);
    641  1.1       leo 	WGfx(ba, GCT_ID_GRAPHICS_MODE,    0x00);
    642  1.1       leo 	WGfx(ba, GCT_ID_MISC,             0x01);
    643  1.1       leo 	WGfx(ba, GCT_ID_COLOR_XCARE,      0x0f);
    644  1.1       leo 	WGfx(ba, GCT_ID_BITMASK,          0xff);
    645  1.1       leo 
    646  1.1       leo 	vgaw(ba, GREG_SEGMENTSELECT,      0x00);
    647  1.1       leo 
    648  1.1       leo 	for (i = 0; i < 0x10; i++)
    649  1.1       leo 		WAttr(ba, i, i);
    650  1.1       leo 	WAttr(ba, ACT_ID_ATTR_MODE_CNTL,  0x01);
    651  1.1       leo 	WAttr(ba, ACT_ID_OVERSCAN_COLOR,  0x00);
    652  1.1       leo 	WAttr(ba, ACT_ID_COLOR_PLANE_ENA, 0x0f);
    653  1.1       leo 	WAttr(ba, ACT_ID_HOR_PEL_PANNING, 0x00);
    654  1.1       leo 	WAttr(ba, ACT_ID_COLOR_SELECT,    0x00);
    655  1.1       leo 	WAttr(ba, ACT_ID_MISCELLANEOUS,   0x00);
    656  1.1       leo 
    657  1.1       leo 	vgaw(ba, VDAC_MASK, 0xff);
    658  1.1       leo 
    659  1.1       leo #if 0 /* XXX: We like to do this: */
    660  1.1       leo 	delay(200000);
    661  1.1       leo #else /* But because we run before the delay is initialized: */
    662  1.1       leo 	for(i = 0; i < 4000; i++)
    663  1.1       leo 		for(j =  0; j < 400; j++);
    664  1.1       leo #endif
    665  1.1       leo 
    666  1.1       leo 	/*
    667  1.1       leo 	 * colors initially set to greyscale
    668  1.1       leo 	 */
    669  1.1       leo 	vgaw(ba, VDAC_ADDRESS_W, 0);
    670  1.1       leo 	for (i = 255; i >= 0; i--) {
    671  1.1       leo 		vgaw(ba, VDAC_DATA, i);
    672  1.1       leo 		vgaw(ba, VDAC_DATA, i);
    673  1.1       leo 		vgaw(ba, VDAC_DATA, i);
    674  1.1       leo 	}
    675  1.3       leo }
    676  1.3       leo 
    677  1.3       leo void
    678  1.3       leo et_hwsave(et_regs)
    679  1.3       leo et_sv_reg_t	*et_regs;
    680  1.3       leo {
    681  1.3       leo 	volatile u_char *ba;
    682  1.3       leo 	int		i, s;
    683  1.3       leo 
    684  1.3       leo 	ba = et_priv.regkva;
    685  1.3       leo 
    686  1.3       leo 	s = splhigh();
    687  1.3       leo 
    688  1.3       leo 	/*
    689  1.3       leo 	 * General VGA registers
    690  1.3       leo 	 */
    691  1.3       leo 	et_regs->misc_output = vgar(ba, GREG_MISC_OUTPUT_R);
    692  1.3       leo 	for(i = 0; i < 25; i++)
    693  1.3       leo 		et_regs->crt[i]  = RCrt(ba, i);
    694  1.3       leo 	for(i = 0; i < 21; i++)
    695  1.3       leo 		et_regs->attr[i] = RAttr(ba, i | 0x20);
    696  1.3       leo 	for(i = 0; i < 9; i++)
    697  1.3       leo 		et_regs->grf[i]  = RGfx(ba, i);
    698  1.3       leo 	for(i = 0; i < 5; i++)
    699  1.3       leo 		et_regs->seq[i]  = RSeq(ba, i);
    700  1.3       leo 
    701  1.3       leo 	/*
    702  1.3       leo 	 * ET4000 extensions
    703  1.3       leo 	 */
    704  1.3       leo 	et_regs->ext_start   = RCrt(ba, CTR_ID_EXT_START);
    705  1.3       leo 	et_regs->compat_6845 = RCrt(ba, CRT_ID_6845_COMPAT);
    706  1.3       leo 	et_regs->overfl_high = RCrt(ba, CRT_ID_OVERFLOW_HIGH);
    707  1.3       leo 	et_regs->hor_overfl  = RCrt(ba, CRT_ID_HOR_OVERFLOW);
    708  1.3       leo 	et_regs->state_ctl   = RSeq(ba, SEQ_ID_STATE_CONTROL);
    709  1.3       leo 	et_regs->aux_mode    = RSeq(ba, SEQ_ID_AUXILIARY_MODE);
    710  1.3       leo 	et_regs->seg_sel     = vgar(ba, GREG_SEGMENTSELECT);
    711  1.3       leo 
    712  1.3       leo 	s = splx(s);
    713  1.3       leo }
    714  1.3       leo 
    715  1.3       leo void
    716  1.3       leo et_hwrest(et_regs)
    717  1.3       leo et_sv_reg_t	*et_regs;
    718  1.3       leo {
    719  1.3       leo 	volatile u_char *ba;
    720  1.3       leo 	int		i, s;
    721  1.3       leo 
    722  1.3       leo 	ba = et_priv.regkva;
    723  1.3       leo 
    724  1.3       leo 	s = splhigh();
    725  1.3       leo 
    726  1.3       leo 	vgaw(ba, GREG_SEGMENTSELECT, 0);
    727  1.3       leo 	vgaw(ba, GREG_MISC_OUTPUT_W, et_regs->misc_output);
    728  1.3       leo 
    729  1.3       leo 	/*
    730  1.3       leo 	 * General VGA registers
    731  1.3       leo 	 */
    732  1.3       leo 	for(i = 0; i < 5; i++)
    733  1.3       leo 		WSeq(ba, i, et_regs->seq[i]);
    734  1.3       leo 
    735  1.3       leo 	/*
    736  1.3       leo 	 * Make sure we're allowed to write all crt-registers
    737  1.3       leo 	 */
    738  1.3       leo 	WCrt(ba, CRT_ID_END_VER_RETR,
    739  1.3       leo 		et_regs->crt[CRT_ID_END_VER_RETR] & 0x7f);
    740  1.3       leo 	for(i = 0; i < 25; i++)
    741  1.3       leo 		WCrt(ba, i, et_regs->crt[i]);
    742  1.3       leo 	for(i = 0; i < 9; i++)
    743  1.3       leo 		WGfx(ba, i, et_regs->grf[i]);
    744  1.3       leo 	for(i = 0; i < 21; i++)
    745  1.3       leo 		WAttr(ba, i | 0x20, et_regs->attr[i]);
    746  1.3       leo 
    747  1.3       leo 	/*
    748  1.3       leo 	 * ET4000 extensions
    749  1.3       leo 	 */
    750  1.3       leo 	WSeq(ba, SEQ_ID_STATE_CONTROL, et_regs->state_ctl);
    751  1.3       leo 	WSeq(ba, SEQ_ID_AUXILIARY_MODE, et_regs->aux_mode);
    752  1.3       leo 	WCrt(ba, CTR_ID_EXT_START, et_regs->ext_start);
    753  1.3       leo 	WCrt(ba, CRT_ID_6845_COMPAT, et_regs->compat_6845);
    754  1.3       leo 	WCrt(ba, CRT_ID_OVERFLOW_HIGH, et_regs->overfl_high);
    755  1.3       leo 	WCrt(ba, CRT_ID_HOR_OVERFLOW, et_regs->hor_overfl);
    756  1.3       leo 	vgaw(ba, GREG_SEGMENTSELECT, et_regs->seg_sel);
    757  1.3       leo 
    758  1.3       leo 	i = et_regs->seq[SEQ_ID_CLOCKING_MODE] & ~0x20;
    759  1.3       leo 	WSeq(ba, SEQ_ID_CLOCKING_MODE, i);
    760  1.3       leo 
    761  1.3       leo 	s = splx(s);
    762  1.1       leo }
    763