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