Home | History | Annotate | Line # | Download | only in dev
grf.c revision 1.24
      1 /*	$NetBSD: grf.c,v 1.24 1995/11/30 00:56:55 jtc 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 /*
     46  * Graphics display driver for the Amiga
     47  * This is the hardware-independent portion of the driver.
     48  * Hardware access is through the grf_softc->g_mode routine.
     49  */
     50 
     51 #include <sys/param.h>
     52 #include <sys/proc.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/device.h>
     55 #include <sys/file.h>
     56 #include <sys/malloc.h>
     57 #include <sys/conf.h>
     58 #include <sys/systm.h>
     59 #include <sys/vnode.h>
     60 #include <sys/mman.h>
     61 #include <vm/vm.h>
     62 #include <vm/vm_kern.h>
     63 #include <vm/vm_page.h>
     64 #include <vm/vm_pager.h>
     65 #include <machine/cpu.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 
     72 #include "view.h"
     73 
     74 #include "grf.h"
     75 #if NGRF > 0
     76 
     77 #include "ite.h"
     78 #if NITE == 0
     79 #define	ite_on(u,f)
     80 #define	ite_off(u,f)
     81 #define ite_reinit(d)
     82 #endif
     83 
     84 int grfopen __P((dev_t, int, int, struct proc *));
     85 int grfclose __P((dev_t, int));
     86 int grfioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
     87 int grfselect __P((dev_t, int));
     88 int grfmmap __P((dev_t, int, int));
     89 
     90 int grfon __P((dev_t));
     91 int grfoff __P((dev_t));
     92 int grfsinfo __P((dev_t, struct grfdyninfo *));
     93 #ifdef BANKEDDEVPAGER
     94 int grfbanked_get __P((dev_t, off_t, int));
     95 int grfbanked_cur __P((dev_t));
     96 int grfbanked_set __P((dev_t, int));
     97 #endif
     98 
     99 void grfattach __P((struct device *, struct device *, void *));
    100 int grfmatch __P((struct device *, struct cfdata *, void *));
    101 int grfprint __P((void *, char *));
    102 /*
    103  * pointers to grf drivers device structs
    104  */
    105 struct grf_softc *grfsp[NGRF];
    106 
    107 
    108 struct cfdriver grfcd = {
    109 	NULL, "grf", (cfmatch_t)grfmatch, grfattach, DV_DULL,
    110 	sizeof(struct device), NULL, 0 };
    111 
    112 /*
    113  * only used in console init.
    114  */
    115 static struct cfdata *cfdata;
    116 
    117 /*
    118  * match if the unit of grf matches its perspective
    119  * low level board driver.
    120  */
    121 int
    122 grfmatch(pdp, cfp, auxp)
    123 	struct device *pdp;
    124 	struct cfdata *cfp;
    125 	void *auxp;
    126 {
    127 	if (cfp->cf_unit != ((struct grf_softc *)pdp)->g_unit)
    128 		return(0);
    129 	cfdata = cfp;
    130 	return(1);
    131 }
    132 
    133 /*
    134  * attach.. plug pointer in and print some info.
    135  * then try and attach an ite to us. note: dp is NULL
    136  * durring console init.
    137  */
    138 void
    139 grfattach(pdp, dp, auxp)
    140 	struct device *pdp, *dp;
    141 	void *auxp;
    142 {
    143 	struct grf_softc *gp;
    144 	int maj;
    145 
    146 	gp = (struct grf_softc *)pdp;
    147 	grfsp[gp->g_unit] = (struct grf_softc *)pdp;
    148 
    149 	/*
    150 	 * find our major device number
    151 	 */
    152 	for(maj = 0; maj < nchrdev; maj++)
    153 		if (cdevsw[maj].d_open == grfopen)
    154 			break;
    155 
    156 	gp->g_grfdev = makedev(maj, gp->g_unit);
    157 	if (dp != NULL) {
    158 		printf(": width %d height %d", gp->g_display.gd_dwidth,
    159 		    gp->g_display.gd_dheight);
    160 		if (gp->g_display.gd_colors == 2)
    161 			printf(" monochrome\n");
    162 		else
    163 			printf(" colors %d\n", gp->g_display.gd_colors);
    164 	}
    165 
    166 	/*
    167 	 * try and attach an ite
    168 	 */
    169 	amiga_config_found(cfdata, dp, gp, grfprint);
    170 }
    171 
    172 int
    173 grfprint(auxp, pnp)
    174 	void *auxp;
    175 	char *pnp;
    176 {
    177 	if (pnp)
    178 		printf("ite at %s", pnp);
    179 	return(UNCONF);
    180 }
    181 
    182 /*ARGSUSED*/
    183 int
    184 grfopen(dev, flags, devtype, p)
    185 	dev_t dev;
    186 	int flags, devtype;
    187 	struct proc *p;
    188 {
    189 	struct grf_softc *gp;
    190 
    191 	if (GRFUNIT(dev) >= NGRF || (gp = grfsp[GRFUNIT(dev)]) == NULL)
    192 		return(ENXIO);
    193 
    194 	if ((gp->g_flags & GF_ALIVE) == 0)
    195 		return(ENXIO);
    196 
    197 	if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
    198 		return(EBUSY);
    199 
    200 	return(0);
    201 }
    202 
    203 /*ARGSUSED*/
    204 int
    205 grfclose(dev, flags)
    206 	dev_t dev;
    207 	int flags;
    208 {
    209 	struct grf_softc *gp;
    210 
    211 	gp = grfsp[GRFUNIT(dev)];
    212 	(void)grfoff(dev);
    213 	gp->g_flags &= GF_ALIVE;
    214 	return(0);
    215 }
    216 
    217 /*ARGSUSED*/
    218 int
    219 grfioctl(dev, cmd, data, flag, p)
    220 	dev_t dev;
    221 	u_long cmd;
    222 	caddr_t data;
    223 	int flag;
    224 	struct proc *p;
    225 {
    226 	struct grf_softc *gp;
    227 	int error;
    228 
    229 	gp = grfsp[GRFUNIT(dev)];
    230 	error = 0;
    231 
    232 	switch (cmd) {
    233 	case OGRFIOCGINFO:
    234 	        /* argl.. no bank-member.. */
    235 	  	bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo)-4);
    236 		break;
    237 	case GRFIOCGINFO:
    238 		bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo));
    239 		break;
    240 	case GRFIOCON:
    241 		error = grfon(dev);
    242 		break;
    243 	case GRFIOCOFF:
    244 		error = grfoff(dev);
    245 		break;
    246 	case GRFIOCSINFO:
    247 		error = grfsinfo(dev, (struct grfdyninfo *) data);
    248 		break;
    249 	case GRFGETVMODE:
    250 		return(gp->g_mode(gp, GM_GRFGETVMODE, data));
    251 	case GRFSETVMODE:
    252 		error = gp->g_mode(gp, GM_GRFSETVMODE, data);
    253 		if (error == 0 && gp->g_itedev && !(gp->g_flags & GF_GRFON))
    254 			ite_reinit(gp->g_itedev);
    255 		break;
    256 	case GRFGETNUMVM:
    257 		return(gp->g_mode(gp, GM_GRFGETNUMVM, data));
    258 	/*
    259 	 * these are all hardware dependant, and have to be resolved
    260 	 * in the respective driver.
    261 	 */
    262 	case GRFIOCPUTCMAP:
    263 	case GRFIOCGETCMAP:
    264 	case GRFIOCSSPRITEPOS:
    265 	case GRFIOCGSPRITEPOS:
    266 	case GRFIOCSSPRITEINF:
    267 	case GRFIOCGSPRITEINF:
    268 	case GRFIOCGSPRITEMAX:
    269 	case GRFIOCBITBLT:
    270     	case GRFIOCSETMON:
    271 	case GRFIOCBLANK:	/* blank ioctl, IOCON/OFF will turn ite on */
    272 	case GRFTOGGLE: /* Toggles between Cirrus boards and native ECS on
    273                      Amiga. 15/11/94 ill */
    274 		/*
    275 		 * We need the minor dev number to get the overlay/image
    276 		 * information for grf_ul.
    277 		 */
    278 		return(gp->g_mode(gp, GM_GRFIOCTL, cmd, data, dev));
    279 	default:
    280 #if NVIEW > 0
    281 		/*
    282 		 * check to see whether it's a command recognized by the
    283 		 * view code if the unit is 0
    284 		 * XXX
    285 		 */
    286 		if (GRFUNIT(dev) == 0)
    287 			return(viewioctl(dev, cmd, data, flag, p));
    288 #endif
    289 		error = EINVAL;
    290 		break;
    291 
    292 	}
    293 	return(error);
    294 }
    295 
    296 /*ARGSUSED*/
    297 int
    298 grfselect(dev, rw)
    299 	dev_t dev;
    300 	int rw;
    301 {
    302 	if (rw == FREAD)
    303 		return(0);
    304 	return(1);
    305 }
    306 
    307 /*
    308  * map the contents of a graphics display card into process'
    309  * memory space.
    310  */
    311 int
    312 grfmmap(dev, off, prot)
    313 	dev_t dev;
    314 	int off, prot;
    315 {
    316 	struct grf_softc *gp;
    317 	struct grfinfo *gi;
    318 
    319 	gp = grfsp[GRFUNIT(dev)];
    320 	gi = &gp->g_display;
    321 
    322 	/*
    323 	 * control registers
    324 	 */
    325 	if (off >= 0 && off < gi->gd_regsize)
    326 		return(((u_int)gi->gd_regaddr + off) >> PGSHIFT);
    327 
    328 	/*
    329 	 * frame buffer
    330 	 */
    331 	if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) {
    332 		off -= gi->gd_regsize;
    333 #ifdef BANKEDDEVPAGER
    334 		if (gi->gd_bank_size)
    335 			off %= gi->gd_bank_size;
    336 #endif
    337 		return(((u_int)gi->gd_fbaddr + off) >> PGSHIFT);
    338 	}
    339 	/* bogus */
    340 	return(-1);
    341 }
    342 
    343 int
    344 grfon(dev)
    345 	dev_t dev;
    346 {
    347 	struct grf_softc *gp;
    348 
    349 	gp = grfsp[GRFUNIT(dev)];
    350 
    351 	if (gp->g_flags & GF_GRFON)
    352 		return(0);
    353 
    354 	gp->g_flags |= GF_GRFON;
    355 	if (gp->g_itedev != NODEV)
    356 		ite_off(gp->g_itedev, 3);
    357 
    358 	return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON));
    359 }
    360 
    361 int
    362 grfoff(dev)
    363 	dev_t dev;
    364 {
    365 	struct grf_softc *gp;
    366 	int error;
    367 
    368 	gp = grfsp[GRFUNIT(dev)];
    369 
    370 	if ((gp->g_flags & GF_GRFON) == 0)
    371 		return(0);
    372 
    373 	gp->g_flags &= ~GF_GRFON;
    374 	error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF);
    375 
    376 	/*
    377 	 * Closely tied together no X's
    378 	 */
    379 	if (gp->g_itedev != NODEV)
    380 		ite_on(gp->g_itedev, 2);
    381 
    382 	return(error);
    383 }
    384 
    385 int
    386 grfsinfo(dev, dyninfo)
    387 	dev_t dev;
    388 	struct grfdyninfo *dyninfo;
    389 {
    390 	struct grf_softc *gp;
    391 	int error;
    392 
    393 	gp = grfsp[GRFUNIT(dev)];
    394 	error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo);
    395 
    396 	/*
    397 	 * Closely tied together no X's
    398 	 */
    399 	if (gp->g_itedev != NODEV)
    400 		ite_reinit(gp->g_itedev);
    401 	return(error);
    402 }
    403 
    404 #ifdef BANKEDDEVPAGER
    405 
    406 int
    407 grfbanked_get (dev, off, prot)
    408      dev_t dev;
    409      off_t off;
    410      int   prot;
    411 {
    412 	struct grf_softc *gp;
    413 	struct grfinfo *gi;
    414 	int error, bank;
    415 
    416 	gp = grfsp[GRFUNIT(dev)];
    417 	gi = &gp->g_display;
    418 
    419 	off -= gi->gd_regsize;
    420 	if (off < 0 || off >= gi->gd_fbsize)
    421 		return -1;
    422 
    423 	error = gp->g_mode(gp, GM_GRFGETBANK, &bank, off, prot);
    424 	return error ? -1 : bank;
    425 }
    426 
    427 int
    428 grfbanked_cur (dev)
    429 	dev_t dev;
    430 {
    431 	struct grf_softc *gp;
    432 	int error, bank;
    433 
    434 	gp = grfsp[GRFUNIT(dev)];
    435 
    436 	error = gp->g_mode(gp, GM_GRFGETCURBANK, &bank);
    437 	return(error ? -1 : bank);
    438 }
    439 
    440 int
    441 grfbanked_set (dev, bank)
    442 	dev_t dev;
    443 	int bank;
    444 {
    445 	struct grf_softc *gp;
    446 
    447 	gp = grfsp[GRFUNIT(dev)];
    448 	return(gp->g_mode(gp, GM_GRFSETBANK, bank) ? -1 : 0);
    449 }
    450 
    451 #endif /* BANKEDDEVPAGER */
    452 #endif	/* NGRF > 0 */
    453