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
15 #include <asm/global_data.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 static const char * const weekdays[] = {
20 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
23 #ifdef CONFIG_NEEDS_MANUAL_RELOC
24 #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
29 int mk_date (const char *, struct rtc_time *);
31 static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
33 static int do_date(struct cmd_tbl *cmdtp, int flag, int argc,
38 int old_bus __maybe_unused;
40 /* switch to correct I2C bus */
44 rcode = uclass_get_device_by_seq(UCLASS_RTC, 0, &dev);
46 rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
48 printf("Cannot find RTC: err=%d\n", rcode);
49 return CMD_RET_FAILURE;
52 #elif CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
53 old_bus = i2c_get_bus_num();
54 i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
56 old_bus = I2C_GET_BUS();
57 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
61 case 2: /* set date & time */
62 if (strcmp(argv[1],"reset") == 0) {
63 puts ("Reset RTC...\n");
65 rcode = dm_rtc_reset(dev);
67 rcode = dm_rtc_set(dev, &default_tm);
70 rcode = rtc_set(&default_tm);
73 puts("## Failed to set date after RTC reset\n");
75 /* initialize tm with current time */
77 rcode = dm_rtc_get(dev, &tm);
82 /* insert new date & time */
83 if (mk_date(argv[1], &tm) != 0) {
84 puts ("## Bad date format\n");
87 /* and write to RTC */
89 rcode = dm_rtc_set(dev, &tm);
94 printf("## Set date failed: err=%d\n",
98 puts("## Get date failed\n");
102 case 1: /* get date & time */
104 rcode = dm_rtc_get(dev, &tm);
106 rcode = rtc_get(&tm);
109 puts("## Get date failed\n");
113 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
114 tm.tm_year, tm.tm_mon, tm.tm_mday,
115 (tm.tm_wday<0 || tm.tm_wday>6) ?
116 "unknown " : RELOC(weekdays[tm.tm_wday]),
117 tm.tm_hour, tm.tm_min, tm.tm_sec);
121 rcode = CMD_RET_USAGE;
124 /* switch back to original I2C bus */
125 #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
126 i2c_set_bus_num(old_bus);
127 #elif !defined(CONFIG_DM_RTC)
128 I2C_SET_BUS(old_bus);
131 return rcode ? CMD_RET_FAILURE : 0;
135 * simple conversion of two-digit string with error checking
137 static int cnvrt2 (const char *str, int *valp)
141 if ((*str < '0') || (*str > '9'))
148 if ((*str < '0') || (*str > '9'))
151 *valp = 10 * val + (*str - '0');
157 * Convert date string: MMDDhhmm[[CC]YY][.ss]
159 * Some basic checking for valid values is done, but this will not catch
160 * all possible error conditions.
162 int mk_date (const char *datestr, struct rtc_time *tmp)
167 ptr = strchr(datestr, '.');
168 len = strlen(datestr);
175 if ((len - (ptr - datestr)) != 2)
180 if (cnvrt2 (ptr, &sec))
188 if (len == 12) { /* MMDDhhmmCCYY */
191 if (cnvrt2 (datestr+ 8, ¢ury) ||
192 cnvrt2 (datestr+10, &year) ) {
195 tmp->tm_year = 100 * century + year;
196 } else if (len == 10) { /* MMDDhhmmYY */
199 century = tmp->tm_year / 100;
200 if (cnvrt2 (datestr+ 8, &year))
202 tmp->tm_year = 100 * century + year;
206 case 8: /* MMDDhhmm */
208 case 10: /* MMDDhhmmYY */
210 case 12: /* MMDDhhmmCCYY */
211 if (cnvrt2 (datestr+0, &val) ||
216 if (cnvrt2 (datestr+2, &val) ||
217 val > ((tmp->tm_mon==2) ? 29 : 31)) {
222 if (cnvrt2 (datestr+4, &val) ||
228 if (cnvrt2 (datestr+6, &val) ||
234 /* calculate day of week */
235 rtc_calc_weekday(tmp);
245 /***************************************************/
249 "get/set/reset date & time",
250 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
251 " - without arguments: print date & time\n"
252 " - with numeric argument: set the system date & time\n"
253 " - with 'reset' argument: reset the RTC"