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