Home | History | Annotate | Line # | Download | only in dev
cgtwo.c revision 1.39
      1 /*	$NetBSD: cgtwo.c,v 1.39 2002/10/01 18:57:51 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 #include <sys/conf.h>
     64 
     65 #include <machine/autoconf.h>
     66 
     67 #include <dev/sun/fbio.h>
     68 #include <dev/sun/fbvar.h>
     69 
     70 #include <dev/vme/vmevar.h>
     71 
     72 #include <machine/eeprom.h>
     73 #include <machine/cgtworeg.h>
     74 
     75 
     76 /* per-display variables */
     77 struct cgtwo_softc {
     78 	struct	device sc_dev;		/* base device */
     79 	struct	fbdevice sc_fb;		/* frame buffer device */
     80 	vme_addr_t		sc_paddr;
     81 	vme_chipset_tag_t	sc_ct;
     82 	bus_space_tag_t		sc_bt;
     83 	volatile struct cg2statusreg *sc_reg;	/* CG2 control registers */
     84 	volatile u_short *sc_cmap;
     85 #define sc_redmap(sc)	((sc)->sc_cmap)
     86 #define sc_greenmap(sc)	((sc)->sc_cmap + CG2_CMSIZE)
     87 #define sc_bluemap(sc)	((sc)->sc_cmap + 2 * CG2_CMSIZE)
     88 };
     89 
     90 /* autoconfiguration driver */
     91 static void	cgtwoattach __P((struct device *, struct device *, void *));
     92 static int	cgtwomatch __P((struct device *, struct cfdata *, void *));
     93 static void	cgtwounblank __P((struct device *));
     94 int		cgtwogetcmap __P((struct cgtwo_softc *, struct fbcmap *));
     95 int		cgtwoputcmap __P((struct cgtwo_softc *, struct fbcmap *));
     96 
     97 CFATTACH_DECL(cgtwo, sizeof(struct cgtwo_softc),
     98     cgtwomatch, cgtwoattach, NULL, NULL)
     99 
    100 extern struct cfdriver cgtwo_cd;
    101 
    102 dev_type_open(cgtwoopen);
    103 dev_type_ioctl(cgtwoioctl);
    104 dev_type_mmap(cgtwommap);
    105 
    106 const struct cdevsw cgtwo_cdevsw = {
    107 	cgtwoopen, nullclose, noread, nowrite, cgtwoioctl,
    108 	nostop, notty, nopoll, cgtwommap,
    109 };
    110 
    111 /* frame buffer generic driver */
    112 static struct fbdriver cgtwofbdriver = {
    113 	cgtwounblank, cgtwoopen, nullclose, cgtwoioctl, nopoll, cgtwommap
    114 };
    115 
    116 /*
    117  * Match a cgtwo.
    118  */
    119 int
    120 cgtwomatch(parent, cf, aux)
    121 	struct device *parent;
    122 	struct cfdata *cf;
    123 	void *aux;
    124 {
    125 	struct vme_attach_args	*va = aux;
    126 	vme_chipset_tag_t	ct = va->va_vct;
    127 	vme_am_t		mod;
    128 
    129 	/*
    130 	 * Mask out invalid flags from the user.
    131 	 */
    132 	cf->cf_flags &= FB_USERMASK;
    133 
    134 	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
    135 	if (vme_probe(ct, va->r[0].offset + CG2_CTLREG_OFF, 2, mod, VME_D16,
    136 			  0, 0)) {
    137 		return (0);
    138 	}
    139 
    140 	return (1);
    141 }
    142 
    143 /*
    144  * Attach a display.  We need to notice if it is the console, too.
    145  */
    146 void
    147 cgtwoattach(parent, self, aux)
    148 	struct device *parent, *self;
    149 	void *aux;
    150 {
    151 	struct vme_attach_args	*va = aux;
    152 	vme_chipset_tag_t	ct = va->va_vct;
    153 	bus_space_tag_t		bt;
    154 	bus_space_handle_t	bh;
    155 	vme_am_t		mod;
    156 	vme_mapresc_t resc;
    157 	struct cgtwo_softc *sc = (struct cgtwo_softc *)self;
    158 	struct fbdevice *fb = &sc->sc_fb;
    159 	struct eeprom *eep = (struct eeprom *)eeprom_va;
    160 	int isconsole = 0;
    161 	char *nam = NULL;
    162 
    163 	sc->sc_ct = ct;
    164 	fb->fb_driver = &cgtwofbdriver;
    165 	fb->fb_device = &sc->sc_dev;
    166 	fb->fb_type.fb_type = FBTYPE_SUN2COLOR;
    167 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
    168 
    169 	fb->fb_type.fb_depth = 8;
    170 	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
    171 
    172 	fb->fb_type.fb_cmsize = 256;
    173 	fb->fb_type.fb_size = roundup(CG2_MAPPED_SIZE, NBPG);
    174 	printf(": %s, %d x %d", nam,
    175 	       fb->fb_type.fb_width, fb->fb_type.fb_height);
    176 
    177 	/*
    178 	 * When the ROM has mapped in a cgtwo display, the address
    179 	 * maps only the video RAM, so in any case we have to map the
    180 	 * registers ourselves.  We only need the video RAM if we are
    181 	 * going to print characters via rconsole.
    182 	 */
    183 	sc->sc_paddr = va->r[0].offset;
    184 	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
    185 
    186 	if (vme_space_map(ct, sc->sc_paddr + CG2_ROPMEM_OFF +
    187 				offsetof(struct cg2fb, status.reg),
    188 			sizeof(struct cg2statusreg), mod, VME_D16, 0,
    189 			&bt, &bh, &resc) != 0)
    190 		panic("cgtwo: vme_map status");
    191 	sc->sc_bt = bt;
    192 	sc->sc_reg = (volatile struct cg2statusreg *)bh; /* XXX */
    193 
    194 	if (vme_space_map(ct, sc->sc_paddr + CG2_ROPMEM_OFF +
    195 				offsetof(struct cg2fb, redmap[0]),
    196 			3 * CG2_CMSIZE, mod, VME_D16, 0,
    197 			&bt, &bh, &resc) != 0)
    198 		panic("cgtwo: vme_map cmap");
    199 	sc->sc_cmap = (volatile u_short *)bh; /* XXX */
    200 
    201 	/*
    202 	 * Assume this is the console if there's no eeprom info
    203 	 * to be found.
    204 	 */
    205 	if (eep == NULL || eep->eeConsole == EE_CONS_COLOR)
    206 		isconsole = fb_is_console(0);
    207 	else
    208 		isconsole = 0;
    209 
    210 	if (isconsole) {
    211 		if (vme_space_map(ct, sc->sc_paddr + CG2_PIXMAP_OFF,
    212 				CG2_PIXMAP_SIZE, mod, VME_D16, 0,
    213 				  &bt, &bh, &resc) != 0)
    214 			panic("cgtwo: vme_map pixels");
    215 
    216 		fb->fb_pixels = (caddr_t)bh; /* XXX */
    217 		printf(" (console)\n");
    218 #ifdef RASTERCONSOLE
    219 		fbrcons_init(fb);
    220 #endif
    221 	} else
    222 		printf("\n");
    223 
    224 	fb_attach(fb, isconsole);
    225 }
    226 
    227 int
    228 cgtwoopen(dev, flags, mode, p)
    229 	dev_t dev;
    230 	int flags, mode;
    231 	struct proc *p;
    232 {
    233 	int unit = minor(dev);
    234 
    235 	if (unit >= cgtwo_cd.cd_ndevs || cgtwo_cd.cd_devs[unit] == NULL)
    236 		return (ENXIO);
    237 	return (0);
    238 }
    239 
    240 int
    241 cgtwoioctl(dev, cmd, data, flags, p)
    242 	dev_t dev;
    243 	u_long cmd;
    244 	register caddr_t data;
    245 	int flags;
    246 	struct proc *p;
    247 {
    248 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
    249 	register struct fbgattr *fba;
    250 
    251 	switch (cmd) {
    252 
    253 	case FBIOGTYPE:
    254 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    255 		break;
    256 
    257 	case FBIOGATTR:
    258 		fba = (struct fbgattr *)data;
    259 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    260 		fba->owner = 0;		/* XXX ??? */
    261 		fba->fbtype = sc->sc_fb.fb_type;
    262 		fba->sattr.flags = 0;
    263 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    264 		fba->sattr.dev_specific[0] = -1;
    265 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    266 		fba->emu_types[1] = -1;
    267 		break;
    268 
    269 	case FBIOGETCMAP:
    270 		return cgtwogetcmap(sc, (struct fbcmap *) data);
    271 
    272 	case FBIOPUTCMAP:
    273 		return cgtwoputcmap(sc, (struct fbcmap *) data);
    274 
    275 	case FBIOGVIDEO:
    276 		*(int *)data = sc->sc_reg->video_enab;
    277 		break;
    278 
    279 	case FBIOSVIDEO:
    280 		sc->sc_reg->video_enab = (*(int*)data) & 1;
    281 		break;
    282 
    283 	default:
    284 		return (ENOTTY);
    285 	}
    286 	return (0);
    287 }
    288 
    289 /*
    290  * Undo the effect of an FBIOSVIDEO that turns the video off.
    291  */
    292 static void
    293 cgtwounblank(dev)
    294 	struct device *dev;
    295 {
    296 	struct cgtwo_softc *sc = (struct cgtwo_softc *)dev;
    297 	sc->sc_reg->video_enab = 1;
    298 }
    299 
    300 /*
    301  */
    302 int
    303 cgtwogetcmap(sc, cmap)
    304 	register struct cgtwo_softc *sc;
    305 	register struct fbcmap *cmap;
    306 {
    307 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
    308 	int error, start, count, ecount;
    309 	register u_int i;
    310 	register volatile u_short *p;
    311 
    312 	start = cmap->index;
    313 	count = cmap->count;
    314 	ecount = start + count;
    315 	if (start >= CG2_CMSIZE || count > CG2_CMSIZE - start)
    316 		return (EINVAL);
    317 
    318 	/* XXX - Wait for retrace? */
    319 
    320 	/* Copy hardware to local arrays. */
    321 	p = &sc_redmap(sc)[start];
    322 	for (i = start; i < ecount; i++)
    323 		red[i] = *p++;
    324 	p = &sc_greenmap(sc)[start];
    325 	for (i = start; i < ecount; i++)
    326 		green[i] = *p++;
    327 	p = &sc_bluemap(sc)[start];
    328 	for (i = start; i < ecount; i++)
    329 		blue[i] = *p++;
    330 
    331 	/* Copy local arrays to user space. */
    332 	if ((error = copyout(red + start, cmap->red, count)) != 0)
    333 		return (error);
    334 	if ((error = copyout(green + start, cmap->green, count)) != 0)
    335 		return (error);
    336 	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
    337 		return (error);
    338 
    339 	return (0);
    340 }
    341 
    342 /*
    343  */
    344 int
    345 cgtwoputcmap(sc, cmap)
    346 	register struct cgtwo_softc *sc;
    347 	register struct fbcmap *cmap;
    348 {
    349 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
    350 	int error;
    351 	u_int start, count, ecount;
    352 	register u_int i;
    353 	register volatile u_short *p;
    354 
    355 	start = cmap->index;
    356 	count = cmap->count;
    357 	ecount = start + count;
    358 	if (start >= CG2_CMSIZE || count > CG2_CMSIZE - start)
    359 		return (EINVAL);
    360 
    361 	/* Copy from user space to local arrays. */
    362 	if ((error = copyin(cmap->red, red + start, count)) != 0)
    363 		return (error);
    364 	if ((error = copyin(cmap->green, green + start, count)) != 0)
    365 		return (error);
    366 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
    367 		return (error);
    368 
    369 	/* XXX - Wait for retrace? */
    370 
    371 	/* Copy from local arrays to hardware. */
    372 	p = &sc_redmap(sc)[start];
    373 	for (i = start; i < ecount; i++)
    374 		*p++ = red[i];
    375 	p = &sc_greenmap(sc)[start];
    376 	for (i = start; i < ecount; i++)
    377 		*p++ = green[i];
    378 	p = &sc_bluemap(sc)[start];
    379 	for (i = start; i < ecount; i++)
    380 		*p++ = blue[i];
    381 
    382 	return (0);
    383 }
    384 
    385 /*
    386  * Return the address that would map the given device at the given
    387  * offset, allowing for the given protection, or return -1 for error.
    388  */
    389 paddr_t
    390 cgtwommap(dev, off, prot)
    391 	dev_t dev;
    392 	off_t off;
    393 	int prot;
    394 {
    395 	register struct cgtwo_softc *sc = cgtwo_cd.cd_devs[minor(dev)];
    396 	vme_am_t mod;
    397 	bus_space_handle_t bh;
    398 	extern int sparc_vme_mmap_cookie __P((vme_addr_t, vme_am_t,
    399 					      bus_space_handle_t *));
    400 
    401 	if (off & PGOFSET)
    402 		panic("cgtwommap");
    403 
    404 	if (off >= sc->sc_fb.fb_type.fb_size)
    405 		return (-1);
    406 
    407 	/* Apparently, the pixels are in 32-bit data space */
    408 	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
    409 
    410 	if (sparc_vme_mmap_cookie(sc->sc_paddr + off, mod, &bh) != 0)
    411 		panic("cgtwommap");
    412 
    413 	return ((paddr_t)bh);
    414 }
    415