]>
Commit | Line | Data |
---|---|---|
3863585b WD |
1 | /* |
2 | * (C) Copyright 2001 | |
3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
4 | * | |
5 | * See file CREDITS for list of people who contributed to this | |
6 | * project. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License as | |
10 | * published by the Free Software Foundation; either version 2 of | |
11 | * the License, or (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
21 | * MA 02111-1307 USA | |
22 | */ | |
23 | ||
24 | /* | |
25 | * RTC, Date & Time support: get and set date & time | |
26 | */ | |
27 | #include <common.h> | |
28 | #include <command.h> | |
29 | #include <rtc.h> | |
0dc018ec | 30 | #include <i2c.h> |
3863585b | 31 | |
d87080b7 WD |
32 | DECLARE_GLOBAL_DATA_PTR; |
33 | ||
bdbc1303 | 34 | static const char * const weekdays[] = { |
3863585b WD |
35 | "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", |
36 | }; | |
37 | ||
2e5167cc | 38 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
3e38691e | 39 | #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off)) |
2e5167cc WD |
40 | #else |
41 | #define RELOC(a) a | |
521af04d | 42 | #endif |
3e38691e | 43 | |
bdbc1303 | 44 | int mk_date (const char *, struct rtc_time *); |
3863585b | 45 | |
088f1b19 | 46 | static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
3863585b WD |
47 | { |
48 | struct rtc_time tm; | |
49 | int rcode = 0; | |
0dc018ec SR |
50 | int old_bus; |
51 | ||
52 | /* switch to correct I2C bus */ | |
3f4978c7 HS |
53 | #ifdef CONFIG_SYS_I2C |
54 | old_bus = i2c_get_bus_num(); | |
55 | i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM); | |
56 | #else | |
0dc018ec | 57 | old_bus = I2C_GET_BUS(); |
6d0f6bcf | 58 | I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM); |
3f4978c7 | 59 | #endif |
3863585b WD |
60 | |
61 | switch (argc) { | |
62 | case 2: /* set date & time */ | |
63 | if (strcmp(argv[1],"reset") == 0) { | |
4b9206ed | 64 | puts ("Reset RTC...\n"); |
3863585b WD |
65 | rtc_reset (); |
66 | } else { | |
67 | /* initialize tm with current time */ | |
d1e23194 JCPV |
68 | rcode = rtc_get (&tm); |
69 | ||
70 | if(!rcode) { | |
71 | /* insert new date & time */ | |
72 | if (mk_date (argv[1], &tm) != 0) { | |
73 | puts ("## Bad date format\n"); | |
74 | break; | |
75 | } | |
76 | /* and write to RTC */ | |
77 | rcode = rtc_set (&tm); | |
78 | if(rcode) | |
d52e3e01 | 79 | puts("## Set date failed\n"); |
d1e23194 | 80 | } else { |
d52e3e01 | 81 | puts("## Get date failed\n"); |
3863585b | 82 | } |
3863585b WD |
83 | } |
84 | /* FALL TROUGH */ | |
85 | case 1: /* get date & time */ | |
d1e23194 JCPV |
86 | rcode = rtc_get (&tm); |
87 | ||
88 | if (rcode) { | |
d52e3e01 | 89 | puts("## Get date failed\n"); |
d1e23194 JCPV |
90 | break; |
91 | } | |
3863585b WD |
92 | |
93 | printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n", | |
94 | tm.tm_year, tm.tm_mon, tm.tm_mday, | |
95 | (tm.tm_wday<0 || tm.tm_wday>6) ? | |
3e38691e | 96 | "unknown " : RELOC(weekdays[tm.tm_wday]), |
3863585b WD |
97 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
98 | ||
0dc018ec | 99 | break; |
3863585b | 100 | default: |
4c12eeb8 | 101 | rcode = CMD_RET_USAGE; |
3863585b | 102 | } |
0dc018ec SR |
103 | |
104 | /* switch back to original I2C bus */ | |
3f4978c7 HS |
105 | #ifdef CONFIG_SYS_I2C |
106 | i2c_set_bus_num(old_bus); | |
107 | #else | |
0dc018ec | 108 | I2C_SET_BUS(old_bus); |
3f4978c7 | 109 | #endif |
0dc018ec | 110 | |
3863585b WD |
111 | return rcode; |
112 | } | |
113 | ||
114 | /* | |
115 | * simple conversion of two-digit string with error checking | |
116 | */ | |
bdbc1303 | 117 | static int cnvrt2 (const char *str, int *valp) |
3863585b WD |
118 | { |
119 | int val; | |
120 | ||
121 | if ((*str < '0') || (*str > '9')) | |
122 | return (-1); | |
123 | ||
124 | val = *str - '0'; | |
125 | ||
126 | ++str; | |
127 | ||
128 | if ((*str < '0') || (*str > '9')) | |
129 | return (-1); | |
130 | ||
131 | *valp = 10 * val + (*str - '0'); | |
132 | ||
133 | return (0); | |
134 | } | |
135 | ||
136 | /* | |
137 | * Convert date string: MMDDhhmm[[CC]YY][.ss] | |
138 | * | |
139 | * Some basic checking for valid values is done, but this will not catch | |
140 | * all possible error conditions. | |
141 | */ | |
bdbc1303 | 142 | int mk_date (const char *datestr, struct rtc_time *tmp) |
3863585b WD |
143 | { |
144 | int len, val; | |
145 | char *ptr; | |
146 | ||
147 | ptr = strchr (datestr,'.'); | |
148 | len = strlen (datestr); | |
149 | ||
150 | /* Set seconds */ | |
151 | if (ptr) { | |
152 | int sec; | |
153 | ||
154 | *ptr++ = '\0'; | |
155 | if ((len - (ptr - datestr)) != 2) | |
156 | return (-1); | |
157 | ||
158 | len = strlen (datestr); | |
159 | ||
160 | if (cnvrt2 (ptr, &sec)) | |
161 | return (-1); | |
162 | ||
163 | tmp->tm_sec = sec; | |
164 | } else { | |
165 | tmp->tm_sec = 0; | |
166 | } | |
167 | ||
168 | if (len == 12) { /* MMDDhhmmCCYY */ | |
169 | int year, century; | |
170 | ||
171 | if (cnvrt2 (datestr+ 8, ¢ury) || | |
172 | cnvrt2 (datestr+10, &year) ) { | |
173 | return (-1); | |
174 | } | |
175 | tmp->tm_year = 100 * century + year; | |
176 | } else if (len == 10) { /* MMDDhhmmYY */ | |
177 | int year, century; | |
178 | ||
179 | century = tmp->tm_year / 100; | |
180 | if (cnvrt2 (datestr+ 8, &year)) | |
181 | return (-1); | |
182 | tmp->tm_year = 100 * century + year; | |
183 | } | |
184 | ||
185 | switch (len) { | |
186 | case 8: /* MMDDhhmm */ | |
187 | /* fall thru */ | |
188 | case 10: /* MMDDhhmmYY */ | |
189 | /* fall thru */ | |
190 | case 12: /* MMDDhhmmCCYY */ | |
191 | if (cnvrt2 (datestr+0, &val) || | |
192 | val > 12) { | |
193 | break; | |
194 | } | |
195 | tmp->tm_mon = val; | |
196 | if (cnvrt2 (datestr+2, &val) || | |
197 | val > ((tmp->tm_mon==2) ? 29 : 31)) { | |
198 | break; | |
199 | } | |
200 | tmp->tm_mday = val; | |
201 | ||
202 | if (cnvrt2 (datestr+4, &val) || | |
203 | val > 23) { | |
204 | break; | |
205 | } | |
206 | tmp->tm_hour = val; | |
207 | ||
208 | if (cnvrt2 (datestr+6, &val) || | |
209 | val > 59) { | |
210 | break; | |
211 | } | |
212 | tmp->tm_min = val; | |
213 | ||
214 | /* calculate day of week */ | |
215 | GregorianDay (tmp); | |
216 | ||
217 | return (0); | |
218 | default: | |
219 | break; | |
220 | } | |
221 | ||
222 | return (-1); | |
223 | } | |
224 | ||
8bde7f77 WD |
225 | /***************************************************/ |
226 | ||
0d498393 WD |
227 | U_BOOT_CMD( |
228 | date, 2, 1, do_date, | |
2fb2604d | 229 | "get/set/reset date & time", |
8bde7f77 WD |
230 | "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n" |
231 | " - without arguments: print date & time\n" | |
232 | " - with numeric argument: set the system date & time\n" | |
a89c33db | 233 | " - with 'reset' argument: reset the RTC" |
8bde7f77 | 234 | ); |