test-fdt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <fdtdec.h>
  10. #include <malloc.h>
  11. #include <asm/io.h>
  12. #include <dm/test.h>
  13. #include <dm/root.h>
  14. #include <dm/uclass-internal.h>
  15. #include <dm/util.h>
  16. #include <test/ut.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
  19. {
  20. const struct dm_test_pdata *pdata = dev->platdata;
  21. struct dm_test_priv *priv = dev_get_priv(dev);
  22. *pingret = pingval + pdata->ping_add;
  23. priv->ping_total += *pingret;
  24. return 0;
  25. }
  26. static const struct test_ops test_ops = {
  27. .ping = testfdt_drv_ping,
  28. };
  29. static int testfdt_ofdata_to_platdata(struct udevice *dev)
  30. {
  31. struct dm_test_pdata *pdata = dev_get_platdata(dev);
  32. pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  33. "ping-add", -1);
  34. pdata->base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset,
  35. "ping-expect");
  36. return 0;
  37. }
  38. static int testfdt_drv_probe(struct udevice *dev)
  39. {
  40. struct dm_test_priv *priv = dev_get_priv(dev);
  41. priv->ping_total += DM_TEST_START_TOTAL;
  42. /*
  43. * If this device is on a bus, the uclass_flag will be set before
  44. * calling this function. This is used by
  45. * dm_test_bus_child_pre_probe_uclass().
  46. */
  47. priv->uclass_total += priv->uclass_flag;
  48. return 0;
  49. }
  50. static const struct udevice_id testfdt_ids[] = {
  51. {
  52. .compatible = "denx,u-boot-fdt-test",
  53. .data = DM_TEST_TYPE_FIRST },
  54. {
  55. .compatible = "google,another-fdt-test",
  56. .data = DM_TEST_TYPE_SECOND },
  57. { }
  58. };
  59. U_BOOT_DRIVER(testfdt_drv) = {
  60. .name = "testfdt_drv",
  61. .of_match = testfdt_ids,
  62. .id = UCLASS_TEST_FDT,
  63. .ofdata_to_platdata = testfdt_ofdata_to_platdata,
  64. .probe = testfdt_drv_probe,
  65. .ops = &test_ops,
  66. .priv_auto_alloc_size = sizeof(struct dm_test_priv),
  67. .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
  68. };
  69. /* From here is the testfdt uclass code */
  70. int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
  71. {
  72. const struct test_ops *ops = device_get_ops(dev);
  73. if (!ops->ping)
  74. return -ENOSYS;
  75. return ops->ping(dev, pingval, pingret);
  76. }
  77. UCLASS_DRIVER(testfdt) = {
  78. .name = "testfdt",
  79. .id = UCLASS_TEST_FDT,
  80. .flags = DM_UC_FLAG_SEQ_ALIAS,
  81. };
  82. int dm_check_devices(struct unit_test_state *uts, int num_devices)
  83. {
  84. struct udevice *dev;
  85. int ret;
  86. int i;
  87. /*
  88. * Now check that the ping adds are what we expect. This is using the
  89. * ping-add property in each node.
  90. */
  91. for (i = 0; i < num_devices; i++) {
  92. uint32_t base;
  93. ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
  94. ut_assert(!ret);
  95. /*
  96. * Get the 'ping-expect' property, which tells us what the
  97. * ping add should be. We don't use the platdata because we
  98. * want to test the code that sets that up
  99. * (testfdt_drv_probe()).
  100. */
  101. base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset,
  102. "ping-expect");
  103. debug("dev=%d, base=%d: %s\n", i, base,
  104. fdt_get_name(gd->fdt_blob, dev->of_offset, NULL));
  105. ut_assert(!dm_check_operations(uts, dev, base,
  106. dev_get_priv(dev)));
  107. }
  108. return 0;
  109. }
  110. /* Test that FDT-based binding works correctly */
  111. static int dm_test_fdt(struct unit_test_state *uts)
  112. {
  113. const int num_devices = 6;
  114. struct udevice *dev;
  115. struct uclass *uc;
  116. int ret;
  117. int i;
  118. ret = dm_scan_fdt(gd->fdt_blob, false);
  119. ut_assert(!ret);
  120. ret = uclass_get(UCLASS_TEST_FDT, &uc);
  121. ut_assert(!ret);
  122. /* These are num_devices compatible root-level device tree nodes */
  123. ut_asserteq(num_devices, list_count_items(&uc->dev_head));
  124. /* Each should have platform data but no private data */
  125. for (i = 0; i < num_devices; i++) {
  126. ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
  127. ut_assert(!ret);
  128. ut_assert(!dev_get_priv(dev));
  129. ut_assert(dev->platdata);
  130. }
  131. ut_assertok(dm_check_devices(uts, num_devices));
  132. return 0;
  133. }
  134. DM_TEST(dm_test_fdt, 0);
  135. static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
  136. {
  137. struct uclass *uc;
  138. int ret;
  139. ret = dm_scan_fdt(gd->fdt_blob, true);
  140. ut_assert(!ret);
  141. ret = uclass_get(UCLASS_TEST_FDT, &uc);
  142. ut_assert(!ret);
  143. /* These is only one pre-reloc device */
  144. ut_asserteq(1, list_count_items(&uc->dev_head));
  145. return 0;
  146. }
  147. DM_TEST(dm_test_fdt_pre_reloc, 0);
  148. /* Test that sequence numbers are allocated properly */
  149. static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
  150. {
  151. struct udevice *dev;
  152. /* A few basic santiy tests */
  153. ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
  154. ut_asserteq_str("b-test", dev->name);
  155. ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev));
  156. ut_asserteq_str("a-test", dev->name);
  157. ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5,
  158. true, &dev));
  159. ut_asserteq_ptr(NULL, dev);
  160. /* Test aliases */
  161. ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
  162. ut_asserteq_str("e-test", dev->name);
  163. ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
  164. true, &dev));
  165. /*
  166. * Note that c-test nodes are not probed since it is not a top-level
  167. * node
  168. */
  169. ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
  170. ut_asserteq_str("b-test", dev->name);
  171. /*
  172. * d-test wants sequence number 3 also, but it can't have it because
  173. * b-test gets it first.
  174. */
  175. ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
  176. ut_asserteq_str("d-test", dev->name);
  177. /* d-test actually gets 0 */
  178. ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 0, &dev));
  179. ut_asserteq_str("d-test", dev->name);
  180. /* initially no one wants seq 1 */
  181. ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 1,
  182. &dev));
  183. ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
  184. ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev));
  185. /* But now that it is probed, we can find it */
  186. ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, &dev));
  187. ut_asserteq_str("f-test", dev->name);
  188. return 0;
  189. }
  190. DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  191. /* Test that we can find a device by device tree offset */
  192. static int dm_test_fdt_offset(struct unit_test_state *uts)
  193. {
  194. const void *blob = gd->fdt_blob;
  195. struct udevice *dev;
  196. int node;
  197. node = fdt_path_offset(blob, "/e-test");
  198. ut_assert(node > 0);
  199. ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
  200. &dev));
  201. ut_asserteq_str("e-test", dev->name);
  202. /* This node should not be bound */
  203. node = fdt_path_offset(blob, "/junk");
  204. ut_assert(node > 0);
  205. ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
  206. node, &dev));
  207. /* This is not a top level node so should not be probed */
  208. node = fdt_path_offset(blob, "/some-bus/c-test@5");
  209. ut_assert(node > 0);
  210. ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
  211. node, &dev));
  212. return 0;
  213. }
  214. DM_TEST(dm_test_fdt_offset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);