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