xref: /netbsd/sys/arch/sparc/dev/cgfour.c (revision bf9ec67e)
1 /*	$NetBSD: cgfour.c,v 1.26 2002/03/11 16:27:01 pk 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_addr_t	sc_paddr;	/* phys address 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	cgfourattach __P((struct device *, struct device *, void *));
129 static int	cgfourmatch __P((struct device *, struct cfdata *, void *));
130 
131 #if defined(SUN4)
132 static void	cgfourunblank __P((struct device *));
133 #endif
134 
135 static int	cg4_pfour_probe __P((void *, void *));
136 
137 /* cdevsw prototypes */
138 cdev_decl(cgfour);
139 
140 struct cfattach cgfour_ca = {
141 	sizeof(struct cgfour_softc), cgfourmatch, cgfourattach
142 };
143 
144 extern struct cfdriver cgfour_cd;
145 
146 #if defined(SUN4)
147 /* frame buffer generic driver */
148 static struct fbdriver cgfourfbdriver = {
149 	cgfourunblank, cgfouropen, cgfourclose, cgfourioctl, cgfourpoll,
150 	cgfourmmap
151 };
152 
153 static void cgfourloadcmap __P((struct cgfour_softc *, int, int));
154 static int cgfour_get_video __P((struct cgfour_softc *));
155 static void cgfour_set_video __P((struct cgfour_softc *, int));
156 #endif
157 
158 /*
159  * Match a cgfour.
160  */
161 int
162 cgfourmatch(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 				cg4_pfour_probe, NULL));
179 }
180 
181 int
182 cg4_pfour_probe(vaddr, arg)
183 	void *vaddr;
184 	void *arg;
185 {
186 
187 	return (fb_pfour_id(vaddr) == PFOUR_ID_COLOR8P1);
188 }
189 
190 /*
191  * Attach a display.  We need to notice if it is the console, too.
192  */
193 void
194 cgfourattach(parent, self, aux)
195 	struct device *parent, *self;
196 	void *aux;
197 {
198 #if defined(SUN4)
199 	struct cgfour_softc *sc = (struct cgfour_softc *)self;
200 	union obio_attach_args *uoba = aux;
201 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
202 	bus_space_handle_t bh;
203 	volatile struct bt_regs *bt;
204 	struct fbdevice *fb = &sc->sc_fb;
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 control registers\n", self->dv_xname);
216 		return;
217 	}
218 	fb->fb_pfour = (volatile u_int32_t *)bh;
219 
220 	fb->fb_driver = &cgfourfbdriver;
221 	fb->fb_device = &sc->sc_dev;
222 	fb->fb_type.fb_type = FBTYPE_SUN4COLOR;
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 = 8;
229 	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
230 
231 	fb->fb_type.fb_cmsize = 256;
232 	fb->fb_type.fb_size = ramsize;
233 	printf(": cgfour/p4, %d x %d",
234 		fb->fb_type.fb_width, 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 (bus_space_map(oba->oba_bustag,
274 			  oba->oba_paddr + PFOUR_COLOR_OFF_CMAP,
275 			  sizeof(struct fbcontrol),
276 			  BUS_SPACE_MAP_LINEAR,
277 			  &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 	/* grab initial (current) color map */
284 	bt = &sc->sc_fbc->fbc_dac;
285 	bt->bt_addr = 0;
286 	for (i = 0; i < 256 * 3 / 4; i++)
287 		((char *)&sc->sc_cmap)[i] = bt->bt_cmap >> 24;
288 
289 	BT_INIT(bt, 24);
290 
291 #if 0	/* See above. */
292 	if (isconsole) {
293 		printf(" (console)\n");
294 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
295 		fbrcons_init(fb);
296 #endif
297 	} else
298 #endif /* 0 */
299 		printf("\n");
300 
301 	/*
302 	 * Even though we're not using rconsole, we'd still like
303 	 * to notice if we're the console framebuffer.
304 	 */
305 	fb_attach(fb, isconsole);
306 #endif
307 }
308 
309 int
310 cgfouropen(dev, flags, mode, p)
311 	dev_t dev;
312 	int flags, mode;
313 	struct proc *p;
314 {
315 	int unit = minor(dev);
316 
317 	if (unit >= cgfour_cd.cd_ndevs || cgfour_cd.cd_devs[unit] == NULL)
318 		return (ENXIO);
319 	return (0);
320 }
321 
322 int
323 cgfourclose(dev, flags, mode, p)
324 	dev_t dev;
325 	int flags, mode;
326 	struct proc *p;
327 {
328 
329 	return (0);
330 }
331 
332 int
333 cgfourioctl(dev, cmd, data, flags, p)
334 	dev_t dev;
335 	u_long cmd;
336 	caddr_t data;
337 	int flags;
338 	struct proc *p;
339 {
340 #if defined(SUN4)
341 	struct cgfour_softc *sc = cgfour_cd.cd_devs[minor(dev)];
342 	struct fbgattr *fba;
343 	int error;
344 
345 	switch (cmd) {
346 
347 	case FBIOGTYPE:
348 		*(struct fbtype *)data = sc->sc_fb.fb_type;
349 		break;
350 
351 	case FBIOGATTR:
352 		fba = (struct fbgattr *)data;
353 		fba->real_type = sc->sc_fb.fb_type.fb_type;
354 		fba->owner = 0;		/* XXX ??? */
355 		fba->fbtype = sc->sc_fb.fb_type;
356 		fba->sattr.flags = 0;
357 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
358 		fba->sattr.dev_specific[0] = -1;
359 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
360 		fba->emu_types[1] = -1;
361 		break;
362 
363 	case FBIOGETCMAP:
364 #define p ((struct fbcmap *)data)
365 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
366 
367 	case FBIOPUTCMAP:
368 		/* copy to software map */
369 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
370 		if (error)
371 			return (error);
372 		/* now blast them into the chip */
373 		/* XXX should use retrace interrupt */
374 		cgfourloadcmap(sc, p->index, p->count);
375 #undef p
376 		break;
377 
378 	case FBIOGVIDEO:
379 		*(int *)data = cgfour_get_video(sc);
380 		break;
381 
382 	case FBIOSVIDEO:
383 		cgfour_set_video(sc, *(int *)data);
384 		break;
385 
386 	default:
387 		return (ENOTTY);
388 	}
389 #endif
390 	return (0);
391 }
392 
393 int
394 cgfourpoll(dev, events, p)
395 	dev_t dev;
396 	int events;
397 	struct proc *p;
398 {
399 
400 	return (seltrue(dev, events, p));
401 }
402 
403 /*
404  * Return the address that would map the given device at the given
405  * offset, allowing for the given protection, or return -1 for error.
406  *
407  * the cg4 maps it's overlay plane for 128K, followed by the enable
408  * plane for 128K, followed by the colour plane (for as much colour
409  * as their is.)
410  *
411  * As well, mapping at an offset of 0x04000000 causes the cg4 to map
412  * only it's colour plane, at 0.
413  */
414 paddr_t
415 cgfourmmap(dev, off, prot)
416 	dev_t dev;
417 	off_t off;
418 	int prot;
419 {
420 	struct cgfour_softc *sc = cgfour_cd.cd_devs[minor(dev)];
421 	off_t poff;
422 
423 #define START_ENABLE	(128*1024)
424 #define START_COLOR	((128*1024) + (128*1024))
425 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
426 			    sc->sc_fb.fb_type.fb_height)
427 #define END_COLOR	(START_COLOR + COLOR_SIZE)
428 #define NOOVERLAY	(0x04000000)
429 
430 	if (off & PGOFSET)
431 		panic("cgfourmap");
432 
433 	if (off < 0)
434 		return (-1);
435 	else if ((u_int)off >= NOOVERLAY) {
436 		off -= NOOVERLAY;
437 
438 		/*
439 		 * X11 maps a huge chunk of the frame buffer; far more than
440 		 * there really is. We compensate by double-mapping the
441 		 * first page for as many other pages as it wants
442 		 */
443 		while ((u_int)off >= COLOR_SIZE)
444 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
445 
446 		poff = off + PFOUR_COLOR_OFF_COLOR;
447 	} else if ((u_int)off < START_ENABLE) {
448 		/*
449 		 * in overlay plane
450 		 */
451 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
452 	} else if ((u_int)off < START_COLOR) {
453 		/*
454 		 * in enable plane
455 		 */
456 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
457 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
458 		/*
459 		 * in colour plane
460 		 */
461 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
462 	} else
463 		return (-1);
464 
465 	return (bus_space_mmap(sc->sc_bustag,
466 		sc->sc_paddr, poff,
467 		prot, BUS_SPACE_MAP_LINEAR));
468 }
469 
470 #if defined(SUN4)
471 /*
472  * Undo the effect of an FBIOSVIDEO that turns the video off.
473  */
474 static void
475 cgfourunblank(dev)
476 	struct device *dev;
477 {
478 
479 	cgfour_set_video((struct cgfour_softc *)dev, 1);
480 }
481 
482 static int
483 cgfour_get_video(sc)
484 	struct cgfour_softc *sc;
485 {
486 
487 	return (fb_pfour_get_video(&sc->sc_fb));
488 }
489 
490 static void
491 cgfour_set_video(sc, enable)
492 	struct cgfour_softc *sc;
493 	int enable;
494 {
495 
496 	fb_pfour_set_video(&sc->sc_fb, enable);
497 }
498 
499 /*
500  * Load a subset of the current (new) colormap into the Brooktree DAC.
501  */
502 static void
503 cgfourloadcmap(sc, start, ncolors)
504 	struct cgfour_softc *sc;
505 	int start, ncolors;
506 {
507 	volatile struct bt_regs *bt;
508 	u_int *ip, i;
509 	int count;
510 
511 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
512 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
513 	bt = &sc->sc_fbc->fbc_dac;
514 	bt->bt_addr = BT_D4M4(start) << 24;
515 	while (--count >= 0) {
516 		i = *ip++;
517 		/* hardware that makes one want to pound boards with hammers */
518 		bt->bt_cmap = i;
519 		bt->bt_cmap = i << 8;
520 		bt->bt_cmap = i << 16;
521 		bt->bt_cmap = i << 24;
522 	}
523 }
524 #endif
525