cgtwo.c revision 1.11 1 /* $NetBSD: cgtwo.c,v 1.11 1996/03/14 19:44:44 christos 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
64 #include <vm/vm.h>
65
66 #include <machine/fbio.h>
67 #include <machine/autoconf.h>
68 #include <machine/pmap.h>
69 #include <machine/fbvar.h>
70 #if defined(SUN4)
71 #include <machine/eeprom.h>
72 #endif
73
74 #include <machine/cgtworeg.h>
75
76 #include <sparc/dev/dev_conf.h>
77
78 /* per-display variables */
79 struct cgtwo_softc {
80 struct device sc_dev; /* base device */
81 struct fbdevice sc_fb; /* frame buffer device */
82 struct rom_reg sc_phys; /* display RAM (phys addr) */
83 int sc_bustype; /* type of bus we live on */
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(struct device *, struct device *, void *);
93 static int cgtwomatch(struct device *, void *, void *);
94 int cgtwoopen __P((dev_t, int, int, struct proc *));
95 int cgtwoclose __P((dev_t, int, int, struct proc *));
96 int cgtwoioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
97 int cgtwommap __P((dev_t, int, int));
98 static void cgtwounblank __P((struct device *));
99 int cgtwogetcmap __P((struct cgtwo_softc *, struct fbcmap *));
100 int cgtwoputcmap __P((struct cgtwo_softc *, struct fbcmap *));
101
102 struct cfdriver cgtwocd = {
103 NULL, "cgtwo", cgtwomatch, cgtwoattach,
104 DV_DULL, sizeof(struct cgtwo_softc)
105 };
106
107 /* frame buffer generic driver */
108 static struct fbdriver cgtwofbdriver = {
109 cgtwounblank, cgtwoopen, cgtwoclose, cgtwoioctl, cgtwommap
110 };
111
112 extern int fbnode;
113 extern struct tty *fbconstty;
114
115 /*
116 * Match a cgtwo.
117 */
118 int
119 cgtwomatch(parent, vcf, aux)
120 struct device *parent;
121 void *vcf, *aux;
122 {
123 struct cfdata *cf = vcf;
124 struct confargs *ca = aux;
125 struct romaux *ra = &ca->ca_ra;
126 caddr_t tmp;
127
128 /*
129 * Mask out invalid flags from the user.
130 */
131 cf->cf_flags &= FB_USERMASK;
132
133 if (ca->ca_bustype != BUS_VME16)
134 return (0);
135
136 if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
137 return (0);
138
139 #if defined(SUN4)
140 if (!CPU_ISSUN4 || cf->cf_unit != 0)
141 return (0);
142
143 /* XXX - Must do our own mapping at CG2_CTLREG_OFF */
144 bus_untmp();
145 tmp = (caddr_t)bus_tmp(ra->ra_paddr + CG2_CTLREG_OFF, ca->ca_bustype);
146 if (probeget(tmp, 2) != -1)
147 return 1;
148 #endif
149 return 0;
150 }
151
152 /*
153 * Attach a display. We need to notice if it is the console, too.
154 */
155 void
156 cgtwoattach(parent, self, args)
157 struct device *parent, *self;
158 void *args;
159 {
160 register struct cgtwo_softc *sc = (struct cgtwo_softc *)self;
161 register struct confargs *ca = args;
162 register int node = 0;
163 int isconsole = 0;
164 char *nam = NULL;
165
166 sc->sc_fb.fb_driver = &cgtwofbdriver;
167 sc->sc_fb.fb_device = &sc->sc_dev;
168 sc->sc_fb.fb_type.fb_type = FBTYPE_SUN2COLOR;
169 sc->sc_fb.fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
170
171 switch (ca->ca_bustype) {
172 case BUS_VME16:
173 node = 0;
174 nam = "cgtwo";
175 break;
176
177 default:
178 panic("cgtwoattach: impossible bustype");
179 /* NOTREACHED */
180 }
181
182 sc->sc_fb.fb_type.fb_depth = 8;
183 fb_setsize(&sc->sc_fb, sc->sc_fb.fb_type.fb_depth,
184 1152, 900, node, ca->ca_bustype);
185
186 sc->sc_fb.fb_type.fb_cmsize = 256;
187 sc->sc_fb.fb_type.fb_size = roundup(CG2_MAPPED_SIZE, NBPG);
188 printf(": %s, %d x %d", nam,
189 sc->sc_fb.fb_type.fb_width, sc->sc_fb.fb_type.fb_height);
190
191 /*
192 * When the ROM has mapped in a cgtwo display, the address
193 * maps only the video RAM, so in any case we have to map the
194 * registers ourselves. We only need the video RAM if we are
195 * going to print characters via rconsole.
196 */
197 #if defined(SUN4)
198 if (CPU_ISSUN4) {
199 struct eeprom *eep = (struct eeprom *)eeprom_va;
200 /*
201 * Assume this is the console if there's no eeprom info
202 * to be found.
203 */
204 if (eep == NULL || eep->eeConsole == EE_CONS_COLOR)
205 isconsole = (fbconstty != NULL);
206 else
207 isconsole = 0;
208 }
209 #endif
210 sc->sc_phys = ca->ca_ra.ra_reg[0];
211 sc->sc_bustype = ca->ca_bustype;
212
213 if ((sc->sc_fb.fb_pixels = ca->ca_ra.ra_vaddr) == NULL && isconsole) {
214 /* this probably cannot happen, but what the heck */
215 sc->sc_fb.fb_pixels = mapiodev(ca->ca_ra.ra_reg, CG2_PIXMAP_OFF,
216 CG2_PIXMAP_SIZE,
217 ca->ca_bustype);
218 }
219 #ifndef offsetof
220 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
221 #endif
222
223 sc->sc_reg = (volatile struct cg2statusreg *)
224 mapiodev(ca->ca_ra.ra_reg,
225 CG2_ROPMEM_OFF + offsetof(struct cg2fb, status.reg),
226 sizeof(struct cg2statusreg), ca->ca_bustype);
227
228 sc->sc_cmap = (volatile u_short *)
229 mapiodev(ca->ca_ra.ra_reg,
230 CG2_ROPMEM_OFF + offsetof(struct cg2fb, redmap[0]),
231 3 * CG2_CMSIZE, ca->ca_bustype);
232
233 if (isconsole) {
234 printf(" (console)\n");
235 #ifdef RASTERCONSOLE
236 fbrcons_init(&sc->sc_fb);
237 #endif
238 } else
239 printf("\n");
240
241 if (node == fbnode || CPU_ISSUN4)
242 fb_attach(&sc->sc_fb, isconsole);
243 }
244
245 int
246 cgtwoopen(dev, flags, mode, p)
247 dev_t dev;
248 int flags, mode;
249 struct proc *p;
250 {
251 int unit = minor(dev);
252
253 if (unit >= cgtwocd.cd_ndevs || cgtwocd.cd_devs[unit] == NULL)
254 return (ENXIO);
255 return (0);
256 }
257
258 int
259 cgtwoclose(dev, flags, mode, p)
260 dev_t dev;
261 int flags, mode;
262 struct proc *p;
263 {
264
265 return (0);
266 }
267
268 int
269 cgtwoioctl(dev, cmd, data, flags, p)
270 dev_t dev;
271 u_long cmd;
272 register caddr_t data;
273 int flags;
274 struct proc *p;
275 {
276 register struct cgtwo_softc *sc = cgtwocd.cd_devs[minor(dev)];
277 register struct fbgattr *fba;
278
279 switch (cmd) {
280
281 case FBIOGTYPE:
282 *(struct fbtype *)data = sc->sc_fb.fb_type;
283 break;
284
285 case FBIOGATTR:
286 fba = (struct fbgattr *)data;
287 fba->real_type = sc->sc_fb.fb_type.fb_type;
288 fba->owner = 0; /* XXX ??? */
289 fba->fbtype = sc->sc_fb.fb_type;
290 fba->sattr.flags = 0;
291 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
292 fba->sattr.dev_specific[0] = -1;
293 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
294 fba->emu_types[1] = -1;
295 break;
296
297 case FBIOGETCMAP:
298 return cgtwogetcmap(sc, (struct fbcmap *) data);
299
300 case FBIOPUTCMAP:
301 return cgtwoputcmap(sc, (struct fbcmap *) data);
302
303 case FBIOGVIDEO:
304 *(int *)data = sc->sc_reg->video_enab;
305 break;
306
307 case FBIOSVIDEO:
308 sc->sc_reg->video_enab = (*(int*)data) & 1;
309 break;
310
311 default:
312 return (ENOTTY);
313 }
314 return (0);
315 }
316
317 /*
318 * Undo the effect of an FBIOSVIDEO that turns the video off.
319 */
320 static void
321 cgtwounblank(dev)
322 struct device *dev;
323 {
324 struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
325 sc->sc_reg->video_enab = 1;
326 }
327
328 /*
329 */
330 int
331 cgtwogetcmap(sc, cmap)
332 register struct cgtwo_softc *sc;
333 register struct fbcmap *cmap;
334 {
335 u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
336 int error, start, count, ecount;
337 register u_int i;
338 register volatile u_short *p;
339
340 start = cmap->index;
341 count = cmap->count;
342 ecount = start + count;
343 if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
344 return (EINVAL);
345
346 /* XXX - Wait for retrace? */
347
348 /* Copy hardware to local arrays. */
349 p = &sc_redmap(sc)[start];
350 for (i = start; i < ecount; i++)
351 red[i] = *p++;
352 p = &sc_greenmap(sc)[start];
353 for (i = start; i < ecount; i++)
354 green[i] = *p++;
355 p = &sc_bluemap(sc)[start];
356 for (i = start; i < ecount; i++)
357 blue[i] = *p++;
358
359 /* Copy local arrays to user space. */
360 if ((error = copyout(red + start, cmap->red, count)) != 0)
361 return (error);
362 if ((error = copyout(green + start, cmap->green, count)) != 0)
363 return (error);
364 if ((error = copyout(blue + start, cmap->blue, count)) != 0)
365 return (error);
366
367 return (0);
368 }
369
370 /*
371 */
372 int
373 cgtwoputcmap(sc, cmap)
374 register struct cgtwo_softc *sc;
375 register struct fbcmap *cmap;
376 {
377 u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
378 int error, start, count, ecount;
379 register u_int i;
380 register volatile u_short *p;
381
382 start = cmap->index;
383 count = cmap->count;
384 ecount = start + count;
385 if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
386 return (EINVAL);
387
388 /* Copy from user space to local arrays. */
389 if ((error = copyin(cmap->red, red + start, count)) != 0)
390 return (error);
391 if ((error = copyin(cmap->green, green + start, count)) != 0)
392 return (error);
393 if ((error = copyin(cmap->blue, blue + start, count)) != 0)
394 return (error);
395
396 /* XXX - Wait for retrace? */
397
398 /* Copy from local arrays to hardware. */
399 p = &sc_redmap(sc)[start];
400 for (i = start; i < ecount; i++)
401 *p++ = red[i];
402 p = &sc_greenmap(sc)[start];
403 for (i = start; i < ecount; i++)
404 *p++ = green[i];
405 p = &sc_bluemap(sc)[start];
406 for (i = start; i < ecount; i++)
407 *p++ = blue[i];
408
409 return (0);
410 }
411
412 /*
413 * Return the address that would map the given device at the given
414 * offset, allowing for the given protection, or return -1 for error.
415 */
416 int
417 cgtwommap(dev, off, prot)
418 dev_t dev;
419 int off, prot;
420 {
421 register struct cgtwo_softc *sc = cgtwocd.cd_devs[minor(dev)];
422
423 if (off & PGOFSET)
424 panic("cgtwommap");
425
426 if ((unsigned)off >= sc->sc_fb.fb_type.fb_size)
427 return (-1);
428
429 return (REG2PHYS(&sc->sc_phys, off, PMAP_VME32/*sc->sc_bustype*/) | PMAP_NC);
430 }
431