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