xref: /netbsd-src/sys/dev/sun/fb.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: fb.c,v 1.24 2006/03/28 17:38:35 thorpej 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.24 2006/03/28 17:38:35 thorpej 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, l)
211 	dev_t dev;
212 	int flags, mode;
213 	struct lwp *l;
214 {
215 	int unit, nunit;
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 	nunit = device_unit(fbl->fb_dev->fb_device);
225 	return (fbl->fb_dev->fb_driver->fbd_open)(makedev(0, nunit), flags,
226 	    mode, l);
227 }
228 
229 int
230 fbclose(dev, flags, mode, l)
231 	dev_t dev;
232 	int flags, mode;
233 	struct lwp *l;
234 {
235 	int unit, nunit;
236 	struct fbdevlist *fbl = &fblist;
237 
238 	unit = minor(dev);
239 	while (unit-- && fbl != NULL)
240 		fbl = fbl->fb_next;
241 	if (fbl == NULL || fbl->fb_dev == NULL)
242 		return (ENXIO);
243 
244 	nunit = device_unit(fbl->fb_dev->fb_device);
245 	return (fbl->fb_dev->fb_driver->fbd_close)(makedev(0, nunit), flags,
246 	    mode, l);
247 }
248 
249 int
250 fbioctl(dev, cmd, data, flags, l)
251 	dev_t dev;
252 	u_long cmd;
253 	caddr_t data;
254 	int flags;
255 	struct lwp *l;
256 {
257 	int unit, nunit;
258 	struct fbdevlist *fbl = &fblist;
259 
260 	unit = minor(dev);
261 	while (unit-- && fbl != NULL)
262 		fbl = fbl->fb_next;
263 	if (fbl == NULL || fbl->fb_dev == NULL)
264 		return (ENXIO);
265 
266 	nunit = device_unit(fbl->fb_dev->fb_device);
267 	return (fbl->fb_dev->fb_driver->fbd_ioctl)(makedev(0, nunit), cmd,
268 	    data, flags, l);
269 }
270 
271 int
272 fbpoll(dev, events, l)
273 	dev_t dev;
274 	int events;
275 	struct lwp *l;
276 {
277 	int unit, nunit;
278 	struct fbdevlist *fbl = &fblist;
279 
280 	unit = minor(dev);
281 	while (unit-- && fbl != NULL)
282 		fbl = fbl->fb_next;
283 	if (fbl == NULL || fbl->fb_dev == NULL)
284 		return (ENXIO);
285 
286 	nunit = device_unit(fbl->fb_dev->fb_device);
287 	return (fbl->fb_dev->fb_driver->fbd_poll)(makedev(0, nunit), events,
288 	    l);
289 }
290 
291 int
292 fbkqfilter(dev, kn)
293 	dev_t dev;
294 	struct knote *kn;
295 {
296 	int unit, nunit;
297 	struct fbdevlist *fbl = &fblist;
298 
299 	unit = minor(dev);
300 	while (unit-- && fbl != NULL)
301 		fbl = fbl->fb_next;
302 	if (fbl == NULL || fbl->fb_dev == NULL)
303 		return (ENXIO);
304 
305 	nunit = device_unit(fbl->fb_dev->fb_device);
306 	return (fbl->fb_dev->fb_driver->fbd_kqfilter)(makedev(0, nunit), kn);
307 }
308 
309 paddr_t
310 fbmmap(dev, off, prot)
311 	dev_t dev;
312 	off_t off;
313 	int prot;
314 {
315 	int unit, nunit;
316 	struct fbdevlist *fbl = &fblist;
317 
318 	unit = minor(dev);
319 	while (unit-- && fbl != NULL)
320 		fbl = fbl->fb_next;
321 	if (fbl == NULL || fbl->fb_dev == NULL)
322 		return (ENXIO);
323 
324 	nunit = device_unit(fbl->fb_dev->fb_device);
325 	paddr_t (*map)(dev_t, off_t, int) = fbl->fb_dev->fb_driver->fbd_mmap;
326 
327 	if (map == NULL)
328 		return (-1);
329 	return (map(makedev(0, nunit), off, prot));
330 }
331 
332 void
333 fb_setsize_obp(fb, depth, def_width, def_height, node)
334 	struct fbdevice *fb;
335 	int depth, def_width, def_height, node;
336 {
337 	fb->fb_type.fb_width = prom_getpropint(node, "width", def_width);
338 	fb->fb_type.fb_height = prom_getpropint(node, "height", def_height);
339 	fb->fb_linebytes = prom_getpropint(node, "linebytes",
340 				     (fb->fb_type.fb_width * depth) / 8);
341 }
342 
343 void
344 fb_setsize_eeprom(fb, depth, def_width, def_height)
345 	struct fbdevice *fb;
346 	int depth, def_width, def_height;
347 {
348 #if !defined(SUN4U)
349 	struct eeprom *eep = (struct eeprom *)eeprom_va;
350 
351 	if (!CPU_ISSUN4) {
352 		printf("fb_setsize_eeprom: not sun4\n");
353 		return;
354 	}
355 
356 	/* Set up some defaults. */
357 	fb->fb_type.fb_width = def_width;
358 	fb->fb_type.fb_height = def_height;
359 
360 	if (fb->fb_flags & FB_PFOUR) {
361 #if NPFOUR > 0
362 		fb_setsize_pfour(fb);
363 #endif
364 	} else if (eep != NULL) {
365 		switch (eep->eeScreenSize) {
366 		case EE_SCR_1152X900:
367 			fb->fb_type.fb_width = 1152;
368 			fb->fb_type.fb_height = 900;
369 			break;
370 
371 		case EE_SCR_1024X1024:
372 			fb->fb_type.fb_width = 1024;
373 			fb->fb_type.fb_height = 1024;
374 			break;
375 
376 		case EE_SCR_1600X1280:
377 			fb->fb_type.fb_width = 1600;
378 			fb->fb_type.fb_height = 1280;
379 			break;
380 
381 		case EE_SCR_1440X1440:
382 			fb->fb_type.fb_width = 1440;
383 			fb->fb_type.fb_height = 1440;
384 			break;
385 
386 		default:
387 			/*
388 			 * XXX: Do nothing, I guess.
389 			 * Should we print a warning about
390 			 * an unknown value? --thorpej
391 			 */
392 			break;
393 		}
394 	}
395 
396 	fb->fb_linebytes = (fb->fb_type.fb_width * depth) / 8;
397 #endif /* !SUN4U */
398 }
399 
400 
401 
402 #ifdef RASTERCONSOLE
403 #include <machine/kbd.h>
404 
405 static void fb_bell(int);
406 
407 static void
408 fb_bell(on)
409 	int on;
410 {
411 #if NKBD > 0
412 	kbd_bell(on);
413 #endif
414 }
415 
416 void
417 fbrcons_init(fb)
418 	struct fbdevice *fb;
419 {
420 	struct rconsole	*rc = &fb->fb_rcons;
421 	struct rasops_info *ri = &fb->fb_rinfo;
422 	int maxrow, maxcol;
423 #if !defined(RASTERCONS_FULLSCREEN)
424 	int *row, *col;
425 #endif
426 
427 	/* Set up what rasops needs to know about */
428 	bzero(ri, sizeof *ri);
429 	ri->ri_stride = fb->fb_linebytes;
430 	ri->ri_bits = (caddr_t)fb->fb_pixels;
431 	ri->ri_depth = fb->fb_type.fb_depth;
432 	ri->ri_width = fb->fb_type.fb_width;
433 	ri->ri_height = fb->fb_type.fb_height;
434 	maxrow = 5000;
435 	maxcol = 5000;
436 
437 #if !defined(RASTERCONS_FULLSCREEN)
438 #if !defined(SUN4U)
439 	if (CPU_ISSUN4) {
440 		struct eeprom *eep = (struct eeprom *)eeprom_va;
441 
442 		if (eep == NULL) {
443 			maxcol = 80;
444 			maxrow = 34;
445 		} else {
446 			maxcol = eep->eeTtyCols;
447 			maxrow = eep->eeTtyRows;
448 		}
449 	}
450 #endif /* !SUN4U */
451 	if (!CPU_ISSUN4) {
452 		char buf[6+1];	/* Enough for six digits */
453 		maxcol = (prom_getoption("screen-#columns", buf, sizeof buf) == 0)
454 			? strtoul(buf, NULL, 10)
455 			: 80;
456 
457 		maxrow = (prom_getoption("screen-#rows", buf, sizeof buf) != 0)
458 			? strtoul(buf, NULL, 10)
459 			: 34;
460 
461 	}
462 #endif /* !RASTERCONS_FULLSCREEN */
463 	/*
464 	 * - force monochrome output
465 	 * - eraserows() hack to clear the *entire* display
466 	 * - cursor is currently enabled
467 	 * - center output
468 	 */
469 	ri->ri_flg = RI_FULLCLEAR | RI_CURSOR | RI_CENTER;
470 
471 	/* Get operations set and connect to rcons */
472 	if (rasops_init(ri, maxrow, maxcol))
473 		panic("fbrcons_init: rasops_init failed!");
474 
475 	if (ri->ri_depth == 8) {
476 		int i;
477 		for (i = 0; i < 16; i++) {
478 
479 			/*
480 			 * Cmap entries are repeated four times in the
481 			 * 32 bit wide `devcmap' entries for optimization
482 			 * purposes; see rasops(9)
483 			 */
484 #define I_TO_DEVCMAP(i)	((i) | ((i)<<8) | ((i)<<16) | ((i)<<24))
485 
486 			/*
487 			 * Use existing colormap entries for black and white
488 			 */
489 			if ((i & 7) == WSCOL_BLACK) {
490 				ri->ri_devcmap[i] = I_TO_DEVCMAP(255);
491 				continue;
492 			}
493 
494 			if ((i & 7) == WSCOL_WHITE) {
495 				ri->ri_devcmap[i] = I_TO_DEVCMAP(0);
496 				continue;
497 			}
498 			/*
499 			 * Other entries refer to ANSI map, which for now
500 			 * is setup in bt_subr.c
501 			 */
502 			ri->ri_devcmap[i] = I_TO_DEVCMAP(i + 1);
503 #undef I_TO_DEVCMAP
504 		}
505 	}
506 
507 	rc->rc_row = rc->rc_col = 0;
508 #if !defined(RASTERCONS_FULLSCREEN)
509 	/* Determine addresses of prom emulator row and column */
510 	if (!CPU_ISSUN4 && !romgetcursoraddr(&row, &col)) {
511 		rc->rc_row = *row;
512 		rc->rc_col = *col;
513 	}
514 #endif
515 	ri->ri_crow = rc->rc_row;
516 	ri->ri_ccol = rc->rc_col;
517 
518 	rc->rc_ops = &ri->ri_ops;
519 	rc->rc_cookie = ri;
520 	rc->rc_bell = fb_bell;
521 	rc->rc_maxcol = ri->ri_cols;
522 	rc->rc_maxrow = ri->ri_rows;
523 	rc->rc_width = ri->ri_emuwidth;
524 	rc->rc_height = ri->ri_emuheight;
525 	rc->rc_deffgcolor = WSCOL_BLACK;
526 	rc->rc_defbgcolor = WSCOL_WHITE;
527 	rcons_init(rc, 0);
528 
529 	/* Hook up virtual console */
530 	v_putc = rcons_cnputc;
531 }
532 
533 int
534 fbrcons_rows()
535 {
536 	return ((fblist.fb_dev != NULL) ?
537 	    fblist.fb_dev->fb_rcons.rc_maxrow : 0);
538 }
539 
540 int
541 fbrcons_cols()
542 {
543 	return ((fblist.fb_dev != NULL) ?
544 	    fblist.fb_dev->fb_rcons.rc_maxcol : 0);
545 }
546 #endif /* RASTERCONSOLE */
547