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