1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
3 * Copyright 2010-2016 Freescale Semiconductor Inc.
9 #include <rte_dpaa_logs.h>
12 static struct dt_dir root_dir;
13 static const char *base_dir;
14 static COMPAT_LIST_HEAD(linear);
17 of_open_dir(const char *relative_path, struct dirent ***d)
20 char full_path[PATH_MAX];
22 snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
23 ret = scandir(full_path, d, 0, versionsort);
25 DPAA_BUS_LOG(ERR, "Failed to open directory %s",
31 of_close_dir(struct dirent **d, int num)
39 of_open_file(const char *relative_path)
42 char full_path[PATH_MAX];
44 snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
45 ret = open(full_path, O_RDONLY);
47 DPAA_BUS_LOG(ERR, "Failed to open directory %s",
53 process_file(struct dirent *dent, struct dt_dir *parent)
56 struct dt_file *f = malloc(sizeof(*f));
59 DPAA_BUS_LOG(DEBUG, "Unable to allocate memory for file node");
63 snprintf(f->node.node.name, NAME_MAX, "%s", dent->d_name);
64 snprintf(f->node.node.full_name, PATH_MAX, "%s/%s",
65 parent->node.node.full_name, dent->d_name);
67 fd = of_open_file(f->node.node.full_name);
69 DPAA_BUS_LOG(DEBUG, "Unable to open file node");
73 f->len = read(fd, f->buf, OF_FILE_BUF_MAX);
76 DPAA_BUS_LOG(DEBUG, "Unable to read file node");
80 list_add_tail(&f->node.list, &parent->files);
83 static const struct dt_dir *
84 node2dir(const struct device_node *n)
86 struct dt_node *dn = container_of((struct device_node *)n,
87 struct dt_node, node);
88 const struct dt_dir *d = container_of(dn, struct dt_dir, node);
94 /* process_dir() calls iterate_dir(), but the latter will also call the former
95 * when recursing into sub-directories, so a predeclaration is needed.
97 static int process_dir(const char *relative_path, struct dt_dir *dt);
100 iterate_dir(struct dirent **d, int num, struct dt_dir *dt)
103 /* Iterate the directory contents */
104 for (loop = 0; loop < num; loop++) {
105 struct dt_dir *subdir;
107 /* Ignore dot files of all types (especially "..") */
108 if (d[loop]->d_name[0] == '.')
110 switch (d[loop]->d_type) {
112 process_file(d[loop], dt);
115 subdir = malloc(sizeof(*subdir));
120 snprintf(subdir->node.node.name, NAME_MAX, "%s",
122 snprintf(subdir->node.node.full_name, PATH_MAX,
123 "%s/%s", dt->node.node.full_name,
126 ret = process_dir(subdir->node.node.full_name, subdir);
129 list_add_tail(&subdir->node.list, &dt->subdirs);
132 DPAA_BUS_LOG(DEBUG, "Ignoring invalid dt entry %s/%s",
133 dt->node.node.full_name, d[loop]->d_name);
140 process_dir(const char *relative_path, struct dt_dir *dt)
145 dt->node.is_file = 0;
146 INIT_LIST_HEAD(&dt->subdirs);
147 INIT_LIST_HEAD(&dt->files);
148 ret = of_open_dir(relative_path, &d);
152 ret = iterate_dir(d, num, dt);
153 of_close_dir(d, num);
154 return (ret < 0) ? ret : 0;
158 linear_dir(struct dt_dir *d)
163 d->compatible = NULL;
169 list_for_each_entry(f, &d->files, node.list) {
170 if (!strcmp(f->node.node.name, "compatible")) {
172 DPAA_BUS_LOG(DEBUG, "Duplicate compatible in"
173 " %s", d->node.node.full_name);
175 } else if (!strcmp(f->node.node.name, "status")) {
177 DPAA_BUS_LOG(DEBUG, "Duplicate status in %s",
178 d->node.node.full_name);
180 } else if (!strcmp(f->node.node.name, "linux,phandle")) {
182 DPAA_BUS_LOG(DEBUG, "Duplicate lphandle in %s",
183 d->node.node.full_name);
185 } else if (!strcmp(f->node.node.name, "phandle")) {
187 DPAA_BUS_LOG(DEBUG, "Duplicate lphandle in %s",
188 d->node.node.full_name);
190 } else if (!strcmp(f->node.node.name, "#address-cells")) {
192 DPAA_BUS_LOG(DEBUG, "Duplicate a_cells in %s",
193 d->node.node.full_name);
195 } else if (!strcmp(f->node.node.name, "#size-cells")) {
197 DPAA_BUS_LOG(DEBUG, "Duplicate s_cells in %s",
198 d->node.node.full_name);
200 } else if (!strcmp(f->node.node.name, "reg")) {
202 DPAA_BUS_LOG(DEBUG, "Duplicate reg in %s",
203 d->node.node.full_name);
208 list_for_each_entry(dd, &d->subdirs, node.list) {
209 list_add_tail(&dd->linear, &linear);
215 of_init_path(const char *dt_path)
221 /* This needs to be singleton initialization */
222 DPAA_BUS_HWWARN(alive, "Double-init of device-tree driver!");
224 /* Prepare root node (the remaining fields are set in process_dir()) */
225 root_dir.node.node.name[0] = '\0';
226 root_dir.node.node.full_name[0] = '\0';
227 INIT_LIST_HEAD(&root_dir.node.list);
228 root_dir.parent = NULL;
230 /* Kick things off... */
231 ret = process_dir("", &root_dir);
233 DPAA_BUS_LOG(ERR, "Unable to parse device tree");
237 /* Now make a flat, linear list of directories */
238 linear_dir(&root_dir);
244 destroy_dir(struct dt_dir *d)
246 struct dt_file *f, *tmpf;
247 struct dt_dir *dd, *tmpd;
249 list_for_each_entry_safe(f, tmpf, &d->files, node.list) {
250 list_del(&f->node.list);
253 list_for_each_entry_safe(dd, tmpd, &d->subdirs, node.list) {
255 list_del(&dd->node.list);
263 DPAA_BUS_HWWARN(!alive, "Double-finish of device-tree driver!");
265 destroy_dir(&root_dir);
266 INIT_LIST_HEAD(&linear);
270 static const struct dt_dir *
271 next_linear(const struct dt_dir *f)
273 if (f->linear.next == &linear)
275 return list_entry(f->linear.next, struct dt_dir, linear);
279 check_compatible(const struct dt_file *f, const char *compatible)
281 const char *c = (char *)f->buf;
282 unsigned int len, remains = f->len;
286 if (!strcmp(c, compatible))
289 if (remains < len + 1)
293 remains -= (len + 1);
298 const struct device_node *
299 of_find_compatible_node(const struct device_node *from,
300 const char *type __always_unused,
301 const char *compatible)
303 const struct dt_dir *d;
305 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
307 if (list_empty(&linear))
310 d = list_entry(linear.next, struct dt_dir, linear);
313 for (d = next_linear(d); d && (!d->compatible ||
314 !check_compatible(d->compatible,
319 return &d->node.node;
324 of_get_property(const struct device_node *from, const char *name,
327 const struct dt_dir *d;
328 const struct dt_file *f;
330 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
333 list_for_each_entry(f, &d->files, node.list)
334 if (!strcmp(f->node.node.name, name)) {
343 of_device_is_available(const struct device_node *dev_node)
345 const struct dt_dir *d;
347 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
348 d = node2dir(dev_node);
351 if (!strcmp((char *)d->status->buf, "okay"))
353 if (!strcmp((char *)d->status->buf, "ok"))
358 const struct device_node *
359 of_find_node_by_phandle(phandle ph)
361 const struct dt_dir *d;
363 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
364 list_for_each_entry(d, &linear, linear)
365 if (d->lphandle && (d->lphandle->len == 4) &&
366 !memcmp(d->lphandle->buf, &ph, 4))
367 return &d->node.node;
371 const struct device_node *
372 of_get_parent(const struct device_node *dev_node)
374 const struct dt_dir *d;
376 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
380 d = node2dir(dev_node);
383 return &d->parent->node.node;
386 const struct device_node *
387 of_get_next_child(const struct device_node *dev_node,
388 const struct device_node *prev)
390 const struct dt_dir *p, *c;
392 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
396 p = node2dir(dev_node);
399 DPAA_BUS_HWWARN((c->parent != p), "Parent/child mismatch");
402 if (c->node.list.next == &p->subdirs)
403 /* prev was the last child */
405 c = list_entry(c->node.list.next, struct dt_dir, node.list);
406 return &c->node.node;
408 /* Return first child */
409 if (list_empty(&p->subdirs))
411 c = list_entry(p->subdirs.next, struct dt_dir, node.list);
412 return &c->node.node;
416 of_n_addr_cells(const struct device_node *dev_node)
418 const struct dt_dir *d;
420 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised");
422 return OF_DEFAULT_NA;
423 d = node2dir(dev_node);
424 while ((d = d->parent))
427 (unsigned char *)&d->a_cells->buf[0];
428 assert(d->a_cells->len == 4);
429 return ((uint32_t)buf[0] << 24) |
430 ((uint32_t)buf[1] << 16) |
431 ((uint32_t)buf[2] << 8) |
434 return OF_DEFAULT_NA;
438 of_n_size_cells(const struct device_node *dev_node)
440 const struct dt_dir *d;
442 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
444 return OF_DEFAULT_NA;
445 d = node2dir(dev_node);
446 while ((d = d->parent))
449 (unsigned char *)&d->s_cells->buf[0];
450 assert(d->s_cells->len == 4);
451 return ((uint32_t)buf[0] << 24) |
452 ((uint32_t)buf[1] << 16) |
453 ((uint32_t)buf[2] << 8) |
456 return OF_DEFAULT_NS;
460 of_get_address(const struct device_node *dev_node, size_t idx,
461 uint64_t *size, uint32_t *flags __rte_unused)
463 const struct dt_dir *d;
464 const unsigned char *buf;
465 uint32_t na = of_n_addr_cells(dev_node);
466 uint32_t ns = of_n_size_cells(dev_node);
471 d = node2dir(dev_node);
474 assert(d->reg->len % ((na + ns) * 4) == 0);
475 assert(d->reg->len / ((na + ns) * 4) > (unsigned int) idx);
476 buf = (const unsigned char *)&d->reg->buf[0];
477 buf += (na + ns) * idx * 4;
479 for (*size = 0; ns > 0; ns--, na++)
480 *size = (*size << 32) +
481 (((uint32_t)buf[4 * na] << 24) |
482 ((uint32_t)buf[4 * na + 1] << 16) |
483 ((uint32_t)buf[4 * na + 2] << 8) |
484 (uint32_t)buf[4 * na + 3]);
485 return (const uint32_t *)buf;
489 of_translate_address(const struct device_node *dev_node,
490 const uint32_t *addr)
492 uint64_t phys_addr, tmp_addr;
493 const struct device_node *parent;
494 const uint32_t *ranges;
498 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
499 assert(dev_node != NULL);
501 na = of_n_addr_cells(dev_node);
502 phys_addr = of_read_number(addr, na);
504 dev_node = of_get_parent(dev_node);
507 else if (node2dir(dev_node) == &root_dir)
511 pna = of_n_addr_cells(dev_node);
512 parent = of_get_parent(dev_node);
516 ranges = of_get_property(dev_node, "ranges", &rlen);
517 /* "ranges" property is missing. Translation breaks */
520 /* "ranges" property is empty. Do 1:1 translation */
524 tmp_addr = of_read_number(ranges + na, pna);
528 phys_addr += tmp_addr;
529 } while (node2dir(parent) != &root_dir);
535 of_device_is_compatible(const struct device_node *dev_node,
536 const char *compatible)
538 const struct dt_dir *d;
540 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
544 d = node2dir(dev_node);
545 if (d->compatible && check_compatible(d->compatible, compatible))
550 static const void *of_get_mac_addr(const struct device_node *np,
553 return of_get_property(np, name, NULL);
557 * Search the device tree for the best MAC address to use. 'mac-address' is
558 * checked first, because that is supposed to contain to "most recent" MAC
559 * address. If that isn't set, then 'local-mac-address' is checked next,
560 * because that is the default address. If that isn't set, then the obsolete
561 * 'address' is checked, just in case we're using an old device tree.
563 * Note that the 'address' property is supposed to contain a virtual address of
564 * the register set, but some DTS files have redefined that property to be the
567 * All-zero MAC addresses are rejected, because those could be properties that
568 * exist in the device tree, but were not set by U-Boot. For example, the
569 * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
570 * addresses. Some older U-Boots only initialized 'local-mac-address'. In
571 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
574 const void *of_get_mac_address(const struct device_node *np)
578 addr = of_get_mac_addr(np, "mac-address");
582 addr = of_get_mac_addr(np, "local-mac-address");
586 return of_get_mac_addr(np, "address");