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