blob: 83e2785e00da512391870fc371e9e7af3ba7bbe5 [file] [log] [blame]
Haavard Skinnemoenb6368462008-05-16 11:10:34 +02001/*
2 * Command for accessing SPI flash.
3 *
4 * Copyright (C) 2008 Atmel Corporation
Mike Frysinger4166ee52009-10-09 17:12:44 -04005 * Licensed under the GPL-2 or later.
Haavard Skinnemoenb6368462008-05-16 11:10:34 +02006 */
Mike Frysinger4166ee52009-10-09 17:12:44 -04007
Haavard Skinnemoenb6368462008-05-16 11:10:34 +02008#include <common.h>
Simon Glass8d1af942011-08-22 16:03:46 -07009#include <malloc.h>
Haavard Skinnemoenb6368462008-05-16 11:10:34 +020010#include <spi_flash.h>
11
12#include <asm/io.h>
13
14#ifndef CONFIG_SF_DEFAULT_SPEED
15# define CONFIG_SF_DEFAULT_SPEED 1000000
16#endif
17#ifndef CONFIG_SF_DEFAULT_MODE
18# define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
19#endif
Eric Nelsonc1173bd2012-01-31 10:52:07 -070020#ifndef CONFIG_SF_DEFAULT_CS
21# define CONFIG_SF_DEFAULT_CS 0
22#endif
23#ifndef CONFIG_SF_DEFAULT_BUS
24# define CONFIG_SF_DEFAULT_BUS 0
25#endif
Haavard Skinnemoenb6368462008-05-16 11:10:34 +020026
27static struct spi_flash *flash;
28
Richard Retanubun334eb4b2011-02-16 16:39:44 -050029
30/*
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).
38 * Input:
39 * arg: length specification (i.e. both command arguments)
40 * Output:
41 * len: computed length for operation
42 * Return:
43 * 1: success
44 * -1: failure (bad format, bad address).
45 */
46static int sf_parse_len_arg(char *arg, ulong *len)
47{
48 char *ep;
49 char round_up_len; /* indicates if the "+length" form used */
50 ulong len_arg;
51
52 round_up_len = 0;
53 if (*arg == '+') {
54 round_up_len = 1;
55 ++arg;
56 }
57
58 len_arg = simple_strtoul(arg, &ep, 16);
59 if (ep == arg || *ep != '\0')
60 return -1;
61
62 if (round_up_len && flash->sector_size > 0)
Anton Staaf155cfb52011-09-02 13:45:28 +000063 *len = ROUND(len_arg, flash->sector_size);
Richard Retanubun334eb4b2011-02-16 16:39:44 -050064 else
65 *len = len_arg;
66
67 return 1;
68}
69
James Millera683c282012-09-28 14:28:00 +000070/**
71 * This function takes a byte length and a delta unit of time to compute the
72 * approximate bytes per second
73 *
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
77 */
78static ulong bytes_per_second(unsigned int len, ulong start_ms)
79{
80 /* less accurate but avoids overflow */
81 if (len >= ((unsigned int) -1) / 1024)
82 return len / (max(get_timer(start_ms) / 1024, 1));
83 else
84 return 1024 * len / max(get_timer(start_ms), 1);
85}
86
Wolfgang Denk54841ab2010-06-28 22:00:46 +020087static int do_spi_flash_probe(int argc, char * const argv[])
Haavard Skinnemoenb6368462008-05-16 11:10:34 +020088{
Eric Nelsonc1173bd2012-01-31 10:52:07 -070089 unsigned int bus = CONFIG_SF_DEFAULT_BUS;
90 unsigned int cs = CONFIG_SF_DEFAULT_CS;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +020091 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
92 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
93 char *endp;
94 struct spi_flash *new;
95
Eric Nelsonc1173bd2012-01-31 10:52:07 -070096 if (argc >= 2) {
97 cs = simple_strtoul(argv[1], &endp, 0);
98 if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
Mike Frysinger8a07de02011-04-12 02:21:31 -040099 return -1;
Eric Nelsonc1173bd2012-01-31 10:52:07 -0700100 if (*endp == ':') {
101 if (endp[1] == 0)
102 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200103
Eric Nelsonc1173bd2012-01-31 10:52:07 -0700104 bus = cs;
105 cs = simple_strtoul(endp + 1, &endp, 0);
106 if (*endp != 0)
107 return -1;
108 }
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200109 }
110
111 if (argc >= 3) {
112 speed = simple_strtoul(argv[2], &endp, 0);
113 if (*argv[2] == 0 || *endp != 0)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400114 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200115 }
116 if (argc >= 4) {
TsiChung Liew6e8d58d2009-06-30 14:30:19 +0000117 mode = simple_strtoul(argv[3], &endp, 16);
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200118 if (*argv[3] == 0 || *endp != 0)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400119 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200120 }
121
122 new = spi_flash_probe(bus, cs, speed, mode);
123 if (!new) {
124 printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
125 return 1;
126 }
127
128 if (flash)
129 spi_flash_free(flash);
130 flash = new;
131
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200132 return 0;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200133}
134
Simon Glass8d1af942011-08-22 16:03:46 -0700135/**
136 * Write a block of data to SPI flash, first checking if it is different from
137 * what is already there.
138 *
139 * If the data being written is the same, then *skipped is incremented by len.
140 *
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
148 */
149static 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)
151{
Andreas Bießmanna97f6ef2011-12-15 09:56:53 +0100152 debug("offset=%#x, sector_size=%#x, len=%#zx\n",
Simon Glass8d1af942011-08-22 16:03:46 -0700153 offset, flash->sector_size, len);
154 if (spi_flash_read(flash, offset, len, cmp_buf))
155 return "read";
156 if (memcmp(cmp_buf, buf, len) == 0) {
Andreas Bießmanna97f6ef2011-12-15 09:56:53 +0100157 debug("Skip region %x size %zx: no change\n",
Simon Glass8d1af942011-08-22 16:03:46 -0700158 offset, len);
159 *skipped += len;
160 return NULL;
161 }
162 if (spi_flash_erase(flash, offset, len))
163 return "erase";
164 if (spi_flash_write(flash, offset, len, buf))
165 return "write";
166 return NULL;
167}
168
169/**
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.
172 *
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
178 */
179static int spi_flash_update(struct spi_flash *flash, u32 offset,
180 size_t len, const char *buf)
181{
182 const char *err_oper = NULL;
183 char *cmp_buf;
184 const char *end = buf + len;
185 size_t todo; /* number of bytes to do in this pass */
Kumar Gala8e8a4bc2011-10-03 04:35:47 +0000186 size_t skipped = 0; /* statistics */
James Millera683c282012-09-28 14:28:00 +0000187 const ulong start_time = get_timer(0);
188 size_t scale = 1;
189 const char *start_buf = buf;
190 ulong delta;
Simon Glass8d1af942011-08-22 16:03:46 -0700191
James Millera683c282012-09-28 14:28:00 +0000192 if (end - buf >= 200)
193 scale = (end - buf) / 100;
Simon Glass8d1af942011-08-22 16:03:46 -0700194 cmp_buf = malloc(flash->sector_size);
195 if (cmp_buf) {
James Millera683c282012-09-28 14:28:00 +0000196 ulong last_update = get_timer(0);
197
Kumar Gala8e8a4bc2011-10-03 04:35:47 +0000198 for (; buf < end && !err_oper; buf += todo, offset += todo) {
Simon Glass8d1af942011-08-22 16:03:46 -0700199 todo = min(end - buf, flash->sector_size);
James Millera683c282012-09-28 14:28:00 +0000200 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,
204 start_time));
205 last_update = get_timer(0);
206 }
Simon Glass8d1af942011-08-22 16:03:46 -0700207 err_oper = spi_flash_update_block(flash, offset, todo,
208 buf, cmp_buf, &skipped);
209 }
210 } else {
211 err_oper = "malloc";
212 }
213 free(cmp_buf);
James Millera683c282012-09-28 14:28:00 +0000214 putc('\r');
Simon Glass8d1af942011-08-22 16:03:46 -0700215 if (err_oper) {
216 printf("SPI flash failed in %s step\n", err_oper);
217 return 1;
218 }
James Millera683c282012-09-28 14:28:00 +0000219
220 delta = get_timer(start_time);
221 printf("%zu bytes written, %zu bytes skipped", len - skipped,
222 skipped);
223 printf(" in %ld.%lds, speed %ld B/s\n",
224 delta / 1000, delta % 1000, bytes_per_second(len, start_time));
Kumar Gala8e8a4bc2011-10-03 04:35:47 +0000225
Simon Glass8d1af942011-08-22 16:03:46 -0700226 return 0;
227}
228
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200229static int do_spi_flash_read_write(int argc, char * const argv[])
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200230{
231 unsigned long addr;
232 unsigned long offset;
233 unsigned long len;
234 void *buf;
235 char *endp;
236 int ret;
237
238 if (argc < 4)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400239 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200240
241 addr = simple_strtoul(argv[1], &endp, 16);
242 if (*argv[1] == 0 || *endp != 0)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400243 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200244 offset = simple_strtoul(argv[2], &endp, 16);
245 if (*argv[2] == 0 || *endp != 0)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400246 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200247 len = simple_strtoul(argv[3], &endp, 16);
248 if (*argv[3] == 0 || *endp != 0)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400249 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200250
Gerlando Falauto86493992012-04-03 04:34:13 +0000251 /* Consistency checking */
252 if (offset + len > flash->size) {
253 printf("ERROR: attempting %s past flash size (%#x)\n",
254 argv[0], flash->size);
255 return 1;
256 }
257
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200258 buf = map_physmem(addr, len, MAP_WRBACK);
259 if (!buf) {
260 puts("Failed to map physical memory\n");
261 return 1;
262 }
263
Simon Glass8d1af942011-08-22 16:03:46 -0700264 if (strcmp(argv[0], "update") == 0)
265 ret = spi_flash_update(flash, offset, len, buf);
266 else if (strcmp(argv[0], "read") == 0)
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200267 ret = spi_flash_read(flash, offset, len, buf);
268 else
269 ret = spi_flash_write(flash, offset, len, buf);
270
271 unmap_physmem(buf, len);
272
273 if (ret) {
274 printf("SPI flash %s failed\n", argv[0]);
275 return 1;
276 }
277
278 return 0;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200279}
280
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200281static int do_spi_flash_erase(int argc, char * const argv[])
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200282{
283 unsigned long offset;
284 unsigned long len;
285 char *endp;
286 int ret;
287
288 if (argc < 3)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400289 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200290
291 offset = simple_strtoul(argv[1], &endp, 16);
292 if (*argv[1] == 0 || *endp != 0)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400293 return -1;
Richard Retanubun334eb4b2011-02-16 16:39:44 -0500294
295 ret = sf_parse_len_arg(argv[2], &len);
296 if (ret != 1)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400297 return -1;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200298
Gerlando Falauto86493992012-04-03 04:34:13 +0000299 /* Consistency checking */
300 if (offset + len > flash->size) {
301 printf("ERROR: attempting %s past flash size (%#x)\n",
302 argv[0], flash->size);
303 return 1;
304 }
305
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200306 ret = spi_flash_erase(flash, offset, len);
307 if (ret) {
308 printf("SPI flash %s failed\n", argv[0]);
309 return 1;
310 }
311
312 return 0;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200313}
314
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200315static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200316{
317 const char *cmd;
Mike Frysinger8a07de02011-04-12 02:21:31 -0400318 int ret;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200319
320 /* need at least two arguments */
321 if (argc < 2)
322 goto usage;
323
324 cmd = argv[1];
Mike Frysinger8a07de02011-04-12 02:21:31 -0400325 --argc;
326 ++argv;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200327
Mike Frysinger8a07de02011-04-12 02:21:31 -0400328 if (strcmp(cmd, "probe") == 0) {
329 ret = do_spi_flash_probe(argc, argv);
330 goto done;
331 }
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200332
333 /* The remaining commands require a selected device */
334 if (!flash) {
335 puts("No SPI flash selected. Please run `sf probe'\n");
336 return 1;
337 }
338
Simon Glass8d1af942011-08-22 16:03:46 -0700339 if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 ||
340 strcmp(cmd, "update") == 0)
Mike Frysinger8a07de02011-04-12 02:21:31 -0400341 ret = do_spi_flash_read_write(argc, argv);
342 else if (strcmp(cmd, "erase") == 0)
343 ret = do_spi_flash_erase(argc, argv);
344 else
345 ret = -1;
346
347done:
348 if (ret != -1)
349 return ret;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200350
351usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000352 return CMD_RET_USAGE;
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200353}
354
355U_BOOT_CMD(
356 sf, 5, 1, do_spi_flash,
Peter Tyser2fb26042009-01-27 18:03:12 -0600357 "SPI flash sub-system",
Eric Nelsonc1173bd2012-01-31 10:52:07 -0700358 "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n"
Haavard Skinnemoenb6368462008-05-16 11:10:34 +0200359 " and chip select\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"
Richard Retanubun334eb4b2011-02-16 16:39:44 -0500364 "sf erase offset [+]len - erase `len' bytes from `offset'\n"
Simon Glass8d1af942011-08-22 16:03:46 -0700365 " `+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'"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200368);