blob: 3141a3968aebaf27cd4a3e55eca39f07cb97508c [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
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>
Stefan Roese0dc018e2007-02-20 10:51:26 +010030#include <i2c.h>
wdenk38635852002-08-27 05:55:31 +000031
Wolfgang Denkd87080b2006-03-31 18:32:53 +020032DECLARE_GLOBAL_DATA_PTR;
33
wdenk38635852002-08-27 05:55:31 +000034const char *weekdays[] = {
35 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
36};
37
Peter Tyser521af042009-09-21 11:20:36 -050038#ifdef CONFIG_RELOC_FIXUP_WORKS
39#define RELOC(a) a
40#else
wdenk3e386912003-04-05 00:53:31 +000041#define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
Peter Tyser521af042009-09-21 11:20:36 -050042#endif
wdenk3e386912003-04-05 00:53:31 +000043
wdenk38635852002-08-27 05:55:31 +000044int mk_date (char *, struct rtc_time *);
45
46int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
47{
48 struct rtc_time tm;
49 int rcode = 0;
Stefan Roese0dc018e2007-02-20 10:51:26 +010050 int old_bus;
51
52 /* switch to correct I2C bus */
53 old_bus = I2C_GET_BUS();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020054 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
wdenk38635852002-08-27 05:55:31 +000055
56 switch (argc) {
57 case 2: /* set date & time */
58 if (strcmp(argv[1],"reset") == 0) {
wdenk4b9206e2004-03-23 22:14:11 +000059 puts ("Reset RTC...\n");
wdenk38635852002-08-27 05:55:31 +000060 rtc_reset ();
61 } else {
62 /* initialize tm with current time */
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020063 rcode = rtc_get (&tm);
64
65 if(!rcode) {
66 /* insert new date & time */
67 if (mk_date (argv[1], &tm) != 0) {
68 puts ("## Bad date format\n");
69 break;
70 }
71 /* and write to RTC */
72 rcode = rtc_set (&tm);
73 if(rcode)
Magnus Liljad52e3e02009-11-11 19:56:36 +010074 puts("## Set date failed\n");
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020075 } else {
Magnus Liljad52e3e02009-11-11 19:56:36 +010076 puts("## Get date failed\n");
wdenk38635852002-08-27 05:55:31 +000077 }
wdenk38635852002-08-27 05:55:31 +000078 }
79 /* FALL TROUGH */
80 case 1: /* get date & time */
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020081 rcode = rtc_get (&tm);
82
83 if (rcode) {
Magnus Liljad52e3e02009-11-11 19:56:36 +010084 puts("## Get date failed\n");
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020085 break;
86 }
wdenk38635852002-08-27 05:55:31 +000087
88 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
89 tm.tm_year, tm.tm_mon, tm.tm_mday,
90 (tm.tm_wday<0 || tm.tm_wday>6) ?
wdenk3e386912003-04-05 00:53:31 +000091 "unknown " : RELOC(weekdays[tm.tm_wday]),
wdenk38635852002-08-27 05:55:31 +000092 tm.tm_hour, tm.tm_min, tm.tm_sec);
93
Stefan Roese0dc018e2007-02-20 10:51:26 +010094 break;
wdenk38635852002-08-27 05:55:31 +000095 default:
Peter Tyser62c3ae72009-01-27 18:03:10 -060096 cmd_usage(cmdtp);
wdenk38635852002-08-27 05:55:31 +000097 rcode = 1;
98 }
Stefan Roese0dc018e2007-02-20 10:51:26 +010099
100 /* switch back to original I2C bus */
101 I2C_SET_BUS(old_bus);
102
wdenk38635852002-08-27 05:55:31 +0000103 return rcode;
104}
105
106/*
107 * simple conversion of two-digit string with error checking
108 */
109static int cnvrt2 (char *str, int *valp)
110{
111 int val;
112
113 if ((*str < '0') || (*str > '9'))
114 return (-1);
115
116 val = *str - '0';
117
118 ++str;
119
120 if ((*str < '0') || (*str > '9'))
121 return (-1);
122
123 *valp = 10 * val + (*str - '0');
124
125 return (0);
126}
127
128/*
129 * Convert date string: MMDDhhmm[[CC]YY][.ss]
130 *
131 * Some basic checking for valid values is done, but this will not catch
132 * all possible error conditions.
133 */
134int mk_date (char *datestr, struct rtc_time *tmp)
135{
136 int len, val;
137 char *ptr;
138
139 ptr = strchr (datestr,'.');
140 len = strlen (datestr);
141
142 /* Set seconds */
143 if (ptr) {
144 int sec;
145
146 *ptr++ = '\0';
147 if ((len - (ptr - datestr)) != 2)
148 return (-1);
149
150 len = strlen (datestr);
151
152 if (cnvrt2 (ptr, &sec))
153 return (-1);
154
155 tmp->tm_sec = sec;
156 } else {
157 tmp->tm_sec = 0;
158 }
159
160 if (len == 12) { /* MMDDhhmmCCYY */
161 int year, century;
162
163 if (cnvrt2 (datestr+ 8, &century) ||
164 cnvrt2 (datestr+10, &year) ) {
165 return (-1);
166 }
167 tmp->tm_year = 100 * century + year;
168 } else if (len == 10) { /* MMDDhhmmYY */
169 int year, century;
170
171 century = tmp->tm_year / 100;
172 if (cnvrt2 (datestr+ 8, &year))
173 return (-1);
174 tmp->tm_year = 100 * century + year;
175 }
176
177 switch (len) {
178 case 8: /* MMDDhhmm */
179 /* fall thru */
180 case 10: /* MMDDhhmmYY */
181 /* fall thru */
182 case 12: /* MMDDhhmmCCYY */
183 if (cnvrt2 (datestr+0, &val) ||
184 val > 12) {
185 break;
186 }
187 tmp->tm_mon = val;
188 if (cnvrt2 (datestr+2, &val) ||
189 val > ((tmp->tm_mon==2) ? 29 : 31)) {
190 break;
191 }
192 tmp->tm_mday = val;
193
194 if (cnvrt2 (datestr+4, &val) ||
195 val > 23) {
196 break;
197 }
198 tmp->tm_hour = val;
199
200 if (cnvrt2 (datestr+6, &val) ||
201 val > 59) {
202 break;
203 }
204 tmp->tm_min = val;
205
206 /* calculate day of week */
207 GregorianDay (tmp);
208
209 return (0);
210 default:
211 break;
212 }
213
214 return (-1);
215}
216
wdenk8bde7f72003-06-27 21:31:46 +0000217/***************************************************/
218
wdenk0d498392003-07-01 21:06:45 +0000219U_BOOT_CMD(
220 date, 2, 1, do_date,
Peter Tyser2fb26042009-01-27 18:03:12 -0600221 "get/set/reset date & time",
wdenk8bde7f72003-06-27 21:31:46 +0000222 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
223 " - without arguments: print date & time\n"
224 " - with numeric argument: set the system date & time\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200225 " - with 'reset' argument: reset the RTC"
wdenk8bde7f72003-06-27 21:31:46 +0000226);