1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <common.h>
24#include <libfdt.h>
25#include <dm/of_access.h>
26#include <linux/ctype.h>
27#include <linux/err.h>
28#include <linux/ioport.h>
29
30DECLARE_GLOBAL_DATA_PTR;
31
32
33LIST_HEAD(aliases_lookup);
34
35
36static struct device_node *of_aliases;
37
38
39static struct device_node *of_chosen;
40
41
42static struct device_node *of_stdout;
43
44
45static const char *of_stdout_options;
46
47
48
49
50
51
52
53
54
55
56
57
58
59struct alias_prop {
60 struct list_head link;
61 const char *alias;
62 struct device_node *np;
63 int id;
64 char stem[0];
65};
66
67int of_n_addr_cells(const struct device_node *np)
68{
69 const __be32 *ip;
70
71 do {
72 if (np->parent)
73 np = np->parent;
74 ip = of_get_property(np, "#address-cells", NULL);
75 if (ip)
76 return be32_to_cpup(ip);
77 } while (np->parent);
78
79
80 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
81}
82
83int of_n_size_cells(const struct device_node *np)
84{
85 const __be32 *ip;
86
87 do {
88 if (np->parent)
89 np = np->parent;
90 ip = of_get_property(np, "#size-cells", NULL);
91 if (ip)
92 return be32_to_cpup(ip);
93 } while (np->parent);
94
95
96 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
97}
98
99int of_simple_addr_cells(const struct device_node *np)
100{
101 const __be32 *ip;
102
103 ip = of_get_property(np, "#address-cells", NULL);
104 if (ip)
105 return be32_to_cpup(ip);
106
107
108 return 2;
109}
110
111int of_simple_size_cells(const struct device_node *np)
112{
113 const __be32 *ip;
114
115 ip = of_get_property(np, "#size-cells", NULL);
116 if (ip)
117 return be32_to_cpup(ip);
118
119
120 return 2;
121}
122
123struct property *of_find_property(const struct device_node *np,
124 const char *name, int *lenp)
125{
126 struct property *pp;
127
128 if (!np)
129 return NULL;
130
131 for (pp = np->properties; pp; pp = pp->next) {
132 if (strcmp(pp->name, name) == 0) {
133 if (lenp)
134 *lenp = pp->length;
135 break;
136 }
137 }
138 if (!pp && lenp)
139 *lenp = -FDT_ERR_NOTFOUND;
140
141 return pp;
142}
143
144struct device_node *of_find_all_nodes(struct device_node *prev)
145{
146 struct device_node *np;
147
148 if (!prev) {
149 np = gd->of_root;
150 } else if (prev->child) {
151 np = prev->child;
152 } else {
153
154
155
156
157 np = prev;
158 while (np->parent && !np->sibling)
159 np = np->parent;
160 np = np->sibling;
161 }
162
163 return np;
164}
165
166const void *of_get_property(const struct device_node *np, const char *name,
167 int *lenp)
168{
169 struct property *pp = of_find_property(np, name, lenp);
170
171 return pp ? pp->value : NULL;
172}
173
174static const char *of_prop_next_string(struct property *prop, const char *cur)
175{
176 const void *curv = cur;
177
178 if (!prop)
179 return NULL;
180
181 if (!cur)
182 return prop->value;
183
184 curv += strlen(cur) + 1;
185 if (curv >= prop->value + prop->length)
186 return NULL;
187
188 return curv;
189}
190
191int of_device_is_compatible(const struct device_node *device,
192 const char *compat, const char *type,
193 const char *name)
194{
195 struct property *prop;
196 const char *cp;
197 int index = 0, score = 0;
198
199
200 if (compat && compat[0]) {
201 prop = of_find_property(device, "compatible", NULL);
202 for (cp = of_prop_next_string(prop, NULL); cp;
203 cp = of_prop_next_string(prop, cp), index++) {
204 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
205 score = INT_MAX/2 - (index << 2);
206 break;
207 }
208 }
209 if (!score)
210 return 0;
211 }
212
213
214 if (type && type[0]) {
215 if (!device->type || of_node_cmp(type, device->type))
216 return 0;
217 score += 2;
218 }
219
220
221 if (name && name[0]) {
222 if (!device->name || of_node_cmp(name, device->name))
223 return 0;
224 score++;
225 }
226
227 return score;
228}
229
230bool of_device_is_available(const struct device_node *device)
231{
232 const char *status;
233 int statlen;
234
235 if (!device)
236 return false;
237
238 status = of_get_property(device, "status", &statlen);
239 if (status == NULL)
240 return true;
241
242 if (statlen > 0) {
243 if (!strcmp(status, "okay"))
244 return true;
245 }
246
247 return false;
248}
249
250struct device_node *of_get_parent(const struct device_node *node)
251{
252 const struct device_node *np;
253
254 if (!node)
255 return NULL;
256
257 np = of_node_get(node->parent);
258
259 return (struct device_node *)np;
260}
261
262static struct device_node *__of_get_next_child(const struct device_node *node,
263 struct device_node *prev)
264{
265 struct device_node *next;
266
267 if (!node)
268 return NULL;
269
270 next = prev ? prev->sibling : node->child;
271
272
273
274
275
276
277 for (; next; next = next->sibling)
278 if (of_node_get(next))
279 break;
280 of_node_put(prev);
281 return next;
282}
283
284#define __for_each_child_of_node(parent, child) \
285 for (child = __of_get_next_child(parent, NULL); child != NULL; \
286 child = __of_get_next_child(parent, child))
287
288static struct device_node *__of_find_node_by_path(struct device_node *parent,
289 const char *path)
290{
291 struct device_node *child;
292 int len;
293
294 len = strcspn(path, "/:");
295 if (!len)
296 return NULL;
297
298 __for_each_child_of_node(parent, child) {
299 const char *name = strrchr(child->full_name, '/');
300
301 name++;
302 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
303 return child;
304 }
305 return NULL;
306}
307
308#define for_each_property_of_node(dn, pp) \
309 for (pp = dn->properties; pp != NULL; pp = pp->next)
310
311struct device_node *of_find_node_opts_by_path(const char *path,
312 const char **opts)
313{
314 struct device_node *np = NULL;
315 struct property *pp;
316 const char *separator = strchr(path, ':');
317
318 if (opts)
319 *opts = separator ? separator + 1 : NULL;
320
321 if (strcmp(path, "/") == 0)
322 return of_node_get(gd->of_root);
323
324
325 if (*path != '/') {
326 int len;
327 const char *p = separator;
328
329 if (!p)
330 p = strchrnul(path, '/');
331 len = p - path;
332
333
334 if (!of_aliases)
335 return NULL;
336
337 for_each_property_of_node(of_aliases, pp) {
338 if (strlen(pp->name) == len && !strncmp(pp->name, path,
339 len)) {
340 np = of_find_node_by_path(pp->value);
341 break;
342 }
343 }
344 if (!np)
345 return NULL;
346 path = p;
347 }
348
349
350 if (!np)
351 np = of_node_get(gd->of_root);
352 while (np && *path == '/') {
353 struct device_node *tmp = np;
354
355 path++;
356 np = __of_find_node_by_path(np, path);
357 of_node_put(tmp);
358 path = strchrnul(path, '/');
359 if (separator && separator < path)
360 break;
361 }
362
363 return np;
364}
365
366struct device_node *of_find_compatible_node(struct device_node *from,
367 const char *type, const char *compatible)
368{
369 struct device_node *np;
370
371 for_each_of_allnodes_from(from, np)
372 if (of_device_is_compatible(np, compatible, type, NULL) &&
373 of_node_get(np))
374 break;
375 of_node_put(from);
376
377 return np;
378}
379
380struct device_node *of_find_node_by_phandle(phandle handle)
381{
382 struct device_node *np;
383
384 if (!handle)
385 return NULL;
386
387 for_each_of_allnodes(np)
388 if (np->phandle == handle)
389 break;
390 (void)of_node_get(np);
391
392 return np;
393}
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408static void *of_find_property_value_of_size(const struct device_node *np,
409 const char *propname, u32 len)
410{
411 struct property *prop = of_find_property(np, propname, NULL);
412
413 if (!prop)
414 return ERR_PTR(-EINVAL);
415 if (!prop->value)
416 return ERR_PTR(-ENODATA);
417 if (len > prop->length)
418 return ERR_PTR(-EOVERFLOW);
419
420 return prop->value;
421}
422
423int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
424{
425 const __be32 *val;
426
427 debug("%s: %s: ", __func__, propname);
428 if (!np)
429 return -EINVAL;
430 val = of_find_property_value_of_size(np, propname, sizeof(*outp));
431 if (IS_ERR(val)) {
432 debug("(not found)\n");
433 return PTR_ERR(val);
434 }
435
436 *outp = be32_to_cpup(val);
437 debug("%#x (%d)\n", *outp, *outp);
438
439 return 0;
440}
441
442int of_read_u32_array(const struct device_node *np, const char *propname,
443 u32 *out_values, size_t sz)
444{
445 const __be32 *val;
446
447 debug("%s: %s: ", __func__, propname);
448 val = of_find_property_value_of_size(np, propname,
449 sz * sizeof(*out_values));
450
451 if (IS_ERR(val))
452 return PTR_ERR(val);
453
454 debug("size %zd\n", sz);
455 while (sz--)
456 *out_values++ = be32_to_cpup(val++);
457
458 return 0;
459}
460
461int of_property_match_string(const struct device_node *np, const char *propname,
462 const char *string)
463{
464 const struct property *prop = of_find_property(np, propname, NULL);
465 size_t l;
466 int i;
467 const char *p, *end;
468
469 if (!prop)
470 return -EINVAL;
471 if (!prop->value)
472 return -ENODATA;
473
474 p = prop->value;
475 end = p + prop->length;
476
477 for (i = 0; p < end; i++, p += l) {
478 l = strnlen(p, end - p) + 1;
479 if (p + l > end)
480 return -EILSEQ;
481 debug("comparing %s with %s\n", string, p);
482 if (strcmp(string, p) == 0)
483 return i;
484 }
485 return -ENODATA;
486}
487
488
489
490
491
492
493
494
495
496
497
498
499int of_property_read_string_helper(const struct device_node *np,
500 const char *propname, const char **out_strs,
501 size_t sz, int skip)
502{
503 const struct property *prop = of_find_property(np, propname, NULL);
504 int l = 0, i = 0;
505 const char *p, *end;
506
507 if (!prop)
508 return -EINVAL;
509 if (!prop->value)
510 return -ENODATA;
511 p = prop->value;
512 end = p + prop->length;
513
514 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
515 l = strnlen(p, end - p) + 1;
516 if (p + l > end)
517 return -EILSEQ;
518 if (out_strs && i >= skip)
519 *out_strs++ = p;
520 }
521 i -= skip;
522 return i <= 0 ? -ENODATA : i;
523}
524
525static int __of_parse_phandle_with_args(const struct device_node *np,
526 const char *list_name,
527 const char *cells_name,
528 int cell_count, int index,
529 struct of_phandle_args *out_args)
530{
531 const __be32 *list, *list_end;
532 int rc = 0, cur_index = 0;
533 uint32_t count = 0;
534 struct device_node *node = NULL;
535 phandle phandle;
536 int size;
537
538
539 list = of_get_property(np, list_name, &size);
540 if (!list)
541 return -ENOENT;
542 list_end = list + size / sizeof(*list);
543
544
545 while (list < list_end) {
546 rc = -EINVAL;
547 count = 0;
548
549
550
551
552
553 phandle = be32_to_cpup(list++);
554 if (phandle) {
555
556
557
558
559
560
561
562
563
564 if (cells_name || cur_index == index) {
565 node = of_find_node_by_phandle(phandle);
566 if (!node) {
567 debug("%s: could not find phandle\n",
568 np->full_name);
569 goto err;
570 }
571 }
572
573 if (cells_name) {
574 if (of_read_u32(node, cells_name, &count)) {
575 debug("%s: could not get %s for %s\n",
576 np->full_name, cells_name,
577 node->full_name);
578 goto err;
579 }
580 } else {
581 count = cell_count;
582 }
583
584
585
586
587
588 if (list + count > list_end) {
589 debug("%s: arguments longer than property\n",
590 np->full_name);
591 goto err;
592 }
593 }
594
595
596
597
598
599
600
601 rc = -ENOENT;
602 if (cur_index == index) {
603 if (!phandle)
604 goto err;
605
606 if (out_args) {
607 int i;
608 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
609 count = OF_MAX_PHANDLE_ARGS;
610 out_args->np = node;
611 out_args->args_count = count;
612 for (i = 0; i < count; i++)
613 out_args->args[i] =
614 be32_to_cpup(list++);
615 } else {
616 of_node_put(node);
617 }
618
619
620 return 0;
621 }
622
623 of_node_put(node);
624 node = NULL;
625 list += count;
626 cur_index++;
627 }
628
629
630
631
632
633
634
635 rc = index < 0 ? cur_index : -ENOENT;
636 err:
637 if (node)
638 of_node_put(node);
639 return rc;
640}
641
642struct device_node *of_parse_phandle(const struct device_node *np,
643 const char *phandle_name, int index)
644{
645 struct of_phandle_args args;
646
647 if (index < 0)
648 return NULL;
649
650 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
651 &args))
652 return NULL;
653
654 return args.np;
655}
656
657int of_parse_phandle_with_args(const struct device_node *np,
658 const char *list_name, const char *cells_name,
659 int index, struct of_phandle_args *out_args)
660{
661 if (index < 0)
662 return -EINVAL;
663
664 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
665 index, out_args);
666}
667
668int of_count_phandle_with_args(const struct device_node *np,
669 const char *list_name, const char *cells_name)
670{
671 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
672 -1, NULL);
673}
674
675static void of_alias_add(struct alias_prop *ap, struct device_node *np,
676 int id, const char *stem, int stem_len)
677{
678 ap->np = np;
679 ap->id = id;
680 strncpy(ap->stem, stem, stem_len);
681 ap->stem[stem_len] = 0;
682 list_add_tail(&ap->link, &aliases_lookup);
683 debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
684 ap->alias, ap->stem, ap->id, of_node_full_name(np));
685}
686
687int of_alias_scan(void)
688{
689 struct property *pp;
690
691 of_aliases = of_find_node_by_path("/aliases");
692 of_chosen = of_find_node_by_path("/chosen");
693 if (of_chosen == NULL)
694 of_chosen = of_find_node_by_path("/chosen@0");
695
696 if (of_chosen) {
697 const char *name;
698
699 name = of_get_property(of_chosen, "stdout-path", NULL);
700 if (name)
701 of_stdout = of_find_node_opts_by_path(name,
702 &of_stdout_options);
703 }
704
705 if (!of_aliases)
706 return 0;
707
708 for_each_property_of_node(of_aliases, pp) {
709 const char *start = pp->name;
710 const char *end = start + strlen(start);
711 struct device_node *np;
712 struct alias_prop *ap;
713 ulong id;
714 int len;
715
716
717 if (!strcmp(pp->name, "name") ||
718 !strcmp(pp->name, "phandle") ||
719 !strcmp(pp->name, "linux,phandle"))
720 continue;
721
722 np = of_find_node_by_path(pp->value);
723 if (!np)
724 continue;
725
726
727
728
729
730 while (isdigit(*(end-1)) && end > start)
731 end--;
732 len = end - start;
733
734 if (strict_strtoul(end, 10, &id) < 0)
735 continue;
736
737
738 ap = malloc(sizeof(*ap) + len + 1);
739 if (!ap)
740 return -ENOMEM;
741 memset(ap, 0, sizeof(*ap) + len + 1);
742 ap->alias = start;
743 of_alias_add(ap, np, id, start, len);
744 }
745
746 return 0;
747}
748
749int of_alias_get_id(const struct device_node *np, const char *stem)
750{
751 struct alias_prop *app;
752 int id = -ENODEV;
753
754 mutex_lock(&of_mutex);
755 list_for_each_entry(app, &aliases_lookup, link) {
756 if (strcmp(app->stem, stem) != 0)
757 continue;
758
759 if (np == app->np) {
760 id = app->id;
761 break;
762 }
763 }
764 mutex_unlock(&of_mutex);
765
766 return id;
767}
768
769struct device_node *of_get_stdout(void)
770{
771 return of_stdout;
772}
773