All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
To: u-boot@lists.denx.de
Subject: [PATCH 6/6] rtc: add rtc command
Date: Mon,  4 May 2020 23:20:32 +0200	[thread overview]
Message-ID: <20200504212032.3759-7-rasmus.villemoes@prevas.dk> (raw)
In-Reply-To: <20200504212032.3759-1-rasmus.villemoes@prevas.dk>

Mostly as an aid for debugging RTC drivers, provide a command that can
be used to read/write arbitrary registers (assuming the driver
provides the read8/write8 methods or their _array variants).

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
---
 cmd/Kconfig  |   6 ++
 cmd/Makefile |   1 +
 cmd/rtc.c    | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 160 insertions(+)
 create mode 100644 cmd/rtc.c

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 6403bc45a5..e5d0e7f7c3 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1673,6 +1673,12 @@ config CMD_DATE
 	  Enable the 'date' command for getting/setting the time/date in RTC
 	  devices.
 
+config CMD_RTC
+	bool "rtc"
+	depends on DM_RTC
+	help
+	  Enable the 'rtc' command for low-level access to RTC devices.
+
 config CMD_TIME
 	bool "time"
 	help
diff --git a/cmd/Makefile b/cmd/Makefile
index f1dd513a4b..871b07f7b2 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -119,6 +119,7 @@ obj-$(CONFIG_CMD_REISER) += reiser.o
 obj-$(CONFIG_CMD_REMOTEPROC) += remoteproc.o
 obj-$(CONFIG_CMD_RNG) += rng.o
 obj-$(CONFIG_CMD_ROCKUSB) += rockusb.o
+obj-$(CONFIG_CMD_RTC) += rtc.o
 obj-$(CONFIG_SANDBOX) += host.o
 obj-$(CONFIG_CMD_SATA) += sata.o
 obj-$(CONFIG_CMD_NVME) += nvme.o
diff --git a/cmd/rtc.c b/cmd/rtc.c
new file mode 100644
index 0000000000..d48c0333fa
--- /dev/null
+++ b/cmd/rtc.c
@@ -0,0 +1,153 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <common.h>
+#include <command.h>
+#include <dm.h>
+#include <hexdump.h>
+#include <i2c.h>
+#include <rtc.h>
+
+#define MAX_RTC_BYTES 32
+
+static int do_rtc_read(struct udevice *dev, int argc, char * const argv[], int mem)
+{
+	u8 buf[MAX_RTC_BYTES];
+	int reg, len, ret;
+	u8 *addr;
+
+	if (argc != 2 + mem)
+		return CMD_RET_USAGE;
+
+	if (mem) {
+		addr = (void*)simple_strtoul(argv[0], NULL, 16);
+		argv++;
+		argv--;
+	} else {
+		addr = buf;
+	}
+	reg = simple_strtoul(argv[0], NULL, 0);
+	len = simple_strtoul(argv[1], NULL, 0);
+
+	if (!mem && len > sizeof(buf)) {
+		printf("can read at most %d registers at a time\n", MAX_RTC_BYTES);
+		return CMD_RET_FAILURE;
+	}
+	ret = rtc_read8_array(dev, reg, buf, len);
+	if (ret) {
+		printf("rtc_read8_array() failed: %d\n", ret);
+		return CMD_RET_FAILURE;
+	}
+	if (!mem) {
+		while (len--)
+			printf("%d: 0x%02x\n", reg++, *addr++);
+	}
+	return CMD_RET_SUCCESS;
+}
+
+static int do_rtc_write(struct udevice *dev, int argc, char * const argv[], int mem)
+{
+	u8 buf[MAX_RTC_BYTES];
+	u8 *addr;
+	int reg, len, ret;
+
+	if (argc != 2 + mem)
+		return CMD_RET_USAGE;
+
+	if (mem) {
+		addr = (void*)simple_strtoul(argv[0], NULL, 16);
+		argv++;
+		argv--;
+	} else {
+		addr = buf;
+	}
+
+	reg = simple_strtoul(argv[0], NULL, 0);
+	if (mem) {
+		len = simple_strtoul(argv[1], NULL, 0);
+	} else {
+		/* Convert hexstring, bail out if too long. */
+		const char *s = argv[1];
+
+		len = strlen(s);
+		if (len > 2*MAX_RTC_BYTES) {
+			printf("hex string too long, can write at most %d bytes\n", MAX_RTC_BYTES);
+			return CMD_RET_FAILURE;
+		}
+		len /= 2;
+		if (hex2bin(addr, s, len)) {
+			printf("invalid hex string\n");
+			return CMD_RET_FAILURE;
+		}
+	}
+
+	ret = rtc_write8_array(dev, reg, buf, len);
+	if (ret) {
+		printf("rtc_write8_array() failed: %d\n", ret);
+		return CMD_RET_FAILURE;
+	}
+	return CMD_RET_SUCCESS;
+}
+
+int do_rtc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	static int curr_rtc = 0;
+	struct udevice *dev;
+	int ret, idx;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	argc--;
+	argv++;
+
+	if (!strcmp(argv[0], "list")) {
+		struct uclass *uc;
+		idx = 0;
+
+		uclass_id_foreach_dev(UCLASS_RTC, dev, uc) {
+			printf("RTC #%d - %s\n", idx++, dev->name);
+		}
+		if (!idx) {
+			printf("*** no RTC devices available ***\n");
+			return CMD_RET_FAILURE;
+		}
+		return CMD_RET_SUCCESS;
+	}
+
+	idx = curr_rtc;
+	if (!strcmp(argv[0], "dev") && argc >= 2)
+		idx = simple_strtoul(argv[1], NULL, 10);
+
+	ret = uclass_get_device(UCLASS_RTC, idx, &dev);
+	if (ret) {
+		printf("Cannot find RTC #%d: err=%d\n", idx, ret);
+		return CMD_RET_FAILURE;
+	}
+
+	if (!strcmp(argv[0], "dev")) {
+		/* Show the existing or newly selected RTC */
+		if (argc >= 2)
+			curr_rtc = idx;
+		printf("RTC #%d - %s\n", idx, dev->name);
+		return CMD_RET_SUCCESS;
+	}
+
+	if (!strcmp(argv[0], "read") || !strcmp(argv[0], "readm"))
+		return do_rtc_read(dev, argc - 1, argv + 1, !strcmp(argv[0], "readm"));
+
+	if (!strcmp(argv[0], "write") || !strcmp(argv[0], "writem"))
+		return do_rtc_write(dev, argc - 1, argv + 1, !strcmp(argv[0], "writem"));
+
+	return CMD_RET_USAGE;
+}
+
+U_BOOT_CMD(
+	rtc,	5,	0,	do_rtc,
+	"RTC subsystem",
+	"list                        - show available rtc devices\n"
+	"rtc dev [n]                     - show or set current rtc device\n"
+	"rtc read <reg> <count>          - read and display 8-bit registers starting at <reg>\n"
+	"rtc readm <addr> <reg> <count>  - read 8-bit registers starting at <reg> to memory\n"
+	"rtc write <reg> <hexstring>     - write 8-bit registers starting at <reg>\n"
+	"rtc writem <addr> <reg> <count> - write memory to 8-bit registers starting at <reg>\n"
+);
-- 
2.23.0

  parent reply	other threads:[~2020-05-04 21:20 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-04 21:20 [PATCH 0/6] rtc: add rtc_{read,write}8_array and rtc command Rasmus Villemoes
2020-05-04 21:20 ` [PATCH 1/6] rtc: add rtc_read8_array helper and ->read8_array method Rasmus Villemoes
2020-05-06  3:42   ` Simon Glass
2020-05-06  8:13     ` Rasmus Villemoes
2020-05-04 21:20 ` [PATCH 2/6] rtc: add rtc_write8_array() helper Rasmus Villemoes
2020-05-06  3:42   ` Simon Glass
2020-05-04 21:20 ` [PATCH 3/6] rtc: fall back to ->{read, write}8_array if ->{read, write}8 are not provided Rasmus Villemoes
2020-05-06  3:42   ` [PATCH 3/6] rtc: fall back to ->{read,write}8_array if ->{read,write}8 " Simon Glass
2020-05-04 21:20 ` [PATCH 4/6] rtc: pcf2127: provide ->read8_array method Rasmus Villemoes
2020-05-06  3:42   ` Simon Glass
2020-05-04 21:20 ` [PATCH 5/6] rtc: pcf2127: provide ->write8_array method Rasmus Villemoes
2020-05-06  3:42   ` Simon Glass
2020-05-04 21:20 ` Rasmus Villemoes [this message]
2020-05-06  3:42   ` [PATCH 6/6] rtc: add rtc command Simon Glass
2020-05-19 22:01 ` [PATCH v2 00/10] new rtc methods, rtc command, and tests Rasmus Villemoes
2020-05-19 22:01   ` [PATCH v2 01/10] rtc: add rtc_read helper and ->read method Rasmus Villemoes
2020-05-19 22:01   ` [PATCH v2 02/10] rtc: add rtc_write() helper Rasmus Villemoes
2020-05-19 22:01   ` [PATCH v2 03/10] rtc: fall back to ->{read, write} if ->{read, write}8 are not provided Rasmus Villemoes
2020-05-19 22:01   ` [PATCH v2 04/10] rtc: pcf2127: provide ->read method Rasmus Villemoes
2020-05-19 22:01   ` [PATCH v2 05/10] rtc: pcf2127: provide ->write method Rasmus Villemoes
2020-05-19 22:01   ` [PATCH v2 06/10] rtc: add rtc command Rasmus Villemoes
2020-05-31 14:07     ` Simon Glass
2020-06-02  9:13       ` Rasmus Villemoes
2020-06-02 13:22         ` Simon Glass
2020-06-02 14:36           ` Rasmus Villemoes
2020-06-02 19:29             ` Simon Glass
2020-05-19 22:01   ` [PATCH v2 07/10] rtc: sandbox-rtc: fix set method Rasmus Villemoes
2020-05-31 14:07     ` Simon Glass
2020-05-19 22:01   ` [PATCH v2 08/10] rtc: i2c_rtc_emul: catch any write to the "reset" register Rasmus Villemoes
2020-05-31 14:07     ` Simon Glass
2020-05-19 22:01   ` [PATCH v2 09/10] test: dm: rtc: add test of rtc_read, rtc_write Rasmus Villemoes
2020-05-31 14:07     ` Simon Glass
2020-05-19 22:01   ` [PATCH v2 10/10] test: dm: rtc: add tests of rtc shell command Rasmus Villemoes
2020-05-31 14:07     ` Simon Glass
2020-06-02  9:15       ` Rasmus Villemoes
2020-06-02 18:40   ` [PATCH v2 00/10] new rtc methods, rtc command, and tests Rasmus Villemoes
2020-06-02 19:29     ` Simon Glass
2020-06-02 19:44       ` Rasmus Villemoes
2020-06-02 20:56         ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200504212032.3759-7-rasmus.villemoes@prevas.dk \
    --to=rasmus.villemoes@prevas.dk \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.