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