blob: 0cf3689fdecbf649eaceb04ef094c6aec155e3f4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +02002/*
Nishanth Menona94a4072023-11-01 15:56:03 -05003 * Copyright (C) 2017 Texas Instruments Incorporated - https://www.ti.com/
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +02004 * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +02005 */
6
7#include <common.h>
8#include <dm.h>
9#include <generic-phy.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +020011#include <dm/test.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060012#include <test/test.h>
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +020013#include <test/ut.h>
14
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +020015/* Base test of the phy uclass */
16static 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 Vasutfa847bb2023-03-10 04:33:13 +010031 ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1_method1));
Jonas Karlmanfeb4b912023-08-31 22:16:33 +000032 ut_assert(generic_phy_valid(&phy1_method1));
Marek Vasutfa847bb2023-03-10 04:33:13 +010033 ut_assertok(generic_phy_get_by_index(parent, 0, &phy1_method2));
Jonas Karlmanfeb4b912023-08-31 22:16:33 +000034 ut_assert(generic_phy_valid(&phy1_method2));
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +020035 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 Vasutfa847bb2023-03-10 04:33:13 +010041 ut_assertok(generic_phy_get_by_name(parent, "phy2", &phy2));
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +020042 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 Vasutfa847bb2023-03-10 04:33:13 +010048 ut_assertok(generic_phy_get_by_name(parent, "phy3", &phy3));
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +020049 ut_assert(phy2.dev != phy3.dev);
50
51 /* Try to get a non-existing phy */
Jonas Karlman14639bf2023-08-31 22:16:35 +000052 ut_asserteq(-ENODEV, uclass_get_device(UCLASS_PHY, 5, &dev));
Simon Glassac206a02017-05-18 20:09:45 -060053 ut_asserteq(-ENODATA, generic_phy_get_by_name(parent,
54 "phy_not_existing", &phy1_method1));
Jonas Karlmanfeb4b912023-08-31 22:16:33 +000055 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 Hiblot86322f52017-04-24 11:51:28 +020059
Jonas Karlman14639bf2023-08-31 22:16:35 +000060 /* 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 Hiblot86322f52017-04-24 11:51:28 +020070 return 0;
71}
Simon Glasse180c2b2020-07-28 19:41:12 -060072DM_TEST(dm_test_phy_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +020073
74/* Test of the phy uclass using the sandbox phy driver operations */
75static 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 Glassac206a02017-05-18 20:09:45 -060086 ut_asserteq(0, phy1.id);
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +020087 ut_assertok(generic_phy_get_by_name(parent, "phy2", &phy2));
Simon Glassac206a02017-05-18 20:09:45 -060088 ut_asserteq(1, phy2.id);
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +020089 ut_assertok(generic_phy_get_by_name(parent, "phy3", &phy3));
Simon Glassac206a02017-05-18 20:09:45 -060090 ut_asserteq(0, phy3.id);
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +020091
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 Yasak226fce62021-12-30 22:36:51 +030098 * 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 Hiblot86322f52017-04-24 11:51:28 +0200103 */
104 ut_assertok(generic_phy_exit(&phy1));
105 ut_assert(generic_phy_power_on(&phy1) != 0);
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300106 ut_assertok(generic_phy_power_off(&phy1));
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +0200107
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 Yasak226fce62021-12-30 22:36:51 +0300121 /*
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 Hiblot86322f52017-04-24 11:51:28 +0200132 /* PHY2 has a known problem with power off */
133 ut_assertok(generic_phy_init(&phy2));
134 ut_assertok(generic_phy_power_on(&phy2));
Simon Glassac206a02017-05-18 20:09:45 -0600135 ut_asserteq(-EIO, generic_phy_power_off(&phy2));
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +0200136
Simon Glassac206a02017-05-18 20:09:45 -0600137 /* PHY3 has a known problem with power off and power on */
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +0200138 ut_assertok(generic_phy_init(&phy3));
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300139 ut_asserteq(-EIO, generic_phy_power_on(&phy3));
140 ut_assertok(generic_phy_power_off(&phy3));
Jean-Jacques Hiblot86322f52017-04-24 11:51:28 +0200141
142 return 0;
143}
Simon Glasse180c2b2020-07-28 19:41:12 -0600144DM_TEST(dm_test_phy_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chunfeng Yun00c82ac2020-05-02 11:35:12 +0200145
146static 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 Glasse180c2b2020-07-28 19:41:12 -0600177DM_TEST(dm_test_phy_bulk, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300178
179static 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}
236DM_TEST(dm_test_phy_multi_exit, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Jonas Karlmanbd78f2c2023-08-31 23:07:08 +0000237
238static 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 Karlman1a4293e2023-08-31 23:07:10 +0000258 /* 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 Karlmanbd78f2c2023-08-31 23:07:08 +0000263 return 0;
264}
265DM_TEST(dm_test_phy_setup, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);