cgfour.c revision 1.27 1 /* $NetBSD: cgfour.c,v 1.27 2002/09/06 13:18:43 gehenna 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. All rights reserved.
41 * Copyright (c) 1992, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by Theo de Raadt.
47 *
48 * This software was developed by the Computer Systems Engineering group
49 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
50 * contributed to Berkeley.
51 *
52 * All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Lawrence Berkeley Laboratory.
56 *
57 * Redistribution and use in source and binary forms, with or without
58 * modification, are permitted provided that the following conditions
59 * are met:
60 * 1. Redistributions of source code must retain the above copyright
61 * notice, this list of conditions and the following disclaimer.
62 * 2. Redistributions in binary form must reproduce the above copyright
63 * notice, this list of conditions and the following disclaimer in the
64 * documentation and/or other materials provided with the distribution.
65 * 3. All advertising materials mentioning features or use of this software
66 * must display the following acknowledgement:
67 * This product includes software developed by the University of
68 * California, Berkeley and its contributors.
69 * 4. Neither the name of the University nor the names of its contributors
70 * may be used to endorse or promote products derived from this software
71 * without specific prior written permission.
72 *
73 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
74 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
77 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * SUCH DAMAGE.
84 *
85 * from @(#)cgthree.c 8.2 (Berkeley) 10/30/93
86 */
87
88 /*
89 * color display (cgfour) driver.
90 *
91 * Does not handle interrupts, even though they can occur.
92 *
93 * XXX should defer colormap updates to vertical retrace interrupts
94 */
95
96 #include <sys/param.h>
97 #include <sys/systm.h>
98 #include <sys/buf.h>
99 #include <sys/device.h>
100 #include <sys/ioctl.h>
101 #include <sys/malloc.h>
102 #include <sys/mman.h>
103 #include <sys/tty.h>
104 #include <sys/conf.h>
105
106 #include <machine/autoconf.h>
107 #include <machine/eeprom.h>
108
109 #include <dev/sun/fbio.h>
110 #include <dev/sun/fbvar.h>
111 #include <dev/sun/btreg.h>
112 #include <dev/sun/btvar.h>
113 #include <dev/sun/pfourreg.h>
114
115 /* per-display variables */
116 struct cgfour_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_addr_t sc_paddr; /* phys address for device mmap() */
121
122 volatile struct fbcontrol *sc_fbc; /* Brooktree registers */
123 union bt_cmap sc_cmap; /* Brooktree color map */
124 };
125
126 /* autoconfiguration driver */
127 static void cgfourattach __P((struct device *, struct device *, void *));
128 static int cgfourmatch __P((struct device *, struct cfdata *, void *));
129
130 #if defined(SUN4)
131 static void cgfourunblank __P((struct device *));
132 #endif
133
134 static int cg4_pfour_probe __P((void *, void *));
135
136 struct cfattach cgfour_ca = {
137 sizeof(struct cgfour_softc), cgfourmatch, cgfourattach
138 };
139
140 extern struct cfdriver cgfour_cd;
141
142 dev_type_open(cgfouropen);
143 dev_type_ioctl(cgfourioctl);
144 dev_type_mmap(cgfourmmap);
145
146 const struct cdevsw cgfour_cdevsw = {
147 cgfouropen, nullclose, noread, nowrite, cgfourioctl,
148 nostop, notty, nopoll, cgfourmmap,
149 };
150
151 #if defined(SUN4)
152 /* frame buffer generic driver */
153 static struct fbdriver cgfourfbdriver = {
154 cgfourunblank, cgfouropen, nullclose, cgfourioctl, nopoll,
155 cgfourmmap
156 };
157
158 static void cgfourloadcmap __P((struct cgfour_softc *, int, int));
159 static int cgfour_get_video __P((struct cgfour_softc *));
160 static void cgfour_set_video __P((struct cgfour_softc *, int));
161 #endif
162
163 /*
164 * Match a cgfour.
165 */
166 int
167 cgfourmatch(parent, cf, aux)
168 struct device *parent;
169 struct cfdata *cf;
170 void *aux;
171 {
172 union obio_attach_args *uoba = aux;
173 struct obio4_attach_args *oba;
174
175 if (uoba->uoba_isobio4 == 0)
176 return (0);
177
178 oba = &uoba->uoba_oba4;
179 return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
180 4, /* probe size */
181 0, /* offset */
182 0, /* flags */
183 cg4_pfour_probe, NULL));
184 }
185
186 int
187 cg4_pfour_probe(vaddr, arg)
188 void *vaddr;
189 void *arg;
190 {
191
192 return (fb_pfour_id(vaddr) == PFOUR_ID_COLOR8P1);
193 }
194
195 /*
196 * Attach a display. We need to notice if it is the console, too.
197 */
198 void
199 cgfourattach(parent, self, aux)
200 struct device *parent, *self;
201 void *aux;
202 {
203 #if defined(SUN4)
204 struct cgfour_softc *sc = (struct cgfour_softc *)self;
205 union obio_attach_args *uoba = aux;
206 struct obio4_attach_args *oba = &uoba->uoba_oba4;
207 bus_space_handle_t bh;
208 volatile struct bt_regs *bt;
209 struct fbdevice *fb = &sc->sc_fb;
210 int ramsize, i, isconsole;
211
212 sc->sc_bustag = oba->oba_bustag;
213 sc->sc_paddr = (bus_addr_t)oba->oba_paddr;
214
215 /* Map the pfour register. */
216 if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
217 sizeof(u_int32_t),
218 BUS_SPACE_MAP_LINEAR,
219 &bh) != 0) {
220 printf("%s: cannot map control registers\n", self->dv_xname);
221 return;
222 }
223 fb->fb_pfour = (volatile u_int32_t *)bh;
224
225 fb->fb_driver = &cgfourfbdriver;
226 fb->fb_device = &sc->sc_dev;
227 fb->fb_type.fb_type = FBTYPE_SUN4COLOR;
228 fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
229 fb->fb_flags |= FB_PFOUR;
230
231 ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
232
233 fb->fb_type.fb_depth = 8;
234 fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
235
236 fb->fb_type.fb_cmsize = 256;
237 fb->fb_type.fb_size = ramsize;
238 printf(": cgfour/p4, %d x %d",
239 fb->fb_type.fb_width, 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 /* grab initial (current) color map */
289 bt = &sc->sc_fbc->fbc_dac;
290 bt->bt_addr = 0;
291 for (i = 0; i < 256 * 3 / 4; i++)
292 ((char *)&sc->sc_cmap)[i] = bt->bt_cmap >> 24;
293
294 BT_INIT(bt, 24);
295
296 #if 0 /* See above. */
297 if (isconsole) {
298 printf(" (console)\n");
299 #if defined(RASTERCONSOLE) && 0 /* XXX been told it doesn't work well. */
300 fbrcons_init(fb);
301 #endif
302 } else
303 #endif /* 0 */
304 printf("\n");
305
306 /*
307 * Even though we're not using rconsole, we'd still like
308 * to notice if we're the console framebuffer.
309 */
310 fb_attach(fb, isconsole);
311 #endif
312 }
313
314 int
315 cgfouropen(dev, flags, mode, p)
316 dev_t dev;
317 int flags, mode;
318 struct proc *p;
319 {
320 int unit = minor(dev);
321
322 if (unit >= cgfour_cd.cd_ndevs || cgfour_cd.cd_devs[unit] == NULL)
323 return (ENXIO);
324 return (0);
325 }
326
327 int
328 cgfourioctl(dev, cmd, data, flags, p)
329 dev_t dev;
330 u_long cmd;
331 caddr_t data;
332 int flags;
333 struct proc *p;
334 {
335 #if defined(SUN4)
336 struct cgfour_softc *sc = cgfour_cd.cd_devs[minor(dev)];
337 struct fbgattr *fba;
338 int error;
339
340 switch (cmd) {
341
342 case FBIOGTYPE:
343 *(struct fbtype *)data = sc->sc_fb.fb_type;
344 break;
345
346 case FBIOGATTR:
347 fba = (struct fbgattr *)data;
348 fba->real_type = sc->sc_fb.fb_type.fb_type;
349 fba->owner = 0; /* XXX ??? */
350 fba->fbtype = sc->sc_fb.fb_type;
351 fba->sattr.flags = 0;
352 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
353 fba->sattr.dev_specific[0] = -1;
354 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
355 fba->emu_types[1] = -1;
356 break;
357
358 case FBIOGETCMAP:
359 #define p ((struct fbcmap *)data)
360 return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
361
362 case FBIOPUTCMAP:
363 /* copy to software map */
364 error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
365 if (error)
366 return (error);
367 /* now blast them into the chip */
368 /* XXX should use retrace interrupt */
369 cgfourloadcmap(sc, p->index, p->count);
370 #undef p
371 break;
372
373 case FBIOGVIDEO:
374 *(int *)data = cgfour_get_video(sc);
375 break;
376
377 case FBIOSVIDEO:
378 cgfour_set_video(sc, *(int *)data);
379 break;
380
381 default:
382 return (ENOTTY);
383 }
384 #endif
385 return (0);
386 }
387
388 /*
389 * Return the address that would map the given device at the given
390 * offset, allowing for the given protection, or return -1 for error.
391 *
392 * the cg4 maps it's overlay plane for 128K, followed by the enable
393 * plane for 128K, followed by the colour plane (for as much colour
394 * as their is.)
395 *
396 * As well, mapping at an offset of 0x04000000 causes the cg4 to map
397 * only it's colour plane, at 0.
398 */
399 paddr_t
400 cgfourmmap(dev, off, prot)
401 dev_t dev;
402 off_t off;
403 int prot;
404 {
405 struct cgfour_softc *sc = cgfour_cd.cd_devs[minor(dev)];
406 off_t poff;
407
408 #define START_ENABLE (128*1024)
409 #define START_COLOR ((128*1024) + (128*1024))
410 #define COLOR_SIZE (sc->sc_fb.fb_type.fb_width * \
411 sc->sc_fb.fb_type.fb_height)
412 #define END_COLOR (START_COLOR + COLOR_SIZE)
413 #define NOOVERLAY (0x04000000)
414
415 if (off & PGOFSET)
416 panic("cgfourmap");
417
418 if (off < 0)
419 return (-1);
420 else if ((u_int)off >= NOOVERLAY) {
421 off -= NOOVERLAY;
422
423 /*
424 * X11 maps a huge chunk of the frame buffer; far more than
425 * there really is. We compensate by double-mapping the
426 * first page for as many other pages as it wants
427 */
428 while ((u_int)off >= COLOR_SIZE)
429 off -= COLOR_SIZE; /* XXX thorpej ??? */
430
431 poff = off + PFOUR_COLOR_OFF_COLOR;
432 } else if ((u_int)off < START_ENABLE) {
433 /*
434 * in overlay plane
435 */
436 poff = PFOUR_COLOR_OFF_OVERLAY + off;
437 } else if ((u_int)off < START_COLOR) {
438 /*
439 * in enable plane
440 */
441 poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
442 } else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
443 /*
444 * in colour plane
445 */
446 poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
447 } else
448 return (-1);
449
450 return (bus_space_mmap(sc->sc_bustag,
451 sc->sc_paddr, poff,
452 prot, BUS_SPACE_MAP_LINEAR));
453 }
454
455 #if defined(SUN4)
456 /*
457 * Undo the effect of an FBIOSVIDEO that turns the video off.
458 */
459 static void
460 cgfourunblank(dev)
461 struct device *dev;
462 {
463
464 cgfour_set_video((struct cgfour_softc *)dev, 1);
465 }
466
467 static int
468 cgfour_get_video(sc)
469 struct cgfour_softc *sc;
470 {
471
472 return (fb_pfour_get_video(&sc->sc_fb));
473 }
474
475 static void
476 cgfour_set_video(sc, enable)
477 struct cgfour_softc *sc;
478 int enable;
479 {
480
481 fb_pfour_set_video(&sc->sc_fb, enable);
482 }
483
484 /*
485 * Load a subset of the current (new) colormap into the Brooktree DAC.
486 */
487 static void
488 cgfourloadcmap(sc, start, ncolors)
489 struct cgfour_softc *sc;
490 int start, ncolors;
491 {
492 volatile struct bt_regs *bt;
493 u_int *ip, i;
494 int count;
495
496 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */
497 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
498 bt = &sc->sc_fbc->fbc_dac;
499 bt->bt_addr = BT_D4M4(start) << 24;
500 while (--count >= 0) {
501 i = *ip++;
502 /* hardware that makes one want to pound boards with hammers */
503 bt->bt_cmap = i;
504 bt->bt_cmap = i << 8;
505 bt->bt_cmap = i << 16;
506 bt->bt_cmap = i << 24;
507 }
508 }
509 #endif
510