blob: 69e0d46a557b14922e30ea20c72611965483e48d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoenb6368462008-05-16 11:10:34 +02002/*
3 * Command for accessing SPI flash.
4 *
5 * Copyright (C) 2008 Atmel Corporation
6 */
Mike Frysinger4166ee52009-10-09 17:12:44 -04007
Haavard Skinnemoenb6368462008-05-16 11:10:34 +02008#include <common.h>
Mingkai Hu381c6e22013-04-07 22:13:32 +00009#include <div64.h>
Simon Glassfbb09912014-10-13 23:42:07 -060010#include <dm.h>
Simon Glassb79fdc72020-05-10 11:39:54 -060011#include <flash.h>
Simon Glass8d1af942011-08-22 16:03:46 -070012#include <malloc.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050013#include <mapmem.h>
Simon Glassff0960f2014-10-13 23:42:04 -060014#include <spi.h>
Haavard Skinnemoenb6368462008-05-16 11:10:34 +020015#include <spi_flash.h>
Simon Glass90526e92020-05-10 11:39:56 -060016#include <asm/cache.h>
Heiko Schocher2ec1a402015-04-27 07:42:06 +020017#include <jffs2/jffs2.h>
18#include <linux/mtd/mtd.h>
Haavard Skinnemoenb6368462008-05-16 11:10:34 +020019
20#include <asm/io.h>
Simon Glassfbb09912014-10-13 23:42:07 -060021#include <dm/device-internal.h>
Haavard Skinnemoenb6368462008-05-16 11:10:34 +020022
Miquel Raynaleb446ef2019-10-25 19:39:29 +020023#include "legacy-mtd-utils.h"
24
Haavard Skinnemoenb6368462008-05-16 11:10:34 +020025static struct spi_flash *flash;
26
Richard Retanubun334eb4b2011-02-16 16:39:44 -050027/*
28 * This function computes the length argument for the erase command.
29 * The length on which the command is to operate can be given in two forms:
30 * 1. <cmd> offset len - operate on <'offset', 'len')
31 * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)')
32 * If the second form is used and the length doesn't fall on the
33 * sector boundary, than it will be adjusted to the next sector boundary.
34 * If it isn't in the flash, the function will fail (return -1).
35 * Input:
36 * arg: length specification (i.e. both command arguments)
37 * Output:
38 * len: computed length for operation
39 * Return:
40 * 1: success
41 * -1: failure (bad format, bad address).
42 */
43static int sf_parse_len_arg(char *arg, ulong *len)
44{
45 char *ep;
46 char round_up_len; /* indicates if the "+length" form used */
47 ulong len_arg;
48
49 round_up_len = 0;
50 if (*arg == '+') {
51 round_up_len = 1;
52 ++arg;
53 }
54
55 len_arg = simple_strtoul(arg, &ep, 16);
56 if (ep == arg || *ep != '\0')
57 return -1;
58
59 if (round_up_len && flash->sector_size > 0)
Anton Staaf155cfb52011-09-02 13:45:28 +000060 *len = ROUND(len_arg, flash->sector_size);
Richard Retanubun334eb4b2011-02-16 16:39:44 -050061 else
62 *len = len_arg;
63
64 return 1;
65}
66
James Millera683c282012-09-28 14:28:00 +000067/**
68 * This function takes a byte length and a delta unit of time to compute the
69 * approximate bytes per second
70 *
71 * @param len amount of bytes currently processed
72 * @param start_ms start time of processing in ms
73 * @return bytes per second if OK, 0 on error
74 */
75static ulong bytes_per_second(unsigned int len, ulong start_ms)
76{
77 /* less accurate but avoids overflow */
78 if (len >= ((unsigned int) -1) / 1024)
Masahiro Yamadab4141192014-11-07 03:03:31 +090079 return len / (max(get_timer(start_ms) / 1024, 1UL));
James Millera683c282012-09-28 14:28:00 +000080 else
Masahiro Yamadab4141192014-11-07 03:03:31 +090081 return 1024 * len / max(get_timer(start_ms), 1UL);
James Millera683c282012-09-28 14:28:00 +000082}
83
Wolfgang Denk54841ab2010-06-28 22:00:46 +020084static int do_spi_flash_probe(int argc, char * const argv[])
Haavard Skinnemoenb6368462008-05-16 11:10:34 +020085{
Eric Nelsonc1173bd2012-01-31 10:52:07 -070086 unsigned int bus = CONFIG_SF_DEFAULT_BUS;
87 unsigned int cs = CONFIG_SF_DEFAULT_CS;
Patrick Delaunayb0cc1b82019-02-27 15:36:44 +010088 /* In DM mode, defaults speed and mode will be taken from DT */
Haavard Skinnemoenb6368462008-05-16 11:10:34 +020089 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
90 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
91 char *endp;
Simon Glassfbb09912014-10-13 23:42:07 -060092#ifdef CONFIG_DM_SPI_FLASH
93 struct udevice *new, *bus_dev;
94 int ret;
95#else
Haavard Skinnemoenb6368462008-05-16 11:10:34 +020096 struct spi_flash *new;
Simon Glassfbb09912014-10-13 23:42:07 -060097#endif
Haavard Skinnemoenb6368462008-05-16 11:10:34 +020098
Eric Nelsonc1173bd2012-01-31 10:52:07 -070099 if (argc >= 2) {
100 cs = simple_strtoul(argv[1], &endp, 0);
101 if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
Mike Frysinger8a07de02011-04-12 02:21:31 -0400102 return -1;
Eric Nelsonc1173bd2012-01-31 10:52:07 -0700103 if (*endp == ':') {
104 if (endp[1] == 0)
105 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200106
Eric Nelsonc1173bd2012-01-31 10:52:07 -0700107 bus = cs;
108 cs = simple_strtoul(endp + 1, &endp, 0);
109 if (*endp != 0)
110 return -1;
111 }
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200112 }
113
114 if (argc >= 3) {
115 speed = simple_strtoul(argv[2], &endp, 0);
116 if (*argv[2] == 0 || *endp != 0)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400117 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200118 }
119 if (argc >= 4) {
TsiChung Liew6e8d58d2009-06-30 14:30:19 +0000120 mode = simple_strtoul(argv[3], &endp, 16);
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200121 if (*argv[3] == 0 || *endp != 0)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400122 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200123 }
124
Simon Glassfbb09912014-10-13 23:42:07 -0600125#ifdef CONFIG_DM_SPI_FLASH
126 /* Remove the old device, otherwise probe will just be a nop */
127 ret = spi_find_bus_and_cs(bus, cs, &bus_dev, &new);
128 if (!ret) {
Stefan Roese706865a2017-03-20 12:51:48 +0100129 device_remove(new, DM_REMOVE_NORMAL);
Simon Glassfbb09912014-10-13 23:42:07 -0600130 }
131 flash = NULL;
132 ret = spi_flash_probe_bus_cs(bus, cs, speed, mode, &new);
133 if (ret) {
134 printf("Failed to initialize SPI flash at %u:%u (error %d)\n",
135 bus, cs, ret);
136 return 1;
137 }
138
Simon Glasse564f052015-03-05 12:25:20 -0700139 flash = dev_get_uclass_priv(new);
Simon Glassfbb09912014-10-13 23:42:07 -0600140#else
Heiko Schocher8ee81b72015-04-27 07:42:07 +0200141 if (flash)
142 spi_flash_free(flash);
143
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200144 new = spi_flash_probe(bus, cs, speed, mode);
Heiko Schocher8ee81b72015-04-27 07:42:07 +0200145 flash = new;
146
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200147 if (!new) {
148 printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
149 return 1;
150 }
151
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200152 flash = new;
Simon Glassfbb09912014-10-13 23:42:07 -0600153#endif
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200154
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200155 return 0;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200156}
157
Simon Glass8d1af942011-08-22 16:03:46 -0700158/**
159 * Write a block of data to SPI flash, first checking if it is different from
160 * what is already there.
161 *
162 * If the data being written is the same, then *skipped is incremented by len.
163 *
164 * @param flash flash context pointer
165 * @param offset flash offset to write
166 * @param len number of bytes to write
167 * @param buf buffer to write from
168 * @param cmp_buf read buffer to use to compare data
169 * @param skipped Count of skipped data (incremented by this function)
170 * @return NULL if OK, else a string containing the stage which failed
171 */
172static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
173 size_t len, const char *buf, char *cmp_buf, size_t *skipped)
174{
Stefan Roeseed627562015-01-09 14:39:22 +0100175 char *ptr = (char *)buf;
176
Andreas Bießmanna97f6ef2011-12-15 09:56:53 +0100177 debug("offset=%#x, sector_size=%#x, len=%#zx\n",
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +0530178 offset, flash->sector_size, len);
Gerlando Falautob95f9582013-08-27 19:13:57 +0530179 /* Read the entire sector so to allow for rewriting */
180 if (spi_flash_read(flash, offset, flash->sector_size, cmp_buf))
Simon Glass8d1af942011-08-22 16:03:46 -0700181 return "read";
Gerlando Falautob95f9582013-08-27 19:13:57 +0530182 /* Compare only what is meaningful (len) */
Simon Glass8d1af942011-08-22 16:03:46 -0700183 if (memcmp(cmp_buf, buf, len) == 0) {
Andreas Bießmanna97f6ef2011-12-15 09:56:53 +0100184 debug("Skip region %x size %zx: no change\n",
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +0530185 offset, len);
Simon Glass8d1af942011-08-22 16:03:46 -0700186 *skipped += len;
187 return NULL;
188 }
Gerlando Falauto34888502013-07-03 20:33:57 +0200189 /* Erase the entire sector */
190 if (spi_flash_erase(flash, offset, flash->sector_size))
Simon Glass8d1af942011-08-22 16:03:46 -0700191 return "erase";
Stefan Roeseed627562015-01-09 14:39:22 +0100192 /* If it's a partial sector, copy the data into the temp-buffer */
Gerlando Falautob95f9582013-08-27 19:13:57 +0530193 if (len != flash->sector_size) {
Stefan Roeseed627562015-01-09 14:39:22 +0100194 memcpy(cmp_buf, buf, len);
195 ptr = cmp_buf;
Gerlando Falautob95f9582013-08-27 19:13:57 +0530196 }
Stefan Roeseed627562015-01-09 14:39:22 +0100197 /* Write one complete sector */
198 if (spi_flash_write(flash, offset, flash->sector_size, ptr))
199 return "write";
Gerlando Falautob95f9582013-08-27 19:13:57 +0530200
Simon Glass8d1af942011-08-22 16:03:46 -0700201 return NULL;
202}
203
204/**
205 * Update an area of SPI flash by erasing and writing any blocks which need
206 * to change. Existing blocks with the correct data are left unchanged.
207 *
208 * @param flash flash context pointer
209 * @param offset flash offset to write
210 * @param len number of bytes to write
211 * @param buf buffer to write from
212 * @return 0 if ok, 1 on error
213 */
214static int spi_flash_update(struct spi_flash *flash, u32 offset,
215 size_t len, const char *buf)
216{
217 const char *err_oper = NULL;
218 char *cmp_buf;
219 const char *end = buf + len;
220 size_t todo; /* number of bytes to do in this pass */
Kumar Gala8e8a4bc2011-10-03 04:35:47 +0000221 size_t skipped = 0; /* statistics */
James Millera683c282012-09-28 14:28:00 +0000222 const ulong start_time = get_timer(0);
223 size_t scale = 1;
224 const char *start_buf = buf;
225 ulong delta;
Simon Glass8d1af942011-08-22 16:03:46 -0700226
James Millera683c282012-09-28 14:28:00 +0000227 if (end - buf >= 200)
228 scale = (end - buf) / 100;
Ravi Babu156e96f2015-08-17 13:29:48 +0530229 cmp_buf = memalign(ARCH_DMA_MINALIGN, flash->sector_size);
Simon Glass8d1af942011-08-22 16:03:46 -0700230 if (cmp_buf) {
James Millera683c282012-09-28 14:28:00 +0000231 ulong last_update = get_timer(0);
232
Kumar Gala8e8a4bc2011-10-03 04:35:47 +0000233 for (; buf < end && !err_oper; buf += todo, offset += todo) {
Masahiro Yamadab4141192014-11-07 03:03:31 +0900234 todo = min_t(size_t, end - buf, flash->sector_size);
James Millera683c282012-09-28 14:28:00 +0000235 if (get_timer(last_update) > 100) {
236 printf(" \rUpdating, %zu%% %lu B/s",
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +0530237 100 - (end - buf) / scale,
James Millera683c282012-09-28 14:28:00 +0000238 bytes_per_second(buf - start_buf,
239 start_time));
240 last_update = get_timer(0);
241 }
Simon Glass8d1af942011-08-22 16:03:46 -0700242 err_oper = spi_flash_update_block(flash, offset, todo,
243 buf, cmp_buf, &skipped);
244 }
245 } else {
246 err_oper = "malloc";
247 }
248 free(cmp_buf);
James Millera683c282012-09-28 14:28:00 +0000249 putc('\r');
Simon Glass8d1af942011-08-22 16:03:46 -0700250 if (err_oper) {
251 printf("SPI flash failed in %s step\n", err_oper);
252 return 1;
253 }
James Millera683c282012-09-28 14:28:00 +0000254
255 delta = get_timer(start_time);
256 printf("%zu bytes written, %zu bytes skipped", len - skipped,
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +0530257 skipped);
James Millera683c282012-09-28 14:28:00 +0000258 printf(" in %ld.%lds, speed %ld B/s\n",
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +0530259 delta / 1000, delta % 1000, bytes_per_second(len, start_time));
Kumar Gala8e8a4bc2011-10-03 04:35:47 +0000260
Simon Glass8d1af942011-08-22 16:03:46 -0700261 return 0;
262}
263
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200264static int do_spi_flash_read_write(int argc, char * const argv[])
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200265{
266 unsigned long addr;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200267 void *buf;
268 char *endp;
Jagannadha Sutradharudu Teki60b66142013-05-26 14:52:52 +0530269 int ret = 1;
Heiko Schocher2ec1a402015-04-27 07:42:06 +0200270 int dev = 0;
271 loff_t offset, len, maxsize;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200272
Heiko Schocher2ec1a402015-04-27 07:42:06 +0200273 if (argc < 3)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400274 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200275
276 addr = simple_strtoul(argv[1], &endp, 16);
277 if (*argv[1] == 0 || *endp != 0)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400278 return -1;
Heiko Schocher2ec1a402015-04-27 07:42:06 +0200279
280 if (mtd_arg_off_size(argc - 2, &argv[2], &dev, &offset, &len,
281 &maxsize, MTD_DEV_TYPE_NOR, flash->size))
Mike Frysinger8a07de02011-04-12 02:21:31 -0400282 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200283
Gerlando Falauto86493992012-04-03 04:34:13 +0000284 /* Consistency checking */
285 if (offset + len > flash->size) {
286 printf("ERROR: attempting %s past flash size (%#x)\n",
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +0530287 argv[0], flash->size);
Gerlando Falauto86493992012-04-03 04:34:13 +0000288 return 1;
289 }
290
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200291 buf = map_physmem(addr, len, MAP_WRBACK);
Liam Beguin10f087f2018-03-14 19:15:11 -0400292 if (!buf && addr) {
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200293 puts("Failed to map physical memory\n");
294 return 1;
295 }
296
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +0530297 if (strcmp(argv[0], "update") == 0) {
Simon Glass8d1af942011-08-22 16:03:46 -0700298 ret = spi_flash_update(flash, offset, len, buf);
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +0530299 } else if (strncmp(argv[0], "read", 4) == 0 ||
Jagannadha Sutradharudu Teki60b66142013-05-26 14:52:52 +0530300 strncmp(argv[0], "write", 5) == 0) {
301 int read;
302
303 read = strncmp(argv[0], "read", 4) == 0;
304 if (read)
305 ret = spi_flash_read(flash, offset, len, buf);
306 else
307 ret = spi_flash_write(flash, offset, len, buf);
308
Simon Glassa7d0711a2015-06-07 08:50:32 -0600309 printf("SF: %zu bytes @ %#x %s: ", (size_t)len, (u32)offset,
310 read ? "Read" : "Written");
311 if (ret)
312 printf("ERROR %d\n", ret);
313 else
314 printf("OK\n");
Jagannadha Sutradharudu Teki60b66142013-05-26 14:52:52 +0530315 }
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200316
317 unmap_physmem(buf, len);
318
Jagannadha Sutradharudu Teki60b66142013-05-26 14:52:52 +0530319 return ret == 0 ? 0 : 1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200320}
321
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200322static int do_spi_flash_erase(int argc, char * const argv[])
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200323{
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200324 int ret;
Heiko Schocher2ec1a402015-04-27 07:42:06 +0200325 int dev = 0;
326 loff_t offset, len, maxsize;
327 ulong size;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200328
329 if (argc < 3)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400330 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200331
Heiko Schocher2ec1a402015-04-27 07:42:06 +0200332 if (mtd_arg_off(argv[1], &dev, &offset, &len, &maxsize,
333 MTD_DEV_TYPE_NOR, flash->size))
Mike Frysinger8a07de02011-04-12 02:21:31 -0400334 return -1;
Richard Retanubun334eb4b2011-02-16 16:39:44 -0500335
Heiko Schocher2ec1a402015-04-27 07:42:06 +0200336 ret = sf_parse_len_arg(argv[2], &size);
Richard Retanubun334eb4b2011-02-16 16:39:44 -0500337 if (ret != 1)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400338 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200339
Gerlando Falauto86493992012-04-03 04:34:13 +0000340 /* Consistency checking */
Heiko Schocher2ec1a402015-04-27 07:42:06 +0200341 if (offset + size > flash->size) {
Gerlando Falauto86493992012-04-03 04:34:13 +0000342 printf("ERROR: attempting %s past flash size (%#x)\n",
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +0530343 argv[0], flash->size);
Gerlando Falauto86493992012-04-03 04:34:13 +0000344 return 1;
345 }
346
Heiko Schocher2ec1a402015-04-27 07:42:06 +0200347 ret = spi_flash_erase(flash, offset, size);
348 printf("SF: %zu bytes @ %#x Erased: %s\n", (size_t)size, (u32)offset,
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +0530349 ret ? "ERROR" : "OK");
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200350
Jagannadha Sutradharudu Teki96bbf552013-05-26 01:35:21 +0530351 return ret == 0 ? 0 : 1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200352}
353
Fabio Estevamc3c016c2015-11-05 12:43:42 -0200354static int do_spi_protect(int argc, char * const argv[])
355{
356 int ret = 0;
357 loff_t start, len;
358 bool prot = false;
359
360 if (argc != 4)
361 return -1;
362
363 if (!str2off(argv[2], &start)) {
364 puts("start sector is not a valid number\n");
365 return 1;
366 }
367
368 if (!str2off(argv[3], &len)) {
369 puts("len is not a valid number\n");
370 return 1;
371 }
372
373 if (strcmp(argv[1], "lock") == 0)
374 prot = true;
375 else if (strcmp(argv[1], "unlock") == 0)
376 prot = false;
377 else
378 return -1; /* Unknown parameter */
379
380 ret = spi_flash_protect(flash, start, len, prot);
381
382 return ret == 0 ? 0 : 1;
383}
384
Simon Glass24007272012-10-08 13:16:02 +0000385#ifdef CONFIG_CMD_SF_TEST
386enum {
387 STAGE_ERASE,
388 STAGE_CHECK,
389 STAGE_WRITE,
390 STAGE_READ,
391
392 STAGE_COUNT,
393};
394
395static char *stage_name[STAGE_COUNT] = {
396 "erase",
397 "check",
398 "write",
399 "read",
400};
401
402struct test_info {
403 int stage;
404 int bytes;
405 unsigned base_ms;
406 unsigned time_ms[STAGE_COUNT];
407};
408
409static void show_time(struct test_info *test, int stage)
410{
411 uint64_t speed; /* KiB/s */
412 int bps; /* Bits per second */
413
414 speed = (long long)test->bytes * 1000;
Simon Glassd1f22d42013-11-10 10:26:51 -0700415 if (test->time_ms[stage])
416 do_div(speed, test->time_ms[stage] * 1024);
Simon Glass24007272012-10-08 13:16:02 +0000417 bps = speed * 8;
418
Heinrich Schuchardtc6b2ea32019-01-06 12:03:37 +0100419 printf("%d %s: %u ticks, %d KiB/s %d.%03d Mbps\n", stage,
Simon Glass24007272012-10-08 13:16:02 +0000420 stage_name[stage], test->time_ms[stage],
421 (int)speed, bps / 1000, bps % 1000);
422}
423
424static void spi_test_next_stage(struct test_info *test)
425{
426 test->time_ms[test->stage] = get_timer(test->base_ms);
427 show_time(test, test->stage);
428 test->base_ms = get_timer(0);
429 test->stage++;
430}
431
432/**
433 * Run a test on the SPI flash
434 *
435 * @param flash SPI flash to use
436 * @param buf Source buffer for data to write
437 * @param len Size of data to read/write
438 * @param offset Offset within flash to check
439 * @param vbuf Verification buffer
440 * @return 0 if ok, -1 on error
441 */
Simon Glass1e7133e2013-03-11 06:08:13 +0000442static int spi_flash_test(struct spi_flash *flash, uint8_t *buf, ulong len,
443 ulong offset, uint8_t *vbuf)
Simon Glass24007272012-10-08 13:16:02 +0000444{
445 struct test_info test;
446 int i;
447
448 printf("SPI flash test:\n");
449 memset(&test, '\0', sizeof(test));
450 test.base_ms = get_timer(0);
451 test.bytes = len;
452 if (spi_flash_erase(flash, offset, len)) {
453 printf("Erase failed\n");
454 return -1;
455 }
456 spi_test_next_stage(&test);
457
458 if (spi_flash_read(flash, offset, len, vbuf)) {
459 printf("Check read failed\n");
460 return -1;
461 }
462 for (i = 0; i < len; i++) {
463 if (vbuf[i] != 0xff) {
464 printf("Check failed at %d\n", i);
Masahiro Yamadab4141192014-11-07 03:03:31 +0900465 print_buffer(i, vbuf + i, 1,
466 min_t(uint, len - i, 0x40), 0);
Simon Glass24007272012-10-08 13:16:02 +0000467 return -1;
468 }
469 }
470 spi_test_next_stage(&test);
471
472 if (spi_flash_write(flash, offset, len, buf)) {
473 printf("Write failed\n");
474 return -1;
475 }
476 memset(vbuf, '\0', len);
477 spi_test_next_stage(&test);
478
479 if (spi_flash_read(flash, offset, len, vbuf)) {
480 printf("Read failed\n");
481 return -1;
482 }
483 spi_test_next_stage(&test);
484
485 for (i = 0; i < len; i++) {
486 if (buf[i] != vbuf[i]) {
487 printf("Verify failed at %d, good data:\n", i);
Masahiro Yamadab4141192014-11-07 03:03:31 +0900488 print_buffer(i, buf + i, 1,
489 min_t(uint, len - i, 0x40), 0);
Simon Glass24007272012-10-08 13:16:02 +0000490 printf("Bad data:\n");
Masahiro Yamadab4141192014-11-07 03:03:31 +0900491 print_buffer(i, vbuf + i, 1,
492 min_t(uint, len - i, 0x40), 0);
Simon Glass24007272012-10-08 13:16:02 +0000493 return -1;
494 }
495 }
496 printf("Test passed\n");
497 for (i = 0; i < STAGE_COUNT; i++)
498 show_time(&test, i);
499
500 return 0;
501}
502
503static int do_spi_flash_test(int argc, char * const argv[])
504{
505 unsigned long offset;
506 unsigned long len;
Simon Glassd1f22d42013-11-10 10:26:51 -0700507 uint8_t *buf, *from;
Simon Glass24007272012-10-08 13:16:02 +0000508 char *endp;
Simon Glass1e7133e2013-03-11 06:08:13 +0000509 uint8_t *vbuf;
Simon Glass24007272012-10-08 13:16:02 +0000510 int ret;
511
Simon Glassd1f22d42013-11-10 10:26:51 -0700512 if (argc < 3)
513 return -1;
Simon Glass24007272012-10-08 13:16:02 +0000514 offset = simple_strtoul(argv[1], &endp, 16);
515 if (*argv[1] == 0 || *endp != 0)
516 return -1;
517 len = simple_strtoul(argv[2], &endp, 16);
518 if (*argv[2] == 0 || *endp != 0)
519 return -1;
520
Ravi Babu156e96f2015-08-17 13:29:48 +0530521 vbuf = memalign(ARCH_DMA_MINALIGN, len);
Simon Glass24007272012-10-08 13:16:02 +0000522 if (!vbuf) {
Simon Glassd1f22d42013-11-10 10:26:51 -0700523 printf("Cannot allocate memory (%lu bytes)\n", len);
Simon Glass24007272012-10-08 13:16:02 +0000524 return 1;
525 }
Ravi Babu156e96f2015-08-17 13:29:48 +0530526 buf = memalign(ARCH_DMA_MINALIGN, len);
Simon Glass24007272012-10-08 13:16:02 +0000527 if (!buf) {
528 free(vbuf);
Simon Glassd1f22d42013-11-10 10:26:51 -0700529 printf("Cannot allocate memory (%lu bytes)\n", len);
Simon Glass24007272012-10-08 13:16:02 +0000530 return 1;
531 }
532
Simon Glassd1f22d42013-11-10 10:26:51 -0700533 from = map_sysmem(CONFIG_SYS_TEXT_BASE, 0);
534 memcpy(buf, from, len);
Simon Glass24007272012-10-08 13:16:02 +0000535 ret = spi_flash_test(flash, buf, len, offset, vbuf);
536 free(vbuf);
537 free(buf);
538 if (ret) {
539 printf("Test failed\n");
540 return 1;
541 }
542
543 return 0;
544}
545#endif /* CONFIG_CMD_SF_TEST */
546
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +0530547static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc,
548 char * const argv[])
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200549{
550 const char *cmd;
Mike Frysinger8a07de02011-04-12 02:21:31 -0400551 int ret;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200552
553 /* need at least two arguments */
554 if (argc < 2)
555 goto usage;
556
557 cmd = argv[1];
Mike Frysinger8a07de02011-04-12 02:21:31 -0400558 --argc;
559 ++argv;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200560
Mike Frysinger8a07de02011-04-12 02:21:31 -0400561 if (strcmp(cmd, "probe") == 0) {
562 ret = do_spi_flash_probe(argc, argv);
563 goto done;
564 }
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200565
566 /* The remaining commands require a selected device */
567 if (!flash) {
568 puts("No SPI flash selected. Please run `sf probe'\n");
569 return 1;
570 }
571
Simon Glass8d1af942011-08-22 16:03:46 -0700572 if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 ||
573 strcmp(cmd, "update") == 0)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400574 ret = do_spi_flash_read_write(argc, argv);
575 else if (strcmp(cmd, "erase") == 0)
576 ret = do_spi_flash_erase(argc, argv);
Fabio Estevamc3c016c2015-11-05 12:43:42 -0200577 else if (strcmp(cmd, "protect") == 0)
578 ret = do_spi_protect(argc, argv);
Simon Glass24007272012-10-08 13:16:02 +0000579#ifdef CONFIG_CMD_SF_TEST
580 else if (!strcmp(cmd, "test"))
581 ret = do_spi_flash_test(argc, argv);
582#endif
Mike Frysinger8a07de02011-04-12 02:21:31 -0400583 else
584 ret = -1;
585
586done:
587 if (ret != -1)
588 return ret;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200589
590usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000591 return CMD_RET_USAGE;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200592}
593
Simon Glass24007272012-10-08 13:16:02 +0000594#ifdef CONFIG_CMD_SF_TEST
595#define SF_TEST_HELP "\nsf test offset len " \
596 "- run a very basic destructive test"
597#else
598#define SF_TEST_HELP
599#endif
600
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200601U_BOOT_CMD(
602 sf, 5, 1, do_spi_flash,
Peter Tyser2fb26042009-01-27 18:03:12 -0600603 "SPI flash sub-system",
Eric Nelsonc1173bd2012-01-31 10:52:07 -0700604 "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n"
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200605 " and chip select\n"
Heiko Schocher2ec1a402015-04-27 07:42:06 +0200606 "sf read addr offset|partition len - read `len' bytes starting at\n"
607 " `offset' or from start of mtd\n"
608 " `partition'to memory at `addr'\n"
609 "sf write addr offset|partition len - write `len' bytes from memory\n"
610 " at `addr' to flash at `offset'\n"
611 " or to start of mtd `partition'\n"
612 "sf erase offset|partition [+]len - erase `len' bytes from `offset'\n"
613 " or from start of mtd `partition'\n"
614 " `+len' round up `len' to block size\n"
615 "sf update addr offset|partition len - erase and write `len' bytes from memory\n"
616 " at `addr' to flash at `offset'\n"
617 " or to start of mtd `partition'\n"
Fabio Estevamc3c016c2015-11-05 12:43:42 -0200618 "sf protect lock/unlock sector len - protect/unprotect 'len' bytes starting\n"
619 " at address 'sector'\n"
Simon Glass24007272012-10-08 13:16:02 +0000620 SF_TEST_HELP
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200621);