cgtwo.c revision 1.20 1 1.20 pk /* $NetBSD: cgtwo.c,v 1.20 1996/12/10 22:54:53 pk Exp $ */
2 1.1 pk
3 1.1 pk /*
4 1.1 pk * Copyright (c) 1992, 1993
5 1.1 pk * The Regents of the University of California. All rights reserved.
6 1.1 pk *
7 1.1 pk * This software was developed by the Computer Systems Engineering group
8 1.1 pk * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 pk * contributed to Berkeley.
10 1.1 pk *
11 1.1 pk * All advertising materials mentioning features or use of this software
12 1.1 pk * must display the following acknowledgement:
13 1.1 pk * This product includes software developed by the University of
14 1.1 pk * California, Lawrence Berkeley Laboratory.
15 1.1 pk *
16 1.1 pk * Redistribution and use in source and binary forms, with or without
17 1.1 pk * modification, are permitted provided that the following conditions
18 1.1 pk * are met:
19 1.1 pk * 1. Redistributions of source code must retain the above copyright
20 1.1 pk * notice, this list of conditions and the following disclaimer.
21 1.1 pk * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 pk * notice, this list of conditions and the following disclaimer in the
23 1.1 pk * documentation and/or other materials provided with the distribution.
24 1.1 pk * 3. All advertising materials mentioning features or use of this software
25 1.1 pk * must display the following acknowledgement:
26 1.1 pk * This product includes software developed by the University of
27 1.1 pk * California, Berkeley and its contributors.
28 1.1 pk * 4. Neither the name of the University nor the names of its contributors
29 1.1 pk * may be used to endorse or promote products derived from this software
30 1.1 pk * without specific prior written permission.
31 1.1 pk *
32 1.1 pk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 1.1 pk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 1.1 pk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 1.1 pk * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 1.1 pk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.1 pk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.1 pk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.1 pk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 1.1 pk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 1.1 pk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 1.1 pk * SUCH DAMAGE.
43 1.1 pk *
44 1.1 pk * from: @(#)cgthree.c 8.2 (Berkeley) 10/30/93
45 1.1 pk */
46 1.1 pk
47 1.1 pk /*
48 1.1 pk * color display (cgtwo) driver.
49 1.1 pk *
50 1.1 pk * Does not handle interrupts, even though they can occur.
51 1.1 pk *
52 1.1 pk * XXX should defer colormap updates to vertical retrace interrupts
53 1.1 pk */
54 1.1 pk
55 1.1 pk #include <sys/param.h>
56 1.11 christos #include <sys/systm.h>
57 1.1 pk #include <sys/buf.h>
58 1.1 pk #include <sys/device.h>
59 1.1 pk #include <sys/ioctl.h>
60 1.1 pk #include <sys/malloc.h>
61 1.1 pk #include <sys/mman.h>
62 1.1 pk #include <sys/tty.h>
63 1.14 christos #include <sys/conf.h>
64 1.1 pk
65 1.1 pk #include <vm/vm.h>
66 1.1 pk
67 1.1 pk #include <machine/fbio.h>
68 1.1 pk #include <machine/autoconf.h>
69 1.1 pk #include <machine/pmap.h>
70 1.1 pk #include <machine/fbvar.h>
71 1.2 pk #if defined(SUN4)
72 1.2 pk #include <machine/eeprom.h>
73 1.2 pk #endif
74 1.14 christos #include <machine/conf.h>
75 1.3 pk #include <machine/cgtworeg.h>
76 1.1 pk
77 1.11 christos
78 1.1 pk /* per-display variables */
79 1.1 pk struct cgtwo_softc {
80 1.1 pk struct device sc_dev; /* base device */
81 1.1 pk struct fbdevice sc_fb; /* frame buffer device */
82 1.8 pk struct rom_reg sc_phys; /* display RAM (phys addr) */
83 1.8 pk int sc_bustype; /* type of bus we live on */
84 1.3 pk volatile struct cg2statusreg *sc_reg; /* CG2 control registers */
85 1.3 pk volatile u_short *sc_cmap;
86 1.3 pk #define sc_redmap(sc) ((sc)->sc_cmap)
87 1.3 pk #define sc_greenmap(sc) ((sc)->sc_cmap + CG2_CMSIZE)
88 1.3 pk #define sc_bluemap(sc) ((sc)->sc_cmap + 2 * CG2_CMSIZE)
89 1.1 pk };
90 1.1 pk
91 1.1 pk /* autoconfiguration driver */
92 1.16 mrg static void cgtwoattach __P((struct device *, struct device *, void *));
93 1.20 pk static int cgtwomatch __P((struct device *, struct cfdata *, void *));
94 1.11 christos static void cgtwounblank __P((struct device *));
95 1.11 christos int cgtwogetcmap __P((struct cgtwo_softc *, struct fbcmap *));
96 1.11 christos int cgtwoputcmap __P((struct cgtwo_softc *, struct fbcmap *));
97 1.1 pk
98 1.17 thorpej /* cdevsw prototypes */
99 1.17 thorpej cdev_decl(cgtwo);
100 1.17 thorpej
101 1.12 thorpej struct cfattach cgtwo_ca = {
102 1.12 thorpej sizeof(struct cgtwo_softc), cgtwomatch, cgtwoattach
103 1.12 thorpej };
104 1.12 thorpej
105 1.12 thorpej struct cfdriver cgtwo_cd = {
106 1.12 thorpej NULL, "cgtwo", DV_DULL
107 1.1 pk };
108 1.1 pk
109 1.1 pk /* frame buffer generic driver */
110 1.1 pk static struct fbdriver cgtwofbdriver = {
111 1.17 thorpej cgtwounblank, cgtwoopen, cgtwoclose, cgtwoioctl, cgtwopoll, cgtwommap
112 1.1 pk };
113 1.1 pk
114 1.1 pk extern int fbnode;
115 1.1 pk extern struct tty *fbconstty;
116 1.1 pk
117 1.1 pk /*
118 1.1 pk * Match a cgtwo.
119 1.1 pk */
120 1.1 pk int
121 1.20 pk cgtwomatch(parent, cf, aux)
122 1.1 pk struct device *parent;
123 1.20 pk struct cfdata *cf;
124 1.20 pk void *aux;
125 1.1 pk {
126 1.1 pk struct confargs *ca = aux;
127 1.1 pk struct romaux *ra = &ca->ca_ra;
128 1.13 pk #if defined(SUN4)
129 1.2 pk caddr_t tmp;
130 1.13 pk #endif
131 1.10 thorpej
132 1.10 thorpej /*
133 1.10 thorpej * Mask out invalid flags from the user.
134 1.10 thorpej */
135 1.10 thorpej cf->cf_flags &= FB_USERMASK;
136 1.2 pk
137 1.2 pk if (ca->ca_bustype != BUS_VME16)
138 1.2 pk return (0);
139 1.1 pk
140 1.1 pk if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
141 1.1 pk return (0);
142 1.2 pk
143 1.2 pk #if defined(SUN4)
144 1.9 pk if (!CPU_ISSUN4 || cf->cf_unit != 0)
145 1.2 pk return (0);
146 1.2 pk
147 1.2 pk /* XXX - Must do our own mapping at CG2_CTLREG_OFF */
148 1.2 pk bus_untmp();
149 1.2 pk tmp = (caddr_t)bus_tmp(ra->ra_paddr + CG2_CTLREG_OFF, ca->ca_bustype);
150 1.2 pk if (probeget(tmp, 2) != -1)
151 1.2 pk return 1;
152 1.2 pk #endif
153 1.2 pk return 0;
154 1.1 pk }
155 1.1 pk
156 1.1 pk /*
157 1.1 pk * Attach a display. We need to notice if it is the console, too.
158 1.1 pk */
159 1.1 pk void
160 1.1 pk cgtwoattach(parent, self, args)
161 1.1 pk struct device *parent, *self;
162 1.1 pk void *args;
163 1.1 pk {
164 1.1 pk register struct cgtwo_softc *sc = (struct cgtwo_softc *)self;
165 1.1 pk register struct confargs *ca = args;
166 1.11 christos register int node = 0;
167 1.11 christos int isconsole = 0;
168 1.11 christos char *nam = NULL;
169 1.1 pk
170 1.1 pk sc->sc_fb.fb_driver = &cgtwofbdriver;
171 1.1 pk sc->sc_fb.fb_device = &sc->sc_dev;
172 1.1 pk sc->sc_fb.fb_type.fb_type = FBTYPE_SUN2COLOR;
173 1.7 pk sc->sc_fb.fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
174 1.5 pk
175 1.1 pk switch (ca->ca_bustype) {
176 1.1 pk case BUS_VME16:
177 1.7 pk node = 0;
178 1.1 pk nam = "cgtwo";
179 1.5 pk break;
180 1.2 pk
181 1.5 pk default:
182 1.5 pk panic("cgtwoattach: impossible bustype");
183 1.5 pk /* NOTREACHED */
184 1.1 pk }
185 1.1 pk
186 1.1 pk sc->sc_fb.fb_type.fb_depth = 8;
187 1.5 pk fb_setsize(&sc->sc_fb, sc->sc_fb.fb_type.fb_depth,
188 1.5 pk 1152, 900, node, ca->ca_bustype);
189 1.5 pk
190 1.1 pk sc->sc_fb.fb_type.fb_cmsize = 256;
191 1.2 pk sc->sc_fb.fb_type.fb_size = roundup(CG2_MAPPED_SIZE, NBPG);
192 1.19 christos printf(": %s, %d x %d", nam,
193 1.1 pk sc->sc_fb.fb_type.fb_width, sc->sc_fb.fb_type.fb_height);
194 1.1 pk
195 1.1 pk /*
196 1.1 pk * When the ROM has mapped in a cgtwo display, the address
197 1.1 pk * maps only the video RAM, so in any case we have to map the
198 1.1 pk * registers ourselves. We only need the video RAM if we are
199 1.1 pk * going to print characters via rconsole.
200 1.1 pk */
201 1.2 pk #if defined(SUN4)
202 1.9 pk if (CPU_ISSUN4) {
203 1.2 pk struct eeprom *eep = (struct eeprom *)eeprom_va;
204 1.2 pk /*
205 1.2 pk * Assume this is the console if there's no eeprom info
206 1.2 pk * to be found.
207 1.2 pk */
208 1.2 pk if (eep == NULL || eep->eeConsole == EE_CONS_COLOR)
209 1.2 pk isconsole = (fbconstty != NULL);
210 1.2 pk else
211 1.2 pk isconsole = 0;
212 1.2 pk }
213 1.2 pk #endif
214 1.8 pk sc->sc_phys = ca->ca_ra.ra_reg[0];
215 1.8 pk sc->sc_bustype = ca->ca_bustype;
216 1.8 pk
217 1.1 pk if ((sc->sc_fb.fb_pixels = ca->ca_ra.ra_vaddr) == NULL && isconsole) {
218 1.1 pk /* this probably cannot happen, but what the heck */
219 1.8 pk sc->sc_fb.fb_pixels = mapiodev(ca->ca_ra.ra_reg, CG2_PIXMAP_OFF,
220 1.2 pk CG2_PIXMAP_SIZE,
221 1.15 pk PMAP_VME32/*ca->ca_bustype*/);
222 1.1 pk }
223 1.3 pk #ifndef offsetof
224 1.3 pk #define offsetof(type, member) ((size_t)(&((type *)0)->member))
225 1.3 pk #endif
226 1.3 pk
227 1.3 pk sc->sc_reg = (volatile struct cg2statusreg *)
228 1.8 pk mapiodev(ca->ca_ra.ra_reg,
229 1.3 pk CG2_ROPMEM_OFF + offsetof(struct cg2fb, status.reg),
230 1.4 pk sizeof(struct cg2statusreg), ca->ca_bustype);
231 1.3 pk
232 1.3 pk sc->sc_cmap = (volatile u_short *)
233 1.8 pk mapiodev(ca->ca_ra.ra_reg,
234 1.3 pk CG2_ROPMEM_OFF + offsetof(struct cg2fb, redmap[0]),
235 1.3 pk 3 * CG2_CMSIZE, ca->ca_bustype);
236 1.1 pk
237 1.1 pk if (isconsole) {
238 1.19 christos printf(" (console)\n");
239 1.4 pk #ifdef RASTERCONSOLE
240 1.1 pk fbrcons_init(&sc->sc_fb);
241 1.1 pk #endif
242 1.1 pk } else
243 1.19 christos printf("\n");
244 1.9 pk
245 1.9 pk if (node == fbnode || CPU_ISSUN4)
246 1.7 pk fb_attach(&sc->sc_fb, isconsole);
247 1.1 pk }
248 1.1 pk
249 1.1 pk int
250 1.1 pk cgtwoopen(dev, flags, mode, p)
251 1.1 pk dev_t dev;
252 1.1 pk int flags, mode;
253 1.1 pk struct proc *p;
254 1.1 pk {
255 1.1 pk int unit = minor(dev);
256 1.1 pk
257 1.12 thorpej if (unit >= cgtwo_cd.cd_ndevs || cgtwo_cd.cd_devs[unit] == NULL)
258 1.1 pk return (ENXIO);
259 1.1 pk return (0);
260 1.1 pk }
261 1.1 pk
262 1.1 pk int
263 1.1 pk cgtwoclose(dev, flags, mode, p)
264 1.1 pk dev_t dev;
265 1.1 pk int flags, mode;
266 1.1 pk struct proc *p;
267 1.1 pk {
268 1.1 pk
269 1.1 pk return (0);
270 1.1 pk }
271 1.1 pk
272 1.1 pk int
273 1.1 pk cgtwoioctl(dev, cmd, data, flags, p)
274 1.1 pk dev_t dev;
275 1.1 pk u_long cmd;
276 1.1 pk register caddr_t data;
277 1.1 pk int flags;
278 1.1 pk struct proc *p;
279 1.1 pk {
280 1.12 thorpej register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
281 1.1 pk register struct fbgattr *fba;
282 1.1 pk
283 1.1 pk switch (cmd) {
284 1.1 pk
285 1.1 pk case FBIOGTYPE:
286 1.1 pk *(struct fbtype *)data = sc->sc_fb.fb_type;
287 1.1 pk break;
288 1.1 pk
289 1.1 pk case FBIOGATTR:
290 1.1 pk fba = (struct fbgattr *)data;
291 1.1 pk fba->real_type = sc->sc_fb.fb_type.fb_type;
292 1.1 pk fba->owner = 0; /* XXX ??? */
293 1.1 pk fba->fbtype = sc->sc_fb.fb_type;
294 1.1 pk fba->sattr.flags = 0;
295 1.1 pk fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
296 1.1 pk fba->sattr.dev_specific[0] = -1;
297 1.1 pk fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
298 1.1 pk fba->emu_types[1] = -1;
299 1.1 pk break;
300 1.1 pk
301 1.1 pk case FBIOGETCMAP:
302 1.11 christos return cgtwogetcmap(sc, (struct fbcmap *) data);
303 1.1 pk
304 1.1 pk case FBIOPUTCMAP:
305 1.11 christos return cgtwoputcmap(sc, (struct fbcmap *) data);
306 1.1 pk
307 1.1 pk case FBIOGVIDEO:
308 1.3 pk *(int *)data = sc->sc_reg->video_enab;
309 1.1 pk break;
310 1.1 pk
311 1.1 pk case FBIOSVIDEO:
312 1.3 pk sc->sc_reg->video_enab = (*(int*)data) & 1;
313 1.1 pk break;
314 1.1 pk
315 1.1 pk default:
316 1.1 pk return (ENOTTY);
317 1.1 pk }
318 1.1 pk return (0);
319 1.17 thorpej }
320 1.17 thorpej
321 1.17 thorpej int
322 1.17 thorpej cgtwopoll(dev, events, p)
323 1.17 thorpej dev_t dev;
324 1.17 thorpej int events;
325 1.17 thorpej struct proc *p;
326 1.17 thorpej {
327 1.17 thorpej
328 1.17 thorpej return (seltrue(dev, events, p));
329 1.1 pk }
330 1.1 pk
331 1.1 pk /*
332 1.1 pk * Undo the effect of an FBIOSVIDEO that turns the video off.
333 1.1 pk */
334 1.1 pk static void
335 1.1 pk cgtwounblank(dev)
336 1.1 pk struct device *dev;
337 1.1 pk {
338 1.1 pk struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
339 1.3 pk sc->sc_reg->video_enab = 1;
340 1.1 pk }
341 1.1 pk
342 1.1 pk /*
343 1.1 pk */
344 1.1 pk int
345 1.1 pk cgtwogetcmap(sc, cmap)
346 1.1 pk register struct cgtwo_softc *sc;
347 1.1 pk register struct fbcmap *cmap;
348 1.1 pk {
349 1.1 pk u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
350 1.1 pk int error, start, count, ecount;
351 1.1 pk register u_int i;
352 1.3 pk register volatile u_short *p;
353 1.1 pk
354 1.1 pk start = cmap->index;
355 1.1 pk count = cmap->count;
356 1.1 pk ecount = start + count;
357 1.1 pk if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
358 1.1 pk return (EINVAL);
359 1.1 pk
360 1.1 pk /* XXX - Wait for retrace? */
361 1.1 pk
362 1.1 pk /* Copy hardware to local arrays. */
363 1.3 pk p = &sc_redmap(sc)[start];
364 1.1 pk for (i = start; i < ecount; i++)
365 1.1 pk red[i] = *p++;
366 1.3 pk p = &sc_greenmap(sc)[start];
367 1.1 pk for (i = start; i < ecount; i++)
368 1.1 pk green[i] = *p++;
369 1.3 pk p = &sc_bluemap(sc)[start];
370 1.1 pk for (i = start; i < ecount; i++)
371 1.1 pk blue[i] = *p++;
372 1.1 pk
373 1.1 pk /* Copy local arrays to user space. */
374 1.1 pk if ((error = copyout(red + start, cmap->red, count)) != 0)
375 1.1 pk return (error);
376 1.1 pk if ((error = copyout(green + start, cmap->green, count)) != 0)
377 1.1 pk return (error);
378 1.1 pk if ((error = copyout(blue + start, cmap->blue, count)) != 0)
379 1.1 pk return (error);
380 1.1 pk
381 1.1 pk return (0);
382 1.1 pk }
383 1.1 pk
384 1.1 pk /*
385 1.1 pk */
386 1.1 pk int
387 1.1 pk cgtwoputcmap(sc, cmap)
388 1.1 pk register struct cgtwo_softc *sc;
389 1.1 pk register struct fbcmap *cmap;
390 1.1 pk {
391 1.1 pk u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
392 1.1 pk int error, start, count, ecount;
393 1.1 pk register u_int i;
394 1.3 pk register volatile u_short *p;
395 1.1 pk
396 1.1 pk start = cmap->index;
397 1.1 pk count = cmap->count;
398 1.1 pk ecount = start + count;
399 1.1 pk if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
400 1.1 pk return (EINVAL);
401 1.1 pk
402 1.1 pk /* Copy from user space to local arrays. */
403 1.1 pk if ((error = copyin(cmap->red, red + start, count)) != 0)
404 1.1 pk return (error);
405 1.1 pk if ((error = copyin(cmap->green, green + start, count)) != 0)
406 1.1 pk return (error);
407 1.1 pk if ((error = copyin(cmap->blue, blue + start, count)) != 0)
408 1.1 pk return (error);
409 1.1 pk
410 1.1 pk /* XXX - Wait for retrace? */
411 1.1 pk
412 1.1 pk /* Copy from local arrays to hardware. */
413 1.3 pk p = &sc_redmap(sc)[start];
414 1.1 pk for (i = start; i < ecount; i++)
415 1.1 pk *p++ = red[i];
416 1.3 pk p = &sc_greenmap(sc)[start];
417 1.1 pk for (i = start; i < ecount; i++)
418 1.1 pk *p++ = green[i];
419 1.3 pk p = &sc_bluemap(sc)[start];
420 1.1 pk for (i = start; i < ecount; i++)
421 1.1 pk *p++ = blue[i];
422 1.1 pk
423 1.1 pk return (0);
424 1.1 pk }
425 1.1 pk
426 1.1 pk /*
427 1.1 pk * Return the address that would map the given device at the given
428 1.1 pk * offset, allowing for the given protection, or return -1 for error.
429 1.1 pk */
430 1.1 pk int
431 1.1 pk cgtwommap(dev, off, prot)
432 1.1 pk dev_t dev;
433 1.1 pk int off, prot;
434 1.1 pk {
435 1.12 thorpej register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
436 1.1 pk
437 1.1 pk if (off & PGOFSET)
438 1.1 pk panic("cgtwommap");
439 1.1 pk
440 1.1 pk if ((unsigned)off >= sc->sc_fb.fb_type.fb_size)
441 1.1 pk return (-1);
442 1.1 pk
443 1.8 pk return (REG2PHYS(&sc->sc_phys, off, PMAP_VME32/*sc->sc_bustype*/) | PMAP_NC);
444 1.1 pk }
445