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