fdtbus.c revision 1.45 1 1.45 thorpej /* $NetBSD: fdtbus.c,v 1.45 2022/01/22 11:49:17 thorpej 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.45 thorpej __KERNEL_RCSID(0, "$NetBSD: fdtbus.c,v 1.45 2022/01/22 11:49:17 thorpej 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.31 martin #include <sys/cpu.h>
37 1.1 jmcneill
38 1.1 jmcneill #include <sys/bus.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/ofw/openfirm.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/fdt/fdtvar.h>
43 1.1 jmcneill
44 1.14 jmcneill #include <libfdt.h>
45 1.14 jmcneill
46 1.9 jmcneill #include "locators.h"
47 1.9 jmcneill
48 1.4 jmcneill #define FDT_MAX_PATH 256
49 1.4 jmcneill
50 1.7 jmcneill struct fdt_node {
51 1.8 jmcneill device_t n_bus;
52 1.7 jmcneill device_t n_dev;
53 1.7 jmcneill int n_phandle;
54 1.21 jmcneill const char *n_name;
55 1.21 jmcneill struct fdt_attach_args n_faa;
56 1.35 jmcneill
57 1.35 jmcneill int n_cfpass;
58 1.28 jmcneill cfdata_t n_cf;
59 1.7 jmcneill
60 1.8 jmcneill u_int n_order;
61 1.8 jmcneill
62 1.30 jmcneill bool n_pinctrl_init;
63 1.30 jmcneill
64 1.7 jmcneill TAILQ_ENTRY(fdt_node) n_nodes;
65 1.7 jmcneill };
66 1.7 jmcneill
67 1.8 jmcneill static TAILQ_HEAD(, fdt_node) fdt_nodes =
68 1.8 jmcneill TAILQ_HEAD_INITIALIZER(fdt_nodes);
69 1.21 jmcneill static bool fdt_need_rescan = false;
70 1.8 jmcneill
71 1.7 jmcneill struct fdt_softc {
72 1.7 jmcneill device_t sc_dev;
73 1.7 jmcneill int sc_phandle;
74 1.7 jmcneill struct fdt_attach_args sc_faa;
75 1.7 jmcneill };
76 1.7 jmcneill
77 1.1 jmcneill static int fdt_match(device_t, cfdata_t, void *);
78 1.1 jmcneill static void fdt_attach(device_t, device_t, void *);
79 1.24 jmcneill static int fdt_rescan(device_t, const char *, const int *);
80 1.24 jmcneill static void fdt_childdet(device_t, device_t);
81 1.24 jmcneill
82 1.9 jmcneill static int fdt_scan_submatch(device_t, cfdata_t, const int *, void *);
83 1.35 jmcneill static void fdt_scan_best(struct fdt_softc *, struct fdt_node *);
84 1.9 jmcneill static void fdt_scan(struct fdt_softc *, int);
85 1.8 jmcneill static void fdt_add_node(struct fdt_node *);
86 1.8 jmcneill static u_int fdt_get_order(int);
87 1.30 jmcneill static void fdt_pre_attach(struct fdt_node *);
88 1.30 jmcneill static void fdt_post_attach(struct fdt_node *);
89 1.8 jmcneill
90 1.36 thorpej static const struct device_compatible_entry compat_data[] = {
91 1.36 thorpej { .compat = "simple-bus" },
92 1.44 jmcneill { .compat = "simple-pm-bus" },
93 1.37 thorpej DEVICE_COMPAT_EOL
94 1.36 thorpej };
95 1.1 jmcneill
96 1.24 jmcneill CFATTACH_DECL2_NEW(simplebus, sizeof(struct fdt_softc),
97 1.24 jmcneill fdt_match, fdt_attach, NULL, NULL, fdt_rescan, fdt_childdet);
98 1.1 jmcneill
99 1.1 jmcneill static int
100 1.1 jmcneill fdt_match(device_t parent, cfdata_t cf, void *aux)
101 1.1 jmcneill {
102 1.1 jmcneill const struct fdt_attach_args *faa = aux;
103 1.14 jmcneill const int phandle = faa->faa_phandle;
104 1.6 jmcneill int match;
105 1.1 jmcneill
106 1.14 jmcneill /* Check compatible string */
107 1.38 thorpej match = of_compatible_match(phandle, compat_data);
108 1.6 jmcneill if (match)
109 1.6 jmcneill return match;
110 1.6 jmcneill
111 1.15 jmcneill /* Some nodes have no compatible string */
112 1.15 jmcneill if (!of_hasprop(phandle, "compatible")) {
113 1.15 jmcneill if (OF_finddevice("/clocks") == phandle)
114 1.15 jmcneill return 1;
115 1.15 jmcneill if (OF_finddevice("/chosen") == phandle)
116 1.15 jmcneill return 1;
117 1.15 jmcneill }
118 1.14 jmcneill
119 1.14 jmcneill /* Always match the root node */
120 1.14 jmcneill return OF_finddevice("/") == phandle;
121 1.1 jmcneill }
122 1.1 jmcneill
123 1.1 jmcneill static void
124 1.1 jmcneill fdt_attach(device_t parent, device_t self, void *aux)
125 1.1 jmcneill {
126 1.7 jmcneill struct fdt_softc *sc = device_private(self);
127 1.1 jmcneill const struct fdt_attach_args *faa = aux;
128 1.1 jmcneill const int phandle = faa->faa_phandle;
129 1.31 martin const char *descr, *model;
130 1.7 jmcneill
131 1.7 jmcneill sc->sc_dev = self;
132 1.8 jmcneill sc->sc_phandle = phandle;
133 1.7 jmcneill sc->sc_faa = *faa;
134 1.1 jmcneill
135 1.1 jmcneill aprint_naive("\n");
136 1.22 jmcneill
137 1.22 jmcneill descr = fdtbus_get_string(phandle, "model");
138 1.22 jmcneill if (descr)
139 1.22 jmcneill aprint_normal(": %s\n", descr);
140 1.21 jmcneill else
141 1.1 jmcneill aprint_normal("\n");
142 1.1 jmcneill
143 1.21 jmcneill /* Find all child nodes */
144 1.21 jmcneill fdt_add_bus(self, phandle, &sc->sc_faa);
145 1.7 jmcneill
146 1.7 jmcneill /* Only the root bus should scan for devices */
147 1.7 jmcneill if (OF_finddevice("/") != faa->faa_phandle)
148 1.7 jmcneill return;
149 1.7 jmcneill
150 1.31 martin /* Set hw.model if available */
151 1.31 martin model = fdtbus_get_string(phandle, "compatible");
152 1.31 martin if (model)
153 1.32 martin cpu_setmodel("%s", model);
154 1.31 martin else if (descr)
155 1.32 martin cpu_setmodel("%s", descr);
156 1.31 martin
157 1.8 jmcneill /* Scan devices */
158 1.24 jmcneill fdt_rescan(self, NULL, NULL);
159 1.24 jmcneill }
160 1.24 jmcneill
161 1.24 jmcneill static int
162 1.24 jmcneill fdt_rescan(device_t self, const char *ifattr, const int *locs)
163 1.24 jmcneill {
164 1.24 jmcneill struct fdt_softc *sc = device_private(self);
165 1.28 jmcneill struct fdt_node *node;
166 1.24 jmcneill int pass;
167 1.24 jmcneill
168 1.28 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
169 1.35 jmcneill fdt_scan_best(sc, node);
170 1.28 jmcneill
171 1.21 jmcneill pass = 0;
172 1.21 jmcneill fdt_need_rescan = false;
173 1.21 jmcneill do {
174 1.9 jmcneill fdt_scan(sc, pass);
175 1.21 jmcneill if (fdt_need_rescan == true) {
176 1.21 jmcneill pass = 0;
177 1.28 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
178 1.35 jmcneill if (node->n_cfpass == -1)
179 1.35 jmcneill fdt_scan_best(sc, node);
180 1.28 jmcneill }
181 1.21 jmcneill fdt_need_rescan = false;
182 1.21 jmcneill } else {
183 1.21 jmcneill pass++;
184 1.21 jmcneill }
185 1.21 jmcneill } while (pass <= FDTCF_PASS_DEFAULT);
186 1.24 jmcneill
187 1.24 jmcneill return 0;
188 1.24 jmcneill }
189 1.24 jmcneill
190 1.24 jmcneill static void
191 1.24 jmcneill fdt_childdet(device_t parent, device_t child)
192 1.24 jmcneill {
193 1.24 jmcneill struct fdt_node *node;
194 1.24 jmcneill
195 1.24 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
196 1.24 jmcneill if (node->n_dev == child) {
197 1.24 jmcneill node->n_dev = NULL;
198 1.24 jmcneill break;
199 1.24 jmcneill }
200 1.8 jmcneill }
201 1.7 jmcneill
202 1.8 jmcneill static void
203 1.21 jmcneill fdt_init_attach_args(const struct fdt_attach_args *faa_tmpl, struct fdt_node *node,
204 1.9 jmcneill bool quiet, struct fdt_attach_args *faa)
205 1.8 jmcneill {
206 1.21 jmcneill *faa = *faa_tmpl;
207 1.8 jmcneill faa->faa_phandle = node->n_phandle;
208 1.8 jmcneill faa->faa_name = node->n_name;
209 1.9 jmcneill faa->faa_quiet = quiet;
210 1.43 jmcneill faa->faa_bst = node->n_faa.faa_bst;
211 1.43 jmcneill faa->faa_dmat = fdtbus_iommu_map(node->n_phandle, 0,
212 1.43 jmcneill node->n_faa.faa_dmat);
213 1.1 jmcneill }
214 1.1 jmcneill
215 1.23 jmcneill static bool
216 1.23 jmcneill fdt_add_bus_stdmatch(void *arg, int child)
217 1.23 jmcneill {
218 1.23 jmcneill return fdtbus_status_okay(child);
219 1.23 jmcneill }
220 1.23 jmcneill
221 1.21 jmcneill void
222 1.21 jmcneill fdt_add_bus(device_t bus, const int phandle, struct fdt_attach_args *faa)
223 1.21 jmcneill {
224 1.23 jmcneill fdt_add_bus_match(bus, phandle, faa, fdt_add_bus_stdmatch, NULL);
225 1.23 jmcneill }
226 1.23 jmcneill
227 1.23 jmcneill void
228 1.23 jmcneill fdt_add_bus_match(device_t bus, const int phandle, struct fdt_attach_args *faa,
229 1.23 jmcneill bool (*fn)(void *, int), void *fnarg)
230 1.23 jmcneill {
231 1.21 jmcneill int child;
232 1.21 jmcneill
233 1.21 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
234 1.23 jmcneill if (fn && !fn(fnarg, child))
235 1.21 jmcneill continue;
236 1.21 jmcneill
237 1.25 jmcneill fdt_add_child(bus, child, faa, fdt_get_order(child));
238 1.25 jmcneill }
239 1.25 jmcneill }
240 1.21 jmcneill
241 1.33 jmcneill static int
242 1.33 jmcneill fdt_dma_translate(int phandle, struct fdt_dma_range **ranges, u_int *nranges)
243 1.33 jmcneill {
244 1.33 jmcneill const uint8_t *data;
245 1.33 jmcneill int len, n;
246 1.33 jmcneill
247 1.33 jmcneill const int parent = OF_parent(phandle);
248 1.33 jmcneill if (parent == -1)
249 1.33 jmcneill return 1; /* done searching */
250 1.33 jmcneill
251 1.33 jmcneill data = fdtbus_get_prop(phandle, "dma-ranges", &len);
252 1.33 jmcneill if (data == NULL)
253 1.33 jmcneill return 1; /* no dma-ranges property, stop searching */
254 1.33 jmcneill
255 1.33 jmcneill if (len == 0)
256 1.33 jmcneill return 0; /* dma-ranges property is empty, keep going */
257 1.33 jmcneill
258 1.33 jmcneill const int addr_cells = fdtbus_get_addr_cells(phandle);
259 1.33 jmcneill const int size_cells = fdtbus_get_size_cells(phandle);
260 1.33 jmcneill const int paddr_cells = fdtbus_get_addr_cells(parent);
261 1.33 jmcneill if (addr_cells == -1 || size_cells == -1 || paddr_cells == -1)
262 1.33 jmcneill return 1;
263 1.33 jmcneill
264 1.33 jmcneill const int entry_size = (addr_cells + paddr_cells + size_cells) * 4;
265 1.33 jmcneill
266 1.33 jmcneill *nranges = len / entry_size;
267 1.33 jmcneill *ranges = kmem_alloc(sizeof(struct fdt_dma_range) * *nranges, KM_SLEEP);
268 1.33 jmcneill for (n = 0; len >= entry_size; n++, len -= entry_size) {
269 1.33 jmcneill const uint64_t cba = fdtbus_get_cells(data, addr_cells);
270 1.33 jmcneill data += addr_cells * 4;
271 1.33 jmcneill const uint64_t pba = fdtbus_get_cells(data, paddr_cells);
272 1.33 jmcneill data += paddr_cells * 4;
273 1.33 jmcneill const uint64_t cl = fdtbus_get_cells(data, size_cells);
274 1.33 jmcneill data += size_cells * 4;
275 1.33 jmcneill
276 1.33 jmcneill (*ranges)[n].dr_sysbase = pba;
277 1.33 jmcneill (*ranges)[n].dr_busbase = cba;
278 1.33 jmcneill (*ranges)[n].dr_len = cl;
279 1.33 jmcneill }
280 1.33 jmcneill
281 1.33 jmcneill return 1;
282 1.33 jmcneill }
283 1.33 jmcneill
284 1.33 jmcneill static bus_dma_tag_t
285 1.33 jmcneill fdt_get_dma_tag(struct fdt_node *node)
286 1.33 jmcneill {
287 1.33 jmcneill struct fdt_dma_range *ranges = NULL;
288 1.33 jmcneill u_int nranges = 0;
289 1.33 jmcneill int parent;
290 1.33 jmcneill
291 1.33 jmcneill parent = OF_parent(node->n_phandle);
292 1.33 jmcneill while (parent != -1) {
293 1.33 jmcneill if (fdt_dma_translate(parent, &ranges, &nranges) != 0)
294 1.33 jmcneill break;
295 1.33 jmcneill parent = OF_parent(parent);
296 1.33 jmcneill }
297 1.33 jmcneill
298 1.33 jmcneill return fdtbus_dma_tag_create(node->n_phandle, ranges, nranges);
299 1.33 jmcneill }
300 1.33 jmcneill
301 1.43 jmcneill static uint32_t
302 1.43 jmcneill fdt_bus_flags(int phandle, uint32_t *flags)
303 1.43 jmcneill {
304 1.43 jmcneill if (of_hasprop(phandle, "nonposted-mmio")) {
305 1.43 jmcneill *flags |= FDT_BUS_SPACE_FLAG_NONPOSTED_MMIO;
306 1.43 jmcneill return 1;
307 1.43 jmcneill }
308 1.43 jmcneill
309 1.43 jmcneill return 0;
310 1.43 jmcneill }
311 1.43 jmcneill
312 1.43 jmcneill static bus_space_tag_t
313 1.43 jmcneill fdt_get_bus_tag(struct fdt_node *node)
314 1.43 jmcneill {
315 1.43 jmcneill uint32_t flags = 0;
316 1.43 jmcneill int parent;
317 1.43 jmcneill
318 1.43 jmcneill parent = OF_parent(node->n_phandle);
319 1.43 jmcneill while (parent != -1) {
320 1.43 jmcneill if (fdt_bus_flags(parent, &flags) != 0) {
321 1.43 jmcneill break;
322 1.43 jmcneill }
323 1.43 jmcneill parent = OF_parent(parent);
324 1.43 jmcneill }
325 1.43 jmcneill
326 1.43 jmcneill return fdtbus_bus_tag_create(node->n_phandle, flags);
327 1.43 jmcneill }
328 1.43 jmcneill
329 1.25 jmcneill void
330 1.25 jmcneill fdt_add_child(device_t bus, const int child, struct fdt_attach_args *faa,
331 1.25 jmcneill u_int order)
332 1.25 jmcneill {
333 1.25 jmcneill struct fdt_node *node;
334 1.25 jmcneill
335 1.25 jmcneill /* Add the node to our device list */
336 1.35 jmcneill node = kmem_zalloc(sizeof(*node), KM_SLEEP);
337 1.25 jmcneill node->n_bus = bus;
338 1.25 jmcneill node->n_dev = NULL;
339 1.25 jmcneill node->n_phandle = child;
340 1.25 jmcneill node->n_name = fdtbus_get_string(child, "name");
341 1.35 jmcneill node->n_cfpass = -1;
342 1.35 jmcneill node->n_cf = NULL;
343 1.25 jmcneill node->n_order = order;
344 1.25 jmcneill node->n_faa = *faa;
345 1.25 jmcneill node->n_faa.faa_phandle = child;
346 1.25 jmcneill node->n_faa.faa_name = node->n_name;
347 1.43 jmcneill node->n_faa.faa_bst = fdt_get_bus_tag(node);
348 1.33 jmcneill node->n_faa.faa_dmat = fdt_get_dma_tag(node);
349 1.25 jmcneill
350 1.25 jmcneill fdt_add_node(node);
351 1.25 jmcneill fdt_need_rescan = true;
352 1.21 jmcneill }
353 1.21 jmcneill
354 1.9 jmcneill static int
355 1.9 jmcneill fdt_scan_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
356 1.9 jmcneill {
357 1.9 jmcneill if (locs[FDTCF_PASS] != FDTCF_PASS_DEFAULT &&
358 1.9 jmcneill locs[FDTCF_PASS] != cf->cf_loc[FDTCF_PASS])
359 1.9 jmcneill return 0;
360 1.9 jmcneill
361 1.9 jmcneill return config_stdsubmatch(parent, cf, locs, aux);
362 1.9 jmcneill }
363 1.9 jmcneill
364 1.35 jmcneill static void
365 1.20 jmcneill fdt_scan_best(struct fdt_softc *sc, struct fdt_node *node)
366 1.20 jmcneill {
367 1.20 jmcneill struct fdt_attach_args faa;
368 1.20 jmcneill cfdata_t cf, best_cf;
369 1.35 jmcneill int match, best_match, best_pass;
370 1.20 jmcneill
371 1.20 jmcneill best_cf = NULL;
372 1.20 jmcneill best_match = 0;
373 1.35 jmcneill best_pass = FDTCF_PASS_DEFAULT;
374 1.20 jmcneill
375 1.20 jmcneill for (int pass = 0; pass <= FDTCF_PASS_DEFAULT; pass++) {
376 1.20 jmcneill const int locs[FDTCF_NLOCS] = {
377 1.20 jmcneill [FDTCF_PASS] = pass
378 1.20 jmcneill };
379 1.21 jmcneill fdt_init_attach_args(&sc->sc_faa, node, true, &faa);
380 1.41 thorpej cf = config_search(node->n_bus, &faa,
381 1.42 thorpej CFARGS(.submatch = fdt_scan_submatch,
382 1.42 thorpej .iattr = "fdt",
383 1.42 thorpej .locators = locs));
384 1.20 jmcneill if (cf == NULL)
385 1.20 jmcneill continue;
386 1.20 jmcneill match = config_match(node->n_bus, cf, &faa);
387 1.20 jmcneill if (match > best_match) {
388 1.20 jmcneill best_match = match;
389 1.20 jmcneill best_cf = cf;
390 1.35 jmcneill best_pass = pass;
391 1.20 jmcneill }
392 1.20 jmcneill }
393 1.20 jmcneill
394 1.35 jmcneill node->n_cf = best_cf;
395 1.35 jmcneill node->n_cfpass = best_pass;
396 1.20 jmcneill }
397 1.20 jmcneill
398 1.8 jmcneill static void
399 1.9 jmcneill fdt_scan(struct fdt_softc *sc, int pass)
400 1.8 jmcneill {
401 1.8 jmcneill struct fdt_node *node;
402 1.8 jmcneill struct fdt_attach_args faa;
403 1.9 jmcneill const int locs[FDTCF_NLOCS] = {
404 1.9 jmcneill [FDTCF_PASS] = pass
405 1.9 jmcneill };
406 1.9 jmcneill bool quiet = pass != FDTCF_PASS_DEFAULT;
407 1.8 jmcneill
408 1.8 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
409 1.35 jmcneill if (node->n_cfpass != pass || node->n_dev != NULL)
410 1.8 jmcneill continue;
411 1.8 jmcneill
412 1.21 jmcneill fdt_init_attach_args(&sc->sc_faa, node, quiet, &faa);
413 1.1 jmcneill
414 1.35 jmcneill if (quiet && node->n_cf == NULL) {
415 1.29 jmcneill /*
416 1.29 jmcneill * No match for this device, skip it.
417 1.29 jmcneill */
418 1.35 jmcneill continue;
419 1.35 jmcneill }
420 1.29 jmcneill
421 1.35 jmcneill /*
422 1.35 jmcneill * Attach the device.
423 1.35 jmcneill */
424 1.35 jmcneill fdt_pre_attach(node);
425 1.29 jmcneill
426 1.45 thorpej devhandle_t nodeh = device_handle(node->n_bus);
427 1.45 thorpej
428 1.35 jmcneill if (quiet) {
429 1.41 thorpej node->n_dev = config_attach(node->n_bus, node->n_cf,
430 1.41 thorpej &faa, fdtbus_print,
431 1.42 thorpej CFARGS(.locators = locs,
432 1.42 thorpej .devhandle =
433 1.45 thorpej devhandle_from_of(nodeh,
434 1.45 thorpej node->n_phandle)));
435 1.29 jmcneill } else {
436 1.29 jmcneill /*
437 1.29 jmcneill * Default pass.
438 1.29 jmcneill */
439 1.41 thorpej node->n_dev = config_found(node->n_bus, &faa,
440 1.41 thorpej fdtbus_print,
441 1.42 thorpej CFARGS(.submatch = fdt_scan_submatch,
442 1.42 thorpej .iattr = "fdt",
443 1.42 thorpej .locators = locs,
444 1.42 thorpej .devhandle =
445 1.45 thorpej devhandle_from_of(nodeh,
446 1.45 thorpej node->n_phandle)));
447 1.29 jmcneill }
448 1.20 jmcneill
449 1.35 jmcneill if (node->n_dev != NULL)
450 1.35 jmcneill fdt_post_attach(node);
451 1.8 jmcneill }
452 1.8 jmcneill }
453 1.8 jmcneill
454 1.8 jmcneill static void
455 1.30 jmcneill fdt_pre_attach(struct fdt_node *node)
456 1.30 jmcneill {
457 1.30 jmcneill const char *cfgname;
458 1.30 jmcneill int error;
459 1.30 jmcneill
460 1.30 jmcneill node->n_pinctrl_init = fdtbus_pinctrl_has_config(node->n_phandle, "init");
461 1.30 jmcneill
462 1.30 jmcneill cfgname = node->n_pinctrl_init ? "init" : "default";
463 1.30 jmcneill
464 1.30 jmcneill aprint_debug_dev(node->n_bus, "set %s config for %s\n", cfgname, node->n_name);
465 1.30 jmcneill
466 1.30 jmcneill error = fdtbus_pinctrl_set_config(node->n_phandle, cfgname);
467 1.30 jmcneill if (error != 0 && error != ENOENT)
468 1.30 jmcneill aprint_debug_dev(node->n_bus,
469 1.30 jmcneill "failed to set %s config on %s: %d\n",
470 1.30 jmcneill cfgname, node->n_name, error);
471 1.30 jmcneill }
472 1.30 jmcneill
473 1.30 jmcneill static void
474 1.30 jmcneill fdt_post_attach(struct fdt_node *node)
475 1.30 jmcneill {
476 1.35 jmcneill char buf[FDT_MAX_PATH];
477 1.35 jmcneill prop_dictionary_t dict;
478 1.30 jmcneill int error;
479 1.30 jmcneill
480 1.35 jmcneill dict = device_properties(node->n_dev);
481 1.35 jmcneill if (fdtbus_get_path(node->n_phandle, buf, sizeof(buf)))
482 1.35 jmcneill prop_dictionary_set_string(dict, "fdt-path", buf);
483 1.35 jmcneill
484 1.30 jmcneill if (node->n_pinctrl_init) {
485 1.30 jmcneill aprint_debug_dev(node->n_bus, "set default config for %s\n", node->n_name);
486 1.30 jmcneill error = fdtbus_pinctrl_set_config(node->n_phandle, "default");
487 1.30 jmcneill if (error != 0 && error != ENOENT)
488 1.30 jmcneill aprint_debug_dev(node->n_bus,
489 1.30 jmcneill "failed to set default config on %s: %d\n",
490 1.30 jmcneill node->n_name, error);
491 1.30 jmcneill }
492 1.30 jmcneill }
493 1.30 jmcneill
494 1.30 jmcneill static void
495 1.8 jmcneill fdt_add_node(struct fdt_node *new_node)
496 1.8 jmcneill {
497 1.8 jmcneill struct fdt_node *node;
498 1.8 jmcneill
499 1.8 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
500 1.8 jmcneill if (node->n_order > new_node->n_order) {
501 1.8 jmcneill TAILQ_INSERT_BEFORE(node, new_node, n_nodes);
502 1.8 jmcneill return;
503 1.7 jmcneill }
504 1.8 jmcneill TAILQ_INSERT_TAIL(&fdt_nodes, new_node, n_nodes);
505 1.8 jmcneill }
506 1.8 jmcneill
507 1.16 bouyer void
508 1.16 bouyer fdt_remove_byhandle(int phandle)
509 1.16 bouyer {
510 1.16 bouyer struct fdt_node *node;
511 1.16 bouyer
512 1.16 bouyer TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
513 1.16 bouyer if (node->n_phandle == phandle) {
514 1.16 bouyer TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
515 1.16 bouyer return;
516 1.16 bouyer }
517 1.16 bouyer }
518 1.16 bouyer }
519 1.16 bouyer
520 1.16 bouyer void
521 1.16 bouyer fdt_remove_bycompat(const char *compatible[])
522 1.16 bouyer {
523 1.16 bouyer struct fdt_node *node, *next;
524 1.16 bouyer
525 1.16 bouyer TAILQ_FOREACH_SAFE(node, &fdt_nodes, n_nodes, next) {
526 1.36 thorpej if (of_compatible(node->n_phandle, compatible)) {
527 1.16 bouyer TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
528 1.16 bouyer }
529 1.16 bouyer }
530 1.16 bouyer }
531 1.16 bouyer
532 1.26 jmcneill int
533 1.26 jmcneill fdt_find_with_property(const char *prop, int *pindex)
534 1.26 jmcneill {
535 1.26 jmcneill struct fdt_node *node;
536 1.26 jmcneill int index = 0;
537 1.26 jmcneill
538 1.26 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
539 1.27 jmcneill if (index++ < *pindex)
540 1.26 jmcneill continue;
541 1.26 jmcneill if (of_hasprop(node->n_phandle, prop)) {
542 1.26 jmcneill *pindex = index;
543 1.26 jmcneill return node->n_phandle;
544 1.26 jmcneill }
545 1.26 jmcneill }
546 1.26 jmcneill
547 1.26 jmcneill return -1;
548 1.26 jmcneill }
549 1.26 jmcneill
550 1.8 jmcneill static u_int
551 1.8 jmcneill fdt_get_order(int phandle)
552 1.8 jmcneill {
553 1.8 jmcneill u_int val = UINT_MAX;
554 1.8 jmcneill int child;
555 1.8 jmcneill
556 1.8 jmcneill of_getprop_uint32(phandle, "phandle", &val);
557 1.8 jmcneill
558 1.8 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
559 1.8 jmcneill u_int child_val = fdt_get_order(child);
560 1.8 jmcneill if (child_val < val)
561 1.8 jmcneill val = child_val;
562 1.1 jmcneill }
563 1.8 jmcneill
564 1.8 jmcneill return val;
565 1.1 jmcneill }
566 1.1 jmcneill
567 1.12 jmcneill int
568 1.12 jmcneill fdtbus_print(void *aux, const char *pnp)
569 1.1 jmcneill {
570 1.1 jmcneill const struct fdt_attach_args * const faa = aux;
571 1.4 jmcneill char buf[FDT_MAX_PATH];
572 1.4 jmcneill const char *name = buf;
573 1.14 jmcneill int len;
574 1.1 jmcneill
575 1.10 jmcneill if (pnp && faa->faa_quiet)
576 1.9 jmcneill return QUIET;
577 1.9 jmcneill
578 1.5 jmcneill /* Skip "not configured" for nodes w/o compatible property */
579 1.10 jmcneill if (pnp && OF_getproplen(faa->faa_phandle, "compatible") <= 0)
580 1.5 jmcneill return QUIET;
581 1.5 jmcneill
582 1.10 jmcneill if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf)))
583 1.10 jmcneill name = faa->faa_name;
584 1.10 jmcneill
585 1.14 jmcneill if (pnp) {
586 1.4 jmcneill aprint_normal("%s at %s", name, pnp);
587 1.14 jmcneill const char *compat = fdt_getprop(fdtbus_get_data(),
588 1.14 jmcneill fdtbus_phandle2offset(faa->faa_phandle), "compatible",
589 1.14 jmcneill &len);
590 1.14 jmcneill while (len > 0) {
591 1.14 jmcneill aprint_debug(" <%s>", compat);
592 1.14 jmcneill len -= (strlen(compat) + 1);
593 1.14 jmcneill compat += (strlen(compat) + 1);
594 1.14 jmcneill }
595 1.14 jmcneill } else
596 1.19 thorpej aprint_debug(" (%s)", name);
597 1.1 jmcneill
598 1.1 jmcneill return UNCONF;
599 1.1 jmcneill }
600