1# SPDX-License-Identifier: GPL-2.0
2# (C) Copyright 2023, Advanced Micro Devices, Inc.
3
4import pytest
5
6"""
7Note: This test relies on boardenv_* containing configuration values to define
8the memory test parameters such as start address, memory size, pattern,
9iterations and timeout. This test will be automatically skipped without this.
10
11For example:
12
13# Setup env__memtest to set the start address of the memory range, size of the
14# memory range to test from starting address, pattern to be written to memory,
15# number of test iterations, and expected time to complete the test of mtest
16# command. start address, size, and pattern parameters value should be in hex
17# and rest of the params value should be integer.
18env__memtest = {
19    'start_addr': 0x0,
20    'size': 0x1000,
21    'pattern': 0x0,
22    'iteration': 16,
23    'timeout': 50000,
24}
25"""
26
27def get_memtest_env(u_boot_console):
28    f = u_boot_console.config.env.get("env__memtest", None)
29    if not f:
30        pytest.skip("memtest is not enabled!")
31    else:
32        start = f.get("start_addr", 0x0)
33        size = f.get("size", 0x1000)
34        pattern = f.get("pattern", 0x0)
35        iteration = f.get("iteration", 2)
36        timeout = f.get("timeout", 50000)
37        end = hex(int(start) + int(size))
38        return start, end, pattern, iteration, timeout
39
40@pytest.mark.buildconfigspec("cmd_memtest")
41def test_memtest_negative(u_boot_console):
42    """Negative testcase where end address is smaller than starting address and
43    pattern is invalid."""
44    start, end, pattern, iteration, timeout = get_memtest_env(u_boot_console)
45    expected_response = "Refusing to do empty test"
46    response = u_boot_console.run_command(
47        f"mtest 2000 1000 {pattern} {hex(iteration)}"
48    )
49    assert expected_response in response
50    output = u_boot_console.run_command("echo $?")
51    assert not output.endswith("0")
52    u_boot_console.run_command(f"mtest {start} {end} 'xyz' {hex(iteration)}")
53    output = u_boot_console.run_command("echo $?")
54    assert not output.endswith("0")
55
56@pytest.mark.buildconfigspec("cmd_memtest")
57def test_memtest_ddr(u_boot_console):
58    """Test that md reads memory as expected, and that memory can be modified
59    using the mw command."""
60    start, end, pattern, iteration, timeout = get_memtest_env(u_boot_console)
61    expected_response = f"Tested {str(iteration)} iteration(s) with 0 errors."
62    with u_boot_console.temporary_timeout(timeout):
63        response = u_boot_console.run_command(
64            f"mtest {start} {end} {pattern} {hex(iteration)}"
65        )
66        assert expected_response in response
67    output = u_boot_console.run_command("echo $?")
68    assert output.endswith("0")
69