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