1 // SPDX-License-Identifier: GPL-2.0+
4 #include <display_options.h>
11 #define MAX_RTC_BYTES 32
13 static int do_rtc_read(struct udevice *dev, int argc, char * const argv[])
15 u8 buf[MAX_RTC_BYTES];
18 if (argc < 2 || argc > 3)
21 reg = hextoul(argv[0], NULL);
22 len = hextoul(argv[1], NULL);
27 addr = map_sysmem(hextoul(argv[2], NULL), len);
28 ret = dm_rtc_read(dev, reg, addr, len);
31 printf("dm_rtc_read() failed: %d\n", ret);
32 return CMD_RET_FAILURE;
34 return CMD_RET_SUCCESS;
38 r = min_t(int, len, sizeof(buf));
39 ret = dm_rtc_read(dev, reg, buf, r);
41 printf("dm_rtc_read() failed: %d\n", ret);
42 return CMD_RET_FAILURE;
44 print_buffer(reg, buf, 1, r, 0);
49 return CMD_RET_SUCCESS;
52 static int do_rtc_write(struct udevice *dev, int argc, char * const argv[])
54 u8 buf[MAX_RTC_BYTES];
59 if (argc < 2 || argc > 3)
62 reg = hextoul(argv[0], NULL);
67 len = hextoul(argv[1], NULL);
68 addr = map_sysmem(hextoul(argv[2], NULL), len);
69 ret = dm_rtc_write(dev, reg, addr, len);
72 printf("dm_rtc_write() failed: %d\n", ret);
73 return CMD_RET_FAILURE;
75 return CMD_RET_SUCCESS;
82 printf("invalid hex string\n");
83 return CMD_RET_FAILURE;
87 len = min_t(int, slen / 2, sizeof(buf));
88 if (hex2bin(buf, s, len)) {
89 printf("invalid hex string\n");
90 return CMD_RET_FAILURE;
93 ret = dm_rtc_write(dev, reg, buf, len);
95 printf("dm_rtc_write() failed: %d\n", ret);
96 return CMD_RET_FAILURE;
102 return CMD_RET_SUCCESS;
105 int do_rtc(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
112 return CMD_RET_USAGE;
117 if (!strcmp(argv[0], "list")) {
121 uclass_id_foreach_dev(UCLASS_RTC, dev, uc) {
122 printf("RTC #%d - %s\n", idx++, dev->name);
125 printf("*** no RTC devices available ***\n");
126 return CMD_RET_FAILURE;
128 return CMD_RET_SUCCESS;
132 if (!strcmp(argv[0], "dev") && argc >= 2)
133 idx = dectoul(argv[1], NULL);
135 ret = uclass_get_device(UCLASS_RTC, idx, &dev);
137 printf("Cannot find RTC #%d: err=%d\n", idx, ret);
138 return CMD_RET_FAILURE;
141 if (!strcmp(argv[0], "dev")) {
142 /* Show the existing or newly selected RTC */
145 printf("RTC #%d - %s\n", idx, dev->name);
146 return CMD_RET_SUCCESS;
149 if (!strcmp(argv[0], "read"))
150 return do_rtc_read(dev, argc - 1, argv + 1);
152 if (!strcmp(argv[0], "write"))
153 return do_rtc_write(dev, argc - 1, argv + 1);
155 return CMD_RET_USAGE;
161 "list - show available rtc devices\n"
162 "rtc dev [n] - show or set current rtc device\n"
163 "rtc read <reg> <count> - read and display 8-bit registers starting at <reg>\n"
164 "rtc read <reg> <count> <addr> - read 8-bit registers starting at <reg> to memory <addr>\n"
165 "rtc write <reg> <hexstring> - write 8-bit registers starting at <reg>\n"
166 "rtc write <reg> <count> <addr> - write from memory <addr> to 8-bit registers starting at <reg>\n"