blob: 194cad0bf706c529895b26d56284ac03cc8afe24 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +02002/*
Nishanth Menoneaa39c62023-11-01 15:56:03 -05003 * Copyright (C) 2017 Texas Instruments Incorporated - https://www.ti.com/
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +02004 * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +02005 */
6
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +02007#include <dm.h>
8#include <generic-phy.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +020010#include <dm/test.h>
Simon Glass75c4d412020-07-19 10:15:37 -060011#include <test/test.h>
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +020012#include <test/ut.h>
13
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +020014/* Base test of the phy uclass */
15static 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 Vasut1531c4e2023-03-10 04:33:13 +010030 ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1_method1));
Jonas Karlmanffe06b42023-08-31 22:16:33 +000031 ut_assert(generic_phy_valid(&phy1_method1));
Marek Vasut1531c4e2023-03-10 04:33:13 +010032 ut_assertok(generic_phy_get_by_index(parent, 0, &phy1_method2));
Jonas Karlmanffe06b42023-08-31 22:16:33 +000033 ut_assert(generic_phy_valid(&phy1_method2));
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +020034 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 Vasut1531c4e2023-03-10 04:33:13 +010040 ut_assertok(generic_phy_get_by_name(parent, "phy2", &phy2));
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +020041 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 Vasut1531c4e2023-03-10 04:33:13 +010047 ut_assertok(generic_phy_get_by_name(parent, "phy3", &phy3));
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +020048 ut_assert(phy2.dev != phy3.dev);
49
50 /* Try to get a non-existing phy */
Jonas Karlman9f89e682023-08-31 22:16:35 +000051 ut_asserteq(-ENODEV, uclass_get_device(UCLASS_PHY, 5, &dev));
Simon Glassd8c9bac2017-05-18 20:09:45 -060052 ut_asserteq(-ENODATA, generic_phy_get_by_name(parent,
53 "phy_not_existing", &phy1_method1));
Jonas Karlmanffe06b42023-08-31 22:16:33 +000054 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 Hiblot7e9db022017-04-24 11:51:28 +020058
Jonas Karlman9f89e682023-08-31 22:16:35 +000059 /* 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 Hiblot7e9db022017-04-24 11:51:28 +020069 return 0;
70}
Simon Glass1a92f832024-08-22 07:57:48 -060071DM_TEST(dm_test_phy_base, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +020072
73/* Test of the phy uclass using the sandbox phy driver operations */
74static 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 Glassd8c9bac2017-05-18 20:09:45 -060085 ut_asserteq(0, phy1.id);
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +020086 ut_assertok(generic_phy_get_by_name(parent, "phy2", &phy2));
Simon Glassd8c9bac2017-05-18 20:09:45 -060087 ut_asserteq(1, phy2.id);
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +020088 ut_assertok(generic_phy_get_by_name(parent, "phy3", &phy3));
Simon Glassd8c9bac2017-05-18 20:09:45 -060089 ut_asserteq(0, phy3.id);
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +020090
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 Yasakbe4e5e12021-12-30 22:36:51 +030097 * 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 Hiblot7e9db022017-04-24 11:51:28 +0200102 */
103 ut_assertok(generic_phy_exit(&phy1));
104 ut_assert(generic_phy_power_on(&phy1) != 0);
Alper Nebi Yasakbe4e5e12021-12-30 22:36:51 +0300105 ut_assertok(generic_phy_power_off(&phy1));
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +0200106
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 Yasakbe4e5e12021-12-30 22:36:51 +0300120 /*
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 Hiblot7e9db022017-04-24 11:51:28 +0200131 /* PHY2 has a known problem with power off */
132 ut_assertok(generic_phy_init(&phy2));
133 ut_assertok(generic_phy_power_on(&phy2));
Simon Glassd8c9bac2017-05-18 20:09:45 -0600134 ut_asserteq(-EIO, generic_phy_power_off(&phy2));
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +0200135
Simon Glassd8c9bac2017-05-18 20:09:45 -0600136 /* PHY3 has a known problem with power off and power on */
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +0200137 ut_assertok(generic_phy_init(&phy3));
Alper Nebi Yasakbe4e5e12021-12-30 22:36:51 +0300138 ut_asserteq(-EIO, generic_phy_power_on(&phy3));
139 ut_assertok(generic_phy_power_off(&phy3));
Jean-Jacques Hiblot7e9db022017-04-24 11:51:28 +0200140
141 return 0;
142}
Simon Glass1a92f832024-08-22 07:57:48 -0600143DM_TEST(dm_test_phy_ops, UTF_SCAN_PDATA | UTF_SCAN_FDT);
developer71092972020-05-02 11:35:12 +0200144
145static 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 Glass1a92f832024-08-22 07:57:48 -0600176DM_TEST(dm_test_phy_bulk, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Alper Nebi Yasakbe4e5e12021-12-30 22:36:51 +0300177
178static 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 Glass1a92f832024-08-22 07:57:48 -0600235DM_TEST(dm_test_phy_multi_exit, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Jonas Karlman12830df2023-08-31 23:07:08 +0000236
237static 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 Vasutf87e00d2024-09-08 23:09:03 +0200246 ut_assertok(generic_setup_phy(parent, &phy, 0, PHY_MODE_USB_HOST, 0));
Jonas Karlman12830df2023-08-31 23:07:08 +0000247 ut_assertok(generic_shutdown_phy(&phy));
248
Marek Vasut2c61c152024-09-08 23:09:05 +0200249 /* 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 Karlman12830df2023-08-31 23:07:08 +0000256 /* power_off fail with -EIO */
Marek Vasutf87e00d2024-09-08 23:09:03 +0200257 ut_assertok(generic_setup_phy(parent, &phy, 1, PHY_MODE_USB_HOST, 0));
Jonas Karlman12830df2023-08-31 23:07:08 +0000258 ut_asserteq(-EIO, generic_shutdown_phy(&phy));
259
260 /* power_on fail with -EIO */
Marek Vasutf87e00d2024-09-08 23:09:03 +0200261 ut_asserteq(-EIO, generic_setup_phy(parent, &phy, 2, PHY_MODE_USB_HOST, 0));
Jonas Karlman12830df2023-08-31 23:07:08 +0000262 ut_assertok(generic_shutdown_phy(&phy));
263
Jonas Karlmanbec90512023-08-31 23:07:10 +0000264 /* generic_phy_get_by_index fail with -ENOENT */
265 ut_asserteq(-ENOENT, generic_phy_get_by_index(parent, 3, &phy));
Marek Vasutf87e00d2024-09-08 23:09:03 +0200266 ut_assertok(generic_setup_phy(parent, &phy, 3, PHY_MODE_USB_HOST, 0));
Jonas Karlmanbec90512023-08-31 23:07:10 +0000267 ut_assertok(generic_shutdown_phy(&phy));
268
Jonas Karlman12830df2023-08-31 23:07:08 +0000269 return 0;
270}
Simon Glass1a92f832024-08-22 07:57:48 -0600271DM_TEST(dm_test_phy_setup, UTF_SCAN_PDATA | UTF_SCAN_FDT);