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