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