test_drv.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /* This is for dtoc testing only */
  85. .per_device_plat_auto = sizeof(struct dm_test_uclass_priv),
  86. };
  87. static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
  88. {
  89. const struct dm_test_pdata *pdata = dev_get_plat(dev);
  90. struct dm_test_priv *priv = dev_get_priv(dev);
  91. *pingret = pingval + pdata->ping_add;
  92. priv->ping_total += *pingret;
  93. return 0;
  94. }
  95. static const struct test_ops test_ops = {
  96. .ping = testfdt_drv_ping,
  97. };
  98. static int testfdt_of_to_plat(struct udevice *dev)
  99. {
  100. struct dm_test_pdata *pdata = dev_get_plat(dev);
  101. pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  102. "ping-add", -1);
  103. pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
  104. "ping-expect");
  105. return 0;
  106. }
  107. static int testfdt_drv_probe(struct udevice *dev)
  108. {
  109. struct dm_test_priv *priv = dev_get_priv(dev);
  110. priv->ping_total += DM_TEST_START_TOTAL;
  111. /*
  112. * If this device is on a bus, the uclass_flag will be set before
  113. * calling this function. In the meantime the uclass_postp is
  114. * initlized to a value -1. These are used respectively by
  115. * dm_test_bus_child_pre_probe_uclass() and
  116. * dm_test_bus_child_post_probe_uclass().
  117. */
  118. priv->uclass_total += priv->uclass_flag;
  119. priv->uclass_postp = -1;
  120. return 0;
  121. }
  122. static const struct udevice_id testfdt_ids[] = {
  123. { .compatible = "denx,u-boot-fdt-test", .data = DM_TEST_TYPE_FIRST },
  124. { .compatible = "google,another-fdt-test", .data = DM_TEST_TYPE_SECOND },
  125. { }
  126. };
  127. DM_DRIVER_ALIAS(denx_u_boot_fdt_test, google_another_fdt_test)
  128. U_BOOT_DRIVER(denx_u_boot_fdt_test) = {
  129. .name = "testfdt_drv",
  130. .of_match = testfdt_ids,
  131. .id = UCLASS_TEST_FDT,
  132. .of_to_plat = testfdt_of_to_plat,
  133. .probe = testfdt_drv_probe,
  134. .ops = &test_ops,
  135. .priv_auto = sizeof(struct dm_test_priv),
  136. .plat_auto = sizeof(struct dm_test_pdata),
  137. };
  138. static const struct udevice_id testfdt1_ids[] = {
  139. { .compatible = "denx,u-boot-fdt-test1", .data = DM_TEST_TYPE_FIRST },
  140. { }
  141. };
  142. U_BOOT_DRIVER(testfdt1_drv) = {
  143. .name = "testfdt1_drv",
  144. .of_match = testfdt1_ids,
  145. .id = UCLASS_TEST_FDT,
  146. .of_to_plat = testfdt_of_to_plat,
  147. .probe = testfdt_drv_probe,
  148. .ops = &test_ops,
  149. .priv_auto = sizeof(struct dm_test_priv),
  150. .plat_auto = sizeof(struct dm_test_pdata),
  151. .flags = DM_FLAG_PRE_RELOC,
  152. };
  153. /* From here is the testfdt uclass code */
  154. int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
  155. {
  156. const struct test_ops *ops = device_get_ops(dev);
  157. if (!ops->ping)
  158. return -ENOSYS;
  159. return ops->ping(dev, pingval, pingret);
  160. }
  161. UCLASS_DRIVER(testfdt) = {
  162. .name = "testfdt",
  163. .id = UCLASS_TEST_FDT,
  164. .flags = DM_UC_FLAG_SEQ_ALIAS,
  165. .priv_auto = sizeof(struct dm_test_uc_priv),
  166. };
  167. static const struct udevice_id testfdtm_ids[] = {
  168. { .compatible = "denx,u-boot-fdtm-test" },
  169. { }
  170. };
  171. U_BOOT_DRIVER(testfdtm_drv) = {
  172. .name = "testfdtm_drv",
  173. .of_match = testfdtm_ids,
  174. .id = UCLASS_TEST_FDT_MANUAL,
  175. };
  176. UCLASS_DRIVER(testfdtm) = {
  177. .name = "testfdtm",
  178. .id = UCLASS_TEST_FDT_MANUAL,
  179. .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
  180. };