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