Home | History | Annotate | Line # | Download | only in dev
cgeight.c revision 1.15
      1 /*	$NetBSD: cgeight.c,v 1.15 1998/01/12 20:23:41 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1995 Theo de Raadt
     41  * Copyright (c) 1992, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * This software was developed by the Computer Systems Engineering group
     45  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     46  * contributed to Berkeley.
     47  *
     48  * All advertising materials mentioning features or use of this software
     49  * must display the following acknowledgement:
     50  *	This product includes software developed by the University of
     51  *	California, Lawrence Berkeley Laboratory.
     52  *
     53  * Redistribution and use in source and binary forms, with or without
     54  * modification, are permitted provided that the following conditions
     55  * are met:
     56  * 1. Redistributions of source code must retain the above copyright
     57  *    notice, this list of conditions and the following disclaimer.
     58  * 2. Redistributions in binary form must reproduce the above copyright
     59  *    notice, this list of conditions and the following disclaimer in the
     60  *    documentation and/or other materials provided with the distribution.
     61  * 3. All advertising materials mentioning features or use of this software
     62  *    must display the following acknowledgement:
     63  *	This product includes software developed by the University of
     64  *	California, Berkeley and its contributors.
     65  * 4. Neither the name of the University nor the names of its contributors
     66  *    may be used to endorse or promote products derived from this software
     67  *    without specific prior written permission.
     68  *
     69  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     70  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     71  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     72  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     73  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     74  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     75  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     76  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     77  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     78  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     79  * SUCH DAMAGE.
     80  *
     81  *	from @(#)cgthree.c	8.2 (Berkeley) 10/30/93
     82  */
     83 
     84 /*
     85  * color display (cgeight) driver.
     86  *
     87  * Does not handle interrupts, even though they can occur.
     88  *
     89  * XXX should defer colormap updates to vertical retrace interrupts
     90  */
     91 
     92 #include <sys/param.h>
     93 #include <sys/systm.h>
     94 #include <sys/buf.h>
     95 #include <sys/device.h>
     96 #include <sys/ioctl.h>
     97 #include <sys/malloc.h>
     98 #include <sys/mman.h>
     99 #include <sys/tty.h>
    100 #include <sys/conf.h>
    101 
    102 #include <vm/vm.h>
    103 
    104 #include <machine/fbio.h>
    105 #include <machine/autoconf.h>
    106 #include <machine/pmap.h>
    107 #include <machine/fbvar.h>
    108 #include <machine/eeprom.h>
    109 #include <machine/conf.h>
    110 
    111 #include <sparc/dev/btreg.h>
    112 #include <sparc/dev/btvar.h>
    113 #include <sparc/dev/pfourreg.h>
    114 
    115 /* per-display variables */
    116 struct cgeight_softc {
    117 	struct	device sc_dev;		/* base device */
    118 	struct	fbdevice sc_fb;		/* frame buffer device */
    119 	struct rom_reg	sc_phys;	/* display RAM (phys addr) */
    120 	volatile struct fbcontrol *sc_fbc;	/* Brooktree registers */
    121 	int	sc_bustype;		/* type of bus we live on */
    122 	union	bt_cmap sc_cmap;	/* Brooktree color map */
    123 };
    124 
    125 /* autoconfiguration driver */
    126 static void	cgeightattach(struct device *, struct device *, void *);
    127 static int	cgeightmatch(struct device *, struct cfdata *, void *);
    128 #if defined(SUN4)
    129 static void	cgeightunblank __P((struct device *));
    130 #endif
    131 
    132 /* cdevsw prototypes */
    133 cdev_decl(cgeight);
    134 
    135 struct cfattach cgeight_ca = {
    136 	sizeof(struct cgeight_softc), cgeightmatch, cgeightattach
    137 };
    138 
    139 extern struct cfdriver cgeight_cd;
    140 
    141 #if defined(SUN4)
    142 /* frame buffer generic driver */
    143 static struct fbdriver cgeightfbdriver = {
    144 	cgeightunblank, cgeightopen, cgeightclose, cgeightioctl,
    145 	cgeightpoll, cgeightmmap
    146 };
    147 
    148 extern int fbnode;
    149 extern struct tty *fbconstty;
    150 
    151 static void cgeightloadcmap __P((struct cgeight_softc *, int, int));
    152 static int cgeight_get_video __P((struct cgeight_softc *));
    153 static void cgeight_set_video __P((struct cgeight_softc *, int));
    154 #endif
    155 
    156 /*
    157  * Match a cgeight.
    158  */
    159 int
    160 cgeightmatch(parent, cf, aux)
    161 	struct device *parent;
    162 	struct cfdata *cf;
    163 	void *aux;
    164 {
    165 	struct confargs *ca = aux;
    166 	struct romaux *ra = &ca->ca_ra;
    167 
    168 	if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
    169 		return (0);
    170 
    171 	/*
    172 	 * Mask out invalid flags from the user.
    173 	 */
    174 	cf->cf_flags &= FB_USERMASK;
    175 
    176 	/*
    177 	 * Only exists on a sun4.
    178 	 */
    179 	if (!CPU_ISSUN4)
    180 		return (0);
    181 
    182 	/*
    183 	 * Only exists on obio.
    184 	 */
    185 	if (ca->ca_bustype != BUS_OBIO)
    186 		return (0);
    187 
    188 	/*
    189 	 * Make sure there's hardware there.
    190 	 */
    191 	if (probeget(ra->ra_vaddr, 4) == -1)
    192 		return (0);
    193 
    194 #if defined(SUN4)
    195 	/*
    196 	 * Check the pfour register.
    197 	 */
    198 	if (fb_pfour_id(ra->ra_vaddr) == PFOUR_ID_COLOR24) {
    199 		cf->cf_flags |= FB_PFOUR;
    200 		return (1);
    201 	}
    202 #endif
    203 
    204 	return (0);
    205 }
    206 
    207 /*
    208  * Attach a display.  We need to notice if it is the console, too.
    209  */
    210 void
    211 cgeightattach(parent, self, args)
    212 	struct device *parent, *self;
    213 	void *args;
    214 {
    215 #if defined(SUN4)
    216 	register struct cgeight_softc *sc = (struct cgeight_softc *)self;
    217 	register struct confargs *ca = args;
    218 	register int node = 0, ramsize, i;
    219 	register volatile struct bt_regs *bt;
    220 	struct fbdevice *fb = &sc->sc_fb;
    221 	int isconsole;
    222 
    223 	fb->fb_driver = &cgeightfbdriver;
    224 	fb->fb_device = &sc->sc_dev;
    225 	fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
    226 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
    227 
    228 	/*
    229 	 * Only pfour cgfours, thank you...
    230 	 */
    231 	if ((ca->ca_bustype != BUS_OBIO) ||
    232 	    ((fb->fb_flags & FB_PFOUR) == 0)) {
    233 		printf("%s: ignoring; not a pfour\n", sc->sc_dev.dv_xname);
    234 		return;
    235 	}
    236 
    237 	/* Map the pfour register. */
    238 	fb->fb_pfour = (volatile u_int32_t *)
    239 	    mapiodev(ca->ca_ra.ra_reg, 0, sizeof(u_int32_t));
    240 
    241 	ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
    242 
    243 	fb->fb_type.fb_depth = 24;
    244 	fb_setsize(fb, fb->fb_type.fb_depth, 1152, 900, node, ca->ca_bustype);
    245 
    246 	sc->sc_fb.fb_type.fb_cmsize = 256;
    247 	sc->sc_fb.fb_type.fb_size = ramsize;
    248 	printf(": cgeight/p4, %d x %d", fb->fb_type.fb_width,
    249 	    fb->fb_type.fb_height);
    250 
    251 	isconsole = 0;
    252 
    253 	if (cputyp == CPU_SUN4) {
    254 		struct eeprom *eep = (struct eeprom *)eeprom_va;
    255 
    256 		/*
    257 		 * Assume this is the console if there's no eeprom info
    258 		 * to be found.
    259 		 */
    260 		if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
    261 			isconsole = (fbconstty != NULL);
    262 	}
    263 
    264 #if 0
    265 	/*
    266 	 * We don't do any of the console handling here.  Instead,
    267 	 * we let the bwtwo driver pick up the overlay plane and
    268 	 * use it instead.  Rconsole should have better performance
    269 	 * with the 1-bit depth.
    270 	 *      -- Jason R. Thorpe <thorpej (at) NetBSD.ORG>
    271 	 */
    272 
    273 	/*
    274 	 * When the ROM has mapped in a cgfour display, the address
    275 	 * maps only the video RAM, so in any case we have to map the
    276 	 * registers ourselves.  We only need the video RAM if we are
    277 	 * going to print characters via rconsole.
    278 	 */
    279 
    280 	if (isconsole) {
    281 		/* XXX this is kind of a waste */
    282 		fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
    283 					 PFOUR_COLOR_OFF_OVERLAY, ramsize);
    284 	}
    285 #endif
    286 
    287 	/* Map the Brooktree. */
    288 	sc->sc_fbc = (volatile struct fbcontrol *)
    289 	    mapiodev(ca->ca_ra.ra_reg,
    290 		     PFOUR_COLOR_OFF_CMAP, sizeof(struct fbcontrol));
    291 
    292 	sc->sc_phys = ca->ca_ra.ra_reg[0];
    293 	sc->sc_bustype = ca->ca_bustype;
    294 
    295 #if 0	/* XXX thorpej ??? */
    296 	/* tell the enable plane to look at the mono image */
    297 	memset(ca->ca_ra.ra_vaddr, 0xff,
    298 	    sc->sc_fb.fb_type.fb_width * sc->sc_fb.fb_type.fb_height / 8);
    299 #endif
    300 
    301 	/* grab initial (current) color map */
    302 	bt = &sc->sc_fbc->fbc_dac;
    303 	bt->bt_addr = 0;
    304 	for (i = 0; i < 256 * 3 / 4; i++)
    305 		sc->sc_cmap.cm_chip[i] = bt->bt_cmap;
    306 
    307 	BT_INIT(bt, 0);
    308 
    309 #if 0	/* see above */
    310 	if (isconsole) {
    311 		printf(" (console)\n");
    312 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
    313 		fbrcons_init(fb);
    314 #endif
    315 	} else
    316 #endif /* 0 */
    317 		printf("\n");
    318 
    319 	/*
    320 	 * Even though we're not using rconsole, we'd still like
    321 	 * to notice if we're the console framebuffer.
    322 	 */
    323 	fb_attach(&sc->sc_fb, isconsole);
    324 #endif
    325 }
    326 
    327 int
    328 cgeightopen(dev, flags, mode, p)
    329 	dev_t dev;
    330 	int flags, mode;
    331 	struct proc *p;
    332 {
    333 	int unit = minor(dev);
    334 
    335 	if (unit >= cgeight_cd.cd_ndevs || cgeight_cd.cd_devs[unit] == NULL)
    336 		return (ENXIO);
    337 	return (0);
    338 }
    339 
    340 int
    341 cgeightclose(dev, flags, mode, p)
    342 	dev_t dev;
    343 	int flags, mode;
    344 	struct proc *p;
    345 {
    346 
    347 	return (0);
    348 }
    349 
    350 int
    351 cgeightioctl(dev, cmd, data, flags, p)
    352 	dev_t dev;
    353 	u_long cmd;
    354 	register caddr_t data;
    355 	int flags;
    356 	struct proc *p;
    357 {
    358 #if defined(SUN4)
    359 	register struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
    360 	register struct fbgattr *fba;
    361 	int error;
    362 
    363 	switch (cmd) {
    364 
    365 	case FBIOGTYPE:
    366 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    367 		break;
    368 
    369 	case FBIOGATTR:
    370 		fba = (struct fbgattr *)data;
    371 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    372 		fba->owner = 0;		/* XXX ??? */
    373 		fba->fbtype = sc->sc_fb.fb_type;
    374 		fba->sattr.flags = 0;
    375 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    376 		fba->sattr.dev_specific[0] = -1;
    377 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    378 		fba->emu_types[1] = -1;
    379 		break;
    380 
    381 	case FBIOGETCMAP:
    382 		return (bt_getcmap((struct fbcmap *)data, &sc->sc_cmap, 256));
    383 
    384 	case FBIOPUTCMAP:
    385 		/* copy to software map */
    386 #define p ((struct fbcmap *)data)
    387 		error = bt_putcmap(p, &sc->sc_cmap, 256);
    388 		if (error)
    389 			return (error);
    390 		/* now blast them into the chip */
    391 		/* XXX should use retrace interrupt */
    392 		cgeightloadcmap(sc, p->index, p->count);
    393 #undef p
    394 		break;
    395 
    396 	case FBIOGVIDEO:
    397 		*(int *)data = cgeight_get_video(sc);
    398 		break;
    399 
    400 	case FBIOSVIDEO:
    401 		cgeight_set_video(sc, *(int *)data);
    402 		break;
    403 
    404 	default:
    405 		return (ENOTTY);
    406 	}
    407 #endif
    408 	return (0);
    409 }
    410 
    411 int
    412 cgeightpoll(dev, events, p)
    413 	dev_t dev;
    414 	int events;
    415 	struct proc *p;
    416 {
    417 
    418 	return (seltrue(dev, events, p));
    419 }
    420 
    421 /*
    422  * Return the address that would map the given device at the given
    423  * offset, allowing for the given protection, or return -1 for error.
    424  *
    425  * The cg8 maps it's overlay plane at 0 for 128K, followed by the
    426  * enable plane for 128K, followed by the colour for as long as it
    427  * goes. Starting at 8MB, it maps the ramdac for NBPG, then the p4
    428  * register for NBPG, then the bootrom for 0x40000.
    429  */
    430 int
    431 cgeightmmap(dev, off, prot)
    432 	dev_t dev;
    433 	int off, prot;
    434 {
    435 	register struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
    436 	int poff;
    437 
    438 #define START_ENABLE	(128*1024)
    439 #define START_COLOR	((128*1024) + (128*1024))
    440 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
    441 			    sc->sc_fb.fb_type.fb_height * 3)
    442 #define END_COLOR	(START_COLOR + COLOR_SIZE)
    443 #define START_SPECIAL	0x800000
    444 #define PROMSIZE	0x40000
    445 #define NOOVERLAY	(0x04000000)
    446 
    447 	if (off & PGOFSET)
    448 		panic("cgeightmap");
    449 
    450 	if ((u_int)off >= NOOVERLAY) {
    451 		off -= NOOVERLAY;
    452 
    453 		/*
    454 		 * X11 maps a huge chunk of the frame buffer; far more than
    455 		 * there really is. We compensate by double-mapping the
    456 		 * first page for as many other pages as it wants
    457 		 */
    458 		while (off >= COLOR_SIZE)
    459 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
    460 
    461 		poff = off + PFOUR_COLOR_OFF_COLOR;
    462 	} else if ((u_int)off < START_ENABLE) {
    463 		/*
    464 		 * in overlay plane
    465 		 */
    466 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
    467 	} else if ((u_int)off < START_COLOR) {
    468 		/*
    469 		 * in enable plane
    470 		 */
    471 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
    472 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
    473 		/*
    474 		 * in colour plane
    475 		 */
    476 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
    477 	} else if ((u_int)off < START_SPECIAL) {
    478 		/*
    479 		 * hole
    480 		 */
    481 		poff = 0;	/* XXX */
    482 	} else if ((u_int)off == START_SPECIAL) {
    483 		/*
    484 		 * colour map (Brooktree)
    485 		 */
    486 		poff = PFOUR_COLOR_OFF_CMAP;
    487 	} else if ((u_int)off == START_SPECIAL + NBPG) {
    488 		/*
    489 		 * p4 register
    490 		 */
    491 		poff = 0;
    492 	} else if ((u_int)off > (START_SPECIAL + (NBPG * 2)) &&
    493 	    (u_int) off < (START_SPECIAL + (NBPG * 2) + PROMSIZE)) {
    494 		/*
    495 		 * rom
    496 		 */
    497 		poff = 0x8000 + (off - (START_SPECIAL + (NBPG * 2)));
    498 	} else
    499 		return (-1);
    500 	/*
    501 	 * I turned on PMAP_NC here to disable the cache as I was
    502 	 * getting horribly broken behaviour with it on.
    503 	 */
    504 	return (REG2PHYS(&sc->sc_phys, poff) | PMAP_NC);
    505 }
    506 
    507 #if defined(SUN4)
    508 /*
    509  * Undo the effect of an FBIOSVIDEO that turns the video off.
    510  */
    511 static void
    512 cgeightunblank(dev)
    513 	struct device *dev;
    514 {
    515 
    516 	cgeight_set_video((struct cgeight_softc *)dev, 1);
    517 }
    518 
    519 static int
    520 cgeight_get_video(sc)
    521 	struct cgeight_softc *sc;
    522 {
    523 
    524 	return (fb_pfour_get_video(&sc->sc_fb));
    525 }
    526 
    527 static void
    528 cgeight_set_video(sc, enable)
    529 	struct cgeight_softc *sc;
    530 	int enable;
    531 {
    532 
    533 	fb_pfour_set_video(&sc->sc_fb, enable);
    534 }
    535 
    536 /*
    537  * Load a subset of the current (new) colormap into the Brooktree DAC.
    538  */
    539 static void
    540 cgeightloadcmap(sc, start, ncolors)
    541 	register struct cgeight_softc *sc;
    542 	register int start, ncolors;
    543 {
    544 	register volatile struct bt_regs *bt;
    545 	register u_int *ip;
    546 	register int count;
    547 
    548 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
    549 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
    550 	bt = &sc->sc_fbc->fbc_dac;
    551 	bt->bt_addr = BT_D4M4(start);
    552 	while (--count >= 0)
    553 		bt->bt_cmap = *ip++;
    554 }
    555 #endif
    556