Home | History | Annotate | Line # | Download | only in dev
cgfour.c revision 1.36
      1 /*	$NetBSD: cgfour.c,v 1.36 2003/08/24 17:31:59 uwe 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.36 2003/08/24 17:31:59 uwe Exp $");
    136 
    137 #include "opt_rcons.h"
    138 
    139 #include <sys/param.h>
    140 #include <sys/systm.h>
    141 #include <sys/buf.h>
    142 #include <sys/device.h>
    143 #include <sys/ioctl.h>
    144 #include <sys/malloc.h>
    145 #include <sys/mman.h>
    146 #include <sys/tty.h>
    147 #include <sys/conf.h>
    148 
    149 #include <machine/autoconf.h>
    150 #include <machine/eeprom.h>
    151 
    152 #include <dev/sun/fbio.h>
    153 #include <dev/sun/fbvar.h>
    154 #include <dev/sun/btreg.h>
    155 #include <dev/sun/btvar.h>
    156 #include <dev/sun/pfourreg.h>
    157 
    158 /* per-display variables */
    159 struct cgfour_softc {
    160 	struct device	sc_dev;		/* base device */
    161 	struct fbdevice	sc_fb;		/* frame buffer device */
    162 	bus_space_tag_t	sc_bustag;
    163 	bus_addr_t	sc_paddr;	/* phys address for device mmap() */
    164 
    165 	volatile struct fbcontrol *sc_fbc;	/* Brooktree registers */
    166 	union bt_cmap	sc_cmap;	/* Brooktree color map */
    167 };
    168 
    169 /* autoconfiguration driver */
    170 static void	cgfourattach __P((struct device *, struct device *, void *));
    171 static int	cgfourmatch __P((struct device *, struct cfdata *, void *));
    172 
    173 #if defined(SUN4)
    174 static void	cgfourunblank __P((struct device *));
    175 #endif
    176 
    177 static int	cg4_pfour_probe __P((void *, void *));
    178 
    179 CFATTACH_DECL(cgfour, sizeof(struct cgfour_softc),
    180     cgfourmatch, cgfourattach, NULL, NULL);
    181 
    182 extern struct cfdriver cgfour_cd;
    183 
    184 dev_type_open(cgfouropen);
    185 dev_type_ioctl(cgfourioctl);
    186 dev_type_mmap(cgfourmmap);
    187 
    188 const struct cdevsw cgfour_cdevsw = {
    189 	cgfouropen, nullclose, noread, nowrite, cgfourioctl,
    190 	nostop, notty, nopoll, cgfourmmap, nokqfilter,
    191 };
    192 
    193 #if defined(SUN4)
    194 /* frame buffer generic driver */
    195 static struct fbdriver cgfourfbdriver = {
    196 	cgfourunblank, cgfouropen, nullclose, cgfourioctl, nopoll,
    197 	cgfourmmap, nokqfilter
    198 };
    199 
    200 static void cgfourloadcmap __P((struct cgfour_softc *, int, int));
    201 static int cgfour_get_video __P((struct cgfour_softc *));
    202 static void cgfour_set_video __P((struct cgfour_softc *, int));
    203 #endif
    204 
    205 /*
    206  * Match a cgfour.
    207  */
    208 int
    209 cgfourmatch(parent, cf, aux)
    210 	struct device *parent;
    211 	struct cfdata *cf;
    212 	void *aux;
    213 {
    214 	union obio_attach_args *uoba = aux;
    215 	struct obio4_attach_args *oba;
    216 
    217 	if (uoba->uoba_isobio4 == 0)
    218 		return (0);
    219 
    220 	oba = &uoba->uoba_oba4;
    221 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
    222 				4,	/* probe size */
    223 				0,	/* offset */
    224 				0,	/* flags */
    225 				cg4_pfour_probe, NULL));
    226 }
    227 
    228 int
    229 cg4_pfour_probe(vaddr, arg)
    230 	void *vaddr;
    231 	void *arg;
    232 {
    233 
    234 	return (fb_pfour_id(vaddr) == PFOUR_ID_COLOR8P1);
    235 }
    236 
    237 /*
    238  * Attach a display.  We need to notice if it is the console, too.
    239  */
    240 void
    241 cgfourattach(parent, self, aux)
    242 	struct device *parent, *self;
    243 	void *aux;
    244 {
    245 #if defined(SUN4)
    246 	struct cgfour_softc *sc = (struct cgfour_softc *)self;
    247 	union obio_attach_args *uoba = aux;
    248 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
    249 	bus_space_handle_t bh;
    250 	volatile struct bt_regs *bt;
    251 	struct fbdevice *fb = &sc->sc_fb;
    252 	int ramsize, i, isconsole;
    253 
    254 	sc->sc_bustag = oba->oba_bustag;
    255 	sc->sc_paddr = (bus_addr_t)oba->oba_paddr;
    256 
    257 	/* Map the pfour register. */
    258 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
    259 			  sizeof(u_int32_t),
    260 			  BUS_SPACE_MAP_LINEAR,
    261 			  &bh) != 0) {
    262 		printf("%s: cannot map control registers\n", self->dv_xname);
    263 		return;
    264 	}
    265 	fb->fb_pfour = (volatile u_int32_t *)bh;
    266 
    267 	fb->fb_driver = &cgfourfbdriver;
    268 	fb->fb_device = &sc->sc_dev;
    269 	fb->fb_type.fb_type = FBTYPE_SUN4COLOR;
    270 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
    271 	fb->fb_flags |= FB_PFOUR;
    272 
    273 	ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
    274 
    275 	fb->fb_type.fb_depth = 8;
    276 	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
    277 
    278 	fb->fb_type.fb_cmsize = 256;
    279 	fb->fb_type.fb_size = ramsize;
    280 	printf(": cgfour/p4, %d x %d",
    281 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    282 
    283 	isconsole = 0;
    284 
    285 	if (CPU_ISSUN4) {
    286 		struct eeprom *eep = (struct eeprom *)eeprom_va;
    287 
    288 		/*
    289 		 * Assume this is the console if there's no eeprom info
    290 		 * to be found.
    291 		 */
    292 		if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
    293 			isconsole = fb_is_console(0);
    294 	}
    295 
    296 #if 0
    297 	/*
    298 	 * We don't do any of the console handling here.  Instead,
    299 	 * we let the bwtwo driver pick up the overlay plane and
    300 	 * use it instead.  Rconsole should have better performance
    301 	 * with the 1-bit depth.
    302 	 *	-- Jason R. Thorpe <thorpej (at) NetBSD.ORG>
    303 	 */
    304 
    305 	/*
    306 	 * When the ROM has mapped in a cgfour display, the address
    307 	 * maps only the video RAM, so in any case we have to map the
    308 	 * registers ourselves.  We only need the video RAM if we are
    309 	 * going to print characters via rconsole.
    310 	 */
    311 
    312 	if (isconsole) {
    313 		/* XXX this is kind of a waste */
    314 		fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
    315 					 PFOUR_COLOR_OFF_OVERLAY, ramsize);
    316 	}
    317 #endif
    318 
    319 	/* Map the Brooktree. */
    320 	if (bus_space_map(oba->oba_bustag,
    321 			  oba->oba_paddr + PFOUR_COLOR_OFF_CMAP,
    322 			  sizeof(struct fbcontrol),
    323 			  BUS_SPACE_MAP_LINEAR,
    324 			  &bh) != 0) {
    325 		printf("%s: cannot map control registers\n", self->dv_xname);
    326 		return;
    327 	}
    328 	sc->sc_fbc = (volatile struct fbcontrol *)bh;
    329 
    330 	/* grab initial (current) color map */
    331 	bt = &sc->sc_fbc->fbc_dac;
    332 	bt->bt_addr = 0;
    333 	for (i = 0; i < 256 * 3 / 4; i++)
    334 		((char *)&sc->sc_cmap)[i] = bt->bt_cmap >> 24;
    335 
    336 	BT_INIT(bt, 24);
    337 
    338 #if 0	/* See above. */
    339 	if (isconsole) {
    340 		printf(" (console)\n");
    341 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
    342 		fbrcons_init(fb);
    343 #endif
    344 	} else
    345 #endif /* 0 */
    346 		printf("\n");
    347 
    348 	/*
    349 	 * Even though we're not using rconsole, we'd still like
    350 	 * to notice if we're the console framebuffer.
    351 	 */
    352 	fb_attach(fb, isconsole);
    353 #endif
    354 }
    355 
    356 int
    357 cgfouropen(dev, flags, mode, p)
    358 	dev_t dev;
    359 	int flags, mode;
    360 	struct proc *p;
    361 {
    362 	int unit = minor(dev);
    363 
    364 	if (unit >= cgfour_cd.cd_ndevs || cgfour_cd.cd_devs[unit] == NULL)
    365 		return (ENXIO);
    366 	return (0);
    367 }
    368 
    369 int
    370 cgfourioctl(dev, cmd, data, flags, p)
    371 	dev_t dev;
    372 	u_long cmd;
    373 	caddr_t data;
    374 	int flags;
    375 	struct proc *p;
    376 {
    377 #if defined(SUN4)
    378 	struct cgfour_softc *sc = cgfour_cd.cd_devs[minor(dev)];
    379 	struct fbgattr *fba;
    380 	int error;
    381 
    382 	switch (cmd) {
    383 
    384 	case FBIOGTYPE:
    385 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    386 		break;
    387 
    388 	case FBIOGATTR:
    389 		fba = (struct fbgattr *)data;
    390 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    391 		fba->owner = 0;		/* XXX ??? */
    392 		fba->fbtype = sc->sc_fb.fb_type;
    393 		fba->sattr.flags = 0;
    394 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    395 		fba->sattr.dev_specific[0] = -1;
    396 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    397 		fba->emu_types[1] = -1;
    398 		break;
    399 
    400 	case FBIOGETCMAP:
    401 #define p ((struct fbcmap *)data)
    402 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
    403 
    404 	case FBIOPUTCMAP:
    405 		/* copy to software map */
    406 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
    407 		if (error)
    408 			return (error);
    409 		/* now blast them into the chip */
    410 		/* XXX should use retrace interrupt */
    411 		cgfourloadcmap(sc, p->index, p->count);
    412 #undef p
    413 		break;
    414 
    415 	case FBIOGVIDEO:
    416 		*(int *)data = cgfour_get_video(sc);
    417 		break;
    418 
    419 	case FBIOSVIDEO:
    420 		cgfour_set_video(sc, *(int *)data);
    421 		break;
    422 
    423 	default:
    424 		return (ENOTTY);
    425 	}
    426 #endif
    427 	return (0);
    428 }
    429 
    430 /*
    431  * Return the address that would map the given device at the given
    432  * offset, allowing for the given protection, or return -1 for error.
    433  *
    434  * the cg4 maps it's overlay plane for 128K, followed by the enable
    435  * plane for 128K, followed by the colour plane (for as much colour
    436  * as their is.)
    437  *
    438  * As well, mapping at an offset of 0x04000000 causes the cg4 to map
    439  * only it's colour plane, at 0.
    440  */
    441 paddr_t
    442 cgfourmmap(dev, off, prot)
    443 	dev_t dev;
    444 	off_t off;
    445 	int prot;
    446 {
    447 	struct cgfour_softc *sc = cgfour_cd.cd_devs[minor(dev)];
    448 	off_t poff;
    449 
    450 #define START_ENABLE	(128*1024)
    451 #define START_COLOR	((128*1024) + (128*1024))
    452 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
    453 			    sc->sc_fb.fb_type.fb_height)
    454 #define END_COLOR	(START_COLOR + COLOR_SIZE)
    455 #define NOOVERLAY	(0x04000000)
    456 
    457 	if (off & PGOFSET)
    458 		panic("cgfourmap");
    459 
    460 	if (off < 0)
    461 		return (-1);
    462 	else if ((u_int)off >= NOOVERLAY) {
    463 		off -= NOOVERLAY;
    464 
    465 		/*
    466 		 * X11 maps a huge chunk of the frame buffer; far more than
    467 		 * there really is. We compensate by double-mapping the
    468 		 * first page for as many other pages as it wants
    469 		 */
    470 		while ((u_int)off >= COLOR_SIZE)
    471 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
    472 
    473 		poff = off + PFOUR_COLOR_OFF_COLOR;
    474 	} else if ((u_int)off < START_ENABLE) {
    475 		/*
    476 		 * in overlay plane
    477 		 */
    478 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
    479 	} else if ((u_int)off < START_COLOR) {
    480 		/*
    481 		 * in enable plane
    482 		 */
    483 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
    484 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
    485 		/*
    486 		 * in colour plane
    487 		 */
    488 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
    489 	} else
    490 		return (-1);
    491 
    492 	return (bus_space_mmap(sc->sc_bustag,
    493 		sc->sc_paddr, poff,
    494 		prot, BUS_SPACE_MAP_LINEAR));
    495 }
    496 
    497 #if defined(SUN4)
    498 /*
    499  * Undo the effect of an FBIOSVIDEO that turns the video off.
    500  */
    501 static void
    502 cgfourunblank(dev)
    503 	struct device *dev;
    504 {
    505 
    506 	cgfour_set_video((struct cgfour_softc *)dev, 1);
    507 }
    508 
    509 static int
    510 cgfour_get_video(sc)
    511 	struct cgfour_softc *sc;
    512 {
    513 
    514 	return (fb_pfour_get_video(&sc->sc_fb));
    515 }
    516 
    517 static void
    518 cgfour_set_video(sc, enable)
    519 	struct cgfour_softc *sc;
    520 	int enable;
    521 {
    522 
    523 	fb_pfour_set_video(&sc->sc_fb, enable);
    524 }
    525 
    526 /*
    527  * Load a subset of the current (new) colormap into the Brooktree DAC.
    528  */
    529 static void
    530 cgfourloadcmap(sc, start, ncolors)
    531 	struct cgfour_softc *sc;
    532 	int start, ncolors;
    533 {
    534 	volatile struct bt_regs *bt;
    535 	u_int *ip, i;
    536 	int count;
    537 
    538 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
    539 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
    540 	bt = &sc->sc_fbc->fbc_dac;
    541 	bt->bt_addr = BT_D4M4(start) << 24;
    542 	while (--count >= 0) {
    543 		i = *ip++;
    544 		/* hardware that makes one want to pound boards with hammers */
    545 		bt->bt_cmap = i;
    546 		bt->bt_cmap = i << 8;
    547 		bt->bt_cmap = i << 16;
    548 		bt->bt_cmap = i << 24;
    549 	}
    550 }
    551 #endif
    552