xref: /netbsd-src/sys/dev/sun/fb.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /*	$NetBSD: fb.c,v 1.21 2005/06/24 06:40:05 jdc 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. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)fb.c	8.1 (Berkeley) 6/11/93
41  */
42 
43 /*
44  * /dev/fb (indirect frame buffer driver).  This is gross; we should
45  * just build cdevsw[] dynamically.
46  */
47 
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: fb.c,v 1.21 2005/06/24 06:40:05 jdc Exp $");
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/device.h>
54 #include <sys/proc.h>
55 #include <sys/conf.h>
56 #include <sys/malloc.h>
57 #include <sys/types.h>
58 
59 #include <machine/promlib.h>
60 #include <machine/autoconf.h>
61 #include <machine/kbd.h>
62 #include <machine/eeprom.h>
63 #include <sparc/dev/cons.h>
64 
65 #include <dev/sun/fbio.h>
66 #include <dev/sun/fbvar.h>
67 
68 #include "kbd.h"
69 #include "pfour.h"
70 
71 
72 struct fbdevlist {
73 	struct fbdevice *fb_dev;
74 	struct fbdevlist *fb_next;
75 };
76 
77 static struct fbdevlist fblist = {
78     NULL,
79     NULL,
80 };
81 
82 dev_type_open(fbopen);
83 dev_type_close(fbclose);
84 dev_type_ioctl(fbioctl);
85 dev_type_poll(fbpoll);
86 dev_type_mmap(fbmmap);
87 dev_type_kqfilter(fbkqfilter);
88 
89 const struct cdevsw fb_cdevsw = {
90 	fbopen, fbclose, noread, nowrite, fbioctl,
91 	nostop, notty, fbpoll, fbmmap, fbkqfilter,
92 };
93 
94 void
95 fb_unblank()
96 {
97 
98 	struct fbdevlist *fbl = &fblist;
99 
100 	while (fbl != NULL && fbl->fb_dev != NULL) {
101 		(*fbl->fb_dev->fb_driver->fbd_unblank)(fbl->fb_dev->fb_device);
102 		fbl = fbl->fb_next;
103 	}
104 }
105 
106 /*
107  * Helper function for frame buffer devices. Decides whether
108  * the device can be the console output device according to
109  * PROM info. The result from this function may not be conclusive
110  * on machines with old PROMs; in that case, drivers should consult
111  * other sources of configuration information (e.g. EEPROM entries).
112  */
113 int
114 fb_is_console(node)
115 	int node;
116 {
117 #if !defined(SUN4U)
118 	int fbnode;
119 
120 	switch (prom_version()) {
121 	case PROM_OLDMON:
122 		/* `node' is not valid; just check for any fb device */
123 		return (prom_stdout() == PROMDEV_SCREEN);
124 
125 	case PROM_OBP_V0:
126 		/*
127 		 * First, check if prom_stdout() represents a frame buffer,
128 		 * then match on the `fb' property on the root node, if any.
129 		 */
130 		if (prom_stdout() != PROMDEV_SCREEN)
131 			return (0);
132 
133 		fbnode = prom_getpropint(findroot(), "fb", 0);
134 		return (fbnode == 0 || node == fbnode);
135 
136 	case PROM_OBP_V2:
137 	case PROM_OBP_V3:
138 	case PROM_OPENFIRM:
139 		/* Just match the nodes */
140 		return (node == prom_stdout_node);
141 	}
142 
143 	return (0);
144 #else
145 		return (node == prom_stdout_node);
146 #endif
147 }
148 
149 void
150 fb_attach(fb, isconsole)
151 	struct fbdevice *fb;
152 	int isconsole;
153 {
154 	static int seen_force = 0;
155 	int nfb = 0;
156 	struct fbdevlist *fbl = &fblist;
157 
158 	/*
159 	 * Check to see if we're being forced into /dev/fb0, or if we're
160 	 * the console.  If we are, then move/replace the current fb0.
161 	 */
162 	if ((fb->fb_flags & FB_FORCE || (isconsole && !seen_force)) &&
163 	    fblist.fb_dev != NULL) {
164 		while (fbl->fb_next != NULL) {
165 			fbl = fbl->fb_next;
166 			nfb++;
167 		}
168 		if ((fbl->fb_next = malloc(sizeof (struct fbdevlist),
169 		    M_DEVBUF, M_NOWAIT)) == NULL)
170 			printf("%s: replacing %s at /dev/fb0\n",
171 			    fb->fb_device->dv_xname,
172 			    fblist.fb_dev->fb_device->dv_xname);
173 		else {
174 			fbl = fbl->fb_next;
175 			nfb++;
176 			fbl->fb_dev = fblist.fb_dev;
177 			fbl->fb_next = NULL;
178 			printf("%s: moved to /dev/fb%d\n",
179 			    fbl->fb_dev->fb_device->dv_xname, nfb);
180 			printf("%s: attached to /dev/fb0\n",
181 			    fb->fb_device->dv_xname);
182 		}
183 		fblist.fb_dev = fb;
184 		if (fb->fb_flags & FB_FORCE)
185 			seen_force = 1;
186 	/* Add to end of fb list. */
187 	} else {
188 		if (fblist.fb_dev != NULL) {
189 			while (fbl->fb_next != NULL) {
190 				fbl = fbl->fb_next;
191 				nfb++;
192 			}
193 			if ((fbl->fb_next = malloc(sizeof (struct fbdevlist),
194 			    M_DEVBUF, M_NOWAIT)) == NULL) {
195 				printf("%s: no space to attach after /dev/fb%d\n",
196 					fb->fb_device->dv_xname, nfb);
197 				return;
198 			}
199 			fbl = fbl->fb_next;
200 			nfb++;
201 		}
202 		fbl->fb_dev = fb;
203 		fbl->fb_next = NULL;
204 		printf("%s: attached to /dev/fb%d\n",
205 			fbl->fb_dev->fb_device->dv_xname, nfb);
206 	}
207 }
208 
209 int
210 fbopen(dev, flags, mode, p)
211 	dev_t dev;
212 	int flags, mode;
213 	struct proc *p;
214 {
215 	int unit;
216 	struct fbdevlist *fbl = &fblist;
217 
218 	unit = minor(dev);
219 	while (unit-- && fbl != NULL)
220 		fbl = fbl->fb_next;
221 	if (fbl == NULL || fbl->fb_dev == NULL)
222 		return (ENXIO);
223 
224 	return (fbl->fb_dev->fb_driver->fbd_open)(dev, flags, mode, p);
225 }
226 
227 int
228 fbclose(dev, flags, mode, p)
229 	dev_t dev;
230 	int flags, mode;
231 	struct proc *p;
232 {
233 	int unit;
234 	struct fbdevlist *fbl = &fblist;
235 
236 	unit = minor(dev);
237 	while (unit-- && fbl != NULL)
238 		fbl = fbl->fb_next;
239 	if (fbl == NULL || fbl->fb_dev == NULL)
240 		return (ENXIO);
241 
242 	return (fbl->fb_dev->fb_driver->fbd_close)(dev, flags, mode, p);
243 }
244 
245 int
246 fbioctl(dev, cmd, data, flags, p)
247 	dev_t dev;
248 	u_long cmd;
249 	caddr_t data;
250 	int flags;
251 	struct proc *p;
252 {
253 	int unit;
254 	struct fbdevlist *fbl = &fblist;
255 
256 	unit = minor(dev);
257 	while (unit-- && fbl != NULL)
258 		fbl = fbl->fb_next;
259 	if (fbl == NULL || fbl->fb_dev == NULL)
260 		return (ENXIO);
261 
262 	return (fbl->fb_dev->fb_driver->fbd_ioctl)(dev, cmd, data, flags, p);
263 }
264 
265 int
266 fbpoll(dev, events, p)
267 	dev_t dev;
268 	int events;
269 	struct proc *p;
270 {
271 	int unit;
272 	struct fbdevlist *fbl = &fblist;
273 
274 	unit = minor(dev);
275 	while (unit-- && fbl != NULL)
276 		fbl = fbl->fb_next;
277 	if (fbl == NULL || fbl->fb_dev == NULL)
278 		return (ENXIO);
279 
280 	return (fbl->fb_dev->fb_driver->fbd_poll)(dev, events, p);
281 }
282 
283 int
284 fbkqfilter(dev, kn)
285 	dev_t dev;
286 	struct knote *kn;
287 {
288 	int unit;
289 	struct fbdevlist *fbl = &fblist;
290 
291 	unit = minor(dev);
292 	while (unit-- && fbl != NULL)
293 		fbl = fbl->fb_next;
294 	if (fbl == NULL || fbl->fb_dev == NULL)
295 		return (ENXIO);
296 
297 	return (fbl->fb_dev->fb_driver->fbd_kqfilter)(dev, kn);
298 }
299 
300 paddr_t
301 fbmmap(dev, off, prot)
302 	dev_t dev;
303 	off_t off;
304 	int prot;
305 {
306 	int unit;
307 	struct fbdevlist *fbl = &fblist;
308 
309 	unit = minor(dev);
310 	while (unit-- && fbl != NULL)
311 		fbl = fbl->fb_next;
312 	if (fbl == NULL || fbl->fb_dev == NULL)
313 		return (ENXIO);
314 
315 	paddr_t (*map)(dev_t, off_t, int) = fbl->fb_dev->fb_driver->fbd_mmap;
316 
317 	if (map == NULL)
318 		return (-1);
319 	return (map(dev, off, prot));
320 }
321 
322 void
323 fb_setsize_obp(fb, depth, def_width, def_height, node)
324 	struct fbdevice *fb;
325 	int depth, def_width, def_height, node;
326 {
327 	fb->fb_type.fb_width = prom_getpropint(node, "width", def_width);
328 	fb->fb_type.fb_height = prom_getpropint(node, "height", def_height);
329 	fb->fb_linebytes = prom_getpropint(node, "linebytes",
330 				     (fb->fb_type.fb_width * depth) / 8);
331 }
332 
333 void
334 fb_setsize_eeprom(fb, depth, def_width, def_height)
335 	struct fbdevice *fb;
336 	int depth, def_width, def_height;
337 {
338 #if !defined(SUN4U)
339 	struct eeprom *eep = (struct eeprom *)eeprom_va;
340 
341 	if (!CPU_ISSUN4) {
342 		printf("fb_setsize_eeprom: not sun4\n");
343 		return;
344 	}
345 
346 	/* Set up some defaults. */
347 	fb->fb_type.fb_width = def_width;
348 	fb->fb_type.fb_height = def_height;
349 
350 	if (fb->fb_flags & FB_PFOUR) {
351 #if NPFOUR > 0
352 		fb_setsize_pfour(fb);
353 #endif
354 	} else if (eep != NULL) {
355 		switch (eep->eeScreenSize) {
356 		case EE_SCR_1152X900:
357 			fb->fb_type.fb_width = 1152;
358 			fb->fb_type.fb_height = 900;
359 			break;
360 
361 		case EE_SCR_1024X1024:
362 			fb->fb_type.fb_width = 1024;
363 			fb->fb_type.fb_height = 1024;
364 			break;
365 
366 		case EE_SCR_1600X1280:
367 			fb->fb_type.fb_width = 1600;
368 			fb->fb_type.fb_height = 1280;
369 			break;
370 
371 		case EE_SCR_1440X1440:
372 			fb->fb_type.fb_width = 1440;
373 			fb->fb_type.fb_height = 1440;
374 			break;
375 
376 		default:
377 			/*
378 			 * XXX: Do nothing, I guess.
379 			 * Should we print a warning about
380 			 * an unknown value? --thorpej
381 			 */
382 			break;
383 		}
384 	}
385 
386 	fb->fb_linebytes = (fb->fb_type.fb_width * depth) / 8;
387 #endif /* !SUN4U */
388 }
389 
390 
391 
392 #ifdef RASTERCONSOLE
393 #include <machine/kbd.h>
394 
395 static void fb_bell(int);
396 
397 static void
398 fb_bell(on)
399 	int on;
400 {
401 #if NKBD > 0
402 	kbd_bell(on);
403 #endif
404 }
405 
406 void
407 fbrcons_init(fb)
408 	struct fbdevice *fb;
409 {
410 	struct rconsole	*rc = &fb->fb_rcons;
411 	struct rasops_info *ri = &fb->fb_rinfo;
412 	int maxrow, maxcol;
413 #if !defined(RASTERCONS_FULLSCREEN)
414 	int *row, *col;
415 #endif
416 
417 	/* Set up what rasops needs to know about */
418 	bzero(ri, sizeof *ri);
419 	ri->ri_stride = fb->fb_linebytes;
420 	ri->ri_bits = (caddr_t)fb->fb_pixels;
421 	ri->ri_depth = fb->fb_type.fb_depth;
422 	ri->ri_width = fb->fb_type.fb_width;
423 	ri->ri_height = fb->fb_type.fb_height;
424 	maxrow = 5000;
425 	maxcol = 5000;
426 
427 #if !defined(RASTERCONS_FULLSCREEN)
428 #if !defined(SUN4U)
429 	if (CPU_ISSUN4) {
430 		struct eeprom *eep = (struct eeprom *)eeprom_va;
431 
432 		if (eep == NULL) {
433 			maxcol = 80;
434 			maxrow = 34;
435 		} else {
436 			maxcol = eep->eeTtyCols;
437 			maxrow = eep->eeTtyRows;
438 		}
439 	}
440 #endif /* !SUN4U */
441 	if (!CPU_ISSUN4) {
442 		char buf[6+1];	/* Enough for six digits */
443 		maxcol = (prom_getoption("screen-#columns", buf, sizeof buf) == 0)
444 			? strtoul(buf, NULL, 10)
445 			: 80;
446 
447 		maxrow = (prom_getoption("screen-#rows", buf, sizeof buf) != 0)
448 			? strtoul(buf, NULL, 10)
449 			: 34;
450 
451 	}
452 #endif /* !RASTERCONS_FULLSCREEN */
453 	/*
454 	 * - force monochrome output
455 	 * - eraserows() hack to clear the *entire* display
456 	 * - cursor is currently enabled
457 	 * - center output
458 	 */
459 	ri->ri_flg = RI_FULLCLEAR | RI_CURSOR | RI_CENTER;
460 
461 	/* Get operations set and connect to rcons */
462 	if (rasops_init(ri, maxrow, maxcol))
463 		panic("fbrcons_init: rasops_init failed!");
464 
465 	if (ri->ri_depth == 8) {
466 		int i;
467 		for (i = 0; i < 16; i++) {
468 
469 			/*
470 			 * Cmap entries are repeated four times in the
471 			 * 32 bit wide `devcmap' entries for optimization
472 			 * purposes; see rasops(9)
473 			 */
474 #define I_TO_DEVCMAP(i)	((i) | ((i)<<8) | ((i)<<16) | ((i)<<24))
475 
476 			/*
477 			 * Use existing colormap entries for black and white
478 			 */
479 			if ((i & 7) == WSCOL_BLACK) {
480 				ri->ri_devcmap[i] = I_TO_DEVCMAP(255);
481 				continue;
482 			}
483 
484 			if ((i & 7) == WSCOL_WHITE) {
485 				ri->ri_devcmap[i] = I_TO_DEVCMAP(0);
486 				continue;
487 			}
488 			/*
489 			 * Other entries refer to ANSI map, which for now
490 			 * is setup in bt_subr.c
491 			 */
492 			ri->ri_devcmap[i] = I_TO_DEVCMAP(i + 1);
493 #undef I_TO_DEVCMAP
494 		}
495 	}
496 
497 	rc->rc_row = rc->rc_col = 0;
498 #if !defined(RASTERCONS_FULLSCREEN)
499 	/* Determine addresses of prom emulator row and column */
500 	if (!CPU_ISSUN4 && !romgetcursoraddr(&row, &col)) {
501 		rc->rc_row = *row;
502 		rc->rc_col = *col;
503 	}
504 #endif
505 	ri->ri_crow = rc->rc_row;
506 	ri->ri_ccol = rc->rc_col;
507 
508 	rc->rc_ops = &ri->ri_ops;
509 	rc->rc_cookie = ri;
510 	rc->rc_bell = fb_bell;
511 	rc->rc_maxcol = ri->ri_cols;
512 	rc->rc_maxrow = ri->ri_rows;
513 	rc->rc_width = ri->ri_emuwidth;
514 	rc->rc_height = ri->ri_emuheight;
515 	rc->rc_deffgcolor = WSCOL_BLACK;
516 	rc->rc_defbgcolor = WSCOL_WHITE;
517 	rcons_init(rc, 0);
518 
519 	/* Hook up virtual console */
520 	v_putc = rcons_cnputc;
521 }
522 
523 int
524 fbrcons_rows()
525 {
526 	return ((fblist.fb_dev != NULL) ?
527 	    fblist.fb_dev->fb_rcons.rc_maxrow : 0);
528 }
529 
530 int
531 fbrcons_cols()
532 {
533 	return ((fblist.fb_dev != NULL) ?
534 	    fblist.fb_dev->fb_rcons.rc_maxcol : 0);
535 }
536 #endif /* RASTERCONSOLE */
537