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