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