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