Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/ |
| 4 | * Written by Jean-Jacques Hiblot <jjhiblot@ti.com> |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <generic-phy.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 11 | #include <dm/test.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 12 | #include <test/test.h> |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 13 | #include <test/ut.h> |
| 14 | |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 15 | /* Base test of the phy uclass */ |
| 16 | static int dm_test_phy_base(struct unit_test_state *uts) |
| 17 | { |
| 18 | struct udevice *dev; |
| 19 | struct phy phy1_method1; |
| 20 | struct phy phy1_method2; |
| 21 | struct phy phy2; |
| 22 | struct phy phy3; |
| 23 | struct udevice *parent; |
| 24 | |
| 25 | /* Get the device using the phy device*/ |
| 26 | ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS, |
| 27 | "gen_phy_user", &parent)); |
| 28 | /* |
| 29 | * Get the same phy port in 2 different ways and compare. |
| 30 | */ |
Marek Vasut | fa847bb | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 31 | ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1_method1)); |
Jonas Karlman | feb4b91 | 2023-08-31 22:16:33 +0000 | [diff] [blame] | 32 | ut_assert(generic_phy_valid(&phy1_method1)); |
Marek Vasut | fa847bb | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 33 | ut_assertok(generic_phy_get_by_index(parent, 0, &phy1_method2)); |
Jonas Karlman | feb4b91 | 2023-08-31 22:16:33 +0000 | [diff] [blame] | 34 | ut_assert(generic_phy_valid(&phy1_method2)); |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 35 | ut_asserteq(phy1_method1.id, phy1_method2.id); |
| 36 | |
| 37 | /* |
| 38 | * Get the second phy port. Check that the same phy provider (device) |
| 39 | * provides this 2nd phy port, but that the IDs are different |
| 40 | */ |
Marek Vasut | fa847bb | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 41 | ut_assertok(generic_phy_get_by_name(parent, "phy2", &phy2)); |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 42 | ut_asserteq_ptr(phy1_method2.dev, phy2.dev); |
| 43 | ut_assert(phy1_method1.id != phy2.id); |
| 44 | |
| 45 | /* |
| 46 | * Get the third phy port. Check that the phy provider is different |
| 47 | */ |
Marek Vasut | fa847bb | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 48 | ut_assertok(generic_phy_get_by_name(parent, "phy3", &phy3)); |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 49 | ut_assert(phy2.dev != phy3.dev); |
| 50 | |
| 51 | /* Try to get a non-existing phy */ |
Jonas Karlman | 14639bf | 2023-08-31 22:16:35 +0000 | [diff] [blame] | 52 | ut_asserteq(-ENODEV, uclass_get_device(UCLASS_PHY, 5, &dev)); |
Simon Glass | ac206a0 | 2017-05-18 20:09:45 -0600 | [diff] [blame] | 53 | ut_asserteq(-ENODATA, generic_phy_get_by_name(parent, |
| 54 | "phy_not_existing", &phy1_method1)); |
Jonas Karlman | feb4b91 | 2023-08-31 22:16:33 +0000 | [diff] [blame] | 55 | ut_assert(!generic_phy_valid(&phy1_method1)); |
| 56 | ut_asserteq(-ENOENT, generic_phy_get_by_index(parent, 3, |
| 57 | &phy1_method2)); |
| 58 | ut_assert(!generic_phy_valid(&phy1_method2)); |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 59 | |
Jonas Karlman | 14639bf | 2023-08-31 22:16:35 +0000 | [diff] [blame] | 60 | /* Try to get a phy where of_xlate fail */ |
| 61 | ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS, |
| 62 | "gen_phy_user2", &parent)); |
| 63 | ut_asserteq(-EINVAL, generic_phy_get_by_name(parent, "phy1", |
| 64 | &phy1_method1)); |
| 65 | ut_assert(!generic_phy_valid(&phy1_method1)); |
| 66 | ut_asserteq(-EINVAL, generic_phy_get_by_index(parent, 0, |
| 67 | &phy1_method2)); |
| 68 | ut_assert(!generic_phy_valid(&phy1_method2)); |
| 69 | |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 70 | return 0; |
| 71 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 72 | DM_TEST(dm_test_phy_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 73 | |
| 74 | /* Test of the phy uclass using the sandbox phy driver operations */ |
| 75 | static int dm_test_phy_ops(struct unit_test_state *uts) |
| 76 | { |
| 77 | struct phy phy1; |
| 78 | struct phy phy2; |
| 79 | struct phy phy3; |
| 80 | struct udevice *parent; |
| 81 | |
| 82 | ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS, |
| 83 | "gen_phy_user", &parent)); |
| 84 | |
| 85 | ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1)); |
Simon Glass | ac206a0 | 2017-05-18 20:09:45 -0600 | [diff] [blame] | 86 | ut_asserteq(0, phy1.id); |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 87 | ut_assertok(generic_phy_get_by_name(parent, "phy2", &phy2)); |
Simon Glass | ac206a0 | 2017-05-18 20:09:45 -0600 | [diff] [blame] | 88 | ut_asserteq(1, phy2.id); |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 89 | ut_assertok(generic_phy_get_by_name(parent, "phy3", &phy3)); |
Simon Glass | ac206a0 | 2017-05-18 20:09:45 -0600 | [diff] [blame] | 90 | ut_asserteq(0, phy3.id); |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 91 | |
| 92 | /* test normal operations */ |
| 93 | ut_assertok(generic_phy_init(&phy1)); |
| 94 | ut_assertok(generic_phy_power_on(&phy1)); |
| 95 | ut_assertok(generic_phy_power_off(&phy1)); |
| 96 | |
| 97 | /* |
Alper Nebi Yasak | 226fce6 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 98 | * Test power_on() failure after exit(). |
| 99 | * The sandbox phy driver does not allow power-on/off after |
| 100 | * exit, but the uclass counts power-on/init calls and skips |
| 101 | * calling the driver's ops when e.g. powering off an already |
| 102 | * powered-off phy. |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 103 | */ |
| 104 | ut_assertok(generic_phy_exit(&phy1)); |
| 105 | ut_assert(generic_phy_power_on(&phy1) != 0); |
Alper Nebi Yasak | 226fce6 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 106 | ut_assertok(generic_phy_power_off(&phy1)); |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * test normal operations again (after re-init) |
| 110 | */ |
| 111 | ut_assertok(generic_phy_init(&phy1)); |
| 112 | ut_assertok(generic_phy_power_on(&phy1)); |
| 113 | ut_assertok(generic_phy_power_off(&phy1)); |
| 114 | |
| 115 | /* |
| 116 | * test calling unimplemented feature. |
| 117 | * The call is expected to succeed |
| 118 | */ |
| 119 | ut_assertok(generic_phy_reset(&phy1)); |
| 120 | |
Alper Nebi Yasak | 226fce6 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 121 | /* |
| 122 | * Test power_off() failure after exit(). |
| 123 | * For this we need to call exit() while the phy is powered-on, |
| 124 | * so that the uclass actually calls the driver's power-off() |
| 125 | * and reports the resulting failure. |
| 126 | */ |
| 127 | ut_assertok(generic_phy_power_on(&phy1)); |
| 128 | ut_assertok(generic_phy_exit(&phy1)); |
| 129 | ut_assert(generic_phy_power_off(&phy1) != 0); |
| 130 | ut_assertok(generic_phy_power_on(&phy1)); |
| 131 | |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 132 | /* PHY2 has a known problem with power off */ |
| 133 | ut_assertok(generic_phy_init(&phy2)); |
| 134 | ut_assertok(generic_phy_power_on(&phy2)); |
Simon Glass | ac206a0 | 2017-05-18 20:09:45 -0600 | [diff] [blame] | 135 | ut_asserteq(-EIO, generic_phy_power_off(&phy2)); |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 136 | |
Simon Glass | ac206a0 | 2017-05-18 20:09:45 -0600 | [diff] [blame] | 137 | /* PHY3 has a known problem with power off and power on */ |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 138 | ut_assertok(generic_phy_init(&phy3)); |
Alper Nebi Yasak | 226fce6 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 139 | ut_asserteq(-EIO, generic_phy_power_on(&phy3)); |
| 140 | ut_assertok(generic_phy_power_off(&phy3)); |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 144 | DM_TEST(dm_test_phy_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Chunfeng Yun | 00c82ac | 2020-05-02 11:35:12 +0200 | [diff] [blame] | 145 | |
| 146 | static int dm_test_phy_bulk(struct unit_test_state *uts) |
| 147 | { |
| 148 | struct phy_bulk phys; |
| 149 | struct udevice *parent; |
| 150 | |
| 151 | /* test normal operations */ |
| 152 | ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS, |
| 153 | "gen_phy_user1", &parent)); |
| 154 | |
| 155 | ut_assertok(generic_phy_get_bulk(parent, &phys)); |
| 156 | ut_asserteq(2, phys.count); |
| 157 | |
| 158 | ut_asserteq(0, generic_phy_init_bulk(&phys)); |
| 159 | ut_asserteq(0, generic_phy_power_on_bulk(&phys)); |
| 160 | ut_asserteq(0, generic_phy_power_off_bulk(&phys)); |
| 161 | ut_asserteq(0, generic_phy_exit_bulk(&phys)); |
| 162 | |
| 163 | /* has a known problem phy */ |
| 164 | ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS, |
| 165 | "gen_phy_user", &parent)); |
| 166 | |
| 167 | ut_assertok(generic_phy_get_bulk(parent, &phys)); |
| 168 | ut_asserteq(3, phys.count); |
| 169 | |
| 170 | ut_asserteq(0, generic_phy_init_bulk(&phys)); |
| 171 | ut_asserteq(-EIO, generic_phy_power_on_bulk(&phys)); |
| 172 | ut_asserteq(-EIO, generic_phy_power_off_bulk(&phys)); |
| 173 | ut_asserteq(0, generic_phy_exit_bulk(&phys)); |
| 174 | |
| 175 | return 0; |
| 176 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 177 | DM_TEST(dm_test_phy_bulk, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Alper Nebi Yasak | 226fce6 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 178 | |
| 179 | static int dm_test_phy_multi_exit(struct unit_test_state *uts) |
| 180 | { |
| 181 | struct phy phy1_method1; |
| 182 | struct phy phy1_method2; |
| 183 | struct phy phy1_method3; |
| 184 | struct udevice *parent; |
| 185 | |
| 186 | /* Get the same phy instance in 3 different ways. */ |
| 187 | ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS, |
| 188 | "gen_phy_user", &parent)); |
| 189 | ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1_method1)); |
| 190 | ut_asserteq(0, phy1_method1.id); |
| 191 | ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1_method2)); |
| 192 | ut_asserteq(0, phy1_method2.id); |
| 193 | ut_asserteq_ptr(phy1_method1.dev, phy1_method1.dev); |
| 194 | |
| 195 | ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS, |
| 196 | "gen_phy_user1", &parent)); |
| 197 | ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1_method3)); |
| 198 | ut_asserteq(0, phy1_method3.id); |
| 199 | ut_asserteq_ptr(phy1_method1.dev, phy1_method3.dev); |
| 200 | |
| 201 | /* |
| 202 | * Test using the same PHY from different handles. |
| 203 | * In non-test code these could be in different drivers. |
| 204 | */ |
| 205 | |
| 206 | /* |
| 207 | * These must only call the driver's ops at the first init() |
| 208 | * and power_on(). |
| 209 | */ |
| 210 | ut_assertok(generic_phy_init(&phy1_method1)); |
| 211 | ut_assertok(generic_phy_init(&phy1_method2)); |
| 212 | ut_assertok(generic_phy_power_on(&phy1_method1)); |
| 213 | ut_assertok(generic_phy_power_on(&phy1_method2)); |
| 214 | ut_assertok(generic_phy_init(&phy1_method3)); |
| 215 | ut_assertok(generic_phy_power_on(&phy1_method3)); |
| 216 | |
| 217 | /* |
| 218 | * These must not call the driver's ops as other handles still |
| 219 | * want the PHY powered-on and initialized. |
| 220 | */ |
| 221 | ut_assertok(generic_phy_power_off(&phy1_method3)); |
| 222 | ut_assertok(generic_phy_exit(&phy1_method3)); |
| 223 | |
| 224 | /* |
| 225 | * We would get an error here if the generic_phy_exit() above |
| 226 | * actually called the driver's exit(), as the sandbox driver |
| 227 | * doesn't allow power-off() after exit(). |
| 228 | */ |
| 229 | ut_assertok(generic_phy_power_off(&phy1_method1)); |
| 230 | ut_assertok(generic_phy_power_off(&phy1_method2)); |
| 231 | ut_assertok(generic_phy_exit(&phy1_method1)); |
| 232 | ut_assertok(generic_phy_exit(&phy1_method2)); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | DM_TEST(dm_test_phy_multi_exit, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Jonas Karlman | bd78f2c | 2023-08-31 23:07:08 +0000 | [diff] [blame] | 237 | |
| 238 | static int dm_test_phy_setup(struct unit_test_state *uts) |
| 239 | { |
| 240 | struct phy phy; |
| 241 | struct udevice *parent; |
| 242 | |
| 243 | ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS, |
| 244 | "gen_phy_user", &parent)); |
| 245 | |
| 246 | /* normal */ |
| 247 | ut_assertok(generic_setup_phy(parent, &phy, 0)); |
| 248 | ut_assertok(generic_shutdown_phy(&phy)); |
| 249 | |
| 250 | /* power_off fail with -EIO */ |
| 251 | ut_assertok(generic_setup_phy(parent, &phy, 1)); |
| 252 | ut_asserteq(-EIO, generic_shutdown_phy(&phy)); |
| 253 | |
| 254 | /* power_on fail with -EIO */ |
| 255 | ut_asserteq(-EIO, generic_setup_phy(parent, &phy, 2)); |
| 256 | ut_assertok(generic_shutdown_phy(&phy)); |
| 257 | |
Jonas Karlman | 1a4293e | 2023-08-31 23:07:10 +0000 | [diff] [blame] | 258 | /* generic_phy_get_by_index fail with -ENOENT */ |
| 259 | ut_asserteq(-ENOENT, generic_phy_get_by_index(parent, 3, &phy)); |
| 260 | ut_assertok(generic_setup_phy(parent, &phy, 3)); |
| 261 | ut_assertok(generic_shutdown_phy(&phy)); |
| 262 | |
Jonas Karlman | bd78f2c | 2023-08-31 23:07:08 +0000 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | DM_TEST(dm_test_phy_setup, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |