Home | History | Annotate | Line # | Download | only in dev
grf.c revision 1.34
      1 /*	$NetBSD: grf.c,v 1.34 1999/08/16 19:55:27 is 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/systm.h>
     58 #include <sys/vnode.h>
     59 #include <sys/mman.h>
     60 #include <sys/poll.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 <machine/fbio.h>
     67 #include <amiga/amiga/color.h>	/* DEBUG */
     68 #include <amiga/amiga/device.h>
     69 #include <amiga/dev/grfioctl.h>
     70 #include <amiga/dev/grfvar.h>
     71 #include <amiga/dev/itevar.h>
     72 #include <amiga/dev/viewioctl.h>
     73 
     74 #include <sys/conf.h>
     75 #include <machine/conf.h>
     76 
     77 #include "view.h"
     78 #include "grf.h"
     79 
     80 #if NGRF > 0
     81 #include "ite.h"
     82 #if NITE == 0
     83 #define	ite_on(u,f)
     84 #define	ite_off(u,f)
     85 #define ite_reinit(d)
     86 #endif
     87 
     88 int grfon __P((dev_t));
     89 int grfoff __P((dev_t));
     90 int grfsinfo __P((dev_t, struct grfdyninfo *));
     91 #ifdef BANKEDDEVPAGER
     92 int grfbanked_get __P((dev_t, off_t, int));
     93 int grfbanked_cur __P((dev_t));
     94 int grfbanked_set __P((dev_t, int));
     95 #endif
     96 
     97 void grfattach __P((struct device *, struct device *, void *));
     98 int grfmatch __P((struct device *, struct cfdata *, void *));
     99 int grfprint __P((void *, const char *));
    100 /*
    101  * pointers to grf drivers device structs
    102  */
    103 struct grf_softc *grfsp[NGRF];
    104 
    105 struct cfattach grf_ca = {
    106 	sizeof(struct device), grfmatch, grfattach
    107 };
    108 
    109 /*
    110  * only used in console init.
    111  */
    112 static struct cfdata *cfdata;
    113 
    114 /*
    115  * match if the unit of grf matches its perspective
    116  * low level board driver.
    117  */
    118 int
    119 grfmatch(pdp, cfp, auxp)
    120 	struct device *pdp;
    121 	struct cfdata *cfp;
    122 	void *auxp;
    123 {
    124 
    125 	if (cfp->cf_unit != ((struct grf_softc *)pdp)->g_unit)
    126 		return(0);
    127 	cfdata = cfp;
    128 	return(1);
    129 }
    130 
    131 /*
    132  * attach.. plug pointer in and print some info.
    133  * then try and attach an ite to us. note: dp is NULL
    134  * durring console init.
    135  */
    136 void
    137 grfattach(pdp, dp, auxp)
    138 	struct device *pdp, *dp;
    139 	void *auxp;
    140 {
    141 	struct grf_softc *gp;
    142 	int maj;
    143 
    144 	gp = (struct grf_softc *)pdp;
    145 	grfsp[gp->g_unit] = (struct grf_softc *)pdp;
    146 
    147 	/*
    148 	 * find our major device number
    149 	 */
    150 	for(maj = 0; maj < nchrdev; maj++)
    151 		if (cdevsw[maj].d_open == grfopen)
    152 			break;
    153 
    154 	gp->g_grfdev = makedev(maj, gp->g_unit);
    155 	if (dp != NULL) {
    156 		printf(": width %d height %d", gp->g_display.gd_dwidth,
    157 		    gp->g_display.gd_dheight);
    158 		if (gp->g_display.gd_colors == 2)
    159 			printf(" monochrome\n");
    160 		else
    161 			printf(" colors %d\n", gp->g_display.gd_colors);
    162 	}
    163 
    164 	/*
    165 	 * try and attach an ite
    166 	 */
    167 	amiga_config_found(cfdata, dp, gp, grfprint);
    168 }
    169 
    170 int
    171 grfprint(auxp, pnp)
    172 	void *auxp;
    173 	const char *pnp;
    174 {
    175 	if (pnp)
    176 		printf("ite at %s", pnp);
    177 	return(UNCONF);
    178 }
    179 
    180 /*ARGSUSED*/
    181 int
    182 grfopen(dev, flags, devtype, p)
    183 	dev_t dev;
    184 	int flags, devtype;
    185 	struct proc *p;
    186 {
    187 	struct grf_softc *gp;
    188 
    189 	if (GRFUNIT(dev) >= NGRF || (gp = grfsp[GRFUNIT(dev)]) == NULL)
    190 		return(ENXIO);
    191 
    192 	if ((gp->g_flags & GF_ALIVE) == 0)
    193 		return(ENXIO);
    194 
    195 	if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
    196 		return(EBUSY);
    197 
    198 	return(0);
    199 }
    200 
    201 /*ARGSUSED*/
    202 int
    203 grfclose(dev, flags, mode, p)
    204 	dev_t dev;
    205 	int flags;
    206 	int mode;
    207 	struct proc *p;
    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, 0, 0));
    251 	case GRFSETVMODE:
    252 		error = gp->g_mode(gp, GM_GRFSETVMODE, data, 0, 0);
    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, 0, 0));
    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 GRFTOGGLE: /* Toggles between Cirrus boards and native ECS on
    272                      Amiga. 15/11/94 ill */
    273 		/*
    274 		 * We need the minor dev number to get the overlay/image
    275 		 * information for grf_ul.
    276 		 */
    277 		return(gp->g_mode(gp, GM_GRFIOCTL, data, cmd, dev));
    278 
    279 	case GRFIOCBLANK:	/* blank ioctl, IOCON/OFF will turn ite on */
    280 	case FBIOSVIDEO:
    281 		error = gp->g_mode(gp, GM_GRFIOCTL, data, GRFIOCBLANK, dev);
    282 		if (!error)
    283 			gp->g_blank = *(int *)data;
    284 		return (error);
    285 
    286 	case FBIOGVIDEO:
    287 		*(int *)data = gp->g_blank;
    288 		return (0);
    289 
    290 	default:
    291 #if NVIEW > 0
    292 		/*
    293 		 * check to see whether it's a command recognized by the
    294 		 * view code if the unit is 0
    295 		 * XXX
    296 		 */
    297 		if (GRFUNIT(dev) == 0)
    298 			return(viewioctl(dev, cmd, data, flag, p));
    299 #endif
    300 		error = EINVAL;
    301 		break;
    302 
    303 	}
    304 	return(error);
    305 }
    306 
    307 /*ARGSUSED*/
    308 int
    309 grfpoll(dev, events, p)
    310 	dev_t dev;
    311 	int events;
    312 	struct proc *p;
    313 {
    314 	return(events & (POLLOUT | POLLWRNORM));
    315 }
    316 
    317 /*
    318  * map the contents of a graphics display card into process'
    319  * memory space.
    320  */
    321 int
    322 grfmmap(dev, off, prot)
    323 	dev_t dev;
    324 	int off, prot;
    325 {
    326 	struct grf_softc *gp;
    327 	struct grfinfo *gi;
    328 
    329 	gp = grfsp[GRFUNIT(dev)];
    330 	gi = &gp->g_display;
    331 
    332 	/*
    333 	 * control registers
    334 	 */
    335 	if (off >= 0 && off < gi->gd_regsize)
    336 		return(((u_int)gi->gd_regaddr + off) >> PGSHIFT);
    337 
    338 	/*
    339 	 * frame buffer
    340 	 */
    341 	if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) {
    342 		off -= gi->gd_regsize;
    343 #ifdef BANKEDDEVPAGER
    344 		if (gi->gd_bank_size)
    345 			off %= gi->gd_bank_size;
    346 #endif
    347 		return(((u_int)gi->gd_fbaddr + off) >> PGSHIFT);
    348 	}
    349 	/* bogus */
    350 	return(-1);
    351 }
    352 
    353 int
    354 grfon(dev)
    355 	dev_t dev;
    356 {
    357 	struct grf_softc *gp;
    358 
    359 	gp = grfsp[GRFUNIT(dev)];
    360 
    361 	if (gp->g_flags & GF_GRFON)
    362 		return(0);
    363 
    364 	gp->g_flags |= GF_GRFON;
    365 	if (gp->g_itedev != NODEV)
    366 		ite_off(gp->g_itedev, 3);
    367 
    368 	return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON,
    369 							NULL, 0, 0));
    370 }
    371 
    372 int
    373 grfoff(dev)
    374 	dev_t dev;
    375 {
    376 	struct grf_softc *gp;
    377 	int error;
    378 
    379 	gp = grfsp[GRFUNIT(dev)];
    380 
    381 	if ((gp->g_flags & GF_GRFON) == 0)
    382 		return(0);
    383 
    384 	gp->g_flags &= ~GF_GRFON;
    385 	error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF,
    386 							NULL, 0, 0);
    387 
    388 	/*
    389 	 * Closely tied together no X's
    390 	 */
    391 	if (gp->g_itedev != NODEV)
    392 		ite_on(gp->g_itedev, 2);
    393 
    394 	return(error);
    395 }
    396 
    397 int
    398 grfsinfo(dev, dyninfo)
    399 	dev_t dev;
    400 	struct grfdyninfo *dyninfo;
    401 {
    402 	struct grf_softc *gp;
    403 	int error;
    404 
    405 	gp = grfsp[GRFUNIT(dev)];
    406 	error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo, 0, 0);
    407 
    408 	/*
    409 	 * Closely tied together no X's
    410 	 */
    411 	if (gp->g_itedev != NODEV)
    412 		ite_reinit(gp->g_itedev);
    413 	return(error);
    414 }
    415 
    416 #ifdef BANKEDDEVPAGER
    417 
    418 int
    419 grfbanked_get (dev, off, prot)
    420      dev_t dev;
    421      off_t off;
    422      int   prot;
    423 {
    424 	struct grf_softc *gp;
    425 	struct grfinfo *gi;
    426 	int error, bank;
    427 
    428 	gp = grfsp[GRFUNIT(dev)];
    429 	gi = &gp->g_display;
    430 
    431 	off -= gi->gd_regsize;
    432 	if (off < 0 || off >= gi->gd_fbsize)
    433 		return -1;
    434 
    435 	error = gp->g_mode(gp, GM_GRFGETBANK, &bank, off, prot);
    436 	return error ? -1 : bank;
    437 }
    438 
    439 int
    440 grfbanked_cur (dev)
    441 	dev_t dev;
    442 {
    443 	struct grf_softc *gp;
    444 	int error, bank;
    445 
    446 	gp = grfsp[GRFUNIT(dev)];
    447 
    448 	error = gp->g_mode(gp, GM_GRFGETCURBANK, &bank, 0, 0);
    449 	return(error ? -1 : bank);
    450 }
    451 
    452 int
    453 grfbanked_set (dev, bank)
    454 	dev_t dev;
    455 	int bank;
    456 {
    457 	struct grf_softc *gp;
    458 
    459 	gp = grfsp[GRFUNIT(dev)];
    460 	return(gp->g_mode(gp, GM_GRFSETBANK, &bank, 0, 0) ? -1 : 0);
    461 }
    462 
    463 #endif /* BANKEDDEVPAGER */
    464 #endif	/* NGRF > 0 */
    465