cg4.c revision 1.2 1 /* $NetBSD: cg4.c,v 1.2 1995/04/07 02:43:25 gwr Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * from: @(#)cgthree.c 8.2 (Berkeley) 10/30/93
45 */
46
47 /*
48 * color display (cg4) driver.
49 *
50 * Does not handle interrupts, even though they can occur.
51 *
52 * XXX should defer colormap updates to vertical retrace interrupts
53 */
54
55 #include <sys/param.h>
56 #include <sys/device.h>
57 #include <sys/ioctl.h>
58 #include <sys/malloc.h>
59 #include <sys/mman.h>
60 #include <sys/tty.h>
61
62 #include <vm/vm.h>
63
64 #include <machine/cpu.h>
65 #include <machine/fbio.h>
66 #include <machine/autoconf.h>
67 #include <machine/pmap.h>
68
69 #include "fbvar.h"
70 #include "btreg.h"
71 #include "btvar.h"
72 #include "cg4reg.h"
73
74 extern unsigned char cpu_machine_id;
75
76 /* per-display variables */
77 struct cg4_softc {
78 struct device sc_dev; /* base device */
79 struct fbdevice sc_fb; /* frame buffer device */
80 volatile struct bt_regs *sc_bt; /* Brooktree registers */
81 int sc_phys; /* display RAM (phys addr) */
82 int sc_blanked; /* true if blanked */
83 union bt_cmap sc_cmap; /* Brooktree color map */
84 };
85
86 /* autoconfiguration driver */
87 static void cg4attach __P((struct device *, struct device *, void *));
88 static int cg4match __P((struct device *, void *, void *));
89
90 struct cfdriver cgfourcd = {
91 NULL, "cgfour", cg4match, cg4attach,
92 DV_DULL, sizeof(struct cg4_softc) };
93
94 /* frame buffer generic driver */
95 int cg4open(), cg4close(), cg4map();
96
97 static int cg4gattr __P((struct fbdevice *, struct fbgattr *));
98 static int cg4gvideo __P((struct fbdevice *, int *));
99 static int cg4svideo __P((struct fbdevice *, int *));
100 static int cg4getcmap __P((struct fbdevice *, struct fbcmap *));
101 static int cg4putcmap __P((struct fbdevice *, struct fbcmap *));
102
103 static struct fbdriver cg4fbdriver = {
104 cg4open, cg4close, cg4map, cg4gattr,
105 cg4gvideo, cg4svideo,
106 cg4getcmap, cg4putcmap };
107
108 static void cg4loadcmap __P((struct cg4_softc *, int, int));
109
110 /*
111 * Match a cg4.
112 */
113 static int
114 cg4match(parent, vcf, args)
115 struct device *parent;
116 void *vcf, *args;
117 {
118 struct confargs *ca = args;
119 int paddr, x;
120
121 /* XXX - Huge hack due to lack of probe info... */
122 switch (cpu_machine_id) {
123 /* Machines that might have a cg4 (gag). */
124 case SUN3_MACH_50:
125 case SUN3_MACH_60:
126 case SUN3_MACH_110:
127 break;
128 default:
129 return (0);
130 }
131
132 if (ca->ca_paddr == -1)
133 ca->ca_paddr = 0xFF200000;
134
135 paddr = ca->ca_paddr + CG4REG_PIXMAP + CG4_PIXMAP_SIZE - 8;
136 x = bus_peek(ca->ca_bustype, paddr, 1);
137 return (x != -1);
138 }
139
140 /*
141 * Attach a display. We need to notice if it is the console, too.
142 */
143 static void
144 cg4attach(parent, self, args)
145 struct device *parent, *self;
146 void *args;
147 {
148 struct cg4_softc *sc = (struct cg4_softc *)self;
149 struct fbdevice *fb = &sc->sc_fb;
150 struct confargs *ca = args;
151 struct fbtype *fbt;
152 volatile struct bt_regs *bt;
153 int i, ramsize, pa;
154
155 fb->fb_driver = &cg4fbdriver;
156 fb->fb_private = sc;
157 fb->fb_name = sc->sc_dev.dv_xname;
158
159 fbt = &fb->fb_fbtype;
160 fbt->fb_type = FBTYPE_SUN4COLOR;
161 fbt->fb_depth = 8;
162 fbt->fb_cmsize = 256;
163
164 fbt->fb_width = 1152;
165 fbt->fb_height = 900;
166 fbt->fb_size = CG4_MMAP_SIZE;
167
168 sc->sc_phys = ca->ca_paddr;
169 sc->sc_bt = (struct bt_regs *)
170 bus_mapin(ca->ca_bustype, ca->ca_paddr,
171 sizeof(struct bt_regs *));
172
173 /* grab initial (current) color map */
174 bt->bt_addr = 0;
175 for (i = 0; i < (256 * 3 / 4); i++)
176 sc->sc_cmap.cm_chip[i] = bt->bt_cmap;
177
178 /*
179 * BT458 chip initialization as described in Brooktree's
180 * 1993 Graphics and Imaging Product Databook (DB004-1/93).
181 */
182 bt->bt_addr = 0x04; /* select read mask register */
183 bt->bt_ctrl = 0xff; /* all planes on */
184 bt->bt_addr = 0x05; /* select blink mask register */
185 bt->bt_ctrl = 0x00; /* all planes non-blinking */
186 bt->bt_addr = 0x06; /* select command register */
187 bt->bt_ctrl = 0x43; /* palette enabled, overlay planes enabled */
188 bt->bt_addr = 0x07; /* select test register */
189 bt->bt_ctrl = 0x00; /* set test mode */
190
191 printf(" (%dx%d)\n", fbt->fb_width, fbt->fb_height);
192 fb_attach(fb, 4);
193 }
194
195 int
196 cg4open(dev, flags, mode, p)
197 dev_t dev;
198 int flags, mode;
199 struct proc *p;
200 {
201 int unit = minor(dev);
202
203 if (unit >= cgfourcd.cd_ndevs || cgfourcd.cd_devs[unit] == NULL)
204 return (ENXIO);
205 return (0);
206 }
207
208 int
209 cg4close(dev, flags, mode, p)
210 dev_t dev;
211 int flags, mode;
212 struct proc *p;
213 {
214
215 return (0);
216 }
217
218 int
219 cg4ioctl(dev, cmd, data, flags, p)
220 dev_t dev;
221 u_long cmd;
222 caddr_t data;
223 int flags;
224 struct proc *p;
225 {
226 struct cg4_softc *sc = cgfourcd.cd_devs[minor(dev)];
227
228 return (fbioctlfb(&sc->sc_fb, cmd, data));
229 }
230
231 /*
232 * Return the address that would map the given device at the given
233 * offset, allowing for the given protection, or return -1 for error.
234 *
235 * X11 expects its mmap'd region to look like this:
236 * 128k overlay memory
237 * 128k overlay-enable bitmap
238 * 1024k color memory
239 *
240 * The hardware really looks like this (starting at ca_paddr)
241 * 4 bytes Brooktree DAC registers
242 * 2MB-4 gap
243 * 128k overlay memory
244 * 1920k gap
245 * 128k overlay-enable bitmap
246 * 1920k gap
247 * 1024k color memory
248 */
249 int
250 cg4map(dev, off, prot)
251 dev_t dev;
252 register int off;
253 int prot;
254 {
255 struct cg4_softc *sc = cgfourcd.cd_devs[minor(dev)];
256 register int physbase;
257
258 if (off & PGOFSET)
259 panic("cg4map");
260
261 if ((unsigned)off >= CG4_MMAP_SIZE)
262 return (-1);
263
264 physbase = sc->sc_phys;
265 if (off < 0x40000) {
266 if (off < 0x20000) {
267 /* overlay plane */
268 physbase += CG4REG_OVERLAY;
269 } else {
270 /* enable plane */
271 off -= 0x20000;
272 physbase += CG4REG_ENABLE;
273 }
274 } else {
275 /* pixel map */
276 off -= 0x40000;
277 physbase += CG4REG_PIXMAP;
278 }
279
280 /*
281 * I turned on PMAP_NC here to disable the cache as I was
282 * getting horribly broken behaviour with it on.
283 */
284 return ((physbase + off) | PMAP_NC);
285 }
286
287 /*
288 * Internal ioctl functions.
289 */
290
291 /* FBIOGATTR: */
292 static int cg4gattr(fb, fba)
293 struct fbdevice *fb;
294 struct fbgattr *fba;
295 {
296
297 fba->real_type = fb->fb_fbtype.fb_type;
298 fba->owner = 0; /* XXX - TIOCCONS stuff? */
299 fba->fbtype = fb->fb_fbtype;
300 fba->sattr.flags = 0;
301 fba->sattr.emu_type = fb->fb_fbtype.fb_type;
302 fba->sattr.dev_specific[0] = -1;
303 fba->emu_types[0] = fb->fb_fbtype.fb_type;
304 fba->emu_types[1] = -1;
305 return (0);
306 }
307
308 /* FBIOGVIDEO: */
309 static int cg4gvideo(fb, on)
310 struct fbdevice *fb;
311 int *on;
312 {
313 struct cg4_softc *sc = fb->fb_private;
314
315 *on = !sc->sc_blanked;
316 return (0);
317 }
318
319 /* FBIOSVIDEO: */
320 static int cg4svideo(fb, on)
321 struct fbdevice *fb;
322 int *on;
323 {
324 struct cg4_softc *sc = fb->fb_private;
325 register volatile struct bt_regs *bt = sc->sc_bt;
326
327 if ((*on == 0) && (sc->sc_blanked == 0)) {
328 /* Turn OFF video (blank it). */
329 bt->bt_addr = 0x06; /* command reg */
330 bt->bt_ctrl = 0x70; /* overlay plane */
331 bt->bt_addr = 0x04; /* read mask */
332 bt->bt_ctrl = 0x00; /* color planes */
333 /*
334 * Set color 0 to black -- note that this overwrites
335 * R of color 1.
336 */
337 bt->bt_addr = 0;
338 bt->bt_cmap = 0;
339
340 sc->sc_blanked = 1;
341 }
342
343 if ((*on != 0) && (sc->sc_blanked != 0)) {
344 /* Turn video back ON (unblank). */
345 sc->sc_blanked = 0;
346
347 /* restore color 0 (and R of color 1) */
348 bt->bt_addr = 0;
349 bt->bt_cmap = sc->sc_cmap.cm_chip[0];
350
351 /* restore read mask */
352 bt->bt_addr = 0x06; /* command reg */
353 bt->bt_ctrl = 0x73; /* overlay plane */
354 bt->bt_addr = 0x04; /* read mask */
355 bt->bt_ctrl = 0xff; /* color planes */
356 }
357 return (0);
358 }
359
360 /* FBIOGETCMAP: */
361 static int cg4getcmap(fb, cmap)
362 struct fbdevice *fb;
363 struct fbcmap *cmap;
364 {
365 struct cg4_softc *sc = fb->fb_private;
366
367 return (bt_getcmap(cmap, &sc->sc_cmap, 256));
368 }
369
370 /* FBIOPUTCMAP: */
371 static int cg4putcmap(fb, cmap)
372 struct fbdevice *fb;
373 struct fbcmap *cmap;
374 {
375 struct cg4_softc *sc = fb->fb_private;
376 int error;
377
378 /* copy to software map */
379 error = bt_putcmap(cmap, &sc->sc_cmap, 256);
380 if (error == 0) {
381 /* now blast them into the chip */
382 /* XXX should use retrace interrupt */
383 cg4loadcmap(sc, cmap->index, cmap->count);
384 }
385 return (error);
386 }
387
388 /*
389 * Load a subset of the current (new) colormap into the Brooktree DAC.
390 */
391 static void
392 cg4loadcmap(sc, start, ncolors)
393 struct cg4_softc *sc;
394 int start, ncolors;
395 {
396 volatile struct bt_regs *bt;
397 u_int *ip;
398 int count;
399
400 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */
401 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
402 bt = sc->sc_bt;
403 bt->bt_addr = BT_D4M4(start);
404 while (--count >= 0)
405 bt->bt_cmap = *ip++;
406 }
407