xref: /openbmc/u-boot/cmd/sf.c (revision 96907c0fe50a856f66f60ade68864a2d7949bf15)
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;
91*96907c0fSVignesh R 	/* In DM mode defaults will be taken from DT */
92*96907c0fSVignesh 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) {
1272e192b24SSimon Glass 		device_remove(new);
1282e192b24SSimon Glass 		device_unbind(new);
1292e192b24SSimon Glass 	}
1302e192b24SSimon Glass 	flash = NULL;
1312e192b24SSimon Glass 	ret = spi_flash_probe_bus_cs(bus, cs, speed, mode, &new);
1322e192b24SSimon Glass 	if (ret) {
1332e192b24SSimon Glass 		printf("Failed to initialize SPI flash at %u:%u (error %d)\n",
1342e192b24SSimon Glass 		       bus, cs, ret);
1352e192b24SSimon Glass 		return 1;
1362e192b24SSimon Glass 	}
1372e192b24SSimon Glass 
1382e192b24SSimon Glass 	flash = dev_get_uclass_priv(new);
1392e192b24SSimon Glass #else
1402e192b24SSimon Glass 	if (flash)
1412e192b24SSimon Glass 		spi_flash_free(flash);
1422e192b24SSimon Glass 
1432e192b24SSimon Glass 	new = spi_flash_probe(bus, cs, speed, mode);
1442e192b24SSimon Glass 	flash = new;
1452e192b24SSimon Glass 
1462e192b24SSimon Glass 	if (!new) {
1472e192b24SSimon Glass 		printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
1482e192b24SSimon Glass 		return 1;
1492e192b24SSimon Glass 	}
1502e192b24SSimon Glass 
1512e192b24SSimon Glass 	flash = new;
1522e192b24SSimon Glass #endif
1532e192b24SSimon Glass 
1542e192b24SSimon Glass 	return 0;
1552e192b24SSimon Glass }
1562e192b24SSimon Glass 
1572e192b24SSimon Glass /**
1582e192b24SSimon Glass  * Write a block of data to SPI flash, first checking if it is different from
1592e192b24SSimon Glass  * what is already there.
1602e192b24SSimon Glass  *
1612e192b24SSimon Glass  * If the data being written is the same, then *skipped is incremented by len.
1622e192b24SSimon Glass  *
1632e192b24SSimon Glass  * @param flash		flash context pointer
1642e192b24SSimon Glass  * @param offset	flash offset to write
1652e192b24SSimon Glass  * @param len		number of bytes to write
1662e192b24SSimon Glass  * @param buf		buffer to write from
1672e192b24SSimon Glass  * @param cmp_buf	read buffer to use to compare data
1682e192b24SSimon Glass  * @param skipped	Count of skipped data (incremented by this function)
1692e192b24SSimon Glass  * @return NULL if OK, else a string containing the stage which failed
1702e192b24SSimon Glass  */
1712e192b24SSimon Glass static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
1722e192b24SSimon Glass 		size_t len, const char *buf, char *cmp_buf, size_t *skipped)
1732e192b24SSimon Glass {
1742e192b24SSimon Glass 	char *ptr = (char *)buf;
1752e192b24SSimon Glass 
1762e192b24SSimon Glass 	debug("offset=%#x, sector_size=%#x, len=%#zx\n",
1772e192b24SSimon Glass 	      offset, flash->sector_size, len);
1782e192b24SSimon Glass 	/* Read the entire sector so to allow for rewriting */
1792e192b24SSimon Glass 	if (spi_flash_read(flash, offset, flash->sector_size, cmp_buf))
1802e192b24SSimon Glass 		return "read";
1812e192b24SSimon Glass 	/* Compare only what is meaningful (len) */
1822e192b24SSimon Glass 	if (memcmp(cmp_buf, buf, len) == 0) {
1832e192b24SSimon Glass 		debug("Skip region %x size %zx: no change\n",
1842e192b24SSimon Glass 		      offset, len);
1852e192b24SSimon Glass 		*skipped += len;
1862e192b24SSimon Glass 		return NULL;
1872e192b24SSimon Glass 	}
1882e192b24SSimon Glass 	/* Erase the entire sector */
1892e192b24SSimon Glass 	if (spi_flash_erase(flash, offset, flash->sector_size))
1902e192b24SSimon Glass 		return "erase";
1912e192b24SSimon Glass 	/* If it's a partial sector, copy the data into the temp-buffer */
1922e192b24SSimon Glass 	if (len != flash->sector_size) {
1932e192b24SSimon Glass 		memcpy(cmp_buf, buf, len);
1942e192b24SSimon Glass 		ptr = cmp_buf;
1952e192b24SSimon Glass 	}
1962e192b24SSimon Glass 	/* Write one complete sector */
1972e192b24SSimon Glass 	if (spi_flash_write(flash, offset, flash->sector_size, ptr))
1982e192b24SSimon Glass 		return "write";
1992e192b24SSimon Glass 
2002e192b24SSimon Glass 	return NULL;
2012e192b24SSimon Glass }
2022e192b24SSimon Glass 
2032e192b24SSimon Glass /**
2042e192b24SSimon Glass  * Update an area of SPI flash by erasing and writing any blocks which need
2052e192b24SSimon Glass  * to change. Existing blocks with the correct data are left unchanged.
2062e192b24SSimon Glass  *
2072e192b24SSimon Glass  * @param flash		flash context pointer
2082e192b24SSimon Glass  * @param offset	flash offset to write
2092e192b24SSimon Glass  * @param len		number of bytes to write
2102e192b24SSimon Glass  * @param buf		buffer to write from
2112e192b24SSimon Glass  * @return 0 if ok, 1 on error
2122e192b24SSimon Glass  */
2132e192b24SSimon Glass static int spi_flash_update(struct spi_flash *flash, u32 offset,
2142e192b24SSimon Glass 		size_t len, const char *buf)
2152e192b24SSimon Glass {
2162e192b24SSimon Glass 	const char *err_oper = NULL;
2172e192b24SSimon Glass 	char *cmp_buf;
2182e192b24SSimon Glass 	const char *end = buf + len;
2192e192b24SSimon Glass 	size_t todo;		/* number of bytes to do in this pass */
2202e192b24SSimon Glass 	size_t skipped = 0;	/* statistics */
2212e192b24SSimon Glass 	const ulong start_time = get_timer(0);
2222e192b24SSimon Glass 	size_t scale = 1;
2232e192b24SSimon Glass 	const char *start_buf = buf;
2242e192b24SSimon Glass 	ulong delta;
2252e192b24SSimon Glass 
2262e192b24SSimon Glass 	if (end - buf >= 200)
2272e192b24SSimon Glass 		scale = (end - buf) / 100;
2282e192b24SSimon Glass 	cmp_buf = memalign(ARCH_DMA_MINALIGN, flash->sector_size);
2292e192b24SSimon Glass 	if (cmp_buf) {
2302e192b24SSimon Glass 		ulong last_update = get_timer(0);
2312e192b24SSimon Glass 
2322e192b24SSimon Glass 		for (; buf < end && !err_oper; buf += todo, offset += todo) {
2332e192b24SSimon Glass 			todo = min_t(size_t, end - buf, flash->sector_size);
2342e192b24SSimon Glass 			if (get_timer(last_update) > 100) {
2352e192b24SSimon Glass 				printf("   \rUpdating, %zu%% %lu B/s",
2362e192b24SSimon Glass 				       100 - (end - buf) / scale,
2372e192b24SSimon Glass 					bytes_per_second(buf - start_buf,
2382e192b24SSimon Glass 							 start_time));
2392e192b24SSimon Glass 				last_update = get_timer(0);
2402e192b24SSimon Glass 			}
2412e192b24SSimon Glass 			err_oper = spi_flash_update_block(flash, offset, todo,
2422e192b24SSimon Glass 					buf, cmp_buf, &skipped);
2432e192b24SSimon Glass 		}
2442e192b24SSimon Glass 	} else {
2452e192b24SSimon Glass 		err_oper = "malloc";
2462e192b24SSimon Glass 	}
2472e192b24SSimon Glass 	free(cmp_buf);
2482e192b24SSimon Glass 	putc('\r');
2492e192b24SSimon Glass 	if (err_oper) {
2502e192b24SSimon Glass 		printf("SPI flash failed in %s step\n", err_oper);
2512e192b24SSimon Glass 		return 1;
2522e192b24SSimon Glass 	}
2532e192b24SSimon Glass 
2542e192b24SSimon Glass 	delta = get_timer(start_time);
2552e192b24SSimon Glass 	printf("%zu bytes written, %zu bytes skipped", len - skipped,
2562e192b24SSimon Glass 	       skipped);
2572e192b24SSimon Glass 	printf(" in %ld.%lds, speed %ld B/s\n",
2582e192b24SSimon Glass 	       delta / 1000, delta % 1000, bytes_per_second(len, start_time));
2592e192b24SSimon Glass 
2602e192b24SSimon Glass 	return 0;
2612e192b24SSimon Glass }
2622e192b24SSimon Glass 
2632e192b24SSimon Glass static int do_spi_flash_read_write(int argc, char * const argv[])
2642e192b24SSimon Glass {
2652e192b24SSimon Glass 	unsigned long addr;
2662e192b24SSimon Glass 	void *buf;
2672e192b24SSimon Glass 	char *endp;
2682e192b24SSimon Glass 	int ret = 1;
2692e192b24SSimon Glass 	int dev = 0;
2702e192b24SSimon Glass 	loff_t offset, len, maxsize;
2712e192b24SSimon Glass 
2722e192b24SSimon Glass 	if (argc < 3)
2732e192b24SSimon Glass 		return -1;
2742e192b24SSimon Glass 
2752e192b24SSimon Glass 	addr = simple_strtoul(argv[1], &endp, 16);
2762e192b24SSimon Glass 	if (*argv[1] == 0 || *endp != 0)
2772e192b24SSimon Glass 		return -1;
2782e192b24SSimon Glass 
2792e192b24SSimon Glass 	if (mtd_arg_off_size(argc - 2, &argv[2], &dev, &offset, &len,
2802e192b24SSimon Glass 			     &maxsize, MTD_DEV_TYPE_NOR, flash->size))
2812e192b24SSimon Glass 		return -1;
2822e192b24SSimon Glass 
2832e192b24SSimon Glass 	/* Consistency checking */
2842e192b24SSimon Glass 	if (offset + len > flash->size) {
2852e192b24SSimon Glass 		printf("ERROR: attempting %s past flash size (%#x)\n",
2862e192b24SSimon Glass 		       argv[0], flash->size);
2872e192b24SSimon Glass 		return 1;
2882e192b24SSimon Glass 	}
2892e192b24SSimon Glass 
2902e192b24SSimon Glass 	buf = map_physmem(addr, len, MAP_WRBACK);
2912e192b24SSimon Glass 	if (!buf) {
2922e192b24SSimon Glass 		puts("Failed to map physical memory\n");
2932e192b24SSimon Glass 		return 1;
2942e192b24SSimon Glass 	}
2952e192b24SSimon Glass 
2962e192b24SSimon Glass 	if (strcmp(argv[0], "update") == 0) {
2972e192b24SSimon Glass 		ret = spi_flash_update(flash, offset, len, buf);
2982e192b24SSimon Glass 	} else if (strncmp(argv[0], "read", 4) == 0 ||
2992e192b24SSimon Glass 			strncmp(argv[0], "write", 5) == 0) {
3002e192b24SSimon Glass 		int read;
3012e192b24SSimon Glass 
3022e192b24SSimon Glass 		read = strncmp(argv[0], "read", 4) == 0;
3032e192b24SSimon Glass 		if (read)
3042e192b24SSimon Glass 			ret = spi_flash_read(flash, offset, len, buf);
3052e192b24SSimon Glass 		else
3062e192b24SSimon Glass 			ret = spi_flash_write(flash, offset, len, buf);
3072e192b24SSimon Glass 
3082e192b24SSimon Glass 		printf("SF: %zu bytes @ %#x %s: ", (size_t)len, (u32)offset,
3092e192b24SSimon Glass 		       read ? "Read" : "Written");
3102e192b24SSimon Glass 		if (ret)
3112e192b24SSimon Glass 			printf("ERROR %d\n", ret);
3122e192b24SSimon Glass 		else
3132e192b24SSimon Glass 			printf("OK\n");
3142e192b24SSimon Glass 	}
3152e192b24SSimon Glass 
3162e192b24SSimon Glass 	unmap_physmem(buf, len);
3172e192b24SSimon Glass 
3182e192b24SSimon Glass 	return ret == 0 ? 0 : 1;
3192e192b24SSimon Glass }
3202e192b24SSimon Glass 
3212e192b24SSimon Glass static int do_spi_flash_erase(int argc, char * const argv[])
3222e192b24SSimon Glass {
3232e192b24SSimon Glass 	int ret;
3242e192b24SSimon Glass 	int dev = 0;
3252e192b24SSimon Glass 	loff_t offset, len, maxsize;
3262e192b24SSimon Glass 	ulong size;
3272e192b24SSimon Glass 
3282e192b24SSimon Glass 	if (argc < 3)
3292e192b24SSimon Glass 		return -1;
3302e192b24SSimon Glass 
3312e192b24SSimon Glass 	if (mtd_arg_off(argv[1], &dev, &offset, &len, &maxsize,
3322e192b24SSimon Glass 			MTD_DEV_TYPE_NOR, flash->size))
3332e192b24SSimon Glass 		return -1;
3342e192b24SSimon Glass 
3352e192b24SSimon Glass 	ret = sf_parse_len_arg(argv[2], &size);
3362e192b24SSimon Glass 	if (ret != 1)
3372e192b24SSimon Glass 		return -1;
3382e192b24SSimon Glass 
3392e192b24SSimon Glass 	/* Consistency checking */
3402e192b24SSimon Glass 	if (offset + size > flash->size) {
3412e192b24SSimon Glass 		printf("ERROR: attempting %s past flash size (%#x)\n",
3422e192b24SSimon Glass 		       argv[0], flash->size);
3432e192b24SSimon Glass 		return 1;
3442e192b24SSimon Glass 	}
3452e192b24SSimon Glass 
3462e192b24SSimon Glass 	ret = spi_flash_erase(flash, offset, size);
3472e192b24SSimon Glass 	printf("SF: %zu bytes @ %#x Erased: %s\n", (size_t)size, (u32)offset,
3482e192b24SSimon Glass 	       ret ? "ERROR" : "OK");
3492e192b24SSimon Glass 
3502e192b24SSimon Glass 	return ret == 0 ? 0 : 1;
3512e192b24SSimon Glass }
3522e192b24SSimon Glass 
3532e192b24SSimon Glass static int do_spi_protect(int argc, char * const argv[])
3542e192b24SSimon Glass {
3552e192b24SSimon Glass 	int ret = 0;
3562e192b24SSimon Glass 	loff_t start, len;
3572e192b24SSimon Glass 	bool prot = false;
3582e192b24SSimon Glass 
3592e192b24SSimon Glass 	if (argc != 4)
3602e192b24SSimon Glass 		return -1;
3612e192b24SSimon Glass 
3622e192b24SSimon Glass 	if (!str2off(argv[2], &start)) {
3632e192b24SSimon Glass 		puts("start sector is not a valid number\n");
3642e192b24SSimon Glass 		return 1;
3652e192b24SSimon Glass 	}
3662e192b24SSimon Glass 
3672e192b24SSimon Glass 	if (!str2off(argv[3], &len)) {
3682e192b24SSimon Glass 		puts("len is not a valid number\n");
3692e192b24SSimon Glass 		return 1;
3702e192b24SSimon Glass 	}
3712e192b24SSimon Glass 
3722e192b24SSimon Glass 	if (strcmp(argv[1], "lock") == 0)
3732e192b24SSimon Glass 		prot = true;
3742e192b24SSimon Glass 	else if (strcmp(argv[1], "unlock") == 0)
3752e192b24SSimon Glass 		prot = false;
3762e192b24SSimon Glass 	else
3772e192b24SSimon Glass 		return -1;  /* Unknown parameter */
3782e192b24SSimon Glass 
3792e192b24SSimon Glass 	ret = spi_flash_protect(flash, start, len, prot);
3802e192b24SSimon Glass 
3812e192b24SSimon Glass 	return ret == 0 ? 0 : 1;
3822e192b24SSimon Glass }
3832e192b24SSimon Glass 
3842e192b24SSimon Glass #ifdef CONFIG_CMD_SF_TEST
3852e192b24SSimon Glass enum {
3862e192b24SSimon Glass 	STAGE_ERASE,
3872e192b24SSimon Glass 	STAGE_CHECK,
3882e192b24SSimon Glass 	STAGE_WRITE,
3892e192b24SSimon Glass 	STAGE_READ,
3902e192b24SSimon Glass 
3912e192b24SSimon Glass 	STAGE_COUNT,
3922e192b24SSimon Glass };
3932e192b24SSimon Glass 
3942e192b24SSimon Glass static char *stage_name[STAGE_COUNT] = {
3952e192b24SSimon Glass 	"erase",
3962e192b24SSimon Glass 	"check",
3972e192b24SSimon Glass 	"write",
3982e192b24SSimon Glass 	"read",
3992e192b24SSimon Glass };
4002e192b24SSimon Glass 
4012e192b24SSimon Glass struct test_info {
4022e192b24SSimon Glass 	int stage;
4032e192b24SSimon Glass 	int bytes;
4042e192b24SSimon Glass 	unsigned base_ms;
4052e192b24SSimon Glass 	unsigned time_ms[STAGE_COUNT];
4062e192b24SSimon Glass };
4072e192b24SSimon Glass 
4082e192b24SSimon Glass static void show_time(struct test_info *test, int stage)
4092e192b24SSimon Glass {
4102e192b24SSimon Glass 	uint64_t speed;	/* KiB/s */
4112e192b24SSimon Glass 	int bps;	/* Bits per second */
4122e192b24SSimon Glass 
4132e192b24SSimon Glass 	speed = (long long)test->bytes * 1000;
4142e192b24SSimon Glass 	if (test->time_ms[stage])
4152e192b24SSimon Glass 		do_div(speed, test->time_ms[stage] * 1024);
4162e192b24SSimon Glass 	bps = speed * 8;
4172e192b24SSimon Glass 
4182e192b24SSimon Glass 	printf("%d %s: %d ticks, %d KiB/s %d.%03d Mbps\n", stage,
4192e192b24SSimon Glass 	       stage_name[stage], test->time_ms[stage],
4202e192b24SSimon Glass 	       (int)speed, bps / 1000, bps % 1000);
4212e192b24SSimon Glass }
4222e192b24SSimon Glass 
4232e192b24SSimon Glass static void spi_test_next_stage(struct test_info *test)
4242e192b24SSimon Glass {
4252e192b24SSimon Glass 	test->time_ms[test->stage] = get_timer(test->base_ms);
4262e192b24SSimon Glass 	show_time(test, test->stage);
4272e192b24SSimon Glass 	test->base_ms = get_timer(0);
4282e192b24SSimon Glass 	test->stage++;
4292e192b24SSimon Glass }
4302e192b24SSimon Glass 
4312e192b24SSimon Glass /**
4322e192b24SSimon Glass  * Run a test on the SPI flash
4332e192b24SSimon Glass  *
4342e192b24SSimon Glass  * @param flash		SPI flash to use
4352e192b24SSimon Glass  * @param buf		Source buffer for data to write
4362e192b24SSimon Glass  * @param len		Size of data to read/write
4372e192b24SSimon Glass  * @param offset	Offset within flash to check
4382e192b24SSimon Glass  * @param vbuf		Verification buffer
4392e192b24SSimon Glass  * @return 0 if ok, -1 on error
4402e192b24SSimon Glass  */
4412e192b24SSimon Glass static int spi_flash_test(struct spi_flash *flash, uint8_t *buf, ulong len,
4422e192b24SSimon Glass 			   ulong offset, uint8_t *vbuf)
4432e192b24SSimon Glass {
4442e192b24SSimon Glass 	struct test_info test;
4452e192b24SSimon Glass 	int i;
4462e192b24SSimon Glass 
4472e192b24SSimon Glass 	printf("SPI flash test:\n");
4482e192b24SSimon Glass 	memset(&test, '\0', sizeof(test));
4492e192b24SSimon Glass 	test.base_ms = get_timer(0);
4502e192b24SSimon Glass 	test.bytes = len;
4512e192b24SSimon Glass 	if (spi_flash_erase(flash, offset, len)) {
4522e192b24SSimon Glass 		printf("Erase failed\n");
4532e192b24SSimon Glass 		return -1;
4542e192b24SSimon Glass 	}
4552e192b24SSimon Glass 	spi_test_next_stage(&test);
4562e192b24SSimon Glass 
4572e192b24SSimon Glass 	if (spi_flash_read(flash, offset, len, vbuf)) {
4582e192b24SSimon Glass 		printf("Check read failed\n");
4592e192b24SSimon Glass 		return -1;
4602e192b24SSimon Glass 	}
4612e192b24SSimon Glass 	for (i = 0; i < len; i++) {
4622e192b24SSimon Glass 		if (vbuf[i] != 0xff) {
4632e192b24SSimon Glass 			printf("Check failed at %d\n", i);
4642e192b24SSimon Glass 			print_buffer(i, vbuf + i, 1,
4652e192b24SSimon Glass 				     min_t(uint, len - i, 0x40), 0);
4662e192b24SSimon Glass 			return -1;
4672e192b24SSimon Glass 		}
4682e192b24SSimon Glass 	}
4692e192b24SSimon Glass 	spi_test_next_stage(&test);
4702e192b24SSimon Glass 
4712e192b24SSimon Glass 	if (spi_flash_write(flash, offset, len, buf)) {
4722e192b24SSimon Glass 		printf("Write failed\n");
4732e192b24SSimon Glass 		return -1;
4742e192b24SSimon Glass 	}
4752e192b24SSimon Glass 	memset(vbuf, '\0', len);
4762e192b24SSimon Glass 	spi_test_next_stage(&test);
4772e192b24SSimon Glass 
4782e192b24SSimon Glass 	if (spi_flash_read(flash, offset, len, vbuf)) {
4792e192b24SSimon Glass 		printf("Read failed\n");
4802e192b24SSimon Glass 		return -1;
4812e192b24SSimon Glass 	}
4822e192b24SSimon Glass 	spi_test_next_stage(&test);
4832e192b24SSimon Glass 
4842e192b24SSimon Glass 	for (i = 0; i < len; i++) {
4852e192b24SSimon Glass 		if (buf[i] != vbuf[i]) {
4862e192b24SSimon Glass 			printf("Verify failed at %d, good data:\n", i);
4872e192b24SSimon Glass 			print_buffer(i, buf + i, 1,
4882e192b24SSimon Glass 				     min_t(uint, len - i, 0x40), 0);
4892e192b24SSimon Glass 			printf("Bad data:\n");
4902e192b24SSimon Glass 			print_buffer(i, vbuf + i, 1,
4912e192b24SSimon Glass 				     min_t(uint, len - i, 0x40), 0);
4922e192b24SSimon Glass 			return -1;
4932e192b24SSimon Glass 		}
4942e192b24SSimon Glass 	}
4952e192b24SSimon Glass 	printf("Test passed\n");
4962e192b24SSimon Glass 	for (i = 0; i < STAGE_COUNT; i++)
4972e192b24SSimon Glass 		show_time(&test, i);
4982e192b24SSimon Glass 
4992e192b24SSimon Glass 	return 0;
5002e192b24SSimon Glass }
5012e192b24SSimon Glass 
5022e192b24SSimon Glass static int do_spi_flash_test(int argc, char * const argv[])
5032e192b24SSimon Glass {
5042e192b24SSimon Glass 	unsigned long offset;
5052e192b24SSimon Glass 	unsigned long len;
5062e192b24SSimon Glass 	uint8_t *buf, *from;
5072e192b24SSimon Glass 	char *endp;
5082e192b24SSimon Glass 	uint8_t *vbuf;
5092e192b24SSimon Glass 	int ret;
5102e192b24SSimon Glass 
5112e192b24SSimon Glass 	if (argc < 3)
5122e192b24SSimon Glass 		return -1;
5132e192b24SSimon Glass 	offset = simple_strtoul(argv[1], &endp, 16);
5142e192b24SSimon Glass 	if (*argv[1] == 0 || *endp != 0)
5152e192b24SSimon Glass 		return -1;
5162e192b24SSimon Glass 	len = simple_strtoul(argv[2], &endp, 16);
5172e192b24SSimon Glass 	if (*argv[2] == 0 || *endp != 0)
5182e192b24SSimon Glass 		return -1;
5192e192b24SSimon Glass 
5202e192b24SSimon Glass 	vbuf = memalign(ARCH_DMA_MINALIGN, len);
5212e192b24SSimon Glass 	if (!vbuf) {
5222e192b24SSimon Glass 		printf("Cannot allocate memory (%lu bytes)\n", len);
5232e192b24SSimon Glass 		return 1;
5242e192b24SSimon Glass 	}
5252e192b24SSimon Glass 	buf = memalign(ARCH_DMA_MINALIGN, len);
5262e192b24SSimon Glass 	if (!buf) {
5272e192b24SSimon Glass 		free(vbuf);
5282e192b24SSimon Glass 		printf("Cannot allocate memory (%lu bytes)\n", len);
5292e192b24SSimon Glass 		return 1;
5302e192b24SSimon Glass 	}
5312e192b24SSimon Glass 
5322e192b24SSimon Glass 	from = map_sysmem(CONFIG_SYS_TEXT_BASE, 0);
5332e192b24SSimon Glass 	memcpy(buf, from, len);
5342e192b24SSimon Glass 	ret = spi_flash_test(flash, buf, len, offset, vbuf);
5352e192b24SSimon Glass 	free(vbuf);
5362e192b24SSimon Glass 	free(buf);
5372e192b24SSimon Glass 	if (ret) {
5382e192b24SSimon Glass 		printf("Test failed\n");
5392e192b24SSimon Glass 		return 1;
5402e192b24SSimon Glass 	}
5412e192b24SSimon Glass 
5422e192b24SSimon Glass 	return 0;
5432e192b24SSimon Glass }
5442e192b24SSimon Glass #endif /* CONFIG_CMD_SF_TEST */
5452e192b24SSimon Glass 
5462e192b24SSimon Glass static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc,
5472e192b24SSimon Glass 			char * const argv[])
5482e192b24SSimon Glass {
5492e192b24SSimon Glass 	const char *cmd;
5502e192b24SSimon Glass 	int ret;
5512e192b24SSimon Glass 
5522e192b24SSimon Glass 	/* need at least two arguments */
5532e192b24SSimon Glass 	if (argc < 2)
5542e192b24SSimon Glass 		goto usage;
5552e192b24SSimon Glass 
5562e192b24SSimon Glass 	cmd = argv[1];
5572e192b24SSimon Glass 	--argc;
5582e192b24SSimon Glass 	++argv;
5592e192b24SSimon Glass 
5602e192b24SSimon Glass 	if (strcmp(cmd, "probe") == 0) {
5612e192b24SSimon Glass 		ret = do_spi_flash_probe(argc, argv);
5622e192b24SSimon Glass 		goto done;
5632e192b24SSimon Glass 	}
5642e192b24SSimon Glass 
5652e192b24SSimon Glass 	/* The remaining commands require a selected device */
5662e192b24SSimon Glass 	if (!flash) {
5672e192b24SSimon Glass 		puts("No SPI flash selected. Please run `sf probe'\n");
5682e192b24SSimon Glass 		return 1;
5692e192b24SSimon Glass 	}
5702e192b24SSimon Glass 
5712e192b24SSimon Glass 	if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 ||
5722e192b24SSimon Glass 	    strcmp(cmd, "update") == 0)
5732e192b24SSimon Glass 		ret = do_spi_flash_read_write(argc, argv);
5742e192b24SSimon Glass 	else if (strcmp(cmd, "erase") == 0)
5752e192b24SSimon Glass 		ret = do_spi_flash_erase(argc, argv);
5762e192b24SSimon Glass 	else if (strcmp(cmd, "protect") == 0)
5772e192b24SSimon Glass 		ret = do_spi_protect(argc, argv);
5782e192b24SSimon Glass #ifdef CONFIG_CMD_SF_TEST
5792e192b24SSimon Glass 	else if (!strcmp(cmd, "test"))
5802e192b24SSimon Glass 		ret = do_spi_flash_test(argc, argv);
5812e192b24SSimon Glass #endif
5822e192b24SSimon Glass 	else
5832e192b24SSimon Glass 		ret = -1;
5842e192b24SSimon Glass 
5852e192b24SSimon Glass done:
5862e192b24SSimon Glass 	if (ret != -1)
5872e192b24SSimon Glass 		return ret;
5882e192b24SSimon Glass 
5892e192b24SSimon Glass usage:
5902e192b24SSimon Glass 	return CMD_RET_USAGE;
5912e192b24SSimon Glass }
5922e192b24SSimon Glass 
5932e192b24SSimon Glass #ifdef CONFIG_CMD_SF_TEST
5942e192b24SSimon Glass #define SF_TEST_HELP "\nsf test offset len		" \
5952e192b24SSimon Glass 		"- run a very basic destructive test"
5962e192b24SSimon Glass #else
5972e192b24SSimon Glass #define SF_TEST_HELP
5982e192b24SSimon Glass #endif
5992e192b24SSimon Glass 
6002e192b24SSimon Glass U_BOOT_CMD(
6012e192b24SSimon Glass 	sf,	5,	1,	do_spi_flash,
6022e192b24SSimon Glass 	"SPI flash sub-system",
6032e192b24SSimon Glass 	"probe [[bus:]cs] [hz] [mode]	- init flash device on given SPI bus\n"
6042e192b24SSimon Glass 	"				  and chip select\n"
6052e192b24SSimon Glass 	"sf read addr offset|partition len	- read `len' bytes starting at\n"
6062e192b24SSimon Glass 	"				          `offset' or from start of mtd\n"
6072e192b24SSimon Glass 	"					  `partition'to memory at `addr'\n"
6082e192b24SSimon Glass 	"sf write addr offset|partition len	- write `len' bytes from memory\n"
6092e192b24SSimon Glass 	"				          at `addr' to flash at `offset'\n"
6102e192b24SSimon Glass 	"					  or to start of mtd `partition'\n"
6112e192b24SSimon Glass 	"sf erase offset|partition [+]len	- erase `len' bytes from `offset'\n"
6122e192b24SSimon Glass 	"					  or from start of mtd `partition'\n"
6132e192b24SSimon Glass 	"					 `+len' round up `len' to block size\n"
6142e192b24SSimon Glass 	"sf update addr offset|partition len	- erase and write `len' bytes from memory\n"
6152e192b24SSimon Glass 	"					  at `addr' to flash at `offset'\n"
6162e192b24SSimon Glass 	"					  or to start of mtd `partition'\n"
6172e192b24SSimon Glass 	"sf protect lock/unlock sector len	- protect/unprotect 'len' bytes starting\n"
6182e192b24SSimon Glass 	"					  at address 'sector'\n"
6192e192b24SSimon Glass 	SF_TEST_HELP
6202e192b24SSimon Glass );
621