summaryrefslogtreecommitdiff
path: root/sys/arch/shark/ofw/ofrom.c
blob: 8f328f874e60c38b0ea65b34da7352e93a342541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*	$NetBSD: ofrom.c,v 1.29 2021/01/27 03:10:21 thorpej Exp $	*/

/*
 * Copyright 1998
 * Digital Equipment Corporation. All rights reserved.
 *
 * This software is furnished under license and may be used and
 * copied only in accordance with the following terms and conditions.
 * Subject to these conditions, you may download, copy, install,
 * use, modify and distribute this software in source and/or binary
 * form. No title or ownership is transferred hereby.
 *
 * 1) Any source code used, modified or distributed must reproduce
 *    and retain this copyright notice and list of conditions as
 *    they appear in the source file.
 *
 * 2) No right is granted to use any trade name, trademark, or logo of
 *    Digital Equipment Corporation. Neither the "Digital Equipment
 *    Corporation" name nor any trademark or logo of Digital Equipment
 *    Corporation may be used to endorse or promote products derived
 *    from this software without the prior written permission of
 *    Digital Equipment Corporation.
 *
 * 3) This software is provided "AS-IS" and any express or implied
 *    warranties, including but not limited to, any implied warranties
 *    of merchantability, fitness for a particular purpose, or
 *    non-infringement are disclaimed. In no event shall DIGITAL be
 *    liable for any damages whatsoever, and in particular, DIGITAL
 *    shall not be liable for special, indirect, consequential, or
 *    incidental damages or damages for lost profits, loss of
 *    revenue or loss of use, whether such damages arise in contract,
 *    negligence, tort, under statute, in equity, at law or otherwise,
 *    even if advised of the possibility of such damage.
 */

/*
 * XXX open for writing (for user programs to mmap, and write contents)
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ofrom.c,v 1.29 2021/01/27 03:10:21 thorpej Exp $");

#include <sys/param.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/fcntl.h>
#include <sys/bus.h>

#include <uvm/uvm_extern.h>

#include <dev/ofw/openfirm.h>

struct ofrom_softc {
	int		enabled;
	paddr_t		base;
	paddr_t		size;
};

int ofromprobe(device_t, cfdata_t, void *);
void ofromattach(device_t, device_t, void *);

CFATTACH_DECL_NEW(ofrom, sizeof(struct ofrom_softc),
    ofromprobe, ofromattach, NULL, NULL);

extern struct cfdriver ofrom_cd;

dev_type_open(ofromopen);
dev_type_read(ofromrw);
dev_type_mmap(ofrommmap);

const struct cdevsw ofrom_cdevsw = {
	.d_open = ofromopen,
	.d_close = nullclose,
	.d_read = ofromrw,
	.d_write = ofromrw,
	.d_ioctl = noioctl,
	.d_stop = nostop,
	.d_tty = notty,
	.d_poll = nopoll,
	.d_mmap = ofrommmap,
	.d_kqfilter = nokqfilter,
	.d_discard = nodiscard,
	.d_flag = 0
};

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "rom" },
	DEVICE_COMPAT_EOL
};

int
ofromprobe(device_t parent, cfdata_t cf, void *aux)
{
	struct ofbus_attach_args *oba = aux;

	return of_compatible_match(oba->oba_phandle, compat_data) * 5;
}


void
ofromattach(device_t parent, device_t self, void *aux)
{
	struct ofrom_softc *sc = device_private(self);
	struct ofbus_attach_args *oba = aux;
	char regbuf[8];

	if (OF_getproplen(oba->oba_phandle, "reg") != 8) {
		printf(": invalid reg property\n");
		return;
	}
	if (OF_getprop(oba->oba_phandle, "reg", regbuf, sizeof regbuf) != 8) {
		printf(": couldn't read reg property\n");
		return;
	}
	sc->base = of_decode_int(&regbuf[0]);
	sc->size = of_decode_int(&regbuf[4]);
	sc->enabled = 1;

	printf(": %#lx-%#lx\n", sc->base, sc->base + sc->size - 1);
}

int
ofromopen(dev_t dev, int oflags, int devtype, struct lwp *l)
{
	struct ofrom_softc *sc;

	sc = device_lookup_private(&ofrom_cd, minor(dev));
	if (!sc || !sc->enabled)
		return (ENXIO);

	if (oflags & FWRITE)
		return (EINVAL);

	return (0);
}

int
ofromrw(dev_t dev, struct uio *uio, int flags)
{
	pmap_t kpm = pmap_kernel();
	struct ofrom_softc *sc;
	int c, error = 0;
	struct iovec *iov;
	paddr_t v;
	psize_t o;
	extern kmutex_t memlock;
	extern char *memhook;

	sc = device_lookup_private(&ofrom_cd, minor(dev));
	if (!sc || !sc->enabled)
		return (ENXIO);			/* XXX PANIC */

	mutex_enter(&memlock);
	while (uio->uio_resid > 0 && error == 0) {
		iov = uio->uio_iov;
		if (iov->iov_len == 0) {
			uio->uio_iov++;
			uio->uio_iovcnt--;
			if (uio->uio_iovcnt < 0)
				panic("ofromrw");
			continue;
		}

		/*
		 * Since everything is page aligned and no more
		 * than the rest of a page is done at once, we
		 * can just check that the offset isn't too big.
		 */
		if (uio->uio_offset >= sc->size)
			break;

		v = sc->base + uio->uio_offset;
		pmap_kenter_pa((vaddr_t)memhook, trunc_page(v),
		    uio->uio_rw == UIO_READ ?  VM_PROT_READ : VM_PROT_WRITE,
		    0);
		pmap_update(kpm);
		o = uio->uio_offset & PGOFSET;
		c = uimin(uio->uio_resid, (int)(PAGE_SIZE - o));
		error = uiomove((char *)memhook + o, c, uio);
		pmap_kremove((vaddr_t)memhook, (vaddr_t)memhook + PAGE_SIZE);
		pmap_update(kpm);
	}
	mutex_exit(&memlock);

	return (error);
}

paddr_t
ofrommmap(dev_t dev, off_t off, int prot)
{
	struct ofrom_softc *sc;

	sc = device_lookup_private(&ofrom_cd, minor(dev));
	if (!sc || !sc->enabled)
		return (-1);			/* XXX PANIC */

	if ((u_int)off >= sc->size)
		return (-1);

	return arm_btop(sc->base + off);
}