fdt-fixup.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. .. SPDX-License-Identifier: GPL-2.0+
  2. .. 2017-01-06, Mario Six <mario.six@gdsys.cc>
  3. Pre-relocation device tree manipulation
  4. =======================================
  5. Purpose
  6. -------
  7. In certain markets, it is beneficial for manufacturers of embedded devices to
  8. offer certain ranges of products, where the functionality of the devices within
  9. one series either don't differ greatly from another, or can be thought of as
  10. "extensions" of each other, where one device only differs from another in the
  11. addition of a small number of features (e.g. an additional output connector).
  12. To realize this in hardware, one method is to have a motherboard, and several
  13. possible daughter boards that can be attached to this mother board. Different
  14. daughter boards then either offer the slightly different functionality, or the
  15. addition of the daughter board to the device realizes the "extension" of
  16. functionality to the device described previously.
  17. For the software, we obviously want to reuse components for all these
  18. variations of the device. This means that the software somehow needs to cope
  19. with the situation that certain ICs may or may not be present on any given
  20. system, depending on which daughter boards are connected to the motherboard.
  21. In the Linux kernel, one possible solution to this problem is to employ the
  22. device tree overlay mechanism: There exists one "base" device tree, which
  23. features only the components guaranteed to exist in all varieties of the
  24. device. At the start of the kernel, the presence and type of the daughter
  25. boards is then detected, and the corresponding device tree overlays are applied
  26. to support the components on the daughter boards.
  27. Note that the components present on every variety of the board must, of course,
  28. provide a way to find out if and which daughter boards are installed for this
  29. mechanism to work.
  30. In the U-Boot boot loader, support for device tree overlays has recently been
  31. integrated, and is used on some boards to alter the device tree that is later
  32. passed to Linux. But since U-Boot's driver model, which is device tree-based as
  33. well, is being used in more and more drivers, the same problem of altering the
  34. device tree starts cropping up in U-Boot itself as well.
  35. An additional problem with the device tree in U-Boot is that it is read-only,
  36. and the current mechanisms don't allow easy manipulation of the device tree
  37. after the driver model has been initialized. While migrating to a live device
  38. tree (at least after the relocation) would greatly simplify the solution of
  39. this problem, it is a non-negligible task to implement it, an a interim
  40. solution is needed to address the problem at least in the medium-term.
  41. Hence, we propose a solution to this problem by offering a board-specific
  42. call-back function, which is passed a writeable pointer to the device tree.
  43. This function is called before the device tree is relocated, and specifically
  44. before the main U-Boot's driver model is instantiated, hence the main U-Boot
  45. "sees" all modifications to the device tree made in this function. Furthermore,
  46. we have the pre-relocation driver model at our disposal at this stage, which
  47. means that we can query the hardware for the existence and variety of the
  48. components easily.
  49. Implementation
  50. --------------
  51. To take advantage of the pre-relocation device tree manipulation mechanism,
  52. boards have to implement the function board_fix_fdt, which has the following
  53. signature:
  54. .. code-block:: c
  55. int board_fix_fdt (void *rw_fdt_blob)
  56. The passed-in void pointer is a writeable pointer to the device tree, which can
  57. be used to manipulate the device tree using e.g. functions from
  58. include/fdt_support.h. The return value should either be 0 in case of
  59. successful execution of the device tree manipulation or something else for a
  60. failure. Note that returning a non-null value from the function will
  61. unrecoverably halt the boot process, as with any function from init_sequence_f
  62. (in common/board_f.c).
  63. Furthermore, the Kconfig option OF_BOARD_FIXUP has to be set for the function
  64. to be called::
  65. Device Tree Control
  66. -> [*] Board-specific manipulation of Device Tree
  67. +----------------------------------------------------------+
  68. | WARNING: The actual manipulation of the device tree has |
  69. | to be the _last_ set of operations in board_fix_fdt! |
  70. | Since the pre-relocation driver model does not adapt to |
  71. | changes made to the device tree either, its references |
  72. | into the device tree will be invalid after manipulating |
  73. | it, and unpredictable behavior might occur when |
  74. | functions that rely on them are executed! |
  75. +----------------------------------------------------------+
  76. Hence, the recommended layout of the board_fixup_fdt call-back function is the
  77. following:
  78. .. code-block:: c
  79. int board_fix_fdt(void *rw_fdt_blob)
  80. {
  81. /*
  82. * Collect information about device's hardware and store
  83. * them in e.g. local variables
  84. */
  85. /* Do device tree manipulation using the values previously collected */
  86. /* Return 0 on successful manipulation and non-zero otherwise */
  87. }
  88. If this convention is kept, both an "additive" approach, meaning that nodes for
  89. detected components are added to the device tree, as well as a "subtractive"
  90. approach, meaning that nodes for absent components are removed from the tree,
  91. as well as a combination of both approaches should work.
  92. Example
  93. -------
  94. The controlcenterdc board (board/gdsys/a38x/controlcenterdc.c) features a
  95. board_fix_fdt function, in which six GPIO expanders (which might be present or
  96. not, since they are on daughter boards) on a I2C bus are queried for, and
  97. subsequently deactivated in the device tree if they are not present.
  98. Note that the dm_i2c_simple_probe function does not use the device tree, hence
  99. it is safe to call it after the tree has already been manipulated.
  100. Work to be done
  101. ---------------
  102. * The application of device tree overlay should be possible in board_fixup_fdt,
  103. but has not been tested at this stage.