xref: /netbsd-src/sys/arch/sparc/dev/cgeight.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: cgeight.c,v 1.13 1997/05/24 20:16:04 pk 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 *, struct cfdata *, 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, cf, aux)
128 	struct device *parent;
129 	struct cfdata *cf;
130 	void *aux;
131 {
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 *)
206 	    mapiodev(ca->ca_ra.ra_reg, 0, sizeof(u_int32_t));
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);
251 	}
252 #endif
253 
254 	/* Map the Brooktree. */
255 	sc->sc_fbc = (volatile struct fbcontrol *)
256 	    mapiodev(ca->ca_ra.ra_reg,
257 		     PFOUR_COLOR_OFF_CMAP, sizeof(struct fbcontrol));
258 
259 	sc->sc_phys = ca->ca_ra.ra_reg[0];
260 	sc->sc_bustype = ca->ca_bustype;
261 
262 #if 0	/* XXX thorpej ??? */
263 	/* tell the enable plane to look at the mono image */
264 	memset(ca->ca_ra.ra_vaddr, 0xff,
265 	    sc->sc_fb.fb_type.fb_width * sc->sc_fb.fb_type.fb_height / 8);
266 #endif
267 
268 	/* grab initial (current) color map */
269 	bt = &sc->sc_fbc->fbc_dac;
270 	bt->bt_addr = 0;
271 	for (i = 0; i < 256 * 3 / 4; i++)
272 		sc->sc_cmap.cm_chip[i] = bt->bt_cmap;
273 
274 	BT_INIT(bt, 0);
275 
276 #if 0	/* see above */
277 	if (isconsole) {
278 		printf(" (console)\n");
279 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
280 		fbrcons_init(fb);
281 #endif
282 	} else
283 #endif /* 0 */
284 		printf("\n");
285 
286 	/*
287 	 * Even though we're not using rconsole, we'd still like
288 	 * to notice if we're the console framebuffer.
289 	 */
290 	fb_attach(&sc->sc_fb, isconsole);
291 #endif
292 }
293 
294 int
295 cgeightopen(dev, flags, mode, p)
296 	dev_t dev;
297 	int flags, mode;
298 	struct proc *p;
299 {
300 	int unit = minor(dev);
301 
302 	if (unit >= cgeight_cd.cd_ndevs || cgeight_cd.cd_devs[unit] == NULL)
303 		return (ENXIO);
304 	return (0);
305 }
306 
307 int
308 cgeightclose(dev, flags, mode, p)
309 	dev_t dev;
310 	int flags, mode;
311 	struct proc *p;
312 {
313 
314 	return (0);
315 }
316 
317 int
318 cgeightioctl(dev, cmd, data, flags, p)
319 	dev_t dev;
320 	u_long cmd;
321 	register caddr_t data;
322 	int flags;
323 	struct proc *p;
324 {
325 #if defined(SUN4)
326 	register struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
327 	register struct fbgattr *fba;
328 	int error;
329 
330 	switch (cmd) {
331 
332 	case FBIOGTYPE:
333 		*(struct fbtype *)data = sc->sc_fb.fb_type;
334 		break;
335 
336 	case FBIOGATTR:
337 		fba = (struct fbgattr *)data;
338 		fba->real_type = sc->sc_fb.fb_type.fb_type;
339 		fba->owner = 0;		/* XXX ??? */
340 		fba->fbtype = sc->sc_fb.fb_type;
341 		fba->sattr.flags = 0;
342 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
343 		fba->sattr.dev_specific[0] = -1;
344 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
345 		fba->emu_types[1] = -1;
346 		break;
347 
348 	case FBIOGETCMAP:
349 		return (bt_getcmap((struct fbcmap *)data, &sc->sc_cmap, 256));
350 
351 	case FBIOPUTCMAP:
352 		/* copy to software map */
353 #define p ((struct fbcmap *)data)
354 		error = bt_putcmap(p, &sc->sc_cmap, 256);
355 		if (error)
356 			return (error);
357 		/* now blast them into the chip */
358 		/* XXX should use retrace interrupt */
359 		cgeightloadcmap(sc, p->index, p->count);
360 #undef p
361 		break;
362 
363 	case FBIOGVIDEO:
364 		*(int *)data = cgeight_get_video(sc);
365 		break;
366 
367 	case FBIOSVIDEO:
368 		cgeight_set_video(sc, *(int *)data);
369 		break;
370 
371 	default:
372 		return (ENOTTY);
373 	}
374 #endif
375 	return (0);
376 }
377 
378 int
379 cgeightpoll(dev, events, p)
380 	dev_t dev;
381 	int events;
382 	struct proc *p;
383 {
384 
385 	return (seltrue(dev, events, p));
386 }
387 
388 /*
389  * Return the address that would map the given device at the given
390  * offset, allowing for the given protection, or return -1 for error.
391  *
392  * The cg8 maps it's overlay plane at 0 for 128K, followed by the
393  * enable plane for 128K, followed by the colour for as long as it
394  * goes. Starting at 8MB, it maps the ramdac for NBPG, then the p4
395  * register for NBPG, then the bootrom for 0x40000.
396  */
397 int
398 cgeightmmap(dev, off, prot)
399 	dev_t dev;
400 	int off, prot;
401 {
402 	register struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
403 	int poff;
404 
405 #define START_ENABLE	(128*1024)
406 #define START_COLOR	((128*1024) + (128*1024))
407 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
408 			    sc->sc_fb.fb_type.fb_height * 3)
409 #define END_COLOR	(START_COLOR + COLOR_SIZE)
410 #define START_SPECIAL	0x800000
411 #define PROMSIZE	0x40000
412 #define NOOVERLAY	(0x04000000)
413 
414 	if (off & PGOFSET)
415 		panic("cgeightmap");
416 
417 	if ((u_int)off >= NOOVERLAY) {
418 		off -= NOOVERLAY;
419 
420 		/*
421 		 * X11 maps a huge chunk of the frame buffer; far more than
422 		 * there really is. We compensate by double-mapping the
423 		 * first page for as many other pages as it wants
424 		 */
425 		while (off >= COLOR_SIZE)
426 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
427 
428 		poff = off + PFOUR_COLOR_OFF_COLOR;
429 	} else if ((u_int)off < START_ENABLE) {
430 		/*
431 		 * in overlay plane
432 		 */
433 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
434 	} else if ((u_int)off < START_COLOR) {
435 		/*
436 		 * in enable plane
437 		 */
438 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
439 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
440 		/*
441 		 * in colour plane
442 		 */
443 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
444 	} else if ((u_int)off < START_SPECIAL) {
445 		/*
446 		 * hole
447 		 */
448 		poff = 0;	/* XXX */
449 	} else if ((u_int)off == START_SPECIAL) {
450 		/*
451 		 * colour map (Brooktree)
452 		 */
453 		poff = PFOUR_COLOR_OFF_CMAP;
454 	} else if ((u_int)off == START_SPECIAL + NBPG) {
455 		/*
456 		 * p4 register
457 		 */
458 		poff = 0;
459 	} else if ((u_int)off > (START_SPECIAL + (NBPG * 2)) &&
460 	    (u_int) off < (START_SPECIAL + (NBPG * 2) + PROMSIZE)) {
461 		/*
462 		 * rom
463 		 */
464 		poff = 0x8000 + (off - (START_SPECIAL + (NBPG * 2)));
465 	} else
466 		return (-1);
467 	/*
468 	 * I turned on PMAP_NC here to disable the cache as I was
469 	 * getting horribly broken behaviour with it on.
470 	 */
471 	return (REG2PHYS(&sc->sc_phys, poff) | PMAP_NC);
472 }
473 
474 #if defined(SUN4)
475 /*
476  * Undo the effect of an FBIOSVIDEO that turns the video off.
477  */
478 static void
479 cgeightunblank(dev)
480 	struct device *dev;
481 {
482 
483 	cgeight_set_video((struct cgeight_softc *)dev, 1);
484 }
485 
486 static int
487 cgeight_get_video(sc)
488 	struct cgeight_softc *sc;
489 {
490 
491 	return (fb_pfour_get_video(&sc->sc_fb));
492 }
493 
494 static void
495 cgeight_set_video(sc, enable)
496 	struct cgeight_softc *sc;
497 	int enable;
498 {
499 
500 	fb_pfour_set_video(&sc->sc_fb, enable);
501 }
502 
503 /*
504  * Load a subset of the current (new) colormap into the Brooktree DAC.
505  */
506 static void
507 cgeightloadcmap(sc, start, ncolors)
508 	register struct cgeight_softc *sc;
509 	register int start, ncolors;
510 {
511 	register volatile struct bt_regs *bt;
512 	register u_int *ip;
513 	register int count;
514 
515 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
516 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
517 	bt = &sc->sc_fbc->fbc_dac;
518 	bt->bt_addr = BT_D4M4(start);
519 	while (--count >= 0)
520 		bt->bt_cmap = *ip++;
521 }
522 #endif
523