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