ofnode.c 3.4 KB

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