cg4.c revision 1.1 1 /* $NetBSD: cg4.c,v 1.1 1995/03/10 01:51:03 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/fbio.h>
65 #include <machine/autoconf.h>
66 #include <machine/pmap.h>
67 #include <machine/eeprom.h>
68
69 #include "fbvar.h"
70 #include "btreg.h"
71 #include "btvar.h"
72 #include "cg4reg.h"
73
74 /* per-display variables */
75 struct cg4_softc {
76 struct device sc_dev; /* base device */
77 struct fbdevice sc_fb; /* frame buffer device */
78 volatile struct bt_regs *sc_bt; /* Brooktree registers */
79 int sc_phys; /* display RAM (phys addr) */
80 int sc_blanked; /* true if blanked */
81 union bt_cmap sc_cmap; /* Brooktree color map */
82 };
83
84 /* autoconfiguration driver */
85 static void cg4attach __P((struct device *, struct device *, void *));
86 static int cg4match __P((struct device *, void *, void *));
87
88 struct cfdriver cgfourcd = {
89 NULL, "cgfour", cg4match, cg4attach,
90 DV_DULL, sizeof(struct cg4_softc) };
91
92 /* frame buffer generic driver */
93 int cg4open(), cg4close(), cg4map();
94
95 static int cg4gattr __P((struct fbdevice *, struct fbgattr *));
96 static int cg4gvideo __P((struct fbdevice *, int *));
97 static int cg4svideo __P((struct fbdevice *, int *));
98 static int cg4getcmap __P((struct fbdevice *, struct fbcmap *));
99 static int cg4putcmap __P((struct fbdevice *, struct fbcmap *));
100
101 static struct fbdriver cg4fbdriver = {
102 cg4open, cg4close, cg4map, cg4gattr,
103 cg4gvideo, cg4svideo,
104 cg4getcmap, cg4putcmap };
105
106 static void cg4loadcmap __P((struct cg4_softc *, int, int));
107
108 /*
109 * Match a cg4.
110 */
111 static int
112 cg4match(parent, vcf, args)
113 struct device *parent;
114 void *vcf, *args;
115 {
116 struct confargs *ca = args;
117 int x;
118
119 if (ca->ca_paddr == -1)
120 ca->ca_paddr = 0xFF200000;
121
122 /* The peek returns -1 on bus error. */
123 x = bus_peek(ca->ca_bustype, ca->ca_paddr, 1);
124 return (x != -1);
125 }
126
127 /*
128 * Attach a display. We need to notice if it is the console, too.
129 */
130 static void
131 cg4attach(parent, self, args)
132 struct device *parent, *self;
133 void *args;
134 {
135 struct cg4_softc *sc = (struct cg4_softc *)self;
136 struct fbdevice *fb = &sc->sc_fb;
137 struct confargs *ca = args;
138 struct fbtype *fbt;
139 volatile struct bt_regs *bt;
140 int i, ramsize, pa;
141
142 fb->fb_driver = &cg4fbdriver;
143 fb->fb_private = sc;
144 fb->fb_name = sc->sc_dev.dv_xname;
145
146 fbt = &fb->fb_fbtype;
147 fbt->fb_type = FBTYPE_SUN4COLOR;
148 fbt->fb_depth = 8;
149 fbt->fb_cmsize = 256;
150
151 fbt->fb_width = 1152;
152 fbt->fb_height = 900;
153 fbt->fb_size = CG4_FBSIZE;
154
155 sc->sc_phys = ca->ca_paddr;
156 sc->sc_bt = (struct bt_regs *)
157 bus_mapin(ca->ca_bustype, ca->ca_paddr,
158 sizeof(struct bt_regs *));
159
160 /* grab initial (current) color map */
161 bt->bt_addr = 0;
162 for (i = 0; i < (256 * 3 / 4); i++)
163 sc->sc_cmap.cm_chip[i] = bt->bt_cmap;
164
165 /*
166 * BT458 chip initialization as described in Brooktree's
167 * 1993 Graphics and Imaging Product Databook (DB004-1/93).
168 */
169 bt->bt_addr = 0x04; /* select read mask register */
170 bt->bt_ctrl = 0xff; /* all planes on */
171 bt->bt_addr = 0x05; /* select blink mask register */
172 bt->bt_ctrl = 0x00; /* all planes non-blinking */
173 bt->bt_addr = 0x06; /* select command register */
174 bt->bt_ctrl = 0x43; /* palette enabled, overlay planes enabled */
175 bt->bt_addr = 0x07; /* select test register */
176 bt->bt_ctrl = 0x00; /* set test mode */
177
178 printf(" (%dx%d)\n", fbt->fb_width, fbt->fb_height);
179 /* Is the EEPROM console set for the cg4? */
180 if ((ee_console == EE_CONS_COLOR) ||
181 (ee_console == EE_CONS_P4OPT) )
182 {
183 fb_attach(fb);
184 }
185 }
186
187 int
188 cg4open(dev, flags, mode, p)
189 dev_t dev;
190 int flags, mode;
191 struct proc *p;
192 {
193 int unit = minor(dev);
194
195 if (unit >= cgfourcd.cd_ndevs || cgfourcd.cd_devs[unit] == NULL)
196 return (ENXIO);
197 return (0);
198 }
199
200 int
201 cg4close(dev, flags, mode, p)
202 dev_t dev;
203 int flags, mode;
204 struct proc *p;
205 {
206
207 return (0);
208 }
209
210 int
211 cg4ioctl(dev, cmd, data, flags, p)
212 dev_t dev;
213 u_long cmd;
214 caddr_t data;
215 int flags;
216 struct proc *p;
217 {
218 struct cg4_softc *sc = cgfourcd.cd_devs[minor(dev)];
219
220 return (fbioctlfb(&sc->sc_fb, cmd, data));
221 }
222
223 /*
224 * Return the address that would map the given device at the given
225 * offset, allowing for the given protection, or return -1 for error.
226 *
227 * X11 expects its mmap'd region to look like this:
228 * 128k overlay memory
229 * 128k overlay-enable bitmap
230 * 1024k color memory
231 *
232 * The hardware really looks like this (starting at ca_paddr)
233 * 4 bytes Brooktree DAC registers
234 * 2MB-4 gap
235 * 128k overlay memory
236 * 1920k gap
237 * 128k overlay-enable bitmap
238 * 1920k gap
239 * 1024k color memory
240 */
241 int
242 cg4map(dev, off, prot)
243 dev_t dev;
244 int off, prot;
245 {
246 struct cg4_softc *sc = cgfourcd.cd_devs[minor(dev)];
247 int realoff;
248
249 if (off & PGOFSET)
250 panic("cg4map");
251
252 realoff = off + CG4REG_OVERLAY;
253 if (off >= 0x20000) {
254 off -= 0x20000;
255 realoff += 0x1e0000;
256 }
257 if (off >= 0x20000) {
258 off -= 0x20000;
259 realoff += 0x1e0000;
260 }
261
262 if ((unsigned)off >= CG4_FBSIZE)
263 return (-1);
264
265 /*
266 * I turned on PMAP_NC here to disable the cache as I was
267 * getting horribly broken behaviour with it on.
268 */
269 return ((sc->sc_phys + realoff) | PMAP_NC);
270 }
271
272 /*
273 * Internal ioctl functions.
274 */
275
276 /* FBIOGATTR: */
277 static int cg4gattr(fb, fba)
278 struct fbdevice *fb;
279 struct fbgattr *fba;
280 {
281
282 fba->real_type = fb->fb_fbtype.fb_type;
283 fba->owner = 0; /* XXX - TIOCCONS stuff? */
284 fba->fbtype = fb->fb_fbtype;
285 fba->sattr.flags = 0;
286 fba->sattr.emu_type = fb->fb_fbtype.fb_type;
287 fba->sattr.dev_specific[0] = -1;
288 fba->emu_types[0] = fb->fb_fbtype.fb_type;
289 fba->emu_types[1] = -1;
290 return (0);
291 }
292
293 /* FBIOGVIDEO: */
294 static int cg4gvideo(fb, on)
295 struct fbdevice *fb;
296 int *on;
297 {
298 struct cg4_softc *sc = fb->fb_private;
299
300 *on = !sc->sc_blanked;
301 return (0);
302 }
303
304 /* FBIOSVIDEO: */
305 static int cg4svideo(fb, on)
306 struct fbdevice *fb;
307 int *on;
308 {
309 struct cg4_softc *sc = fb->fb_private;
310 register volatile struct bt_regs *bt = sc->sc_bt;
311
312 if ((*on == 0) && (sc->sc_blanked == 0)) {
313 /* Turn OFF video (blank it). */
314 bt->bt_addr = 0x06; /* command reg */
315 bt->bt_ctrl = 0x70; /* overlay plane */
316 bt->bt_addr = 0x04; /* read mask */
317 bt->bt_ctrl = 0x00; /* color planes */
318 /*
319 * Set color 0 to black -- note that this overwrites
320 * R of color 1.
321 */
322 bt->bt_addr = 0;
323 bt->bt_cmap = 0;
324
325 sc->sc_blanked = 1;
326 }
327
328 if ((*on != 0) && (sc->sc_blanked != 0)) {
329 /* Turn video back ON (unblank). */
330 sc->sc_blanked = 0;
331
332 /* restore color 0 (and R of color 1) */
333 bt->bt_addr = 0;
334 bt->bt_cmap = sc->sc_cmap.cm_chip[0];
335
336 /* restore read mask */
337 bt->bt_addr = 0x06; /* command reg */
338 bt->bt_ctrl = 0x73; /* overlay plane */
339 bt->bt_addr = 0x04; /* read mask */
340 bt->bt_ctrl = 0xff; /* color planes */
341 }
342 return (0);
343 }
344
345 /* FBIOGETCMAP: */
346 static int cg4getcmap(fb, cmap)
347 struct fbdevice *fb;
348 struct fbcmap *cmap;
349 {
350 struct cg4_softc *sc = fb->fb_private;
351
352 return (bt_getcmap(cmap, &sc->sc_cmap, 256));
353 }
354
355 /* FBIOPUTCMAP: */
356 static int cg4putcmap(fb, cmap)
357 struct fbdevice *fb;
358 struct fbcmap *cmap;
359 {
360 struct cg4_softc *sc = fb->fb_private;
361 int error;
362
363 /* copy to software map */
364 error = bt_putcmap(cmap, &sc->sc_cmap, 256);
365 if (error == 0) {
366 /* now blast them into the chip */
367 /* XXX should use retrace interrupt */
368 cg4loadcmap(sc, cmap->index, cmap->count);
369 }
370 return (error);
371 }
372
373 /*
374 * Load a subset of the current (new) colormap into the Brooktree DAC.
375 */
376 static void
377 cg4loadcmap(sc, start, ncolors)
378 struct cg4_softc *sc;
379 int start, ncolors;
380 {
381 volatile struct bt_regs *bt;
382 u_int *ip;
383 int count;
384
385 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */
386 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
387 bt = sc->sc_bt;
388 bt->bt_addr = BT_D4M4(start);
389 while (--count >= 0)
390 bt->bt_cmap = *ip++;
391 }
392