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