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