xref: /netbsd-src/sys/arch/sparc/dev/cgtwo.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: cgtwo.c,v 1.22 1997/05/24 20:16:12 pk 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 *, struct cfdata *, 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, cf, aux)
122 	struct device *parent;
123 	struct cfdata *cf;
124 	void *aux;
125 {
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)mapdev(ra->ra_reg, TMPMAP_VA, CG2_CTLREG_OFF, NBPG);
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 	/* Apparently, the pixels are 32-bit data space */
216 	sc->sc_phys.rr_iospace = PMAP_VME32;
217 	sc->sc_bustype = ca->ca_bustype;
218 
219 	if ((sc->sc_fb.fb_pixels = ca->ca_ra.ra_vaddr) == NULL && isconsole) {
220 		/* this probably cannot happen, but what the heck */
221 		sc->sc_fb.fb_pixels = mapiodev(&sc->sc_phys, CG2_PIXMAP_OFF,
222 					       CG2_PIXMAP_SIZE);
223 	}
224 #ifndef offsetof
225 #define	offsetof(type, member)  ((size_t)(&((type *)0)->member))
226 #endif
227 
228 	sc->sc_reg = (volatile struct cg2statusreg *)
229 	    mapiodev(ca->ca_ra.ra_reg,
230 		     CG2_ROPMEM_OFF + offsetof(struct cg2fb, status.reg),
231 		     sizeof(struct cg2statusreg));
232 
233 	sc->sc_cmap = (volatile u_short *)
234 	    mapiodev(ca->ca_ra.ra_reg,
235 		     CG2_ROPMEM_OFF + offsetof(struct cg2fb, redmap[0]),
236 		     3 * CG2_CMSIZE);
237 
238 	if (isconsole) {
239 		printf(" (console)\n");
240 #ifdef RASTERCONSOLE
241 		fbrcons_init(&sc->sc_fb);
242 #endif
243 	} else
244 		printf("\n");
245 
246 	if (node == fbnode || CPU_ISSUN4)
247 		fb_attach(&sc->sc_fb, isconsole);
248 }
249 
250 int
251 cgtwoopen(dev, flags, mode, p)
252 	dev_t dev;
253 	int flags, mode;
254 	struct proc *p;
255 {
256 	int unit = minor(dev);
257 
258 	if (unit >= cgtwo_cd.cd_ndevs || cgtwo_cd.cd_devs[unit] == NULL)
259 		return (ENXIO);
260 	return (0);
261 }
262 
263 int
264 cgtwoclose(dev, flags, mode, p)
265 	dev_t dev;
266 	int flags, mode;
267 	struct proc *p;
268 {
269 
270 	return (0);
271 }
272 
273 int
274 cgtwoioctl(dev, cmd, data, flags, p)
275 	dev_t dev;
276 	u_long cmd;
277 	register caddr_t data;
278 	int flags;
279 	struct proc *p;
280 {
281 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
282 	register struct fbgattr *fba;
283 
284 	switch (cmd) {
285 
286 	case FBIOGTYPE:
287 		*(struct fbtype *)data = sc->sc_fb.fb_type;
288 		break;
289 
290 	case FBIOGATTR:
291 		fba = (struct fbgattr *)data;
292 		fba->real_type = sc->sc_fb.fb_type.fb_type;
293 		fba->owner = 0;		/* XXX ??? */
294 		fba->fbtype = sc->sc_fb.fb_type;
295 		fba->sattr.flags = 0;
296 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
297 		fba->sattr.dev_specific[0] = -1;
298 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
299 		fba->emu_types[1] = -1;
300 		break;
301 
302 	case FBIOGETCMAP:
303 		return cgtwogetcmap(sc, (struct fbcmap *) data);
304 
305 	case FBIOPUTCMAP:
306 		return cgtwoputcmap(sc, (struct fbcmap *) data);
307 
308 	case FBIOGVIDEO:
309 		*(int *)data = sc->sc_reg->video_enab;
310 		break;
311 
312 	case FBIOSVIDEO:
313 		sc->sc_reg->video_enab = (*(int*)data) & 1;
314 		break;
315 
316 	default:
317 		return (ENOTTY);
318 	}
319 	return (0);
320 }
321 
322 int
323 cgtwopoll(dev, events, p)
324 	dev_t dev;
325 	int events;
326 	struct proc *p;
327 {
328 
329 	return (seltrue(dev, events, p));
330 }
331 
332 /*
333  * Undo the effect of an FBIOSVIDEO that turns the video off.
334  */
335 static void
336 cgtwounblank(dev)
337 	struct device *dev;
338 {
339 	struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
340 	sc->sc_reg->video_enab = 1;
341 }
342 
343 /*
344  */
345 int
346 cgtwogetcmap(sc, cmap)
347 	register struct cgtwo_softc *sc;
348 	register struct fbcmap *cmap;
349 {
350 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
351 	int error, start, count, ecount;
352 	register u_int i;
353 	register volatile u_short *p;
354 
355 	start = cmap->index;
356 	count = cmap->count;
357 	ecount = start + count;
358 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
359 		return (EINVAL);
360 
361 	/* XXX - Wait for retrace? */
362 
363 	/* Copy hardware to local arrays. */
364 	p = &sc_redmap(sc)[start];
365 	for (i = start; i < ecount; i++)
366 		red[i] = *p++;
367 	p = &sc_greenmap(sc)[start];
368 	for (i = start; i < ecount; i++)
369 		green[i] = *p++;
370 	p = &sc_bluemap(sc)[start];
371 	for (i = start; i < ecount; i++)
372 		blue[i] = *p++;
373 
374 	/* Copy local arrays to user space. */
375 	if ((error = copyout(red + start, cmap->red, count)) != 0)
376 		return (error);
377 	if ((error = copyout(green + start, cmap->green, count)) != 0)
378 		return (error);
379 	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
380 		return (error);
381 
382 	return (0);
383 }
384 
385 /*
386  */
387 int
388 cgtwoputcmap(sc, cmap)
389 	register struct cgtwo_softc *sc;
390 	register struct fbcmap *cmap;
391 {
392 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
393 	int error, start, count, ecount;
394 	register u_int i;
395 	register volatile u_short *p;
396 
397 	start = cmap->index;
398 	count = cmap->count;
399 	ecount = start + count;
400 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
401 		return (EINVAL);
402 
403 	/* Copy from user space to local arrays. */
404 	if ((error = copyin(cmap->red, red + start, count)) != 0)
405 		return (error);
406 	if ((error = copyin(cmap->green, green + start, count)) != 0)
407 		return (error);
408 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
409 		return (error);
410 
411 	/* XXX - Wait for retrace? */
412 
413 	/* Copy from local arrays to hardware. */
414 	p = &sc_redmap(sc)[start];
415 	for (i = start; i < ecount; i++)
416 		*p++ = red[i];
417 	p = &sc_greenmap(sc)[start];
418 	for (i = start; i < ecount; i++)
419 		*p++ = green[i];
420 	p = &sc_bluemap(sc)[start];
421 	for (i = start; i < ecount; i++)
422 		*p++ = blue[i];
423 
424 	return (0);
425 }
426 
427 /*
428  * Return the address that would map the given device at the given
429  * offset, allowing for the given protection, or return -1 for error.
430  */
431 int
432 cgtwommap(dev, off, prot)
433 	dev_t dev;
434 	int off, prot;
435 {
436 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
437 
438 	if (off & PGOFSET)
439 		panic("cgtwommap");
440 
441 	if ((unsigned)off >= sc->sc_fb.fb_type.fb_size)
442 		return (-1);
443 
444 	return (REG2PHYS(&sc->sc_phys, off) | PMAP_NC);
445 }
446