ofrom.c revision 1.8
1/*	$NetBSD: ofrom.c,v 1.8 2002/10/02 15:52:39 thorpej 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
61CFATTACH_DECL(ofrom, sizeof(struct ofrom_softc),
62    ofromprobe, ofromattach, NULL, NULL);
63
64extern struct cfdriver ofrom_cd;
65
66dev_type_open(ofromopen);
67dev_type_read(ofromrw);
68dev_type_mmap(ofrommmap);
69
70const struct cdevsw ofrom_cdevsw = {
71	ofromopen, nullclose, ofromrw, ofromrw, noioctl,
72	nostop, notty, nopoll, ofrommmap,
73};
74
75int
76ofromprobe(parent, cf, aux)
77	struct device *parent;
78	struct cfdata *cf;
79	void *aux;
80{
81	struct ofbus_attach_args *oba = aux;
82	static const char *const compatible_strings[] = { "rom", NULL };
83
84	return (of_compatible(oba->oba_phandle, compatible_strings) == -1) ?
85	    0 : 5;
86}
87
88
89void
90ofromattach(parent, self, aux)
91	struct device *parent, *self;
92	void *aux;
93{
94	struct ofrom_softc *sc = (struct ofrom_softc *)self;
95	struct ofbus_attach_args *oba = aux;
96	char regbuf[8];
97
98	if (OF_getproplen(oba->oba_phandle, "reg") != 8) {
99		printf(": invalid reg property\n");
100		return;
101	}
102	if (OF_getprop(oba->oba_phandle, "reg", regbuf, sizeof regbuf) != 8) {
103		printf(": couldn't read reg property\n");
104		return;
105	}
106	sc->base = of_decode_int(&regbuf[0]);
107	sc->size = of_decode_int(&regbuf[4]);
108	sc->enabled = 1;
109
110	printf(": %#lx-%#lx\n", sc->base, sc->base + sc->size - 1);
111}
112
113int
114ofromopen(dev, oflags, devtype, p)
115	dev_t dev;
116	int oflags, devtype;
117	struct proc *p;
118{
119	struct ofrom_softc *sc;
120	int unit = minor(dev);
121
122	if (unit >= ofrom_cd.cd_ndevs)
123		return (ENXIO);
124	sc = ofrom_cd.cd_devs[unit];
125	if (!sc || !sc->enabled)
126		return (ENXIO);
127
128	if (oflags & FWRITE)
129		return (EINVAL);
130
131	return (0);
132}
133
134int
135ofromrw(dev, uio, flags)
136	dev_t dev;
137	struct uio *uio;
138	int flags;
139{
140	struct ofrom_softc *sc;
141	int c, error = 0, unit = minor(dev);
142	struct iovec *iov;
143	vm_offset_t o, v;
144	extern int physlock;
145	extern char *memhook;
146
147	if (unit >= ofrom_cd.cd_ndevs)
148		return (ENXIO);			/* XXX PANIC */
149	sc = ofrom_cd.cd_devs[unit];
150	if (!sc || !sc->enabled)
151		return (ENXIO);			/* XXX PANIC */
152
153	/* lock against other uses of shared vmmap */
154	while (physlock > 0) {
155		physlock++;
156		error = tsleep((caddr_t)&physlock, PZERO | PCATCH, "ofromrw",
157		    0);
158		if (error)
159			return (error);
160	}
161	physlock = 1;
162
163	while (uio->uio_resid > 0 && error == 0) {
164		iov = uio->uio_iov;
165		if (iov->iov_len == 0) {
166			uio->uio_iov++;
167			uio->uio_iovcnt--;
168			if (uio->uio_iovcnt < 0)
169				panic("ofromrw");
170			continue;
171		}
172
173		/*
174		 * Since everything is page aligned and no more
175		 * than the rest of a page is done at once, we
176		 * can just check that the offset isn't too big.
177		 */
178		if (uio->uio_offset >= sc->size)
179			break;
180
181		v = sc->base + uio->uio_offset;
182		pmap_enter(pmap_kernel(), (vm_offset_t)memhook,
183		    trunc_page(v), uio->uio_rw == UIO_READ ?
184		    VM_PROT_READ : VM_PROT_WRITE, PMAP_WIRED);
185		pmap_update(pmap_kernel());
186		o = uio->uio_offset & PGOFSET;
187		c = min(uio->uio_resid, (int)(NBPG - o));
188		error = uiomove((caddr_t)memhook + o, c, uio);
189		pmap_remove(pmap_kernel(), (vm_offset_t)memhook,
190		    (vm_offset_t)memhook + NBPG);
191		pmap_update(pmap_kernel());
192	}
193
194	if (physlock > 1)
195		wakeup((caddr_t)&physlock);
196	physlock = 0;
197
198	return (error);
199}
200
201paddr_t
202ofrommmap(dev, off, prot)
203	dev_t dev;
204	off_t off;
205	int prot;
206{
207	struct ofrom_softc *sc;
208	int unit = minor(dev);
209
210	if (unit >= ofrom_cd.cd_ndevs)
211		return (-1);			/* XXX PANIC */
212	sc = ofrom_cd.cd_devs[unit];
213	if (!sc || !sc->enabled)
214		return (-1);			/* XXX PANIC */
215
216	if ((u_int)off >= sc->size)
217		return (-1);
218
219	return arm_btop(sc->base + off);
220}
221