xref: /netbsd-src/sys/arch/sparc/dev/cgfour.c (revision 8e6ab8837d8d6b9198e67c1c445300b483e2f304)
1 /*	$NetBSD: cgfour.c,v 1.34 2003/07/15 00:04:52 lukem 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/cdefs.h>
97 __KERNEL_RCSID(0, "$NetBSD: cgfour.c,v 1.34 2003/07/15 00:04:52 lukem Exp $");
98 
99 #include <sys/param.h>
100 #include <sys/systm.h>
101 #include <sys/buf.h>
102 #include <sys/device.h>
103 #include <sys/ioctl.h>
104 #include <sys/malloc.h>
105 #include <sys/mman.h>
106 #include <sys/tty.h>
107 #include <sys/conf.h>
108 
109 #include <machine/autoconf.h>
110 #include <machine/eeprom.h>
111 
112 #include <dev/sun/fbio.h>
113 #include <dev/sun/fbvar.h>
114 #include <dev/sun/btreg.h>
115 #include <dev/sun/btvar.h>
116 #include <dev/sun/pfourreg.h>
117 
118 /* per-display variables */
119 struct cgfour_softc {
120 	struct device	sc_dev;		/* base device */
121 	struct fbdevice	sc_fb;		/* frame buffer device */
122 	bus_space_tag_t	sc_bustag;
123 	bus_addr_t	sc_paddr;	/* phys address 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 CFATTACH_DECL(cgfour, sizeof(struct cgfour_softc),
140     cgfourmatch, cgfourattach, NULL, NULL);
141 
142 extern struct cfdriver cgfour_cd;
143 
144 dev_type_open(cgfouropen);
145 dev_type_ioctl(cgfourioctl);
146 dev_type_mmap(cgfourmmap);
147 
148 const struct cdevsw cgfour_cdevsw = {
149 	cgfouropen, nullclose, noread, nowrite, cgfourioctl,
150 	nostop, notty, nopoll, cgfourmmap, nokqfilter,
151 };
152 
153 #if defined(SUN4)
154 /* frame buffer generic driver */
155 static struct fbdriver cgfourfbdriver = {
156 	cgfourunblank, cgfouropen, nullclose, cgfourioctl, nopoll,
157 	cgfourmmap, nokqfilter
158 };
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, 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_paddr = (bus_addr_t)oba->oba_paddr;
216 
217 	/* Map the pfour register. */
218 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
219 			  sizeof(u_int32_t),
220 			  BUS_SPACE_MAP_LINEAR,
221 			  &bh) != 0) {
222 		printf("%s: cannot map control registers\n", self->dv_xname);
223 		return;
224 	}
225 	fb->fb_pfour = (volatile u_int32_t *)bh;
226 
227 	fb->fb_driver = &cgfourfbdriver;
228 	fb->fb_device = &sc->sc_dev;
229 	fb->fb_type.fb_type = FBTYPE_SUN4COLOR;
230 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
231 	fb->fb_flags |= FB_PFOUR;
232 
233 	ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
234 
235 	fb->fb_type.fb_depth = 8;
236 	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
237 
238 	fb->fb_type.fb_cmsize = 256;
239 	fb->fb_type.fb_size = ramsize;
240 	printf(": cgfour/p4, %d x %d",
241 		fb->fb_type.fb_width, fb->fb_type.fb_height);
242 
243 	isconsole = 0;
244 
245 	if (CPU_ISSUN4) {
246 		struct eeprom *eep = (struct eeprom *)eeprom_va;
247 
248 		/*
249 		 * Assume this is the console if there's no eeprom info
250 		 * to be found.
251 		 */
252 		if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
253 			isconsole = fb_is_console(0);
254 	}
255 
256 #if 0
257 	/*
258 	 * We don't do any of the console handling here.  Instead,
259 	 * we let the bwtwo driver pick up the overlay plane and
260 	 * use it instead.  Rconsole should have better performance
261 	 * with the 1-bit depth.
262 	 *	-- Jason R. Thorpe <thorpej@NetBSD.ORG>
263 	 */
264 
265 	/*
266 	 * When the ROM has mapped in a cgfour display, the address
267 	 * maps only the video RAM, so in any case we have to map the
268 	 * registers ourselves.  We only need the video RAM if we are
269 	 * going to print characters via rconsole.
270 	 */
271 
272 	if (isconsole) {
273 		/* XXX this is kind of a waste */
274 		fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
275 					 PFOUR_COLOR_OFF_OVERLAY, ramsize);
276 	}
277 #endif
278 
279 	/* Map the Brooktree. */
280 	if (bus_space_map(oba->oba_bustag,
281 			  oba->oba_paddr + PFOUR_COLOR_OFF_CMAP,
282 			  sizeof(struct fbcontrol),
283 			  BUS_SPACE_MAP_LINEAR,
284 			  &bh) != 0) {
285 		printf("%s: cannot map control registers\n", self->dv_xname);
286 		return;
287 	}
288 	sc->sc_fbc = (volatile struct fbcontrol *)bh;
289 
290 	/* grab initial (current) color map */
291 	bt = &sc->sc_fbc->fbc_dac;
292 	bt->bt_addr = 0;
293 	for (i = 0; i < 256 * 3 / 4; i++)
294 		((char *)&sc->sc_cmap)[i] = bt->bt_cmap >> 24;
295 
296 	BT_INIT(bt, 24);
297 
298 #if 0	/* See above. */
299 	if (isconsole) {
300 		printf(" (console)\n");
301 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
302 		fbrcons_init(fb);
303 #endif
304 	} else
305 #endif /* 0 */
306 		printf("\n");
307 
308 	/*
309 	 * Even though we're not using rconsole, we'd still like
310 	 * to notice if we're the console framebuffer.
311 	 */
312 	fb_attach(fb, isconsole);
313 #endif
314 }
315 
316 int
317 cgfouropen(dev, flags, mode, p)
318 	dev_t dev;
319 	int flags, mode;
320 	struct proc *p;
321 {
322 	int unit = minor(dev);
323 
324 	if (unit >= cgfour_cd.cd_ndevs || cgfour_cd.cd_devs[unit] == NULL)
325 		return (ENXIO);
326 	return (0);
327 }
328 
329 int
330 cgfourioctl(dev, cmd, data, flags, p)
331 	dev_t dev;
332 	u_long cmd;
333 	caddr_t data;
334 	int flags;
335 	struct proc *p;
336 {
337 #if defined(SUN4)
338 	struct cgfour_softc *sc = cgfour_cd.cd_devs[minor(dev)];
339 	struct fbgattr *fba;
340 	int error;
341 
342 	switch (cmd) {
343 
344 	case FBIOGTYPE:
345 		*(struct fbtype *)data = sc->sc_fb.fb_type;
346 		break;
347 
348 	case FBIOGATTR:
349 		fba = (struct fbgattr *)data;
350 		fba->real_type = sc->sc_fb.fb_type.fb_type;
351 		fba->owner = 0;		/* XXX ??? */
352 		fba->fbtype = sc->sc_fb.fb_type;
353 		fba->sattr.flags = 0;
354 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
355 		fba->sattr.dev_specific[0] = -1;
356 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
357 		fba->emu_types[1] = -1;
358 		break;
359 
360 	case FBIOGETCMAP:
361 #define p ((struct fbcmap *)data)
362 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
363 
364 	case FBIOPUTCMAP:
365 		/* copy to software map */
366 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
367 		if (error)
368 			return (error);
369 		/* now blast them into the chip */
370 		/* XXX should use retrace interrupt */
371 		cgfourloadcmap(sc, p->index, p->count);
372 #undef p
373 		break;
374 
375 	case FBIOGVIDEO:
376 		*(int *)data = cgfour_get_video(sc);
377 		break;
378 
379 	case FBIOSVIDEO:
380 		cgfour_set_video(sc, *(int *)data);
381 		break;
382 
383 	default:
384 		return (ENOTTY);
385 	}
386 #endif
387 	return (0);
388 }
389 
390 /*
391  * Return the address that would map the given device at the given
392  * offset, allowing for the given protection, or return -1 for error.
393  *
394  * the cg4 maps it's overlay plane for 128K, followed by the enable
395  * plane for 128K, followed by the colour plane (for as much colour
396  * as their is.)
397  *
398  * As well, mapping at an offset of 0x04000000 causes the cg4 to map
399  * only it's colour plane, at 0.
400  */
401 paddr_t
402 cgfourmmap(dev, off, prot)
403 	dev_t dev;
404 	off_t off;
405 	int prot;
406 {
407 	struct cgfour_softc *sc = cgfour_cd.cd_devs[minor(dev)];
408 	off_t poff;
409 
410 #define START_ENABLE	(128*1024)
411 #define START_COLOR	((128*1024) + (128*1024))
412 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
413 			    sc->sc_fb.fb_type.fb_height)
414 #define END_COLOR	(START_COLOR + COLOR_SIZE)
415 #define NOOVERLAY	(0x04000000)
416 
417 	if (off & PGOFSET)
418 		panic("cgfourmap");
419 
420 	if (off < 0)
421 		return (-1);
422 	else if ((u_int)off >= NOOVERLAY) {
423 		off -= NOOVERLAY;
424 
425 		/*
426 		 * X11 maps a huge chunk of the frame buffer; far more than
427 		 * there really is. We compensate by double-mapping the
428 		 * first page for as many other pages as it wants
429 		 */
430 		while ((u_int)off >= COLOR_SIZE)
431 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
432 
433 		poff = off + PFOUR_COLOR_OFF_COLOR;
434 	} else if ((u_int)off < START_ENABLE) {
435 		/*
436 		 * in overlay plane
437 		 */
438 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
439 	} else if ((u_int)off < START_COLOR) {
440 		/*
441 		 * in enable plane
442 		 */
443 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
444 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
445 		/*
446 		 * in colour plane
447 		 */
448 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
449 	} else
450 		return (-1);
451 
452 	return (bus_space_mmap(sc->sc_bustag,
453 		sc->sc_paddr, poff,
454 		prot, BUS_SPACE_MAP_LINEAR));
455 }
456 
457 #if defined(SUN4)
458 /*
459  * Undo the effect of an FBIOSVIDEO that turns the video off.
460  */
461 static void
462 cgfourunblank(dev)
463 	struct device *dev;
464 {
465 
466 	cgfour_set_video((struct cgfour_softc *)dev, 1);
467 }
468 
469 static int
470 cgfour_get_video(sc)
471 	struct cgfour_softc *sc;
472 {
473 
474 	return (fb_pfour_get_video(&sc->sc_fb));
475 }
476 
477 static void
478 cgfour_set_video(sc, enable)
479 	struct cgfour_softc *sc;
480 	int enable;
481 {
482 
483 	fb_pfour_set_video(&sc->sc_fb, enable);
484 }
485 
486 /*
487  * Load a subset of the current (new) colormap into the Brooktree DAC.
488  */
489 static void
490 cgfourloadcmap(sc, start, ncolors)
491 	struct cgfour_softc *sc;
492 	int start, ncolors;
493 {
494 	volatile struct bt_regs *bt;
495 	u_int *ip, i;
496 	int count;
497 
498 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
499 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
500 	bt = &sc->sc_fbc->fbc_dac;
501 	bt->bt_addr = BT_D4M4(start) << 24;
502 	while (--count >= 0) {
503 		i = *ip++;
504 		/* hardware that makes one want to pound boards with hammers */
505 		bt->bt_cmap = i;
506 		bt->bt_cmap = i << 8;
507 		bt->bt_cmap = i << 16;
508 		bt->bt_cmap = i << 24;
509 	}
510 }
511 #endif
512