test/py: mmc: Add 'mmc info' test
[oweals/u-boot.git] / test / py / tests / test_mmc_rd.py
1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
3
4 # Test U-Boot's "mmc read" command. The test reads data from the eMMC or SD
5 # card, and validates the no errors occurred, and that the expected data was
6 # read if the test configuration contains a CRC of the expected data.
7
8 import pytest
9 import u_boot_utils
10
11 """
12 This test relies on boardenv_* to containing configuration values to define
13 which MMC devices should be tested. For example:
14
15 env__mmc_rd_configs = (
16     {
17         'fixture_id': 'emmc-boot0',
18         'is_emmc': True,
19         'devid': 0,
20         'partid': 1,
21         'sector': 0x10,
22         'count': 1,
23     },
24     {
25         'fixture_id': 'emmc-boot1',
26         'is_emmc': True,
27         'devid': 0,
28         'partid': 2,
29         'sector': 0x10,
30         'count': 1,
31     },
32     {
33         'fixture_id': 'emmc-data',
34         'is_emmc': True,
35         'devid': 0,
36         'partid': 0,
37         'sector': 0x10,
38         'count': 0x1000,
39     },
40     {
41         'fixture_id': 'sd-mbr',
42         'is_emmc': False,
43         'devid': 1,
44         'partid': None,
45         'sector': 0,
46         'count': 1,
47         'crc32': '8f6ecf0d',
48     },
49     {
50         'fixture_id': 'sd-large',
51         'is_emmc': False,
52         'devid': 1,
53         'partid': None,
54         'sector': 0x10,
55         'count': 0x1000,
56     },
57 )
58 """
59
60 def mmc_dev(u_boot_console, is_emmc, devid, partid):
61     """Run the "mmc dev" command.
62
63     Args:
64         u_boot_console: A U-Boot console connection.
65         is_emmc: Whether the device is eMMC
66         devid: Device ID
67         partid: Partition ID
68
69     Returns:
70         Nothing.
71     """
72
73     # Select MMC device
74     cmd = 'mmc dev %d' % devid
75     if is_emmc:
76         cmd += ' %d' % partid
77     response = u_boot_console.run_command(cmd)
78     assert 'no card present' not in response
79     if is_emmc:
80         partid_response = '(part %d)' % partid
81     else:
82         partid_response = ''
83     good_response = 'mmc%d%s is current device' % (devid, partid_response)
84     assert good_response in response
85
86 @pytest.mark.buildconfigspec('cmd_mmc')
87 def test_mmc_dev(u_boot_console, env__mmc_rd_config):
88     """Test the "mmc dev" command.
89
90     Args:
91         u_boot_console: A U-Boot console connection.
92         env__mmc_rd_config: The single MMC configuration on which
93             to run the test. See the file-level comment above for details
94             of the format.
95
96     Returns:
97         Nothing.
98     """
99
100     is_emmc = env__mmc_rd_config['is_emmc']
101     devid = env__mmc_rd_config['devid']
102     partid = env__mmc_rd_config.get('partid', 0)
103
104     # Select MMC device
105     mmc_dev(u_boot_console, is_emmc, devid, partid)
106
107 @pytest.mark.buildconfigspec('cmd_mmc')
108 def test_mmc_rescan(u_boot_console, env__mmc_rd_config):
109     """Test the "mmc rescan" command.
110
111     Args:
112         u_boot_console: A U-Boot console connection.
113         env__mmc_rd_config: The single MMC configuration on which
114             to run the test. See the file-level comment above for details
115             of the format.
116
117     Returns:
118         Nothing.
119     """
120
121     is_emmc = env__mmc_rd_config['is_emmc']
122     devid = env__mmc_rd_config['devid']
123     partid = env__mmc_rd_config.get('partid', 0)
124
125     # Select MMC device
126     mmc_dev(u_boot_console, is_emmc, devid, partid)
127
128     # Rescan MMC device
129     cmd = 'mmc rescan'
130     response = u_boot_console.run_command(cmd)
131     assert 'no card present' not in response
132
133 @pytest.mark.buildconfigspec('cmd_mmc')
134 def test_mmc_info(u_boot_console, env__mmc_rd_config):
135     """Test the "mmc info" command.
136
137     Args:
138         u_boot_console: A U-Boot console connection.
139         env__mmc_rd_config: The single MMC configuration on which
140             to run the test. See the file-level comment above for details
141             of the format.
142
143     Returns:
144         Nothing.
145     """
146
147     is_emmc = env__mmc_rd_config['is_emmc']
148     devid = env__mmc_rd_config['devid']
149     partid = env__mmc_rd_config.get('partid', 0)
150     info_device = env__mmc_rd_config['info_device']
151     info_speed = env__mmc_rd_config['info_speed']
152     info_mode = env__mmc_rd_config['info_mode']
153     info_buswidth = env__mmc_rd_config['info_buswidth']
154
155     # Select MMC device
156     mmc_dev(u_boot_console, is_emmc, devid, partid)
157
158     # Read MMC device information
159     cmd = 'mmc info'
160     response = u_boot_console.run_command(cmd)
161     good_response = "Device: %s" % info_device
162     assert good_response in response
163     good_response = "Bus Speed: %s" % info_speed
164     assert good_response in response
165     good_response = "Mode : %s" % info_mode
166     assert good_response in response
167     good_response = "Bus Width: %s" % info_buswidth
168     assert good_response in response
169
170 @pytest.mark.buildconfigspec('cmd_mmc')
171 def test_mmc_rd(u_boot_console, env__mmc_rd_config):
172     """Test the "mmc read" command.
173
174     Args:
175         u_boot_console: A U-Boot console connection.
176         env__mmc_rd_config: The single MMC configuration on which
177             to run the test. See the file-level comment above for details
178             of the format.
179
180     Returns:
181         Nothing.
182     """
183
184     is_emmc = env__mmc_rd_config['is_emmc']
185     devid = env__mmc_rd_config['devid']
186     partid = env__mmc_rd_config.get('partid', 0)
187     sector = env__mmc_rd_config.get('sector', 0)
188     count_sectors = env__mmc_rd_config.get('count', 1)
189     expected_crc32 = env__mmc_rd_config.get('crc32', None)
190
191     count_bytes = count_sectors * 512
192     bcfg = u_boot_console.config.buildconfig
193     has_cmd_memory = bcfg.get('config_cmd_memory', 'n') == 'y'
194     has_cmd_crc32 = bcfg.get('config_cmd_crc32', 'n') == 'y'
195     ram_base = u_boot_utils.find_ram_base(u_boot_console)
196     addr = '0x%08x' % ram_base
197
198     # Select MMC device
199     mmc_dev(u_boot_console, is_emmc, devid, partid)
200
201     # Clear target RAM
202     if expected_crc32:
203         if has_cmd_memory and has_cmd_crc32:
204             cmd = 'mw.b %s 0 0x%x' % (addr, count_bytes)
205             u_boot_console.run_command(cmd)
206
207             cmd = 'crc32 %s 0x%x' % (addr, count_bytes)
208             response = u_boot_console.run_command(cmd)
209             assert expected_crc32 not in response
210         else:
211             u_boot_console.log.warning(
212                 'CONFIG_CMD_MEMORY or CONFIG_CMD_CRC32 != y: Skipping RAM clear')
213
214     # Read data
215     cmd = 'mmc read %s %x %x' % (addr, sector, count_sectors)
216     response = u_boot_console.run_command(cmd)
217     good_response = 'MMC read: dev # %d, block # %d, count %d ... %d blocks read: OK' % (
218         devid, sector, count_sectors, count_sectors)
219     assert good_response in response
220
221     # Check target RAM
222     if expected_crc32:
223         if has_cmd_crc32:
224             cmd = 'crc32 %s 0x%x' % (addr, count_bytes)
225             response = u_boot_console.run_command(cmd)
226             assert expected_crc32 in response
227         else:
228             u_boot_console.log.warning('CONFIG_CMD_CRC32 != y: Skipping check')