xref: /netbsd-src/sys/arch/sparc/dev/cgeight.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: cgeight.c,v 1.11 1996/10/13 02:59:33 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Jason R. Thorpe.  All rights reserved.
5  * Copyright (c) 1995 Theo de Raadt
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This software was developed by the Computer Systems Engineering group
10  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11  * contributed to Berkeley.
12  *
13  * All advertising materials mentioning features or use of this software
14  * must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Lawrence Berkeley Laboratory.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. All advertising materials mentioning features or use of this software
27  *    must display the following acknowledgement:
28  *	This product includes software developed by the University of
29  *	California, Berkeley and its contributors.
30  * 4. Neither the name of the University nor the names of its contributors
31  *    may be used to endorse or promote products derived from this software
32  *    without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  *
46  *	from @(#)cgthree.c	8.2 (Berkeley) 10/30/93
47  */
48 
49 /*
50  * color display (cgeight) driver.
51  *
52  * Does not handle interrupts, even though they can occur.
53  *
54  * XXX should defer colormap updates to vertical retrace interrupts
55  */
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/buf.h>
60 #include <sys/device.h>
61 #include <sys/ioctl.h>
62 #include <sys/malloc.h>
63 #include <sys/mman.h>
64 #include <sys/tty.h>
65 #include <sys/conf.h>
66 
67 #include <vm/vm.h>
68 
69 #include <machine/fbio.h>
70 #include <machine/autoconf.h>
71 #include <machine/pmap.h>
72 #include <machine/fbvar.h>
73 #include <machine/eeprom.h>
74 #include <machine/conf.h>
75 
76 #include <sparc/dev/btreg.h>
77 #include <sparc/dev/btvar.h>
78 #include <sparc/dev/pfourreg.h>
79 
80 /* per-display variables */
81 struct cgeight_softc {
82 	struct	device sc_dev;		/* base device */
83 	struct	fbdevice sc_fb;		/* frame buffer device */
84 	struct rom_reg	sc_phys;	/* display RAM (phys addr) */
85 	volatile struct fbcontrol *sc_fbc;	/* Brooktree registers */
86 	int	sc_bustype;		/* type of bus we live on */
87 	union	bt_cmap sc_cmap;	/* Brooktree color map */
88 };
89 
90 /* autoconfiguration driver */
91 static void	cgeightattach(struct device *, struct device *, void *);
92 static int	cgeightmatch(struct device *, void *, void *);
93 #if defined(SUN4)
94 static void	cgeightunblank __P((struct device *));
95 #endif
96 
97 /* cdevsw prototypes */
98 cdev_decl(cgeight);
99 
100 struct cfattach cgeight_ca = {
101 	sizeof(struct cgeight_softc), cgeightmatch, cgeightattach
102 };
103 
104 struct cfdriver cgeight_cd = {
105 	NULL, "cgeight", DV_DULL
106 };
107 
108 #if defined(SUN4)
109 /* frame buffer generic driver */
110 static struct fbdriver cgeightfbdriver = {
111 	cgeightunblank, cgeightopen, cgeightclose, cgeightioctl,
112 	cgeightpoll, cgeightmmap
113 };
114 
115 extern int fbnode;
116 extern struct tty *fbconstty;
117 
118 static void cgeightloadcmap __P((struct cgeight_softc *, int, int));
119 static int cgeight_get_video __P((struct cgeight_softc *));
120 static void cgeight_set_video __P((struct cgeight_softc *, int));
121 #endif
122 
123 /*
124  * Match a cgeight.
125  */
126 int
127 cgeightmatch(parent, vcf, aux)
128 	struct device *parent;
129 	void *vcf, *aux;
130 {
131 	struct cfdata *cf = vcf;
132 	struct confargs *ca = aux;
133 	struct romaux *ra = &ca->ca_ra;
134 
135 	if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
136 		return (0);
137 
138 	/*
139 	 * Mask out invalid flags from the user.
140 	 */
141 	cf->cf_flags &= FB_USERMASK;
142 
143 	/*
144 	 * Only exists on a sun4.
145 	 */
146 	if (!CPU_ISSUN4)
147 		return (0);
148 
149 	/*
150 	 * Only exists on obio.
151 	 */
152 	if (ca->ca_bustype != BUS_OBIO)
153 		return (0);
154 
155 	/*
156 	 * Make sure there's hardware there.
157 	 */
158 	if (probeget(ra->ra_vaddr, 4) == -1)
159 		return (0);
160 
161 #if defined(SUN4)
162 	/*
163 	 * Check the pfour register.
164 	 */
165 	if (fb_pfour_id(ra->ra_vaddr) == PFOUR_ID_COLOR24) {
166 		cf->cf_flags |= FB_PFOUR;
167 		return (1);
168 	}
169 #endif
170 
171 	return (0);
172 }
173 
174 /*
175  * Attach a display.  We need to notice if it is the console, too.
176  */
177 void
178 cgeightattach(parent, self, args)
179 	struct device *parent, *self;
180 	void *args;
181 {
182 #if defined(SUN4)
183 	register struct cgeight_softc *sc = (struct cgeight_softc *)self;
184 	register struct confargs *ca = args;
185 	register int node = 0, ramsize, i;
186 	register volatile struct bt_regs *bt;
187 	struct fbdevice *fb = &sc->sc_fb;
188 	int isconsole;
189 
190 	fb->fb_driver = &cgeightfbdriver;
191 	fb->fb_device = &sc->sc_dev;
192 	fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
193 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
194 
195 	/*
196 	 * Only pfour cgfours, thank you...
197 	 */
198 	if ((ca->ca_bustype != BUS_OBIO) ||
199 	    ((fb->fb_flags & FB_PFOUR) == 0)) {
200 		printf("%s: ignoring; not a pfour\n", sc->sc_dev.dv_xname);
201 		return;
202 	}
203 
204 	/* Map the pfour register. */
205 	fb->fb_pfour = (volatile u_int32_t *)mapiodev(ca->ca_ra.ra_reg, 0,
206 	    sizeof(u_int32_t), ca->ca_bustype);
207 
208 	ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
209 
210 	fb->fb_type.fb_depth = 24;
211 	fb_setsize(fb, fb->fb_type.fb_depth, 1152, 900, node, ca->ca_bustype);
212 
213 	sc->sc_fb.fb_type.fb_cmsize = 256;
214 	sc->sc_fb.fb_type.fb_size = ramsize;
215 	printf(": cgeight/p4, %d x %d", fb->fb_type.fb_width,
216 	    fb->fb_type.fb_height);
217 
218 	isconsole = 0;
219 
220 	if (cputyp == CPU_SUN4) {
221 		struct eeprom *eep = (struct eeprom *)eeprom_va;
222 
223 		/*
224 		 * Assume this is the console if there's no eeprom info
225 		 * to be found.
226 		 */
227 		if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
228 			isconsole = (fbconstty != NULL);
229 	}
230 
231 #if 0
232 	/*
233 	 * We don't do any of the console handling here.  Instead,
234 	 * we let the bwtwo driver pick up the overlay plane and
235 	 * use it instead.  Rconsole should have better performance
236 	 * with the 1-bit depth.
237 	 *      -- Jason R. Thorpe <thorpej@NetBSD.ORG>
238 	 */
239 
240 	/*
241 	 * When the ROM has mapped in a cgfour display, the address
242 	 * maps only the video RAM, so in any case we have to map the
243 	 * registers ourselves.  We only need the video RAM if we are
244 	 * going to print characters via rconsole.
245 	 */
246 
247 	if (isconsole) {
248 		/* XXX this is kind of a waste */
249 		fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
250 		    PFOUR_COLOR_OFF_OVERLAY, ramsize, ca->ca_bustype);
251 	}
252 #endif
253 
254 	/* Map the Brooktree. */
255 	sc->sc_fbc = (volatile struct fbcontrol *)mapiodev(ca->ca_ra.ra_reg,
256 	    PFOUR_COLOR_OFF_CMAP, sizeof(struct fbcontrol), ca->ca_bustype);
257 
258 	sc->sc_phys = ca->ca_ra.ra_reg[0];
259 	sc->sc_bustype = ca->ca_bustype;
260 
261 #if 0	/* XXX thorpej ??? */
262 	/* tell the enable plane to look at the mono image */
263 	memset(ca->ca_ra.ra_vaddr, 0xff,
264 	    sc->sc_fb.fb_type.fb_width * sc->sc_fb.fb_type.fb_height / 8);
265 #endif
266 
267 	/* grab initial (current) color map */
268 	bt = &sc->sc_fbc->fbc_dac;
269 	bt->bt_addr = 0;
270 	for (i = 0; i < 256 * 3 / 4; i++)
271 		sc->sc_cmap.cm_chip[i] = bt->bt_cmap;
272 
273 	BT_INIT(bt, 0);
274 
275 #if 0	/* see above */
276 	if (isconsole) {
277 		printf(" (console)\n");
278 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
279 		fbrcons_init(fb);
280 #endif
281 	} else
282 #endif /* 0 */
283 		printf("\n");
284 
285 	/*
286 	 * Even though we're not using rconsole, we'd still like
287 	 * to notice if we're the console framebuffer.
288 	 */
289 	fb_attach(&sc->sc_fb, isconsole);
290 #endif
291 }
292 
293 int
294 cgeightopen(dev, flags, mode, p)
295 	dev_t dev;
296 	int flags, mode;
297 	struct proc *p;
298 {
299 	int unit = minor(dev);
300 
301 	if (unit >= cgeight_cd.cd_ndevs || cgeight_cd.cd_devs[unit] == NULL)
302 		return (ENXIO);
303 	return (0);
304 }
305 
306 int
307 cgeightclose(dev, flags, mode, p)
308 	dev_t dev;
309 	int flags, mode;
310 	struct proc *p;
311 {
312 
313 	return (0);
314 }
315 
316 int
317 cgeightioctl(dev, cmd, data, flags, p)
318 	dev_t dev;
319 	u_long cmd;
320 	register caddr_t data;
321 	int flags;
322 	struct proc *p;
323 {
324 #if defined(SUN4)
325 	register struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
326 	register struct fbgattr *fba;
327 	int error;
328 
329 	switch (cmd) {
330 
331 	case FBIOGTYPE:
332 		*(struct fbtype *)data = sc->sc_fb.fb_type;
333 		break;
334 
335 	case FBIOGATTR:
336 		fba = (struct fbgattr *)data;
337 		fba->real_type = sc->sc_fb.fb_type.fb_type;
338 		fba->owner = 0;		/* XXX ??? */
339 		fba->fbtype = sc->sc_fb.fb_type;
340 		fba->sattr.flags = 0;
341 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
342 		fba->sattr.dev_specific[0] = -1;
343 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
344 		fba->emu_types[1] = -1;
345 		break;
346 
347 	case FBIOGETCMAP:
348 		return (bt_getcmap((struct fbcmap *)data, &sc->sc_cmap, 256));
349 
350 	case FBIOPUTCMAP:
351 		/* copy to software map */
352 #define p ((struct fbcmap *)data)
353 		error = bt_putcmap(p, &sc->sc_cmap, 256);
354 		if (error)
355 			return (error);
356 		/* now blast them into the chip */
357 		/* XXX should use retrace interrupt */
358 		cgeightloadcmap(sc, p->index, p->count);
359 #undef p
360 		break;
361 
362 	case FBIOGVIDEO:
363 		*(int *)data = cgeight_get_video(sc);
364 		break;
365 
366 	case FBIOSVIDEO:
367 		cgeight_set_video(sc, *(int *)data);
368 		break;
369 
370 	default:
371 		return (ENOTTY);
372 	}
373 #endif
374 	return (0);
375 }
376 
377 int
378 cgeightpoll(dev, events, p)
379 	dev_t dev;
380 	int events;
381 	struct proc *p;
382 {
383 
384 	return (seltrue(dev, events, p));
385 }
386 
387 /*
388  * Return the address that would map the given device at the given
389  * offset, allowing for the given protection, or return -1 for error.
390  *
391  * The cg8 maps it's overlay plane at 0 for 128K, followed by the
392  * enable plane for 128K, followed by the colour for as long as it
393  * goes. Starting at 8MB, it maps the ramdac for NBPG, then the p4
394  * register for NBPG, then the bootrom for 0x40000.
395  */
396 int
397 cgeightmmap(dev, off, prot)
398 	dev_t dev;
399 	int off, prot;
400 {
401 	register struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
402 	int poff;
403 
404 #define START_ENABLE	(128*1024)
405 #define START_COLOR	((128*1024) + (128*1024))
406 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
407 			    sc->sc_fb.fb_type.fb_height * 3)
408 #define END_COLOR	(START_COLOR + COLOR_SIZE)
409 #define START_SPECIAL	0x800000
410 #define PROMSIZE	0x40000
411 #define NOOVERLAY	(0x04000000)
412 
413 	if (off & PGOFSET)
414 		panic("cgeightmap");
415 
416 	if ((u_int)off >= NOOVERLAY) {
417 		off -= NOOVERLAY;
418 
419 		/*
420 		 * X11 maps a huge chunk of the frame buffer; far more than
421 		 * there really is. We compensate by double-mapping the
422 		 * first page for as many other pages as it wants
423 		 */
424 		while (off >= COLOR_SIZE)
425 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
426 
427 		poff = off + PFOUR_COLOR_OFF_COLOR;
428 	} else if ((u_int)off < START_ENABLE) {
429 		/*
430 		 * in overlay plane
431 		 */
432 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
433 	} else if ((u_int)off < START_COLOR) {
434 		/*
435 		 * in enable plane
436 		 */
437 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
438 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
439 		/*
440 		 * in colour plane
441 		 */
442 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
443 	} else if ((u_int)off < START_SPECIAL) {
444 		/*
445 		 * hole
446 		 */
447 		poff = 0;	/* XXX */
448 	} else if ((u_int)off == START_SPECIAL) {
449 		/*
450 		 * colour map (Brooktree)
451 		 */
452 		poff = PFOUR_COLOR_OFF_CMAP;
453 	} else if ((u_int)off == START_SPECIAL + NBPG) {
454 		/*
455 		 * p4 register
456 		 */
457 		poff = 0;
458 	} else if ((u_int)off > (START_SPECIAL + (NBPG * 2)) &&
459 	    (u_int) off < (START_SPECIAL + (NBPG * 2) + PROMSIZE)) {
460 		/*
461 		 * rom
462 		 */
463 		poff = 0x8000 + (off - (START_SPECIAL + (NBPG * 2)));
464 	} else
465 		return (-1);
466 	/*
467 	 * I turned on PMAP_NC here to disable the cache as I was
468 	 * getting horribly broken behaviour with it on.
469 	 */
470 	return (REG2PHYS(&sc->sc_phys, poff, sc->sc_bustype) | PMAP_NC);
471 }
472 
473 #if defined(SUN4)
474 /*
475  * Undo the effect of an FBIOSVIDEO that turns the video off.
476  */
477 static void
478 cgeightunblank(dev)
479 	struct device *dev;
480 {
481 
482 	cgeight_set_video((struct cgeight_softc *)dev, 1);
483 }
484 
485 static int
486 cgeight_get_video(sc)
487 	struct cgeight_softc *sc;
488 {
489 
490 	return (fb_pfour_get_video(&sc->sc_fb));
491 }
492 
493 static void
494 cgeight_set_video(sc, enable)
495 	struct cgeight_softc *sc;
496 	int enable;
497 {
498 
499 	fb_pfour_set_video(&sc->sc_fb, enable);
500 }
501 
502 /*
503  * Load a subset of the current (new) colormap into the Brooktree DAC.
504  */
505 static void
506 cgeightloadcmap(sc, start, ncolors)
507 	register struct cgeight_softc *sc;
508 	register int start, ncolors;
509 {
510 	register volatile struct bt_regs *bt;
511 	register u_int *ip;
512 	register int count;
513 
514 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
515 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
516 	bt = &sc->sc_fbc->fbc_dac;
517 	bt->bt_addr = BT_D4M4(start);
518 	while (--count >= 0)
519 		bt->bt_cmap = *ip++;
520 }
521 #endif
522