fdtbus.c revision 1.22 1 1.22 jmcneill /* $NetBSD: fdtbus.c,v 1.22 2018/06/30 17:28:09 jmcneill 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.22 jmcneill __KERNEL_RCSID(0, "$NetBSD: fdtbus.c,v 1.22 2018/06/30 17:28:09 jmcneill 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.21 jmcneill const char *n_name;
54 1.21 jmcneill struct fdt_attach_args n_faa;
55 1.7 jmcneill
56 1.8 jmcneill u_int n_order;
57 1.8 jmcneill
58 1.7 jmcneill TAILQ_ENTRY(fdt_node) n_nodes;
59 1.7 jmcneill };
60 1.7 jmcneill
61 1.8 jmcneill static TAILQ_HEAD(, fdt_node) fdt_nodes =
62 1.8 jmcneill TAILQ_HEAD_INITIALIZER(fdt_nodes);
63 1.21 jmcneill static bool fdt_need_rescan = false;
64 1.8 jmcneill
65 1.7 jmcneill struct fdt_softc {
66 1.7 jmcneill device_t sc_dev;
67 1.7 jmcneill int sc_phandle;
68 1.7 jmcneill struct fdt_attach_args sc_faa;
69 1.7 jmcneill };
70 1.7 jmcneill
71 1.1 jmcneill static int fdt_match(device_t, cfdata_t, void *);
72 1.1 jmcneill static void fdt_attach(device_t, device_t, void *);
73 1.9 jmcneill static int fdt_scan_submatch(device_t, cfdata_t, const int *, void *);
74 1.9 jmcneill static void fdt_scan(struct fdt_softc *, int);
75 1.8 jmcneill static void fdt_add_node(struct fdt_node *);
76 1.8 jmcneill static u_int fdt_get_order(int);
77 1.8 jmcneill
78 1.7 jmcneill static const char * const fdtbus_compatible[] =
79 1.17 jmcneill { "simple-bus", "simple-mfd", NULL };
80 1.1 jmcneill
81 1.21 jmcneill CFATTACH_DECL_NEW(simplebus, sizeof(struct fdt_softc),
82 1.1 jmcneill fdt_match, fdt_attach, NULL, NULL);
83 1.1 jmcneill
84 1.1 jmcneill static int
85 1.1 jmcneill fdt_match(device_t parent, cfdata_t cf, void *aux)
86 1.1 jmcneill {
87 1.1 jmcneill const struct fdt_attach_args *faa = aux;
88 1.14 jmcneill const int phandle = faa->faa_phandle;
89 1.6 jmcneill int match;
90 1.1 jmcneill
91 1.14 jmcneill /* Check compatible string */
92 1.14 jmcneill match = of_match_compatible(phandle, fdtbus_compatible);
93 1.6 jmcneill if (match)
94 1.6 jmcneill return match;
95 1.6 jmcneill
96 1.15 jmcneill /* Some nodes have no compatible string */
97 1.15 jmcneill if (!of_hasprop(phandle, "compatible")) {
98 1.15 jmcneill if (OF_finddevice("/clocks") == phandle)
99 1.15 jmcneill return 1;
100 1.15 jmcneill if (OF_finddevice("/chosen") == phandle)
101 1.15 jmcneill return 1;
102 1.15 jmcneill }
103 1.14 jmcneill
104 1.14 jmcneill /* Always match the root node */
105 1.14 jmcneill return OF_finddevice("/") == phandle;
106 1.1 jmcneill }
107 1.1 jmcneill
108 1.1 jmcneill static void
109 1.1 jmcneill fdt_attach(device_t parent, device_t self, void *aux)
110 1.1 jmcneill {
111 1.7 jmcneill struct fdt_softc *sc = device_private(self);
112 1.1 jmcneill const struct fdt_attach_args *faa = aux;
113 1.1 jmcneill const int phandle = faa->faa_phandle;
114 1.22 jmcneill const char *descr;
115 1.21 jmcneill int pass;
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.22 jmcneill
123 1.22 jmcneill descr = fdtbus_get_string(phandle, "model");
124 1.22 jmcneill if (descr)
125 1.22 jmcneill aprint_normal(": %s\n", descr);
126 1.21 jmcneill else
127 1.1 jmcneill aprint_normal("\n");
128 1.1 jmcneill
129 1.21 jmcneill /* Find all child nodes */
130 1.21 jmcneill fdt_add_bus(self, phandle, &sc->sc_faa);
131 1.7 jmcneill
132 1.7 jmcneill /* Only the root bus should scan for devices */
133 1.7 jmcneill if (OF_finddevice("/") != faa->faa_phandle)
134 1.7 jmcneill return;
135 1.7 jmcneill
136 1.8 jmcneill /* Scan devices */
137 1.21 jmcneill pass = 0;
138 1.21 jmcneill fdt_need_rescan = false;
139 1.21 jmcneill do {
140 1.9 jmcneill fdt_scan(sc, pass);
141 1.21 jmcneill if (fdt_need_rescan == true) {
142 1.21 jmcneill pass = 0;
143 1.21 jmcneill fdt_need_rescan = false;
144 1.21 jmcneill } else {
145 1.21 jmcneill pass++;
146 1.21 jmcneill }
147 1.21 jmcneill } while (pass <= FDTCF_PASS_DEFAULT);
148 1.8 jmcneill }
149 1.7 jmcneill
150 1.8 jmcneill static void
151 1.21 jmcneill fdt_init_attach_args(const struct fdt_attach_args *faa_tmpl, struct fdt_node *node,
152 1.9 jmcneill bool quiet, struct fdt_attach_args *faa)
153 1.8 jmcneill {
154 1.21 jmcneill *faa = *faa_tmpl;
155 1.8 jmcneill faa->faa_phandle = node->n_phandle;
156 1.8 jmcneill faa->faa_name = node->n_name;
157 1.9 jmcneill faa->faa_quiet = quiet;
158 1.1 jmcneill }
159 1.1 jmcneill
160 1.21 jmcneill void
161 1.21 jmcneill fdt_add_bus(device_t bus, const int phandle, struct fdt_attach_args *faa)
162 1.21 jmcneill {
163 1.21 jmcneill struct fdt_node *node;
164 1.21 jmcneill int child;
165 1.21 jmcneill
166 1.21 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
167 1.21 jmcneill if (!fdtbus_status_okay(child))
168 1.21 jmcneill continue;
169 1.21 jmcneill
170 1.21 jmcneill /* Add the node to our device list */
171 1.21 jmcneill node = kmem_alloc(sizeof(*node), KM_SLEEP);
172 1.21 jmcneill node->n_bus = bus;
173 1.21 jmcneill node->n_dev = NULL;
174 1.21 jmcneill node->n_phandle = child;
175 1.21 jmcneill node->n_name = fdtbus_get_string(child, "name");
176 1.21 jmcneill node->n_order = fdt_get_order(child);
177 1.21 jmcneill node->n_faa = *faa;
178 1.21 jmcneill node->n_faa.faa_phandle = child;
179 1.21 jmcneill node->n_faa.faa_name = node->n_name;
180 1.21 jmcneill
181 1.21 jmcneill fdt_add_node(node);
182 1.21 jmcneill fdt_need_rescan = true;
183 1.21 jmcneill }
184 1.21 jmcneill }
185 1.21 jmcneill
186 1.9 jmcneill static int
187 1.9 jmcneill fdt_scan_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
188 1.9 jmcneill {
189 1.9 jmcneill if (locs[FDTCF_PASS] != FDTCF_PASS_DEFAULT &&
190 1.9 jmcneill locs[FDTCF_PASS] != cf->cf_loc[FDTCF_PASS])
191 1.9 jmcneill return 0;
192 1.9 jmcneill
193 1.9 jmcneill return config_stdsubmatch(parent, cf, locs, aux);
194 1.9 jmcneill }
195 1.9 jmcneill
196 1.20 jmcneill static cfdata_t
197 1.20 jmcneill fdt_scan_best(struct fdt_softc *sc, struct fdt_node *node)
198 1.20 jmcneill {
199 1.20 jmcneill struct fdt_attach_args faa;
200 1.20 jmcneill cfdata_t cf, best_cf;
201 1.20 jmcneill int match, best_match;
202 1.20 jmcneill
203 1.20 jmcneill best_cf = NULL;
204 1.20 jmcneill best_match = 0;
205 1.20 jmcneill
206 1.20 jmcneill for (int pass = 0; pass <= FDTCF_PASS_DEFAULT; pass++) {
207 1.20 jmcneill const int locs[FDTCF_NLOCS] = {
208 1.20 jmcneill [FDTCF_PASS] = pass
209 1.20 jmcneill };
210 1.21 jmcneill fdt_init_attach_args(&sc->sc_faa, node, true, &faa);
211 1.20 jmcneill cf = config_search_loc(fdt_scan_submatch, node->n_bus, "fdt", locs, &faa);
212 1.20 jmcneill if (cf == NULL)
213 1.20 jmcneill continue;
214 1.20 jmcneill match = config_match(node->n_bus, cf, &faa);
215 1.20 jmcneill if (match > best_match) {
216 1.20 jmcneill best_match = match;
217 1.20 jmcneill best_cf = cf;
218 1.20 jmcneill }
219 1.20 jmcneill }
220 1.20 jmcneill
221 1.20 jmcneill return best_cf;
222 1.20 jmcneill }
223 1.20 jmcneill
224 1.8 jmcneill static void
225 1.9 jmcneill fdt_scan(struct fdt_softc *sc, int pass)
226 1.8 jmcneill {
227 1.8 jmcneill struct fdt_node *node;
228 1.8 jmcneill struct fdt_attach_args faa;
229 1.9 jmcneill const int locs[FDTCF_NLOCS] = {
230 1.9 jmcneill [FDTCF_PASS] = pass
231 1.9 jmcneill };
232 1.9 jmcneill bool quiet = pass != FDTCF_PASS_DEFAULT;
233 1.22 jmcneill prop_dictionary_t dict;
234 1.22 jmcneill char buf[FDT_MAX_PATH];
235 1.8 jmcneill
236 1.8 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
237 1.8 jmcneill if (node->n_dev != NULL)
238 1.8 jmcneill continue;
239 1.8 jmcneill
240 1.21 jmcneill fdt_init_attach_args(&sc->sc_faa, node, quiet, &faa);
241 1.1 jmcneill
242 1.7 jmcneill /*
243 1.20 jmcneill * Make sure we don't attach before a better match in a later pass.
244 1.20 jmcneill */
245 1.20 jmcneill cfdata_t cf_best = fdt_scan_best(sc, node);
246 1.20 jmcneill cfdata_t cf_pass =
247 1.20 jmcneill config_search_loc(fdt_scan_submatch, node->n_bus, "fdt", locs, &faa);
248 1.20 jmcneill if (cf_best != cf_pass)
249 1.20 jmcneill continue;
250 1.20 jmcneill
251 1.20 jmcneill /*
252 1.8 jmcneill * Attach the device.
253 1.7 jmcneill */
254 1.9 jmcneill node->n_dev = config_found_sm_loc(node->n_bus, "fdt", locs,
255 1.12 jmcneill &faa, fdtbus_print, fdt_scan_submatch);
256 1.22 jmcneill if (node->n_dev) {
257 1.22 jmcneill dict = device_properties(node->n_dev);
258 1.22 jmcneill if (fdtbus_get_path(node->n_phandle, buf, sizeof(buf)))
259 1.22 jmcneill prop_dictionary_set_cstring(dict, "fdt-path", buf);
260 1.22 jmcneill }
261 1.8 jmcneill }
262 1.8 jmcneill }
263 1.8 jmcneill
264 1.8 jmcneill static void
265 1.8 jmcneill fdt_add_node(struct fdt_node *new_node)
266 1.8 jmcneill {
267 1.8 jmcneill struct fdt_node *node;
268 1.8 jmcneill
269 1.8 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
270 1.8 jmcneill if (node->n_order > new_node->n_order) {
271 1.8 jmcneill TAILQ_INSERT_BEFORE(node, new_node, n_nodes);
272 1.8 jmcneill return;
273 1.7 jmcneill }
274 1.8 jmcneill TAILQ_INSERT_TAIL(&fdt_nodes, new_node, n_nodes);
275 1.8 jmcneill }
276 1.8 jmcneill
277 1.16 bouyer void
278 1.16 bouyer fdt_remove_byhandle(int phandle)
279 1.16 bouyer {
280 1.16 bouyer struct fdt_node *node;
281 1.16 bouyer
282 1.16 bouyer TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
283 1.16 bouyer if (node->n_phandle == phandle) {
284 1.16 bouyer TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
285 1.16 bouyer return;
286 1.16 bouyer }
287 1.16 bouyer }
288 1.16 bouyer }
289 1.16 bouyer
290 1.16 bouyer void
291 1.16 bouyer fdt_remove_bycompat(const char *compatible[])
292 1.16 bouyer {
293 1.16 bouyer struct fdt_node *node, *next;
294 1.16 bouyer
295 1.16 bouyer TAILQ_FOREACH_SAFE(node, &fdt_nodes, n_nodes, next) {
296 1.16 bouyer if (of_match_compatible(node->n_phandle, compatible)) {
297 1.16 bouyer TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
298 1.16 bouyer }
299 1.16 bouyer }
300 1.16 bouyer }
301 1.16 bouyer
302 1.8 jmcneill static u_int
303 1.8 jmcneill fdt_get_order(int phandle)
304 1.8 jmcneill {
305 1.8 jmcneill u_int val = UINT_MAX;
306 1.8 jmcneill int child;
307 1.8 jmcneill
308 1.8 jmcneill of_getprop_uint32(phandle, "phandle", &val);
309 1.8 jmcneill
310 1.8 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
311 1.8 jmcneill u_int child_val = fdt_get_order(child);
312 1.8 jmcneill if (child_val < val)
313 1.8 jmcneill val = child_val;
314 1.1 jmcneill }
315 1.8 jmcneill
316 1.8 jmcneill return val;
317 1.1 jmcneill }
318 1.1 jmcneill
319 1.12 jmcneill int
320 1.12 jmcneill fdtbus_print(void *aux, const char *pnp)
321 1.1 jmcneill {
322 1.1 jmcneill const struct fdt_attach_args * const faa = aux;
323 1.4 jmcneill char buf[FDT_MAX_PATH];
324 1.4 jmcneill const char *name = buf;
325 1.14 jmcneill int len;
326 1.1 jmcneill
327 1.10 jmcneill if (pnp && faa->faa_quiet)
328 1.9 jmcneill return QUIET;
329 1.9 jmcneill
330 1.5 jmcneill /* Skip "not configured" for nodes w/o compatible property */
331 1.10 jmcneill if (pnp && OF_getproplen(faa->faa_phandle, "compatible") <= 0)
332 1.5 jmcneill return QUIET;
333 1.5 jmcneill
334 1.10 jmcneill if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf)))
335 1.10 jmcneill name = faa->faa_name;
336 1.10 jmcneill
337 1.14 jmcneill if (pnp) {
338 1.4 jmcneill aprint_normal("%s at %s", name, pnp);
339 1.14 jmcneill const char *compat = fdt_getprop(fdtbus_get_data(),
340 1.14 jmcneill fdtbus_phandle2offset(faa->faa_phandle), "compatible",
341 1.14 jmcneill &len);
342 1.14 jmcneill while (len > 0) {
343 1.14 jmcneill aprint_debug(" <%s>", compat);
344 1.14 jmcneill len -= (strlen(compat) + 1);
345 1.14 jmcneill compat += (strlen(compat) + 1);
346 1.14 jmcneill }
347 1.14 jmcneill } else
348 1.19 thorpej aprint_debug(" (%s)", name);
349 1.1 jmcneill
350 1.1 jmcneill return UNCONF;
351 1.1 jmcneill }
352