Home | History | Annotate | Line # | Download | only in dev
cgeight.c revision 1.16
      1 /*	$NetBSD: cgeight.c,v 1.16 1998/03/21 20:11:30 pk 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 	bus_space_tag_t	sc_bustag;
    120 	bus_type_t	sc_btype;	/* phys address description */
    121 	bus_addr_t	sc_paddr;	/* for device mmap() */
    122 
    123 	volatile struct fbcontrol *sc_fbc;	/* Brooktree registers */
    124 	union bt_cmap	sc_cmap;	/* Brooktree color map */
    125 };
    126 
    127 /* autoconfiguration driver */
    128 static void	cgeightattach(struct device *, struct device *, void *);
    129 static int	cgeightmatch(struct device *, struct cfdata *, void *);
    130 #if defined(SUN4)
    131 static void	cgeightunblank __P((struct device *));
    132 #endif
    133 
    134 static int	cg8_pfour_probe __P((void *, void *));
    135 
    136 /* cdevsw prototypes */
    137 cdev_decl(cgeight);
    138 
    139 struct cfattach cgeight_ca = {
    140 	sizeof(struct cgeight_softc), cgeightmatch, cgeightattach
    141 };
    142 
    143 extern struct cfdriver cgeight_cd;
    144 
    145 #if defined(SUN4)
    146 /* frame buffer generic driver */
    147 static struct fbdriver cgeightfbdriver = {
    148 	cgeightunblank, cgeightopen, cgeightclose, cgeightioctl,
    149 	cgeightpoll, cgeightmmap
    150 };
    151 
    152 extern int fbnode;
    153 extern struct tty *fbconstty;
    154 
    155 static void cgeightloadcmap __P((struct cgeight_softc *, int, int));
    156 static int cgeight_get_video __P((struct cgeight_softc *));
    157 static void cgeight_set_video __P((struct cgeight_softc *, int));
    158 #endif
    159 
    160 /*
    161  * Match a cgeight.
    162  */
    163 int
    164 cgeightmatch(parent, cf, aux)
    165 	struct device *parent;
    166 	struct cfdata *cf;
    167 	void *aux;
    168 {
    169 	union obio_attach_args *uoba = aux;
    170 	struct obio4_attach_args *oba;
    171 
    172 	if (uoba->uoba_isobio4 == 0)
    173 		return (0);
    174 
    175 	oba = &uoba->uoba_oba4;
    176 	return (obio_bus_probe(oba->oba_bustag, oba->oba_paddr,
    177 			       0, 4, cg8_pfour_probe, NULL));
    178 
    179 }
    180 
    181 int
    182 cg8_pfour_probe(vaddr, arg)
    183 	void *vaddr;
    184 	void *arg;
    185 {
    186 
    187 	return (fb_pfour_id(vaddr) == PFOUR_ID_COLOR24);
    188 }
    189 
    190 /*
    191  * Attach a display.  We need to notice if it is the console, too.
    192  */
    193 void
    194 cgeightattach(parent, self, aux)
    195 	struct device *parent, *self;
    196 	void *aux;
    197 {
    198 #if defined(SUN4)
    199 	union obio_attach_args *uoba = aux;
    200 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
    201 	struct cgeight_softc *sc = (struct cgeight_softc *)self;
    202 	struct fbdevice *fb = &sc->sc_fb;
    203 	bus_space_handle_t bh;
    204 	volatile struct bt_regs *bt;
    205 	int ramsize, i, isconsole;
    206 
    207 	sc->sc_bustag = oba->oba_bustag;
    208 	sc->sc_btype = (bus_type_t)0;
    209 	sc->sc_paddr = (bus_addr_t)oba->oba_paddr;
    210 
    211 	/* Map the pfour register. */
    212 	if (obio_bus_map(oba->oba_bustag, oba->oba_paddr,
    213 			 0,
    214 			 sizeof(u_int32_t),
    215 			 BUS_SPACE_MAP_LINEAR,
    216 			 0, &bh) != 0) {
    217 		printf("%s: cannot map pfour register\n", self->dv_xname);
    218 		return;
    219 	}
    220 	fb->fb_pfour = (volatile u_int32_t *)bh;
    221 
    222 	fb->fb_driver = &cgeightfbdriver;
    223 	fb->fb_device = &sc->sc_dev;
    224 	fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
    225 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
    226 	fb->fb_flags |= FB_PFOUR;
    227 
    228 	ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
    229 
    230 	fb->fb_type.fb_depth = 24;
    231 	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
    232 
    233 	sc->sc_fb.fb_type.fb_cmsize = 256;
    234 	sc->sc_fb.fb_type.fb_size = ramsize;
    235 	printf(": cgeight/p4, %d x %d",
    236 		fb->fb_type.fb_width,
    237 		fb->fb_type.fb_height);
    238 
    239 	isconsole = 0;
    240 
    241 	if (CPU_ISSUN4) {
    242 		struct eeprom *eep = (struct eeprom *)eeprom_va;
    243 
    244 		/*
    245 		 * Assume this is the console if there's no eeprom info
    246 		 * to be found.
    247 		 */
    248 		if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
    249 			isconsole = (fbconstty != NULL);
    250 	}
    251 
    252 #if 0
    253 	/*
    254 	 * We don't do any of the console handling here.  Instead,
    255 	 * we let the bwtwo driver pick up the overlay plane and
    256 	 * use it instead.  Rconsole should have better performance
    257 	 * with the 1-bit depth.
    258 	 *      -- Jason R. Thorpe <thorpej (at) NetBSD.ORG>
    259 	 */
    260 
    261 	/*
    262 	 * When the ROM has mapped in a cgfour display, the address
    263 	 * maps only the video RAM, so in any case we have to map the
    264 	 * registers ourselves.  We only need the video RAM if we are
    265 	 * going to print characters via rconsole.
    266 	 */
    267 
    268 	if (isconsole) {
    269 		/* XXX this is kind of a waste */
    270 		fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
    271 					 PFOUR_COLOR_OFF_OVERLAY, ramsize);
    272 	}
    273 #endif
    274 
    275 	/* Map the Brooktree. */
    276 	if (obio_bus_map(oba->oba_bustag, oba->oba_paddr,
    277 			 PFOUR_COLOR_OFF_CMAP,
    278 			 sizeof(struct fbcontrol),
    279 			 BUS_SPACE_MAP_LINEAR,
    280 			 0, &bh) != 0) {
    281 		printf("%s: cannot map control registers\n", self->dv_xname);
    282 		return;
    283 	}
    284 	sc->sc_fbc = (volatile struct fbcontrol *)bh;
    285 
    286 #if 0	/* XXX thorpej ??? */
    287 	/* tell the enable plane to look at the mono image */
    288 	memset(ca->ca_ra.ra_vaddr, 0xff,
    289 	    sc->sc_fb.fb_type.fb_width * sc->sc_fb.fb_type.fb_height / 8);
    290 #endif
    291 
    292 	/* grab initial (current) color map */
    293 	bt = &sc->sc_fbc->fbc_dac;
    294 	bt->bt_addr = 0;
    295 	for (i = 0; i < 256 * 3 / 4; i++)
    296 		sc->sc_cmap.cm_chip[i] = bt->bt_cmap;
    297 
    298 	BT_INIT(bt, 0);
    299 
    300 #if 0	/* see above */
    301 	if (isconsole) {
    302 		printf(" (console)\n");
    303 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
    304 		fbrcons_init(fb);
    305 #endif
    306 	} else
    307 #endif /* 0 */
    308 		printf("\n");
    309 
    310 	/*
    311 	 * Even though we're not using rconsole, we'd still like
    312 	 * to notice if we're the console framebuffer.
    313 	 */
    314 	fb_attach(&sc->sc_fb, isconsole);
    315 #endif
    316 }
    317 
    318 int
    319 cgeightopen(dev, flags, mode, p)
    320 	dev_t dev;
    321 	int flags, mode;
    322 	struct proc *p;
    323 {
    324 	int unit = minor(dev);
    325 
    326 	if (unit >= cgeight_cd.cd_ndevs || cgeight_cd.cd_devs[unit] == NULL)
    327 		return (ENXIO);
    328 	return (0);
    329 }
    330 
    331 int
    332 cgeightclose(dev, flags, mode, p)
    333 	dev_t dev;
    334 	int flags, mode;
    335 	struct proc *p;
    336 {
    337 
    338 	return (0);
    339 }
    340 
    341 int
    342 cgeightioctl(dev, cmd, data, flags, p)
    343 	dev_t dev;
    344 	u_long cmd;
    345 	register caddr_t data;
    346 	int flags;
    347 	struct proc *p;
    348 {
    349 #if defined(SUN4)
    350 	register struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
    351 	register struct fbgattr *fba;
    352 	int error;
    353 
    354 	switch (cmd) {
    355 
    356 	case FBIOGTYPE:
    357 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    358 		break;
    359 
    360 	case FBIOGATTR:
    361 		fba = (struct fbgattr *)data;
    362 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    363 		fba->owner = 0;		/* XXX ??? */
    364 		fba->fbtype = sc->sc_fb.fb_type;
    365 		fba->sattr.flags = 0;
    366 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    367 		fba->sattr.dev_specific[0] = -1;
    368 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    369 		fba->emu_types[1] = -1;
    370 		break;
    371 
    372 	case FBIOGETCMAP:
    373 		return (bt_getcmap((struct fbcmap *)data, &sc->sc_cmap, 256));
    374 
    375 	case FBIOPUTCMAP:
    376 		/* copy to software map */
    377 #define p ((struct fbcmap *)data)
    378 		error = bt_putcmap(p, &sc->sc_cmap, 256);
    379 		if (error)
    380 			return (error);
    381 		/* now blast them into the chip */
    382 		/* XXX should use retrace interrupt */
    383 		cgeightloadcmap(sc, p->index, p->count);
    384 #undef p
    385 		break;
    386 
    387 	case FBIOGVIDEO:
    388 		*(int *)data = cgeight_get_video(sc);
    389 		break;
    390 
    391 	case FBIOSVIDEO:
    392 		cgeight_set_video(sc, *(int *)data);
    393 		break;
    394 
    395 	default:
    396 		return (ENOTTY);
    397 	}
    398 #endif
    399 	return (0);
    400 }
    401 
    402 int
    403 cgeightpoll(dev, events, p)
    404 	dev_t dev;
    405 	int events;
    406 	struct proc *p;
    407 {
    408 
    409 	return (seltrue(dev, events, p));
    410 }
    411 
    412 /*
    413  * Return the address that would map the given device at the given
    414  * offset, allowing for the given protection, or return -1 for error.
    415  *
    416  * The cg8 maps it's overlay plane at 0 for 128K, followed by the
    417  * enable plane for 128K, followed by the colour for as long as it
    418  * goes. Starting at 8MB, it maps the ramdac for NBPG, then the p4
    419  * register for NBPG, then the bootrom for 0x40000.
    420  */
    421 int
    422 cgeightmmap(dev, off, prot)
    423 	dev_t dev;
    424 	int off, prot;
    425 {
    426 	register struct cgeight_softc *sc = cgeight_cd.cd_devs[minor(dev)];
    427 	int poff;
    428 
    429 #define START_ENABLE	(128*1024)
    430 #define START_COLOR	((128*1024) + (128*1024))
    431 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
    432 			    sc->sc_fb.fb_type.fb_height * 3)
    433 #define END_COLOR	(START_COLOR + COLOR_SIZE)
    434 #define START_SPECIAL	0x800000
    435 #define PROMSIZE	0x40000
    436 #define NOOVERLAY	(0x04000000)
    437 
    438 	if (off & PGOFSET)
    439 		panic("cgeightmap");
    440 
    441 	if ((u_int)off >= NOOVERLAY) {
    442 		off -= NOOVERLAY;
    443 
    444 		/*
    445 		 * X11 maps a huge chunk of the frame buffer; far more than
    446 		 * there really is. We compensate by double-mapping the
    447 		 * first page for as many other pages as it wants
    448 		 */
    449 		while (off >= COLOR_SIZE)
    450 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
    451 
    452 		poff = off + PFOUR_COLOR_OFF_COLOR;
    453 	} else if ((u_int)off < START_ENABLE) {
    454 		/*
    455 		 * in overlay plane
    456 		 */
    457 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
    458 	} else if ((u_int)off < START_COLOR) {
    459 		/*
    460 		 * in enable plane
    461 		 */
    462 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
    463 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
    464 		/*
    465 		 * in colour plane
    466 		 */
    467 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
    468 	} else if ((u_int)off < START_SPECIAL) {
    469 		/*
    470 		 * hole
    471 		 */
    472 		poff = 0;	/* XXX */
    473 	} else if ((u_int)off == START_SPECIAL) {
    474 		/*
    475 		 * colour map (Brooktree)
    476 		 */
    477 		poff = PFOUR_COLOR_OFF_CMAP;
    478 	} else if ((u_int)off == START_SPECIAL + NBPG) {
    479 		/*
    480 		 * p4 register
    481 		 */
    482 		poff = 0;
    483 	} else if ((u_int)off > (START_SPECIAL + (NBPG * 2)) &&
    484 	    (u_int) off < (START_SPECIAL + (NBPG * 2) + PROMSIZE)) {
    485 		/*
    486 		 * rom
    487 		 */
    488 		poff = 0x8000 + (off - (START_SPECIAL + (NBPG * 2)));
    489 	} else
    490 		return (-1);
    491 
    492 	return (bus_space_mmap (sc->sc_bustag,
    493 				sc->sc_btype,
    494 				sc->sc_paddr + poff,
    495 				BUS_SPACE_MAP_LINEAR));
    496 }
    497 
    498 #if defined(SUN4)
    499 /*
    500  * Undo the effect of an FBIOSVIDEO that turns the video off.
    501  */
    502 static void
    503 cgeightunblank(dev)
    504 	struct device *dev;
    505 {
    506 
    507 	cgeight_set_video((struct cgeight_softc *)dev, 1);
    508 }
    509 
    510 static int
    511 cgeight_get_video(sc)
    512 	struct cgeight_softc *sc;
    513 {
    514 
    515 	return (fb_pfour_get_video(&sc->sc_fb));
    516 }
    517 
    518 static void
    519 cgeight_set_video(sc, enable)
    520 	struct cgeight_softc *sc;
    521 	int enable;
    522 {
    523 
    524 	fb_pfour_set_video(&sc->sc_fb, enable);
    525 }
    526 
    527 /*
    528  * Load a subset of the current (new) colormap into the Brooktree DAC.
    529  */
    530 static void
    531 cgeightloadcmap(sc, start, ncolors)
    532 	register struct cgeight_softc *sc;
    533 	register int start, ncolors;
    534 {
    535 	register volatile struct bt_regs *bt;
    536 	register u_int *ip;
    537 	register int count;
    538 
    539 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
    540 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
    541 	bt = &sc->sc_fbc->fbc_dac;
    542 	bt->bt_addr = BT_D4M4(start);
    543 	while (--count >= 0)
    544 		bt->bt_cmap = *ip++;
    545 }
    546 #endif
    547