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