cgfour.c revision 1.8 1 /* $NetBSD: cgfour.c,v 1.8 1996/08/13 20:52:43 thorpej 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 #include <sys/conf.h>
70
71 #include <vm/vm.h>
72
73 #include <machine/fbio.h>
74 #include <machine/autoconf.h>
75 #include <machine/pmap.h>
76 #include <machine/fbvar.h>
77 #include <machine/eeprom.h>
78 #include <machine/conf.h>
79
80 #include <sparc/dev/btreg.h>
81 #include <sparc/dev/btvar.h>
82 #include <sparc/dev/pfourreg.h>
83
84 /* per-display variables */
85 struct cgfour_softc {
86 struct device sc_dev; /* base device */
87 struct fbdevice sc_fb; /* frame buffer device */
88 struct rom_reg sc_phys; /* display RAM (phys addr) */
89 volatile struct fbcontrol *sc_fbc; /* Brooktree registers */
90 int sc_bustype; /* type of bus we live on */
91 union bt_cmap sc_cmap; /* Brooktree color map */
92 };
93
94 /* autoconfiguration driver */
95 static void cgfourattach __P((struct device *, struct device *, void *));
96 static int cgfourmatch __P((struct device *, void *, void *));
97 int cgfouropen __P((dev_t, int, int, struct proc *));
98 int cgfourclose __P((dev_t, int, int, struct proc *));
99 int cgfourioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
100 int cgfourmmap __P((dev_t, int, int));
101 #if defined(SUN4)
102 static void cgfourunblank __P((struct device *));
103 #endif
104
105 struct cfattach cgfour_ca = {
106 sizeof(struct cgfour_softc), cgfourmatch, cgfourattach
107 };
108
109 struct cfdriver cgfour_cd = {
110 NULL, "cgfour", DV_DULL
111 };
112
113 #if defined(SUN4)
114 /* frame buffer generic driver */
115 static struct fbdriver cgfourfbdriver = {
116 cgfourunblank, cgfouropen, cgfourclose, cgfourioctl, cgfourmmap
117 };
118
119 extern int fbnode;
120 extern struct tty *fbconstty;
121
122 static void cgfourloadcmap __P((struct cgfour_softc *, int, int));
123 static int cgfour_get_video __P((struct cgfour_softc *));
124 static void cgfour_set_video __P((struct cgfour_softc *, int));
125 #endif
126
127 /*
128 * Match a cgfour.
129 */
130 int
131 cgfourmatch(parent, vcf, aux)
132 struct device *parent;
133 void *vcf, *aux;
134 {
135 struct cfdata *cf = vcf;
136 struct confargs *ca = aux;
137 struct romaux *ra = &ca->ca_ra;
138
139 if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
140 return (0);
141
142 /*
143 * Mask out invalid flags from the user.
144 */
145 cf->cf_flags &= FB_USERMASK;
146
147 /*
148 * Only exists on a sun4.
149 */
150 if (!CPU_ISSUN4)
151 return (0);
152
153 /*
154 * Only exists on obio.
155 */
156 if (ca->ca_bustype != BUS_OBIO)
157 return (0);
158
159 /*
160 * Make sure there's hardware there.
161 */
162 if (probeget(ra->ra_vaddr, 4) == -1)
163 return (0);
164
165 #if defined(SUN4)
166 /*
167 * Check the pfour register.
168 */
169 if (fb_pfour_id(ra->ra_vaddr) == PFOUR_ID_COLOR8P1) {
170 cf->cf_flags |= FB_PFOUR;
171 return (1);
172 }
173 #endif
174
175 return (0);
176 }
177
178 /*
179 * Attach a display. We need to notice if it is the console, too.
180 */
181 void
182 cgfourattach(parent, self, args)
183 struct device *parent, *self;
184 void *args;
185 {
186 #if defined(SUN4)
187 register struct cgfour_softc *sc = (struct cgfour_softc *)self;
188 register struct confargs *ca = args;
189 register int node = 0, ramsize, i;
190 register volatile struct bt_regs *bt;
191 struct fbdevice *fb = &sc->sc_fb;
192 int isconsole;
193
194 fb->fb_driver = &cgfourfbdriver;
195 fb->fb_device = &sc->sc_dev;
196 fb->fb_type.fb_type = FBTYPE_SUN4COLOR;
197 fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
198
199 /*
200 * Only pfour cgfours, thank you...
201 */
202 if ((ca->ca_bustype != BUS_OBIO) ||
203 ((fb->fb_flags & FB_PFOUR) == 0)) {
204 printf("%s: ignoring; not a pfour\n", sc->sc_dev.dv_xname);
205 return;
206 }
207
208 /* Map the pfour register. */
209 fb->fb_pfour = (volatile u_int32_t *)mapiodev(ca->ca_ra.ra_reg, 0,
210 sizeof(u_int32_t), ca->ca_bustype);
211
212 ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
213
214 fb->fb_type.fb_depth = 8;
215 fb_setsize(fb, fb->fb_type.fb_depth, 1152, 900, node, ca->ca_bustype);
216
217 fb->fb_type.fb_cmsize = 256;
218 fb->fb_type.fb_size = ramsize;
219 printf(": cgfour/p4, %d x %d", fb->fb_type.fb_width,
220 fb->fb_type.fb_height);
221
222 isconsole = 0;
223
224 if (CPU_ISSUN4) {
225 struct eeprom *eep = (struct eeprom *)eeprom_va;
226
227 /*
228 * Assume this is the console if there's no eeprom info
229 * to be found.
230 */
231 if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
232 isconsole = (fbconstty != NULL);
233 }
234
235 #if 0
236 /*
237 * We don't do any of the console handling here. Instead,
238 * we let the bwtwo driver pick up the overlay plane and
239 * use it instead. Rconsole should have better performance
240 * with the 1-bit depth.
241 * -- Jason R. Thorpe <thorpej (at) NetBSD.ORG>
242 */
243
244 /*
245 * When the ROM has mapped in a cgfour display, the address
246 * maps only the video RAM, so in any case we have to map the
247 * registers ourselves. We only need the video RAM if we are
248 * going to print characters via rconsole.
249 */
250
251 if (isconsole) {
252 /* XXX this is kind of a waste */
253 fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
254 PFOUR_COLOR_OFF_OVERLAY, ramsize, ca->ca_bustype);
255 }
256 #endif
257
258 /* Map the Brooktree. */
259 sc->sc_fbc = (volatile struct fbcontrol *)mapiodev(ca->ca_ra.ra_reg,
260 PFOUR_COLOR_OFF_CMAP, sizeof(struct fbcontrol), ca->ca_bustype);
261
262 sc->sc_phys = ca->ca_ra.ra_reg[0];
263 sc->sc_bustype = ca->ca_bustype;
264
265 /* grab initial (current) color map */
266 bt = &sc->sc_fbc->fbc_dac;
267 bt->bt_addr = 0;
268 for (i = 0; i < 256 * 3 / 4; i++)
269 ((char *)&sc->sc_cmap)[i] = bt->bt_cmap >> 24;
270
271 BT_INIT(bt, 24);
272
273 #if 0 /* See above. */
274 if (isconsole) {
275 printf(" (console)\n");
276 #if defined(RASTERCONSOLE) && 0 /* XXX been told it doesn't work well. */
277 fbrcons_init(fb);
278 #endif
279 } else
280 #endif /* 0 */
281 printf("\n");
282
283 /*
284 * Even though we're not using rconsole, we'd still like
285 * to notice if we're the console framebuffer.
286 */
287 fb_attach(fb, isconsole);
288 #endif
289 }
290
291 int
292 cgfouropen(dev, flags, mode, p)
293 dev_t dev;
294 int flags, mode;
295 struct proc *p;
296 {
297 int unit = minor(dev);
298
299 if (unit >= cgfour_cd.cd_ndevs || cgfour_cd.cd_devs[unit] == NULL)
300 return (ENXIO);
301 return (0);
302 }
303
304 int
305 cgfourclose(dev, flags, mode, p)
306 dev_t dev;
307 int flags, mode;
308 struct proc *p;
309 {
310
311 return (0);
312 }
313
314 int
315 cgfourioctl(dev, cmd, data, flags, p)
316 dev_t dev;
317 u_long cmd;
318 register caddr_t data;
319 int flags;
320 struct proc *p;
321 {
322 #if defined(SUN4)
323 register struct cgfour_softc *sc = cgfour_cd.cd_devs[minor(dev)];
324 register struct fbgattr *fba;
325 int error;
326
327 switch (cmd) {
328
329 case FBIOGTYPE:
330 *(struct fbtype *)data = sc->sc_fb.fb_type;
331 break;
332
333 case FBIOGATTR:
334 fba = (struct fbgattr *)data;
335 fba->real_type = sc->sc_fb.fb_type.fb_type;
336 fba->owner = 0; /* XXX ??? */
337 fba->fbtype = sc->sc_fb.fb_type;
338 fba->sattr.flags = 0;
339 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
340 fba->sattr.dev_specific[0] = -1;
341 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
342 fba->emu_types[1] = -1;
343 break;
344
345 case FBIOGETCMAP:
346 return (bt_getcmap((struct fbcmap *)data, &sc->sc_cmap, 256));
347
348 case FBIOPUTCMAP:
349 /* copy to software map */
350 #define p ((struct fbcmap *)data)
351 error = bt_putcmap(p, &sc->sc_cmap, 256);
352 if (error)
353 return (error);
354 /* now blast them into the chip */
355 /* XXX should use retrace interrupt */
356 cgfourloadcmap(sc, p->index, p->count);
357 #undef p
358 break;
359
360 case FBIOGVIDEO:
361 *(int *)data = cgfour_get_video(sc);
362 break;
363
364 case FBIOSVIDEO:
365 cgfour_set_video(sc, *(int *)data);
366 break;
367
368 default:
369 return (ENOTTY);
370 }
371 #endif
372 return (0);
373 }
374
375 /*
376 * Return the address that would map the given device at the given
377 * offset, allowing for the given protection, or return -1 for error.
378 *
379 * the cg4 maps it's overlay plane for 128K, followed by the enable
380 * plane for 128K, followed by the colour plane (for as much colour
381 * as their is.)
382 *
383 * As well, mapping at an offset of 0x04000000 causes the cg4 to map
384 * only it's colour plane, at 0.
385 */
386 int
387 cgfourmmap(dev, off, prot)
388 dev_t dev;
389 int off, prot;
390 {
391 register struct cgfour_softc *sc = cgfour_cd.cd_devs[minor(dev)];
392 int poff;
393
394 #define START_ENABLE (128*1024)
395 #define START_COLOR ((128*1024) + (128*1024))
396 #define COLOR_SIZE (sc->sc_fb.fb_type.fb_width * \
397 sc->sc_fb.fb_type.fb_height)
398 #define END_COLOR (START_COLOR + COLOR_SIZE)
399 #define NOOVERLAY (0x04000000)
400
401 if (off & PGOFSET)
402 panic("cgfourmap");
403
404 if ((u_int)off >= NOOVERLAY) {
405 off -= NOOVERLAY;
406
407 /*
408 * X11 maps a huge chunk of the frame buffer; far more than
409 * there really is. We compensate by double-mapping the
410 * first page for as many other pages as it wants
411 */
412 while (off >= COLOR_SIZE)
413 off -= COLOR_SIZE; /* XXX thorpej ??? */
414
415 poff = off + PFOUR_COLOR_OFF_COLOR;
416 } else if ((u_int)off < START_ENABLE) {
417 /*
418 * in overlay plane
419 */
420 poff = PFOUR_COLOR_OFF_OVERLAY + off;
421 } else if ((u_int)off < START_COLOR) {
422 /*
423 * in enable plane
424 */
425 poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
426 } else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
427 /*
428 * in colour plane
429 */
430 poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
431 } else
432 return (-1);
433
434 return (REG2PHYS(&sc->sc_phys, poff, sc->sc_bustype) | PMAP_NC);
435 }
436
437 #if defined(SUN4)
438 /*
439 * Undo the effect of an FBIOSVIDEO that turns the video off.
440 */
441 static void
442 cgfourunblank(dev)
443 struct device *dev;
444 {
445
446 cgfour_set_video((struct cgfour_softc *)dev, 1);
447 }
448
449 static int
450 cgfour_get_video(sc)
451 struct cgfour_softc *sc;
452 {
453
454 return (fb_pfour_get_video(&sc->sc_fb));
455 }
456
457 static void
458 cgfour_set_video(sc, enable)
459 struct cgfour_softc *sc;
460 int enable;
461 {
462
463 fb_pfour_set_video(&sc->sc_fb, enable);
464 }
465
466 /*
467 * Load a subset of the current (new) colormap into the Brooktree DAC.
468 */
469 static void
470 cgfourloadcmap(sc, start, ncolors)
471 register struct cgfour_softc *sc;
472 register int start, ncolors;
473 {
474 register volatile struct bt_regs *bt;
475 register u_int *ip, i;
476 register int count;
477
478 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */
479 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
480 bt = &sc->sc_fbc->fbc_dac;
481 bt->bt_addr = BT_D4M4(start) << 24;
482 while (--count >= 0) {
483 i = *ip++;
484 /* hardware that makes one want to pound boards with hammers */
485 bt->bt_cmap = i;
486 bt->bt_cmap = i << 8;
487 bt->bt_cmap = i << 16;
488 bt->bt_cmap = i << 24;
489 }
490 }
491 #endif
492