blob: 751159847a487da0fd3c034d355417eed03ca646 [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
wdenk3e386912003-04-05 00:53:31 +000038#define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
39
wdenk38635852002-08-27 05:55:31 +000040int mk_date (char *, struct rtc_time *);
41
42int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
43{
44 struct rtc_time tm;
45 int rcode = 0;
Stefan Roese0dc018e2007-02-20 10:51:26 +010046 int old_bus;
47
48 /* switch to correct I2C bus */
49 old_bus = I2C_GET_BUS();
50 I2C_SET_BUS(CFG_RTC_BUS_NUM);
wdenk38635852002-08-27 05:55:31 +000051
52 switch (argc) {
53 case 2: /* set date & time */
54 if (strcmp(argv[1],"reset") == 0) {
wdenk4b9206e2004-03-23 22:14:11 +000055 puts ("Reset RTC...\n");
wdenk38635852002-08-27 05:55:31 +000056 rtc_reset ();
57 } else {
58 /* initialize tm with current time */
59 rtc_get (&tm);
60 /* insert new date & time */
61 if (mk_date (argv[1], &tm) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +000062 puts ("## Bad date format\n");
Stefan Roese0dc018e2007-02-20 10:51:26 +010063 break;
wdenk38635852002-08-27 05:55:31 +000064 }
65 /* and write to RTC */
66 rtc_set (&tm);
67 }
68 /* FALL TROUGH */
69 case 1: /* get date & time */
70 rtc_get (&tm);
71
72 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
73 tm.tm_year, tm.tm_mon, tm.tm_mday,
74 (tm.tm_wday<0 || tm.tm_wday>6) ?
wdenk3e386912003-04-05 00:53:31 +000075 "unknown " : RELOC(weekdays[tm.tm_wday]),
wdenk38635852002-08-27 05:55:31 +000076 tm.tm_hour, tm.tm_min, tm.tm_sec);
77
Stefan Roese0dc018e2007-02-20 10:51:26 +010078 break;
wdenk38635852002-08-27 05:55:31 +000079 default:
80 printf ("Usage:\n%s\n", cmdtp->usage);
81 rcode = 1;
82 }
Stefan Roese0dc018e2007-02-20 10:51:26 +010083
84 /* switch back to original I2C bus */
85 I2C_SET_BUS(old_bus);
86
wdenk38635852002-08-27 05:55:31 +000087 return rcode;
88}
89
90/*
91 * simple conversion of two-digit string with error checking
92 */
93static int cnvrt2 (char *str, int *valp)
94{
95 int val;
96
97 if ((*str < '0') || (*str > '9'))
98 return (-1);
99
100 val = *str - '0';
101
102 ++str;
103
104 if ((*str < '0') || (*str > '9'))
105 return (-1);
106
107 *valp = 10 * val + (*str - '0');
108
109 return (0);
110}
111
112/*
113 * Convert date string: MMDDhhmm[[CC]YY][.ss]
114 *
115 * Some basic checking for valid values is done, but this will not catch
116 * all possible error conditions.
117 */
118int mk_date (char *datestr, struct rtc_time *tmp)
119{
120 int len, val;
121 char *ptr;
122
123 ptr = strchr (datestr,'.');
124 len = strlen (datestr);
125
126 /* Set seconds */
127 if (ptr) {
128 int sec;
129
130 *ptr++ = '\0';
131 if ((len - (ptr - datestr)) != 2)
132 return (-1);
133
134 len = strlen (datestr);
135
136 if (cnvrt2 (ptr, &sec))
137 return (-1);
138
139 tmp->tm_sec = sec;
140 } else {
141 tmp->tm_sec = 0;
142 }
143
144 if (len == 12) { /* MMDDhhmmCCYY */
145 int year, century;
146
147 if (cnvrt2 (datestr+ 8, &century) ||
148 cnvrt2 (datestr+10, &year) ) {
149 return (-1);
150 }
151 tmp->tm_year = 100 * century + year;
152 } else if (len == 10) { /* MMDDhhmmYY */
153 int year, century;
154
155 century = tmp->tm_year / 100;
156 if (cnvrt2 (datestr+ 8, &year))
157 return (-1);
158 tmp->tm_year = 100 * century + year;
159 }
160
161 switch (len) {
162 case 8: /* MMDDhhmm */
163 /* fall thru */
164 case 10: /* MMDDhhmmYY */
165 /* fall thru */
166 case 12: /* MMDDhhmmCCYY */
167 if (cnvrt2 (datestr+0, &val) ||
168 val > 12) {
169 break;
170 }
171 tmp->tm_mon = val;
172 if (cnvrt2 (datestr+2, &val) ||
173 val > ((tmp->tm_mon==2) ? 29 : 31)) {
174 break;
175 }
176 tmp->tm_mday = val;
177
178 if (cnvrt2 (datestr+4, &val) ||
179 val > 23) {
180 break;
181 }
182 tmp->tm_hour = val;
183
184 if (cnvrt2 (datestr+6, &val) ||
185 val > 59) {
186 break;
187 }
188 tmp->tm_min = val;
189
190 /* calculate day of week */
191 GregorianDay (tmp);
192
193 return (0);
194 default:
195 break;
196 }
197
198 return (-1);
199}
200
wdenk8bde7f72003-06-27 21:31:46 +0000201/***************************************************/
202
wdenk0d498392003-07-01 21:06:45 +0000203U_BOOT_CMD(
204 date, 2, 1, do_date,
wdenk8bde7f72003-06-27 21:31:46 +0000205 "date - get/set/reset date & time\n",
206 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
207 " - without arguments: print date & time\n"
208 " - with numeric argument: set the system date & time\n"
209 " - with 'reset' argument: reset the RTC\n"
210);