blob: 24d5dc692db6158349dd6a5b0aac5491ec8f8bac [file] [log] [blame]
Simon Glass9fa20b22021-09-19 15:49:35 -06001.. SPDX-License-Identifier: GPL-2.0+:
2
Heinrich Schuchardt1b0c3162024-01-14 14:53:13 +01003.. index::
4 single: sf (command)
5
Simon Glass9fa20b22021-09-19 15:49:35 -06006sf command
7==========
8
9Synopis
10-------
11
12::
13
14 sf probe [[[<bus>:]<cs>] [<hz> [<mode>]]]
15 sf read <addr> <offset>|<partition> <len>
16 sf write <addr> <offset>|<partition> <len>
17 sf erase <offset>|<partition> <len>
18 sf update <addr> <offset>|<partition> <len>
19 sf protect lock|unlock <sector> <len>
20 sf test <offset>|<partition> <len>
21
22Description
23-----------
24
25The *sf* command is used to access SPI flash, supporting read/write/erase and
26a few other functions.
27
28Probe
29-----
30
31The flash must first be probed with *sf probe* before any of the other
32subcommands can be used. All of the parameters are optional:
33
34bus
35 SPI bus number containing the SPI-flash chip, e.g. 0. If you don't know
36 the number, you can use 'dm uclass' to see all the spi devices,
37 and check the value for 'seq' for each one (here 0 and 2)::
38
39 uclass 89: spi
40 0 spi@0 @ 05484960, seq 0
41 1 spi@1 @ 05484b40, seq 2
42
43cs
44 SPI chip-select to use for the chip. This is often 0 and can be omitted,
45 but in some cases multiple slaves are attached to a SPI controller,
46 selected by a chip-select line for each one.
47
48hz
49 Speed of the SPI bus in hertz. This normally defaults to 100000, i.e.
50 100KHz, which is very slow. Note that if the device exists in the
51 device tree, there might be a speed provided there, in which case this
52 setting is ignored.
53
54mode
55 SPI mode to use:
56
57 ===== ================
58 Mode Meaning
59 ===== ================
60 0 CPOL=0, CPHA=0
61 1 CPOL=0, CPHA=1
62 2 CPOL=1, CPHA=0
63 3 CPOL=1, CPHA=1
64 ===== ================
65
66 Clock phase (CPHA) 0 means that data is transferred (sampled) on the
67 first clock edge; 1 means the second.
68
69 Clock polarity (CPOL) controls the idle state of the clock, 0 for low,
70 1 for high.
71 The active state is the opposite of idle.
72
73 You may find this `SPI documentation`_ useful.
74
75Parameters for other subcommands (described below) are as follows:
76
77addr
78 Memory address to start transfer
79
80offset
81 Flash offset to start transfer
82
83partition
84 If the parameter is not numeric, it is assumed to be a partition
85 description in the format <dev_type><dev_num>,<part_num> which is not
86 covered here. This requires CONFIG_CMD_MTDPARTS.
87
88len
89 Number of bytes to transfer
90
91Read
92~~~~
93
94Use *sf read* to read from SPI flash to memory. The read will fail if an
95attempt is made to read past the end of the flash.
96
97
98Write
99~~~~~
100
101Use *sf write* to write from memory to SPI flash. The SPI flash should be
102erased first, since otherwise the result is undefined.
103
104The write will fail if an attempt is made to read past the end of the flash.
105
106
107Erase
108~~~~~
109
110Use *sf erase* to erase a region of SPI flash. The erase will fail if any part
111of the region to be erased is protected or lies past the end of the flash. It
112may also fail if the start offset or length are not aligned to an erase region
113(e.g. 256 bytes).
114
115
116Update
117~~~~~~
118
119Use *sf update* to automatically erase and update a region of SPI flash from
120memory. This works a sector at a time (typical 4KB or 64KB). For each
121sector it first checks if the sector already has the right data. If so it is
122skipped. If not, the sector is erased and the new data written. Note that if
123the length is not a multiple of the erase size, the space after the data in
124the last sector will be erased. If the offset does not start at the beginning
125of an erase block, the operation will fail.
126
127Speed statistics are shown including the number of bytes that were already
128correct.
129
130
131Protect
132~~~~~~~
133
134SPI-flash chips often have a protection feature where the chip is split up into
135regions which can be locked or unlocked. With *sf protect* it is possible to
136change these settings, if supported by the driver.
137
138lock|unlock
139 Selects whether to lock or unlock the sectors
140
141<sector>
142 Start sector number to lock/unlock. This may be the byte offset or some
143 other value, depending on the chip.
144
145<len>
146 Number of bytes to lock/unlock
147
148
149Test
150~~~~
151
152A convenient and fast *sf test* subcommand provides a way to check that SPI
153flash is working as expected. This works in four stages:
154
155 * erase - erases the entire region
156 * check - checks that the region is erased
157 * write - writes a test pattern to the region, consisting of the U-Boot code
158 * read - reads back the test pattern to check that it was written correctly
159
160Memory is allocated for two buffers, each <len> bytes in size. At typical
161size is 64KB to 1MB. The offset and size must be aligned to an erase boundary.
162
163Note that this test will fail if any part of the SPI flash is write-protected.
164
165
166Examples
167--------
168
169This first example uses sandbox::
170
171 => sf probe
172 SF: Detected m25p16 with page size 256 Bytes, erase size 64 KiB, total 2 MiB
173 => sf read 1000 1100 80000
174 device 0 offset 0x1100, size 0x80000
175 SF: 524288 bytes @ 0x1100 Read: OK
176 => md 1000
177 00001000: edfe0dd0 f33a0000 78000000 84250000 ......:....x..%.
178 00001010: 28000000 11000000 10000000 00000000 ...(............
179 00001020: 6f050000 0c250000 00000000 00000000 ...o..%.........
180 00001030: 00000000 00000000 00000000 00000000 ................
181 00001040: 00000000 00000000 00000000 00000000 ................
182 00001050: 00000000 00000000 00000000 00000000 ................
183 00001060: 00000000 00000000 00000000 00000000 ................
184 00001070: 00000000 00000000 01000000 00000000 ................
185 00001080: 03000000 04000000 00000000 01000000 ................
186 00001090: 03000000 04000000 0f000000 01000000 ................
187 000010a0: 03000000 08000000 1b000000 646e6173 ............sand
188 000010b0: 00786f62 03000000 08000000 21000000 box............!
189 000010c0: 646e6173 00786f62 01000000 61696c61 sandbox.....alia
190 000010d0: 00736573 03000000 07000000 2c000000 ses............,
191 000010e0: 6332692f 00003040 03000000 07000000 /i2c@0..........
192 000010f0: 31000000 6963702f 00003040 03000000 ...1/pci@0......
193 => sf erase 0 80000
194 SF: 524288 bytes @ 0x0 Erased: OK
195 => sf read 1000 1100 80000
196 device 0 offset 0x1100, size 0x80000
197 SF: 524288 bytes @ 0x1100 Read: OK
198 => md 1000
199 00001000: ffffffff ffffffff ffffffff ffffffff ................
200 00001010: ffffffff ffffffff ffffffff ffffffff ................
201 00001020: ffffffff ffffffff ffffffff ffffffff ................
202 00001030: ffffffff ffffffff ffffffff ffffffff ................
203 00001040: ffffffff ffffffff ffffffff ffffffff ................
204 00001050: ffffffff ffffffff ffffffff ffffffff ................
205 00001060: ffffffff ffffffff ffffffff ffffffff ................
206 00001070: ffffffff ffffffff ffffffff ffffffff ................
207 00001080: ffffffff ffffffff ffffffff ffffffff ................
208 00001090: ffffffff ffffffff ffffffff ffffffff ................
209 000010a0: ffffffff ffffffff ffffffff ffffffff ................
210 000010b0: ffffffff ffffffff ffffffff ffffffff ................
211 000010c0: ffffffff ffffffff ffffffff ffffffff ................
212 000010d0: ffffffff ffffffff ffffffff ffffffff ................
213 000010e0: ffffffff ffffffff ffffffff ffffffff ................
214 000010f0: ffffffff ffffffff ffffffff ffffffff ................
215
216This second example is running on coral, an x86 Chromebook::
217
218 => sf probe
219 SF: Detected w25q128fw with page size 256 Bytes, erase size 4 KiB, total 16 MiB
220 => sf erase 300000 80000
221 SF: 524288 bytes @ 0x300000 Erased: OK
222 => sf update 1110000 300000 80000
223 device 0 offset 0x300000, size 0x80000
224 524288 bytes written, 0 bytes skipped in 0.457s, speed 1164578 B/s
225
226 # This does nothing as the flash is already updated
227 => sf update 1110000 300000 80000
228 device 0 offset 0x300000, size 0x80000
229 0 bytes written, 524288 bytes skipped in 0.196s, speed 2684354 B/s
230 => sf test 00000 80000 # try a protected region
231 SPI flash test:
232 Erase failed (err = -5)
233 Test failed
234 => sf test 800000 80000
235 SPI flash test:
236 0 erase: 18 ticks, 28444 KiB/s 227.552 Mbps
237 1 check: 192 ticks, 2666 KiB/s 21.328 Mbps
238 2 write: 227 ticks, 2255 KiB/s 18.040 Mbps
239 3 read: 189 ticks, 2708 KiB/s 21.664 Mbps
240 Test passed
241 0 erase: 18 ticks, 28444 KiB/s 227.552 Mbps
242 1 check: 192 ticks, 2666 KiB/s 21.328 Mbps
243 2 write: 227 ticks, 2255 KiB/s 18.040 Mbps
244 3 read: 189 ticks, 2708 KiB/s 21.664 Mbps
245
246
247.. _SPI documentation:
248 https://en.wikipedia.org/wiki/Serial_Peripheral_Interface