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