of_platdata.c 6.5 KB

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