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> |
Mingkai Hu | 381c6e2 | 2013-04-07 22:13:32 +0000 | [diff] [blame] | 9 | #include <div64.h> |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 11 | #include <spi_flash.h> |
| 12 | |
| 13 | #include <asm/io.h> |
| 14 | |
| 15 | #ifndef CONFIG_SF_DEFAULT_SPEED |
| 16 | # define CONFIG_SF_DEFAULT_SPEED 1000000 |
| 17 | #endif |
| 18 | #ifndef CONFIG_SF_DEFAULT_MODE |
| 19 | # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3 |
| 20 | #endif |
Eric Nelson | c1173bd | 2012-01-31 10:52:07 -0700 | [diff] [blame] | 21 | #ifndef CONFIG_SF_DEFAULT_CS |
| 22 | # define CONFIG_SF_DEFAULT_CS 0 |
| 23 | #endif |
| 24 | #ifndef CONFIG_SF_DEFAULT_BUS |
| 25 | # define CONFIG_SF_DEFAULT_BUS 0 |
| 26 | #endif |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 27 | |
| 28 | static struct spi_flash *flash; |
| 29 | |
Richard Retanubun | 334eb4b | 2011-02-16 16:39:44 -0500 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * This function computes the length argument for the erase command. |
| 33 | * The length on which the command is to operate can be given in two forms: |
| 34 | * 1. <cmd> offset len - operate on <'offset', 'len') |
| 35 | * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)') |
| 36 | * If the second form is used and the length doesn't fall on the |
| 37 | * sector boundary, than it will be adjusted to the next sector boundary. |
| 38 | * If it isn't in the flash, the function will fail (return -1). |
| 39 | * Input: |
| 40 | * arg: length specification (i.e. both command arguments) |
| 41 | * Output: |
| 42 | * len: computed length for operation |
| 43 | * Return: |
| 44 | * 1: success |
| 45 | * -1: failure (bad format, bad address). |
| 46 | */ |
| 47 | static int sf_parse_len_arg(char *arg, ulong *len) |
| 48 | { |
| 49 | char *ep; |
| 50 | char round_up_len; /* indicates if the "+length" form used */ |
| 51 | ulong len_arg; |
| 52 | |
| 53 | round_up_len = 0; |
| 54 | if (*arg == '+') { |
| 55 | round_up_len = 1; |
| 56 | ++arg; |
| 57 | } |
| 58 | |
| 59 | len_arg = simple_strtoul(arg, &ep, 16); |
| 60 | if (ep == arg || *ep != '\0') |
| 61 | return -1; |
| 62 | |
| 63 | if (round_up_len && flash->sector_size > 0) |
Anton Staaf | 155cfb5 | 2011-09-02 13:45:28 +0000 | [diff] [blame] | 64 | *len = ROUND(len_arg, flash->sector_size); |
Richard Retanubun | 334eb4b | 2011-02-16 16:39:44 -0500 | [diff] [blame] | 65 | else |
| 66 | *len = len_arg; |
| 67 | |
| 68 | return 1; |
| 69 | } |
| 70 | |
James Miller | a683c28 | 2012-09-28 14:28:00 +0000 | [diff] [blame] | 71 | /** |
| 72 | * This function takes a byte length and a delta unit of time to compute the |
| 73 | * approximate bytes per second |
| 74 | * |
| 75 | * @param len amount of bytes currently processed |
| 76 | * @param start_ms start time of processing in ms |
| 77 | * @return bytes per second if OK, 0 on error |
| 78 | */ |
| 79 | static ulong bytes_per_second(unsigned int len, ulong start_ms) |
| 80 | { |
| 81 | /* less accurate but avoids overflow */ |
| 82 | if (len >= ((unsigned int) -1) / 1024) |
| 83 | return len / (max(get_timer(start_ms) / 1024, 1)); |
| 84 | else |
| 85 | return 1024 * len / max(get_timer(start_ms), 1); |
| 86 | } |
| 87 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 88 | static int do_spi_flash_probe(int argc, char * const argv[]) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 89 | { |
Eric Nelson | c1173bd | 2012-01-31 10:52:07 -0700 | [diff] [blame] | 90 | unsigned int bus = CONFIG_SF_DEFAULT_BUS; |
| 91 | unsigned int cs = CONFIG_SF_DEFAULT_CS; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 92 | unsigned int speed = CONFIG_SF_DEFAULT_SPEED; |
| 93 | unsigned int mode = CONFIG_SF_DEFAULT_MODE; |
| 94 | char *endp; |
| 95 | struct spi_flash *new; |
| 96 | |
Eric Nelson | c1173bd | 2012-01-31 10:52:07 -0700 | [diff] [blame] | 97 | if (argc >= 2) { |
| 98 | cs = simple_strtoul(argv[1], &endp, 0); |
| 99 | if (*argv[1] == 0 || (*endp != 0 && *endp != ':')) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 100 | return -1; |
Eric Nelson | c1173bd | 2012-01-31 10:52:07 -0700 | [diff] [blame] | 101 | if (*endp == ':') { |
| 102 | if (endp[1] == 0) |
| 103 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 104 | |
Eric Nelson | c1173bd | 2012-01-31 10:52:07 -0700 | [diff] [blame] | 105 | bus = cs; |
| 106 | cs = simple_strtoul(endp + 1, &endp, 0); |
| 107 | if (*endp != 0) |
| 108 | return -1; |
| 109 | } |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | if (argc >= 3) { |
| 113 | speed = simple_strtoul(argv[2], &endp, 0); |
| 114 | if (*argv[2] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 115 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 116 | } |
| 117 | if (argc >= 4) { |
TsiChung Liew | 6e8d58d | 2009-06-30 14:30:19 +0000 | [diff] [blame] | 118 | mode = simple_strtoul(argv[3], &endp, 16); |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 119 | if (*argv[3] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 120 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | new = spi_flash_probe(bus, cs, speed, mode); |
| 124 | if (!new) { |
| 125 | printf("Failed to initialize SPI flash at %u:%u\n", bus, cs); |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | if (flash) |
| 130 | spi_flash_free(flash); |
| 131 | flash = new; |
| 132 | |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 133 | return 0; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 136 | /** |
| 137 | * Write a block of data to SPI flash, first checking if it is different from |
| 138 | * what is already there. |
| 139 | * |
| 140 | * If the data being written is the same, then *skipped is incremented by len. |
| 141 | * |
| 142 | * @param flash flash context pointer |
| 143 | * @param offset flash offset to write |
| 144 | * @param len number of bytes to write |
| 145 | * @param buf buffer to write from |
| 146 | * @param cmp_buf read buffer to use to compare data |
| 147 | * @param skipped Count of skipped data (incremented by this function) |
| 148 | * @return NULL if OK, else a string containing the stage which failed |
| 149 | */ |
| 150 | static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset, |
| 151 | size_t len, const char *buf, char *cmp_buf, size_t *skipped) |
| 152 | { |
Andreas Bießmann | a97f6ef | 2011-12-15 09:56:53 +0100 | [diff] [blame] | 153 | debug("offset=%#x, sector_size=%#x, len=%#zx\n", |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 154 | offset, flash->sector_size, len); |
Gerlando Falauto | b95f958 | 2013-08-27 19:13:57 +0530 | [diff] [blame] | 155 | /* Read the entire sector so to allow for rewriting */ |
| 156 | if (spi_flash_read(flash, offset, flash->sector_size, cmp_buf)) |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 157 | return "read"; |
Gerlando Falauto | b95f958 | 2013-08-27 19:13:57 +0530 | [diff] [blame] | 158 | /* Compare only what is meaningful (len) */ |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 159 | if (memcmp(cmp_buf, buf, len) == 0) { |
Andreas Bießmann | a97f6ef | 2011-12-15 09:56:53 +0100 | [diff] [blame] | 160 | debug("Skip region %x size %zx: no change\n", |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 161 | offset, len); |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 162 | *skipped += len; |
| 163 | return NULL; |
| 164 | } |
Gerlando Falauto | 3488850 | 2013-07-03 20:33:57 +0200 | [diff] [blame] | 165 | /* Erase the entire sector */ |
| 166 | if (spi_flash_erase(flash, offset, flash->sector_size)) |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 167 | return "erase"; |
Gerlando Falauto | b95f958 | 2013-08-27 19:13:57 +0530 | [diff] [blame] | 168 | /* Write the initial part of the block from the source */ |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 169 | if (spi_flash_write(flash, offset, len, buf)) |
| 170 | return "write"; |
Gerlando Falauto | b95f958 | 2013-08-27 19:13:57 +0530 | [diff] [blame] | 171 | /* If it's a partial sector, rewrite the existing part */ |
| 172 | if (len != flash->sector_size) { |
| 173 | /* Rewrite the original data to the end of the sector */ |
| 174 | if (spi_flash_write(flash, offset + len, |
| 175 | flash->sector_size - len, &cmp_buf[len])) |
| 176 | return "write"; |
| 177 | } |
| 178 | |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Update an area of SPI flash by erasing and writing any blocks which need |
| 184 | * to change. Existing blocks with the correct data are left unchanged. |
| 185 | * |
| 186 | * @param flash flash context pointer |
| 187 | * @param offset flash offset to write |
| 188 | * @param len number of bytes to write |
| 189 | * @param buf buffer to write from |
| 190 | * @return 0 if ok, 1 on error |
| 191 | */ |
| 192 | static int spi_flash_update(struct spi_flash *flash, u32 offset, |
| 193 | size_t len, const char *buf) |
| 194 | { |
| 195 | const char *err_oper = NULL; |
| 196 | char *cmp_buf; |
| 197 | const char *end = buf + len; |
| 198 | size_t todo; /* number of bytes to do in this pass */ |
Kumar Gala | 8e8a4bc | 2011-10-03 04:35:47 +0000 | [diff] [blame] | 199 | size_t skipped = 0; /* statistics */ |
James Miller | a683c28 | 2012-09-28 14:28:00 +0000 | [diff] [blame] | 200 | const ulong start_time = get_timer(0); |
| 201 | size_t scale = 1; |
| 202 | const char *start_buf = buf; |
| 203 | ulong delta; |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 204 | |
James Miller | a683c28 | 2012-09-28 14:28:00 +0000 | [diff] [blame] | 205 | if (end - buf >= 200) |
| 206 | scale = (end - buf) / 100; |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 207 | cmp_buf = malloc(flash->sector_size); |
| 208 | if (cmp_buf) { |
James Miller | a683c28 | 2012-09-28 14:28:00 +0000 | [diff] [blame] | 209 | ulong last_update = get_timer(0); |
| 210 | |
Kumar Gala | 8e8a4bc | 2011-10-03 04:35:47 +0000 | [diff] [blame] | 211 | for (; buf < end && !err_oper; buf += todo, offset += todo) { |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 212 | todo = min(end - buf, flash->sector_size); |
James Miller | a683c28 | 2012-09-28 14:28:00 +0000 | [diff] [blame] | 213 | if (get_timer(last_update) > 100) { |
| 214 | printf(" \rUpdating, %zu%% %lu B/s", |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 215 | 100 - (end - buf) / scale, |
James Miller | a683c28 | 2012-09-28 14:28:00 +0000 | [diff] [blame] | 216 | bytes_per_second(buf - start_buf, |
| 217 | start_time)); |
| 218 | last_update = get_timer(0); |
| 219 | } |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 220 | err_oper = spi_flash_update_block(flash, offset, todo, |
| 221 | buf, cmp_buf, &skipped); |
| 222 | } |
| 223 | } else { |
| 224 | err_oper = "malloc"; |
| 225 | } |
| 226 | free(cmp_buf); |
James Miller | a683c28 | 2012-09-28 14:28:00 +0000 | [diff] [blame] | 227 | putc('\r'); |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 228 | if (err_oper) { |
| 229 | printf("SPI flash failed in %s step\n", err_oper); |
| 230 | return 1; |
| 231 | } |
James Miller | a683c28 | 2012-09-28 14:28:00 +0000 | [diff] [blame] | 232 | |
| 233 | delta = get_timer(start_time); |
| 234 | printf("%zu bytes written, %zu bytes skipped", len - skipped, |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 235 | skipped); |
James Miller | a683c28 | 2012-09-28 14:28:00 +0000 | [diff] [blame] | 236 | printf(" in %ld.%lds, speed %ld B/s\n", |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 237 | delta / 1000, delta % 1000, bytes_per_second(len, start_time)); |
Kumar Gala | 8e8a4bc | 2011-10-03 04:35:47 +0000 | [diff] [blame] | 238 | |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 242 | static int do_spi_flash_read_write(int argc, char * const argv[]) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 243 | { |
| 244 | unsigned long addr; |
| 245 | unsigned long offset; |
| 246 | unsigned long len; |
| 247 | void *buf; |
| 248 | char *endp; |
Jagannadha Sutradharudu Teki | 60b6614 | 2013-05-26 14:52:52 +0530 | [diff] [blame] | 249 | int ret = 1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 250 | |
| 251 | if (argc < 4) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 252 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 253 | |
| 254 | addr = simple_strtoul(argv[1], &endp, 16); |
| 255 | if (*argv[1] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 256 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 257 | offset = simple_strtoul(argv[2], &endp, 16); |
| 258 | if (*argv[2] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 259 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 260 | len = simple_strtoul(argv[3], &endp, 16); |
| 261 | if (*argv[3] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 262 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 263 | |
Gerlando Falauto | 8649399 | 2012-04-03 04:34:13 +0000 | [diff] [blame] | 264 | /* Consistency checking */ |
| 265 | if (offset + len > flash->size) { |
| 266 | printf("ERROR: attempting %s past flash size (%#x)\n", |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 267 | argv[0], flash->size); |
Gerlando Falauto | 8649399 | 2012-04-03 04:34:13 +0000 | [diff] [blame] | 268 | return 1; |
| 269 | } |
| 270 | |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 271 | buf = map_physmem(addr, len, MAP_WRBACK); |
| 272 | if (!buf) { |
| 273 | puts("Failed to map physical memory\n"); |
| 274 | return 1; |
| 275 | } |
| 276 | |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 277 | if (strcmp(argv[0], "update") == 0) { |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 278 | ret = spi_flash_update(flash, offset, len, buf); |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 279 | } else if (strncmp(argv[0], "read", 4) == 0 || |
Jagannadha Sutradharudu Teki | 60b6614 | 2013-05-26 14:52:52 +0530 | [diff] [blame] | 280 | strncmp(argv[0], "write", 5) == 0) { |
| 281 | int read; |
| 282 | |
| 283 | read = strncmp(argv[0], "read", 4) == 0; |
| 284 | if (read) |
| 285 | ret = spi_flash_read(flash, offset, len, buf); |
| 286 | else |
| 287 | ret = spi_flash_write(flash, offset, len, buf); |
| 288 | |
| 289 | printf("SF: %zu bytes @ %#x %s: %s\n", (size_t)len, (u32)offset, |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 290 | read ? "Read" : "Written", ret ? "ERROR" : "OK"); |
Jagannadha Sutradharudu Teki | 60b6614 | 2013-05-26 14:52:52 +0530 | [diff] [blame] | 291 | } |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 292 | |
| 293 | unmap_physmem(buf, len); |
| 294 | |
Jagannadha Sutradharudu Teki | 60b6614 | 2013-05-26 14:52:52 +0530 | [diff] [blame] | 295 | return ret == 0 ? 0 : 1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 298 | static int do_spi_flash_erase(int argc, char * const argv[]) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 299 | { |
| 300 | unsigned long offset; |
| 301 | unsigned long len; |
| 302 | char *endp; |
| 303 | int ret; |
| 304 | |
| 305 | if (argc < 3) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 306 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 307 | |
| 308 | offset = simple_strtoul(argv[1], &endp, 16); |
| 309 | if (*argv[1] == 0 || *endp != 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 310 | return -1; |
Richard Retanubun | 334eb4b | 2011-02-16 16:39:44 -0500 | [diff] [blame] | 311 | |
| 312 | ret = sf_parse_len_arg(argv[2], &len); |
| 313 | if (ret != 1) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 314 | return -1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 315 | |
Gerlando Falauto | 8649399 | 2012-04-03 04:34:13 +0000 | [diff] [blame] | 316 | /* Consistency checking */ |
| 317 | if (offset + len > flash->size) { |
| 318 | printf("ERROR: attempting %s past flash size (%#x)\n", |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 319 | argv[0], flash->size); |
Gerlando Falauto | 8649399 | 2012-04-03 04:34:13 +0000 | [diff] [blame] | 320 | return 1; |
| 321 | } |
| 322 | |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 323 | ret = spi_flash_erase(flash, offset, len); |
Jagannadha Sutradharudu Teki | 96bbf55 | 2013-05-26 01:35:21 +0530 | [diff] [blame] | 324 | printf("SF: %zu bytes @ %#x Erased: %s\n", (size_t)len, (u32)offset, |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 325 | ret ? "ERROR" : "OK"); |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 326 | |
Jagannadha Sutradharudu Teki | 96bbf55 | 2013-05-26 01:35:21 +0530 | [diff] [blame] | 327 | return ret == 0 ? 0 : 1; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Simon Glass | 2400727 | 2012-10-08 13:16:02 +0000 | [diff] [blame] | 330 | #ifdef CONFIG_CMD_SF_TEST |
| 331 | enum { |
| 332 | STAGE_ERASE, |
| 333 | STAGE_CHECK, |
| 334 | STAGE_WRITE, |
| 335 | STAGE_READ, |
| 336 | |
| 337 | STAGE_COUNT, |
| 338 | }; |
| 339 | |
| 340 | static char *stage_name[STAGE_COUNT] = { |
| 341 | "erase", |
| 342 | "check", |
| 343 | "write", |
| 344 | "read", |
| 345 | }; |
| 346 | |
| 347 | struct test_info { |
| 348 | int stage; |
| 349 | int bytes; |
| 350 | unsigned base_ms; |
| 351 | unsigned time_ms[STAGE_COUNT]; |
| 352 | }; |
| 353 | |
| 354 | static void show_time(struct test_info *test, int stage) |
| 355 | { |
| 356 | uint64_t speed; /* KiB/s */ |
| 357 | int bps; /* Bits per second */ |
| 358 | |
| 359 | speed = (long long)test->bytes * 1000; |
| 360 | do_div(speed, test->time_ms[stage] * 1024); |
| 361 | bps = speed * 8; |
| 362 | |
| 363 | printf("%d %s: %d ticks, %d KiB/s %d.%03d Mbps\n", stage, |
| 364 | stage_name[stage], test->time_ms[stage], |
| 365 | (int)speed, bps / 1000, bps % 1000); |
| 366 | } |
| 367 | |
| 368 | static void spi_test_next_stage(struct test_info *test) |
| 369 | { |
| 370 | test->time_ms[test->stage] = get_timer(test->base_ms); |
| 371 | show_time(test, test->stage); |
| 372 | test->base_ms = get_timer(0); |
| 373 | test->stage++; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Run a test on the SPI flash |
| 378 | * |
| 379 | * @param flash SPI flash to use |
| 380 | * @param buf Source buffer for data to write |
| 381 | * @param len Size of data to read/write |
| 382 | * @param offset Offset within flash to check |
| 383 | * @param vbuf Verification buffer |
| 384 | * @return 0 if ok, -1 on error |
| 385 | */ |
Simon Glass | 1e7133e | 2013-03-11 06:08:13 +0000 | [diff] [blame] | 386 | static int spi_flash_test(struct spi_flash *flash, uint8_t *buf, ulong len, |
| 387 | ulong offset, uint8_t *vbuf) |
Simon Glass | 2400727 | 2012-10-08 13:16:02 +0000 | [diff] [blame] | 388 | { |
| 389 | struct test_info test; |
| 390 | int i; |
| 391 | |
| 392 | printf("SPI flash test:\n"); |
| 393 | memset(&test, '\0', sizeof(test)); |
| 394 | test.base_ms = get_timer(0); |
| 395 | test.bytes = len; |
| 396 | if (spi_flash_erase(flash, offset, len)) { |
| 397 | printf("Erase failed\n"); |
| 398 | return -1; |
| 399 | } |
| 400 | spi_test_next_stage(&test); |
| 401 | |
| 402 | if (spi_flash_read(flash, offset, len, vbuf)) { |
| 403 | printf("Check read failed\n"); |
| 404 | return -1; |
| 405 | } |
| 406 | for (i = 0; i < len; i++) { |
| 407 | if (vbuf[i] != 0xff) { |
| 408 | printf("Check failed at %d\n", i); |
| 409 | print_buffer(i, vbuf + i, 1, min(len - i, 0x40), 0); |
| 410 | return -1; |
| 411 | } |
| 412 | } |
| 413 | spi_test_next_stage(&test); |
| 414 | |
| 415 | if (spi_flash_write(flash, offset, len, buf)) { |
| 416 | printf("Write failed\n"); |
| 417 | return -1; |
| 418 | } |
| 419 | memset(vbuf, '\0', len); |
| 420 | spi_test_next_stage(&test); |
| 421 | |
| 422 | if (spi_flash_read(flash, offset, len, vbuf)) { |
| 423 | printf("Read failed\n"); |
| 424 | return -1; |
| 425 | } |
| 426 | spi_test_next_stage(&test); |
| 427 | |
| 428 | for (i = 0; i < len; i++) { |
| 429 | if (buf[i] != vbuf[i]) { |
| 430 | printf("Verify failed at %d, good data:\n", i); |
| 431 | print_buffer(i, buf + i, 1, min(len - i, 0x40), 0); |
| 432 | printf("Bad data:\n"); |
| 433 | print_buffer(i, vbuf + i, 1, min(len - i, 0x40), 0); |
| 434 | return -1; |
| 435 | } |
| 436 | } |
| 437 | printf("Test passed\n"); |
| 438 | for (i = 0; i < STAGE_COUNT; i++) |
| 439 | show_time(&test, i); |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int do_spi_flash_test(int argc, char * const argv[]) |
| 445 | { |
| 446 | unsigned long offset; |
| 447 | unsigned long len; |
Simon Glass | 1e7133e | 2013-03-11 06:08:13 +0000 | [diff] [blame] | 448 | uint8_t *buf = (uint8_t *)CONFIG_SYS_TEXT_BASE; |
Simon Glass | 2400727 | 2012-10-08 13:16:02 +0000 | [diff] [blame] | 449 | char *endp; |
Simon Glass | 1e7133e | 2013-03-11 06:08:13 +0000 | [diff] [blame] | 450 | uint8_t *vbuf; |
Simon Glass | 2400727 | 2012-10-08 13:16:02 +0000 | [diff] [blame] | 451 | int ret; |
| 452 | |
| 453 | offset = simple_strtoul(argv[1], &endp, 16); |
| 454 | if (*argv[1] == 0 || *endp != 0) |
| 455 | return -1; |
| 456 | len = simple_strtoul(argv[2], &endp, 16); |
| 457 | if (*argv[2] == 0 || *endp != 0) |
| 458 | return -1; |
| 459 | |
| 460 | vbuf = malloc(len); |
| 461 | if (!vbuf) { |
| 462 | printf("Cannot allocate memory\n"); |
| 463 | return 1; |
| 464 | } |
| 465 | buf = malloc(len); |
| 466 | if (!buf) { |
| 467 | free(vbuf); |
| 468 | printf("Cannot allocate memory\n"); |
| 469 | return 1; |
| 470 | } |
| 471 | |
| 472 | memcpy(buf, (char *)CONFIG_SYS_TEXT_BASE, len); |
| 473 | ret = spi_flash_test(flash, buf, len, offset, vbuf); |
| 474 | free(vbuf); |
| 475 | free(buf); |
| 476 | if (ret) { |
| 477 | printf("Test failed\n"); |
| 478 | return 1; |
| 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | #endif /* CONFIG_CMD_SF_TEST */ |
| 484 | |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 485 | static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, |
| 486 | char * const argv[]) |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 487 | { |
| 488 | const char *cmd; |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 489 | int ret; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 490 | |
| 491 | /* need at least two arguments */ |
| 492 | if (argc < 2) |
| 493 | goto usage; |
| 494 | |
| 495 | cmd = argv[1]; |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 496 | --argc; |
| 497 | ++argv; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 498 | |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 499 | if (strcmp(cmd, "probe") == 0) { |
| 500 | ret = do_spi_flash_probe(argc, argv); |
| 501 | goto done; |
| 502 | } |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 503 | |
| 504 | /* The remaining commands require a selected device */ |
| 505 | if (!flash) { |
| 506 | puts("No SPI flash selected. Please run `sf probe'\n"); |
| 507 | return 1; |
| 508 | } |
| 509 | |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 510 | if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 || |
| 511 | strcmp(cmd, "update") == 0) |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 512 | ret = do_spi_flash_read_write(argc, argv); |
| 513 | else if (strcmp(cmd, "erase") == 0) |
| 514 | ret = do_spi_flash_erase(argc, argv); |
Simon Glass | 2400727 | 2012-10-08 13:16:02 +0000 | [diff] [blame] | 515 | #ifdef CONFIG_CMD_SF_TEST |
| 516 | else if (!strcmp(cmd, "test")) |
| 517 | ret = do_spi_flash_test(argc, argv); |
| 518 | #endif |
Mike Frysinger | 8a07de0 | 2011-04-12 02:21:31 -0400 | [diff] [blame] | 519 | else |
| 520 | ret = -1; |
| 521 | |
| 522 | done: |
| 523 | if (ret != -1) |
| 524 | return ret; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 525 | |
| 526 | usage: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 527 | return CMD_RET_USAGE; |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Simon Glass | 2400727 | 2012-10-08 13:16:02 +0000 | [diff] [blame] | 530 | #ifdef CONFIG_CMD_SF_TEST |
| 531 | #define SF_TEST_HELP "\nsf test offset len " \ |
| 532 | "- run a very basic destructive test" |
| 533 | #else |
| 534 | #define SF_TEST_HELP |
| 535 | #endif |
| 536 | |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 537 | U_BOOT_CMD( |
| 538 | sf, 5, 1, do_spi_flash, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 539 | "SPI flash sub-system", |
Eric Nelson | c1173bd | 2012-01-31 10:52:07 -0700 | [diff] [blame] | 540 | "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] | 541 | " and chip select\n" |
Jagannadha Sutradharudu Teki | 402ba1e | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 542 | "sf read addr offset len - read `len' bytes starting at\n" |
Haavard Skinnemoen | b636846 | 2008-05-16 11:10:34 +0200 | [diff] [blame] | 543 | " `offset' to memory at `addr'\n" |
| 544 | "sf write addr offset len - write `len' bytes from memory\n" |
| 545 | " at `addr' to flash at `offset'\n" |
Richard Retanubun | 334eb4b | 2011-02-16 16:39:44 -0500 | [diff] [blame] | 546 | "sf erase offset [+]len - erase `len' bytes from `offset'\n" |
Simon Glass | 8d1af94 | 2011-08-22 16:03:46 -0700 | [diff] [blame] | 547 | " `+len' round up `len' to block size\n" |
| 548 | "sf update addr offset len - erase and write `len' bytes from memory\n" |
| 549 | " at `addr' to flash at `offset'" |
Simon Glass | 2400727 | 2012-10-08 13:16:02 +0000 | [diff] [blame] | 550 | SF_TEST_HELP |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 551 | ); |