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