Home | History | Annotate | Line # | Download | only in dev
cgtwo.c revision 1.57
      1 /*	$NetBSD: cgtwo.c,v 1.57 2014/07/25 08:10:34 dholland 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. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	from: @(#)cgthree.c	8.2 (Berkeley) 10/30/93
     41  */
     42 
     43 /*
     44  * color display (cgtwo) driver.
     45  *
     46  * Does not handle interrupts, even though they can occur.
     47  *
     48  * XXX should defer colormap updates to vertical retrace interrupts
     49  */
     50 
     51 #include <sys/cdefs.h>
     52 __KERNEL_RCSID(0, "$NetBSD: cgtwo.c,v 1.57 2014/07/25 08:10:34 dholland Exp $");
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.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 #include <sys/conf.h>
     63 
     64 #include <uvm/uvm_extern.h>
     65 
     66 #include <machine/autoconf.h>
     67 
     68 #include <dev/sun/fbio.h>
     69 #include <dev/sun/fbvar.h>
     70 
     71 #include <dev/vme/vmevar.h>
     72 
     73 #include <machine/eeprom.h>
     74 #include <machine/cgtworeg.h>
     75 
     76 
     77 /* per-display variables */
     78 struct cgtwo_softc {
     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 int	cgtwomatch(device_t, cfdata_t, void *);
     92 static void	cgtwoattach(device_t, device_t, void *);
     93 static void	cgtwounblank(device_t);
     94 int		cgtwogetcmap(struct cgtwo_softc *, struct fbcmap *);
     95 int		cgtwoputcmap(struct cgtwo_softc *, struct fbcmap *);
     96 
     97 CFATTACH_DECL_NEW(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 	.d_open = cgtwoopen,
    108 	.d_close = nullclose,
    109 	.d_read = noread,
    110 	.d_write = nowrite,
    111 	.d_ioctl = cgtwoioctl,
    112 	.d_stop = nostop,
    113 	.d_tty = notty,
    114 	.d_poll = nopoll,
    115 	.d_mmap = cgtwommap,
    116 	.d_kqfilter = nokqfilter,
    117 	.d_discard = nodiscard,
    118 	.d_flag = 0
    119 };
    120 
    121 /* frame buffer generic driver */
    122 static struct fbdriver cgtwofbdriver = {
    123 	cgtwounblank, cgtwoopen, nullclose, cgtwoioctl, nopoll, cgtwommap,
    124 	nokqfilter
    125 };
    126 
    127 /*
    128  * Match a cgtwo.
    129  */
    130 static int
    131 cgtwomatch(device_t parent, cfdata_t cf, void *aux)
    132 {
    133 	struct vme_attach_args	*va = aux;
    134 	vme_chipset_tag_t	ct = va->va_vct;
    135 	vme_am_t		mod;
    136 
    137 	/*
    138 	 * Mask out invalid flags from the user.
    139 	 */
    140 	cf->cf_flags &= FB_USERMASK;
    141 
    142 	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
    143 	if (vme_probe(ct, va->r[0].offset + CG2_CTLREG_OFF, 2, mod, VME_D16,
    144 			  0, 0)) {
    145 		return (0);
    146 	}
    147 
    148 	return (1);
    149 }
    150 
    151 /*
    152  * Attach a display.  We need to notice if it is the console, too.
    153  */
    154 static void
    155 cgtwoattach(device_t parent, device_t self, 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 = device_private(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 = self;
    171 	fb->fb_type.fb_type = FBTYPE_SUN2COLOR;
    172 	fb->fb_flags = device_cfdata(self)->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 = (void *)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_t dev, int flags, int mode, struct lwp *l)
    234 {
    235 	int unit = minor(dev);
    236 
    237 	if (device_lookup(&cgtwo_cd, unit) == NULL)
    238 		return (ENXIO);
    239 	return (0);
    240 }
    241 
    242 int
    243 cgtwoioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    244 {
    245 	register struct cgtwo_softc *sc = device_lookup_private(&cgtwo_cd,
    246 								minor(dev));
    247 	register struct fbgattr *fba;
    248 
    249 	switch (cmd) {
    250 
    251 	case FBIOGTYPE:
    252 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    253 		break;
    254 
    255 	case FBIOGATTR:
    256 		fba = (struct fbgattr *)data;
    257 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    258 		fba->owner = 0;		/* XXX ??? */
    259 		fba->fbtype = sc->sc_fb.fb_type;
    260 		fba->sattr.flags = 0;
    261 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    262 		fba->sattr.dev_specific[0] = -1;
    263 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    264 		fba->emu_types[1] = -1;
    265 		break;
    266 
    267 	case FBIOGETCMAP:
    268 		return cgtwogetcmap(sc, (struct fbcmap *) data);
    269 
    270 	case FBIOPUTCMAP:
    271 		return cgtwoputcmap(sc, (struct fbcmap *) data);
    272 
    273 	case FBIOGVIDEO:
    274 		*(int *)data = sc->sc_reg->video_enab;
    275 		break;
    276 
    277 	case FBIOSVIDEO:
    278 		sc->sc_reg->video_enab = (*(int*)data) & 1;
    279 		break;
    280 
    281 	default:
    282 		return (ENOTTY);
    283 	}
    284 	return (0);
    285 }
    286 
    287 /*
    288  * Undo the effect of an FBIOSVIDEO that turns the video off.
    289  */
    290 static void
    291 cgtwounblank(device_t dev)
    292 {
    293 	struct cgtwo_softc *sc = device_private(dev);
    294 	sc->sc_reg->video_enab = 1;
    295 }
    296 
    297 /*
    298  */
    299 int
    300 cgtwogetcmap(struct cgtwo_softc *sc, struct fbcmap *cmap)
    301 {
    302 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
    303 	int error, start, count, ecount;
    304 	register u_int i;
    305 	register volatile u_short *p;
    306 
    307 	start = cmap->index;
    308 	count = cmap->count;
    309 	ecount = start + count;
    310 	if (start >= CG2_CMSIZE || count > CG2_CMSIZE - start)
    311 		return (EINVAL);
    312 
    313 	/* XXX - Wait for retrace? */
    314 
    315 	/* Copy hardware to local arrays. */
    316 	p = &sc_redmap(sc)[start];
    317 	for (i = start; i < ecount; i++)
    318 		red[i] = *p++;
    319 	p = &sc_greenmap(sc)[start];
    320 	for (i = start; i < ecount; i++)
    321 		green[i] = *p++;
    322 	p = &sc_bluemap(sc)[start];
    323 	for (i = start; i < ecount; i++)
    324 		blue[i] = *p++;
    325 
    326 	/* Copy local arrays to user space. */
    327 	if ((error = copyout(red + start, cmap->red, count)) != 0)
    328 		return (error);
    329 	if ((error = copyout(green + start, cmap->green, count)) != 0)
    330 		return (error);
    331 	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
    332 		return (error);
    333 
    334 	return (0);
    335 }
    336 
    337 /*
    338  */
    339 int
    340 cgtwoputcmap(struct cgtwo_softc *sc, struct fbcmap *cmap)
    341 {
    342 	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
    343 	int error;
    344 	u_int start, count, ecount;
    345 	register u_int i;
    346 	register volatile u_short *p;
    347 
    348 	start = cmap->index;
    349 	count = cmap->count;
    350 	ecount = start + count;
    351 	if (start >= CG2_CMSIZE || count > CG2_CMSIZE - start)
    352 		return (EINVAL);
    353 
    354 	/* Copy from user space to local arrays. */
    355 	if ((error = copyin(cmap->red, red + start, count)) != 0)
    356 		return (error);
    357 	if ((error = copyin(cmap->green, green + start, count)) != 0)
    358 		return (error);
    359 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
    360 		return (error);
    361 
    362 	/* XXX - Wait for retrace? */
    363 
    364 	/* Copy from local arrays to hardware. */
    365 	p = &sc_redmap(sc)[start];
    366 	for (i = start; i < ecount; i++)
    367 		*p++ = red[i];
    368 	p = &sc_greenmap(sc)[start];
    369 	for (i = start; i < ecount; i++)
    370 		*p++ = green[i];
    371 	p = &sc_bluemap(sc)[start];
    372 	for (i = start; i < ecount; i++)
    373 		*p++ = blue[i];
    374 
    375 	return (0);
    376 }
    377 
    378 /*
    379  * Return the address that would map the given device at the given
    380  * offset, allowing for the given protection, or return -1 for error.
    381  */
    382 paddr_t
    383 cgtwommap(dev_t dev, off_t off, int prot)
    384 {
    385 	extern int sparc_vme_mmap_cookie(vme_addr_t, vme_am_t,
    386 					 bus_space_handle_t *);
    387 
    388 	register struct cgtwo_softc *sc = device_lookup_private(&cgtwo_cd,
    389 								minor(dev));
    390 	vme_am_t mod;
    391 	bus_space_handle_t bh;
    392 
    393 	if (off & PGOFSET)
    394 		panic("cgtwommap");
    395 
    396 	if (off >= sc->sc_fb.fb_type.fb_size)
    397 		return (-1);
    398 
    399 	/* Apparently, the pixels are in 32-bit data space */
    400 	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
    401 
    402 	if (sparc_vme_mmap_cookie(sc->sc_paddr + off, mod, &bh) != 0)
    403 		panic("cgtwommap");
    404 
    405 	return ((paddr_t)bh);
    406 }
    407