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