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