of-plat.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. Driver Model Compiled-in Device Tree / Platform Data
  2. ====================================================
  3. Introduction
  4. ------------
  5. Device tree is the standard configuration method in U-Boot. It is used to
  6. define what devices are in the system and provide configuration information
  7. to these devices.
  8. The overhead of adding device tree access to U-Boot is fairly modest,
  9. approximately 3KB on Thumb 2 (plus the size of the DT itself). This means
  10. that in most cases it is best to use device tree for configuration.
  11. However there are some very constrained environments where U-Boot needs to
  12. work. These include SPL with severe memory limitations. For example, some
  13. SoCs require a 16KB SPL image which must include a full MMC stack. In this
  14. case the overhead of device tree access may be too great.
  15. It is possible to create platform data manually by defining C structures
  16. for it, and reference that data in a U_BOOT_DEVICE() declaration. This
  17. bypasses the use of device tree completely, effectively creating a parallel
  18. configuration mechanism. But it is an available option for SPL.
  19. As an alternative, a new 'of-platdata' feature is provided. This converts the
  20. device tree contents into C code which can be compiled into the SPL binary.
  21. This saves the 3KB of code overhead and perhaps a few hundred more bytes due
  22. to more efficient storage of the data.
  23. Note: Quite a bit of thought has gone into the design of this feature.
  24. However it still has many rough edges and comments and suggestions are
  25. strongly encouraged! Quite possibly there is a much better approach.
  26. Caveats
  27. -------
  28. There are many problems with this features. It should only be used when
  29. strictly necessary. Notable problems include:
  30. - Device tree does not describe data types. But the C code must define a
  31. type for each property. These are guessed using heuristics which
  32. are wrong in several fairly common cases. For example an 8-byte value
  33. is considered to be a 2-item integer array, and is byte-swapped. A
  34. boolean value that is not present means 'false', but cannot be
  35. included in the structures since there is generally no mention of it
  36. in the device tree file.
  37. - Naming of nodes and properties is automatic. This means that they follow
  38. the naming in the device tree, which may result in C identifiers that
  39. look a bit strange.
  40. - It is not possible to find a value given a property name. Code must use
  41. the associated C member variable directly in the code. This makes
  42. the code less robust in the face of device-tree changes. It also
  43. makes it very unlikely that your driver code will be useful for more
  44. than one SoC. Even if the code is common, each SoC will end up with
  45. a different C struct name, and a likely a different format for the
  46. platform data.
  47. - The platform data is provided to drivers as a C structure. The driver
  48. must use the same structure to access the data. Since a driver
  49. normally also supports device tree it must use #ifdef to separate
  50. out this code, since the structures are only available in SPL.
  51. How it works
  52. ------------
  53. The feature is enabled by CONFIG SPL_OF_PLATDATA. This is only available
  54. in SPL and should be tested with:
  55. #if CONFIG_IS_ENABLED(SPL_OF_PLATDATA)
  56. A new tool called 'dtoc' converts a device tree file either into a set of
  57. struct declarations, one for each compatible node, or a set of
  58. U_BOOT_DEVICE() declarations along with the actual platform data for each
  59. device. As an example, consider this MMC node:
  60. sdmmc: dwmmc@ff0c0000 {
  61. compatible = "rockchip,rk3288-dw-mshc";
  62. clock-freq-min-max = <400000 150000000>;
  63. clocks = <&cru HCLK_SDMMC>, <&cru SCLK_SDMMC>,
  64. <&cru SCLK_SDMMC_DRV>, <&cru SCLK_SDMMC_SAMPLE>;
  65. clock-names = "biu", "ciu", "ciu_drv", "ciu_sample";
  66. fifo-depth = <0x100>;
  67. interrupts = <GIC_SPI 32 IRQ_TYPE_LEVEL_HIGH>;
  68. reg = <0xff0c0000 0x4000>;
  69. bus-width = <4>;
  70. cap-mmc-highspeed;
  71. cap-sd-highspeed;
  72. card-detect-delay = <200>;
  73. disable-wp;
  74. num-slots = <1>;
  75. pinctrl-names = "default";
  76. pinctrl-0 = <&sdmmc_clk>, <&sdmmc_cmd>, <&sdmmc_cd>, <&sdmmc_bus4>;
  77. vmmc-supply = <&vcc_sd>;
  78. status = "okay";
  79. u-boot,dm-pre-reloc;
  80. };
  81. Some of these properties are dropped by U-Boot under control of the
  82. CONFIG_OF_SPL_REMOVE_PROPS option. The rest are processed. This will produce
  83. the following C struct declaration:
  84. struct dtd_rockchip_rk3288_dw_mshc {
  85. fdt32_t bus_width;
  86. bool cap_mmc_highspeed;
  87. bool cap_sd_highspeed;
  88. fdt32_t card_detect_delay;
  89. fdt32_t clock_freq_min_max[2];
  90. struct phandle_1_arg clocks[4];
  91. bool disable_wp;
  92. fdt32_t fifo_depth;
  93. fdt32_t interrupts[3];
  94. fdt32_t num_slots;
  95. fdt32_t reg[2];
  96. fdt32_t vmmc_supply;
  97. };
  98. and the following device declaration:
  99. static struct dtd_rockchip_rk3288_dw_mshc dtv_dwmmc_at_ff0c0000 = {
  100. .fifo_depth = 0x100,
  101. .cap_sd_highspeed = true,
  102. .interrupts = {0x0, 0x20, 0x4},
  103. .clock_freq_min_max = {0x61a80, 0x8f0d180},
  104. .vmmc_supply = 0xb,
  105. .num_slots = 0x1,
  106. .clocks = {{&dtv_clock_controller_at_ff760000, 456},
  107. {&dtv_clock_controller_at_ff760000, 68},
  108. {&dtv_clock_controller_at_ff760000, 114},
  109. {&dtv_clock_controller_at_ff760000, 118}},
  110. .cap_mmc_highspeed = true,
  111. .disable_wp = true,
  112. .bus_width = 0x4,
  113. .u_boot_dm_pre_reloc = true,
  114. .reg = {0xff0c0000, 0x4000},
  115. .card_detect_delay = 0xc8,
  116. };
  117. U_BOOT_DEVICE(dwmmc_at_ff0c0000) = {
  118. .name = "rockchip_rk3288_dw_mshc",
  119. .platdata = &dtv_dwmmc_at_ff0c0000,
  120. .platdata_size = sizeof(dtv_dwmmc_at_ff0c0000),
  121. };
  122. The device is then instantiated at run-time and the platform data can be
  123. accessed using:
  124. struct udevice *dev;
  125. struct dtd_rockchip_rk3288_dw_mshc *plat = dev_get_platdata(dev);
  126. This avoids the code overhead of converting the device tree data to
  127. platform data in the driver. The ofdata_to_platdata() method should
  128. therefore do nothing in such a driver.
  129. Where a node has multiple compatible strings, a #define is used to make them
  130. equivalent, e.g.:
  131. #define dtd_rockchip_rk3299_dw_mshc dtd_rockchip_rk3288_dw_mshc
  132. Converting of-platdata to a useful form
  133. ---------------------------------------
  134. Of course it would be possible use the of-platdata directly in your driver
  135. whenever configuration information is required. However this meands that the
  136. driver will not be able to support device tree, since the of-platdata
  137. structure is not available when device tree is used. It would make no sense
  138. to use this structure if device tree were available, since the structure has
  139. all the limitations metioned in caveats above.
  140. Therefore it is recommended that the of-platdata structure should be used
  141. only in the probe() method of your driver. It cannot be used in the
  142. ofdata_to_platdata() method since this is not called when platform data is
  143. already present.
  144. How to structure your driver
  145. ----------------------------
  146. Drivers should always support device tree as an option. The of-platdata
  147. feature is intended as a add-on to existing drivers.
  148. Your driver should convert the platdata struct in its probe() method. The
  149. existing device tree decoding logic should be kept in the
  150. ofdata_to_platdata() method and wrapped with #if.
  151. For example:
  152. #include <dt-structs.h>
  153. struct mmc_platdata {
  154. #if CONFIG_IS_ENABLED(SPL_OF_PLATDATA)
  155. /* Put this first since driver model will copy the data here */
  156. struct dtd_mmc dtplat;
  157. #endif
  158. /*
  159. * Other fields can go here, to be filled in by decoding from
  160. * the device tree (or the C structures when of-platdata is used).
  161. */
  162. int fifo_depth;
  163. };
  164. static int mmc_ofdata_to_platdata(struct udevice *dev)
  165. {
  166. #if !CONFIG_IS_ENABLED(SPL_OF_PLATDATA)
  167. /* Decode the device tree data */
  168. struct mmc_platdata *plat = dev_get_platdata(dev);
  169. const void *blob = gd->fdt_blob;
  170. int node = dev_of_offset(dev);
  171. plat->fifo_depth = fdtdec_get_int(blob, node, "fifo-depth", 0);
  172. #endif
  173. return 0;
  174. }
  175. static int mmc_probe(struct udevice *dev)
  176. {
  177. struct mmc_platdata *plat = dev_get_platdata(dev);
  178. #if CONFIG_IS_ENABLED(SPL_OF_PLATDATA)
  179. /* Decode the of-platdata from the C structures */
  180. struct dtd_mmc *dtplat = &plat->dtplat;
  181. plat->fifo_depth = dtplat->fifo_depth;
  182. #endif
  183. /* Set up the device from the plat data */
  184. writel(plat->fifo_depth, ...)
  185. }
  186. static const struct udevice_id mmc_ids[] = {
  187. { .compatible = "vendor,mmc" },
  188. { }
  189. };
  190. U_BOOT_DRIVER(mmc_drv) = {
  191. .name = "mmc",
  192. .id = UCLASS_MMC,
  193. .of_match = mmc_ids,
  194. .ofdata_to_platdata = mmc_ofdata_to_platdata,
  195. .probe = mmc_probe,
  196. .priv_auto_alloc_size = sizeof(struct mmc_priv),
  197. .platdata_auto_alloc_size = sizeof(struct mmc_platdata),
  198. };
  199. In the case where SPL_OF_PLATDATA is enabled, platdata_auto_alloc_size is
  200. still used to allocate space for the platform data. This is different from
  201. the normal behaviour and is triggered by the use of of-platdata (strictly
  202. speaking it is a non-zero platdata_size which triggers this).
  203. The of-platdata struct contents is copied from the C structure data to the
  204. start of the newly allocated area. In the case where device tree is used,
  205. the platform data is allocated, and starts zeroed. In this case the
  206. ofdata_to_platdata() method should still set up the platform data (and the
  207. of-platdata struct will not be present).
  208. SPL must use either of-platdata or device tree. Drivers cannot use both at
  209. the same time, but they must support device tree. Supporting of-platdata is
  210. optional.
  211. The device tree becomes in accessible when CONFIG_SPL_OF_PLATDATA is enabled,
  212. since the device-tree access code is not compiled in. A corollary is that
  213. a board can only move to using of-platdata if all the drivers it uses support
  214. it. There would be little point in having some drivers require the device
  215. tree data, since then libfdt would still be needed for those drivers and
  216. there would be no code-size benefit.
  217. Internals
  218. ---------
  219. The dt-structs.h file includes the generated file
  220. (include/generated//dt-structs.h) if CONFIG_SPL_OF_PLATDATA is enabled.
  221. Otherwise (such as in U-Boot proper) these structs are not available. This
  222. prevents them being used inadvertently. All usage must be bracketed with
  223. #if CONFIG_IS_ENABLED(SPL_OF_PLATDATA).
  224. The dt-platdata.c file contains the device declarations and is is built in
  225. spl/dt-platdata.c.
  226. Some phandles (thsoe that are recognised as such) are converted into
  227. points to platform data. This pointer can potentially be used to access the
  228. referenced device (by searching for the pointer value). This feature is not
  229. yet implemented, however.
  230. The beginnings of a libfdt Python module are provided. So far this only
  231. implements a subset of the features.
  232. The 'swig' tool is needed to build the libfdt Python module. If this is not
  233. found then the Python model is not used and a fallback is used instead, which
  234. makes use of fdtget.
  235. Credits
  236. -------
  237. This is an implementation of an idea by Tom Rini <trini@konsulko.com>.
  238. Future work
  239. -----------
  240. - Consider programmatically reading binding files instead of device tree
  241. contents
  242. - Complete the phandle feature
  243. - Move to using a full Python libfdt module
  244. --
  245. Simon Glass <sjg@chromium.org>
  246. Google, Inc
  247. 6/6/16
  248. Updated Independence Day 2016