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