grf.c revision 1.29 1 /* $NetBSD: grf.c,v 1.29 2002/10/02 05:04:25 thorpej 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
64 #include <machine/cpu.h>
65
66 #include <uvm/uvm_extern.h>
67
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
93 int grfbusprint __P((void *auxp, const char *));
94 int grfbusmatch __P((struct device *, struct cfdata *, void *));
95 void grfbusattach __P((struct device *, struct device *, void *));
96
97 /*
98 * pointers to grf drivers device structs
99 */
100 struct grf_softc *grfsp[NGRF]; /* XXX */
101
102 CFATTACH_DECL(grfbus, sizeof(struct device),
103 grfbusmatch, grfbusattach, NULL, NULL);
104
105 extern struct cfdriver grfbus_cd;
106
107 dev_type_open(grfopen);
108 dev_type_close(grfclose);
109 dev_type_ioctl(grfioctl);
110 dev_type_poll(grfpoll);
111 dev_type_mmap(grfmmap);
112
113 const struct cdevsw grf_cdevsw = {
114 grfopen, grfclose, noread, nowrite, grfioctl,
115 nostop, notty, grfpoll, grfmmap,
116 };
117
118 /*
119 * only used in console init.
120 */
121 static struct cfdata *cfdata_gbus = NULL;
122
123 int
124 grfbusmatch(pdp, cfp, auxp)
125 struct device *pdp;
126 struct cfdata *cfp;
127 void *auxp;
128 {
129 if(strcmp(auxp, grfbus_cd.cd_name))
130 return(0);
131
132 if(atari_realconfig == 0)
133 cfdata_gbus = cfp;
134 return(1); /* Always there */
135 }
136
137 void
138 grfbusattach(pdp, dp, auxp)
139 struct device *pdp, *dp;
140 void *auxp;
141 {
142 grf_auxp_t grf_auxp;
143
144 grf_auxp.busprint = grfbusprint;
145 grf_auxp.from_bus_match = 1;
146
147 if(dp == NULL) /* Console init */
148 atari_config_found(cfdata_gbus, NULL, (void*)&grf_auxp, grfbusprint);
149 else {
150 printf("\n");
151 config_found(dp, (void*)&grf_auxp, grfbusprint);
152 }
153 }
154
155 int
156 grfbusprint(auxp, name)
157 void *auxp;
158 const char *name;
159 {
160 if(name == NULL)
161 return(UNCONF);
162 return(QUIET);
163 }
164
165 /*ARGSUSED*/
166 int
167 grfopen(dev, flags, devtype, p)
168 dev_t dev;
169 int flags, devtype;
170 struct proc *p;
171 {
172 struct grf_softc *gp;
173
174 if (GRFUNIT(dev) >= NGRF)
175 return(ENXIO);
176
177 gp = grfsp[GRFUNIT(dev)];
178 if (gp == NULL)
179 return(ENXIO);
180
181 if ((gp->g_flags & GF_ALIVE) == 0)
182 return(ENXIO);
183
184 if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
185 return(EBUSY);
186 grf_viewsync(gp);
187
188 return(0);
189 }
190
191 /*ARGSUSED*/
192 int
193 grfclose(dev, flags, mode, p)
194 dev_t dev;
195 int flags;
196 int mode;
197 struct proc *p;
198 {
199 struct grf_softc *gp;
200
201 gp = grfsp[GRFUNIT(dev)];
202 (void)grfoff(dev);
203 gp->g_flags &= GF_ALIVE;
204 return(0);
205 }
206
207 /*ARGSUSED*/
208 int
209 grfioctl(dev, cmd, data, flag, p)
210 dev_t dev;
211 u_long cmd;
212 int flag;
213 caddr_t data;
214 struct proc *p;
215 {
216 struct grf_softc *gp;
217 int error;
218 extern const struct cdevsw view_cdevsw;
219
220 gp = grfsp[GRFUNIT(dev)];
221 error = 0;
222
223 switch (cmd) {
224 case OGRFIOCGINFO:
225 /* argl.. no bank-member.. */
226 bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo)-4);
227 break;
228 case GRFIOCGINFO:
229 bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo));
230 break;
231 case GRFIOCON:
232 error = grfon(dev);
233 break;
234 case GRFIOCOFF:
235 error = grfoff(dev);
236 break;
237 case GRFIOCSINFO:
238 error = grfsinfo(dev, (struct grfdyninfo *) data);
239 break;
240 case GRFGETVMODE:
241 return(gp->g_mode(gp, GM_GRFGETVMODE, data, 0, 0));
242 case GRFSETVMODE:
243 error = gp->g_mode(gp, GM_GRFSETVMODE, data, 0, 0);
244 if (error == 0 && gp->g_itedev)
245 ite_reinit(gp->g_itedev);
246 break;
247 case GRFGETNUMVM:
248 return(gp->g_mode(gp, GM_GRFGETNUMVM, data, 0, 0));
249 /*
250 * these are all hardware dependant, and have to be resolved
251 * in the respective driver.
252 */
253 case GRFIOCPUTCMAP:
254 case GRFIOCGETCMAP:
255 case GRFIOCSSPRITEPOS:
256 case GRFIOCGSPRITEPOS:
257 case GRFIOCSSPRITEINF:
258 case GRFIOCGSPRITEINF:
259 case GRFIOCGSPRITEMAX:
260 default:
261 /*
262 * check to see whether it's a command recognized by the
263 * view code.
264 */
265 return((*view_cdevsw.d_ioctl)(gp->g_viewdev, cmd, data, flag,
266 p));
267 error = EINVAL;
268 break;
269
270 }
271 return(error);
272 }
273
274 /*ARGSUSED*/
275 int
276 grfpoll(dev, events, p)
277 dev_t dev;
278 int events;
279 struct proc *p;
280 {
281 int revents = 0;
282
283 if (events & (POLLOUT | POLLWRNORM))
284 revents |= events & (POLLOUT | POLLWRNORM);
285 return (revents);
286 }
287
288 /*
289 * map the contents of a graphics display card into process'
290 * memory space.
291 */
292 paddr_t
293 grfmmap(dev, off, prot)
294 dev_t dev;
295 off_t off;
296 int prot;
297 {
298 struct grf_softc *gp;
299 struct grfinfo *gi;
300 u_int vgabase, linbase;
301
302 gp = grfsp[GRFUNIT(dev)];
303 gi = &gp->g_display;
304
305 vgabase = gi->gd_vgabase;
306 linbase = gi->gd_linbase;
307
308 /*
309 * control registers
310 */
311 if (off >= 0 && off < gi->gd_regsize)
312 return(((paddr_t)gi->gd_regaddr + off) >> PGSHIFT);
313
314 /*
315 * VGA memory
316 */
317 if (off >= vgabase && off < (vgabase + gi->gd_vgasize))
318 return(((paddr_t)gi->gd_vgaaddr - vgabase + off) >> PGSHIFT);
319
320 /*
321 * frame buffer
322 */
323 if (off >= linbase && off < (linbase + gi->gd_fbsize))
324 return(((paddr_t)gi->gd_fbaddr - linbase + off) >> PGSHIFT);
325 return(-1);
326 }
327
328 int
329 grfon(dev)
330 dev_t dev;
331 {
332 struct grf_softc *gp;
333
334 gp = grfsp[GRFUNIT(dev)];
335
336 if (gp->g_flags & GF_GRFON)
337 return(0);
338
339 gp->g_flags |= GF_GRFON;
340 if (gp->g_itedev != NODEV)
341 ite_off(gp->g_itedev, 3);
342
343 return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON,
344 NULL, 0, 0));
345 }
346
347 int
348 grfoff(dev)
349 dev_t dev;
350 {
351 struct grf_softc *gp;
352 int error;
353
354 gp = grfsp[GRFUNIT(dev)];
355
356 if ((gp->g_flags & GF_GRFON) == 0)
357 return(0);
358
359 gp->g_flags &= ~GF_GRFON;
360 error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF,
361 NULL, 0, 0);
362
363 /*
364 * Closely tied together no X's
365 */
366 if (gp->g_itedev != NODEV)
367 ite_on(gp->g_itedev, 2);
368
369 return(error);
370 }
371
372 int
373 grfsinfo(dev, dyninfo)
374 dev_t dev;
375 struct grfdyninfo *dyninfo;
376 {
377 struct grf_softc *gp;
378 int error;
379
380 gp = grfsp[GRFUNIT(dev)];
381 error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo, 0, 0);
382
383 /*
384 * Closely tied together no X's
385 */
386 if (gp->g_itedev != NODEV)
387 ite_reinit(gp->g_itedev);
388 return(error);
389 }
390
391 /*
392 * Get the grf-info in sync with underlying view.
393 */
394 void
395 grf_viewsync(gp)
396 struct grf_softc *gp;
397 {
398 struct view_size vs;
399 bmap_t bm;
400 struct grfinfo *gi;
401 extern const struct cdevsw view_cdevsw;
402
403 gi = &gp->g_display;
404
405 (*view_cdevsw.d_ioctl)(gp->g_viewdev, VIOCGBMAP, (caddr_t)&bm,
406 0, NOPROC);
407
408 gp->g_data = (caddr_t) 0xDeadBeaf; /* not particularly clean.. */
409
410 gi->gd_fbaddr = bm.hw_address;
411 gi->gd_fbsize = bm.phys_mappable;
412 gi->gd_linbase = bm.lin_base;
413 gi->gd_regaddr = bm.hw_regs;
414 gi->gd_regsize = bm.reg_size;
415 gi->gd_vgaaddr = bm.vga_address;
416 gi->gd_vgasize = bm.vga_mappable;
417 gi->gd_vgabase = bm.vga_base;
418
419 if((*view_cdevsw.d_ioctl)(gp->g_viewdev, VIOCGSIZE, (caddr_t)&vs, 0,
420 NOPROC)) {
421 /*
422 * fill in some default values...
423 * XXX: Should _never_ happen
424 */
425 vs.width = 640;
426 vs.height = 400;
427 vs.depth = 1;
428 }
429 gi->gd_colors = 1 << vs.depth;
430 gi->gd_planes = vs.depth;
431
432 gi->gd_fbwidth = vs.width;
433 gi->gd_fbheight = vs.height;
434 gi->gd_dyn.gdi_fbx = 0;
435 gi->gd_dyn.gdi_fby = 0;
436 gi->gd_dyn.gdi_dwidth = vs.width;
437 gi->gd_dyn.gdi_dheight = vs.height;
438 gi->gd_dyn.gdi_dx = 0;
439 gi->gd_dyn.gdi_dy = 0;
440 }
441
442 /*
443 * Change the mode of the display.
444 * Right now all we can do is grfon/grfoff.
445 * Return a UNIX error number or 0 for success.
446 */
447 /*ARGSUSED*/
448 int
449 grf_mode(gp, cmd, arg, a2, a3)
450 struct grf_softc *gp;
451 int cmd, a2, a3;
452 void *arg;
453 {
454 extern const struct cdevsw view_cdevsw;
455
456 switch (cmd) {
457 case GM_GRFON:
458 /*
459 * Get in sync with view, ite might have changed it.
460 */
461 grf_viewsync(gp);
462 (*view_cdevsw.d_ioctl)(gp->g_viewdev, VIOCDISPLAY,
463 NULL, 0, NOPROC);
464 return(0);
465 case GM_GRFOFF:
466 (*view_cdevsw.d_ioctl)(gp->g_viewdev, VIOCREMOVE,
467 NULL, 0, NOPROC);
468 return(0);
469 case GM_GRFCONFIG:
470 default:
471 break;
472 }
473 return(EPASSTHROUGH);
474 }
475 #endif /* NGRF > 0 */
476