cgeight.c revision 1.35 1 /* $NetBSD: cgeight.c,v 1.35 2003/07/15 00:04:52 lukem 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/cdefs.h>
93 __KERNEL_RCSID(0, "$NetBSD: cgeight.c,v 1.35 2003/07/15 00:04:52 lukem Exp $");
94
95 #include <sys/param.h>
96 #include <sys/systm.h>
97 #include <sys/buf.h>
98 #include <sys/device.h>
99 #include <sys/ioctl.h>
100 #include <sys/malloc.h>
101 #include <sys/mman.h>
102 #include <sys/tty.h>
103 #include <sys/conf.h>
104
105 #include <uvm/uvm_extern.h>
106
107 #include <machine/autoconf.h>
108 #include <machine/eeprom.h>
109
110 #include <dev/sun/fbio.h>
111 #include <dev/sun/fbvar.h>
112 #include <dev/sun/btreg.h>
113 #include <dev/sun/btvar.h>
114 #include <dev/sun/pfourreg.h>
115
116 /* per-display variables */
117 struct cgeight_softc {
118 struct device sc_dev; /* base device */
119 struct fbdevice sc_fb; /* frame buffer device */
120 bus_space_tag_t sc_bustag;
121 bus_addr_t sc_paddr; /* phys address 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 CFATTACH_DECL(cgeight, sizeof(struct cgeight_softc),
137 cgeightmatch, cgeightattach, NULL, NULL);
138
139 extern struct cfdriver cgeight_cd;
140
141 dev_type_open(cgeightopen);
142 dev_type_ioctl(cgeightioctl);
143 dev_type_mmap(cgeightmmap);
144
145 const struct cdevsw cgeight_cdevsw = {
146 cgeightopen, nullclose, noread, nowrite, cgeightioctl,
147 nostop, notty, nopoll, cgeightmmap, nokqfilter
148 };
149
150 #if defined(SUN4)
151 /* frame buffer generic driver */
152 static struct fbdriver cgeightfbdriver = {
153 cgeightunblank, cgeightopen, nullclose, cgeightioctl,
154 nopoll, cgeightmmap, nokqfilter
155 };
156
157 static void cgeightloadcmap __P((struct cgeight_softc *, int, int));
158 static int cgeight_get_video __P((struct cgeight_softc *));
159 static void cgeight_set_video __P((struct cgeight_softc *, int));
160 #endif
161
162 /*
163 * Match a cgeight.
164 */
165 int
166 cgeightmatch(parent, cf, aux)
167 struct device *parent;
168 struct cfdata *cf;
169 void *aux;
170 {
171 union obio_attach_args *uoba = aux;
172 struct obio4_attach_args *oba;
173
174 if (uoba->uoba_isobio4 == 0)
175 return (0);
176
177 oba = &uoba->uoba_oba4;
178 return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
179 4, /* probe size */
180 0, /* offset */
181 0, /* flags */
182 cg8_pfour_probe, NULL));
183 }
184
185 int
186 cg8_pfour_probe(vaddr, arg)
187 void *vaddr;
188 void *arg;
189 {
190
191 return (fb_pfour_id(vaddr) == PFOUR_ID_COLOR24);
192 }
193
194 /*
195 * Attach a display. We need to notice if it is the console, too.
196 */
197 void
198 cgeightattach(parent, self, aux)
199 struct device *parent, *self;
200 void *aux;
201 {
202 #if defined(SUN4)
203 union obio_attach_args *uoba = aux;
204 struct obio4_attach_args *oba = &uoba->uoba_oba4;
205 struct cgeight_softc *sc = (struct cgeight_softc *)self;
206 struct fbdevice *fb = &sc->sc_fb;
207 bus_space_handle_t bh;
208 volatile struct bt_regs *bt;
209 int ramsize, i, isconsole;
210
211 sc->sc_bustag = oba->oba_bustag;
212 sc->sc_paddr = (bus_addr_t)oba->oba_paddr;
213
214 /* Map the pfour register. */
215 if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
216 sizeof(u_int32_t),
217 BUS_SPACE_MAP_LINEAR,
218 &bh) != 0) {
219 printf("%s: cannot map pfour register\n", self->dv_xname);
220 return;
221 }
222 fb->fb_pfour = (volatile u_int32_t *)bh;
223
224 fb->fb_driver = &cgeightfbdriver;
225 fb->fb_device = &sc->sc_dev;
226 fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
227 fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
228 fb->fb_flags |= FB_PFOUR;
229
230 ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
231
232 fb->fb_type.fb_depth = 24;
233 fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
234
235 sc->sc_fb.fb_type.fb_cmsize = 256;
236 sc->sc_fb.fb_type.fb_size = ramsize;
237 printf(": cgeight/p4, %d x %d",
238 fb->fb_type.fb_width,
239 fb->fb_type.fb_height);
240
241 isconsole = 0;
242
243 if (CPU_ISSUN4) {
244 struct eeprom *eep = (struct eeprom *)eeprom_va;
245
246 /*
247 * Assume this is the console if there's no eeprom info
248 * to be found.
249 */
250 if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
251 isconsole = fb_is_console(0);
252 }
253
254 #if 0
255 /*
256 * We don't do any of the console handling here. Instead,
257 * we let the bwtwo driver pick up the overlay plane and
258 * use it instead. Rconsole should have better performance
259 * with the 1-bit depth.
260 * -- Jason R. Thorpe <thorpej (at) NetBSD.ORG>
261 */
262
263 /*
264 * When the ROM has mapped in a cgfour display, the address
265 * maps only the video RAM, so in any case we have to map the
266 * registers ourselves. We only need the video RAM if we are
267 * going to print characters via rconsole.
268 */
269
270 if (isconsole) {
271 /* XXX this is kind of a waste */
272 fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
273 PFOUR_COLOR_OFF_OVERLAY, ramsize);
274 }
275 #endif
276
277 /* Map the Brooktree. */
278 if (bus_space_map(oba->oba_bustag,
279 oba->oba_paddr + PFOUR_COLOR_OFF_CMAP,
280 sizeof(struct fbcontrol),
281 BUS_SPACE_MAP_LINEAR,
282 &bh) != 0) {
283 printf("%s: cannot map control registers\n", self->dv_xname);
284 return;
285 }
286 sc->sc_fbc = (volatile struct fbcontrol *)bh;
287
288 #if 0 /* XXX thorpej ??? */
289 /* tell the enable plane to look at the mono image */
290 memset(ca->ca_ra.ra_vaddr, 0xff,
291 sc->sc_fb.fb_type.fb_width * sc->sc_fb.fb_type.fb_height / 8);
292 #endif
293
294 /* grab initial (current) color map */
295 bt = &sc->sc_fbc->fbc_dac;
296 bt->bt_addr = 0;
297 for (i = 0; i < 256 * 3 / 4; i++)
298 sc->sc_cmap.cm_chip[i] = bt->bt_cmap;
299
300 BT_INIT(bt, 0);
301
302 #if 0 /* see above */
303 if (isconsole) {
304 printf(" (console)\n");
305 #if defined(RASTERCONSOLE) && 0 /* XXX been told it doesn't work well. */
306 fbrcons_init(fb);
307 #endif
308 } else
309 #endif /* 0 */
310 printf("\n");
311
312 /*
313 * Even though we're not using rconsole, we'd still like
314 * to notice if we're the console framebuffer.
315 */
316 fb_attach(&sc->sc_fb, isconsole);
317 #endif
318 }
319
320 int
321 cgeightopen(dev, flags, mode, p)
322 dev_t dev;
323 int flags, mode;
324 struct proc *p;
325 {
326 int unit = minor(dev);
327
328 if (unit >= cgeight_cd.cd_ndevs || cgeight_cd.cd_devs[unit] == NULL)
329 return (ENXIO);
330 return (0);
331 }
332
333 int
334 cgeightioctl(dev, cmd, data, flags, p)
335 dev_t dev;
336 u_long cmd;
337 caddr_t data;
338 int flags;
339 struct proc *p;
340 {
341 #if defined(SUN4)
342 struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
343 struct fbgattr *fba;
344 int error;
345
346 switch (cmd) {
347
348 case FBIOGTYPE:
349 *(struct fbtype *)data = sc->sc_fb.fb_type;
350 break;
351
352 case FBIOGATTR:
353 fba = (struct fbgattr *)data;
354 fba->real_type = sc->sc_fb.fb_type.fb_type;
355 fba->owner = 0; /* XXX ??? */
356 fba->fbtype = sc->sc_fb.fb_type;
357 fba->sattr.flags = 0;
358 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
359 fba->sattr.dev_specific[0] = -1;
360 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
361 fba->emu_types[1] = -1;
362 break;
363
364 case FBIOGETCMAP:
365 #define p ((struct fbcmap *)data)
366 return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
367
368 case FBIOPUTCMAP:
369 /* copy to software map */
370 error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
371 if (error)
372 return (error);
373 /* now blast them into the chip */
374 /* XXX should use retrace interrupt */
375 cgeightloadcmap(sc, p->index, p->count);
376 #undef p
377 break;
378
379 case FBIOGVIDEO:
380 *(int *)data = cgeight_get_video(sc);
381 break;
382
383 case FBIOSVIDEO:
384 cgeight_set_video(sc, *(int *)data);
385 break;
386
387 default:
388 return (ENOTTY);
389 }
390 #endif
391 return (0);
392 }
393
394 /*
395 * Return the address that would map the given device at the given
396 * offset, allowing for the given protection, or return -1 for error.
397 *
398 * The cg8 maps it's overlay plane at 0 for 128K, followed by the
399 * enable plane for 128K, followed by the colour for as long as it
400 * goes. Starting at 8MB, it maps the ramdac for PAGE_SIZE, then the p4
401 * register for PAGE_SIZE, then the bootrom for 0x40000.
402 */
403 paddr_t
404 cgeightmmap(dev, off, prot)
405 dev_t dev;
406 off_t off;
407 int prot;
408 {
409 struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
410 off_t poff;
411
412 #define START_ENABLE (128*1024)
413 #define START_COLOR ((128*1024) + (128*1024))
414 #define COLOR_SIZE (sc->sc_fb.fb_type.fb_width * \
415 sc->sc_fb.fb_type.fb_height * 3)
416 #define END_COLOR (START_COLOR + COLOR_SIZE)
417 #define START_SPECIAL 0x800000
418 #define PROMSIZE 0x40000
419 #define NOOVERLAY (0x04000000)
420
421 if (off & PGOFSET)
422 panic("cgeightmap");
423
424 if (off < 0)
425 return (-1);
426 else 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 ((u_int)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 < sc->sc_fb.fb_type.fb_size) {
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 + PAGE_SIZE) {
464 /*
465 * p4 register
466 */
467 poff = 0;
468 } else if ((u_int)off > (START_SPECIAL + (PAGE_SIZE * 2)) &&
469 (u_int) off < (START_SPECIAL + (PAGE_SIZE * 2) + PROMSIZE)) {
470 /*
471 * rom
472 */
473 poff = 0x8000 + (off - (START_SPECIAL + (PAGE_SIZE * 2)));
474 } else
475 return (-1);
476
477 return (bus_space_mmap(sc->sc_bustag,
478 sc->sc_paddr, poff,
479 prot, BUS_SPACE_MAP_LINEAR));
480 }
481
482 #if defined(SUN4)
483 /*
484 * Undo the effect of an FBIOSVIDEO that turns the video off.
485 */
486 static void
487 cgeightunblank(dev)
488 struct device *dev;
489 {
490
491 cgeight_set_video((struct cgeight_softc *)dev, 1);
492 }
493
494 static int
495 cgeight_get_video(sc)
496 struct cgeight_softc *sc;
497 {
498
499 return (fb_pfour_get_video(&sc->sc_fb));
500 }
501
502 static void
503 cgeight_set_video(sc, enable)
504 struct cgeight_softc *sc;
505 int enable;
506 {
507
508 fb_pfour_set_video(&sc->sc_fb, enable);
509 }
510
511 /*
512 * Load a subset of the current (new) colormap into the Brooktree DAC.
513 */
514 static void
515 cgeightloadcmap(sc, start, ncolors)
516 register struct cgeight_softc *sc;
517 register int start, ncolors;
518 {
519 volatile struct bt_regs *bt;
520 u_int *ip;
521 int count;
522
523 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */
524 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
525 bt = &sc->sc_fbc->fbc_dac;
526 bt->bt_addr = BT_D4M4(start);
527 while (--count >= 0)
528 bt->bt_cmap = *ip++;
529 }
530 #endif
531