Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 5e060d8 | 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 | 5e060d8 | 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 | * |
| 34 | * @name: Node name |
| 35 | * @type: Node type (value of device_type property) or "<NULL>" if none |
| 36 | * @phandle: Phandle value of this none, or 0 if none |
| 37 | * @full_name: Full path to node, e.g. "/bus@1/spi@1100" |
| 38 | * @properties: Pointer to head of list of properties, or NULL if none |
| 39 | * @parent: Pointer to parent node, or NULL if this is the root node |
| 40 | * @child: Pointer to head of child node list, or NULL if no children |
| 41 | * @sibling: Pointer to the next sibling node, or NULL if this is the last |
| 42 | */ |
| 43 | struct device_node { |
| 44 | const char *name; |
| 45 | const char *type; |
| 46 | phandle phandle; |
| 47 | const char *full_name; |
| 48 | |
| 49 | struct property *properties; |
| 50 | struct device_node *parent; |
| 51 | struct device_node *child; |
| 52 | struct device_node *sibling; |
| 53 | }; |
| 54 | |
| 55 | #define OF_MAX_PHANDLE_ARGS 16 |
| 56 | |
| 57 | /** |
| 58 | * struct of_phandle_args - structure to hold phandle and arguments |
| 59 | * |
| 60 | * This is used when decoding a phandle in a device tree property. Typically |
Patrick Delaunay | be74f71 | 2022-01-12 10:53:49 +0100 | [diff] [blame] | 61 | * these look like this:: |
Simon Glass | 5e060d8 | 2017-05-18 20:08:53 -0600 | [diff] [blame] | 62 | * |
Patrick Delaunay | be74f71 | 2022-01-12 10:53:49 +0100 | [diff] [blame] | 63 | * wibble { |
| 64 | * phandle = <5>; |
| 65 | * }; |
| 66 | * ... |
| 67 | * some-prop = <&wibble 1 2 3> |
Simon Glass | 5e060d8 | 2017-05-18 20:08:53 -0600 | [diff] [blame] | 68 | * |
| 69 | * Here &node is the phandle of the node 'wibble', i.e. 5. There are three |
| 70 | * arguments: 1, 2, 3. |
| 71 | * |
| 72 | * So when decoding the phandle in some-prop, np will point to wibble, |
| 73 | * args_count will be 3 and the three arguments will be in args. |
| 74 | * |
| 75 | * @np: Node that the phandle refers to |
| 76 | * @args_count: Number of arguments |
| 77 | * @args: Argument values |
| 78 | */ |
| 79 | struct of_phandle_args { |
| 80 | struct device_node *np; |
| 81 | int args_count; |
| 82 | uint32_t args[OF_MAX_PHANDLE_ARGS]; |
| 83 | }; |
| 84 | |
| 85 | DECLARE_GLOBAL_DATA_PTR; |
| 86 | |
| 87 | /** |
| 88 | * of_live_active() - check if livetree is active |
| 89 | * |
| 90 | * @returns true if livetree is active, false it not |
| 91 | */ |
Simon Glass | 5e060d8 | 2017-05-18 20:08:53 -0600 | [diff] [blame] | 92 | static inline bool of_live_active(void) |
| 93 | { |
Simon Glass | a652d9c | 2020-10-03 09:25:22 -0600 | [diff] [blame] | 94 | return gd_of_root() != NULL; |
Simon Glass | 5e060d8 | 2017-05-18 20:08:53 -0600 | [diff] [blame] | 95 | } |
Simon Glass | 5e060d8 | 2017-05-18 20:08:53 -0600 | [diff] [blame] | 96 | |
Simon Glass | a4b8e37 | 2017-05-18 20:09:27 -0600 | [diff] [blame] | 97 | #define OF_BAD_ADDR ((u64)-1) |
| 98 | |
| 99 | static inline const char *of_node_full_name(const struct device_node *np) |
| 100 | { |
| 101 | return np ? np->full_name : "<no-node>"; |
| 102 | } |
| 103 | |
| 104 | /* Default #address and #size cells */ |
| 105 | #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT) |
Matthias Brugger | 7a3f15e | 2019-09-05 10:48:49 +0200 | [diff] [blame] | 106 | #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 2 |
Simon Glass | a4b8e37 | 2017-05-18 20:09:27 -0600 | [diff] [blame] | 107 | #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1 |
| 108 | #endif |
| 109 | |
| 110 | /* Default string compare functions */ |
| 111 | #if !defined(of_compat_cmp) |
| 112 | #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2)) |
| 113 | #define of_prop_cmp(s1, s2) strcmp((s1), (s2)) |
| 114 | #define of_node_cmp(s1, s2) strcasecmp((s1), (s2)) |
| 115 | #endif |
| 116 | |
| 117 | /* Helper to read a big number; size is in cells (not bytes) */ |
| 118 | static inline u64 of_read_number(const __be32 *cell, int size) |
| 119 | { |
| 120 | u64 r = 0; |
| 121 | while (size--) |
| 122 | r = (r << 32) | be32_to_cpu(*(cell++)); |
| 123 | return r; |
| 124 | } |
| 125 | |
| 126 | /* Like of_read_number, but we want an unsigned long result */ |
| 127 | static inline unsigned long of_read_ulong(const __be32 *cell, int size) |
| 128 | { |
| 129 | /* toss away upper bits if unsigned long is smaller than u64 */ |
| 130 | return of_read_number(cell, size); |
| 131 | } |
| 132 | |
Simon Glass | 5e060d8 | 2017-05-18 20:08:53 -0600 | [diff] [blame] | 133 | #endif |