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