Home | History | Annotate | Line # | Download | only in dev
grf_ul.c revision 1.17
      1 /*	$NetBSD: grf_ul.c,v 1.17 1996/05/09 20:31:25 is Exp $	*/
      2 #define UL_DEBUG
      3 
      4 /*
      5  * Copyright (c) 1995 Ignatios Souvatzis
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Lutz Vieweg.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 #include "grful.h"
     34 #if NGRFUL > 0
     35 
     36 /* Graphics routines for the University of Lowell A2410 board,
     37    using the TMS34010 processor. */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/errno.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/device.h>
     44 #include <sys/malloc.h>
     45 #include <sys/syslog.h>
     46 
     47 #include <machine/cpu.h>
     48 
     49 #include <amiga/amiga/device.h>
     50 #include <amiga/amiga/isr.h>
     51 #include <amiga/dev/zbusvar.h>
     52 #include <amiga/dev/grfioctl.h>
     53 #include <amiga/dev/grfvar.h>
     54 #include <amiga/dev/grf_ulreg.h>
     55 
     56 extern u_int16_t tmscode[];
     57 
     58 int ul_ioctl __P((struct grf_softc *, u_long, void *, dev_t));
     59 int ul_getcmap __P((struct grf_softc *, struct grf_colormap *, dev_t));
     60 int ul_putcmap __P((struct grf_softc *, struct grf_colormap *, dev_t));
     61 int ul_bitblt __P((struct grf_softc *, struct grf_bitblt *, dev_t));
     62 int ul_blank __P((struct grf_softc *, int *, dev_t));
     63 
     64 static int ulisr __P((void *));
     65 int ulowell_alive __P((struct grfvideo_mode *));
     66 static void ul_load_code __P((struct grf_softc *));
     67 static int ul_load_mon __P((struct grf_softc *, struct grfvideo_mode *));
     68 static int ul_getvmode __P((struct grf_softc *, struct grfvideo_mode *));
     69 static int ul_setvmode __P((struct grf_softc *, unsigned));
     70 static __inline void ul_setfb __P((struct grf_softc *, u_long));
     71 
     72 /*
     73  * marked true early so that ulowell_cnprobe() can tell if we are alive.
     74  */
     75 int ulowell_inited;
     76 
     77 /* standard-palette definition */
     78 u_int8_t ul_std_palette[] = {
     79 	0,128,  0,128,   0,128,  0,128,  0,255,  0,255,   0,255,  0,255,
     80 	0,  0,128,128,   0,  0,128,128,  0,  0,255,255,   0,  0,255,255,
     81 	0,  0,  0,  0, 128,128,128,128,  0,  0,  0,  0, 255,255,255,255};
     82 
     83 u_int8_t ul_ovl_palette[] = {
     84 	128, 0, 0, 0,
     85 	128, 0, 0, 0,
     86 	128, 0, 0, 0};
     87 
     88 struct grfvideo_mode ul_monitor_defs[] = {
     89 
     90  	/*
     91 	 * Horizontal values are given in TMS units, that is, for the
     92 	 * A2410 board, units of 16 pixels. The ioctl multiplies (when
     93 	 * exporting) or divides (when importing) them by 16 to conform to.
     94 	 *
     95 	 * XXX This used to be in units of 8 pixel times. We
     96 	 * must also change amiga/stand/grfconfig/grfconfig.c,
     97 	 * grf_{rt,rh,cl,cv}.c and egsgrfconfig (the latter to generate the
     98 	 * horizontal timings in units of pixels instead of 8 pixels.
     99 	 * You will also have to write warnings in BIG BOLD RED BLINKING
    100 	 * LETTERS all over the docs, and still people will fry their monitors.
    101 	 *
    102 	 * btw, the _totals are always sync_start+1, to compute the frequencies
    103 	 * correctly. (see TMS34010 manual)
    104 	 */
    105 
    106 	/* 1024x768, 60Hz */
    107 	{1, "1024x768",   66667000, 1024, 768, 8,
    108 		82, 18, 86, 12, 87, 794, 26, 797, 2, 798},
    109 
    110 	/* 864x648, 70Hz */
    111 	{2, "864x648",    50000000,  864, 648, 8,
    112 		61,  7, 65,  3, 66, 667, 19, 677, 4, 678},
    113 
    114 	/* 800x600, 60Hz */
    115 	{3, "800x600",    36000000,  800, 600, 8,
    116 		57,  7, 61,  3, 62, 619, 19, 629, 4, 630},
    117 
    118 	/* 640x400, 60 Hz, interlaced */
    119 	{4, "640x400I",   14318000,  640, 400, 8,
    120 		48,  8, 56,  3, 57, 239, 39, 262, 2, 240},
    121 
    122 	/* 1024x768, 65Hz interlaced, s.th. is strange */
    123 	{5, "1024x768?I", 44980000, 1024, 768, 8,
    124 		76, 12, 79,  3, 80, 512, 24, 533, 2, 534},
    125 
    126 	/* 1024x1024, 60Hz */
    127 	{6, "1024x1024", 80000000, 1024,1024, 8,
    128 		77, 13, 78,  5, 78,1051, 27,1054, 2,1055},
    129 
    130 	/* 736x480, 60 Hz */
    131 	{7, "736x480", 28636300, 736, 480, 8,
    132 		54,  8, 57,  3, 58, 503, 23, 514, 3, 515},
    133 };
    134 
    135 int ulowell_mon_max = sizeof (ul_monitor_defs)/sizeof (ul_monitor_defs[0]);
    136 
    137 /* option settable */
    138 #ifndef ULOWELL_OSC1
    139 #define ULOWELL_OSC1 36000000
    140 #endif
    141 
    142 #ifndef ULOWELL_OSC2
    143 #define ULOWELL_OSC2 66667000
    144 #endif
    145 
    146 #ifndef ULOWELL_DEFAULT_MON
    147 #define ULOWELL_DEFAULT_MON 1
    148 #endif
    149 
    150 /* patchable */
    151 int ulowell_default_mon = ULOWELL_DEFAULT_MON;
    152 int ulowell_default_gfx = ULOWELL_DEFAULT_MON;
    153 
    154 /*
    155  * yes, this should be per board. We don't pay service to multiple boards,
    156  * anyway.
    157  */
    158 
    159 u_long ulowell_clock[2] = { ULOWELL_OSC2, ULOWELL_OSC1 };
    160 
    161 static struct grfvideo_mode *current_mon;
    162 
    163 /*
    164  * We dont use ints at the moment, but will need this later to avoid
    165  * busy_waiting in gsp_write, and we use it for spurious int warnings.
    166  */
    167 
    168 static int
    169 ulisr(arg)
    170 	void *arg;
    171 {
    172 	struct grf_softc *gp = arg;
    173 	struct gspregs *ba;
    174 	u_int16_t	thebits;
    175 
    176 	if (gp == NULL)
    177 		return 0;
    178 
    179 	ba = (struct gspregs *)gp->g_regkva;
    180 
    181 	if (ba == NULL)
    182 		return 0;
    183 
    184 	thebits = ba->ctrl;
    185 	if (thebits & INTOUT) {
    186 		log(LOG_INFO, "grf4: got interrupt, ctrl=0x%4x\n", thebits);
    187 		/* clear int */
    188 		ba->ctrl = thebits & ~INTOUT;
    189 		return 1;
    190 	}
    191 	return 0;
    192 }
    193 
    194 /*
    195  * used to query the ulowell board to see if its alive.
    196  * for the moment, a NOP.
    197  */
    198 int
    199 ulowell_alive(mdp)
    200 	struct grfvideo_mode *mdp;
    201 {
    202 	return 1;
    203 }
    204 
    205 /*
    206  * Load the (mostly) ite support code and the default colormaps.
    207  */
    208 static void
    209 ul_load_code(gp)
    210 	struct grf_softc *gp;
    211 {
    212 	struct grf_ul_softc *gup;
    213 	struct gspregs *ba;
    214 	struct grfinfo *gi;
    215 	int i,j;
    216 #if 0
    217 	struct grf_colormap gcm;
    218 #endif
    219 
    220 	gup = (struct grf_ul_softc *)gp;
    221 	ba = (struct gspregs *)gp->g_regkva;
    222 	gi = &gp->g_display;
    223 
    224 	gi->gd_regaddr	= ztwopa((caddr_t)ba);
    225 	gi->gd_regsize	= sizeof(struct gspregs);
    226 	gi->gd_fbaddr	= NULL;
    227 	gi->gd_fbsize	= 0;
    228 	gi->gd_fbwidth	= 1024;
    229 	gi->gd_fbheight	= 1024;
    230 	gi->gd_colors	= 256;
    231 
    232 	ba->ctrl = (ba->ctrl & ~INCR) | (LBL | INCW);
    233 	ba->hstadrh = 0xC000;
    234 	ba->hstadrl = 0x0080;
    235 	ba->data = 0x0;		/* disable screen refresh and video output */
    236 	ba->data = 0xFFFC;	/* screen refresh base address */
    237 	ba->data = 0xFFFF;	/* no display int possible */
    238 	ba->data = 0x000C;	/* CAS before RAS refresh each 64 local clks */
    239 
    240 	ba->ctrl = (ba->ctrl & ~INCW) | LBL;
    241 	ba->hstadrh = 0xfe80;
    242 	ba->hstadrl = 0;
    243 	ba->data = 4;
    244 	ba->hstadrl = 0x20;
    245 	ba->data = 0xFF;	/* all color planes visible */
    246 
    247 	ba->hstadrl = 0;
    248 	ba->data = 5;
    249 	ba->hstadrl = 0x20;
    250 	ba->data = 0;		/* no color planes blinking */
    251 
    252 	ba->hstadrl = 0;
    253 	ba->data = 6;
    254 	ba->hstadrl = 0x20;
    255 	ba->data = gup->gus_ovslct = 0x43;
    256 	/* overlay visible, no overlay blinking, overlay color 0 transparent */
    257 
    258 	ba->hstadrl = 0;
    259 	ba->data = 7;
    260 	ba->hstadrl = 0x20;
    261 	ba->data = 0;		/* voodoo */
    262 
    263 	/* clear overlay planes */
    264 	ba->ctrl |= INCW;
    265 	ba->hstadrh = 0xff80;
    266 	ba->hstadrl = 0x0000;
    267 	for (i=0xff80000; i< 0xffa0000; ++i) {
    268 		ba->data = 0;
    269 	}
    270 
    271 	/* download tms code */
    272 
    273 	ba->ctrl = LBL | INCW | NMI | NMIM | HLT | CF;
    274 
    275 	printf("\ndownloading TMS code");
    276 	i=0;
    277 	while ((j = tmscode[i++])) {
    278 		printf(".");
    279 		ba->hstadrh = tmscode[i++];
    280 		ba->hstadrl = tmscode[i++];
    281 		while (j-- > 0) {
    282 			ba->data = tmscode[i++];
    283 		}
    284 	}
    285 
    286 	/* font info was uploaded in ite_ul.c(ite_ulinit). */
    287 
    288 #if 1
    289 	/* XXX load image palette with some initial values, slightly hacky */
    290 
    291 	ba->hstadrh = 0xfe80;
    292 	ba->hstadrl = 0x0000;
    293 	ba->ctrl |= INCW;
    294 	ba->data = 0;
    295 	ba->ctrl &= ~INCW;
    296 
    297 	for (i=0; i<16; ++i) {
    298 		ba->data = gup->gus_imcmap[i+  0] = ul_std_palette[i+ 0];
    299 		ba->data = gup->gus_imcmap[i+256] = ul_std_palette[i+16];
    300 		ba->data = gup->gus_imcmap[i+512] = ul_std_palette[i+32];
    301 	}
    302 
    303 	/*
    304 	 * XXX load shadow overlay palette with what the TMS code will load
    305 	 * into the real one some time after the TMS code is started below.
    306 	 * This might be considered a rude hack.
    307 	 */
    308 	bcopy(ul_ovl_palette, gup->gus_ovcmap, 3*4);
    309 
    310 	/*
    311 	 * Unflush cache, unhalt cpu -> nmi starts to run. This MUST NOT BE
    312 	 * DONE before the image color map initialization above, to guarantee
    313 	 * the index register in the BT458 is not used by more than one CPU
    314 	 * at once.
    315 	 *
    316 	 * XXX For the same reason, we'll have to rething ul_putcmap(). For
    317 	 * details, look at comment there.
    318 	 */
    319 	ba->ctrl &= ~(HLT|CF);
    320 
    321 #else
    322 	/*
    323 	 * XXX I wonder why this partially ever worked.
    324 	 *
    325 	 * This can't possibly work this way, as we are copyin()ing data in
    326 	 * ul_putcmap.
    327 	 *
    328 	 * I guess this partially worked because SFC happened to point to
    329 	 * to supervisor data space on 68030 machines coming from the old
    330 	 * boot loader.
    331 	 *
    332 	 * While this looks more correct than the hack in the other part of the
    333 	 * loop, we would have to do our own version of the loop through
    334 	 * colormap entries, set up command buffer, and call gsp_write(), or
    335 	 * factor out some code.
    336 	 */
    337 
    338 	/*
    339 	 * XXX This version will work for the overlay, if our queue codes
    340 	 * initial conditions are set at load time (not start time).
    341 	 * It further assumes that ul_putcmap only uses the
    342 	 * GRFIMDEV/GRFOVDEV bits of the dev parameter.
    343 	 */
    344 
    345 
    346 	/* unflush cache, unhalt cpu first -> nmi starts to run */
    347 	ba->ctrl &= ~(HLT|CF);
    348 
    349 	gcm.index = 0;
    350 	gcm.count = 16;
    351 	gcm.red   = ul_std_palette +  0;
    352 	gcm.green = ul_std_palette + 16;
    353 	gcm.blue  = ul_std_palette + 32;
    354 	ul_putcmap(gp, &gcm, GRFIMDEV);
    355 
    356 	gcm.index = 0;
    357 	gcm.count = 4;
    358 	gcm.red   = ul_ovl_palette + 0;
    359 	gcm.green = ul_ovl_palette + 4;
    360 	gcm.blue  = ul_ovl_palette + 8;
    361 	ul_putcmap(gp, &gcm, GRFOVDEV);
    362 #endif
    363 
    364 }
    365 
    366 static int
    367 ul_load_mon(gp, md)
    368 	struct grf_softc *gp;
    369 	struct grfvideo_mode *md;
    370 {
    371 	struct grf_ul_softc *gup;
    372 	struct grfinfo *gi;
    373 	struct gspregs *ba;
    374 	u_int16_t buf[8];
    375 
    376 	gup = (struct grf_ul_softc *)gp;
    377 	gi = &gp->g_display;
    378 	ba = (struct gspregs *)gp->g_regkva;
    379 
    380 	gi->gd_dyn.gdi_fbx	= 0;
    381 	gi->gd_dyn.gdi_fby	= 0;
    382 	gi->gd_dyn.gdi_dwidth	= md->disp_width;
    383 	gi->gd_dyn.gdi_dheight	= md->disp_height;
    384 	gi->gd_dyn.gdi_dx	= 0;
    385 	gi->gd_dyn.gdi_dy	= 0;
    386 
    387 	ba->ctrl = (ba->ctrl & ~INCR) | (LBL | INCW); /* XXX */
    388 
    389 	ba->hstadrh = 0xC000;
    390 	ba->hstadrl = 0x0000;
    391 	ba->data = md->hsync_stop;
    392 	ba->data = md->hblank_stop;
    393 	ba->data = md->hblank_start;
    394 	ba->data = md->hsync_start;
    395 	ba->data = md->vsync_stop;
    396 	ba->data = md->vblank_stop;
    397 	ba->data = md->vblank_start;
    398 	ba->data = md->vsync_start;
    399 
    400 	ba->ctrl &= ~INCW;
    401 	ba->hstadrh = 0xFE90;
    402 	ba->hstadrl = 0x0000;
    403 
    404 	if (abs(md->pixel_clock - ulowell_clock[0]) >
    405 	    abs(md->pixel_clock - ulowell_clock[1])) {
    406 
    407 		ba->data = (ba->data & 0xFC) | 2 | 1;
    408 		md->pixel_clock = ulowell_clock[1];
    409 
    410 	} else {
    411 		ba->data = (ba->data & 0xFC) | 2 | 0;
    412 		md->pixel_clock = ulowell_clock[0];
    413 	}
    414 
    415 	ba->ctrl |= LBL | INCW;
    416 	ba->hstadrh = 0xC000;
    417 	ba->hstadrl = 0x0080;
    418 	ba->data = (md->vblank_start - md->vblank_stop == md->disp_height ?
    419 	    0xf020 : 0xb020);
    420 
    421 	/* I guess this should be in the yet unimplemented mode select ioctl */
    422 	/* Hm.. maybe not. We always put the console on overlay plane no 0. */
    423 	/* Anyway, this _IS_ called in the mode select ioctl. */
    424 
    425 	/* ite support code parameters: */
    426 	buf[0] = GCMD_MCHG;
    427 	buf[1] = md->disp_width;	/* display width */
    428 	buf[2] = md->disp_height;	/* display height */
    429 	buf[3] = 0;			/* LSW of frame buffer origin */
    430 	buf[4] = 0xFF80;		/* MSW of frame buffer origin */
    431 	buf[5] = gi->gd_fbwidth * 1;	/* frame buffer pitch */
    432 	buf[6] = 1;			/* frame buffer depth */
    433 	gsp_write(ba, buf, 7);
    434 
    435 	return(1);
    436 }
    437 
    438 int ul_mode __P((struct grf_softc *, u_long, void *, u_long, int));
    439 
    440 void grfulattach __P((struct device *, struct device *, void *));
    441 int grfulprint __P((void *, char *));
    442 int grfulmatch __P((struct device *, void *, void *));
    443 
    444 struct cfattach grful_ca = {
    445 	sizeof(struct grf_ul_softc), grfulmatch, grfulattach
    446 };
    447 
    448 struct cfdriver grful_cd = {
    449 	NULL, "grful", DV_DULL, NULL, 0
    450 };
    451 
    452 /*
    453  * only used in console init
    454  */
    455 static struct cfdata *cfdata;
    456 
    457 /*
    458  * we make sure to only init things once.  this is somewhat
    459  * tricky regarding the console.
    460  */
    461 int
    462 grfulmatch(pdp, match, auxp)
    463 	struct device *pdp;
    464 	void *match, *auxp;
    465 {
    466 #ifdef ULOWELLCONSOLE
    467 	struct cfdata *cfp = match;
    468 	static int ulconunit = -1;
    469 #endif
    470 	struct zbus_args *zap;
    471 
    472 	zap = auxp;
    473 
    474 	/*
    475 	 * allow only one ulowell console
    476 	 */
    477         if (amiga_realconfig == 0)
    478 #ifdef ULOWELLCONSOLE
    479 		if (ulconunit != -1)
    480 #endif
    481 			return(0);
    482 
    483 	if (zap->manid != 1030 || zap->prodid != 0)
    484 		return(0);
    485 
    486 #ifdef ULOWELLCONSOLE
    487 	if (amiga_realconfig == 0 || ulconunit != cfp->cf_unit) {
    488 #endif
    489 		if ((unsigned)ulowell_default_mon > ulowell_mon_max)
    490 			ulowell_default_mon = 1;
    491 
    492 		current_mon = ul_monitor_defs + ulowell_default_mon - 1;
    493 		if (ulowell_alive(current_mon) == 0)
    494 			return(0);
    495 #ifdef ULOWELLCONSOLE
    496 		if (amiga_realconfig == 0) {
    497 			ulconunit = cfp->cf_unit;
    498 			cfdata = cfp;
    499 		}
    500 	}
    501 #endif
    502 	return(1);
    503 }
    504 
    505 /*
    506  * attach to the grfbus (zbus)
    507  */
    508 void
    509 grfulattach(pdp, dp, auxp)
    510 	struct device *pdp, *dp;
    511 	void *auxp;
    512 {
    513 	static struct grf_ul_softc congrf;
    514 	struct zbus_args *zap;
    515 	struct grf_softc *gp;
    516 	struct grf_ul_softc *gup;
    517 
    518 	zap = auxp;
    519 
    520 	if (dp == NULL)
    521 		gup = &congrf;
    522 	else
    523 		gup = (struct grf_ul_softc *)dp;
    524 
    525 	gp = &gup->gus_sc;
    526 
    527 	if (dp != NULL && congrf.gus_sc.g_regkva != 0) {
    528 		/*
    529 		 * inited earlier, just copy (not device struct)
    530 		 */
    531 		bcopy(&congrf.gus_sc.g_display, &gp->g_display,
    532 		    (char *)&gup->gus_isr - (char *)&gp->g_display);
    533 
    534 		/* ...and transfer the isr */
    535 		gup->gus_isr.isr_ipl = 2;
    536 		gup->gus_isr.isr_intr = ulisr;
    537 		gup->gus_isr.isr_arg = (void *)gp;
    538 		/*
    539 		 * To make sure ints are always catched, first add new isr
    540 		 * then remove old:
    541 		 */
    542 		add_isr(&gup->gus_isr);
    543 		remove_isr(&congrf.gus_isr);
    544 	} else {
    545 		gp->g_regkva = (caddr_t)zap->va;
    546 		gp->g_fbkva = NULL;
    547 		gp->g_unit = GRF_ULOWELL_UNIT;
    548 		gp->g_flags = GF_ALIVE;
    549 		gp->g_mode = ul_mode;
    550 		gp->g_conpri = grful_cnprobe();
    551 		gp->g_data = NULL;
    552 
    553 		gup->gus_isr.isr_ipl = 2;
    554 		gup->gus_isr.isr_intr = ulisr;
    555 		gup->gus_isr.isr_arg = (void *)gp;
    556 		add_isr(&gup->gus_isr);
    557 
    558 		(void)ul_load_code(gp);
    559 		(void)ul_load_mon(gp, current_mon);
    560 		grful_iteinit(gp);
    561 	}
    562 	if (dp != NULL)
    563 		printf("\n");
    564 	/*
    565 	 * attach grf
    566 	 */
    567 	amiga_config_found(cfdata, &gp->g_device, gp, grfulprint);
    568 }
    569 
    570 int
    571 grfulprint(auxp, pnp)
    572 	void *auxp;
    573 	char *pnp;
    574 {
    575 	if (pnp)
    576 		printf("grf%d at %s", ((struct grf_softc *)auxp)->g_unit,
    577 			pnp);
    578 	return(UNCONF);
    579 }
    580 
    581 static int
    582 ul_getvmode (gp, vm)
    583 	struct grf_softc *gp;
    584 	struct grfvideo_mode *vm;
    585 {
    586 	struct grfvideo_mode *md;
    587 
    588 	if (vm->mode_num && vm->mode_num > ulowell_mon_max)
    589 		return EINVAL;
    590 
    591 	if (! vm->mode_num)
    592 		vm->mode_num = current_mon - ul_monitor_defs + 1;
    593 
    594 	md = ul_monitor_defs + vm->mode_num - 1;
    595 	strncpy (vm->mode_descr, md->mode_descr,
    596 		sizeof (vm->mode_descr));
    597 
    598 	/* XXX should tell TMS to measure it */
    599 	vm->pixel_clock  = md->pixel_clock;
    600 	vm->disp_width   = md->disp_width;
    601 	vm->disp_height  = md->disp_height;
    602 	vm->depth        = md->depth;
    603 
    604 	vm->hblank_start = (md->hblank_start - md->hblank_stop) * 16;
    605 	vm->hblank_stop  = (md->htotal - 1) * 16;
    606 	vm->hsync_start  = (md->hsync_start  - md->hblank_stop) * 16;
    607 	vm->hsync_stop   = (md->hsync_stop + md->htotal - md->hblank_stop) * 16;
    608 	vm->htotal       = md->htotal * 16;
    609 
    610 	vm->vblank_start = md->vblank_start - md->vblank_stop;
    611 	vm->vblank_stop  = md->vtotal - 1;
    612 	vm->vsync_start  = md->vsync_start - md->vblank_stop;
    613 	vm->vsync_stop   = md->vsync_stop + md->vtotal - md->vblank_stop;
    614 	vm->vtotal       = md->vtotal;
    615 
    616 	return 0;
    617 }
    618 
    619 
    620 static int
    621 ul_setvmode (gp, mode)
    622 	struct grf_softc *gp;
    623 	unsigned mode;
    624 {
    625 	struct grf_ul_softc *gup;
    626 	struct gspregs *ba;
    627 	int error;
    628 
    629 	if (!mode || mode > ulowell_mon_max)
    630 		return EINVAL;
    631 
    632 	ba = (struct gspregs *)gp->g_regkva;
    633 	gup = (struct grf_ul_softc *)gp;
    634 	current_mon = ul_monitor_defs + mode - 1;
    635 
    636 	error = ul_load_mon (gp, current_mon) ? 0 : EINVAL;
    637 
    638 	return error;
    639 }
    640 
    641 /*
    642  * Set the frame buffer or overlay planes on or off.
    643  * Always succeeds.
    644  */
    645 
    646 static __inline void
    647 ul_setfb(gp, cmd)
    648 	struct grf_softc *gp;
    649 	u_long cmd;
    650 {
    651 	struct grf_ul_softc *gup;
    652 	struct gspregs *ba;
    653 
    654 	gup = (struct grf_ul_softc *)gp;
    655 
    656 	ba = (struct gspregs *)gp->g_regkva;
    657 	ba->ctrl = LBL;
    658 	ba->hstadrh = 0xfe80;
    659 	ba->hstadrl = 0x0000;
    660 	ba->data = 6;
    661 	ba->hstadrl = 0x0020;
    662 
    663 	switch (cmd) {
    664 	case GM_GRFON:
    665 		gup->gus_ovslct |= 0x40;
    666 		break;
    667 	case GM_GRFOFF:
    668 		gup->gus_ovslct &= ~0x40;
    669 		break;
    670 	case GM_GRFOVON:
    671 		gup->gus_ovslct |= 3;
    672 		break;
    673 	case GM_GRFOVOFF:
    674 		gup->gus_ovslct &= ~3;
    675 		break;
    676 	}
    677 	ba->data = gup->gus_ovslct;
    678 }
    679 
    680 /*
    681  * Change the mode of the display.
    682  * Return a UNIX error number or 0 for success.
    683  */
    684 int
    685 ul_mode(gp, cmd, arg, a2, a3)
    686 	struct grf_softc *gp;
    687 	u_long cmd;
    688 	void *arg;
    689 	u_long a2;
    690 	int a3;
    691 {
    692 	int i;
    693 	struct grfdyninfo *gd;
    694 
    695 	switch (cmd) {
    696 	case GM_GRFON:
    697 	case GM_GRFOFF:
    698 	case GM_GRFOVON:
    699 	case GM_GRFOVOFF:
    700 		ul_setfb (gp, cmd);
    701 		return 0;
    702 
    703 	case GM_GRFCONFIG:
    704 		gd = (struct grfdyninfo *)arg;
    705 		for (i=0; i<ulowell_mon_max; ++i) {
    706 			if (ul_monitor_defs[i].disp_width == gd->gdi_dwidth &&
    707 			    ul_monitor_defs[i].disp_height == gd->gdi_dheight)
    708 				return ul_setvmode(gp, i+1);
    709 		}
    710 		return EINVAL;
    711 
    712 	case GM_GRFGETVMODE:
    713 		return ul_getvmode (gp, (struct grfvideo_mode *) arg);
    714 
    715 	case GM_GRFSETVMODE:
    716 		return ul_setvmode (gp, *(unsigned *) arg);
    717 
    718 	case GM_GRFGETNUMVM:
    719 		*(int *)arg = ulowell_mon_max;
    720 		return 0;
    721 
    722 	case GM_GRFIOCTL:
    723 		return ul_ioctl (gp, a2, arg, (dev_t)a3);
    724 
    725 	default:
    726 		break;
    727 	}
    728 
    729 	return EINVAL;
    730 }
    731 
    732 int
    733 ul_ioctl (gp, cmd, data, dev)
    734 	register struct grf_softc *gp;
    735 	u_long cmd;
    736 	void *data;
    737 	dev_t dev;
    738 {
    739 	switch (cmd) {
    740 #if 0
    741 	/*
    742 	 * XXX we have no hardware sprites, but might implement them
    743 	 * later in TMS code.
    744 	 */
    745 
    746 	case GRFIOCGSPRITEPOS:
    747 		return ul_getspritepos (gp, (struct grf_position *) data);
    748 
    749 	case GRFIOCSSPRITEPOS:
    750 		return ul_setspritepos (gp, (struct grf_position *) data);
    751 
    752 	case GRFIOCSSPRITEINF:
    753 		return ul_setspriteinfo (gp, (struct grf_spriteinfo *) data);
    754 
    755 	case GRFIOCGSPRITEINF:
    756 		return ul_getspriteinfo (gp, (struct grf_spriteinfo *) data);
    757 
    758 	case GRFIOCGSPRITEMAX:
    759 		return ul_getspritemax (gp, (struct grf_position *) data);
    760 
    761 #endif
    762 
    763 	case GRFIOCGETCMAP:
    764 		return ul_getcmap (gp, (struct grf_colormap *) data, dev);
    765 
    766 	case GRFIOCPUTCMAP:
    767 		return ul_putcmap (gp, (struct grf_colormap *) data, dev);
    768 
    769 	case GRFIOCBITBLT:
    770 		return ul_bitblt (gp, (struct grf_bitblt *) data, dev);
    771 
    772 	case GRFIOCBLANK:
    773 		return ul_blank (gp, (int *) data, dev);
    774 	}
    775 
    776 	return EINVAL;
    777 }
    778 
    779 int
    780 ul_getcmap (gp, cmap, dev)
    781 	struct grf_softc *gp;
    782 	struct grf_colormap *cmap;
    783 	dev_t dev;
    784 {
    785 	struct grf_ul_softc *gup;
    786 	u_int8_t *mymap;
    787 	int mxidx, error;
    788 
    789 	gup = (struct grf_ul_softc *)gp;
    790 
    791 	if (minor(dev) & GRFIMDEV) {
    792 		mxidx = 256;
    793 		mymap = gup->gus_imcmap;
    794 	} else {
    795 		mxidx = 4;
    796 		mymap = gup->gus_ovcmap;
    797 	}
    798 
    799 	if (cmap->count == 0 || cmap->index >= mxidx)
    800 		return 0;
    801 
    802 	if (cmap->index + cmap->count > mxidx)
    803 		cmap->count = mxidx - cmap->index;
    804 
    805 	/* just copyout from the shadow color map */
    806 
    807 	if ((error = copyout(mymap + cmap->index, cmap->red, cmap->count))
    808 
    809 	    || (error = copyout(mymap + mxidx + cmap->index, cmap->green,
    810 		cmap->count))
    811 
    812 	    || (error = copyout(mymap + mxidx * 2 + cmap->index, cmap->blue,
    813 		cmap->count)))
    814 
    815 		return(error);
    816 
    817 	return(0);
    818 }
    819 
    820 int
    821 ul_putcmap (gp, cmap, dev)
    822 	struct grf_softc *gp;
    823 	struct grf_colormap *cmap;
    824 	dev_t dev;
    825 {
    826 	struct grf_ul_softc *gup;
    827 	struct gspregs *ba;
    828 	u_int16_t cmd[8];
    829 	int x, mxidx, error;
    830 	u_int8_t *mymap;
    831 
    832 	gup = (struct grf_ul_softc *)gp;
    833 
    834 	if (minor(dev) & GRFIMDEV) {
    835 		mxidx = 256;
    836 		mymap = gup->gus_imcmap;
    837 	} else {
    838 		mxidx = 4;
    839 		mymap = gup->gus_ovcmap;
    840 	}
    841 
    842 	if (cmap->count == 0 || cmap->index >= mxidx)
    843 		return 0;
    844 
    845 	if (cmap->index + cmap->count > mxidx)
    846 		cmap->count = mxidx - cmap->index;
    847 
    848 	/* first copyin to our shadow color map */
    849 
    850 	if ((error = copyin(cmap->red, mymap + cmap->index, cmap->count))
    851 
    852 	    || (error = copyin(cmap->green, mymap + cmap->index + mxidx,
    853 		cmap->count))
    854 
    855 	    || (error = copyin(cmap->blue,  mymap + cmap->index + mxidx*2,
    856 		cmap->count)))
    857 
    858 		return error;
    859 
    860 
    861 	/* then write from there to the hardware */
    862 	ba = (struct gspregs *)gp->g_regkva;
    863 	/*
    864 	 * XXX This is a bad thing to do.
    865 	 * We should always use the gsp call, or have a means to arbitrate
    866 	 * the usage of the BT458 index register. Else there might be a
    867 	 * race condition (when writing both colormaps at nearly the same
    868 	 * time), where one CPU changes the index register when the other
    869 	 * one has not finished using it.
    870 	 */
    871 	if (mxidx > 4) {
    872 		/* image color map: we can write, with a hack, directly */
    873 		ba->ctrl = LBL;
    874 		ba->hstadrh = 0xfe80;
    875 		ba->hstadrl = 0x0000;
    876 		ba->ctrl |= INCW;
    877 		ba->data = cmap->index;
    878 		ba->ctrl &= ~INCW;
    879 
    880 		for (x=cmap->index; x < cmap->index + cmap->count; ++x) {
    881 			ba->data = (u_int16_t) mymap[x];
    882 			ba->data = (u_int16_t) mymap[x + mxidx];
    883 			ba->data = (u_int16_t) mymap[x + mxidx * 2];
    884 		}
    885 	} else {
    886 
    887 		/* overlay planes color map: have to call tms to do it */
    888 		cmd[0] = GCMD_CMAP;
    889 		cmd[1] = 1;
    890 		for (x=cmap->index; x < cmap->index + cmap->count; ++x) {
    891 			cmd[2] = x;
    892 			cmd[3] = mymap[x];
    893 			cmd[4] = mymap[x + mxidx];
    894 			cmd[5] = mymap[x + mxidx * 2];
    895 			gsp_write(ba, cmd, 6);
    896 		}
    897 	}
    898 	return 0;
    899 }
    900 
    901 int
    902 ul_blank(gp, onoff, dev)
    903 	struct grf_softc *gp;
    904 	int *onoff;
    905 	dev_t dev;
    906 {
    907 	struct gspregs *gsp;
    908 
    909 	gsp = (struct gspregs *)gp->g_regkva;
    910 	gsp->ctrl = (gsp->ctrl & ~(INCR | INCW)) | LBL;
    911 	gsp->hstadrh = 0xC000;
    912 	gsp->hstadrl = 0x0080;
    913 	if (*onoff)
    914 		gsp->data |= 0x9000;
    915 	else
    916 		gsp->data &= ~0x9000;
    917 
    918 	return 0;
    919 }
    920 /*
    921  * !!! THIS AREA UNDER CONSTRUCTION !!!
    922  */
    923 int ul_BltOpMap[16] = {
    924 	3, 1, 2, 0, 11,  9, 10, 8,
    925 	7, 5, 6, 4, 15, 13, 14, 12
    926 };
    927 
    928 int
    929 ul_bitblt (gp, bb, dev)
    930 	struct grf_softc *gp;
    931 	struct grf_bitblt *bb;
    932 	dev_t dev;
    933 {
    934 	/* XXX not yet implemented, but pretty trivial */
    935 	return EINVAL;
    936 }
    937 
    938 void
    939 gsp_write(gsp, ptr, size)
    940 	struct gspregs *gsp;
    941 	u_short *ptr;
    942 	size_t size;
    943 {
    944 	u_short put, new_put, next, oc;
    945 	u_long put_hi, oa;
    946 	size_t n;
    947 
    948 	if (size == 0 || size > 8)
    949 	        return;
    950 
    951 	n = size;
    952 
    953 	oc = gsp->ctrl;
    954 	oa = GSPGETHADRS(gsp);
    955 
    956 	gsp->ctrl = (oc & ~INCR) | LBL | INCW;
    957 	GSPSETHADRS(gsp, GSP_MODE_ADRS);
    958 	gsp->data &= ~GMODE_FLUSH;
    959 
    960 	GSPSETHADRS(gsp, PUT_HI_PTR_ADRS);
    961 	put_hi = gsp->data << 16;
    962 
    963 	GSPSETHADRS(gsp, PUT_PTR_ADRS);
    964 	put = gsp->data;
    965 	new_put = put + (8<<4);
    966 
    967 	GSPSETHADRS(gsp, GET_PTR_ADRS);
    968 	next = gsp->data;
    969 
    970 	while (next == new_put) {
    971 		/*
    972 		 * we should use an intr. here. unfortunately, we already
    973 		 * are called from an interupt and can't use tsleep.
    974 		 * so we do busy waiting, at least for the moment.
    975 		 */
    976 
    977 		GSPSETHADRS(gsp,GET_PTR_ADRS);
    978 		next = gsp->data;
    979 	}
    980 
    981 	GSPSETHADRS(gsp,put|put_hi);
    982 	gsp->data = *ptr++ | 8<<4;
    983 	while ( --n > 0) {
    984 		gsp->data = *ptr++;
    985 	}
    986 
    987 	GSPSETHADRS(gsp,PUT_PTR_ADRS);
    988 	gsp->data = new_put;
    989 	GSPSETHADRS(gsp,oa);
    990 	gsp->ctrl = oc;
    991 
    992 	return;
    993 }
    994 
    995 #endif	/* NGRF */
    996