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