No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hp300 / dev / grf.c
blob3530712b184f2df1e1ab4cace488b223992c41ac
1 /* $NetBSD: grf.c,v 1.65 2008/06/13 09:41:15 cegger Exp $ */
3 /*
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 * from: Utah $Hdr: grf.c 1.36 93/08/13$
37 * @(#)grf.c 8.4 (Berkeley) 1/12/94
40 * Copyright (c) 1988 University of Utah.
42 * This code is derived from software contributed to Berkeley by
43 * the Systems Programming Group of the University of Utah Computer
44 * Science Department.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
74 * from: Utah $Hdr: grf.c 1.36 93/08/13$
76 * @(#)grf.c 8.4 (Berkeley) 1/12/94
80 * Graphics display driver for HP 300/400/700/800 machines.
81 * This is the hardware-independent portion of the driver.
82 * Hardware access is through the machine dependent grf switch routines.
85 #include <sys/cdefs.h>
86 __KERNEL_RCSID(0, "$NetBSD: grf.c,v 1.65 2008/06/13 09:41:15 cegger Exp $");
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/conf.h>
91 #include <sys/device.h>
92 #include <sys/file.h>
93 #include <sys/ioctl.h>
94 #include <sys/malloc.h>
95 #include <sys/mman.h>
96 #include <sys/proc.h>
97 #include <sys/vnode.h>
98 #include <sys/resourcevar.h>
100 #include <machine/autoconf.h>
101 #include <machine/cpu.h>
103 #include <hp300/dev/intioreg.h>
104 #include <hp300/dev/grfioctl.h>
105 #include <hp300/dev/grfvar.h>
106 #include <hp300/dev/grfreg.h>
108 #include <uvm/uvm_extern.h>
109 #include <uvm/uvm_map.h>
111 #include <miscfs/specfs/specdev.h>
113 #include "ite.h"
114 #if NITE > 0
115 #include <hp300/dev/itevar.h>
116 #else
117 #define iteon(u,f) 0 /* noramlly returns int */
118 #define iteoff(u,f)
119 #endif /* NITE > 0 */
121 #include "ioconf.h"
123 static int grfmatch(device_t, cfdata_t, void *);
124 static void grfattach(device_t, device_t, void *);
126 CFATTACH_DECL_NEW(grf, sizeof(struct grf_softc),
127 grfmatch, grfattach, NULL, NULL);
129 static dev_type_open(grfopen);
130 static dev_type_close(grfclose);
131 static dev_type_ioctl(grfioctl);
132 static dev_type_mmap(grfmmap);
134 const struct cdevsw grf_cdevsw = {
135 grfopen, grfclose, nullread, nullwrite, grfioctl,
136 nostop, notty, nopoll, grfmmap, nokqfilter,
139 static int grfprint(void *, const char *);
142 * Frambuffer state information, statically allocated for benefit
143 * of the console.
145 struct grf_data grf_cn;
147 #ifdef DEBUG
148 int grfdebug = 0;
149 #define GDB_DEVNO 0x01
150 #define GDB_MMAP 0x02
151 #define GDB_IOMAP 0x04
152 #define GDB_LOCK 0x08
153 #endif
155 static int
156 grfmatch(device_t parent, cfdata_t cf, void *aux)
159 return 1;
162 static void
163 grfattach(device_t parent, device_t self, void *aux)
165 struct grf_softc *sc = device_private(self);
166 struct grfdev_attach_args *ga = aux;
168 sc->sc_dev = self;
169 aprint_normal("\n");
171 sc->sc_data = ga->ga_data;
172 sc->sc_scode = ga->ga_scode; /* XXX */
174 /* Attach an ITE. */
175 (void)config_found(self, aux, grfprint);
178 static int
179 grfprint(void *aux, const char *pnp)
182 /* Only ITEs can attach to GRFs, easy... */
183 if (pnp)
184 aprint_normal("ite at %s", pnp);
186 return UNCONF;
189 /*ARGSUSED*/
190 static int
191 grfopen(dev_t dev, int flags, int mode, struct lwp *l)
193 struct grf_softc *sc;
194 struct grf_data *gp;
195 int error = 0;
197 sc = device_lookup_private(&grf_cd, GRFUNIT(dev));
198 if (sc == NULL)
199 return ENXIO;
201 gp = sc->sc_data;
203 if ((gp->g_flags & GF_ALIVE) == 0)
204 return ENXIO;
206 if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
207 return EBUSY;
209 * First open.
210 * XXX: always put in graphics mode.
212 error = 0;
213 if ((gp->g_flags & GF_OPEN) == 0) {
214 gp->g_flags |= GF_OPEN;
215 error = grfon(dev);
217 return error;
220 /*ARGSUSED*/
221 static int
222 grfclose(dev_t dev, int flags, int mode, struct lwp *l)
224 struct grf_softc *sc;
225 struct grf_data *gp;
227 sc = device_lookup_private(&grf_cd, GRFUNIT(dev));
229 gp = sc->sc_data;
231 if ((gp->g_flags & GF_ALIVE) == 0)
232 return ENXIO;
234 (void) grfoff(dev);
235 gp->g_flags &= GF_ALIVE;
236 return 0;
239 /*ARGSUSED*/
240 static int
241 grfioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
243 struct grf_softc *sc;
244 struct grf_data *gp;
245 int error;
247 sc = device_lookup_private(&grf_cd, GRFUNIT(dev));
249 gp = sc->sc_data;
251 if ((gp->g_flags & GF_ALIVE) == 0)
252 return ENXIO;
254 error = 0;
255 switch (cmd) {
257 case GRFIOCGINFO:
258 memcpy(data, (void *)&gp->g_display, sizeof(struct grfinfo));
259 break;
261 case GRFIOCON:
262 error = grfon(dev);
263 break;
265 case GRFIOCOFF:
266 error = grfoff(dev);
267 break;
269 case GRFIOCMAP:
270 error = grfmap(dev, (void **)data, l->l_proc);
271 break;
273 case GRFIOCUNMAP:
274 error = grfunmap(dev, *(void **)data, l->l_proc);
275 break;
277 default:
278 error = EINVAL;
279 break;
282 return error;
285 /*ARGSUSED*/
286 static paddr_t
287 grfmmap(dev_t dev, off_t off, int prot)
289 struct grf_softc *sc = device_lookup_private(&grf_cd,GRFUNIT(dev));
291 return grfaddr(sc, off);
295 grfon(dev_t dev/*XXX*/)
297 struct grf_softc *sc;
298 struct grf_data *gp;
300 sc = device_lookup_private(&grf_cd, GRFUNIT(dev));
301 gp = sc->sc_data;
304 * XXX: iteoff call relies on devices being in same order
305 * as ITEs and the fact that iteoff only uses the minor part
306 * of the dev arg.
308 iteoff(sc->sc_ite->sc_data, 3);
309 return (*gp->g_sw->gd_mode)(gp,
310 (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON, (void *)0);
314 grfoff(dev_t dev/*XXX*/)
316 struct grf_softc *sc;
317 struct grf_data *gp;
318 int error;
320 sc = device_lookup_private(&grf_cd, GRFUNIT(dev));
321 gp = sc->sc_data;
323 (void) grfunmap(dev, (void *)0, curproc);
324 error = (*gp->g_sw->gd_mode)(gp,
325 (dev&GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF,
326 (void *)0);
327 /* XXX: see comment for iteoff above */
328 (void) iteon(sc->sc_ite->sc_data, 2);
329 return error;
332 paddr_t
333 grfaddr(struct grf_softc *sc, off_t off)
335 struct grf_data *gp= sc->sc_data;
336 struct grfinfo *gi = &gp->g_display;
338 /* control registers */
339 if (off >= 0 && off < gi->gd_regsize)
340 return ((paddr_t)gi->gd_regaddr + off) >> PGSHIFT;
342 /* frame buffer */
343 if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) {
344 off -= gi->gd_regsize;
345 return ((paddr_t)gi->gd_fbaddr + off) >> PGSHIFT;
347 /* bogus */
348 return -1;
352 grfmap(dev_t dev, void **addrp, struct proc *p)
354 struct grf_softc *sc = device_lookup_private(&grf_cd,GRFUNIT(dev));
355 struct grf_data *gp = sc->sc_data;
356 int len, error;
357 struct vnode vn;
358 int flags;
360 #ifdef DEBUG
361 if (grfdebug & GDB_MMAP)
362 printf("grfmap(%d): addr %p\n", p->p_pid, *addrp);
363 #endif
364 len = gp->g_display.gd_regsize + gp->g_display.gd_fbsize;
365 flags = MAP_SHARED;
366 if (*addrp)
367 flags |= MAP_FIXED;
368 else
369 *addrp = (void *)
370 VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, len);
371 vn.v_type = VCHR; /* XXX */
372 vn.v_rdev = dev; /* XXX */
373 error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
374 (vsize_t)len, VM_PROT_READ|VM_PROT_WRITE,
375 VM_PROT_READ|VM_PROT_WRITE,
376 flags, (void *)&vn, 0,
377 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
378 if (error == 0)
379 (void) (*gp->g_sw->gd_mode)(gp, GM_MAP, *addrp);
380 return error;
384 grfunmap(dev_t dev, void *addr, struct proc *p)
386 struct grf_softc *sc = device_lookup_private(&grf_cd,GRFUNIT(dev));
387 struct grf_data *gp = sc->sc_data;
388 vsize_t size;
390 #ifdef DEBUG
391 if (grfdebug & GDB_MMAP)
392 printf("grfunmap(%d): dev %x addr %p\n", p->p_pid, dev, addr);
393 #endif
394 if (addr == 0)
395 return EINVAL; /* XXX: how do we deal with this? */
396 (void) (*gp->g_sw->gd_mode)(gp, GM_UNMAP, 0);
397 size = round_page(gp->g_display.gd_regsize + gp->g_display.gd_fbsize);
398 uvm_unmap(&p->p_vmspace->vm_map, (vaddr_t)addr, (vaddr_t)addr + size);
399 return 0;