test-driver.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013 Google, Inc
  4. *
  5. * (C) Copyright 2012
  6. * Pavel Herrmann <morpheus.ibis@gmail.com>
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <errno.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <asm/io.h>
  14. #include <dm/test.h>
  15. #include <test/test.h>
  16. #include <test/ut.h>
  17. int dm_testdrv_op_count[DM_TEST_OP_COUNT];
  18. static struct unit_test_state *uts = &global_dm_test_state;
  19. static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
  20. {
  21. const struct dm_test_pdata *pdata = dev_get_platdata(dev);
  22. struct dm_test_priv *priv = dev_get_priv(dev);
  23. *pingret = pingval + pdata->ping_add;
  24. priv->ping_total += *pingret;
  25. return 0;
  26. }
  27. static const struct test_ops test_ops = {
  28. .ping = testdrv_ping,
  29. };
  30. static int test_bind(struct udevice *dev)
  31. {
  32. /* Private data should not be allocated */
  33. ut_assert(!dev_get_priv(dev));
  34. dm_testdrv_op_count[DM_TEST_OP_BIND]++;
  35. return 0;
  36. }
  37. static int test_probe(struct udevice *dev)
  38. {
  39. struct dm_test_priv *priv = dev_get_priv(dev);
  40. /* Private data should be allocated */
  41. ut_assert(priv);
  42. dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
  43. priv->ping_total += DM_TEST_START_TOTAL;
  44. return 0;
  45. }
  46. static int test_remove(struct udevice *dev)
  47. {
  48. /* Private data should still be allocated */
  49. ut_assert(dev_get_priv(dev));
  50. dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
  51. return 0;
  52. }
  53. static int test_unbind(struct udevice *dev)
  54. {
  55. /* Private data should not be allocated */
  56. ut_assert(!dev->priv);
  57. dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
  58. return 0;
  59. }
  60. U_BOOT_DRIVER(test_drv) = {
  61. .name = "test_drv",
  62. .id = UCLASS_TEST,
  63. .ops = &test_ops,
  64. .bind = test_bind,
  65. .probe = test_probe,
  66. .remove = test_remove,
  67. .unbind = test_unbind,
  68. .priv_auto_alloc_size = sizeof(struct dm_test_priv),
  69. };
  70. U_BOOT_DRIVER(test2_drv) = {
  71. .name = "test2_drv",
  72. .id = UCLASS_TEST,
  73. .ops = &test_ops,
  74. .bind = test_bind,
  75. .probe = test_probe,
  76. .remove = test_remove,
  77. .unbind = test_unbind,
  78. .priv_auto_alloc_size = sizeof(struct dm_test_priv),
  79. };
  80. static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
  81. {
  82. *pingret = pingval + 2;
  83. return 0;
  84. }
  85. static const struct test_ops test_manual_ops = {
  86. .ping = test_manual_drv_ping,
  87. };
  88. static int test_manual_bind(struct udevice *dev)
  89. {
  90. dm_testdrv_op_count[DM_TEST_OP_BIND]++;
  91. return 0;
  92. }
  93. static int test_manual_probe(struct udevice *dev)
  94. {
  95. struct dm_test_state *dms = uts->priv;
  96. dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
  97. if (!dms->force_fail_alloc)
  98. dev->priv = calloc(1, sizeof(struct dm_test_priv));
  99. if (!dev->priv)
  100. return -ENOMEM;
  101. return 0;
  102. }
  103. static int test_manual_remove(struct udevice *dev)
  104. {
  105. dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
  106. return 0;
  107. }
  108. static int test_manual_unbind(struct udevice *dev)
  109. {
  110. dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
  111. return 0;
  112. }
  113. U_BOOT_DRIVER(test_manual_drv) = {
  114. .name = "test_manual_drv",
  115. .id = UCLASS_TEST,
  116. .ops = &test_manual_ops,
  117. .bind = test_manual_bind,
  118. .probe = test_manual_probe,
  119. .remove = test_manual_remove,
  120. .unbind = test_manual_unbind,
  121. };
  122. U_BOOT_DRIVER(test_pre_reloc_drv) = {
  123. .name = "test_pre_reloc_drv",
  124. .id = UCLASS_TEST,
  125. .ops = &test_manual_ops,
  126. .bind = test_manual_bind,
  127. .probe = test_manual_probe,
  128. .remove = test_manual_remove,
  129. .unbind = test_manual_unbind,
  130. .flags = DM_FLAG_PRE_RELOC,
  131. };
  132. U_BOOT_DRIVER(test_act_dma_drv) = {
  133. .name = "test_act_dma_drv",
  134. .id = UCLASS_TEST,
  135. .ops = &test_manual_ops,
  136. .bind = test_manual_bind,
  137. .probe = test_manual_probe,
  138. .remove = test_manual_remove,
  139. .unbind = test_manual_unbind,
  140. .flags = DM_FLAG_ACTIVE_DMA,
  141. };