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