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