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 |
| 20 | |
| 21 | static struct spi_flash *flash; |
| 22 | |
Richard Retanubun | 334eb4b | 2011-02-16 16:39:44 -0500 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * This function computes the length argument for the erase command. |
| 26 | * The length on which the command is to operate can be given in two forms: |
| 27 | * 1. <cmd> offset len - operate on <'offset', 'len') |
| 28 | * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)') |
| 29 | * If the second form is used and the length doesn't fall on the |
| 30 | * sector boundary, than it will be adjusted to the next sector boundary. |
| 31 | * If it isn't in the flash, the function will fail (return -1). |
| 32 | * Input: |
| 33 | * arg: length specification (i.e. both command arguments) |
| 34 | * Output: |
| 35 | * len: computed length for operation |
| 36 | * Return: |
| 37 | * 1: success |
| 38 | * -1: failure (bad format, bad address). |
| 39 | */ |
| 40 | static int sf_parse_len_arg(char *arg, ulong *len) |
| 41 | { |
| 42 | char *ep; |
| 43 | char round_up_len; /* indicates if the "+length" form used */ |
| 44 | ulong len_arg; |
| 45 | |
| 46 | round_up_len = 0; |
| 47 | if (*arg == '+') { |
| 48 | round_up_len = 1; |
| 49 | ++arg; |
| 50 | } |
| 51 | |
| 52 | len_arg = simple_strtoul(arg, &ep, 16); |
| 53 | if (ep == arg || *ep != '\0') |
| 54 | return -1; |
| 55 | |
| 56 | if (round_up_len && flash->sector_size > 0) |
Anton Staaf | 155cfb5 | 2011-09-02 13:45:28 +0000 | [diff] [blame] | 57 | *len = ROUND(len_arg, flash->sector_size); |
Richard Retanubun | 334eb4b | 2011-02-16 16:39:44 -0500 | [diff] [blame] | 58 | else |
| 59 | *len = len_arg; |
| 60 | |
| 61 | return 1; |
| 62 | } |
| 63 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 64 | static int do_spi_flash_probe(int argc, char * const argv[]) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 65 | { |
| 66 | unsigned int bus = 0; |
| 67 | unsigned int cs; |
| 68 | unsigned int speed = CONFIG_SF_DEFAULT_SPEED; |
| 69 | unsigned int mode = CONFIG_SF_DEFAULT_MODE; |
| 70 | char *endp; |
| 71 | struct spi_flash *new; |
| 72 | |
| 73 | if (argc < 2) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 74 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 75 | |
| 76 | cs = simple_strtoul(argv[1], &endp, 0); |
| 77 | if (*argv[1] == 0 || (*endp != 0 && *endp != ':')) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 78 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 79 | if (*endp == ':') { |
| 80 | if (endp[1] == 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 81 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 82 | |
| 83 | bus = cs; |
| 84 | cs = simple_strtoul(endp + 1, &endp, 0); |
| 85 | if (*endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 86 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (argc >= 3) { |
| 90 | speed = simple_strtoul(argv[2], &endp, 0); |
| 91 | if (*argv[2] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 92 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 93 | } |
| 94 | if (argc >= 4) { |
TsiChung Liew | 6e8d58d | 2009-06-30 14:30:19 +0000 | [diff] [blame] | 95 | mode = simple_strtoul(argv[3], &endp, 16); |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 96 | if (*argv[3] == 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 | |
| 100 | new = spi_flash_probe(bus, cs, speed, mode); |
| 101 | if (!new) { |
| 102 | printf("Failed to initialize SPI flash at %u:%u\n", bus, cs); |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | if (flash) |
| 107 | spi_flash_free(flash); |
| 108 | flash = new; |
| 109 | |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 110 | return 0; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Write a block of data to SPI flash, first checking if it is different from |
| 115 | * what is already there. |
| 116 | * |
| 117 | * If the data being written is the same, then *skipped is incremented by len. |
| 118 | * |
| 119 | * @param flash flash context pointer |
| 120 | * @param offset flash offset to write |
| 121 | * @param len number of bytes to write |
| 122 | * @param buf buffer to write from |
| 123 | * @param cmp_buf read buffer to use to compare data |
| 124 | * @param skipped Count of skipped data (incremented by this function) |
| 125 | * @return NULL if OK, else a string containing the stage which failed |
| 126 | */ |
| 127 | static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset, |
| 128 | size_t len, const char *buf, char *cmp_buf, size_t *skipped) |
| 129 | { |
Andreas Bießmann | a97f6ef | 2011-12-15 09:56:53 +0100 | [diff] [blame] | 130 | debug("offset=%#x, sector_size=%#x, len=%#zx\n", |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 131 | offset, flash->sector_size, len); |
| 132 | if (spi_flash_read(flash, offset, len, cmp_buf)) |
| 133 | return "read"; |
| 134 | if (memcmp(cmp_buf, buf, len) == 0) { |
Andreas Bießmann | a97f6ef | 2011-12-15 09:56:53 +0100 | [diff] [blame] | 135 | debug("Skip region %x size %zx: no change\n", |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 136 | offset, len); |
| 137 | *skipped += len; |
| 138 | return NULL; |
| 139 | } |
| 140 | if (spi_flash_erase(flash, offset, len)) |
| 141 | return "erase"; |
| 142 | if (spi_flash_write(flash, offset, len, buf)) |
| 143 | return "write"; |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Update an area of SPI flash by erasing and writing any blocks which need |
| 149 | * to change. Existing blocks with the correct data are left unchanged. |
| 150 | * |
| 151 | * @param flash flash context pointer |
| 152 | * @param offset flash offset to write |
| 153 | * @param len number of bytes to write |
| 154 | * @param buf buffer to write from |
| 155 | * @return 0 if ok, 1 on error |
| 156 | */ |
| 157 | static int spi_flash_update(struct spi_flash *flash, u32 offset, |
| 158 | size_t len, const char *buf) |
| 159 | { |
| 160 | const char *err_oper = NULL; |
| 161 | char *cmp_buf; |
| 162 | const char *end = buf + len; |
| 163 | size_t todo; /* number of bytes to do in this pass */ |
Kumar Gala | 8e8a4bc | 2011-10-03 04:35:47 +0000 | [diff] [blame] | 164 | size_t skipped = 0; /* statistics */ |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 165 | |
| 166 | cmp_buf = malloc(flash->sector_size); |
| 167 | if (cmp_buf) { |
Kumar Gala | 8e8a4bc | 2011-10-03 04:35:47 +0000 | [diff] [blame] | 168 | for (; buf < end && !err_oper; buf += todo, offset += todo) { |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 169 | todo = min(end - buf, flash->sector_size); |
| 170 | err_oper = spi_flash_update_block(flash, offset, todo, |
| 171 | buf, cmp_buf, &skipped); |
| 172 | } |
| 173 | } else { |
| 174 | err_oper = "malloc"; |
| 175 | } |
| 176 | free(cmp_buf); |
| 177 | if (err_oper) { |
| 178 | printf("SPI flash failed in %s step\n", err_oper); |
| 179 | return 1; |
| 180 | } |
| 181 | printf("%zu bytes written, %zu bytes skipped\n", len - skipped, |
| 182 | skipped); |
Kumar Gala | 8e8a4bc | 2011-10-03 04:35:47 +0000 | [diff] [blame] | 183 | |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 187 | static int do_spi_flash_read_write(int argc, char * const argv[]) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 188 | { |
| 189 | unsigned long addr; |
| 190 | unsigned long offset; |
| 191 | unsigned long len; |
| 192 | void *buf; |
| 193 | char *endp; |
| 194 | int ret; |
| 195 | |
| 196 | if (argc < 4) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 197 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 198 | |
| 199 | addr = simple_strtoul(argv[1], &endp, 16); |
| 200 | if (*argv[1] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 201 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 202 | offset = simple_strtoul(argv[2], &endp, 16); |
| 203 | if (*argv[2] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 204 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 205 | len = simple_strtoul(argv[3], &endp, 16); |
| 206 | if (*argv[3] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 207 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 208 | |
| 209 | buf = map_physmem(addr, len, MAP_WRBACK); |
| 210 | if (!buf) { |
| 211 | puts("Failed to map physical memory\n"); |
| 212 | return 1; |
| 213 | } |
| 214 | |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 215 | if (strcmp(argv[0], "update") == 0) |
| 216 | ret = spi_flash_update(flash, offset, len, buf); |
| 217 | else if (strcmp(argv[0], "read") == 0) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 218 | ret = spi_flash_read(flash, offset, len, buf); |
| 219 | else |
| 220 | ret = spi_flash_write(flash, offset, len, buf); |
| 221 | |
| 222 | unmap_physmem(buf, len); |
| 223 | |
| 224 | if (ret) { |
| 225 | printf("SPI flash %s failed\n", argv[0]); |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | return 0; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 232 | static int do_spi_flash_erase(int argc, char * const argv[]) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 233 | { |
| 234 | unsigned long offset; |
| 235 | unsigned long len; |
| 236 | char *endp; |
| 237 | int ret; |
| 238 | |
| 239 | if (argc < 3) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 240 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 241 | |
| 242 | offset = simple_strtoul(argv[1], &endp, 16); |
| 243 | if (*argv[1] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 244 | return -1; |
Richard Retanubun | 334eb4b | 2011-02-16 16:39:44 -0500 | [diff] [blame] | 245 | |
| 246 | ret = sf_parse_len_arg(argv[2], &len); |
| 247 | if (ret != 1) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 248 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 249 | |
| 250 | ret = spi_flash_erase(flash, offset, len); |
| 251 | if (ret) { |
| 252 | printf("SPI flash %s failed\n", argv[0]); |
| 253 | return 1; |
| 254 | } |
| 255 | |
| 256 | return 0; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 259 | 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] | 260 | { |
| 261 | const char *cmd; |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 262 | int ret; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 263 | |
| 264 | /* need at least two arguments */ |
| 265 | if (argc < 2) |
| 266 | goto usage; |
| 267 | |
| 268 | cmd = argv[1]; |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 269 | --argc; |
| 270 | ++argv; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 271 | |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 272 | if (strcmp(cmd, "probe") == 0) { |
| 273 | ret = do_spi_flash_probe(argc, argv); |
| 274 | goto done; |
| 275 | } |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 276 | |
| 277 | /* The remaining commands require a selected device */ |
| 278 | if (!flash) { |
| 279 | puts("No SPI flash selected. Please run `sf probe'\n"); |
| 280 | return 1; |
| 281 | } |
| 282 | |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 283 | if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 || |
| 284 | strcmp(cmd, "update") == 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 285 | ret = do_spi_flash_read_write(argc, argv); |
| 286 | else if (strcmp(cmd, "erase") == 0) |
| 287 | ret = do_spi_flash_erase(argc, argv); |
| 288 | else |
| 289 | ret = -1; |
| 290 | |
| 291 | done: |
| 292 | if (ret != -1) |
| 293 | return ret; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 294 | |
| 295 | usage: |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 296 | return cmd_usage(cmdtp); |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | U_BOOT_CMD( |
| 300 | sf, 5, 1, do_spi_flash, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 301 | "SPI flash sub-system", |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 302 | "probe [bus:]cs [hz] [mode] - init flash device on given SPI bus\n" |
| 303 | " and chip select\n" |
| 304 | "sf read addr offset len - read `len' bytes starting at\n" |
| 305 | " `offset' to memory at `addr'\n" |
| 306 | "sf write addr offset len - write `len' bytes from memory\n" |
| 307 | " at `addr' to flash at `offset'\n" |
Richard Retanubun | 334eb4b | 2011-02-16 16:39:44 -0500 | [diff] [blame] | 308 | "sf erase offset [+]len - erase `len' bytes from `offset'\n" |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 309 | " `+len' round up `len' to block size\n" |
| 310 | "sf update addr offset len - erase and write `len' bytes from memory\n" |
| 311 | " at `addr' to flash at `offset'" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 312 | ); |