fdtbus.c revision 1.16 1 1.16 bouyer /* $NetBSD: fdtbus.c,v 1.16 2018/04/07 18:05:08 bouyer Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.16 bouyer __KERNEL_RCSID(0, "$NetBSD: fdtbus.c,v 1.16 2018/04/07 18:05:08 bouyer Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/systm.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/kmem.h>
36 1.1 jmcneill
37 1.1 jmcneill #include <sys/bus.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <dev/ofw/openfirm.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/fdt/fdtvar.h>
42 1.1 jmcneill
43 1.14 jmcneill #include <libfdt.h>
44 1.14 jmcneill
45 1.9 jmcneill #include "locators.h"
46 1.9 jmcneill
47 1.4 jmcneill #define FDT_MAX_PATH 256
48 1.4 jmcneill
49 1.7 jmcneill struct fdt_node {
50 1.8 jmcneill device_t n_bus;
51 1.7 jmcneill device_t n_dev;
52 1.7 jmcneill int n_phandle;
53 1.7 jmcneill char *n_name;
54 1.7 jmcneill
55 1.8 jmcneill u_int n_order;
56 1.8 jmcneill
57 1.7 jmcneill TAILQ_ENTRY(fdt_node) n_nodes;
58 1.7 jmcneill };
59 1.7 jmcneill
60 1.8 jmcneill static TAILQ_HEAD(, fdt_node) fdt_nodes =
61 1.8 jmcneill TAILQ_HEAD_INITIALIZER(fdt_nodes);
62 1.8 jmcneill
63 1.7 jmcneill struct fdt_softc {
64 1.7 jmcneill device_t sc_dev;
65 1.7 jmcneill int sc_phandle;
66 1.7 jmcneill struct fdt_attach_args sc_faa;
67 1.7 jmcneill };
68 1.7 jmcneill
69 1.1 jmcneill static int fdt_match(device_t, cfdata_t, void *);
70 1.1 jmcneill static void fdt_attach(device_t, device_t, void *);
71 1.9 jmcneill static int fdt_scan_submatch(device_t, cfdata_t, const int *, void *);
72 1.8 jmcneill static void fdt_scan_bus(struct fdt_softc *);
73 1.9 jmcneill static void fdt_scan(struct fdt_softc *, int);
74 1.8 jmcneill static void fdt_add_node(struct fdt_node *);
75 1.8 jmcneill static u_int fdt_get_order(int);
76 1.8 jmcneill
77 1.7 jmcneill static const char * const fdtbus_compatible[] =
78 1.7 jmcneill { "simple-bus", NULL };
79 1.1 jmcneill
80 1.7 jmcneill CFATTACH_DECL_NEW(fdt, sizeof(struct fdt_softc),
81 1.1 jmcneill fdt_match, fdt_attach, NULL, NULL);
82 1.1 jmcneill
83 1.1 jmcneill static int
84 1.1 jmcneill fdt_match(device_t parent, cfdata_t cf, void *aux)
85 1.1 jmcneill {
86 1.1 jmcneill const struct fdt_attach_args *faa = aux;
87 1.14 jmcneill const int phandle = faa->faa_phandle;
88 1.6 jmcneill int match;
89 1.1 jmcneill
90 1.14 jmcneill /* Check compatible string */
91 1.14 jmcneill match = of_match_compatible(phandle, fdtbus_compatible);
92 1.6 jmcneill if (match)
93 1.6 jmcneill return match;
94 1.6 jmcneill
95 1.15 jmcneill /* Some nodes have no compatible string */
96 1.15 jmcneill if (!of_hasprop(phandle, "compatible")) {
97 1.15 jmcneill if (OF_finddevice("/clocks") == phandle)
98 1.15 jmcneill return 1;
99 1.15 jmcneill if (OF_finddevice("/chosen") == phandle)
100 1.15 jmcneill return 1;
101 1.15 jmcneill }
102 1.14 jmcneill
103 1.14 jmcneill /* Always match the root node */
104 1.14 jmcneill return OF_finddevice("/") == phandle;
105 1.1 jmcneill }
106 1.1 jmcneill
107 1.1 jmcneill static void
108 1.1 jmcneill fdt_attach(device_t parent, device_t self, void *aux)
109 1.1 jmcneill {
110 1.7 jmcneill struct fdt_softc *sc = device_private(self);
111 1.1 jmcneill const struct fdt_attach_args *faa = aux;
112 1.1 jmcneill const int phandle = faa->faa_phandle;
113 1.7 jmcneill struct fdt_node *node;
114 1.11 jmcneill char *model, *name;
115 1.8 jmcneill int len, child;
116 1.7 jmcneill
117 1.7 jmcneill sc->sc_dev = self;
118 1.8 jmcneill sc->sc_phandle = phandle;
119 1.7 jmcneill sc->sc_faa = *faa;
120 1.1 jmcneill
121 1.1 jmcneill aprint_naive("\n");
122 1.1 jmcneill len = OF_getproplen(phandle, "model");
123 1.1 jmcneill if (len > 0) {
124 1.1 jmcneill model = kmem_zalloc(len, KM_SLEEP);
125 1.1 jmcneill if (OF_getprop(phandle, "model", model, len) == len) {
126 1.1 jmcneill aprint_normal(": %s\n", model);
127 1.1 jmcneill } else {
128 1.1 jmcneill aprint_normal("\n");
129 1.1 jmcneill }
130 1.1 jmcneill kmem_free(model, len);
131 1.1 jmcneill } else {
132 1.1 jmcneill aprint_normal("\n");
133 1.1 jmcneill }
134 1.1 jmcneill
135 1.1 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
136 1.11 jmcneill if (!fdtbus_status_okay(child))
137 1.11 jmcneill continue;
138 1.1 jmcneill
139 1.1 jmcneill len = OF_getproplen(child, "name");
140 1.11 jmcneill if (len <= 0)
141 1.1 jmcneill continue;
142 1.11 jmcneill
143 1.1 jmcneill name = kmem_zalloc(len, KM_SLEEP);
144 1.11 jmcneill if (OF_getprop(child, "name", name, len) != len)
145 1.7 jmcneill continue;
146 1.7 jmcneill
147 1.7 jmcneill /* Add the node to our device list */
148 1.7 jmcneill node = kmem_alloc(sizeof(*node), KM_SLEEP);
149 1.8 jmcneill node->n_bus = self;
150 1.7 jmcneill node->n_dev = NULL;
151 1.7 jmcneill node->n_phandle = child;
152 1.7 jmcneill node->n_name = name;
153 1.8 jmcneill node->n_order = fdt_get_order(node->n_phandle);
154 1.8 jmcneill fdt_add_node(node);
155 1.1 jmcneill }
156 1.7 jmcneill
157 1.7 jmcneill /* Scan and attach all known busses in the tree. */
158 1.8 jmcneill fdt_scan_bus(sc);
159 1.7 jmcneill
160 1.7 jmcneill /* Only the root bus should scan for devices */
161 1.7 jmcneill if (OF_finddevice("/") != faa->faa_phandle)
162 1.7 jmcneill return;
163 1.7 jmcneill
164 1.9 jmcneill aprint_debug_dev(sc->sc_dev, " order phandle bus path\n");
165 1.9 jmcneill aprint_debug_dev(sc->sc_dev, " ===== ======= === ====\n");
166 1.9 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
167 1.9 jmcneill char buf[FDT_MAX_PATH];
168 1.9 jmcneill const char *path = buf;
169 1.9 jmcneill if (!fdtbus_get_path(node->n_phandle, buf, sizeof(buf)))
170 1.9 jmcneill path = node->n_name;
171 1.9 jmcneill aprint_debug_dev(sc->sc_dev, " %04x 0x%04x %s %s\n",
172 1.9 jmcneill node->n_order & 0xffff, node->n_phandle,
173 1.9 jmcneill device_xname(node->n_bus), path);
174 1.9 jmcneill }
175 1.9 jmcneill
176 1.8 jmcneill /* Scan devices */
177 1.13 jmcneill for (int pass = 0; pass <= FDTCF_PASS_DEFAULT; pass++)
178 1.9 jmcneill fdt_scan(sc, pass);
179 1.8 jmcneill }
180 1.7 jmcneill
181 1.8 jmcneill static void
182 1.8 jmcneill fdt_init_attach_args(struct fdt_softc *sc, struct fdt_node *node,
183 1.9 jmcneill bool quiet, struct fdt_attach_args *faa)
184 1.8 jmcneill {
185 1.8 jmcneill *faa = sc->sc_faa;
186 1.8 jmcneill faa->faa_phandle = node->n_phandle;
187 1.8 jmcneill faa->faa_name = node->n_name;
188 1.9 jmcneill faa->faa_quiet = quiet;
189 1.1 jmcneill }
190 1.1 jmcneill
191 1.7 jmcneill static void
192 1.8 jmcneill fdt_scan_bus(struct fdt_softc *sc)
193 1.1 jmcneill {
194 1.7 jmcneill struct fdt_node *node;
195 1.7 jmcneill struct fdt_attach_args faa;
196 1.7 jmcneill cfdata_t cf;
197 1.7 jmcneill
198 1.8 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
199 1.8 jmcneill if (node->n_bus != sc->sc_dev)
200 1.8 jmcneill continue;
201 1.8 jmcneill if (node->n_dev != NULL)
202 1.7 jmcneill continue;
203 1.7 jmcneill
204 1.9 jmcneill fdt_init_attach_args(sc, node, true, &faa);
205 1.7 jmcneill
206 1.7 jmcneill /*
207 1.7 jmcneill * Only attach busses to nodes where this driver is the best
208 1.7 jmcneill * match.
209 1.7 jmcneill */
210 1.8 jmcneill cf = config_search_loc(NULL, node->n_bus, NULL, NULL, &faa);
211 1.8 jmcneill if (cf == NULL || strcmp(cf->cf_name, "fdt") != 0)
212 1.8 jmcneill continue;
213 1.8 jmcneill
214 1.8 jmcneill /*
215 1.8 jmcneill * Attach the bus.
216 1.8 jmcneill */
217 1.12 jmcneill node->n_dev = config_found(node->n_bus, &faa, fdtbus_print);
218 1.8 jmcneill }
219 1.8 jmcneill }
220 1.8 jmcneill
221 1.9 jmcneill static int
222 1.9 jmcneill fdt_scan_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
223 1.9 jmcneill {
224 1.9 jmcneill if (locs[FDTCF_PASS] != FDTCF_PASS_DEFAULT &&
225 1.9 jmcneill locs[FDTCF_PASS] != cf->cf_loc[FDTCF_PASS])
226 1.9 jmcneill return 0;
227 1.9 jmcneill
228 1.9 jmcneill return config_stdsubmatch(parent, cf, locs, aux);
229 1.9 jmcneill }
230 1.9 jmcneill
231 1.8 jmcneill static void
232 1.9 jmcneill fdt_scan(struct fdt_softc *sc, int pass)
233 1.8 jmcneill {
234 1.8 jmcneill struct fdt_node *node;
235 1.8 jmcneill struct fdt_attach_args faa;
236 1.9 jmcneill const int locs[FDTCF_NLOCS] = {
237 1.9 jmcneill [FDTCF_PASS] = pass
238 1.9 jmcneill };
239 1.9 jmcneill bool quiet = pass != FDTCF_PASS_DEFAULT;
240 1.8 jmcneill
241 1.8 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
242 1.8 jmcneill if (node->n_dev != NULL)
243 1.8 jmcneill continue;
244 1.8 jmcneill
245 1.9 jmcneill fdt_init_attach_args(sc, node, quiet, &faa);
246 1.1 jmcneill
247 1.7 jmcneill /*
248 1.8 jmcneill * Attach the device.
249 1.7 jmcneill */
250 1.9 jmcneill node->n_dev = config_found_sm_loc(node->n_bus, "fdt", locs,
251 1.12 jmcneill &faa, fdtbus_print, fdt_scan_submatch);
252 1.8 jmcneill }
253 1.8 jmcneill }
254 1.8 jmcneill
255 1.8 jmcneill static void
256 1.8 jmcneill fdt_add_node(struct fdt_node *new_node)
257 1.8 jmcneill {
258 1.8 jmcneill struct fdt_node *node;
259 1.8 jmcneill
260 1.8 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
261 1.8 jmcneill if (node->n_order > new_node->n_order) {
262 1.8 jmcneill TAILQ_INSERT_BEFORE(node, new_node, n_nodes);
263 1.8 jmcneill return;
264 1.7 jmcneill }
265 1.8 jmcneill TAILQ_INSERT_TAIL(&fdt_nodes, new_node, n_nodes);
266 1.8 jmcneill }
267 1.8 jmcneill
268 1.16 bouyer void
269 1.16 bouyer fdt_remove_byhandle(int phandle)
270 1.16 bouyer {
271 1.16 bouyer struct fdt_node *node;
272 1.16 bouyer
273 1.16 bouyer TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
274 1.16 bouyer if (node->n_phandle == phandle) {
275 1.16 bouyer TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
276 1.16 bouyer return;
277 1.16 bouyer }
278 1.16 bouyer }
279 1.16 bouyer }
280 1.16 bouyer
281 1.16 bouyer void
282 1.16 bouyer fdt_remove_bycompat(const char *compatible[])
283 1.16 bouyer {
284 1.16 bouyer struct fdt_node *node, *next;
285 1.16 bouyer
286 1.16 bouyer TAILQ_FOREACH_SAFE(node, &fdt_nodes, n_nodes, next) {
287 1.16 bouyer if (of_match_compatible(node->n_phandle, compatible)) {
288 1.16 bouyer TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
289 1.16 bouyer }
290 1.16 bouyer }
291 1.16 bouyer }
292 1.16 bouyer
293 1.8 jmcneill static u_int
294 1.8 jmcneill fdt_get_order(int phandle)
295 1.8 jmcneill {
296 1.8 jmcneill u_int val = UINT_MAX;
297 1.8 jmcneill int child;
298 1.8 jmcneill
299 1.8 jmcneill of_getprop_uint32(phandle, "phandle", &val);
300 1.8 jmcneill
301 1.8 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
302 1.8 jmcneill u_int child_val = fdt_get_order(child);
303 1.8 jmcneill if (child_val < val)
304 1.8 jmcneill val = child_val;
305 1.1 jmcneill }
306 1.8 jmcneill
307 1.8 jmcneill return val;
308 1.1 jmcneill }
309 1.1 jmcneill
310 1.12 jmcneill int
311 1.12 jmcneill fdtbus_print(void *aux, const char *pnp)
312 1.1 jmcneill {
313 1.1 jmcneill const struct fdt_attach_args * const faa = aux;
314 1.4 jmcneill char buf[FDT_MAX_PATH];
315 1.4 jmcneill const char *name = buf;
316 1.14 jmcneill int len;
317 1.1 jmcneill
318 1.10 jmcneill if (pnp && faa->faa_quiet)
319 1.9 jmcneill return QUIET;
320 1.9 jmcneill
321 1.5 jmcneill /* Skip "not configured" for nodes w/o compatible property */
322 1.10 jmcneill if (pnp && OF_getproplen(faa->faa_phandle, "compatible") <= 0)
323 1.5 jmcneill return QUIET;
324 1.5 jmcneill
325 1.10 jmcneill if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf)))
326 1.10 jmcneill name = faa->faa_name;
327 1.10 jmcneill
328 1.14 jmcneill if (pnp) {
329 1.4 jmcneill aprint_normal("%s at %s", name, pnp);
330 1.14 jmcneill const char *compat = fdt_getprop(fdtbus_get_data(),
331 1.14 jmcneill fdtbus_phandle2offset(faa->faa_phandle), "compatible",
332 1.14 jmcneill &len);
333 1.14 jmcneill while (len > 0) {
334 1.14 jmcneill aprint_debug(" <%s>", compat);
335 1.14 jmcneill len -= (strlen(compat) + 1);
336 1.14 jmcneill compat += (strlen(compat) + 1);
337 1.14 jmcneill }
338 1.14 jmcneill } else
339 1.10 jmcneill aprint_debug(" (%s)", name);
340 1.1 jmcneill
341 1.1 jmcneill return UNCONF;
342 1.1 jmcneill }
343