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