1# SPDX-License-Identifier: GPL-2.0
2# (C) Copyright 2023, Advanced Micro Devices, Inc.
3
4import pytest
5import re
6
7"""
8Note: This test relies on boardenv_* containing configuration values to define
9the PHY device info including the device name, address, register address/value
10and write data value. This test will be automatically skipped without this.
11
12For example:
13
14# Setup env__mdio_util_test to set the PHY address, device names, register
15# address, register address value, and write data value to test mdio commands.
16# Test will be skipped if env_mdio_util_test is not set
17env__mdio_util_test = {
18    "eth0": {"phy_addr": 0xc, "device_name": "TI DP83867", "reg": 0,
19                 "reg_val": 0x1000, "write_val": 0x100},
20    "eth1": {"phy_addr": 0xa0, "device_name": "TI DP83867", "reg": 1,
21                 "reg_val": 0x2000, "write_val": 0x100},
22}
23"""
24
25def get_mdio_test_env(ubman):
26    f = ubman.config.env.get("env__mdio_util_test", None)
27    if not f or len(f) == 0:
28        pytest.skip("No PHY device to test!")
29    else:
30        return f
31
32@pytest.mark.buildconfigspec("cmd_mii")
33@pytest.mark.buildconfigspec("phylib")
34def test_mdio_list(ubman):
35    f = get_mdio_test_env(ubman)
36    output = ubman.run_command("mdio list")
37    for dev, val in f.items():
38        phy_addr = val.get("phy_addr")
39        dev_name = val.get("device_name")
40
41        assert f"{phy_addr:x} -" in output
42        assert dev_name in output
43
44@pytest.mark.buildconfigspec("cmd_mii")
45@pytest.mark.buildconfigspec("phylib")
46def test_mdio_read(ubman):
47    f = get_mdio_test_env(ubman)
48    output = ubman.run_command("mdio list")
49    for dev, val in f.items():
50        phy_addr = hex(val.get("phy_addr"))
51        dev_name = val.get("device_name")
52        reg = hex(val.get("reg"))
53        reg_val = hex(val.get("reg_val"))
54
55        output = ubman.run_command(f"mdio read {phy_addr} {reg}")
56        assert f"PHY at address {int(phy_addr, 16):x}:" in output
57        assert f"{int(reg, 16):x} - {reg_val}" in output
58
59@pytest.mark.buildconfigspec("cmd_mii")
60@pytest.mark.buildconfigspec("phylib")
61def test_mdio_write(ubman):
62    f = get_mdio_test_env(ubman)
63    output = ubman.run_command("mdio list")
64    for dev, val in f.items():
65        phy_addr = hex(val.get("phy_addr"))
66        dev_name = val.get("device_name")
67        reg = hex(val.get("reg"))
68        reg_val = hex(val.get("reg_val"))
69        wr_val = hex(val.get("write_val"))
70
71        ubman.run_command(f"mdio write {phy_addr} {reg} {wr_val}")
72        output = ubman.run_command(f"mdio read {phy_addr} {reg}")
73        assert f"PHY at address {int(phy_addr, 16):x}:" in output
74        assert f"{int(reg, 16):x} - {wr_val}" in output
75
76        ubman.run_command(f"mdio write {phy_addr} {reg} {reg_val}")
77        output = ubman.run_command(f"mdio read {phy_addr} {reg}")
78        assert f"PHY at address {int(phy_addr, 16):x}:" in output
79        assert f"{int(reg, 16):x} - {reg_val}" in output
80