ofnode.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2017 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #ifndef _DM_OFNODE_H
  8. #define _DM_OFNODE_H
  9. /**
  10. * ofnode - reference to a device tree node
  11. *
  12. * This union can hold either a straightforward pointer to a struct device_node
  13. * in the live device tree, or an offset within the flat device tree. In the
  14. * latter case, the pointer value is just the integer offset within the flat DT.
  15. *
  16. * Thus we can reference nodes in both the live tree (once available) and the
  17. * flat tree (until then). Functions are available to translate between an
  18. * ofnode and either an offset or a struct device_node *.
  19. *
  20. * The reference can also hold a null offset, in which case the pointer value
  21. * here is (void *)-1. This corresponds to a struct device_node * value of
  22. * NULL, or an offset of -1.
  23. *
  24. * There is no ambiguity as to whether ofnode holds an offset or a node
  25. * pointer: when the live tree is active it holds a node pointer, otherwise it
  26. * holds an offset. The value itself does not need to be unique and in theory
  27. * the same value could point to a valid device node or a valid offset. We
  28. * could arrange for a unique value to be used (e.g. by making the pointer
  29. * point to an offset within the flat device tree in the case of an offset) but
  30. * this increases code size slightly due to the subtraction. Since it offers no
  31. * real benefit, the approach described here seems best.
  32. *
  33. * For now these points use constant types, since we don't allow writing
  34. * the DT.
  35. *
  36. * @np: Pointer to device node, used for live tree
  37. * @flat_ptr: Pointer into flat device tree, used for flat tree. Note that this
  38. * is not a really a pointer to a node: it is an offset value. See above.
  39. */
  40. typedef union ofnode_union {
  41. const struct device_node *np; /* will be used for future live tree */
  42. long of_offset;
  43. } ofnode;
  44. /**
  45. * ofnode_to_offset() - convert an ofnode to a flat DT offset
  46. *
  47. * This cannot be called if the reference contains a node pointer.
  48. *
  49. * @node: Reference containing offset (possibly invalid)
  50. * @return DT offset (can be -1)
  51. */
  52. static inline int ofnode_to_offset(ofnode node)
  53. {
  54. return node.of_offset;
  55. }
  56. /**
  57. * ofnode_valid() - check if an ofnode is valid
  58. *
  59. * @return true if the reference contains a valid ofnode, false if it is NULL
  60. */
  61. static inline bool ofnode_valid(ofnode node)
  62. {
  63. return node.of_offset != -1;
  64. }
  65. /**
  66. * offset_to_ofnode() - convert a DT offset to an ofnode
  67. *
  68. * @of_offset: DT offset (either valid, or -1)
  69. * @return reference to the associated DT offset
  70. */
  71. static inline ofnode offset_to_ofnode(int of_offset)
  72. {
  73. ofnode node;
  74. node.of_offset = of_offset;
  75. return node;
  76. }
  77. /**
  78. * ofnode_equal() - check if two references are equal
  79. *
  80. * @return true if equal, else false
  81. */
  82. static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
  83. {
  84. /* We only need to compare the contents */
  85. return ref1.of_offset == ref2.of_offset;
  86. }
  87. #endif