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);
109 printf("%u KiB %s at %u:%u is now current device\n",
110 flash->size >> 10, flash->name, bus, cs);
115 puts("Usage: sf probe [bus:]cs [hz] [mode]\n");
119 static int do_spi_flash_read_write(int argc, char * const argv[])
122 unsigned long offset;
131 addr = simple_strtoul(argv[1], &endp, 16);
132 if (*argv[1] == 0 || *endp != 0)
134 offset = simple_strtoul(argv[2], &endp, 16);
135 if (*argv[2] == 0 || *endp != 0)
137 len = simple_strtoul(argv[3], &endp, 16);
138 if (*argv[3] == 0 || *endp != 0)
141 buf = map_physmem(addr, len, MAP_WRBACK);
143 puts("Failed to map physical memory\n");
147 if (strcmp(argv[0], "read") == 0)
148 ret = spi_flash_read(flash, offset, len, buf);
150 ret = spi_flash_write(flash, offset, len, buf);
152 unmap_physmem(buf, len);
155 printf("SPI flash %s failed\n", argv[0]);
162 printf("Usage: sf %s addr offset len\n", argv[0]);
166 static int do_spi_flash_erase(int argc, char * const argv[])
168 unsigned long offset;
176 offset = simple_strtoul(argv[1], &endp, 16);
177 if (*argv[1] == 0 || *endp != 0)
180 ret = sf_parse_len_arg(argv[2], &len);
184 ret = spi_flash_erase(flash, offset, len);
186 printf("SPI flash %s failed\n", argv[0]);
193 puts("Usage: sf erase offset [+]len\n");
197 static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
201 /* need at least two arguments */
207 if (strcmp(cmd, "probe") == 0)
208 return do_spi_flash_probe(argc - 1, argv + 1);
210 /* The remaining commands require a selected device */
212 puts("No SPI flash selected. Please run `sf probe'\n");
216 if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0)
217 return do_spi_flash_read_write(argc - 1, argv + 1);
218 if (strcmp(cmd, "erase") == 0)
219 return do_spi_flash_erase(argc - 1, argv + 1);
222 return cmd_usage(cmdtp);
226 sf, 5, 1, do_spi_flash,
227 "SPI flash sub-system",
228 "probe [bus:]cs [hz] [mode] - init flash device on given SPI bus\n"
230 "sf read addr offset len - read `len' bytes starting at\n"
231 " `offset' to memory at `addr'\n"
232 "sf write addr offset len - write `len' bytes from memory\n"
233 " at `addr' to flash at `offset'\n"
234 "sf erase offset [+]len - erase `len' bytes from `offset'\n"
235 " `+len' round up `len' to block size"