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