xref: /netbsd-src/sys/arch/sparc/dev/cgtwo.c (revision 81b108b45f75f89f1e3ffad9fb6f074e771c0935)
1 /*	$NetBSD: cgtwo.c,v 1.16 1996/05/18 12:19:14 mrg 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 int		cgtwoopen __P((dev_t, int, int, struct proc *));
95 int		cgtwoclose __P((dev_t, int, int, struct proc *));
96 int		cgtwoioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
97 int		cgtwommap __P((dev_t, int, int));
98 static void	cgtwounblank __P((struct device *));
99 int		cgtwogetcmap __P((struct cgtwo_softc *, struct fbcmap *));
100 int		cgtwoputcmap __P((struct cgtwo_softc *, struct fbcmap *));
101 
102 struct cfattach cgtwo_ca = {
103 	sizeof(struct cgtwo_softc), cgtwomatch, cgtwoattach
104 };
105 
106 struct cfdriver cgtwo_cd = {
107 	NULL, "cgtwo", DV_DULL
108 };
109 
110 /* frame buffer generic driver */
111 static struct fbdriver cgtwofbdriver = {
112 	cgtwounblank, cgtwoopen, cgtwoclose, cgtwoioctl, cgtwommap
113 };
114 
115 extern int fbnode;
116 extern struct tty *fbconstty;
117 
118 /*
119  * Match a cgtwo.
120  */
121 int
122 cgtwomatch(parent, vcf, aux)
123 	struct device *parent;
124 	void *vcf, *aux;
125 {
126 	struct cfdata *cf = vcf;
127 	struct confargs *ca = aux;
128 	struct romaux *ra = &ca->ca_ra;
129 #if defined(SUN4)
130 	caddr_t tmp;
131 #endif
132 
133 	/*
134 	 * Mask out invalid flags from the user.
135 	 */
136 	cf->cf_flags &= FB_USERMASK;
137 
138 	if (ca->ca_bustype != BUS_VME16)
139 		return (0);
140 
141 	if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
142 		return (0);
143 
144 #if defined(SUN4)
145 	if (!CPU_ISSUN4 || cf->cf_unit != 0)
146 		return (0);
147 
148 	/* XXX - Must do our own mapping at CG2_CTLREG_OFF */
149 	bus_untmp();
150 	tmp = (caddr_t)bus_tmp(ra->ra_paddr + CG2_CTLREG_OFF, ca->ca_bustype);
151 	if (probeget(tmp, 2) != -1)
152 		return 1;
153 #endif
154 	return 0;
155 }
156 
157 /*
158  * Attach a display.  We need to notice if it is the console, too.
159  */
160 void
161 cgtwoattach(parent, self, args)
162 	struct device *parent, *self;
163 	void *args;
164 {
165 	register struct cgtwo_softc *sc = (struct cgtwo_softc *)self;
166 	register struct confargs *ca = args;
167 	register int node = 0;
168 	int isconsole = 0;
169 	char *nam = NULL;
170 
171 	sc->sc_fb.fb_driver = &cgtwofbdriver;
172 	sc->sc_fb.fb_device = &sc->sc_dev;
173 	sc->sc_fb.fb_type.fb_type = FBTYPE_SUN2COLOR;
174 	sc->sc_fb.fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
175 
176 	switch (ca->ca_bustype) {
177 	case BUS_VME16:
178 		node = 0;
179 		nam = "cgtwo";
180 		break;
181 
182 	default:
183 		panic("cgtwoattach: impossible bustype");
184 		/* NOTREACHED */
185 	}
186 
187 	sc->sc_fb.fb_type.fb_depth = 8;
188 	fb_setsize(&sc->sc_fb, sc->sc_fb.fb_type.fb_depth,
189 	    1152, 900, node, ca->ca_bustype);
190 
191 	sc->sc_fb.fb_type.fb_cmsize = 256;
192 	sc->sc_fb.fb_type.fb_size = roundup(CG2_MAPPED_SIZE, NBPG);
193 	printf(": %s, %d x %d", nam,
194 	    sc->sc_fb.fb_type.fb_width, sc->sc_fb.fb_type.fb_height);
195 
196 	/*
197 	 * When the ROM has mapped in a cgtwo display, the address
198 	 * maps only the video RAM, so in any case we have to map the
199 	 * registers ourselves.  We only need the video RAM if we are
200 	 * going to print characters via rconsole.
201 	 */
202 #if defined(SUN4)
203 	if (CPU_ISSUN4) {
204 		struct eeprom *eep = (struct eeprom *)eeprom_va;
205 		/*
206 		 * Assume this is the console if there's no eeprom info
207 		 * to be found.
208 		 */
209 		if (eep == NULL || eep->eeConsole == EE_CONS_COLOR)
210 			isconsole = (fbconstty != NULL);
211 		else
212 			isconsole = 0;
213 	}
214 #endif
215 	sc->sc_phys = ca->ca_ra.ra_reg[0];
216 	sc->sc_bustype = ca->ca_bustype;
217 
218 	if ((sc->sc_fb.fb_pixels = ca->ca_ra.ra_vaddr) == NULL && isconsole) {
219 		/* this probably cannot happen, but what the heck */
220 		sc->sc_fb.fb_pixels = mapiodev(ca->ca_ra.ra_reg, CG2_PIXMAP_OFF,
221 					       CG2_PIXMAP_SIZE,
222 					       PMAP_VME32/*ca->ca_bustype*/);
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), ca->ca_bustype);
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, ca->ca_bustype);
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 /*
323  * Undo the effect of an FBIOSVIDEO that turns the video off.
324  */
325 static void
326 cgtwounblank(dev)
327 	struct device *dev;
328 {
329 	struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
330 	sc->sc_reg->video_enab = 1;
331 }
332 
333 /*
334  */
335 int
336 cgtwogetcmap(sc, cmap)
337 	register struct cgtwo_softc *sc;
338 	register struct fbcmap *cmap;
339 {
340 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
341 	int error, start, count, ecount;
342 	register u_int i;
343 	register volatile u_short *p;
344 
345 	start = cmap->index;
346 	count = cmap->count;
347 	ecount = start + count;
348 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
349 		return (EINVAL);
350 
351 	/* XXX - Wait for retrace? */
352 
353 	/* Copy hardware to local arrays. */
354 	p = &sc_redmap(sc)[start];
355 	for (i = start; i < ecount; i++)
356 		red[i] = *p++;
357 	p = &sc_greenmap(sc)[start];
358 	for (i = start; i < ecount; i++)
359 		green[i] = *p++;
360 	p = &sc_bluemap(sc)[start];
361 	for (i = start; i < ecount; i++)
362 		blue[i] = *p++;
363 
364 	/* Copy local arrays to user space. */
365 	if ((error = copyout(red + start, cmap->red, count)) != 0)
366 		return (error);
367 	if ((error = copyout(green + start, cmap->green, count)) != 0)
368 		return (error);
369 	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
370 		return (error);
371 
372 	return (0);
373 }
374 
375 /*
376  */
377 int
378 cgtwoputcmap(sc, cmap)
379 	register struct cgtwo_softc *sc;
380 	register struct fbcmap *cmap;
381 {
382 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
383 	int error, start, count, ecount;
384 	register u_int i;
385 	register volatile u_short *p;
386 
387 	start = cmap->index;
388 	count = cmap->count;
389 	ecount = start + count;
390 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
391 		return (EINVAL);
392 
393 	/* Copy from user space to local arrays. */
394 	if ((error = copyin(cmap->red, red + start, count)) != 0)
395 		return (error);
396 	if ((error = copyin(cmap->green, green + start, count)) != 0)
397 		return (error);
398 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
399 		return (error);
400 
401 	/* XXX - Wait for retrace? */
402 
403 	/* Copy from local arrays to hardware. */
404 	p = &sc_redmap(sc)[start];
405 	for (i = start; i < ecount; i++)
406 		*p++ = red[i];
407 	p = &sc_greenmap(sc)[start];
408 	for (i = start; i < ecount; i++)
409 		*p++ = green[i];
410 	p = &sc_bluemap(sc)[start];
411 	for (i = start; i < ecount; i++)
412 		*p++ = blue[i];
413 
414 	return (0);
415 }
416 
417 /*
418  * Return the address that would map the given device at the given
419  * offset, allowing for the given protection, or return -1 for error.
420  */
421 int
422 cgtwommap(dev, off, prot)
423 	dev_t dev;
424 	int off, prot;
425 {
426 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
427 
428 	if (off & PGOFSET)
429 		panic("cgtwommap");
430 
431 	if ((unsigned)off >= sc->sc_fb.fb_type.fb_size)
432 		return (-1);
433 
434 	return (REG2PHYS(&sc->sc_phys, off, PMAP_VME32/*sc->sc_bustype*/) | PMAP_NC);
435 }
436