blob: f7ae1da1e246491ada89b3abba64d8b66486e973 [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
Mike Frysingerbdbc1302010-10-20 07:17:23 -040034static const char * const weekdays[] = {
wdenk38635852002-08-27 05:55:31 +000035 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
36};
37
Wolfgang Denk2e5167c2010-10-28 20:00:11 +020038#ifdef CONFIG_NEEDS_MANUAL_RELOC
wdenk3e386912003-04-05 00:53:31 +000039#define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
Wolfgang Denk2e5167c2010-10-28 20:00:11 +020040#else
41#define RELOC(a) a
Peter Tyser521af042009-09-21 11:20:36 -050042#endif
wdenk3e386912003-04-05 00:53:31 +000043
Mike Frysingerbdbc1302010-10-20 07:17:23 -040044int mk_date (const char *, struct rtc_time *);
wdenk38635852002-08-27 05:55:31 +000045
Kim Phillips088f1b12012-10-29 13:34:31 +000046static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000047{
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 */
Heiko Schocher3f4978c2012-01-16 21:12:24 +000053#ifdef CONFIG_SYS_I2C
54 old_bus = i2c_get_bus_num();
55 i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
56#else
Stefan Roese0dc018e2007-02-20 10:51:26 +010057 old_bus = I2C_GET_BUS();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020058 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
Heiko Schocher3f4978c2012-01-16 21:12:24 +000059#endif
wdenk38635852002-08-27 05:55:31 +000060
61 switch (argc) {
62 case 2: /* set date & time */
63 if (strcmp(argv[1],"reset") == 0) {
wdenk4b9206e2004-03-23 22:14:11 +000064 puts ("Reset RTC...\n");
wdenk38635852002-08-27 05:55:31 +000065 rtc_reset ();
66 } else {
67 /* initialize tm with current time */
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020068 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)
Magnus Liljad52e3e02009-11-11 19:56:36 +010079 puts("## Set date failed\n");
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020080 } else {
Magnus Liljad52e3e02009-11-11 19:56:36 +010081 puts("## Get date failed\n");
wdenk38635852002-08-27 05:55:31 +000082 }
wdenk38635852002-08-27 05:55:31 +000083 }
84 /* FALL TROUGH */
85 case 1: /* get date & time */
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020086 rcode = rtc_get (&tm);
87
88 if (rcode) {
Magnus Liljad52e3e02009-11-11 19:56:36 +010089 puts("## Get date failed\n");
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020090 break;
91 }
wdenk38635852002-08-27 05:55:31 +000092
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) ?
wdenk3e386912003-04-05 00:53:31 +000096 "unknown " : RELOC(weekdays[tm.tm_wday]),
wdenk38635852002-08-27 05:55:31 +000097 tm.tm_hour, tm.tm_min, tm.tm_sec);
98
Stefan Roese0dc018e2007-02-20 10:51:26 +010099 break;
wdenk38635852002-08-27 05:55:31 +0000100 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000101 rcode = CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000102 }
Stefan Roese0dc018e2007-02-20 10:51:26 +0100103
104 /* switch back to original I2C bus */
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000105#ifdef CONFIG_SYS_I2C
106 i2c_set_bus_num(old_bus);
107#else
Stefan Roese0dc018e2007-02-20 10:51:26 +0100108 I2C_SET_BUS(old_bus);
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000109#endif
Stefan Roese0dc018e2007-02-20 10:51:26 +0100110
wdenk38635852002-08-27 05:55:31 +0000111 return rcode;
112}
113
114/*
115 * simple conversion of two-digit string with error checking
116 */
Mike Frysingerbdbc1302010-10-20 07:17:23 -0400117static int cnvrt2 (const char *str, int *valp)
wdenk38635852002-08-27 05:55:31 +0000118{
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 */
Mike Frysingerbdbc1302010-10-20 07:17:23 -0400142int mk_date (const char *datestr, struct rtc_time *tmp)
wdenk38635852002-08-27 05:55:31 +0000143{
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, &century) ||
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
wdenk8bde7f72003-06-27 21:31:46 +0000225/***************************************************/
226
wdenk0d498392003-07-01 21:06:45 +0000227U_BOOT_CMD(
228 date, 2, 1, do_date,
Peter Tyser2fb26042009-01-27 18:03:12 -0600229 "get/set/reset date & time",
wdenk8bde7f72003-06-27 21:31:46 +0000230 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
231 " - without arguments: print date & time\n"
232 " - with numeric argument: set the system date & time\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200233 " - with 'reset' argument: reset the RTC"
wdenk8bde7f72003-06-27 21:31:46 +0000234);