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(UCLASS_RTC, 0, &dev);
46 printf("Cannot find RTC: err=%d\n", rcode);
47 return CMD_RET_FAILURE;
49 #elif defined(CONFIG_SYS_I2C_LEGACY)
50 old_bus = i2c_get_bus_num();
51 i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
53 old_bus = I2C_GET_BUS();
54 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
58 case 2: /* set date & time */
59 if (strcmp(argv[1],"reset") == 0) {
60 puts ("Reset RTC...\n");
62 rcode = dm_rtc_reset(dev);
64 rcode = dm_rtc_set(dev, &default_tm);
67 rcode = rtc_set(&default_tm);
70 puts("## Failed to set date after RTC reset\n");
72 /* initialize tm with current time */
74 rcode = dm_rtc_get(dev, &tm);
79 /* insert new date & time */
80 if (mk_date(argv[1], &tm) != 0) {
81 puts ("## Bad date format\n");
84 /* and write to RTC */
86 rcode = dm_rtc_set(dev, &tm);
91 printf("## Set date failed: err=%d\n",
95 puts("## Get date failed\n");
99 case 1: /* get date & time */
101 rcode = dm_rtc_get(dev, &tm);
103 rcode = rtc_get(&tm);
106 puts("## Get date failed\n");
110 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
111 tm.tm_year, tm.tm_mon, tm.tm_mday,
112 (tm.tm_wday<0 || tm.tm_wday>6) ?
113 "unknown " : RELOC(weekdays[tm.tm_wday]),
114 tm.tm_hour, tm.tm_min, tm.tm_sec);
118 rcode = CMD_RET_USAGE;
121 /* switch back to original I2C bus */
122 #ifdef CONFIG_SYS_I2C_LEGACY
123 i2c_set_bus_num(old_bus);
124 #elif !defined(CONFIG_DM_RTC)
125 I2C_SET_BUS(old_bus);
128 return rcode ? CMD_RET_FAILURE : 0;
132 * simple conversion of two-digit string with error checking
134 static int cnvrt2 (const char *str, int *valp)
138 if ((*str < '0') || (*str > '9'))
145 if ((*str < '0') || (*str > '9'))
148 *valp = 10 * val + (*str - '0');
154 * Convert date string: MMDDhhmm[[CC]YY][.ss]
156 * Some basic checking for valid values is done, but this will not catch
157 * all possible error conditions.
159 int mk_date (const char *datestr, struct rtc_time *tmp)
164 ptr = strchr(datestr, '.');
165 len = strlen(datestr);
172 if ((len - (ptr - datestr)) != 2)
177 if (cnvrt2 (ptr, &sec))
185 if (len == 12) { /* MMDDhhmmCCYY */
188 if (cnvrt2 (datestr+ 8, ¢ury) ||
189 cnvrt2 (datestr+10, &year) ) {
192 tmp->tm_year = 100 * century + year;
193 } else if (len == 10) { /* MMDDhhmmYY */
196 century = tmp->tm_year / 100;
197 if (cnvrt2 (datestr+ 8, &year))
199 tmp->tm_year = 100 * century + year;
203 case 8: /* MMDDhhmm */
205 case 10: /* MMDDhhmmYY */
207 case 12: /* MMDDhhmmCCYY */
208 if (cnvrt2 (datestr+0, &val) ||
213 if (cnvrt2 (datestr+2, &val) ||
214 val > ((tmp->tm_mon==2) ? 29 : 31)) {
219 if (cnvrt2 (datestr+4, &val) ||
225 if (cnvrt2 (datestr+6, &val) ||
231 /* calculate day of week */
232 rtc_calc_weekday(tmp);
242 /***************************************************/
246 "get/set/reset date & time",
247 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
248 " - without arguments: print date & time\n"
249 " - with numeric argument: set the system date & time\n"
250 " - with 'reset' argument: reset the RTC"