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