From 560ebe3eb6197322b9d00c8e3cf30fb7e679d8b2 Mon Sep 17 00:00:00 2001
From: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
Date: Thu, 22 Dec 2022 16:20:46 +0000
Subject: [PATCH 33/43] nvmxip: provide a u-boot shell test command

nvmxip command allows probing the NVM XIP devices manually

The command is provided for test purposes only.

Use:

nvmxip probe

Upstream-Status: Submitted
Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
---
 cmd/Kconfig                    |  7 +++++
 cmd/Makefile                   |  1 +
 cmd/nvmxip.c                   | 47 ++++++++++++++++++++++++++++++++++
 configs/corstone1000_defconfig |  1 +
 configs/sandbox_defconfig      |  1 +
 5 files changed, 57 insertions(+)
 create mode 100644 cmd/nvmxip.c

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 5e278ecb1597..b6a3e5908534 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -938,6 +938,13 @@ config CMD_ARMFFA
 		- Sending a data pattern to the specified partition
 		- Displaying the arm_ffa device info
 
+config CMD_NVMXIP
+	bool "NVM XIP probe command"
+	depends on NVMXIP
+	help
+	  Probes all NVM XIP devices. The command is for
+	  test purposes only (not to be upstreamed)
+
 config CMD_ARMFLASH
 	#depends on FLASH_CFI_DRIVER
 	bool "armflash"
diff --git a/cmd/Makefile b/cmd/Makefile
index c757f1647da6..0a3d98100703 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -154,6 +154,7 @@ obj-$(CONFIG_CMD_RTC) += rtc.o
 obj-$(CONFIG_SANDBOX) += host.o
 obj-$(CONFIG_CMD_SATA) += sata.o
 obj-$(CONFIG_CMD_NVME) += nvme.o
+obj-$(CONFIG_CMD_NVMXIP) += nvmxip.o
 obj-$(CONFIG_SANDBOX) += sb.o
 obj-$(CONFIG_CMD_SF) += sf.o
 obj-$(CONFIG_CMD_SCSI) += scsi.o disk.o
diff --git a/cmd/nvmxip.c b/cmd/nvmxip.c
new file mode 100644
index 000000000000..3eb0d84afc04
--- /dev/null
+++ b/cmd/nvmxip.c
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2022 ARM Limited
+ * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
+ */
+
+#include <common.h>
+#include <command.h>
+#include <dm.h>
+
+int do_nvmxip_probe(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	struct udevice *dev = NULL;
+	for (uclass_first_device(UCLASS_NVMXIP, &dev); dev; uclass_next_device(&dev));
+
+	return 0;
+}
+
+static struct cmd_tbl nvmxip_commands[] = {
+	U_BOOT_CMD_MKENT(probe, 1, 1, do_nvmxip_probe, "", ""),
+};
+
+static int do_nvmxip(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	struct cmd_tbl *nvmxip_cmd;
+	int ret;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	nvmxip_cmd = find_cmd_tbl(argv[1], nvmxip_commands, ARRAY_SIZE(nvmxip_commands));
+
+	argc -= 2;
+	argv += 2;
+
+	if (!nvmxip_cmd || argc > nvmxip_cmd->maxargs)
+		return CMD_RET_USAGE;
+
+	ret = nvmxip_cmd->cmd(nvmxip_cmd, flag, argc, argv);
+
+	return cmd_process_error(nvmxip_cmd, ret);
+}
+
+U_BOOT_CMD(nvmxip, 4, 1, do_nvmxip,
+	   "NVM XIP probe command",
+	   "probe\n"
+	"	 - probes all NVM XIP devices\n");
diff --git a/configs/corstone1000_defconfig b/configs/corstone1000_defconfig
index 2986cc95932f..e009faee0252 100644
--- a/configs/corstone1000_defconfig
+++ b/configs/corstone1000_defconfig
@@ -59,6 +59,7 @@ CONFIG_USB=y
 CONFIG_USB_ISP1760=y
 CONFIG_ERRNO_STR=y
 CONFIG_NVMXIP_QSPI=y
+CONFIG_CMD_NVMXIP=y
 CONFIG_EFI_MM_COMM_TEE=y
 CONFIG_ARM_FFA_TRANSPORT=y
 CONFIG_EFI_RUNTIME_UPDATE_CAPSULE=y
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index f22230b5cce2..3b895be9e4ba 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -139,6 +139,7 @@ CONFIG_IP_DEFRAG=y
 CONFIG_BOOTP_SERVERIP=y
 CONFIG_IPV6=y
 CONFIG_NVMXIP_QSPI=y
+CONFIG_CMD_NVMXIP=y
 CONFIG_DM_DMA=y
 CONFIG_DEVRES=y
 CONFIG_DEBUG_DEVRES=y
-- 
2.39.2

