ofnode.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_by_prop_value(struct unit_test_state *uts)
  18. {
  19. const char propname[] = "compatible";
  20. const char propval[] = "denx,u-boot-fdt-test";
  21. const char *str;
  22. ofnode node = ofnode_null();
  23. /* Find first matching node, there should be at least one */
  24. node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
  25. ut_assert(ofnode_valid(node));
  26. str = ofnode_read_string(node, propname);
  27. ut_assert(str && !strcmp(str, propval));
  28. /* Find the rest of the matching nodes */
  29. while (true) {
  30. node = ofnode_by_prop_value(node, propname, propval,
  31. sizeof(propval));
  32. if (!ofnode_valid(node))
  33. break;
  34. str = ofnode_read_string(node, propname);
  35. ut_assert(str && !strcmp(str, propval));
  36. }
  37. return 0;
  38. }
  39. DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
  40. static int dm_test_ofnode_fmap(struct unit_test_state *uts)
  41. {
  42. struct fmap_entry entry;
  43. ofnode node;
  44. node = ofnode_path("/cros-ec/flash");
  45. ut_assert(ofnode_valid(node));
  46. ut_assertok(ofnode_read_fmap_entry(node, &entry));
  47. ut_asserteq(0x08000000, entry.offset);
  48. ut_asserteq(0x20000, entry.length);
  49. return 0;
  50. }
  51. DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  52. static int dm_test_ofnode_read(struct unit_test_state *uts)
  53. {
  54. const u32 *val;
  55. ofnode node;
  56. int size;
  57. node = ofnode_path("/a-test");
  58. ut_assert(ofnode_valid(node));
  59. val = ofnode_read_prop(node, "int-value", &size);
  60. ut_assertnonnull(val);
  61. ut_asserteq(4, size);
  62. ut_asserteq(1234, fdt32_to_cpu(val[0]));
  63. val = ofnode_read_prop(node, "missing", &size);
  64. ut_assertnull(val);
  65. ut_asserteq(-FDT_ERR_NOTFOUND, size);
  66. /* Check it works without a size parameter */
  67. val = ofnode_read_prop(node, "missing", NULL);
  68. ut_assertnull(val);
  69. return 0;
  70. }
  71. DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  72. static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
  73. {
  74. const char *str;
  75. const u32 *val;
  76. ofnode node;
  77. int size;
  78. str = ofnode_read_chosen_string("setting");
  79. ut_assertnonnull(str);
  80. ut_asserteq_str("sunrise ohoka", str);
  81. ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
  82. node = ofnode_get_chosen_node("other-node");
  83. ut_assert(ofnode_valid(node));
  84. ut_asserteq_str("c-test@5", ofnode_get_name(node));
  85. node = ofnode_get_chosen_node("setting");
  86. ut_assert(!ofnode_valid(node));
  87. val = ofnode_read_chosen_prop("int-values", &size);
  88. ut_assertnonnull(val);
  89. ut_asserteq(8, size);
  90. ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
  91. ut_asserteq(72993, fdt32_to_cpu(val[1]));
  92. return 0;
  93. }
  94. DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  95. static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
  96. {
  97. ofnode node, child_node;
  98. u32 val;
  99. node = ofnode_path("/i-test");
  100. ut_assert(ofnode_valid(node));
  101. val = ofnode_get_child_count(node);
  102. ut_asserteq(3, val);
  103. child_node = ofnode_first_subnode(node);
  104. ut_assert(ofnode_valid(child_node));
  105. val = ofnode_get_child_count(child_node);
  106. ut_asserteq(0, val);
  107. return 0;
  108. }
  109. DM_TEST(dm_test_ofnode_get_child_count,
  110. UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);