xref: /netbsd-src/sys/arch/sun3/dev/cg2.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: cg2.c,v 1.9 1997/10/17 03:43:56 gwr 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 (cg2) 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/conf.h>
58 #include <sys/device.h>
59 #include <sys/ioctl.h>
60 #include <sys/malloc.h>
61 #include <sys/mman.h>
62 #include <sys/proc.h>
63 #include <sys/tty.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/cg2reg.h>
71 
72 #include "fbvar.h"
73 
74 cdev_decl(cg2);
75 
76 #define	CMSIZE 256
77 
78 /* offset to and size of mapped part of frame buffer */
79 #define PLANEMAP_SIZE		0x100000
80 #define PIXELMAP_SIZE		0x100000
81 #define ROWOPMAP_SIZE		0x100000
82 
83 #define	CTLREGS_OFF 		0x300000
84 #define	CTLREGS_SIZE		 0x10600
85 
86 #define	CG2_MAPPED_OFFSET	(PLANEMAP_SIZE + PIXELMAP_SIZE)
87 #define	CG2_MAPPED_SIZE 	(CTLREGS_OFF + CTLREGS_SIZE)
88 
89 /* per-display variables */
90 struct cg2_softc {
91 	struct	device sc_dev;		/* base device */
92 	struct	fbdevice sc_fb;		/* frame buffer device */
93 	int 	sc_phys;		/* display RAM (phys addr) */
94 	int 	sc_pmtype;		/* pmap type bits */
95 	struct	cg2fb *sc_ctlreg;	/* control registers */
96 };
97 
98 /* autoconfiguration driver */
99 static void	cg2attach __P((struct device *, struct device *, void *));
100 static int	cg2match __P((struct device *, struct cfdata *, void *));
101 
102 struct cfattach cgtwo_ca = {
103 	sizeof(struct cg2_softc), cg2match, cg2attach
104 };
105 
106 struct cfdriver cgtwo_cd = {
107 	NULL, "cgtwo", DV_DULL
108 };
109 
110 static int  cg2gattr __P((struct fbdevice *,  void *));
111 static int  cg2gvideo __P((struct fbdevice *, void *));
112 static int	cg2svideo __P((struct fbdevice *, void *));
113 static int	cg2getcmap __P((struct fbdevice *, void *));
114 static int	cg2putcmap __P((struct fbdevice *, void *));
115 
116 static struct fbdriver cg2fbdriver = {
117 	cg2open, cg2close, cg2mmap, cg2gattr,
118 	cg2gvideo, cg2svideo,
119 	cg2getcmap, cg2putcmap };
120 
121 static int cg2intr __P((void*));
122 
123 /*
124  * Match a cg2.
125  */
126 static int
127 cg2match(parent, cf, aux)
128 	struct device *parent;
129 	struct cfdata *cf;
130 	void *aux;
131 {
132 	struct confargs *ca = aux;
133 	int probe_addr;
134 
135 	/* No default VME address. */
136 	if (ca->ca_paddr == -1)
137 		return (0);
138 
139 	/* Make sure something is there... */
140 	probe_addr = ca->ca_paddr + CTLREGS_OFF;
141 	if (bus_peek(ca->ca_bustype, probe_addr, 1) == -1)
142 		return (0);
143 
144 	/* XXX: look at the ID reg? */
145 	/* printf("cg2: id=0x%x\n", x); */
146 
147 	/* Default interrupt priority. */
148 	if (ca->ca_intpri == -1)
149 		ca->ca_intpri = 4;
150 
151 	return (1);
152 }
153 
154 /*
155  * Attach a display.  We need to notice if it is the console, too.
156  */
157 static void
158 cg2attach(parent, self, args)
159 	struct device *parent, *self;
160 	void *args;
161 {
162 	struct cg2_softc *sc = (struct cg2_softc *)self;
163 	struct fbdevice *fb = &sc->sc_fb;
164 	struct confargs *ca = args;
165 	struct fbtype *fbt;
166 
167 	sc->sc_phys = ca->ca_paddr;
168 	sc->sc_pmtype = PMAP_NC | PMAP_VME16;
169 
170 	sc->sc_ctlreg = (struct cg2fb *) bus_mapin(ca->ca_bustype,
171 			ca->ca_paddr + CTLREGS_OFF, CTLREGS_SIZE);
172 
173 	isr_add_vectored(cg2intr, (void*)sc,
174 					 ca->ca_intpri, ca->ca_intvec);
175 
176 	/*
177 	 * XXX - Initialize?  Determine type?
178 	 */
179 	sc->sc_ctlreg->intrptvec.reg = ca->ca_intvec;
180 	sc->sc_ctlreg->status.word = 1;
181 
182 	fb->fb_driver = &cg2fbdriver;
183 	fb->fb_private = sc;
184 	fb->fb_name = sc->sc_dev.dv_xname;
185 
186 	fbt = &fb->fb_fbtype;
187 	fbt->fb_type = FBTYPE_SUN2COLOR;
188 	fbt->fb_depth = 8;
189 	fbt->fb_cmsize = CMSIZE;
190 
191 	fbt->fb_width = 1152;
192 	fbt->fb_height = 900;
193 	fbt->fb_size = CG2_MAPPED_SIZE;
194 
195 	printf(" (%dx%d)\n", fbt->fb_width, fbt->fb_height);
196 	fb_attach(fb, 2);
197 }
198 
199 int
200 cg2open(dev, flags, mode, p)
201 	dev_t dev;
202 	int flags, mode;
203 	struct proc *p;
204 {
205 	int unit = minor(dev);
206 
207 	if (unit >= cgtwo_cd.cd_ndevs || cgtwo_cd.cd_devs[unit] == NULL)
208 		return (ENXIO);
209 	return (0);
210 }
211 
212 int
213 cg2close(dev, flags, mode, p)
214 	dev_t dev;
215 	int flags, mode;
216 	struct proc *p;
217 {
218 
219 	return (0);
220 }
221 
222 int
223 cg2ioctl(dev, cmd, data, flags, p)
224 	dev_t dev;
225 	u_long cmd;
226 	caddr_t data;
227 	int flags;
228 	struct proc *p;
229 {
230 	struct cg2_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
231 
232 	return (fbioctlfb(&sc->sc_fb, cmd, data));
233 }
234 
235 /*
236  * Return the address that would map the given device at the given
237  * offset, allowing for the given protection, or return -1 for error.
238  */
239 int
240 cg2mmap(dev, off, prot)
241 	dev_t dev;
242 	int off, prot;
243 {
244 	struct cg2_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
245 
246 	if (off & PGOFSET)
247 		panic("cg2mmap");
248 
249 	if ((unsigned)off >= CG2_MAPPED_SIZE)
250 		return (-1);
251 
252 	/*
253 	 * I turned on PMAP_NC here to disable the cache as I was
254 	 * getting horribly broken behaviour with it on.
255 	 */
256 	return ((sc->sc_phys + off) | sc->sc_pmtype);
257 }
258 
259 /*
260  * Internal ioctl functions.
261  */
262 
263 /* FBIOGATTR: */
264 static int  cg2gattr(fb, data)
265 	struct fbdevice *fb;
266 	void *data;
267 {
268 	struct fbgattr *fba = data;
269 
270 	fba->real_type = fb->fb_fbtype.fb_type;
271 	fba->owner = 0;		/* XXX - TIOCCONS stuff? */
272 	fba->fbtype = fb->fb_fbtype;
273 	fba->sattr.flags = 0;
274 	fba->sattr.emu_type = fb->fb_fbtype.fb_type;
275 	fba->sattr.dev_specific[0] = -1;
276 	fba->emu_types[0] = fb->fb_fbtype.fb_type;
277 	fba->emu_types[1] = -1;
278 	return (0);
279 }
280 
281 /* FBIOGVIDEO: */
282 static int  cg2gvideo(fb, data)
283 	struct fbdevice *fb;
284 	void *data;
285 {
286 	int *on = data;
287 	struct cg2_softc *sc = fb->fb_private;
288 
289 	*on = sc->sc_ctlreg->status.reg.video_enab;
290 	return (0);
291 }
292 
293 /* FBIOSVIDEO: */
294 static int cg2svideo(fb, data)
295 	struct fbdevice *fb;
296 	void *data;
297 {
298 	int *on = data;
299 	struct cg2_softc *sc = fb->fb_private;
300 
301 	sc->sc_ctlreg->status.reg.video_enab = (*on) & 1;
302 
303 	return (0);
304 }
305 
306 /* FBIOGETCMAP: */
307 static int cg2getcmap(fb, data)
308 	struct fbdevice *fb;
309 	void *data;
310 {
311 	struct fbcmap *cmap = data;
312 	struct cg2_softc *sc = fb->fb_private;
313 	u_char red[CMSIZE], green[CMSIZE], blue[CMSIZE];
314 	int error, start, count, ecount;
315 	register u_int i;
316 	register u_short *p;
317 
318 	start = cmap->index;
319 	count = cmap->count;
320 	ecount = start + count;
321 	if (start >= CMSIZE || ecount > CMSIZE)
322 		return (EINVAL);
323 
324 	/* XXX - Wait for retrace? */
325 
326 	/* Copy hardware to local arrays. */
327 	p = &sc->sc_ctlreg->redmap[start];
328 	for (i = start; i < ecount; i++)
329 		red[i] = *p++;
330 	p = &sc->sc_ctlreg->greenmap[start];
331 	for (i = start; i < ecount; i++)
332 		green[i] = *p++;
333 	p = &sc->sc_ctlreg->bluemap[start];
334 	for (i = start; i < ecount; i++)
335 		blue[i] = *p++;
336 
337 	/* Copy local arrays to user space. */
338 	if ((error = copyout(red + start, cmap->red, count)) != 0)
339 		return (error);
340 	if ((error = copyout(green + start, cmap->green, count)) != 0)
341 		return (error);
342 	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
343 		return (error);
344 
345 	return (0);
346 }
347 
348 /* FBIOPUTCMAP: */
349 static int cg2putcmap(fb, data)
350 	struct fbdevice *fb;
351 	void *data;
352 {
353 	struct fbcmap *cmap = data;
354 	struct cg2_softc *sc = fb->fb_private;
355 	u_char red[CMSIZE], green[CMSIZE], blue[CMSIZE];
356 	int error, start, count, ecount;
357 	register u_int i;
358 	register u_short *p;
359 
360 	start = cmap->index;
361 	count = cmap->count;
362 	ecount = start + count;
363 	if (start >= CMSIZE || ecount > CMSIZE)
364 		return (EINVAL);
365 
366 	/* Copy from user space to local arrays. */
367 	if ((error = copyin(cmap->red, red + start, count)) != 0)
368 		return (error);
369 	if ((error = copyin(cmap->green, green + start, count)) != 0)
370 		return (error);
371 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
372 		return (error);
373 
374 	/* XXX - Wait for retrace? */
375 
376 	/* Copy from local arrays to hardware. */
377 	p = &sc->sc_ctlreg->redmap[start];
378 	for (i = start; i < ecount; i++)
379 		*p++ = red[i];
380 	p = &sc->sc_ctlreg->greenmap[start];
381 	for (i = start; i < ecount; i++)
382 		*p++ = green[i];
383 	p = &sc->sc_ctlreg->bluemap[start];
384 	for (i = start; i < ecount; i++)
385 		*p++ = blue[i];
386 
387 	return (0);
388 
389 }
390 
391 static int
392 cg2intr(vsc)
393 	void *vsc;
394 {
395 	struct cg2_softc *sc = vsc;
396 
397 	/* XXX - Just disable interrupts for now. */
398 	sc->sc_ctlreg->status.reg.inten = 0;
399 
400 	printf("cg2intr\n");
401 	return (1);
402 }
403