xref: /netbsd-src/sys/dev/sun/fb.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: fb.c,v 1.28 2008/04/05 16:46:15 cegger 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.28 2008/04/05 16:46:15 cegger 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, D_OTHER
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 			    device_xname(fb->fb_device),
172 			    device_xname(fblist.fb_dev->fb_device));
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 			    device_xname(fbl->fb_dev->fb_device), nfb);
180 			printf("%s: attached to /dev/fb0\n",
181 			    device_xname(fb->fb_device));
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 				aprint_error_dev(fb->fb_device, "no space to attach after /dev/fb%d\n",
196 					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 			device_xname(fbl->fb_dev->fb_device), 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 	void *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 static void fb_bell(int);
404 
405 static void
406 fb_bell(on)
407 	int on;
408 {
409 #if NKBD > 0
410 	kbd_bell(on);
411 #endif
412 }
413 
414 void
415 fbrcons_init(fb)
416 	struct fbdevice *fb;
417 {
418 	struct rconsole	*rc = &fb->fb_rcons;
419 	struct rasops_info *ri = &fb->fb_rinfo;
420 	int maxrow, maxcol;
421 #if !defined(RASTERCONS_FULLSCREEN)
422 	int *row, *col;
423 #endif
424 
425 	/* Set up what rasops needs to know about */
426 	bzero(ri, sizeof *ri);
427 	ri->ri_stride = fb->fb_linebytes;
428 	ri->ri_bits = (void *)fb->fb_pixels;
429 	ri->ri_depth = fb->fb_type.fb_depth;
430 	ri->ri_width = fb->fb_type.fb_width;
431 	ri->ri_height = fb->fb_type.fb_height;
432 	maxrow = 5000;
433 	maxcol = 5000;
434 
435 #if !defined(RASTERCONS_FULLSCREEN)
436 #if !defined(SUN4U)
437 	if (CPU_ISSUN4) {
438 		struct eeprom *eep = (struct eeprom *)eeprom_va;
439 
440 		if (eep == NULL) {
441 			maxcol = 80;
442 			maxrow = 34;
443 		} else {
444 			maxcol = eep->eeTtyCols;
445 			maxrow = eep->eeTtyRows;
446 		}
447 	}
448 #endif /* !SUN4U */
449 	if (!CPU_ISSUN4) {
450 		char buf[6+1];	/* Enough for six digits */
451 		maxcol = (prom_getoption("screen-#columns", buf, sizeof buf) == 0)
452 			? strtoul(buf, NULL, 10)
453 			: 80;
454 
455 		maxrow = (prom_getoption("screen-#rows", buf, sizeof buf) != 0)
456 			? strtoul(buf, NULL, 10)
457 			: 34;
458 
459 	}
460 #endif /* !RASTERCONS_FULLSCREEN */
461 	/*
462 	 * - force monochrome output
463 	 * - eraserows() hack to clear the *entire* display
464 	 * - cursor is currently enabled
465 	 * - center output
466 	 */
467 	ri->ri_flg = RI_FULLCLEAR | RI_CURSOR | RI_CENTER;
468 
469 	/* Get operations set and connect to rcons */
470 	if (rasops_init(ri, maxrow, maxcol))
471 		panic("fbrcons_init: rasops_init failed!");
472 
473 	if (ri->ri_depth == 8) {
474 		int i;
475 		for (i = 0; i < 16; i++) {
476 
477 			/*
478 			 * Cmap entries are repeated four times in the
479 			 * 32 bit wide `devcmap' entries for optimization
480 			 * purposes; see rasops(9)
481 			 */
482 #define I_TO_DEVCMAP(i)	((i) | ((i)<<8) | ((i)<<16) | ((i)<<24))
483 
484 			/*
485 			 * Use existing colormap entries for black and white
486 			 */
487 			if ((i & 7) == WSCOL_BLACK) {
488 				ri->ri_devcmap[i] = I_TO_DEVCMAP(255);
489 				continue;
490 			}
491 
492 			if ((i & 7) == WSCOL_WHITE) {
493 				ri->ri_devcmap[i] = I_TO_DEVCMAP(0);
494 				continue;
495 			}
496 			/*
497 			 * Other entries refer to ANSI map, which for now
498 			 * is setup in bt_subr.c
499 			 */
500 			ri->ri_devcmap[i] = I_TO_DEVCMAP(i + 1);
501 #undef I_TO_DEVCMAP
502 		}
503 	}
504 
505 	rc->rc_row = rc->rc_col = 0;
506 #if !defined(RASTERCONS_FULLSCREEN)
507 	/* Determine addresses of prom emulator row and column */
508 	if (!CPU_ISSUN4 && !romgetcursoraddr(&row, &col)) {
509 		rc->rc_row = *row;
510 		rc->rc_col = *col;
511 	}
512 #endif
513 	ri->ri_crow = rc->rc_row;
514 	ri->ri_ccol = rc->rc_col;
515 
516 	rc->rc_ops = &ri->ri_ops;
517 	rc->rc_cookie = ri;
518 	rc->rc_bell = fb_bell;
519 	rc->rc_maxcol = ri->ri_cols;
520 	rc->rc_maxrow = ri->ri_rows;
521 	rc->rc_width = ri->ri_emuwidth;
522 	rc->rc_height = ri->ri_emuheight;
523 	rc->rc_deffgcolor = WSCOL_BLACK;
524 	rc->rc_defbgcolor = WSCOL_WHITE;
525 	rcons_init(rc, 0);
526 
527 	/* Hook up virtual console */
528 	v_putc = rcons_cnputc;
529 }
530 
531 int
532 fbrcons_rows()
533 {
534 	return ((fblist.fb_dev != NULL) ?
535 	    fblist.fb_dev->fb_rcons.rc_maxrow : 0);
536 }
537 
538 int
539 fbrcons_cols()
540 {
541 	return ((fblist.fb_dev != NULL) ?
542 	    fblist.fb_dev->fb_rcons.rc_maxcol : 0);
543 }
544 #endif /* RASTERCONSOLE */
545