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