fdtbus.c revision 1.42 1 /* $NetBSD: fdtbus.c,v 1.42 2021/08/07 16:19:10 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: fdtbus.c,v 1.42 2021/08/07 16:19:10 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/kmem.h>
36 #include <sys/cpu.h>
37
38 #include <sys/bus.h>
39
40 #include <dev/ofw/openfirm.h>
41
42 #include <dev/fdt/fdtvar.h>
43
44 #include <libfdt.h>
45
46 #include "locators.h"
47
48 #define FDT_MAX_PATH 256
49
50 struct fdt_node {
51 device_t n_bus;
52 device_t n_dev;
53 int n_phandle;
54 const char *n_name;
55 struct fdt_attach_args n_faa;
56
57 int n_cfpass;
58 cfdata_t n_cf;
59
60 u_int n_order;
61
62 bool n_pinctrl_init;
63
64 TAILQ_ENTRY(fdt_node) n_nodes;
65 };
66
67 static TAILQ_HEAD(, fdt_node) fdt_nodes =
68 TAILQ_HEAD_INITIALIZER(fdt_nodes);
69 static bool fdt_need_rescan = false;
70
71 struct fdt_softc {
72 device_t sc_dev;
73 int sc_phandle;
74 struct fdt_attach_args sc_faa;
75 };
76
77 static int fdt_match(device_t, cfdata_t, void *);
78 static void fdt_attach(device_t, device_t, void *);
79 static int fdt_rescan(device_t, const char *, const int *);
80 static void fdt_childdet(device_t, device_t);
81
82 static int fdt_scan_submatch(device_t, cfdata_t, const int *, void *);
83 static void fdt_scan_best(struct fdt_softc *, struct fdt_node *);
84 static void fdt_scan(struct fdt_softc *, int);
85 static void fdt_add_node(struct fdt_node *);
86 static u_int fdt_get_order(int);
87 static void fdt_pre_attach(struct fdt_node *);
88 static void fdt_post_attach(struct fdt_node *);
89
90 static const struct device_compatible_entry compat_data[] = {
91 { .compat = "simple-bus" },
92 DEVICE_COMPAT_EOL
93 };
94
95 CFATTACH_DECL2_NEW(simplebus, sizeof(struct fdt_softc),
96 fdt_match, fdt_attach, NULL, NULL, fdt_rescan, fdt_childdet);
97
98 static int
99 fdt_match(device_t parent, cfdata_t cf, void *aux)
100 {
101 const struct fdt_attach_args *faa = aux;
102 const int phandle = faa->faa_phandle;
103 int match;
104
105 /* Check compatible string */
106 match = of_compatible_match(phandle, compat_data);
107 if (match)
108 return match;
109
110 /* Some nodes have no compatible string */
111 if (!of_hasprop(phandle, "compatible")) {
112 if (OF_finddevice("/clocks") == phandle)
113 return 1;
114 if (OF_finddevice("/chosen") == phandle)
115 return 1;
116 }
117
118 /* Always match the root node */
119 return OF_finddevice("/") == phandle;
120 }
121
122 static void
123 fdt_attach(device_t parent, device_t self, void *aux)
124 {
125 struct fdt_softc *sc = device_private(self);
126 const struct fdt_attach_args *faa = aux;
127 const int phandle = faa->faa_phandle;
128 const char *descr, *model;
129
130 sc->sc_dev = self;
131 sc->sc_phandle = phandle;
132 sc->sc_faa = *faa;
133
134 aprint_naive("\n");
135
136 descr = fdtbus_get_string(phandle, "model");
137 if (descr)
138 aprint_normal(": %s\n", descr);
139 else
140 aprint_normal("\n");
141
142 /* Find all child nodes */
143 fdt_add_bus(self, phandle, &sc->sc_faa);
144
145 /* Only the root bus should scan for devices */
146 if (OF_finddevice("/") != faa->faa_phandle)
147 return;
148
149 /* Set hw.model if available */
150 model = fdtbus_get_string(phandle, "compatible");
151 if (model)
152 cpu_setmodel("%s", model);
153 else if (descr)
154 cpu_setmodel("%s", descr);
155
156 /* Scan devices */
157 fdt_rescan(self, NULL, NULL);
158 }
159
160 static int
161 fdt_rescan(device_t self, const char *ifattr, const int *locs)
162 {
163 struct fdt_softc *sc = device_private(self);
164 struct fdt_node *node;
165 int pass;
166
167 TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
168 fdt_scan_best(sc, node);
169
170 pass = 0;
171 fdt_need_rescan = false;
172 do {
173 fdt_scan(sc, pass);
174 if (fdt_need_rescan == true) {
175 pass = 0;
176 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
177 if (node->n_cfpass == -1)
178 fdt_scan_best(sc, node);
179 }
180 fdt_need_rescan = false;
181 } else {
182 pass++;
183 }
184 } while (pass <= FDTCF_PASS_DEFAULT);
185
186 return 0;
187 }
188
189 static void
190 fdt_childdet(device_t parent, device_t child)
191 {
192 struct fdt_node *node;
193
194 TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
195 if (node->n_dev == child) {
196 node->n_dev = NULL;
197 break;
198 }
199 }
200
201 static void
202 fdt_init_attach_args(const struct fdt_attach_args *faa_tmpl, struct fdt_node *node,
203 bool quiet, struct fdt_attach_args *faa)
204 {
205 *faa = *faa_tmpl;
206 faa->faa_phandle = node->n_phandle;
207 faa->faa_name = node->n_name;
208 faa->faa_quiet = quiet;
209 faa->faa_dmat = node->n_faa.faa_dmat;
210 }
211
212 static bool
213 fdt_add_bus_stdmatch(void *arg, int child)
214 {
215 return fdtbus_status_okay(child);
216 }
217
218 void
219 fdt_add_bus(device_t bus, const int phandle, struct fdt_attach_args *faa)
220 {
221 fdt_add_bus_match(bus, phandle, faa, fdt_add_bus_stdmatch, NULL);
222 }
223
224 void
225 fdt_add_bus_match(device_t bus, const int phandle, struct fdt_attach_args *faa,
226 bool (*fn)(void *, int), void *fnarg)
227 {
228 int child;
229
230 for (child = OF_child(phandle); child; child = OF_peer(child)) {
231 if (fn && !fn(fnarg, child))
232 continue;
233
234 fdt_add_child(bus, child, faa, fdt_get_order(child));
235 }
236 }
237
238 static int
239 fdt_dma_translate(int phandle, struct fdt_dma_range **ranges, u_int *nranges)
240 {
241 const uint8_t *data;
242 int len, n;
243
244 const int parent = OF_parent(phandle);
245 if (parent == -1)
246 return 1; /* done searching */
247
248 data = fdtbus_get_prop(phandle, "dma-ranges", &len);
249 if (data == NULL)
250 return 1; /* no dma-ranges property, stop searching */
251
252 if (len == 0)
253 return 0; /* dma-ranges property is empty, keep going */
254
255 const int addr_cells = fdtbus_get_addr_cells(phandle);
256 const int size_cells = fdtbus_get_size_cells(phandle);
257 const int paddr_cells = fdtbus_get_addr_cells(parent);
258 if (addr_cells == -1 || size_cells == -1 || paddr_cells == -1)
259 return 1;
260
261 const int entry_size = (addr_cells + paddr_cells + size_cells) * 4;
262
263 *nranges = len / entry_size;
264 *ranges = kmem_alloc(sizeof(struct fdt_dma_range) * *nranges, KM_SLEEP);
265 for (n = 0; len >= entry_size; n++, len -= entry_size) {
266 const uint64_t cba = fdtbus_get_cells(data, addr_cells);
267 data += addr_cells * 4;
268 const uint64_t pba = fdtbus_get_cells(data, paddr_cells);
269 data += paddr_cells * 4;
270 const uint64_t cl = fdtbus_get_cells(data, size_cells);
271 data += size_cells * 4;
272
273 (*ranges)[n].dr_sysbase = pba;
274 (*ranges)[n].dr_busbase = cba;
275 (*ranges)[n].dr_len = cl;
276 }
277
278 return 1;
279 }
280
281 static bus_dma_tag_t
282 fdt_get_dma_tag(struct fdt_node *node)
283 {
284 struct fdt_dma_range *ranges = NULL;
285 u_int nranges = 0;
286 int parent;
287
288 parent = OF_parent(node->n_phandle);
289 while (parent != -1) {
290 if (fdt_dma_translate(parent, &ranges, &nranges) != 0)
291 break;
292 parent = OF_parent(parent);
293 }
294
295 return fdtbus_dma_tag_create(node->n_phandle, ranges, nranges);
296 }
297
298 void
299 fdt_add_child(device_t bus, const int child, struct fdt_attach_args *faa,
300 u_int order)
301 {
302 struct fdt_node *node;
303
304 /* Add the node to our device list */
305 node = kmem_zalloc(sizeof(*node), KM_SLEEP);
306 node->n_bus = bus;
307 node->n_dev = NULL;
308 node->n_phandle = child;
309 node->n_name = fdtbus_get_string(child, "name");
310 node->n_cfpass = -1;
311 node->n_cf = NULL;
312 node->n_order = order;
313 node->n_faa = *faa;
314 node->n_faa.faa_phandle = child;
315 node->n_faa.faa_name = node->n_name;
316 node->n_faa.faa_dmat = fdt_get_dma_tag(node);
317
318 fdt_add_node(node);
319 fdt_need_rescan = true;
320 }
321
322 static int
323 fdt_scan_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
324 {
325 if (locs[FDTCF_PASS] != FDTCF_PASS_DEFAULT &&
326 locs[FDTCF_PASS] != cf->cf_loc[FDTCF_PASS])
327 return 0;
328
329 return config_stdsubmatch(parent, cf, locs, aux);
330 }
331
332 static void
333 fdt_scan_best(struct fdt_softc *sc, struct fdt_node *node)
334 {
335 struct fdt_attach_args faa;
336 cfdata_t cf, best_cf;
337 int match, best_match, best_pass;
338
339 best_cf = NULL;
340 best_match = 0;
341 best_pass = FDTCF_PASS_DEFAULT;
342
343 for (int pass = 0; pass <= FDTCF_PASS_DEFAULT; pass++) {
344 const int locs[FDTCF_NLOCS] = {
345 [FDTCF_PASS] = pass
346 };
347 fdt_init_attach_args(&sc->sc_faa, node, true, &faa);
348 cf = config_search(node->n_bus, &faa,
349 CFARGS(.submatch = fdt_scan_submatch,
350 .iattr = "fdt",
351 .locators = locs));
352 if (cf == NULL)
353 continue;
354 match = config_match(node->n_bus, cf, &faa);
355 if (match > best_match) {
356 best_match = match;
357 best_cf = cf;
358 best_pass = pass;
359 }
360 }
361
362 node->n_cf = best_cf;
363 node->n_cfpass = best_pass;
364 }
365
366 static void
367 fdt_scan(struct fdt_softc *sc, int pass)
368 {
369 struct fdt_node *node;
370 struct fdt_attach_args faa;
371 const int locs[FDTCF_NLOCS] = {
372 [FDTCF_PASS] = pass
373 };
374 bool quiet = pass != FDTCF_PASS_DEFAULT;
375
376 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
377 if (node->n_cfpass != pass || node->n_dev != NULL)
378 continue;
379
380 fdt_init_attach_args(&sc->sc_faa, node, quiet, &faa);
381
382 if (quiet && node->n_cf == NULL) {
383 /*
384 * No match for this device, skip it.
385 */
386 continue;
387 }
388
389 /*
390 * Attach the device.
391 */
392 fdt_pre_attach(node);
393
394 if (quiet) {
395 node->n_dev = config_attach(node->n_bus, node->n_cf,
396 &faa, fdtbus_print,
397 CFARGS(.locators = locs,
398 .devhandle =
399 devhandle_from_of(node->n_phandle)));
400 } else {
401 /*
402 * Default pass.
403 */
404 node->n_dev = config_found(node->n_bus, &faa,
405 fdtbus_print,
406 CFARGS(.submatch = fdt_scan_submatch,
407 .iattr = "fdt",
408 .locators = locs,
409 .devhandle =
410 devhandle_from_of(node->n_phandle)));
411 }
412
413 if (node->n_dev != NULL)
414 fdt_post_attach(node);
415 }
416 }
417
418 static void
419 fdt_pre_attach(struct fdt_node *node)
420 {
421 const char *cfgname;
422 int error;
423
424 node->n_pinctrl_init = fdtbus_pinctrl_has_config(node->n_phandle, "init");
425
426 cfgname = node->n_pinctrl_init ? "init" : "default";
427
428 aprint_debug_dev(node->n_bus, "set %s config for %s\n", cfgname, node->n_name);
429
430 error = fdtbus_pinctrl_set_config(node->n_phandle, cfgname);
431 if (error != 0 && error != ENOENT)
432 aprint_debug_dev(node->n_bus,
433 "failed to set %s config on %s: %d\n",
434 cfgname, node->n_name, error);
435 }
436
437 static void
438 fdt_post_attach(struct fdt_node *node)
439 {
440 char buf[FDT_MAX_PATH];
441 prop_dictionary_t dict;
442 int error;
443
444 dict = device_properties(node->n_dev);
445 if (fdtbus_get_path(node->n_phandle, buf, sizeof(buf)))
446 prop_dictionary_set_string(dict, "fdt-path", buf);
447
448 if (node->n_pinctrl_init) {
449 aprint_debug_dev(node->n_bus, "set default config for %s\n", node->n_name);
450 error = fdtbus_pinctrl_set_config(node->n_phandle, "default");
451 if (error != 0 && error != ENOENT)
452 aprint_debug_dev(node->n_bus,
453 "failed to set default config on %s: %d\n",
454 node->n_name, error);
455 }
456 }
457
458 static void
459 fdt_add_node(struct fdt_node *new_node)
460 {
461 struct fdt_node *node;
462
463 TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
464 if (node->n_order > new_node->n_order) {
465 TAILQ_INSERT_BEFORE(node, new_node, n_nodes);
466 return;
467 }
468 TAILQ_INSERT_TAIL(&fdt_nodes, new_node, n_nodes);
469 }
470
471 void
472 fdt_remove_byhandle(int phandle)
473 {
474 struct fdt_node *node;
475
476 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
477 if (node->n_phandle == phandle) {
478 TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
479 return;
480 }
481 }
482 }
483
484 void
485 fdt_remove_bycompat(const char *compatible[])
486 {
487 struct fdt_node *node, *next;
488
489 TAILQ_FOREACH_SAFE(node, &fdt_nodes, n_nodes, next) {
490 if (of_compatible(node->n_phandle, compatible)) {
491 TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
492 }
493 }
494 }
495
496 int
497 fdt_find_with_property(const char *prop, int *pindex)
498 {
499 struct fdt_node *node;
500 int index = 0;
501
502 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
503 if (index++ < *pindex)
504 continue;
505 if (of_hasprop(node->n_phandle, prop)) {
506 *pindex = index;
507 return node->n_phandle;
508 }
509 }
510
511 return -1;
512 }
513
514 static u_int
515 fdt_get_order(int phandle)
516 {
517 u_int val = UINT_MAX;
518 int child;
519
520 of_getprop_uint32(phandle, "phandle", &val);
521
522 for (child = OF_child(phandle); child; child = OF_peer(child)) {
523 u_int child_val = fdt_get_order(child);
524 if (child_val < val)
525 val = child_val;
526 }
527
528 return val;
529 }
530
531 int
532 fdtbus_print(void *aux, const char *pnp)
533 {
534 const struct fdt_attach_args * const faa = aux;
535 char buf[FDT_MAX_PATH];
536 const char *name = buf;
537 int len;
538
539 if (pnp && faa->faa_quiet)
540 return QUIET;
541
542 /* Skip "not configured" for nodes w/o compatible property */
543 if (pnp && OF_getproplen(faa->faa_phandle, "compatible") <= 0)
544 return QUIET;
545
546 if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf)))
547 name = faa->faa_name;
548
549 if (pnp) {
550 aprint_normal("%s at %s", name, pnp);
551 const char *compat = fdt_getprop(fdtbus_get_data(),
552 fdtbus_phandle2offset(faa->faa_phandle), "compatible",
553 &len);
554 while (len > 0) {
555 aprint_debug(" <%s>", compat);
556 len -= (strlen(compat) + 1);
557 compat += (strlen(compat) + 1);
558 }
559 } else
560 aprint_debug(" (%s)", name);
561
562 return UNCONF;
563 }
564