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