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