of.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2017 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #ifndef _DM_OF_H
  7. #define _DM_OF_H
  8. #include <asm/u-boot.h>
  9. #include <asm/global_data.h>
  10. /* integer value within a device tree property which references another node */
  11. typedef u32 phandle;
  12. /**
  13. * struct property: Device tree property
  14. *
  15. * @name: Property name
  16. * @length: Length of property in bytes
  17. * @value: Pointer to property value
  18. * @next: Pointer to next property, or NULL if none
  19. */
  20. struct property {
  21. char *name;
  22. int length;
  23. void *value;
  24. struct property *next;
  25. };
  26. /**
  27. * struct device_node: Device tree node
  28. *
  29. * @name: Node name
  30. * @type: Node type (value of device_type property) or "<NULL>" if none
  31. * @phandle: Phandle value of this none, or 0 if none
  32. * @full_name: Full path to node, e.g. "/bus@1/spi@1100"
  33. * @properties: Pointer to head of list of properties, or NULL if none
  34. * @parent: Pointer to parent node, or NULL if this is the root node
  35. * @child: Pointer to head of child node list, or NULL if no children
  36. * @sibling: Pointer to the next sibling node, or NULL if this is the last
  37. */
  38. struct device_node {
  39. const char *name;
  40. const char *type;
  41. phandle phandle;
  42. const char *full_name;
  43. struct property *properties;
  44. struct device_node *parent;
  45. struct device_node *child;
  46. struct device_node *sibling;
  47. };
  48. #define OF_MAX_PHANDLE_ARGS 16
  49. /**
  50. * struct of_phandle_args - structure to hold phandle and arguments
  51. *
  52. * This is used when decoding a phandle in a device tree property. Typically
  53. * these look like this:
  54. *
  55. * wibble {
  56. * phandle = <5>;
  57. * };
  58. *
  59. * ...
  60. * some-prop = <&wibble 1 2 3>
  61. *
  62. * Here &node is the phandle of the node 'wibble', i.e. 5. There are three
  63. * arguments: 1, 2, 3.
  64. *
  65. * So when decoding the phandle in some-prop, np will point to wibble,
  66. * args_count will be 3 and the three arguments will be in args.
  67. *
  68. * @np: Node that the phandle refers to
  69. * @args_count: Number of arguments
  70. * @args: Argument values
  71. */
  72. struct of_phandle_args {
  73. struct device_node *np;
  74. int args_count;
  75. uint32_t args[OF_MAX_PHANDLE_ARGS];
  76. };
  77. DECLARE_GLOBAL_DATA_PTR;
  78. /**
  79. * of_live_active() - check if livetree is active
  80. *
  81. * @returns true if livetree is active, false it not
  82. */
  83. #ifdef CONFIG_OF_LIVE
  84. static inline bool of_live_active(void)
  85. {
  86. return gd->of_root != NULL;
  87. }
  88. #else
  89. static inline bool of_live_active(void)
  90. {
  91. return false;
  92. }
  93. #endif
  94. #define OF_BAD_ADDR ((u64)-1)
  95. static inline const char *of_node_full_name(const struct device_node *np)
  96. {
  97. return np ? np->full_name : "<no-node>";
  98. }
  99. /* Default #address and #size cells */
  100. #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
  101. #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 2
  102. #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
  103. #endif
  104. /* Default string compare functions */
  105. #if !defined(of_compat_cmp)
  106. #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
  107. #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
  108. #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
  109. #endif
  110. /* Helper to read a big number; size is in cells (not bytes) */
  111. static inline u64 of_read_number(const __be32 *cell, int size)
  112. {
  113. u64 r = 0;
  114. while (size--)
  115. r = (r << 32) | be32_to_cpu(*(cell++));
  116. return r;
  117. }
  118. /* Like of_read_number, but we want an unsigned long result */
  119. static inline unsigned long of_read_ulong(const __be32 *cell, int size)
  120. {
  121. /* toss away upper bits if unsigned long is smaller than u64 */
  122. return of_read_number(cell, size);
  123. }
  124. #endif