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