xref: /netbsd-src/sys/arch/sun3/dev/fb.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: fb.c,v 1.5 1998/02/08 05:15:35 gwr 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. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by the University of
27  *	California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *	@(#)fb.c	8.1 (Berkeley) 6/11/93
45  */
46 
47 /*
48  * /dev/fb (indirect frame buffer driver).
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/conf.h>
54 #include <sys/device.h>
55 #include <sys/ioctl.h>
56 #include <sys/proc.h>
57 
58 #include <machine/eeprom.h>
59 #include <machine/fbio.h>
60 
61 #include <sun3/dev/fbvar.h>
62 #include <sun3/dev/p4reg.h>
63 
64 cdev_decl(fb);
65 
66 static struct fbdevice *devfb;
67 static int fbpriority;
68 
69 /*
70  * This is called by the real driver (i.e. bw2, cg3, ...)
71  * to declare itself as a potential default frame buffer.
72  */
73 void
74 fb_attach(fb, newpri)
75 	struct fbdevice *fb;
76 	int newpri;
77 {
78 	if (fbpriority < newpri) {
79 		fbpriority = newpri;
80 		devfb = fb;
81 	}
82 }
83 
84 int
85 fbopen(dev, flags, mode, p)
86 	dev_t dev;
87 	int flags, mode;
88 	struct proc *p;
89 {
90 
91 	if (devfb == NULL)
92 		return (ENXIO);
93 	return ((*devfb->fb_driver->fbd_open)(dev, flags, mode, p));
94 }
95 
96 int
97 fbclose(dev, flags, mode, p)
98 	dev_t dev;
99 	int flags, mode;
100 	struct proc *p;
101 {
102 
103 	return ((*devfb->fb_driver->fbd_close)(dev, flags, mode, p));
104 }
105 
106 int
107 fbioctl(dev, cmd, data, flags, p)
108 	dev_t dev;
109 	u_long cmd;
110 	caddr_t data;
111 	int flags;
112 	struct proc *p;
113 {
114 	return (fbioctlfb(devfb, cmd, data));
115 }
116 
117 int
118 fbmmap(dev, off, prot)
119 	dev_t dev;
120 	int off, prot;
121 {
122 	return ((*devfb->fb_driver->fbd_mmap)(dev, off, prot));
123 }
124 
125 /*
126  * Common fb ioctl function
127  */
128 int
129 fbioctlfb(fb, cmd, data)
130 	struct fbdevice *fb;
131 	u_long cmd;
132 	caddr_t data;
133 {
134 	struct fbdriver *fbd = fb->fb_driver;
135 	void *vp = (void *)data;
136 	int error;
137 
138 	switch (cmd) {
139 
140 	case FBIOGTYPE:
141 		*(struct fbtype *)vp = fb->fb_fbtype;
142 		error = 0;
143 		break;
144 
145 	case FBIOGATTR:
146 		error = (*fbd->fbd_gattr)(fb, vp);
147 		break;
148 
149 	case FBIOGVIDEO:
150 		error = (*fbd->fbd_gvideo)(fb, vp);
151 		break;
152 
153 	case FBIOSVIDEO:
154 		error = (*fbd->fbd_svideo)(fb, vp);
155 		break;
156 
157 	case FBIOGETCMAP:
158 		error = (*fbd->fbd_getcmap)(fb, vp);
159 		break;
160 
161 	case FBIOPUTCMAP:
162 		error = (*fbd->fbd_putcmap)(fb, vp);
163 		break;
164 
165 	default:
166 		error = ENOTTY;
167 	}
168 	return (error);
169 }
170 
171 void
172 fb_unblank()
173 {
174 	int on = 1;
175 
176 	if (devfb == NULL)
177 		return;
178 
179 	(*devfb->fb_driver->fbd_svideo)(devfb, (void *)&on);
180 }
181 
182 /*
183  * Default ioctl function to put in struct fbdriver
184  * for functions that are not supported.
185  */
186 int
187 fb_noioctl(fbd, vp)
188 	struct fbdevice *fbd;
189 	void *vp;
190 {
191 	return ENOTTY;
192 }
193 
194 /****************************************************************
195  * Misc. helpers...
196  */
197 
198 /* Set FB size based on EEPROM screen shape code. */
199 void
200 fb_eeprom_setsize(fb)
201 	struct fbdevice *fb;
202 {
203 	int szcode;
204 	int w, h;
205 
206 	/* Go get the EEPROM screen size byte. */
207 	szcode = eeprom_copy->eeScreenSize;
208 
209 	w = h = 0;
210 	switch (szcode) {
211 	case EE_SCR_1152X900:
212 		w = 1152;
213 		h = 900;
214 		break;
215 	case EE_SCR_1024X1024:
216 		w = 1024;
217 		h = 1024;
218 		break;
219 	case EE_SCR_1600X1280:
220 		w = 1600;
221 		h = 1280;
222 		break;
223 	case EE_SCR_1440X1440:
224 		w = 1440;
225 		h = 1440;
226 		break;
227 	default:
228 		break;
229 	}
230 
231 	if (w && h) {
232 		fb->fb_fbtype.fb_width  = w;
233 		fb->fb_fbtype.fb_height = h;
234 	} else {
235 		printf("%s: EE size code %d unknown\n",
236 			   fb->fb_name, szcode);
237 	}
238 }
239 
240 /*
241  * Probe for a P4 register at the passed virtual address.
242  * Returns P4 ID value, or -1 if no P4 register.
243  */
244 int
245 fb_pfour_id(va)
246 	void *va;
247 {
248 	volatile u_int32_t val, save, *pfour = va;
249 
250 	/* Read the P4 register. */
251 	save = *pfour;
252 
253 	/*
254 	 * Try to modify the type code.  If it changes, put the
255 	 * original value back, and tell the caller that the
256 	 * framebuffer does not have a P4 register.
257 	 */
258 	val = save & ~P4_REG_RESET;
259 	*pfour = (val ^ P4_FBTYPE_MASK);
260 	if ((*pfour ^ val) & P4_FBTYPE_MASK) {
261 		*pfour = save;
262 		return (-1);
263 	}
264 
265 	return (P4_ID(val));
266 }
267 
268 /*
269  * Return the status of the video enable.
270  */
271 int
272 fb_pfour_get_video(fb)
273 	struct fbdevice *fb;
274 {
275 
276 	return ((*fb->fb_pfour & P4_REG_VIDEO) != 0);
277 }
278 
279 /*
280  * Turn video on or off using the P4 register.
281  */
282 void
283 fb_pfour_set_video(fb, on)
284 	struct fbdevice *fb;
285 	int on;
286 {
287 	int pfour;
288 
289 	pfour = *fb->fb_pfour & ~(P4_REG_INTCLR|P4_REG_VIDEO);
290 	*fb->fb_pfour = pfour | (on ? P4_REG_VIDEO : 0);
291 }
292 
293 static const struct {
294 	int w, h;
295 } fb_p4sizedecode[P4_SIZE_MASK+1] = {
296 	{ 1600, 1280 },
297 	{ 1152,  900 },
298 	{ 1024, 1024 },
299 	{ 1280, 1024 },
300 	{ 1440, 1440 },
301 	{  640,  480 },
302 };
303 
304 /*
305  * Use the P4 register to determine the screen size.
306  */
307 void
308 fb_pfour_setsize(fb)
309 	struct fbdevice *fb;
310 {
311 	int p4, p4type, p4size;
312 	int h, w;
313 
314 	if (fb->fb_pfour == 0)
315 		return;
316 
317 	p4 = *fb->fb_pfour;
318 	p4type = P4_FBTYPE(p4);
319 
320 	/* These do not encode the screen size. */
321 	if (p4type == P4_ID_COLOR24)
322 		return;
323 	if ((p4type & P4_ID_MASK) == P4_ID_FASTCOLOR)
324 		return;
325 
326 	p4size = p4type & P4_SIZE_MASK;
327 	w = fb_p4sizedecode[p4size].w;
328 	h = fb_p4sizedecode[p4size].h;
329 	if (w && h) {
330 		fb->fb_fbtype.fb_width  = w;
331 		fb->fb_fbtype.fb_height = h;
332 	} else {
333 		printf("%s: P4 size code %d unknown\n",
334 			   fb->fb_name, p4size);
335 	}
336 }
337