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