2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright 2010-2016 Freescale Semiconductor Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * * Neither the name of the above-listed copyright holders nor the
18 * names of any contributors may be used to endorse or promote products
19 * derived from this software without specific prior written permission.
23 * ALTERNATIVELY, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") as published by the Free Software
25 * Foundation, either version 2 of that License or (at your option) any
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
42 #include <rte_dpaa_logs.h>
45 static struct dt_dir root_dir;
46 static const char *base_dir;
47 static COMPAT_LIST_HEAD(linear);
50 of_open_dir(const char *relative_path, struct dirent ***d)
53 char full_path[PATH_MAX];
55 snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
56 ret = scandir(full_path, d, 0, versionsort);
58 DPAA_BUS_LOG(ERR, "Failed to open directory %s",
64 of_close_dir(struct dirent **d, int num)
72 of_open_file(const char *relative_path)
75 char full_path[PATH_MAX];
77 snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
78 ret = open(full_path, O_RDONLY);
80 DPAA_BUS_LOG(ERR, "Failed to open directory %s",
86 process_file(struct dirent *dent, struct dt_dir *parent)
89 struct dt_file *f = malloc(sizeof(*f));
92 DPAA_BUS_LOG(DEBUG, "Unable to allocate memory for file node");
96 snprintf(f->node.node.name, NAME_MAX, "%s", dent->d_name);
97 snprintf(f->node.node.full_name, PATH_MAX, "%s/%s",
98 parent->node.node.full_name, dent->d_name);
100 fd = of_open_file(f->node.node.full_name);
102 DPAA_BUS_LOG(DEBUG, "Unable to open file node");
106 f->len = read(fd, f->buf, OF_FILE_BUF_MAX);
109 DPAA_BUS_LOG(DEBUG, "Unable to read file node");
113 list_add_tail(&f->node.list, &parent->files);
116 static const struct dt_dir *
117 node2dir(const struct device_node *n)
119 struct dt_node *dn = container_of((struct device_node *)n,
120 struct dt_node, node);
121 const struct dt_dir *d = container_of(dn, struct dt_dir, node);
123 assert(!dn->is_file);
127 /* process_dir() calls iterate_dir(), but the latter will also call the former
128 * when recursing into sub-directories, so a predeclaration is needed.
130 static int process_dir(const char *relative_path, struct dt_dir *dt);
133 iterate_dir(struct dirent **d, int num, struct dt_dir *dt)
136 /* Iterate the directory contents */
137 for (loop = 0; loop < num; loop++) {
138 struct dt_dir *subdir;
140 /* Ignore dot files of all types (especially "..") */
141 if (d[loop]->d_name[0] == '.')
143 switch (d[loop]->d_type) {
145 process_file(d[loop], dt);
148 subdir = malloc(sizeof(*subdir));
153 snprintf(subdir->node.node.name, NAME_MAX, "%s",
155 snprintf(subdir->node.node.full_name, PATH_MAX,
156 "%s/%s", dt->node.node.full_name,
159 ret = process_dir(subdir->node.node.full_name, subdir);
162 list_add_tail(&subdir->node.list, &dt->subdirs);
165 DPAA_BUS_LOG(DEBUG, "Ignoring invalid dt entry %s/%s",
166 dt->node.node.full_name, d[loop]->d_name);
173 process_dir(const char *relative_path, struct dt_dir *dt)
178 dt->node.is_file = 0;
179 INIT_LIST_HEAD(&dt->subdirs);
180 INIT_LIST_HEAD(&dt->files);
181 ret = of_open_dir(relative_path, &d);
185 ret = iterate_dir(d, num, dt);
186 of_close_dir(d, num);
187 return (ret < 0) ? ret : 0;
191 linear_dir(struct dt_dir *d)
196 d->compatible = NULL;
202 list_for_each_entry(f, &d->files, node.list) {
203 if (!strcmp(f->node.node.name, "compatible")) {
205 DPAA_BUS_LOG(DEBUG, "Duplicate compatible in"
206 " %s", d->node.node.full_name);
208 } else if (!strcmp(f->node.node.name, "status")) {
210 DPAA_BUS_LOG(DEBUG, "Duplicate status in %s",
211 d->node.node.full_name);
213 } else if (!strcmp(f->node.node.name, "linux,phandle")) {
215 DPAA_BUS_LOG(DEBUG, "Duplicate lphandle in %s",
216 d->node.node.full_name);
218 } else if (!strcmp(f->node.node.name, "phandle")) {
220 DPAA_BUS_LOG(DEBUG, "Duplicate lphandle in %s",
221 d->node.node.full_name);
223 } else if (!strcmp(f->node.node.name, "#address-cells")) {
225 DPAA_BUS_LOG(DEBUG, "Duplicate a_cells in %s",
226 d->node.node.full_name);
228 } else if (!strcmp(f->node.node.name, "#size-cells")) {
230 DPAA_BUS_LOG(DEBUG, "Duplicate s_cells in %s",
231 d->node.node.full_name);
233 } else if (!strcmp(f->node.node.name, "reg")) {
235 DPAA_BUS_LOG(DEBUG, "Duplicate reg in %s",
236 d->node.node.full_name);
241 list_for_each_entry(dd, &d->subdirs, node.list) {
242 list_add_tail(&dd->linear, &linear);
248 of_init_path(const char *dt_path)
254 /* This needs to be singleton initialization */
255 DPAA_BUS_HWWARN(alive, "Double-init of device-tree driver!");
257 /* Prepare root node (the remaining fields are set in process_dir()) */
258 root_dir.node.node.name[0] = '\0';
259 root_dir.node.node.full_name[0] = '\0';
260 INIT_LIST_HEAD(&root_dir.node.list);
261 root_dir.parent = NULL;
263 /* Kick things off... */
264 ret = process_dir("", &root_dir);
266 DPAA_BUS_LOG(ERR, "Unable to parse device tree");
270 /* Now make a flat, linear list of directories */
271 linear_dir(&root_dir);
277 destroy_dir(struct dt_dir *d)
279 struct dt_file *f, *tmpf;
280 struct dt_dir *dd, *tmpd;
282 list_for_each_entry_safe(f, tmpf, &d->files, node.list) {
283 list_del(&f->node.list);
286 list_for_each_entry_safe(dd, tmpd, &d->subdirs, node.list) {
288 list_del(&dd->node.list);
296 DPAA_BUS_HWWARN(!alive, "Double-finish of device-tree driver!");
298 destroy_dir(&root_dir);
299 INIT_LIST_HEAD(&linear);
303 static const struct dt_dir *
304 next_linear(const struct dt_dir *f)
306 if (f->linear.next == &linear)
308 return list_entry(f->linear.next, struct dt_dir, linear);
312 check_compatible(const struct dt_file *f, const char *compatible)
314 const char *c = (char *)f->buf;
315 unsigned int len, remains = f->len;
319 if (!strcmp(c, compatible))
322 if (remains < len + 1)
326 remains -= (len + 1);
331 const struct device_node *
332 of_find_compatible_node(const struct device_node *from,
333 const char *type __always_unused,
334 const char *compatible)
336 const struct dt_dir *d;
338 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
340 if (list_empty(&linear))
343 d = list_entry(linear.next, struct dt_dir, linear);
346 for (d = next_linear(d); d && (!d->compatible ||
347 !check_compatible(d->compatible,
352 return &d->node.node;
357 of_get_property(const struct device_node *from, const char *name,
360 const struct dt_dir *d;
361 const struct dt_file *f;
363 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
366 list_for_each_entry(f, &d->files, node.list)
367 if (!strcmp(f->node.node.name, name)) {
376 of_device_is_available(const struct device_node *dev_node)
378 const struct dt_dir *d;
380 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
381 d = node2dir(dev_node);
384 if (!strcmp((char *)d->status->buf, "okay"))
386 if (!strcmp((char *)d->status->buf, "ok"))
391 const struct device_node *
392 of_find_node_by_phandle(phandle ph)
394 const struct dt_dir *d;
396 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
397 list_for_each_entry(d, &linear, linear)
398 if (d->lphandle && (d->lphandle->len == 4) &&
399 !memcmp(d->lphandle->buf, &ph, 4))
400 return &d->node.node;
404 const struct device_node *
405 of_get_parent(const struct device_node *dev_node)
407 const struct dt_dir *d;
409 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
413 d = node2dir(dev_node);
416 return &d->parent->node.node;
419 const struct device_node *
420 of_get_next_child(const struct device_node *dev_node,
421 const struct device_node *prev)
423 const struct dt_dir *p, *c;
425 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
429 p = node2dir(dev_node);
432 DPAA_BUS_HWWARN((c->parent != p), "Parent/child mismatch");
435 if (c->node.list.next == &p->subdirs)
436 /* prev was the last child */
438 c = list_entry(c->node.list.next, struct dt_dir, node.list);
439 return &c->node.node;
441 /* Return first child */
442 if (list_empty(&p->subdirs))
444 c = list_entry(p->subdirs.next, struct dt_dir, node.list);
445 return &c->node.node;
449 of_n_addr_cells(const struct device_node *dev_node)
451 const struct dt_dir *d;
453 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised");
455 return OF_DEFAULT_NA;
456 d = node2dir(dev_node);
457 while ((d = d->parent))
460 (unsigned char *)&d->a_cells->buf[0];
461 assert(d->a_cells->len == 4);
462 return ((uint32_t)buf[0] << 24) |
463 ((uint32_t)buf[1] << 16) |
464 ((uint32_t)buf[2] << 8) |
467 return OF_DEFAULT_NA;
471 of_n_size_cells(const struct device_node *dev_node)
473 const struct dt_dir *d;
475 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
477 return OF_DEFAULT_NA;
478 d = node2dir(dev_node);
479 while ((d = d->parent))
482 (unsigned char *)&d->s_cells->buf[0];
483 assert(d->s_cells->len == 4);
484 return ((uint32_t)buf[0] << 24) |
485 ((uint32_t)buf[1] << 16) |
486 ((uint32_t)buf[2] << 8) |
489 return OF_DEFAULT_NS;
493 of_get_address(const struct device_node *dev_node, size_t idx,
494 uint64_t *size, uint32_t *flags __rte_unused)
496 const struct dt_dir *d;
497 const unsigned char *buf;
498 uint32_t na = of_n_addr_cells(dev_node);
499 uint32_t ns = of_n_size_cells(dev_node);
504 d = node2dir(dev_node);
507 assert(d->reg->len % ((na + ns) * 4) == 0);
508 assert(d->reg->len / ((na + ns) * 4) > (unsigned int) idx);
509 buf = (const unsigned char *)&d->reg->buf[0];
510 buf += (na + ns) * idx * 4;
512 for (*size = 0; ns > 0; ns--, na++)
513 *size = (*size << 32) +
514 (((uint32_t)buf[4 * na] << 24) |
515 ((uint32_t)buf[4 * na + 1] << 16) |
516 ((uint32_t)buf[4 * na + 2] << 8) |
517 (uint32_t)buf[4 * na + 3]);
518 return (const uint32_t *)buf;
522 of_translate_address(const struct device_node *dev_node,
523 const uint32_t *addr)
525 uint64_t phys_addr, tmp_addr;
526 const struct device_node *parent;
527 const uint32_t *ranges;
531 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
532 assert(dev_node != NULL);
534 na = of_n_addr_cells(dev_node);
535 phys_addr = of_read_number(addr, na);
537 dev_node = of_get_parent(dev_node);
540 else if (node2dir(dev_node) == &root_dir)
544 pna = of_n_addr_cells(dev_node);
545 parent = of_get_parent(dev_node);
549 ranges = of_get_property(dev_node, "ranges", &rlen);
550 /* "ranges" property is missing. Translation breaks */
553 /* "ranges" property is empty. Do 1:1 translation */
557 tmp_addr = of_read_number(ranges + na, pna);
561 phys_addr += tmp_addr;
562 } while (node2dir(parent) != &root_dir);
568 of_device_is_compatible(const struct device_node *dev_node,
569 const char *compatible)
571 const struct dt_dir *d;
573 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
577 d = node2dir(dev_node);
578 if (d->compatible && check_compatible(d->compatible, compatible))