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