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