xref: /netbsd-src/sys/arch/sparc/dev/cgtwo.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: cgtwo.c,v 1.19 1996/10/13 02:59:41 christos Exp $ */
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *	This product includes software developed by the University of
14  *	California, Lawrence Berkeley Laboratory.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by the University of
27  *	California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *	from: @(#)cgthree.c	8.2 (Berkeley) 10/30/93
45  */
46 
47 /*
48  * color display (cgtwo) driver.
49  *
50  * Does not handle interrupts, even though they can occur.
51  *
52  * XXX should defer colormap updates to vertical retrace interrupts
53  */
54 
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/buf.h>
58 #include <sys/device.h>
59 #include <sys/ioctl.h>
60 #include <sys/malloc.h>
61 #include <sys/mman.h>
62 #include <sys/tty.h>
63 #include <sys/conf.h>
64 
65 #include <vm/vm.h>
66 
67 #include <machine/fbio.h>
68 #include <machine/autoconf.h>
69 #include <machine/pmap.h>
70 #include <machine/fbvar.h>
71 #if defined(SUN4)
72 #include <machine/eeprom.h>
73 #endif
74 #include <machine/conf.h>
75 #include <machine/cgtworeg.h>
76 
77 
78 /* per-display variables */
79 struct cgtwo_softc {
80 	struct	device sc_dev;		/* base device */
81 	struct	fbdevice sc_fb;		/* frame buffer device */
82 	struct rom_reg	sc_phys;	/* display RAM (phys addr) */
83 	int	sc_bustype;		/* type of bus we live on */
84 	volatile struct cg2statusreg *sc_reg;	/* CG2 control registers */
85 	volatile u_short *sc_cmap;
86 #define sc_redmap(sc)	((sc)->sc_cmap)
87 #define sc_greenmap(sc)	((sc)->sc_cmap + CG2_CMSIZE)
88 #define sc_bluemap(sc)	((sc)->sc_cmap + 2 * CG2_CMSIZE)
89 };
90 
91 /* autoconfiguration driver */
92 static void	cgtwoattach __P((struct device *, struct device *, void *));
93 static int	cgtwomatch __P((struct device *, void *, void *));
94 static void	cgtwounblank __P((struct device *));
95 int		cgtwogetcmap __P((struct cgtwo_softc *, struct fbcmap *));
96 int		cgtwoputcmap __P((struct cgtwo_softc *, struct fbcmap *));
97 
98 /* cdevsw prototypes */
99 cdev_decl(cgtwo);
100 
101 struct cfattach cgtwo_ca = {
102 	sizeof(struct cgtwo_softc), cgtwomatch, cgtwoattach
103 };
104 
105 struct cfdriver cgtwo_cd = {
106 	NULL, "cgtwo", DV_DULL
107 };
108 
109 /* frame buffer generic driver */
110 static struct fbdriver cgtwofbdriver = {
111 	cgtwounblank, cgtwoopen, cgtwoclose, cgtwoioctl, cgtwopoll, cgtwommap
112 };
113 
114 extern int fbnode;
115 extern struct tty *fbconstty;
116 
117 /*
118  * Match a cgtwo.
119  */
120 int
121 cgtwomatch(parent, vcf, aux)
122 	struct device *parent;
123 	void *vcf, *aux;
124 {
125 	struct cfdata *cf = vcf;
126 	struct confargs *ca = aux;
127 	struct romaux *ra = &ca->ca_ra;
128 #if defined(SUN4)
129 	caddr_t tmp;
130 #endif
131 
132 	/*
133 	 * Mask out invalid flags from the user.
134 	 */
135 	cf->cf_flags &= FB_USERMASK;
136 
137 	if (ca->ca_bustype != BUS_VME16)
138 		return (0);
139 
140 	if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
141 		return (0);
142 
143 #if defined(SUN4)
144 	if (!CPU_ISSUN4 || cf->cf_unit != 0)
145 		return (0);
146 
147 	/* XXX - Must do our own mapping at CG2_CTLREG_OFF */
148 	bus_untmp();
149 	tmp = (caddr_t)bus_tmp(ra->ra_paddr + CG2_CTLREG_OFF, ca->ca_bustype);
150 	if (probeget(tmp, 2) != -1)
151 		return 1;
152 #endif
153 	return 0;
154 }
155 
156 /*
157  * Attach a display.  We need to notice if it is the console, too.
158  */
159 void
160 cgtwoattach(parent, self, args)
161 	struct device *parent, *self;
162 	void *args;
163 {
164 	register struct cgtwo_softc *sc = (struct cgtwo_softc *)self;
165 	register struct confargs *ca = args;
166 	register int node = 0;
167 	int isconsole = 0;
168 	char *nam = NULL;
169 
170 	sc->sc_fb.fb_driver = &cgtwofbdriver;
171 	sc->sc_fb.fb_device = &sc->sc_dev;
172 	sc->sc_fb.fb_type.fb_type = FBTYPE_SUN2COLOR;
173 	sc->sc_fb.fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
174 
175 	switch (ca->ca_bustype) {
176 	case BUS_VME16:
177 		node = 0;
178 		nam = "cgtwo";
179 		break;
180 
181 	default:
182 		panic("cgtwoattach: impossible bustype");
183 		/* NOTREACHED */
184 	}
185 
186 	sc->sc_fb.fb_type.fb_depth = 8;
187 	fb_setsize(&sc->sc_fb, sc->sc_fb.fb_type.fb_depth,
188 	    1152, 900, node, ca->ca_bustype);
189 
190 	sc->sc_fb.fb_type.fb_cmsize = 256;
191 	sc->sc_fb.fb_type.fb_size = roundup(CG2_MAPPED_SIZE, NBPG);
192 	printf(": %s, %d x %d", nam,
193 	    sc->sc_fb.fb_type.fb_width, sc->sc_fb.fb_type.fb_height);
194 
195 	/*
196 	 * When the ROM has mapped in a cgtwo display, the address
197 	 * maps only the video RAM, so in any case we have to map the
198 	 * registers ourselves.  We only need the video RAM if we are
199 	 * going to print characters via rconsole.
200 	 */
201 #if defined(SUN4)
202 	if (CPU_ISSUN4) {
203 		struct eeprom *eep = (struct eeprom *)eeprom_va;
204 		/*
205 		 * Assume this is the console if there's no eeprom info
206 		 * to be found.
207 		 */
208 		if (eep == NULL || eep->eeConsole == EE_CONS_COLOR)
209 			isconsole = (fbconstty != NULL);
210 		else
211 			isconsole = 0;
212 	}
213 #endif
214 	sc->sc_phys = ca->ca_ra.ra_reg[0];
215 	sc->sc_bustype = ca->ca_bustype;
216 
217 	if ((sc->sc_fb.fb_pixels = ca->ca_ra.ra_vaddr) == NULL && isconsole) {
218 		/* this probably cannot happen, but what the heck */
219 		sc->sc_fb.fb_pixels = mapiodev(ca->ca_ra.ra_reg, CG2_PIXMAP_OFF,
220 					       CG2_PIXMAP_SIZE,
221 					       PMAP_VME32/*ca->ca_bustype*/);
222 	}
223 #ifndef offsetof
224 #define	offsetof(type, member)  ((size_t)(&((type *)0)->member))
225 #endif
226 
227 	sc->sc_reg = (volatile struct cg2statusreg *)
228 	    mapiodev(ca->ca_ra.ra_reg,
229 		     CG2_ROPMEM_OFF + offsetof(struct cg2fb, status.reg),
230 		     sizeof(struct cg2statusreg), ca->ca_bustype);
231 
232 	sc->sc_cmap = (volatile u_short *)
233 	    mapiodev(ca->ca_ra.ra_reg,
234 		     CG2_ROPMEM_OFF + offsetof(struct cg2fb, redmap[0]),
235 		     3 * CG2_CMSIZE, ca->ca_bustype);
236 
237 	if (isconsole) {
238 		printf(" (console)\n");
239 #ifdef RASTERCONSOLE
240 		fbrcons_init(&sc->sc_fb);
241 #endif
242 	} else
243 		printf("\n");
244 
245 	if (node == fbnode || CPU_ISSUN4)
246 		fb_attach(&sc->sc_fb, isconsole);
247 }
248 
249 int
250 cgtwoopen(dev, flags, mode, p)
251 	dev_t dev;
252 	int flags, mode;
253 	struct proc *p;
254 {
255 	int unit = minor(dev);
256 
257 	if (unit >= cgtwo_cd.cd_ndevs || cgtwo_cd.cd_devs[unit] == NULL)
258 		return (ENXIO);
259 	return (0);
260 }
261 
262 int
263 cgtwoclose(dev, flags, mode, p)
264 	dev_t dev;
265 	int flags, mode;
266 	struct proc *p;
267 {
268 
269 	return (0);
270 }
271 
272 int
273 cgtwoioctl(dev, cmd, data, flags, p)
274 	dev_t dev;
275 	u_long cmd;
276 	register caddr_t data;
277 	int flags;
278 	struct proc *p;
279 {
280 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
281 	register struct fbgattr *fba;
282 
283 	switch (cmd) {
284 
285 	case FBIOGTYPE:
286 		*(struct fbtype *)data = sc->sc_fb.fb_type;
287 		break;
288 
289 	case FBIOGATTR:
290 		fba = (struct fbgattr *)data;
291 		fba->real_type = sc->sc_fb.fb_type.fb_type;
292 		fba->owner = 0;		/* XXX ??? */
293 		fba->fbtype = sc->sc_fb.fb_type;
294 		fba->sattr.flags = 0;
295 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
296 		fba->sattr.dev_specific[0] = -1;
297 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
298 		fba->emu_types[1] = -1;
299 		break;
300 
301 	case FBIOGETCMAP:
302 		return cgtwogetcmap(sc, (struct fbcmap *) data);
303 
304 	case FBIOPUTCMAP:
305 		return cgtwoputcmap(sc, (struct fbcmap *) data);
306 
307 	case FBIOGVIDEO:
308 		*(int *)data = sc->sc_reg->video_enab;
309 		break;
310 
311 	case FBIOSVIDEO:
312 		sc->sc_reg->video_enab = (*(int*)data) & 1;
313 		break;
314 
315 	default:
316 		return (ENOTTY);
317 	}
318 	return (0);
319 }
320 
321 int
322 cgtwopoll(dev, events, p)
323 	dev_t dev;
324 	int events;
325 	struct proc *p;
326 {
327 
328 	return (seltrue(dev, events, p));
329 }
330 
331 /*
332  * Undo the effect of an FBIOSVIDEO that turns the video off.
333  */
334 static void
335 cgtwounblank(dev)
336 	struct device *dev;
337 {
338 	struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
339 	sc->sc_reg->video_enab = 1;
340 }
341 
342 /*
343  */
344 int
345 cgtwogetcmap(sc, cmap)
346 	register struct cgtwo_softc *sc;
347 	register struct fbcmap *cmap;
348 {
349 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
350 	int error, start, count, ecount;
351 	register u_int i;
352 	register volatile u_short *p;
353 
354 	start = cmap->index;
355 	count = cmap->count;
356 	ecount = start + count;
357 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
358 		return (EINVAL);
359 
360 	/* XXX - Wait for retrace? */
361 
362 	/* Copy hardware to local arrays. */
363 	p = &sc_redmap(sc)[start];
364 	for (i = start; i < ecount; i++)
365 		red[i] = *p++;
366 	p = &sc_greenmap(sc)[start];
367 	for (i = start; i < ecount; i++)
368 		green[i] = *p++;
369 	p = &sc_bluemap(sc)[start];
370 	for (i = start; i < ecount; i++)
371 		blue[i] = *p++;
372 
373 	/* Copy local arrays to user space. */
374 	if ((error = copyout(red + start, cmap->red, count)) != 0)
375 		return (error);
376 	if ((error = copyout(green + start, cmap->green, count)) != 0)
377 		return (error);
378 	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
379 		return (error);
380 
381 	return (0);
382 }
383 
384 /*
385  */
386 int
387 cgtwoputcmap(sc, cmap)
388 	register struct cgtwo_softc *sc;
389 	register struct fbcmap *cmap;
390 {
391 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
392 	int error, start, count, ecount;
393 	register u_int i;
394 	register volatile u_short *p;
395 
396 	start = cmap->index;
397 	count = cmap->count;
398 	ecount = start + count;
399 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
400 		return (EINVAL);
401 
402 	/* Copy from user space to local arrays. */
403 	if ((error = copyin(cmap->red, red + start, count)) != 0)
404 		return (error);
405 	if ((error = copyin(cmap->green, green + start, count)) != 0)
406 		return (error);
407 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
408 		return (error);
409 
410 	/* XXX - Wait for retrace? */
411 
412 	/* Copy from local arrays to hardware. */
413 	p = &sc_redmap(sc)[start];
414 	for (i = start; i < ecount; i++)
415 		*p++ = red[i];
416 	p = &sc_greenmap(sc)[start];
417 	for (i = start; i < ecount; i++)
418 		*p++ = green[i];
419 	p = &sc_bluemap(sc)[start];
420 	for (i = start; i < ecount; i++)
421 		*p++ = blue[i];
422 
423 	return (0);
424 }
425 
426 /*
427  * Return the address that would map the given device at the given
428  * offset, allowing for the given protection, or return -1 for error.
429  */
430 int
431 cgtwommap(dev, off, prot)
432 	dev_t dev;
433 	int off, prot;
434 {
435 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
436 
437 	if (off & PGOFSET)
438 		panic("cgtwommap");
439 
440 	if ((unsigned)off >= sc->sc_fb.fb_type.fb_size)
441 		return (-1);
442 
443 	return (REG2PHYS(&sc->sc_phys, off, PMAP_VME32/*sc->sc_bustype*/) | PMAP_NC);
444 }
445