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