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