Home | History | Annotate | Line # | Download | only in dev
cgtwo.c revision 1.12
      1 /*	$NetBSD: cgtwo.c,v 1.12 1996/03/17 02:00:54 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratory.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. All advertising materials mentioning features or use of this software
     25  *    must display the following acknowledgement:
     26  *	This product includes software developed by the University of
     27  *	California, Berkeley and its contributors.
     28  * 4. Neither the name of the University nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  *
     44  *	from: @(#)cgthree.c	8.2 (Berkeley) 10/30/93
     45  */
     46 
     47 /*
     48  * color display (cgtwo) driver.
     49  *
     50  * Does not handle interrupts, even though they can occur.
     51  *
     52  * XXX should defer colormap updates to vertical retrace interrupts
     53  */
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/buf.h>
     58 #include <sys/device.h>
     59 #include <sys/ioctl.h>
     60 #include <sys/malloc.h>
     61 #include <sys/mman.h>
     62 #include <sys/tty.h>
     63 
     64 #include <vm/vm.h>
     65 
     66 #include <machine/fbio.h>
     67 #include <machine/autoconf.h>
     68 #include <machine/pmap.h>
     69 #include <machine/fbvar.h>
     70 #if defined(SUN4)
     71 #include <machine/eeprom.h>
     72 #endif
     73 
     74 #include <machine/cgtworeg.h>
     75 
     76 #include <sparc/dev/dev_conf.h>
     77 
     78 /* per-display variables */
     79 struct cgtwo_softc {
     80 	struct	device sc_dev;		/* base device */
     81 	struct	fbdevice sc_fb;		/* frame buffer device */
     82 	struct rom_reg	sc_phys;	/* display RAM (phys addr) */
     83 	int	sc_bustype;		/* type of bus we live on */
     84 	volatile struct cg2statusreg *sc_reg;	/* CG2 control registers */
     85 	volatile u_short *sc_cmap;
     86 #define sc_redmap(sc)	((sc)->sc_cmap)
     87 #define sc_greenmap(sc)	((sc)->sc_cmap + CG2_CMSIZE)
     88 #define sc_bluemap(sc)	((sc)->sc_cmap + 2 * CG2_CMSIZE)
     89 };
     90 
     91 /* autoconfiguration driver */
     92 static void	cgtwoattach(struct device *, struct device *, void *);
     93 static int	cgtwomatch(struct device *, void *, void *);
     94 int		cgtwoopen __P((dev_t, int, int, struct proc *));
     95 int		cgtwoclose __P((dev_t, int, int, struct proc *));
     96 int		cgtwoioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
     97 int		cgtwommap __P((dev_t, int, int));
     98 static void	cgtwounblank __P((struct device *));
     99 int		cgtwogetcmap __P((struct cgtwo_softc *, struct fbcmap *));
    100 int		cgtwoputcmap __P((struct cgtwo_softc *, struct fbcmap *));
    101 
    102 struct cfattach cgtwo_ca = {
    103 	sizeof(struct cgtwo_softc), cgtwomatch, cgtwoattach
    104 };
    105 
    106 struct cfdriver cgtwo_cd = {
    107 	NULL, "cgtwo", DV_DULL
    108 };
    109 
    110 /* frame buffer generic driver */
    111 static struct fbdriver cgtwofbdriver = {
    112 	cgtwounblank, cgtwoopen, cgtwoclose, cgtwoioctl, cgtwommap
    113 };
    114 
    115 extern int fbnode;
    116 extern struct tty *fbconstty;
    117 
    118 /*
    119  * Match a cgtwo.
    120  */
    121 int
    122 cgtwomatch(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 	caddr_t tmp;
    130 
    131 	/*
    132 	 * Mask out invalid flags from the user.
    133 	 */
    134 	cf->cf_flags &= FB_USERMASK;
    135 
    136 	if (ca->ca_bustype != BUS_VME16)
    137 		return (0);
    138 
    139 	if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
    140 		return (0);
    141 
    142 #if defined(SUN4)
    143 	if (!CPU_ISSUN4 || cf->cf_unit != 0)
    144 		return (0);
    145 
    146 	/* XXX - Must do our own mapping at CG2_CTLREG_OFF */
    147 	bus_untmp();
    148 	tmp = (caddr_t)bus_tmp(ra->ra_paddr + CG2_CTLREG_OFF, ca->ca_bustype);
    149 	if (probeget(tmp, 2) != -1)
    150 		return 1;
    151 #endif
    152 	return 0;
    153 }
    154 
    155 /*
    156  * Attach a display.  We need to notice if it is the console, too.
    157  */
    158 void
    159 cgtwoattach(parent, self, args)
    160 	struct device *parent, *self;
    161 	void *args;
    162 {
    163 	register struct cgtwo_softc *sc = (struct cgtwo_softc *)self;
    164 	register struct confargs *ca = args;
    165 	register int node = 0;
    166 	int isconsole = 0;
    167 	char *nam = NULL;
    168 
    169 	sc->sc_fb.fb_driver = &cgtwofbdriver;
    170 	sc->sc_fb.fb_device = &sc->sc_dev;
    171 	sc->sc_fb.fb_type.fb_type = FBTYPE_SUN2COLOR;
    172 	sc->sc_fb.fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
    173 
    174 	switch (ca->ca_bustype) {
    175 	case BUS_VME16:
    176 		node = 0;
    177 		nam = "cgtwo";
    178 		break;
    179 
    180 	default:
    181 		panic("cgtwoattach: impossible bustype");
    182 		/* NOTREACHED */
    183 	}
    184 
    185 	sc->sc_fb.fb_type.fb_depth = 8;
    186 	fb_setsize(&sc->sc_fb, sc->sc_fb.fb_type.fb_depth,
    187 	    1152, 900, node, ca->ca_bustype);
    188 
    189 	sc->sc_fb.fb_type.fb_cmsize = 256;
    190 	sc->sc_fb.fb_type.fb_size = roundup(CG2_MAPPED_SIZE, NBPG);
    191 	printf(": %s, %d x %d", nam,
    192 	    sc->sc_fb.fb_type.fb_width, sc->sc_fb.fb_type.fb_height);
    193 
    194 	/*
    195 	 * When the ROM has mapped in a cgtwo display, the address
    196 	 * maps only the video RAM, so in any case we have to map the
    197 	 * registers ourselves.  We only need the video RAM if we are
    198 	 * going to print characters via rconsole.
    199 	 */
    200 #if defined(SUN4)
    201 	if (CPU_ISSUN4) {
    202 		struct eeprom *eep = (struct eeprom *)eeprom_va;
    203 		/*
    204 		 * Assume this is the console if there's no eeprom info
    205 		 * to be found.
    206 		 */
    207 		if (eep == NULL || eep->eeConsole == EE_CONS_COLOR)
    208 			isconsole = (fbconstty != NULL);
    209 		else
    210 			isconsole = 0;
    211 	}
    212 #endif
    213 	sc->sc_phys = ca->ca_ra.ra_reg[0];
    214 	sc->sc_bustype = ca->ca_bustype;
    215 
    216 	if ((sc->sc_fb.fb_pixels = ca->ca_ra.ra_vaddr) == NULL && isconsole) {
    217 		/* this probably cannot happen, but what the heck */
    218 		sc->sc_fb.fb_pixels = mapiodev(ca->ca_ra.ra_reg, CG2_PIXMAP_OFF,
    219 					       CG2_PIXMAP_SIZE,
    220 					       ca->ca_bustype);
    221 	}
    222 #ifndef offsetof
    223 #define	offsetof(type, member)  ((size_t)(&((type *)0)->member))
    224 #endif
    225 
    226 	sc->sc_reg = (volatile struct cg2statusreg *)
    227 	    mapiodev(ca->ca_ra.ra_reg,
    228 		     CG2_ROPMEM_OFF + offsetof(struct cg2fb, status.reg),
    229 		     sizeof(struct cg2statusreg), ca->ca_bustype);
    230 
    231 	sc->sc_cmap = (volatile u_short *)
    232 	    mapiodev(ca->ca_ra.ra_reg,
    233 		     CG2_ROPMEM_OFF + offsetof(struct cg2fb, redmap[0]),
    234 		     3 * CG2_CMSIZE, ca->ca_bustype);
    235 
    236 	if (isconsole) {
    237 		printf(" (console)\n");
    238 #ifdef RASTERCONSOLE
    239 		fbrcons_init(&sc->sc_fb);
    240 #endif
    241 	} else
    242 		printf("\n");
    243 
    244 	if (node == fbnode || CPU_ISSUN4)
    245 		fb_attach(&sc->sc_fb, isconsole);
    246 }
    247 
    248 int
    249 cgtwoopen(dev, flags, mode, p)
    250 	dev_t dev;
    251 	int flags, mode;
    252 	struct proc *p;
    253 {
    254 	int unit = minor(dev);
    255 
    256 	if (unit >= cgtwo_cd.cd_ndevs || cgtwo_cd.cd_devs[unit] == NULL)
    257 		return (ENXIO);
    258 	return (0);
    259 }
    260 
    261 int
    262 cgtwoclose(dev, flags, mode, p)
    263 	dev_t dev;
    264 	int flags, mode;
    265 	struct proc *p;
    266 {
    267 
    268 	return (0);
    269 }
    270 
    271 int
    272 cgtwoioctl(dev, cmd, data, flags, p)
    273 	dev_t dev;
    274 	u_long cmd;
    275 	register caddr_t data;
    276 	int flags;
    277 	struct proc *p;
    278 {
    279 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
    280 	register struct fbgattr *fba;
    281 
    282 	switch (cmd) {
    283 
    284 	case FBIOGTYPE:
    285 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    286 		break;
    287 
    288 	case FBIOGATTR:
    289 		fba = (struct fbgattr *)data;
    290 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    291 		fba->owner = 0;		/* XXX ??? */
    292 		fba->fbtype = sc->sc_fb.fb_type;
    293 		fba->sattr.flags = 0;
    294 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    295 		fba->sattr.dev_specific[0] = -1;
    296 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    297 		fba->emu_types[1] = -1;
    298 		break;
    299 
    300 	case FBIOGETCMAP:
    301 		return cgtwogetcmap(sc, (struct fbcmap *) data);
    302 
    303 	case FBIOPUTCMAP:
    304 		return cgtwoputcmap(sc, (struct fbcmap *) data);
    305 
    306 	case FBIOGVIDEO:
    307 		*(int *)data = sc->sc_reg->video_enab;
    308 		break;
    309 
    310 	case FBIOSVIDEO:
    311 		sc->sc_reg->video_enab = (*(int*)data) & 1;
    312 		break;
    313 
    314 	default:
    315 		return (ENOTTY);
    316 	}
    317 	return (0);
    318 }
    319 
    320 /*
    321  * Undo the effect of an FBIOSVIDEO that turns the video off.
    322  */
    323 static void
    324 cgtwounblank(dev)
    325 	struct device *dev;
    326 {
    327 	struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
    328 	sc->sc_reg->video_enab = 1;
    329 }
    330 
    331 /*
    332  */
    333 int
    334 cgtwogetcmap(sc, cmap)
    335 	register struct cgtwo_softc *sc;
    336 	register struct fbcmap *cmap;
    337 {
    338 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
    339 	int error, start, count, ecount;
    340 	register u_int i;
    341 	register volatile u_short *p;
    342 
    343 	start = cmap->index;
    344 	count = cmap->count;
    345 	ecount = start + count;
    346 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
    347 		return (EINVAL);
    348 
    349 	/* XXX - Wait for retrace? */
    350 
    351 	/* Copy hardware to local arrays. */
    352 	p = &sc_redmap(sc)[start];
    353 	for (i = start; i < ecount; i++)
    354 		red[i] = *p++;
    355 	p = &sc_greenmap(sc)[start];
    356 	for (i = start; i < ecount; i++)
    357 		green[i] = *p++;
    358 	p = &sc_bluemap(sc)[start];
    359 	for (i = start; i < ecount; i++)
    360 		blue[i] = *p++;
    361 
    362 	/* Copy local arrays to user space. */
    363 	if ((error = copyout(red + start, cmap->red, count)) != 0)
    364 		return (error);
    365 	if ((error = copyout(green + start, cmap->green, count)) != 0)
    366 		return (error);
    367 	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
    368 		return (error);
    369 
    370 	return (0);
    371 }
    372 
    373 /*
    374  */
    375 int
    376 cgtwoputcmap(sc, cmap)
    377 	register struct cgtwo_softc *sc;
    378 	register struct fbcmap *cmap;
    379 {
    380 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
    381 	int error, start, count, ecount;
    382 	register u_int i;
    383 	register volatile u_short *p;
    384 
    385 	start = cmap->index;
    386 	count = cmap->count;
    387 	ecount = start + count;
    388 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
    389 		return (EINVAL);
    390 
    391 	/* Copy from user space to local arrays. */
    392 	if ((error = copyin(cmap->red, red + start, count)) != 0)
    393 		return (error);
    394 	if ((error = copyin(cmap->green, green + start, count)) != 0)
    395 		return (error);
    396 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
    397 		return (error);
    398 
    399 	/* XXX - Wait for retrace? */
    400 
    401 	/* Copy from local arrays to hardware. */
    402 	p = &sc_redmap(sc)[start];
    403 	for (i = start; i < ecount; i++)
    404 		*p++ = red[i];
    405 	p = &sc_greenmap(sc)[start];
    406 	for (i = start; i < ecount; i++)
    407 		*p++ = green[i];
    408 	p = &sc_bluemap(sc)[start];
    409 	for (i = start; i < ecount; i++)
    410 		*p++ = blue[i];
    411 
    412 	return (0);
    413 }
    414 
    415 /*
    416  * Return the address that would map the given device at the given
    417  * offset, allowing for the given protection, or return -1 for error.
    418  */
    419 int
    420 cgtwommap(dev, off, prot)
    421 	dev_t dev;
    422 	int off, prot;
    423 {
    424 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
    425 
    426 	if (off & PGOFSET)
    427 		panic("cgtwommap");
    428 
    429 	if ((unsigned)off >= sc->sc_fb.fb_type.fb_size)
    430 		return (-1);
    431 
    432 	return (REG2PHYS(&sc->sc_phys, off, PMAP_VME32/*sc->sc_bustype*/) | PMAP_NC);
    433 }
    434