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