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