1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2001
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 /*
8  * RTC, Date & Time support: get and set date & time
9  */
10 #include <common.h>
11 #include <command.h>
12 #include <dm.h>
13 #include <rtc.h>
14 #include <i2c.h>
15 #include <asm/global_data.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
19 static const char * const weekdays[] = {
20 	"Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
21 };
22 
23 #ifdef CONFIG_NEEDS_MANUAL_RELOC
24 #define RELOC(a)	((typeof(a))((unsigned long)(a) + gd->reloc_off))
25 #else
26 #define RELOC(a)	a
27 #endif
28 
29 int mk_date (const char *, struct rtc_time *);
30 
31 static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
32 
do_date(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])33 static int do_date(struct cmd_tbl *cmdtp, int flag, int argc,
34 		   char *const argv[])
35 {
36 	struct rtc_time tm;
37 	int rcode = 0;
38 	int old_bus __maybe_unused;
39 
40 	/* switch to correct I2C bus */
41 #ifdef CONFIG_DM_RTC
42 	struct udevice *dev;
43 
44 	rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
45 	if (rcode) {
46 		printf("Cannot find RTC: err=%d\n", rcode);
47 		return CMD_RET_FAILURE;
48 	}
49 #elif defined(CONFIG_SYS_I2C)
50 	old_bus = i2c_get_bus_num();
51 	i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
52 #else
53 	old_bus = I2C_GET_BUS();
54 	I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
55 #endif
56 
57 	switch (argc) {
58 	case 2:			/* set date & time */
59 		if (strcmp(argv[1],"reset") == 0) {
60 			puts ("Reset RTC...\n");
61 #ifdef CONFIG_DM_RTC
62 			rcode = dm_rtc_reset(dev);
63 			if (!rcode)
64 				rcode = dm_rtc_set(dev, &default_tm);
65 #else
66 			rtc_reset();
67 			rcode = rtc_set(&default_tm);
68 #endif
69 			if (rcode)
70 				puts("## Failed to set date after RTC reset\n");
71 		} else {
72 			/* initialize tm with current time */
73 #ifdef CONFIG_DM_RTC
74 			rcode = dm_rtc_get(dev, &tm);
75 #else
76 			rcode = rtc_get(&tm);
77 #endif
78 			if (!rcode) {
79 				/* insert new date & time */
80 				if (mk_date(argv[1], &tm) != 0) {
81 					puts ("## Bad date format\n");
82 					break;
83 				}
84 				/* and write to RTC */
85 #ifdef CONFIG_DM_RTC
86 				rcode = dm_rtc_set(dev, &tm);
87 #else
88 				rcode = rtc_set(&tm);
89 #endif
90 				if (rcode) {
91 					printf("## Set date failed: err=%d\n",
92 					       rcode);
93 				}
94 			} else {
95 				puts("## Get date failed\n");
96 			}
97 		}
98 		/* FALL TROUGH */
99 	case 1:			/* get date & time */
100 #ifdef CONFIG_DM_RTC
101 		rcode = dm_rtc_get(dev, &tm);
102 #else
103 		rcode = rtc_get(&tm);
104 #endif
105 		if (rcode) {
106 			puts("## Get date failed\n");
107 			break;
108 		}
109 
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);
115 
116 		break;
117 	default:
118 		rcode = CMD_RET_USAGE;
119 	}
120 
121 	/* switch back to original I2C bus */
122 #ifdef CONFIG_SYS_I2C
123 	i2c_set_bus_num(old_bus);
124 #elif !defined(CONFIG_DM_RTC)
125 	I2C_SET_BUS(old_bus);
126 #endif
127 
128 	return rcode ? CMD_RET_FAILURE : 0;
129 }
130 
131 /*
132  * simple conversion of two-digit string with error checking
133  */
cnvrt2(const char * str,int * valp)134 static int cnvrt2 (const char *str, int *valp)
135 {
136 	int val;
137 
138 	if ((*str < '0') || (*str > '9'))
139 		return (-1);
140 
141 	val = *str - '0';
142 
143 	++str;
144 
145 	if ((*str < '0') || (*str > '9'))
146 		return (-1);
147 
148 	*valp = 10 * val + (*str - '0');
149 
150 	return (0);
151 }
152 
153 /*
154  * Convert date string: MMDDhhmm[[CC]YY][.ss]
155  *
156  * Some basic checking for valid values is done, but this will not catch
157  * all possible error conditions.
158  */
mk_date(const char * datestr,struct rtc_time * tmp)159 int mk_date (const char *datestr, struct rtc_time *tmp)
160 {
161 	int len, val;
162 	char *ptr;
163 
164 	ptr = strchr(datestr, '.');
165 	len = strlen(datestr);
166 
167 	/* Set seconds */
168 	if (ptr) {
169 		int sec;
170 
171 		ptr++;
172 		if ((len - (ptr - datestr)) != 2)
173 			return (-1);
174 
175 		len -= 3;
176 
177 		if (cnvrt2 (ptr, &sec))
178 			return (-1);
179 
180 		tmp->tm_sec = sec;
181 	} else {
182 		tmp->tm_sec = 0;
183 	}
184 
185 	if (len == 12) {		/* MMDDhhmmCCYY	*/
186 		int year, century;
187 
188 		if (cnvrt2 (datestr+ 8, &century) ||
189 		    cnvrt2 (datestr+10, &year) ) {
190 			return (-1);
191 		}
192 		tmp->tm_year = 100 * century + year;
193 	} else if (len == 10) {		/* MMDDhhmmYY	*/
194 		int year, century;
195 
196 		century = tmp->tm_year / 100;
197 		if (cnvrt2 (datestr+ 8, &year))
198 			return (-1);
199 		tmp->tm_year = 100 * century + year;
200 	}
201 
202 	switch (len) {
203 	case 8:			/* MMDDhhmm	*/
204 		/* fall thru */
205 	case 10:		/* MMDDhhmmYY	*/
206 		/* fall thru */
207 	case 12:		/* MMDDhhmmCCYY	*/
208 		if (cnvrt2 (datestr+0, &val) ||
209 		    val > 12) {
210 			break;
211 		}
212 		tmp->tm_mon  = val;
213 		if (cnvrt2 (datestr+2, &val) ||
214 		    val > ((tmp->tm_mon==2) ? 29 : 31)) {
215 			break;
216 		}
217 		tmp->tm_mday = val;
218 
219 		if (cnvrt2 (datestr+4, &val) ||
220 		    val > 23) {
221 			break;
222 		}
223 		tmp->tm_hour = val;
224 
225 		if (cnvrt2 (datestr+6, &val) ||
226 		    val > 59) {
227 			break;
228 		}
229 		tmp->tm_min  = val;
230 
231 		/* calculate day of week */
232 		rtc_calc_weekday(tmp);
233 
234 		return (0);
235 	default:
236 		break;
237 	}
238 
239 	return (-1);
240 }
241 
242 /***************************************************/
243 
244 U_BOOT_CMD(
245 	date,	2,	1,	do_date,
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"
251 );
252