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