1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
3 * Copyright 2010-2016 Freescale Semiconductor Inc.
10 #include <rte_string_fns.h>
11 #include <dpaax_logs.h>
14 static struct dt_dir root_dir;
15 static const char *base_dir;
16 static COMPAT_LIST_HEAD(linear);
19 of_open_dir(const char *relative_path, struct dirent ***d)
22 char full_path[PATH_MAX];
24 snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
25 ret = scandir(full_path, d, 0, versionsort);
27 DPAAX_LOG(ERR, "Failed to open directory %s",
33 of_close_dir(struct dirent **d, int num)
41 of_open_file(const char *relative_path)
44 char full_path[PATH_MAX];
46 snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
47 ret = open(full_path, O_RDONLY);
49 DPAAX_LOG(ERR, "Failed to open directory %s",
55 process_file(struct dirent *dent, struct dt_dir *parent)
58 struct dt_file *f = malloc(sizeof(*f));
61 DPAAX_LOG(DEBUG, "Unable to allocate memory for file node");
65 strlcpy(f->node.node.name, dent->d_name, NAME_MAX);
66 snprintf(f->node.node.full_name, PATH_MAX, "%s/%s",
67 parent->node.node.full_name, dent->d_name);
69 fd = of_open_file(f->node.node.full_name);
71 DPAAX_LOG(DEBUG, "Unable to open file node");
75 f->len = read(fd, f->buf, OF_FILE_BUF_MAX);
78 DPAAX_LOG(DEBUG, "Unable to read file node");
82 list_add_tail(&f->node.list, &parent->files);
85 static const struct dt_dir *
86 node2dir(const struct device_node *n)
88 struct dt_node *dn = container_of((struct device_node *)n,
89 struct dt_node, node);
90 const struct dt_dir *d = container_of(dn, struct dt_dir, node);
96 /* process_dir() calls iterate_dir(), but the latter will also call the former
97 * when recursing into sub-directories, so a predeclaration is needed.
99 static int process_dir(const char *relative_path, struct dt_dir *dt);
102 iterate_dir(struct dirent **d, int num, struct dt_dir *dt)
105 /* Iterate the directory contents */
106 for (loop = 0; loop < num; loop++) {
107 struct dt_dir *subdir;
109 /* Ignore dot files of all types (especially "..") */
110 if (d[loop]->d_name[0] == '.')
112 switch (d[loop]->d_type) {
114 process_file(d[loop], dt);
117 subdir = malloc(sizeof(*subdir));
122 strlcpy(subdir->node.node.name, d[loop]->d_name,
124 snprintf(subdir->node.node.full_name, PATH_MAX,
125 "%s/%s", dt->node.node.full_name,
128 ret = process_dir(subdir->node.node.full_name, subdir);
131 list_add_tail(&subdir->node.list, &dt->subdirs);
134 DPAAX_LOG(DEBUG, "Ignoring invalid dt entry %s/%s",
135 dt->node.node.full_name, d[loop]->d_name);
142 process_dir(const char *relative_path, struct dt_dir *dt)
147 dt->node.is_file = 0;
148 INIT_LIST_HEAD(&dt->subdirs);
149 INIT_LIST_HEAD(&dt->files);
150 ret = of_open_dir(relative_path, &d);
154 ret = iterate_dir(d, num, dt);
155 of_close_dir(d, num);
156 return (ret < 0) ? ret : 0;
160 linear_dir(struct dt_dir *d)
165 d->compatible = NULL;
171 list_for_each_entry(f, &d->files, node.list) {
172 if (!strcmp(f->node.node.name, "compatible")) {
174 DPAAX_LOG(DEBUG, "Duplicate compatible in"
175 " %s", d->node.node.full_name);
177 } else if (!strcmp(f->node.node.name, "status")) {
179 DPAAX_LOG(DEBUG, "Duplicate status in %s",
180 d->node.node.full_name);
182 } else if (!strcmp(f->node.node.name, "linux,phandle")) {
184 DPAAX_LOG(DEBUG, "Duplicate lphandle in %s",
185 d->node.node.full_name);
187 } else if (!strcmp(f->node.node.name, "phandle")) {
189 DPAAX_LOG(DEBUG, "Duplicate lphandle in %s",
190 d->node.node.full_name);
192 } else if (!strcmp(f->node.node.name, "#address-cells")) {
194 DPAAX_LOG(DEBUG, "Duplicate a_cells in %s",
195 d->node.node.full_name);
197 } else if (!strcmp(f->node.node.name, "#size-cells")) {
199 DPAAX_LOG(DEBUG, "Duplicate s_cells in %s",
200 d->node.node.full_name);
202 } else if (!strcmp(f->node.node.name, "reg")) {
204 DPAAX_LOG(DEBUG, "Duplicate reg in %s",
205 d->node.node.full_name);
210 list_for_each_entry(dd, &d->subdirs, node.list) {
211 list_add_tail(&dd->linear, &linear);
217 of_init_path(const char *dt_path)
223 /* This needs to be singleton initialization */
224 DPAAX_HWWARN(alive, "Double-init of device-tree driver!");
226 /* Prepare root node (the remaining fields are set in process_dir()) */
227 root_dir.node.node.name[0] = '\0';
228 root_dir.node.node.full_name[0] = '\0';
229 INIT_LIST_HEAD(&root_dir.node.list);
230 root_dir.parent = NULL;
232 /* Kick things off... */
233 ret = process_dir("", &root_dir);
235 DPAAX_LOG(ERR, "Unable to parse device tree");
239 /* Now make a flat, linear list of directories */
240 linear_dir(&root_dir);
246 destroy_dir(struct dt_dir *d)
248 struct dt_file *f, *tmpf;
249 struct dt_dir *dd, *tmpd;
251 list_for_each_entry_safe(f, tmpf, &d->files, node.list) {
252 list_del(&f->node.list);
255 list_for_each_entry_safe(dd, tmpd, &d->subdirs, node.list) {
257 list_del(&dd->node.list);
265 DPAAX_HWWARN(!alive, "Double-finish of device-tree driver!");
267 destroy_dir(&root_dir);
268 INIT_LIST_HEAD(&linear);
272 static const struct dt_dir *
273 next_linear(const struct dt_dir *f)
275 if (f->linear.next == &linear)
277 return list_entry(f->linear.next, struct dt_dir, linear);
281 check_compatible(const struct dt_file *f, const char *compatible)
283 const char *c = (char *)f->buf;
284 unsigned int len, remains = f->len;
288 if (!strcmp(c, compatible))
291 if (remains < len + 1)
295 remains -= (len + 1);
300 const struct device_node *
301 of_find_compatible_node(const struct device_node *from,
302 const char *type __rte_unused,
303 const char *compatible)
305 const struct dt_dir *d;
307 DPAAX_HWWARN(!alive, "Device-tree driver not initialised!");
309 if (list_empty(&linear))
312 d = list_entry(linear.next, struct dt_dir, linear);
315 for (d = next_linear(d); d && (!d->compatible ||
316 !check_compatible(d->compatible,
321 return &d->node.node;
326 of_get_property(const struct device_node *from, const char *name,
329 const struct dt_dir *d;
330 const struct dt_file *f;
332 DPAAX_HWWARN(!alive, "Device-tree driver not initialised!");
335 list_for_each_entry(f, &d->files, node.list)
336 if (!strcmp(f->node.node.name, name)) {
345 of_device_is_available(const struct device_node *dev_node)
347 const struct dt_dir *d;
349 DPAAX_HWWARN(!alive, "Device-tree driver not initialised!");
350 d = node2dir(dev_node);
353 if (!strcmp((char *)d->status->buf, "okay"))
355 if (!strcmp((char *)d->status->buf, "ok"))
360 const struct device_node *
361 of_find_node_by_phandle(uint64_t ph)
363 const struct dt_dir *d;
365 DPAAX_HWWARN(!alive, "Device-tree driver not initialised!");
366 list_for_each_entry(d, &linear, linear)
367 if (d->lphandle && (d->lphandle->len == 4) &&
368 !memcmp(d->lphandle->buf, &ph, 4))
369 return &d->node.node;
373 const struct device_node *
374 of_get_parent(const struct device_node *dev_node)
376 const struct dt_dir *d;
378 DPAAX_HWWARN(!alive, "Device-tree driver not initialised!");
382 d = node2dir(dev_node);
385 return &d->parent->node.node;
388 const struct device_node *
389 of_get_next_child(const struct device_node *dev_node,
390 const struct device_node *prev)
392 const struct dt_dir *p, *c;
394 DPAAX_HWWARN(!alive, "Device-tree driver not initialised!");
398 p = node2dir(dev_node);
401 DPAAX_HWWARN((c->parent != p), "Parent/child mismatch");
404 if (c->node.list.next == &p->subdirs)
405 /* prev was the last child */
407 c = list_entry(c->node.list.next, struct dt_dir, node.list);
408 return &c->node.node;
410 /* Return first child */
411 if (list_empty(&p->subdirs))
413 c = list_entry(p->subdirs.next, struct dt_dir, node.list);
414 return &c->node.node;
418 of_n_addr_cells(const struct device_node *dev_node)
420 const struct dt_dir *d;
422 DPAAX_HWWARN(!alive, "Device-tree driver not initialised");
424 return OF_DEFAULT_NA;
425 d = node2dir(dev_node);
426 while ((d = d->parent))
429 (unsigned char *)&d->a_cells->buf[0];
430 assert(d->a_cells->len == 4);
431 return ((uint32_t)buf[0] << 24) |
432 ((uint32_t)buf[1] << 16) |
433 ((uint32_t)buf[2] << 8) |
436 return OF_DEFAULT_NA;
440 of_n_size_cells(const struct device_node *dev_node)
442 const struct dt_dir *d;
444 DPAAX_HWWARN(!alive, "Device-tree driver not initialised!");
446 return OF_DEFAULT_NA;
447 d = node2dir(dev_node);
448 while ((d = d->parent))
451 (unsigned char *)&d->s_cells->buf[0];
452 assert(d->s_cells->len == 4);
453 return ((uint32_t)buf[0] << 24) |
454 ((uint32_t)buf[1] << 16) |
455 ((uint32_t)buf[2] << 8) |
458 return OF_DEFAULT_NS;
462 of_get_address(const struct device_node *dev_node, size_t idx,
463 uint64_t *size, uint32_t *flags __rte_unused)
465 const struct dt_dir *d;
466 const unsigned char *buf;
467 uint32_t na = of_n_addr_cells(dev_node);
468 uint32_t ns = of_n_size_cells(dev_node);
473 d = node2dir(dev_node);
476 assert(d->reg->len % ((na + ns) * 4) == 0);
477 assert(d->reg->len / ((na + ns) * 4) > (unsigned int) idx);
478 buf = (const unsigned char *)&d->reg->buf[0];
479 buf += (na + ns) * idx * 4;
481 for (*size = 0; ns > 0; ns--, na++)
482 *size = (*size << 32) +
483 (((uint32_t)buf[4 * na] << 24) |
484 ((uint32_t)buf[4 * na + 1] << 16) |
485 ((uint32_t)buf[4 * na + 2] << 8) |
486 (uint32_t)buf[4 * na + 3]);
487 return (const uint32_t *)buf;
491 of_translate_address(const struct device_node *dev_node,
492 const uint32_t *addr)
494 uint64_t phys_addr, tmp_addr;
495 const struct device_node *parent;
496 const uint32_t *ranges;
500 DPAAX_HWWARN(!alive, "Device-tree driver not initialised!");
501 assert(dev_node != NULL);
503 na = of_n_addr_cells(dev_node);
504 phys_addr = of_read_number(addr, na);
506 dev_node = of_get_parent(dev_node);
509 else if (node2dir(dev_node) == &root_dir)
513 pna = of_n_addr_cells(dev_node);
514 parent = of_get_parent(dev_node);
518 ranges = of_get_property(dev_node, "ranges", &rlen);
519 /* "ranges" property is missing. Translation breaks */
522 /* "ranges" property is empty. Do 1:1 translation */
526 tmp_addr = of_read_number(ranges + na, pna);
530 phys_addr += tmp_addr;
531 } while (node2dir(parent) != &root_dir);
537 of_device_is_compatible(const struct device_node *dev_node,
538 const char *compatible)
540 const struct dt_dir *d;
542 DPAAX_HWWARN(!alive, "Device-tree driver not initialised!");
546 d = node2dir(dev_node);
547 if (d->compatible && check_compatible(d->compatible, compatible))
552 static const void *of_get_mac_addr(const struct device_node *np,
555 return of_get_property(np, name, NULL);
559 * Search the device tree for the best MAC address to use. 'mac-address' is
560 * checked first, because that is supposed to contain to "most recent" MAC
561 * address. If that isn't set, then 'local-mac-address' is checked next,
562 * because that is the default address. If that isn't set, then the obsolete
563 * 'address' is checked, just in case we're using an old device tree.
565 * Note that the 'address' property is supposed to contain a virtual address of
566 * the register set, but some DTS files have redefined that property to be the
569 * All-zero MAC addresses are rejected, because those could be properties that
570 * exist in the device tree, but were not set by U-Boot. For example, the
571 * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
572 * addresses. Some older U-Boots only initialized 'local-mac-address'. In
573 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
576 const void *of_get_mac_address(const struct device_node *np)
580 addr = of_get_mac_addr(np, "mac-address");
584 addr = of_get_mac_addr(np, "local-mac-address");
588 return of_get_mac_addr(np, "address");