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