Home | History | Annotate | Line # | Download | only in dev
cgfour.c revision 1.3
      1 /*	$NetBSD: cgfour.c,v 1.3 1996/03/14 19:44:38 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Jason R. Thorpe.  All rights reserved.
      5  * Copyright (c) 1995 Theo de Raadt.  All rights reserved.
      6  * Copyright (c) 1992, 1993
      7  *	The Regents of the University of California.  All rights reserved.
      8  *
      9  * All advertising materials mentioning features or use of this software
     10  * must display the following acknowledgement:
     11  *	This product includes software developed by Theo de Raadt.
     12  *
     13  * This software was developed by the Computer Systems Engineering group
     14  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     15  * contributed to Berkeley.
     16  *
     17  * All advertising materials mentioning features or use of this software
     18  * must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Lawrence Berkeley Laboratory.
     21  *
     22  * Redistribution and use in source and binary forms, with or without
     23  * modification, are permitted provided that the following conditions
     24  * are met:
     25  * 1. Redistributions of source code must retain the above copyright
     26  *    notice, this list of conditions and the following disclaimer.
     27  * 2. Redistributions in binary form must reproduce the above copyright
     28  *    notice, this list of conditions and the following disclaimer in the
     29  *    documentation and/or other materials provided with the distribution.
     30  * 3. All advertising materials mentioning features or use of this software
     31  *    must display the following acknowledgement:
     32  *	This product includes software developed by the University of
     33  *	California, Berkeley and its contributors.
     34  * 4. Neither the name of the University nor the names of its contributors
     35  *    may be used to endorse or promote products derived from this software
     36  *    without specific prior written permission.
     37  *
     38  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     39  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     41  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     42  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     43  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     44  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     46  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     47  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     48  * SUCH DAMAGE.
     49  *
     50  *	from @(#)cgthree.c	8.2 (Berkeley) 10/30/93
     51  */
     52 
     53 /*
     54  * color display (cgfour) driver.
     55  *
     56  * Does not handle interrupts, even though they can occur.
     57  *
     58  * XXX should defer colormap updates to vertical retrace interrupts
     59  */
     60 
     61 #include <sys/param.h>
     62 #include <sys/buf.h>
     63 #include <sys/device.h>
     64 #include <sys/ioctl.h>
     65 #include <sys/malloc.h>
     66 #include <sys/mman.h>
     67 #include <sys/tty.h>
     68 
     69 #include <vm/vm.h>
     70 
     71 #include <machine/fbio.h>
     72 #include <machine/autoconf.h>
     73 #include <machine/pmap.h>
     74 #include <machine/fbvar.h>
     75 #include <machine/eeprom.h>
     76 
     77 #include <sparc/dev/btreg.h>
     78 #include <sparc/dev/btvar.h>
     79 #include <sparc/dev/pfourreg.h>
     80 #include <sparc/dev/dev_conf.h>
     81 
     82 /* per-display variables */
     83 struct cgfour_softc {
     84 	struct	device sc_dev;		/* base device */
     85 	struct	fbdevice sc_fb;		/* frame buffer device */
     86 	struct rom_reg	sc_phys;	/* display RAM (phys addr) */
     87 	volatile struct fbcontrol *sc_fbc;	/* Brooktree registers */
     88 	int	sc_bustype;		/* type of bus we live on */
     89 	union	bt_cmap sc_cmap;	/* Brooktree color map */
     90 };
     91 
     92 /* autoconfiguration driver */
     93 static void	cgfourattach __P((struct device *, struct device *, void *));
     94 static int	cgfourmatch __P((struct device *, void *, void *));
     95 int		cgfouropen __P((dev_t, int, int, struct proc *));
     96 int		cgfourclose __P((dev_t, int, int, struct proc *));
     97 int		cgfourioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
     98 int		cgfourmmap __P((dev_t, int, int));
     99 static void	cgfourunblank __P((struct device *));
    100 
    101 struct cfdriver cgfourcd = {
    102 	NULL, "cgfour", cgfourmatch, cgfourattach,
    103 	DV_DULL, sizeof(struct cgfour_softc)
    104 };
    105 
    106 /* frame buffer generic driver */
    107 static struct fbdriver cgfourfbdriver = {
    108 	cgfourunblank, cgfouropen, cgfourclose, cgfourioctl, cgfourmmap
    109 };
    110 
    111 extern int fbnode;
    112 extern struct tty *fbconstty;
    113 
    114 static void cgfourloadcmap __P((struct cgfour_softc *, int, int));
    115 static int cgfour_get_video __P((struct cgfour_softc *));
    116 static void cgfour_set_video __P((struct cgfour_softc *, int));
    117 
    118 /*
    119  * Match a cgfour.
    120  */
    121 int
    122 cgfourmatch(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_COLOR8P1) {
    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 cgfourattach(parent, self, args)
    174 	struct device *parent, *self;
    175 	void *args;
    176 {
    177 	register struct cgfour_softc *sc = (struct cgfour_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 = &cgfourfbdriver;
    185 	fb->fb_device = &sc->sc_dev;
    186 	fb->fb_type.fb_type = FBTYPE_SUN4COLOR;
    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 = 8;
    205 	fb_setsize(fb, fb->fb_type.fb_depth, 1152, 900, node, ca->ca_bustype);
    206 
    207 	fb->fb_type.fb_cmsize = 256;
    208 	fb->fb_type.fb_size = ramsize;
    209 	printf(": cgfour/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 	/* grab initial (current) color map */
    258 	bt = &sc->sc_fbc->fbc_dac;
    259 	bt->bt_addr = 0;
    260 	for (i = 0; i < 256 * 3 / 4; i++)
    261 		((char *)&sc->sc_cmap)[i] = bt->bt_cmap >> 24;
    262 
    263 	BT_INIT(bt, 24);
    264 
    265 #if 0	/* See above. */
    266 	if (isconsole) {
    267 		printf(" (console)\n");
    268 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
    269 		fbrcons_init(fb);
    270 #endif
    271 	} else
    272 #endif /* 0 */
    273 		printf("\n");
    274 
    275 	/*
    276 	 * Even though we're not using rconsole, we'd still like
    277 	 * to notice if we're the console framebuffer.
    278 	 */
    279 	fb_attach(fb, isconsole);
    280 }
    281 
    282 int
    283 cgfouropen(dev, flags, mode, p)
    284 	dev_t dev;
    285 	int flags, mode;
    286 	struct proc *p;
    287 {
    288 	int unit = minor(dev);
    289 
    290 	if (unit >= cgfourcd.cd_ndevs || cgfourcd.cd_devs[unit] == NULL)
    291 		return (ENXIO);
    292 	return (0);
    293 }
    294 
    295 int
    296 cgfourclose(dev, flags, mode, p)
    297 	dev_t dev;
    298 	int flags, mode;
    299 	struct proc *p;
    300 {
    301 
    302 	return (0);
    303 }
    304 
    305 int
    306 cgfourioctl(dev, cmd, data, flags, p)
    307 	dev_t dev;
    308 	u_long cmd;
    309 	register caddr_t data;
    310 	int flags;
    311 	struct proc *p;
    312 {
    313 	register struct cgfour_softc *sc = cgfourcd.cd_devs[minor(dev)];
    314 	register struct fbgattr *fba;
    315 	int error;
    316 
    317 	switch (cmd) {
    318 
    319 	case FBIOGTYPE:
    320 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    321 		break;
    322 
    323 	case FBIOGATTR:
    324 		fba = (struct fbgattr *)data;
    325 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    326 		fba->owner = 0;		/* XXX ??? */
    327 		fba->fbtype = sc->sc_fb.fb_type;
    328 		fba->sattr.flags = 0;
    329 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    330 		fba->sattr.dev_specific[0] = -1;
    331 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    332 		fba->emu_types[1] = -1;
    333 		break;
    334 
    335 	case FBIOGETCMAP:
    336 		return (bt_getcmap((struct fbcmap *)data, &sc->sc_cmap, 256));
    337 
    338 	case FBIOPUTCMAP:
    339 		/* copy to software map */
    340 #define p ((struct fbcmap *)data)
    341 		error = bt_putcmap(p, &sc->sc_cmap, 256);
    342 		if (error)
    343 			return (error);
    344 		/* now blast them into the chip */
    345 		/* XXX should use retrace interrupt */
    346 		cgfourloadcmap(sc, p->index, p->count);
    347 #undef p
    348 		break;
    349 
    350 	case FBIOGVIDEO:
    351 		*(int *)data = cgfour_get_video(sc);
    352 		break;
    353 
    354 	case FBIOSVIDEO:
    355 		cgfour_set_video(sc, *(int *)data);
    356 		break;
    357 
    358 	default:
    359 		return (ENOTTY);
    360 	}
    361 	return (0);
    362 }
    363 
    364 /*
    365  * Undo the effect of an FBIOSVIDEO that turns the video off.
    366  */
    367 static void
    368 cgfourunblank(dev)
    369 	struct device *dev;
    370 {
    371 
    372 	cgfour_set_video((struct cgfour_softc *)dev, 1);
    373 }
    374 
    375 /*
    376  * Load a subset of the current (new) colormap into the Brooktree DAC.
    377  */
    378 static void
    379 cgfourloadcmap(sc, start, ncolors)
    380 	register struct cgfour_softc *sc;
    381 	register int start, ncolors;
    382 {
    383 	register volatile struct bt_regs *bt;
    384 	register u_int *ip, i;
    385 	register int count;
    386 
    387 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
    388 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
    389 	bt = &sc->sc_fbc->fbc_dac;
    390 	bt->bt_addr = BT_D4M4(start) << 24;
    391 	while (--count >= 0) {
    392 		i = *ip++;
    393 		/* hardware that makes one want to pound boards with hammers */
    394 		bt->bt_cmap = i;
    395 		bt->bt_cmap = i << 8;
    396 		bt->bt_cmap = i << 16;
    397 		bt->bt_cmap = i << 24;
    398 	}
    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 cg4 maps it's overlay plane for 128K, followed by the enable
    406  * plane for 128K, followed by the colour plane (for as much colour
    407  * as their is.)
    408  *
    409  * As well, mapping at an offset of 0x04000000 causes the cg4 to map
    410  * only it's colour plane, at 0.
    411  */
    412 int
    413 cgfourmmap(dev, off, prot)
    414 	dev_t dev;
    415 	int off, prot;
    416 {
    417 	register struct cgfour_softc *sc = cgfourcd.cd_devs[minor(dev)];
    418 	int poff;
    419 
    420 #define START_ENABLE	(128*1024)
    421 #define START_COLOR	((128*1024) + (128*1024))
    422 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
    423 			    sc->sc_fb.fb_type.fb_height)
    424 #define END_COLOR	(START_COLOR + COLOR_SIZE)
    425 #define NOOVERLAY	(0x04000000)
    426 
    427 	if (off & PGOFSET)
    428 		panic("cgfourmap");
    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
    458 		return (-1);
    459 
    460 	return (REG2PHYS(&sc->sc_phys, poff, sc->sc_bustype) | PMAP_NC);
    461 }
    462 
    463 static int
    464 cgfour_get_video(sc)
    465 	struct cgfour_softc *sc;
    466 {
    467 
    468 	return (fb_pfour_get_video(&sc->sc_fb));
    469 }
    470 
    471 static void
    472 cgfour_set_video(sc, enable)
    473 	struct cgfour_softc *sc;
    474 	int enable;
    475 {
    476 
    477 	fb_pfour_set_video(&sc->sc_fb, enable);
    478 }
    479