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