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