of-plat.rst 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. .. SPDX-License-Identifier: GPL-2.0+
  2. Compiled-in Device Tree / Platform Data
  3. =======================================
  4. Introduction
  5. ------------
  6. Device tree is the standard configuration method in U-Boot. It is used to
  7. define what devices are in the system and provide configuration information
  8. to these devices.
  9. The overhead of adding devicetree access to U-Boot is fairly modest,
  10. approximately 3KB on Thumb 2 (plus the size of the DT itself). This means
  11. that in most cases it is best to use devicetree for configuration.
  12. However there are some very constrained environments where U-Boot needs to
  13. work. These include SPL with severe memory limitations. For example, some
  14. SoCs require a 16KB SPL image which must include a full MMC stack. In this
  15. case the overhead of devicetree access may be too great.
  16. It is possible to create platform data manually by defining C structures
  17. for it, and reference that data in a `U_BOOT_DRVINFO()` declaration. This
  18. bypasses the use of devicetree completely, effectively creating a parallel
  19. configuration mechanism. But it is an available option for SPL.
  20. As an alternative, the 'of-platdata' feature is provided. This converts the
  21. devicetree contents into C code which can be compiled into the SPL binary.
  22. This saves the 3KB of code overhead and perhaps a few hundred more bytes due
  23. to more efficient storage of the data.
  24. How it works
  25. ------------
  26. The feature is enabled by CONFIG OF_PLATDATA. This is only available in
  27. SPL/TPL and should be tested with:
  28. .. code-block:: c
  29. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  30. A tool called 'dtoc' converts a devicetree file either into a set of
  31. struct declarations, one for each compatible node, and a set of
  32. `U_BOOT_DRVINFO()` declarations along with the actual platform data for each
  33. device. As an example, consider this MMC node:
  34. .. code-block:: none
  35. sdmmc: dwmmc@ff0c0000 {
  36. compatible = "rockchip,rk3288-dw-mshc";
  37. clock-freq-min-max = <400000 150000000>;
  38. clocks = <&cru HCLK_SDMMC>, <&cru SCLK_SDMMC>,
  39. <&cru SCLK_SDMMC_DRV>, <&cru SCLK_SDMMC_SAMPLE>;
  40. clock-names = "biu", "ciu", "ciu_drv", "ciu_sample";
  41. fifo-depth = <0x100>;
  42. interrupts = <GIC_SPI 32 IRQ_TYPE_LEVEL_HIGH>;
  43. reg = <0xff0c0000 0x4000>;
  44. bus-width = <4>;
  45. cap-mmc-highspeed;
  46. cap-sd-highspeed;
  47. card-detect-delay = <200>;
  48. disable-wp;
  49. num-slots = <1>;
  50. pinctrl-names = "default";
  51. pinctrl-0 = <&sdmmc_clk>, <&sdmmc_cmd>, <&sdmmc_cd>, <&sdmmc_bus4>;
  52. vmmc-supply = <&vcc_sd>;
  53. status = "okay";
  54. bootph-all;
  55. };
  56. Some of these properties are dropped by U-Boot under control of the
  57. CONFIG_OF_SPL_REMOVE_PROPS option. The rest are processed. This will produce
  58. the following C struct declaration:
  59. .. code-block:: c
  60. struct dtd_rockchip_rk3288_dw_mshc {
  61. fdt32_t bus_width;
  62. bool cap_mmc_highspeed;
  63. bool cap_sd_highspeed;
  64. fdt32_t card_detect_delay;
  65. fdt32_t clock_freq_min_max[2];
  66. struct phandle_1_arg clocks[4];
  67. bool disable_wp;
  68. fdt32_t fifo_depth;
  69. fdt32_t interrupts[3];
  70. fdt32_t num_slots;
  71. fdt32_t reg[2];
  72. fdt32_t vmmc_supply;
  73. };
  74. and the following device declarations:
  75. .. code-block:: c
  76. /* Node /clock-controller@ff760000 index 0 */
  77. ...
  78. /* Node /dwmmc@ff0c0000 index 2 */
  79. static struct dtd_rockchip_rk3288_dw_mshc dtv_dwmmc_at_ff0c0000 = {
  80. .fifo_depth = 0x100,
  81. .cap_sd_highspeed = true,
  82. .interrupts = {0x0, 0x20, 0x4},
  83. .clock_freq_min_max = {0x61a80, 0x8f0d180},
  84. .vmmc_supply = 0xb,
  85. .num_slots = 0x1,
  86. .clocks = {{0, 456},
  87. {0, 68},
  88. {0, 114},
  89. {0, 118}},
  90. .cap_mmc_highspeed = true,
  91. .disable_wp = true,
  92. .bus_width = 0x4,
  93. .u_boot_dm_pre_reloc = true,
  94. .reg = {0xff0c0000, 0x4000},
  95. .card_detect_delay = 0xc8,
  96. };
  97. U_BOOT_DRVINFO(dwmmc_at_ff0c0000) = {
  98. .name = "rockchip_rk3288_dw_mshc",
  99. .plat = &dtv_dwmmc_at_ff0c0000,
  100. .plat_size = sizeof(dtv_dwmmc_at_ff0c0000),
  101. .parent_idx = -1,
  102. };
  103. The device is then instantiated at run-time and the platform data can be
  104. accessed using:
  105. .. code-block:: c
  106. struct udevice *dev;
  107. struct dtd_rockchip_rk3288_dw_mshc *plat = dev_get_plat(dev);
  108. This avoids the code overhead of converting the devicetree data to
  109. platform data in the driver. The `of_to_plat()` method should
  110. therefore do nothing in such a driver.
  111. Note that for the platform data to be matched with a driver, the 'name'
  112. property of the `U_BOOT_DRVINFO()` declaration has to match a driver declared
  113. via `U_BOOT_DRIVER()`. This effectively means that a `U_BOOT_DRIVER()` with a
  114. 'name' corresponding to the devicetree 'compatible' string (after converting
  115. it to a valid name for C) is needed, so a dedicated driver is required for
  116. each 'compatible' string.
  117. In order to make this a bit more flexible, the `DM_DRIVER_ALIAS()` macro can be
  118. used to declare an alias for a driver name, typically a 'compatible' string.
  119. This macro produces no code, but is used by dtoc tool. It must be located in the
  120. same file as its associated driver, ideally just after it.
  121. The parent_idx is the index of the parent `driver_info` structure within its
  122. linker list (instantiated by the `U_BOOT_DRVINFO()` macro). This is used to
  123. support `dev_get_parent()`.
  124. During the build process dtoc parses both `U_BOOT_DRIVER()` and
  125. `DM_DRIVER_ALIAS()` to build a list of valid driver names and driver aliases.
  126. If the 'compatible' string used for a device does not not match a valid driver
  127. name, it will be checked against the list of driver aliases in order to get the
  128. right driver name to use. If in this step there is no match found a warning is
  129. issued to avoid run-time failures.
  130. Where a node has multiple compatible strings, dtoc generates a `#define` to
  131. make them equivalent, e.g.:
  132. .. code-block:: c
  133. #define dtd_rockchip_rk3299_dw_mshc dtd_rockchip_rk3288_dw_mshc
  134. Converting of-platdata to a useful form
  135. ---------------------------------------
  136. Of course it would be possible to use the of-platdata directly in your driver
  137. whenever configuration information is required. However this means that the
  138. driver will not be able to support devicetree, since the of-platdata
  139. structure is not available when devicetree is used. It would make no sense
  140. to use this structure if devicetree were available, since the structure has
  141. all the limitations metioned in caveats below.
  142. Therefore it is recommended that the of-platdata structure should be used
  143. only in the `probe()` method of your driver. It cannot be used in the
  144. `of_to_plat()` method since this is not called when platform data is
  145. already present.
  146. How to structure your driver
  147. ----------------------------
  148. Drivers should always support devicetree as an option. The of-platdata
  149. feature is intended as a add-on to existing drivers.
  150. Your driver should convert the plat struct in its `probe()` method. The
  151. existing devicetree decoding logic should be kept in the
  152. `of_to_plat()` method and wrapped with `#if`.
  153. For example:
  154. .. code-block:: c
  155. #include <dt-structs.h>
  156. struct mmc_plat {
  157. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  158. /* Put this first since driver model will copy the data here */
  159. struct dtd_mmc dtplat;
  160. #endif
  161. /*
  162. * Other fields can go here, to be filled in by decoding from
  163. * the devicetree (or the C structures when of-platdata is used).
  164. */
  165. int fifo_depth;
  166. };
  167. static int mmc_of_to_plat(struct udevice *dev)
  168. {
  169. if (CONFIG_IS_ENABLED(OF_REAL)) {
  170. /* Decode the devicetree data */
  171. struct mmc_plat *plat = dev_get_plat(dev);
  172. const void *blob = gd->fdt_blob;
  173. int node = dev_of_offset(dev);
  174. plat->fifo_depth = fdtdec_get_int(blob, node, "fifo-depth", 0);
  175. }
  176. return 0;
  177. }
  178. static int mmc_probe(struct udevice *dev)
  179. {
  180. struct mmc_plat *plat = dev_get_plat(dev);
  181. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  182. /* Decode the of-platdata from the C structures */
  183. struct dtd_mmc *dtplat = &plat->dtplat;
  184. plat->fifo_depth = dtplat->fifo_depth;
  185. #endif
  186. /* Set up the device from the plat data */
  187. writel(plat->fifo_depth, ...)
  188. }
  189. static const struct udevice_id mmc_ids[] = {
  190. { .compatible = "vendor,mmc" },
  191. { }
  192. };
  193. U_BOOT_DRIVER(mmc_drv) = {
  194. .name = "mmc_drv",
  195. .id = UCLASS_MMC,
  196. .of_match = mmc_ids,
  197. .of_to_plat = mmc_of_to_plat,
  198. .probe = mmc_probe,
  199. .priv_auto = sizeof(struct mmc_priv),
  200. .plat_auto = sizeof(struct mmc_plat),
  201. };
  202. DM_DRIVER_ALIAS(mmc_drv, vendor_mmc) /* matches compatible string */
  203. Note that `struct mmc_plat` is defined in the C file, not in a header. This
  204. is to avoid needing to include dt-structs.h in a header file. The idea is to
  205. keep the use of each of-platdata struct to the smallest possible code area.
  206. There is just one driver C file for each struct, that can convert from the
  207. of-platdata struct to the standard one used by the driver.
  208. In the case where SPL_OF_PLATDATA is enabled, `plat_auto` is
  209. still used to allocate space for the platform data. This is different from
  210. the normal behaviour and is triggered by the use of of-platdata (strictly
  211. speaking it is a non-zero `plat_size` which triggers this).
  212. The of-platdata struct contents is copied from the C structure data to the
  213. start of the newly allocated area. In the case where devicetree is used,
  214. the platform data is allocated, and starts zeroed. In this case the
  215. `of_to_plat()` method should still set up the platform data (and the
  216. of-platdata struct will not be present).
  217. SPL must use either of-platdata or devicetree. Drivers cannot use both at
  218. the same time, but they must support devicetree. Supporting of-platdata is
  219. optional.
  220. The devicetree becomes inaccessible when CONFIG_SPL_OF_PLATDATA is enabled,
  221. since the devicetree access code is not compiled in. A corollary is that
  222. a board can only move to using of-platdata if all the drivers it uses support
  223. it. There would be little point in having some drivers require the device
  224. tree data, since then libfdt would still be needed for those drivers and
  225. there would be no code-size benefit.
  226. Build-time instantiation
  227. ------------------------
  228. Even with of-platdata there is a fair amount of code required in driver model.
  229. It is possible to have U-Boot handle the instantiation of devices at build-time,
  230. so avoiding the need for the `device_bind()` code and some parts of
  231. `device_probe()`.
  232. The feature is enabled by CONFIG_OF_PLATDATA_INST.
  233. Here is an example device, as generated by dtoc::
  234. /*
  235. * Node /serial index 6
  236. * driver sandbox_serial parent root_driver
  237. */
  238. #include <asm/serial.h>
  239. struct sandbox_serial_plat __attribute__ ((section (".priv_data")))
  240. _sandbox_serial_plat_serial = {
  241. .dtplat = {
  242. .sandbox_text_colour = "cyan",
  243. },
  244. };
  245. #include <asm/serial.h>
  246. u8 _sandbox_serial_priv_serial[sizeof(struct sandbox_serial_priv)]
  247. __attribute__ ((section (".priv_data")));
  248. #include <serial.h>
  249. u8 _sandbox_serial_uc_priv_serial[sizeof(struct serial_dev_priv)]
  250. __attribute__ ((section (".priv_data")));
  251. DM_DEVICE_INST(serial) = {
  252. .driver = DM_DRIVER_REF(sandbox_serial),
  253. .name = "sandbox_serial",
  254. .plat_ = &_sandbox_serial_plat_serial,
  255. .priv_ = _sandbox_serial_priv_serial,
  256. .uclass = DM_UCLASS_REF(serial),
  257. .uclass_priv_ = _sandbox_serial_uc_priv_serial,
  258. .uclass_node = {
  259. .prev = &DM_UCLASS_REF(serial)->dev_head,
  260. .next = &DM_UCLASS_REF(serial)->dev_head,
  261. },
  262. .child_head = {
  263. .prev = &DM_DEVICE_REF(serial)->child_head,
  264. .next = &DM_DEVICE_REF(serial)->child_head,
  265. },
  266. .sibling_node = {
  267. .prev = &DM_DEVICE_REF(i2c_at_0)->sibling_node,
  268. .next = &DM_DEVICE_REF(spl_test)->sibling_node,
  269. },
  270. .seq_ = 0,
  271. };
  272. Here is part of the driver, for reference::
  273. static const struct udevice_id sandbox_serial_ids[] = {
  274. { .compatible = "sandbox,serial" },
  275. { }
  276. };
  277. U_BOOT_DRIVER(sandbox_serial) = {
  278. .name = "sandbox_serial",
  279. .id = UCLASS_SERIAL,
  280. .of_match = sandbox_serial_ids,
  281. .of_to_plat = sandbox_serial_of_to_plat,
  282. .plat_auto = sizeof(struct sandbox_serial_plat),
  283. .priv_auto = sizeof(struct sandbox_serial_priv),
  284. .probe = sandbox_serial_probe,
  285. .remove = sandbox_serial_remove,
  286. .ops = &sandbox_serial_ops,
  287. .flags = DM_FLAG_PRE_RELOC,
  288. };
  289. The `DM_DEVICE_INST()` macro declares a struct udevice so you can see that the
  290. members are from that struct. The private data is declared immediately above,
  291. as `_sandbox_serial_priv_serial`, so there is no need for run-time memory
  292. allocation. The #include lines are generated as well, since dtoc searches the
  293. U-Boot source code for the definition of `struct sandbox_serial_priv` and adds
  294. the relevant header so that the code will compile without errors.
  295. The `plat_` member is set to the dtv data which is declared immediately above
  296. the device. This is similar to how it would look without of-platdata-inst, but
  297. node that the `dtplat` member inside is part of the wider
  298. `_sandbox_serial_plat_serial` struct. This is because the driver declares its
  299. own platform data, and the part generated by dtoc can only be a portion of it.
  300. The `dtplat` part is always first in the struct. If the device has no
  301. `.plat_auto` field, then a simple dtv struct can be used as with this example::
  302. static struct dtd_sandbox_clk dtv_clk_sbox = {
  303. .assigned_clock_rates = 0x141,
  304. .assigned_clocks = {0x7, 0x3},
  305. };
  306. #include <asm/clk.h>
  307. u8 _sandbox_clk_priv_clk_sbox[sizeof(struct sandbox_clk_priv)]
  308. __attribute__ ((section (".priv_data")));
  309. DM_DEVICE_INST(clk_sbox) = {
  310. .driver = DM_DRIVER_REF(sandbox_clk),
  311. .name = "sandbox_clk",
  312. .plat_ = &dtv_clk_sbox,
  313. Here is part of the driver, for reference::
  314. static const struct udevice_id sandbox_clk_ids[] = {
  315. { .compatible = "sandbox,clk" },
  316. { }
  317. };
  318. U_BOOT_DRIVER(sandbox_clk) = {
  319. .name = "sandbox_clk",
  320. .id = UCLASS_CLK,
  321. .of_match = sandbox_clk_ids,
  322. .ops = &sandbox_clk_ops,
  323. .probe = sandbox_clk_probe,
  324. .priv_auto = sizeof(struct sandbox_clk_priv),
  325. };
  326. You can see that `dtv_clk_sbox` just has the devicetree contents and there is
  327. no need for the `dtplat` separation, since the driver has no platform data of
  328. its own, besides that provided by the devicetree (i.e. no `.plat_auto` field).
  329. The doubly linked lists are handled by explicitly declaring the value of each
  330. node, as you can see with the `.prev` and `.next` values in the example above.
  331. Since dtoc knows the order of devices it can link them into the appropriate
  332. lists correctly.
  333. One of the features of driver model is the ability for a uclass to have a
  334. small amount of private data for each device in that uclass. This is used to
  335. provide a generic data structure that the uclass can use for all devices, thus
  336. allowing generic features to be implemented in common code. An example is I2C,
  337. which stores the bus speed there.
  338. Similarly, parent devices can have data associated with each of their children.
  339. This is used to provide information common to all children of a particular bus.
  340. For an I2C bus, this is used to store the I2C address of each child on the bus.
  341. This is all handled automatically by dtoc::
  342. #include <asm/i2c.h>
  343. u8 _sandbox_i2c_priv_i2c_at_0[sizeof(struct sandbox_i2c_priv)]
  344. __attribute__ ((section (".priv_data")));
  345. #include <i2c.h>
  346. u8 _sandbox_i2c_uc_priv_i2c_at_0[sizeof(struct dm_i2c_bus)]
  347. __attribute__ ((section (".priv_data")));
  348. DM_DEVICE_INST(i2c_at_0) = {
  349. .driver = DM_DRIVER_REF(sandbox_i2c),
  350. .name = "sandbox_i2c",
  351. .plat_ = &dtv_i2c_at_0,
  352. .priv_ = _sandbox_i2c_priv_i2c_at_0,
  353. .uclass = DM_UCLASS_REF(i2c),
  354. .uclass_priv_ = _sandbox_i2c_uc_priv_i2c_at_0,
  355. ...
  356. Part of driver, for reference::
  357. static const struct udevice_id sandbox_i2c_ids[] = {
  358. { .compatible = "sandbox,i2c" },
  359. { }
  360. };
  361. U_BOOT_DRIVER(sandbox_i2c) = {
  362. .name = "sandbox_i2c",
  363. .id = UCLASS_I2C,
  364. .of_match = sandbox_i2c_ids,
  365. .ops = &sandbox_i2c_ops,
  366. .priv_auto = sizeof(struct sandbox_i2c_priv),
  367. };
  368. Part of I2C uclass, for reference::
  369. UCLASS_DRIVER(i2c) = {
  370. .id = UCLASS_I2C,
  371. .name = "i2c",
  372. .flags = DM_UC_FLAG_SEQ_ALIAS,
  373. .post_bind = i2c_post_bind,
  374. .pre_probe = i2c_pre_probe,
  375. .post_probe = i2c_post_probe,
  376. .per_device_auto = sizeof(struct dm_i2c_bus),
  377. .per_child_plat_auto = sizeof(struct dm_i2c_chip),
  378. .child_post_bind = i2c_child_post_bind,
  379. };
  380. Here, `_sandbox_i2c_uc_priv_i2c_at_0` is required by the uclass but is declared
  381. in the device, as required by driver model. The required header file is included
  382. so that the code will compile without errors. A similar mechanism is used for
  383. child devices, but is not shown by this example.
  384. It would not be that useful to avoid binding devices but still need to allocate
  385. uclasses at runtime. So dtoc generates uclass instances as well::
  386. struct list_head uclass_head = {
  387. .prev = &DM_UCLASS_REF(serial)->sibling_node,
  388. .next = &DM_UCLASS_REF(clk)->sibling_node,
  389. };
  390. DM_UCLASS_INST(clk) = {
  391. .uc_drv = DM_UCLASS_DRIVER_REF(clk),
  392. .sibling_node = {
  393. .prev = &uclass_head,
  394. .next = &DM_UCLASS_REF(i2c)->sibling_node,
  395. },
  396. .dev_head = {
  397. .prev = &DM_DEVICE_REF(clk_sbox)->uclass_node,
  398. .next = &DM_DEVICE_REF(clk_fixed)->uclass_node,
  399. },
  400. };
  401. At the top is the list head. Driver model uses this on start-up, instead of
  402. creating its own.
  403. Below that are a set of `DM_UCLASS_INST()` macros, each declaring a
  404. `struct uclass`. The doubly linked lists work as for devices.
  405. All private data is placed into a `.priv_data` section so that it is contiguous
  406. in the resulting output binary.
  407. Indexes
  408. -------
  409. U-Boot stores drivers, devices and many other things in linker_list structures.
  410. These are sorted by name, so dtoc knows the order that they will appear when
  411. the linker runs. Each driver_info / udevice is referenced by its index in the
  412. linker_list array, called 'idx' in the code.
  413. When CONFIG_OF_PLATDATA_INST is enabled, idx is the udevice index, otherwise it
  414. is the driver_info index. In either case, indexes are used to reference devices
  415. using device_get_by_ofplat_idx(). This allows phandles to work as expected.
  416. Phases
  417. ------
  418. U-Boot operates in several phases, typically TPL, SPL and U-Boot proper.
  419. The latter does not use dtoc.
  420. In some rare cases different drivers are used for two phases. For example,
  421. in TPL it may not be necessary to use the full PCI subsystem, so a simple
  422. driver can be used instead.
  423. This works in the build system simply by compiling in one driver or the
  424. other (e.g. PCI driver + uclass for SPL; simple_bus for TPL). But dtoc has
  425. no way of knowing which code is compiled in for which phase, since it does
  426. not inspect Makefiles or dependency graphs.
  427. So to make this work for dtoc, we need to be able to explicitly mark
  428. drivers with their phase. This is done by adding a macro to the driver::
  429. /* code in tpl.c only compiled into TPL */
  430. U_BOOT_DRIVER(pci_x86) = {
  431. .name = "pci_x86",
  432. .id = UCLASS_SIMPLE_BUS,
  433. .of_match = of_match_ptr(tpl_fake_pci_ids),
  434. DM_PHASE(tpl)
  435. };
  436. /* code in pci_x86.c compiled into SPL and U-Boot proper */
  437. U_BOOT_DRIVER(pci_x86) = {
  438. .name = "pci_x86",
  439. .id = UCLASS_PCI,
  440. .of_match = pci_x86_ids,
  441. .ops = &pci_x86_ops,
  442. };
  443. Notice that the second driver has the same name but no DM_PHASE(), so it will be
  444. used for SPL and U-Boot.
  445. Note also that this only affects the code generated by dtoc. You still need to
  446. make sure that only the required driver is build into each phase.
  447. Header files
  448. ------------
  449. With OF_PLATDATA_INST, dtoc must include the correct header file in the
  450. generated code for any structs that are used, so that the code will compile.
  451. For example, if `struct ns16550_plat` is used, the code must include the
  452. `ns16550.h` header file.
  453. Typically dtoc can detect the header file needed for a driver by looking
  454. for the structs that it uses. For example, if a driver as a `.priv_auto`
  455. that uses `struct ns16550_plat`, then dtoc can search header files for the
  456. definition of that struct and use the file.
  457. In some cases, enums are used in drivers, typically with the `.data` field
  458. of `struct udevice_id`. Since dtoc does not support searching for these,
  459. you must use the `DM_HDR()` macro to tell dtoc which header to use. This works
  460. as a macro included in the driver definition::
  461. static const struct udevice_id apl_syscon_ids[] = {
  462. { .compatible = "intel,apl-punit", .data = X86_SYSCON_PUNIT },
  463. { }
  464. };
  465. U_BOOT_DRIVER(intel_apl_punit) = {
  466. .name = "intel_apl_punit",
  467. .id = UCLASS_SYSCON,
  468. .of_match = apl_syscon_ids,
  469. .probe = apl_punit_probe,
  470. DM_HEADER(<asm/cpu.h>) /* for X86_SYSCON_PUNIT */
  471. };
  472. Problems
  473. --------
  474. This section shows some common problems and how to fix them.
  475. Driver not found
  476. ~~~~~~~~~~~~~~~~
  477. In some cases you will you see something like this::
  478. WARNING: the driver rockchip_rk3188_grf was not found in the driver list
  479. The driver list is a list of drivers, each with a name. The name is in the
  480. U_BOOT_DRIVER() declaration, repeated twice, one in brackets and once as the
  481. .name member. For example, in the following declaration the driver name is
  482. `rockchip_rk3188_grf`::
  483. U_BOOT_DRIVER(rockchip_rk3188_grf) = {
  484. .name = "rockchip_rk3188_grf",
  485. .id = UCLASS_SYSCON,
  486. .of_match = rk3188_syscon_ids + 1,
  487. .bind = rk3188_syscon_bind_of_plat,
  488. };
  489. The first name U_BOOT_DRIVER(xx) is used to create a linker symbol so that the
  490. driver can be accessed at build-time without any overhead. The second one
  491. (.name = "xx") is used at runtime when something wants to print out the driver
  492. name.
  493. The dtoc tool expects to be able to find a driver for each compatible string in
  494. the devicetree. For example, if the devicetree has::
  495. grf: grf@20008000 {
  496. compatible = "rockchip,rk3188-grf", "syscon";
  497. reg = <0x20008000 0x200>;
  498. bootph-pre-ram;
  499. };
  500. then dtoc looks at the first compatible string ("rockchip,rk3188-grf"),
  501. converts that to a C identifier (rockchip_rk3188_grf) and then looks for that.
  502. Missing .compatible or Missing .id
  503. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  504. Various things can cause dtoc to fail to find the driver and it tries to
  505. warn about these. For example::
  506. rockchip_rk3188_uart: Missing .compatible in drivers/serial/serial_rockchip.c
  507. : WARNING: the driver rockchip_rk3188_uart was not found in the driver list
  508. Without a compatible string a driver cannot be used by dtoc, even if the
  509. compatible string is not actually needed at runtime.
  510. If the problem is simply that there are multiple compatible strings, the
  511. DM_DRIVER_ALIAS() macro can be used to tell dtoc about this and avoid a problem.
  512. Checks are also made to confirm that the referenced driver has a .compatible
  513. member and a .id member. The first provides the array of compatible strings and
  514. the second provides the uclass ID.
  515. Missing parent
  516. ~~~~~~~~~~~~~~
  517. When a device is used, its parent must be present as well. If you see an error
  518. like::
  519. Node '/i2c@0/emul/emul0' requires parent node '/i2c@0/emul' but it is not in
  520. the valid list
  521. it indicates that you are using a node whose parent is not present in the
  522. devicetree. In this example, if you look at the device tree output
  523. (e.g. fdtdump tpl/u-boot-tpl.dtb in your build directory), you may see something
  524. like this::
  525. emul {
  526. emul0 {
  527. compatible = "sandbox,i2c-rtc-emul";
  528. #emul-cells = <0x00000000>;
  529. phandle = <0x00000003>;
  530. };
  531. };
  532. In this example, 'emul0' exists but its parent 'emul' has no properties. These
  533. have been dropped by fdtgrep in an effort to reduce the devicetree size. This
  534. indicates that the two nodes have different phase settings. Looking at the
  535. source .dts::
  536. i2c_emul: emul {
  537. bootph-pre-ram;
  538. reg = <0xff>;
  539. compatible = "sandbox,i2c-emul-parent";
  540. emul0: emul0 {
  541. bootph-all;
  542. compatible = "sandbox,i2c-rtc-emul";
  543. #emul-cells = <0>;
  544. };
  545. };
  546. you can see that the child node 'emul0' usees 'bootph-all', indicating
  547. that the node is present in all SPL builds, but its parent uses
  548. 'bootph-pre-ram' indicating it is only present in SPL, not TPL. For a TPL
  549. build, this will fail with the above message. The fix is to change 'emul0' to
  550. use the same 'bootph-pre-ram' condition, so that it is not present in TPL,
  551. like its parent.
  552. Link errors / undefined reference
  553. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  554. Sometimes dtoc does not find the problem for you, but something is wrong and
  555. you get a link error, e.g.::
  556. :(__u_boot_list_2_udevice_2_spl_test5+0x0): undefined reference to
  557. `_u_boot_list_2_driver_2_sandbox_spl_test'
  558. /usr/bin/ld: dts/dt-uclass.o:(__u_boot_list_2_uclass_2_misc+0x8):
  559. undefined reference to `_u_boot_list_2_uclass_driver_2_misc'
  560. The first one indicates that the device cannot find its driver. This means that
  561. there is a driver 'sandbox_spl_test' but it is not compiled into the build.
  562. Check your Kconfig settings to make sure it is. If you don't want that in the
  563. build, adjust your phase settings, e.g. by using 'bootph-pre-ram' in the node
  564. to exclude it from the TPL build::
  565. spl-test5 {
  566. bootph-pre-sram;
  567. compatible = "sandbox,spl-test";
  568. stringarray = "tpl";
  569. };
  570. We can drop the 'bootph-pre-sram' line so this node won't appear in the TPL
  571. devicetree and thus the driver won't be needed.
  572. The second error above indicates that the MISC uclass is needed by the driver
  573. (since it is in the MISC uclass) but that uclass is not compiled in the build.
  574. The fix above would fix this error too. But if you do want this uclass in the
  575. build, check your Kconfig settings to make sure the uclass is being built
  576. (CONFIG_MISC in this case).
  577. Another error that can crop up is something like::
  578. spl/dts/dt-device.c:257:38: error: invalid application of ‘sizeof’ to
  579. incomplete type ‘struct sandbox_irq_priv’
  580. 257 | u8 _sandbox_irq_priv_irq_sbox[sizeof(struct sandbox_irq_priv)]
  581. | ^~~~~~
  582. This indicates that `struct sandbox_irq_priv` is not defined anywhere. The
  583. solution is to add a DM_HEADER() line, as below, so this is included in the
  584. dt-device.c file::
  585. U_BOOT_DRIVER(sandbox_irq) = {
  586. .name = "sandbox_irq",
  587. .id = UCLASS_IRQ,
  588. .of_match = sandbox_irq_ids,
  589. .ops = &sandbox_irq_ops,
  590. .priv_auto = sizeof(struct sandbox_irq_priv),
  591. DM_HEADER(<asm/irq.h>)
  592. };
  593. Note that there is no dependency checking on the above, so U-Boot will not
  594. regenerate the dt-device.c file when you update the source file (here,
  595. `irq_sandbox.c`). You need to run `make mrproper` first to get a fresh build.
  596. Another error that can crop up is something like::
  597. spl/dts/dt-device.c:257:38: error: invalid application of ‘sizeof’ to
  598. incomplete type ‘struct sandbox_irq_priv’
  599. 257 | u8 _sandbox_irq_priv_irq_sbox[sizeof(struct sandbox_irq_priv)]
  600. | ^~~~~~
  601. This indicates that `struct sandbox_irq_priv` is not defined anywhere. The
  602. solution is to add a DM_HEADER() line, as below, so this is included in the
  603. dt-device.c file::
  604. U_BOOT_DRIVER(sandbox_irq) = {
  605. .name = "sandbox_irq",
  606. .id = UCLASS_IRQ,
  607. .of_match = sandbox_irq_ids,
  608. .ops = &sandbox_irq_ops,
  609. .priv_auto = sizeof(struct sandbox_irq_priv),
  610. DM_HEADER(<asm/irq.h>)
  611. };
  612. Note that there is no dependency checking on the above, so U-Boot will not
  613. regenerate the dt-device.c file when you update the source file (here,
  614. `irq_sandbox.c`). You need to run `make mrproper` first to get a fresh build.
  615. Caveats
  616. -------
  617. There are various complications with this feature which mean it should only
  618. be used when strictly necessary, i.e. in SPL with limited memory. Notable
  619. caveats include:
  620. - Device tree does not describe data types. But the C code must define a
  621. type for each property. These are guessed using heuristics which
  622. are wrong in several fairly common cases. For example an 8-byte value
  623. is considered to be a 2-item integer array, and is byte-swapped. A
  624. boolean value that is not present means 'false', but cannot be
  625. included in the structures since there is generally no mention of it
  626. in the devicetree file.
  627. - Naming of nodes and properties is automatic. This means that they follow
  628. the naming in the devicetree, which may result in C identifiers that
  629. look a bit strange.
  630. - It is not possible to find a value given a property name. Code must use
  631. the associated C member variable directly in the code. This makes
  632. the code less robust in the face of devicetree changes. To avoid having
  633. a second struct with similar members and names you need to explicitly
  634. declare it as an alias with `DM_DRIVER_ALIAS()`.
  635. - The platform data is provided to drivers as a C structure. The driver
  636. must use the same structure to access the data. Since a driver
  637. normally also supports devicetree it must use `#ifdef` to separate
  638. out this code, since the structures are only available in SPL. This could
  639. be fixed fairly easily by making the structs available outside SPL, so
  640. that `IS_ENABLED()` could be used.
  641. - With CONFIG_OF_PLATDATA_INST all binding happens at build-time, meaning
  642. that (by default) it is not possible to call `device_bind()` from C code.
  643. This means that all devices must have an associated devicetree node and
  644. compatible string. For example if a GPIO device currently creates child
  645. devices in its `bind()` method, it will not work with
  646. CONFIG_OF_PLATDATA_INST. Arguably this is bad practice anyway and the
  647. devicetree binding should be updated to declare compatible strings for
  648. the child devices. It is possible to disable OF_PLATDATA_NO_BIND but this
  649. is not recommended since it increases code size.
  650. Internals
  651. ---------
  652. Generated files
  653. ~~~~~~~~~~~~~~~
  654. When enabled, dtoc generates the following five files:
  655. include/generated/dt-decl.h (OF_PLATDATA_INST only)
  656. Contains declarations for all drivers, devices and uclasses. This allows
  657. any `struct udevice`, `struct driver` or `struct uclass` to be located by its
  658. name
  659. include/generated/dt-structs-gen.h
  660. Contains the struct definitions for the devicetree nodes that are used. This
  661. is the same as without OF_PLATDATA_INST
  662. spl/dts/dt-plat.c (only with !OF_PLATDATA_INST)
  663. Contains the `U_BOOT_DRVINFO()` declarations that U-Boot uses to bind devices
  664. at start-up. See above for an example
  665. spl/dts/dt-device.c (only with OF_PLATDATA_INST)
  666. Contains `DM_DEVICE_INST()` declarations for each device that can be used at
  667. run-time. These are declared in the file along with any private/platform data
  668. that they use. Every device has an idx, as above. Since each device must be
  669. part of a double-linked list, the nodes are declared in the code as well.
  670. spl/dts/dt-uclass.c (only with OF_PLATDATA_INST)
  671. Contains `DM_UCLASS_INST()` declarations for each uclass that can be used at
  672. run-time. These are declared in the file along with any private data
  673. associated with the uclass itself (the `.priv_auto` member). Since each
  674. uclass must be part of a double-linked list, the nodes are declared in the
  675. code as well.
  676. The dt-structs.h file includes the generated file
  677. `(include/generated/dt-structs.h`) if CONFIG_SPL_OF_PLATDATA is enabled.
  678. Otherwise (such as in U-Boot proper) these structs are not available. This
  679. prevents them being used inadvertently. All usage must be bracketed with
  680. `#if CONFIG_IS_ENABLED(OF_PLATDATA)`.
  681. The dt-plat.c file contains the device declarations and is is built in
  682. spl/dt-plat.c.
  683. CONFIG options
  684. ~~~~~~~~~~~~~~
  685. Several CONFIG options are used to control the behaviour of of-platdata, all
  686. available for both SPL and TPL:
  687. OF_PLATDATA
  688. This is the main option which enables the of-platdata feature
  689. OF_PLATDATA_PARENT
  690. This allows `device_get_parent()` to work. Without this, all devices exist as
  691. direct children of the root node. This option is highly desirable (if not
  692. always absolutely essential) for buses such as I2C.
  693. OF_PLATDATA_INST
  694. This controls the instantiation of devices at build time. With it disabled,
  695. only `U_BOOT_DRVINFO()` records are created, with U-Boot handling the binding
  696. in `device_bind()` on start-up. With it enabled, only `DM_DEVICE_INST()` and
  697. `DM_UCLASS_INST()` records are created, and `device_bind()` is not needed at
  698. runtime.
  699. OF_PLATDATA_NO_BIND
  700. This controls whether `device_bind()` is supported. It is enabled by default
  701. with OF_PLATDATA_INST since code-size reduction is really the main point of
  702. the feature. It can be disabled if needed but is not likely to be supported
  703. in the long term.
  704. OF_PLATDATA_DRIVER_RT
  705. This controls whether the `struct driver_rt` records are used by U-Boot.
  706. Normally when a device is bound, U-Boot stores the device pointer in one of
  707. these records. There is one for every `struct driver_info` in the system,
  708. i.e. one for every device that is bound from those records. It provides a
  709. way to locate a device in the code and is used by
  710. `device_get_by_ofplat_idx()`. This option is always enabled with of-platdata,
  711. provided OF_PLATDATA_INST is not. In that case the records are useless since
  712. we don't have any `struct driver_info` records.
  713. OF_PLATDATA_RT
  714. This controls whether the `struct udevice_rt` records are used by U-Boot.
  715. It moves the updatable fields from `struct udevice` (currently only `flags`)
  716. into a separate structure, allowing the records to be kept in read-only
  717. memory. It is generally enabled if OF_PLATDATA_INST is enabled. This option
  718. also controls whether the private data is used in situ, or first copied into
  719. an allocated region. Again this is to allow the private data declared by
  720. dtoc-generated code to be in read-only memory. Note that access to private
  721. data must be done via accessor functions, such as `dev_get_priv()`, so that
  722. the relocation is handled.
  723. READ_ONLY
  724. This indicates that the data generated by dtoc should not be modified. Only
  725. a few fields actually do get changed in U-Boot, such as device flags. This
  726. option causes those to move into an allocated space (see OF_PLATDATA_RT).
  727. Also, since updating doubly linked lists is generally impossible when some of
  728. the nodes cannot be updated, OF_PLATDATA_NO_BIND is enabled.
  729. Data structures
  730. ~~~~~~~~~~~~~~~
  731. A few extra data structures are used with of-platdata:
  732. `struct udevice_rt`
  733. Run-time information for devices. When OF_PLATDATA_RT is enabled, this holds
  734. the flags for each device, so that `struct udevice` can remain unchanged by
  735. U-Boot, and potentially reside in read-only memory. Access to flags is then
  736. via functions like `dev_get_flags()` and `dev_or_flags()`. This data
  737. structure is allocated on start-up, where the private data is also copied.
  738. All flags values start at 0 and any changes are handled by `dev_or_flags()`
  739. and `dev_bic_flags()`. It would be more correct for the flags to be set to
  740. `DM_FLAG_BOUND`, or perhaps `DM_FLAG_BOUND | DM_FLAG_ALLOC_PDATA`, but since
  741. there is no code to bind/unbind devices and no code to allocate/free
  742. private data / platform data, it doesn't matter.
  743. `struct driver_rt`
  744. Run-time information for `struct driver_info` records. When
  745. OF_PLATDATA_DRIVER_RT is enabled, this holds a pointer to the device
  746. created by each record. This is needed so that is it possible to locate a
  747. device from C code. Specifically, the code can use `DM_DRVINFO_GET(name)` to
  748. get a reference to a particular `struct driver_info`, with `name` being the
  749. name of the devicetree node. This is very convenient. It is also fast, since
  750. no searching or string comparison is needed. This data structure is
  751. allocated on start-up, filled out by `device_bind()` and used by
  752. `device_get_by_ofplat_idx()`.
  753. Other changes
  754. ~~~~~~~~~~~~~
  755. Some other changes are made with of-platdata:
  756. Accessor functions
  757. Accessing private / platform data via functions such as `dev_get_priv()` has
  758. always been encouraged. With OF_PLATDATA_RT this is essential, since the
  759. `priv_` and `plat_` (etc.) values point to the data generated by dtoc, not
  760. the read-write copy that is sometimes made on start-up. Changing the
  761. private / platform data pointers has always been discouraged (the API is
  762. marked internal) but with OF_PLATDATA_RT this is not currently supported in
  763. general, since it assumes that all such pointers point to the relocated data.
  764. Note also that the renaming of struct members to have a trailing underscore
  765. was partly done to make people aware that they should not be accessed
  766. directly.
  767. `gd->uclass_root_s`
  768. Normally U-Boot sets up the head of the uclass list here and makes
  769. `gd->uclass_root` point to it. With OF_PLATDATA_INST, dtoc generates a
  770. declaration of `uclass_head` in `dt-uclass.c` since it needs to link the
  771. head node into the list. In that case, `gd->uclass_root_s` is not used and
  772. U-Boot just makes `gd->uclass_root` point to `uclass_head`.
  773. `gd->dm_driver_rt`
  774. This holds a pointer to a list of `struct driver_rt` records, one for each
  775. `struct driver_info`. The list is in alphabetical order by the name used
  776. in `U_BOOT_DRVINFO(name)` and indexed by idx, with the first record having
  777. an index of 0. It is only used if OF_PLATDATA_INST is not enabled. This is
  778. accessed via macros so that it can be used inside IS_ENABLED(), rather than
  779. requiring #ifdefs in the C code when it is not present.
  780. `gd->dm_udevice_rt`
  781. This holds a pointer to a list of `struct udevice_rt` records, one for each
  782. `struct udevice`. The list is in alphabetical order by the name used
  783. in `DM_DEVICE_INST(name)` (a C version of the devicetree node) and indexed by
  784. idx, with the first record having an index of 0. It is only used if
  785. OF_PLATDATA_INST is enabled. This is accessed via macros so that it can be
  786. used inside `IS_ENABLED()`, rather than requiring #ifdefs in the C code when
  787. it is not present.
  788. `gd->dm_priv_base`
  789. When OF_PLATDATA_RT is enabled, the private/platform data for each device is
  790. copied into an allocated region by U-Boot on start-up. This points to that
  791. region. All calls to accessor functions (e.g. `dev_get_priv()`) then
  792. translate from the pointer provided by the caller (assumed to lie between
  793. `__priv_data_start` and `__priv_data_end`) to the new allocated region. This
  794. member is accessed via macros so that it can be used inside IS_ENABLED(),
  795. rather than required #ifdefs in the C code when it is not present.
  796. `struct udevice->flags_`
  797. When OF_PLATDATA_RT is enabled, device flags are no-longer part of
  798. `struct udevice`, but are instead kept in `struct udevice_rt`, as described
  799. above. Flags are accessed via functions, such as `dev_get_flags()` and
  800. `dev_or_flags()`.
  801. `struct udevice->node_`
  802. When OF_PLATDATA is enabled, there is no devicetree at runtime, so no need
  803. for this field. It is removed, just to save space.
  804. `DM_PHASE`
  805. This macro is used to indicate which phase of U-Boot a driver is intended
  806. for. See above for details.
  807. `DM_HDR`
  808. This macro is used to indicate which header file dtoc should use to allow
  809. a driver declaration to compile correctly. See above for details.
  810. `device_get_by_ofplat_idx()`
  811. There used to be a function called `device_get_by_driver_info()` which
  812. looked up a `struct driver_info` pointer and returned the `struct udevice`
  813. that was created from it. It was only available for use with of-platdata.
  814. This has been removed in favour of `device_get_by_ofplat_idx()` which uses
  815. `idx`, the index of the `struct driver_info` or `struct udevice` in the
  816. linker_list. Similarly, the `struct phandle_0_arg` (etc.) structs have been
  817. updated to use this index instead of a pointer to `struct driver_info`.
  818. `DM_DRVINFO_GET`
  819. This has been removed since we now use indexes to obtain a driver from
  820. `struct phandle_0_arg` and the like.
  821. Two-pass binding
  822. The original of-platdata tried to order `U_BOOT_DRVINFO()` in the generated
  823. files so as to have parents declared ahead of children. This was convenient
  824. as it avoided any special code in U-Boot. With OF_PLATDATA_INST this does
  825. not work as the idx value relies on using alphabetical order for everything,
  826. so that dtoc and U-Boot's linker_lists agree on the idx value. Devices are
  827. then bound in order of idx, having no regard to parent/child relationships.
  828. For this reason, device binding now hapens in multiple passes, with parents
  829. being bound before their children. This is important so that children can
  830. find their parents in the bind() method if needed.
  831. Root device
  832. The root device is generally bound by U-Boot but with OF_PLATDATA_INST it
  833. cannot be, since binding needs to be done at build time. So in this case
  834. dtoc sets up a root device using `DM_DEVICE_INST()` in `dt-device.c` and
  835. U-Boot makes use of that. When OF_PLATDATA_INST is not enabled, U-Boot
  836. generally ignores the root node and does not create a `U_BOOT_DRVINFO()`
  837. record for it. This means that the idx numbers used by `struct driver_info`
  838. (when OF_PLATDATA_INST is disabled) and the idx numbers used by
  839. `struct udevice` (when OF_PLATDATA_INST is enabled) differ, since one has a
  840. root node and the other does not. This does not actually matter, since only
  841. one of them is actually used for any particular build, but it is worth
  842. keeping in mind if comparing index values and switching OF_PLATDATA_INST on
  843. and off.
  844. `__priv_data_start` and `__priv_data_end`
  845. The private/platform data declared by dtoc is all collected together in
  846. a linker section and these symbols mark the start and end of it. This allows
  847. U-Boot to relocate the area to a new location if needed (with
  848. OF_PLATDATA_RT)
  849. `dm_priv_to_rw()`
  850. This function converts a private- or platform-data pointer value generated by
  851. dtoc into one that can be used by U-Boot. It is a NOP unless OF_PLATDATA_RT
  852. is enabled, in which case it translates the address to the relocated
  853. region. See above for more information.
  854. The dm_populate_phandle_data() function that was previous needed has now been
  855. removed, since dtoc can address the drivers directly from dt-plat.c and does
  856. not need to fix up things at runtime.
  857. The pylibfdt Python module is used to access the devicetree.
  858. Credits
  859. -------
  860. This is an implementation of an idea by Tom Rini <trini@konsulko.com>.
  861. Future work
  862. -----------
  863. - Consider programmatically reading binding files instead of devicetree
  864. contents
  865. - Allow IS_ENABLED() to be used in the C code instead of #if
  866. .. Simon Glass <sjg@chromium.org>
  867. .. Google, Inc
  868. .. 6/6/16
  869. .. Updated Independence Day 2016
  870. .. Updated 1st October 2020
  871. .. Updated 5th February 2021