xref: /netbsd-src/sys/arch/sparc/dev/cgtwo.c (revision 89c5a767f8fc7a4633b2d409966e2becbb98ff92)
1 /*	$NetBSD: cgtwo.c,v 1.30 1999/06/30 15:18:58 drochner 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 
72 #include <dev/vme/vmevar.h>
73 
74 #include <machine/eeprom.h>
75 #include <machine/conf.h>
76 #include <machine/cgtworeg.h>
77 
78 
79 /* per-display variables */
80 struct cgtwo_softc {
81 	struct	device sc_dev;		/* base device */
82 	struct	fbdevice sc_fb;		/* frame buffer device */
83 	vme_addr_t		sc_paddr;
84 	vme_chipset_tag_t	sc_ct;
85 	bus_space_tag_t		sc_bt;
86 	volatile struct cg2statusreg *sc_reg;	/* CG2 control registers */
87 	volatile u_short *sc_cmap;
88 #define sc_redmap(sc)	((sc)->sc_cmap)
89 #define sc_greenmap(sc)	((sc)->sc_cmap + CG2_CMSIZE)
90 #define sc_bluemap(sc)	((sc)->sc_cmap + 2 * CG2_CMSIZE)
91 };
92 
93 /* autoconfiguration driver */
94 static void	cgtwoattach __P((struct device *, struct device *, void *));
95 static int	cgtwomatch __P((struct device *, struct cfdata *, void *));
96 static void	cgtwounblank __P((struct device *));
97 int		cgtwogetcmap __P((struct cgtwo_softc *, struct fbcmap *));
98 int		cgtwoputcmap __P((struct cgtwo_softc *, struct fbcmap *));
99 
100 /* cdevsw prototypes */
101 cdev_decl(cgtwo);
102 
103 struct cfattach cgtwo_ca = {
104 	sizeof(struct cgtwo_softc), cgtwomatch, cgtwoattach
105 };
106 
107 extern struct cfdriver cgtwo_cd;
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 vme_attach_args	*va = aux;
127 	vme_chipset_tag_t	ct = va->va_vct;
128 	vme_am_t		mod;
129 
130 	/*
131 	 * Mask out invalid flags from the user.
132 	 */
133 	cf->cf_flags &= FB_USERMASK;
134 
135 	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
136 	if (vme_probe(ct, va->r[0].offset + CG2_CTLREG_OFF, 2, mod, VME_D16,
137 			  0, 0)) {
138 		return (0);
139 	}
140 
141 	return (1);
142 }
143 
144 /*
145  * Attach a display.  We need to notice if it is the console, too.
146  */
147 void
148 cgtwoattach(parent, self, aux)
149 	struct device *parent, *self;
150 	void *aux;
151 {
152 	struct vme_attach_args	*va = aux;
153 	vme_chipset_tag_t	ct = va->va_vct;
154 	bus_space_tag_t		bt;
155 	bus_space_handle_t	bh;
156 	vme_am_t		mod;
157 	vme_mapresc_t resc;
158 	struct cgtwo_softc *sc = (struct cgtwo_softc *)self;
159 	struct fbdevice *fb = &sc->sc_fb;
160 	struct eeprom *eep = (struct eeprom *)eeprom_va;
161 	int isconsole = 0;
162 	char *nam = NULL;
163 
164 	sc->sc_ct = ct;
165 	fb->fb_driver = &cgtwofbdriver;
166 	fb->fb_device = &sc->sc_dev;
167 	fb->fb_type.fb_type = FBTYPE_SUN2COLOR;
168 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
169 
170 	fb->fb_type.fb_depth = 8;
171 	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
172 
173 	fb->fb_type.fb_cmsize = 256;
174 	fb->fb_type.fb_size = roundup(CG2_MAPPED_SIZE, NBPG);
175 	printf(": %s, %d x %d", nam,
176 	       fb->fb_type.fb_width, fb->fb_type.fb_height);
177 
178 	/*
179 	 * When the ROM has mapped in a cgtwo display, the address
180 	 * maps only the video RAM, so in any case we have to map the
181 	 * registers ourselves.  We only need the video RAM if we are
182 	 * going to print characters via rconsole.
183 	 */
184 	sc->sc_paddr = va->r[0].offset;
185 	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
186 
187 	if (vme_space_map(ct, sc->sc_paddr + CG2_ROPMEM_OFF +
188 				offsetof(struct cg2fb, status.reg),
189 			sizeof(struct cg2statusreg), mod, VME_D16, 0,
190 			&bt, &bh, &resc) != 0)
191 		panic("cgtwo: vme_map status");
192 	sc->sc_bt = bt;
193 	sc->sc_reg = (volatile struct cg2statusreg *)bh; /* XXX */
194 
195 	if (vme_space_map(ct, sc->sc_paddr + CG2_ROPMEM_OFF +
196 				offsetof(struct cg2fb, redmap[0]),
197 			3 * CG2_CMSIZE, mod, VME_D16, 0,
198 			&bt, &bh, &resc) != 0)
199 		panic("cgtwo: vme_map cmap");
200 	sc->sc_cmap = (volatile u_short *)bh; /* XXX */
201 
202 	/*
203 	 * Assume this is the console if there's no eeprom info
204 	 * to be found.
205 	 */
206 	if (eep == NULL || eep->eeConsole == EE_CONS_COLOR)
207 		isconsole = (fbconstty != NULL);
208 	else
209 		isconsole = 0;
210 
211 	if (isconsole) {
212 		if (vme_space_map(ct, sc->sc_paddr + CG2_PIXMAP_OFF,
213 				CG2_PIXMAP_SIZE, mod, VME_D16, 0,
214 				  &bt, &bh, &resc) != 0)
215 			panic("cgtwo: vme_map pixels");
216 
217 		fb->fb_pixels = (caddr_t)bh; /* XXX */
218 		printf(" (console)\n");
219 #ifdef RASTERCONSOLE
220 		fbrcons_init(fb);
221 #endif
222 	} else
223 		printf("\n");
224 
225 	fb_attach(fb, isconsole);
226 }
227 
228 int
229 cgtwoopen(dev, flags, mode, p)
230 	dev_t dev;
231 	int flags, mode;
232 	struct proc *p;
233 {
234 	int unit = minor(dev);
235 
236 	if (unit >= cgtwo_cd.cd_ndevs || cgtwo_cd.cd_devs[unit] == NULL)
237 		return (ENXIO);
238 	return (0);
239 }
240 
241 int
242 cgtwoclose(dev, flags, mode, p)
243 	dev_t dev;
244 	int flags, mode;
245 	struct proc *p;
246 {
247 
248 	return (0);
249 }
250 
251 int
252 cgtwoioctl(dev, cmd, data, flags, p)
253 	dev_t dev;
254 	u_long cmd;
255 	register caddr_t data;
256 	int flags;
257 	struct proc *p;
258 {
259 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
260 	register struct fbgattr *fba;
261 
262 	switch (cmd) {
263 
264 	case FBIOGTYPE:
265 		*(struct fbtype *)data = sc->sc_fb.fb_type;
266 		break;
267 
268 	case FBIOGATTR:
269 		fba = (struct fbgattr *)data;
270 		fba->real_type = sc->sc_fb.fb_type.fb_type;
271 		fba->owner = 0;		/* XXX ??? */
272 		fba->fbtype = sc->sc_fb.fb_type;
273 		fba->sattr.flags = 0;
274 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
275 		fba->sattr.dev_specific[0] = -1;
276 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
277 		fba->emu_types[1] = -1;
278 		break;
279 
280 	case FBIOGETCMAP:
281 		return cgtwogetcmap(sc, (struct fbcmap *) data);
282 
283 	case FBIOPUTCMAP:
284 		return cgtwoputcmap(sc, (struct fbcmap *) data);
285 
286 	case FBIOGVIDEO:
287 		*(int *)data = sc->sc_reg->video_enab;
288 		break;
289 
290 	case FBIOSVIDEO:
291 		sc->sc_reg->video_enab = (*(int*)data) & 1;
292 		break;
293 
294 	default:
295 		return (ENOTTY);
296 	}
297 	return (0);
298 }
299 
300 int
301 cgtwopoll(dev, events, p)
302 	dev_t dev;
303 	int events;
304 	struct proc *p;
305 {
306 
307 	return (seltrue(dev, events, p));
308 }
309 
310 /*
311  * Undo the effect of an FBIOSVIDEO that turns the video off.
312  */
313 static void
314 cgtwounblank(dev)
315 	struct device *dev;
316 {
317 	struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
318 	sc->sc_reg->video_enab = 1;
319 }
320 
321 /*
322  */
323 int
324 cgtwogetcmap(sc, cmap)
325 	register struct cgtwo_softc *sc;
326 	register struct fbcmap *cmap;
327 {
328 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
329 	int error, start, count, ecount;
330 	register u_int i;
331 	register volatile u_short *p;
332 
333 	start = cmap->index;
334 	count = cmap->count;
335 	ecount = start + count;
336 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
337 		return (EINVAL);
338 
339 	/* XXX - Wait for retrace? */
340 
341 	/* Copy hardware to local arrays. */
342 	p = &sc_redmap(sc)[start];
343 	for (i = start; i < ecount; i++)
344 		red[i] = *p++;
345 	p = &sc_greenmap(sc)[start];
346 	for (i = start; i < ecount; i++)
347 		green[i] = *p++;
348 	p = &sc_bluemap(sc)[start];
349 	for (i = start; i < ecount; i++)
350 		blue[i] = *p++;
351 
352 	/* Copy local arrays to user space. */
353 	if ((error = copyout(red + start, cmap->red, count)) != 0)
354 		return (error);
355 	if ((error = copyout(green + start, cmap->green, count)) != 0)
356 		return (error);
357 	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
358 		return (error);
359 
360 	return (0);
361 }
362 
363 /*
364  */
365 int
366 cgtwoputcmap(sc, cmap)
367 	register struct cgtwo_softc *sc;
368 	register struct fbcmap *cmap;
369 {
370 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
371 	int error, start, count, ecount;
372 	register u_int i;
373 	register volatile u_short *p;
374 
375 	start = cmap->index;
376 	count = cmap->count;
377 	ecount = start + count;
378 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
379 		return (EINVAL);
380 
381 	/* Copy from user space to local arrays. */
382 	if ((error = copyin(cmap->red, red + start, count)) != 0)
383 		return (error);
384 	if ((error = copyin(cmap->green, green + start, count)) != 0)
385 		return (error);
386 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
387 		return (error);
388 
389 	/* XXX - Wait for retrace? */
390 
391 	/* Copy from local arrays to hardware. */
392 	p = &sc_redmap(sc)[start];
393 	for (i = start; i < ecount; i++)
394 		*p++ = red[i];
395 	p = &sc_greenmap(sc)[start];
396 	for (i = start; i < ecount; i++)
397 		*p++ = green[i];
398 	p = &sc_bluemap(sc)[start];
399 	for (i = start; i < ecount; i++)
400 		*p++ = blue[i];
401 
402 	return (0);
403 }
404 
405 /*
406  * Return the address that would map the given device at the given
407  * offset, allowing for the given protection, or return -1 for error.
408  */
409 int
410 cgtwommap(dev, off, prot)
411 	dev_t dev;
412 	int off, prot;
413 {
414 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
415 	vme_am_t mod;
416 	bus_space_handle_t bh;
417 	extern int sparc_vme_mmap_cookie __P((vme_addr_t, vme_am_t,
418 					      bus_space_handle_t *));
419 
420 	if (off & PGOFSET)
421 		panic("cgtwommap");
422 
423 	if ((unsigned)off >= sc->sc_fb.fb_type.fb_size)
424 		return (-1);
425 
426 	/* Apparently, the pixels are in 32-bit data space */
427 	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
428 
429 	if (sparc_vme_mmap_cookie(sc->sc_paddr + off, mod, &bh) != 0)
430 		panic("cgtwommap");
431 
432 	return ((int)bh);
433 }
434