ofrom.c revision 1.4
1/*	$NetBSD: ofrom.c,v 1.4 2002/06/08 16:44:14 yamt Exp $	*/
2
3/*
4 * Copyright 1998
5 * Digital Equipment Corporation. All rights reserved.
6 *
7 * This software is furnished under license and may be used and
8 * copied only in accordance with the following terms and conditions.
9 * Subject to these conditions, you may download, copy, install,
10 * use, modify and distribute this software in source and/or binary
11 * form. No title or ownership is transferred hereby.
12 *
13 * 1) Any source code used, modified or distributed must reproduce
14 *    and retain this copyright notice and list of conditions as
15 *    they appear in the source file.
16 *
17 * 2) No right is granted to use any trade name, trademark, or logo of
18 *    Digital Equipment Corporation. Neither the "Digital Equipment
19 *    Corporation" name nor any trademark or logo of Digital Equipment
20 *    Corporation may be used to endorse or promote products derived
21 *    from this software without the prior written permission of
22 *    Digital Equipment Corporation.
23 *
24 * 3) This software is provided "AS-IS" and any express or implied
25 *    warranties, including but not limited to, any implied warranties
26 *    of merchantability, fitness for a particular purpose, or
27 *    non-infringement are disclaimed. In no event shall DIGITAL be
28 *    liable for any damages whatsoever, and in particular, DIGITAL
29 *    shall not be liable for special, indirect, consequential, or
30 *    incidental damages or damages for lost profits, loss of
31 *    revenue or loss of use, whether such damages arise in contract,
32 *    negligence, tort, under statute, in equity, at law or otherwise,
33 *    even if advised of the possibility of such damage.
34 */
35
36/*
37 * XXX open for writing (for user programs to mmap, and write contents)
38 */
39
40#include <sys/param.h>
41#include <sys/device.h>
42#include <sys/systm.h>
43#include <sys/conf.h>
44#include <sys/fcntl.h>
45
46#include <uvm/uvm_extern.h>
47
48#include <machine/bus.h>
49#include <dev/ofw/openfirm.h>
50
51struct ofrom_softc {
52	struct device	sc_dev;
53	int		enabled;
54	vm_offset_t	base;
55	vm_size_t	size;
56};
57
58int ofromprobe __P((struct device *, struct cfdata *, void *));
59void ofromattach __P((struct device *, struct device *, void *));
60
61struct cfattach ofrom_ca = {
62	sizeof(struct ofrom_softc), ofromprobe, ofromattach
63};
64
65extern struct cfdriver ofrom_cd;
66
67cdev_decl(ofrom);
68
69int
70ofromprobe(parent, cf, aux)
71	struct device *parent;
72	struct cfdata *cf;
73	void *aux;
74{
75	struct ofbus_attach_args *oba = aux;
76	static const char *const compatible_strings[] = { "rom", NULL };
77
78	return (of_compatible(oba->oba_phandle, compatible_strings) == -1) ?
79	    0 : 5;
80}
81
82
83void
84ofromattach(parent, self, aux)
85	struct device *parent, *self;
86	void *aux;
87{
88	struct ofrom_softc *sc = (struct ofrom_softc *)self;
89	struct ofbus_attach_args *oba = aux;
90	char regbuf[8];
91
92	if (OF_getproplen(oba->oba_phandle, "reg") != 8) {
93		printf(": invalid reg property\n");
94		return;
95	}
96	if (OF_getprop(oba->oba_phandle, "reg", regbuf, sizeof regbuf) != 8) {
97		printf(": couldn't read reg property\n");
98		return;
99	}
100	sc->base = of_decode_int(&regbuf[0]);
101	sc->size = of_decode_int(&regbuf[4]);
102	sc->enabled = 1;
103
104	printf(": %#lx-%#lx\n", sc->base, sc->base + sc->size - 1);
105}
106
107int
108ofromopen(dev, oflags, devtype, p)
109	dev_t dev;
110	int oflags, devtype;
111	struct proc *p;
112{
113	struct ofrom_softc *sc;
114	int unit = minor(dev);
115
116	if (unit >= ofrom_cd.cd_ndevs)
117		return (ENXIO);
118	sc = ofrom_cd.cd_devs[unit];
119	if (!sc || !sc->enabled)
120		return (ENXIO);
121
122	if (oflags & FWRITE)
123		return (EINVAL);
124
125	return (0);
126}
127
128int
129ofromclose(dev, fflag, devtype, p)
130	dev_t dev;
131	int fflag, devtype;
132	struct proc *p;
133{
134
135	return (0);
136}
137
138int
139ofromrw(dev, uio, flags)
140	dev_t dev;
141	struct uio *uio;
142	int flags;
143{
144	struct ofrom_softc *sc;
145	int c, error = 0, unit = minor(dev);
146	struct iovec *iov;
147	vm_offset_t o, v;
148	extern int physlock;
149	extern char *memhook;
150
151	if (unit >= ofrom_cd.cd_ndevs)
152		return (ENXIO);			/* XXX PANIC */
153	sc = ofrom_cd.cd_devs[unit];
154	if (!sc || !sc->enabled)
155		return (ENXIO);			/* XXX PANIC */
156
157	/* lock against other uses of shared vmmap */
158	while (physlock > 0) {
159		physlock++;
160		error = tsleep((caddr_t)&physlock, PZERO | PCATCH, "ofromrw",
161		    0);
162		if (error)
163			return (error);
164	}
165	physlock = 1;
166
167	while (uio->uio_resid > 0 && error == 0) {
168		iov = uio->uio_iov;
169		if (iov->iov_len == 0) {
170			uio->uio_iov++;
171			uio->uio_iovcnt--;
172			if (uio->uio_iovcnt < 0)
173				panic("ofromrw");
174			continue;
175		}
176
177		/*
178		 * Since everything is page aligned and no more
179		 * than the rest of a page is done at once, we
180		 * can just check that the offset isn't too big.
181		 */
182		if (uio->uio_offset >= sc->size)
183			break;
184
185		v = sc->base + uio->uio_offset;
186		pmap_enter(pmap_kernel(), (vm_offset_t)memhook,
187		    trunc_page(v), uio->uio_rw == UIO_READ ?
188		    VM_PROT_READ : VM_PROT_WRITE, PMAP_WIRED);
189		pmap_update(pmap_kernel());
190		o = uio->uio_offset & PGOFSET;
191		c = min(uio->uio_resid, (int)(NBPG - o));
192		error = uiomove((caddr_t)memhook + o, c, uio);
193		pmap_remove(pmap_kernel(), (vm_offset_t)memhook,
194		    (vm_offset_t)memhook + NBPG);
195		pmap_update(pmap_kernel());
196	}
197
198	if (physlock > 1)
199		wakeup((caddr_t)&physlock);
200	physlock = 0;
201
202	return (error);
203}
204
205int
206ofromioctl(dev, cmd, data, flag, p)
207	dev_t dev;
208	u_long cmd;
209	caddr_t data;
210	int flag;
211	struct proc *p;
212{
213
214	return (ENOTTY);
215}
216
217paddr_t
218ofrommmap(dev, off, prot)
219	dev_t dev;
220	off_t off;
221	int prot;
222{
223	struct ofrom_softc *sc;
224	int unit = minor(dev);
225
226	if (unit >= ofrom_cd.cd_ndevs)
227		return (-1);			/* XXX PANIC */
228	sc = ofrom_cd.cd_devs[unit];
229	if (!sc || !sc->enabled)
230		return (-1);			/* XXX PANIC */
231
232	if ((u_int)off >= sc->size)
233		return (-1);
234
235	return arm_btop(sc->base + off);
236}
237