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