summaryrefslogtreecommitdiffstats
path: root/sys/alpha/mcbus/mcbus.c
blob: a0bf2d6fbe25bf6f5b3df319b254634c8a1156e7 (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
/* $FreeBSD$ */
/*
 * Copyright (c) 2000 by Matthew Jacob
 * 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 immediately at the beginning of the file, without modification,
 *    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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
 */

/*
 * Autoconfiguration and support routines for the main backplane bus
 * for Rawhide (Alpha 4100) systems.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/malloc.h>

#include <machine/rpb.h>
#include <machine/cpuconf.h>

#include <alpha/mcbus/mcbusreg.h>
#include <alpha/mcbus/mcbusvar.h>

struct mcbus_device *mcbus_primary_cpu = NULL;

#define KV(_addr)	((caddr_t)ALPHA_PHYS_TO_K0SEG((_addr)))
#define	NO_MCPCIA_AT(mid, gid) \
	(badaddr((void *)KV(MCPCIA_BRIDGE_ADDR(gid, mid)), sizeof (u_int32_t)))

struct mcbus_softc {
	device_t	mcbus_dev;
	driver_intr_t * sub_intr;
	u_int8_t	mcbus_types[MCBUS_MID_MAX];
};

static void mcbus_add_child(struct mcbus_softc *, struct mcbus_device *);
static void mcbus_intr(void *, u_long);

static struct mcbus_softc *	mcbus0_softc = NULL;
static devclass_t		mcbus_devclass;

/*
 * Device methods
 */
static int mcbus_probe(device_t);
static int mcbus_print_child(device_t, device_t);
static int mcbus_read_ivar(device_t, device_t, int, u_long *);
static int mcbus_setup_intr(device_t, device_t, struct resource *, int,
    driver_intr_t *, void *, void **);
static int
mcbus_teardown_intr(device_t, device_t, struct resource *, void *);

static device_method_t mcbus_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		mcbus_probe),
	DEVMETHOD(device_attach,	bus_generic_attach),
	DEVMETHOD(device_detach,	bus_generic_detach),
	DEVMETHOD(device_shutdown,	bus_generic_shutdown),

	/* Bus interface */
	DEVMETHOD(bus_print_child,	mcbus_print_child),
	DEVMETHOD(bus_read_ivar,	mcbus_read_ivar),
	DEVMETHOD(bus_write_ivar,	bus_generic_write_ivar),
	DEVMETHOD(bus_setup_intr,	mcbus_setup_intr),
	DEVMETHOD(bus_teardown_intr,	mcbus_teardown_intr),

	{ 0, 0 }
};

static driver_t mcbus_driver = {
	"mcbus", mcbus_methods, sizeof (struct mcbus_softc),
};

/*
 * Tru64 UNIX (formerly Digital UNIX (formerly DEC OSF/1)) probes for MCPCIAs
 * in the following order:
 *
 *	5, 4, 7, 6
 *
 * This is so that the built-in CD-ROM on the internal 53c810 is always
 * dka500.  We probe them in the same order, for consistency.
 */
static const int mcbus_mcpcia_probe_order[] = { 5, 4, 7, 6 };

/*
 * At 'probe' time, we add all the devices which we know about to the
 * bus.  The generic attach routine will probe and attach them if they
 * are alive.
 */
static int
mcbus_probe(device_t dev)
{
	struct mcbus_softc *sc = device_get_softc(dev);
	struct mcbus_device *mdev;
	int i, mid, gid;

	device_set_desc(dev, "MCBUS Backplane Bus");

	/*
	 * XXX A note about GIDs...
	 * XXX If we ever support more than one MCBUS, we'll
	 * XXX have to probe for them, and map them to unit
	 * XXX numbers.
	 */

	sc->mcbus_dev = dev;
	mcbus0_softc = sc;
	set_iointr(mcbus_intr);
	gid = MCBUS_GID_FROM_INSTANCE(0);

	mcbus0_softc->mcbus_types[0] = MCBUS_TYPE_RES;
	for (mid = 1; mid <= MCBUS_MID_MAX; mid++) {
		mcbus0_softc->mcbus_types[mid] = MCBUS_TYPE_UNK;
	}

	/*
	 * First, add 'memory' children to probe.
	 */
	mdev = (struct mcbus_device *)
	    malloc(sizeof (struct mcbus_device), M_DEVBUF, M_NOWAIT);
	if (!mdev) {
		printf("mcbus_probe: unable to malloc softc for memory dev\n");
		return (ENOMEM);
	}
	mdev->ma_gid = gid;
	mdev->ma_mid = 1;
	mdev->ma_order = MCPCIA_PER_MCBUS;
	mdev->ma_type = MCBUS_TYPE_MEM;
	mcbus0_softc->mcbus_types[1] = MCBUS_TYPE_MEM;
	mcbus_add_child(sc, mdev);

	/*
	 * Now add I/O (MCPCIA) modules to probe (but only if they're there).
	 */
	for (i = 0; i < MCPCIA_PER_MCBUS; ++i) {
		mid = mcbus_mcpcia_probe_order[i];

		if (NO_MCPCIA_AT(mid, gid)) {
			continue;
		}
		mdev = (struct mcbus_device *)
		    malloc(sizeof (struct mcbus_device), M_DEVBUF, M_NOWAIT);
		if (!mdev) {
			printf("mcbus_probe: unable to malloc for MCPCIA\n");
			continue;
		}
		mdev->ma_gid = gid;
		mdev->ma_mid = mid;
		mdev->ma_order = i;
		mdev->ma_type = MCBUS_TYPE_PCI;
		mcbus0_softc->mcbus_types[1] = MCBUS_TYPE_PCI;
		mcbus_add_child(sc, mdev);
	}

	/*
	 * XXX: ToDo Add CPU nodes.
	 */

	return (0);
}

static int
mcbus_print_child(device_t dev, device_t child)
{
	struct mcbus_device *mdev = DEVTOMCBUS(child);
	int retval = 0;

	retval += bus_print_child_header(dev, child);
	retval += printf(" at %s gid %d mid %d\n",
	    device_get_nameunit(dev), mdev->ma_gid, mdev->ma_mid);
	return (retval);
}

static int
mcbus_read_ivar(device_t dev, device_t child, int index, u_long *result)
{
	struct mcbus_device *mdev = DEVTOMCBUS(child);

	switch (index) {
	case MCBUS_IVAR_MID:
		*result = mdev->ma_mid;
		break;

	case MCBUS_IVAR_GID:
		*result = mdev->ma_gid;
		break;

	case MCBUS_IVAR_TYPE:
		*result = mdev->ma_type;
		break;

	}
	return (ENOENT);
}

static int
mcbus_setup_intr(device_t dev, device_t child, struct resource *r, int f,
    driver_intr_t *intr, void *a, void **ac)
{
	if (strncmp(device_get_name(child), "pcib", 6) == 0) {
		if (mcbus0_softc->sub_intr == NULL)
			mcbus0_softc->sub_intr = intr;
		return (0);
	} else {
		return (ENXIO);
	}
}

static int
mcbus_teardown_intr(device_t dev, device_t child, struct resource *i, void *c)
{
	if (strncmp(device_get_name(child), "pcib", 6) == 0) {
		mcbus0_softc->sub_intr = NULL;
		return (0);
	} else {
		return (ENXIO);
	}
}

static void
mcbus_intr(void *frame, u_long vector)
{
	if (vector && mcbus0_softc->sub_intr)
		(*mcbus0_softc->sub_intr)((void *)vector);
}

static void
mcbus_add_child(struct mcbus_softc *mcbus, struct mcbus_device *mdev)
{
	static int mcpciaproto, memproto, cpuproto;
	device_t cd;
	int un;
	char *dn, *ds;

	switch (mdev->ma_type) {
	case MCBUS_TYPE_PCI:
		dn = "pcib";
		ds = "MCPCIA PCI Bus Bridge";
		un = mcpciaproto++;
		break;
	case MCBUS_TYPE_MEM:
		dn = "mcmem";
		un = memproto++;
		ds = "MCBUS Memory Module";
		break;
	case MCBUS_TYPE_CPU:
		dn = "mccpu";
		un = cpuproto++;
		ds = "MCBUS Processor Module";
		break;
	default:
		printf("mcbus_add_child: unknown MCBUS type 0x%x\n",
		    mdev->ma_type);
		return;
	}

	cd = device_add_child_ordered(mcbus->mcbus_dev, mdev->ma_type, dn, un);
	if (cd == NULL) {
		return;
	}
	device_set_ivars(cd, mdev);
	device_set_desc(cd, ds);
}
DRIVER_MODULE(mcbus, root, mcbus_driver, mcbus_devclass, 0, 0);
OpenPOWER on IntegriCloud