cgeight.c revision 1.3 1 /* $NetBSD: cgeight.c,v 1.3 1996/03/14 19:44:36 christos Exp $ */
2
3 /*
4 * Copyright (c) 1996 Jason R. Thorpe. All rights reserved.
5 * Copyright (c) 1995 Theo de Raadt
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This software was developed by the Computer Systems Engineering group
10 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11 * contributed to Berkeley.
12 *
13 * All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Lawrence Berkeley Laboratory.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 *
46 * from @(#)cgthree.c 8.2 (Berkeley) 10/30/93
47 */
48
49 /*
50 * color display (cgeight) driver.
51 *
52 * Does not handle interrupts, even though they can occur.
53 *
54 * XXX should defer colormap updates to vertical retrace interrupts
55 */
56
57 #include <sys/param.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
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 #include <machine/eeprom.h>
72
73 #include <sparc/dev/btreg.h>
74 #include <sparc/dev/btvar.h>
75 #include <sparc/dev/pfourreg.h>
76 #include <sparc/dev/dev_conf.h>
77
78 /* per-display variables */
79 struct cgeight_softc {
80 struct device sc_dev; /* base device */
81 struct fbdevice sc_fb; /* frame buffer device */
82 struct rom_reg sc_phys; /* display RAM (phys addr) */
83 volatile struct fbcontrol *sc_fbc; /* Brooktree registers */
84 int sc_bustype; /* type of bus we live on */
85 union bt_cmap sc_cmap; /* Brooktree color map */
86 };
87
88 /* autoconfiguration driver */
89 static void cgeightattach(struct device *, struct device *, void *);
90 static int cgeightmatch(struct device *, void *, void *);
91 int cgeightopen __P((dev_t, int, int, struct proc *));
92 int cgeightclose __P((dev_t, int, int, struct proc *));
93 int cgeightioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
94 int cgeightmmap __P((dev_t, int, int));
95 static void cgeightunblank __P((struct device *));
96
97 struct cfdriver cgeightcd = {
98 NULL, "cgeight", cgeightmatch, cgeightattach,
99 DV_DULL, sizeof(struct cgeight_softc)
100 };
101
102 /* frame buffer generic driver */
103 static struct fbdriver cgeightfbdriver = {
104 cgeightunblank, cgeightopen, cgeightclose, cgeightioctl, cgeightmmap
105 };
106
107 extern int fbnode;
108 extern struct tty *fbconstty;
109
110 static void cgeightloadcmap __P((struct cgeight_softc *, int, int));
111 static int cgeight_get_video __P((struct cgeight_softc *));
112 static void cgeight_set_video __P((struct cgeight_softc *, int));
113
114 /*
115 * Match a cgeight.
116 */
117 int
118 cgeightmatch(parent, vcf, aux)
119 struct device *parent;
120 void *vcf, *aux;
121 {
122 struct cfdata *cf = vcf;
123 struct confargs *ca = aux;
124 struct romaux *ra = &ca->ca_ra;
125
126 if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
127 return (0);
128
129 /*
130 * Mask out invalid flags from the user.
131 */
132 cf->cf_flags &= FB_USERMASK;
133
134 /*
135 * Only exists on a sun4.
136 */
137 if (!CPU_ISSUN4)
138 return (0);
139
140 /*
141 * Only exists on obio.
142 */
143 if (ca->ca_bustype != BUS_OBIO)
144 return (0);
145
146 /*
147 * Make sure there's hardware there.
148 */
149 if (probeget(ra->ra_vaddr, 4) == -1)
150 return (0);
151
152 #if defined(SUN4)
153 /*
154 * Check the pfour register.
155 */
156 if (fb_pfour_id(ra->ra_vaddr) == PFOUR_ID_COLOR24) {
157 cf->cf_flags |= FB_PFOUR;
158 return (1);
159 }
160 #endif
161
162 return (0);
163 }
164
165 /*
166 * Attach a display. We need to notice if it is the console, too.
167 */
168 void
169 cgeightattach(parent, self, args)
170 struct device *parent, *self;
171 void *args;
172 {
173 register struct cgeight_softc *sc = (struct cgeight_softc *)self;
174 register struct confargs *ca = args;
175 register int node = 0, ramsize, i;
176 register volatile struct bt_regs *bt;
177 struct fbdevice *fb;
178 int isconsole;
179
180 fb->fb_driver = &cgeightfbdriver;
181 fb->fb_device = &sc->sc_dev;
182 fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
183 fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
184
185 /*
186 * Only pfour cgfours, thank you...
187 */
188 if ((ca->ca_bustype != BUS_OBIO) ||
189 ((fb->fb_flags & FB_PFOUR) == 0)) {
190 printf("%s: ignoring; not a pfour\n", sc->sc_dev.dv_xname);
191 return;
192 }
193
194 /* Map the pfour register. */
195 fb->fb_pfour = (volatile u_int32_t *)mapiodev(ca->ca_ra.ra_reg, 0,
196 sizeof(u_int32_t), ca->ca_bustype);
197
198 ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
199
200 fb->fb_type.fb_depth = 24;
201 fb_setsize(fb, fb->fb_type.fb_depth, 1152, 900, node, ca->ca_bustype);
202
203 sc->sc_fb.fb_type.fb_cmsize = 256;
204 sc->sc_fb.fb_type.fb_size = ramsize;
205 printf(": cgeight/p4, %d x %d", fb->fb_type.fb_width,
206 fb->fb_type.fb_height);
207
208 isconsole = 0;
209
210 #if defined(SUN4)
211 if (cputyp == CPU_SUN4) {
212 struct eeprom *eep = (struct eeprom *)eeprom_va;
213
214 /*
215 * Assume this is the console if there's no eeprom info
216 * to be found.
217 */
218 if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
219 isconsole = (fbconstty != NULL);
220 }
221 #endif
222
223 #if 0
224 /*
225 * We don't do any of the console handling here. Instead,
226 * we let the bwtwo driver pick up the overlay plane and
227 * use it instead. Rconsole should have better performance
228 * with the 1-bit depth.
229 * -- Jason R. Thorpe <thorpej (at) NetBSD.ORG>
230 */
231
232 /*
233 * When the ROM has mapped in a cgfour display, the address
234 * maps only the video RAM, so in any case we have to map the
235 * registers ourselves. We only need the video RAM if we are
236 * going to print characters via rconsole.
237 */
238
239 if (isconsole) {
240 /* XXX this is kind of a waste */
241 fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
242 PFOUR_COLOR_OFF_OVERLAY, ramsize, ca->ca_bustype);
243 }
244 #endif
245
246 /* Map the Brooktree. */
247 sc->sc_fbc = (volatile struct fbcontrol *)mapiodev(ca->ca_ra.ra_reg,
248 PFOUR_COLOR_OFF_CMAP, sizeof(struct fbcontrol), ca->ca_bustype);
249
250 sc->sc_phys = ca->ca_ra.ra_reg[0];
251 sc->sc_bustype = ca->ca_bustype;
252
253 #if 0 /* XXX thorpej ??? */
254 /* tell the enable plane to look at the mono image */
255 memset(ca->ca_ra.ra_vaddr, 0xff,
256 sc->sc_fb.fb_type.fb_width * sc->sc_fb.fb_type.fb_height / 8);
257 #endif
258
259 /* grab initial (current) color map */
260 bt = &sc->sc_fbc->fbc_dac;
261 bt->bt_addr = 0;
262 for (i = 0; i < 256 * 3 / 4; i++)
263 sc->sc_cmap.cm_chip[i] = bt->bt_cmap;
264
265 BT_INIT(bt, 0);
266
267 #if 0 /* see above */
268 if (isconsole) {
269 printf(" (console)\n");
270 #if defined(RASTERCONSOLE) && 0 /* XXX been told it doesn't work well. */
271 fbrcons_init(fb);
272 #endif
273 } else
274 #endif /* 0 */
275 printf("\n");
276
277 /*
278 * Even though we're not using rconsole, we'd still like
279 * to notice if we're the console framebuffer.
280 */
281 fb_attach(&sc->sc_fb, isconsole);
282 }
283
284 int
285 cgeightopen(dev, flags, mode, p)
286 dev_t dev;
287 int flags, mode;
288 struct proc *p;
289 {
290 int unit = minor(dev);
291
292 if (unit >= cgeightcd.cd_ndevs || cgeightcd.cd_devs[unit] == NULL)
293 return (ENXIO);
294 return (0);
295 }
296
297 int
298 cgeightclose(dev, flags, mode, p)
299 dev_t dev;
300 int flags, mode;
301 struct proc *p;
302 {
303
304 return (0);
305 }
306
307 int
308 cgeightioctl(dev, cmd, data, flags, p)
309 dev_t dev;
310 u_long cmd;
311 register caddr_t data;
312 int flags;
313 struct proc *p;
314 {
315 register struct cgeight_softc *sc = cgeightcd.cd_devs[minor(dev)];
316 register struct fbgattr *fba;
317 int error;
318
319 switch (cmd) {
320
321 case FBIOGTYPE:
322 *(struct fbtype *)data = sc->sc_fb.fb_type;
323 break;
324
325 case FBIOGATTR:
326 fba = (struct fbgattr *)data;
327 fba->real_type = sc->sc_fb.fb_type.fb_type;
328 fba->owner = 0; /* XXX ??? */
329 fba->fbtype = sc->sc_fb.fb_type;
330 fba->sattr.flags = 0;
331 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
332 fba->sattr.dev_specific[0] = -1;
333 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
334 fba->emu_types[1] = -1;
335 break;
336
337 case FBIOGETCMAP:
338 return (bt_getcmap((struct fbcmap *)data, &sc->sc_cmap, 256));
339
340 case FBIOPUTCMAP:
341 /* copy to software map */
342 #define p ((struct fbcmap *)data)
343 error = bt_putcmap(p, &sc->sc_cmap, 256);
344 if (error)
345 return (error);
346 /* now blast them into the chip */
347 /* XXX should use retrace interrupt */
348 cgeightloadcmap(sc, p->index, p->count);
349 #undef p
350 break;
351
352 case FBIOGVIDEO:
353 *(int *)data = cgeight_get_video(sc);
354 break;
355
356 case FBIOSVIDEO:
357 cgeight_set_video(sc, *(int *)data);
358 break;
359
360 default:
361 return (ENOTTY);
362 }
363 return (0);
364 }
365
366 /*
367 * Undo the effect of an FBIOSVIDEO that turns the video off.
368 */
369 static void
370 cgeightunblank(dev)
371 struct device *dev;
372 {
373
374 cgeight_set_video((struct cgeight_softc *)dev, 1);
375 }
376
377 /*
378 * Load a subset of the current (new) colormap into the Brooktree DAC.
379 */
380 static void
381 cgeightloadcmap(sc, start, ncolors)
382 register struct cgeight_softc *sc;
383 register int start, ncolors;
384 {
385 register volatile struct bt_regs *bt;
386 register u_int *ip;
387 register int count;
388
389 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */
390 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
391 bt = &sc->sc_fbc->fbc_dac;
392 bt->bt_addr = BT_D4M4(start);
393 while (--count >= 0)
394 bt->bt_cmap = *ip++;
395 }
396
397 /*
398 * Return the address that would map the given device at the given
399 * offset, allowing for the given protection, or return -1 for error.
400 *
401 * The cg8 maps it's overlay plane at 0 for 128K, followed by the
402 * enable plane for 128K, followed by the colour for as long as it
403 * goes. Starting at 8MB, it maps the ramdac for NBPG, then the p4
404 * register for NBPG, then the bootrom for 0x40000.
405 */
406 int
407 cgeightmmap(dev, off, prot)
408 dev_t dev;
409 int off, prot;
410 {
411 register struct cgeight_softc *sc = cgeightcd.cd_devs[minor(dev)];
412 int poff;
413
414 #define START_ENABLE (128*1024)
415 #define START_COLOR ((128*1024) + (128*1024))
416 #define COLOR_SIZE (sc->sc_fb.fb_type.fb_width * \
417 sc->sc_fb.fb_type.fb_height * 3)
418 #define END_COLOR (START_COLOR + COLOR_SIZE)
419 #define START_SPECIAL 0x800000
420 #define PROMSIZE 0x40000
421 #define NOOVERLAY (0x04000000)
422
423 if (off & PGOFSET)
424 panic("cgeightmap");
425
426 if ((u_int)off >= NOOVERLAY) {
427 off =- NOOVERLAY;
428
429 /*
430 * X11 maps a huge chunk of the frame buffer; far more than
431 * there really is. We compensate by double-mapping the
432 * first page for as many other pages as it wants
433 */
434 while (off >= COLOR_SIZE)
435 off -= COLOR_SIZE; /* XXX thorpej ??? */
436
437 poff = off + PFOUR_COLOR_OFF_COLOR;
438 } else if ((u_int)off < START_ENABLE) {
439 /*
440 * in overlay plane
441 */
442 poff = PFOUR_COLOR_OFF_OVERLAY + off;
443 } else if ((u_int)off < START_COLOR) {
444 /*
445 * in enable plane
446 */
447 poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
448 } else if ((u_int)off < END_COLOR) {
449 /*
450 * in colour plane
451 */
452 poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
453 } else if ((u_int)off < START_SPECIAL) {
454 /*
455 * hole
456 */
457 poff = 0; /* XXX */
458 } else if ((u_int)off == START_SPECIAL) {
459 /*
460 * colour map (Brooktree)
461 */
462 poff = PFOUR_COLOR_OFF_CMAP;
463 } else if ((u_int)off == START_SPECIAL + NBPG) {
464 /*
465 * p4 register
466 */
467 poff = 0;
468 } else if ((u_int)off > (START_SPECIAL + (NBPG * 2)) &&
469 (u_int) off < (START_SPECIAL + (NBPG * 2) + PROMSIZE)) {
470 /*
471 * rom
472 */
473 poff = 0x8000 + (off - (START_SPECIAL + (NBPG * 2)));
474 } else
475 return (-1);
476 /*
477 * I turned on PMAP_NC here to disable the cache as I was
478 * getting horribly broken behaviour with it on.
479 */
480 return (REG2PHYS(&sc->sc_phys, poff, sc->sc_bustype) | PMAP_NC);
481 }
482
483 static int
484 cgeight_get_video(sc)
485 struct cgeight_softc *sc;
486 {
487
488 return (fb_pfour_get_video(&sc->sc_fb));
489 }
490
491 static void
492 cgeight_set_video(sc, enable)
493 struct cgeight_softc *sc;
494 int enable;
495 {
496
497 fb_pfour_set_video(&sc->sc_fb, enable);
498 }
499