1 // SPDX-License-Identifier: GPL-2.0+
5 #include <display_options.h>
12 #define MAX_RTC_BYTES 32
14 static int do_rtc_read(struct udevice *dev, int argc, char * const argv[])
16 u8 buf[MAX_RTC_BYTES];
19 if (argc < 2 || argc > 3)
22 reg = hextoul(argv[0], NULL);
23 len = hextoul(argv[1], NULL);
28 addr = map_sysmem(hextoul(argv[2], NULL), len);
29 ret = dm_rtc_read(dev, reg, addr, len);
32 printf("dm_rtc_read() failed: %d\n", ret);
33 return CMD_RET_FAILURE;
35 return CMD_RET_SUCCESS;
39 r = min_t(int, len, sizeof(buf));
40 ret = dm_rtc_read(dev, reg, buf, r);
42 printf("dm_rtc_read() failed: %d\n", ret);
43 return CMD_RET_FAILURE;
45 print_buffer(reg, buf, 1, r, 0);
50 return CMD_RET_SUCCESS;
53 static int do_rtc_write(struct udevice *dev, int argc, char * const argv[])
55 u8 buf[MAX_RTC_BYTES];
60 if (argc < 2 || argc > 3)
63 reg = hextoul(argv[0], NULL);
68 len = hextoul(argv[1], NULL);
69 addr = map_sysmem(hextoul(argv[2], NULL), len);
70 ret = dm_rtc_write(dev, reg, addr, len);
73 printf("dm_rtc_write() failed: %d\n", ret);
74 return CMD_RET_FAILURE;
76 return CMD_RET_SUCCESS;
83 printf("invalid hex string\n");
84 return CMD_RET_FAILURE;
88 len = min_t(int, slen / 2, sizeof(buf));
89 if (hex2bin(buf, s, len)) {
90 printf("invalid hex string\n");
91 return CMD_RET_FAILURE;
94 ret = dm_rtc_write(dev, reg, buf, len);
96 printf("dm_rtc_write() failed: %d\n", ret);
97 return CMD_RET_FAILURE;
103 return CMD_RET_SUCCESS;
106 int do_rtc(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
113 return CMD_RET_USAGE;
118 if (!strcmp(argv[0], "list")) {
122 uclass_id_foreach_dev(UCLASS_RTC, dev, uc) {
123 printf("RTC #%d - %s\n", idx++, dev->name);
126 printf("*** no RTC devices available ***\n");
127 return CMD_RET_FAILURE;
129 return CMD_RET_SUCCESS;
133 if (!strcmp(argv[0], "dev") && argc >= 2)
134 idx = dectoul(argv[1], NULL);
136 ret = uclass_get_device(UCLASS_RTC, idx, &dev);
138 printf("Cannot find RTC #%d: err=%d\n", idx, ret);
139 return CMD_RET_FAILURE;
142 if (!strcmp(argv[0], "dev")) {
143 /* Show the existing or newly selected RTC */
146 printf("RTC #%d - %s\n", idx, dev->name);
147 return CMD_RET_SUCCESS;
150 if (!strcmp(argv[0], "read"))
151 return do_rtc_read(dev, argc - 1, argv + 1);
153 if (!strcmp(argv[0], "write"))
154 return do_rtc_write(dev, argc - 1, argv + 1);
156 return CMD_RET_USAGE;
162 "list - show available rtc devices\n"
163 "rtc dev [n] - show or set current rtc device\n"
164 "rtc read <reg> <count> - read and display 8-bit registers starting at <reg>\n"
165 "rtc read <reg> <count> <addr> - read 8-bit registers starting at <reg> to memory <addr>\n"
166 "rtc write <reg> <hexstring> - write 8-bit registers starting at <reg>\n"
167 "rtc write <reg> <count> <addr> - write from memory <addr> to 8-bit registers starting at <reg>\n"