183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
22e192b24SSimon Glass /*
32e192b24SSimon Glass * Command for accessing SPI flash.
42e192b24SSimon Glass *
52e192b24SSimon Glass * Copyright (C) 2008 Atmel Corporation
62e192b24SSimon Glass */
72e192b24SSimon Glass
82e192b24SSimon Glass #include <common.h>
92e192b24SSimon Glass #include <div64.h>
102e192b24SSimon Glass #include <dm.h>
112e192b24SSimon Glass #include <malloc.h>
122e192b24SSimon Glass #include <mapmem.h>
132e192b24SSimon Glass #include <spi.h>
142e192b24SSimon Glass #include <spi_flash.h>
152e192b24SSimon Glass #include <jffs2/jffs2.h>
162e192b24SSimon Glass #include <linux/mtd/mtd.h>
172e192b24SSimon Glass
182e192b24SSimon Glass #include <asm/io.h>
192e192b24SSimon Glass #include <dm/device-internal.h>
202e192b24SSimon Glass
212e192b24SSimon Glass static struct spi_flash *flash;
222e192b24SSimon Glass
232e192b24SSimon Glass /*
242e192b24SSimon Glass * This function computes the length argument for the erase command.
252e192b24SSimon Glass * The length on which the command is to operate can be given in two forms:
262e192b24SSimon Glass * 1. <cmd> offset len - operate on <'offset', 'len')
272e192b24SSimon Glass * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)')
282e192b24SSimon Glass * If the second form is used and the length doesn't fall on the
292e192b24SSimon Glass * sector boundary, than it will be adjusted to the next sector boundary.
302e192b24SSimon Glass * If it isn't in the flash, the function will fail (return -1).
312e192b24SSimon Glass * Input:
322e192b24SSimon Glass * arg: length specification (i.e. both command arguments)
332e192b24SSimon Glass * Output:
342e192b24SSimon Glass * len: computed length for operation
352e192b24SSimon Glass * Return:
362e192b24SSimon Glass * 1: success
372e192b24SSimon Glass * -1: failure (bad format, bad address).
382e192b24SSimon Glass */
sf_parse_len_arg(char * arg,ulong * len)392e192b24SSimon Glass static int sf_parse_len_arg(char *arg, ulong *len)
402e192b24SSimon Glass {
412e192b24SSimon Glass char *ep;
422e192b24SSimon Glass char round_up_len; /* indicates if the "+length" form used */
432e192b24SSimon Glass ulong len_arg;
442e192b24SSimon Glass
452e192b24SSimon Glass round_up_len = 0;
462e192b24SSimon Glass if (*arg == '+') {
472e192b24SSimon Glass round_up_len = 1;
482e192b24SSimon Glass ++arg;
492e192b24SSimon Glass }
502e192b24SSimon Glass
512e192b24SSimon Glass len_arg = simple_strtoul(arg, &ep, 16);
522e192b24SSimon Glass if (ep == arg || *ep != '\0')
532e192b24SSimon Glass return -1;
542e192b24SSimon Glass
552e192b24SSimon Glass if (round_up_len && flash->sector_size > 0)
562e192b24SSimon Glass *len = ROUND(len_arg, flash->sector_size);
572e192b24SSimon Glass else
582e192b24SSimon Glass *len = len_arg;
592e192b24SSimon Glass
602e192b24SSimon Glass return 1;
612e192b24SSimon Glass }
622e192b24SSimon Glass
632e192b24SSimon Glass /**
642e192b24SSimon Glass * This function takes a byte length and a delta unit of time to compute the
652e192b24SSimon Glass * approximate bytes per second
662e192b24SSimon Glass *
672e192b24SSimon Glass * @param len amount of bytes currently processed
682e192b24SSimon Glass * @param start_ms start time of processing in ms
692e192b24SSimon Glass * @return bytes per second if OK, 0 on error
702e192b24SSimon Glass */
bytes_per_second(unsigned int len,ulong start_ms)712e192b24SSimon Glass static ulong bytes_per_second(unsigned int len, ulong start_ms)
722e192b24SSimon Glass {
732e192b24SSimon Glass /* less accurate but avoids overflow */
742e192b24SSimon Glass if (len >= ((unsigned int) -1) / 1024)
752e192b24SSimon Glass return len / (max(get_timer(start_ms) / 1024, 1UL));
762e192b24SSimon Glass else
772e192b24SSimon Glass return 1024 * len / max(get_timer(start_ms), 1UL);
782e192b24SSimon Glass }
792e192b24SSimon Glass
do_spi_flash_probe(int argc,char * const argv[])802e192b24SSimon Glass static int do_spi_flash_probe(int argc, char * const argv[])
812e192b24SSimon Glass {
822e192b24SSimon Glass unsigned int bus = CONFIG_SF_DEFAULT_BUS;
832e192b24SSimon Glass unsigned int cs = CONFIG_SF_DEFAULT_CS;
842e192b24SSimon Glass unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
852e192b24SSimon Glass unsigned int mode = CONFIG_SF_DEFAULT_MODE;
862e192b24SSimon Glass char *endp;
872e192b24SSimon Glass #ifdef CONFIG_DM_SPI_FLASH
882e192b24SSimon Glass struct udevice *new, *bus_dev;
892e192b24SSimon Glass int ret;
9096907c0fSVignesh R /* In DM mode defaults will be taken from DT */
9196907c0fSVignesh R speed = 0, mode = 0;
922e192b24SSimon Glass #else
932e192b24SSimon Glass struct spi_flash *new;
942e192b24SSimon Glass #endif
952e192b24SSimon Glass
962e192b24SSimon Glass if (argc >= 2) {
972e192b24SSimon Glass cs = simple_strtoul(argv[1], &endp, 0);
982e192b24SSimon Glass if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
992e192b24SSimon Glass return -1;
1002e192b24SSimon Glass if (*endp == ':') {
1012e192b24SSimon Glass if (endp[1] == 0)
1022e192b24SSimon Glass return -1;
1032e192b24SSimon Glass
1042e192b24SSimon Glass bus = cs;
1052e192b24SSimon Glass cs = simple_strtoul(endp + 1, &endp, 0);
1062e192b24SSimon Glass if (*endp != 0)
1072e192b24SSimon Glass return -1;
1082e192b24SSimon Glass }
1092e192b24SSimon Glass }
1102e192b24SSimon Glass
1112e192b24SSimon Glass if (argc >= 3) {
1122e192b24SSimon Glass speed = simple_strtoul(argv[2], &endp, 0);
1132e192b24SSimon Glass if (*argv[2] == 0 || *endp != 0)
1142e192b24SSimon Glass return -1;
1152e192b24SSimon Glass }
1162e192b24SSimon Glass if (argc >= 4) {
1172e192b24SSimon Glass mode = simple_strtoul(argv[3], &endp, 16);
1182e192b24SSimon Glass if (*argv[3] == 0 || *endp != 0)
1192e192b24SSimon Glass return -1;
1202e192b24SSimon Glass }
1212e192b24SSimon Glass
1222e192b24SSimon Glass #ifdef CONFIG_DM_SPI_FLASH
1232e192b24SSimon Glass /* Remove the old device, otherwise probe will just be a nop */
1242e192b24SSimon Glass ret = spi_find_bus_and_cs(bus, cs, &bus_dev, &new);
1252e192b24SSimon Glass if (!ret) {
126706865afSStefan Roese device_remove(new, DM_REMOVE_NORMAL);
1272e192b24SSimon Glass }
1282e192b24SSimon Glass flash = NULL;
1292e192b24SSimon Glass ret = spi_flash_probe_bus_cs(bus, cs, speed, mode, &new);
1302e192b24SSimon Glass if (ret) {
1312e192b24SSimon Glass printf("Failed to initialize SPI flash at %u:%u (error %d)\n",
1322e192b24SSimon Glass bus, cs, ret);
1332e192b24SSimon Glass return 1;
1342e192b24SSimon Glass }
1352e192b24SSimon Glass
1362e192b24SSimon Glass flash = dev_get_uclass_priv(new);
1372e192b24SSimon Glass #else
1382e192b24SSimon Glass if (flash)
1392e192b24SSimon Glass spi_flash_free(flash);
1402e192b24SSimon Glass
1412e192b24SSimon Glass new = spi_flash_probe(bus, cs, speed, mode);
1422e192b24SSimon Glass flash = new;
1432e192b24SSimon Glass
1442e192b24SSimon Glass if (!new) {
1452e192b24SSimon Glass printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
1462e192b24SSimon Glass return 1;
1472e192b24SSimon Glass }
1482e192b24SSimon Glass
1492e192b24SSimon Glass flash = new;
1502e192b24SSimon Glass #endif
1512e192b24SSimon Glass
1522e192b24SSimon Glass return 0;
1532e192b24SSimon Glass }
1542e192b24SSimon Glass
1552e192b24SSimon Glass /**
1562e192b24SSimon Glass * Write a block of data to SPI flash, first checking if it is different from
1572e192b24SSimon Glass * what is already there.
1582e192b24SSimon Glass *
1592e192b24SSimon Glass * If the data being written is the same, then *skipped is incremented by len.
1602e192b24SSimon Glass *
1612e192b24SSimon Glass * @param flash flash context pointer
1622e192b24SSimon Glass * @param offset flash offset to write
1632e192b24SSimon Glass * @param len number of bytes to write
1642e192b24SSimon Glass * @param buf buffer to write from
1652e192b24SSimon Glass * @param cmp_buf read buffer to use to compare data
1662e192b24SSimon Glass * @param skipped Count of skipped data (incremented by this function)
1672e192b24SSimon Glass * @return NULL if OK, else a string containing the stage which failed
1682e192b24SSimon Glass */
spi_flash_update_block(struct spi_flash * flash,u32 offset,size_t len,const char * buf,char * cmp_buf,size_t * skipped)1692e192b24SSimon Glass static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
1702e192b24SSimon Glass size_t len, const char *buf, char *cmp_buf, size_t *skipped)
1712e192b24SSimon Glass {
1722e192b24SSimon Glass char *ptr = (char *)buf;
1732e192b24SSimon Glass
1742e192b24SSimon Glass debug("offset=%#x, sector_size=%#x, len=%#zx\n",
1752e192b24SSimon Glass offset, flash->sector_size, len);
1762e192b24SSimon Glass /* Read the entire sector so to allow for rewriting */
1772e192b24SSimon Glass if (spi_flash_read(flash, offset, flash->sector_size, cmp_buf))
1782e192b24SSimon Glass return "read";
1792e192b24SSimon Glass /* Compare only what is meaningful (len) */
1802e192b24SSimon Glass if (memcmp(cmp_buf, buf, len) == 0) {
1812e192b24SSimon Glass debug("Skip region %x size %zx: no change\n",
1822e192b24SSimon Glass offset, len);
1832e192b24SSimon Glass *skipped += len;
1842e192b24SSimon Glass return NULL;
1852e192b24SSimon Glass }
1862e192b24SSimon Glass /* Erase the entire sector */
1872e192b24SSimon Glass if (spi_flash_erase(flash, offset, flash->sector_size))
1882e192b24SSimon Glass return "erase";
1892e192b24SSimon Glass /* If it's a partial sector, copy the data into the temp-buffer */
1902e192b24SSimon Glass if (len != flash->sector_size) {
1912e192b24SSimon Glass memcpy(cmp_buf, buf, len);
1922e192b24SSimon Glass ptr = cmp_buf;
1932e192b24SSimon Glass }
1942e192b24SSimon Glass /* Write one complete sector */
1952e192b24SSimon Glass if (spi_flash_write(flash, offset, flash->sector_size, ptr))
1962e192b24SSimon Glass return "write";
1972e192b24SSimon Glass
1982e192b24SSimon Glass return NULL;
1992e192b24SSimon Glass }
2002e192b24SSimon Glass
2012e192b24SSimon Glass /**
2022e192b24SSimon Glass * Update an area of SPI flash by erasing and writing any blocks which need
2032e192b24SSimon Glass * to change. Existing blocks with the correct data are left unchanged.
2042e192b24SSimon Glass *
2052e192b24SSimon Glass * @param flash flash context pointer
2062e192b24SSimon Glass * @param offset flash offset to write
2072e192b24SSimon Glass * @param len number of bytes to write
2082e192b24SSimon Glass * @param buf buffer to write from
2092e192b24SSimon Glass * @return 0 if ok, 1 on error
2102e192b24SSimon Glass */
spi_flash_update(struct spi_flash * flash,u32 offset,size_t len,const char * buf)2112e192b24SSimon Glass static int spi_flash_update(struct spi_flash *flash, u32 offset,
2122e192b24SSimon Glass size_t len, const char *buf)
2132e192b24SSimon Glass {
2142e192b24SSimon Glass const char *err_oper = NULL;
2152e192b24SSimon Glass char *cmp_buf;
2162e192b24SSimon Glass const char *end = buf + len;
2172e192b24SSimon Glass size_t todo; /* number of bytes to do in this pass */
2182e192b24SSimon Glass size_t skipped = 0; /* statistics */
2192e192b24SSimon Glass const ulong start_time = get_timer(0);
2202e192b24SSimon Glass size_t scale = 1;
2212e192b24SSimon Glass const char *start_buf = buf;
2222e192b24SSimon Glass ulong delta;
2232e192b24SSimon Glass
2242e192b24SSimon Glass if (end - buf >= 200)
2252e192b24SSimon Glass scale = (end - buf) / 100;
2262e192b24SSimon Glass cmp_buf = memalign(ARCH_DMA_MINALIGN, flash->sector_size);
2272e192b24SSimon Glass if (cmp_buf) {
2282e192b24SSimon Glass ulong last_update = get_timer(0);
2292e192b24SSimon Glass
2302e192b24SSimon Glass for (; buf < end && !err_oper; buf += todo, offset += todo) {
2312e192b24SSimon Glass todo = min_t(size_t, end - buf, flash->sector_size);
2322e192b24SSimon Glass if (get_timer(last_update) > 100) {
2332e192b24SSimon Glass printf(" \rUpdating, %zu%% %lu B/s",
2342e192b24SSimon Glass 100 - (end - buf) / scale,
2352e192b24SSimon Glass bytes_per_second(buf - start_buf,
2362e192b24SSimon Glass start_time));
2372e192b24SSimon Glass last_update = get_timer(0);
2382e192b24SSimon Glass }
2392e192b24SSimon Glass err_oper = spi_flash_update_block(flash, offset, todo,
2402e192b24SSimon Glass buf, cmp_buf, &skipped);
2412e192b24SSimon Glass }
2422e192b24SSimon Glass } else {
2432e192b24SSimon Glass err_oper = "malloc";
2442e192b24SSimon Glass }
2452e192b24SSimon Glass free(cmp_buf);
2462e192b24SSimon Glass putc('\r');
2472e192b24SSimon Glass if (err_oper) {
2482e192b24SSimon Glass printf("SPI flash failed in %s step\n", err_oper);
2492e192b24SSimon Glass return 1;
2502e192b24SSimon Glass }
2512e192b24SSimon Glass
2522e192b24SSimon Glass delta = get_timer(start_time);
2532e192b24SSimon Glass printf("%zu bytes written, %zu bytes skipped", len - skipped,
2542e192b24SSimon Glass skipped);
2552e192b24SSimon Glass printf(" in %ld.%lds, speed %ld B/s\n",
2562e192b24SSimon Glass delta / 1000, delta % 1000, bytes_per_second(len, start_time));
2572e192b24SSimon Glass
2582e192b24SSimon Glass return 0;
2592e192b24SSimon Glass }
2602e192b24SSimon Glass
do_spi_flash_read_write(int argc,char * const argv[])2612e192b24SSimon Glass static int do_spi_flash_read_write(int argc, char * const argv[])
2622e192b24SSimon Glass {
2632e192b24SSimon Glass unsigned long addr;
2642e192b24SSimon Glass void *buf;
2652e192b24SSimon Glass char *endp;
2662e192b24SSimon Glass int ret = 1;
2672e192b24SSimon Glass int dev = 0;
2682e192b24SSimon Glass loff_t offset, len, maxsize;
2692e192b24SSimon Glass
2702e192b24SSimon Glass if (argc < 3)
2712e192b24SSimon Glass return -1;
2722e192b24SSimon Glass
2732e192b24SSimon Glass addr = simple_strtoul(argv[1], &endp, 16);
2742e192b24SSimon Glass if (*argv[1] == 0 || *endp != 0)
2752e192b24SSimon Glass return -1;
2762e192b24SSimon Glass
2772e192b24SSimon Glass if (mtd_arg_off_size(argc - 2, &argv[2], &dev, &offset, &len,
2782e192b24SSimon Glass &maxsize, MTD_DEV_TYPE_NOR, flash->size))
2792e192b24SSimon Glass return -1;
2802e192b24SSimon Glass
2812e192b24SSimon Glass /* Consistency checking */
2822e192b24SSimon Glass if (offset + len > flash->size) {
2832e192b24SSimon Glass printf("ERROR: attempting %s past flash size (%#x)\n",
2842e192b24SSimon Glass argv[0], flash->size);
2852e192b24SSimon Glass return 1;
2862e192b24SSimon Glass }
2872e192b24SSimon Glass
2882e192b24SSimon Glass buf = map_physmem(addr, len, MAP_WRBACK);
28910f087f7SLiam Beguin if (!buf && addr) {
2902e192b24SSimon Glass puts("Failed to map physical memory\n");
2912e192b24SSimon Glass return 1;
2922e192b24SSimon Glass }
2932e192b24SSimon Glass
2942e192b24SSimon Glass if (strcmp(argv[0], "update") == 0) {
2952e192b24SSimon Glass ret = spi_flash_update(flash, offset, len, buf);
2962e192b24SSimon Glass } else if (strncmp(argv[0], "read", 4) == 0 ||
2972e192b24SSimon Glass strncmp(argv[0], "write", 5) == 0) {
2982e192b24SSimon Glass int read;
2992e192b24SSimon Glass
3002e192b24SSimon Glass read = strncmp(argv[0], "read", 4) == 0;
3012e192b24SSimon Glass if (read)
3022e192b24SSimon Glass ret = spi_flash_read(flash, offset, len, buf);
3032e192b24SSimon Glass else
3042e192b24SSimon Glass ret = spi_flash_write(flash, offset, len, buf);
3052e192b24SSimon Glass
3062e192b24SSimon Glass printf("SF: %zu bytes @ %#x %s: ", (size_t)len, (u32)offset,
3072e192b24SSimon Glass read ? "Read" : "Written");
3082e192b24SSimon Glass if (ret)
3092e192b24SSimon Glass printf("ERROR %d\n", ret);
3102e192b24SSimon Glass else
3112e192b24SSimon Glass printf("OK\n");
3122e192b24SSimon Glass }
3132e192b24SSimon Glass
3142e192b24SSimon Glass unmap_physmem(buf, len);
3152e192b24SSimon Glass
3162e192b24SSimon Glass return ret == 0 ? 0 : 1;
3172e192b24SSimon Glass }
3182e192b24SSimon Glass
do_spi_flash_erase(int argc,char * const argv[])3192e192b24SSimon Glass static int do_spi_flash_erase(int argc, char * const argv[])
3202e192b24SSimon Glass {
3212e192b24SSimon Glass int ret;
3222e192b24SSimon Glass int dev = 0;
3232e192b24SSimon Glass loff_t offset, len, maxsize;
3242e192b24SSimon Glass ulong size;
3252e192b24SSimon Glass
3262e192b24SSimon Glass if (argc < 3)
3272e192b24SSimon Glass return -1;
3282e192b24SSimon Glass
3292e192b24SSimon Glass if (mtd_arg_off(argv[1], &dev, &offset, &len, &maxsize,
3302e192b24SSimon Glass MTD_DEV_TYPE_NOR, flash->size))
3312e192b24SSimon Glass return -1;
3322e192b24SSimon Glass
3332e192b24SSimon Glass ret = sf_parse_len_arg(argv[2], &size);
3342e192b24SSimon Glass if (ret != 1)
3352e192b24SSimon Glass return -1;
3362e192b24SSimon Glass
3372e192b24SSimon Glass /* Consistency checking */
3382e192b24SSimon Glass if (offset + size > flash->size) {
3392e192b24SSimon Glass printf("ERROR: attempting %s past flash size (%#x)\n",
3402e192b24SSimon Glass argv[0], flash->size);
3412e192b24SSimon Glass return 1;
3422e192b24SSimon Glass }
3432e192b24SSimon Glass
3442e192b24SSimon Glass ret = spi_flash_erase(flash, offset, size);
3452e192b24SSimon Glass printf("SF: %zu bytes @ %#x Erased: %s\n", (size_t)size, (u32)offset,
3462e192b24SSimon Glass ret ? "ERROR" : "OK");
3472e192b24SSimon Glass
3482e192b24SSimon Glass return ret == 0 ? 0 : 1;
3492e192b24SSimon Glass }
3502e192b24SSimon Glass
do_spi_protect(int argc,char * const argv[])3512e192b24SSimon Glass static int do_spi_protect(int argc, char * const argv[])
3522e192b24SSimon Glass {
3532e192b24SSimon Glass int ret = 0;
3542e192b24SSimon Glass loff_t start, len;
3552e192b24SSimon Glass bool prot = false;
3562e192b24SSimon Glass
3572e192b24SSimon Glass if (argc != 4)
3582e192b24SSimon Glass return -1;
3592e192b24SSimon Glass
3602e192b24SSimon Glass if (!str2off(argv[2], &start)) {
3612e192b24SSimon Glass puts("start sector is not a valid number\n");
3622e192b24SSimon Glass return 1;
3632e192b24SSimon Glass }
3642e192b24SSimon Glass
3652e192b24SSimon Glass if (!str2off(argv[3], &len)) {
3662e192b24SSimon Glass puts("len is not a valid number\n");
3672e192b24SSimon Glass return 1;
3682e192b24SSimon Glass }
3692e192b24SSimon Glass
3702e192b24SSimon Glass if (strcmp(argv[1], "lock") == 0)
3712e192b24SSimon Glass prot = true;
3722e192b24SSimon Glass else if (strcmp(argv[1], "unlock") == 0)
3732e192b24SSimon Glass prot = false;
3742e192b24SSimon Glass else
3752e192b24SSimon Glass return -1; /* Unknown parameter */
3762e192b24SSimon Glass
3772e192b24SSimon Glass ret = spi_flash_protect(flash, start, len, prot);
3782e192b24SSimon Glass
3792e192b24SSimon Glass return ret == 0 ? 0 : 1;
3802e192b24SSimon Glass }
3812e192b24SSimon Glass
3822e192b24SSimon Glass #ifdef CONFIG_CMD_SF_TEST
3832e192b24SSimon Glass enum {
3842e192b24SSimon Glass STAGE_ERASE,
3852e192b24SSimon Glass STAGE_CHECK,
3862e192b24SSimon Glass STAGE_WRITE,
3872e192b24SSimon Glass STAGE_READ,
3882e192b24SSimon Glass
3892e192b24SSimon Glass STAGE_COUNT,
3902e192b24SSimon Glass };
3912e192b24SSimon Glass
3922e192b24SSimon Glass static char *stage_name[STAGE_COUNT] = {
3932e192b24SSimon Glass "erase",
3942e192b24SSimon Glass "check",
3952e192b24SSimon Glass "write",
3962e192b24SSimon Glass "read",
3972e192b24SSimon Glass };
3982e192b24SSimon Glass
3992e192b24SSimon Glass struct test_info {
4002e192b24SSimon Glass int stage;
4012e192b24SSimon Glass int bytes;
4022e192b24SSimon Glass unsigned base_ms;
4032e192b24SSimon Glass unsigned time_ms[STAGE_COUNT];
4042e192b24SSimon Glass };
4052e192b24SSimon Glass
show_time(struct test_info * test,int stage)4062e192b24SSimon Glass static void show_time(struct test_info *test, int stage)
4072e192b24SSimon Glass {
4082e192b24SSimon Glass uint64_t speed; /* KiB/s */
4092e192b24SSimon Glass int bps; /* Bits per second */
4102e192b24SSimon Glass
4112e192b24SSimon Glass speed = (long long)test->bytes * 1000;
4122e192b24SSimon Glass if (test->time_ms[stage])
4132e192b24SSimon Glass do_div(speed, test->time_ms[stage] * 1024);
4142e192b24SSimon Glass bps = speed * 8;
4152e192b24SSimon Glass
416c6b2ea37SHeinrich Schuchardt printf("%d %s: %u ticks, %d KiB/s %d.%03d Mbps\n", stage,
4172e192b24SSimon Glass stage_name[stage], test->time_ms[stage],
4182e192b24SSimon Glass (int)speed, bps / 1000, bps % 1000);
4192e192b24SSimon Glass }
4202e192b24SSimon Glass
spi_test_next_stage(struct test_info * test)4212e192b24SSimon Glass static void spi_test_next_stage(struct test_info *test)
4222e192b24SSimon Glass {
4232e192b24SSimon Glass test->time_ms[test->stage] = get_timer(test->base_ms);
4242e192b24SSimon Glass show_time(test, test->stage);
4252e192b24SSimon Glass test->base_ms = get_timer(0);
4262e192b24SSimon Glass test->stage++;
4272e192b24SSimon Glass }
4282e192b24SSimon Glass
4292e192b24SSimon Glass /**
4302e192b24SSimon Glass * Run a test on the SPI flash
4312e192b24SSimon Glass *
4322e192b24SSimon Glass * @param flash SPI flash to use
4332e192b24SSimon Glass * @param buf Source buffer for data to write
4342e192b24SSimon Glass * @param len Size of data to read/write
4352e192b24SSimon Glass * @param offset Offset within flash to check
4362e192b24SSimon Glass * @param vbuf Verification buffer
4372e192b24SSimon Glass * @return 0 if ok, -1 on error
4382e192b24SSimon Glass */
spi_flash_test(struct spi_flash * flash,uint8_t * buf,ulong len,ulong offset,uint8_t * vbuf)4392e192b24SSimon Glass static int spi_flash_test(struct spi_flash *flash, uint8_t *buf, ulong len,
4402e192b24SSimon Glass ulong offset, uint8_t *vbuf)
4412e192b24SSimon Glass {
4422e192b24SSimon Glass struct test_info test;
4432e192b24SSimon Glass int i;
4442e192b24SSimon Glass
4452e192b24SSimon Glass printf("SPI flash test:\n");
4462e192b24SSimon Glass memset(&test, '\0', sizeof(test));
4472e192b24SSimon Glass test.base_ms = get_timer(0);
4482e192b24SSimon Glass test.bytes = len;
4492e192b24SSimon Glass if (spi_flash_erase(flash, offset, len)) {
4502e192b24SSimon Glass printf("Erase failed\n");
4512e192b24SSimon Glass return -1;
4522e192b24SSimon Glass }
4532e192b24SSimon Glass spi_test_next_stage(&test);
4542e192b24SSimon Glass
4552e192b24SSimon Glass if (spi_flash_read(flash, offset, len, vbuf)) {
4562e192b24SSimon Glass printf("Check read failed\n");
4572e192b24SSimon Glass return -1;
4582e192b24SSimon Glass }
4592e192b24SSimon Glass for (i = 0; i < len; i++) {
4602e192b24SSimon Glass if (vbuf[i] != 0xff) {
4612e192b24SSimon Glass printf("Check failed at %d\n", i);
4622e192b24SSimon Glass print_buffer(i, vbuf + i, 1,
4632e192b24SSimon Glass min_t(uint, len - i, 0x40), 0);
4642e192b24SSimon Glass return -1;
4652e192b24SSimon Glass }
4662e192b24SSimon Glass }
4672e192b24SSimon Glass spi_test_next_stage(&test);
4682e192b24SSimon Glass
4692e192b24SSimon Glass if (spi_flash_write(flash, offset, len, buf)) {
4702e192b24SSimon Glass printf("Write failed\n");
4712e192b24SSimon Glass return -1;
4722e192b24SSimon Glass }
4732e192b24SSimon Glass memset(vbuf, '\0', len);
4742e192b24SSimon Glass spi_test_next_stage(&test);
4752e192b24SSimon Glass
4762e192b24SSimon Glass if (spi_flash_read(flash, offset, len, vbuf)) {
4772e192b24SSimon Glass printf("Read failed\n");
4782e192b24SSimon Glass return -1;
4792e192b24SSimon Glass }
4802e192b24SSimon Glass spi_test_next_stage(&test);
4812e192b24SSimon Glass
4822e192b24SSimon Glass for (i = 0; i < len; i++) {
4832e192b24SSimon Glass if (buf[i] != vbuf[i]) {
4842e192b24SSimon Glass printf("Verify failed at %d, good data:\n", i);
4852e192b24SSimon Glass print_buffer(i, buf + i, 1,
4862e192b24SSimon Glass min_t(uint, len - i, 0x40), 0);
4872e192b24SSimon Glass printf("Bad data:\n");
4882e192b24SSimon Glass print_buffer(i, vbuf + i, 1,
4892e192b24SSimon Glass min_t(uint, len - i, 0x40), 0);
4902e192b24SSimon Glass return -1;
4912e192b24SSimon Glass }
4922e192b24SSimon Glass }
4932e192b24SSimon Glass printf("Test passed\n");
4942e192b24SSimon Glass for (i = 0; i < STAGE_COUNT; i++)
4952e192b24SSimon Glass show_time(&test, i);
4962e192b24SSimon Glass
4972e192b24SSimon Glass return 0;
4982e192b24SSimon Glass }
4992e192b24SSimon Glass
do_spi_flash_test(int argc,char * const argv[])5002e192b24SSimon Glass static int do_spi_flash_test(int argc, char * const argv[])
5012e192b24SSimon Glass {
5022e192b24SSimon Glass unsigned long offset;
5032e192b24SSimon Glass unsigned long len;
5042e192b24SSimon Glass uint8_t *buf, *from;
5052e192b24SSimon Glass char *endp;
5062e192b24SSimon Glass uint8_t *vbuf;
5072e192b24SSimon Glass int ret;
5082e192b24SSimon Glass
5092e192b24SSimon Glass if (argc < 3)
5102e192b24SSimon Glass return -1;
5112e192b24SSimon Glass offset = simple_strtoul(argv[1], &endp, 16);
5122e192b24SSimon Glass if (*argv[1] == 0 || *endp != 0)
5132e192b24SSimon Glass return -1;
5142e192b24SSimon Glass len = simple_strtoul(argv[2], &endp, 16);
5152e192b24SSimon Glass if (*argv[2] == 0 || *endp != 0)
5162e192b24SSimon Glass return -1;
5172e192b24SSimon Glass
5182e192b24SSimon Glass vbuf = memalign(ARCH_DMA_MINALIGN, len);
5192e192b24SSimon Glass if (!vbuf) {
5202e192b24SSimon Glass printf("Cannot allocate memory (%lu bytes)\n", len);
5212e192b24SSimon Glass return 1;
5222e192b24SSimon Glass }
5232e192b24SSimon Glass buf = memalign(ARCH_DMA_MINALIGN, len);
5242e192b24SSimon Glass if (!buf) {
5252e192b24SSimon Glass free(vbuf);
5262e192b24SSimon Glass printf("Cannot allocate memory (%lu bytes)\n", len);
5272e192b24SSimon Glass return 1;
5282e192b24SSimon Glass }
5292e192b24SSimon Glass
5302e192b24SSimon Glass from = map_sysmem(CONFIG_SYS_TEXT_BASE, 0);
5312e192b24SSimon Glass memcpy(buf, from, len);
5322e192b24SSimon Glass ret = spi_flash_test(flash, buf, len, offset, vbuf);
5332e192b24SSimon Glass free(vbuf);
5342e192b24SSimon Glass free(buf);
5352e192b24SSimon Glass if (ret) {
5362e192b24SSimon Glass printf("Test failed\n");
5372e192b24SSimon Glass return 1;
5382e192b24SSimon Glass }
5392e192b24SSimon Glass
5402e192b24SSimon Glass return 0;
5412e192b24SSimon Glass }
5422e192b24SSimon Glass #endif /* CONFIG_CMD_SF_TEST */
5432e192b24SSimon Glass
544*591e1cf0SChin-Ting Kuo #ifdef CONFIG_ASPEED_SPI_FLASH_WRITE_PROTECTION
do_spi_flash_ctrl_wlock(int argc,char * const argv[])545*591e1cf0SChin-Ting Kuo static int do_spi_flash_ctrl_wlock(int argc, char * const argv[])
546*591e1cf0SChin-Ting Kuo {
547*591e1cf0SChin-Ting Kuo int ret = 0;
548*591e1cf0SChin-Ting Kuo unsigned long offset;
549*591e1cf0SChin-Ting Kuo unsigned long len;
550*591e1cf0SChin-Ting Kuo char *endp;
551*591e1cf0SChin-Ting Kuo bool wlock;
552*591e1cf0SChin-Ting Kuo
553*591e1cf0SChin-Ting Kuo if (argc != 4)
554*591e1cf0SChin-Ting Kuo return -1;
555*591e1cf0SChin-Ting Kuo
556*591e1cf0SChin-Ting Kuo offset = simple_strtoul(argv[2], &endp, 16);
557*591e1cf0SChin-Ting Kuo if (*argv[2] == 0 || *endp != 0)
558*591e1cf0SChin-Ting Kuo return -1;
559*591e1cf0SChin-Ting Kuo
560*591e1cf0SChin-Ting Kuo len = simple_strtoul(argv[3], &endp, 16);
561*591e1cf0SChin-Ting Kuo if (*argv[3] == 0 || *endp != 0)
562*591e1cf0SChin-Ting Kuo return -1;
563*591e1cf0SChin-Ting Kuo
564*591e1cf0SChin-Ting Kuo if (strcmp(argv[1], "lock") == 0)
565*591e1cf0SChin-Ting Kuo wlock = true;
566*591e1cf0SChin-Ting Kuo else if (strcmp(argv[1], "unlock") == 0)
567*591e1cf0SChin-Ting Kuo wlock = false;
568*591e1cf0SChin-Ting Kuo else
569*591e1cf0SChin-Ting Kuo return -1; /* Unknown parameter */
570*591e1cf0SChin-Ting Kuo
571*591e1cf0SChin-Ting Kuo if (wlock)
572*591e1cf0SChin-Ting Kuo ret = spi_flash_ctrl_wlock(flash, (u32)offset, (size_t)len);
573*591e1cf0SChin-Ting Kuo else
574*591e1cf0SChin-Ting Kuo ret = spi_flash_ctrl_wunlock(flash, (u32)offset, (size_t)len);
575*591e1cf0SChin-Ting Kuo
576*591e1cf0SChin-Ting Kuo return ret == 0 ? 0 : 1;
577*591e1cf0SChin-Ting Kuo }
578*591e1cf0SChin-Ting Kuo #endif
579*591e1cf0SChin-Ting Kuo
do_spi_flash(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])5802e192b24SSimon Glass static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc,
5812e192b24SSimon Glass char * const argv[])
5822e192b24SSimon Glass {
5832e192b24SSimon Glass const char *cmd;
5842e192b24SSimon Glass int ret;
5852e192b24SSimon Glass
5862e192b24SSimon Glass /* need at least two arguments */
5872e192b24SSimon Glass if (argc < 2)
5882e192b24SSimon Glass goto usage;
5892e192b24SSimon Glass
5902e192b24SSimon Glass cmd = argv[1];
5912e192b24SSimon Glass --argc;
5922e192b24SSimon Glass ++argv;
5932e192b24SSimon Glass
5942e192b24SSimon Glass if (strcmp(cmd, "probe") == 0) {
5952e192b24SSimon Glass ret = do_spi_flash_probe(argc, argv);
5962e192b24SSimon Glass goto done;
5972e192b24SSimon Glass }
5982e192b24SSimon Glass
5992e192b24SSimon Glass /* The remaining commands require a selected device */
6002e192b24SSimon Glass if (!flash) {
6012e192b24SSimon Glass puts("No SPI flash selected. Please run `sf probe'\n");
6022e192b24SSimon Glass return 1;
6032e192b24SSimon Glass }
6042e192b24SSimon Glass
6052e192b24SSimon Glass if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 ||
6062e192b24SSimon Glass strcmp(cmd, "update") == 0)
6072e192b24SSimon Glass ret = do_spi_flash_read_write(argc, argv);
6082e192b24SSimon Glass else if (strcmp(cmd, "erase") == 0)
6092e192b24SSimon Glass ret = do_spi_flash_erase(argc, argv);
6102e192b24SSimon Glass else if (strcmp(cmd, "protect") == 0)
6112e192b24SSimon Glass ret = do_spi_protect(argc, argv);
6122e192b24SSimon Glass #ifdef CONFIG_CMD_SF_TEST
6132e192b24SSimon Glass else if (!strcmp(cmd, "test"))
6142e192b24SSimon Glass ret = do_spi_flash_test(argc, argv);
6152e192b24SSimon Glass #endif
616*591e1cf0SChin-Ting Kuo #ifdef CONFIG_ASPEED_SPI_FLASH_WRITE_PROTECTION
617*591e1cf0SChin-Ting Kuo else if (!strcmp(cmd, "ctrl_wlock"))
618*591e1cf0SChin-Ting Kuo ret = do_spi_flash_ctrl_wlock(argc, argv);
619*591e1cf0SChin-Ting Kuo #endif
6202e192b24SSimon Glass else
6212e192b24SSimon Glass ret = -1;
6222e192b24SSimon Glass
6232e192b24SSimon Glass done:
6242e192b24SSimon Glass if (ret != -1)
6252e192b24SSimon Glass return ret;
6262e192b24SSimon Glass
6272e192b24SSimon Glass usage:
6282e192b24SSimon Glass return CMD_RET_USAGE;
6292e192b24SSimon Glass }
6302e192b24SSimon Glass
6312e192b24SSimon Glass #ifdef CONFIG_CMD_SF_TEST
6322e192b24SSimon Glass #define SF_TEST_HELP "\nsf test offset len " \
6332e192b24SSimon Glass "- run a very basic destructive test"
6342e192b24SSimon Glass #else
6352e192b24SSimon Glass #define SF_TEST_HELP
6362e192b24SSimon Glass #endif
6372e192b24SSimon Glass
6382e192b24SSimon Glass U_BOOT_CMD(
6392e192b24SSimon Glass sf, 5, 1, do_spi_flash,
6402e192b24SSimon Glass "SPI flash sub-system",
6412e192b24SSimon Glass "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n"
6422e192b24SSimon Glass " and chip select\n"
6432e192b24SSimon Glass "sf read addr offset|partition len - read `len' bytes starting at\n"
6442e192b24SSimon Glass " `offset' or from start of mtd\n"
6452e192b24SSimon Glass " `partition'to memory at `addr'\n"
6462e192b24SSimon Glass "sf write addr offset|partition len - write `len' bytes from memory\n"
6472e192b24SSimon Glass " at `addr' to flash at `offset'\n"
6482e192b24SSimon Glass " or to start of mtd `partition'\n"
6492e192b24SSimon Glass "sf erase offset|partition [+]len - erase `len' bytes from `offset'\n"
6502e192b24SSimon Glass " or from start of mtd `partition'\n"
6512e192b24SSimon Glass " `+len' round up `len' to block size\n"
6522e192b24SSimon Glass "sf update addr offset|partition len - erase and write `len' bytes from memory\n"
6532e192b24SSimon Glass " at `addr' to flash at `offset'\n"
6542e192b24SSimon Glass " or to start of mtd `partition'\n"
6552e192b24SSimon Glass "sf protect lock/unlock sector len - protect/unprotect 'len' bytes starting\n"
6562e192b24SSimon Glass " at address 'sector'\n"
6572e192b24SSimon Glass SF_TEST_HELP
6582e192b24SSimon Glass );
659