xref: /openbmc/u-boot/test/dm/phy.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
286322f59SJean-Jacques Hiblot /*
386322f59SJean-Jacques Hiblot  * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
486322f59SJean-Jacques Hiblot  * Written by Jean-Jacques Hiblot  <jjhiblot@ti.com>
586322f59SJean-Jacques Hiblot  */
686322f59SJean-Jacques Hiblot 
786322f59SJean-Jacques Hiblot #include <common.h>
886322f59SJean-Jacques Hiblot #include <dm.h>
986322f59SJean-Jacques Hiblot #include <generic-phy.h>
1086322f59SJean-Jacques Hiblot #include <dm/test.h>
1186322f59SJean-Jacques Hiblot #include <test/ut.h>
1286322f59SJean-Jacques Hiblot 
1386322f59SJean-Jacques Hiblot /* Base test of the phy uclass */
dm_test_phy_base(struct unit_test_state * uts)1486322f59SJean-Jacques Hiblot static int dm_test_phy_base(struct unit_test_state *uts)
1586322f59SJean-Jacques Hiblot {
1686322f59SJean-Jacques Hiblot 	struct udevice *dev;
1786322f59SJean-Jacques Hiblot 	struct phy phy1_method1;
1886322f59SJean-Jacques Hiblot 	struct phy phy1_method2;
1986322f59SJean-Jacques Hiblot 	struct phy phy2;
2086322f59SJean-Jacques Hiblot 	struct phy phy3;
2186322f59SJean-Jacques Hiblot 	struct udevice *parent;
2286322f59SJean-Jacques Hiblot 
2386322f59SJean-Jacques Hiblot 	/* Get the device using the phy device*/
2486322f59SJean-Jacques Hiblot 	ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS,
2586322f59SJean-Jacques Hiblot 					      "gen_phy_user", &parent));
2686322f59SJean-Jacques Hiblot 	/*
2786322f59SJean-Jacques Hiblot 	 * Get the same phy port in 2 different ways and compare.
2886322f59SJean-Jacques Hiblot 	 */
2986322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1_method1))
3086322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_get_by_index(parent, 0, &phy1_method2))
3186322f59SJean-Jacques Hiblot 	ut_asserteq(phy1_method1.id, phy1_method2.id);
3286322f59SJean-Jacques Hiblot 
3386322f59SJean-Jacques Hiblot 	/*
3486322f59SJean-Jacques Hiblot 	 * Get the second phy port. Check that the same phy provider (device)
3586322f59SJean-Jacques Hiblot 	 * provides this 2nd phy port, but that the IDs are different
3686322f59SJean-Jacques Hiblot 	 */
3786322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_get_by_name(parent, "phy2", &phy2))
3886322f59SJean-Jacques Hiblot 	ut_asserteq_ptr(phy1_method2.dev, phy2.dev);
3986322f59SJean-Jacques Hiblot 	ut_assert(phy1_method1.id != phy2.id);
4086322f59SJean-Jacques Hiblot 
4186322f59SJean-Jacques Hiblot 	/*
4286322f59SJean-Jacques Hiblot 	 * Get the third phy port. Check that the phy provider is different
4386322f59SJean-Jacques Hiblot 	 */
4486322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_get_by_name(parent, "phy3", &phy3))
4586322f59SJean-Jacques Hiblot 	ut_assert(phy2.dev != phy3.dev);
4686322f59SJean-Jacques Hiblot 
4786322f59SJean-Jacques Hiblot 	/* Try to get a non-existing phy */
4886322f59SJean-Jacques Hiblot 	ut_asserteq(-ENODEV, uclass_get_device(UCLASS_PHY, 3, &dev));
49ac206a0fSSimon Glass 	ut_asserteq(-ENODATA, generic_phy_get_by_name(parent,
50ac206a0fSSimon Glass 					"phy_not_existing", &phy1_method1));
5186322f59SJean-Jacques Hiblot 
5286322f59SJean-Jacques Hiblot 	return 0;
5386322f59SJean-Jacques Hiblot }
5486322f59SJean-Jacques Hiblot DM_TEST(dm_test_phy_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
5586322f59SJean-Jacques Hiblot 
5686322f59SJean-Jacques Hiblot /* Test of the phy uclass using the sandbox phy driver operations */
dm_test_phy_ops(struct unit_test_state * uts)5786322f59SJean-Jacques Hiblot static int dm_test_phy_ops(struct unit_test_state *uts)
5886322f59SJean-Jacques Hiblot {
5986322f59SJean-Jacques Hiblot 	struct phy phy1;
6086322f59SJean-Jacques Hiblot 	struct phy phy2;
6186322f59SJean-Jacques Hiblot 	struct phy phy3;
6286322f59SJean-Jacques Hiblot 	struct udevice *parent;
6386322f59SJean-Jacques Hiblot 
6486322f59SJean-Jacques Hiblot 	ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS,
6586322f59SJean-Jacques Hiblot 					      "gen_phy_user", &parent));
6686322f59SJean-Jacques Hiblot 
6786322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1));
68ac206a0fSSimon Glass 	ut_asserteq(0, phy1.id);
6986322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_get_by_name(parent, "phy2", &phy2));
70ac206a0fSSimon Glass 	ut_asserteq(1, phy2.id);
7186322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_get_by_name(parent, "phy3", &phy3));
72ac206a0fSSimon Glass 	ut_asserteq(0, phy3.id);
7386322f59SJean-Jacques Hiblot 
7486322f59SJean-Jacques Hiblot 	/* test normal operations */
7586322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_init(&phy1));
7686322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_power_on(&phy1));
7786322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_power_off(&phy1));
7886322f59SJean-Jacques Hiblot 
7986322f59SJean-Jacques Hiblot 	/*
8086322f59SJean-Jacques Hiblot 	 * test operations after exit().
8186322f59SJean-Jacques Hiblot 	 * The sandbox phy driver does not allow it.
8286322f59SJean-Jacques Hiblot 	 */
8386322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_exit(&phy1));
8486322f59SJean-Jacques Hiblot 	ut_assert(generic_phy_power_on(&phy1) != 0);
8586322f59SJean-Jacques Hiblot 	ut_assert(generic_phy_power_off(&phy1) != 0);
8686322f59SJean-Jacques Hiblot 
8786322f59SJean-Jacques Hiblot 	/*
8886322f59SJean-Jacques Hiblot 	 * test normal operations again (after re-init)
8986322f59SJean-Jacques Hiblot 	 */
9086322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_init(&phy1));
9186322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_power_on(&phy1));
9286322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_power_off(&phy1));
9386322f59SJean-Jacques Hiblot 
9486322f59SJean-Jacques Hiblot 	/*
9586322f59SJean-Jacques Hiblot 	 * test calling unimplemented feature.
9686322f59SJean-Jacques Hiblot 	 * The call is expected to succeed
9786322f59SJean-Jacques Hiblot 	 */
9886322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_reset(&phy1));
9986322f59SJean-Jacques Hiblot 
10086322f59SJean-Jacques Hiblot 	/* PHY2 has a known problem with power off */
10186322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_init(&phy2));
10286322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_power_on(&phy2));
103ac206a0fSSimon Glass 	ut_asserteq(-EIO, generic_phy_power_off(&phy2));
10486322f59SJean-Jacques Hiblot 
10586322f59SJean-Jacques Hiblot 	/* PHY3 has a known problem with power off and power on */
10686322f59SJean-Jacques Hiblot 	ut_assertok(generic_phy_init(&phy3));
107ac206a0fSSimon Glass 	ut_asserteq(-EIO, generic_phy_power_off(&phy3));
108ac206a0fSSimon Glass 	ut_asserteq(-EIO, generic_phy_power_off(&phy3));
10986322f59SJean-Jacques Hiblot 
11086322f59SJean-Jacques Hiblot 	return 0;
11186322f59SJean-Jacques Hiblot }
11286322f59SJean-Jacques Hiblot DM_TEST(dm_test_phy_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
113