cgthree.c revision 1.9 1 /* $NetBSD: cgthree.c,v 1.9 2003/08/24 17:32:06 uwe 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.9 2003/08/24 17:32:06 uwe Exp $");
49
50 #include "opt_rcons.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/buf.h>
55 #include <sys/device.h>
56 #include <sys/ioctl.h>
57 #include <sys/malloc.h>
58 #include <sys/mman.h>
59 #include <sys/tty.h>
60 #include <sys/conf.h>
61
62 #include <machine/bus.h>
63 #include <machine/autoconf.h>
64
65 #include <dev/sun/fbio.h>
66 #include <dev/sun/fbvar.h>
67
68 #include <dev/sun/btreg.h>
69 #include <dev/sun/btvar.h>
70 #include <dev/sun/cgthreereg.h>
71 #include <dev/sun/cgthreevar.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 extern struct cfdriver cgthree_cd;
79
80 dev_type_open(cgthreeopen);
81 dev_type_ioctl(cgthreeioctl);
82 dev_type_mmap(cgthreemmap);
83
84 const struct cdevsw cgthree_cdevsw = {
85 cgthreeopen, nullclose, noread, nowrite, cgthreeioctl,
86 nostop, notty, nopoll, cgthreemmap, nokqfilter
87 };
88
89 /* frame buffer generic driver */
90 static struct fbdriver cgthreefbdriver = {
91 cgthreeunblank, cgthreeopen, nullclose, cgthreeioctl, nopoll,
92 cgthreemmap, nokqfilter
93 };
94
95 /* Video control parameters */
96 struct cg3_videoctrl {
97 unsigned char sense; /* Monitor sense value */
98 unsigned char vctrl[12];
99 } cg3_videoctrl[] = {
100 /* Missing entries: sense 0x10, 0x30, 0x50 */
101 { 0x40, /* this happens to be my 19'' 1152x900 gray-scale monitor */
102 {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1}
103 },
104 { 0x00, /* default? must be last */
105 {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1}
106 }
107 };
108
109
110 void
111 cgthreeattach(sc, name, isconsole)
112 struct cgthree_softc *sc;
113 char *name;
114 int isconsole;
115 {
116 int i;
117 struct fbdevice *fb = &sc->sc_fb;
118 volatile struct fbcontrol *fbc = sc->sc_fbc;
119 volatile struct bt_regs *bt = &fbc->fbc_dac;
120
121 fb->fb_driver = &cgthreefbdriver;
122 fb->fb_type.fb_cmsize = 256;
123 fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
124 printf(": %s, %d x %d", name,
125 fb->fb_type.fb_width, fb->fb_type.fb_height);
126
127 /* Transfer video magic to board, if it's not running */
128 if ((fbc->fbc_ctrl & FBC_TIMING) == 0) {
129 int sense = (fbc->fbc_status & FBS_MSENSE);
130 /* Search table for video timings fitting this monitor */
131 for (i = 0; i < sizeof(cg3_videoctrl)/sizeof(cg3_videoctrl[0]);
132 i++) {
133 int j;
134 if (sense != cg3_videoctrl[i].sense)
135 continue;
136
137 printf(" setting video ctrl");
138 for (j = 0; j < 12; j++)
139 fbc->fbc_vcontrol[j] =
140 cg3_videoctrl[i].vctrl[j];
141 fbc->fbc_ctrl |= FBC_TIMING;
142 break;
143 }
144 }
145
146 /* Initialize the default color map. */
147 bt_initcmap(&sc->sc_cmap, 256);
148 cgthreeloadcmap(sc, 0, 256);
149
150 /* make sure we are not blanked */
151 cgthree_set_video(sc, 1);
152 BT_INIT(bt, 0);
153
154 if (isconsole) {
155 printf(" (console)\n");
156 #ifdef RASTERCONSOLE
157 fbrcons_init(fb);
158 #endif
159 } else
160 printf("\n");
161
162 fb_attach(fb, isconsole);
163 }
164
165
166 int
167 cgthreeopen(dev, flags, mode, p)
168 dev_t dev;
169 int flags, mode;
170 struct proc *p;
171 {
172 int unit = minor(dev);
173
174 if (unit >= cgthree_cd.cd_ndevs || cgthree_cd.cd_devs[unit] == NULL)
175 return (ENXIO);
176 return (0);
177 }
178
179 int
180 cgthreeioctl(dev, cmd, data, flags, p)
181 dev_t dev;
182 u_long cmd;
183 caddr_t data;
184 int flags;
185 struct proc *p;
186 {
187 struct cgthree_softc *sc = cgthree_cd.cd_devs[minor(dev)];
188 struct fbgattr *fba;
189 int error;
190
191 switch (cmd) {
192
193 case FBIOGTYPE:
194 *(struct fbtype *)data = sc->sc_fb.fb_type;
195 break;
196
197 case FBIOGATTR:
198 fba = (struct fbgattr *)data;
199 fba->real_type = sc->sc_fb.fb_type.fb_type;
200 fba->owner = 0; /* XXX ??? */
201 fba->fbtype = sc->sc_fb.fb_type;
202 fba->sattr.flags = 0;
203 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
204 fba->sattr.dev_specific[0] = -1;
205 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
206 fba->emu_types[1] = -1;
207 break;
208
209 case FBIOGETCMAP:
210 #define p ((struct fbcmap *)data)
211 return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
212
213 case FBIOPUTCMAP:
214 /* copy to software map */
215 error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
216 if (error)
217 return (error);
218 /* now blast them into the chip */
219 /* XXX should use retrace interrupt */
220 cgthreeloadcmap(sc, p->index, p->count);
221 #undef p
222 break;
223
224 case FBIOGVIDEO:
225 *(int *)data = cgthree_get_video(sc);
226 break;
227
228 case FBIOSVIDEO:
229 cgthree_set_video(sc, *(int *)data);
230 break;
231
232 default:
233 return (ENOTTY);
234 }
235 return (0);
236 }
237
238 /*
239 * Undo the effect of an FBIOSVIDEO that turns the video off.
240 */
241 static void
242 cgthreeunblank(dev)
243 struct device *dev;
244 {
245
246 cgthree_set_video((struct cgthree_softc *)dev, 1);
247 }
248
249 static void
250 cgthree_set_video(sc, enable)
251 struct cgthree_softc *sc;
252 int enable;
253 {
254
255 if (enable)
256 sc->sc_fbc->fbc_ctrl |= FBC_VENAB;
257 else
258 sc->sc_fbc->fbc_ctrl &= ~FBC_VENAB;
259 }
260
261 static int
262 cgthree_get_video(sc)
263 struct cgthree_softc *sc;
264 {
265
266 return ((sc->sc_fbc->fbc_ctrl & FBC_VENAB) != 0);
267 }
268
269 /*
270 * Load a subset of the current (new) colormap into the Brooktree DAC.
271 */
272 static void
273 cgthreeloadcmap(sc, start, ncolors)
274 struct cgthree_softc *sc;
275 int start, ncolors;
276 {
277 volatile struct bt_regs *bt;
278 u_int *ip;
279 int count;
280
281 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */
282 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
283 bt = &sc->sc_fbc->fbc_dac;
284 bt->bt_addr = BT_D4M4(start);
285 while (--count >= 0)
286 bt->bt_cmap = *ip++;
287 }
288
289 /*
290 * Return the address that would map the given device at the given
291 * offset, allowing for the given protection, or return -1 for error.
292 *
293 * The cg3 is mapped starting at 256KB, for pseudo-compatibility with
294 * the cg4 (which had an overlay plane in the first 128K and an enable
295 * plane in the next 128K). X11 uses only 256k+ region but tries to
296 * map the whole thing, so we repeatedly map the first 256K to the
297 * first page of the color screen. If someone tries to use the overlay
298 * and enable regions, they will get a surprise....
299 *
300 * As well, mapping at an offset of 0x04000000 causes the cg3 to be
301 * mapped in flat mode without the cg4 emulation.
302 */
303 paddr_t
304 cgthreemmap(dev, off, prot)
305 dev_t dev;
306 off_t off;
307 int prot;
308 {
309 struct cgthree_softc *sc = cgthree_cd.cd_devs[minor(dev)];
310
311 #define START (128*1024 + 128*1024)
312 #define NOOVERLAY (0x04000000)
313
314 if (off & PGOFSET)
315 panic("cgthreemmap");
316 if (off < 0)
317 return (-1);
318 if ((u_int)off >= NOOVERLAY)
319 off -= NOOVERLAY;
320 else if ((u_int)off >= START)
321 off -= START;
322 else
323 off = 0;
324
325 if (off >= sc->sc_fb.fb_type.fb_size)
326 return (-1);
327
328 return (bus_space_mmap(sc->sc_bustag,
329 sc->sc_paddr, CG3REG_MEM + off,
330 prot, BUS_SPACE_MAP_LINEAR));
331 }
332