1 // SPDX-License-Identifier: GPL-2.0+
8 * RTC, Date & Time support: get and set date & time
14 #include <asm/global_data.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 static const char * const weekdays[] = {
19 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
22 int mk_date (const char *, struct rtc_time *);
24 static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
26 static int do_date(struct cmd_tbl *cmdtp, int flag, int argc,
31 int old_bus __maybe_unused;
33 /* switch to correct I2C bus */
37 rcode = uclass_get_device_by_seq(UCLASS_RTC, 0, &dev);
39 rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
41 printf("Cannot find RTC: err=%d\n", rcode);
42 return CMD_RET_FAILURE;
45 #elif CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
46 old_bus = i2c_get_bus_num();
47 i2c_set_bus_num(CFG_SYS_RTC_BUS_NUM);
49 old_bus = I2C_GET_BUS();
50 I2C_SET_BUS(CFG_SYS_RTC_BUS_NUM);
54 case 2: /* set date & time */
55 if (strcmp(argv[1],"reset") == 0) {
56 puts ("Reset RTC...\n");
58 rcode = dm_rtc_reset(dev);
60 rcode = dm_rtc_set(dev, &default_tm);
63 rcode = rtc_set(&default_tm);
66 puts("## Failed to set date after RTC reset\n");
68 /* initialize tm with current time */
70 rcode = dm_rtc_get(dev, &tm);
75 /* insert new date & time */
76 if (mk_date(argv[1], &tm) != 0) {
77 puts ("## Bad date format\n");
80 /* and write to RTC */
82 rcode = dm_rtc_set(dev, &tm);
87 printf("## Set date failed: err=%d\n",
91 puts("## Get date failed\n");
95 case 1: /* get date & time */
97 rcode = dm_rtc_get(dev, &tm);
102 puts("## Get date failed\n");
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) ?
109 "unknown " : weekdays[tm.tm_wday],
110 tm.tm_hour, tm.tm_min, tm.tm_sec);
114 rcode = CMD_RET_USAGE;
117 /* switch back to original I2C bus */
118 #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
119 i2c_set_bus_num(old_bus);
120 #elif !defined(CONFIG_DM_RTC)
121 I2C_SET_BUS(old_bus);
124 return rcode ? CMD_RET_FAILURE : 0;
128 * simple conversion of two-digit string with error checking
130 static int cnvrt2 (const char *str, int *valp)
134 if ((*str < '0') || (*str > '9'))
141 if ((*str < '0') || (*str > '9'))
144 *valp = 10 * val + (*str - '0');
150 * Convert date string: MMDDhhmm[[CC]YY][.ss]
152 * Some basic checking for valid values is done, but this will not catch
153 * all possible error conditions.
155 int mk_date (const char *datestr, struct rtc_time *tmp)
160 ptr = strchr(datestr, '.');
161 len = strlen(datestr);
168 if ((len - (ptr - datestr)) != 2)
173 if (cnvrt2 (ptr, &sec))
181 if (len == 12) { /* MMDDhhmmCCYY */
184 if (cnvrt2 (datestr+ 8, ¢ury) ||
185 cnvrt2 (datestr+10, &year) ) {
188 tmp->tm_year = 100 * century + year;
189 } else if (len == 10) { /* MMDDhhmmYY */
192 century = tmp->tm_year / 100;
193 if (cnvrt2 (datestr+ 8, &year))
195 tmp->tm_year = 100 * century + year;
199 case 8: /* MMDDhhmm */
201 case 10: /* MMDDhhmmYY */
203 case 12: /* MMDDhhmmCCYY */
204 if (cnvrt2 (datestr+0, &val) ||
209 if (cnvrt2 (datestr+2, &val) ||
210 val > ((tmp->tm_mon==2) ? 29 : 31)) {
215 if (cnvrt2 (datestr+4, &val) ||
221 if (cnvrt2 (datestr+6, &val) ||
227 /* calculate day of week */
228 rtc_calc_weekday(tmp);
238 /***************************************************/
242 "get/set/reset date & time",
243 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
244 " - without arguments: print date & time\n"
245 " - with numeric argument: set the system date & time\n"
246 " - with 'reset' argument: reset the RTC"