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