fdtbus.c revision 1.37 1 /* $NetBSD: fdtbus.c,v 1.37 2021/01/27 02:24: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.37 2021/01/27 02:24: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_match_compat_data(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_loc(fdt_scan_submatch, node->n_bus, "fdt", locs, &faa);
349 if (cf == NULL)
350 continue;
351 match = config_match(node->n_bus, cf, &faa);
352 if (match > best_match) {
353 best_match = match;
354 best_cf = cf;
355 best_pass = pass;
356 }
357 }
358
359 node->n_cf = best_cf;
360 node->n_cfpass = best_pass;
361 }
362
363 static void
364 fdt_scan(struct fdt_softc *sc, int pass)
365 {
366 struct fdt_node *node;
367 struct fdt_attach_args faa;
368 const int locs[FDTCF_NLOCS] = {
369 [FDTCF_PASS] = pass
370 };
371 bool quiet = pass != FDTCF_PASS_DEFAULT;
372
373 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
374 if (node->n_cfpass != pass || node->n_dev != NULL)
375 continue;
376
377 fdt_init_attach_args(&sc->sc_faa, node, quiet, &faa);
378
379 if (quiet && node->n_cf == NULL) {
380 /*
381 * No match for this device, skip it.
382 */
383 continue;
384 }
385
386 /*
387 * Attach the device.
388 */
389 fdt_pre_attach(node);
390
391 if (quiet) {
392 node->n_dev = config_attach_loc(node->n_bus, node->n_cf, locs,
393 &faa, fdtbus_print);
394 } else {
395 /*
396 * Default pass.
397 */
398 node->n_dev = config_found_sm_loc(node->n_bus, "fdt", locs,
399 &faa, fdtbus_print, fdt_scan_submatch);
400 }
401
402 if (node->n_dev != NULL)
403 fdt_post_attach(node);
404 }
405 }
406
407 static void
408 fdt_pre_attach(struct fdt_node *node)
409 {
410 const char *cfgname;
411 int error;
412
413 node->n_pinctrl_init = fdtbus_pinctrl_has_config(node->n_phandle, "init");
414
415 cfgname = node->n_pinctrl_init ? "init" : "default";
416
417 aprint_debug_dev(node->n_bus, "set %s config for %s\n", cfgname, node->n_name);
418
419 error = fdtbus_pinctrl_set_config(node->n_phandle, cfgname);
420 if (error != 0 && error != ENOENT)
421 aprint_debug_dev(node->n_bus,
422 "failed to set %s config on %s: %d\n",
423 cfgname, node->n_name, error);
424 }
425
426 static void
427 fdt_post_attach(struct fdt_node *node)
428 {
429 char buf[FDT_MAX_PATH];
430 prop_dictionary_t dict;
431 int error;
432
433 dict = device_properties(node->n_dev);
434 if (fdtbus_get_path(node->n_phandle, buf, sizeof(buf)))
435 prop_dictionary_set_string(dict, "fdt-path", buf);
436
437 if (node->n_pinctrl_init) {
438 aprint_debug_dev(node->n_bus, "set default config for %s\n", node->n_name);
439 error = fdtbus_pinctrl_set_config(node->n_phandle, "default");
440 if (error != 0 && error != ENOENT)
441 aprint_debug_dev(node->n_bus,
442 "failed to set default config on %s: %d\n",
443 node->n_name, error);
444 }
445 }
446
447 static void
448 fdt_add_node(struct fdt_node *new_node)
449 {
450 struct fdt_node *node;
451
452 TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
453 if (node->n_order > new_node->n_order) {
454 TAILQ_INSERT_BEFORE(node, new_node, n_nodes);
455 return;
456 }
457 TAILQ_INSERT_TAIL(&fdt_nodes, new_node, n_nodes);
458 }
459
460 void
461 fdt_remove_byhandle(int phandle)
462 {
463 struct fdt_node *node;
464
465 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
466 if (node->n_phandle == phandle) {
467 TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
468 return;
469 }
470 }
471 }
472
473 void
474 fdt_remove_bycompat(const char *compatible[])
475 {
476 struct fdt_node *node, *next;
477
478 TAILQ_FOREACH_SAFE(node, &fdt_nodes, n_nodes, next) {
479 if (of_compatible(node->n_phandle, compatible)) {
480 TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
481 }
482 }
483 }
484
485 int
486 fdt_find_with_property(const char *prop, int *pindex)
487 {
488 struct fdt_node *node;
489 int index = 0;
490
491 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
492 if (index++ < *pindex)
493 continue;
494 if (of_hasprop(node->n_phandle, prop)) {
495 *pindex = index;
496 return node->n_phandle;
497 }
498 }
499
500 return -1;
501 }
502
503 static u_int
504 fdt_get_order(int phandle)
505 {
506 u_int val = UINT_MAX;
507 int child;
508
509 of_getprop_uint32(phandle, "phandle", &val);
510
511 for (child = OF_child(phandle); child; child = OF_peer(child)) {
512 u_int child_val = fdt_get_order(child);
513 if (child_val < val)
514 val = child_val;
515 }
516
517 return val;
518 }
519
520 int
521 fdtbus_print(void *aux, const char *pnp)
522 {
523 const struct fdt_attach_args * const faa = aux;
524 char buf[FDT_MAX_PATH];
525 const char *name = buf;
526 int len;
527
528 if (pnp && faa->faa_quiet)
529 return QUIET;
530
531 /* Skip "not configured" for nodes w/o compatible property */
532 if (pnp && OF_getproplen(faa->faa_phandle, "compatible") <= 0)
533 return QUIET;
534
535 if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf)))
536 name = faa->faa_name;
537
538 if (pnp) {
539 aprint_normal("%s at %s", name, pnp);
540 const char *compat = fdt_getprop(fdtbus_get_data(),
541 fdtbus_phandle2offset(faa->faa_phandle), "compatible",
542 &len);
543 while (len > 0) {
544 aprint_debug(" <%s>", compat);
545 len -= (strlen(compat) + 1);
546 compat += (strlen(compat) + 1);
547 }
548 } else
549 aprint_debug(" (%s)", name);
550
551 return UNCONF;
552 }
553