1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * RTC, Date & Time support: get and set date & time
16 DECLARE_GLOBAL_DATA_PTR;
18 static const char * const weekdays[] = {
19 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
22 #ifdef CONFIG_NEEDS_MANUAL_RELOC
23 #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
28 int mk_date (const char *, struct rtc_time *);
30 static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
32 static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
36 int old_bus __maybe_unused;
38 /* switch to correct I2C bus */
42 rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
44 printf("Cannot find RTC: err=%d\n", rcode);
45 return CMD_RET_FAILURE;
47 #elif defined(CONFIG_SYS_I2C)
48 old_bus = i2c_get_bus_num();
49 i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
51 old_bus = I2C_GET_BUS();
52 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
56 case 2: /* set date & time */
57 if (strcmp(argv[1],"reset") == 0) {
58 puts ("Reset RTC...\n");
60 rcode = dm_rtc_reset(dev);
62 rcode = dm_rtc_set(dev, &default_tm);
65 rcode = rtc_set(&default_tm);
68 puts("## Failed to set date after RTC reset\n");
70 /* initialize tm with current time */
72 rcode = dm_rtc_get(dev, &tm);
77 /* insert new date & time */
78 if (mk_date(argv[1], &tm) != 0) {
79 puts ("## Bad date format\n");
82 /* and write to RTC */
84 rcode = dm_rtc_set(dev, &tm);
89 printf("## Set date failed: err=%d\n",
93 puts("## Get date failed\n");
97 case 1: /* get date & time */
99 rcode = dm_rtc_get(dev, &tm);
101 rcode = rtc_get(&tm);
104 puts("## Get date failed\n");
108 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
109 tm.tm_year, tm.tm_mon, tm.tm_mday,
110 (tm.tm_wday<0 || tm.tm_wday>6) ?
111 "unknown " : RELOC(weekdays[tm.tm_wday]),
112 tm.tm_hour, tm.tm_min, tm.tm_sec);
116 rcode = CMD_RET_USAGE;
119 /* switch back to original I2C bus */
120 #ifdef CONFIG_SYS_I2C
121 i2c_set_bus_num(old_bus);
122 #elif !defined(CONFIG_DM_RTC)
123 I2C_SET_BUS(old_bus);
126 return rcode ? CMD_RET_FAILURE : 0;
130 * simple conversion of two-digit string with error checking
132 static int cnvrt2 (const char *str, int *valp)
136 if ((*str < '0') || (*str > '9'))
143 if ((*str < '0') || (*str > '9'))
146 *valp = 10 * val + (*str - '0');
152 * Convert date string: MMDDhhmm[[CC]YY][.ss]
154 * Some basic checking for valid values is done, but this will not catch
155 * all possible error conditions.
157 int mk_date (const char *datestr, struct rtc_time *tmp)
162 ptr = strchr (datestr,'.');
163 len = strlen (datestr);
170 if ((len - (ptr - datestr)) != 2)
173 len = strlen (datestr);
175 if (cnvrt2 (ptr, &sec))
183 if (len == 12) { /* MMDDhhmmCCYY */
186 if (cnvrt2 (datestr+ 8, ¢ury) ||
187 cnvrt2 (datestr+10, &year) ) {
190 tmp->tm_year = 100 * century + year;
191 } else if (len == 10) { /* MMDDhhmmYY */
194 century = tmp->tm_year / 100;
195 if (cnvrt2 (datestr+ 8, &year))
197 tmp->tm_year = 100 * century + year;
201 case 8: /* MMDDhhmm */
203 case 10: /* MMDDhhmmYY */
205 case 12: /* MMDDhhmmCCYY */
206 if (cnvrt2 (datestr+0, &val) ||
211 if (cnvrt2 (datestr+2, &val) ||
212 val > ((tmp->tm_mon==2) ? 29 : 31)) {
217 if (cnvrt2 (datestr+4, &val) ||
223 if (cnvrt2 (datestr+6, &val) ||
229 /* calculate day of week */
230 rtc_calc_weekday(tmp);
240 /***************************************************/
244 "get/set/reset date & time",
245 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
246 " - without arguments: print date & time\n"
247 " - with numeric argument: set the system date & time\n"
248 " - with 'reset' argument: reset the RTC"