]> git.baikalelectronics.ru Git - uboot.git/commitdiff
rtc: add rtc command
authorRasmus Villemoes <rasmus.villemoes@prevas.dk>
Mon, 6 Jul 2020 20:01:15 +0000 (22:01 +0200)
committerHeiko Schocher <hs@denx.de>
Thu, 9 Jul 2020 04:02:45 +0000 (06:02 +0200)
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 read/write methods or their single-register-at-a-time
variants).

Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heiko Schocher <hs@denx.de>
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
cmd/Kconfig
cmd/Makefile
cmd/rtc.c [new file with mode: 0644]

index 2b823dd2608ce430dd90e6ddb3e67a5858d30617..846c905c9cc5598b68736b80b649c82457ace0d9 100644 (file)
@@ -1739,6 +1739,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
index 7008dd42dc18b8a353ab281f314db8249982e8a8..dc412d1106dfad57843bff0ba024260197a2d10f 100644 (file)
@@ -122,6 +122,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 (file)
index 0000000..b4f61b2
--- /dev/null
+++ b/cmd/rtc.c
@@ -0,0 +1,167 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <common.h>
+#include <command.h>
+#include <dm.h>
+#include <hexdump.h>
+#include <i2c.h>
+#include <mapmem.h>
+#include <rtc.h>
+
+#define MAX_RTC_BYTES 32
+
+static int do_rtc_read(struct udevice *dev, int argc, char * const argv[])
+{
+       u8 buf[MAX_RTC_BYTES];
+       int reg, len, ret, r;
+
+       if (argc < 2 || argc > 3)
+               return CMD_RET_USAGE;
+
+       reg = simple_strtoul(argv[0], NULL, 16);
+       len = simple_strtoul(argv[1], NULL, 16);
+
+       if (argc == 3) {
+               u8 *addr;
+
+               addr = map_sysmem(simple_strtoul(argv[2], NULL, 16), len);
+               ret = dm_rtc_read(dev, reg, addr, len);
+               unmap_sysmem(addr);
+               if (ret) {
+                       printf("dm_rtc_read() failed: %d\n", ret);
+                       return CMD_RET_FAILURE;
+               }
+               return CMD_RET_SUCCESS;
+       }
+
+       while (len) {
+               r = min_t(int, len, sizeof(buf));
+               ret = dm_rtc_read(dev, reg, buf, r);
+               if (ret) {
+                       printf("dm_rtc_read() failed: %d\n", ret);
+                       return CMD_RET_FAILURE;
+               }
+               print_buffer(reg, buf, 1, r, 0);
+               len -= r;
+               reg += r;
+       }
+
+       return CMD_RET_SUCCESS;
+}
+
+static int do_rtc_write(struct udevice *dev, int argc, char * const argv[])
+{
+       u8 buf[MAX_RTC_BYTES];
+       int reg, len, ret;
+       const char *s;
+       int slen;
+
+       if (argc < 2 || argc > 3)
+               return CMD_RET_USAGE;
+
+       reg = simple_strtoul(argv[0], NULL, 16);
+
+       if (argc == 3) {
+               u8 *addr;
+
+               len = simple_strtoul(argv[1], NULL, 16);
+               addr = map_sysmem(simple_strtoul(argv[2], NULL, 16), len);
+               ret = dm_rtc_write(dev, reg, addr, len);
+               unmap_sysmem(addr);
+               if (ret) {
+                       printf("dm_rtc_write() failed: %d\n", ret);
+                       return CMD_RET_FAILURE;
+               }
+               return CMD_RET_SUCCESS;
+       }
+
+       s = argv[1];
+       slen = strlen(s);
+
+       if (slen % 2) {
+               printf("invalid hex string\n");
+               return CMD_RET_FAILURE;
+       }
+
+       while (slen) {
+               len = min_t(int, slen / 2, sizeof(buf));
+               if (hex2bin(buf, s, len)) {
+                       printf("invalid hex string\n");
+                       return CMD_RET_FAILURE;
+               }
+
+               ret = dm_rtc_write(dev, reg, buf, len);
+               if (ret) {
+                       printf("dm_rtc_write() failed: %d\n", ret);
+                       return CMD_RET_FAILURE;
+               }
+               s += 2 * len;
+               slen -= 2 * len;
+       }
+
+       return CMD_RET_SUCCESS;
+}
+
+int do_rtc(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
+{
+       static int curr_rtc;
+       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"))
+               return do_rtc_read(dev, argc - 1, argv + 1);
+
+       if (!strcmp(argv[0], "write"))
+               return do_rtc_write(dev, argc - 1, argv + 1);
+
+       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 read <reg> <count> <addr>   - read 8-bit registers starting at <reg> to memory <addr>\n"
+       "rtc write <reg> <hexstring>     - write 8-bit registers starting at <reg>\n"
+       "rtc write <reg> <count> <addr>  - write from memory <addr> to 8-bit registers starting at <reg>\n"
+);