cgeight.c revision 1.4 1 /* $NetBSD: cgeight.c,v 1.4 1996/03/16 23:28:23 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/systm.h>
59 #include <sys/buf.h>
60 #include <sys/device.h>
61 #include <sys/ioctl.h>
62 #include <sys/malloc.h>
63 #include <sys/mman.h>
64 #include <sys/tty.h>
65
66 #include <vm/vm.h>
67
68 #include <machine/fbio.h>
69 #include <machine/autoconf.h>
70 #include <machine/pmap.h>
71 #include <machine/fbvar.h>
72 #include <machine/eeprom.h>
73
74 #include <sparc/dev/btreg.h>
75 #include <sparc/dev/btvar.h>
76 #include <sparc/dev/pfourreg.h>
77 #include <sparc/dev/dev_conf.h>
78
79 /* per-display variables */
80 struct cgeight_softc {
81 struct device sc_dev; /* base device */
82 struct fbdevice sc_fb; /* frame buffer device */
83 struct rom_reg sc_phys; /* display RAM (phys addr) */
84 volatile struct fbcontrol *sc_fbc; /* Brooktree registers */
85 int sc_bustype; /* type of bus we live on */
86 union bt_cmap sc_cmap; /* Brooktree color map */
87 };
88
89 /* autoconfiguration driver */
90 static void cgeightattach(struct device *, struct device *, void *);
91 static int cgeightmatch(struct device *, void *, void *);
92 int cgeightopen __P((dev_t, int, int, struct proc *));
93 int cgeightclose __P((dev_t, int, int, struct proc *));
94 int cgeightioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
95 int cgeightmmap __P((dev_t, int, int));
96 static void cgeightunblank __P((struct device *));
97
98 struct cfdriver cgeightcd = {
99 NULL, "cgeight", cgeightmatch, cgeightattach,
100 DV_DULL, sizeof(struct cgeight_softc)
101 };
102
103 /* frame buffer generic driver */
104 static struct fbdriver cgeightfbdriver = {
105 cgeightunblank, cgeightopen, cgeightclose, cgeightioctl, cgeightmmap
106 };
107
108 extern int fbnode;
109 extern struct tty *fbconstty;
110
111 static void cgeightloadcmap __P((struct cgeight_softc *, int, int));
112 static int cgeight_get_video __P((struct cgeight_softc *));
113 static void cgeight_set_video __P((struct cgeight_softc *, int));
114
115 /*
116 * Match a cgeight.
117 */
118 int
119 cgeightmatch(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
127 if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
128 return (0);
129
130 /*
131 * Mask out invalid flags from the user.
132 */
133 cf->cf_flags &= FB_USERMASK;
134
135 /*
136 * Only exists on a sun4.
137 */
138 if (!CPU_ISSUN4)
139 return (0);
140
141 /*
142 * Only exists on obio.
143 */
144 if (ca->ca_bustype != BUS_OBIO)
145 return (0);
146
147 /*
148 * Make sure there's hardware there.
149 */
150 if (probeget(ra->ra_vaddr, 4) == -1)
151 return (0);
152
153 #if defined(SUN4)
154 /*
155 * Check the pfour register.
156 */
157 if (fb_pfour_id(ra->ra_vaddr) == PFOUR_ID_COLOR24) {
158 cf->cf_flags |= FB_PFOUR;
159 return (1);
160 }
161 #endif
162
163 return (0);
164 }
165
166 /*
167 * Attach a display. We need to notice if it is the console, too.
168 */
169 void
170 cgeightattach(parent, self, args)
171 struct device *parent, *self;
172 void *args;
173 {
174 register struct cgeight_softc *sc = (struct cgeight_softc *)self;
175 register struct confargs *ca = args;
176 register int node = 0, ramsize, i;
177 register volatile struct bt_regs *bt;
178 struct fbdevice *fb = &sc->sc_fb;
179 int isconsole;
180
181 fb->fb_driver = &cgeightfbdriver;
182 fb->fb_device = &sc->sc_dev;
183 fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
184 fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
185
186 /*
187 * Only pfour cgfours, thank you...
188 */
189 if ((ca->ca_bustype != BUS_OBIO) ||
190 ((fb->fb_flags & FB_PFOUR) == 0)) {
191 printf("%s: ignoring; not a pfour\n", sc->sc_dev.dv_xname);
192 return;
193 }
194
195 /* Map the pfour register. */
196 fb->fb_pfour = (volatile u_int32_t *)mapiodev(ca->ca_ra.ra_reg, 0,
197 sizeof(u_int32_t), ca->ca_bustype);
198
199 ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
200
201 fb->fb_type.fb_depth = 24;
202 fb_setsize(fb, fb->fb_type.fb_depth, 1152, 900, node, ca->ca_bustype);
203
204 sc->sc_fb.fb_type.fb_cmsize = 256;
205 sc->sc_fb.fb_type.fb_size = ramsize;
206 printf(": cgeight/p4, %d x %d", fb->fb_type.fb_width,
207 fb->fb_type.fb_height);
208
209 isconsole = 0;
210
211 #if defined(SUN4)
212 if (cputyp == CPU_SUN4) {
213 struct eeprom *eep = (struct eeprom *)eeprom_va;
214
215 /*
216 * Assume this is the console if there's no eeprom info
217 * to be found.
218 */
219 if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
220 isconsole = (fbconstty != NULL);
221 }
222 #endif
223
224 #if 0
225 /*
226 * We don't do any of the console handling here. Instead,
227 * we let the bwtwo driver pick up the overlay plane and
228 * use it instead. Rconsole should have better performance
229 * with the 1-bit depth.
230 * -- Jason R. Thorpe <thorpej (at) NetBSD.ORG>
231 */
232
233 /*
234 * When the ROM has mapped in a cgfour 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
240 if (isconsole) {
241 /* XXX this is kind of a waste */
242 fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
243 PFOUR_COLOR_OFF_OVERLAY, ramsize, ca->ca_bustype);
244 }
245 #endif
246
247 /* Map the Brooktree. */
248 sc->sc_fbc = (volatile struct fbcontrol *)mapiodev(ca->ca_ra.ra_reg,
249 PFOUR_COLOR_OFF_CMAP, sizeof(struct fbcontrol), ca->ca_bustype);
250
251 sc->sc_phys = ca->ca_ra.ra_reg[0];
252 sc->sc_bustype = ca->ca_bustype;
253
254 #if 0 /* XXX thorpej ??? */
255 /* tell the enable plane to look at the mono image */
256 memset(ca->ca_ra.ra_vaddr, 0xff,
257 sc->sc_fb.fb_type.fb_width * sc->sc_fb.fb_type.fb_height / 8);
258 #endif
259
260 /* grab initial (current) color map */
261 bt = &sc->sc_fbc->fbc_dac;
262 bt->bt_addr = 0;
263 for (i = 0; i < 256 * 3 / 4; i++)
264 sc->sc_cmap.cm_chip[i] = bt->bt_cmap;
265
266 BT_INIT(bt, 0);
267
268 #if 0 /* see above */
269 if (isconsole) {
270 printf(" (console)\n");
271 #if defined(RASTERCONSOLE) && 0 /* XXX been told it doesn't work well. */
272 fbrcons_init(fb);
273 #endif
274 } else
275 #endif /* 0 */
276 printf("\n");
277
278 /*
279 * Even though we're not using rconsole, we'd still like
280 * to notice if we're the console framebuffer.
281 */
282 fb_attach(&sc->sc_fb, isconsole);
283 }
284
285 int
286 cgeightopen(dev, flags, mode, p)
287 dev_t dev;
288 int flags, mode;
289 struct proc *p;
290 {
291 int unit = minor(dev);
292
293 if (unit >= cgeightcd.cd_ndevs || cgeightcd.cd_devs[unit] == NULL)
294 return (ENXIO);
295 return (0);
296 }
297
298 int
299 cgeightclose(dev, flags, mode, p)
300 dev_t dev;
301 int flags, mode;
302 struct proc *p;
303 {
304
305 return (0);
306 }
307
308 int
309 cgeightioctl(dev, cmd, data, flags, p)
310 dev_t dev;
311 u_long cmd;
312 register caddr_t data;
313 int flags;
314 struct proc *p;
315 {
316 register struct cgeight_softc *sc = cgeightcd.cd_devs[minor(dev)];
317 register struct fbgattr *fba;
318 int error;
319
320 switch (cmd) {
321
322 case FBIOGTYPE:
323 *(struct fbtype *)data = sc->sc_fb.fb_type;
324 break;
325
326 case FBIOGATTR:
327 fba = (struct fbgattr *)data;
328 fba->real_type = sc->sc_fb.fb_type.fb_type;
329 fba->owner = 0; /* XXX ??? */
330 fba->fbtype = sc->sc_fb.fb_type;
331 fba->sattr.flags = 0;
332 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
333 fba->sattr.dev_specific[0] = -1;
334 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
335 fba->emu_types[1] = -1;
336 break;
337
338 case FBIOGETCMAP:
339 return (bt_getcmap((struct fbcmap *)data, &sc->sc_cmap, 256));
340
341 case FBIOPUTCMAP:
342 /* copy to software map */
343 #define p ((struct fbcmap *)data)
344 error = bt_putcmap(p, &sc->sc_cmap, 256);
345 if (error)
346 return (error);
347 /* now blast them into the chip */
348 /* XXX should use retrace interrupt */
349 cgeightloadcmap(sc, p->index, p->count);
350 #undef p
351 break;
352
353 case FBIOGVIDEO:
354 *(int *)data = cgeight_get_video(sc);
355 break;
356
357 case FBIOSVIDEO:
358 cgeight_set_video(sc, *(int *)data);
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 cgeightunblank(dev)
372 struct device *dev;
373 {
374
375 cgeight_set_video((struct cgeight_softc *)dev, 1);
376 }
377
378 /*
379 * Load a subset of the current (new) colormap into the Brooktree DAC.
380 */
381 static void
382 cgeightloadcmap(sc, start, ncolors)
383 register struct cgeight_softc *sc;
384 register int start, ncolors;
385 {
386 register volatile struct bt_regs *bt;
387 register u_int *ip;
388 register int count;
389
390 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */
391 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
392 bt = &sc->sc_fbc->fbc_dac;
393 bt->bt_addr = BT_D4M4(start);
394 while (--count >= 0)
395 bt->bt_cmap = *ip++;
396 }
397
398 /*
399 * Return the address that would map the given device at the given
400 * offset, allowing for the given protection, or return -1 for error.
401 *
402 * The cg8 maps it's overlay plane at 0 for 128K, followed by the
403 * enable plane for 128K, followed by the colour for as long as it
404 * goes. Starting at 8MB, it maps the ramdac for NBPG, then the p4
405 * register for NBPG, then the bootrom for 0x40000.
406 */
407 int
408 cgeightmmap(dev, off, prot)
409 dev_t dev;
410 int off, prot;
411 {
412 register struct cgeight_softc *sc = cgeightcd.cd_devs[minor(dev)];
413 int poff;
414
415 #define START_ENABLE (128*1024)
416 #define START_COLOR ((128*1024) + (128*1024))
417 #define COLOR_SIZE (sc->sc_fb.fb_type.fb_width * \
418 sc->sc_fb.fb_type.fb_height * 3)
419 #define END_COLOR (START_COLOR + COLOR_SIZE)
420 #define START_SPECIAL 0x800000
421 #define PROMSIZE 0x40000
422 #define NOOVERLAY (0x04000000)
423
424 if (off & PGOFSET)
425 panic("cgeightmap");
426
427 if ((u_int)off >= NOOVERLAY) {
428 off =- NOOVERLAY;
429
430 /*
431 * X11 maps a huge chunk of the frame buffer; far more than
432 * there really is. We compensate by double-mapping the
433 * first page for as many other pages as it wants
434 */
435 while (off >= COLOR_SIZE)
436 off -= COLOR_SIZE; /* XXX thorpej ??? */
437
438 poff = off + PFOUR_COLOR_OFF_COLOR;
439 } else if ((u_int)off < START_ENABLE) {
440 /*
441 * in overlay plane
442 */
443 poff = PFOUR_COLOR_OFF_OVERLAY + off;
444 } else if ((u_int)off < START_COLOR) {
445 /*
446 * in enable plane
447 */
448 poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
449 } else if ((u_int)off < END_COLOR) {
450 /*
451 * in colour plane
452 */
453 poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
454 } else if ((u_int)off < START_SPECIAL) {
455 /*
456 * hole
457 */
458 poff = 0; /* XXX */
459 } else if ((u_int)off == START_SPECIAL) {
460 /*
461 * colour map (Brooktree)
462 */
463 poff = PFOUR_COLOR_OFF_CMAP;
464 } else if ((u_int)off == START_SPECIAL + NBPG) {
465 /*
466 * p4 register
467 */
468 poff = 0;
469 } else if ((u_int)off > (START_SPECIAL + (NBPG * 2)) &&
470 (u_int) off < (START_SPECIAL + (NBPG * 2) + PROMSIZE)) {
471 /*
472 * rom
473 */
474 poff = 0x8000 + (off - (START_SPECIAL + (NBPG * 2)));
475 } else
476 return (-1);
477 /*
478 * I turned on PMAP_NC here to disable the cache as I was
479 * getting horribly broken behaviour with it on.
480 */
481 return (REG2PHYS(&sc->sc_phys, poff, sc->sc_bustype) | PMAP_NC);
482 }
483
484 static int
485 cgeight_get_video(sc)
486 struct cgeight_softc *sc;
487 {
488
489 return (fb_pfour_get_video(&sc->sc_fb));
490 }
491
492 static void
493 cgeight_set_video(sc, enable)
494 struct cgeight_softc *sc;
495 int enable;
496 {
497
498 fb_pfour_set_video(&sc->sc_fb, enable);
499 }
500