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