2 * Command for accessing SPI flash.
4 * Copyright (C) 2008 Atmel Corporation
5 * Licensed under the GPL-2 or later.
13 #ifndef CONFIG_SF_DEFAULT_SPEED
14 # define CONFIG_SF_DEFAULT_SPEED 1000000
16 #ifndef CONFIG_SF_DEFAULT_MODE
17 # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
20 static struct spi_flash *flash;
24 * This function computes the length argument for the erase command.
25 * The length on which the command is to operate can be given in two forms:
26 * 1. <cmd> offset len - operate on <'offset', 'len')
27 * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)')
28 * If the second form is used and the length doesn't fall on the
29 * sector boundary, than it will be adjusted to the next sector boundary.
30 * If it isn't in the flash, the function will fail (return -1).
32 * arg: length specification (i.e. both command arguments)
34 * len: computed length for operation
37 * -1: failure (bad format, bad address).
39 static int sf_parse_len_arg(char *arg, ulong *len)
42 char round_up_len; /* indicates if the "+length" form used */
51 len_arg = simple_strtoul(arg, &ep, 16);
52 if (ep == arg || *ep != '\0')
55 if (round_up_len && flash->sector_size > 0)
56 *len = ROUND(len_arg - 1, flash->sector_size);
63 static int do_spi_flash_probe(int argc, char * const argv[])
67 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
68 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
70 struct spi_flash *new;
75 cs = simple_strtoul(argv[1], &endp, 0);
76 if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
83 cs = simple_strtoul(endp + 1, &endp, 0);
89 speed = simple_strtoul(argv[2], &endp, 0);
90 if (*argv[2] == 0 || *endp != 0)
94 mode = simple_strtoul(argv[3], &endp, 16);
95 if (*argv[3] == 0 || *endp != 0)
99 new = spi_flash_probe(bus, cs, speed, mode);
101 printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
106 spi_flash_free(flash);
112 puts("Usage: sf probe [bus:]cs [hz] [mode]\n");
116 static int do_spi_flash_read_write(int argc, char * const argv[])
119 unsigned long offset;
128 addr = simple_strtoul(argv[1], &endp, 16);
129 if (*argv[1] == 0 || *endp != 0)
131 offset = simple_strtoul(argv[2], &endp, 16);
132 if (*argv[2] == 0 || *endp != 0)
134 len = simple_strtoul(argv[3], &endp, 16);
135 if (*argv[3] == 0 || *endp != 0)
138 buf = map_physmem(addr, len, MAP_WRBACK);
140 puts("Failed to map physical memory\n");
144 if (strcmp(argv[0], "read") == 0)
145 ret = spi_flash_read(flash, offset, len, buf);
147 ret = spi_flash_write(flash, offset, len, buf);
149 unmap_physmem(buf, len);
152 printf("SPI flash %s failed\n", argv[0]);
159 printf("Usage: sf %s addr offset len\n", argv[0]);
163 static int do_spi_flash_erase(int argc, char * const argv[])
165 unsigned long offset;
173 offset = simple_strtoul(argv[1], &endp, 16);
174 if (*argv[1] == 0 || *endp != 0)
177 ret = sf_parse_len_arg(argv[2], &len);
181 ret = spi_flash_erase(flash, offset, len);
183 printf("SPI flash %s failed\n", argv[0]);
190 puts("Usage: sf erase offset [+]len\n");
194 static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
198 /* need at least two arguments */
204 if (strcmp(cmd, "probe") == 0)
205 return do_spi_flash_probe(argc - 1, argv + 1);
207 /* The remaining commands require a selected device */
209 puts("No SPI flash selected. Please run `sf probe'\n");
213 if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0)
214 return do_spi_flash_read_write(argc - 1, argv + 1);
215 if (strcmp(cmd, "erase") == 0)
216 return do_spi_flash_erase(argc - 1, argv + 1);
219 return cmd_usage(cmdtp);
223 sf, 5, 1, do_spi_flash,
224 "SPI flash sub-system",
225 "probe [bus:]cs [hz] [mode] - init flash device on given SPI bus\n"
227 "sf read addr offset len - read `len' bytes starting at\n"
228 " `offset' to memory at `addr'\n"
229 "sf write addr offset len - write `len' bytes from memory\n"
230 " at `addr' to flash at `offset'\n"
231 "sf erase offset [+]len - erase `len' bytes from `offset'\n"
232 " `+len' round up `len' to block size"