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