xref: /netbsd-src/sys/arch/sparc/dev/cgfour.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: cgfour.c,v 1.45 2008/06/11 21:25:31 drochner 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.45 2008/06/11 21:25:31 drochner 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 device	sc_dev;		/* base device */
129 	struct fbdevice	sc_fb;		/* frame buffer device */
130 	bus_space_tag_t	sc_bustag;
131 	bus_addr_t	sc_paddr;	/* phys address for device mmap() */
132 
133 	volatile struct fbcontrol *sc_fbc;	/* Brooktree registers */
134 	union bt_cmap	sc_cmap;	/* Brooktree color map */
135 };
136 
137 /* autoconfiguration driver */
138 static int	cgfourmatch(struct device *, struct cfdata *, void *);
139 static void	cgfourattach(struct device *, struct device *, void *);
140 
141 #if defined(SUN4)
142 static void	cgfourunblank(struct device *);
143 #endif
144 
145 static int	cg4_pfour_probe(void *, void *);
146 
147 CFATTACH_DECL(cgfour, sizeof(struct cgfour_softc),
148     cgfourmatch, cgfourattach, NULL, NULL);
149 
150 extern struct cfdriver cgfour_cd;
151 
152 dev_type_open(cgfouropen);
153 dev_type_ioctl(cgfourioctl);
154 dev_type_mmap(cgfourmmap);
155 
156 const struct cdevsw cgfour_cdevsw = {
157 	cgfouropen, nullclose, noread, nowrite, cgfourioctl,
158 	nostop, notty, nopoll, cgfourmmap, nokqfilter,
159 };
160 
161 #if defined(SUN4)
162 /* frame buffer generic driver */
163 static struct fbdriver cgfourfbdriver = {
164 	cgfourunblank, cgfouropen, nullclose, cgfourioctl, nopoll,
165 	cgfourmmap, nokqfilter
166 };
167 
168 static void cgfourloadcmap(struct cgfour_softc *, int, int);
169 static int cgfour_get_video(struct cgfour_softc *);
170 static void cgfour_set_video(struct cgfour_softc *, int);
171 #endif
172 
173 /*
174  * Match a cgfour.
175  */
176 static int
177 cgfourmatch(struct device *parent, struct cfdata *cf, void *aux)
178 {
179 	union obio_attach_args *uoba = aux;
180 	struct obio4_attach_args *oba;
181 
182 	if (uoba->uoba_isobio4 == 0)
183 		return (0);
184 
185 	oba = &uoba->uoba_oba4;
186 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
187 				4,	/* probe size */
188 				0,	/* offset */
189 				0,	/* flags */
190 				cg4_pfour_probe, NULL));
191 }
192 
193 static int
194 cg4_pfour_probe(void *vaddr, void *arg)
195 {
196 
197 	return (fb_pfour_id(vaddr) == PFOUR_ID_COLOR8P1);
198 }
199 
200 /*
201  * Attach a display.  We need to notice if it is the console, too.
202  */
203 static void
204 cgfourattach(struct device *parent, struct device *self, void *aux)
205 {
206 #if defined(SUN4)
207 	struct cgfour_softc *sc = device_private(self);
208 	union obio_attach_args *uoba = aux;
209 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
210 	bus_space_handle_t bh;
211 	volatile struct bt_regs *bt;
212 	struct fbdevice *fb = &sc->sc_fb;
213 	int ramsize, i, isconsole;
214 
215 	sc->sc_bustag = oba->oba_bustag;
216 	sc->sc_paddr = (bus_addr_t)oba->oba_paddr;
217 
218 	/* Map the pfour register. */
219 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
220 			  sizeof(uint32_t),
221 			  BUS_SPACE_MAP_LINEAR,
222 			  &bh) != 0) {
223 		printf("%s: cannot map control registers\n", self->dv_xname);
224 		return;
225 	}
226 	fb->fb_pfour = (volatile uint32_t *)bh;
227 
228 	fb->fb_driver = &cgfourfbdriver;
229 	fb->fb_device = &sc->sc_dev;
230 	fb->fb_type.fb_type = FBTYPE_SUN4COLOR;
231 	fb->fb_flags = device_cfdata(&sc->sc_dev)->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", self->dv_xname);
287 		return;
288 	}
289 	sc->sc_fbc = (volatile struct fbcontrol *)bh;
290 
291 	/* grab initial (current) color map */
292 	bt = &sc->sc_fbc->fbc_dac;
293 	bt->bt_addr = 0;
294 	for (i = 0; i < 256 * 3 / 4; i++)
295 		((char *)&sc->sc_cmap)[i] = bt->bt_cmap >> 24;
296 
297 	BT_INIT(bt, 24);
298 
299 #if 0	/* See above. */
300 	if (isconsole) {
301 		printf(" (console)\n");
302 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
303 		fbrcons_init(fb);
304 #endif
305 	} else
306 #endif /* 0 */
307 		printf("\n");
308 
309 	/*
310 	 * Even though we're not using rconsole, we'd still like
311 	 * to notice if we're the console framebuffer.
312 	 */
313 	fb_attach(fb, isconsole);
314 #endif /* SUN4 */
315 }
316 
317 int
318 cgfouropen(dev_t dev, int flags, int mode, struct lwp *l)
319 {
320 	int unit = minor(dev);
321 
322 	if (device_lookup(&cgfour_cd, unit) == NULL)
323 		return (ENXIO);
324 	return (0);
325 }
326 
327 int
328 cgfourioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
329 {
330 #if defined(SUN4)
331 	struct cgfour_softc *sc = device_lookup_private(&cgfour_cd, minor(dev));
332 	struct fbgattr *fba;
333 	int error;
334 
335 	switch (cmd) {
336 
337 	case FBIOGTYPE:
338 		*(struct fbtype *)data = sc->sc_fb.fb_type;
339 		break;
340 
341 	case FBIOGATTR:
342 		fba = (struct fbgattr *)data;
343 		fba->real_type = sc->sc_fb.fb_type.fb_type;
344 		fba->owner = 0;		/* XXX ??? */
345 		fba->fbtype = sc->sc_fb.fb_type;
346 		fba->sattr.flags = 0;
347 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
348 		fba->sattr.dev_specific[0] = -1;
349 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
350 		fba->emu_types[1] = -1;
351 		break;
352 
353 	case FBIOGETCMAP:
354 #define p ((struct fbcmap *)data)
355 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
356 
357 	case FBIOPUTCMAP:
358 		/* copy to software map */
359 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
360 		if (error)
361 			return (error);
362 		/* now blast them into the chip */
363 		/* XXX should use retrace interrupt */
364 		cgfourloadcmap(sc, p->index, p->count);
365 #undef p
366 		break;
367 
368 	case FBIOGVIDEO:
369 		*(int *)data = cgfour_get_video(sc);
370 		break;
371 
372 	case FBIOSVIDEO:
373 		cgfour_set_video(sc, *(int *)data);
374 		break;
375 
376 	default:
377 		return (ENOTTY);
378 	}
379 #endif /* SUN4 */
380 
381 	return (0);
382 }
383 
384 /*
385  * Return the address that would map the given device at the given
386  * offset, allowing for the given protection, or return -1 for error.
387  *
388  * the cg4 maps it's overlay plane for 128K, followed by the enable
389  * plane for 128K, followed by the colour plane (for as much colour
390  * as their is.)
391  *
392  * As well, mapping at an offset of 0x04000000 causes the cg4 to map
393  * only it's colour plane, at 0.
394  */
395 paddr_t
396 cgfourmmap(dev_t dev, off_t off, int prot)
397 {
398 	struct cgfour_softc *sc = device_lookup_private(&cgfour_cd, minor(dev));
399 	off_t poff;
400 
401 #define START_ENABLE	(128*1024)
402 #define START_COLOR	((128*1024) + (128*1024))
403 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
404 			    sc->sc_fb.fb_type.fb_height)
405 #define END_COLOR	(START_COLOR + COLOR_SIZE)
406 #define NOOVERLAY	(0x04000000)
407 
408 	if (off & PGOFSET)
409 		panic("cgfourmap");
410 
411 	if (off < 0)
412 		return (-1);
413 	else if ((u_int)off >= NOOVERLAY) {
414 		off -= NOOVERLAY;
415 
416 		/*
417 		 * X11 maps a huge chunk of the frame buffer; far more than
418 		 * there really is. We compensate by double-mapping the
419 		 * first page for as many other pages as it wants
420 		 */
421 		while ((u_int)off >= COLOR_SIZE)
422 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
423 
424 		poff = off + PFOUR_COLOR_OFF_COLOR;
425 	} else if ((u_int)off < START_ENABLE) {
426 		/*
427 		 * in overlay plane
428 		 */
429 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
430 	} else if ((u_int)off < START_COLOR) {
431 		/*
432 		 * in enable plane
433 		 */
434 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
435 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
436 		/*
437 		 * in colour plane
438 		 */
439 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
440 	} else
441 		return (-1);
442 
443 	return (bus_space_mmap(sc->sc_bustag,
444 		sc->sc_paddr, poff,
445 		prot, BUS_SPACE_MAP_LINEAR));
446 }
447 
448 #if defined(SUN4)
449 /*
450  * Undo the effect of an FBIOSVIDEO that turns the video off.
451  */
452 static void
453 cgfourunblank(struct device *dev)
454 {
455 
456 	cgfour_set_video(device_private(dev), 1);
457 }
458 
459 static int
460 cgfour_get_video(struct cgfour_softc *sc)
461 {
462 
463 	return (fb_pfour_get_video(&sc->sc_fb));
464 }
465 
466 static void
467 cgfour_set_video(struct cgfour_softc *sc, int enable)
468 {
469 
470 	fb_pfour_set_video(&sc->sc_fb, enable);
471 }
472 
473 /*
474  * Load a subset of the current (new) colormap into the Brooktree DAC.
475  */
476 static void
477 cgfourloadcmap(struct cgfour_softc *sc, int start, int ncolors)
478 {
479 	volatile struct bt_regs *bt;
480 	u_int *ip, i;
481 	int count;
482 
483 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
484 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
485 	bt = &sc->sc_fbc->fbc_dac;
486 	bt->bt_addr = BT_D4M4(start) << 24;
487 	while (--count >= 0) {
488 		i = *ip++;
489 		/* hardware that makes one want to pound boards with hammers */
490 		bt->bt_cmap = i;
491 		bt->bt_cmap = i << 8;
492 		bt->bt_cmap = i << 16;
493 		bt->bt_cmap = i << 24;
494 	}
495 }
496 #endif /* SUN4 */
497