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, "#address-cells")) {
220 DPAA_BUS_LOG(DEBUG, "Duplicate a_cells in %s",
221 d->node.node.full_name);
223 } else if (!strcmp(f->node.node.name, "#size-cells")) {
225 DPAA_BUS_LOG(DEBUG, "Duplicate s_cells in %s",
226 d->node.node.full_name);
228 } else if (!strcmp(f->node.node.name, "reg")) {
230 DPAA_BUS_LOG(DEBUG, "Duplicate reg in %s",
231 d->node.node.full_name);
236 list_for_each_entry(dd, &d->subdirs, node.list) {
237 list_add_tail(&dd->linear, &linear);
243 of_init_path(const char *dt_path)
249 /* This needs to be singleton initialization */
250 DPAA_BUS_HWWARN(alive, "Double-init of device-tree driver!");
252 /* Prepare root node (the remaining fields are set in process_dir()) */
253 root_dir.node.node.name[0] = '\0';
254 root_dir.node.node.full_name[0] = '\0';
255 INIT_LIST_HEAD(&root_dir.node.list);
256 root_dir.parent = NULL;
258 /* Kick things off... */
259 ret = process_dir("", &root_dir);
261 DPAA_BUS_LOG(ERR, "Unable to parse device tree");
265 /* Now make a flat, linear list of directories */
266 linear_dir(&root_dir);
272 destroy_dir(struct dt_dir *d)
274 struct dt_file *f, *tmpf;
275 struct dt_dir *dd, *tmpd;
277 list_for_each_entry_safe(f, tmpf, &d->files, node.list) {
278 list_del(&f->node.list);
281 list_for_each_entry_safe(dd, tmpd, &d->subdirs, node.list) {
283 list_del(&dd->node.list);
291 DPAA_BUS_HWWARN(!alive, "Double-finish of device-tree driver!");
293 destroy_dir(&root_dir);
294 INIT_LIST_HEAD(&linear);
298 static const struct dt_dir *
299 next_linear(const struct dt_dir *f)
301 if (f->linear.next == &linear)
303 return list_entry(f->linear.next, struct dt_dir, linear);
307 check_compatible(const struct dt_file *f, const char *compatible)
309 const char *c = (char *)f->buf;
310 unsigned int len, remains = f->len;
314 if (!strcmp(c, compatible))
317 if (remains < len + 1)
321 remains -= (len + 1);
326 const struct device_node *
327 of_find_compatible_node(const struct device_node *from,
328 const char *type __always_unused,
329 const char *compatible)
331 const struct dt_dir *d;
333 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
335 if (list_empty(&linear))
338 d = list_entry(linear.next, struct dt_dir, linear);
341 for (d = next_linear(d); d && (!d->compatible ||
342 !check_compatible(d->compatible,
347 return &d->node.node;
352 of_get_property(const struct device_node *from, const char *name,
355 const struct dt_dir *d;
356 const struct dt_file *f;
358 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
361 list_for_each_entry(f, &d->files, node.list)
362 if (!strcmp(f->node.node.name, name)) {
371 of_device_is_available(const struct device_node *dev_node)
373 const struct dt_dir *d;
375 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
376 d = node2dir(dev_node);
379 if (!strcmp((char *)d->status->buf, "okay"))
381 if (!strcmp((char *)d->status->buf, "ok"))
386 const struct device_node *
387 of_find_node_by_phandle(phandle ph)
389 const struct dt_dir *d;
391 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
392 list_for_each_entry(d, &linear, linear)
393 if (d->lphandle && (d->lphandle->len == 4) &&
394 !memcmp(d->lphandle->buf, &ph, 4))
395 return &d->node.node;
399 const struct device_node *
400 of_get_parent(const struct device_node *dev_node)
402 const struct dt_dir *d;
404 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
408 d = node2dir(dev_node);
411 return &d->parent->node.node;
414 const struct device_node *
415 of_get_next_child(const struct device_node *dev_node,
416 const struct device_node *prev)
418 const struct dt_dir *p, *c;
420 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
424 p = node2dir(dev_node);
427 DPAA_BUS_HWWARN((c->parent != p), "Parent/child mismatch");
430 if (c->node.list.next == &p->subdirs)
431 /* prev was the last child */
433 c = list_entry(c->node.list.next, struct dt_dir, node.list);
434 return &c->node.node;
436 /* Return first child */
437 if (list_empty(&p->subdirs))
439 c = list_entry(p->subdirs.next, struct dt_dir, node.list);
440 return &c->node.node;
444 of_n_addr_cells(const struct device_node *dev_node)
446 const struct dt_dir *d;
448 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised");
450 return OF_DEFAULT_NA;
451 d = node2dir(dev_node);
452 while ((d = d->parent))
455 (unsigned char *)&d->a_cells->buf[0];
456 assert(d->a_cells->len == 4);
457 return ((uint32_t)buf[0] << 24) |
458 ((uint32_t)buf[1] << 16) |
459 ((uint32_t)buf[2] << 8) |
462 return OF_DEFAULT_NA;
466 of_n_size_cells(const struct device_node *dev_node)
468 const struct dt_dir *d;
470 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
472 return OF_DEFAULT_NA;
473 d = node2dir(dev_node);
474 while ((d = d->parent))
477 (unsigned char *)&d->s_cells->buf[0];
478 assert(d->s_cells->len == 4);
479 return ((uint32_t)buf[0] << 24) |
480 ((uint32_t)buf[1] << 16) |
481 ((uint32_t)buf[2] << 8) |
484 return OF_DEFAULT_NS;
488 of_get_address(const struct device_node *dev_node, size_t idx,
489 uint64_t *size, uint32_t *flags __rte_unused)
491 const struct dt_dir *d;
492 const unsigned char *buf;
493 uint32_t na = of_n_addr_cells(dev_node);
494 uint32_t ns = of_n_size_cells(dev_node);
499 d = node2dir(dev_node);
502 assert(d->reg->len % ((na + ns) * 4) == 0);
503 assert(d->reg->len / ((na + ns) * 4) > (unsigned int) idx);
504 buf = (const unsigned char *)&d->reg->buf[0];
505 buf += (na + ns) * idx * 4;
507 for (*size = 0; ns > 0; ns--, na++)
508 *size = (*size << 32) +
509 (((uint32_t)buf[4 * na] << 24) |
510 ((uint32_t)buf[4 * na + 1] << 16) |
511 ((uint32_t)buf[4 * na + 2] << 8) |
512 (uint32_t)buf[4 * na + 3]);
513 return (const uint32_t *)buf;
517 of_translate_address(const struct device_node *dev_node,
518 const uint32_t *addr)
520 uint64_t phys_addr, tmp_addr;
521 const struct device_node *parent;
522 const uint32_t *ranges;
526 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
527 assert(dev_node != NULL);
529 na = of_n_addr_cells(dev_node);
530 phys_addr = of_read_number(addr, na);
532 dev_node = of_get_parent(dev_node);
535 else if (node2dir(dev_node) == &root_dir)
539 pna = of_n_addr_cells(dev_node);
540 parent = of_get_parent(dev_node);
544 ranges = of_get_property(dev_node, "ranges", &rlen);
545 /* "ranges" property is missing. Translation breaks */
548 /* "ranges" property is empty. Do 1:1 translation */
552 tmp_addr = of_read_number(ranges + na, pna);
556 phys_addr += tmp_addr;
557 } while (node2dir(parent) != &root_dir);
563 of_device_is_compatible(const struct device_node *dev_node,
564 const char *compatible)
566 const struct dt_dir *d;
568 DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
572 d = node2dir(dev_node);
573 if (d->compatible && check_compatible(d->compatible, compatible))