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