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