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