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