Home | History | Annotate | Line # | Download | only in dev
cgtwo.c revision 1.5
      1 /*	$NetBSD: cgtwo.c,v 1.5 1995/10/08 01:39:15 pk 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 
    168 	switch (ca->ca_bustype) {
    169 	case BUS_VME16:
    170 		sbus = node = 0;
    171 		nam = "cgtwo";
    172 		break;
    173 
    174 	default:
    175 		panic("cgtwoattach: impossible bustype");
    176 		/* NOTREACHED */
    177 	}
    178 
    179 	sc->sc_fb.fb_type.fb_depth = 8;
    180 	fb_setsize(&sc->sc_fb, sc->sc_fb.fb_type.fb_depth,
    181 	    1152, 900, node, ca->ca_bustype);
    182 
    183 	sc->sc_fb.fb_type.fb_cmsize = 256;
    184 	sc->sc_fb.fb_type.fb_size = roundup(CG2_MAPPED_SIZE, NBPG);
    185 	printf(": %s, %d x %d", nam,
    186 	    sc->sc_fb.fb_type.fb_width, sc->sc_fb.fb_type.fb_height);
    187 
    188 	/*
    189 	 * When the ROM has mapped in a cgtwo display, the address
    190 	 * maps only the video RAM, so in any case we have to map the
    191 	 * registers ourselves.  We only need the video RAM if we are
    192 	 * going to print characters via rconsole.
    193 	 */
    194 #if defined(SUN4)
    195 	if (cputyp == CPU_SUN4) {
    196 		struct eeprom *eep = (struct eeprom *)eeprom_va;
    197 		/*
    198 		 * Assume this is the console if there's no eeprom info
    199 		 * to be found.
    200 		 */
    201 		if (eep == NULL || eep->eeConsole == EE_CONS_COLOR)
    202 			isconsole = (fbconstty != NULL);
    203 		else
    204 			isconsole = 0;
    205 	}
    206 #endif
    207 #if defined(SUN4C) || defined(SUN4M)
    208 	if (cputyp == CPU_SUN4C || cputyp == CPU_SUN4M)
    209 		isconsole = node == fbnode && fbconstty != NULL;
    210 #endif
    211 	sc->sc_phys = (caddr_t)ca->ca_ra.ra_paddr;
    212 	if ((sc->sc_fb.fb_pixels = ca->ca_ra.ra_vaddr) == NULL && isconsole) {
    213 		/* this probably cannot happen, but what the heck */
    214 		sc->sc_fb.fb_pixels = mapiodev(sc->sc_phys + CG2_PIXMAP_OFF,
    215 					       CG2_PIXMAP_SIZE,
    216 					       ca->ca_bustype);
    217 	}
    218 #ifndef offsetof
    219 #define	offsetof(type, member)  ((size_t)(&((type *)0)->member))
    220 #endif
    221 
    222 	sc->sc_reg = (volatile struct cg2statusreg *)
    223 	    mapiodev((caddr_t)sc->sc_phys +
    224 		     CG2_ROPMEM_OFF + offsetof(struct cg2fb, status.reg),
    225 		     sizeof(struct cg2statusreg), ca->ca_bustype);
    226 
    227 	sc->sc_cmap = (volatile u_short *)
    228 	    mapiodev((caddr_t)sc->sc_phys +
    229 		     CG2_ROPMEM_OFF + offsetof(struct cg2fb, redmap[0]),
    230 		     3 * CG2_CMSIZE, ca->ca_bustype);
    231 
    232 	if (isconsole) {
    233 		printf(" (console)\n");
    234 #ifdef RASTERCONSOLE
    235 		fbrcons_init(&sc->sc_fb);
    236 #endif
    237 	} else
    238 		printf("\n");
    239 	if (sbus)
    240 		sbus_establish(&sc->sc_sd, &sc->sc_dev);
    241 	if (node == fbnode)
    242 		fb_attach(&sc->sc_fb);
    243 }
    244 
    245 int
    246 cgtwoopen(dev, flags, mode, p)
    247 	dev_t dev;
    248 	int flags, mode;
    249 	struct proc *p;
    250 {
    251 	int unit = minor(dev);
    252 
    253 	if (unit >= cgtwocd.cd_ndevs || cgtwocd.cd_devs[unit] == NULL)
    254 		return (ENXIO);
    255 	return (0);
    256 }
    257 
    258 int
    259 cgtwoclose(dev, flags, mode, p)
    260 	dev_t dev;
    261 	int flags, mode;
    262 	struct proc *p;
    263 {
    264 
    265 	return (0);
    266 }
    267 
    268 int
    269 cgtwoioctl(dev, cmd, data, flags, p)
    270 	dev_t dev;
    271 	u_long cmd;
    272 	register caddr_t data;
    273 	int flags;
    274 	struct proc *p;
    275 {
    276 	register struct cgtwo_softc *sc = cgtwocd.cd_devs[minor(dev)];
    277 	register struct fbgattr *fba;
    278 	int error;
    279 
    280 	switch (cmd) {
    281 
    282 	case FBIOGTYPE:
    283 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    284 		break;
    285 
    286 	case FBIOGATTR:
    287 		fba = (struct fbgattr *)data;
    288 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    289 		fba->owner = 0;		/* XXX ??? */
    290 		fba->fbtype = sc->sc_fb.fb_type;
    291 		fba->sattr.flags = 0;
    292 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    293 		fba->sattr.dev_specific[0] = -1;
    294 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    295 		fba->emu_types[1] = -1;
    296 		break;
    297 
    298 	case FBIOGETCMAP:
    299 		return cgtwogetcmap(sc, data);
    300 
    301 	case FBIOPUTCMAP:
    302 		return cgtwoputcmap(sc, data);
    303 
    304 	case FBIOGVIDEO:
    305 		*(int *)data = sc->sc_reg->video_enab;
    306 		break;
    307 
    308 	case FBIOSVIDEO:
    309 		sc->sc_reg->video_enab = (*(int*)data) & 1;
    310 		break;
    311 
    312 	default:
    313 		return (ENOTTY);
    314 	}
    315 	return (0);
    316 }
    317 
    318 /*
    319  * Undo the effect of an FBIOSVIDEO that turns the video off.
    320  */
    321 static void
    322 cgtwounblank(dev)
    323 	struct device *dev;
    324 {
    325 	struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
    326 	sc->sc_reg->video_enab = 1;
    327 }
    328 
    329 /*
    330  */
    331 int
    332 cgtwogetcmap(sc, cmap)
    333 	register struct cgtwo_softc *sc;
    334 	register struct fbcmap *cmap;
    335 {
    336 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
    337 	int error, start, count, ecount;
    338 	register u_int i;
    339 	register volatile u_short *p;
    340 
    341 	start = cmap->index;
    342 	count = cmap->count;
    343 	ecount = start + count;
    344 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
    345 		return (EINVAL);
    346 
    347 	/* XXX - Wait for retrace? */
    348 
    349 	/* Copy hardware to local arrays. */
    350 	p = &sc_redmap(sc)[start];
    351 	for (i = start; i < ecount; i++)
    352 		red[i] = *p++;
    353 	p = &sc_greenmap(sc)[start];
    354 	for (i = start; i < ecount; i++)
    355 		green[i] = *p++;
    356 	p = &sc_bluemap(sc)[start];
    357 	for (i = start; i < ecount; i++)
    358 		blue[i] = *p++;
    359 
    360 	/* Copy local arrays to user space. */
    361 	if ((error = copyout(red + start, cmap->red, count)) != 0)
    362 		return (error);
    363 	if ((error = copyout(green + start, cmap->green, count)) != 0)
    364 		return (error);
    365 	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
    366 		return (error);
    367 
    368 	return (0);
    369 }
    370 
    371 /*
    372  */
    373 int
    374 cgtwoputcmap(sc, cmap)
    375 	register struct cgtwo_softc *sc;
    376 	register struct fbcmap *cmap;
    377 {
    378 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
    379 	int error, start, count, ecount;
    380 	register u_int i;
    381 	register volatile u_short *p;
    382 
    383 	start = cmap->index;
    384 	count = cmap->count;
    385 	ecount = start + count;
    386 	if (start >= CG2_CMSIZE || ecount > CG2_CMSIZE)
    387 		return (EINVAL);
    388 
    389 	/* Copy from user space to local arrays. */
    390 	if ((error = copyin(cmap->red, red + start, count)) != 0)
    391 		return (error);
    392 	if ((error = copyin(cmap->green, green + start, count)) != 0)
    393 		return (error);
    394 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
    395 		return (error);
    396 
    397 	/* XXX - Wait for retrace? */
    398 
    399 	/* Copy from local arrays to hardware. */
    400 	p = &sc_redmap(sc)[start];
    401 	for (i = start; i < ecount; i++)
    402 		*p++ = red[i];
    403 	p = &sc_greenmap(sc)[start];
    404 	for (i = start; i < ecount; i++)
    405 		*p++ = green[i];
    406 	p = &sc_bluemap(sc)[start];
    407 	for (i = start; i < ecount; i++)
    408 		*p++ = blue[i];
    409 
    410 	return (0);
    411 }
    412 
    413 /*
    414  * Return the address that would map the given device at the given
    415  * offset, allowing for the given protection, or return -1 for error.
    416  */
    417 int
    418 cgtwommap(dev, off, prot)
    419 	dev_t dev;
    420 	int off, prot;
    421 {
    422 	register struct cgtwo_softc *sc = cgtwocd.cd_devs[minor(dev)];
    423 
    424 	if (off & PGOFSET)
    425 		panic("cgtwommap");
    426 
    427 	if ((unsigned)off >= sc->sc_fb.fb_type.fb_size)
    428 		return (-1);
    429 
    430 	return ((int)sc->sc_phys + off + PMAP_VME16 + PMAP_NC);
    431 }
    432