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