2 * Command for accessing SPI flash.
4 * Copyright (C) 2008 Atmel Corporation
5 * Licensed under the GPL-2 or later.
10 #include <spi_flash.h>
14 #ifndef CONFIG_SF_DEFAULT_SPEED
15 # define CONFIG_SF_DEFAULT_SPEED 1000000
17 #ifndef CONFIG_SF_DEFAULT_MODE
18 # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
20 #ifndef CONFIG_SF_DEFAULT_CS
21 # define CONFIG_SF_DEFAULT_CS 0
23 #ifndef CONFIG_SF_DEFAULT_BUS
24 # define CONFIG_SF_DEFAULT_BUS 0
27 static struct spi_flash *flash;
31 * This function computes the length argument for the erase command.
32 * The length on which the command is to operate can be given in two forms:
33 * 1. <cmd> offset len - operate on <'offset', 'len')
34 * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)')
35 * If the second form is used and the length doesn't fall on the
36 * sector boundary, than it will be adjusted to the next sector boundary.
37 * If it isn't in the flash, the function will fail (return -1).
39 * arg: length specification (i.e. both command arguments)
41 * len: computed length for operation
44 * -1: failure (bad format, bad address).
46 static int sf_parse_len_arg(char *arg, ulong *len)
49 char round_up_len; /* indicates if the "+length" form used */
58 len_arg = simple_strtoul(arg, &ep, 16);
59 if (ep == arg || *ep != '\0')
62 if (round_up_len && flash->sector_size > 0)
63 *len = ROUND(len_arg, flash->sector_size);
71 * This function takes a byte length and a delta unit of time to compute the
72 * approximate bytes per second
74 * @param len amount of bytes currently processed
75 * @param start_ms start time of processing in ms
76 * @return bytes per second if OK, 0 on error
78 static ulong bytes_per_second(unsigned int len, ulong start_ms)
80 /* less accurate but avoids overflow */
81 if (len >= ((unsigned int) -1) / 1024)
82 return len / (max(get_timer(start_ms) / 1024, 1));
84 return 1024 * len / max(get_timer(start_ms), 1);
87 static int do_spi_flash_probe(int argc, char * const argv[])
89 unsigned int bus = CONFIG_SF_DEFAULT_BUS;
90 unsigned int cs = CONFIG_SF_DEFAULT_CS;
91 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
92 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
94 struct spi_flash *new;
97 cs = simple_strtoul(argv[1], &endp, 0);
98 if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
105 cs = simple_strtoul(endp + 1, &endp, 0);
112 speed = simple_strtoul(argv[2], &endp, 0);
113 if (*argv[2] == 0 || *endp != 0)
117 mode = simple_strtoul(argv[3], &endp, 16);
118 if (*argv[3] == 0 || *endp != 0)
122 new = spi_flash_probe(bus, cs, speed, mode);
124 printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
129 spi_flash_free(flash);
136 * Write a block of data to SPI flash, first checking if it is different from
137 * what is already there.
139 * If the data being written is the same, then *skipped is incremented by len.
141 * @param flash flash context pointer
142 * @param offset flash offset to write
143 * @param len number of bytes to write
144 * @param buf buffer to write from
145 * @param cmp_buf read buffer to use to compare data
146 * @param skipped Count of skipped data (incremented by this function)
147 * @return NULL if OK, else a string containing the stage which failed
149 static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
150 size_t len, const char *buf, char *cmp_buf, size_t *skipped)
152 debug("offset=%#x, sector_size=%#x, len=%#zx\n",
153 offset, flash->sector_size, len);
154 if (spi_flash_read(flash, offset, len, cmp_buf))
156 if (memcmp(cmp_buf, buf, len) == 0) {
157 debug("Skip region %x size %zx: no change\n",
162 if (spi_flash_erase(flash, offset, len))
164 if (spi_flash_write(flash, offset, len, buf))
170 * Update an area of SPI flash by erasing and writing any blocks which need
171 * to change. Existing blocks with the correct data are left unchanged.
173 * @param flash flash context pointer
174 * @param offset flash offset to write
175 * @param len number of bytes to write
176 * @param buf buffer to write from
177 * @return 0 if ok, 1 on error
179 static int spi_flash_update(struct spi_flash *flash, u32 offset,
180 size_t len, const char *buf)
182 const char *err_oper = NULL;
184 const char *end = buf + len;
185 size_t todo; /* number of bytes to do in this pass */
186 size_t skipped = 0; /* statistics */
187 const ulong start_time = get_timer(0);
189 const char *start_buf = buf;
192 if (end - buf >= 200)
193 scale = (end - buf) / 100;
194 cmp_buf = malloc(flash->sector_size);
196 ulong last_update = get_timer(0);
198 for (; buf < end && !err_oper; buf += todo, offset += todo) {
199 todo = min(end - buf, flash->sector_size);
200 if (get_timer(last_update) > 100) {
201 printf(" \rUpdating, %zu%% %lu B/s",
202 100 - (end - buf) / scale,
203 bytes_per_second(buf - start_buf,
205 last_update = get_timer(0);
207 err_oper = spi_flash_update_block(flash, offset, todo,
208 buf, cmp_buf, &skipped);
216 printf("SPI flash failed in %s step\n", err_oper);
220 delta = get_timer(start_time);
221 printf("%zu bytes written, %zu bytes skipped", len - skipped,
223 printf(" in %ld.%lds, speed %ld B/s\n",
224 delta / 1000, delta % 1000, bytes_per_second(len, start_time));
229 static int do_spi_flash_read_write(int argc, char * const argv[])
232 unsigned long offset;
241 addr = simple_strtoul(argv[1], &endp, 16);
242 if (*argv[1] == 0 || *endp != 0)
244 offset = simple_strtoul(argv[2], &endp, 16);
245 if (*argv[2] == 0 || *endp != 0)
247 len = simple_strtoul(argv[3], &endp, 16);
248 if (*argv[3] == 0 || *endp != 0)
251 /* Consistency checking */
252 if (offset + len > flash->size) {
253 printf("ERROR: attempting %s past flash size (%#x)\n",
254 argv[0], flash->size);
258 buf = map_physmem(addr, len, MAP_WRBACK);
260 puts("Failed to map physical memory\n");
264 if (strcmp(argv[0], "update") == 0)
265 ret = spi_flash_update(flash, offset, len, buf);
266 else if (strcmp(argv[0], "read") == 0)
267 ret = spi_flash_read(flash, offset, len, buf);
269 ret = spi_flash_write(flash, offset, len, buf);
271 unmap_physmem(buf, len);
274 printf("SPI flash %s failed\n", argv[0]);
281 static int do_spi_flash_erase(int argc, char * const argv[])
283 unsigned long offset;
291 offset = simple_strtoul(argv[1], &endp, 16);
292 if (*argv[1] == 0 || *endp != 0)
295 ret = sf_parse_len_arg(argv[2], &len);
299 /* Consistency checking */
300 if (offset + len > flash->size) {
301 printf("ERROR: attempting %s past flash size (%#x)\n",
302 argv[0], flash->size);
306 ret = spi_flash_erase(flash, offset, len);
308 printf("SPI flash %s failed\n", argv[0]);
315 static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
320 /* need at least two arguments */
328 if (strcmp(cmd, "probe") == 0) {
329 ret = do_spi_flash_probe(argc, argv);
333 /* The remaining commands require a selected device */
335 puts("No SPI flash selected. Please run `sf probe'\n");
339 if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 ||
340 strcmp(cmd, "update") == 0)
341 ret = do_spi_flash_read_write(argc, argv);
342 else if (strcmp(cmd, "erase") == 0)
343 ret = do_spi_flash_erase(argc, argv);
352 return CMD_RET_USAGE;
356 sf, 5, 1, do_spi_flash,
357 "SPI flash sub-system",
358 "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n"
360 "sf read addr offset len - read `len' bytes starting at\n"
361 " `offset' to memory at `addr'\n"
362 "sf write addr offset len - write `len' bytes from memory\n"
363 " at `addr' to flash at `offset'\n"
364 "sf erase offset [+]len - erase `len' bytes from `offset'\n"
365 " `+len' round up `len' to block size\n"
366 "sf update addr offset len - erase and write `len' bytes from memory\n"
367 " at `addr' to flash at `offset'"