cgtwo.c revision 1.4 1 /* $NetBSD: cgtwo.c,v 1.4 1995/10/04 23:35:16 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/buf.h>
57 #include <sys/device.h>
58 #include <sys/ioctl.h>
59 #include <sys/malloc.h>
60 #include <sys/mman.h>
61 #include <sys/tty.h>
62
63 #include <vm/vm.h>
64
65 #include <machine/fbio.h>
66 #include <machine/autoconf.h>
67 #include <machine/pmap.h>
68 #include <machine/fbvar.h>
69 #if defined(SUN4)
70 #include <machine/eeprom.h>
71 #endif
72
73 #include <machine/cgtworeg.h>
74 #include <sparc/dev/sbusvar.h>
75
76 /* per-display variables */
77 struct cgtwo_softc {
78 struct device sc_dev; /* base device */
79 struct sbusdev sc_sd; /* sbus device */
80 struct fbdevice sc_fb; /* frame buffer device */
81 caddr_t sc_phys; /* display RAM (phys addr) */
82 volatile struct cg2statusreg *sc_reg; /* CG2 control registers */
83 volatile u_short *sc_cmap;
84 #define sc_redmap(sc) ((sc)->sc_cmap)
85 #define sc_greenmap(sc) ((sc)->sc_cmap + CG2_CMSIZE)
86 #define sc_bluemap(sc) ((sc)->sc_cmap + 2 * CG2_CMSIZE)
87 };
88
89 /* autoconfiguration driver */
90 static void cgtwoattach(struct device *, struct device *, void *);
91 static int cgtwomatch(struct device *, void *, void *);
92 int cgtwoopen __P((dev_t, int, int, struct proc *));
93 int cgtwoclose __P((dev_t, int, int, struct proc *));
94 int cgtwoioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
95 int cgtwommap __P((dev_t, int, int));
96 static void cgtwounblank(struct device *);
97
98 struct cfdriver cgtwocd = {
99 NULL, "cgtwo", cgtwomatch, cgtwoattach,
100 DV_DULL, sizeof(struct cgtwo_softc)
101 };
102
103 /* frame buffer generic driver */
104 static struct fbdriver cgtwofbdriver = {
105 cgtwounblank, cgtwoopen, cgtwoclose, cgtwoioctl, cgtwommap
106 };
107
108 extern int fbnode;
109 extern struct tty *fbconstty;
110 extern int nullop();
111 static int cgtwo_cnputc();
112
113 static void cgtwoloadcmap __P((struct cgtwo_softc *, int, int));
114
115 /*
116 * Match a cgtwo.
117 */
118 int
119 cgtwomatch(parent, vcf, aux)
120 struct device *parent;
121 void *vcf, *aux;
122 {
123 struct cfdata *cf = vcf;
124 struct confargs *ca = aux;
125 struct romaux *ra = &ca->ca_ra;
126 int probe;
127 caddr_t tmp;
128
129 if (ca->ca_bustype != BUS_VME16)
130 return (0);
131
132 if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
133 return (0);
134
135 #if defined(SUN4)
136 if (cputyp != CPU_SUN4 || cf->cf_unit != 0)
137 return (0);
138
139 /* XXX - Must do our own mapping at CG2_CTLREG_OFF */
140 bus_untmp();
141 tmp = (caddr_t)bus_tmp(ra->ra_paddr + CG2_CTLREG_OFF, ca->ca_bustype);
142 if (probeget(tmp, 2) != -1)
143 return 1;
144 #endif
145 return 0;
146 }
147
148 /*
149 * Attach a display. We need to notice if it is the console, too.
150 */
151 void
152 cgtwoattach(parent, self, args)
153 struct device *parent, *self;
154 void *args;
155 {
156 register struct cgtwo_softc *sc = (struct cgtwo_softc *)self;
157 register struct confargs *ca = args;
158 register int node, i;
159 register struct cgtwo_all *p;
160 int isconsole;
161 int sbus = 1;
162 char *nam;
163
164 sc->sc_fb.fb_driver = &cgtwofbdriver;
165 sc->sc_fb.fb_device = &sc->sc_dev;
166 /*
167 * The defaults below match my screen, but are not guaranteed
168 * to be correct as defaults go...
169 */
170 sc->sc_fb.fb_type.fb_type = FBTYPE_SUN2COLOR;
171 switch (ca->ca_bustype) {
172 case BUS_OBIO:
173 case BUS_VME32:
174 case BUS_VME16:
175 sbus = node = 0;
176 sc->sc_fb.fb_type.fb_width = 1152;
177 sc->sc_fb.fb_type.fb_height = 900;
178 sc->sc_fb.fb_linebytes = 1152;
179 nam = "cgtwo";
180
181 #if defined(SUN4)
182 if (cputyp==CPU_SUN4) {
183 struct eeprom *eep = (struct eeprom *)eeprom_va;
184 if (eep != NULL) {
185 switch (eep->eeScreenSize) {
186 case EE_SCR_1152X900:
187 /* Handled above. */
188 break;
189
190 case EE_SCR_1024X1024:
191 sc->sc_fb.fb_type.fb_width = 1024;
192 sc->sc_fb.fb_type.fb_height = 1024;
193 sc->sc_fb.fb_linebytes = 128;
194 break;
195
196 case EE_SCR_1600X1280:
197 sc->sc_fb.fb_type.fb_width = 1600;
198 sc->sc_fb.fb_type.fb_height = 1280;
199 sc->sc_fb.fb_linebytes = 200;
200 break;
201
202 case EE_SCR_1440X1440:
203 sc->sc_fb.fb_type.fb_width = 1440;
204 sc->sc_fb.fb_type.fb_height = 1440;
205 sc->sc_fb.fb_linebytes = 180;
206 break;
207
208 default:
209 /*
210 * XXX: Do nothing, I guess.
211 * Should we print a warning about
212 * an unknown value? --thorpej
213 */
214 break;
215 }
216 }
217 }
218 #endif
219 break;
220 #if 0
221 case BUS_SBUS:
222 node = ca->ca_ra.ra_node;
223 sc->sc_fb.fb_type.fb_width = getpropint(node, "width", 1152);
224 sc->sc_fb.fb_type.fb_height = getpropint(node, "height", 900);
225 sc->sc_fb.fb_linebytes = getpropint(node, "linebytes", 1152);
226 nam = getpropstring(node, "model");
227 break;
228 #endif
229 }
230
231 sc->sc_fb.fb_type.fb_depth = 8;
232 sc->sc_fb.fb_type.fb_cmsize = 256;
233 sc->sc_fb.fb_type.fb_size = roundup(CG2_MAPPED_SIZE, NBPG);
234 printf(": %s, %d x %d", nam,
235 sc->sc_fb.fb_type.fb_width, sc->sc_fb.fb_type.fb_height);
236
237 /*
238 * When the ROM has mapped in a cgtwo display, the address
239 * maps only the video RAM, so in any case we have to map the
240 * registers ourselves. We only need the video RAM if we are
241 * going to print characters via rconsole.
242 */
243 #if defined(SUN4)
244 if (cputyp == CPU_SUN4) {
245 struct eeprom *eep = (struct eeprom *)eeprom_va;
246 /*
247 * Assume this is the console if there's no eeprom info
248 * to be found.
249 */
250 if (eep == NULL || eep->eeConsole == EE_CONS_COLOR)
251 isconsole = (fbconstty != NULL);
252 else
253 isconsole = 0;
254 }
255 #endif
256 #if defined(SUN4C) || defined(SUN4M)
257 if (cputyp == CPU_SUN4C || cputyp == CPU_SUN4M)
258 isconsole = node == fbnode && fbconstty != NULL;
259 #endif
260 sc->sc_phys = (caddr_t)ca->ca_ra.ra_paddr;
261 if ((sc->sc_fb.fb_pixels = ca->ca_ra.ra_vaddr) == NULL && isconsole) {
262 /* this probably cannot happen, but what the heck */
263 sc->sc_fb.fb_pixels = mapiodev(sc->sc_phys + CG2_PIXMAP_OFF,
264 CG2_PIXMAP_SIZE,
265 ca->ca_bustype);
266 }
267 #ifndef offsetof
268 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
269 #endif
270
271 sc->sc_reg = (volatile struct cg2statusreg *)
272 mapiodev((caddr_t)sc->sc_phys +
273 CG2_ROPMEM_OFF + offsetof(struct cg2fb, status.reg),
274 sizeof(struct cg2statusreg), ca->ca_bustype);
275
276 sc->sc_cmap = (volatile u_short *)
277 mapiodev((caddr_t)sc->sc_phys +
278 CG2_ROPMEM_OFF + offsetof(struct cg2fb, redmap[0]),
279 3 * CG2_CMSIZE, ca->ca_bustype);
280
281 if (isconsole) {
282 printf(" (console)\n");
283 #ifdef RASTERCONSOLE
284 fbrcons_init(&sc->sc_fb);
285 #endif
286 } else
287 printf("\n");
288 if (sbus)
289 sbus_establish(&sc->sc_sd, &sc->sc_dev);
290 if (node == fbnode)
291 fb_attach(&sc->sc_fb);
292 }
293
294 int
295 cgtwoopen(dev, flags, mode, p)
296 dev_t dev;
297 int flags, mode;
298 struct proc *p;
299 {
300 int unit = minor(dev);
301
302 if (unit >= cgtwocd.cd_ndevs || cgtwocd.cd_devs[unit] == NULL)
303 return (ENXIO);
304 return (0);
305 }
306
307 int
308 cgtwoclose(dev, flags, mode, p)
309 dev_t dev;
310 int flags, mode;
311 struct proc *p;
312 {
313
314 return (0);
315 }
316
317 int
318 cgtwoioctl(dev, cmd, data, flags, p)
319 dev_t dev;
320 u_long cmd;
321 register caddr_t data;
322 int flags;
323 struct proc *p;
324 {
325 register struct cgtwo_softc *sc = cgtwocd.cd_devs[minor(dev)];
326 register struct fbgattr *fba;
327 int error;
328
329 switch (cmd) {
330
331 case FBIOGTYPE:
332 *(struct fbtype *)data = sc->sc_fb.fb_type;
333 break;
334
335 case FBIOGATTR:
336 fba = (struct fbgattr *)data;
337 fba->real_type = sc->sc_fb.fb_type.fb_type;
338 fba->owner = 0; /* XXX ??? */
339 fba->fbtype = sc->sc_fb.fb_type;
340 fba->sattr.flags = 0;
341 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
342 fba->sattr.dev_specific[0] = -1;
343 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
344 fba->emu_types[1] = -1;
345 break;
346
347 case FBIOGETCMAP:
348 return cgtwogetcmap(sc, data);
349
350 case FBIOPUTCMAP:
351 return cgtwoputcmap(sc, data);
352
353 case FBIOGVIDEO:
354 *(int *)data = sc->sc_reg->video_enab;
355 break;
356
357 case FBIOSVIDEO:
358 sc->sc_reg->video_enab = (*(int*)data) & 1;
359 break;
360
361 default:
362 return (ENOTTY);
363 }
364 return (0);
365 }
366
367 /*
368 * Undo the effect of an FBIOSVIDEO that turns the video off.
369 */
370 static void
371 cgtwounblank(dev)
372 struct device *dev;
373 {
374 struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
375 sc->sc_reg->video_enab = 1;
376 }
377
378 /*
379 */
380 int
381 cgtwogetcmap(sc, cmap)
382 register struct cgtwo_softc *sc;
383 register struct fbcmap *cmap;
384 {
385 u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
386 int error, start, count, ecount;
387 register u_int i;
388 register volatile u_short *p;
389
390 start = cmap->index;
391 count = cmap->count;
392 ecount = start + count;
393 if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
394 return (EINVAL);
395
396 /* XXX - Wait for retrace? */
397
398 /* Copy hardware to local arrays. */
399 p = &sc_redmap(sc)[start];
400 for (i = start; i < ecount; i++)
401 red[i] = *p++;
402 p = &sc_greenmap(sc)[start];
403 for (i = start; i < ecount; i++)
404 green[i] = *p++;
405 p = &sc_bluemap(sc)[start];
406 for (i = start; i < ecount; i++)
407 blue[i] = *p++;
408
409 /* Copy local arrays to user space. */
410 if ((error = copyout(red + start, cmap->red, count)) != 0)
411 return (error);
412 if ((error = copyout(green + start, cmap->green, count)) != 0)
413 return (error);
414 if ((error = copyout(blue + start, cmap->blue, count)) != 0)
415 return (error);
416
417 return (0);
418 }
419
420 /*
421 */
422 int
423 cgtwoputcmap(sc, cmap)
424 register struct cgtwo_softc *sc;
425 register struct fbcmap *cmap;
426 {
427 u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
428 int error, start, count, ecount;
429 register u_int i;
430 register volatile u_short *p;
431
432 start = cmap->index;
433 count = cmap->count;
434 ecount = start + count;
435 if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
436 return (EINVAL);
437
438 /* Copy from user space to local arrays. */
439 if ((error = copyin(cmap->red, red + start, count)) != 0)
440 return (error);
441 if ((error = copyin(cmap->green, green + start, count)) != 0)
442 return (error);
443 if ((error = copyin(cmap->blue, blue + start, count)) != 0)
444 return (error);
445
446 /* XXX - Wait for retrace? */
447
448 /* Copy from local arrays to hardware. */
449 p = &sc_redmap(sc)[start];
450 for (i = start; i < ecount; i++)
451 *p++ = red[i];
452 p = &sc_greenmap(sc)[start];
453 for (i = start; i < ecount; i++)
454 *p++ = green[i];
455 p = &sc_bluemap(sc)[start];
456 for (i = start; i < ecount; i++)
457 *p++ = blue[i];
458
459 return (0);
460 }
461
462 /*
463 * Return the address that would map the given device at the given
464 * offset, allowing for the given protection, or return -1 for error.
465 */
466 int
467 cgtwommap(dev, off, prot)
468 dev_t dev;
469 int off, prot;
470 {
471 register struct cgtwo_softc *sc = cgtwocd.cd_devs[minor(dev)];
472
473 if (off & PGOFSET)
474 panic("cgtwommap");
475
476 if ((unsigned)off >= sc->sc_fb.fb_type.fb_size)
477 return (-1);
478
479 return ((int)sc->sc_phys + off + PMAP_VME16 + PMAP_NC);
480 }
481