cgtwo.c revision 1.48 1 /* $NetBSD: cgtwo.c,v 1.48 2003/08/24 17:32:00 uwe 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. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: @(#)cgthree.c 8.2 (Berkeley) 10/30/93
41 */
42
43 /*
44 * color display (cgtwo) driver.
45 *
46 * Does not handle interrupts, even though they can occur.
47 *
48 * XXX should defer colormap updates to vertical retrace interrupts
49 */
50
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: cgtwo.c,v 1.48 2003/08/24 17:32:00 uwe Exp $");
53
54 #include "opt_rcons.h"
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/buf.h>
59 #include <sys/device.h>
60 #include <sys/ioctl.h>
61 #include <sys/malloc.h>
62 #include <sys/mman.h>
63 #include <sys/tty.h>
64 #include <sys/conf.h>
65
66 #include <uvm/uvm_extern.h>
67
68 #include <machine/autoconf.h>
69
70 #include <dev/sun/fbio.h>
71 #include <dev/sun/fbvar.h>
72
73 #include <dev/vme/vmevar.h>
74
75 #include <machine/eeprom.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 CFATTACH_DECL(cgtwo, sizeof(struct cgtwo_softc),
101 cgtwomatch, cgtwoattach, NULL, NULL);
102
103 extern struct cfdriver cgtwo_cd;
104
105 dev_type_open(cgtwoopen);
106 dev_type_ioctl(cgtwoioctl);
107 dev_type_mmap(cgtwommap);
108
109 const struct cdevsw cgtwo_cdevsw = {
110 cgtwoopen, nullclose, noread, nowrite, cgtwoioctl,
111 nostop, notty, nopoll, cgtwommap, nokqfilter,
112 };
113
114 /* frame buffer generic driver */
115 static struct fbdriver cgtwofbdriver = {
116 cgtwounblank, cgtwoopen, nullclose, cgtwoioctl, nopoll, cgtwommap,
117 nokqfilter
118 };
119
120 /*
121 * Match a cgtwo.
122 */
123 int
124 cgtwomatch(parent, cf, aux)
125 struct device *parent;
126 struct cfdata *cf;
127 void *aux;
128 {
129 struct vme_attach_args *va = aux;
130 vme_chipset_tag_t ct = va->va_vct;
131 vme_am_t mod;
132
133 /*
134 * Mask out invalid flags from the user.
135 */
136 cf->cf_flags &= FB_USERMASK;
137
138 mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
139 if (vme_probe(ct, va->r[0].offset + CG2_CTLREG_OFF, 2, mod, VME_D16,
140 0, 0)) {
141 return (0);
142 }
143
144 return (1);
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->va_vct;
157 bus_space_tag_t bt;
158 bus_space_handle_t bh;
159 vme_am_t mod;
160 vme_mapresc_t resc;
161 struct cgtwo_softc *sc = (struct cgtwo_softc *)self;
162 struct fbdevice *fb = &sc->sc_fb;
163 struct eeprom *eep = (struct eeprom *)eeprom_va;
164 int isconsole = 0;
165
166 sc->sc_ct = ct;
167 fb->fb_driver = &cgtwofbdriver;
168 fb->fb_device = &sc->sc_dev;
169 fb->fb_type.fb_type = FBTYPE_SUN2COLOR;
170 fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
171
172 fb->fb_type.fb_depth = 8;
173 fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
174
175 fb->fb_type.fb_cmsize = 256;
176 fb->fb_type.fb_size = roundup(CG2_MAPPED_SIZE, PAGE_SIZE);
177 printf(": cgtwo, %d x %d",
178 fb->fb_type.fb_width, 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 sc->sc_paddr = va->r[0].offset;
187 mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
188
189 if (vme_space_map(ct, sc->sc_paddr + CG2_ROPMEM_OFF +
190 offsetof(struct cg2fb, status.reg),
191 sizeof(struct cg2statusreg), mod, VME_D16, 0,
192 &bt, &bh, &resc) != 0)
193 panic("cgtwo: vme_map status");
194 sc->sc_bt = bt;
195 sc->sc_reg = (volatile struct cg2statusreg *)bh; /* XXX */
196
197 if (vme_space_map(ct, sc->sc_paddr + CG2_ROPMEM_OFF +
198 offsetof(struct cg2fb, redmap[0]),
199 3 * CG2_CMSIZE, mod, VME_D16, 0,
200 &bt, &bh, &resc) != 0)
201 panic("cgtwo: vme_map cmap");
202 sc->sc_cmap = (volatile u_short *)bh; /* XXX */
203
204 /*
205 * Assume this is the console if there's no eeprom info
206 * to be found.
207 */
208 if (eep == NULL || eep->eeConsole == EE_CONS_COLOR)
209 isconsole = fb_is_console(0);
210 else
211 isconsole = 0;
212
213 if (isconsole) {
214 if (vme_space_map(ct, sc->sc_paddr + CG2_PIXMAP_OFF,
215 CG2_PIXMAP_SIZE, mod, VME_D16, 0,
216 &bt, &bh, &resc) != 0)
217 panic("cgtwo: vme_map pixels");
218
219 fb->fb_pixels = (caddr_t)bh; /* XXX */
220 printf(" (console)\n");
221 #ifdef RASTERCONSOLE
222 fbrcons_init(fb);
223 #endif
224 } else
225 printf("\n");
226
227 fb_attach(fb, isconsole);
228 }
229
230 int
231 cgtwoopen(dev, flags, mode, p)
232 dev_t dev;
233 int flags, mode;
234 struct proc *p;
235 {
236 int unit = minor(dev);
237
238 if (unit >= cgtwo_cd.cd_ndevs || cgtwo_cd.cd_devs[unit] == NULL)
239 return (ENXIO);
240 return (0);
241 }
242
243 int
244 cgtwoioctl(dev, cmd, data, flags, p)
245 dev_t dev;
246 u_long cmd;
247 register caddr_t data;
248 int flags;
249 struct proc *p;
250 {
251 register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
252 register struct fbgattr *fba;
253
254 switch (cmd) {
255
256 case FBIOGTYPE:
257 *(struct fbtype *)data = sc->sc_fb.fb_type;
258 break;
259
260 case FBIOGATTR:
261 fba = (struct fbgattr *)data;
262 fba->real_type = sc->sc_fb.fb_type.fb_type;
263 fba->owner = 0; /* XXX ??? */
264 fba->fbtype = sc->sc_fb.fb_type;
265 fba->sattr.flags = 0;
266 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
267 fba->sattr.dev_specific[0] = -1;
268 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
269 fba->emu_types[1] = -1;
270 break;
271
272 case FBIOGETCMAP:
273 return cgtwogetcmap(sc, (struct fbcmap *) data);
274
275 case FBIOPUTCMAP:
276 return cgtwoputcmap(sc, (struct fbcmap *) data);
277
278 case FBIOGVIDEO:
279 *(int *)data = sc->sc_reg->video_enab;
280 break;
281
282 case FBIOSVIDEO:
283 sc->sc_reg->video_enab = (*(int*)data) & 1;
284 break;
285
286 default:
287 return (ENOTTY);
288 }
289 return (0);
290 }
291
292 /*
293 * Undo the effect of an FBIOSVIDEO that turns the video off.
294 */
295 static void
296 cgtwounblank(dev)
297 struct device *dev;
298 {
299 struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
300 sc->sc_reg->video_enab = 1;
301 }
302
303 /*
304 */
305 int
306 cgtwogetcmap(sc, cmap)
307 register struct cgtwo_softc *sc;
308 register struct fbcmap *cmap;
309 {
310 u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
311 int error, start, count, ecount;
312 register u_int i;
313 register volatile u_short *p;
314
315 start = cmap->index;
316 count = cmap->count;
317 ecount = start + count;
318 if (start >= CG2_CMSIZE || count > CG2_CMSIZE - start)
319 return (EINVAL);
320
321 /* XXX - Wait for retrace? */
322
323 /* Copy hardware to local arrays. */
324 p = &sc_redmap(sc)[start];
325 for (i = start; i < ecount; i++)
326 red[i] = *p++;
327 p = &sc_greenmap(sc)[start];
328 for (i = start; i < ecount; i++)
329 green[i] = *p++;
330 p = &sc_bluemap(sc)[start];
331 for (i = start; i < ecount; i++)
332 blue[i] = *p++;
333
334 /* Copy local arrays to user space. */
335 if ((error = copyout(red + start, cmap->red, count)) != 0)
336 return (error);
337 if ((error = copyout(green + start, cmap->green, count)) != 0)
338 return (error);
339 if ((error = copyout(blue + start, cmap->blue, count)) != 0)
340 return (error);
341
342 return (0);
343 }
344
345 /*
346 */
347 int
348 cgtwoputcmap(sc, cmap)
349 register struct cgtwo_softc *sc;
350 register struct fbcmap *cmap;
351 {
352 u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
353 int error;
354 u_int start, count, ecount;
355 register u_int i;
356 register volatile u_short *p;
357
358 start = cmap->index;
359 count = cmap->count;
360 ecount = start + count;
361 if (start >= CG2_CMSIZE || count > CG2_CMSIZE - start)
362 return (EINVAL);
363
364 /* Copy from user space to local arrays. */
365 if ((error = copyin(cmap->red, red + start, count)) != 0)
366 return (error);
367 if ((error = copyin(cmap->green, green + start, count)) != 0)
368 return (error);
369 if ((error = copyin(cmap->blue, blue + start, count)) != 0)
370 return (error);
371
372 /* XXX - Wait for retrace? */
373
374 /* Copy from local arrays to hardware. */
375 p = &sc_redmap(sc)[start];
376 for (i = start; i < ecount; i++)
377 *p++ = red[i];
378 p = &sc_greenmap(sc)[start];
379 for (i = start; i < ecount; i++)
380 *p++ = green[i];
381 p = &sc_bluemap(sc)[start];
382 for (i = start; i < ecount; i++)
383 *p++ = blue[i];
384
385 return (0);
386 }
387
388 /*
389 * Return the address that would map the given device at the given
390 * offset, allowing for the given protection, or return -1 for error.
391 */
392 paddr_t
393 cgtwommap(dev, off, prot)
394 dev_t dev;
395 off_t off;
396 int prot;
397 {
398 register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
399 vme_am_t mod;
400 bus_space_handle_t bh;
401 extern int sparc_vme_mmap_cookie __P((vme_addr_t, vme_am_t,
402 bus_space_handle_t *));
403
404 if (off & PGOFSET)
405 panic("cgtwommap");
406
407 if (off >= sc->sc_fb.fb_type.fb_size)
408 return (-1);
409
410 /* Apparently, the pixels are in 32-bit data space */
411 mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
412
413 if (sparc_vme_mmap_cookie(sc->sc_paddr + off, mod, &bh) != 0)
414 panic("cgtwommap");
415
416 return ((paddr_t)bh);
417 }
418