grf.c revision 1.38 1 /* $NetBSD: grf.c,v 1.38 2009/03/14 15:36:03 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1995 Leo Weppelman
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 /*
42 * Copyright (c) 1988 University of Utah.
43 *
44 * This code is derived from software contributed to Berkeley by
45 * the Systems Programming Group of the University of Utah Computer
46 * Science Department.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. All advertising materials mentioning features or use of this software
57 * must display the following acknowledgement:
58 * This product includes software developed by the University of
59 * California, Berkeley and its contributors.
60 * 4. Neither the name of the University nor the names of its contributors
61 * may be used to endorse or promote products derived from this software
62 * without specific prior written permission.
63 *
64 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * SUCH DAMAGE.
75 *
76 * from: Utah $Hdr: grf.c 1.31 91/01/21$
77 *
78 * @(#)grf.c 7.8 (Berkeley) 5/7/91
79 */
80
81 /*
82 * Graphics display driver for the Atari
83 * This is the hardware-independent portion of the driver.
84 * Hardware access is through the grf_softc->g_mode routine.
85 */
86
87 #include <sys/cdefs.h>
88 __KERNEL_RCSID(0, "$NetBSD: grf.c,v 1.38 2009/03/14 15:36:03 dsl Exp $");
89
90 #include <sys/param.h>
91 #include <sys/proc.h>
92 #include <sys/ioctl.h>
93 #include <sys/device.h>
94 #include <sys/file.h>
95 #include <sys/malloc.h>
96 #include <sys/conf.h>
97 #include <sys/systm.h>
98 #include <sys/vnode.h>
99 #include <sys/mman.h>
100
101 #include <machine/cpu.h>
102
103 #include <uvm/uvm_extern.h>
104
105 #include <atari/atari/device.h>
106 #include <atari/dev/grfioctl.h>
107 #include <atari/dev/grfabs_reg.h>
108 #include <atari/dev/grfvar.h>
109 #include <atari/dev/itevar.h>
110 #include <atari/dev/viewioctl.h>
111 #include <atari/dev/viewvar.h>
112
113 #include "grfcc.h"
114 #include "grfet.h"
115 #define NGRF (NGRFCC + NGRFET)
116
117 #if NGRF > 0
118
119 #include "ite.h"
120 #if NITE == 0
121 #define ite_on(u,f)
122 #define ite_off(u,f)
123 #define ite_reinit(d)
124 #endif
125
126 int grfon(dev_t);
127 int grfoff(dev_t);
128 int grfsinfo(dev_t, struct grfdyninfo *);
129
130 int grfbusprint(void *auxp, const char *);
131 int grfbusmatch(struct device *, struct cfdata *, void *);
132 void grfbusattach(struct device *, struct device *, void *);
133
134 /*
135 * pointers to grf drivers device structs
136 */
137 struct grf_softc *grfsp[NGRF]; /* XXX */
138
139 CFATTACH_DECL(grfbus, sizeof(struct device),
140 grfbusmatch, grfbusattach, NULL, NULL);
141
142 extern struct cfdriver grfbus_cd;
143
144 dev_type_open(grfopen);
145 dev_type_close(grfclose);
146 dev_type_ioctl(grfioctl);
147 dev_type_mmap(grfmmap);
148
149 const struct cdevsw grf_cdevsw = {
150 grfopen, grfclose, noread, nowrite, grfioctl,
151 nostop, notty, nopoll, grfmmap, nokqfilter,
152 };
153
154 /*
155 * only used in console init.
156 */
157 static struct cfdata *cfdata_gbus = NULL;
158
159 int
160 grfbusmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
161 {
162 if(strcmp(auxp, grfbus_cd.cd_name))
163 return(0);
164
165 if(atari_realconfig == 0)
166 cfdata_gbus = cfp;
167 return(1); /* Always there */
168 }
169
170 void
171 grfbusattach(pdp, dp, auxp)
172 struct device *pdp, *dp;
173 void *auxp;
174 {
175 grf_auxp_t grf_auxp;
176
177 grf_auxp.busprint = grfbusprint;
178 grf_auxp.from_bus_match = 1;
179
180 if(dp == NULL) /* Console init */
181 atari_config_found(cfdata_gbus, NULL, (void*)&grf_auxp, grfbusprint);
182 else {
183 printf("\n");
184 config_found(dp, (void*)&grf_auxp, grfbusprint);
185 }
186 }
187
188 int
189 grfbusprint(void *auxp, const char *name)
190 {
191 if(name == NULL)
192 return(UNCONF);
193 return(QUIET);
194 }
195
196 /*ARGSUSED*/
197 int
198 grfopen(dev, flags, devtype, l)
199 dev_t dev;
200 int flags, devtype;
201 struct lwp *l;
202 {
203 struct grf_softc *gp;
204
205 if (GRFUNIT(dev) >= NGRF)
206 return(ENXIO);
207
208 gp = grfsp[GRFUNIT(dev)];
209 if (gp == NULL)
210 return(ENXIO);
211
212 if ((gp->g_flags & GF_ALIVE) == 0)
213 return(ENXIO);
214
215 if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
216 return(EBUSY);
217 grf_viewsync(gp);
218
219 return(0);
220 }
221
222 /*ARGSUSED*/
223 int
224 grfclose(dev_t dev, int flags, int mode, struct lwp *l)
225 {
226 struct grf_softc *gp;
227
228 gp = grfsp[GRFUNIT(dev)];
229 (void)grfoff(dev);
230 gp->g_flags &= GF_ALIVE;
231 return(0);
232 }
233
234 /*ARGSUSED*/
235 int
236 grfioctl(dev_t dev, u_long cmd, void * data, int flag, struct lwp *l)
237 {
238 struct grf_softc *gp;
239 int error;
240 extern const struct cdevsw view_cdevsw;
241
242 gp = grfsp[GRFUNIT(dev)];
243 error = 0;
244
245 switch (cmd) {
246 case OGRFIOCGINFO:
247 /* argl.. no bank-member.. */
248 bcopy((void *)&gp->g_display, data, sizeof(struct grfinfo)-4);
249 break;
250 case GRFIOCGINFO:
251 bcopy((void *)&gp->g_display, data, sizeof(struct grfinfo));
252 break;
253 case GRFIOCON:
254 error = grfon(dev);
255 break;
256 case GRFIOCOFF:
257 error = grfoff(dev);
258 break;
259 case GRFIOCSINFO:
260 error = grfsinfo(dev, (struct grfdyninfo *) data);
261 break;
262 case GRFGETVMODE:
263 return(gp->g_mode(gp, GM_GRFGETVMODE, data, 0, 0));
264 case GRFSETVMODE:
265 error = gp->g_mode(gp, GM_GRFSETVMODE, data, 0, 0);
266 if (error == 0 && gp->g_itedev)
267 ite_reinit(gp->g_itedev);
268 break;
269 case GRFGETNUMVM:
270 return(gp->g_mode(gp, GM_GRFGETNUMVM, data, 0, 0));
271 /*
272 * these are all hardware dependant, and have to be resolved
273 * in the respective driver.
274 */
275 case GRFIOCPUTCMAP:
276 case GRFIOCGETCMAP:
277 case GRFIOCSSPRITEPOS:
278 case GRFIOCGSPRITEPOS:
279 case GRFIOCSSPRITEINF:
280 case GRFIOCGSPRITEINF:
281 case GRFIOCGSPRITEMAX:
282 default:
283 /*
284 * check to see whether it's a command recognized by the
285 * view code.
286 */
287 return((*view_cdevsw.d_ioctl)(gp->g_viewdev, cmd, data, flag,
288 l));
289 error = EINVAL;
290 break;
291
292 }
293 return(error);
294 }
295
296 /*
297 * map the contents of a graphics display card into process'
298 * memory space.
299 */
300 paddr_t
301 grfmmap(dev_t dev, off_t off, int prot)
302 {
303 struct grf_softc *gp;
304 struct grfinfo *gi;
305 u_int vgabase, linbase;
306
307 gp = grfsp[GRFUNIT(dev)];
308 gi = &gp->g_display;
309
310 vgabase = gi->gd_vgabase;
311 linbase = gi->gd_linbase;
312
313 /*
314 * control registers
315 */
316 if (off >= 0 && off < gi->gd_regsize)
317 return(((paddr_t)gi->gd_regaddr + off) >> PGSHIFT);
318
319 /*
320 * VGA memory
321 */
322 if (off >= vgabase && off < (vgabase + gi->gd_vgasize))
323 return(((paddr_t)gi->gd_vgaaddr - vgabase + off) >> PGSHIFT);
324
325 /*
326 * frame buffer
327 */
328 if (off >= linbase && off < (linbase + gi->gd_fbsize))
329 return(((paddr_t)gi->gd_fbaddr - linbase + off) >> PGSHIFT);
330 return(-1);
331 }
332
333 int
334 grfon(dev_t dev)
335 {
336 struct grf_softc *gp;
337
338 gp = grfsp[GRFUNIT(dev)];
339
340 if (gp->g_flags & GF_GRFON)
341 return(0);
342
343 gp->g_flags |= GF_GRFON;
344 if (gp->g_itedev != NODEV)
345 ite_off(gp->g_itedev, 3);
346
347 return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON,
348 NULL, 0, 0));
349 }
350
351 int
352 grfoff(dev_t dev)
353 {
354 struct grf_softc *gp;
355 int error;
356
357 gp = grfsp[GRFUNIT(dev)];
358
359 if ((gp->g_flags & GF_GRFON) == 0)
360 return(0);
361
362 gp->g_flags &= ~GF_GRFON;
363 error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF,
364 NULL, 0, 0);
365
366 /*
367 * Closely tied together no X's
368 */
369 if (gp->g_itedev != NODEV)
370 ite_on(gp->g_itedev, 2);
371
372 return(error);
373 }
374
375 int
376 grfsinfo(dev_t dev, struct grfdyninfo *dyninfo)
377 {
378 struct grf_softc *gp;
379 int error;
380
381 gp = grfsp[GRFUNIT(dev)];
382 error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo, 0, 0);
383
384 /*
385 * Closely tied together no X's
386 */
387 if (gp->g_itedev != NODEV)
388 ite_reinit(gp->g_itedev);
389 return(error);
390 }
391
392 /*
393 * Get the grf-info in sync with underlying view.
394 */
395 void
396 grf_viewsync(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, (void *)&bm,
406 0, NOLWP);
407
408 gp->g_data = (void *) 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, (void *)&vs, 0,
420 NOLWP)) {
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, NOLWP);
464 return(0);
465 case GM_GRFOFF:
466 (*view_cdevsw.d_ioctl)(gp->g_viewdev, VIOCREMOVE,
467 NULL, 0, NOLWP);
468 return(0);
469 case GM_GRFCONFIG:
470 default:
471 break;
472 }
473 return(EPASSTHROUGH);
474 }
475 #endif /* NGRF > 0 */
476