ofrom.c revision 1.25
1/*	$NetBSD: ofrom.c,v 1.25 2014/03/16 05:20:25 dholland 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/cdefs.h>
41__KERNEL_RCSID(0, "$NetBSD: ofrom.c,v 1.25 2014/03/16 05:20:25 dholland Exp $");
42
43#include <sys/param.h>
44#include <sys/device.h>
45#include <sys/systm.h>
46#include <sys/conf.h>
47#include <sys/fcntl.h>
48#include <sys/bus.h>
49
50#include <uvm/uvm_extern.h>
51
52#include <dev/ofw/openfirm.h>
53
54struct ofrom_softc {
55	int		enabled;
56	paddr_t		base;
57	paddr_t		size;
58};
59
60int ofromprobe(device_t, cfdata_t, void *);
61void ofromattach(device_t, device_t, void *);
62
63CFATTACH_DECL_NEW(ofrom, sizeof(struct ofrom_softc),
64    ofromprobe, ofromattach, NULL, NULL);
65
66extern struct cfdriver ofrom_cd;
67
68dev_type_open(ofromopen);
69dev_type_read(ofromrw);
70dev_type_mmap(ofrommmap);
71
72const struct cdevsw ofrom_cdevsw = {
73	.d_open = ofromopen,
74	.d_close = nullclose,
75	.d_read = ofromrw,
76	.d_write = ofromrw,
77	.d_ioctl = noioctl,
78	.d_stop = nostop,
79	.d_tty = notty,
80	.d_poll = nopoll,
81	.d_mmap = ofrommmap,
82	.d_kqfilter = nokqfilter,
83	.d_flag = 0
84};
85
86int
87ofromprobe(device_t parent, cfdata_t cf, void *aux)
88{
89	struct ofbus_attach_args *oba = aux;
90	static const char *const compatible_strings[] = { "rom", NULL };
91
92	return (of_compatible(oba->oba_phandle, compatible_strings) == -1) ?
93	    0 : 5;
94}
95
96
97void
98ofromattach(device_t parent, device_t self, void *aux)
99{
100	struct ofrom_softc *sc = device_private(self);
101	struct ofbus_attach_args *oba = aux;
102	char regbuf[8];
103
104	if (OF_getproplen(oba->oba_phandle, "reg") != 8) {
105		printf(": invalid reg property\n");
106		return;
107	}
108	if (OF_getprop(oba->oba_phandle, "reg", regbuf, sizeof regbuf) != 8) {
109		printf(": couldn't read reg property\n");
110		return;
111	}
112	sc->base = of_decode_int(&regbuf[0]);
113	sc->size = of_decode_int(&regbuf[4]);
114	sc->enabled = 1;
115
116	printf(": %#lx-%#lx\n", sc->base, sc->base + sc->size - 1);
117}
118
119int
120ofromopen(dev_t dev, int oflags, int devtype, struct lwp *l)
121{
122	struct ofrom_softc *sc;
123
124	sc = device_lookup_private(&ofrom_cd, minor(dev));
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_t dev, struct uio *uio, int flags)
136{
137	pmap_t kpm = pmap_kernel();
138	struct ofrom_softc *sc;
139	int c, error = 0;
140	struct iovec *iov;
141	paddr_t v;
142	psize_t o;
143	extern kmutex_t memlock;
144	extern char *memhook;
145
146	sc = device_lookup_private(&ofrom_cd, minor(dev));
147	if (!sc || !sc->enabled)
148		return (ENXIO);			/* XXX PANIC */
149
150	mutex_enter(&memlock);
151	while (uio->uio_resid > 0 && error == 0) {
152		iov = uio->uio_iov;
153		if (iov->iov_len == 0) {
154			uio->uio_iov++;
155			uio->uio_iovcnt--;
156			if (uio->uio_iovcnt < 0)
157				panic("ofromrw");
158			continue;
159		}
160
161		/*
162		 * Since everything is page aligned and no more
163		 * than the rest of a page is done at once, we
164		 * can just check that the offset isn't too big.
165		 */
166		if (uio->uio_offset >= sc->size)
167			break;
168
169		/* XXX: Use unamanged mapping. */
170		v = sc->base + uio->uio_offset;
171		pmap_kenter_pa((vaddr_t)memhook, trunc_page(v),
172		    uio->uio_rw == UIO_READ ?  VM_PROT_READ : VM_PROT_WRITE,
173		    0);
174		pmap_update(kpm);
175		o = uio->uio_offset & PGOFSET;
176		c = min(uio->uio_resid, (int)(PAGE_SIZE - o));
177		error = uiomove((char *)memhook + o, c, uio);
178		pmap_kremove((vaddr_t)memhook, (vaddr_t)memhook + PAGE_SIZE);
179		pmap_update(kpm);
180	}
181	mutex_exit(&memlock);
182
183	return (error);
184}
185
186paddr_t
187ofrommmap(dev_t dev, off_t off, int prot)
188{
189	struct ofrom_softc *sc;
190
191	sc = device_lookup_private(&ofrom_cd, minor(dev));
192	if (!sc || !sc->enabled)
193		return (-1);			/* XXX PANIC */
194
195	if ((u_int)off >= sc->size)
196		return (-1);
197
198	return arm_btop(sc->base + off);
199}
200