test_drv.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2014 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <dm/test.h>
  8. #include <asm/global_data.h>
  9. /* Records the last testbus device that was removed */
  10. static struct udevice *testbus_removed;
  11. struct udevice *testbus_get_clear_removed(void)
  12. {
  13. struct udevice *removed = testbus_removed;
  14. testbus_removed = NULL;
  15. return removed;
  16. }
  17. static int testbus_drv_probe(struct udevice *dev)
  18. {
  19. if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
  20. int ret;
  21. ret = dm_scan_fdt_dev(dev);
  22. if (ret)
  23. return ret;
  24. }
  25. return 0;
  26. }
  27. static int testbus_child_post_bind(struct udevice *dev)
  28. {
  29. struct dm_test_parent_plat *plat;
  30. plat = dev_get_parent_plat(dev);
  31. plat->bind_flag = 1;
  32. plat->uclass_bind_flag = 2;
  33. return 0;
  34. }
  35. static int testbus_child_pre_probe(struct udevice *dev)
  36. {
  37. struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
  38. parent_data->flag += TEST_FLAG_CHILD_PROBED;
  39. return 0;
  40. }
  41. static int testbus_child_pre_probe_uclass(struct udevice *dev)
  42. {
  43. struct dm_test_priv *priv = dev_get_priv(dev);
  44. priv->uclass_flag++;
  45. return 0;
  46. }
  47. static int testbus_child_post_probe_uclass(struct udevice *dev)
  48. {
  49. struct dm_test_priv *priv = dev_get_priv(dev);
  50. priv->uclass_postp++;
  51. return 0;
  52. }
  53. static int testbus_child_post_remove(struct udevice *dev)
  54. {
  55. struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
  56. parent_data->flag += TEST_FLAG_CHILD_REMOVED;
  57. testbus_removed = dev;
  58. return 0;
  59. }
  60. static const struct udevice_id testbus_ids[] = {
  61. { .compatible = "denx,u-boot-test-bus", .data = DM_TEST_TYPE_FIRST },
  62. { }
  63. };
  64. U_BOOT_DRIVER(denx_u_boot_test_bus) = {
  65. .name = "testbus_drv",
  66. .of_match = testbus_ids,
  67. .id = UCLASS_TEST_BUS,
  68. .probe = testbus_drv_probe,
  69. .child_post_bind = testbus_child_post_bind,
  70. .priv_auto = sizeof(struct dm_test_priv),
  71. .plat_auto = sizeof(struct dm_test_pdata),
  72. .per_child_auto = sizeof(struct dm_test_parent_data),
  73. .per_child_plat_auto = sizeof(struct dm_test_parent_plat),
  74. .child_pre_probe = testbus_child_pre_probe,
  75. .child_post_remove = testbus_child_post_remove,
  76. DM_HEADER(<test.h>)
  77. };
  78. UCLASS_DRIVER(testbus) = {
  79. .name = "testbus",
  80. .id = UCLASS_TEST_BUS,
  81. .flags = DM_UC_FLAG_SEQ_ALIAS,
  82. .child_pre_probe = testbus_child_pre_probe_uclass,
  83. .child_post_probe = testbus_child_post_probe_uclass,
  84. .per_device_auto = sizeof(struct dm_test_uclass_priv),
  85. /* Note: this is for dtoc testing as well as tags*/
  86. .per_device_plat_auto = sizeof(struct dm_test_uclass_plat),
  87. };
  88. static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
  89. {
  90. const struct dm_test_pdata *pdata = dev_get_plat(dev);
  91. struct dm_test_priv *priv = dev_get_priv(dev);
  92. *pingret = pingval + pdata->ping_add;
  93. priv->ping_total += *pingret;
  94. return 0;
  95. }
  96. static const struct test_ops test_ops = {
  97. .ping = testfdt_drv_ping,
  98. };
  99. static int testfdt_of_to_plat(struct udevice *dev)
  100. {
  101. struct dm_test_pdata *pdata = dev_get_plat(dev);
  102. pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  103. "ping-add", -1);
  104. pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
  105. "ping-expect");
  106. return 0;
  107. }
  108. static int testfdt_drv_probe(struct udevice *dev)
  109. {
  110. struct dm_test_priv *priv = dev_get_priv(dev);
  111. priv->ping_total += DM_TEST_START_TOTAL;
  112. /*
  113. * If this device is on a bus, the uclass_flag will be set before
  114. * calling this function. In the meantime the uclass_postp is
  115. * initlized to a value -1. These are used respectively by
  116. * dm_test_bus_child_pre_probe_uclass() and
  117. * dm_test_bus_child_post_probe_uclass().
  118. */
  119. priv->uclass_total += priv->uclass_flag;
  120. priv->uclass_postp = -1;
  121. return 0;
  122. }
  123. static const struct udevice_id testfdt_ids[] = {
  124. { .compatible = "denx,u-boot-fdt-test", .data = DM_TEST_TYPE_FIRST },
  125. { .compatible = "google,another-fdt-test", .data = DM_TEST_TYPE_SECOND },
  126. { }
  127. };
  128. DM_DRIVER_ALIAS(denx_u_boot_fdt_test, google_another_fdt_test)
  129. U_BOOT_DRIVER(denx_u_boot_fdt_test) = {
  130. .name = "testfdt_drv",
  131. .of_match = testfdt_ids,
  132. .id = UCLASS_TEST_FDT,
  133. .of_to_plat = testfdt_of_to_plat,
  134. .probe = testfdt_drv_probe,
  135. .ops = &test_ops,
  136. .priv_auto = sizeof(struct dm_test_priv),
  137. .plat_auto = sizeof(struct dm_test_pdata),
  138. };
  139. static const struct udevice_id testfdt1_ids[] = {
  140. { .compatible = "denx,u-boot-fdt-test1", .data = DM_TEST_TYPE_FIRST },
  141. { }
  142. };
  143. U_BOOT_DRIVER(testfdt1_drv) = {
  144. .name = "testfdt1_drv",
  145. .of_match = testfdt1_ids,
  146. .id = UCLASS_TEST_FDT,
  147. .of_to_plat = testfdt_of_to_plat,
  148. .probe = testfdt_drv_probe,
  149. .ops = &test_ops,
  150. .priv_auto = sizeof(struct dm_test_priv),
  151. .plat_auto = sizeof(struct dm_test_pdata),
  152. .flags = DM_FLAG_PRE_RELOC,
  153. };
  154. /* From here is the testfdt uclass code */
  155. int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
  156. {
  157. const struct test_ops *ops = device_get_ops(dev);
  158. if (!ops->ping)
  159. return -ENOSYS;
  160. return ops->ping(dev, pingval, pingret);
  161. }
  162. UCLASS_DRIVER(testfdt) = {
  163. .name = "testfdt",
  164. .id = UCLASS_TEST_FDT,
  165. .flags = DM_UC_FLAG_SEQ_ALIAS,
  166. .priv_auto = sizeof(struct dm_test_uc_priv),
  167. };
  168. static const struct udevice_id testfdtm_ids[] = {
  169. { .compatible = "denx,u-boot-fdtm-test" },
  170. { }
  171. };
  172. U_BOOT_DRIVER(testfdtm_drv) = {
  173. .name = "testfdtm_drv",
  174. .of_match = testfdtm_ids,
  175. .id = UCLASS_TEST_FDT_MANUAL,
  176. };
  177. UCLASS_DRIVER(testfdtm) = {
  178. .name = "testfdtm",
  179. .id = UCLASS_TEST_FDT_MANUAL,
  180. .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
  181. };