Home | History | Annotate | Line # | Download | only in dev
grf.c revision 1.47
      1 /*	$NetBSD: grf.c,v 1.47 2003/01/01 00:28:57 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1988 University of Utah.
      5  * Copyright (c) 1990 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * the Systems Programming Group of the University of Utah Computer
     10  * Science Department.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. 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: Utah $Hdr: grf.c 1.31 91/01/21$
     41  *
     42  *	@(#)grf.c	7.8 (Berkeley) 5/7/91
     43  */
     44 
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: grf.c,v 1.47 2003/01/01 00:28:57 thorpej Exp $");
     47 
     48 /*
     49  * Graphics display driver for the Amiga
     50  * This is the hardware-independent portion of the driver.
     51  * Hardware access is through the grf_softc->g_mode routine.
     52  */
     53 
     54 #include <sys/param.h>
     55 #include <sys/proc.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/device.h>
     58 #include <sys/file.h>
     59 #include <sys/malloc.h>
     60 #include <sys/systm.h>
     61 #include <sys/vnode.h>
     62 #include <sys/mman.h>
     63 #include <uvm/uvm_extern.h>
     64 #include <machine/cpu.h>
     65 #include <dev/sun/fbio.h>
     66 #include <amiga/amiga/color.h>	/* DEBUG */
     67 #include <amiga/amiga/device.h>
     68 #include <amiga/dev/grfioctl.h>
     69 #include <amiga/dev/grfvar.h>
     70 #include <amiga/dev/itevar.h>
     71 #include <amiga/dev/viewioctl.h>
     72 
     73 #include <sys/conf.h>
     74 
     75 #include "view.h"
     76 #include "grf.h"
     77 
     78 #if NGRF > 0
     79 #include "ite.h"
     80 #if NITE == 0
     81 #define	ite_on(u,f)
     82 #define	ite_off(u,f)
     83 #define ite_reinit(d)
     84 #endif
     85 
     86 int grfon(dev_t);
     87 int grfoff(dev_t);
     88 int grfsinfo(dev_t, struct grfdyninfo *);
     89 
     90 void grfattach(struct device *, struct device *, void *);
     91 int grfmatch(struct device *, struct cfdata *, void *);
     92 int grfprint(void *, const char *);
     93 /*
     94  * pointers to grf drivers device structs
     95  */
     96 struct grf_softc *grfsp[NGRF];
     97 
     98 CFATTACH_DECL(grf, sizeof(struct device),
     99     grfmatch, grfattach, NULL, NULL);
    100 
    101 dev_type_open(grfopen);
    102 dev_type_close(grfclose);
    103 dev_type_ioctl(grfioctl);
    104 dev_type_mmap(grfmmap);
    105 
    106 const struct cdevsw grf_cdevsw = {
    107 	grfopen, grfclose, nullread, nullwrite, grfioctl,
    108 	nostop, notty, nopoll, grfmmap, nokqfilter,
    109 };
    110 
    111 /*
    112  * only used in console init.
    113  */
    114 static struct cfdata *cfdata;
    115 
    116 /*
    117  * match if the unit of grf matches its perspective
    118  * low level board driver.
    119  */
    120 int
    121 grfmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
    122 {
    123 
    124 	if (cfp->cf_unit != ((struct grf_softc *)pdp)->g_unit)
    125 		return(0);
    126 	cfdata = cfp;
    127 	return(1);
    128 }
    129 
    130 /*
    131  * attach.. plug pointer in and print some info.
    132  * then try and attach an ite to us. note: dp is NULL
    133  * durring console init.
    134  */
    135 void
    136 grfattach(struct device *pdp, struct device *dp, void *auxp)
    137 {
    138 	struct grf_softc *gp;
    139 	int maj;
    140 
    141 	gp = (struct grf_softc *)pdp;
    142 	grfsp[gp->g_unit] = (struct grf_softc *)pdp;
    143 
    144 	/*
    145 	 * find our major device number
    146 	 */
    147 	maj = cdevsw_lookup_major(&grf_cdevsw);
    148 
    149 	gp->g_grfdev = makedev(maj, gp->g_unit);
    150 	if (dp != NULL) {
    151 		printf(": width %d height %d", gp->g_display.gd_dwidth,
    152 		    gp->g_display.gd_dheight);
    153 		if (gp->g_display.gd_colors == 2)
    154 			printf(" monochrome\n");
    155 		else
    156 			printf(" colors %d\n", gp->g_display.gd_colors);
    157 	}
    158 
    159 	/*
    160 	 * try and attach an ite
    161 	 */
    162 	amiga_config_found(cfdata, dp, gp, grfprint);
    163 }
    164 
    165 int
    166 grfprint(void *auxp, const char *pnp)
    167 {
    168 	if (pnp)
    169 		aprint_normal("ite at %s", pnp);
    170 	return(UNCONF);
    171 }
    172 
    173 /*ARGSUSED*/
    174 int
    175 grfopen(dev_t dev, int flags, int devtype, struct proc *p)
    176 {
    177 	struct grf_softc *gp;
    178 
    179 	if (GRFUNIT(dev) >= NGRF || (gp = grfsp[GRFUNIT(dev)]) == NULL)
    180 		return(ENXIO);
    181 
    182 	if ((gp->g_flags & GF_ALIVE) == 0)
    183 		return(ENXIO);
    184 
    185 	if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
    186 		return(EBUSY);
    187 
    188 	return(0);
    189 }
    190 
    191 /*ARGSUSED*/
    192 int
    193 grfclose(dev_t dev, int flags, int mode, struct proc *p)
    194 {
    195 	struct grf_softc *gp;
    196 
    197 	gp = grfsp[GRFUNIT(dev)];
    198 	(void)grfoff(dev);
    199 	gp->g_flags &= GF_ALIVE;
    200 	return(0);
    201 }
    202 
    203 /*ARGSUSED*/
    204 int
    205 grfioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    206 {
    207 	struct grf_softc *gp;
    208 	int error;
    209 
    210 	gp = grfsp[GRFUNIT(dev)];
    211 	error = 0;
    212 
    213 	switch (cmd) {
    214 	case OGRFIOCGINFO:
    215 	        /* argl.. no bank-member.. */
    216 	  	bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo)-4);
    217 		break;
    218 	case GRFIOCGINFO:
    219 		bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo));
    220 		break;
    221 	case GRFIOCON:
    222 		error = grfon(dev);
    223 		break;
    224 	case GRFIOCOFF:
    225 		error = grfoff(dev);
    226 		break;
    227 	case GRFIOCSINFO:
    228 		error = grfsinfo(dev, (struct grfdyninfo *) data);
    229 		break;
    230 	case GRFGETVMODE:
    231 		return(gp->g_mode(gp, GM_GRFGETVMODE, data, 0, 0));
    232 	case GRFSETVMODE:
    233 		error = gp->g_mode(gp, GM_GRFSETVMODE, data, 0, 0);
    234 		if (error == 0 && gp->g_itedev && !(gp->g_flags & GF_GRFON))
    235 			ite_reinit(gp->g_itedev);
    236 		break;
    237 	case GRFGETNUMVM:
    238 		return(gp->g_mode(gp, GM_GRFGETNUMVM, data, 0, 0));
    239 	/*
    240 	 * these are all hardware dependant, and have to be resolved
    241 	 * in the respective driver.
    242 	 */
    243 	case GRFIOCPUTCMAP:
    244 	case GRFIOCGETCMAP:
    245 	case GRFIOCSSPRITEPOS:
    246 	case GRFIOCGSPRITEPOS:
    247 	case GRFIOCSSPRITEINF:
    248 	case GRFIOCGSPRITEINF:
    249 	case GRFIOCGSPRITEMAX:
    250 	case GRFIOCBITBLT:
    251     	case GRFIOCSETMON:
    252 	case GRFTOGGLE: /* Toggles between Cirrus boards and native ECS on
    253                      Amiga. 15/11/94 ill */
    254 		/*
    255 		 * We need the minor dev number to get the overlay/image
    256 		 * information for grf_ul.
    257 		 */
    258 		return(gp->g_mode(gp, GM_GRFIOCTL, data, cmd, dev));
    259 
    260 	case GRFIOCBLANK:	/* blank ioctl, IOCON/OFF will turn ite on */
    261 	case FBIOSVIDEO:
    262 		error = gp->g_mode(gp, GM_GRFIOCTL, data, GRFIOCBLANK, dev);
    263 		if (!error)
    264 			gp->g_blank = *(int *)data;
    265 		return (error);
    266 
    267 	case FBIOGVIDEO:
    268 		*(int *)data = gp->g_blank;
    269 		return (0);
    270 
    271 	default:
    272 #if NVIEW > 0
    273 		/*
    274 		 * check to see whether it's a command recognized by the
    275 		 * view code if the unit is 0
    276 		 * XXX
    277 		 */
    278 		if (GRFUNIT(dev) == 0) {
    279 			extern const struct cdevsw view_cdevsw;
    280 
    281 			return((*view_cdevsw.d_ioctl)(dev, cmd, data, flag, p));
    282 		}
    283 #endif
    284 		error = EPASSTHROUGH;
    285 		break;
    286 
    287 	}
    288 	return(error);
    289 }
    290 
    291 /*
    292  * map the contents of a graphics display card into process'
    293  * memory space.
    294  */
    295 paddr_t
    296 grfmmap(dev_t dev, off_t off, int prot)
    297 {
    298 	struct grf_softc *gp;
    299 	struct grfinfo *gi;
    300 
    301 	gp = grfsp[GRFUNIT(dev)];
    302 	gi = &gp->g_display;
    303 
    304 	/*
    305 	 * control registers
    306 	 */
    307 	if (off >= 0 && off < gi->gd_regsize)
    308 		return(((paddr_t)gi->gd_regaddr + off) >> PGSHIFT);
    309 
    310 	/*
    311 	 * frame buffer
    312 	 */
    313 	if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) {
    314 		off -= gi->gd_regsize;
    315 		return(((paddr_t)gi->gd_fbaddr + off) >> PGSHIFT);
    316 	}
    317 	/* bogus */
    318 	return(-1);
    319 }
    320 
    321 int
    322 grfon(dev_t dev)
    323 {
    324 	struct grf_softc *gp;
    325 
    326 	gp = grfsp[GRFUNIT(dev)];
    327 
    328 	if (gp->g_flags & GF_GRFON)
    329 		return(0);
    330 
    331 	gp->g_flags |= GF_GRFON;
    332 	if (gp->g_itedev != NODEV)
    333 		ite_off(gp->g_itedev, 3);
    334 
    335 	return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON,
    336 							NULL, 0, 0));
    337 }
    338 
    339 int
    340 grfoff(dev_t dev)
    341 {
    342 	struct grf_softc *gp;
    343 	int error;
    344 
    345 	gp = grfsp[GRFUNIT(dev)];
    346 
    347 	if ((gp->g_flags & GF_GRFON) == 0)
    348 		return(0);
    349 
    350 	gp->g_flags &= ~GF_GRFON;
    351 	error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF,
    352 							NULL, 0, 0);
    353 
    354 	/*
    355 	 * Closely tied together no X's
    356 	 */
    357 	if (gp->g_itedev != NODEV)
    358 		ite_on(gp->g_itedev, 2);
    359 
    360 	return(error);
    361 }
    362 
    363 int
    364 grfsinfo(dev_t dev, struct grfdyninfo *dyninfo)
    365 {
    366 	struct grf_softc *gp;
    367 	int error;
    368 
    369 	gp = grfsp[GRFUNIT(dev)];
    370 	error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo, 0, 0);
    371 
    372 	/*
    373 	 * Closely tied together no X's
    374 	 */
    375 	if (gp->g_itedev != NODEV)
    376 		ite_reinit(gp->g_itedev);
    377 	return(error);
    378 }
    379 
    380 #endif	/* NGRF > 0 */
    381