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