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