xref: /netbsd-src/sys/arch/sparc/dev/cgtwo.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /*	$NetBSD: cgtwo.c,v 1.49 2003/08/25 17:50:25 uwe 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. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	from: @(#)cgthree.c	8.2 (Berkeley) 10/30/93
41  */
42 
43 /*
44  * color display (cgtwo) driver.
45  *
46  * Does not handle interrupts, even though they can occur.
47  *
48  * XXX should defer colormap updates to vertical retrace interrupts
49  */
50 
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: cgtwo.c,v 1.49 2003/08/25 17:50:25 uwe Exp $");
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/buf.h>
57 #include <sys/device.h>
58 #include <sys/ioctl.h>
59 #include <sys/malloc.h>
60 #include <sys/mman.h>
61 #include <sys/tty.h>
62 #include <sys/conf.h>
63 
64 #include <uvm/uvm_extern.h>
65 
66 #include <machine/autoconf.h>
67 
68 #include <dev/sun/fbio.h>
69 #include <dev/sun/fbvar.h>
70 
71 #include <dev/vme/vmevar.h>
72 
73 #include <machine/eeprom.h>
74 #include <machine/cgtworeg.h>
75 
76 
77 /* per-display variables */
78 struct cgtwo_softc {
79 	struct	device sc_dev;		/* base device */
80 	struct	fbdevice sc_fb;		/* frame buffer device */
81 	vme_addr_t		sc_paddr;
82 	vme_chipset_tag_t	sc_ct;
83 	bus_space_tag_t		sc_bt;
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 CFATTACH_DECL(cgtwo, sizeof(struct cgtwo_softc),
99     cgtwomatch, cgtwoattach, NULL, NULL);
100 
101 extern struct cfdriver cgtwo_cd;
102 
103 dev_type_open(cgtwoopen);
104 dev_type_ioctl(cgtwoioctl);
105 dev_type_mmap(cgtwommap);
106 
107 const struct cdevsw cgtwo_cdevsw = {
108 	cgtwoopen, nullclose, noread, nowrite, cgtwoioctl,
109 	nostop, notty, nopoll, cgtwommap, nokqfilter,
110 };
111 
112 /* frame buffer generic driver */
113 static struct fbdriver cgtwofbdriver = {
114 	cgtwounblank, cgtwoopen, nullclose, cgtwoioctl, nopoll, cgtwommap,
115 	nokqfilter
116 };
117 
118 /*
119  * Match a cgtwo.
120  */
121 int
122 cgtwomatch(parent, cf, aux)
123 	struct device *parent;
124 	struct cfdata *cf;
125 	void *aux;
126 {
127 	struct vme_attach_args	*va = aux;
128 	vme_chipset_tag_t	ct = va->va_vct;
129 	vme_am_t		mod;
130 
131 	/*
132 	 * Mask out invalid flags from the user.
133 	 */
134 	cf->cf_flags &= FB_USERMASK;
135 
136 	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
137 	if (vme_probe(ct, va->r[0].offset + CG2_CTLREG_OFF, 2, mod, VME_D16,
138 			  0, 0)) {
139 		return (0);
140 	}
141 
142 	return (1);
143 }
144 
145 /*
146  * Attach a display.  We need to notice if it is the console, too.
147  */
148 void
149 cgtwoattach(parent, self, aux)
150 	struct device *parent, *self;
151 	void *aux;
152 {
153 	struct vme_attach_args	*va = aux;
154 	vme_chipset_tag_t	ct = va->va_vct;
155 	bus_space_tag_t		bt;
156 	bus_space_handle_t	bh;
157 	vme_am_t		mod;
158 	vme_mapresc_t resc;
159 	struct cgtwo_softc *sc = (struct cgtwo_softc *)self;
160 	struct fbdevice *fb = &sc->sc_fb;
161 	struct eeprom *eep = (struct eeprom *)eeprom_va;
162 	int isconsole = 0;
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, PAGE_SIZE);
175 	printf(": cgtwo, %d x %d",
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 = fb_is_console(0);
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 cgtwoioctl(dev, cmd, data, flags, p)
243 	dev_t dev;
244 	u_long cmd;
245 	register caddr_t data;
246 	int flags;
247 	struct proc *p;
248 {
249 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
250 	register struct fbgattr *fba;
251 
252 	switch (cmd) {
253 
254 	case FBIOGTYPE:
255 		*(struct fbtype *)data = sc->sc_fb.fb_type;
256 		break;
257 
258 	case FBIOGATTR:
259 		fba = (struct fbgattr *)data;
260 		fba->real_type = sc->sc_fb.fb_type.fb_type;
261 		fba->owner = 0;		/* XXX ??? */
262 		fba->fbtype = sc->sc_fb.fb_type;
263 		fba->sattr.flags = 0;
264 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
265 		fba->sattr.dev_specific[0] = -1;
266 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
267 		fba->emu_types[1] = -1;
268 		break;
269 
270 	case FBIOGETCMAP:
271 		return cgtwogetcmap(sc, (struct fbcmap *) data);
272 
273 	case FBIOPUTCMAP:
274 		return cgtwoputcmap(sc, (struct fbcmap *) data);
275 
276 	case FBIOGVIDEO:
277 		*(int *)data = sc->sc_reg->video_enab;
278 		break;
279 
280 	case FBIOSVIDEO:
281 		sc->sc_reg->video_enab = (*(int*)data) & 1;
282 		break;
283 
284 	default:
285 		return (ENOTTY);
286 	}
287 	return (0);
288 }
289 
290 /*
291  * Undo the effect of an FBIOSVIDEO that turns the video off.
292  */
293 static void
294 cgtwounblank(dev)
295 	struct device *dev;
296 {
297 	struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
298 	sc->sc_reg->video_enab = 1;
299 }
300 
301 /*
302  */
303 int
304 cgtwogetcmap(sc, cmap)
305 	register struct cgtwo_softc *sc;
306 	register struct fbcmap *cmap;
307 {
308 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
309 	int error, start, count, ecount;
310 	register u_int i;
311 	register volatile u_short *p;
312 
313 	start = cmap->index;
314 	count = cmap->count;
315 	ecount = start + count;
316 	if (start >= CG2_CMSIZE || count > CG2_CMSIZE - start)
317 		return (EINVAL);
318 
319 	/* XXX - Wait for retrace? */
320 
321 	/* Copy hardware to local arrays. */
322 	p = &sc_redmap(sc)[start];
323 	for (i = start; i < ecount; i++)
324 		red[i] = *p++;
325 	p = &sc_greenmap(sc)[start];
326 	for (i = start; i < ecount; i++)
327 		green[i] = *p++;
328 	p = &sc_bluemap(sc)[start];
329 	for (i = start; i < ecount; i++)
330 		blue[i] = *p++;
331 
332 	/* Copy local arrays to user space. */
333 	if ((error = copyout(red + start, cmap->red, count)) != 0)
334 		return (error);
335 	if ((error = copyout(green + start, cmap->green, count)) != 0)
336 		return (error);
337 	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
338 		return (error);
339 
340 	return (0);
341 }
342 
343 /*
344  */
345 int
346 cgtwoputcmap(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;
352 	u_int start, count, ecount;
353 	register u_int i;
354 	register volatile u_short *p;
355 
356 	start = cmap->index;
357 	count = cmap->count;
358 	ecount = start + count;
359 	if (start >= CG2_CMSIZE || count > CG2_CMSIZE - start)
360 		return (EINVAL);
361 
362 	/* Copy from user space to local arrays. */
363 	if ((error = copyin(cmap->red, red + start, count)) != 0)
364 		return (error);
365 	if ((error = copyin(cmap->green, green + start, count)) != 0)
366 		return (error);
367 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
368 		return (error);
369 
370 	/* XXX - Wait for retrace? */
371 
372 	/* Copy from local arrays to hardware. */
373 	p = &sc_redmap(sc)[start];
374 	for (i = start; i < ecount; i++)
375 		*p++ = red[i];
376 	p = &sc_greenmap(sc)[start];
377 	for (i = start; i < ecount; i++)
378 		*p++ = green[i];
379 	p = &sc_bluemap(sc)[start];
380 	for (i = start; i < ecount; i++)
381 		*p++ = blue[i];
382 
383 	return (0);
384 }
385 
386 /*
387  * Return the address that would map the given device at the given
388  * offset, allowing for the given protection, or return -1 for error.
389  */
390 paddr_t
391 cgtwommap(dev, off, prot)
392 	dev_t dev;
393 	off_t off;
394 	int prot;
395 {
396 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
397 	vme_am_t mod;
398 	bus_space_handle_t bh;
399 	extern int sparc_vme_mmap_cookie __P((vme_addr_t, vme_am_t,
400 					      bus_space_handle_t *));
401 
402 	if (off & PGOFSET)
403 		panic("cgtwommap");
404 
405 	if (off >= sc->sc_fb.fb_type.fb_size)
406 		return (-1);
407 
408 	/* Apparently, the pixels are in 32-bit data space */
409 	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
410 
411 	if (sparc_vme_mmap_cookie(sc->sc_paddr + off, mod, &bh) != 0)
412 		panic("cgtwommap");
413 
414 	return ((paddr_t)bh);
415 }
416