ofrom.c revision 1.27
1/*	$NetBSD: ofrom.c,v 1.27 2017/09/08 08:16:09 skrll 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.27 2017/09/08 08:16:09 skrll 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_discard = nodiscard,
84	.d_flag = 0
85};
86
87int
88ofromprobe(device_t parent, cfdata_t cf, void *aux)
89{
90	struct ofbus_attach_args *oba = aux;
91	static const char *const compatible_strings[] = { "rom", NULL };
92
93	return (of_compatible(oba->oba_phandle, compatible_strings) == -1) ?
94	    0 : 5;
95}
96
97
98void
99ofromattach(device_t parent, device_t self, void *aux)
100{
101	struct ofrom_softc *sc = device_private(self);
102	struct ofbus_attach_args *oba = aux;
103	char regbuf[8];
104
105	if (OF_getproplen(oba->oba_phandle, "reg") != 8) {
106		printf(": invalid reg property\n");
107		return;
108	}
109	if (OF_getprop(oba->oba_phandle, "reg", regbuf, sizeof regbuf) != 8) {
110		printf(": couldn't read reg property\n");
111		return;
112	}
113	sc->base = of_decode_int(&regbuf[0]);
114	sc->size = of_decode_int(&regbuf[4]);
115	sc->enabled = 1;
116
117	printf(": %#lx-%#lx\n", sc->base, sc->base + sc->size - 1);
118}
119
120int
121ofromopen(dev_t dev, int oflags, int devtype, struct lwp *l)
122{
123	struct ofrom_softc *sc;
124
125	sc = device_lookup_private(&ofrom_cd, minor(dev));
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_t dev, struct uio *uio, int flags)
137{
138	pmap_t kpm = pmap_kernel();
139	struct ofrom_softc *sc;
140	int c, error = 0;
141	struct iovec *iov;
142	paddr_t v;
143	psize_t o;
144	extern kmutex_t memlock;
145	extern char *memhook;
146
147	sc = device_lookup_private(&ofrom_cd, minor(dev));
148	if (!sc || !sc->enabled)
149		return (ENXIO);			/* XXX PANIC */
150
151	mutex_enter(&memlock);
152	while (uio->uio_resid > 0 && error == 0) {
153		iov = uio->uio_iov;
154		if (iov->iov_len == 0) {
155			uio->uio_iov++;
156			uio->uio_iovcnt--;
157			if (uio->uio_iovcnt < 0)
158				panic("ofromrw");
159			continue;
160		}
161
162		/*
163		 * Since everything is page aligned and no more
164		 * than the rest of a page is done at once, we
165		 * can just check that the offset isn't too big.
166		 */
167		if (uio->uio_offset >= sc->size)
168			break;
169
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