Home | History | Annotate | Line # | Download | only in dev
cg2.c revision 1.1
      1 /*	$NetBSD: cg2.c,v 1.1 1995/04/07 02:31:45 gwr 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/device.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/malloc.h>
     59 #include <sys/mman.h>
     60 #include <sys/tty.h>
     61 
     62 #include <vm/vm.h>
     63 
     64 #include <machine/fbio.h>
     65 #include <machine/autoconf.h>
     66 #include <machine/pmap.h>
     67 #include <machine/eeprom.h>
     68 #include <machine/cg2reg.h>
     69 
     70 #include "fbvar.h"
     71 
     72 #define	CMSIZE 256
     73 
     74 /* offset to and size of mapped part of frame buffer */
     75 #define PLANEMAP_SIZE		0x100000
     76 #define PIXELMAP_SIZE		0x100000
     77 #define ROWOPMAP_SIZE		0x100000
     78 
     79 #define	CTLREGS_OFF 		0x300000
     80 #define	CTLREGS_SIZE		 0x10600
     81 
     82 #define	CG2_MAPPED_OFFSET	(PLANEMAP_SIZE + PIXELMAP_SIZE)
     83 #define	CG2_MAPPED_SIZE 	(ROWOPMAP_SIZE /* XXX + CTLREGS_SIZE */)
     84 #define	CG2_MAPPED_LIMIT	(CTLREGS_OFF + CTLREGS_SIZE)
     85 
     86 /* per-display variables */
     87 struct cg2_softc {
     88 	struct	device sc_dev;		/* base device */
     89 	struct	fbdevice sc_fb;		/* frame buffer device */
     90 	int 	sc_phys;		/* display RAM (phys addr) */
     91 	int 	sc_pmtype;		/* pmap type bits */
     92 	struct	cg2fb *sc_ctlreg;	/* control registers */
     93 };
     94 
     95 /* autoconfiguration driver */
     96 static void	cg2attach __P((struct device *, struct device *, void *));
     97 static int	cg2match __P((struct device *, void *, void *));
     98 
     99 struct cfdriver cgtwocd = {
    100 	NULL, "cgtwo", cg2match, cg2attach,
    101 	DV_DULL, sizeof(struct cg2_softc) };
    102 
    103 /* frame buffer generic driver */
    104 int cg2open(), cg2close(), cg2map();
    105 
    106 static int  cg2gattr __P((struct fbdevice *, struct fbgattr *));
    107 static int  cg2gvideo __P((struct fbdevice *, int *));
    108 static int	cg2svideo __P((struct fbdevice *, int *));
    109 static int	cg2getcmap __P((struct fbdevice *, struct fbcmap *));
    110 static int	cg2putcmap __P((struct fbdevice *, struct fbcmap *));
    111 
    112 static struct fbdriver cg2fbdriver = {
    113 	cg2open, cg2close, cg2map, cg2gattr,
    114 	cg2gvideo, cg2svideo,
    115 	cg2getcmap, cg2putcmap };
    116 
    117 static void cg2loadcmap __P((struct cg2_softc *, int, int));
    118 static int cg2intr __P((void*));
    119 
    120 /*
    121  * Match a cg2.
    122  */
    123 static int
    124 cg2match(parent, vcf, args)
    125 	struct device *parent;
    126 	void *vcf, *args;
    127 {
    128 	struct confargs *ca = args;
    129 	struct cg2fb *crp;
    130 	int x;
    131 
    132 	if (ca->ca_paddr == -1)
    133 		return (0);
    134 
    135 	if (ca->ca_bustype != BUS_VME16)
    136 		return (0);
    137 
    138 	/* Look at the value in the ID register. */
    139 	crp = (struct cg2fb *) (ca->ca_paddr + CTLREGS_OFF);
    140 
    141 	/* The peek returns -1 on bus error. */
    142 	x = bus_peek(ca->ca_bustype, &crp->id.reg, 2);
    143 	if (x == -1)
    144 		return (0);
    145 
    146 	/* XXX */
    147 	printf("cg2: id=0x%x\n", x);
    148 
    149 	return (1);
    150 }
    151 
    152 /*
    153  * Attach a display.  We need to notice if it is the console, too.
    154  */
    155 static void
    156 cg2attach(parent, self, args)
    157 	struct device *parent, *self;
    158 	void *args;
    159 {
    160 	struct cg2_softc *sc = (struct cg2_softc *)self;
    161 	struct fbdevice *fb = &sc->sc_fb;
    162 	struct confargs *ca = args;
    163 	struct fbtype *fbt;
    164 	int i, ramsize, pa;
    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(dev, flags, mode, p)
    200 	dev_t dev;
    201 	int flags, mode;
    202 	struct proc *p;
    203 {
    204 	int unit = minor(dev);
    205 
    206 	if (unit >= cgtwocd.cd_ndevs || cgtwocd.cd_devs[unit] == NULL)
    207 		return (ENXIO);
    208 	return (0);
    209 }
    210 
    211 int
    212 cg2close(dev, flags, mode, p)
    213 	dev_t dev;
    214 	int flags, mode;
    215 	struct proc *p;
    216 {
    217 
    218 	return (0);
    219 }
    220 
    221 int
    222 cg2ioctl(dev, cmd, data, flags, p)
    223 	dev_t dev;
    224 	u_long cmd;
    225 	caddr_t data;
    226 	int flags;
    227 	struct proc *p;
    228 {
    229 	struct cg2_softc *sc = cgtwocd.cd_devs[minor(dev)];
    230 
    231 	return (fbioctlfb(&sc->sc_fb, cmd, data));
    232 }
    233 
    234 /*
    235  * Return the address that would map the given device at the given
    236  * offset, allowing for the given protection, or return -1 for error.
    237  */
    238 int
    239 cg2map(dev, off, prot)
    240 	dev_t dev;
    241 	int off, prot;
    242 {
    243 	struct cg2_softc *sc = cgtwocd.cd_devs[minor(dev)];
    244 	int realoff;
    245 
    246 	if (off & PGOFSET)
    247 		panic("cg2map");
    248 
    249 	if ((unsigned)off >= CG2_MAPPED_LIMIT)
    250 		return (-1);
    251 
    252 	/*
    253 	 * I turned on PMAP_NC here to disable the cache as I was
    254 	 * getting horribly broken behaviour with it on.
    255 	 */
    256 	return ((sc->sc_phys + off) | sc->sc_pmtype);
    257 }
    258 
    259 /*
    260  * Internal ioctl functions.
    261  */
    262 
    263 /* FBIOGATTR: */
    264 static int  cg2gattr(fb, fba)
    265 	struct fbdevice *fb;
    266 	struct fbgattr *fba;
    267 {
    268 
    269 	fba->real_type = fb->fb_fbtype.fb_type;
    270 	fba->owner = 0;		/* XXX - TIOCCONS stuff? */
    271 	fba->fbtype = fb->fb_fbtype;
    272 	fba->sattr.flags = 0;
    273 	fba->sattr.emu_type = fb->fb_fbtype.fb_type;
    274 	fba->sattr.dev_specific[0] = -1;
    275 	fba->emu_types[0] = fb->fb_fbtype.fb_type;
    276 	fba->emu_types[1] = -1;
    277 	return (0);
    278 }
    279 
    280 /* FBIOGVIDEO: */
    281 static int  cg2gvideo(fb, on)
    282 	struct fbdevice *fb;
    283 	int *on;
    284 {
    285 	struct cg2_softc *sc = fb->fb_private;
    286 
    287 	*on = sc->sc_ctlreg->status.reg.video_enab;
    288 	return (0);
    289 }
    290 
    291 /* FBIOSVIDEO: */
    292 static int cg2svideo(fb, on)
    293 	struct fbdevice *fb;
    294 	int *on;
    295 {
    296 	struct cg2_softc *sc = fb->fb_private;
    297 
    298 	sc->sc_ctlreg->status.reg.video_enab = (*on) & 1;
    299 
    300 	return (0);
    301 }
    302 
    303 /* FBIOGETCMAP: */
    304 static int cg2getcmap(fb, cmap)
    305 	struct fbdevice *fb;
    306 	struct fbcmap *cmap;
    307 {
    308 	struct cg2_softc *sc = fb->fb_private;
    309 	u_char red[CMSIZE], green[CMSIZE], blue[CMSIZE];
    310 	int error, start, count, ecount;
    311 	register u_int i;
    312 	register u_short *p;
    313 
    314 	start = cmap->index;
    315 	count = cmap->count;
    316 	ecount = start + count;
    317 	if (start >= CMSIZE || ecount > CMSIZE)
    318 		return (EINVAL);
    319 
    320 	/* XXX - Wait for retrace? */
    321 
    322 	/* Copy hardware to local arrays. */
    323 	p = &sc->sc_ctlreg->redmap[start];
    324 	for (i = start; i < ecount; i++)
    325 		red[i] = *p++;
    326 	p = &sc->sc_ctlreg->greenmap[start];
    327 	for (i = start; i < ecount; i++)
    328 		green[i] = *p++;
    329 	p = &sc->sc_ctlreg->bluemap[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 /* FBIOPUTCMAP: */
    345 static int cg2putcmap(fb, cmap)
    346 	struct fbdevice *fb;
    347 	struct fbcmap *cmap;
    348 {
    349 	struct cg2_softc *sc = fb->fb_private;
    350 	u_char red[CMSIZE], green[CMSIZE], blue[CMSIZE];
    351 	int error, start, count, ecount;
    352 	register u_int i;
    353 	register u_short *p;
    354 
    355 	start = cmap->index;
    356 	count = cmap->count;
    357 	ecount = start + count;
    358 	if (start >= CMSIZE || ecount > CMSIZE)
    359 		return (EINVAL);
    360 
    361 	/* Copy from user space to local arrays. */
    362 	if ((error = copyin(cmap->red, red + start, count)) != 0)
    363 		return (error);
    364 	if ((error = copyin(cmap->green, green + start, count)) != 0)
    365 		return (error);
    366 	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
    367 		return (error);
    368 
    369 	/* XXX - Wait for retrace? */
    370 
    371 	/* Copy from local arrays to hardware. */
    372 	p = &sc->sc_ctlreg->redmap[start];
    373 	for (i = start; i < ecount; i++)
    374 		*p++ = red[i];
    375 	p = &sc->sc_ctlreg->greenmap[start];
    376 	for (i = start; i < ecount; i++)
    377 		*p++ = green[i];
    378 	p = &sc->sc_ctlreg->bluemap[start];
    379 	for (i = start; i < ecount; i++)
    380 		*p++ = blue[i];
    381 
    382 	return (0);
    383 
    384 }
    385 
    386 static int
    387 cg2intr(vsc)
    388 	void *vsc;
    389 {
    390 	struct cg2_softc *sc = vsc;
    391 
    392 	/* XXX - Just disable interrupts for now. */
    393 	sc->sc_ctlreg->status.reg.inten = 0;
    394 
    395 	printf("cg2intr\n");
    396 	return (1);
    397 }
    398