cgthree.c revision 1.4 1 /* $NetBSD: cgthree.c,v 1.4 2001/11/13 06:54:32 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * color display (cgthree) driver.
41 *
42 * Does not handle interrupts, even though they can occur.
43 *
44 * XXX should defer colormap updates to vertical retrace interrupts
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: cgthree.c,v 1.4 2001/11/13 06:54:32 lukem Exp $");
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/buf.h>
53 #include <sys/device.h>
54 #include <sys/ioctl.h>
55 #include <sys/malloc.h>
56 #include <sys/mman.h>
57 #include <sys/tty.h>
58 #include <sys/conf.h>
59
60 #include <machine/bus.h>
61 #include <machine/autoconf.h>
62
63 #include <dev/sun/fbio.h>
64 #include <dev/sun/fbvar.h>
65
66 #include <dev/sun/btreg.h>
67 #include <dev/sun/btvar.h>
68 #include <dev/sun/cgthreereg.h>
69 #include <dev/sun/cgthreevar.h>
70
71 #include <machine/conf.h>
72
73 static void cgthreeunblank(struct device *);
74 static void cgthreeloadcmap(struct cgthree_softc *, int, int);
75 static void cgthree_set_video(struct cgthree_softc *, int);
76 static int cgthree_get_video(struct cgthree_softc *);
77
78 /* cdevsw prototypes */
79 cdev_decl(cgthree);
80
81 extern struct cfdriver cgthree_cd;
82
83 /* frame buffer generic driver */
84 static struct fbdriver cgthreefbdriver = {
85 cgthreeunblank, cgthreeopen, cgthreeclose, cgthreeioctl, cgthreepoll,
86 cgthreemmap
87 };
88
89 /* Video control parameters */
90 struct cg3_videoctrl {
91 unsigned char sense; /* Monitor sense value */
92 unsigned char vctrl[12];
93 } cg3_videoctrl[] = {
94 /* Missing entries: sense 0x10, 0x30, 0x50 */
95 { 0x40, /* this happens to be my 19'' 1152x900 gray-scale monitor */
96 {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1}
97 },
98 { 0x00, /* default? must be last */
99 {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1}
100 }
101 };
102
103
104 void
105 cgthreeattach(sc, name, isconsole)
106 struct cgthree_softc *sc;
107 char *name;
108 int isconsole;
109 {
110 int i;
111 struct fbdevice *fb = &sc->sc_fb;
112 volatile struct fbcontrol *fbc = sc->sc_fbc;
113 volatile struct bt_regs *bt = &fbc->fbc_dac;
114
115 fb->fb_driver = &cgthreefbdriver;
116 fb->fb_type.fb_cmsize = 256;
117 fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
118 printf(": %s, %d x %d", name,
119 fb->fb_type.fb_width, fb->fb_type.fb_height);
120
121 /* Transfer video magic to board, if it's not running */
122 if ((fbc->fbc_ctrl & FBC_TIMING) == 0) {
123 int sense = (fbc->fbc_status & FBS_MSENSE);
124 /* Search table for video timings fitting this monitor */
125 for (i = 0; i < sizeof(cg3_videoctrl)/sizeof(cg3_videoctrl[0]);
126 i++) {
127 int j;
128 if (sense != cg3_videoctrl[i].sense)
129 continue;
130
131 printf(" setting video ctrl");
132 for (j = 0; j < 12; j++)
133 fbc->fbc_vcontrol[j] =
134 cg3_videoctrl[i].vctrl[j];
135 fbc->fbc_ctrl |= FBC_TIMING;
136 break;
137 }
138 }
139
140 /* Initialize the default color map. */
141 bt_initcmap(&sc->sc_cmap, 256);
142 cgthreeloadcmap(sc, 0, 256);
143
144 /* make sure we are not blanked */
145 cgthree_set_video(sc, 1);
146 BT_INIT(bt, 0);
147
148 if (isconsole) {
149 printf(" (console)\n");
150 #ifdef RASTERCONSOLE
151 fbrcons_init(fb);
152 #endif
153 } else
154 printf("\n");
155
156 fb_attach(fb, isconsole);
157 }
158
159
160 int
161 cgthreeopen(dev, flags, mode, p)
162 dev_t dev;
163 int flags, mode;
164 struct proc *p;
165 {
166 int unit = minor(dev);
167
168 if (unit >= cgthree_cd.cd_ndevs || cgthree_cd.cd_devs[unit] == NULL)
169 return (ENXIO);
170 return (0);
171 }
172
173 int
174 cgthreeclose(dev, flags, mode, p)
175 dev_t dev;
176 int flags, mode;
177 struct proc *p;
178 {
179
180 return (0);
181 }
182
183 int
184 cgthreeioctl(dev, cmd, data, flags, p)
185 dev_t dev;
186 u_long cmd;
187 caddr_t data;
188 int flags;
189 struct proc *p;
190 {
191 struct cgthree_softc *sc = cgthree_cd.cd_devs[minor(dev)];
192 struct fbgattr *fba;
193 int error;
194
195 switch (cmd) {
196
197 case FBIOGTYPE:
198 *(struct fbtype *)data = sc->sc_fb.fb_type;
199 break;
200
201 case FBIOGATTR:
202 fba = (struct fbgattr *)data;
203 fba->real_type = sc->sc_fb.fb_type.fb_type;
204 fba->owner = 0; /* XXX ??? */
205 fba->fbtype = sc->sc_fb.fb_type;
206 fba->sattr.flags = 0;
207 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
208 fba->sattr.dev_specific[0] = -1;
209 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
210 fba->emu_types[1] = -1;
211 break;
212
213 case FBIOGETCMAP:
214 #define p ((struct fbcmap *)data)
215 return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
216
217 case FBIOPUTCMAP:
218 /* copy to software map */
219 error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
220 if (error)
221 return (error);
222 /* now blast them into the chip */
223 /* XXX should use retrace interrupt */
224 cgthreeloadcmap(sc, p->index, p->count);
225 #undef p
226 break;
227
228 case FBIOGVIDEO:
229 *(int *)data = cgthree_get_video(sc);
230 break;
231
232 case FBIOSVIDEO:
233 cgthree_set_video(sc, *(int *)data);
234 break;
235
236 default:
237 return (ENOTTY);
238 }
239 return (0);
240 }
241
242 int
243 cgthreepoll(dev, events, p)
244 dev_t dev;
245 int events;
246 struct proc *p;
247 {
248
249 return (seltrue(dev, events, p));
250 }
251
252 /*
253 * Undo the effect of an FBIOSVIDEO that turns the video off.
254 */
255 static void
256 cgthreeunblank(dev)
257 struct device *dev;
258 {
259
260 cgthree_set_video((struct cgthree_softc *)dev, 1);
261 }
262
263 static void
264 cgthree_set_video(sc, enable)
265 struct cgthree_softc *sc;
266 int enable;
267 {
268
269 if (enable)
270 sc->sc_fbc->fbc_ctrl |= FBC_VENAB;
271 else
272 sc->sc_fbc->fbc_ctrl &= ~FBC_VENAB;
273 }
274
275 static int
276 cgthree_get_video(sc)
277 struct cgthree_softc *sc;
278 {
279
280 return ((sc->sc_fbc->fbc_ctrl & FBC_VENAB) != 0);
281 }
282
283 /*
284 * Load a subset of the current (new) colormap into the Brooktree DAC.
285 */
286 static void
287 cgthreeloadcmap(sc, start, ncolors)
288 struct cgthree_softc *sc;
289 int start, ncolors;
290 {
291 volatile struct bt_regs *bt;
292 u_int *ip;
293 int count;
294
295 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */
296 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
297 bt = &sc->sc_fbc->fbc_dac;
298 bt->bt_addr = BT_D4M4(start);
299 while (--count >= 0)
300 bt->bt_cmap = *ip++;
301 }
302
303 /*
304 * Return the address that would map the given device at the given
305 * offset, allowing for the given protection, or return -1 for error.
306 *
307 * The cg3 is mapped starting at 256KB, for pseudo-compatibility with
308 * the cg4 (which had an overlay plane in the first 128K and an enable
309 * plane in the next 128K). X11 uses only 256k+ region but tries to
310 * map the whole thing, so we repeatedly map the first 256K to the
311 * first page of the color screen. If someone tries to use the overlay
312 * and enable regions, they will get a surprise....
313 *
314 * As well, mapping at an offset of 0x04000000 causes the cg3 to be
315 * mapped in flat mode without the cg4 emulation.
316 */
317 paddr_t
318 cgthreemmap(dev, off, prot)
319 dev_t dev;
320 off_t off;
321 int prot;
322 {
323 struct cgthree_softc *sc = cgthree_cd.cd_devs[minor(dev)];
324
325 #define START (128*1024 + 128*1024)
326 #define NOOVERLAY (0x04000000)
327
328 if (off & PGOFSET)
329 panic("cgthreemmap");
330 if (off < 0)
331 return (-1);
332 if ((u_int)off >= NOOVERLAY)
333 off -= NOOVERLAY;
334 else if ((u_int)off >= START)
335 off -= START;
336 else
337 off = 0;
338
339 if (off >= sc->sc_fb.fb_type.fb_size)
340 return (-1);
341
342 return (bus_space_mmap(sc->sc_bustag,
343 sc->sc_paddr, CG3REG_MEM + off,
344 prot, BUS_SPACE_MAP_LINEAR));
345 }
346