xref: /freebsd/sys/dev/ofw/ofwbus.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>.
4  * Copyright 2006 by Marius Strobl <marius@FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and
8  * its documentation for any purpose and without fee is hereby
9  * granted, provided that both the above copyright notice and this
10  * permission notice appear in all copies, that both the above
11  * copyright notice and this permission notice appear in all
12  * supporting documentation, and that the name of M.I.T. not be used
13  * in advertising or publicity pertaining to distribution of the
14  * software without specific, written prior permission.  M.I.T. makes
15  * no representations about the suitability of this software for any
16  * purpose.  It is provided "as is" without express or implied
17  * warranty.
18  *
19  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
20  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
23  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * 	from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09
33  */
34 
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/rman.h>
42 
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/ofw/openfirm.h>
46 #include <dev/fdt/simplebus.h>
47 
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 
51 /*
52  * The ofwbus (which is a pseudo-bus actually) iterates over the nodes that
53  * hang from the Open Firmware root node and adds them as devices to this bus
54  * (except some special nodes which are excluded) so that drivers can be
55  * attached to them. There should be only one ofwbus in the system, added
56  * directly as a child of nexus0.
57  */
58 
59 static device_probe_t ofwbus_probe;
60 static device_attach_t ofwbus_attach;
61 static bus_alloc_resource_t ofwbus_alloc_resource;
62 static bus_release_resource_t ofwbus_release_resource;
63 
64 static device_method_t ofwbus_methods[] = {
65 	/* Device interface */
66 	DEVMETHOD(device_probe,		ofwbus_probe),
67 	DEVMETHOD(device_attach,	ofwbus_attach),
68 
69 	/* Bus interface */
70 	DEVMETHOD(bus_alloc_resource,	ofwbus_alloc_resource),
71 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
72 	DEVMETHOD(bus_release_resource,	ofwbus_release_resource),
73 
74 	DEVMETHOD_END
75 };
76 
77 DEFINE_CLASS_1(ofwbus, ofwbus_driver, ofwbus_methods,
78     sizeof(struct simplebus_softc), simplebus_driver);
79 EARLY_DRIVER_MODULE(ofwbus, nexus, ofwbus_driver, 0, 0,
80     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
81 MODULE_VERSION(ofwbus, 1);
82 
83 static int
84 ofwbus_probe(device_t dev)
85 {
86 
87 	if (OF_peer(0) == 0)
88 		return (ENXIO);
89 
90 	/* Only one instance of ofwbus. */
91 	if (device_get_unit(dev) != 0)
92 		panic("ofwbus added with non-zero unit number: %d\n",
93 		    device_get_unit(dev));
94 
95 	device_set_desc(dev, "Open Firmware Device Tree");
96 	return (BUS_PROBE_NOWILDCARD);
97 }
98 
99 static int
100 ofwbus_attach(device_t dev)
101 {
102 	phandle_t node;
103 
104 	node = OF_peer(0);
105 
106 	/*
107 	 * If no Open Firmware, bail early
108 	 */
109 	if (node == -1)
110 		return (ENXIO);
111 
112 	/*
113 	 * ofwbus bus starts on unamed node in FDT, so we cannot make
114 	 * ofw_bus_devinfo from it. Pass node to simplebus_init directly.
115 	 */
116 	simplebus_init(dev, node);
117 
118 	/*
119 	 * Allow devices to identify.
120 	 */
121 	bus_generic_probe(dev);
122 
123 	/*
124 	 * Now walk the OFW tree and attach top-level devices.
125 	 */
126 	for (node = OF_child(node); node > 0; node = OF_peer(node))
127 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
128 
129 	return (bus_generic_attach(dev));
130 }
131 
132 static struct resource *
133 ofwbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
134     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
135 {
136 	struct resource *rv;
137 	struct resource_list_entry *rle;
138 	bool isdefault, passthrough;
139 
140 	isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
141 	passthrough = (device_get_parent(child) != bus);
142 	rle = NULL;
143 	if (!passthrough && isdefault) {
144 		rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child),
145 		    type, *rid);
146 		if (rle == NULL) {
147 			if (bootverbose)
148 				device_printf(bus, "no default resources for "
149 				    "rid = %d, type = %d\n", *rid, type);
150 			return (NULL);
151 		}
152 		start = rle->start;
153 		count = ummax(count, rle->count);
154 		end = ummax(rle->end, start + count - 1);
155 	}
156 
157 	/* Let nexus handle the allocation. */
158 	rv = bus_generic_alloc_resource(bus, child, type, rid, start, end,
159 	    count, flags);
160 	if (rv == NULL)
161 		return (NULL);
162 
163 	if (!passthrough && rle != NULL) {
164 		rle->res = rv;
165 		rle->start = rman_get_start(rv);
166 		rle->end = rman_get_end(rv);
167 		rle->count = rle->end - rle->start + 1;
168 	}
169 
170 	return (rv);
171 }
172 
173 static int
174 ofwbus_release_resource(device_t bus, device_t child, int type,
175     int rid, struct resource *r)
176 {
177 	struct resource_list_entry *rle;
178 	bool passthrough;
179 
180 	passthrough = (device_get_parent(child) != bus);
181 	if (!passthrough) {
182 		/* Clean resource list entry */
183 		rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child),
184 		    type, rid);
185 		if (rle != NULL)
186 			rle->res = NULL;
187 	}
188 
189 	/* Let nexus handle the release. */
190 	return (bus_generic_release_resource(bus, child, type, rid, r));
191 }
192