ofnode.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <common.h>
  3. #include <dm.h>
  4. #include <log.h>
  5. #include <dm/of_extra.h>
  6. #include <dm/test.h>
  7. #include <test/test.h>
  8. #include <test/ut.h>
  9. static int dm_test_ofnode_compatible(struct unit_test_state *uts)
  10. {
  11. ofnode root_node = ofnode_path("/");
  12. ut_assert(ofnode_valid(root_node));
  13. ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
  14. return 0;
  15. }
  16. DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  17. static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
  18. {
  19. /* test invalid phandle */
  20. ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
  21. ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
  22. /* test first valid phandle */
  23. ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
  24. /* test unknown phandle */
  25. ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
  26. return 0;
  27. }
  28. DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  29. static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
  30. {
  31. const char propname[] = "compatible";
  32. const char propval[] = "denx,u-boot-fdt-test";
  33. const char *str;
  34. ofnode node = ofnode_null();
  35. /* Find first matching node, there should be at least one */
  36. node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
  37. ut_assert(ofnode_valid(node));
  38. str = ofnode_read_string(node, propname);
  39. ut_assert(str && !strcmp(str, propval));
  40. /* Find the rest of the matching nodes */
  41. while (true) {
  42. node = ofnode_by_prop_value(node, propname, propval,
  43. sizeof(propval));
  44. if (!ofnode_valid(node))
  45. break;
  46. str = ofnode_read_string(node, propname);
  47. ut_assert(str && !strcmp(str, propval));
  48. }
  49. return 0;
  50. }
  51. DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
  52. static int dm_test_ofnode_fmap(struct unit_test_state *uts)
  53. {
  54. struct fmap_entry entry;
  55. ofnode node;
  56. node = ofnode_path("/cros-ec/flash");
  57. ut_assert(ofnode_valid(node));
  58. ut_assertok(ofnode_read_fmap_entry(node, &entry));
  59. ut_asserteq(0x08000000, entry.offset);
  60. ut_asserteq(0x20000, entry.length);
  61. return 0;
  62. }
  63. DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  64. static int dm_test_ofnode_read(struct unit_test_state *uts)
  65. {
  66. const u32 *val;
  67. ofnode node;
  68. int size;
  69. node = ofnode_path("/a-test");
  70. ut_assert(ofnode_valid(node));
  71. val = ofnode_read_prop(node, "int-value", &size);
  72. ut_assertnonnull(val);
  73. ut_asserteq(4, size);
  74. ut_asserteq(1234, fdt32_to_cpu(val[0]));
  75. val = ofnode_read_prop(node, "missing", &size);
  76. ut_assertnull(val);
  77. ut_asserteq(-FDT_ERR_NOTFOUND, size);
  78. /* Check it works without a size parameter */
  79. val = ofnode_read_prop(node, "missing", NULL);
  80. ut_assertnull(val);
  81. return 0;
  82. }
  83. DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  84. static int dm_test_ofnode_phandle(struct unit_test_state *uts)
  85. {
  86. struct ofnode_phandle_args args;
  87. ofnode node;
  88. int ret;
  89. const char prop[] = "test-gpios";
  90. const char cell[] = "#gpio-cells";
  91. const char prop2[] = "phandle-value";
  92. node = ofnode_path("/a-test");
  93. ut_assert(ofnode_valid(node));
  94. /* Test ofnode_count_phandle_with_args with cell name */
  95. ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
  96. ut_asserteq(-ENOENT, ret);
  97. ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
  98. ut_asserteq(-EINVAL, ret);
  99. ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
  100. ut_asserteq(5, ret);
  101. /* Test ofnode_parse_phandle_with_args with cell name */
  102. ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
  103. &args);
  104. ut_asserteq(-ENOENT, ret);
  105. ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
  106. &args);
  107. ut_asserteq(-EINVAL, ret);
  108. ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
  109. ut_assertok(ret);
  110. ut_asserteq(1, args.args_count);
  111. ut_asserteq(1, args.args[0]);
  112. ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
  113. ut_assertok(ret);
  114. ut_asserteq(1, args.args_count);
  115. ut_asserteq(4, args.args[0]);
  116. ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
  117. ut_assertok(ret);
  118. ut_asserteq(5, args.args_count);
  119. ut_asserteq(5, args.args[0]);
  120. ut_asserteq(1, args.args[4]);
  121. ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
  122. ut_asserteq(-ENOENT, ret);
  123. ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
  124. ut_assertok(ret);
  125. ut_asserteq(1, args.args_count);
  126. ut_asserteq(12, args.args[0]);
  127. ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
  128. ut_asserteq(-ENOENT, ret);
  129. /* Test ofnode_count_phandle_with_args with cell count */
  130. ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
  131. ut_asserteq(-ENOENT, ret);
  132. ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
  133. ut_asserteq(3, ret);
  134. /* Test ofnode_parse_phandle_with_args with cell count */
  135. ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
  136. ut_assertok(ret);
  137. ut_asserteq(1, ofnode_valid(args.node));
  138. ut_asserteq(1, args.args_count);
  139. ut_asserteq(10, args.args[0]);
  140. ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
  141. ut_asserteq(-EINVAL, ret);
  142. ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
  143. ut_assertok(ret);
  144. ut_asserteq(1, ofnode_valid(args.node));
  145. ut_asserteq(1, args.args_count);
  146. ut_asserteq(30, args.args[0]);
  147. ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
  148. ut_asserteq(-ENOENT, ret);
  149. return 0;
  150. }
  151. DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  152. static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
  153. {
  154. const char *str;
  155. const u32 *val;
  156. ofnode node;
  157. int size;
  158. str = ofnode_read_chosen_string("setting");
  159. ut_assertnonnull(str);
  160. ut_asserteq_str("sunrise ohoka", str);
  161. ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
  162. node = ofnode_get_chosen_node("other-node");
  163. ut_assert(ofnode_valid(node));
  164. ut_asserteq_str("c-test@5", ofnode_get_name(node));
  165. node = ofnode_get_chosen_node("setting");
  166. ut_assert(!ofnode_valid(node));
  167. val = ofnode_read_chosen_prop("int-values", &size);
  168. ut_assertnonnull(val);
  169. ut_asserteq(8, size);
  170. ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
  171. ut_asserteq(72993, fdt32_to_cpu(val[1]));
  172. return 0;
  173. }
  174. DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  175. static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
  176. {
  177. const void *val;
  178. ofnode node;
  179. int size;
  180. node = ofnode_get_aliases_node("eth3");
  181. ut_assert(ofnode_valid(node));
  182. ut_asserteq_str("sbe5", ofnode_get_name(node));
  183. node = ofnode_get_aliases_node("unknown");
  184. ut_assert(!ofnode_valid(node));
  185. val = ofnode_read_aliases_prop("spi0", &size);
  186. ut_assertnonnull(val);
  187. ut_asserteq(7, size);
  188. ut_asserteq_str("/spi@0", (const char *)val);
  189. return 0;
  190. }
  191. DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  192. static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
  193. {
  194. ofnode node, child_node;
  195. u32 val;
  196. node = ofnode_path("/i-test");
  197. ut_assert(ofnode_valid(node));
  198. val = ofnode_get_child_count(node);
  199. ut_asserteq(3, val);
  200. child_node = ofnode_first_subnode(node);
  201. ut_assert(ofnode_valid(child_node));
  202. val = ofnode_get_child_count(child_node);
  203. ut_asserteq(0, val);
  204. return 0;
  205. }
  206. DM_TEST(dm_test_ofnode_get_child_count,
  207. UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);