blob: b7404c139d1912c87b4c06a332ec64e288b0b194 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassa6eedb82017-05-18 20:08:53 -06002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassa6eedb82017-05-18 20:08:53 -06005 */
6
7#ifndef _DM_OF_H
8#define _DM_OF_H
9
Simon Glassa6eedb82017-05-18 20:08:53 -060010#include <asm/global_data.h>
11
12/* integer value within a device tree property which references another node */
13typedef u32 phandle;
14
15/**
16 * struct property: Device tree property
17 *
18 * @name: Property name
19 * @length: Length of property in bytes
20 * @value: Pointer to property value
21 * @next: Pointer to next property, or NULL if none
22 */
23struct property {
24 char *name;
25 int length;
26 void *value;
27 struct property *next;
28};
29
30/**
31 * struct device_node: Device tree node
32 *
Simon Glassef5ca002022-09-06 20:27:01 -060033 * The top of this tree is typically gd->of_root which points to the root node.
34 *
35 * The head of the list of children for the root node (and any other node) is
36 * in @child, with @sibling providing a link to the next child.
37 *
38 * Each child has a pointer to its parent in @parent.
39 *
40 * A node may have properties in which case the head of the list of properties
41 * @properties pointers to the first one, with struct property->@next pointing
42 * to the next one.
43 *
44 * @name: Node name, "" for the root node
Simon Glassa6eedb82017-05-18 20:08:53 -060045 * @type: Node type (value of device_type property) or "<NULL>" if none
46 * @phandle: Phandle value of this none, or 0 if none
Simon Glassef5ca002022-09-06 20:27:01 -060047 * @full_name: Full path to node, e.g. "/bus@1/spi@1100" ("/" for the root node)
Simon Glassa6eedb82017-05-18 20:08:53 -060048 * @properties: Pointer to head of list of properties, or NULL if none
49 * @parent: Pointer to parent node, or NULL if this is the root node
50 * @child: Pointer to head of child node list, or NULL if no children
51 * @sibling: Pointer to the next sibling node, or NULL if this is the last
52 */
53struct device_node {
54 const char *name;
55 const char *type;
56 phandle phandle;
57 const char *full_name;
58
59 struct property *properties;
60 struct device_node *parent;
61 struct device_node *child;
62 struct device_node *sibling;
63};
64
Simon Glasse88e2fe2023-06-01 10:22:40 -060065#define BAD_OF_ROOT 0xdead11e3
66
Simon Glassa6eedb82017-05-18 20:08:53 -060067#define OF_MAX_PHANDLE_ARGS 16
68
69/**
70 * struct of_phandle_args - structure to hold phandle and arguments
71 *
72 * This is used when decoding a phandle in a device tree property. Typically
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +010073 * these look like this::
Simon Glassa6eedb82017-05-18 20:08:53 -060074 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +010075 * wibble {
76 * phandle = <5>;
77 * };
78 * ...
79 * some-prop = <&wibble 1 2 3>
Simon Glassa6eedb82017-05-18 20:08:53 -060080 *
81 * Here &node is the phandle of the node 'wibble', i.e. 5. There are three
82 * arguments: 1, 2, 3.
83 *
84 * So when decoding the phandle in some-prop, np will point to wibble,
85 * args_count will be 3 and the three arguments will be in args.
86 *
87 * @np: Node that the phandle refers to
88 * @args_count: Number of arguments
89 * @args: Argument values
90 */
91struct of_phandle_args {
92 struct device_node *np;
93 int args_count;
94 uint32_t args[OF_MAX_PHANDLE_ARGS];
95};
96
97DECLARE_GLOBAL_DATA_PTR;
98
99/**
100 * of_live_active() - check if livetree is active
101 *
102 * @returns true if livetree is active, false it not
103 */
Simon Glassa6eedb82017-05-18 20:08:53 -0600104static inline bool of_live_active(void)
105{
Simon Glass40916e62020-10-03 09:25:22 -0600106 return gd_of_root() != NULL;
Simon Glassa6eedb82017-05-18 20:08:53 -0600107}
Simon Glassa6eedb82017-05-18 20:08:53 -0600108
Simon Glassa67cc632017-05-18 20:09:27 -0600109#define OF_BAD_ADDR ((u64)-1)
110
111static inline const char *of_node_full_name(const struct device_node *np)
112{
113 return np ? np->full_name : "<no-node>";
114}
115
116/* Default #address and #size cells */
117#if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
Matthias Brugger597fdfc2019-09-05 10:48:49 +0200118#define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 2
Simon Glassa67cc632017-05-18 20:09:27 -0600119#define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
120#endif
121
122/* Default string compare functions */
123#if !defined(of_compat_cmp)
124#define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
125#define of_prop_cmp(s1, s2) strcmp((s1), (s2))
126#define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
127#endif
128
129/* Helper to read a big number; size is in cells (not bytes) */
130static inline u64 of_read_number(const __be32 *cell, int size)
131{
132 u64 r = 0;
133 while (size--)
134 r = (r << 32) | be32_to_cpu(*(cell++));
135 return r;
136}
137
138/* Like of_read_number, but we want an unsigned long result */
139static inline unsigned long of_read_ulong(const __be32 *cell, int size)
140{
141 /* toss away upper bits if unsigned long is smaller than u64 */
142 return of_read_number(cell, size);
143}
144
Simon Glassa6eedb82017-05-18 20:08:53 -0600145#endif