Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Command for accessing SPI flash. |
| 3 | * |
| 4 | * Copyright (C) 2008 Atmel Corporation |
Mike Frysinger | 4166ee5 | 2009-10-09 17:12:44 -0400 | [diff] [blame] | 5 | * Licensed under the GPL-2 or later. |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 6 | */ |
Mike Frysinger | 4166ee5 | 2009-10-09 17:12:44 -0400 | [diff] [blame] | 7 | |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 8 | #include <common.h> |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 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 |
Eric Nelson | c1173bd | 2012-01-31 10:52:07 -0700 | [diff] [blame^] | 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 |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 26 | |
| 27 | static struct spi_flash *flash; |
| 28 | |
Richard Retanubun | 334eb4b | 2011-02-16 16:39:44 -0500 | [diff] [blame] | 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 | */ |
| 46 | static 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 Staaf | 155cfb5 | 2011-09-02 13:45:28 +0000 | [diff] [blame] | 63 | *len = ROUND(len_arg, flash->sector_size); |
Richard Retanubun | 334eb4b | 2011-02-16 16:39:44 -0500 | [diff] [blame] | 64 | else |
| 65 | *len = len_arg; |
| 66 | |
| 67 | return 1; |
| 68 | } |
| 69 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 70 | static int do_spi_flash_probe(int argc, char * const argv[]) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 71 | { |
Eric Nelson | c1173bd | 2012-01-31 10:52:07 -0700 | [diff] [blame^] | 72 | unsigned int bus = CONFIG_SF_DEFAULT_BUS; |
| 73 | unsigned int cs = CONFIG_SF_DEFAULT_CS; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 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 | |
Eric Nelson | c1173bd | 2012-01-31 10:52:07 -0700 | [diff] [blame^] | 79 | if (argc >= 2) { |
| 80 | cs = simple_strtoul(argv[1], &endp, 0); |
| 81 | if (*argv[1] == 0 || (*endp != 0 && *endp != ':')) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 82 | return -1; |
Eric Nelson | c1173bd | 2012-01-31 10:52:07 -0700 | [diff] [blame^] | 83 | if (*endp == ':') { |
| 84 | if (endp[1] == 0) |
| 85 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 86 | |
Eric Nelson | c1173bd | 2012-01-31 10:52:07 -0700 | [diff] [blame^] | 87 | bus = cs; |
| 88 | cs = simple_strtoul(endp + 1, &endp, 0); |
| 89 | if (*endp != 0) |
| 90 | return -1; |
| 91 | } |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if (argc >= 3) { |
| 95 | speed = simple_strtoul(argv[2], &endp, 0); |
| 96 | if (*argv[2] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 97 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 98 | } |
| 99 | if (argc >= 4) { |
TsiChung Liew | 6e8d58d | 2009-06-30 14:30:19 +0000 | [diff] [blame] | 100 | mode = simple_strtoul(argv[3], &endp, 16); |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 101 | if (*argv[3] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 102 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 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 | |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 115 | return 0; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 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 | */ |
| 132 | static 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 | { |
Andreas Bießmann | a97f6ef | 2011-12-15 09:56:53 +0100 | [diff] [blame] | 135 | debug("offset=%#x, sector_size=%#x, len=%#zx\n", |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 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) { |
Andreas Bießmann | a97f6ef | 2011-12-15 09:56:53 +0100 | [diff] [blame] | 140 | debug("Skip region %x size %zx: no change\n", |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 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 | */ |
| 162 | static 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 */ |
Kumar Gala | 8e8a4bc | 2011-10-03 04:35:47 +0000 | [diff] [blame] | 169 | size_t skipped = 0; /* statistics */ |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 170 | |
| 171 | cmp_buf = malloc(flash->sector_size); |
| 172 | if (cmp_buf) { |
Kumar Gala | 8e8a4bc | 2011-10-03 04:35:47 +0000 | [diff] [blame] | 173 | for (; buf < end && !err_oper; buf += todo, offset += todo) { |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 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); |
Kumar Gala | 8e8a4bc | 2011-10-03 04:35:47 +0000 | [diff] [blame] | 188 | |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 192 | static int do_spi_flash_read_write(int argc, char * const argv[]) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 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) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 202 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 203 | |
| 204 | addr = simple_strtoul(argv[1], &endp, 16); |
| 205 | if (*argv[1] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 206 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 207 | offset = simple_strtoul(argv[2], &endp, 16); |
| 208 | if (*argv[2] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 209 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 210 | len = simple_strtoul(argv[3], &endp, 16); |
| 211 | if (*argv[3] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 212 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 213 | |
| 214 | buf = map_physmem(addr, len, MAP_WRBACK); |
| 215 | if (!buf) { |
| 216 | puts("Failed to map physical memory\n"); |
| 217 | return 1; |
| 218 | } |
| 219 | |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 220 | if (strcmp(argv[0], "update") == 0) |
| 221 | ret = spi_flash_update(flash, offset, len, buf); |
| 222 | else if (strcmp(argv[0], "read") == 0) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 223 | ret = spi_flash_read(flash, offset, len, buf); |
| 224 | else |
| 225 | ret = spi_flash_write(flash, offset, len, buf); |
| 226 | |
| 227 | unmap_physmem(buf, len); |
| 228 | |
| 229 | if (ret) { |
| 230 | printf("SPI flash %s failed\n", argv[0]); |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | return 0; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 237 | static int do_spi_flash_erase(int argc, char * const argv[]) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 238 | { |
| 239 | unsigned long offset; |
| 240 | unsigned long len; |
| 241 | char *endp; |
| 242 | int ret; |
| 243 | |
| 244 | if (argc < 3) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 245 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 246 | |
| 247 | offset = simple_strtoul(argv[1], &endp, 16); |
| 248 | if (*argv[1] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 249 | return -1; |
Richard Retanubun | 334eb4b | 2011-02-16 16:39:44 -0500 | [diff] [blame] | 250 | |
| 251 | ret = sf_parse_len_arg(argv[2], &len); |
| 252 | if (ret != 1) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 253 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 254 | |
| 255 | ret = spi_flash_erase(flash, offset, len); |
| 256 | if (ret) { |
| 257 | printf("SPI flash %s failed\n", argv[0]); |
| 258 | return 1; |
| 259 | } |
| 260 | |
| 261 | return 0; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 264 | static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 265 | { |
| 266 | const char *cmd; |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 267 | int ret; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 268 | |
| 269 | /* need at least two arguments */ |
| 270 | if (argc < 2) |
| 271 | goto usage; |
| 272 | |
| 273 | cmd = argv[1]; |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 274 | --argc; |
| 275 | ++argv; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 276 | |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 277 | if (strcmp(cmd, "probe") == 0) { |
| 278 | ret = do_spi_flash_probe(argc, argv); |
| 279 | goto done; |
| 280 | } |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 281 | |
| 282 | /* The remaining commands require a selected device */ |
| 283 | if (!flash) { |
| 284 | puts("No SPI flash selected. Please run `sf probe'\n"); |
| 285 | return 1; |
| 286 | } |
| 287 | |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 288 | if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 || |
| 289 | strcmp(cmd, "update") == 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 290 | ret = do_spi_flash_read_write(argc, argv); |
| 291 | else if (strcmp(cmd, "erase") == 0) |
| 292 | ret = do_spi_flash_erase(argc, argv); |
| 293 | else |
| 294 | ret = -1; |
| 295 | |
| 296 | done: |
| 297 | if (ret != -1) |
| 298 | return ret; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 299 | |
| 300 | usage: |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 301 | return cmd_usage(cmdtp); |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | U_BOOT_CMD( |
| 305 | sf, 5, 1, do_spi_flash, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 306 | "SPI flash sub-system", |
Eric Nelson | c1173bd | 2012-01-31 10:52:07 -0700 | [diff] [blame^] | 307 | "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n" |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 308 | " and chip select\n" |
| 309 | "sf read addr offset len - read `len' bytes starting at\n" |
| 310 | " `offset' to memory at `addr'\n" |
| 311 | "sf write addr offset len - write `len' bytes from memory\n" |
| 312 | " at `addr' to flash at `offset'\n" |
Richard Retanubun | 334eb4b | 2011-02-16 16:39:44 -0500 | [diff] [blame] | 313 | "sf erase offset [+]len - erase `len' bytes from `offset'\n" |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 314 | " `+len' round up `len' to block size\n" |
| 315 | "sf update addr offset len - erase and write `len' bytes from memory\n" |
| 316 | " at `addr' to flash at `offset'" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 317 | ); |