Home | History | Annotate | Line # | Download | only in dev
cg2.c revision 1.15.2.2
      1 /*	$NetBSD: cg2.c,v 1.15.2.2 2001/10/10 11:56:37 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 (cg2) 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/conf.h>
     58 #include <sys/device.h>
     59 #include <sys/ioctl.h>
     60 #include <sys/malloc.h>
     61 #include <sys/mman.h>
     62 #include <sys/proc.h>
     63 #include <sys/tty.h>
     64 #include <sys/vnode.h>
     65 
     66 #include <uvm/uvm_extern.h>
     67 
     68 #include <dev/sun/fbio.h>
     69 #include <machine/autoconf.h>
     70 #include <machine/pmap.h>
     71 #include <machine/cg2reg.h>
     72 
     73 #include "fbvar.h"
     74 
     75 cdev_decl(cg2);
     76 
     77 #define	CMSIZE 256
     78 
     79 /* offset to and size of mapped part of frame buffer */
     80 #define PLANEMAP_SIZE		0x100000
     81 #define PIXELMAP_SIZE		0x100000
     82 #define ROWOPMAP_SIZE		0x100000
     83 
     84 #define	CTLREGS_OFF 		0x300000
     85 #define	CTLREGS_SIZE		 0x10600
     86 
     87 #define	CG2_MAPPED_OFFSET	(PLANEMAP_SIZE + PIXELMAP_SIZE)
     88 #define	CG2_MAPPED_SIZE 	(CTLREGS_OFF + CTLREGS_SIZE)
     89 
     90 /* per-display variables */
     91 struct cg2_softc {
     92 	struct	device sc_dev;		/* base device */
     93 	struct	fbdevice sc_fb;		/* frame buffer device */
     94 	int 	sc_phys;		/* display RAM (phys addr) */
     95 	int 	sc_pmtype;		/* pmap type bits */
     96 	struct	cg2fb *sc_ctlreg;	/* control registers */
     97 };
     98 
     99 /* autoconfiguration driver */
    100 static void	cg2attach __P((struct device *, struct device *, void *));
    101 static int	cg2match __P((struct device *, struct cfdata *, void *));
    102 
    103 struct cfattach cgtwo_ca = {
    104 	sizeof(struct cg2_softc), cg2match, cg2attach
    105 };
    106 
    107 extern struct cfdriver cgtwo_cd;
    108 
    109 static int  cg2gattr __P((struct fbdevice *,  void *));
    110 static int  cg2gvideo __P((struct fbdevice *, void *));
    111 static int	cg2svideo __P((struct fbdevice *, void *));
    112 static int	cg2getcmap __P((struct fbdevice *, void *));
    113 static int	cg2putcmap __P((struct fbdevice *, void *));
    114 
    115 static struct fbdriver cg2fbdriver = {
    116 	cg2open, cg2close, cg2mmap, cg2gattr,
    117 	cg2gvideo, cg2svideo,
    118 	cg2getcmap, cg2putcmap };
    119 
    120 static int cg2intr __P((void*));
    121 
    122 /*
    123  * Match a cg2.
    124  */
    125 static int
    126 cg2match(parent, cf, aux)
    127 	struct device *parent;
    128 	struct cfdata *cf;
    129 	void *aux;
    130 {
    131 	struct confargs *ca = aux;
    132 	int probe_addr;
    133 
    134 	/* No default VME address. */
    135 	if (ca->ca_paddr == -1)
    136 		return (0);
    137 
    138 	/* Make sure something is there... */
    139 	probe_addr = ca->ca_paddr + CTLREGS_OFF;
    140 	if (bus_peek(ca->ca_bustype, probe_addr, 1) == -1)
    141 		return (0);
    142 
    143 	/* XXX: look at the ID reg? */
    144 	/* printf("cg2: id=0x%x\n", x); */
    145 
    146 	/* Default interrupt priority. */
    147 	if (ca->ca_intpri == -1)
    148 		ca->ca_intpri = 4;
    149 
    150 	return (1);
    151 }
    152 
    153 /*
    154  * Attach a display.  We need to notice if it is the console, too.
    155  */
    156 static void
    157 cg2attach(parent, self, args)
    158 	struct device *parent, *self;
    159 	void *args;
    160 {
    161 	struct cg2_softc *sc = (struct cg2_softc *)self;
    162 	struct fbdevice *fb = &sc->sc_fb;
    163 	struct confargs *ca = args;
    164 	struct fbtype *fbt;
    165 
    166 	sc->sc_phys = ca->ca_paddr;
    167 	sc->sc_pmtype = PMAP_NC | PMAP_VME16;
    168 
    169 	sc->sc_ctlreg = (struct cg2fb *) bus_mapin(ca->ca_bustype,
    170 			ca->ca_paddr + CTLREGS_OFF, CTLREGS_SIZE);
    171 
    172 	isr_add_vectored(cg2intr, (void*)sc,
    173 					 ca->ca_intpri, ca->ca_intvec);
    174 
    175 	/*
    176 	 * XXX - Initialize?  Determine type?
    177 	 */
    178 	sc->sc_ctlreg->intrptvec.reg = ca->ca_intvec;
    179 	sc->sc_ctlreg->status.word = 1;
    180 
    181 	fb->fb_driver = &cg2fbdriver;
    182 	fb->fb_private = sc;
    183 	fb->fb_name = sc->sc_dev.dv_xname;
    184 
    185 	fbt = &fb->fb_fbtype;
    186 	fbt->fb_type = FBTYPE_SUN2COLOR;
    187 	fbt->fb_depth = 8;
    188 	fbt->fb_cmsize = CMSIZE;
    189 
    190 	fbt->fb_width = 1152;
    191 	fbt->fb_height = 900;
    192 	fbt->fb_size = CG2_MAPPED_SIZE;
    193 
    194 	printf(" (%dx%d)\n", fbt->fb_width, fbt->fb_height);
    195 	fb_attach(fb, 2);
    196 }
    197 
    198 int
    199 cg2open(devvp, flags, mode, p)
    200 	struct vnode *devvp;
    201 	int flags, mode;
    202 	struct proc *p;
    203 {
    204 	dev_t dev = vdev_rdev(devvp);
    205 	int unit = minor(dev);
    206 
    207 	if (unit >= cgtwo_cd.cd_ndevs || cgtwo_cd.cd_devs[unit] == NULL)
    208 		return (ENXIO);
    209 	vdev_setprivdata(devvp, cgtwo_cd.cd_devs[unit]);
    210 	return (0);
    211 }
    212 
    213 int
    214 cg2close(devvp, flags, mode, p)
    215 	struct vnode *devvp;
    216 	int flags, mode;
    217 	struct proc *p;
    218 {
    219 
    220 	return (0);
    221 }
    222 
    223 int
    224 cg2ioctl(devvp, cmd, data, flags, p)
    225 	struct vnode *devvp;
    226 	u_long cmd;
    227 	caddr_t data;
    228 	int flags;
    229 	struct proc *p;
    230 {
    231 	struct cg2_softc *sc = vdev_privdata(devvp);
    232 
    233 	return (fbioctlfb(&sc->sc_fb, cmd, data));
    234 }
    235 
    236 /*
    237  * Return the address that would map the given device at the given
    238  * offset, allowing for the given protection, or return -1 for error.
    239  */
    240 paddr_t
    241 cg2mmap(devvp, off, prot)
    242 	struct vnode *devvp;
    243 	off_t off;
    244 	int prot;
    245 {
    246 	struct cg2_softc *sc = vdev_privdata(devvp);
    247 
    248 	if (off & PGOFSET)
    249 		panic("cg2mmap");
    250 
    251 	if (off >= CG2_MAPPED_SIZE)
    252 		return (-1);
    253 
    254 	/*
    255 	 * I turned on PMAP_NC here to disable the cache as I was
    256 	 * getting horribly broken behaviour with it on.
    257 	 */
    258 	return ((sc->sc_phys + off) | sc->sc_pmtype);
    259 }
    260 
    261 /*
    262  * Internal ioctl functions.
    263  */
    264 
    265 /* FBIOGATTR: */
    266 static int  cg2gattr(fb, data)
    267 	struct fbdevice *fb;
    268 	void *data;
    269 {
    270 	struct fbgattr *fba = data;
    271 
    272 	fba->real_type = fb->fb_fbtype.fb_type;
    273 	fba->owner = 0;		/* XXX - TIOCCONS stuff? */
    274 	fba->fbtype = fb->fb_fbtype;
    275 	fba->sattr.flags = 0;
    276 	fba->sattr.emu_type = fb->fb_fbtype.fb_type;
    277 	fba->sattr.dev_specific[0] = -1;
    278 	fba->emu_types[0] = fb->fb_fbtype.fb_type;
    279 	fba->emu_types[1] = -1;
    280 	return (0);
    281 }
    282 
    283 /* FBIOGVIDEO: */
    284 static int  cg2gvideo(fb, data)
    285 	struct fbdevice *fb;
    286 	void *data;
    287 {
    288 	int *on = data;
    289 	struct cg2_softc *sc = fb->fb_private;
    290 
    291 	*on = sc->sc_ctlreg->status.reg.video_enab;
    292 	return (0);
    293 }
    294 
    295 /* FBIOSVIDEO: */
    296 static int cg2svideo(fb, data)
    297 	struct fbdevice *fb;
    298 	void *data;
    299 {
    300 	int *on = data;
    301 	struct cg2_softc *sc = fb->fb_private;
    302 
    303 	sc->sc_ctlreg->status.reg.video_enab = (*on) & 1;
    304 
    305 	return (0);
    306 }
    307 
    308 /* FBIOGETCMAP: */
    309 static int cg2getcmap(fb, data)
    310 	struct fbdevice *fb;
    311 	void *data;
    312 {
    313 	struct fbcmap *cmap = data;
    314 	struct cg2_softc *sc = fb->fb_private;
    315 	u_char red[CMSIZE], green[CMSIZE], blue[CMSIZE];
    316 	int error, start, count, ecount;
    317 	u_int i;
    318 	u_short *p;
    319 
    320 	start = cmap->index;
    321 	count = cmap->count;
    322 	ecount = start + count;
    323 	if (start >= CMSIZE || ecount > CMSIZE)
    324 		return (EINVAL);
    325 
    326 	/* XXX - Wait for retrace? */
    327 
    328 	/* Copy hardware to local arrays. */
    329 	p = &sc->sc_ctlreg->redmap[start];
    330 	for (i = start; i < ecount; i++)
    331 		red[i] = *p++;
    332 	p = &sc->sc_ctlreg->greenmap[start];
    333 	for (i = start; i < ecount; i++)
    334 		green[i] = *p++;
    335 	p = &sc->sc_ctlreg->bluemap[start];
    336 	for (i = start; i < ecount; i++)
    337 		blue[i] = *p++;
    338 
    339 	/* Copy local arrays to user space. */
    340 	if ((error = copyout(red + start, cmap->red, count)) != 0)
    341 		return (error);
    342 	if ((error = copyout(green + start, cmap->green, count)) != 0)
    343 		return (error);
    344 	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
    345 		return (error);
    346 
    347 	return (0);
    348 }
    349 
    350 /* FBIOPUTCMAP: */
    351 static int cg2putcmap(fb, data)
    352 	struct fbdevice *fb;
    353 	void *data;
    354 {
    355 	struct fbcmap *cmap = data;
    356 	struct cg2_softc *sc = fb->fb_private;
    357 	u_char red[CMSIZE], green[CMSIZE], blue[CMSIZE];
    358 	int error;
    359 	u_int start, count, ecount;
    360 	u_int i;
    361 	u_short *p;
    362 
    363 	start = cmap->index;
    364 	count = cmap->count;
    365 	ecount = start + count;
    366 	if (start >= CMSIZE || ecount > CMSIZE)
    367 		return (EINVAL);
    368 
    369 	/* Copy from user space to local arrays. */
    370 	if ((error = copyin(cmap->red, red + start, count)) != 0)
    371 		return (error);
    372 	if ((error = copyin(cmap->green, green + start, count)) != 0)
    373 		return (error);
    374 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
    375 		return (error);
    376 
    377 	/* XXX - Wait for retrace? */
    378 
    379 	/* Copy from local arrays to hardware. */
    380 	p = &sc->sc_ctlreg->redmap[start];
    381 	for (i = start; i < ecount; i++)
    382 		*p++ = red[i];
    383 	p = &sc->sc_ctlreg->greenmap[start];
    384 	for (i = start; i < ecount; i++)
    385 		*p++ = green[i];
    386 	p = &sc->sc_ctlreg->bluemap[start];
    387 	for (i = start; i < ecount; i++)
    388 		*p++ = blue[i];
    389 
    390 	return (0);
    391 
    392 }
    393 
    394 static int
    395 cg2intr(vsc)
    396 	void *vsc;
    397 {
    398 	struct cg2_softc *sc = vsc;
    399 
    400 	/* XXX - Just disable interrupts for now. */
    401 	sc->sc_ctlreg->status.reg.inten = 0;
    402 
    403 	printf("cg2intr\n");
    404 	return (1);
    405 }
    406