cg4.c revision 1.16 1 1.16 gwr /* $NetBSD: cg4.c,v 1.16 1998/03/08 18:53:17 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.11 gwr * Credits, history:
51 1.11 gwr * Gordon Ross created this driver based on the cg3 driver from
52 1.11 gwr * the sparc port as distributed in BSD 4.4 Lite, but included
53 1.11 gwr * support for only the "type B" adapter (Brooktree DACs).
54 1.11 gwr * Ezra Story added support for the "type A" (AMD DACs).
55 1.1 gwr *
56 1.11 gwr * Todo:
57 1.11 gwr * Make this driver handle video interrupts.
58 1.11 gwr * Defer colormap updates to vertical retrace interrupts.
59 1.1 gwr */
60 1.1 gwr
61 1.1 gwr #include <sys/param.h>
62 1.11 gwr #include <sys/systm.h>
63 1.12 gwr #include <sys/conf.h>
64 1.1 gwr #include <sys/device.h>
65 1.1 gwr #include <sys/ioctl.h>
66 1.1 gwr #include <sys/malloc.h>
67 1.1 gwr #include <sys/mman.h>
68 1.12 gwr #include <sys/proc.h>
69 1.1 gwr #include <sys/tty.h>
70 1.1 gwr
71 1.1 gwr #include <vm/vm.h>
72 1.1 gwr
73 1.12 gwr #include <machine/autoconf.h>
74 1.2 gwr #include <machine/cpu.h>
75 1.1 gwr #include <machine/fbio.h>
76 1.12 gwr #include <machine/idprom.h>
77 1.1 gwr #include <machine/pmap.h>
78 1.1 gwr
79 1.15 gwr #include <sun3/dev/fbvar.h>
80 1.15 gwr #include <sun3/dev/btreg.h>
81 1.15 gwr #include <sun3/dev/btvar.h>
82 1.15 gwr #include <sun3/dev/cg4reg.h>
83 1.15 gwr #include <sun3/dev/p4reg.h>
84 1.15 gwr
85 1.15 gwr #define CG4_TYPE_A 0 /* AMD DACs */
86 1.15 gwr #define CG4_TYPE_B 1 /* Brooktree DACs */
87 1.1 gwr
88 1.12 gwr cdev_decl(cg4);
89 1.12 gwr
90 1.11 gwr #define CG4_MMAP_SIZE (CG4_OVERLAY_SIZE + CG4_ENABLE_SIZE + CG4_PIXMAP_SIZE)
91 1.11 gwr
92 1.11 gwr #define CMAP_SIZE 256
93 1.11 gwr struct soft_cmap {
94 1.11 gwr u_char r[CMAP_SIZE];
95 1.11 gwr u_char g[CMAP_SIZE];
96 1.11 gwr u_char b[CMAP_SIZE];
97 1.11 gwr };
98 1.11 gwr
99 1.1 gwr /* per-display variables */
100 1.1 gwr struct cg4_softc {
101 1.1 gwr struct device sc_dev; /* base device */
102 1.1 gwr struct fbdevice sc_fb; /* frame buffer device */
103 1.11 gwr int sc_cg4type; /* A or B */
104 1.11 gwr int sc_pa_overlay; /* phys. addr. of overlay plane */
105 1.11 gwr int sc_pa_enable; /* phys. addr. of enable plane */
106 1.11 gwr int sc_pa_pixmap; /* phys. addr. of color plane */
107 1.14 gwr int sc_video_on; /* zero if blanked */
108 1.14 gwr void *sc_va_cmap; /* Colormap h/w (mapped KVA) */
109 1.14 gwr void *sc_btcm; /* Soft cmap, Brooktree format */
110 1.14 gwr struct soft_cmap sc_cmap; /* Soft cmap, user format */
111 1.15 gwr void (*sc_ldcmap) __P((struct cg4_softc *));
112 1.1 gwr };
113 1.1 gwr
114 1.1 gwr /* autoconfiguration driver */
115 1.1 gwr static void cg4attach __P((struct device *, struct device *, void *));
116 1.12 gwr static int cg4match __P((struct device *, struct cfdata *, void *));
117 1.1 gwr
118 1.7 thorpej struct cfattach cgfour_ca = {
119 1.7 thorpej sizeof(struct cg4_softc), cg4match, cg4attach
120 1.7 thorpej };
121 1.7 thorpej
122 1.13 thorpej extern struct cfdriver cgfour_cd;
123 1.1 gwr
124 1.12 gwr static int cg4gattr __P((struct fbdevice *, void *));
125 1.12 gwr static int cg4gvideo __P((struct fbdevice *, void *));
126 1.12 gwr static int cg4svideo __P((struct fbdevice *, void *));
127 1.12 gwr static int cg4getcmap __P((struct fbdevice *, void *));
128 1.12 gwr static int cg4putcmap __P((struct fbdevice *, void *));
129 1.1 gwr
130 1.11 gwr static void cg4a_init __P((struct cg4_softc *));
131 1.11 gwr static void cg4a_ldcmap __P((struct cg4_softc *));
132 1.11 gwr
133 1.11 gwr static void cg4b_init __P((struct cg4_softc *));
134 1.11 gwr static void cg4b_ldcmap __P((struct cg4_softc *));
135 1.11 gwr
136 1.11 gwr static struct fbdriver cg4_fbdriver = {
137 1.6 gwr cg4open, cg4close, cg4mmap, cg4gattr,
138 1.1 gwr cg4gvideo, cg4svideo,
139 1.1 gwr cg4getcmap, cg4putcmap };
140 1.1 gwr
141 1.1 gwr /*
142 1.1 gwr * Match a cg4.
143 1.1 gwr */
144 1.1 gwr static int
145 1.12 gwr cg4match(parent, cf, args)
146 1.1 gwr struct device *parent;
147 1.12 gwr struct cfdata *cf;
148 1.12 gwr void *args;
149 1.1 gwr {
150 1.1 gwr struct confargs *ca = args;
151 1.15 gwr int mid, p4id, peekval;
152 1.15 gwr void *p4reg;
153 1.15 gwr
154 1.15 gwr /* No default address support. */
155 1.15 gwr if (ca->ca_paddr == -1)
156 1.15 gwr return (0);
157 1.15 gwr
158 1.15 gwr /*
159 1.15 gwr * Slight hack here: The low four bits of the
160 1.15 gwr * config flags, if set, restrict the match to
161 1.15 gwr * that machine "implementation" only.
162 1.15 gwr */
163 1.15 gwr mid = cf->cf_flags & IDM_IMPL_MASK;
164 1.15 gwr if (mid && (mid != (cpu_machine_id & IDM_IMPL_MASK)))
165 1.15 gwr return (0);
166 1.2 gwr
167 1.15 gwr /*
168 1.15 gwr * Make sure something is there, and if so,
169 1.15 gwr * see if it looks like a P4 register.
170 1.15 gwr */
171 1.15 gwr p4reg = bus_tmapin(ca->ca_bustype, ca->ca_paddr);
172 1.15 gwr peekval = peek_long(p4reg);
173 1.15 gwr p4id = (peekval == -1) ?
174 1.15 gwr P4_NOTFOUND : fb_pfour_id(p4reg);
175 1.15 gwr bus_tmapout(p4reg);
176 1.15 gwr if (peekval == -1)
177 1.15 gwr return (0);
178 1.15 gwr
179 1.15 gwr /*
180 1.15 gwr * The config flag 0x10 if set means we are
181 1.15 gwr * looking for a Type A (3/110) which has
182 1.15 gwr * AMD RAMDACs in control space.
183 1.15 gwr */
184 1.15 gwr #ifdef _SUN3_
185 1.15 gwr if (cf->cf_flags & 0x10) {
186 1.11 gwr if (bus_peek(BUS_OBIO, CG4A_OBIO_CMAP, 1) == -1)
187 1.11 gwr return (0);
188 1.15 gwr /* OK, assume it really is a Type A. */
189 1.15 gwr return (1);
190 1.15 gwr }
191 1.15 gwr #endif /* SUN3 */
192 1.11 gwr
193 1.15 gwr /*
194 1.15 gwr * OK, we are expecting a "Type B" CG4, and
195 1.15 gwr * there may or may not be a P4 register.
196 1.15 gwr */
197 1.15 gwr switch (p4id) {
198 1.15 gwr case P4_ID_COLOR8P1:
199 1.15 gwr case P4_NOTFOUND:
200 1.15 gwr return (1);
201 1.2 gwr default:
202 1.15 gwr #ifdef DEBUG
203 1.15 gwr printf("cgfour at 0x%x match p4id=0x%x fails\n",
204 1.15 gwr ca->ca_paddr, p4id & 0xFF);
205 1.15 gwr #endif
206 1.2 gwr }
207 1.1 gwr
208 1.15 gwr return (0);
209 1.1 gwr }
210 1.1 gwr
211 1.1 gwr /*
212 1.1 gwr * Attach a display. We need to notice if it is the console, too.
213 1.1 gwr */
214 1.1 gwr static void
215 1.1 gwr cg4attach(parent, self, args)
216 1.1 gwr struct device *parent, *self;
217 1.1 gwr void *args;
218 1.1 gwr {
219 1.1 gwr struct cg4_softc *sc = (struct cg4_softc *)self;
220 1.1 gwr struct fbdevice *fb = &sc->sc_fb;
221 1.1 gwr struct confargs *ca = args;
222 1.1 gwr struct fbtype *fbt;
223 1.15 gwr void *p4reg;
224 1.15 gwr int p4id, tmp;
225 1.1 gwr
226 1.1 gwr fbt = &fb->fb_fbtype;
227 1.1 gwr fbt->fb_type = FBTYPE_SUN4COLOR;
228 1.15 gwr fbt->fb_width = 1152; /* default - see below */
229 1.15 gwr fbt->fb_height = 900; /* default - see below */
230 1.1 gwr fbt->fb_depth = 8;
231 1.1 gwr fbt->fb_cmsize = 256;
232 1.15 gwr fbt->fb_size = CG4_MMAP_SIZE;
233 1.15 gwr fb->fb_driver = &cg4_fbdriver;
234 1.15 gwr fb->fb_private = sc;
235 1.15 gwr fb->fb_name = sc->sc_dev.dv_xname;
236 1.15 gwr fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
237 1.1 gwr
238 1.15 gwr p4reg = NULL;
239 1.1 gwr
240 1.15 gwr /*
241 1.15 gwr * The config flag 0x10 if set means we are
242 1.15 gwr * attaching a Type A (3/110) which has the
243 1.15 gwr * AMD RAMDACs in control space, and no P4.
244 1.15 gwr */
245 1.15 gwr if (fb->fb_flags & 0x10) {
246 1.15 gwr #ifdef _SUN3_
247 1.15 gwr sc->sc_cg4type = CG4_TYPE_A;
248 1.15 gwr sc->sc_ldcmap = cg4a_ldcmap;
249 1.11 gwr sc->sc_pa_overlay = ca->ca_paddr + CG4A_OFF_OVERLAY;
250 1.11 gwr sc->sc_pa_enable = ca->ca_paddr + CG4A_OFF_ENABLE;
251 1.11 gwr sc->sc_pa_pixmap = ca->ca_paddr + CG4A_OFF_PIXMAP;
252 1.16 gwr sc->sc_va_cmap = bus_mapin(BUS_OBIO, CG4A_OBIO_CMAP,
253 1.16 gwr sizeof(struct amd_regs));
254 1.11 gwr cg4a_init(sc);
255 1.15 gwr #else /* SUN3 */
256 1.15 gwr panic("cgfour flags 0x10");
257 1.15 gwr #endif /* SUN3 */
258 1.15 gwr } else {
259 1.15 gwr sc->sc_cg4type = CG4_TYPE_B;
260 1.15 gwr sc->sc_ldcmap = cg4b_ldcmap;
261 1.11 gwr sc->sc_pa_overlay = ca->ca_paddr + CG4B_OFF_OVERLAY;
262 1.11 gwr sc->sc_pa_enable = ca->ca_paddr + CG4B_OFF_ENABLE;
263 1.11 gwr sc->sc_pa_pixmap = ca->ca_paddr + CG4B_OFF_PIXMAP;
264 1.16 gwr tmp = ca->ca_paddr + CG4B_OFF_CMAP,
265 1.16 gwr sc->sc_va_cmap = bus_mapin(ca->ca_bustype, tmp,
266 1.16 gwr sizeof(struct bt_regs));
267 1.11 gwr cg4b_init(sc);
268 1.15 gwr
269 1.15 gwr /* Does it have a P4 register? */
270 1.15 gwr p4reg = bus_mapin(ca->ca_bustype, ca->ca_paddr, 4);
271 1.15 gwr p4id = fb_pfour_id(p4reg);
272 1.15 gwr if (p4id != P4_NOTFOUND)
273 1.15 gwr fb->fb_pfour = p4reg;
274 1.15 gwr else
275 1.15 gwr bus_mapout(p4reg, 4);
276 1.11 gwr }
277 1.1 gwr
278 1.15 gwr /*
279 1.15 gwr * Determine width and height as follows:
280 1.15 gwr * If it has a P4 register, use that;
281 1.15 gwr * else if unit==0, use the EEPROM size,
282 1.15 gwr * else make our best guess.
283 1.15 gwr */
284 1.15 gwr if (fb->fb_pfour)
285 1.15 gwr fb_pfour_setsize(fb);
286 1.15 gwr else if (sc->sc_dev.dv_unit == 0)
287 1.15 gwr fb_eeprom_setsize(fb);
288 1.15 gwr else {
289 1.15 gwr /* Guess based on machine ID. */
290 1.15 gwr switch (cpu_machine_id) {
291 1.15 gwr default:
292 1.15 gwr /* Leave the defaults set above. */
293 1.15 gwr break;
294 1.15 gwr }
295 1.15 gwr }
296 1.10 christos printf(" (%dx%d)\n", fbt->fb_width, fbt->fb_height);
297 1.15 gwr
298 1.15 gwr /*
299 1.15 gwr * Make sure video is on. This driver uses a
300 1.15 gwr * black colormap to blank the screen, so if
301 1.15 gwr * there is any global enable, set it here.
302 1.15 gwr */
303 1.15 gwr tmp = 1;
304 1.15 gwr cg4svideo(fb, &tmp);
305 1.15 gwr if (fb->fb_pfour)
306 1.15 gwr fb_pfour_set_video(fb, 1);
307 1.15 gwr else
308 1.15 gwr enable_video(1);
309 1.15 gwr
310 1.15 gwr /* Let /dev/fb know we are here. */
311 1.2 gwr fb_attach(fb, 4);
312 1.1 gwr }
313 1.1 gwr
314 1.1 gwr int
315 1.1 gwr cg4open(dev, flags, mode, p)
316 1.1 gwr dev_t dev;
317 1.1 gwr int flags, mode;
318 1.1 gwr struct proc *p;
319 1.1 gwr {
320 1.1 gwr int unit = minor(dev);
321 1.1 gwr
322 1.7 thorpej if (unit >= cgfour_cd.cd_ndevs || cgfour_cd.cd_devs[unit] == NULL)
323 1.1 gwr return (ENXIO);
324 1.1 gwr return (0);
325 1.1 gwr }
326 1.1 gwr
327 1.1 gwr int
328 1.1 gwr cg4close(dev, flags, mode, p)
329 1.1 gwr dev_t dev;
330 1.1 gwr int flags, mode;
331 1.1 gwr struct proc *p;
332 1.1 gwr {
333 1.1 gwr
334 1.1 gwr return (0);
335 1.1 gwr }
336 1.1 gwr
337 1.1 gwr int
338 1.1 gwr cg4ioctl(dev, cmd, data, flags, p)
339 1.1 gwr dev_t dev;
340 1.1 gwr u_long cmd;
341 1.1 gwr caddr_t data;
342 1.1 gwr int flags;
343 1.1 gwr struct proc *p;
344 1.1 gwr {
345 1.7 thorpej struct cg4_softc *sc = cgfour_cd.cd_devs[minor(dev)];
346 1.1 gwr
347 1.1 gwr return (fbioctlfb(&sc->sc_fb, cmd, data));
348 1.1 gwr }
349 1.1 gwr
350 1.1 gwr /*
351 1.1 gwr * Return the address that would map the given device at the given
352 1.1 gwr * offset, allowing for the given protection, or return -1 for error.
353 1.1 gwr *
354 1.1 gwr * X11 expects its mmap'd region to look like this:
355 1.11 gwr * 128k overlay data memory
356 1.11 gwr * 128k overlay enable bitmap
357 1.1 gwr * 1024k color memory
358 1.15 gwr *
359 1.15 gwr * The hardware looks completely different.
360 1.1 gwr */
361 1.1 gwr int
362 1.4 mycroft cg4mmap(dev, off, prot)
363 1.1 gwr dev_t dev;
364 1.14 gwr int off;
365 1.2 gwr int prot;
366 1.1 gwr {
367 1.7 thorpej struct cg4_softc *sc = cgfour_cd.cd_devs[minor(dev)];
368 1.2 gwr register int physbase;
369 1.1 gwr
370 1.1 gwr if (off & PGOFSET)
371 1.5 mycroft panic("cg4mmap");
372 1.1 gwr
373 1.14 gwr if ((off < 0) || (off >= CG4_MMAP_SIZE))
374 1.2 gwr return (-1);
375 1.2 gwr
376 1.2 gwr if (off < 0x40000) {
377 1.2 gwr if (off < 0x20000) {
378 1.11 gwr physbase = sc->sc_pa_overlay;
379 1.2 gwr } else {
380 1.2 gwr /* enable plane */
381 1.2 gwr off -= 0x20000;
382 1.11 gwr physbase = sc->sc_pa_enable;
383 1.2 gwr }
384 1.2 gwr } else {
385 1.2 gwr /* pixel map */
386 1.2 gwr off -= 0x40000;
387 1.11 gwr physbase = sc->sc_pa_pixmap;
388 1.1 gwr }
389 1.1 gwr
390 1.1 gwr /*
391 1.1 gwr * I turned on PMAP_NC here to disable the cache as I was
392 1.15 gwr * getting horribly broken behaviour without it.
393 1.1 gwr */
394 1.2 gwr return ((physbase + off) | PMAP_NC);
395 1.1 gwr }
396 1.1 gwr
397 1.1 gwr /*
398 1.1 gwr * Internal ioctl functions.
399 1.1 gwr */
400 1.1 gwr
401 1.1 gwr /* FBIOGATTR: */
402 1.12 gwr static int cg4gattr(fb, data)
403 1.1 gwr struct fbdevice *fb;
404 1.12 gwr void *data;
405 1.1 gwr {
406 1.12 gwr struct fbgattr *fba = data;
407 1.1 gwr
408 1.1 gwr fba->real_type = fb->fb_fbtype.fb_type;
409 1.1 gwr fba->owner = 0; /* XXX - TIOCCONS stuff? */
410 1.1 gwr fba->fbtype = fb->fb_fbtype;
411 1.1 gwr fba->sattr.flags = 0;
412 1.1 gwr fba->sattr.emu_type = fb->fb_fbtype.fb_type;
413 1.1 gwr fba->sattr.dev_specific[0] = -1;
414 1.1 gwr fba->emu_types[0] = fb->fb_fbtype.fb_type;
415 1.1 gwr fba->emu_types[1] = -1;
416 1.1 gwr return (0);
417 1.1 gwr }
418 1.1 gwr
419 1.1 gwr /* FBIOGVIDEO: */
420 1.12 gwr static int cg4gvideo(fb, data)
421 1.1 gwr struct fbdevice *fb;
422 1.12 gwr void *data;
423 1.1 gwr {
424 1.14 gwr struct cg4_softc *sc = fb->fb_private;
425 1.12 gwr int *on = data;
426 1.1 gwr
427 1.14 gwr *on = sc->sc_video_on;
428 1.1 gwr return (0);
429 1.1 gwr }
430 1.1 gwr
431 1.1 gwr /* FBIOSVIDEO: */
432 1.12 gwr static int cg4svideo(fb, data)
433 1.1 gwr struct fbdevice *fb;
434 1.12 gwr void *data;
435 1.1 gwr {
436 1.14 gwr struct cg4_softc *sc = fb->fb_private;
437 1.12 gwr int *on = data;
438 1.11 gwr
439 1.14 gwr if (sc->sc_video_on == *on)
440 1.14 gwr return (0);
441 1.14 gwr sc->sc_video_on = *on;
442 1.14 gwr
443 1.15 gwr (*sc->sc_ldcmap)(sc);
444 1.11 gwr return (0);
445 1.11 gwr }
446 1.11 gwr
447 1.11 gwr /*
448 1.11 gwr * FBIOGETCMAP:
449 1.11 gwr * Copy current colormap out to user space.
450 1.11 gwr */
451 1.12 gwr static int cg4getcmap(fb, data)
452 1.11 gwr struct fbdevice *fb;
453 1.12 gwr void *data;
454 1.11 gwr {
455 1.11 gwr struct cg4_softc *sc = fb->fb_private;
456 1.11 gwr struct soft_cmap *cm = &sc->sc_cmap;
457 1.14 gwr struct fbcmap *fbcm = data;
458 1.11 gwr int error, start, count;
459 1.11 gwr
460 1.11 gwr start = fbcm->index;
461 1.11 gwr count = fbcm->count;
462 1.11 gwr if ((start < 0) || (start >= CMAP_SIZE) ||
463 1.11 gwr (count < 0) || (start + count > CMAP_SIZE) )
464 1.11 gwr return (EINVAL);
465 1.11 gwr
466 1.11 gwr if ((error = copyout(&cm->r[start], fbcm->red, count)) != 0)
467 1.11 gwr return (error);
468 1.11 gwr
469 1.11 gwr if ((error = copyout(&cm->g[start], fbcm->green, count)) != 0)
470 1.11 gwr return (error);
471 1.11 gwr
472 1.11 gwr if ((error = copyout(&cm->b[start], fbcm->blue, count)) != 0)
473 1.11 gwr return (error);
474 1.11 gwr
475 1.11 gwr return (0);
476 1.11 gwr }
477 1.11 gwr
478 1.11 gwr /*
479 1.11 gwr * FBIOPUTCMAP:
480 1.11 gwr * Copy new colormap from user space and load.
481 1.11 gwr */
482 1.12 gwr static int cg4putcmap(fb, data)
483 1.11 gwr struct fbdevice *fb;
484 1.12 gwr void *data;
485 1.11 gwr {
486 1.11 gwr struct cg4_softc *sc = fb->fb_private;
487 1.11 gwr struct soft_cmap *cm = &sc->sc_cmap;
488 1.14 gwr struct fbcmap *fbcm = data;
489 1.11 gwr int error, start, count;
490 1.11 gwr
491 1.11 gwr start = fbcm->index;
492 1.11 gwr count = fbcm->count;
493 1.11 gwr if ((start < 0) || (start >= CMAP_SIZE) ||
494 1.11 gwr (count < 0) || (start + count > CMAP_SIZE) )
495 1.11 gwr return (EINVAL);
496 1.11 gwr
497 1.11 gwr if ((error = copyin(fbcm->red, &cm->r[start], count)) != 0)
498 1.11 gwr return (error);
499 1.11 gwr
500 1.11 gwr if ((error = copyin(fbcm->green, &cm->g[start], count)) != 0)
501 1.11 gwr return (error);
502 1.11 gwr
503 1.11 gwr if ((error = copyin(fbcm->blue, &cm->b[start], count)) != 0)
504 1.11 gwr return (error);
505 1.11 gwr
506 1.15 gwr (*sc->sc_ldcmap)(sc);
507 1.11 gwr return (0);
508 1.11 gwr }
509 1.11 gwr
510 1.11 gwr /****************************************************************
511 1.11 gwr * Routines for the "Type A" hardware
512 1.11 gwr ****************************************************************/
513 1.15 gwr #ifdef _SUN3_
514 1.11 gwr
515 1.11 gwr static void
516 1.11 gwr cg4a_init(sc)
517 1.11 gwr struct cg4_softc *sc;
518 1.11 gwr {
519 1.11 gwr volatile struct amd_regs *ar = sc->sc_va_cmap;
520 1.11 gwr struct soft_cmap *cm = &sc->sc_cmap;
521 1.11 gwr int i;
522 1.11 gwr
523 1.14 gwr /* Grab initial (current) color map. */
524 1.11 gwr for(i = 0; i < 256; i++) {
525 1.11 gwr cm->r[i] = ar->r[i];
526 1.11 gwr cm->g[i] = ar->g[i];
527 1.11 gwr cm->b[i] = ar->b[i];
528 1.11 gwr }
529 1.11 gwr }
530 1.11 gwr
531 1.11 gwr static void
532 1.11 gwr cg4a_ldcmap(sc)
533 1.11 gwr struct cg4_softc *sc;
534 1.11 gwr {
535 1.11 gwr volatile struct amd_regs *ar = sc->sc_va_cmap;
536 1.11 gwr struct soft_cmap *cm = &sc->sc_cmap;
537 1.11 gwr int i;
538 1.11 gwr
539 1.11 gwr /*
540 1.11 gwr * Now blast them into the chip!
541 1.11 gwr * XXX Should use retrace interrupt!
542 1.11 gwr * Just set a "need load" bit and let the
543 1.11 gwr * retrace interrupt handler do the work.
544 1.11 gwr */
545 1.14 gwr if (sc->sc_video_on) {
546 1.14 gwr /* Update H/W colormap. */
547 1.14 gwr for (i = 0; i < 256; i++) {
548 1.14 gwr ar->r[i] = cm->r[i];
549 1.14 gwr ar->g[i] = cm->g[i];
550 1.14 gwr ar->b[i] = cm->b[i];
551 1.14 gwr }
552 1.14 gwr } else {
553 1.14 gwr /* Clear H/W colormap. */
554 1.11 gwr for (i = 0; i < 256; i++) {
555 1.11 gwr ar->r[i] = 0;
556 1.11 gwr ar->g[i] = 0;
557 1.11 gwr ar->b[i] = 0;
558 1.11 gwr }
559 1.1 gwr }
560 1.11 gwr }
561 1.15 gwr #endif /* SUN3 */
562 1.11 gwr
563 1.11 gwr /****************************************************************
564 1.11 gwr * Routines for the "Type B" hardware
565 1.11 gwr ****************************************************************/
566 1.1 gwr
567 1.11 gwr static void
568 1.11 gwr cg4b_init(sc)
569 1.11 gwr struct cg4_softc *sc;
570 1.11 gwr {
571 1.11 gwr volatile struct bt_regs *bt = sc->sc_va_cmap;
572 1.11 gwr struct soft_cmap *cm = &sc->sc_cmap;
573 1.14 gwr union bt_cmap *btcm;
574 1.11 gwr int i;
575 1.11 gwr
576 1.14 gwr /* Need a buffer for colormap format translation. */
577 1.15 gwr btcm = malloc(sizeof(*btcm), M_DEVBUF, M_WAITOK);
578 1.14 gwr sc->sc_btcm = btcm;
579 1.14 gwr
580 1.11 gwr /*
581 1.11 gwr * BT458 chip initialization as described in Brooktree's
582 1.11 gwr * 1993 Graphics and Imaging Product Databook (DB004-1/93).
583 1.11 gwr */
584 1.11 gwr bt->bt_addr = 0x04; /* select read mask register */
585 1.11 gwr bt->bt_ctrl = 0xff; /* all planes on */
586 1.11 gwr bt->bt_addr = 0x05; /* select blink mask register */
587 1.11 gwr bt->bt_ctrl = 0x00; /* all planes non-blinking */
588 1.11 gwr bt->bt_addr = 0x06; /* select command register */
589 1.11 gwr bt->bt_ctrl = 0x43; /* palette enabled, overlay planes enabled */
590 1.11 gwr bt->bt_addr = 0x07; /* select test register */
591 1.11 gwr bt->bt_ctrl = 0x00; /* set test mode */
592 1.11 gwr
593 1.11 gwr /* grab initial (current) color map */
594 1.11 gwr bt->bt_addr = 0;
595 1.11 gwr for (i = 0; i < (256 * 3 / 4); i++) {
596 1.11 gwr btcm->cm_chip[i] = bt->bt_cmap;
597 1.11 gwr }
598 1.1 gwr
599 1.14 gwr /* Transpose into H/W cmap into S/W form. */
600 1.11 gwr for (i = 0; i < 256; i++) {
601 1.11 gwr cm->r[i] = btcm->cm_map[i][0];
602 1.11 gwr cm->g[i] = btcm->cm_map[i][1];
603 1.11 gwr cm->b[i] = btcm->cm_map[i][2];
604 1.1 gwr }
605 1.1 gwr }
606 1.1 gwr
607 1.11 gwr static void
608 1.11 gwr cg4b_ldcmap(sc)
609 1.11 gwr struct cg4_softc *sc;
610 1.1 gwr {
611 1.11 gwr volatile struct bt_regs *bt = sc->sc_va_cmap;
612 1.11 gwr struct soft_cmap *cm = &sc->sc_cmap;
613 1.11 gwr union bt_cmap *btcm = sc->sc_btcm;
614 1.11 gwr int i;
615 1.1 gwr
616 1.14 gwr /* Transpose S/W cmap into H/W form. */
617 1.14 gwr for (i = 0; i < 256; i++) {
618 1.14 gwr btcm->cm_map[i][0] = cm->r[i];
619 1.14 gwr btcm->cm_map[i][1] = cm->g[i];
620 1.14 gwr btcm->cm_map[i][2] = cm->b[i];
621 1.14 gwr }
622 1.14 gwr
623 1.11 gwr /*
624 1.11 gwr * Now blast them into the chip!
625 1.11 gwr * XXX Should use retrace interrupt!
626 1.11 gwr * Just set a "need load" bit and let the
627 1.11 gwr * retrace interrupt handler do the work.
628 1.11 gwr */
629 1.11 gwr bt->bt_addr = 0;
630 1.14 gwr if (sc->sc_video_on) {
631 1.14 gwr /* Update H/W colormap. */
632 1.14 gwr for (i = 0; i < (256 * 3 / 4); i++)
633 1.14 gwr bt->bt_cmap = btcm->cm_chip[i];
634 1.14 gwr } else {
635 1.14 gwr /* Clear H/W colormap. */
636 1.11 gwr for (i = 0; i < (256 * 3 / 4); i++)
637 1.11 gwr bt->bt_cmap = 0;
638 1.11 gwr }
639 1.1 gwr }
640 1.11 gwr
641