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