phy.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
  4. * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <generic-phy.h>
  9. #include <log.h>
  10. #include <dm/test.h>
  11. #include <test/test.h>
  12. #include <test/ut.h>
  13. /* Base test of the phy uclass */
  14. static int dm_test_phy_base(struct unit_test_state *uts)
  15. {
  16. struct udevice *dev;
  17. struct phy phy1_method1;
  18. struct phy phy1_method2;
  19. struct phy phy2;
  20. struct phy phy3;
  21. struct udevice *parent;
  22. /* Get the device using the phy device*/
  23. ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS,
  24. "gen_phy_user", &parent));
  25. /*
  26. * Get the same phy port in 2 different ways and compare.
  27. */
  28. ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1_method1));
  29. ut_assertok(generic_phy_get_by_index(parent, 0, &phy1_method2));
  30. ut_asserteq(phy1_method1.id, phy1_method2.id);
  31. /*
  32. * Get the second phy port. Check that the same phy provider (device)
  33. * provides this 2nd phy port, but that the IDs are different
  34. */
  35. ut_assertok(generic_phy_get_by_name(parent, "phy2", &phy2));
  36. ut_asserteq_ptr(phy1_method2.dev, phy2.dev);
  37. ut_assert(phy1_method1.id != phy2.id);
  38. /*
  39. * Get the third phy port. Check that the phy provider is different
  40. */
  41. ut_assertok(generic_phy_get_by_name(parent, "phy3", &phy3));
  42. ut_assert(phy2.dev != phy3.dev);
  43. /* Try to get a non-existing phy */
  44. ut_asserteq(-ENODEV, uclass_get_device(UCLASS_PHY, 4, &dev));
  45. ut_asserteq(-ENODATA, generic_phy_get_by_name(parent,
  46. "phy_not_existing", &phy1_method1));
  47. return 0;
  48. }
  49. DM_TEST(dm_test_phy_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  50. /* Test of the phy uclass using the sandbox phy driver operations */
  51. static int dm_test_phy_ops(struct unit_test_state *uts)
  52. {
  53. struct phy phy1;
  54. struct phy phy2;
  55. struct phy phy3;
  56. struct udevice *parent;
  57. ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS,
  58. "gen_phy_user", &parent));
  59. ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1));
  60. ut_asserteq(0, phy1.id);
  61. ut_assertok(generic_phy_get_by_name(parent, "phy2", &phy2));
  62. ut_asserteq(1, phy2.id);
  63. ut_assertok(generic_phy_get_by_name(parent, "phy3", &phy3));
  64. ut_asserteq(0, phy3.id);
  65. /* test normal operations */
  66. ut_assertok(generic_phy_init(&phy1));
  67. ut_assertok(generic_phy_power_on(&phy1));
  68. ut_assertok(generic_phy_power_off(&phy1));
  69. /*
  70. * Test power_on() failure after exit().
  71. * The sandbox phy driver does not allow power-on/off after
  72. * exit, but the uclass counts power-on/init calls and skips
  73. * calling the driver's ops when e.g. powering off an already
  74. * powered-off phy.
  75. */
  76. ut_assertok(generic_phy_exit(&phy1));
  77. ut_assert(generic_phy_power_on(&phy1) != 0);
  78. ut_assertok(generic_phy_power_off(&phy1));
  79. /*
  80. * test normal operations again (after re-init)
  81. */
  82. ut_assertok(generic_phy_init(&phy1));
  83. ut_assertok(generic_phy_power_on(&phy1));
  84. ut_assertok(generic_phy_power_off(&phy1));
  85. /*
  86. * test calling unimplemented feature.
  87. * The call is expected to succeed
  88. */
  89. ut_assertok(generic_phy_reset(&phy1));
  90. /*
  91. * Test power_off() failure after exit().
  92. * For this we need to call exit() while the phy is powered-on,
  93. * so that the uclass actually calls the driver's power-off()
  94. * and reports the resulting failure.
  95. */
  96. ut_assertok(generic_phy_power_on(&phy1));
  97. ut_assertok(generic_phy_exit(&phy1));
  98. ut_assert(generic_phy_power_off(&phy1) != 0);
  99. ut_assertok(generic_phy_power_on(&phy1));
  100. /* PHY2 has a known problem with power off */
  101. ut_assertok(generic_phy_init(&phy2));
  102. ut_assertok(generic_phy_power_on(&phy2));
  103. ut_asserteq(-EIO, generic_phy_power_off(&phy2));
  104. /* PHY3 has a known problem with power off and power on */
  105. ut_assertok(generic_phy_init(&phy3));
  106. ut_asserteq(-EIO, generic_phy_power_on(&phy3));
  107. ut_assertok(generic_phy_power_off(&phy3));
  108. return 0;
  109. }
  110. DM_TEST(dm_test_phy_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  111. static int dm_test_phy_bulk(struct unit_test_state *uts)
  112. {
  113. struct phy_bulk phys;
  114. struct udevice *parent;
  115. /* test normal operations */
  116. ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS,
  117. "gen_phy_user1", &parent));
  118. ut_assertok(generic_phy_get_bulk(parent, &phys));
  119. ut_asserteq(2, phys.count);
  120. ut_asserteq(0, generic_phy_init_bulk(&phys));
  121. ut_asserteq(0, generic_phy_power_on_bulk(&phys));
  122. ut_asserteq(0, generic_phy_power_off_bulk(&phys));
  123. ut_asserteq(0, generic_phy_exit_bulk(&phys));
  124. /* has a known problem phy */
  125. ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS,
  126. "gen_phy_user", &parent));
  127. ut_assertok(generic_phy_get_bulk(parent, &phys));
  128. ut_asserteq(3, phys.count);
  129. ut_asserteq(0, generic_phy_init_bulk(&phys));
  130. ut_asserteq(-EIO, generic_phy_power_on_bulk(&phys));
  131. ut_asserteq(-EIO, generic_phy_power_off_bulk(&phys));
  132. ut_asserteq(0, generic_phy_exit_bulk(&phys));
  133. return 0;
  134. }
  135. DM_TEST(dm_test_phy_bulk, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  136. static int dm_test_phy_multi_exit(struct unit_test_state *uts)
  137. {
  138. struct phy phy1_method1;
  139. struct phy phy1_method2;
  140. struct phy phy1_method3;
  141. struct udevice *parent;
  142. /* Get the same phy instance in 3 different ways. */
  143. ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS,
  144. "gen_phy_user", &parent));
  145. ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1_method1));
  146. ut_asserteq(0, phy1_method1.id);
  147. ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1_method2));
  148. ut_asserteq(0, phy1_method2.id);
  149. ut_asserteq_ptr(phy1_method1.dev, phy1_method1.dev);
  150. ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS,
  151. "gen_phy_user1", &parent));
  152. ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1_method3));
  153. ut_asserteq(0, phy1_method3.id);
  154. ut_asserteq_ptr(phy1_method1.dev, phy1_method3.dev);
  155. /*
  156. * Test using the same PHY from different handles.
  157. * In non-test code these could be in different drivers.
  158. */
  159. /*
  160. * These must only call the driver's ops at the first init()
  161. * and power_on().
  162. */
  163. ut_assertok(generic_phy_init(&phy1_method1));
  164. ut_assertok(generic_phy_init(&phy1_method2));
  165. ut_assertok(generic_phy_power_on(&phy1_method1));
  166. ut_assertok(generic_phy_power_on(&phy1_method2));
  167. ut_assertok(generic_phy_init(&phy1_method3));
  168. ut_assertok(generic_phy_power_on(&phy1_method3));
  169. /*
  170. * These must not call the driver's ops as other handles still
  171. * want the PHY powered-on and initialized.
  172. */
  173. ut_assertok(generic_phy_power_off(&phy1_method3));
  174. ut_assertok(generic_phy_exit(&phy1_method3));
  175. /*
  176. * We would get an error here if the generic_phy_exit() above
  177. * actually called the driver's exit(), as the sandbox driver
  178. * doesn't allow power-off() after exit().
  179. */
  180. ut_assertok(generic_phy_power_off(&phy1_method1));
  181. ut_assertok(generic_phy_power_off(&phy1_method2));
  182. ut_assertok(generic_phy_exit(&phy1_method1));
  183. ut_assertok(generic_phy_exit(&phy1_method2));
  184. return 0;
  185. }
  186. DM_TEST(dm_test_phy_multi_exit, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);