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