blob: 755adec1e71e9dfe6d31ce243392989b31a295b2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00002/*
3 * (C) Copyright 2001
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk38635852002-08-27 05:55:31 +00005 */
6
7/*
8 * RTC, Date & Time support: get and set date & time
9 */
wdenk38635852002-08-27 05:55:31 +000010#include <command.h>
Simon Glassa8efb4a2015-04-20 12:37:26 -060011#include <dm.h>
wdenk38635852002-08-27 05:55:31 +000012#include <rtc.h>
Stefan Roese096cc9b2007-02-20 10:51:26 +010013#include <i2c.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
wdenk38635852002-08-27 05:55:31 +000015
Wolfgang Denk6405a152006-03-31 18:32:53 +020016DECLARE_GLOBAL_DATA_PTR;
17
Mike Frysinger59e14722010-10-20 07:17:23 -040018static const char * const weekdays[] = {
wdenk38635852002-08-27 05:55:31 +000019 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
20};
21
Mike Frysinger59e14722010-10-20 07:17:23 -040022int mk_date (const char *, struct rtc_time *);
wdenk38635852002-08-27 05:55:31 +000023
Marek Vasutd45c1b82015-04-10 01:11:54 +020024static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
25
Simon Glassed38aef2020-05-10 11:40:03 -060026static int do_date(struct cmd_tbl *cmdtp, int flag, int argc,
27 char *const argv[])
wdenk38635852002-08-27 05:55:31 +000028{
29 struct rtc_time tm;
30 int rcode = 0;
Simon Glassa8efb4a2015-04-20 12:37:26 -060031 int old_bus __maybe_unused;
Stefan Roese096cc9b2007-02-20 10:51:26 +010032
33 /* switch to correct I2C bus */
Bin Meng8755d2c2015-06-23 12:18:42 +080034#ifdef CONFIG_DM_RTC
Simon Glassa8efb4a2015-04-20 12:37:26 -060035 struct udevice *dev;
36
Michal Simek452359e2021-07-29 12:59:12 +020037 rcode = uclass_get_device_by_seq(UCLASS_RTC, 0, &dev);
Simon Glassa8efb4a2015-04-20 12:37:26 -060038 if (rcode) {
Michal Simek452359e2021-07-29 12:59:12 +020039 rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
40 if (rcode) {
41 printf("Cannot find RTC: err=%d\n", rcode);
42 return CMD_RET_FAILURE;
43 }
Simon Glassa8efb4a2015-04-20 12:37:26 -060044 }
Tom Rini52b2e262021-08-18 23:12:24 -040045#elif CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
Heiko Schochere0e55bc2012-01-16 21:12:24 +000046 old_bus = i2c_get_bus_num();
Tom Rini6a5dccc2022-11-16 13:10:41 -050047 i2c_set_bus_num(CFG_SYS_RTC_BUS_NUM);
Heiko Schochere0e55bc2012-01-16 21:12:24 +000048#else
Stefan Roese096cc9b2007-02-20 10:51:26 +010049 old_bus = I2C_GET_BUS();
Tom Rini6a5dccc2022-11-16 13:10:41 -050050 I2C_SET_BUS(CFG_SYS_RTC_BUS_NUM);
Heiko Schochere0e55bc2012-01-16 21:12:24 +000051#endif
wdenk38635852002-08-27 05:55:31 +000052
53 switch (argc) {
54 case 2: /* set date & time */
55 if (strcmp(argv[1],"reset") == 0) {
wdenk42c05472004-03-23 22:14:11 +000056 puts ("Reset RTC...\n");
Bin Meng8755d2c2015-06-23 12:18:42 +080057#ifdef CONFIG_DM_RTC
Simon Glassa8efb4a2015-04-20 12:37:26 -060058 rcode = dm_rtc_reset(dev);
59 if (!rcode)
60 rcode = dm_rtc_set(dev, &default_tm);
61#else
62 rtc_reset();
Marek Vasutd45c1b82015-04-10 01:11:54 +020063 rcode = rtc_set(&default_tm);
Simon Glassa8efb4a2015-04-20 12:37:26 -060064#endif
Marek Vasutd45c1b82015-04-10 01:11:54 +020065 if (rcode)
66 puts("## Failed to set date after RTC reset\n");
wdenk38635852002-08-27 05:55:31 +000067 } else {
68 /* initialize tm with current time */
Bin Meng8755d2c2015-06-23 12:18:42 +080069#ifdef CONFIG_DM_RTC
Simon Glassa8efb4a2015-04-20 12:37:26 -060070 rcode = dm_rtc_get(dev, &tm);
71#else
72 rcode = rtc_get(&tm);
73#endif
74 if (!rcode) {
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020075 /* insert new date & time */
Simon Glassa8efb4a2015-04-20 12:37:26 -060076 if (mk_date(argv[1], &tm) != 0) {
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020077 puts ("## Bad date format\n");
78 break;
79 }
80 /* and write to RTC */
Bin Meng8755d2c2015-06-23 12:18:42 +080081#ifdef CONFIG_DM_RTC
Simon Glassa8efb4a2015-04-20 12:37:26 -060082 rcode = dm_rtc_set(dev, &tm);
83#else
84 rcode = rtc_set(&tm);
85#endif
86 if (rcode) {
87 printf("## Set date failed: err=%d\n",
88 rcode);
89 }
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020090 } else {
Magnus Lilja55992362009-11-11 19:56:36 +010091 puts("## Get date failed\n");
wdenk38635852002-08-27 05:55:31 +000092 }
wdenk38635852002-08-27 05:55:31 +000093 }
Heinrich Schuchardt5d520cf2023-04-01 09:14:11 +020094 fallthrough;
wdenk38635852002-08-27 05:55:31 +000095 case 1: /* get date & time */
Bin Meng8755d2c2015-06-23 12:18:42 +080096#ifdef CONFIG_DM_RTC
Simon Glassa8efb4a2015-04-20 12:37:26 -060097 rcode = dm_rtc_get(dev, &tm);
98#else
99 rcode = rtc_get(&tm);
100#endif
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +0200101 if (rcode) {
Magnus Lilja55992362009-11-11 19:56:36 +0100102 puts("## Get date failed\n");
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +0200103 break;
104 }
wdenk38635852002-08-27 05:55:31 +0000105
106 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
107 tm.tm_year, tm.tm_mon, tm.tm_mday,
108 (tm.tm_wday<0 || tm.tm_wday>6) ?
Marek Vasutb3c29df2023-09-06 23:29:51 +0200109 "unknown " : weekdays[tm.tm_wday],
wdenk38635852002-08-27 05:55:31 +0000110 tm.tm_hour, tm.tm_min, tm.tm_sec);
111
Stefan Roese096cc9b2007-02-20 10:51:26 +0100112 break;
wdenk38635852002-08-27 05:55:31 +0000113 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000114 rcode = CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000115 }
Stefan Roese096cc9b2007-02-20 10:51:26 +0100116
117 /* switch back to original I2C bus */
Tom Rini52b2e262021-08-18 23:12:24 -0400118#if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
Heiko Schochere0e55bc2012-01-16 21:12:24 +0000119 i2c_set_bus_num(old_bus);
Bin Meng8755d2c2015-06-23 12:18:42 +0800120#elif !defined(CONFIG_DM_RTC)
Stefan Roese096cc9b2007-02-20 10:51:26 +0100121 I2C_SET_BUS(old_bus);
Heiko Schochere0e55bc2012-01-16 21:12:24 +0000122#endif
Stefan Roese096cc9b2007-02-20 10:51:26 +0100123
Simon Glassa8efb4a2015-04-20 12:37:26 -0600124 return rcode ? CMD_RET_FAILURE : 0;
wdenk38635852002-08-27 05:55:31 +0000125}
126
127/*
128 * simple conversion of two-digit string with error checking
129 */
Mike Frysinger59e14722010-10-20 07:17:23 -0400130static int cnvrt2 (const char *str, int *valp)
wdenk38635852002-08-27 05:55:31 +0000131{
132 int val;
133
134 if ((*str < '0') || (*str > '9'))
135 return (-1);
136
137 val = *str - '0';
138
139 ++str;
140
141 if ((*str < '0') || (*str > '9'))
142 return (-1);
143
144 *valp = 10 * val + (*str - '0');
145
146 return (0);
147}
148
149/*
150 * Convert date string: MMDDhhmm[[CC]YY][.ss]
151 *
152 * Some basic checking for valid values is done, but this will not catch
153 * all possible error conditions.
154 */
Mike Frysinger59e14722010-10-20 07:17:23 -0400155int mk_date (const char *datestr, struct rtc_time *tmp)
wdenk38635852002-08-27 05:55:31 +0000156{
157 int len, val;
158 char *ptr;
159
Roman Kapl0a0dbd42019-02-08 10:01:02 +0100160 ptr = strchr(datestr, '.');
161 len = strlen(datestr);
wdenk38635852002-08-27 05:55:31 +0000162
163 /* Set seconds */
164 if (ptr) {
165 int sec;
166
Roman Kapl0a0dbd42019-02-08 10:01:02 +0100167 ptr++;
wdenk38635852002-08-27 05:55:31 +0000168 if ((len - (ptr - datestr)) != 2)
169 return (-1);
170
Roman Kapl0a0dbd42019-02-08 10:01:02 +0100171 len -= 3;
wdenk38635852002-08-27 05:55:31 +0000172
173 if (cnvrt2 (ptr, &sec))
174 return (-1);
175
176 tmp->tm_sec = sec;
177 } else {
178 tmp->tm_sec = 0;
179 }
180
181 if (len == 12) { /* MMDDhhmmCCYY */
182 int year, century;
183
184 if (cnvrt2 (datestr+ 8, &century) ||
185 cnvrt2 (datestr+10, &year) ) {
186 return (-1);
187 }
188 tmp->tm_year = 100 * century + year;
189 } else if (len == 10) { /* MMDDhhmmYY */
190 int year, century;
191
192 century = tmp->tm_year / 100;
193 if (cnvrt2 (datestr+ 8, &year))
194 return (-1);
195 tmp->tm_year = 100 * century + year;
196 }
197
198 switch (len) {
199 case 8: /* MMDDhhmm */
200 /* fall thru */
201 case 10: /* MMDDhhmmYY */
202 /* fall thru */
203 case 12: /* MMDDhhmmCCYY */
204 if (cnvrt2 (datestr+0, &val) ||
205 val > 12) {
206 break;
207 }
208 tmp->tm_mon = val;
209 if (cnvrt2 (datestr+2, &val) ||
210 val > ((tmp->tm_mon==2) ? 29 : 31)) {
211 break;
212 }
213 tmp->tm_mday = val;
214
215 if (cnvrt2 (datestr+4, &val) ||
216 val > 23) {
217 break;
218 }
219 tmp->tm_hour = val;
220
221 if (cnvrt2 (datestr+6, &val) ||
222 val > 59) {
223 break;
224 }
225 tmp->tm_min = val;
226
227 /* calculate day of week */
Simon Glass2d937bb2015-04-20 12:37:17 -0600228 rtc_calc_weekday(tmp);
wdenk38635852002-08-27 05:55:31 +0000229
230 return (0);
231 default:
232 break;
233 }
234
235 return (-1);
236}
237
wdenk57b2d802003-06-27 21:31:46 +0000238/***************************************************/
239
wdenkf287a242003-07-01 21:06:45 +0000240U_BOOT_CMD(
241 date, 2, 1, do_date,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600242 "get/set/reset date & time",
wdenk57b2d802003-06-27 21:31:46 +0000243 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
244 " - without arguments: print date & time\n"
245 " - with numeric argument: set the system date & time\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200246 " - with 'reset' argument: reset the RTC"
wdenk57b2d802003-06-27 21:31:46 +0000247);