summaryrefslogtreecommitdiff
path: root/sys/dev/vme/vme.c
blob: 86ad111c71542ada507abf76acdbbf872c68bc58 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* $NetBSD: vme.c,v 1.29 2021/08/07 16:19:17 thorpej Exp $ */

/*
 * Copyright (c) 1999
 *	Matthias Drochner.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: vme.c,v 1.29 2021/08/07 16:19:17 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/extent.h>
#include <sys/bus.h>

#include <dev/vme/vmereg.h>
#include <dev/vme/vmevar.h>

static void vme_extractlocators(int*, struct vme_attach_args *);
static int vmeprint(struct vme_attach_args *, char *);
static int vmesubmatch1(device_t, cfdata_t, const int *, void *);
static int vmesubmatch(device_t, cfdata_t, const int *, void *);
int vmematch(device_t, cfdata_t, void *);
void vmeattach(device_t, device_t, void *);
static struct extent *vme_select_map(struct vmebus_softc*, vme_am_t);

#define VME_SLAVE_DUMMYDRV "vme_slv"

#define VME_NUMCFRANGES 3 /* cf. "files.vme" */

CFATTACH_DECL_NEW(vme, sizeof(struct vmebus_softc),
    vmematch, vmeattach, NULL, NULL);

const struct cfattach vme_slv_ca = {
	0	/* never used */
};

static void
vme_extractlocators(int *loc, struct vme_attach_args *aa)
{
	int i = 0;

	/* XXX can't use constants in locators.h this way */

	while (i < VME_NUMCFRANGES && i < VME_MAXCFRANGES &&
	       loc[i] != -1) {
		aa->r[i].offset = (vme_addr_t)loc[i];
		aa->r[i].size = (vme_size_t)loc[3 + i];
		aa->r[i].am = (vme_am_t)loc[6 + i];
		i++;
	}
	aa->numcfranges = i;
	aa->ilevel = loc[9];
	aa->ivector = loc[10];
}

static int
vmeprint(struct vme_attach_args *v, char *dummy)
{
	int i;

	for (i = 0; i < v->numcfranges; i++) {
		aprint_normal(" addr %x", v->r[i].offset);
		if (v->r[i].size != -1)
			aprint_normal("-%x", v->r[i].offset + v->r[i].size - 1);
		if (v->r[i].am != -1)
			aprint_normal(" am %02x", v->r[i].am);
	}
	if (v->ilevel != -1) {
		aprint_normal(" irq %d", v->ilevel);
		if (v->ivector != -1)
			aprint_normal(" vector %x", v->ivector);
	}
	return (UNCONF);
}

/*
 * This looks for a (dummy) vme device "VME_SLAVE_DUMMYDRV".
 * A callback provided by the bus's parent is called for every such
 * entry in the config database.
 * This is a special hack allowing to communicate the address settings
 * of the VME master's slave side to its driver via the normal
 * configuration mechanism.
 * Needed in following cases:
 *  -DMA windows are hardware settable but not readable by software
 *   (driver gets offsets for DMA address calculations this way)
 *  -DMA windows are software settable, but not persistent
 *   (hardware is set up from config file entry)
 *  -other adapter VME slave ranges which should be kept track of
 *   for address space accounting
 * In any case, the adapter driver must get the data before VME
 * devices are attached.
 */
static int
vmesubmatch1(device_t bus, cfdata_t dev, const int *ldesc, void *aux)
{
	struct vmebus_softc *sc = device_private(bus);
	struct vme_attach_args v;

	if (strcmp(dev->cf_name, VME_SLAVE_DUMMYDRV))
		return (0);

	vme_extractlocators(dev->cf_loc, &v);

	v.va_vct = sc->sc_vct; /* for space allocation */

	(*sc->slaveconfig)(device_parent(bus), &v);
	return (0);
}

static int
vmesubmatch(device_t bus, cfdata_t dev, const int *ldesc, void *aux)
{
	struct vmebus_softc *sc = device_private(bus);
	struct vme_attach_args v;

	if (!strcmp(dev->cf_name, VME_SLAVE_DUMMYDRV))
		return (0);

	vme_extractlocators(dev->cf_loc, &v);

	v.va_vct = sc->sc_vct;
	v.va_bdt = sc->sc_bdt;

	if (config_probe(bus, dev, &v)) {
		config_attach(bus, dev, &v, (cfprint_t)vmeprint, CFARGS_NONE);
		return (1);
	}
	return (0);
}

int
vmematch(device_t parent, cfdata_t match, void *aux)
{
	return (1);
}

void
vmeattach(device_t parent, device_t self, void *aux)
{
	struct vmebus_softc *sc = device_private(self);

	struct vmebus_attach_args *aa = aux;

	sc->sc_vct = aa->va_vct;
	sc->sc_bdt = aa->va_bdt;

	/* the "bus" are we ourselves */
	sc->sc_vct->bus = sc;

	sc->slaveconfig = aa->va_slaveconfig;

	printf("\n");

	/*
	 * set up address space accounting - assume incomplete decoding
	 */
	sc->vme32ext = extent_create("vme32", 0, 0xffffffff, 0, 0, 0);
	if (!sc->vme32ext) {
		printf("error creating A32 map\n");
		return;
	}

	sc->vme24ext = extent_create("vme24", 0, 0x00ffffff, 0, 0, 0);
	if (!sc->vme24ext) {
		printf("error creating A24 map\n");
		return;
	}

	sc->vme16ext = extent_create("vme16", 0, 0x0000ffff, 0, 0, 0);
	if (!sc->vme16ext) {
		printf("error creating A16 map\n");
		return;
	}

	if (sc->slaveconfig) {
		/* first get info about the bus master's slave side,
		 if present */
		config_search(self, NULL,
		    CFARGS(.search = vmesubmatch1));
	}
	config_search(self, NULL,
	    CFARGS(.search = vmesubmatch));

#ifdef VMEDEBUG
	if (sc->vme32ext)
		extent_print(sc->vme32ext);
	if (sc->vme24ext)
		extent_print(sc->vme24ext);
	if (sc->vme16ext)
		extent_print(sc->vme16ext);
#endif
}

#ifdef notyet
int
vmedetach(device_t dev)
{
	struct vmebus_softc *sc = device_private(dev);

	if (sc->slaveconfig) {
		/* allow bus master to free its bus resources */
		(*sc->slaveconfig)(device_parent(dev), 0);
	}

	/* extent maps should be empty now */

	if (sc->vme32ext) {
#ifdef VMEDEBUG
		extent_print(sc->vme32ext);
#endif
		extent_destroy(sc->vme32ext);
	}
	if (sc->vme24ext) {
#ifdef VMEDEBUG
		extent_print(sc->vme24ext);
#endif
		extent_destroy(sc->vme24ext);
	}
	if (sc->vme16ext) {
#ifdef VMEDEBUG
		extent_print(sc->vme16ext);
#endif
		extent_destroy(sc->vme16ext);
	}

	return (0);
}
#endif

static struct extent *
vme_select_map(struct vmebus_softc *sc, vme_am_t ams)
{
	if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A32)
		return (sc->vme32ext);
	else if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A24)
		return (sc->vme24ext);
	else if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A16)
		return (sc->vme16ext);
	else
		return (0);
}

int
_vme_space_alloc(struct vmebus_softc *sc, vme_addr_t addr, vme_size_t len, vme_am_t ams)
{
	struct extent *ex;

	ex = vme_select_map(sc, ams);
	if (!ex)
		return (EINVAL);

	return (extent_alloc_region(ex, addr, len, EX_NOWAIT));
}

void
_vme_space_free(struct vmebus_softc *sc, vme_addr_t addr, vme_size_t len, vme_am_t ams)
{
	struct extent *ex;

	ex = vme_select_map(sc, ams);
	if (!ex) {
		panic("vme_space_free: invalid am %x", ams);
		return;
	}

	extent_free(ex, addr, len, EX_NOWAIT);
}

int
_vme_space_get(struct vmebus_softc *sc, vme_size_t len, vme_am_t ams, u_long align, vme_addr_t *addr)
{
	struct extent *ex;
	u_long help;
	int res;

	ex = vme_select_map(sc, ams);
	if (!ex)
		return (EINVAL);

	res = extent_alloc(ex, len, align, EX_NOBOUNDARY, EX_NOWAIT, &help);
	if (!res)
		*addr = help;
	return (res);
}