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