test-driver.c 3.5 KB

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