grf.c revision 1.8 1 /* $NetBSD: grf.c,v 1.8 1996/03/17 01:26:47 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 <vm/vm.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_page.h>
65 #include <vm/vm_pager.h>
66 #include <machine/cpu.h>
67 #include <atari/atari/device.h>
68 #include <atari/dev/grfioctl.h>
69 #include <atari/dev/grfabs_reg.h>
70 #include <atari/dev/grfvar.h>
71 #include <atari/dev/itevar.h>
72 #include <atari/dev/viewioctl.h>
73 #include <atari/dev/viewvar.h>
74
75 #include "grf.h"
76 #if NGRF > 0
77
78 #include "ite.h"
79 #if NITE == 0
80 #define ite_on(u,f)
81 #define ite_off(u,f)
82 #define ite_reinit(d)
83 #endif
84
85 int grfopen __P((dev_t, int, int, struct proc *));
86 int grfclose __P((dev_t, int));
87 int grfioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
88 int grfselect __P((dev_t, int));
89 int grfmmap __P((dev_t, int, int));
90
91 int grfon __P((dev_t));
92 int grfoff __P((dev_t));
93 int grfsinfo __P((dev_t, struct grfdyninfo *));
94 #ifdef BANKEDDEVPAGER
95 int grfbanked_get __P((dev_t, off_t, int));
96 int grfbanked_cur __P((dev_t));
97 int grfbanked_set __P((dev_t, int));
98 #endif
99 static void grf_viewsync __P((struct grf_softc *));
100 static int grf_mode __P((struct grf_softc *, int, void *, int, int));
101
102 int grfbusprint __P((void *auxp, char *));
103 int grfbusmatch __P((struct device *, void *, void *));
104 void grfbusattach __P((struct device *, struct device *, void *));
105
106 void grfattach __P((struct device *, struct device *, void *));
107 int grfmatch __P((struct device *, void *, void *));
108 int grfprint __P((void *, char *));
109 /*
110 * pointers to grf drivers device structs
111 */
112 struct grf_softc *grfsp[NGRF];
113
114 struct cfattach grfbus_ca = {
115 sizeof(struct device), grfbusmatch, grfbusattach
116 };
117
118 struct cfdriver grfbus_cd = {
119 NULL, "grfbus", DV_DULL, sizeof(struct device)
120 };
121
122 struct cfattach grf_ca = {
123 sizeof(struct grf_softc), grfmatch, grfattach
124 };
125
126 struct cfdriver grf_cd = {
127 NULL, "grf", DV_DULL, NULL, 0
128 };
129
130 /*
131 * only used in console init.
132 */
133 static struct cfdata *cfdata_gbus = NULL;
134 static struct cfdata *cfdata_grf = NULL;
135
136 int
137 grfbusmatch(pdp, match, auxp)
138 struct device *pdp;
139 void *match, *auxp;
140 {
141 struct cfdata *cfp = match;
142
143 if(strcmp(auxp, grfbus_cd.cd_name))
144 return(0);
145
146 if((atari_realconfig == 0) || (cfdata_gbus == NULL)) {
147 /*
148 * Probe layers we depend on
149 */
150 if(grfabs_probe() == 0)
151 return(0);
152 viewprobe();
153
154 if(atari_realconfig == 0) {
155 /*
156 * XXX: console init opens view 0
157 */
158 if(viewopen(0, 0))
159 return(0);
160 cfdata_gbus = cfp;
161 }
162 }
163 return(1); /* Always there */
164 }
165
166 void
167 grfbusattach(pdp, dp, auxp)
168 struct device *pdp, *dp;
169 void *auxp;
170 {
171 static int did_cons = 0;
172 int i;
173
174 if(dp == NULL) { /* Console init */
175 did_cons = 1;
176 i = 0;
177 atari_config_found(cfdata_gbus, NULL, (void*)&i, grfbusprint);
178 }
179 else {
180 printf("\n");
181 for(i = 0; i < NGRF; i++) {
182 /*
183 * Skip opening view[0] when we this is the console.
184 */
185 if(!did_cons || (i > 0))
186 if(viewopen(i, 0))
187 break;
188 config_found(dp, (void*)&i, grfbusprint);
189 }
190 }
191 }
192
193 int
194 grfbusprint(auxp, name)
195 void *auxp;
196 char *name;
197 {
198 if(name == NULL)
199 return(UNCONF);
200 return(QUIET);
201 }
202
203
204 int
205 grfmatch(pdp, match, auxp)
206 struct device *pdp;
207 void *match, *auxp;
208 {
209 int unit = *(int*)auxp;
210 struct cfdata *cfp = match;
211
212 /*
213 * Match only on unit indicated by grfbus attach.
214 */
215 if(cfp->cf_unit != unit)
216 return(0);
217
218 cfdata_grf = cfp;
219 return(1);
220 }
221
222 /*
223 * attach: initialize the grf-structure and try to attach an ite to us.
224 * note : dp is NULL during early console init.
225 */
226 void
227 grfattach(pdp, dp, auxp)
228 struct device *pdp, *dp;
229 void *auxp;
230 {
231 static struct grf_softc congrf;
232 struct grf_softc *gp;
233 int maj;
234
235 /*
236 * find our major device number
237 */
238 for(maj = 0; maj < nchrdev; maj++)
239 if (cdevsw[maj].d_open == grfopen)
240 break;
241
242 /*
243 * Handle exeption case: early console init
244 */
245 if(dp == NULL) {
246 congrf.g_unit = 0;
247 congrf.g_grfdev = makedev(maj, 0);
248 congrf.g_flags = GF_ALIVE;
249 congrf.g_mode = grf_mode;
250 congrf.g_conpri = grfcc_cnprobe();
251 congrf.g_viewdev = congrf.g_unit;
252 grfcc_iteinit(&congrf);
253 grf_viewsync(&congrf);
254
255 /* Attach console ite */
256 atari_config_found(cfdata_grf, NULL, &congrf, grfprint);
257 return;
258 }
259
260 gp = (struct grf_softc *)dp;
261 gp->g_unit = gp->g_device.dv_unit;
262 grfsp[gp->g_unit] = gp;
263
264 if((cfdata_grf != NULL) && (gp->g_unit == 0)) {
265 /*
266 * We inited earlier just copy the info, take care
267 * not to copy the device struct though.
268 */
269 bcopy(&congrf.g_display, &gp->g_display,
270 (char *)&gp[1] - (char *)&gp->g_display);
271 }
272 else {
273 gp->g_grfdev = makedev(maj, gp->g_unit);
274 gp->g_flags = GF_ALIVE;
275 gp->g_mode = grf_mode;
276 gp->g_conpri = 0;
277 gp->g_viewdev = gp->g_unit;
278 grfcc_iteinit(gp);
279 grf_viewsync(gp);
280 }
281
282 printf(": width %d height %d", gp->g_display.gd_dwidth,
283 gp->g_display.gd_dheight);
284 if(gp->g_display.gd_colors == 2)
285 printf(" monochrome\n");
286 else printf(" colors %d\n", gp->g_display.gd_colors);
287
288 /*
289 * try and attach an ite
290 */
291 config_found(dp, gp, grfprint);
292 }
293
294 int
295 grfprint(auxp, pnp)
296 void *auxp;
297 char *pnp;
298 {
299 if(pnp)
300 printf("ite at %s", pnp);
301 return(UNCONF);
302 }
303
304 /*ARGSUSED*/
305 int
306 grfopen(dev, flags, devtype, p)
307 dev_t dev;
308 int flags, devtype;
309 struct proc *p;
310 {
311 struct grf_softc *gp;
312
313 if (GRFUNIT(dev) >= NGRF)
314 return(ENXIO);
315
316 gp = grfsp[GRFUNIT(dev)];
317
318 if ((gp->g_flags & GF_ALIVE) == 0)
319 return(ENXIO);
320
321 if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
322 return(EBUSY);
323 grf_viewsync(gp);
324
325 return(0);
326 }
327
328 /*ARGSUSED*/
329 int
330 grfclose(dev, flags)
331 dev_t dev;
332 int flags;
333 {
334 struct grf_softc *gp;
335
336 gp = grfsp[GRFUNIT(dev)];
337 (void)grfoff(dev);
338 gp->g_flags &= GF_ALIVE;
339 return(0);
340 }
341
342 /*ARGSUSED*/
343 int
344 grfioctl(dev, cmd, data, flag, p)
345 dev_t dev;
346 u_long cmd;
347 int flag;
348 caddr_t data;
349 struct proc *p;
350 {
351 struct grf_softc *gp;
352 int error;
353
354 gp = grfsp[GRFUNIT(dev)];
355 error = 0;
356
357 switch (cmd) {
358 case OGRFIOCGINFO:
359 /* argl.. no bank-member.. */
360 bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo)-4);
361 break;
362 case GRFIOCGINFO:
363 bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo));
364 break;
365 case GRFIOCON:
366 error = grfon(dev);
367 break;
368 case GRFIOCOFF:
369 error = grfoff(dev);
370 break;
371 case GRFIOCSINFO:
372 error = grfsinfo(dev, (struct grfdyninfo *) data);
373 break;
374 case GRFGETVMODE:
375 return(gp->g_mode(gp, GM_GRFGETVMODE, data, 0, 0));
376 case GRFSETVMODE:
377 error = gp->g_mode(gp, GM_GRFSETVMODE, data, 0, 0);
378 if (error == 0 && gp->g_itedev)
379 ite_reinit(gp->g_itedev);
380 break;
381 case GRFGETNUMVM:
382 return(gp->g_mode(gp, GM_GRFGETNUMVM, data, 0, 0));
383 /*
384 * these are all hardware dependant, and have to be resolved
385 * in the respective driver.
386 */
387 case GRFIOCPUTCMAP:
388 case GRFIOCGETCMAP:
389 case GRFIOCSSPRITEPOS:
390 case GRFIOCGSPRITEPOS:
391 case GRFIOCSSPRITEINF:
392 case GRFIOCGSPRITEINF:
393 case GRFIOCGSPRITEMAX:
394 default:
395 /*
396 * check to see whether it's a command recognized by the
397 * view code.
398 */
399 return(viewioctl(gp->g_viewdev, cmd, data, flag, p));
400 error = EINVAL;
401 break;
402
403 }
404 return(error);
405 }
406
407 /*ARGSUSED*/
408 int
409 grfselect(dev, rw)
410 dev_t dev;
411 int rw;
412 {
413 if (rw == FREAD)
414 return(0);
415 return(1);
416 }
417
418 /*
419 * map the contents of a graphics display card into process'
420 * memory space.
421 */
422 int
423 grfmmap(dev, off, prot)
424 dev_t dev;
425 int off, prot;
426 {
427 struct grf_softc *gp;
428 struct grfinfo *gi;
429
430 gp = grfsp[GRFUNIT(dev)];
431 gi = &gp->g_display;
432
433 /*
434 * frame buffer
435 */
436 if ((off >= 0) && (off < gi->gd_fbsize))
437 return (((u_int)gi->gd_fbaddr + off) >> PGSHIFT);
438 return(-1);
439 }
440
441 int
442 grfon(dev)
443 dev_t dev;
444 {
445 struct grf_softc *gp;
446
447 gp = grfsp[GRFUNIT(dev)];
448
449 if (gp->g_flags & GF_GRFON)
450 return(0);
451
452 gp->g_flags |= GF_GRFON;
453 if (gp->g_itedev != NODEV)
454 ite_off(gp->g_itedev, 3);
455
456 return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON,
457 NULL, 0, 0));
458 }
459
460 int
461 grfoff(dev)
462 dev_t dev;
463 {
464 struct grf_softc *gp;
465 int error;
466
467 gp = grfsp[GRFUNIT(dev)];
468
469 if ((gp->g_flags & GF_GRFON) == 0)
470 return(0);
471
472 gp->g_flags &= ~GF_GRFON;
473 error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF,
474 NULL, 0, 0);
475
476 /*
477 * Closely tied together no X's
478 */
479 if (gp->g_itedev != NODEV)
480 ite_on(gp->g_itedev, 2);
481
482 return(error);
483 }
484
485 int
486 grfsinfo(dev, dyninfo)
487 dev_t dev;
488 struct grfdyninfo *dyninfo;
489 {
490 struct grf_softc *gp;
491 int error;
492
493 gp = grfsp[GRFUNIT(dev)];
494 error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo, 0, 0);
495
496 /*
497 * Closely tied together no X's
498 */
499 if (gp->g_itedev != NODEV)
500 ite_reinit(gp->g_itedev);
501 return(error);
502 }
503
504 /*
505 * Get the grf-info in sync with underlying view.
506 */
507 static void
508 grf_viewsync(gp)
509 struct grf_softc *gp;
510 {
511 struct view_size vs;
512 bmap_t bm;
513 struct grfinfo *gi;
514
515 gi = &gp->g_display;
516
517 viewioctl(gp->g_viewdev, VIOCGBMAP, &bm, 0, -1);
518
519 gp->g_data = (caddr_t) 0xDeadBeaf; /* not particularly clean.. */
520
521 gi->gd_fbaddr = bm.hw_address;
522 gi->gd_fbsize = bm.depth*bm.bytes_per_row*bm.rows;
523
524 if(viewioctl(gp->g_viewdev, VIOCGSIZE, &vs, 0, -1)) {
525 /*
526 * fill in some default values...
527 * XXX: Should _never_ happen
528 */
529 vs.width = 640;
530 vs.height = 400;
531 vs.depth = 1;
532 }
533 gi->gd_colors = 1 << vs.depth;
534 gi->gd_planes = vs.depth;
535
536 gi->gd_fbwidth = vs.width;
537 gi->gd_fbheight = vs.height;
538 gi->gd_dyn.gdi_fbx = 0;
539 gi->gd_dyn.gdi_fby = 0;
540 gi->gd_dyn.gdi_dwidth = vs.width;
541 gi->gd_dyn.gdi_dheight = vs.height;
542 gi->gd_dyn.gdi_dx = 0;
543 gi->gd_dyn.gdi_dy = 0;
544 }
545
546 /*
547 * Change the mode of the display.
548 * Right now all we can do is grfon/grfoff.
549 * Return a UNIX error number or 0 for success.
550 */
551 /*ARGSUSED*/
552 static int
553 grf_mode(gp, cmd, arg, a2, a3)
554 struct grf_softc *gp;
555 int cmd, a2, a3;
556 void *arg;
557 {
558 switch (cmd) {
559 case GM_GRFON:
560 /*
561 * Get in sync with view, ite might have changed it.
562 */
563 grf_viewsync(gp);
564 viewioctl(gp->g_viewdev, VIOCDISPLAY, NULL, 0, -1);
565 return(0);
566 case GM_GRFOFF:
567 viewioctl(gp->g_viewdev, VIOCREMOVE, NULL, 0, -1);
568 return(0);
569 case GM_GRFCONFIG:
570 default:
571 break;
572 }
573 return(EINVAL);
574 }
575 #endif /* NGRF > 0 */
576