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