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