1 # Copyright (c) 2016, Xilinx Inc. Michal Simek
2 # Copyright (c) 2017, Xiphos Systems Corp. All rights reserved.
4 # SPDX-License-Identifier: GPL-2.0
12 Note: This test relies on boardenv_* containing configuration values to define
13 which SPI Flash areas are available for testing. Without this, this test will
14 be automatically skipped.
17 # A list of sections of Flash memory to be tested.
20 # Where in SPI Flash should the test operate.
22 # This value is optional.
23 # If present, specifies the [[bus:]cs] argument used in `sf probe`
24 # If missing, defaults to 0.
26 # This value is optional.
27 # If set as a number, specifies the speed of the SPI Flash.
28 # If set as an array of 2, specifies a range for a random speed.
29 # If missing, defaults to 0.
31 # This value is optional.
32 # If present, specifies the size to use for read/write operations.
33 # If missing, the SPI Flash page size is used as a default (based on
34 # the `sf probe` output).
36 # This value is optional.
37 # If present, specifies if the test can write to Flash offset
38 # If missing, defaults to False.
40 # This value is optional.
41 # If present, specifies the expected CRC32 value of the flash area.
42 # If missing, extra check is ignored.
48 def sf_prepare(u_boot_console, env__sf_config):
49 """Check global state of the SPI Flash before running any test.
52 u_boot_console: A U-Boot console connection.
53 env__sf_config: The single SPI Flash device configuration on which to
57 sf_params: a dictionary of SPI Flash parameters.
61 sf_params['ram_base'] = u_boot_utils.find_ram_base(u_boot_console)
63 probe_id = env__sf_config.get('id', 0)
64 speed = env__sf_config.get('speed', 0)
65 if isinstance(speed, int):
66 sf_params['speed'] = speed
68 assert len(speed) == 2, "If speed is a list, it must have 2 entries"
69 sf_params['speed'] = random.randint(speed[0], speed[1])
71 cmd = 'sf probe %d %d' % (probe_id, sf_params['speed'])
73 output = u_boot_console.run_command(cmd)
74 assert 'SF: Detected' in output, 'No Flash device available'
76 m = re.search('page size (.+?) Bytes', output)
77 assert m, 'SPI Flash page size not recognized'
78 sf_params['page_size'] = int(m.group(1))
80 m = re.search('erase size (.+?) KiB', output)
81 assert m, 'SPI Flash erase size not recognized'
82 sf_params['erase_size'] = int(m.group(1))
83 sf_params['erase_size'] *= 1024
85 m = re.search('total (.+?) MiB', output)
86 assert m, 'SPI Flash total size not recognized'
87 sf_params['total_size'] = int(m.group(1))
88 sf_params['total_size'] *= 1024 * 1024
90 assert 'offset' in env__sf_config, \
91 '\'offset\' is required for this test.'
92 sf_params['len'] = env__sf_config.get('len', sf_params['erase_size'])
94 assert not env__sf_config['offset'] % sf_params['erase_size'], \
95 'offset not multiple of erase size.'
96 assert not sf_params['len'] % sf_params['erase_size'], \
97 'erase length not multiple of erase size.'
99 assert not (env__sf_config.get('writeable', False) and
100 'crc32' in env__sf_config), \
101 'Cannot check crc32 on writeable sections'
105 def sf_read(u_boot_console, env__sf_config, sf_params):
106 """Helper function used to read and compute the CRC32 value of a section of
110 u_boot_console: A U-Boot console connection.
111 env__sf_config: The single SPI Flash device configuration on which to
113 sf_params: SPI Flash parameters.
116 CRC32 value of SPI Flash section
119 addr = sf_params['ram_base']
120 offset = env__sf_config['offset']
121 count = sf_params['len']
122 pattern = random.randint(0, 0xFF)
123 crc_expected = env__sf_config.get('crc32', None)
125 cmd = 'mw.b %08x %02x %x' % (addr, pattern, count)
126 u_boot_console.run_command(cmd)
127 crc_pattern = u_boot_utils.crc32(u_boot_console, addr, count)
129 assert crc_pattern != crc_expected
131 cmd = 'sf read %08x %08x %x' % (addr, offset, count)
132 response = u_boot_console.run_command(cmd)
133 assert 'Read: OK' in response, 'Read operation failed'
134 crc_readback = u_boot_utils.crc32(u_boot_console, addr, count)
135 assert crc_pattern != crc_readback, 'sf read did not update RAM content.'
137 assert crc_readback == crc_expected
141 def sf_update(u_boot_console, env__sf_config, sf_params):
142 """Helper function used to update a section of SPI Flash memory.
145 u_boot_console: A U-Boot console connection.
146 env__sf_config: The single SPI Flash device configuration on which to
150 CRC32 value of SPI Flash section
153 addr = sf_params['ram_base']
154 offset = env__sf_config['offset']
155 count = sf_params['len']
156 pattern = int(random.random() * 0xFF)
158 cmd = 'mw.b %08x %02x %x' % (addr, pattern, count)
159 u_boot_console.run_command(cmd)
160 crc_pattern = u_boot_utils.crc32(u_boot_console, addr, count)
162 cmd = 'sf update %08x %08x %x' % (addr, offset, count)
163 u_boot_console.run_command(cmd)
164 crc_readback = sf_read(u_boot_console, env__sf_config, sf_params)
166 assert crc_readback == crc_pattern
168 @pytest.mark.buildconfigspec('cmd_sf')
169 @pytest.mark.buildconfigspec('cmd_crc32')
170 @pytest.mark.buildconfigspec('cmd_memory')
171 def test_sf_read(u_boot_console, env__sf_config):
172 sf_params = sf_prepare(u_boot_console, env__sf_config)
173 sf_read(u_boot_console, env__sf_config, sf_params)
175 @pytest.mark.buildconfigspec('cmd_sf')
176 @pytest.mark.buildconfigspec('cmd_crc32')
177 @pytest.mark.buildconfigspec('cmd_memory')
178 def test_sf_read_twice(u_boot_console, env__sf_config):
179 sf_params = sf_prepare(u_boot_console, env__sf_config)
181 crc1 = sf_read(u_boot_console, env__sf_config, sf_params)
182 sf_params['ram_base'] += 0x100
183 crc2 = sf_read(u_boot_console, env__sf_config, sf_params)
185 assert crc1 == crc2, 'CRC32 of two successive read operation do not match'
187 @pytest.mark.buildconfigspec('cmd_sf')
188 @pytest.mark.buildconfigspec('cmd_crc32')
189 @pytest.mark.buildconfigspec('cmd_memory')
190 def test_sf_erase(u_boot_console, env__sf_config):
191 if not env__sf_config.get('writeable', False):
192 pytest.skip('Flash config is tagged as not writeable')
194 sf_params = sf_prepare(u_boot_console, env__sf_config)
195 addr = sf_params['ram_base']
196 offset = env__sf_config['offset']
197 count = sf_params['len']
199 cmd = 'sf erase %08x %x' % (offset, count)
200 output = u_boot_console.run_command(cmd)
201 assert 'Erased: OK' in output, 'Erase operation failed'
203 cmd = 'mw.b %08x ff %x' % (addr, count)
204 u_boot_console.run_command(cmd)
205 crc_ffs = u_boot_utils.crc32(u_boot_console, addr, count)
207 crc_read = sf_read(u_boot_console, env__sf_config, sf_params)
208 assert crc_ffs == crc_read, 'Unexpected CRC32 after erase operation.'
210 @pytest.mark.buildconfigspec('cmd_sf')
211 @pytest.mark.buildconfigspec('cmd_crc32')
212 @pytest.mark.buildconfigspec('cmd_memory')
213 def test_sf_update(u_boot_console, env__sf_config):
214 if not env__sf_config.get('writeable', False):
215 pytest.skip('Flash config is tagged as not writeable')
217 sf_params = sf_prepare(u_boot_console, env__sf_config)
218 sf_update(u_boot_console, env__sf_config, sf_params)