of_platdata.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <common.h>
  3. #include <dm.h>
  4. #include <dt-structs.h>
  5. #include <dm/test.h>
  6. #include <test/test.h>
  7. #include <test/ut.h>
  8. #include <asm/global_data.h>
  9. /* Test that we can find a device using of-platdata */
  10. static int dm_test_of_plat_base(struct unit_test_state *uts)
  11. {
  12. struct udevice *dev;
  13. ut_assertok(uclass_first_device_err(UCLASS_SERIAL, &dev));
  14. ut_asserteq_str("sandbox_serial", dev->name);
  15. return 0;
  16. }
  17. DM_TEST(dm_test_of_plat_base, UT_TESTF_SCAN_PDATA);
  18. /* Test that we can read properties from a device */
  19. static int dm_test_of_plat_props(struct unit_test_state *uts)
  20. {
  21. struct dtd_sandbox_spl_test *plat;
  22. struct udevice *dev;
  23. int i;
  24. /* Skip the clock */
  25. ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev));
  26. ut_asserteq_str("sandbox_clk_test", dev->name);
  27. ut_assertok(uclass_next_device_err(&dev));
  28. plat = dev_get_plat(dev);
  29. ut_assert(plat->boolval);
  30. ut_asserteq(1, plat->intval);
  31. ut_asserteq(3, ARRAY_SIZE(plat->intarray));
  32. ut_asserteq(2, plat->intarray[0]);
  33. ut_asserteq(3, plat->intarray[1]);
  34. ut_asserteq(4, plat->intarray[2]);
  35. ut_asserteq(5, plat->byteval);
  36. ut_asserteq(3, ARRAY_SIZE(plat->bytearray));
  37. ut_asserteq(6, plat->bytearray[0]);
  38. ut_asserteq(0, plat->bytearray[1]);
  39. ut_asserteq(0, plat->bytearray[2]);
  40. ut_asserteq(9, ARRAY_SIZE(plat->longbytearray));
  41. for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++)
  42. ut_asserteq(9 + i, plat->longbytearray[i]);
  43. ut_asserteq_str("message", plat->stringval);
  44. ut_asserteq(3, ARRAY_SIZE(plat->stringarray));
  45. ut_asserteq_str("multi-word", plat->stringarray[0]);
  46. ut_asserteq_str("message", plat->stringarray[1]);
  47. ut_asserteq_str("", plat->stringarray[2]);
  48. ut_assertok(uclass_next_device_err(&dev));
  49. plat = dev_get_plat(dev);
  50. ut_assert(!plat->boolval);
  51. ut_asserteq(3, plat->intval);
  52. ut_asserteq(5, plat->intarray[0]);
  53. ut_asserteq(0, plat->intarray[1]);
  54. ut_asserteq(0, plat->intarray[2]);
  55. ut_asserteq(8, plat->byteval);
  56. ut_asserteq(3, ARRAY_SIZE(plat->bytearray));
  57. ut_asserteq(1, plat->bytearray[0]);
  58. ut_asserteq(0x23, plat->bytearray[1]);
  59. ut_asserteq(0x34, plat->bytearray[2]);
  60. for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++)
  61. ut_asserteq(i < 4 ? 9 + i : 0, plat->longbytearray[i]);
  62. ut_asserteq_str("message2", plat->stringval);
  63. ut_asserteq_str("another", plat->stringarray[0]);
  64. ut_asserteq_str("multi-word", plat->stringarray[1]);
  65. ut_asserteq_str("message", plat->stringarray[2]);
  66. ut_assertok(uclass_next_device_err(&dev));
  67. plat = dev_get_plat(dev);
  68. ut_assert(!plat->boolval);
  69. ut_asserteq_str("one", plat->stringarray[0]);
  70. ut_asserteq_str("", plat->stringarray[1]);
  71. ut_asserteq_str("", plat->stringarray[2]);
  72. ut_assertok(uclass_next_device_err(&dev));
  73. plat = dev_get_plat(dev);
  74. ut_assert(!plat->boolval);
  75. ut_asserteq_str("spl", plat->stringarray[0]);
  76. ut_asserteq(-ENODEV, uclass_next_device_err(&dev));
  77. return 0;
  78. }
  79. DM_TEST(dm_test_of_plat_props, UT_TESTF_SCAN_PDATA);
  80. /*
  81. * find_driver_info - recursively find the driver_info for a device
  82. *
  83. * This sets found[idx] to true when it finds the driver_info record for a
  84. * device, where idx is the index in the driver_info linker list.
  85. *
  86. * @uts: Test state
  87. * @parent: Parent to search
  88. * @found: bool array to update
  89. * @return 0 if OK, non-zero on error
  90. */
  91. static int find_driver_info(struct unit_test_state *uts, struct udevice *parent,
  92. bool found[])
  93. {
  94. struct udevice *dev;
  95. /* If not the root device, find the entry that caused it to be bound */
  96. if (parent->parent) {
  97. const int n_ents =
  98. ll_entry_count(struct driver_info, driver_info);
  99. int idx = -1;
  100. int i;
  101. for (i = 0; i < n_ents; i++) {
  102. const struct driver_rt *drt = gd_dm_driver_rt() + i;
  103. if (drt->dev == parent) {
  104. idx = i;
  105. found[idx] = true;
  106. break;
  107. }
  108. }
  109. ut_assert(idx != -1);
  110. }
  111. device_foreach_child(dev, parent) {
  112. int ret;
  113. ret = find_driver_info(uts, dev, found);
  114. if (ret < 0)
  115. return ret;
  116. }
  117. return 0;
  118. }
  119. /* Check that every device is recorded in its driver_info struct */
  120. static int dm_test_of_plat_dev(struct unit_test_state *uts)
  121. {
  122. const int n_ents = ll_entry_count(struct driver_info, driver_info);
  123. bool found[n_ents];
  124. uint i;
  125. /* Skip this test if there is no platform data */
  126. if (!CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT))
  127. return 0;
  128. /* Record the indexes that are found */
  129. memset(found, '\0', sizeof(found));
  130. ut_assertok(find_driver_info(uts, gd->dm_root, found));
  131. /* Make sure that the driver entries without devices have no ->dev */
  132. for (i = 0; i < n_ents; i++) {
  133. const struct driver_rt *drt = gd_dm_driver_rt() + i;
  134. struct udevice *dev;
  135. if (found[i]) {
  136. /* Make sure we can find it */
  137. ut_assertnonnull(drt->dev);
  138. ut_assertok(device_get_by_ofplat_idx(i, &dev));
  139. ut_asserteq_ptr(dev, drt->dev);
  140. } else {
  141. ut_assertnull(drt->dev);
  142. ut_asserteq(-ENOENT, device_get_by_ofplat_idx(i, &dev));
  143. }
  144. }
  145. return 0;
  146. }
  147. DM_TEST(dm_test_of_plat_dev, UT_TESTF_SCAN_PDATA);
  148. /* Test handling of phandles that point to other devices */
  149. static int dm_test_of_plat_phandle(struct unit_test_state *uts)
  150. {
  151. struct dtd_sandbox_clk_test *plat;
  152. struct udevice *dev, *clk;
  153. ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev));
  154. ut_asserteq_str("sandbox_clk_test", dev->name);
  155. plat = dev_get_plat(dev);
  156. ut_assertok(device_get_by_ofplat_idx(plat->clocks[0].idx, &clk));
  157. ut_asserteq_str("sandbox_fixed_clock", clk->name);
  158. ut_assertok(device_get_by_ofplat_idx(plat->clocks[1].idx, &clk));
  159. ut_asserteq_str("sandbox_clk", clk->name);
  160. ut_asserteq(1, plat->clocks[1].arg[0]);
  161. ut_assertok(device_get_by_ofplat_idx(plat->clocks[2].idx, &clk));
  162. ut_asserteq_str("sandbox_clk", clk->name);
  163. ut_asserteq(0, plat->clocks[2].arg[0]);
  164. ut_assertok(device_get_by_ofplat_idx(plat->clocks[3].idx, &clk));
  165. ut_asserteq_str("sandbox_clk", clk->name);
  166. ut_asserteq(3, plat->clocks[3].arg[0]);
  167. ut_assertok(device_get_by_ofplat_idx(plat->clocks[4].idx, &clk));
  168. ut_asserteq_str("sandbox_clk", clk->name);
  169. ut_asserteq(2, plat->clocks[4].arg[0]);
  170. return 0;
  171. }
  172. DM_TEST(dm_test_of_plat_phandle, UT_TESTF_SCAN_PDATA);
  173. #if CONFIG_IS_ENABLED(OF_PLATDATA_PARENT)
  174. /* Test that device parents are correctly set up */
  175. static int dm_test_of_plat_parent(struct unit_test_state *uts)
  176. {
  177. struct udevice *rtc, *i2c;
  178. ut_assertok(uclass_first_device_err(UCLASS_RTC, &rtc));
  179. ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
  180. ut_asserteq_ptr(i2c, dev_get_parent(rtc));
  181. return 0;
  182. }
  183. DM_TEST(dm_test_of_plat_parent, UT_TESTF_SCAN_PDATA);
  184. #endif