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