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