xref: /netbsd-src/sys/arch/sparc/dev/cgeight.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: cgeight.c,v 1.40 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 (cgeight) 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: cgeight.c,v 1.40 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 <uvm/uvm_extern.h>
125 
126 #include <machine/autoconf.h>
127 #include <machine/eeprom.h>
128 
129 #include <dev/sun/fbio.h>
130 #include <dev/sun/fbvar.h>
131 #include <dev/sun/btreg.h>
132 #include <dev/sun/btvar.h>
133 #include <dev/sun/pfourreg.h>
134 
135 /* per-display variables */
136 struct cgeight_softc {
137 	struct device	sc_dev;		/* base device */
138 	struct fbdevice	sc_fb;		/* frame buffer device */
139 	bus_space_tag_t	sc_bustag;
140 	bus_addr_t	sc_paddr;	/* phys address for device mmap() */
141 
142 	volatile struct fbcontrol *sc_fbc;	/* Brooktree registers */
143 	union bt_cmap	sc_cmap;	/* Brooktree color map */
144 };
145 
146 /* autoconfiguration driver */
147 static void	cgeightattach(struct device *, struct device *, void *);
148 static int	cgeightmatch(struct device *, struct cfdata *, void *);
149 #if defined(SUN4)
150 static void	cgeightunblank __P((struct device *));
151 #endif
152 
153 static int	cg8_pfour_probe __P((void *, void *));
154 
155 CFATTACH_DECL(cgeight, sizeof(struct cgeight_softc),
156     cgeightmatch, cgeightattach, NULL, NULL);
157 
158 extern struct cfdriver cgeight_cd;
159 
160 dev_type_open(cgeightopen);
161 dev_type_ioctl(cgeightioctl);
162 dev_type_mmap(cgeightmmap);
163 
164 const struct cdevsw cgeight_cdevsw = {
165 	cgeightopen, nullclose, noread, nowrite, cgeightioctl,
166 	nostop, notty, nopoll, cgeightmmap, nokqfilter
167 };
168 
169 #if defined(SUN4)
170 /* frame buffer generic driver */
171 static struct fbdriver cgeightfbdriver = {
172 	cgeightunblank, cgeightopen, nullclose, cgeightioctl,
173 	nopoll, cgeightmmap, nokqfilter
174 };
175 
176 static void cgeightloadcmap __P((struct cgeight_softc *, int, int));
177 static int cgeight_get_video __P((struct cgeight_softc *));
178 static void cgeight_set_video __P((struct cgeight_softc *, int));
179 #endif
180 
181 /*
182  * Match a cgeight.
183  */
184 int
185 cgeightmatch(parent, cf, aux)
186 	struct device *parent;
187 	struct cfdata *cf;
188 	void *aux;
189 {
190 	union obio_attach_args *uoba = aux;
191 	struct obio4_attach_args *oba;
192 
193 	if (uoba->uoba_isobio4 == 0)
194 		return (0);
195 
196 	oba = &uoba->uoba_oba4;
197 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
198 				4,	/* probe size */
199 				0,	/* offset */
200 				0,	/* flags */
201 				cg8_pfour_probe, NULL));
202 }
203 
204 int
205 cg8_pfour_probe(vaddr, arg)
206 	void *vaddr;
207 	void *arg;
208 {
209 
210 	return (fb_pfour_id(vaddr) == PFOUR_ID_COLOR24);
211 }
212 
213 /*
214  * Attach a display.  We need to notice if it is the console, too.
215  */
216 void
217 cgeightattach(parent, self, aux)
218 	struct device *parent, *self;
219 	void *aux;
220 {
221 #if defined(SUN4)
222 	union obio_attach_args *uoba = aux;
223 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
224 	struct cgeight_softc *sc = (struct cgeight_softc *)self;
225 	struct fbdevice *fb = &sc->sc_fb;
226 	bus_space_handle_t bh;
227 	volatile struct bt_regs *bt;
228 	int ramsize, i, isconsole;
229 
230 	sc->sc_bustag = oba->oba_bustag;
231 	sc->sc_paddr = (bus_addr_t)oba->oba_paddr;
232 
233 	/* Map the pfour register. */
234 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
235 			  sizeof(u_int32_t),
236 			  BUS_SPACE_MAP_LINEAR,
237 			  &bh) != 0) {
238 		printf("%s: cannot map pfour register\n", self->dv_xname);
239 		return;
240 	}
241 	fb->fb_pfour = (volatile u_int32_t *)bh;
242 
243 	fb->fb_driver = &cgeightfbdriver;
244 	fb->fb_device = &sc->sc_dev;
245 	fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
246 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
247 	fb->fb_flags |= FB_PFOUR;
248 
249 	ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
250 
251 	fb->fb_type.fb_depth = 24;
252 	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
253 
254 	sc->sc_fb.fb_type.fb_cmsize = 256;
255 	sc->sc_fb.fb_type.fb_size = ramsize;
256 	printf(": cgeight/p4, %d x %d",
257 		fb->fb_type.fb_width,
258 		fb->fb_type.fb_height);
259 
260 	isconsole = 0;
261 
262 	if (CPU_ISSUN4) {
263 		struct eeprom *eep = (struct eeprom *)eeprom_va;
264 
265 		/*
266 		 * Assume this is the console if there's no eeprom info
267 		 * to be found.
268 		 */
269 		if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
270 			isconsole = fb_is_console(0);
271 	}
272 
273 #if 0
274 	/*
275 	 * We don't do any of the console handling here.  Instead,
276 	 * we let the bwtwo driver pick up the overlay plane and
277 	 * use it instead.  Rconsole should have better performance
278 	 * with the 1-bit depth.
279 	 *      -- Jason R. Thorpe <thorpej@NetBSD.org>
280 	 */
281 
282 	/*
283 	 * When the ROM has mapped in a cgfour display, the address
284 	 * maps only the video RAM, so in any case we have to map the
285 	 * registers ourselves.  We only need the video RAM if we are
286 	 * going to print characters via rconsole.
287 	 */
288 
289 	if (isconsole) {
290 		/* XXX this is kind of a waste */
291 		fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
292 					 PFOUR_COLOR_OFF_OVERLAY, ramsize);
293 	}
294 #endif
295 
296 	/* Map the Brooktree. */
297 	if (bus_space_map(oba->oba_bustag,
298 			  oba->oba_paddr + PFOUR_COLOR_OFF_CMAP,
299 			  sizeof(struct fbcontrol),
300 			  BUS_SPACE_MAP_LINEAR,
301 			  &bh) != 0) {
302 		printf("%s: cannot map control registers\n", self->dv_xname);
303 		return;
304 	}
305 	sc->sc_fbc = (volatile struct fbcontrol *)bh;
306 
307 #if 0	/* XXX thorpej ??? */
308 	/* tell the enable plane to look at the mono image */
309 	memset(ca->ca_ra.ra_vaddr, 0xff,
310 	    sc->sc_fb.fb_type.fb_width * sc->sc_fb.fb_type.fb_height / 8);
311 #endif
312 
313 	/* grab initial (current) color map */
314 	bt = &sc->sc_fbc->fbc_dac;
315 	bt->bt_addr = 0;
316 	for (i = 0; i < 256 * 3 / 4; i++)
317 		sc->sc_cmap.cm_chip[i] = bt->bt_cmap;
318 
319 	BT_INIT(bt, 0);
320 
321 #if 0	/* see above */
322 	if (isconsole) {
323 		printf(" (console)\n");
324 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
325 		fbrcons_init(fb);
326 #endif
327 	} else
328 #endif /* 0 */
329 		printf("\n");
330 
331 	/*
332 	 * Even though we're not using rconsole, we'd still like
333 	 * to notice if we're the console framebuffer.
334 	 */
335 	fb_attach(&sc->sc_fb, isconsole);
336 #endif
337 }
338 
339 int
340 cgeightopen(dev, flags, mode, p)
341 	dev_t dev;
342 	int flags, mode;
343 	struct proc *p;
344 {
345 	int unit = minor(dev);
346 
347 	if (unit >= cgeight_cd.cd_ndevs || cgeight_cd.cd_devs[unit] == NULL)
348 		return (ENXIO);
349 	return (0);
350 }
351 
352 int
353 cgeightioctl(dev, cmd, data, flags, p)
354 	dev_t dev;
355 	u_long cmd;
356 	caddr_t data;
357 	int flags;
358 	struct proc *p;
359 {
360 #if defined(SUN4)
361 	struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
362 	struct fbgattr *fba;
363 	int error;
364 
365 	switch (cmd) {
366 
367 	case FBIOGTYPE:
368 		*(struct fbtype *)data = sc->sc_fb.fb_type;
369 		break;
370 
371 	case FBIOGATTR:
372 		fba = (struct fbgattr *)data;
373 		fba->real_type = sc->sc_fb.fb_type.fb_type;
374 		fba->owner = 0;		/* XXX ??? */
375 		fba->fbtype = sc->sc_fb.fb_type;
376 		fba->sattr.flags = 0;
377 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
378 		fba->sattr.dev_specific[0] = -1;
379 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
380 		fba->emu_types[1] = -1;
381 		break;
382 
383 	case FBIOGETCMAP:
384 #define p ((struct fbcmap *)data)
385 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
386 
387 	case FBIOPUTCMAP:
388 		/* copy to software map */
389 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
390 		if (error)
391 			return (error);
392 		/* now blast them into the chip */
393 		/* XXX should use retrace interrupt */
394 		cgeightloadcmap(sc, p->index, p->count);
395 #undef p
396 		break;
397 
398 	case FBIOGVIDEO:
399 		*(int *)data = cgeight_get_video(sc);
400 		break;
401 
402 	case FBIOSVIDEO:
403 		cgeight_set_video(sc, *(int *)data);
404 		break;
405 
406 	default:
407 		return (ENOTTY);
408 	}
409 #endif
410 	return (0);
411 }
412 
413 /*
414  * Return the address that would map the given device at the given
415  * offset, allowing for the given protection, or return -1 for error.
416  *
417  * The cg8 maps it's overlay plane at 0 for 128K, followed by the
418  * enable plane for 128K, followed by the colour for as long as it
419  * goes. Starting at 8MB, it maps the ramdac for PAGE_SIZE, then the p4
420  * register for PAGE_SIZE, then the bootrom for 0x40000.
421  */
422 paddr_t
423 cgeightmmap(dev, off, prot)
424 	dev_t dev;
425 	off_t off;
426 	int prot;
427 {
428 	struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
429 	off_t poff;
430 
431 #define START_ENABLE	(128*1024)
432 #define START_COLOR	((128*1024) + (128*1024))
433 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
434 			    sc->sc_fb.fb_type.fb_height * 3)
435 #define END_COLOR	(START_COLOR + COLOR_SIZE)
436 #define START_SPECIAL	0x800000
437 #define PROMSIZE	0x40000
438 #define NOOVERLAY	(0x04000000)
439 
440 	if (off & PGOFSET)
441 		panic("cgeightmap");
442 
443 	if (off < 0)
444 		return (-1);
445 	else if ((u_int)off >= NOOVERLAY) {
446 		off -= NOOVERLAY;
447 
448 		/*
449 		 * X11 maps a huge chunk of the frame buffer; far more than
450 		 * there really is. We compensate by double-mapping the
451 		 * first page for as many other pages as it wants
452 		 */
453 		while ((u_int)off >= COLOR_SIZE)
454 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
455 
456 		poff = off + PFOUR_COLOR_OFF_COLOR;
457 	} else if ((u_int)off < START_ENABLE) {
458 		/*
459 		 * in overlay plane
460 		 */
461 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
462 	} else if ((u_int)off < START_COLOR) {
463 		/*
464 		 * in enable plane
465 		 */
466 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
467 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
468 		/*
469 		 * in colour plane
470 		 */
471 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
472 	} else if ((u_int)off < START_SPECIAL) {
473 		/*
474 		 * hole
475 		 */
476 		poff = 0;	/* XXX */
477 	} else if ((u_int)off == START_SPECIAL) {
478 		/*
479 		 * colour map (Brooktree)
480 		 */
481 		poff = PFOUR_COLOR_OFF_CMAP;
482 	} else if ((u_int)off == START_SPECIAL + PAGE_SIZE) {
483 		/*
484 		 * p4 register
485 		 */
486 		poff = 0;
487 	} else if ((u_int)off > (START_SPECIAL + (PAGE_SIZE * 2)) &&
488 	    (u_int) off < (START_SPECIAL + (PAGE_SIZE * 2) + PROMSIZE)) {
489 		/*
490 		 * rom
491 		 */
492 		poff = 0x8000 + (off - (START_SPECIAL + (PAGE_SIZE * 2)));
493 	} else
494 		return (-1);
495 
496 	return (bus_space_mmap(sc->sc_bustag,
497 		sc->sc_paddr, poff,
498 		prot, BUS_SPACE_MAP_LINEAR));
499 }
500 
501 #if defined(SUN4)
502 /*
503  * Undo the effect of an FBIOSVIDEO that turns the video off.
504  */
505 static void
506 cgeightunblank(dev)
507 	struct device *dev;
508 {
509 
510 	cgeight_set_video((struct cgeight_softc *)dev, 1);
511 }
512 
513 static int
514 cgeight_get_video(sc)
515 	struct cgeight_softc *sc;
516 {
517 
518 	return (fb_pfour_get_video(&sc->sc_fb));
519 }
520 
521 static void
522 cgeight_set_video(sc, enable)
523 	struct cgeight_softc *sc;
524 	int enable;
525 {
526 
527 	fb_pfour_set_video(&sc->sc_fb, enable);
528 }
529 
530 /*
531  * Load a subset of the current (new) colormap into the Brooktree DAC.
532  */
533 static void
534 cgeightloadcmap(sc, start, ncolors)
535 	register struct cgeight_softc *sc;
536 	register int start, ncolors;
537 {
538 	volatile struct bt_regs *bt;
539 	u_int *ip;
540 	int count;
541 
542 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
543 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
544 	bt = &sc->sc_fbc->fbc_dac;
545 	bt->bt_addr = BT_D4M4(start);
546 	while (--count >= 0)
547 		bt->bt_cmap = *ip++;
548 }
549 #endif
550