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