]> Git Repo - J-u-boot.git/blame - common/cmd_sf.c
Merge branch 'master' of git://git.denx.de/u-boot-net
[J-u-boot.git] / common / cmd_sf.c
CommitLineData
b6368467
HS
1/*
2 * Command for accessing SPI flash.
3 *
4 * Copyright (C) 2008 Atmel Corporation
4166ee58 5 * Licensed under the GPL-2 or later.
b6368467 6 */
4166ee58 7
b6368467 8#include <common.h>
8d1af942 9#include <malloc.h>
b6368467
HS
10#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
c1173bd0
EN
20#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
b6368467
HS
26
27static struct spi_flash *flash;
28
334eb4b0
RR
29
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)
155cfb5e 63 *len = ROUND(len_arg, flash->sector_size);
334eb4b0
RR
64 else
65 *len = len_arg;
66
67 return 1;
68}
69
54841ab5 70static int do_spi_flash_probe(int argc, char * const argv[])
b6368467 71{
c1173bd0
EN
72 unsigned int bus = CONFIG_SF_DEFAULT_BUS;
73 unsigned int cs = CONFIG_SF_DEFAULT_CS;
b6368467
HS
74 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
75 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
76 char *endp;
77 struct spi_flash *new;
78
c1173bd0
EN
79 if (argc >= 2) {
80 cs = simple_strtoul(argv[1], &endp, 0);
81 if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
8a07de03 82 return -1;
c1173bd0
EN
83 if (*endp == ':') {
84 if (endp[1] == 0)
85 return -1;
86
87 bus = cs;
88 cs = simple_strtoul(endp + 1, &endp, 0);
89 if (*endp != 0)
90 return -1;
91 }
b6368467
HS
92 }
93
94 if (argc >= 3) {
95 speed = simple_strtoul(argv[2], &endp, 0);
96 if (*argv[2] == 0 || *endp != 0)
8a07de03 97 return -1;
b6368467
HS
98 }
99 if (argc >= 4) {
6e8d58d3 100 mode = simple_strtoul(argv[3], &endp, 16);
b6368467 101 if (*argv[3] == 0 || *endp != 0)
8a07de03 102 return -1;
b6368467
HS
103 }
104
105 new = spi_flash_probe(bus, cs, speed, mode);
106 if (!new) {
107 printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
108 return 1;
109 }
110
111 if (flash)
112 spi_flash_free(flash);
113 flash = new;
114
b6368467 115 return 0;
b6368467
HS
116}
117
8d1af942
SG
118/**
119 * Write a block of data to SPI flash, first checking if it is different from
120 * what is already there.
121 *
122 * If the data being written is the same, then *skipped is incremented by len.
123 *
124 * @param flash flash context pointer
125 * @param offset flash offset to write
126 * @param len number of bytes to write
127 * @param buf buffer to write from
128 * @param cmp_buf read buffer to use to compare data
129 * @param skipped Count of skipped data (incremented by this function)
130 * @return NULL if OK, else a string containing the stage which failed
131 */
132static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
133 size_t len, const char *buf, char *cmp_buf, size_t *skipped)
134{
a97f6efd 135 debug("offset=%#x, sector_size=%#x, len=%#zx\n",
8d1af942
SG
136 offset, flash->sector_size, len);
137 if (spi_flash_read(flash, offset, len, cmp_buf))
138 return "read";
139 if (memcmp(cmp_buf, buf, len) == 0) {
a97f6efd 140 debug("Skip region %x size %zx: no change\n",
8d1af942
SG
141 offset, len);
142 *skipped += len;
143 return NULL;
144 }
145 if (spi_flash_erase(flash, offset, len))
146 return "erase";
147 if (spi_flash_write(flash, offset, len, buf))
148 return "write";
149 return NULL;
150}
151
152/**
153 * Update an area of SPI flash by erasing and writing any blocks which need
154 * to change. Existing blocks with the correct data are left unchanged.
155 *
156 * @param flash flash context pointer
157 * @param offset flash offset to write
158 * @param len number of bytes to write
159 * @param buf buffer to write from
160 * @return 0 if ok, 1 on error
161 */
162static int spi_flash_update(struct spi_flash *flash, u32 offset,
163 size_t len, const char *buf)
164{
165 const char *err_oper = NULL;
166 char *cmp_buf;
167 const char *end = buf + len;
168 size_t todo; /* number of bytes to do in this pass */
8e8a4bc2 169 size_t skipped = 0; /* statistics */
8d1af942
SG
170
171 cmp_buf = malloc(flash->sector_size);
172 if (cmp_buf) {
8e8a4bc2 173 for (; buf < end && !err_oper; buf += todo, offset += todo) {
8d1af942
SG
174 todo = min(end - buf, flash->sector_size);
175 err_oper = spi_flash_update_block(flash, offset, todo,
176 buf, cmp_buf, &skipped);
177 }
178 } else {
179 err_oper = "malloc";
180 }
181 free(cmp_buf);
182 if (err_oper) {
183 printf("SPI flash failed in %s step\n", err_oper);
184 return 1;
185 }
186 printf("%zu bytes written, %zu bytes skipped\n", len - skipped,
187 skipped);
8e8a4bc2 188
8d1af942
SG
189 return 0;
190}
191
54841ab5 192static int do_spi_flash_read_write(int argc, char * const argv[])
b6368467
HS
193{
194 unsigned long addr;
195 unsigned long offset;
196 unsigned long len;
197 void *buf;
198 char *endp;
199 int ret;
200
201 if (argc < 4)
8a07de03 202 return -1;
b6368467
HS
203
204 addr = simple_strtoul(argv[1], &endp, 16);
205 if (*argv[1] == 0 || *endp != 0)
8a07de03 206 return -1;
b6368467
HS
207 offset = simple_strtoul(argv[2], &endp, 16);
208 if (*argv[2] == 0 || *endp != 0)
8a07de03 209 return -1;
b6368467
HS
210 len = simple_strtoul(argv[3], &endp, 16);
211 if (*argv[3] == 0 || *endp != 0)
8a07de03 212 return -1;
b6368467 213
86493994
GF
214 /* Consistency checking */
215 if (offset + len > flash->size) {
216 printf("ERROR: attempting %s past flash size (%#x)\n",
217 argv[0], flash->size);
218 return 1;
219 }
220
b6368467
HS
221 buf = map_physmem(addr, len, MAP_WRBACK);
222 if (!buf) {
223 puts("Failed to map physical memory\n");
224 return 1;
225 }
226
8d1af942
SG
227 if (strcmp(argv[0], "update") == 0)
228 ret = spi_flash_update(flash, offset, len, buf);
229 else if (strcmp(argv[0], "read") == 0)
b6368467
HS
230 ret = spi_flash_read(flash, offset, len, buf);
231 else
232 ret = spi_flash_write(flash, offset, len, buf);
233
234 unmap_physmem(buf, len);
235
236 if (ret) {
237 printf("SPI flash %s failed\n", argv[0]);
238 return 1;
239 }
240
241 return 0;
b6368467
HS
242}
243
54841ab5 244static int do_spi_flash_erase(int argc, char * const argv[])
b6368467
HS
245{
246 unsigned long offset;
247 unsigned long len;
248 char *endp;
249 int ret;
250
251 if (argc < 3)
8a07de03 252 return -1;
b6368467
HS
253
254 offset = simple_strtoul(argv[1], &endp, 16);
255 if (*argv[1] == 0 || *endp != 0)
8a07de03 256 return -1;
334eb4b0
RR
257
258 ret = sf_parse_len_arg(argv[2], &len);
259 if (ret != 1)
8a07de03 260 return -1;
b6368467 261
86493994
GF
262 /* Consistency checking */
263 if (offset + len > flash->size) {
264 printf("ERROR: attempting %s past flash size (%#x)\n",
265 argv[0], flash->size);
266 return 1;
267 }
268
b6368467
HS
269 ret = spi_flash_erase(flash, offset, len);
270 if (ret) {
271 printf("SPI flash %s failed\n", argv[0]);
272 return 1;
273 }
274
275 return 0;
b6368467
HS
276}
277
54841ab5 278static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
b6368467
HS
279{
280 const char *cmd;
8a07de03 281 int ret;
b6368467
HS
282
283 /* need at least two arguments */
284 if (argc < 2)
285 goto usage;
286
287 cmd = argv[1];
8a07de03
MF
288 --argc;
289 ++argv;
b6368467 290
8a07de03
MF
291 if (strcmp(cmd, "probe") == 0) {
292 ret = do_spi_flash_probe(argc, argv);
293 goto done;
294 }
b6368467
HS
295
296 /* The remaining commands require a selected device */
297 if (!flash) {
298 puts("No SPI flash selected. Please run `sf probe'\n");
299 return 1;
300 }
301
8d1af942
SG
302 if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 ||
303 strcmp(cmd, "update") == 0)
8a07de03
MF
304 ret = do_spi_flash_read_write(argc, argv);
305 else if (strcmp(cmd, "erase") == 0)
306 ret = do_spi_flash_erase(argc, argv);
307 else
308 ret = -1;
309
310done:
311 if (ret != -1)
312 return ret;
b6368467
HS
313
314usage:
4c12eeb8 315 return CMD_RET_USAGE;
b6368467
HS
316}
317
318U_BOOT_CMD(
319 sf, 5, 1, do_spi_flash,
2fb2604d 320 "SPI flash sub-system",
c1173bd0 321 "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n"
b6368467
HS
322 " and chip select\n"
323 "sf read addr offset len - read `len' bytes starting at\n"
324 " `offset' to memory at `addr'\n"
325 "sf write addr offset len - write `len' bytes from memory\n"
326 " at `addr' to flash at `offset'\n"
334eb4b0 327 "sf erase offset [+]len - erase `len' bytes from `offset'\n"
8d1af942
SG
328 " `+len' round up `len' to block size\n"
329 "sf update addr offset len - erase and write `len' bytes from memory\n"
330 " at `addr' to flash at `offset'"
a89c33db 331);
This page took 0.225612 seconds and 4 git commands to generate.