Home | History | Annotate | Line # | Download | only in sun
fb.c revision 1.3.2.3
      1 /*	$NetBSD: fb.c,v 1.3.2.3 2002/06/28 08:04:13 jdolecek 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).  This is gross; we should
     49  * just build cdevsw[] dynamically.
     50  */
     51 
     52 #include <sys/cdefs.h>
     53 __KERNEL_RCSID(0, "$NetBSD: fb.c,v 1.3.2.3 2002/06/28 08:04:13 jdolecek Exp $");
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/device.h>
     58 #include <sys/proc.h>
     59 #include <sys/conf.h>
     60 
     61 #include <machine/autoconf.h>
     62 #include <machine/kbd.h>
     63 #include <machine/conf.h>
     64 #include <machine/eeprom.h>
     65 #include <sparc/dev/cons.h>
     66 
     67 #include <dev/sun/fbio.h>
     68 #include <dev/sun/fbvar.h>
     69 
     70 #include "kbd.h"
     71 #include "pfour.h"
     72 
     73 static struct fbdevice *devfb;
     74 
     75 void
     76 fb_unblank()
     77 {
     78 
     79 	if (devfb)
     80 		(*devfb->fb_driver->fbd_unblank)(devfb->fb_device);
     81 }
     82 
     83 /*
     84  * Helper function for frame buffer devices. Decides whether
     85  * the device can be the console output device according to
     86  * PROM info. The result from this function may not be conclusive
     87  * on machines with old PROMs; in that case, drivers should consult
     88  * other sources of configuration information (e.g. EEPROM entries).
     89  */
     90 #if defined(SUN4U)
     91 /* Temporary special case for sun4u */
     92 int
     93 fb_is_console(node)
     94 	int node;
     95 {
     96 	extern int fbnode;
     97 	return (node == fbnode);
     98 }
     99 #else
    100 int
    101 fb_is_console(node)
    102 	int node;
    103 {
    104 	int fbnode;
    105 
    106 	switch (prom_version()) {
    107 	case PROM_OLDMON:
    108 		/* `node' is not valid; just check for any fb device */
    109 		return (prom_stdout() == PROMDEV_SCREEN);
    110 
    111 	case PROM_OBP_V0:
    112 		/*
    113 		 * First, check if prom_stdout() represents a frame buffer,
    114 		 * then match on the `fb' property on the root node, if any.
    115 		 */
    116 		if (prom_stdout() != PROMDEV_SCREEN)
    117 			return (0);
    118 
    119 		fbnode = PROM_getpropint(findroot(), "fb", 0);
    120 		return (fbnode == 0 || node == fbnode);
    121 
    122 	case PROM_OBP_V2:
    123 	case PROM_OBP_V3:
    124 	case PROM_OPENFIRM:
    125 		/* Just match the nodes */
    126 		return (node == prom_stdout_node);
    127 	}
    128 
    129 	return (0);
    130 }
    131 #endif /* SUN4U */
    132 
    133 void
    134 fb_attach(fb, isconsole)
    135 	struct fbdevice *fb;
    136 	int isconsole;
    137 {
    138 	static int no_replace, seen_force;
    139 
    140 	/*
    141 	 * We've already had a framebuffer forced into /dev/fb.  Don't
    142 	 * allow any more, even if this is the console.
    143 	 */
    144 	if (seen_force) {
    145 		if (devfb) {	/* sanity */
    146 			printf("%s: /dev/fb already full\n",
    147 				fb->fb_device->dv_xname);
    148 			return;
    149 		} else
    150 			seen_force = 0;
    151 	}
    152 
    153 	/*
    154 	 * Check to see if we're being forced into /dev/fb.
    155 	 */
    156 	if (fb->fb_flags & FB_FORCE) {
    157 		if (devfb)
    158 			printf("%s: forcefully replacing %s\n",
    159 				fb->fb_device->dv_xname,
    160 				devfb->fb_device->dv_xname);
    161 		devfb = fb;
    162 		seen_force = no_replace = 1;
    163 		goto attached;
    164 	}
    165 
    166 	/*
    167 	 * Check to see if we're the console.  If we are, then replace
    168 	 * any currently existing framebuffer.
    169 	 */
    170 	if (isconsole) {
    171 		if (devfb)
    172 			printf("%s: replacing %s\n", fb->fb_device->dv_xname,
    173 				devfb->fb_device->dv_xname);
    174 		devfb = fb;
    175 		no_replace = 1;
    176 		goto attached;
    177 	}
    178 
    179 	/*
    180 	 * For the final case, we check to see if we can replace an
    181 	 * existing framebuffer, if not, say so and return.
    182 	 */
    183 	if (no_replace) {
    184 		if (devfb) {	/* sanity */
    185 			printf("%s: /dev/fb already full\n",
    186 				fb->fb_device->dv_xname);
    187 			return;
    188 		} else
    189 			no_replace = 0;
    190 	}
    191 
    192 	if (devfb)
    193 		printf("%s: replacing %s\n", fb->fb_device->dv_xname,
    194 			devfb->fb_device->dv_xname);
    195 	devfb = fb;
    196 
    197  attached:
    198 	printf("%s: attached to /dev/fb\n", devfb->fb_device->dv_xname);
    199 }
    200 
    201 int
    202 fbopen(dev, flags, mode, p)
    203 	dev_t dev;
    204 	int flags, mode;
    205 	struct proc *p;
    206 {
    207 
    208 	if (devfb == NULL)
    209 		return (ENXIO);
    210 	return (devfb->fb_driver->fbd_open)(dev, flags, mode, p);
    211 }
    212 
    213 int
    214 fbclose(dev, flags, mode, p)
    215 	dev_t dev;
    216 	int flags, mode;
    217 	struct proc *p;
    218 {
    219 
    220 	return (devfb->fb_driver->fbd_close)(dev, flags, mode, p);
    221 }
    222 
    223 int
    224 fbioctl(dev, cmd, data, flags, p)
    225 	dev_t dev;
    226 	u_long cmd;
    227 	caddr_t data;
    228 	int flags;
    229 	struct proc *p;
    230 {
    231 
    232 	return (devfb->fb_driver->fbd_ioctl)(dev, cmd, data, flags, p);
    233 }
    234 
    235 int
    236 fbpoll(dev, events, p)
    237 	dev_t dev;
    238 	int events;
    239 	struct proc *p;
    240 {
    241 
    242 	return (devfb->fb_driver->fbd_poll)(dev, events, p);
    243 }
    244 
    245 int
    246 fbkqfilter(dev, kn)
    247 	dev_t dev;
    248 	struct knote *kn;
    249 {
    250 
    251 	return (devfb->fb_driver->fbd_kqfilter)(dev, kn);
    252 }
    253 
    254 paddr_t
    255 fbmmap(dev, off, prot)
    256 	dev_t dev;
    257 	off_t off;
    258 	int prot;
    259 {
    260 	paddr_t (*map)__P((dev_t, off_t, int)) = devfb->fb_driver->fbd_mmap;
    261 
    262 	if (map == NULL)
    263 		return (-1);
    264 	return (map(dev, off, prot));
    265 }
    266 
    267 void
    268 fb_setsize_obp(fb, depth, def_width, def_height, node)
    269 	struct fbdevice *fb;
    270 	int depth, def_width, def_height, node;
    271 {
    272 	fb->fb_type.fb_width = PROM_getpropint(node, "width", def_width);
    273 	fb->fb_type.fb_height = PROM_getpropint(node, "height", def_height);
    274 	fb->fb_linebytes = PROM_getpropint(node, "linebytes",
    275 				     (fb->fb_type.fb_width * depth) / 8);
    276 }
    277 
    278 void
    279 fb_setsize_eeprom(fb, depth, def_width, def_height)
    280 	struct fbdevice *fb;
    281 	int depth, def_width, def_height;
    282 {
    283 #if !defined(SUN4U)
    284 	struct eeprom *eep = (struct eeprom *)eeprom_va;
    285 
    286 	if (!CPU_ISSUN4) {
    287 		printf("fb_setsize_eeprom: not sun4\n");
    288 		return;
    289 	}
    290 
    291 	/* Set up some defaults. */
    292 	fb->fb_type.fb_width = def_width;
    293 	fb->fb_type.fb_height = def_height;
    294 
    295 	if (fb->fb_flags & FB_PFOUR) {
    296 #if NPFOUR > 0
    297 		fb_setsize_pfour(fb);
    298 #endif
    299 	} else if (eep != NULL) {
    300 		switch (eep->eeScreenSize) {
    301 		case EE_SCR_1152X900:
    302 			fb->fb_type.fb_width = 1152;
    303 			fb->fb_type.fb_height = 900;
    304 			break;
    305 
    306 		case EE_SCR_1024X1024:
    307 			fb->fb_type.fb_width = 1024;
    308 			fb->fb_type.fb_height = 1024;
    309 			break;
    310 
    311 		case EE_SCR_1600X1280:
    312 			fb->fb_type.fb_width = 1600;
    313 			fb->fb_type.fb_height = 1280;
    314 			break;
    315 
    316 		case EE_SCR_1440X1440:
    317 			fb->fb_type.fb_width = 1440;
    318 			fb->fb_type.fb_height = 1440;
    319 			break;
    320 
    321 		default:
    322 			/*
    323 			 * XXX: Do nothing, I guess.
    324 			 * Should we print a warning about
    325 			 * an unknown value? --thorpej
    326 			 */
    327 			break;
    328 		}
    329 	}
    330 
    331 	fb->fb_linebytes = (fb->fb_type.fb_width * depth) / 8;
    332 #endif /* !SUN4U */
    333 }
    334 
    335 
    336 
    337 #ifdef RASTERCONSOLE
    338 #include <machine/kbd.h>
    339 
    340 static void fb_bell __P((int));
    341 
    342 #if !defined(RASTERCONS_FULLSCREEN)
    343 static int a2int __P((char *, int));
    344 
    345 static int
    346 a2int(cp, deflt)
    347 	register char *cp;
    348 	register int deflt;
    349 {
    350 	register int i = 0;
    351 
    352 	if (*cp == '\0')
    353 		return (deflt);
    354 	while (*cp != '\0')
    355 		i = i * 10 + *cp++ - '0';
    356 	return (i);
    357 }
    358 #endif
    359 
    360 static void
    361 fb_bell(on)
    362 	int on;
    363 {
    364 #if NKBD > 0
    365 	(void)kbd_docmd(on?KBD_CMD_BELL:KBD_CMD_NOBELL, 0);
    366 #endif
    367 }
    368 
    369 void
    370 fbrcons_init(fb)
    371 	struct fbdevice *fb;
    372 {
    373 	struct rconsole	*rc = &fb->fb_rcons;
    374 	struct rasops_info *ri = &fb->fb_rinfo;
    375 	int maxrow, maxcol;
    376 #if !defined(RASTERCONS_FULLSCREEN)
    377 	int *row, *col;
    378 #endif
    379 
    380 	/* Set up what rasops needs to know about */
    381 	bzero(ri, sizeof *ri);
    382 	ri->ri_stride = fb->fb_linebytes;
    383 	ri->ri_bits = (caddr_t)fb->fb_pixels;
    384 	ri->ri_depth = fb->fb_type.fb_depth;
    385 	ri->ri_width = fb->fb_type.fb_width;
    386 	ri->ri_height = fb->fb_type.fb_height;
    387 	maxrow = 5000;
    388 	maxcol = 5000;
    389 
    390 #if !defined(RASTERCONS_FULLSCREEN)
    391 #if !defined(SUN4U)
    392 	if (CPU_ISSUN4) {
    393 		struct eeprom *eep = (struct eeprom *)eeprom_va;
    394 
    395 		if (eep == NULL) {
    396 			maxcol = 80;
    397 			maxrow = 34;
    398 		} else {
    399 			maxcol = eep->eeTtyCols;
    400 			maxrow = eep->eeTtyRows;
    401 		}
    402 	}
    403 #endif /* !SUN4U */
    404 	if (!CPU_ISSUN4) {
    405 		maxcol =
    406 		    a2int(PROM_getpropstring(optionsnode, "screen-#columns"), 80);
    407 		maxrow =
    408 		    a2int(PROM_getpropstring(optionsnode, "screen-#rows"), 34);
    409 	}
    410 #endif /* !RASTERCONS_FULLSCREEN */
    411 	/*
    412 	 * - force monochrome output
    413 	 * - eraserows() hack to clear the *entire* display
    414 	 * - cursor is currently enabled
    415 	 * - center output
    416 	 */
    417 	ri->ri_flg = RI_FULLCLEAR | RI_CURSOR | RI_CENTER;
    418 
    419 	/* Get operations set and connect to rcons */
    420 	if (rasops_init(ri, maxrow, maxcol))
    421 		panic("fbrcons_init: rasops_init failed!");
    422 
    423 	if (ri->ri_depth == 8) {
    424 		int i;
    425 		for (i = 0; i < 16; i++) {
    426 
    427 			/*
    428 			 * Cmap entries are repeated four times in the
    429 			 * 32 bit wide `devcmap' entries for optimization
    430 			 * purposes; see rasops(9)
    431 			 */
    432 #define I_TO_DEVCMAP(i)	((i) | ((i)<<8) | ((i)<<16) | ((i)<<24))
    433 
    434 			/*
    435 			 * Use existing colormap entries for black and white
    436 			 */
    437 			if ((i & 7) == WSCOL_BLACK) {
    438 				ri->ri_devcmap[i] = I_TO_DEVCMAP(255);
    439 				continue;
    440 			}
    441 
    442 			if ((i & 7) == WSCOL_WHITE) {
    443 				ri->ri_devcmap[i] = I_TO_DEVCMAP(0);
    444 				continue;
    445 			}
    446 			/*
    447 			 * Other entries refer to ANSI map, which for now
    448 			 * is setup in bt_subr.c
    449 			 */
    450 			ri->ri_devcmap[i] = I_TO_DEVCMAP(i + 1);
    451 #undef I_TO_DEVCMAP
    452 		}
    453 	}
    454 
    455 	rc->rc_row = rc->rc_col = 0;
    456 #if !defined(RASTERCONS_FULLSCREEN)
    457 	/* Determine addresses of prom emulator row and column */
    458 	if (!CPU_ISSUN4 && !romgetcursoraddr(&row, &col)) {
    459 		rc->rc_row = *row;
    460 		rc->rc_col = *col;
    461 	}
    462 #endif
    463 	ri->ri_crow = rc->rc_row;
    464 	ri->ri_ccol = rc->rc_col;
    465 
    466 	rc->rc_ops = &ri->ri_ops;
    467 	rc->rc_cookie = ri;
    468 	rc->rc_bell = fb_bell;
    469 	rc->rc_maxcol = ri->ri_cols;
    470 	rc->rc_maxrow = ri->ri_rows;
    471 	rc->rc_width = ri->ri_emuwidth;
    472 	rc->rc_height = ri->ri_emuheight;
    473 	rc->rc_deffgcolor = WSCOL_BLACK;
    474 	rc->rc_defbgcolor = WSCOL_WHITE;
    475 	rcons_init(rc, 0);
    476 
    477 	/* Hook up virtual console */
    478 	v_putc = rcons_cnputc;
    479 }
    480 
    481 int
    482 fbrcons_rows()
    483 {
    484 	return (devfb ? devfb->fb_rcons.rc_maxrow : 0);
    485 }
    486 
    487 int
    488 fbrcons_cols()
    489 {
    490 	return (devfb ? devfb->fb_rcons.rc_maxcol : 0);
    491 }
    492 #endif /* RASTERCONSOLE */
    493