xref: /netbsd-src/sys/arch/atari/dev/grf.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: grf.c,v 1.16 1996/10/13 04:10:55 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Leo Weppelman
5  * Copyright (c) 1988 University of Utah.
6  * Copyright (c) 1990 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * the Systems Programming Group of the University of Utah Computer
11  * Science Department.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  * from: Utah $Hdr: grf.c 1.31 91/01/21$
42  *
43  *	@(#)grf.c	7.8 (Berkeley) 5/7/91
44  */
45 
46 /*
47  * Graphics display driver for the Atari
48  * This is the hardware-independent portion of the driver.
49  * Hardware access is through the grf_softc->g_mode routine.
50  */
51 
52 #include <sys/param.h>
53 #include <sys/proc.h>
54 #include <sys/ioctl.h>
55 #include <sys/device.h>
56 #include <sys/file.h>
57 #include <sys/malloc.h>
58 #include <sys/conf.h>
59 #include <sys/systm.h>
60 #include <sys/vnode.h>
61 #include <sys/mman.h>
62 #include <sys/poll.h>
63 #include <vm/vm.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_pager.h>
67 #include <machine/cpu.h>
68 #include <atari/atari/device.h>
69 #include <atari/dev/grfioctl.h>
70 #include <atari/dev/grfabs_reg.h>
71 #include <atari/dev/grfvar.h>
72 #include <atari/dev/itevar.h>
73 #include <atari/dev/viewioctl.h>
74 #include <atari/dev/viewvar.h>
75 
76 #include "grfcc.h"
77 #include "grfet.h"
78 #define	NGRF	(NGRFCC + NGRFET)
79 
80 #if NGRF > 0
81 
82 #include "ite.h"
83 #if NITE == 0
84 #define	ite_on(u,f)
85 #define	ite_off(u,f)
86 #define ite_reinit(d)
87 #endif
88 
89 int grfon __P((dev_t));
90 int grfoff __P((dev_t));
91 int grfsinfo __P((dev_t, struct grfdyninfo *));
92 #ifdef BANKEDDEVPAGER
93 int grfbanked_get __P((dev_t, off_t, int));
94 int grfbanked_cur __P((dev_t));
95 int grfbanked_set __P((dev_t, int));
96 #endif
97 
98 int grfbusprint __P((void *auxp, const char *));
99 int grfbusmatch __P((struct device *, void *, void *));
100 void grfbusattach __P((struct device *, struct device *, void *));
101 
102 /*
103  * pointers to grf drivers device structs
104  */
105 struct grf_softc *grfsp[NGRF]; /* XXX */
106 
107 struct cfattach grfbus_ca = {
108 	sizeof(struct device), grfbusmatch, grfbusattach
109 };
110 
111 struct cfdriver grfbus_cd = {
112 	NULL, "grfbus", DV_DULL
113 };
114 
115 /*
116  * only used in console init.
117  */
118 static struct cfdata *cfdata_gbus  = NULL;
119 
120 int
121 grfbusmatch(pdp, match, auxp)
122 struct device	*pdp;
123 void		*match, *auxp;
124 {
125 	struct cfdata *cfp = match;
126 
127 	if(strcmp(auxp, grfbus_cd.cd_name))
128 		return(0);
129 
130 	if(atari_realconfig == 0)
131 		cfdata_gbus = cfp;
132 	return(1);	/* Always there	*/
133 }
134 
135 void
136 grfbusattach(pdp, dp, auxp)
137 struct device	*pdp, *dp;
138 void		*auxp;
139 {
140     grf_auxp_t	grf_auxp;
141 
142     grf_auxp.busprint       = grfbusprint;
143     grf_auxp.from_bus_match = 1;
144 
145     if(dp == NULL) /* Console init	*/
146 	atari_config_found(cfdata_gbus, NULL, (void*)&grf_auxp, grfbusprint);
147     else {
148 	printf("\n");
149 	config_found(dp, (void*)&grf_auxp, grfbusprint);
150     }
151 }
152 
153 int
154 grfbusprint(auxp, name)
155 void		*auxp;
156 const char	*name;
157 {
158 	if(name == NULL)
159 		return(UNCONF);
160 	return(QUIET);
161 }
162 
163 /*ARGSUSED*/
164 int
165 grfopen(dev, flags, devtype, p)
166 	dev_t dev;
167 	int flags, devtype;
168 	struct proc *p;
169 {
170 	struct grf_softc *gp;
171 
172 	if (GRFUNIT(dev) >= NGRF)
173 		return(ENXIO);
174 
175 	gp = grfsp[GRFUNIT(dev)];
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 	grf_viewsync(gp);
183 
184 	return(0);
185 }
186 
187 /*ARGSUSED*/
188 int
189 grfclose(dev, flags, mode, p)
190 	dev_t		dev;
191 	int		flags;
192 	int		mode;
193 	struct proc	*p;
194 {
195 	struct grf_softc *gp;
196 
197 	gp = grfsp[GRFUNIT(dev)];
198 	(void)grfoff(dev);
199 	gp->g_flags &= GF_ALIVE;
200 	return(0);
201 }
202 
203 /*ARGSUSED*/
204 int
205 grfioctl(dev, cmd, data, flag, p)
206 dev_t		dev;
207 u_long		cmd;
208 int		flag;
209 caddr_t		data;
210 struct proc	*p;
211 {
212 	struct grf_softc	*gp;
213 	int			error;
214 
215 	gp = grfsp[GRFUNIT(dev)];
216 	error = 0;
217 
218 	switch (cmd) {
219 	case OGRFIOCGINFO:
220 	        /* argl.. no bank-member.. */
221 	  	bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo)-4);
222 		break;
223 	case GRFIOCGINFO:
224 		bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo));
225 		break;
226 	case GRFIOCON:
227 		error = grfon(dev);
228 		break;
229 	case GRFIOCOFF:
230 		error = grfoff(dev);
231 		break;
232 	case GRFIOCSINFO:
233 		error = grfsinfo(dev, (struct grfdyninfo *) data);
234 		break;
235 	case GRFGETVMODE:
236 		return(gp->g_mode(gp, GM_GRFGETVMODE, data, 0, 0));
237 	case GRFSETVMODE:
238 		error = gp->g_mode(gp, GM_GRFSETVMODE, data, 0, 0);
239 		if (error == 0 && gp->g_itedev)
240 			ite_reinit(gp->g_itedev);
241 		break;
242 	case GRFGETNUMVM:
243 		return(gp->g_mode(gp, GM_GRFGETNUMVM, data, 0, 0));
244 	/*
245 	 * these are all hardware dependant, and have to be resolved
246 	 * in the respective driver.
247 	 */
248 	case GRFIOCPUTCMAP:
249 	case GRFIOCGETCMAP:
250 	case GRFIOCSSPRITEPOS:
251 	case GRFIOCGSPRITEPOS:
252 	case GRFIOCSSPRITEINF:
253 	case GRFIOCGSPRITEINF:
254 	case GRFIOCGSPRITEMAX:
255 	default:
256 		/*
257 		 * check to see whether it's a command recognized by the
258 		 * view code.
259 		 */
260 		return(viewioctl(gp->g_viewdev, cmd, data, flag, p));
261 		error = EINVAL;
262 		break;
263 
264 	}
265 	return(error);
266 }
267 
268 /*ARGSUSED*/
269 int
270 grfpoll(dev, events, p)
271 	dev_t		dev;
272 	int		events;
273 	struct proc	*p;
274 {
275 	int revents = 0;
276 
277 	if (events & (POLLOUT | POLLWRNORM))
278 		revents |= events & (POLLOUT | POLLWRNORM);
279 	return (revents);
280 }
281 
282 /*
283  * map the contents of a graphics display card into process'
284  * memory space.
285  */
286 int
287 grfmmap(dev, off, prot)
288 dev_t	dev;
289 int	off, prot;
290 {
291 	struct grf_softc	*gp;
292 	struct grfinfo		*gi;
293 
294 	gp = grfsp[GRFUNIT(dev)];
295 	gi = &gp->g_display;
296 
297 	/*
298 	 * frame buffer
299 	 */
300 	if ((off >= 0) && (off < gi->gd_fbsize))
301 		return (((u_int)gi->gd_fbaddr + off) >> PGSHIFT);
302 	return(-1);
303 }
304 
305 int
306 grfon(dev)
307 	dev_t dev;
308 {
309 	struct grf_softc *gp;
310 
311 	gp = grfsp[GRFUNIT(dev)];
312 
313 	if (gp->g_flags & GF_GRFON)
314 		return(0);
315 
316 	gp->g_flags |= GF_GRFON;
317 	if (gp->g_itedev != NODEV)
318 		ite_off(gp->g_itedev, 3);
319 
320 	return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON,
321 							NULL, 0, 0));
322 }
323 
324 int
325 grfoff(dev)
326 	dev_t dev;
327 {
328 	struct grf_softc *gp;
329 	int error;
330 
331 	gp = grfsp[GRFUNIT(dev)];
332 
333 	if ((gp->g_flags & GF_GRFON) == 0)
334 		return(0);
335 
336 	gp->g_flags &= ~GF_GRFON;
337 	error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF,
338 						NULL, 0, 0);
339 
340 	/*
341 	 * Closely tied together no X's
342 	 */
343 	if (gp->g_itedev != NODEV)
344 		ite_on(gp->g_itedev, 2);
345 
346 	return(error);
347 }
348 
349 int
350 grfsinfo(dev, dyninfo)
351 	dev_t dev;
352 	struct grfdyninfo *dyninfo;
353 {
354 	struct grf_softc *gp;
355 	int error;
356 
357 	gp = grfsp[GRFUNIT(dev)];
358 	error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo, 0, 0);
359 
360 	/*
361 	 * Closely tied together no X's
362 	 */
363 	if (gp->g_itedev != NODEV)
364 		ite_reinit(gp->g_itedev);
365 	return(error);
366 }
367 
368 /*
369  * Get the grf-info in sync with underlying view.
370  */
371 void
372 grf_viewsync(gp)
373 struct grf_softc *gp;
374 {
375 	struct view_size	vs;
376 	bmap_t			bm;
377 	struct grfinfo		*gi;
378 
379 	gi = &gp->g_display;
380 
381 	viewioctl(gp->g_viewdev, VIOCGBMAP, (caddr_t)&bm, 0, NOPROC);
382 
383 	gp->g_data = (caddr_t) 0xDeadBeaf; /* not particularly clean.. */
384 
385 	gi->gd_fbaddr  = bm.hw_address;
386 	gi->gd_fbsize  = bm.depth*bm.bytes_per_row*bm.rows;
387 	gi->gd_regaddr = bm.hw_regs;
388 	gi->gd_regsize = bm.reg_size;
389 
390 	if(viewioctl(gp->g_viewdev, VIOCGSIZE, (caddr_t)&vs, 0, NOPROC)) {
391 		/*
392 		 * fill in some default values...
393 		 * XXX: Should _never_ happen
394 		 */
395 		vs.width  = 640;
396 		vs.height = 400;
397 		vs.depth  = 1;
398 	}
399 	gi->gd_colors = 1 << vs.depth;
400 	gi->gd_planes = vs.depth;
401 
402 	gi->gd_fbwidth         = vs.width;
403 	gi->gd_fbheight        = vs.height;
404 	gi->gd_dyn.gdi_fbx     = 0;
405 	gi->gd_dyn.gdi_fby     = 0;
406 	gi->gd_dyn.gdi_dwidth  = vs.width;
407 	gi->gd_dyn.gdi_dheight = vs.height;
408 	gi->gd_dyn.gdi_dx      = 0;
409 	gi->gd_dyn.gdi_dy      = 0;
410 }
411 
412 /*
413  * Change the mode of the display.
414  * Right now all we can do is grfon/grfoff.
415  * Return a UNIX error number or 0 for success.
416  */
417 /*ARGSUSED*/
418 int
419 grf_mode(gp, cmd, arg, a2, a3)
420 struct grf_softc	*gp;
421 int			cmd, a2, a3;
422 void			*arg;
423 {
424 	switch (cmd) {
425 		case GM_GRFON:
426 			/*
427 			 * Get in sync with view, ite might have changed it.
428 			 */
429 			grf_viewsync(gp);
430 			viewioctl(gp->g_viewdev, VIOCDISPLAY, NULL, 0, NOPROC);
431 			return(0);
432 	case GM_GRFOFF:
433 			viewioctl(gp->g_viewdev, VIOCREMOVE, NULL, 0, NOPROC);
434 			return(0);
435 	case GM_GRFCONFIG:
436 	default:
437 			break;
438 	}
439 	return(EINVAL);
440 }
441 #endif	/* NGRF > 0 */
442