grf.c revision 1.39 1 /* $NetBSD: grf.c,v 1.39 2002/01/26 13:40:54 aymeric 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: Utah $Hdr: grf.c 1.31 91/01/21$
41 *
42 * @(#)grf.c 7.8 (Berkeley) 5/7/91
43 */
44
45 /*
46 * Graphics display driver for the Amiga
47 * This is the hardware-independent portion of the driver.
48 * Hardware access is through the grf_softc->g_mode routine.
49 */
50
51 #include <sys/param.h>
52 #include <sys/proc.h>
53 #include <sys/ioctl.h>
54 #include <sys/device.h>
55 #include <sys/file.h>
56 #include <sys/malloc.h>
57 #include <sys/systm.h>
58 #include <sys/vnode.h>
59 #include <sys/mman.h>
60 #include <sys/poll.h>
61 #include <uvm/uvm_extern.h>
62 #include <machine/cpu.h>
63 #include <dev/sun/fbio.h>
64 #include <amiga/amiga/color.h> /* DEBUG */
65 #include <amiga/amiga/device.h>
66 #include <amiga/dev/grfioctl.h>
67 #include <amiga/dev/grfvar.h>
68 #include <amiga/dev/itevar.h>
69 #include <amiga/dev/viewioctl.h>
70
71 #include <sys/conf.h>
72 #include <machine/conf.h>
73
74 #include "view.h"
75 #include "grf.h"
76
77 #if NGRF > 0
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 grfon(dev_t);
86 int grfoff(dev_t);
87 int grfsinfo(dev_t, struct grfdyninfo *);
88 #ifdef BANKEDDEVPAGER
89 int grfbanked_get(dev_t, off_t, int);
90 int grfbanked_cur(dev_t);
91 int grfbanked_set(dev_t, int);
92 #endif
93
94 void grfattach(struct device *, struct device *, void *);
95 int grfmatch(struct device *, struct cfdata *, void *);
96 int grfprint(void *, const char *);
97 /*
98 * pointers to grf drivers device structs
99 */
100 struct grf_softc *grfsp[NGRF];
101
102 struct cfattach grf_ca = {
103 sizeof(struct device), grfmatch, grfattach
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 for(maj = 0; maj < nchrdev; maj++)
143 if (cdevsw[maj].d_open == grfopen)
144 break;
145
146 gp->g_grfdev = makedev(maj, gp->g_unit);
147 if (dp != NULL) {
148 printf(": width %d height %d", gp->g_display.gd_dwidth,
149 gp->g_display.gd_dheight);
150 if (gp->g_display.gd_colors == 2)
151 printf(" monochrome\n");
152 else
153 printf(" colors %d\n", gp->g_display.gd_colors);
154 }
155
156 /*
157 * try and attach an ite
158 */
159 amiga_config_found(cfdata, dp, gp, grfprint);
160 }
161
162 int
163 grfprint(void *auxp, const char *pnp)
164 {
165 if (pnp)
166 printf("ite at %s", pnp);
167 return(UNCONF);
168 }
169
170 /*ARGSUSED*/
171 int
172 grfopen(dev_t dev, int flags, int devtype, struct proc *p)
173 {
174 struct grf_softc *gp;
175
176 if (GRFUNIT(dev) >= NGRF || (gp = grfsp[GRFUNIT(dev)]) == NULL)
177 return(ENXIO);
178
179 if ((gp->g_flags & GF_ALIVE) == 0)
180 return(ENXIO);
181
182 if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
183 return(EBUSY);
184
185 return(0);
186 }
187
188 /*ARGSUSED*/
189 int
190 grfclose(dev_t dev, int flags, int mode, struct proc *p)
191 {
192 struct grf_softc *gp;
193
194 gp = grfsp[GRFUNIT(dev)];
195 (void)grfoff(dev);
196 gp->g_flags &= GF_ALIVE;
197 return(0);
198 }
199
200 /*ARGSUSED*/
201 int
202 grfioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
203 {
204 struct grf_softc *gp;
205 int error;
206
207 gp = grfsp[GRFUNIT(dev)];
208 error = 0;
209
210 switch (cmd) {
211 case OGRFIOCGINFO:
212 /* argl.. no bank-member.. */
213 bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo)-4);
214 break;
215 case GRFIOCGINFO:
216 bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo));
217 break;
218 case GRFIOCON:
219 error = grfon(dev);
220 break;
221 case GRFIOCOFF:
222 error = grfoff(dev);
223 break;
224 case GRFIOCSINFO:
225 error = grfsinfo(dev, (struct grfdyninfo *) data);
226 break;
227 case GRFGETVMODE:
228 return(gp->g_mode(gp, GM_GRFGETVMODE, data, 0, 0));
229 case GRFSETVMODE:
230 error = gp->g_mode(gp, GM_GRFSETVMODE, data, 0, 0);
231 if (error == 0 && gp->g_itedev && !(gp->g_flags & GF_GRFON))
232 ite_reinit(gp->g_itedev);
233 break;
234 case GRFGETNUMVM:
235 return(gp->g_mode(gp, GM_GRFGETNUMVM, data, 0, 0));
236 /*
237 * these are all hardware dependant, and have to be resolved
238 * in the respective driver.
239 */
240 case GRFIOCPUTCMAP:
241 case GRFIOCGETCMAP:
242 case GRFIOCSSPRITEPOS:
243 case GRFIOCGSPRITEPOS:
244 case GRFIOCSSPRITEINF:
245 case GRFIOCGSPRITEINF:
246 case GRFIOCGSPRITEMAX:
247 case GRFIOCBITBLT:
248 case GRFIOCSETMON:
249 case GRFTOGGLE: /* Toggles between Cirrus boards and native ECS on
250 Amiga. 15/11/94 ill */
251 /*
252 * We need the minor dev number to get the overlay/image
253 * information for grf_ul.
254 */
255 return(gp->g_mode(gp, GM_GRFIOCTL, data, cmd, dev));
256
257 case GRFIOCBLANK: /* blank ioctl, IOCON/OFF will turn ite on */
258 case FBIOSVIDEO:
259 error = gp->g_mode(gp, GM_GRFIOCTL, data, GRFIOCBLANK, dev);
260 if (!error)
261 gp->g_blank = *(int *)data;
262 return (error);
263
264 case FBIOGVIDEO:
265 *(int *)data = gp->g_blank;
266 return (0);
267
268 default:
269 #if NVIEW > 0
270 /*
271 * check to see whether it's a command recognized by the
272 * view code if the unit is 0
273 * XXX
274 */
275 if (GRFUNIT(dev) == 0)
276 return(viewioctl(dev, cmd, data, flag, p));
277 #endif
278 error = EINVAL;
279 break;
280
281 }
282 return(error);
283 }
284
285 /*ARGSUSED*/
286 int
287 grfpoll(dev_t dev, int events, struct proc *p)
288 {
289 return(events & (POLLOUT | POLLWRNORM));
290 }
291
292 /*
293 * map the contents of a graphics display card into process'
294 * memory space.
295 */
296 paddr_t
297 grfmmap(dev_t dev, off_t off, int prot)
298 {
299 struct grf_softc *gp;
300 struct grfinfo *gi;
301
302 gp = grfsp[GRFUNIT(dev)];
303 gi = &gp->g_display;
304
305 /*
306 * control registers
307 */
308 if (off >= 0 && off < gi->gd_regsize)
309 return(((paddr_t)gi->gd_regaddr + off) >> PGSHIFT);
310
311 /*
312 * frame buffer
313 */
314 if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) {
315 off -= gi->gd_regsize;
316 #ifdef BANKEDDEVPAGER
317 if (gi->gd_bank_size)
318 off %= gi->gd_bank_size;
319 #endif
320 return(((paddr_t)gi->gd_fbaddr + off) >> PGSHIFT);
321 }
322 /* bogus */
323 return(-1);
324 }
325
326 int
327 grfon(dev_t dev)
328 {
329 struct grf_softc *gp;
330
331 gp = grfsp[GRFUNIT(dev)];
332
333 if (gp->g_flags & GF_GRFON)
334 return(0);
335
336 gp->g_flags |= GF_GRFON;
337 if (gp->g_itedev != NODEV)
338 ite_off(gp->g_itedev, 3);
339
340 return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON,
341 NULL, 0, 0));
342 }
343
344 int
345 grfoff(dev_t dev)
346 {
347 struct grf_softc *gp;
348 int error;
349
350 gp = grfsp[GRFUNIT(dev)];
351
352 if ((gp->g_flags & GF_GRFON) == 0)
353 return(0);
354
355 gp->g_flags &= ~GF_GRFON;
356 error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF,
357 NULL, 0, 0);
358
359 /*
360 * Closely tied together no X's
361 */
362 if (gp->g_itedev != NODEV)
363 ite_on(gp->g_itedev, 2);
364
365 return(error);
366 }
367
368 int
369 grfsinfo(dev_t dev, struct grfdyninfo *dyninfo)
370 {
371 struct grf_softc *gp;
372 int error;
373
374 gp = grfsp[GRFUNIT(dev)];
375 error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo, 0, 0);
376
377 /*
378 * Closely tied together no X's
379 */
380 if (gp->g_itedev != NODEV)
381 ite_reinit(gp->g_itedev);
382 return(error);
383 }
384
385 #ifdef BANKEDDEVPAGER
386
387 int
388 grfbanked_get(dev_t dev, off_t off, int prot)
389 {
390 struct grf_softc *gp;
391 struct grfinfo *gi;
392 int error, bank;
393
394 gp = grfsp[GRFUNIT(dev)];
395 gi = &gp->g_display;
396
397 off -= gi->gd_regsize;
398 if (off < 0 || off >= gi->gd_fbsize)
399 return -1;
400
401 error = gp->g_mode(gp, GM_GRFGETBANK, &bank, off, prot);
402 return error ? -1 : bank;
403 }
404
405 int
406 grfbanked_cur(dev_t dev)
407 {
408 struct grf_softc *gp;
409 int error, bank;
410
411 gp = grfsp[GRFUNIT(dev)];
412
413 error = gp->g_mode(gp, GM_GRFGETCURBANK, &bank, 0, 0);
414 return(error ? -1 : bank);
415 }
416
417 int
418 grfbanked_set(dev_t dev, int bank)
419 {
420 struct grf_softc *gp;
421
422 gp = grfsp[GRFUNIT(dev)];
423 return(gp->g_mode(gp, GM_GRFSETBANK, &bank, 0, 0) ? -1 : 0);
424 }
425
426 #endif /* BANKEDDEVPAGER */
427 #endif /* NGRF > 0 */
428