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