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