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 static int do_spi_flash_read_write(int argc, char * const argv[])
115 unsigned long offset;
124 addr = simple_strtoul(argv[1], &endp, 16);
125 if (*argv[1] == 0 || *endp != 0)
127 offset = simple_strtoul(argv[2], &endp, 16);
128 if (*argv[2] == 0 || *endp != 0)
130 len = simple_strtoul(argv[3], &endp, 16);
131 if (*argv[3] == 0 || *endp != 0)
134 buf = map_physmem(addr, len, MAP_WRBACK);
136 puts("Failed to map physical memory\n");
140 if (strcmp(argv[0], "read") == 0)
141 ret = spi_flash_read(flash, offset, len, buf);
143 ret = spi_flash_write(flash, offset, len, buf);
145 unmap_physmem(buf, len);
148 printf("SPI flash %s failed\n", argv[0]);
155 static int do_spi_flash_erase(int argc, char * const argv[])
157 unsigned long offset;
165 offset = simple_strtoul(argv[1], &endp, 16);
166 if (*argv[1] == 0 || *endp != 0)
169 ret = sf_parse_len_arg(argv[2], &len);
173 ret = spi_flash_erase(flash, offset, len);
175 printf("SPI flash %s failed\n", argv[0]);
182 static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
187 /* need at least two arguments */
195 if (strcmp(cmd, "probe") == 0) {
196 ret = do_spi_flash_probe(argc, argv);
200 /* The remaining commands require a selected device */
202 puts("No SPI flash selected. Please run `sf probe'\n");
206 if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0)
207 ret = do_spi_flash_read_write(argc, argv);
208 else if (strcmp(cmd, "erase") == 0)
209 ret = do_spi_flash_erase(argc, argv);
218 return cmd_usage(cmdtp);
222 sf, 5, 1, do_spi_flash,
223 "SPI flash sub-system",
224 "probe [bus:]cs [hz] [mode] - init flash device on given SPI bus\n"
226 "sf read addr offset len - read `len' bytes starting at\n"
227 " `offset' to memory at `addr'\n"
228 "sf write addr offset len - write `len' bytes from memory\n"
229 " at `addr' to flash at `offset'\n"
230 "sf erase offset [+]len - erase `len' bytes from `offset'\n"
231 " `+len' round up `len' to block size"