1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2019
4  * Alex Marginean, NXP
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <log.h>
10 #include <miiphy.h>
11 #include <misc.h>
12 #include <dm/test.h>
13 #include <test/test.h>
14 #include <test/ut.h>
15 
16 /* macros copied over from mdio_sandbox.c */
17 #define SANDBOX_PHY_ADDR	5
18 #define SANDBOX_PHY_REG_CNT	2
19 
20 /* test using 1st register, 0 */
21 #define SANDBOX_PHY_REG		0
22 
23 #define TEST_REG_VALUE		0xabcd
24 
dm_test_mdio(struct unit_test_state * uts)25 static int dm_test_mdio(struct unit_test_state *uts)
26 {
27 	struct uclass *uc;
28 	struct udevice *dev;
29 	struct mdio_ops *ops;
30 	u16 reg;
31 
32 	ut_assertok(uclass_get(UCLASS_MDIO, &uc));
33 
34 	ut_assertok(uclass_get_device_by_name(UCLASS_MDIO, "mdio-test", &dev));
35 
36 	ops = mdio_get_ops(dev);
37 	ut_assertnonnull(ops);
38 	ut_assertnonnull(ops->read);
39 	ut_assertnonnull(ops->write);
40 
41 	ut_assertok(ops->write(dev, SANDBOX_PHY_ADDR, MDIO_DEVAD_NONE,
42 			       SANDBOX_PHY_REG, TEST_REG_VALUE));
43 	reg = ops->read(dev, SANDBOX_PHY_ADDR, MDIO_DEVAD_NONE,
44 			SANDBOX_PHY_REG);
45 	ut_asserteq(reg, TEST_REG_VALUE);
46 
47 	ut_assert(ops->read(dev, SANDBOX_PHY_ADDR + 1, MDIO_DEVAD_NONE,
48 			    SANDBOX_PHY_REG) != 0);
49 
50 	ut_assertok(ops->reset(dev));
51 	reg = ops->read(dev, SANDBOX_PHY_ADDR, MDIO_DEVAD_NONE,
52 			SANDBOX_PHY_REG);
53 	ut_asserteq(reg, 0);
54 
55 	return 0;
56 }
57 
58 DM_TEST(dm_test_mdio, UT_TESTF_SCAN_FDT);
59