xref: /netbsd-src/sys/arch/sparc/dev/cgeight.c (revision 89c5a767f8fc7a4633b2d409966e2becbb98ff92)
1 /*	$NetBSD: cgeight.c,v 1.19 1998/11/19 15:38:24 mrg 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 	bus_space_tag_t	sc_bustag;
120 	bus_type_t	sc_btype;	/* phys address description */
121 	bus_addr_t	sc_paddr;	/* for device mmap() */
122 
123 	volatile struct fbcontrol *sc_fbc;	/* Brooktree registers */
124 	union bt_cmap	sc_cmap;	/* Brooktree color map */
125 };
126 
127 /* autoconfiguration driver */
128 static void	cgeightattach(struct device *, struct device *, void *);
129 static int	cgeightmatch(struct device *, struct cfdata *, void *);
130 #if defined(SUN4)
131 static void	cgeightunblank __P((struct device *));
132 #endif
133 
134 static int	cg8_pfour_probe __P((void *, void *));
135 
136 /* cdevsw prototypes */
137 cdev_decl(cgeight);
138 
139 struct cfattach cgeight_ca = {
140 	sizeof(struct cgeight_softc), cgeightmatch, cgeightattach
141 };
142 
143 extern struct cfdriver cgeight_cd;
144 
145 #if defined(SUN4)
146 /* frame buffer generic driver */
147 static struct fbdriver cgeightfbdriver = {
148 	cgeightunblank, cgeightopen, cgeightclose, cgeightioctl,
149 	cgeightpoll, cgeightmmap
150 };
151 
152 extern int fbnode;
153 extern struct tty *fbconstty;
154 
155 static void cgeightloadcmap __P((struct cgeight_softc *, int, int));
156 static int cgeight_get_video __P((struct cgeight_softc *));
157 static void cgeight_set_video __P((struct cgeight_softc *, int));
158 #endif
159 
160 /*
161  * Match a cgeight.
162  */
163 int
164 cgeightmatch(parent, cf, aux)
165 	struct device *parent;
166 	struct cfdata *cf;
167 	void *aux;
168 {
169 	union obio_attach_args *uoba = aux;
170 	struct obio4_attach_args *oba;
171 
172 	if (uoba->uoba_isobio4 == 0)
173 		return (0);
174 
175 	oba = &uoba->uoba_oba4;
176 	return (bus_space_probe(oba->oba_bustag, 0, oba->oba_paddr,
177 				4,	/* probe size */
178 				0,	/* offset */
179 				0,	/* flags */
180 				cg8_pfour_probe, NULL));
181 }
182 
183 int
184 cg8_pfour_probe(vaddr, arg)
185 	void *vaddr;
186 	void *arg;
187 {
188 
189 	return (fb_pfour_id(vaddr) == PFOUR_ID_COLOR24);
190 }
191 
192 /*
193  * Attach a display.  We need to notice if it is the console, too.
194  */
195 void
196 cgeightattach(parent, self, aux)
197 	struct device *parent, *self;
198 	void *aux;
199 {
200 #if defined(SUN4)
201 	union obio_attach_args *uoba = aux;
202 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
203 	struct cgeight_softc *sc = (struct cgeight_softc *)self;
204 	struct fbdevice *fb = &sc->sc_fb;
205 	bus_space_handle_t bh;
206 	volatile struct bt_regs *bt;
207 	int ramsize, i, isconsole;
208 
209 	sc->sc_bustag = oba->oba_bustag;
210 	sc->sc_btype = (bus_type_t)0;
211 	sc->sc_paddr = (bus_addr_t)oba->oba_paddr;
212 
213 	/* Map the pfour register. */
214 	if (obio_bus_map(oba->oba_bustag, oba->oba_paddr,
215 			 0,
216 			 sizeof(u_int32_t),
217 			 BUS_SPACE_MAP_LINEAR,
218 			 0, &bh) != 0) {
219 		printf("%s: cannot map pfour register\n", self->dv_xname);
220 		return;
221 	}
222 	fb->fb_pfour = (volatile u_int32_t *)bh;
223 
224 	fb->fb_driver = &cgeightfbdriver;
225 	fb->fb_device = &sc->sc_dev;
226 	fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
227 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
228 	fb->fb_flags |= FB_PFOUR;
229 
230 	ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
231 
232 	fb->fb_type.fb_depth = 24;
233 	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
234 
235 	sc->sc_fb.fb_type.fb_cmsize = 256;
236 	sc->sc_fb.fb_type.fb_size = ramsize;
237 	printf(": cgeight/p4, %d x %d",
238 		fb->fb_type.fb_width,
239 		fb->fb_type.fb_height);
240 
241 	isconsole = 0;
242 
243 	if (CPU_ISSUN4) {
244 		struct eeprom *eep = (struct eeprom *)eeprom_va;
245 
246 		/*
247 		 * Assume this is the console if there's no eeprom info
248 		 * to be found.
249 		 */
250 		if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
251 			isconsole = (fbconstty != NULL);
252 	}
253 
254 #if 0
255 	/*
256 	 * We don't do any of the console handling here.  Instead,
257 	 * we let the bwtwo driver pick up the overlay plane and
258 	 * use it instead.  Rconsole should have better performance
259 	 * with the 1-bit depth.
260 	 *      -- Jason R. Thorpe <thorpej@NetBSD.ORG>
261 	 */
262 
263 	/*
264 	 * When the ROM has mapped in a cgfour display, the address
265 	 * maps only the video RAM, so in any case we have to map the
266 	 * registers ourselves.  We only need the video RAM if we are
267 	 * going to print characters via rconsole.
268 	 */
269 
270 	if (isconsole) {
271 		/* XXX this is kind of a waste */
272 		fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
273 					 PFOUR_COLOR_OFF_OVERLAY, ramsize);
274 	}
275 #endif
276 
277 	/* Map the Brooktree. */
278 	if (obio_bus_map(oba->oba_bustag, oba->oba_paddr,
279 			 PFOUR_COLOR_OFF_CMAP,
280 			 sizeof(struct fbcontrol),
281 			 BUS_SPACE_MAP_LINEAR,
282 			 0, &bh) != 0) {
283 		printf("%s: cannot map control registers\n", self->dv_xname);
284 		return;
285 	}
286 	sc->sc_fbc = (volatile struct fbcontrol *)bh;
287 
288 #if 0	/* XXX thorpej ??? */
289 	/* tell the enable plane to look at the mono image */
290 	memset(ca->ca_ra.ra_vaddr, 0xff,
291 	    sc->sc_fb.fb_type.fb_width * sc->sc_fb.fb_type.fb_height / 8);
292 #endif
293 
294 	/* grab initial (current) color map */
295 	bt = &sc->sc_fbc->fbc_dac;
296 	bt->bt_addr = 0;
297 	for (i = 0; i < 256 * 3 / 4; i++)
298 		sc->sc_cmap.cm_chip[i] = bt->bt_cmap;
299 
300 	BT_INIT(bt, 0);
301 
302 #if 0	/* see above */
303 	if (isconsole) {
304 		printf(" (console)\n");
305 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
306 		fbrcons_init(fb);
307 #endif
308 	} else
309 #endif /* 0 */
310 		printf("\n");
311 
312 	/*
313 	 * Even though we're not using rconsole, we'd still like
314 	 * to notice if we're the console framebuffer.
315 	 */
316 	fb_attach(&sc->sc_fb, isconsole);
317 #endif
318 }
319 
320 int
321 cgeightopen(dev, flags, mode, p)
322 	dev_t dev;
323 	int flags, mode;
324 	struct proc *p;
325 {
326 	int unit = minor(dev);
327 
328 	if (unit >= cgeight_cd.cd_ndevs || cgeight_cd.cd_devs[unit] == NULL)
329 		return (ENXIO);
330 	return (0);
331 }
332 
333 int
334 cgeightclose(dev, flags, mode, p)
335 	dev_t dev;
336 	int flags, mode;
337 	struct proc *p;
338 {
339 
340 	return (0);
341 }
342 
343 int
344 cgeightioctl(dev, cmd, data, flags, p)
345 	dev_t dev;
346 	u_long cmd;
347 	caddr_t data;
348 	int flags;
349 	struct proc *p;
350 {
351 #if defined(SUN4)
352 	struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
353 	struct fbgattr *fba;
354 	int error;
355 
356 	switch (cmd) {
357 
358 	case FBIOGTYPE:
359 		*(struct fbtype *)data = sc->sc_fb.fb_type;
360 		break;
361 
362 	case FBIOGATTR:
363 		fba = (struct fbgattr *)data;
364 		fba->real_type = sc->sc_fb.fb_type.fb_type;
365 		fba->owner = 0;		/* XXX ??? */
366 		fba->fbtype = sc->sc_fb.fb_type;
367 		fba->sattr.flags = 0;
368 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
369 		fba->sattr.dev_specific[0] = -1;
370 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
371 		fba->emu_types[1] = -1;
372 		break;
373 
374 	case FBIOGETCMAP:
375 		return (bt_getcmap((struct fbcmap *)data, &sc->sc_cmap, 256));
376 
377 	case FBIOPUTCMAP:
378 		/* copy to software map */
379 #define p ((struct fbcmap *)data)
380 		error = bt_putcmap(p, &sc->sc_cmap, 256);
381 		if (error)
382 			return (error);
383 		/* now blast them into the chip */
384 		/* XXX should use retrace interrupt */
385 		cgeightloadcmap(sc, p->index, p->count);
386 #undef p
387 		break;
388 
389 	case FBIOGVIDEO:
390 		*(int *)data = cgeight_get_video(sc);
391 		break;
392 
393 	case FBIOSVIDEO:
394 		cgeight_set_video(sc, *(int *)data);
395 		break;
396 
397 	default:
398 		return (ENOTTY);
399 	}
400 #endif
401 	return (0);
402 }
403 
404 int
405 cgeightpoll(dev, events, p)
406 	dev_t dev;
407 	int events;
408 	struct proc *p;
409 {
410 
411 	return (seltrue(dev, events, p));
412 }
413 
414 /*
415  * Return the address that would map the given device at the given
416  * offset, allowing for the given protection, or return -1 for error.
417  *
418  * The cg8 maps it's overlay plane at 0 for 128K, followed by the
419  * enable plane for 128K, followed by the colour for as long as it
420  * goes. Starting at 8MB, it maps the ramdac for NBPG, then the p4
421  * register for NBPG, then the bootrom for 0x40000.
422  */
423 int
424 cgeightmmap(dev, off, prot)
425 	dev_t dev;
426 	int off, prot;
427 {
428 	struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
429 	bus_space_handle_t bh;
430 	int poff;
431 
432 #define START_ENABLE	(128*1024)
433 #define START_COLOR	((128*1024) + (128*1024))
434 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
435 			    sc->sc_fb.fb_type.fb_height * 3)
436 #define END_COLOR	(START_COLOR + COLOR_SIZE)
437 #define START_SPECIAL	0x800000
438 #define PROMSIZE	0x40000
439 #define NOOVERLAY	(0x04000000)
440 
441 	if (off & PGOFSET)
442 		panic("cgeightmap");
443 
444 	if (off < 0)
445 		return (-1);
446 	else if ((u_int)off >= NOOVERLAY) {
447 		off -= NOOVERLAY;
448 
449 		/*
450 		 * X11 maps a huge chunk of the frame buffer; far more than
451 		 * there really is. We compensate by double-mapping the
452 		 * first page for as many other pages as it wants
453 		 */
454 		while ((u_int)off >= COLOR_SIZE)
455 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
456 
457 		poff = off + PFOUR_COLOR_OFF_COLOR;
458 	} else if ((u_int)off < START_ENABLE) {
459 		/*
460 		 * in overlay plane
461 		 */
462 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
463 	} else if ((u_int)off < START_COLOR) {
464 		/*
465 		 * in enable plane
466 		 */
467 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
468 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
469 		/*
470 		 * in colour plane
471 		 */
472 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
473 	} else if ((u_int)off < START_SPECIAL) {
474 		/*
475 		 * hole
476 		 */
477 		poff = 0;	/* XXX */
478 	} else if ((u_int)off == START_SPECIAL) {
479 		/*
480 		 * colour map (Brooktree)
481 		 */
482 		poff = PFOUR_COLOR_OFF_CMAP;
483 	} else if ((u_int)off == START_SPECIAL + NBPG) {
484 		/*
485 		 * p4 register
486 		 */
487 		poff = 0;
488 	} else if ((u_int)off > (START_SPECIAL + (NBPG * 2)) &&
489 	    (u_int) off < (START_SPECIAL + (NBPG * 2) + PROMSIZE)) {
490 		/*
491 		 * rom
492 		 */
493 		poff = 0x8000 + (off - (START_SPECIAL + (NBPG * 2)));
494 	} else
495 		return (-1);
496 
497 	if (bus_space_mmap(sc->sc_bustag,
498 			   sc->sc_btype,
499 			   sc->sc_paddr + poff,
500 			   BUS_SPACE_MAP_LINEAR, &bh))
501 		return (-1);
502 
503 	return ((int)bh);
504 }
505 
506 #if defined(SUN4)
507 /*
508  * Undo the effect of an FBIOSVIDEO that turns the video off.
509  */
510 static void
511 cgeightunblank(dev)
512 	struct device *dev;
513 {
514 
515 	cgeight_set_video((struct cgeight_softc *)dev, 1);
516 }
517 
518 static int
519 cgeight_get_video(sc)
520 	struct cgeight_softc *sc;
521 {
522 
523 	return (fb_pfour_get_video(&sc->sc_fb));
524 }
525 
526 static void
527 cgeight_set_video(sc, enable)
528 	struct cgeight_softc *sc;
529 	int enable;
530 {
531 
532 	fb_pfour_set_video(&sc->sc_fb, enable);
533 }
534 
535 /*
536  * Load a subset of the current (new) colormap into the Brooktree DAC.
537  */
538 static void
539 cgeightloadcmap(sc, start, ncolors)
540 	register struct cgeight_softc *sc;
541 	register int start, ncolors;
542 {
543 	volatile struct bt_regs *bt;
544 	u_int *ip;
545 	int count;
546 
547 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
548 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
549 	bt = &sc->sc_fbc->fbc_dac;
550 	bt->bt_addr = BT_D4M4(start);
551 	while (--count >= 0)
552 		bt->bt_cmap = *ip++;
553 }
554 #endif
555