Coverage Report

Created: 2026-12-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/drivers/of/base.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * base.c - basic devicetree functions
4
 *
5
 * Copyright (c) 2012 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6
 *
7
 * based on Linux devicetree support
8
 */
9
#include <common.h>
10
#include <deep-probe.h>
11
#include <of.h>
12
#include <of_address.h>
13
#include <errno.h>
14
#include <malloc.h>
15
#include <init.h>
16
#include <memory.h>
17
#include <bootsource.h>
18
#include <linux/sizes.h>
19
#include <of_graph.h>
20
#include <string.h>
21
#include <libfile.h>
22
#include <linux/clk.h>
23
#include <linux/ctype.h>
24
#include <linux/err.h>
25
#include <linux/string_helpers.h>
26
#include <pm_domain.h>
27
#include <tee/optee.h>
28
29
static struct device_node *root_node;
30
31
/**
32
 * of_node_has_prefix - Test if a node name has a given prefix
33
 * @np: The node name to test
34
 * @prefix: The prefix to see if @np starts with
35
 *
36
 * Returns:
37
 * * strlen(@prefix) if @np starts with @prefix
38
 * * 0 if @np does not start with @prefix
39
 */
40
size_t of_node_has_prefix(const struct device_node *np, const char *prefix)
41
2.80k
{
42
2.80k
  return np ? str_has_prefix(kbasename(np->full_name), prefix) : 0;
43
2.80k
}
44
EXPORT_SYMBOL(of_node_has_prefix);
45
46
bool of_node_name_eq(const struct device_node *np, const char *name)
47
1.02k
{
48
1.02k
  const char *node_name;
49
1.02k
  size_t len;
50
51
1.02k
  if (!np)
52
0
    return false;
53
54
1.02k
  node_name = kbasename(np->full_name);
55
1.02k
  len = strchrnul(node_name, '@') - node_name;
56
57
1.02k
  return (strlen(name) == len) && (strncmp(node_name, name, len) == 0);
58
1.02k
}
59
EXPORT_SYMBOL(of_node_name_eq);
60
61
/*
62
 * Iterate over all nodes of a tree. As a devicetree does not
63
 * have a dedicated list head, the start node (usually the root
64
 * node) will not be iterated over.
65
 */
66
static inline struct device_node *of_next_node(struct device_node *node)
67
24.6k
{
68
24.6k
  struct device_node *next;
69
70
24.6k
  if (!node)
71
96
    return root_node;
72
73
24.5k
  next = list_first_entry(&node->list, struct device_node, list);
74
75
24.5k
  return next->parent ? next : NULL;
76
24.6k
}
77
78
#define of_tree_for_each_node_from(node, from) \
79
24.6k
  for (node = of_next_node(from); node; node = of_next_node(node))
80
81
/**
82
 * struct alias_prop - Alias property in 'aliases' node
83
 * @link: List node to link the structure in aliases_lookup list
84
 * @alias:  Alias property name
85
 * @np:   Pointer to device_node that the alias stands for
86
 * @id:   Index value from end of alias name
87
 * @stem: Alias string without the index
88
 *
89
 * The structure represents one alias property of 'aliases' node as
90
 * an entry in aliases_lookup list.
91
 */
92
struct alias_prop {
93
  struct list_head link;
94
  const char *alias;
95
  struct device_node *np;
96
  int id;
97
  char stem[0];
98
};
99
100
static LIST_HEAD(aliases_lookup);
101
102
static struct device_node *of_aliases;
103
104
0
#define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
105
0
#define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
106
107
int of_bus_n_addr_cells(struct device_node *np)
108
1.44k
{
109
1.44k
  u32 cells;
110
111
1.56k
  for (; np; np = np->parent)
112
1.56k
    if (!of_property_read_u32(np, "#address-cells", &cells))
113
1.44k
      return cells;
114
115
  /* No #address-cells property for the root node */
116
0
  return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
117
1.44k
}
118
119
int of_n_addr_cells(struct device_node *np)
120
1.44k
{
121
1.44k
  if (np->parent)
122
1.44k
    np = np->parent;
123
124
1.44k
  return of_bus_n_addr_cells(np);
125
1.44k
}
126
EXPORT_SYMBOL(of_n_addr_cells);
127
128
int of_bus_n_size_cells(struct device_node *np)
129
1.37k
{
130
1.37k
  u32 cells;
131
132
1.50k
  for (; np; np = np->parent)
133
1.50k
    if (!of_property_read_u32(np, "#size-cells", &cells))
134
1.37k
      return cells;
135
136
  /* No #size-cells property for the root node */
137
0
  return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
138
1.37k
}
139
140
int of_n_size_cells(struct device_node *np)
141
1.37k
{
142
1.37k
  if (np->parent)
143
1.37k
    np = np->parent;
144
145
1.37k
  return of_bus_n_size_cells(np);
146
1.37k
}
147
EXPORT_SYMBOL(of_n_size_cells);
148
149
struct property *of_find_property(const struct device_node *np,
150
          const char *name, int *lenp)
151
272k
{
152
272k
  struct property *pp;
153
154
272k
  if (!np)
155
9.09k
    return NULL;
156
157
263k
  list_for_each_entry(pp, &np->properties, list)
158
1.82M
    if (of_prop_cmp(pp->name, name) == 0) {
159
131k
      if (lenp)
160
17.4k
        *lenp = pp->length;
161
131k
      return pp;
162
131k
    }
163
164
132k
  return NULL;
165
263k
}
166
EXPORT_SYMBOL(of_find_property);
167
168
static void of_alias_add(struct alias_prop *ap, struct device_node *np,
169
       int id, const char *stem, int stem_len)
170
64
{
171
64
  ap->np = np;
172
64
  ap->id = id;
173
64
  strncpy(ap->stem, stem, stem_len);
174
64
  ap->stem[stem_len] = 0;
175
64
  list_add_tail(&ap->link, &aliases_lookup);
176
64
  pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
177
64
     ap->alias, ap->stem, ap->id, np);
178
64
}
179
180
static struct device_node *of_alias_resolve(struct device_node *root, struct property *pp)
181
64
{
182
64
  const char *path = of_property_get_value(pp);
183
184
  /* Skip those we do not want to proceed */
185
64
  if (!of_prop_cmp(pp->name, "name") ||
186
64
      !of_prop_cmp(pp->name, "phandle") ||
187
64
      !of_prop_cmp(pp->name, "linux,phandle"))
188
0
    return NULL;
189
190
64
  if (!path || !string_is_terminated(path, pp->length))
191
0
    return NULL;
192
193
64
  return of_find_node_by_path_from(root, path);
194
64
}
195
196
static int of_alias_id_parse(const char *start, int *len)
197
64
{
198
64
  const char *end = start + strlen(start);
199
200
  /* walk the alias backwards to extract the id and work out
201
   * the 'stem' string */
202
64
  while (isdigit(*(end-1)) && end > start)
203
0
    end--;
204
205
64
  *len = end - start;
206
207
64
  return simple_strtol(end, NULL, 10);
208
64
}
209
210
/**
211
 * of_alias_scan - Scan all properties of 'aliases' node
212
 *
213
 * The function scans all the properties of 'aliases' node and populates
214
 * the global lookup table with the properties.
215
 */
216
void of_alias_scan(void)
217
32
{
218
32
  struct property *pp;
219
32
  struct alias_prop *app, *tmp;
220
221
32
  list_for_each_entry_safe(app, tmp, &aliases_lookup, link)
222
0
    free(app);
223
224
32
  INIT_LIST_HEAD(&aliases_lookup);
225
226
32
  if (!root_node)
227
0
    return;
228
229
32
  of_aliases = of_find_node_by_path("/aliases");
230
32
  if (!of_aliases)
231
0
    return;
232
233
64
  list_for_each_entry(pp, &of_aliases->properties, list) {
234
64
    const char *start = pp->name;
235
64
    struct device_node *np;
236
64
    struct alias_prop *ap;
237
64
    int id, len;
238
239
64
    np = of_alias_resolve(root_node, pp);
240
64
    if (!np)
241
0
      continue;
242
243
64
    id = of_alias_id_parse(start, &len);
244
64
    if (id < 0)
245
0
      continue;
246
247
    /* Allocate an alias_prop with enough space for the stem */
248
64
    ap = xzalloc(sizeof(*ap) + len + 1);
249
64
    ap->alias = start;
250
64
    of_alias_add(ap, np, id, start, len);
251
64
  }
252
32
}
253
EXPORT_SYMBOL(of_alias_scan);
254
255
/**
256
 * of_alias_get_id - Get alias id for the given device_node
257
 * @np:   Pointer to the given device_node
258
 * @stem: Alias stem of the given device_node
259
 *
260
 * The function travels the lookup table to get alias id for the given
261
 * device_node and alias stem.  It returns the alias id if find it.
262
 */
263
int of_alias_get_id(struct device_node *np, const char *stem)
264
0
{
265
0
  struct alias_prop *app;
266
0
  int id = -ENODEV;
267
268
0
  list_for_each_entry(app, &aliases_lookup, link) {
269
0
    if (of_node_cmp(app->stem, stem) != 0)
270
0
      continue;
271
272
0
    if (np == app->np) {
273
0
      id = app->id;
274
0
      break;
275
0
    }
276
0
  }
277
278
0
  return id;
279
0
}
280
EXPORT_SYMBOL_GPL(of_alias_get_id);
281
282
/**
283
 * of_alias_get_free_id - Get lowest free alias id for the given stem
284
 * @stem: Alias stem to be examined
285
 *
286
 * The function travels the lookup table to get the highest alias id for the
287
 * given alias stem. It returns the lowest free alias id.
288
 */
289
int of_alias_get_free_id(const char *stem)
290
0
{
291
0
  struct alias_prop *app;
292
0
  int id = -1;
293
294
0
  list_for_each_entry(app, &aliases_lookup, link) {
295
0
    if (strcmp(app->stem, stem) != 0)
296
0
      continue;
297
298
0
    if (app->id > id)
299
0
      id = app->id;
300
0
  }
301
302
0
  return id + 1;
303
0
}
304
EXPORT_SYMBOL_GPL(of_alias_get_free_id);
305
306
int of_alias_get_id_from(struct device_node *root, struct device_node *np,
307
       const char *stem)
308
0
{
309
0
  struct device_node *aliasnp, *entrynp;
310
0
  struct property *pp;
311
312
0
  if (!root)
313
0
    return of_alias_get_id(np, stem);
314
315
0
  aliasnp = of_find_node_by_path_from(root, "/aliases");
316
0
  if (!aliasnp)
317
0
    return -ENODEV;
318
319
0
  for_each_property_of_node(aliasnp, pp) {
320
0
    const char *start = pp->name;
321
0
    int id, len;
322
323
0
    entrynp = of_alias_resolve(root_node, pp);
324
0
    if (entrynp != np)
325
0
      continue;
326
327
0
    id = of_alias_id_parse(start, &len);
328
0
    if (id < 0)
329
0
      continue;
330
331
0
    if (strncmp(start, stem, len))
332
0
      continue;
333
334
0
    return id;
335
0
  }
336
337
0
  return -ENODEV;
338
0
}
339
EXPORT_SYMBOL_GPL(of_alias_get_id_from);
340
341
const char *of_alias_get(struct device_node *np)
342
128
{
343
128
  struct alias_prop *app;
344
345
224
  list_for_each_entry(app, &aliases_lookup, link) {
346
224
    if (np == app->np)
347
64
      return app->alias;
348
224
  }
349
350
64
  return NULL;
351
128
}
352
EXPORT_SYMBOL_GPL(of_alias_get);
353
354
static const char *of_get_partition_device_alias(struct device_node *np)
355
0
{
356
0
  const char *alias;
357
358
0
  alias = of_alias_get(np);
359
0
  if (alias)
360
0
    return alias;
361
362
0
  np = of_get_parent(np);
363
0
  if (np && of_node_is_fixed_partitions(np))
364
0
    np = of_get_parent(np);
365
366
0
  return of_alias_get(np);
367
0
}
368
369
const char *of_property_get_alias_from(struct device_node *root,
370
               const char *np_name, const char *propname,
371
               int index)
372
0
{
373
0
  struct device_node *node, *rnode;
374
0
  const char *path;
375
0
  int ret;
376
377
0
  node = of_find_node_by_path_or_alias(root, np_name);
378
0
  if (!node)
379
0
    return NULL;
380
381
0
  ret = of_property_read_string_index(node, propname, index, &path);
382
0
  if (ret < 0)
383
0
    return NULL;
384
385
0
  rnode = of_find_node_by_path(path);
386
0
  if (!rnode)
387
0
    return NULL;
388
389
0
  return of_get_partition_device_alias(rnode);
390
0
}
391
EXPORT_SYMBOL_GPL(of_property_get_alias_from);
392
393
const char *of_parse_phandle_and_get_alias_from(struct device_node *root,
394
            const char *np_name, const char *phandle_name,
395
            int index)
396
0
{
397
0
  struct device_node *node, *rnode;
398
399
0
  node = of_find_node_by_path_or_alias(root, np_name);
400
0
  if (!node)
401
0
    return NULL;
402
403
0
  rnode = of_parse_phandle_from(node, root, phandle_name, index);
404
0
  if (!rnode)
405
0
    return NULL;
406
407
0
  return of_get_partition_device_alias(rnode);
408
0
}
409
EXPORT_SYMBOL_GPL(of_parse_phandle_and_get_alias_from);
410
411
/*
412
 * of_find_node_by_alias - Find a node given an alias name
413
 * @root:    the root node of the tree. If NULL, use internal tree
414
 * @alias:   the alias name to find
415
 */
416
struct device_node *of_find_node_by_alias(struct device_node *root, const char *alias)
417
0
{
418
0
  struct device_node *aliasnp;
419
0
  int ret;
420
0
  const char *path;
421
422
0
  if (!root)
423
0
    root = root_node;
424
425
0
  aliasnp = of_find_node_by_path_from(root, "/aliases");
426
0
  if (!aliasnp)
427
0
    return NULL;
428
429
0
  ret = of_property_read_string(aliasnp, alias, &path);
430
0
  if (ret)
431
0
    return NULL;
432
433
0
  return of_find_node_by_path_from(root, path);
434
0
}
435
EXPORT_SYMBOL_GPL(of_find_node_by_alias);
436
437
/*
438
 * of_find_node_by_phandle_from - Find a node given a phandle from given
439
 * root node.
440
 * @handle:  phandle of the node to find
441
 * @root:    root node of the tree to search in. If NULL use the
442
 *           internal tree.
443
 */
444
struct device_node *of_find_node_by_phandle_from(phandle phandle,
445
    struct device_node *root)
446
192
{
447
192
  struct device_node *node;
448
449
192
  of_tree_for_each_node_from(node, root)
450
2.04k
    if (node->phandle == phandle)
451
192
      return node;
452
453
0
  return NULL;
454
192
}
455
EXPORT_SYMBOL(of_find_node_by_phandle_from);
456
457
/*
458
 * of_find_node_by_phandle - Find a node given a phandle
459
 * @handle:    phandle of the node to find
460
 */
461
struct device_node *of_find_node_by_phandle(phandle phandle)
462
160
{
463
160
  return of_find_node_by_phandle_from(phandle, root_node);
464
160
}
465
EXPORT_SYMBOL(of_find_node_by_phandle);
466
467
/*
468
 * of_get_tree_max_phandle - Find the maximum phandle of a tree
469
 * @root:    root node of the tree to search in. If NULL use the
470
 *           internal tree.
471
 */
472
phandle of_get_tree_max_phandle(struct device_node *root)
473
339
{
474
339
  struct device_node *n;
475
339
  phandle max = 0;
476
477
339
  of_tree_for_each_node_from(n, root) {
478
0
    if (n->phandle > max)
479
0
      max = n->phandle;
480
0
  }
481
482
339
  return max;
483
339
}
484
EXPORT_SYMBOL(of_get_tree_max_phandle);
485
486
/*
487
 * of_node_create_phandle - create a phandle for a node
488
 * @node:    The node to create a phandle in
489
 *
490
 * returns the new phandle or the existing phandle if the node
491
 * already has a phandle.
492
 */
493
phandle of_node_create_phandle(struct device_node *node)
494
0
{
495
0
  phandle p;
496
0
  struct device_node *root;
497
498
0
  if (node->phandle)
499
0
    return node->phandle;
500
501
0
  root = of_find_root_node(node);
502
503
0
  p = of_get_tree_max_phandle(root) + 1;
504
505
0
  node->phandle = p;
506
507
0
  p = cpu_to_be32(p);
508
509
0
  of_set_property(node, "phandle", &p, sizeof(p), 1);
510
511
0
  return node->phandle;
512
0
}
513
EXPORT_SYMBOL(of_node_create_phandle);
514
515
int of_set_property_to_child_phandle(struct device_node *node, char *prop_name)
516
0
{
517
0
  int ret;
518
0
  phandle p;
519
520
  /* Check if property exist */
521
0
  if (!of_get_property(of_get_parent(node), prop_name, NULL))
522
0
    return -EINVAL;
523
524
  /* Create or get existing phandle of child node */
525
0
  p = of_node_create_phandle(node);
526
0
  p = cpu_to_be32(p);
527
528
0
  node = of_get_parent(node);
529
530
0
  ret = of_set_property(node, prop_name, &p, sizeof(p), 0);
531
532
0
  return ret;
533
0
}
534
EXPORT_SYMBOL(of_set_property_to_child_phandle);
535
536
/*
537
 * Find a property with a given name for a given node
538
 * and return the value.
539
 */
540
const void *of_get_property(const struct device_node *np, const char *name,
541
       int *lenp)
542
46.4k
{
543
46.4k
  struct property *pp = of_find_property(np, name, lenp);
544
545
46.4k
  if (!pp)
546
28.3k
    return NULL;
547
548
18.1k
  return of_property_get_value(pp);
549
46.4k
}
550
EXPORT_SYMBOL(of_get_property);
551
552
/*
553
 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
554
 *
555
 * @cpu: logical cpu index of a core/thread
556
 * @phys_id: physical identifier of a core/thread
557
 *
558
 * Returns true if the physical identifier and the logical cpu index
559
 * correspond to the same core/thread, false otherwise.
560
 */
561
static bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
562
0
{
563
0
  return (u32)phys_id == cpu;
564
0
}
565
566
/**
567
 * Checks if the given "prop_name" property holds the physical id of the
568
 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
569
 * NULL, local thread number within the core is returned in it.
570
 */
571
static bool __of_find_n_match_cpu_property(struct device_node *cpun,
572
      const char *prop_name, int cpu, unsigned int *thread)
573
0
{
574
0
  const __be32 *cell;
575
0
  int ac, prop_len, tid;
576
0
  u64 hwid;
577
578
0
  ac = of_n_addr_cells(cpun);
579
0
  cell = of_get_property(cpun, prop_name, &prop_len);
580
0
  if (!cell || !ac)
581
0
    return false;
582
0
  prop_len /= sizeof(*cell) * ac;
583
0
  for (tid = 0; tid < prop_len; tid++) {
584
0
    hwid = of_read_number(cell, ac);
585
0
    if (arch_match_cpu_phys_id(cpu, hwid)) {
586
0
      if (thread)
587
0
        *thread = tid;
588
0
      return true;
589
0
    }
590
0
    cell += ac;
591
0
  }
592
0
  return false;
593
0
}
594
595
/*
596
 * arch_find_n_match_cpu_physical_id - See if the given device node is
597
 * for the cpu corresponding to logical cpu 'cpu'.  Return true if so,
598
 * else false.  If 'thread' is non-NULL, the local thread number within the
599
 * core is returned in it.
600
 */
601
static bool arch_find_n_match_cpu_physical_id(struct device_node *cpun,
602
                int cpu, unsigned int *thread)
603
0
{
604
0
  return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
605
0
}
606
607
/**
608
 * of_get_cpu_node - Get device node associated with the given logical CPU
609
 *
610
 * @cpu: CPU number(logical index) for which device node is required
611
 * @thread: if not NULL, local thread number within the physical core is
612
 *          returned
613
 *
614
 * The main purpose of this function is to retrieve the device node for the
615
 * given logical CPU index. It should be used to initialize the of_node in
616
 * cpu device. Once of_node in cpu device is populated, all the further
617
 * references can use that instead.
618
 *
619
 * CPU logical to physical index mapping is architecture specific and is built
620
 * before booting secondary cores. This function uses arch_match_cpu_phys_id
621
 * which can be overridden by architecture specific implementation.
622
 *
623
 * Returns a node pointer for the logical cpu with refcount incremented, use
624
 * of_node_put() on it when done. Returns NULL if not found.
625
 */
626
struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
627
0
{
628
0
  struct device_node *cpun;
629
630
0
  for_each_node_by_type(cpun, "cpu") {
631
0
    if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
632
0
      return cpun;
633
0
  }
634
0
  return NULL;
635
0
}
636
EXPORT_SYMBOL(of_get_cpu_node);
637
638
/** Checks if the given "compat" string matches one of the strings in
639
 * the device's "compatible" property. Returns 0 on mismatch and a
640
 * positive score on match with the maximum being OF_DEVICE_COMPATIBLE_MAX_SCORE,
641
 * which is only returned if the first compatible matched.
642
 */
643
int of_device_is_compatible(const struct device_node *device,
644
    const char *compat)
645
17.6k
{
646
17.6k
  struct property *prop;
647
17.6k
  const char *cp;
648
17.6k
  int index = 0, score = 0;
649
650
17.6k
  prop = of_find_property(device, "compatible", NULL);
651
31.3k
  for (cp = of_prop_next_string(prop, NULL); cp;
652
17.6k
       cp = of_prop_next_string(prop, cp), index++) {
653
14.5k
    if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
654
859
      score = OF_DEVICE_COMPATIBLE_MAX_SCORE - (index << 2);
655
859
      break;
656
859
    }
657
14.5k
  }
658
659
17.6k
  return score;
660
17.6k
}
661
EXPORT_SYMBOL(of_device_is_compatible);
662
663
/**
664
 *  of_find_node_by_name_address - Find a node by its full name
665
 *  @from:  The node to start searching from or NULL, the node
666
 *    you pass will not be searched, only the next one
667
 *    will; typically, you pass what the previous call
668
 *    returned.
669
 *  @name:  The name string to match against
670
 *
671
 *  Returns a pointer to the node found or NULL.
672
 */
673
struct device_node *of_find_node_by_name_address(struct device_node *from,
674
  const char *name)
675
806
{
676
806
  struct device_node *np;
677
678
806
  of_tree_for_each_node_from(np, from)
679
16.8k
    if (np->name && !of_node_cmp(np->name, name))
680
343
      return np;
681
682
463
  return NULL;
683
806
}
684
EXPORT_SYMBOL(of_find_node_by_name_address);
685
686
/**
687
 *  of_find_node_by_name - Find a node by its "name" property
688
 *  @from:  The node to start searching from or NULL, the node
689
 *    you pass will not be searched, only the next one
690
 *    will; typically, you pass what the previous call
691
 *    returned.
692
 *  @name:  The name string to match against
693
 *
694
 *  Returns a pointer to the node found or NULL.
695
 */
696
struct device_node *of_find_node_by_name(struct device_node *from,
697
  const char *name)
698
32
{
699
32
  struct device_node *np;
700
701
32
  of_tree_for_each_node_from(np, from)
702
928
    if (np->name && of_node_name_eq(np, name))
703
0
      return np;
704
705
32
  return NULL;
706
32
}
707
EXPORT_SYMBOL(of_find_node_by_name);
708
709
/**
710
 *  of_find_node_by_type - Find a node by its "device_type" property
711
 *  @from:  The node to start searching from, or NULL to start searching
712
 *    the entire device tree. The node you pass will not be
713
 *    searched, only the next one will; typically, you pass
714
 *    what the previous call returned.
715
 *  @type:  The type string to match against.
716
 *
717
 *  Returns a pointer to the node found or NULL.
718
 */
719
struct device_node *of_find_node_by_type(struct device_node *from,
720
    const char *type)
721
64
{
722
64
  struct device_node *np;
723
724
896
  of_tree_for_each_node_from(np, from) {
725
896
    if (of_node_is_type(np, type))
726
32
      return np;
727
896
  }
728
32
  return NULL;
729
64
}
730
EXPORT_SYMBOL(of_find_node_by_type);
731
732
/**
733
 *  of_find_compatible_node - Find a node based on type and one of the
734
 *                                tokens in its "compatible" property
735
 *  @from:    The node to start searching from or NULL, the node
736
 *      you pass will not be searched, only the next one
737
 *      will; typically, you pass what the previous call
738
 *      returned.
739
 *  @type:    The type string to match "device_type" or NULL to ignore
740
 *                      (currently always ignored in barebox)
741
 *  @compatible:  The string to match to one of the tokens in the device
742
 *      "compatible" list.
743
 *
744
 *  Returns a pointer to the node found or NULL.
745
 */
746
struct device_node *of_find_compatible_node(struct device_node *from,
747
  const char *type, const char *compatible)
748
160
{
749
160
  struct device_node *np;
750
751
160
  of_tree_for_each_node_from(np, from)
752
2.01k
    if (of_device_is_compatible(np, compatible))
753
96
      return np;
754
755
64
  return NULL;
756
160
}
757
EXPORT_SYMBOL(of_find_compatible_node);
758
759
/**
760
 *  of_find_node_with_property - Find a node which has a property with
761
 *                                   the given name.
762
 *  @from:    The node to start searching from or NULL, the node
763
 *      you pass will not be searched, only the next one
764
 *      will; typically, you pass what the previous call
765
 *      returned.
766
 *  @prop_name: The name of the property to look for.
767
 *
768
 *  Returns a pointer to the node found or NULL.
769
 */
770
struct device_node *of_find_node_with_property(struct device_node *from,
771
  const char *prop_name)
772
0
{
773
0
  struct device_node *np;
774
775
0
  of_tree_for_each_node_from(np, from) {
776
0
    struct property *pp = of_find_property(np, prop_name, NULL);
777
0
    if (pp)
778
0
      return np;
779
0
  }
780
781
0
  return NULL;
782
0
}
783
EXPORT_SYMBOL(of_find_node_with_property);
784
785
/**
786
 * of_match_node - Tell if a device_node has a matching of_device_id structure
787
 *      @matches:       array of of device match structures to search in
788
 *      @node:          the of device structure to match against
789
 *
790
 *      Low level utility function used by device matching.
791
 *
792
 *      Return: pointer to the best matching of_device_id structure, or NULL
793
 */
794
const struct of_device_id *of_match_node(const struct of_device_id *matches,
795
           const struct device_node *node)
796
7.32k
{
797
7.32k
  const struct of_device_id *best_match = NULL;
798
7.32k
  int score, best_score = 0;
799
800
7.32k
  if (!matches || !node)
801
64
    return NULL;
802
803
17.4k
  for (; matches->compatible; matches++) {
804
10.5k
    score = of_device_is_compatible(node, matches->compatible);
805
806
10.5k
    if (score > best_score) {
807
352
      best_match = matches;
808
352
      best_score = score;
809
810
352
      if (score == OF_DEVICE_COMPATIBLE_MAX_SCORE)
811
352
        break;
812
352
    }
813
10.5k
  }
814
815
7.26k
  return best_match;
816
7.32k
}
817
818
/**
819
 *  of_find_matching_node_and_match - Find a node based on an of_device_id
820
 *            match table.
821
 *  @from:    The node to start searching from or NULL, the node
822
 *      you pass will not be searched, only the next one
823
 *      will; typically, you pass what the previous call
824
 *      returned.
825
 *  @matches: array of of device match structures to search in
826
 *  @match    Updated to point at the matches entry which matched
827
 *
828
 *  Returns a pointer to the node found or NULL.
829
 */
830
struct device_node *of_find_matching_node_and_match(struct device_node *from,
831
          const struct of_device_id *matches,
832
          const struct of_device_id **match)
833
32
{
834
32
  struct device_node *np;
835
836
32
  if (match)
837
0
    *match = NULL;
838
839
928
  of_tree_for_each_node_from(np, from) {
840
928
    const struct of_device_id *m = of_match_node(matches, np);
841
928
    if (m) {
842
0
      if (match)
843
0
        *match = m;
844
0
      return np;
845
0
    }
846
928
  }
847
848
32
  return NULL;
849
32
}
850
EXPORT_SYMBOL(of_find_matching_node_and_match);
851
852
bool of_match(struct device *dev, const struct driver *drv)
853
6.01k
{
854
6.01k
  const struct of_device_id *id;
855
856
6.01k
  id = of_match_node(drv->of_compatible, dev->of_node);
857
6.01k
  if (!id)
858
5.69k
    return false;
859
860
320
  dev->of_id_entry = id;
861
862
320
  return true;
863
6.01k
}
864
EXPORT_SYMBOL(of_match);
865
/**
866
 * of_find_property_value_of_size
867
 *
868
 * @np:   device node from which the property value is to be read.
869
 * @propname: name of the property to be searched.
870
 * @min:  minimum allowed length of property value
871
 * @max:  maximum allowed length of property value (0 means unlimited)
872
 * @len:  if !=NULL, actual length is written to here
873
 *
874
 * Search for a property in a device node and valid the requested size.
875
 *
876
 * Return: The property value on success, -EINVAL if the property does not
877
 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
878
 * property data is too small or too large.
879
 *
880
 */
881
static const void *of_find_property_value_of_size(const struct device_node *np,
882
      const char *propname, u32 min, u32 max, size_t *len)
883
4.99k
{
884
4.99k
  struct property *prop = of_find_property(np, propname, NULL);
885
4.99k
  const void *value;
886
887
4.99k
  if (!prop)
888
565
    return ERR_PTR(-EINVAL);
889
4.42k
  value = of_property_get_value(prop);
890
4.42k
  if (!value)
891
0
    return ERR_PTR(-ENODATA);
892
4.42k
  if (prop->length < min)
893
5
    return ERR_PTR(-EOVERFLOW);
894
4.42k
  if (max && prop->length > max)
895
0
    return ERR_PTR(-EOVERFLOW);
896
897
4.42k
  if (len)
898
64
    *len = prop->length;
899
900
4.42k
  return value;
901
4.42k
}
902
903
/**
904
 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
905
 *
906
 * @np:   device node from which the property value is to be read.
907
 * @propname: name of the property to be searched.
908
 * @index:  index of the u32 in the list of values
909
 * @out_value:  pointer to return value, modified only if no error.
910
 *
911
 * Search for a property in a device node and read nth 32-bit value from
912
 * it. Returns 0 on success, -EINVAL if the property does not exist,
913
 * -ENODATA if property does not have a value, and -EOVERFLOW if the
914
 * property data isn't large enough.
915
 *
916
 * The out_value is modified only if a valid u32 value can be decoded.
917
 */
918
int of_property_read_u32_index(const struct device_node *np,
919
               const char *propname,
920
               u32 index, u32 *out_value)
921
1.15k
{
922
1.15k
  const u32 *val = of_find_property_value_of_size(np, propname,
923
1.15k
          ((index + 1) * sizeof(*out_value)),
924
1.15k
              0, NULL);
925
926
1.15k
  if (IS_ERR(val))
927
26
    return PTR_ERR(val);
928
929
1.12k
  *out_value = be32_to_cpup(((__be32 *)val) + index);
930
1.12k
  return 0;
931
1.15k
}
932
EXPORT_SYMBOL_GPL(of_property_read_u32_index);
933
934
/**
935
 * of_property_count_elems_of_size - Count the number of elements in a property
936
 *
937
 * @np:   device node from which the property value is to be read.
938
 * @propname: name of the property to be searched.
939
 * @elem_size:  size of the individual element
940
 *
941
 * Search for a property in a device node and count the number of elements of
942
 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
943
 * property does not exist or its length does not match a multiple of elem_size
944
 * and -ENODATA if the property does not have a value.
945
 */
946
int of_property_count_elems_of_size(const struct device_node *np,
947
        const char *propname, int elem_size)
948
0
{
949
0
  struct property *prop = of_find_property(np, propname, NULL);
950
951
0
  if (!prop)
952
0
    return -EINVAL;
953
0
  if (!of_property_get_value(prop))
954
0
    return -ENODATA;
955
956
0
  if (prop->length % elem_size != 0) {
957
0
    pr_err("size of %s in node %pOF is not a multiple of %d\n",
958
0
           propname, np, elem_size);
959
0
    return -EINVAL;
960
0
  }
961
962
0
  return prop->length / elem_size;
963
0
}
964
EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
965
966
/**
967
 * of_property_read_u8_array - Find and read an array of u8 from a property.
968
 *
969
 * @np:   device node from which the property value is to be read.
970
 * @propname: name of the property to be searched.
971
 * @out_value:  pointer to return value, modified only if return value is 0.
972
 * @sz:   number of array elements to read
973
 *
974
 * Search for a property in a device node and read 8-bit value(s) from
975
 * it. Returns 0 on success, -EINVAL if the property does not exist,
976
 * -ENODATA if property does not have a value, and -EOVERFLOW if the
977
 * property data isn't large enough.
978
 *
979
 * dts entry of array should be like:
980
 *  property = /bits/ 8 <0x50 0x60 0x70>;
981
 *
982
 * The out_value is modified only if a valid u8 value can be decoded.
983
 */
984
int of_property_read_u8_array(const struct device_node *np,
985
      const char *propname, u8 *out_values, size_t sz)
986
0
{
987
0
  const u8 *val = of_find_property_value_of_size(np, propname,
988
0
            (sz * sizeof(*out_values)),
989
0
                   0, NULL);
990
991
0
  if (IS_ERR(val))
992
0
    return PTR_ERR(val);
993
994
0
  while (sz--)
995
0
    *out_values++ = *val++;
996
0
  return 0;
997
0
}
998
EXPORT_SYMBOL_GPL(of_property_read_u8_array);
999
1000
/**
1001
 * of_property_read_u16_array - Find and read an array of u16 from a property.
1002
 *
1003
 * @np:   device node from which the property value is to be read.
1004
 * @propname: name of the property to be searched.
1005
 * @out_value:  pointer to return value, modified only if return value is 0.
1006
 * @sz:   number of array elements to read
1007
 *
1008
 * Search for a property in a device node and read 16-bit value(s) from
1009
 * it. Returns 0 on success, -EINVAL if the property does not exist,
1010
 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1011
 * property data isn't large enough.
1012
 *
1013
 * dts entry of array should be like:
1014
 *  property = /bits/ 16 <0x5000 0x6000 0x7000>;
1015
 *
1016
 * The out_value is modified only if a valid u16 value can be decoded.
1017
 */
1018
int of_property_read_u16_array(const struct device_node *np,
1019
      const char *propname, u16 *out_values, size_t sz)
1020
0
{
1021
0
  const __be16 *val = of_find_property_value_of_size(np, propname,
1022
0
            (sz * sizeof(*out_values)),
1023
0
                 0, NULL);
1024
1025
0
  if (IS_ERR(val))
1026
0
    return PTR_ERR(val);
1027
1028
0
  while (sz--)
1029
0
    *out_values++ = be16_to_cpup(val++);
1030
0
  return 0;
1031
0
}
1032
EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1033
1034
/**
1035
 * of_property_read_u32_array - Find and read an array of 32 bit integers
1036
 * from a property.
1037
 *
1038
 * @np:   device node from which the property value is to be read.
1039
 * @propname: name of the property to be searched.
1040
 * @out_value:  pointer to return value, modified only if return value is 0.
1041
 * @sz:   number of array elements to read
1042
 *
1043
 * Search for a property in a device node and read 32-bit value(s) from
1044
 * it. Returns 0 on success, -EINVAL if the property does not exist,
1045
 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1046
 * property data isn't large enough.
1047
 *
1048
 * The out_value is modified only if a valid u32 value can be decoded.
1049
 */
1050
int of_property_read_u32_array(const struct device_node *np,
1051
             const char *propname, u32 *out_values,
1052
             size_t sz)
1053
3.77k
{
1054
3.77k
  const __be32 *val = of_find_property_value_of_size(np, propname,
1055
3.77k
            (sz * sizeof(*out_values)),
1056
3.77k
                 0, NULL);
1057
1058
3.77k
  if (IS_ERR(val))
1059
544
    return PTR_ERR(val);
1060
1061
6.52k
  while (sz--)
1062
3.29k
    *out_values++ = be32_to_cpup(val++);
1063
3.23k
  return 0;
1064
3.77k
}
1065
EXPORT_SYMBOL_GPL(of_property_read_u32_array);
1066
1067
/**
1068
 * of_property_read_u64 - Find and read a 64 bit integer from a property
1069
 * @np:   device node from which the property value is to be read.
1070
 * @propname: name of the property to be searched.
1071
 * @out_value:  pointer to return value, modified only if return value is 0.
1072
 *
1073
 * Search for a property in a device node and read a 64-bit value from
1074
 * it. Returns 0 on success, -EINVAL if the property does not exist,
1075
 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1076
 * property data isn't large enough.
1077
 *
1078
 * The out_value is modified only if a valid u64 value can be decoded.
1079
 */
1080
int of_property_read_u64(const struct device_node *np, const char *propname,
1081
       u64 *out_value)
1082
0
{
1083
0
  const __be32 *val = of_find_property_value_of_size(np, propname,
1084
0
            sizeof(*out_value), 0, NULL);
1085
1086
0
  if (IS_ERR(val))
1087
0
    return PTR_ERR(val);
1088
1089
0
  *out_value = of_read_number(val, 2);
1090
0
  return 0;
1091
0
}
1092
EXPORT_SYMBOL_GPL(of_property_read_u64);
1093
1094
/**
1095
 * of_property_read_variable_u8_array - Find and read an array of u8 from a
1096
 * property, with bounds on the minimum and maximum array size.
1097
 *
1098
 * @np:   device node from which the property value is to be read.
1099
 * @propname: name of the property to be searched.
1100
 * @out_values: pointer to found values.
1101
 * @sz_min: minimum number of array elements to read
1102
 * @sz_max: maximum number of array elements to read, if zero there is no
1103
 *    upper limit on the number of elements in the dts entry but only
1104
 *    sz_min will be read.
1105
 *
1106
 * Search for a property in a device node and read 8-bit value(s) from
1107
 * it.
1108
 *
1109
 * dts entry of array should be like:
1110
 *  ``property = /bits/ 8 <0x50 0x60 0x70>;``
1111
 *
1112
 * Return: The number of elements read on success, -EINVAL if the property
1113
 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1114
 * if the property data is smaller than sz_min or longer than sz_max.
1115
 *
1116
 * The out_values is modified only if a valid u8 value can be decoded.
1117
 */
1118
int of_property_read_variable_u8_array(const struct device_node *np,
1119
          const char *propname, u8 *out_values,
1120
          size_t sz_min, size_t sz_max)
1121
0
{
1122
0
  size_t sz, count;
1123
0
  const u8 *val = of_find_property_value_of_size(np, propname,
1124
0
            (sz_min * sizeof(*out_values)),
1125
0
            (sz_max * sizeof(*out_values)),
1126
0
            &sz);
1127
1128
0
  if (IS_ERR(val))
1129
0
    return PTR_ERR(val);
1130
1131
0
  if (!sz_max)
1132
0
    sz = sz_min;
1133
0
  else
1134
0
    sz /= sizeof(*out_values);
1135
1136
0
  count = sz;
1137
0
  while (count--)
1138
0
    *out_values++ = *val++;
1139
1140
0
  return sz;
1141
0
}
1142
EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
1143
1144
/**
1145
 * of_property_read_variable_u16_array - Find and read an array of u16 from a
1146
 * property, with bounds on the minimum and maximum array size.
1147
 *
1148
 * @np:   device node from which the property value is to be read.
1149
 * @propname: name of the property to be searched.
1150
 * @out_values: pointer to found values.
1151
 * @sz_min: minimum number of array elements to read
1152
 * @sz_max: maximum number of array elements to read, if zero there is no
1153
 *    upper limit on the number of elements in the dts entry but only
1154
 *    sz_min will be read.
1155
 *
1156
 * Search for a property in a device node and read 16-bit value(s) from
1157
 * it.
1158
 *
1159
 * dts entry of array should be like:
1160
 *  ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
1161
 *
1162
 * Return: The number of elements read on success, -EINVAL if the property
1163
 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1164
 * if the property data is smaller than sz_min or longer than sz_max.
1165
 *
1166
 * The out_values is modified only if a valid u16 value can be decoded.
1167
 */
1168
int of_property_read_variable_u16_array(const struct device_node *np,
1169
          const char *propname, u16 *out_values,
1170
          size_t sz_min, size_t sz_max)
1171
0
{
1172
0
  size_t sz, count;
1173
0
  const __be16 *val = of_find_property_value_of_size(np, propname,
1174
0
            (sz_min * sizeof(*out_values)),
1175
0
            (sz_max * sizeof(*out_values)),
1176
0
            &sz);
1177
1178
0
  if (IS_ERR(val))
1179
0
    return PTR_ERR(val);
1180
1181
0
  if (!sz_max)
1182
0
    sz = sz_min;
1183
0
  else
1184
0
    sz /= sizeof(*out_values);
1185
1186
0
  count = sz;
1187
0
  while (count--)
1188
0
    *out_values++ = be16_to_cpup(val++);
1189
1190
0
  return sz;
1191
0
}
1192
EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
1193
1194
/**
1195
 * of_property_read_variable_u32_array - Find and read an array of 32 bit
1196
 * integers from a property, with bounds on the minimum and maximum array size.
1197
 *
1198
 * @np:   device node from which the property value is to be read.
1199
 * @propname: name of the property to be searched.
1200
 * @out_values: pointer to return found values.
1201
 * @sz_min: minimum number of array elements to read
1202
 * @sz_max: maximum number of array elements to read, if zero there is no
1203
 *    upper limit on the number of elements in the dts entry but only
1204
 *    sz_min will be read.
1205
 *
1206
 * Search for a property in a device node and read 32-bit value(s) from
1207
 * it.
1208
 *
1209
 * Return: The number of elements read on success, -EINVAL if the property
1210
 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1211
 * if the property data is smaller than sz_min or longer than sz_max.
1212
 *
1213
 * The out_values is modified only if a valid u32 value can be decoded.
1214
 */
1215
int of_property_read_variable_u32_array(const struct device_node *np,
1216
             const char *propname, u32 *out_values,
1217
             size_t sz_min, size_t sz_max)
1218
0
{
1219
0
  size_t sz, count;
1220
0
  const __be32 *val = of_find_property_value_of_size(np, propname,
1221
0
            (sz_min * sizeof(*out_values)),
1222
0
            (sz_max * sizeof(*out_values)),
1223
0
            &sz);
1224
1225
0
  if (IS_ERR(val))
1226
0
    return PTR_ERR(val);
1227
1228
0
  if (!sz_max)
1229
0
    sz = sz_min;
1230
0
  else
1231
0
    sz /= sizeof(*out_values);
1232
1233
0
  count = sz;
1234
0
  while (count--)
1235
0
    *out_values++ = be32_to_cpup(val++);
1236
1237
0
  return sz;
1238
0
}
1239
EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
1240
1241
/**
1242
 * of_property_read_variable_u64_array - Find and read an array of 64 bit integers
1243
 * from a property.
1244
 *
1245
 * @np:   device node from which the property value is to be read.
1246
 * @propname: name of the property to be searched.
1247
 * @out_value:  pointer to return value, modified only if return value is 0.
1248
 * @sz:   number of array elements to read
1249
 *
1250
 * Search for a property in a device node and read 64-bit value(s) from
1251
 * it. Returns 0 on success, -EINVAL if the property does not exist,
1252
 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1253
 * property data isn't large enough.
1254
 *
1255
 * The out_value is modified only if a valid u64 value can be decoded.
1256
 */
1257
int of_property_read_variable_u64_array(const struct device_node *np,
1258
             const char *propname, u64 *out_values,
1259
             size_t sz_min, size_t sz_max)
1260
64
{
1261
64
  size_t sz, count;
1262
64
  const __be32 *val = of_find_property_value_of_size(np, propname,
1263
64
            (sz_min * sizeof(*out_values)),
1264
64
            (sz_max * sizeof(*out_values)),
1265
64
            &sz);
1266
1267
64
  if (IS_ERR(val))
1268
0
    return PTR_ERR(val);
1269
1270
64
  if (!sz_max)
1271
64
    sz = sz_min;
1272
0
  else
1273
0
    sz /= sizeof(*out_values);
1274
1275
64
  count = sz;
1276
192
  while (count--) {
1277
128
    *out_values++ = of_read_number(val, 2);
1278
128
    val += 2;
1279
128
  }
1280
1281
64
  return sz;
1282
64
}
1283
EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
1284
1285
/**
1286
 * of_property_read_string - Find and read a string from a property
1287
 * @np:   device node from which the property value is to be read.
1288
 * @propname: name of the property to be searched.
1289
 * @out_string: pointer to null terminated return string, modified only if
1290
 *    return value is 0.
1291
 *
1292
 * Search for a property in a device tree node and retrieve a null
1293
 * terminated string value (pointer to data, not a copy). Returns 0 on
1294
 * success, -EINVAL if the property does not exist, -ENODATA if property
1295
 * does not have a value, and -EILSEQ if the string is not null-terminated
1296
 * within the length of the property data.
1297
 *
1298
 * The out_string pointer is modified only if a valid string can be decoded.
1299
 */
1300
int of_property_read_string(struct device_node *np, const char *propname,
1301
        const char **out_string)
1302
84.2k
{
1303
84.2k
  struct property *prop = of_find_property(np, propname, NULL);
1304
84.2k
  const void *value;
1305
1306
84.2k
  if (!prop)
1307
31.5k
    return -EINVAL;
1308
52.7k
  value = of_property_get_value(prop);
1309
52.7k
  if (!value)
1310
0
    return -ENODATA;
1311
52.7k
  if (strnlen(value, prop->length) >= prop->length)
1312
296
    return -EILSEQ;
1313
52.4k
  *out_string = value;
1314
52.4k
  return 0;
1315
52.7k
}
1316
EXPORT_SYMBOL_GPL(of_property_read_string);
1317
1318
/**
1319
 * of_property_match_property_string - find a property's string in an array
1320
 * @np: pointer to node containing string property
1321
 * @propname: string property name
1322
 * @array: array of strings to match the property value against
1323
 * @n: number of strings in the array
1324
 *
1325
 * Read a string property and look it up in an array of strings, like
1326
 * Linux' fwnode_property_match_property_string().
1327
 *
1328
 * Return: the index of the matching string in @array on success,
1329
 * -EINVAL if the property does not exist, -ENODATA if property does not
1330
 * have a value, -EILSEQ if the string is not null-terminated and
1331
 * -ENOENT if the value is not in @array.
1332
 */
1333
int of_property_match_property_string(const struct device_node *np,
1334
              const char *propname,
1335
              const char * const *array, size_t n)
1336
0
{
1337
0
  const char *string;
1338
0
  int ret;
1339
1340
0
  ret = of_property_read_string((struct device_node *)np, propname,
1341
0
              &string);
1342
0
  if (ret)
1343
0
    return ret;
1344
1345
0
  ret = match_string(array, n, string);
1346
0
  if (ret < 0)
1347
0
    ret = -ENOENT;
1348
1349
0
  return ret;
1350
0
}
1351
EXPORT_SYMBOL_GPL(of_property_match_property_string);
1352
1353
/**
1354
 * of_property_match_string() - Find string in a list and return index
1355
 * @np: pointer to node containing string list property
1356
 * @propname: string list property name
1357
 * @string: pointer to string to search for in string list
1358
 *
1359
 * This function searches a string list property and returns the index
1360
 * of a specific string value.
1361
 */
1362
int of_property_match_string(const struct device_node *np, const char *propname,
1363
           const char *string)
1364
1.47k
{
1365
1.47k
  struct property *prop = of_find_property(np, propname, NULL);
1366
1.47k
  size_t l;
1367
1.47k
  int i;
1368
1.47k
  const char *p, *end;
1369
1370
1.47k
  if (!prop)
1371
1.01k
    return -EINVAL;
1372
1373
461
  p = of_property_get_value(prop);
1374
461
  if (!p)
1375
0
    return -ENODATA;
1376
1377
461
  end = p + prop->length;
1378
1379
19.7k
  for (i = 0; p < end; i++, p += l) {
1380
19.5k
    l = strlen(p) + 1;
1381
19.5k
    if (p + l > end)
1382
105
      return -EILSEQ;
1383
19.4k
    pr_debug("comparing %s with %s\n", string, p);
1384
19.4k
    if (strcmp(string, p) == 0)
1385
169
      return i; /* Found it; return index */
1386
19.4k
  }
1387
187
  return -ENODATA;
1388
461
}
1389
EXPORT_SYMBOL_GPL(of_property_match_string);
1390
1391
const __be32 *of_prop_next_u32(const struct property *prop, const __be32 *cur,
1392
      u32 *pu)
1393
0
{
1394
0
  const void *curv = cur;
1395
0
  const void *value;
1396
1397
0
  if (!prop)
1398
0
    return NULL;
1399
1400
0
  value = of_property_get_value(prop);
1401
1402
0
  if (!cur) {
1403
0
    curv = value;
1404
0
    goto out_val;
1405
0
  }
1406
1407
0
  curv += sizeof(*cur);
1408
0
  if (curv >= value + prop->length)
1409
0
    return NULL;
1410
1411
0
out_val:
1412
0
  *pu = be32_to_cpup(curv);
1413
0
  return curv;
1414
0
}
1415
EXPORT_SYMBOL_GPL(of_prop_next_u32);
1416
1417
const char *of_prop_next_string(const struct property *prop, const char *cur)
1418
31.3k
{
1419
31.3k
  const void *curv = cur;
1420
31.3k
  const void *value;
1421
1422
31.3k
  if (!prop)
1423
5.95k
    return NULL;
1424
1425
25.3k
  value = of_property_get_value(prop);
1426
1427
25.3k
  if (!cur)
1428
11.6k
    return value;
1429
1430
13.6k
  curv += strlen(cur) + 1;
1431
13.6k
  if (curv >= value + prop->length)
1432
10.8k
    return NULL;
1433
1434
2.89k
  return curv;
1435
13.6k
}
1436
EXPORT_SYMBOL_GPL(of_prop_next_string);
1437
1438
/**
1439
 * of_property_write_bool - Create/Delete empty (bool) property.
1440
 *
1441
 * @np:   device node from which the property is to be set.
1442
 * @propname: name of the property to be set.
1443
 * @value true to set, false to delete
1444
 *
1445
 * Search for a property in a device node and create or delete the property.
1446
 * If the property already exists and write value is false, the property is
1447
 * deleted. If write value is true and the property does not exist, it is
1448
 * created. Returns 0 on success, -ENOMEM if the property or array
1449
 * of elements cannot be created.
1450
 */
1451
int of_property_write_bool(struct device_node *np, const char *propname,
1452
         const bool value)
1453
64
{
1454
64
  struct property *prop = of_find_property(np, propname, NULL);
1455
1456
64
  if (!value) {
1457
32
    if (prop)
1458
0
      of_delete_property(prop);
1459
32
    return 0;
1460
32
  }
1461
1462
32
  if (!prop)
1463
32
    prop = of_new_property(np, propname, NULL, 0);
1464
32
  if (!prop)
1465
0
    return -ENOMEM;
1466
1467
32
  return 0;
1468
32
}
1469
1470
/**
1471
 * of_property_write_u8_array - Write an array of u8 to a property. If
1472
 * the property does not exist, it will be created and appended to the given
1473
 * device node.
1474
 *
1475
 * @np:   device node to which the property value is to be written.
1476
 * @propname: name of the property to be written.
1477
 * @values: pointer to array elements to write.
1478
 * @sz:   number of array elements to write.
1479
 *
1480
 * Search for a property in a device node and write 8-bit value(s) to
1481
 * it. If the property does not exist, it will be created and appended to
1482
 * the device node. Returns 0 on success, -ENOMEM if the property or array
1483
 * of elements cannot be created.
1484
 */
1485
int of_property_write_u8_array(struct device_node *np,
1486
             const char *propname, const u8 *values,
1487
             size_t sz)
1488
0
{
1489
0
  struct property *prop = of_find_property(np, propname, NULL);
1490
1491
0
  if (prop)
1492
0
    of_delete_property(prop);
1493
1494
0
  prop = of_new_property(np, propname, values, sizeof(*values) * sz);
1495
0
  if (!prop)
1496
0
    return -ENOMEM;
1497
1498
0
  return 0;
1499
0
}
1500
1501
/**
1502
 * of_property_write_u16_array - Write an array of u16 to a property. If
1503
 * the property does not exist, it will be created and appended to the given
1504
 * device node.
1505
 *
1506
 * @np:   device node to which the property value is to be written.
1507
 * @propname: name of the property to be written.
1508
 * @values: pointer to array elements to write.
1509
 * @sz:   number of array elements to write.
1510
 *
1511
 * Search for a property in a device node and write 16-bit value(s) to
1512
 * it. If the property does not exist, it will be created and appended to
1513
 * the device node. Returns 0 on success, -ENOMEM if the property or array
1514
 * of elements cannot be created.
1515
 */
1516
int of_property_write_u16_array(struct device_node *np,
1517
        const char *propname, const u16 *values,
1518
        size_t sz)
1519
0
{
1520
0
  struct property *prop = of_find_property(np, propname, NULL);
1521
0
  __be16 *val;
1522
1523
0
  if (prop)
1524
0
    of_delete_property(prop);
1525
1526
0
  prop = of_new_property(np, propname, NULL, sizeof(*val) * sz);
1527
0
  if (!prop)
1528
0
    return -ENOMEM;
1529
1530
0
  val = prop->value;
1531
0
  while (sz--)
1532
0
    *val++ = cpu_to_be16(*values++);
1533
1534
0
  return 0;
1535
0
}
1536
1537
/**
1538
 * of_property_write_u32_array - Write an array of u32 to a property. If
1539
 * the property does not exist, it will be created and appended to the given
1540
 * device node.
1541
 *
1542
 * @np:   device node to which the property value is to be written.
1543
 * @propname: name of the property to be written.
1544
 * @values: pointer to array elements to write.
1545
 * @sz:   number of array elements to write.
1546
 *
1547
 * Search for a property in a device node and write 32-bit value(s) to
1548
 * it. If the property does not exist, it will be created and appended to
1549
 * the device node. Returns 0 on success, -ENOMEM if the property or array
1550
 * of elements cannot be created.
1551
 */
1552
int of_property_write_u32_array(struct device_node *np,
1553
        const char *propname, const u32 *values,
1554
        size_t sz)
1555
32
{
1556
32
  struct property *prop = of_find_property(np, propname, NULL);
1557
32
  __be32 *val;
1558
1559
32
  if (prop)
1560
0
    of_delete_property(prop);
1561
1562
32
  prop = of_new_property(np, propname, NULL, sizeof(*val) * sz);
1563
32
  if (!prop)
1564
0
    return -ENOMEM;
1565
1566
32
  val = prop->value;
1567
64
  while (sz--)
1568
32
    *val++ = cpu_to_be32(*values++);
1569
1570
32
  return 0;
1571
32
}
1572
1573
/**
1574
 * of_property_write_u64_array - Write an array of u64 to a property. If
1575
 * the property does not exist, it will be created and appended to the given
1576
 * device node.
1577
 *
1578
 * @np:   device node to which the property value is to be written.
1579
 * @propname: name of the property to be written.
1580
 * @values: pointer to array elements to write.
1581
 * @sz:   number of array elements to write.
1582
 *
1583
 * Search for a property in a device node and write 64-bit value(s) to
1584
 * it. If the property does not exist, it will be created and appended to
1585
 * the device node. Returns 0 on success, -ENOMEM if the property or array
1586
 * of elements cannot be created.
1587
 */
1588
int of_property_write_u64_array(struct device_node *np,
1589
        const char *propname, const u64 *values,
1590
        size_t sz)
1591
96
{
1592
96
  struct property *prop = of_find_property(np, propname, NULL);
1593
96
  __be32 *val;
1594
1595
96
  if (prop)
1596
32
    of_delete_property(prop);
1597
1598
96
  prop = of_new_property(np, propname, NULL, 2 * sizeof(*val) * sz);
1599
96
  if (!prop)
1600
0
    return -ENOMEM;
1601
1602
96
  val = prop->value;
1603
320
  while (sz--) {
1604
224
    of_write_number(val, *values++, 2);
1605
224
    val += 2;
1606
224
  }
1607
1608
96
  return 0;
1609
96
}
1610
1611
/**
1612
 * of_property_write_string_array - Write strings to a property. If
1613
 * the property does not exist, it will be created and appended to the given
1614
 * device node.
1615
 *
1616
 * @np:   device node to which the property value is to be written.
1617
 * @propname: name of the property to be written.
1618
 * @values: pointer to array elements to write.
1619
 * @sz:   number of array elements to write.
1620
 *
1621
 * Search for a property in a device node and write a string to
1622
 * it. If the property does not exist, it will be created and appended to
1623
 * the device node. Returns 0 on success, -ENOMEM if the property or array
1624
 * of elements cannot be created, -EINVAL if no strings specified.
1625
 */
1626
int of_property_write_string_array(struct device_node *np,
1627
           const char *propname, const char *const *values,
1628
           size_t sz)
1629
0
{
1630
0
  char *buf = NULL, *next;
1631
0
  size_t len = 0;
1632
0
  int ret = 0, i;
1633
1634
0
  for (i = 0; i < sz; i++)
1635
0
    len += strlen(values[i]) + 1;
1636
1637
0
  if (!len)
1638
0
    return -EINVAL;
1639
1640
0
  buf = malloc(len);
1641
0
  if (!buf)
1642
0
    return -ENOMEM;
1643
1644
0
  next = buf;
1645
1646
0
  for (i = 0; i < sz; i++)
1647
0
    next = stpcpy(next, values[i]) + 1;
1648
1649
0
  ret = of_set_property(np, propname, buf, len, 1);
1650
0
  free(buf);
1651
0
  return ret;
1652
0
}
1653
1654
/**
1655
 * of_property_write_strings - Write strings to a property. If
1656
 * the property does not exist, it will be created and appended to the given
1657
 * device node.
1658
 *
1659
 * @np:   device node to which the property value is to be written.
1660
 * @propname: name of the property to be written.
1661
 * @...:  pointers to strings to write
1662
 *
1663
 * Search for a property in a device node and write a string to
1664
 * it. If the property does not exist, it will be created and appended to
1665
 * the device node. Returns 0 on success, -ENOMEM if the property or array
1666
 * of elements cannot be created, -EINVAL if no strings specified.
1667
 */
1668
int of_property_write_strings(struct device_node *np,
1669
            const char *propname, ...)
1670
32
{
1671
32
  const char *val;
1672
32
  char *buf = NULL, *next;
1673
32
  size_t len = 0;
1674
32
  va_list ap;
1675
32
  int ret = 0;
1676
1677
32
  va_start(ap, propname);
1678
96
  for (val = va_arg(ap, char *); val; val = va_arg(ap, char *))
1679
64
    len += strlen(val) + 1;
1680
32
  va_end(ap);
1681
1682
32
  if (!len)
1683
0
    return -EINVAL;
1684
1685
32
  buf = malloc(len);
1686
32
  if (!buf)
1687
0
    return -ENOMEM;
1688
1689
32
  next = buf;
1690
1691
32
  va_start(ap, propname);
1692
96
  for (val = va_arg(ap, char *); val; val = va_arg(ap, char *))
1693
64
    next = stpcpy(next, val) + 1;
1694
32
  va_end(ap);
1695
1696
32
  ret = of_set_property(np, propname, buf, len, 1);
1697
32
  free(buf);
1698
32
  return ret;
1699
32
}
1700
1701
/**
1702
 * of_property_write_string - Write a string to a property. If
1703
 * the property does not exist, it will be created and appended to the given
1704
 * device node.
1705
 *
1706
 * @np:   device node to which the property value is to be written.
1707
 * @propname: name of the property to be written.
1708
 * @value:  pointer to the string to write
1709
 *
1710
 * Search for a property in a device node and write a string to
1711
 * it. If the property does not exist, it will be created and appended to
1712
 * the device node. Returns 0 on success, -ENOMEM if the property or array
1713
 * of elements cannot be created.
1714
 */
1715
int of_property_write_string(struct device_node *np,
1716
           const char *propname, const char *value)
1717
32
{
1718
32
  size_t len = strlen(value);
1719
1720
32
  return of_set_property(np, propname, value, len + 1, 1);
1721
32
}
1722
1723
/**
1724
 * of_parse_phandle_from - Resolve a phandle property to a device_node pointer from
1725
 * a given root node
1726
 * @np: Pointer to device node holding phandle property
1727
 * @root: root node of the tree to search in. If NULL use the internal tree.
1728
 * @phandle_name: Name of property holding a phandle value
1729
 * @index: For properties holding a table of phandles, this is the index into
1730
 *         the table
1731
 *
1732
 * Returns the device_node pointer found or NULL.
1733
 */
1734
struct device_node *of_parse_phandle_from(const struct device_node *np,
1735
          struct device_node *root,
1736
          const char *phandle_name, int index)
1737
32
{
1738
32
  const __be32 *phandle;
1739
32
  int size;
1740
1741
32
  phandle = of_get_property(np, phandle_name, &size);
1742
32
  if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
1743
0
    return NULL;
1744
1745
32
  return of_find_node_by_phandle_from(be32_to_cpup(phandle + index),
1746
32
          root);
1747
32
}
1748
EXPORT_SYMBOL(of_parse_phandle_from);
1749
1750
/**
1751
 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1752
 * @np: Pointer to device node holding phandle property
1753
 * @phandle_name: Name of property holding a phandle value
1754
 * @index: For properties holding a table of phandles, this is the index into
1755
 *         the table
1756
 *
1757
 * Returns the device_node pointer found or NULL.
1758
 */
1759
struct device_node *of_parse_phandle(const struct device_node *np,
1760
             const char *phandle_name, int index)
1761
32
{
1762
32
  return of_parse_phandle_from(np, root_node, phandle_name, index);
1763
32
}
1764
EXPORT_SYMBOL(of_parse_phandle);
1765
1766
int of_phandle_iterator_init(struct of_phandle_iterator *it,
1767
    const struct device_node *np,
1768
    const char *list_name,
1769
    const char *cells_name,
1770
    int cell_count)
1771
10.0k
{
1772
10.0k
  const __be32 *list;
1773
10.0k
  int size;
1774
1775
10.0k
  memset(it, 0, sizeof(*it));
1776
1777
  /*
1778
   * one of cell_count or cells_name must be provided to determine the
1779
   * argument length.
1780
   */
1781
10.0k
  if (cell_count < 0 && !cells_name)
1782
0
    return -EINVAL;
1783
1784
10.0k
  list = of_get_property(np, list_name, &size);
1785
10.0k
  if (!list)
1786
9.90k
    return -ENOENT;
1787
1788
160
  it->cells_name = cells_name;
1789
160
  it->cell_count = cell_count;
1790
160
  it->parent = np;
1791
160
  it->list_end = list + size / sizeof(*list);
1792
160
  it->phandle_end = list;
1793
160
  it->cur = list;
1794
1795
160
  return 0;
1796
10.0k
}
1797
EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
1798
1799
int of_phandle_iterator_next(struct of_phandle_iterator *it)
1800
192
{
1801
192
  uint32_t count = 0;
1802
1803
192
  if (it->node) {
1804
32
    of_node_put(it->node);
1805
32
    it->node = NULL;
1806
32
  }
1807
1808
192
  if (!it->cur || it->phandle_end >= it->list_end)
1809
32
    return -ENOENT;
1810
1811
160
  it->cur = it->phandle_end;
1812
1813
  /* If phandle is 0, then it is an empty entry with no arguments. */
1814
160
  it->phandle = be32_to_cpup(it->cur++);
1815
1816
160
  if (it->phandle) {
1817
1818
    /*
1819
     * Find the provider node and parse the #*-cells property to
1820
     * determine the argument length.
1821
     */
1822
160
    it->node = of_find_node_by_phandle(it->phandle);
1823
1824
160
    if (it->cells_name) {
1825
160
      if (!it->node) {
1826
0
        pr_err("%pOF: could not find phandle %d\n",
1827
0
               it->parent, it->phandle);
1828
0
        goto err;
1829
0
      }
1830
1831
160
      if (of_property_read_u32(it->node, it->cells_name,
1832
160
             &count)) {
1833
        /*
1834
         * If both cell_count and cells_name is given,
1835
         * fall back to cell_count in absence
1836
         * of the cells_name property
1837
         */
1838
96
        if (it->cell_count >= 0) {
1839
96
          count = it->cell_count;
1840
96
        } else {
1841
0
          pr_err("%pOF: could not get %s for %pOF\n",
1842
0
                 it->parent,
1843
0
                 it->cells_name,
1844
0
                 it->node);
1845
0
          goto err;
1846
0
        }
1847
96
      }
1848
160
    } else {
1849
0
      count = it->cell_count;
1850
0
    }
1851
1852
    /*
1853
     * Make sure that the arguments actually fit in the remaining
1854
     * property data length
1855
     */
1856
160
    if (it->cur + count > it->list_end) {
1857
0
      if (it->cells_name)
1858
0
        pr_err("%pOF: %s = %d found %td\n",
1859
0
          it->parent, it->cells_name,
1860
0
          count, it->list_end - it->cur);
1861
0
      else
1862
0
        pr_err("%pOF: phandle %s needs %d, found %td\n",
1863
0
          it->parent, of_node_full_name(it->node),
1864
0
          count, it->list_end - it->cur);
1865
0
      goto err;
1866
0
    }
1867
160
  }
1868
1869
160
  it->phandle_end = it->cur + count;
1870
160
  it->cur_count = count;
1871
1872
160
  return 0;
1873
1874
0
err:
1875
0
  if (it->node) {
1876
0
    of_node_put(it->node);
1877
0
    it->node = NULL;
1878
0
  }
1879
1880
0
  return -EINVAL;
1881
160
}
1882
EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
1883
1884
int of_phandle_iterator_args(struct of_phandle_iterator *it,
1885
           uint32_t *args,
1886
           int size)
1887
128
{
1888
128
  int i, count;
1889
1890
128
  count = it->cur_count;
1891
1892
128
  if (WARN_ON(size < count))
1893
0
    count = size;
1894
1895
128
  for (i = 0; i < count; i++)
1896
0
    args[i] = be32_to_cpup(it->cur++);
1897
1898
128
  return count;
1899
128
}
1900
1901
int __of_parse_phandle_with_args(const struct device_node *np,
1902
         const char *list_name,
1903
         const char *cells_name,
1904
         int cell_count, int index,
1905
         struct of_phandle_args *out_args)
1906
128
{
1907
128
  struct of_phandle_iterator it;
1908
128
  int rc, cur_index = 0;
1909
1910
128
  if (index < 0)
1911
0
    return -EINVAL;
1912
1913
  /* Loop over the phandles until all the requested entry is found */
1914
128
  of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
1915
    /*
1916
     * All of the error cases bail out of the loop, so at
1917
     * this point, the parsing is successful. If the requested
1918
     * index matches, then fill the out_args structure and return,
1919
     * or return -ENOENT for an empty entry.
1920
     */
1921
128
    rc = -ENOENT;
1922
128
    if (cur_index == index) {
1923
128
      if (!it.phandle)
1924
0
        goto err;
1925
1926
128
      if (out_args) {
1927
128
        int c;
1928
1929
128
        c = of_phandle_iterator_args(&it,
1930
128
                   out_args->args,
1931
128
                   MAX_PHANDLE_ARGS);
1932
128
        out_args->np = it.node;
1933
128
        out_args->args_count = c;
1934
128
      } else {
1935
0
        of_node_put(it.node);
1936
0
      }
1937
1938
      /* Found it! return success */
1939
128
      return 0;
1940
128
    }
1941
1942
0
    cur_index++;
1943
0
  }
1944
1945
  /*
1946
   * Unlock node before returning result; will be one of:
1947
   * -ENOENT : index is for empty phandle
1948
   * -EINVAL : parsing error on data
1949
   */
1950
1951
0
 err:
1952
0
  of_node_put(it.node);
1953
0
  return rc;
1954
128
}
1955
EXPORT_SYMBOL(__of_parse_phandle_with_args);
1956
1957
/**
1958
 * of_count_phandle_with_args() - Find the number of phandles references in a property
1959
 * @np:   pointer to a device tree node containing a list
1960
 * @list_name:  property name that contains a list
1961
 * @cells_name: property name that specifies phandles' arguments count
1962
 *
1963
 * Return: The number of phandle + argument tuples within a property. It
1964
 * is a typical pattern to encode a list of phandle and variable
1965
 * arguments into a single property. The number of arguments is encoded
1966
 * by a property in the phandle-target node. For example, a gpios
1967
 * property would contain a list of GPIO specifies consisting of a
1968
 * phandle and 1 or more arguments. The number of arguments are
1969
 * determined by the #gpio-cells property in the node pointed to by the
1970
 * phandle.
1971
 */
1972
int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1973
        const char *cells_name)
1974
9.93k
{
1975
9.93k
  struct of_phandle_iterator it;
1976
9.93k
  int rc, cur_index = 0;
1977
1978
  /*
1979
   * If cells_name is NULL we assume a cell count of 0. This makes
1980
   * counting the phandles trivial as each 32bit word in the list is a
1981
   * phandle and no arguments are to consider. So we don't iterate through
1982
   * the list but just use the length to determine the phandle count.
1983
   */
1984
9.93k
  if (!cells_name) {
1985
0
    const __be32 *list;
1986
0
    int size;
1987
1988
0
    list = of_get_property(np, list_name, &size);
1989
0
    if (!list)
1990
0
      return -ENOENT;
1991
1992
0
    return size / sizeof(*list);
1993
0
  }
1994
1995
9.93k
  rc = of_phandle_iterator_init(&it, np, list_name, cells_name, -1);
1996
9.93k
  if (rc)
1997
9.90k
    return rc;
1998
1999
64
  while ((rc = of_phandle_iterator_next(&it)) == 0)
2000
32
    cur_index += 1;
2001
2002
32
  if (rc != -ENOENT)
2003
0
    return rc;
2004
2005
32
  return cur_index;
2006
32
}
2007
EXPORT_SYMBOL(of_count_phandle_with_args);
2008
2009
/**
2010
 * of_machine_is_compatible - Test root of device tree for a given compatible value
2011
 * @compat: compatible string to look for in root node's compatible property.
2012
 *
2013
 * Returns true if the root node has the given value in its
2014
 * compatible property.
2015
 */
2016
int of_machine_is_compatible(const char *compat)
2017
32
{
2018
32
  if (!root_node)
2019
0
    return 0;
2020
2021
32
  return of_device_is_compatible(root_node, compat);
2022
32
}
2023
EXPORT_SYMBOL(of_machine_is_compatible);
2024
2025
/**
2026
 *  of_find_node_by_path_from - Find a node matching a full OF path
2027
 *      relative to a given root node.
2028
 *  @path:  The full path to match
2029
 *
2030
 *  Returns a pointer to the node found or NULL.
2031
 */
2032
struct device_node *of_find_node_by_path_from(struct device_node *from,
2033
          const char *path)
2034
293
{
2035
293
  char *slash, *p, *freep;
2036
2037
293
  if (!from)
2038
0
    from = root_node;
2039
2040
293
  if (!from || !path || *path != '/')
2041
0
    return NULL;
2042
2043
293
  path++;
2044
2045
293
  freep = p = xstrdup(path);
2046
2047
325
  while (1) {
2048
325
    if (!*p)
2049
0
      goto out;
2050
2051
325
    slash = strchr(p, '/');
2052
325
    if (slash)
2053
32
      *slash = 0;
2054
2055
325
    from = of_get_child_by_name(from, p);
2056
325
    if (!from)
2057
197
      goto out;
2058
2059
128
    if (!slash)
2060
96
      goto out;
2061
2062
32
    p = slash + 1;
2063
32
  }
2064
293
out:
2065
293
  free(freep);
2066
2067
293
  return from;
2068
293
}
2069
EXPORT_SYMBOL(of_find_node_by_path_from);
2070
2071
/**
2072
 *  of_find_node_by_path - Find a node matching a full OF path
2073
 *  @path:  The full path to match
2074
 *
2075
 *  Returns a pointer to the node found or NULL.
2076
 */
2077
struct device_node *of_find_node_by_path(const char *path)
2078
224
{
2079
224
  return of_find_node_by_path_from(root_node, path);
2080
224
}
2081
EXPORT_SYMBOL(of_find_node_by_path);
2082
2083
/**
2084
 *  of_find_node_by_path_or_alias - Find a node matching a full OF path
2085
 *  or an alias
2086
 *  @root:  The root node. If NULL the internal tree is used
2087
 *  @str: the full path or alias
2088
 *
2089
 *  Returns a pointer to the node found or NULL.
2090
 */
2091
struct device_node *of_find_node_by_path_or_alias(struct device_node *root,
2092
    const char *str)
2093
0
{
2094
0
  struct device_node *node;
2095
0
  const char *slash;
2096
0
  char *alias;
2097
0
  size_t len = 0;
2098
2099
0
  if (*str ==  '/')
2100
0
    return of_find_node_by_path_from(root, str);
2101
2102
0
  slash = strchr(str, '/');
2103
2104
0
  if (!slash)
2105
0
    return of_find_node_by_alias(root, str);
2106
2107
0
  len = slash - str + 1;
2108
0
  alias = xmalloc(len);
2109
0
  strlcpy(alias, str, len);
2110
2111
0
  node = of_find_node_by_alias(root, alias);
2112
2113
0
  if (!node)
2114
0
    goto out;
2115
2116
0
  node = of_find_node_by_path_from(node, slash);
2117
0
out:
2118
0
  free(alias);
2119
0
  return node;
2120
0
}
2121
EXPORT_SYMBOL(of_find_node_by_path_or_alias);
2122
2123
/**
2124
 * of_modalias_node - Lookup appropriate modalias for a device node
2125
 * @node: pointer to a device tree node
2126
 * @modalias: Pointer to buffer that modalias value will be copied into
2127
 * @len:  Length of modalias value
2128
 *
2129
 * Based on the value of the compatible property, this routine will attempt
2130
 * to choose an appropriate modalias value for a particular device tree node.
2131
 * It does this by stripping the manufacturer prefix (as delimited by a ',')
2132
 * from the first entry in the compatible list property.
2133
 *
2134
 * This routine returns 0 on success, <0 on failure.
2135
 */
2136
int of_modalias_node(struct device_node *node, char *modalias, int len)
2137
0
{
2138
0
  const char *compatible, *p;
2139
0
  int cplen;
2140
2141
0
  compatible = of_get_property(node, "compatible", &cplen);
2142
0
  if (!compatible || strlen(compatible) > cplen)
2143
0
    return -ENODEV;
2144
0
  p = strchr(compatible, ',');
2145
0
  strlcpy(modalias, p ? p + 1 : compatible, len);
2146
0
  return 0;
2147
0
}
2148
EXPORT_SYMBOL_GPL(of_modalias_node);
2149
2150
static struct device_node *of_chosen;
2151
static const char *of_model;
2152
2153
struct device_node *of_get_root_node(void)
2154
4.87k
{
2155
4.87k
  return root_node;
2156
4.87k
}
2157
EXPORT_SYMBOL(of_get_root_node);
2158
2159
int of_set_root_node(struct device_node *node)
2160
32
{
2161
32
  if (node && root_node)
2162
0
    return -EBUSY;
2163
2164
32
  root_node = node;
2165
2166
32
  of_chosen = of_find_node_by_path("/chosen");
2167
32
  of_property_read_string(root_node, "model", &of_model);
2168
2169
32
  if (root_node)
2170
32
    of_property_write_bool(root_node, "$barebox,root-node", true);
2171
2172
32
  if (of_model)
2173
32
    barebox_set_model(of_model);
2174
2175
32
  of_alias_scan();
2176
2177
32
  return 0;
2178
32
}
2179
2180
static int barebox_of_populate(void)
2181
32
{
2182
32
  if (IS_ENABLED(CONFIG_OFDEVICE) && deep_probe_is_supported())
2183
32
    return of_probe();
2184
2185
0
  return 0;
2186
32
}
2187
of_populate_initcall(barebox_of_populate);
2188
2189
int barebox_register_of(struct device_node *root)
2190
32
{
2191
32
  if (root_node) {
2192
    /*
2193
     * Only a tentative device tree, i.e. one without any nodes,
2194
     * may be replaced by a real one.
2195
     */
2196
0
    if (!list_empty(&root_node->children))
2197
0
      return -EBUSY;
2198
    /* Not necessarily safe to free, so leak it.. */
2199
0
    root_node = NULL;
2200
0
  }
2201
2202
  /* of_set_root_node() consumes /chosen, model and aliases, so patch first */
2203
32
  of_overlay_apply_builtin(root);
2204
2205
32
  of_set_root_node(root);
2206
2207
32
  of_fix_tree(root);
2208
2209
32
  if (IS_ENABLED(CONFIG_OFDEVICE)) {
2210
32
    of_clk_init();
2211
32
    if (deep_probe_is_supported())
2212
32
      genpd_activate();
2213
0
    else
2214
0
      return of_probe();
2215
32
  }
2216
2217
32
  return 0;
2218
32
}
2219
2220
int barebox_register_fdt(const void *dtb)
2221
32
{
2222
32
  struct device_node *root;
2223
2224
32
  if (root_node)
2225
0
    return -EBUSY;
2226
2227
32
  root = of_unflatten_dtb(dtb, INT_MAX);
2228
32
  if (IS_ERR(root)) {
2229
0
    pr_err("Cannot unflatten dtb: %pe\n", root);
2230
0
    return PTR_ERR(root);
2231
0
  }
2232
2233
32
  optee_register_overlay();
2234
2235
32
  return barebox_register_of(root);
2236
32
}
2237
2238
/**
2239
 *  of_device_is_available - check if a device is available for use
2240
 *
2241
 *  @device: Node to check for availability
2242
 *
2243
 *  Returns 1 if the status property is absent or set to "okay" or "ok",
2244
 *  0 otherwise
2245
 */
2246
int of_device_is_available(const struct device_node *device)
2247
448
{
2248
448
  const char *status;
2249
448
  int statlen;
2250
2251
448
  status = of_get_property(device, "barebox,status", &statlen);
2252
448
  if (status == NULL)
2253
448
    status = of_get_property(device, "status", &statlen);
2254
448
  if (status == NULL)
2255
448
    return 1;
2256
2257
0
  if (statlen > 0) {
2258
0
    if (!strcmp(status, "okay") || !strcmp(status, "ok"))
2259
0
      return 1;
2260
0
  }
2261
2262
0
  return 0;
2263
0
}
2264
EXPORT_SYMBOL(of_device_is_available);
2265
2266
/**
2267
 *  of_device_is_big_endian - check if a device has BE registers
2268
 *
2269
 *  @device: Node to check for endianness
2270
 *
2271
 *  Returns true if the device has a "big-endian" property, or if the kernel
2272
 *  was compiled for BE *and* the device has a "native-endian" property.
2273
 *  Returns false otherwise.
2274
 *
2275
 *  Callers would nominally use ioread32be/iowrite32be if
2276
 *  of_device_is_big_endian() == true, or readl/writel otherwise.
2277
 */
2278
bool of_device_is_big_endian(const struct device_node *device)
2279
0
{
2280
0
  if (of_property_read_bool(device, "big-endian"))
2281
0
    return true;
2282
0
  if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
2283
0
      of_property_read_bool(device, "native-endian"))
2284
0
    return true;
2285
0
  return false;
2286
0
}
2287
EXPORT_SYMBOL(of_device_is_big_endian);
2288
2289
/**
2290
 *  of_get_parent - Get a node's parent if any
2291
 *  @node:  Node to get parent
2292
 *
2293
 *  Returns a pointer to the parent node or NULL if already at root.
2294
 */
2295
struct device_node *of_get_parent(const struct device_node *node)
2296
2.81k
{
2297
2.81k
  return (!node) ? NULL : node->parent;
2298
2.81k
}
2299
EXPORT_SYMBOL(of_get_parent);
2300
2301
/**
2302
 *  of_get_next_available_child - Find the next available child node
2303
 *  @node:  parent node
2304
 *  @prev:  previous child of the parent node, or NULL to get first
2305
 *
2306
 *      This function is like of_get_next_child(), except that it
2307
 *      automatically skips any disabled nodes (i.e. status = "disabled").
2308
 */
2309
struct device_node *of_get_next_available_child(const struct device_node *node,
2310
  struct device_node *prev)
2311
0
{
2312
0
  if (!node)
2313
0
    return NULL;
2314
2315
0
  prev = list_prepare_entry(prev, &node->children, parent_list);
2316
0
  list_for_each_entry_continue(prev, &node->children, parent_list)
2317
0
    if (of_device_is_available(prev))
2318
0
      return prev;
2319
0
  return NULL;
2320
0
}
2321
EXPORT_SYMBOL(of_get_next_available_child);
2322
2323
/**
2324
 *  of_get_next_child - Iterate a node children
2325
 *  @node:  parent node
2326
 *  @prev:  previous child of the parent node, or NULL to get first
2327
 *
2328
 *  Returns a node pointer with refcount incremented.
2329
 */
2330
struct device_node *of_get_next_child(const struct device_node *node,
2331
  struct device_node *prev)
2332
0
{
2333
0
  if (prev)
2334
0
    node = prev->parent;
2335
2336
0
  if (!node)
2337
0
    return NULL;
2338
2339
0
  prev = list_prepare_entry(prev, &node->children, parent_list);
2340
0
  list_for_each_entry_continue(prev, &node->children, parent_list)
2341
0
    return prev;
2342
0
  return NULL;
2343
0
}
2344
EXPORT_SYMBOL(of_get_next_child);
2345
2346
/**
2347
 *  of_get_child_count - Count child nodes of given parent node
2348
 *  @parent:  parent node
2349
 *
2350
 *      Returns the number of child nodes or -EINVAL on NULL parent node.
2351
 */
2352
int of_get_child_count(const struct device_node *parent)
2353
0
{
2354
0
  struct device_node *child;
2355
0
  int num = 0;
2356
2357
0
  if (!parent)
2358
0
    return -EINVAL;
2359
2360
0
  for_each_child_of_node(parent, child)
2361
0
    num++;
2362
2363
0
  return num;
2364
0
}
2365
EXPORT_SYMBOL(of_get_child_count);
2366
2367
/**
2368
 *  of_get_available_child_count - Count available child nodes of given
2369
 *      parent node
2370
 *  @parent:  parent node
2371
 *
2372
 *      Returns the number of available child nodes or -EINVAL on NULL parent
2373
 *      node.
2374
 */
2375
int of_get_available_child_count(const struct device_node *parent)
2376
0
{
2377
0
  struct device_node *child;
2378
0
  int num = 0;
2379
2380
0
  if (!parent)
2381
0
    return -EINVAL;
2382
2383
0
  for_each_child_of_node(parent, child)
2384
0
    if (of_device_is_available(child))
2385
0
      num++;
2386
2387
0
  return num;
2388
0
}
2389
EXPORT_SYMBOL(of_get_available_child_count);
2390
2391
/**
2392
 * of_get_compatible_child - Find compatible child node
2393
 * @parent: parent node
2394
 * @compatible: compatible string
2395
 *
2396
 * Lookup child node whose compatible property contains the given compatible
2397
 * string.
2398
 *
2399
 * Returns a node pointer with refcount incremented, use of_node_put() on it
2400
 * when done; or NULL if not found.
2401
 */
2402
struct device_node *of_get_compatible_child(const struct device_node *parent,
2403
        const char *compatible)
2404
0
{
2405
0
  struct device_node *child;
2406
2407
0
  for_each_child_of_node(parent, child) {
2408
0
    if (of_device_is_compatible(child, compatible))
2409
0
      return child;
2410
0
  }
2411
2412
0
  return NULL;
2413
0
}
2414
EXPORT_SYMBOL(of_get_compatible_child);
2415
2416
/**
2417
 *  of_get_child_by_name - Find the child node by name for a given parent
2418
 *  @node:  parent node
2419
 *  @name:  child name to look for.
2420
 *
2421
 *      This function looks for child node for given matching full name
2422
 *
2423
 *  Returns a node pointer if found or NULL.
2424
 */
2425
struct device_node *of_get_child_by_name(const struct device_node *node,
2426
        const char *name)
2427
16.8k
{
2428
16.8k
  struct device_node *child;
2429
2430
16.8k
  for_each_child_of_node(node, child)
2431
18.1k
    if (child->name && (of_node_cmp(child->name, name) == 0))
2432
13.4k
      return child;
2433
2434
3.41k
  return NULL;
2435
16.8k
}
2436
EXPORT_SYMBOL(of_get_child_by_name);
2437
2438
/**
2439
 *  of_get_child_by_name_stem - Find the child node by name for a given parent
2440
 *  @node:  parent node
2441
 *  @name:  child name to look for.
2442
 *
2443
 *      This function looks for child node for given matching name excluding the
2444
 *      unit address
2445
 *
2446
 *  Returns a node pointer if found or NULL.
2447
 */
2448
struct device_node *of_get_child_by_name_stem(const struct device_node *node,
2449
        const char *name)
2450
0
{
2451
0
  struct device_node *child;
2452
2453
0
  for_each_child_of_node(node, child)
2454
0
    if (of_node_name_eq(child, name))
2455
0
      break;
2456
0
  return child;
2457
0
}
2458
EXPORT_SYMBOL(of_get_child_by_name_stem);
2459
2460
/**
2461
 * of_property_read_string_helper() - Utility helper for parsing string properties
2462
 * @np:   device node from which the property value is to be read.
2463
 * @propname: name of the property to be searched.
2464
 * @out_strs: output array of string pointers.
2465
 * @sz:   number of array elements to read.
2466
 * @skip: Number of strings to skip over at beginning of list.
2467
 *
2468
 * Don't call this function directly. It is a utility helper for the
2469
 * of_property_read_string*() family of functions.
2470
 */
2471
int of_property_read_string_helper(const struct device_node *np,
2472
           const char *propname, const char **out_strs,
2473
           size_t sz, int skip)
2474
94.8k
{
2475
94.8k
  const struct property *prop = of_find_property(np, propname, NULL);
2476
94.8k
  int l = 0, i = 0;
2477
94.8k
  const char *p, *end;
2478
2479
94.8k
  if (!prop)
2480
52.5k
    return -EINVAL;
2481
42.2k
  p = of_property_get_value(prop);
2482
42.2k
  if (!p)
2483
0
    return -ENODATA;
2484
42.2k
  end = p + prop->length;
2485
2486
222k
  for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
2487
181k
    l = strnlen(p, end - p) + 1;
2488
181k
    if (p + l > end)
2489
625
      return -EILSEQ;
2490
180k
    if (out_strs && i >= skip)
2491
31.8k
      *out_strs++ = p;
2492
180k
  }
2493
41.6k
  i -= skip;
2494
41.6k
  return i <= 0 ? -ENODATA : i;
2495
42.2k
}
2496
2497
static void __of_print_property_prefixed(const struct property *p, int indent,
2498
           unsigned maxpropsize, const char *prefix)
2499
0
{
2500
0
  unsigned length;
2501
2502
0
  printf("%s%*s%s", prefix, indent * 8, "", p->name);
2503
2504
0
  length = min_t(unsigned, p->length, maxpropsize);
2505
0
  if (length) {
2506
0
    printf(" = ");
2507
0
    of_print_property(of_property_get_value(p), length);
2508
0
  }
2509
0
  if (length != p->length)
2510
0
    printf(" /* %u more bytes omitted */", p->length - length);
2511
2512
0
  printf(";\n");
2513
0
}
2514
2515
static int __of_print_nodes(struct device_node *node, int indent,
2516
          unsigned maxpropsize, const char *prefix)
2517
0
{
2518
0
  struct device_node *n;
2519
0
  struct property *p;
2520
0
  int ret;
2521
2522
0
  if (!node)
2523
0
    return 0;
2524
2525
0
  if (!prefix)
2526
0
    prefix = "";
2527
2528
0
  printf("%s%*s%s%s\n", prefix, indent * 8, "", node->name, node->name ? " {" : "{");
2529
2530
0
  list_for_each_entry(p, &node->properties, list)
2531
0
    __of_print_property_prefixed(p, indent + 1, maxpropsize, prefix);
2532
2533
0
  if (ctrlc())
2534
0
    return -EINTR;
2535
2536
0
  list_for_each_entry(n, &node->children, parent_list) {
2537
0
    ret = __of_print_nodes(n, indent + 1, maxpropsize, prefix);
2538
0
    if (ret)
2539
0
      return ret;
2540
0
  }
2541
2542
0
  printf("%s%*s};\n", prefix, indent * 8, "");
2543
0
  return 0;
2544
0
}
2545
2546
void of_print_nodes(struct device_node *node, int indent, unsigned maxpropsize)
2547
0
{
2548
0
  __of_print_nodes(node, indent, maxpropsize, NULL);
2549
0
}
2550
2551
static void __of_print_property(struct property *p, int indent, unsigned maxpropsize)
2552
0
{
2553
0
  __of_print_property_prefixed(p, indent, maxpropsize, "");
2554
0
}
2555
2556
void of_print_properties(struct device_node *node, unsigned maxpropsize)
2557
0
{
2558
0
  struct property *prop;
2559
2560
0
  list_for_each_entry(prop, &node->properties, list)
2561
0
    __of_print_property(prop, 0, maxpropsize);
2562
0
}
2563
2564
static int __of_print_parents(struct device_node *node)
2565
0
{
2566
0
  int indent;
2567
2568
0
  if (!node->parent)
2569
0
    return 0;
2570
2571
0
  indent = __of_print_parents(node->parent);
2572
2573
0
  printf("%*s%s {\n", indent * 8, "", node->name);
2574
2575
0
  return indent + 1;
2576
0
}
2577
2578
static void of_print_parents(struct device_node *node, int *printed)
2579
0
{
2580
0
  if (*printed)
2581
0
    return;
2582
2583
0
  __of_print_parents(node);
2584
2585
0
  *printed = 1;
2586
0
}
2587
2588
static void of_print_close(struct device_node *node, int *printed)
2589
0
{
2590
0
  int depth = 0, i, j;
2591
2592
0
  if (!*printed)
2593
0
    return;
2594
2595
0
  while ((node = node->parent))
2596
0
    depth++;
2597
2598
0
  for (i = depth; i > 0; i--) {
2599
0
    for (j = 0; j + 1 < i; j++)
2600
0
      printf("\t");
2601
0
    printf("};\n");
2602
0
  }
2603
0
}
2604
2605
/**
2606
 * of_diff - compare two device trees against each other
2607
 * @a: The first device tree
2608
 * @b: The second device tree
2609
 * @indent: The initial indentation level when printing
2610
 *
2611
 * This function compares two device trees against each other and prints
2612
 * a diff-like result.
2613
 */
2614
int of_diff(struct device_node *a, struct device_node *b, int indent)
2615
0
{
2616
0
  struct property *ap, *bp;
2617
0
  struct device_node *ca, *cb;
2618
0
  int printed = 0, diff = 0;
2619
0
  bool silent = indent < 0;
2620
2621
0
  list_for_each_entry(ap, &a->properties, list) {
2622
0
    bp = of_find_property(b, ap->name, NULL);
2623
0
    if (!bp) {
2624
0
      diff++;
2625
0
      if (silent)
2626
0
        continue;
2627
0
      of_print_parents(a, &printed);
2628
0
      printf("- ");
2629
0
      __of_print_property(ap, indent, ~0);
2630
0
      continue;
2631
0
    }
2632
2633
0
    if (ap->length != bp->length || memcmp(of_property_get_value(ap), of_property_get_value(bp), bp->length)) {
2634
0
      diff++;
2635
0
      if (silent)
2636
0
        continue;
2637
0
      of_print_parents(a, &printed);
2638
0
      printf("- ");
2639
0
      __of_print_property(ap, indent, ~0);
2640
0
      printf("+ ");
2641
0
      __of_print_property(bp, indent, ~0);
2642
0
    }
2643
0
  }
2644
2645
0
  list_for_each_entry(bp, &b->properties, list) {
2646
0
    ap = of_find_property(a, bp->name, NULL);
2647
0
    if (!ap) {
2648
0
      diff++;
2649
0
      if (silent)
2650
0
        continue;
2651
0
      of_print_parents(a, &printed);
2652
0
      printf("+ ");
2653
0
      __of_print_property(bp, indent, ~0);
2654
0
    }
2655
0
  }
2656
2657
0
  for_each_child_of_node(a, ca) {
2658
0
    cb = of_get_child_by_name(b, ca->name);
2659
0
    if (cb) {
2660
0
      diff += of_diff(ca, cb, silent ? indent : indent + 1);
2661
0
    } else {
2662
0
      diff++;
2663
0
      if (silent)
2664
0
        continue;
2665
0
      of_print_parents(a, &printed);
2666
0
      __of_print_nodes(ca, indent, ~0, "- ");
2667
0
    }
2668
0
  }
2669
2670
0
  for_each_child_of_node(b, cb) {
2671
0
    if (!of_get_child_by_name(a, cb->name)) {
2672
0
      diff++;
2673
0
      if (silent)
2674
0
        continue;
2675
0
      of_print_parents(a, &printed);
2676
0
      __of_print_nodes(cb, indent, ~0, "+ ");
2677
0
    }
2678
0
  }
2679
2680
0
  if (!silent)
2681
0
    of_print_close(a, &printed);
2682
2683
0
  return diff;
2684
0
}
2685
2686
struct device_node *of_new_node(struct device_node *parent, const char *name)
2687
83.7k
{
2688
83.7k
  struct device_node *node;
2689
2690
83.7k
  node = xzalloc(sizeof(*node));
2691
83.7k
  node->parent = parent;
2692
83.7k
  if (parent)
2693
75.3k
    list_add_tail(&node->parent_list, &parent->children);
2694
2695
83.7k
  INIT_LIST_HEAD(&node->children);
2696
83.7k
  INIT_LIST_HEAD(&node->properties);
2697
2698
83.7k
  if (parent) {
2699
75.3k
    node->name = xstrdup_const(name);
2700
75.3k
    node->full_name = basprintf("%pOF/%s",
2701
75.3k
                node->parent, name);
2702
75.3k
    list_add(&node->list, &parent->list);
2703
75.3k
  } else {
2704
8.45k
    node->name = xstrdup_const("");
2705
8.45k
    node->full_name = xstrdup("");
2706
8.45k
    INIT_LIST_HEAD(&node->list);
2707
8.45k
  }
2708
2709
83.7k
  return node;
2710
83.7k
}
2711
2712
struct property *__of_new_property(struct device_node *node, const char *name,
2713
           void *data, int len)
2714
45.0k
{
2715
45.0k
  struct property *prop;
2716
2717
45.0k
  prop = xzalloc(sizeof(*prop));
2718
45.0k
  prop->name = xstrdup_const(name);
2719
45.0k
  prop->length = len;
2720
45.0k
  prop->value = data;
2721
2722
45.0k
  list_add_tail(&prop->list, &node->properties);
2723
2724
45.0k
  return prop;
2725
45.0k
}
2726
2727
/**
2728
 * of_new_property - Add a new property to a node
2729
 * @node: device node to which the property is added
2730
 * @name: Name of the new property
2731
 * @data: Value of the property (can be NULL)
2732
 * @len:  Length of the value
2733
 *
2734
 * This adds a new property to a device node. @data is copied and no longer needed
2735
 * after calling this function.
2736
 *
2737
 * Return: A pointer to the new property
2738
 */
2739
struct property *of_new_property(struct device_node *node, const char *name,
2740
    const void *data, int len)
2741
31.5k
{
2742
31.5k
  char *buf;
2743
2744
31.5k
  buf = xzalloc(len);
2745
31.5k
  if (data)
2746
31.5k
    memcpy(buf, data, len);
2747
2748
31.5k
  return __of_new_property(node, name, buf, len);
2749
31.5k
}
2750
2751
/**
2752
 * of_new_property_const - Add a new property to a node
2753
 * @node: device node to which the property is added
2754
 * @name: Name of the new property
2755
 * @data: Value of the property (can be NULL)
2756
 * @len:  Length of the value
2757
 *
2758
 * This adds a new property to a device node. @data is used directly in the
2759
 * property and must be valid until the property is deleted again or set to
2760
 * another value. Normally you shouldn't use this function, use of_new_property()
2761
 * instead.
2762
 *
2763
 * Return: A pointer to the new property
2764
 */
2765
struct property *of_new_property_const(struct device_node *node, const char *name,
2766
    const void *data, int len)
2767
68.3k
{
2768
68.3k
  struct property *prop;
2769
2770
68.3k
  prop = xzalloc(sizeof(*prop));
2771
68.3k
  prop->name = xstrdup(name);
2772
68.3k
  prop->length = len;
2773
68.3k
  prop->value_const = data;
2774
2775
68.3k
  list_add_tail(&prop->list, &node->properties);
2776
2777
68.3k
  return prop;
2778
68.3k
}
2779
2780
void of_delete_property(struct property *pp)
2781
122k
{
2782
122k
  if (!pp)
2783
13.0k
    return;
2784
2785
109k
  list_del(&pp->list);
2786
2787
109k
  free_const(pp->name);
2788
109k
  free(pp->value);
2789
109k
  free(pp);
2790
109k
}
2791
2792
struct property *of_rename_property(struct device_node *np,
2793
            const char *old_name, const char *new_name)
2794
0
{
2795
0
  struct property *pp;
2796
2797
0
  pp = of_find_property(np, old_name, NULL);
2798
0
  if (!pp)
2799
0
    return NULL;
2800
2801
0
  of_property_write_bool(np, new_name, false);
2802
2803
0
  free_const(pp->name);
2804
0
  pp->name = xstrdup(new_name);
2805
0
  return pp;
2806
0
}
2807
2808
struct property *of_copy_property(const struct device_node *src,
2809
          const char *propname,
2810
          struct device_node *dst)
2811
0
{
2812
0
  struct property *prop;
2813
2814
0
  prop = of_find_property(src, propname, NULL);
2815
0
  if (!prop)
2816
0
    return NULL;
2817
2818
0
  if (of_property_present(dst, propname))
2819
0
    return ERR_PTR(-EEXIST);
2820
2821
0
  return of_new_property(dst, propname,
2822
0
             of_property_get_value(prop), prop->length);
2823
0
}
2824
EXPORT_SYMBOL_GPL(of_copy_property);
2825
2826
2827
/**
2828
 * of_set_property - create a property for a given node
2829
 * @node - the node
2830
 * @name - the name of the property
2831
 * @val - the value for the property
2832
 * @len - the length of the properties value
2833
 * @create - if true, the property is created if not existing already
2834
 */
2835
int of_set_property(struct device_node *np, const char *name, const void *val, int len,
2836
    int create)
2837
64
{
2838
64
  struct property *pp = of_find_property(np, name, NULL);
2839
2840
64
  if (!np)
2841
0
    return -ENOENT;
2842
2843
64
  if (!pp && !create)
2844
0
    return -ENOENT;
2845
2846
64
  of_delete_property(pp);
2847
2848
64
  pp = of_new_property(np, name, val, len);
2849
64
  if (!pp)
2850
0
    return -ENOMEM;
2851
2852
64
  return 0;
2853
64
}
2854
2855
int of_append_property(struct device_node *np, const char *name, const void *val, int len)
2856
437
{
2857
437
  struct property *pp;
2858
437
  int orig_len;
2859
437
  void *buf;
2860
2861
437
  if (!np)
2862
0
    return -ENOENT;
2863
2864
437
  pp = of_find_property(np, name, NULL);
2865
437
  if (!pp) {
2866
437
    of_new_property(np, name, val, len);
2867
437
    return 0;
2868
437
  }
2869
2870
0
  orig_len = pp->length;
2871
0
  buf = realloc(pp->value, orig_len + len);
2872
0
  if (!buf)
2873
0
    return -ENOMEM;
2874
2875
0
  memcpy(buf + orig_len, val, len);
2876
2877
0
  pp->value = buf;
2878
0
  pp->length += len;
2879
2880
0
  if (pp->value_const) {
2881
0
    memcpy(buf, pp->value_const, orig_len);
2882
0
    pp->value_const = NULL;
2883
0
  }
2884
2885
0
  return 0;
2886
0
}
2887
2888
int of_prepend_property(struct device_node *np, const char *name, const void *val, int len)
2889
0
{
2890
0
  struct property *pp;
2891
0
  const void *oldval;
2892
0
  void *buf;
2893
0
  int oldlen;
2894
2895
0
  pp = of_find_property(np, name, &oldlen);
2896
0
  if (!pp) {
2897
0
    of_new_property(np, name, val, len);
2898
0
    return 0;
2899
0
  }
2900
2901
0
  oldval = of_property_get_value(pp);
2902
2903
0
  buf = malloc(len + oldlen);
2904
0
  if (!buf)
2905
0
    return -ENOMEM;
2906
2907
0
  memcpy(buf, val, len);
2908
0
  memcpy(buf + len, oldval, oldlen);
2909
2910
0
  free(pp->value);
2911
0
  pp->value = buf;
2912
0
  pp->length = len + oldlen;
2913
0
  pp->value_const = NULL;
2914
2915
0
  return 0;
2916
0
}
2917
2918
int of_property_sprintf(struct device_node *np,
2919
      const char *propname, const char *fmt, ...)
2920
13.0k
{
2921
13.0k
  struct property *pp;
2922
13.0k
  struct va_format vaf;
2923
13.0k
  char *buf = NULL;
2924
13.0k
  va_list args;
2925
13.0k
  int len;
2926
2927
13.0k
  if (!np)
2928
0
    return -ENOENT;
2929
2930
13.0k
  va_start(args, fmt);
2931
13.0k
  vaf.fmt = fmt;
2932
13.0k
  vaf.va = &args;
2933
13.0k
  len = asprintf(&buf, "%pV", &vaf);
2934
13.0k
  va_end(args);
2935
2936
13.0k
  if (len < 0)
2937
0
    return -ENOMEM;
2938
2939
13.0k
  len++; /* trailing NUL */
2940
2941
13.0k
  pp = of_find_property(np, propname, NULL);
2942
13.0k
  of_delete_property(pp);
2943
2944
13.0k
  __of_new_property(np, propname, buf, len);
2945
13.0k
  return len;
2946
13.0k
}
2947
2948
static int mem_bank_num;
2949
2950
int of_add_memory(struct device_node *node, bool dump)
2951
32
{
2952
32
  struct resource res;
2953
32
  int n = 0, ret = 0;
2954
2955
32
  if (!of_node_is_type(node, "memory"))
2956
0
    return -ENXIO;
2957
2958
64
  while (!of_address_to_resource(node, n, &res)) {
2959
32
    int err;
2960
32
    n++;
2961
32
    if (!resource_size(&res))
2962
32
      continue;
2963
2964
0
    if (!of_device_is_available(node))
2965
0
      continue;
2966
2967
0
    err = of_add_memory_bank(node, dump, mem_bank_num,
2968
0
        res.start, resource_size(&res));
2969
0
    if (err)
2970
0
      ret = err;
2971
2972
0
    mem_bank_num++;
2973
0
  }
2974
2975
32
  return ret;
2976
32
}
2977
2978
const char *of_get_model(void)
2979
0
{
2980
0
  return of_model;
2981
0
}
2982
2983
const struct of_device_id of_default_bus_match_table[] = {
2984
  {
2985
    .compatible = "simple-bus",
2986
  }, {
2987
#ifndef CONFIG_OF_SIMPLE_PM_BUS
2988
    .compatible = "simple-pm-bus",
2989
  }, {
2990
#endif
2991
    .compatible = "simple-mfd",
2992
  }, {
2993
    /* sentinel */
2994
  }
2995
};
2996
MODULE_DEVICE_TABLE(of, of_default_bus_match_table);
2997
2998
static int of_probe_memory(void)
2999
32
{
3000
32
  struct device_node *memory = root_node;
3001
32
  int ret = 0;
3002
3003
32
  if (!IS_ENABLED(CONFIG_OFDEVICE))
3004
0
    return 0;
3005
3006
  /* Parse all available node with "memory" device_type */
3007
64
  while (1) {
3008
64
    int err;
3009
3010
64
    memory = of_find_node_by_type(memory, "memory");
3011
64
    if (!memory)
3012
32
      break;
3013
3014
32
    err = of_add_memory(memory, false);
3015
32
    if (err)
3016
0
      ret = err;
3017
32
  }
3018
3019
32
  return ret;
3020
32
}
3021
mem_initcall(of_probe_memory);
3022
3023
struct device *of_platform_root_device;
3024
3025
static void of_platform_device_create_root(struct device_node *np)
3026
32
{
3027
32
  struct device *dev;
3028
32
  int ret;
3029
3030
32
  if (of_platform_root_device)
3031
0
    return;
3032
3033
32
  dev = xzalloc(sizeof(*dev));
3034
32
  dev->id = DEVICE_ID_SINGLE;
3035
32
  dev->of_node = np;
3036
32
  dev_set_name(dev, "machine");
3037
3038
32
  ret = platform_device_register(dev);
3039
32
  if (WARN_ON(ret)) {
3040
0
    free_device(dev);
3041
0
    return;
3042
0
  }
3043
3044
32
  of_platform_root_device = dev;
3045
32
}
3046
3047
static const struct of_device_id reserved_mem_matches[] = {
3048
  { .compatible = "ramoops" },
3049
  { .compatible = "nvmem-rmem" },
3050
  {}
3051
};
3052
MODULE_DEVICE_TABLE(of, reserved_mem_matches);
3053
3054
/**
3055
 * of_probe - Probe unflattened device tree starting at of_get_root_node
3056
 *
3057
 * The function walks the device tree and creates devices as needed.
3058
 * With care, it can be called more than once, but if you really need that,
3059
 * consider first if deep probe would help instead.
3060
 */
3061
int of_probe(void)
3062
32
{
3063
32
  struct device_node *node;
3064
3065
32
  if(!root_node)
3066
0
    return -ENODEV;
3067
3068
  /*
3069
   * We do this first thing, so board drivers can patch the device
3070
   * tree prior to device creation if needed.
3071
   */
3072
32
  of_platform_device_create_root(root_node);
3073
3074
  /*
3075
   * Handle certain compatibles explicitly, since we don't want to create
3076
   * platform_devices for every node in /reserved-memory with a
3077
   * "compatible",
3078
   */
3079
32
  for_each_matching_node(node, reserved_mem_matches)
3080
0
    of_platform_device_create(node, NULL);
3081
3082
32
  node = of_find_node_by_path("/firmware");
3083
32
  if (node)
3084
0
    of_platform_populate(node, of_default_bus_match_table, NULL);
3085
3086
32
  of_platform_populate(root_node, of_default_bus_match_table, NULL);
3087
3088
32
  return 0;
3089
32
}
3090
3091
/**
3092
 * of_create_node - create a new node including its parents
3093
 * @path - the nodepath to create
3094
 */
3095
struct device_node *of_create_node(struct device_node *root, const char *path)
3096
0
{
3097
0
  char *slash, *p, *freep;
3098
0
  struct device_node *tmp, *dn = root;
3099
3100
0
  if (*path != '/')
3101
0
    return NULL;
3102
3103
0
  path++;
3104
3105
0
  p = freep = xstrdup(path);
3106
3107
0
  while (1) {
3108
0
    if (!*p)
3109
0
      goto out;
3110
3111
0
    slash = strchr(p, '/');
3112
0
    if (slash)
3113
0
      *slash = 0;
3114
3115
0
    tmp = of_get_child_by_name(dn, p);
3116
0
    if (tmp)
3117
0
      dn = tmp;
3118
0
    else
3119
0
      dn = of_new_node(dn, p);
3120
3121
0
    if (!dn)
3122
0
      goto out;
3123
3124
0
    if (!slash)
3125
0
      goto out;
3126
3127
0
    p = slash + 1;
3128
0
  }
3129
0
out:
3130
0
  free(freep);
3131
3132
0
  return dn;
3133
0
}
3134
3135
void of_merge_nodes(struct device_node *np, const struct device_node *other)
3136
6.37k
{
3137
6.37k
  struct device_node *child;
3138
6.37k
  struct property *pp;
3139
3140
6.37k
  list_for_each_entry(pp, &other->properties, list)
3141
2.82k
    of_new_property(np, pp->name, pp->value, pp->length);
3142
3143
6.37k
  for_each_child_of_node(other, child)
3144
6.04k
    of_copy_node(np, child);
3145
6.37k
}
3146
3147
struct device_node *of_copy_node(struct device_node *parent, const struct device_node *other)
3148
6.37k
{
3149
6.37k
  struct device_node *np;
3150
3151
6.37k
  np = of_new_node(parent, other->name);
3152
6.37k
  np->phandle = other->phandle;
3153
3154
6.37k
  of_merge_nodes(np, other);
3155
3156
6.37k
  return np;
3157
6.37k
}
3158
3159
struct device_node *of_dup(const struct device_node *root)
3160
0
{
3161
0
  if (IS_ERR_OR_NULL(root))
3162
0
    return ERR_CAST(root);
3163
3164
0
  return of_copy_node(NULL, root);
3165
0
}
3166
3167
void of_delete_node(struct device_node *node)
3168
82.8k
{
3169
82.8k
  struct device_node *n, *nt;
3170
82.8k
  struct property *p, *pt;
3171
3172
82.8k
  if (!node)
3173
0
    return;
3174
3175
82.8k
  if (node == root_node) {
3176
0
    pr_err("Won't delete root device node\n");
3177
0
    return;
3178
0
  }
3179
3180
82.8k
  list_for_each_entry_safe(p, pt, &node->properties, list)
3181
109k
    of_delete_property(p);
3182
3183
82.8k
  list_for_each_entry_safe(n, nt, &node->children, parent_list)
3184
73.7k
    of_delete_node(n);
3185
3186
82.8k
  if (node->parent) {
3187
74.4k
    list_del(&node->parent_list);
3188
74.4k
    list_del(&node->list);
3189
74.4k
  }
3190
3191
82.8k
  free_const(node->name);
3192
82.8k
  free(node->full_name);
3193
82.8k
  free(node);
3194
82.8k
}
3195
3196
/*
3197
 * of_find_node_by_chosen - Find a node given a chosen property pointing at it
3198
 * @propname:   the name of the property containing a path or alias
3199
 *              The function will lookup the first string in the property
3200
 *              value up to the first : character or till \0.
3201
 * @options     The Remainder (without : or \0 at the end) will be written
3202
 *              to *options if not NULL.
3203
 */
3204
struct device_node *of_find_node_by_chosen(const char *propname,
3205
             const char **options)
3206
64
{
3207
64
  const char *value, *p;
3208
64
  char *buf;
3209
64
  struct device_node *dn;
3210
3211
64
  value = of_get_property(of_chosen, propname, NULL);
3212
64
  if (!value)
3213
64
    return NULL;
3214
3215
0
  p = strchrnul(value, ':');
3216
0
  buf = xstrndup(value, p - value);
3217
3218
0
  dn = of_find_node_by_path_or_alias(NULL, buf);
3219
3220
0
  free(buf);
3221
3222
0
  if (options && *p)
3223
0
    *options = p + 1;
3224
3225
0
  return dn;
3226
64
}
3227
3228
struct device_node *of_get_stdoutpath(unsigned int *baudrate)
3229
32
{
3230
32
  const char *opts = NULL;
3231
32
  struct device_node *dn;
3232
3233
32
  dn = of_find_node_by_chosen("stdout-path", &opts);
3234
32
  if (!dn)
3235
32
    dn = of_find_node_by_chosen("linux,stdout-path", &opts);
3236
32
  if (!dn)
3237
32
    return NULL;
3238
3239
0
  if (baudrate && opts) {
3240
0
    unsigned rate = simple_strtoul(opts, NULL, 10);
3241
0
    if (rate)
3242
0
      *baudrate = rate;
3243
0
  }
3244
3245
0
  return dn;
3246
32
}
3247
3248
int of_device_is_stdout_path(struct device *dev, unsigned int *baudrate)
3249
32
{
3250
32
  unsigned int tmp = *baudrate;
3251
3252
32
  if (!dev || !dev->of_node || dev->of_node != of_get_stdoutpath(&tmp))
3253
32
    return false;
3254
3255
0
  *baudrate = tmp;
3256
0
  return true;
3257
32
}
3258
3259
/**
3260
 * of_add_initrd - add initrd properties to the devicetree
3261
 * @root - the root node of the tree
3262
 * @start - physical start address of the initrd image
3263
 * @end - physical end address of the initrd image
3264
 *
3265
 * Add initrd properties to the devicetree, or, if end is 0,
3266
 * delete them.
3267
 *
3268
 * Note that Linux interprets end differently than barebox. For Linux end points
3269
 * to the first address after the memory occupied by the image while barebox
3270
 * lets end pointing to the last occupied byte.
3271
 */
3272
int of_add_initrd(struct device_node *root, resource_size_t start,
3273
    resource_size_t end)
3274
0
{
3275
0
  struct device_node *chosen;
3276
0
  __be32 buf[2];
3277
3278
0
  chosen = of_create_node(root, "/chosen");
3279
0
  if (!chosen)
3280
0
    return -ENOMEM;
3281
3282
0
  if (end) {
3283
0
    of_write_number(buf, start, 2);
3284
0
    of_set_property(chosen, "linux,initrd-start", buf, 8, 1);
3285
0
    of_write_number(buf, end + 1, 2);
3286
0
    of_set_property(chosen, "linux,initrd-end", buf, 8, 1);
3287
0
  } else {
3288
0
    struct property *pp;
3289
3290
0
    pp = of_find_property(chosen, "linux,initrd-start", NULL);
3291
0
    if (pp)
3292
0
      of_delete_property(pp);
3293
3294
0
    pp = of_find_property(chosen, "linux,initrd-end", NULL);
3295
0
    if (pp)
3296
0
      of_delete_property(pp);
3297
0
  }
3298
3299
0
  return 0;
3300
0
}
3301
3302
/**
3303
 * of_device_enable - enable a devicenode device
3304
 * @node - the node to enable
3305
 *
3306
 * This deletes the status property of a devicenode effectively
3307
 * enabling the device.
3308
 */
3309
int of_device_enable(struct device_node *node)
3310
0
{
3311
0
  struct property *pp;
3312
3313
0
  pp = of_find_property(node, "status", NULL);
3314
0
  if (!pp)
3315
0
    return 0;
3316
3317
0
  of_delete_property(pp);
3318
3319
0
  return 0;
3320
0
}
3321
3322
/**
3323
 * of_device_enable_path - enable a devicenode
3324
 * @path - the nodepath to enable
3325
 *
3326
 * wrapper around of_device_enable taking the nodepath as argument
3327
 */
3328
int of_device_enable_path(const char *path)
3329
0
{
3330
0
  struct device_node *node;
3331
3332
0
  node = of_find_node_by_path(path);
3333
0
  if (!node)
3334
0
    return -ENODEV;
3335
3336
0
  return of_device_enable(node);
3337
0
}
3338
3339
/**
3340
 * of_device_enable_by_alias - enable a device node by alias
3341
 * @alias - the alias of the device tree node to enable
3342
 */
3343
int of_device_enable_by_alias(const char *alias)
3344
0
{
3345
0
  struct device_node *node;
3346
3347
0
  node = of_find_node_by_alias(NULL, alias);
3348
0
  if (!node)
3349
0
    return -ENODEV;
3350
3351
0
  return of_device_enable(node);
3352
0
}
3353
3354
/**
3355
 * of_device_disable - disable a devicenode device
3356
 * @node - the node to disable
3357
 *
3358
 * This sets the status of a devicenode to "disabled"
3359
 */
3360
int of_device_disable(struct device_node *node)
3361
0
{
3362
0
  return of_property_write_string(node, "status", "disabled");
3363
0
}
3364
3365
/**
3366
 * of_device_disable_path - disable a devicenode
3367
 * @path - the nodepath to disable
3368
 *
3369
 * wrapper around of_device_disable taking the nodepath as argument
3370
 */
3371
int of_device_disable_path(const char *path)
3372
0
{
3373
0
  struct device_node *node;
3374
3375
0
  node = of_find_node_by_path(path);
3376
0
  if (!node)
3377
0
    return -ENODEV;
3378
3379
0
  return of_device_disable(node);
3380
0
}
3381
3382
/**
3383
 * of_device_disable_by_alias - disable a devicenode by alias
3384
 * @alias - the alias of the device tree node to disable
3385
 */
3386
int of_device_disable_by_alias(const char *alias)
3387
0
{
3388
0
  struct device_node *node;
3389
3390
0
  node = of_find_node_by_alias(NULL, alias);
3391
0
  if (!node)
3392
0
    return -ENODEV;
3393
3394
0
  return of_device_disable(node);
3395
0
}
3396
3397
/**
3398
 * of_read_file - unflatten oftree file
3399
 * @filename - path to file to unflatten its contents
3400
 *
3401
 * Returns the root node of the tree or an error pointer on error.
3402
 */
3403
struct device_node *of_read_file(const char *filename)
3404
0
{
3405
0
  void *fdt;
3406
0
  size_t size;
3407
0
  struct device_node *root;
3408
3409
0
  fdt = read_file(filename, &size);
3410
0
  if (!fdt) {
3411
0
    pr_err("unable to read %s: %m\n", filename);
3412
0
    return ERR_PTR(-errno);
3413
0
  }
3414
3415
0
  if (IS_ENABLED(CONFIG_FILETYPE) && file_detect_type(fdt, size) != filetype_oftree) {
3416
0
    pr_err("%s is not a flat device tree file.\n", filename);
3417
0
    root = ERR_PTR(-EINVAL);
3418
0
    goto out;
3419
0
  }
3420
3421
0
  root = of_unflatten_dtb(fdt, size);
3422
0
out:
3423
0
  free(fdt);
3424
3425
0
  return root;
3426
0
}
3427
3428
/**
3429
 * of_get_reproducible_name() - get a reproducible name of a node
3430
 * @node: The node to get a name from
3431
 *
3432
 * This function constructs a reproducible name for a node. This name can be
3433
 * used to find the same node in another device tree. The name is constructed
3434
 * from different patterns which are appended to each other.
3435
 * - If a node has no "reg" property, the name of the node is used in angle
3436
 *   brackets, prepended with the result of the parent node
3437
 * - If the parent node has a "ranges" property then the address in MMIO space
3438
 *   is used in square brackets
3439
 * - If a node has a "reg" property, but is not translatable in MMIO space then
3440
 *   the start address is used in curly brackets, prepended with the result of
3441
 *   the parent node.
3442
 *
3443
 * Returns a dynamically allocated string containing the name
3444
 */
3445
char *of_get_reproducible_name(struct device_node *node)
3446
128
{
3447
128
  const __be32 *reg;
3448
128
  u64 addr;
3449
128
  u64 offset;
3450
128
  int na;
3451
128
  char *str, *res;
3452
3453
128
  if (!node)
3454
32
    return 0;
3455
3456
96
  reg = of_get_property(node, "reg", NULL);
3457
96
        if (!reg) {
3458
32
    str = of_get_reproducible_name(node->parent);
3459
32
    res = basprintf("%s<%s>", str, node->name);
3460
32
    free(str);
3461
32
    return res;
3462
32
  }
3463
3464
64
  if (node->parent && of_get_property(node->parent, "ranges", NULL)) {
3465
0
    addr = of_translate_address(node, reg);
3466
0
    if (addr != OF_BAD_ADDR)
3467
0
      return basprintf("[0x%llx]", addr);
3468
    /*
3469
     * Untranslatable - e.g. a PCI config-space address whose
3470
     * tag (space=0) doesn't appear in the parent's ranges.
3471
     * Fall through to the parent-prefixed encoding so distinct
3472
     * untranslatable nodes don't all collide on OF_BAD_ADDR.
3473
     */
3474
0
  }
3475
3476
64
  na = of_n_addr_cells(node);
3477
3478
  /*
3479
   * Special workaround for the of partition binding. In the old binding
3480
   * the partitions are directly under the hardware devicenode whereas in
3481
   * the new binding the partitions are in an extra subnode with
3482
   * "fixed-partitions" compatible. We skip this extra subnode from the
3483
   * reproducible name to get the same name for both bindings.
3484
   */
3485
64
  if (node->parent && of_node_is_fixed_partitions(node->parent))
3486
32
    node = node->parent;
3487
3488
64
  offset = of_read_number(reg, na);
3489
3490
64
  str = of_get_reproducible_name(node->parent);
3491
64
  res = basprintf("%s{%llx}", str, offset);
3492
64
  free(str);
3493
3494
64
  return res;
3495
64
}
3496
3497
struct device_node *of_find_node_by_reproducible_name(struct device_node *from,
3498
                  const char *name)
3499
0
{
3500
0
  struct device_node *np;
3501
3502
0
  of_tree_for_each_node_from(np, from) {
3503
0
    char *rep = of_get_reproducible_name(np);
3504
0
    int res;
3505
3506
0
    res = of_node_cmp(rep, name);
3507
3508
0
    free(rep);
3509
3510
0
    if (!res)
3511
0
      return np;
3512
0
  }
3513
0
  return NULL;
3514
0
}
3515
3516
struct device_node *of_get_node_by_reproducible_name(struct device_node *dstroot,
3517
                 struct device_node *srcnp)
3518
0
{
3519
0
  struct device_node *dstnp;
3520
0
  char *name;
3521
3522
0
  name = of_get_reproducible_name(srcnp);
3523
0
  dstnp = of_find_node_by_reproducible_name(dstroot, name);
3524
0
  free(name);
3525
3526
0
  return dstnp;
3527
0
}
3528
3529
/**
3530
 * of_graph_parse_endpoint() - parse common endpoint node properties
3531
 * @node: pointer to endpoint device_node
3532
 * @endpoint: pointer to the OF endpoint data structure
3533
 */
3534
int of_graph_parse_endpoint(const struct device_node *node,
3535
          struct of_endpoint *endpoint)
3536
0
{
3537
0
  struct device_node *port_node = of_get_parent(node);
3538
3539
0
  if (!port_node)
3540
0
    pr_warn("%s(): endpoint %pOF has no parent node\n",
3541
0
      __func__, node);
3542
3543
0
  memset(endpoint, 0, sizeof(*endpoint));
3544
3545
0
  endpoint->local_node = node;
3546
  /*
3547
   * It doesn't matter whether the two calls below succeed.
3548
   * If they don't then the default value 0 is used.
3549
   */
3550
0
  of_property_read_u32(port_node, "reg", &endpoint->port);
3551
0
  of_property_read_u32(node, "reg", &endpoint->id);
3552
3553
0
  return 0;
3554
0
}
3555
EXPORT_SYMBOL(of_graph_parse_endpoint);
3556
3557
/**
3558
 * of_graph_get_port_by_id() - get the port matching a given id
3559
 * @parent: pointer to the parent device node
3560
 * @id: id of the port
3561
 *
3562
 * Return: A 'port' node pointer with refcount incremented.
3563
 */
3564
struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
3565
0
{
3566
0
  struct device_node *port, *node = of_get_child_by_name(parent, "ports");
3567
3568
0
  if (node)
3569
0
    parent = node;
3570
3571
0
  for_each_child_of_node(parent, port) {
3572
0
    u32 port_id = 0;
3573
3574
0
    if (strncmp(port->name, "port", 4) != 0)
3575
0
      continue;
3576
0
    of_property_read_u32(port, "reg", &port_id);
3577
0
    if (id == port_id)
3578
0
      return port;
3579
0
  }
3580
3581
0
  return NULL;
3582
0
}
3583
EXPORT_SYMBOL(of_graph_get_port_by_id);
3584
3585
/**
3586
 * of_graph_get_next_endpoint() - get next endpoint node
3587
 * @parent: pointer to the parent device node
3588
 * @prev: previous endpoint node, or NULL to get first
3589
 *
3590
 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
3591
 * of the passed @prev node is decremented.
3592
 */
3593
struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
3594
          struct device_node *prev)
3595
0
{
3596
0
  struct device_node *endpoint;
3597
0
  struct device_node *port;
3598
3599
0
  if (!parent)
3600
0
    return NULL;
3601
3602
  /*
3603
   * Start by locating the port node. If no previous endpoint is specified
3604
   * search for the first port node, otherwise get the previous endpoint
3605
   * parent port node.
3606
   */
3607
0
  if (!prev) {
3608
0
    struct device_node *node;
3609
3610
0
    node = of_get_child_by_name(parent, "ports");
3611
0
    if (node)
3612
0
      parent = node;
3613
3614
0
    port = of_get_child_by_name_stem(parent, "port");
3615
0
    if (!port) {
3616
0
      pr_err("%s(): no port node found in %pOF\n",
3617
0
             __func__, parent);
3618
0
      return NULL;
3619
0
    }
3620
0
  } else {
3621
0
    port = of_get_parent(prev);
3622
0
    if (!port) {
3623
0
      pr_warn("%s(): endpoint %pOF has no parent node\n",
3624
0
            __func__, prev);
3625
0
      return NULL;
3626
0
    }
3627
0
  }
3628
3629
0
  while (1) {
3630
    /*
3631
     * Now that we have a port node, get the next endpoint by
3632
     * getting the next child. If the previous endpoint is NULL this
3633
     * will return the first child.
3634
     */
3635
0
    endpoint = of_get_next_child(port, prev);
3636
0
    if (endpoint)
3637
0
      return endpoint;
3638
3639
    /* No more endpoints under this port, try the next one. */
3640
0
    prev = NULL;
3641
3642
0
    do {
3643
0
      port = of_get_next_child(parent, port);
3644
0
      if (!port)
3645
0
        return NULL;
3646
0
    } while (port->name && !of_node_name_eq(port, "port"));
3647
0
  }
3648
0
}
3649
EXPORT_SYMBOL(of_graph_get_next_endpoint);
3650
3651
/**
3652
 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
3653
 * @parent: pointer to the parent device node
3654
 * @port_reg: identifier (value of reg property) of the parent port node
3655
 * @reg: identifier (value of reg property) of the endpoint node
3656
 *
3657
 * Return: An 'endpoint' node pointer which is identified by reg and at the same
3658
 * is the child of a port node identified by port_reg. reg and port_reg are
3659
 * ignored when they are -1. Use of_node_put() on the pointer when done.
3660
 */
3661
struct device_node *of_graph_get_endpoint_by_regs(
3662
  const struct device_node *parent, int port_reg, int reg)
3663
0
{
3664
0
  struct of_endpoint endpoint;
3665
0
  struct device_node *node = NULL;
3666
3667
0
  for_each_endpoint_of_node(parent, node) {
3668
0
    of_graph_parse_endpoint(node, &endpoint);
3669
0
    if (((port_reg == -1) || (endpoint.port == port_reg)) &&
3670
0
      ((reg == -1) || (endpoint.id == reg)))
3671
0
      return node;
3672
0
  }
3673
0
  return NULL;
3674
0
}
3675
EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
3676
3677
/**
3678
 * of_graph_get_remote_port_parent() - get remote port's parent node
3679
 * @node: pointer to a local endpoint device_node
3680
 *
3681
 * Return: Remote device node associated with remote endpoint node linked
3682
 *     to @node.
3683
 */
3684
struct device_node *of_graph_get_remote_port_parent(
3685
             const struct device_node *node)
3686
0
{
3687
0
  struct device_node *np;
3688
0
  unsigned int depth;
3689
3690
  /* Get remote endpoint node. */
3691
0
  np = of_parse_phandle(node, "remote-endpoint", 0);
3692
3693
  /* Walk 3 levels up only if there is 'ports' node. */
3694
0
  for (depth = 3; depth && np; depth--) {
3695
0
    np = np->parent;
3696
0
    if (depth == 2 && !of_node_name_eq(np, "ports"))
3697
0
      break;
3698
0
  }
3699
0
  return np;
3700
0
}
3701
EXPORT_SYMBOL(of_graph_get_remote_port_parent);
3702
3703
/**
3704
 * of_graph_get_remote_port() - get remote port node
3705
 * @node: pointer to a local endpoint device_node
3706
 *
3707
 * Return: Remote port node associated with remote endpoint node linked
3708
 *     to @node.
3709
 */
3710
struct device_node *of_graph_get_remote_port(const struct device_node *node)
3711
0
{
3712
0
  struct device_node *np;
3713
3714
  /* Get remote endpoint node. */
3715
0
  np = of_parse_phandle(node, "remote-endpoint", 0);
3716
0
  if (!np)
3717
0
    return NULL;
3718
0
  return np->parent;
3719
0
}
3720
EXPORT_SYMBOL(of_graph_get_remote_port);
3721
3722
/**
3723
 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
3724
 * @node: pointer to parent device_node containing graph port/endpoint
3725
 * @port: identifier (value of reg property) of the parent port node
3726
 * @endpoint: identifier (value of reg property) of the endpoint node
3727
 *
3728
 * Return: Remote device node associated with remote endpoint node linked
3729
 * to @node. Use of_node_put() on it when done.
3730
 */
3731
struct device_node *of_graph_get_remote_node(const struct device_node *node,
3732
               u32 port, u32 endpoint)
3733
0
{
3734
0
  struct device_node *endpoint_node, *remote;
3735
3736
0
  endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
3737
0
  if (!endpoint_node) {
3738
0
    pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
3739
0
       port, endpoint, node);
3740
0
    return NULL;
3741
0
  }
3742
3743
0
  remote = of_graph_get_remote_port_parent(endpoint_node);
3744
0
  of_node_put(endpoint_node);
3745
0
  if (!remote) {
3746
0
    pr_debug("no valid remote node\n");
3747
0
    return NULL;
3748
0
  }
3749
3750
0
  if (!of_device_is_available(remote)) {
3751
0
    pr_debug("not available for remote node\n");
3752
0
    of_node_put(remote);
3753
0
    return NULL;
3754
0
  }
3755
3756
0
  return remote;
3757
0
}
3758
EXPORT_SYMBOL(of_graph_get_remote_node);
3759
3760
int of_graph_port_is_available(struct device_node *node)
3761
0
{
3762
0
  struct device_node *endpoint;
3763
0
  int available = 0;
3764
3765
0
  for_each_child_of_node(node, endpoint) {
3766
0
    struct device_node *remote_parent;
3767
3768
0
    remote_parent = of_graph_get_remote_port_parent(endpoint);
3769
0
    if (!remote_parent)
3770
0
      continue;
3771
3772
0
    if (!of_device_is_available(remote_parent))
3773
0
      continue;
3774
3775
0
    available = 1;
3776
0
  }
3777
3778
0
  return available;
3779
0
}
3780
EXPORT_SYMBOL(of_graph_port_is_available);
3781
3782
/**
3783
 * of_alias_from_compatible - Lookup appropriate alias for a device node
3784
 *            depending on compatible
3785
 * @node: pointer to a device tree node
3786
 * @alias:  Pointer to buffer that alias value will be copied into
3787
 * @len:  Length of alias value
3788
 *
3789
 * Based on the value of the compatible property, this routine will attempt
3790
 * to choose an appropriate alias value for a particular device tree node.
3791
 * It does this by stripping the manufacturer prefix (as delimited by a ',')
3792
 * from the first entry in the compatible list property.
3793
 *
3794
 * Note: The matching on just the "product" side of the compatible is a relic
3795
 * from I2C and SPI. Please do not add any new user.
3796
 *
3797
 * Return: This routine returns 0 on success, <0 on failure.
3798
 */
3799
int of_alias_from_compatible(const struct device_node *node, char *alias, int len)
3800
0
{
3801
0
  const char *compatible, *p;
3802
0
  int cplen;
3803
3804
0
  compatible = of_get_property(node, "compatible", &cplen);
3805
0
  if (!compatible || strlen(compatible) > cplen)
3806
0
    return -ENODEV;
3807
0
  p = strchr(compatible, ',');
3808
0
  strscpy(alias, p ? p + 1 : compatible, len);
3809
0
  return 0;
3810
0
}
3811
EXPORT_SYMBOL_GPL(of_alias_from_compatible);
3812
3813
/**
3814
 * of_get_machine_compatible - get first compatible string from the root node.
3815
 *
3816
 * Returns the string or NULL.
3817
 */
3818
const char *of_get_machine_compatible(void)
3819
32
{
3820
32
  struct property *prop;
3821
32
  const char *name, *p;
3822
3823
32
  if (!root_node)
3824
0
    return NULL;
3825
3826
32
  prop = of_find_property(root_node, "compatible", NULL);
3827
32
  name = of_prop_next_string(prop, NULL);
3828
32
  if (!name)
3829
0
    return NULL;
3830
3831
32
  p = strchr(name, ',');
3832
32
  return nonempty(p ? p + 1 : name);
3833
32
}
3834
EXPORT_SYMBOL(of_get_machine_compatible);
3835
3836
/**
3837
 * of_get_machine_vendor - get first vendor prefix string from the root node.
3838
 *
3839
 * Returns the string or NULL.
3840
 */
3841
char *of_get_machine_vendor(void)
3842
0
{
3843
0
  const char *name = NULL;
3844
0
  char *p;
3845
3846
0
  if (!root_node)
3847
0
    return NULL;
3848
3849
3850
0
  of_property_read_string(root_node, "compatible", &name);
3851
3852
0
  p = strdup(name);
3853
0
  if (p)
3854
0
    *strchrnul(p, ',') = '\0';
3855
0
  return p;
3856
0
}
3857
EXPORT_SYMBOL(of_get_machine_vendor);
3858
3859
static int of_init_early_vars(void)
3860
32
{
3861
32
  struct device_node *bootsource;
3862
3863
32
  if (!IS_ENABLED(CONFIG_BAREBOX_DT_2ND))
3864
32
    return 0;
3865
3866
0
  bootsource = of_find_node_by_chosen("bootsource", NULL);
3867
0
  if (bootsource)
3868
0
    bootsource_of_node_set(bootsource);
3869
3870
0
  return 0;
3871
32
}
3872
postcore_initcall(of_init_early_vars);
3873
3874
static int of_init_late_vars(void)
3875
32
{
3876
32
  const char *name;
3877
3878
32
  name = of_get_machine_compatible();
3879
32
  barebox_set_hostname_no_overwrite(name ?: "barebox");
3880
3881
32
  return 0;
3882
32
}
3883
late_initcall(of_init_late_vars);