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