From patchwork Mon May 4 21:20:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 245051 List-Id: U-Boot discussion From: rasmus.villemoes at prevas.dk (Rasmus Villemoes) Date: Mon, 4 May 2020 23:20:27 +0200 Subject: [PATCH 1/6] rtc: add rtc_read8_array helper and ->read8_array method In-Reply-To: <20200504212032.3759-1-rasmus.villemoes@prevas.dk> References: <20200504212032.3759-1-rasmus.villemoes@prevas.dk> Message-ID: <20200504212032.3759-2-rasmus.villemoes@prevas.dk> Some users may want to read multiple consecutive 8-bit registers. Instead of each caller having to implement the loop, provide a rtc_read8_array() helper. Also, allow a driver to provide a read8_array method, which can be more efficient than reading one register at a time. Signed-off-by: Rasmus Villemoes Reviewed-by: Simon Glass --- drivers/rtc/rtc-uclass.c | 19 +++++++++++++++++++ include/rtc.h | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/drivers/rtc/rtc-uclass.c b/drivers/rtc/rtc-uclass.c index a0a238aedd..5070fb416d 100644 --- a/drivers/rtc/rtc-uclass.c +++ b/drivers/rtc/rtc-uclass.c @@ -49,6 +49,25 @@ int rtc_read8(struct udevice *dev, unsigned int reg) return ops->read8(dev, reg); } +int rtc_read8_array(struct udevice *dev, unsigned int reg, + u8 *buf, unsigned int len) +{ + struct rtc_ops *ops = rtc_get_ops(dev); + + assert(ops); + if (ops->read8_array) + return ops->read8_array(dev, reg, buf, len); + if (!ops->read8) + return -ENOSYS; + while (len--) { + int ret = ops->read8(dev, reg++); + if (ret < 0) + return ret; + *buf++ = ret; + } + return 0; +} + int rtc_write8(struct udevice *dev, unsigned int reg, int val) { struct rtc_ops *ops = rtc_get_ops(dev); diff --git a/include/rtc.h b/include/rtc.h index 8aabfc1162..f7f622c1db 100644 --- a/include/rtc.h +++ b/include/rtc.h @@ -64,6 +64,18 @@ struct rtc_ops { */ int (*read8)(struct udevice *dev, unsigned int reg); + /** + * read8_array() - Read multiple 8-bit registers + * + * @dev: Device to read from + * @reg: First register to read + * @buf: Output buffer + * @len: Number of registers to read + * @return 0 if OK, -ve on error + */ + int (*read8_array)(struct udevice *dev, unsigned int reg, + u8 *buf, unsigned int len); + /** * write8() - Write an 8-bit register * @@ -118,6 +130,18 @@ int dm_rtc_reset(struct udevice *dev); */ int rtc_read8(struct udevice *dev, unsigned int reg); +/** + * rtc_read8_array() - Read multiple 8-bit registers + * + * @dev: Device to read from + * @reg: First register to read + * @buf: Output buffer + * @len: Number of registers to read + * @return 0 if OK, -ve on error + */ +int rtc_read8_array(struct udevice *dev, unsigned int reg, + u8 *buf, unsigned int len); + /** * rtc_write8() - Write an 8-bit register * From patchwork Mon May 4 21:20:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 245052 List-Id: U-Boot discussion From: rasmus.villemoes at prevas.dk (Rasmus Villemoes) Date: Mon, 4 May 2020 23:20:28 +0200 Subject: [PATCH 2/6] rtc: add rtc_write8_array() helper In-Reply-To: <20200504212032.3759-1-rasmus.villemoes@prevas.dk> References: <20200504212032.3759-1-rasmus.villemoes@prevas.dk> Message-ID: <20200504212032.3759-3-rasmus.villemoes@prevas.dk> Similar to the rtc_read8_array(), introduce a helper that allows the caller to write multiple consecutive 8-bit registers with one call. If the driver provides the ->write8_array method, use that, otherwise loop using ->write8. Signed-off-by: Rasmus Villemoes Reviewed-by: Simon Glass --- drivers/rtc/rtc-uclass.c | 18 ++++++++++++++++++ include/rtc.h | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/drivers/rtc/rtc-uclass.c b/drivers/rtc/rtc-uclass.c index 5070fb416d..56490a876f 100644 --- a/drivers/rtc/rtc-uclass.c +++ b/drivers/rtc/rtc-uclass.c @@ -78,6 +78,24 @@ int rtc_write8(struct udevice *dev, unsigned int reg, int val) return ops->write8(dev, reg, val); } +int rtc_write8_array(struct udevice *dev, unsigned int reg, + const u8 *buf, unsigned int len) +{ + struct rtc_ops *ops = rtc_get_ops(dev); + + assert(ops); + if (ops->write8_array) + return ops->write8_array(dev, reg, buf, len); + if (!ops->write8) + return -ENOSYS; + while (len--) { + int ret = ops->write8(dev, reg++, *buf++); + if (ret < 0) + return ret; + } + return 0; +} + int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep) { u16 value = 0; diff --git a/include/rtc.h b/include/rtc.h index f7f622c1db..08b2a00567 100644 --- a/include/rtc.h +++ b/include/rtc.h @@ -85,6 +85,18 @@ struct rtc_ops { * @return 0 if OK, -ve on error */ int (*write8)(struct udevice *dev, unsigned int reg, int val); + + /** + * write8_array() - Write multiple 8-bit registers + * + * @dev: Device to write to + * @reg: First register to write + * @buf: Input buffer + * @len: Number of registers to write + * @return 0 if OK, -ve on error + */ + int (*write8_array)(struct udevice *dev, unsigned int reg, + const u8 *buf, unsigned int len); }; /* Access the operations for an RTC device */ @@ -152,6 +164,18 @@ int rtc_read8_array(struct udevice *dev, unsigned int reg, */ int rtc_write8(struct udevice *dev, unsigned int reg, int val); +/** + * rtc_write8_array() - Write multiple 8-bit registers + * + * @dev: Device to write to + * @reg: First register to write + * @buf: Input buffer + * @len: Number of registers to write + * @return 0 if OK, -ve on error + */ +int rtc_write8_array(struct udevice *dev, unsigned int reg, + const u8 *buf, unsigned int len); + /** * rtc_read16() - Read a 16-bit value from the RTC * From patchwork Mon May 4 21:20:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 245054 List-Id: U-Boot discussion From: rasmus.villemoes at prevas.dk (Rasmus Villemoes) Date: Mon, 4 May 2020 23:20:29 +0200 Subject: [PATCH 3/6] rtc: fall back to ->{read, write}8_array if ->{read, write}8 are not provided In-Reply-To: <20200504212032.3759-1-rasmus.villemoes@prevas.dk> References: <20200504212032.3759-1-rasmus.villemoes@prevas.dk> Message-ID: <20200504212032.3759-4-rasmus.villemoes@prevas.dk> Similar to how the rtc_{read,write}8_array functions fall back to using the {read,write}8 methods, do the opposite in the rtc_{read,write}8 functions. This way, each driver only needs to provide either ->read8 or ->read8_array to make both rtc_read8() and rtc_read8_array() work - without this, a driver that provides rtc_read8_array() would most likely just duplicate the logic here for implementing a ->read8() method in term of its ->read8_array() method. The same remarks of course apply to the write case. Signed-off-by: Rasmus Villemoes Reviewed-by: Simon Glass --- drivers/rtc/rtc-uclass.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/drivers/rtc/rtc-uclass.c b/drivers/rtc/rtc-uclass.c index 56490a876f..6bf964f937 100644 --- a/drivers/rtc/rtc-uclass.c +++ b/drivers/rtc/rtc-uclass.c @@ -44,9 +44,17 @@ int rtc_read8(struct udevice *dev, unsigned int reg) struct rtc_ops *ops = rtc_get_ops(dev); assert(ops); - if (!ops->read8) - return -ENOSYS; - return ops->read8(dev, reg); + if (ops->read8) + return ops->read8(dev, reg); + if (ops->read8_array) { + u8 buf[1]; + int ret = ops->read8_array(dev, reg, buf, 1); + + if (ret < 0) + return ret; + return buf[0]; + } + return -ENOSYS; } int rtc_read8_array(struct udevice *dev, unsigned int reg, @@ -73,9 +81,13 @@ int rtc_write8(struct udevice *dev, unsigned int reg, int val) struct rtc_ops *ops = rtc_get_ops(dev); assert(ops); - if (!ops->write8) - return -ENOSYS; - return ops->write8(dev, reg, val); + if (ops->write8) + return ops->write8(dev, reg, val); + if (ops->write8_array) { + u8 buf[1] = { val }; + return ops->write8_array(dev, reg, buf, 1); + } + return -ENOSYS; } int rtc_write8_array(struct udevice *dev, unsigned int reg, From patchwork Mon May 4 21:20:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 245053 List-Id: U-Boot discussion From: rasmus.villemoes at prevas.dk (Rasmus Villemoes) Date: Mon, 4 May 2020 23:20:30 +0200 Subject: [PATCH 4/6] rtc: pcf2127: provide ->read8_array method In-Reply-To: <20200504212032.3759-1-rasmus.villemoes@prevas.dk> References: <20200504212032.3759-1-rasmus.villemoes@prevas.dk> Message-ID: <20200504212032.3759-5-rasmus.villemoes@prevas.dk> This simply consists of renaming the existing pcf2127_read_reg() helper to follow the naming of the other methods (i.e. pcf2127_rtc_) and changing the type of its "len" parameter. Signed-off-by: Rasmus Villemoes Reviewed-by: Simon Glass --- drivers/rtc/pcf2127.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/rtc/pcf2127.c b/drivers/rtc/pcf2127.c index b34ed63bf0..b3f6de8496 100644 --- a/drivers/rtc/pcf2127.c +++ b/drivers/rtc/pcf2127.c @@ -22,8 +22,8 @@ #define PCF2127_REG_MO 0x08 #define PCF2127_REG_YR 0x09 -static int pcf2127_read_reg(struct udevice *dev, uint offset, - u8 *buffer, int len) +static int pcf2127_rtc_read8_array(struct udevice *dev, uint offset, + u8 *buffer, uint len) { struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); struct i2c_msg msg; @@ -72,7 +72,7 @@ static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm) int ret = 0; uchar buf[10] = { PCF2127_REG_CTRL1 }; - ret = pcf2127_read_reg(dev, PCF2127_REG_CTRL1, buf, sizeof(buf)); + ret = pcf2127_rtc_read8_array(dev, PCF2127_REG_CTRL1, buf, sizeof(buf)); if (ret < 0) return ret; @@ -109,6 +109,7 @@ static const struct rtc_ops pcf2127_rtc_ops = { .get = pcf2127_rtc_get, .set = pcf2127_rtc_set, .reset = pcf2127_rtc_reset, + .read8_array = pcf2127_rtc_read8_array, }; static const struct udevice_id pcf2127_rtc_ids[] = { From patchwork Mon May 4 21:20:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 245055 List-Id: U-Boot discussion From: rasmus.villemoes at prevas.dk (Rasmus Villemoes) Date: Mon, 4 May 2020 23:20:31 +0200 Subject: [PATCH 5/6] rtc: pcf2127: provide ->write8_array method In-Reply-To: <20200504212032.3759-1-rasmus.villemoes@prevas.dk> References: <20200504212032.3759-1-rasmus.villemoes@prevas.dk> Message-ID: <20200504212032.3759-6-rasmus.villemoes@prevas.dk> Signed-off-by: Rasmus Villemoes Reviewed-by: Simon Glass --- drivers/rtc/pcf2127.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/rtc/pcf2127.c b/drivers/rtc/pcf2127.c index b3f6de8496..227ab09880 100644 --- a/drivers/rtc/pcf2127.c +++ b/drivers/rtc/pcf2127.c @@ -43,6 +43,12 @@ static int pcf2127_rtc_read8_array(struct udevice *dev, uint offset, return dm_i2c_xfer(dev, &msg, 1); } +static int pcf2127_rtc_write8_array(struct udevice *dev, uint offset, + const u8 *buffer, uint len) +{ + return dm_i2c_write(dev, offset, buffer, len); +} + static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm) { uchar buf[7] = {0}; @@ -110,6 +116,7 @@ static const struct rtc_ops pcf2127_rtc_ops = { .set = pcf2127_rtc_set, .reset = pcf2127_rtc_reset, .read8_array = pcf2127_rtc_read8_array, + .write8_array = pcf2127_rtc_write8_array, }; static const struct udevice_id pcf2127_rtc_ids[] = { From patchwork Mon May 4 21:20:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 245056 List-Id: U-Boot discussion From: rasmus.villemoes at prevas.dk (Rasmus Villemoes) Date: Mon, 4 May 2020 23:20:32 +0200 Subject: [PATCH 6/6] rtc: add rtc command In-Reply-To: <20200504212032.3759-1-rasmus.villemoes@prevas.dk> References: <20200504212032.3759-1-rasmus.villemoes@prevas.dk> Message-ID: <20200504212032.3759-7-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 --- 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 +#include +#include +#include +#include +#include + +#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 - read and display 8-bit registers starting at \n" + "rtc readm - read 8-bit registers starting at to memory\n" + "rtc write - write 8-bit registers starting at \n" + "rtc writem - write memory to 8-bit registers starting at \n" +);