Merge https://gitlab.denx.de/u-boot/custodians/u-boot-sh
[platform/kernel/u-boot.git] / cmd / sf.c
index e7ff9a6..eac27ed 100644 (file)
--- a/cmd/sf.c
+++ b/cmd/sf.c
@@ -1,24 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Command for accessing SPI flash.
  *
  * Copyright (C) 2008 Atmel Corporation
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
+#include <command.h>
 #include <div64.h>
 #include <dm.h>
+#include <flash.h>
+#include <log.h>
 #include <malloc.h>
 #include <mapmem.h>
 #include <spi.h>
 #include <spi_flash.h>
+#include <asm/cache.h>
 #include <jffs2/jffs2.h>
 #include <linux/mtd/mtd.h>
 
 #include <asm/io.h>
 #include <dm/device-internal.h>
 
+#include "legacy-mtd-utils.h"
+
 static struct spi_flash *flash;
 
 /*
@@ -49,7 +54,7 @@ static int sf_parse_len_arg(char *arg, ulong *len)
                ++arg;
        }
 
-       len_arg = simple_strtoul(arg, &ep, 16);
+       len_arg = hextoul(arg, &ep);
        if (ep == arg || *ep != '\0')
                return -1;
 
@@ -78,18 +83,17 @@ static ulong bytes_per_second(unsigned int len, ulong start_ms)
                return 1024 * len / max(get_timer(start_ms), 1UL);
 }
 
-static int do_spi_flash_probe(int argc, char * const argv[])
+static int do_spi_flash_probe(int argc, char *const argv[])
 {
        unsigned int bus = CONFIG_SF_DEFAULT_BUS;
        unsigned int cs = CONFIG_SF_DEFAULT_CS;
+       /* In DM mode, defaults speed and mode will be taken from DT */
        unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
        unsigned int mode = CONFIG_SF_DEFAULT_MODE;
        char *endp;
-#ifdef CONFIG_DM_SPI_FLASH
+#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
        struct udevice *new, *bus_dev;
        int ret;
-       /* In DM mode defaults will be taken from DT */
-       speed = 0, mode = 0;
 #else
        struct spi_flash *new;
 #endif
@@ -115,12 +119,12 @@ static int do_spi_flash_probe(int argc, char * const argv[])
                        return -1;
        }
        if (argc >= 4) {
-               mode = simple_strtoul(argv[3], &endp, 16);
+               mode = hextoul(argv[3], &endp);
                if (*argv[3] == 0 || *endp != 0)
                        return -1;
        }
 
-#ifdef CONFIG_DM_SPI_FLASH
+#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
        /* Remove the old device, otherwise probe will just be a nop */
        ret = spi_find_bus_and_cs(bus, cs, &bus_dev, &new);
        if (!ret) {
@@ -141,13 +145,10 @@ static int do_spi_flash_probe(int argc, char * const argv[])
 
        new = spi_flash_probe(bus, cs, speed, mode);
        flash = new;
-
        if (!new) {
                printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
                return 1;
        }
-
-       flash = new;
 #endif
 
        return 0;
@@ -259,7 +260,7 @@ static int spi_flash_update(struct spi_flash *flash, u32 offset,
        return 0;
 }
 
-static int do_spi_flash_read_write(int argc, char * const argv[])
+static int do_spi_flash_read_write(int argc, char *const argv[])
 {
        unsigned long addr;
        void *buf;
@@ -271,7 +272,7 @@ static int do_spi_flash_read_write(int argc, char * const argv[])
        if (argc < 3)
                return -1;
 
-       addr = simple_strtoul(argv[1], &endp, 16);
+       addr = hextoul(argv[1], &endp);
        if (*argv[1] == 0 || *endp != 0)
                return -1;
 
@@ -317,7 +318,7 @@ static int do_spi_flash_read_write(int argc, char * const argv[])
        return ret == 0 ? 0 : 1;
 }
 
-static int do_spi_flash_erase(int argc, char * const argv[])
+static int do_spi_flash_erase(int argc, char *const argv[])
 {
        int ret;
        int dev = 0;
@@ -343,13 +344,16 @@ static int do_spi_flash_erase(int argc, char * const argv[])
        }
 
        ret = spi_flash_erase(flash, offset, size);
-       printf("SF: %zu bytes @ %#x Erased: %s\n", (size_t)size, (u32)offset,
-              ret ? "ERROR" : "OK");
+       printf("SF: %zu bytes @ %#x Erased: ", (size_t)size, (u32)offset);
+       if (ret)
+               printf("ERROR %d\n", ret);
+       else
+               printf("OK\n");
 
        return ret == 0 ? 0 : 1;
 }
 
-static int do_spi_protect(int argc, char * const argv[])
+static int do_spi_protect(int argc, char *const argv[])
 {
        int ret = 0;
        loff_t start, len;
@@ -414,7 +418,7 @@ static void show_time(struct test_info *test, int stage)
                do_div(speed, test->time_ms[stage] * 1024);
        bps = speed * 8;
 
-       printf("%d %s: %d ticks, %d KiB/s %d.%03d Mbps\n", stage,
+       printf("%d %s: %u ticks, %d KiB/s %d.%03d Mbps\n", stage,
               stage_name[stage], test->time_ms[stage],
               (int)speed, bps / 1000, bps % 1000);
 }
@@ -441,20 +445,22 @@ static int spi_flash_test(struct spi_flash *flash, uint8_t *buf, ulong len,
                           ulong offset, uint8_t *vbuf)
 {
        struct test_info test;
-       int i;
+       int err, i;
 
        printf("SPI flash test:\n");
        memset(&test, '\0', sizeof(test));
        test.base_ms = get_timer(0);
        test.bytes = len;
-       if (spi_flash_erase(flash, offset, len)) {
-               printf("Erase failed\n");
+       err = spi_flash_erase(flash, offset, len);
+       if (err) {
+               printf("Erase failed (err = %d)\n", err);
                return -1;
        }
        spi_test_next_stage(&test);
 
-       if (spi_flash_read(flash, offset, len, vbuf)) {
-               printf("Check read failed\n");
+       err = spi_flash_read(flash, offset, len, vbuf);
+       if (err) {
+               printf("Check read failed (err = %d)\n", err);
                return -1;
        }
        for (i = 0; i < len; i++) {
@@ -467,15 +473,17 @@ static int spi_flash_test(struct spi_flash *flash, uint8_t *buf, ulong len,
        }
        spi_test_next_stage(&test);
 
-       if (spi_flash_write(flash, offset, len, buf)) {
-               printf("Write failed\n");
+       err = spi_flash_write(flash, offset, len, buf);
+       if (err) {
+               printf("Write failed (err = %d)\n", err);
                return -1;
        }
        memset(vbuf, '\0', len);
        spi_test_next_stage(&test);
 
-       if (spi_flash_read(flash, offset, len, vbuf)) {
-               printf("Read failed\n");
+       err = spi_flash_read(flash, offset, len, vbuf);
+       if (err) {
+               printf("Read failed (ret = %d)\n", err);
                return -1;
        }
        spi_test_next_stage(&test);
@@ -498,7 +506,7 @@ static int spi_flash_test(struct spi_flash *flash, uint8_t *buf, ulong len,
        return 0;
 }
 
-static int do_spi_flash_test(int argc, char * const argv[])
+static int do_spi_flash_test(int argc, char *const argv[])
 {
        unsigned long offset;
        unsigned long len;
@@ -509,10 +517,10 @@ static int do_spi_flash_test(int argc, char * const argv[])
 
        if (argc < 3)
                return -1;
-       offset = simple_strtoul(argv[1], &endp, 16);
+       offset = hextoul(argv[1], &endp);
        if (*argv[1] == 0 || *endp != 0)
                return -1;
-       len = simple_strtoul(argv[2], &endp, 16);
+       len = hextoul(argv[2], &endp);
        if (*argv[2] == 0 || *endp != 0)
                return -1;
 
@@ -542,8 +550,8 @@ static int do_spi_flash_test(int argc, char * const argv[])
 }
 #endif /* CONFIG_CMD_SF_TEST */
 
-static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc,
-                       char * const argv[])
+static int do_spi_flash(struct cmd_tbl *cmdtp, int flag, int argc,
+                       char *const argv[])
 {
        const char *cmd;
        int ret;