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