Home | History | Annotate | Line # | Download | only in dev
cgtwo.c revision 1.5.2.2
      1 /*	$NetBSD: cgtwo.c,v 1.5.2.2 1996/02/16 00:24:28 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/buf.h>
     57 #include <sys/device.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/malloc.h>
     60 #include <sys/mman.h>
     61 #include <sys/tty.h>
     62 
     63 #include <vm/vm.h>
     64 
     65 #include <machine/fbio.h>
     66 #include <machine/autoconf.h>
     67 #include <machine/pmap.h>
     68 #include <machine/fbvar.h>
     69 #if defined(SUN4)
     70 #include <machine/eeprom.h>
     71 #endif
     72 
     73 #include <machine/cgtworeg.h>
     74 #include <sparc/dev/sbusvar.h>
     75 
     76 /* per-display variables */
     77 struct cgtwo_softc {
     78 	struct	device sc_dev;		/* base device */
     79 	struct	sbusdev sc_sd;		/* sbus device */
     80 	struct	fbdevice sc_fb;		/* frame buffer device */
     81 	caddr_t	sc_phys;		/* display RAM (phys addr) */
     82 	volatile struct cg2statusreg *sc_reg;	/* CG2 control registers */
     83 	volatile u_short *sc_cmap;
     84 #define sc_redmap(sc)	((sc)->sc_cmap)
     85 #define sc_greenmap(sc)	((sc)->sc_cmap + CG2_CMSIZE)
     86 #define sc_bluemap(sc)	((sc)->sc_cmap + 2 * CG2_CMSIZE)
     87 };
     88 
     89 /* autoconfiguration driver */
     90 static void	cgtwoattach(struct device *, struct device *, void *);
     91 static int	cgtwomatch(struct device *, void *, void *);
     92 int		cgtwoopen __P((dev_t, int, int, struct proc *));
     93 int		cgtwoclose __P((dev_t, int, int, struct proc *));
     94 int		cgtwoioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
     95 int		cgtwommap __P((dev_t, int, int));
     96 static void	cgtwounblank(struct device *);
     97 
     98 struct cfdriver cgtwocd = {
     99 	NULL, "cgtwo", cgtwomatch, cgtwoattach,
    100 	DV_DULL, sizeof(struct cgtwo_softc)
    101 };
    102 
    103 /* frame buffer generic driver */
    104 static struct fbdriver cgtwofbdriver = {
    105 	cgtwounblank, cgtwoopen, cgtwoclose, cgtwoioctl, cgtwommap
    106 };
    107 
    108 extern int fbnode;
    109 extern struct tty *fbconstty;
    110 extern int nullop();
    111 static int cgtwo_cnputc();
    112 
    113 static void cgtwoloadcmap __P((struct cgtwo_softc *, int, int));
    114 
    115 /*
    116  * Match a cgtwo.
    117  */
    118 int
    119 cgtwomatch(parent, vcf, aux)
    120 	struct device *parent;
    121 	void *vcf, *aux;
    122 {
    123 	struct cfdata *cf = vcf;
    124 	struct confargs *ca = aux;
    125 	struct romaux *ra = &ca->ca_ra;
    126 	int probe;
    127 	caddr_t tmp;
    128 
    129 	if (ca->ca_bustype != BUS_VME16)
    130 		return (0);
    131 
    132 	if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
    133 		return (0);
    134 
    135 #if defined(SUN4)
    136 	if (cputyp != CPU_SUN4 || cf->cf_unit != 0)
    137 		return (0);
    138 
    139 	/* XXX - Must do our own mapping at CG2_CTLREG_OFF */
    140 	bus_untmp();
    141 	tmp = (caddr_t)bus_tmp(ra->ra_paddr + CG2_CTLREG_OFF, ca->ca_bustype);
    142 	if (probeget(tmp, 2) != -1)
    143 		return 1;
    144 #endif
    145 	return 0;
    146 }
    147 
    148 /*
    149  * Attach a display.  We need to notice if it is the console, too.
    150  */
    151 void
    152 cgtwoattach(parent, self, args)
    153 	struct device *parent, *self;
    154 	void *args;
    155 {
    156 	register struct cgtwo_softc *sc = (struct cgtwo_softc *)self;
    157 	register struct confargs *ca = args;
    158 	register int node, i;
    159 	register struct cgtwo_all *p;
    160 	int isconsole;
    161 	int sbus = 1;
    162 	char *nam;
    163 
    164 	sc->sc_fb.fb_driver = &cgtwofbdriver;
    165 	sc->sc_fb.fb_device = &sc->sc_dev;
    166 	sc->sc_fb.fb_type.fb_type = FBTYPE_SUN2COLOR;
    167 	sc->sc_fb.fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
    168 
    169 	switch (ca->ca_bustype) {
    170 	case BUS_VME16:
    171 		sbus = node = 0;
    172 		nam = "cgtwo";
    173 		break;
    174 
    175 	default:
    176 		panic("cgtwoattach: impossible bustype");
    177 		/* NOTREACHED */
    178 	}
    179 
    180 	sc->sc_fb.fb_type.fb_depth = 8;
    181 	fb_setsize(&sc->sc_fb, sc->sc_fb.fb_type.fb_depth,
    182 	    1152, 900, node, ca->ca_bustype);
    183 
    184 	sc->sc_fb.fb_type.fb_cmsize = 256;
    185 	sc->sc_fb.fb_type.fb_size = roundup(CG2_MAPPED_SIZE, NBPG);
    186 	printf(": %s, %d x %d", nam,
    187 	    sc->sc_fb.fb_type.fb_width, sc->sc_fb.fb_type.fb_height);
    188 
    189 	/*
    190 	 * When the ROM has mapped in a cgtwo display, the address
    191 	 * maps only the video RAM, so in any case we have to map the
    192 	 * registers ourselves.  We only need the video RAM if we are
    193 	 * going to print characters via rconsole.
    194 	 */
    195 #if defined(SUN4)
    196 	if (cputyp == CPU_SUN4) {
    197 		struct eeprom *eep = (struct eeprom *)eeprom_va;
    198 		/*
    199 		 * Assume this is the console if there's no eeprom info
    200 		 * to be found.
    201 		 */
    202 		if (eep == NULL || eep->eeConsole == EE_CONS_COLOR)
    203 			isconsole = (fbconstty != NULL);
    204 		else
    205 			isconsole = 0;
    206 	}
    207 #endif
    208 #if defined(SUN4C) || defined(SUN4M)
    209 	if (cputyp == CPU_SUN4C || cputyp == CPU_SUN4M)
    210 		isconsole = node == fbnode && fbconstty != NULL;
    211 #endif
    212 	sc->sc_phys = (caddr_t)ca->ca_ra.ra_paddr;
    213 	if ((sc->sc_fb.fb_pixels = ca->ca_ra.ra_vaddr) == NULL && isconsole) {
    214 		/* this probably cannot happen, but what the heck */
    215 		sc->sc_fb.fb_pixels = mapiodev(sc->sc_phys + CG2_PIXMAP_OFF,
    216 					       CG2_PIXMAP_SIZE,
    217 					       ca->ca_bustype);
    218 	}
    219 #ifndef offsetof
    220 #define	offsetof(type, member)  ((size_t)(&((type *)0)->member))
    221 #endif
    222 
    223 	sc->sc_reg = (volatile struct cg2statusreg *)
    224 	    mapiodev((caddr_t)sc->sc_phys +
    225 		     CG2_ROPMEM_OFF + offsetof(struct cg2fb, status.reg),
    226 		     sizeof(struct cg2statusreg), ca->ca_bustype);
    227 
    228 	sc->sc_cmap = (volatile u_short *)
    229 	    mapiodev((caddr_t)sc->sc_phys +
    230 		     CG2_ROPMEM_OFF + offsetof(struct cg2fb, redmap[0]),
    231 		     3 * CG2_CMSIZE, ca->ca_bustype);
    232 
    233 	if (isconsole) {
    234 		printf(" (console)\n");
    235 #ifdef RASTERCONSOLE
    236 		fbrcons_init(&sc->sc_fb);
    237 #endif
    238 	} else
    239 		printf("\n");
    240 	if (sbus)
    241 		sbus_establish(&sc->sc_sd, &sc->sc_dev);
    242 	if (node == fbnode || cputyp == CPU_SUN4)
    243 		fb_attach(&sc->sc_fb, isconsole);
    244 }
    245 
    246 int
    247 cgtwoopen(dev, flags, mode, p)
    248 	dev_t dev;
    249 	int flags, mode;
    250 	struct proc *p;
    251 {
    252 	int unit = minor(dev);
    253 
    254 	if (unit >= cgtwocd.cd_ndevs || cgtwocd.cd_devs[unit] == NULL)
    255 		return (ENXIO);
    256 	return (0);
    257 }
    258 
    259 int
    260 cgtwoclose(dev, flags, mode, p)
    261 	dev_t dev;
    262 	int flags, mode;
    263 	struct proc *p;
    264 {
    265 
    266 	return (0);
    267 }
    268 
    269 int
    270 cgtwoioctl(dev, cmd, data, flags, p)
    271 	dev_t dev;
    272 	u_long cmd;
    273 	register caddr_t data;
    274 	int flags;
    275 	struct proc *p;
    276 {
    277 	register struct cgtwo_softc *sc = cgtwocd.cd_devs[minor(dev)];
    278 	register struct fbgattr *fba;
    279 	int error;
    280 
    281 	switch (cmd) {
    282 
    283 	case FBIOGTYPE:
    284 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    285 		break;
    286 
    287 	case FBIOGATTR:
    288 		fba = (struct fbgattr *)data;
    289 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    290 		fba->owner = 0;		/* XXX ??? */
    291 		fba->fbtype = sc->sc_fb.fb_type;
    292 		fba->sattr.flags = 0;
    293 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    294 		fba->sattr.dev_specific[0] = -1;
    295 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    296 		fba->emu_types[1] = -1;
    297 		break;
    298 
    299 	case FBIOGETCMAP:
    300 		return cgtwogetcmap(sc, data);
    301 
    302 	case FBIOPUTCMAP:
    303 		return cgtwoputcmap(sc, data);
    304 
    305 	case FBIOGVIDEO:
    306 		*(int *)data = sc->sc_reg->video_enab;
    307 		break;
    308 
    309 	case FBIOSVIDEO:
    310 		sc->sc_reg->video_enab = (*(int*)data) & 1;
    311 		break;
    312 
    313 	default:
    314 		return (ENOTTY);
    315 	}
    316 	return (0);
    317 }
    318 
    319 /*
    320  * Undo the effect of an FBIOSVIDEO that turns the video off.
    321  */
    322 static void
    323 cgtwounblank(dev)
    324 	struct device *dev;
    325 {
    326 	struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
    327 	sc->sc_reg->video_enab = 1;
    328 }
    329 
    330 /*
    331  */
    332 int
    333 cgtwogetcmap(sc, cmap)
    334 	register struct cgtwo_softc *sc;
    335 	register struct fbcmap *cmap;
    336 {
    337 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
    338 	int error, start, count, ecount;
    339 	register u_int i;
    340 	register volatile u_short *p;
    341 
    342 	start = cmap->index;
    343 	count = cmap->count;
    344 	ecount = start + count;
    345 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
    346 		return (EINVAL);
    347 
    348 	/* XXX - Wait for retrace? */
    349 
    350 	/* Copy hardware to local arrays. */
    351 	p = &sc_redmap(sc)[start];
    352 	for (i = start; i < ecount; i++)
    353 		red[i] = *p++;
    354 	p = &sc_greenmap(sc)[start];
    355 	for (i = start; i < ecount; i++)
    356 		green[i] = *p++;
    357 	p = &sc_bluemap(sc)[start];
    358 	for (i = start; i < ecount; i++)
    359 		blue[i] = *p++;
    360 
    361 	/* Copy local arrays to user space. */
    362 	if ((error = copyout(red + start, cmap->red, count)) != 0)
    363 		return (error);
    364 	if ((error = copyout(green + start, cmap->green, count)) != 0)
    365 		return (error);
    366 	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
    367 		return (error);
    368 
    369 	return (0);
    370 }
    371 
    372 /*
    373  */
    374 int
    375 cgtwoputcmap(sc, cmap)
    376 	register struct cgtwo_softc *sc;
    377 	register struct fbcmap *cmap;
    378 {
    379 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
    380 	int error, start, count, ecount;
    381 	register u_int i;
    382 	register volatile u_short *p;
    383 
    384 	start = cmap->index;
    385 	count = cmap->count;
    386 	ecount = start + count;
    387 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
    388 		return (EINVAL);
    389 
    390 	/* Copy from user space to local arrays. */
    391 	if ((error = copyin(cmap->red, red + start, count)) != 0)
    392 		return (error);
    393 	if ((error = copyin(cmap->green, green + start, count)) != 0)
    394 		return (error);
    395 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
    396 		return (error);
    397 
    398 	/* XXX - Wait for retrace? */
    399 
    400 	/* Copy from local arrays to hardware. */
    401 	p = &sc_redmap(sc)[start];
    402 	for (i = start; i < ecount; i++)
    403 		*p++ = red[i];
    404 	p = &sc_greenmap(sc)[start];
    405 	for (i = start; i < ecount; i++)
    406 		*p++ = green[i];
    407 	p = &sc_bluemap(sc)[start];
    408 	for (i = start; i < ecount; i++)
    409 		*p++ = blue[i];
    410 
    411 	return (0);
    412 }
    413 
    414 /*
    415  * Return the address that would map the given device at the given
    416  * offset, allowing for the given protection, or return -1 for error.
    417  */
    418 int
    419 cgtwommap(dev, off, prot)
    420 	dev_t dev;
    421 	int off, prot;
    422 {
    423 	register struct cgtwo_softc *sc = cgtwocd.cd_devs[minor(dev)];
    424 
    425 	if (off & PGOFSET)
    426 		panic("cgtwommap");
    427 
    428 	if ((unsigned)off >= sc->sc_fb.fb_type.fb_size)
    429 		return (-1);
    430 
    431 	return ((int)sc->sc_phys + off + PMAP_VME32 + PMAP_NC);
    432 }
    433