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, "#address-cells")) {
187 DPAA_BUS_LOG(DEBUG, "Duplicate a_cells in %s",
188 d->node.node.full_name);
190 } else if (!strcmp(f->node.node.name, "#size-cells")) {
192 DPAA_BUS_LOG(DEBUG, "Duplicate s_cells in %s",
193 d->node.node.full_name);
195 } else if (!strcmp(f->node.node.name, "reg")) {
197 DPAA_BUS_LOG(DEBUG, "Duplicate reg in %s",
198 d->node.node.full_name);
203 list_for_each_entry(dd, &d->subdirs, node.list) {
204 list_add_tail(&dd->linear, &linear);
210 of_init_path(const char *dt_path)
216 /* This needs to be singleton initialization */
217 DPAA_BUS_HWWARN(alive, "Double-init of device-tree driver!");
219 /* Prepare root node (the remaining fields are set in process_dir()) */
220 root_dir.node.node.name[0] = '\0';
221 root_dir.node.node.full_name[0] = '\0';
222 INIT_LIST_HEAD(&root_dir.node.list);
223 root_dir.parent = NULL;
225 /* Kick things off... */
226 ret = process_dir("", &root_dir);
228 DPAA_BUS_LOG(ERR, "Unable to parse device tree");
232 /* Now make a flat, linear list of directories */
233 linear_dir(&root_dir);
239 destroy_dir(struct dt_dir *d)
241 struct dt_file *f, *tmpf;
242 struct dt_dir *dd, *tmpd;
244 list_for_each_entry_safe(f, tmpf, &d->files, node.list) {
245 list_del(&f->node.list);
248 list_for_each_entry_safe(dd, tmpd, &d->subdirs, node.list) {
250 list_del(&dd->node.list);
258 DPAA_BUS_HWWARN(!alive, "Double-finish of device-tree driver!");
260 destroy_dir(&root_dir);
261 INIT_LIST_HEAD(&linear);
265 static const struct dt_dir *
266 next_linear(const struct dt_dir *f)
268 if (f->linear.next == &linear)
270 return list_entry(f->linear.next, struct dt_dir, linear);
274 check_compatible(const struct dt_file *f, const char *compatible)
276 const char *c = (char *)f->buf;
277 unsigned int len, remains = f->len;
281 if (!strcmp(c, compatible))
284 if (remains < len + 1)
288 remains -= (len + 1);
293 const struct device_node *
294 of_find_compatible_node(const struct device_node *from,
295 const char *type __always_unused,
296 const char *compatible)
298 const struct dt_dir *d;
300 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
302 if (list_empty(&linear))
305 d = list_entry(linear.next, struct dt_dir, linear);
308 for (d = next_linear(d); d && (!d->compatible ||
309 !check_compatible(d->compatible,
314 return &d->node.node;
319 of_get_property(const struct device_node *from, const char *name,
322 const struct dt_dir *d;
323 const struct dt_file *f;
325 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
328 list_for_each_entry(f, &d->files, node.list)
329 if (!strcmp(f->node.node.name, name)) {
338 of_device_is_available(const struct device_node *dev_node)
340 const struct dt_dir *d;
342 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
343 d = node2dir(dev_node);
346 if (!strcmp((char *)d->status->buf, "okay"))
348 if (!strcmp((char *)d->status->buf, "ok"))
353 const struct device_node *
354 of_find_node_by_phandle(phandle ph)
356 const struct dt_dir *d;
358 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
359 list_for_each_entry(d, &linear, linear)
360 if (d->lphandle && (d->lphandle->len == 4) &&
361 !memcmp(d->lphandle->buf, &ph, 4))
362 return &d->node.node;
366 const struct device_node *
367 of_get_parent(const struct device_node *dev_node)
369 const struct dt_dir *d;
371 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
375 d = node2dir(dev_node);
378 return &d->parent->node.node;
381 const struct device_node *
382 of_get_next_child(const struct device_node *dev_node,
383 const struct device_node *prev)
385 const struct dt_dir *p, *c;
387 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
391 p = node2dir(dev_node);
394 DPAA_BUS_HWWARN((c->parent != p), "Parent/child mismatch");
397 if (c->node.list.next == &p->subdirs)
398 /* prev was the last child */
400 c = list_entry(c->node.list.next, struct dt_dir, node.list);
401 return &c->node.node;
403 /* Return first child */
404 if (list_empty(&p->subdirs))
406 c = list_entry(p->subdirs.next, struct dt_dir, node.list);
407 return &c->node.node;
411 of_n_addr_cells(const struct device_node *dev_node)
413 const struct dt_dir *d;
415 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised");
417 return OF_DEFAULT_NA;
418 d = node2dir(dev_node);
419 while ((d = d->parent))
422 (unsigned char *)&d->a_cells->buf[0];
423 assert(d->a_cells->len == 4);
424 return ((uint32_t)buf[0] << 24) |
425 ((uint32_t)buf[1] << 16) |
426 ((uint32_t)buf[2] << 8) |
429 return OF_DEFAULT_NA;
433 of_n_size_cells(const struct device_node *dev_node)
435 const struct dt_dir *d;
437 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
439 return OF_DEFAULT_NA;
440 d = node2dir(dev_node);
441 while ((d = d->parent))
444 (unsigned char *)&d->s_cells->buf[0];
445 assert(d->s_cells->len == 4);
446 return ((uint32_t)buf[0] << 24) |
447 ((uint32_t)buf[1] << 16) |
448 ((uint32_t)buf[2] << 8) |
451 return OF_DEFAULT_NS;
455 of_get_address(const struct device_node *dev_node, size_t idx,
456 uint64_t *size, uint32_t *flags __rte_unused)
458 const struct dt_dir *d;
459 const unsigned char *buf;
460 uint32_t na = of_n_addr_cells(dev_node);
461 uint32_t ns = of_n_size_cells(dev_node);
466 d = node2dir(dev_node);
469 assert(d->reg->len % ((na + ns) * 4) == 0);
470 assert(d->reg->len / ((na + ns) * 4) > (unsigned int) idx);
471 buf = (const unsigned char *)&d->reg->buf[0];
472 buf += (na + ns) * idx * 4;
474 for (*size = 0; ns > 0; ns--, na++)
475 *size = (*size << 32) +
476 (((uint32_t)buf[4 * na] << 24) |
477 ((uint32_t)buf[4 * na + 1] << 16) |
478 ((uint32_t)buf[4 * na + 2] << 8) |
479 (uint32_t)buf[4 * na + 3]);
480 return (const uint32_t *)buf;
484 of_translate_address(const struct device_node *dev_node,
485 const uint32_t *addr)
487 uint64_t phys_addr, tmp_addr;
488 const struct device_node *parent;
489 const uint32_t *ranges;
493 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
494 assert(dev_node != NULL);
496 na = of_n_addr_cells(dev_node);
497 phys_addr = of_read_number(addr, na);
499 dev_node = of_get_parent(dev_node);
502 else if (node2dir(dev_node) == &root_dir)
506 pna = of_n_addr_cells(dev_node);
507 parent = of_get_parent(dev_node);
511 ranges = of_get_property(dev_node, "ranges", &rlen);
512 /* "ranges" property is missing. Translation breaks */
515 /* "ranges" property is empty. Do 1:1 translation */
519 tmp_addr = of_read_number(ranges + na, pna);
523 phys_addr += tmp_addr;
524 } while (node2dir(parent) != &root_dir);
530 of_device_is_compatible(const struct device_node *dev_node,
531 const char *compatible)
533 const struct dt_dir *d;
535 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
539 d = node2dir(dev_node);
540 if (d->compatible && check_compatible(d->compatible, compatible))