1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
3 * Copyright 2010-2016 Freescale Semiconductor Inc.
9 #include <rte_string_fns.h>
10 #include <rte_dpaa_logs.h>
13 static struct dt_dir root_dir;
14 static const char *base_dir;
15 static COMPAT_LIST_HEAD(linear);
18 of_open_dir(const char *relative_path, struct dirent ***d)
21 char full_path[PATH_MAX];
23 snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
24 ret = scandir(full_path, d, 0, versionsort);
26 DPAA_BUS_LOG(ERR, "Failed to open directory %s",
32 of_close_dir(struct dirent **d, int num)
40 of_open_file(const char *relative_path)
43 char full_path[PATH_MAX];
45 snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
46 ret = open(full_path, O_RDONLY);
48 DPAA_BUS_LOG(ERR, "Failed to open directory %s",
54 process_file(struct dirent *dent, struct dt_dir *parent)
57 struct dt_file *f = malloc(sizeof(*f));
60 DPAA_BUS_LOG(DEBUG, "Unable to allocate memory for file node");
64 strlcpy(f->node.node.name, dent->d_name, NAME_MAX);
65 snprintf(f->node.node.full_name, PATH_MAX, "%s/%s",
66 parent->node.node.full_name, dent->d_name);
68 fd = of_open_file(f->node.node.full_name);
70 DPAA_BUS_LOG(DEBUG, "Unable to open file node");
74 f->len = read(fd, f->buf, OF_FILE_BUF_MAX);
77 DPAA_BUS_LOG(DEBUG, "Unable to read file node");
81 list_add_tail(&f->node.list, &parent->files);
84 static const struct dt_dir *
85 node2dir(const struct device_node *n)
87 struct dt_node *dn = container_of((struct device_node *)n,
88 struct dt_node, node);
89 const struct dt_dir *d = container_of(dn, struct dt_dir, node);
95 /* process_dir() calls iterate_dir(), but the latter will also call the former
96 * when recursing into sub-directories, so a predeclaration is needed.
98 static int process_dir(const char *relative_path, struct dt_dir *dt);
101 iterate_dir(struct dirent **d, int num, struct dt_dir *dt)
104 /* Iterate the directory contents */
105 for (loop = 0; loop < num; loop++) {
106 struct dt_dir *subdir;
108 /* Ignore dot files of all types (especially "..") */
109 if (d[loop]->d_name[0] == '.')
111 switch (d[loop]->d_type) {
113 process_file(d[loop], dt);
116 subdir = malloc(sizeof(*subdir));
121 strlcpy(subdir->node.node.name, d[loop]->d_name,
123 snprintf(subdir->node.node.full_name, PATH_MAX,
124 "%s/%s", dt->node.node.full_name,
127 ret = process_dir(subdir->node.node.full_name, subdir);
130 list_add_tail(&subdir->node.list, &dt->subdirs);
133 DPAA_BUS_LOG(DEBUG, "Ignoring invalid dt entry %s/%s",
134 dt->node.node.full_name, d[loop]->d_name);
141 process_dir(const char *relative_path, struct dt_dir *dt)
146 dt->node.is_file = 0;
147 INIT_LIST_HEAD(&dt->subdirs);
148 INIT_LIST_HEAD(&dt->files);
149 ret = of_open_dir(relative_path, &d);
153 ret = iterate_dir(d, num, dt);
154 of_close_dir(d, num);
155 return (ret < 0) ? ret : 0;
159 linear_dir(struct dt_dir *d)
164 d->compatible = NULL;
170 list_for_each_entry(f, &d->files, node.list) {
171 if (!strcmp(f->node.node.name, "compatible")) {
173 DPAA_BUS_LOG(DEBUG, "Duplicate compatible in"
174 " %s", d->node.node.full_name);
176 } else if (!strcmp(f->node.node.name, "status")) {
178 DPAA_BUS_LOG(DEBUG, "Duplicate status in %s",
179 d->node.node.full_name);
181 } else if (!strcmp(f->node.node.name, "linux,phandle")) {
183 DPAA_BUS_LOG(DEBUG, "Duplicate lphandle in %s",
184 d->node.node.full_name);
186 } else if (!strcmp(f->node.node.name, "phandle")) {
188 DPAA_BUS_LOG(DEBUG, "Duplicate lphandle in %s",
189 d->node.node.full_name);
191 } else if (!strcmp(f->node.node.name, "#address-cells")) {
193 DPAA_BUS_LOG(DEBUG, "Duplicate a_cells in %s",
194 d->node.node.full_name);
196 } else if (!strcmp(f->node.node.name, "#size-cells")) {
198 DPAA_BUS_LOG(DEBUG, "Duplicate s_cells in %s",
199 d->node.node.full_name);
201 } else if (!strcmp(f->node.node.name, "reg")) {
203 DPAA_BUS_LOG(DEBUG, "Duplicate reg in %s",
204 d->node.node.full_name);
209 list_for_each_entry(dd, &d->subdirs, node.list) {
210 list_add_tail(&dd->linear, &linear);
216 of_init_path(const char *dt_path)
222 /* This needs to be singleton initialization */
223 DPAA_BUS_HWWARN(alive, "Double-init of device-tree driver!");
225 /* Prepare root node (the remaining fields are set in process_dir()) */
226 root_dir.node.node.name[0] = '\0';
227 root_dir.node.node.full_name[0] = '\0';
228 INIT_LIST_HEAD(&root_dir.node.list);
229 root_dir.parent = NULL;
231 /* Kick things off... */
232 ret = process_dir("", &root_dir);
234 DPAA_BUS_LOG(ERR, "Unable to parse device tree");
238 /* Now make a flat, linear list of directories */
239 linear_dir(&root_dir);
245 destroy_dir(struct dt_dir *d)
247 struct dt_file *f, *tmpf;
248 struct dt_dir *dd, *tmpd;
250 list_for_each_entry_safe(f, tmpf, &d->files, node.list) {
251 list_del(&f->node.list);
254 list_for_each_entry_safe(dd, tmpd, &d->subdirs, node.list) {
256 list_del(&dd->node.list);
264 DPAA_BUS_HWWARN(!alive, "Double-finish of device-tree driver!");
266 destroy_dir(&root_dir);
267 INIT_LIST_HEAD(&linear);
271 static const struct dt_dir *
272 next_linear(const struct dt_dir *f)
274 if (f->linear.next == &linear)
276 return list_entry(f->linear.next, struct dt_dir, linear);
280 check_compatible(const struct dt_file *f, const char *compatible)
282 const char *c = (char *)f->buf;
283 unsigned int len, remains = f->len;
287 if (!strcmp(c, compatible))
290 if (remains < len + 1)
294 remains -= (len + 1);
299 const struct device_node *
300 of_find_compatible_node(const struct device_node *from,
301 const char *type __always_unused,
302 const char *compatible)
304 const struct dt_dir *d;
306 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
308 if (list_empty(&linear))
311 d = list_entry(linear.next, struct dt_dir, linear);
314 for (d = next_linear(d); d && (!d->compatible ||
315 !check_compatible(d->compatible,
320 return &d->node.node;
325 of_get_property(const struct device_node *from, const char *name,
328 const struct dt_dir *d;
329 const struct dt_file *f;
331 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
334 list_for_each_entry(f, &d->files, node.list)
335 if (!strcmp(f->node.node.name, name)) {
344 of_device_is_available(const struct device_node *dev_node)
346 const struct dt_dir *d;
348 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
349 d = node2dir(dev_node);
352 if (!strcmp((char *)d->status->buf, "okay"))
354 if (!strcmp((char *)d->status->buf, "ok"))
359 const struct device_node *
360 of_find_node_by_phandle(phandle ph)
362 const struct dt_dir *d;
364 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
365 list_for_each_entry(d, &linear, linear)
366 if (d->lphandle && (d->lphandle->len == 4) &&
367 !memcmp(d->lphandle->buf, &ph, 4))
368 return &d->node.node;
372 const struct device_node *
373 of_get_parent(const struct device_node *dev_node)
375 const struct dt_dir *d;
377 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
381 d = node2dir(dev_node);
384 return &d->parent->node.node;
387 const struct device_node *
388 of_get_next_child(const struct device_node *dev_node,
389 const struct device_node *prev)
391 const struct dt_dir *p, *c;
393 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
397 p = node2dir(dev_node);
400 DPAA_BUS_HWWARN((c->parent != p), "Parent/child mismatch");
403 if (c->node.list.next == &p->subdirs)
404 /* prev was the last child */
406 c = list_entry(c->node.list.next, struct dt_dir, node.list);
407 return &c->node.node;
409 /* Return first child */
410 if (list_empty(&p->subdirs))
412 c = list_entry(p->subdirs.next, struct dt_dir, node.list);
413 return &c->node.node;
417 of_n_addr_cells(const struct device_node *dev_node)
419 const struct dt_dir *d;
421 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised");
423 return OF_DEFAULT_NA;
424 d = node2dir(dev_node);
425 while ((d = d->parent))
428 (unsigned char *)&d->a_cells->buf[0];
429 assert(d->a_cells->len == 4);
430 return ((uint32_t)buf[0] << 24) |
431 ((uint32_t)buf[1] << 16) |
432 ((uint32_t)buf[2] << 8) |
435 return OF_DEFAULT_NA;
439 of_n_size_cells(const struct device_node *dev_node)
441 const struct dt_dir *d;
443 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
445 return OF_DEFAULT_NA;
446 d = node2dir(dev_node);
447 while ((d = d->parent))
450 (unsigned char *)&d->s_cells->buf[0];
451 assert(d->s_cells->len == 4);
452 return ((uint32_t)buf[0] << 24) |
453 ((uint32_t)buf[1] << 16) |
454 ((uint32_t)buf[2] << 8) |
457 return OF_DEFAULT_NS;
461 of_get_address(const struct device_node *dev_node, size_t idx,
462 uint64_t *size, uint32_t *flags __rte_unused)
464 const struct dt_dir *d;
465 const unsigned char *buf;
466 uint32_t na = of_n_addr_cells(dev_node);
467 uint32_t ns = of_n_size_cells(dev_node);
472 d = node2dir(dev_node);
475 assert(d->reg->len % ((na + ns) * 4) == 0);
476 assert(d->reg->len / ((na + ns) * 4) > (unsigned int) idx);
477 buf = (const unsigned char *)&d->reg->buf[0];
478 buf += (na + ns) * idx * 4;
480 for (*size = 0; ns > 0; ns--, na++)
481 *size = (*size << 32) +
482 (((uint32_t)buf[4 * na] << 24) |
483 ((uint32_t)buf[4 * na + 1] << 16) |
484 ((uint32_t)buf[4 * na + 2] << 8) |
485 (uint32_t)buf[4 * na + 3]);
486 return (const uint32_t *)buf;
490 of_translate_address(const struct device_node *dev_node,
491 const uint32_t *addr)
493 uint64_t phys_addr, tmp_addr;
494 const struct device_node *parent;
495 const uint32_t *ranges;
499 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
500 assert(dev_node != NULL);
502 na = of_n_addr_cells(dev_node);
503 phys_addr = of_read_number(addr, na);
505 dev_node = of_get_parent(dev_node);
508 else if (node2dir(dev_node) == &root_dir)
512 pna = of_n_addr_cells(dev_node);
513 parent = of_get_parent(dev_node);
517 ranges = of_get_property(dev_node, "ranges", &rlen);
518 /* "ranges" property is missing. Translation breaks */
521 /* "ranges" property is empty. Do 1:1 translation */
525 tmp_addr = of_read_number(ranges + na, pna);
529 phys_addr += tmp_addr;
530 } while (node2dir(parent) != &root_dir);
536 of_device_is_compatible(const struct device_node *dev_node,
537 const char *compatible)
539 const struct dt_dir *d;
541 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
545 d = node2dir(dev_node);
546 if (d->compatible && check_compatible(d->compatible, compatible))
551 static const void *of_get_mac_addr(const struct device_node *np,
554 return of_get_property(np, name, NULL);
558 * Search the device tree for the best MAC address to use. 'mac-address' is
559 * checked first, because that is supposed to contain to "most recent" MAC
560 * address. If that isn't set, then 'local-mac-address' is checked next,
561 * because that is the default address. If that isn't set, then the obsolete
562 * 'address' is checked, just in case we're using an old device tree.
564 * Note that the 'address' property is supposed to contain a virtual address of
565 * the register set, but some DTS files have redefined that property to be the
568 * All-zero MAC addresses are rejected, because those could be properties that
569 * exist in the device tree, but were not set by U-Boot. For example, the
570 * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
571 * addresses. Some older U-Boots only initialized 'local-mac-address'. In
572 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
575 const void *of_get_mac_address(const struct device_node *np)
579 addr = of_get_mac_addr(np, "mac-address");
583 addr = of_get_mac_addr(np, "local-mac-address");
587 return of_get_mac_addr(np, "address");