blob: 61727e3d1f257c53fce37bb2f5dd41cb9087c783 [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00006 */
7
8/*
9 * RTC, Date & Time support: get and set date & time
10 */
11#include <common.h>
12#include <command.h>
Simon Glassf9951ea2015-04-20 12:37:26 -060013#include <dm.h>
wdenk38635852002-08-27 05:55:31 +000014#include <rtc.h>
Stefan Roese0dc018e2007-02-20 10:51:26 +010015#include <i2c.h>
wdenk38635852002-08-27 05:55:31 +000016
Wolfgang Denkd87080b2006-03-31 18:32:53 +020017DECLARE_GLOBAL_DATA_PTR;
18
Mike Frysingerbdbc1302010-10-20 07:17:23 -040019static const char * const weekdays[] = {
wdenk38635852002-08-27 05:55:31 +000020 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
21};
22
Wolfgang Denk2e5167c2010-10-28 20:00:11 +020023#ifdef CONFIG_NEEDS_MANUAL_RELOC
wdenk3e386912003-04-05 00:53:31 +000024#define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
Wolfgang Denk2e5167c2010-10-28 20:00:11 +020025#else
26#define RELOC(a) a
Peter Tyser521af042009-09-21 11:20:36 -050027#endif
wdenk3e386912003-04-05 00:53:31 +000028
Mike Frysingerbdbc1302010-10-20 07:17:23 -040029int mk_date (const char *, struct rtc_time *);
wdenk38635852002-08-27 05:55:31 +000030
Marek Vasut1a1fa242015-04-10 01:11:54 +020031static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
32
Kim Phillips088f1b12012-10-29 13:34:31 +000033static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000034{
35 struct rtc_time tm;
36 int rcode = 0;
Simon Glassf9951ea2015-04-20 12:37:26 -060037 int old_bus __maybe_unused;
Stefan Roese0dc018e2007-02-20 10:51:26 +010038
39 /* switch to correct I2C bus */
Simon Glassf9951ea2015-04-20 12:37:26 -060040#ifdef CONFIG_DM_I2C
41 struct udevice *dev;
42
43 rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
44 if (rcode) {
45 printf("Cannot find RTC: err=%d\n", rcode);
46 return CMD_RET_FAILURE;
47 }
48#elif defined(CONFIG_SYS_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +000049 old_bus = i2c_get_bus_num();
50 i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
51#else
Stefan Roese0dc018e2007-02-20 10:51:26 +010052 old_bus = I2C_GET_BUS();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020053 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
Heiko Schocher3f4978c2012-01-16 21:12:24 +000054#endif
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");
Simon Glassf9951ea2015-04-20 12:37:26 -060060#ifdef CONFIG_DM_I2C
61 rcode = dm_rtc_reset(dev);
62 if (!rcode)
63 rcode = dm_rtc_set(dev, &default_tm);
64#else
65 rtc_reset();
Marek Vasut1a1fa242015-04-10 01:11:54 +020066 rcode = rtc_set(&default_tm);
Simon Glassf9951ea2015-04-20 12:37:26 -060067#endif
Marek Vasut1a1fa242015-04-10 01:11:54 +020068 if (rcode)
69 puts("## Failed to set date after RTC reset\n");
wdenk38635852002-08-27 05:55:31 +000070 } else {
71 /* initialize tm with current time */
Simon Glassf9951ea2015-04-20 12:37:26 -060072#ifdef CONFIG_DM_I2C
73 rcode = dm_rtc_get(dev, &tm);
74#else
75 rcode = rtc_get(&tm);
76#endif
77 if (!rcode) {
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020078 /* insert new date & time */
Simon Glassf9951ea2015-04-20 12:37:26 -060079 if (mk_date(argv[1], &tm) != 0) {
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020080 puts ("## Bad date format\n");
81 break;
82 }
83 /* and write to RTC */
Simon Glassf9951ea2015-04-20 12:37:26 -060084#ifdef CONFIG_DM_I2C
85 rcode = dm_rtc_set(dev, &tm);
86#else
87 rcode = rtc_set(&tm);
88#endif
89 if (rcode) {
90 printf("## Set date failed: err=%d\n",
91 rcode);
92 }
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020093 } else {
Magnus Liljad52e3e02009-11-11 19:56:36 +010094 puts("## Get date failed\n");
wdenk38635852002-08-27 05:55:31 +000095 }
wdenk38635852002-08-27 05:55:31 +000096 }
97 /* FALL TROUGH */
98 case 1: /* get date & time */
Simon Glassf9951ea2015-04-20 12:37:26 -060099#ifdef CONFIG_DM_I2C
100 rcode = dm_rtc_get(dev, &tm);
101#else
102 rcode = rtc_get(&tm);
103#endif
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200104 if (rcode) {
Magnus Liljad52e3e02009-11-11 19:56:36 +0100105 puts("## Get date failed\n");
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200106 break;
107 }
wdenk38635852002-08-27 05:55:31 +0000108
109 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
110 tm.tm_year, tm.tm_mon, tm.tm_mday,
111 (tm.tm_wday<0 || tm.tm_wday>6) ?
wdenk3e386912003-04-05 00:53:31 +0000112 "unknown " : RELOC(weekdays[tm.tm_wday]),
wdenk38635852002-08-27 05:55:31 +0000113 tm.tm_hour, tm.tm_min, tm.tm_sec);
114
Stefan Roese0dc018e2007-02-20 10:51:26 +0100115 break;
wdenk38635852002-08-27 05:55:31 +0000116 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000117 rcode = CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000118 }
Stefan Roese0dc018e2007-02-20 10:51:26 +0100119
120 /* switch back to original I2C bus */
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000121#ifdef CONFIG_SYS_I2C
122 i2c_set_bus_num(old_bus);
Simon Glassf9951ea2015-04-20 12:37:26 -0600123#elif !defined(CONFIG_DM_I2C)
Stefan Roese0dc018e2007-02-20 10:51:26 +0100124 I2C_SET_BUS(old_bus);
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000125#endif
Stefan Roese0dc018e2007-02-20 10:51:26 +0100126
Simon Glassf9951ea2015-04-20 12:37:26 -0600127 return rcode ? CMD_RET_FAILURE : 0;
wdenk38635852002-08-27 05:55:31 +0000128}
129
130/*
131 * simple conversion of two-digit string with error checking
132 */
Mike Frysingerbdbc1302010-10-20 07:17:23 -0400133static int cnvrt2 (const char *str, int *valp)
wdenk38635852002-08-27 05:55:31 +0000134{
135 int val;
136
137 if ((*str < '0') || (*str > '9'))
138 return (-1);
139
140 val = *str - '0';
141
142 ++str;
143
144 if ((*str < '0') || (*str > '9'))
145 return (-1);
146
147 *valp = 10 * val + (*str - '0');
148
149 return (0);
150}
151
152/*
153 * Convert date string: MMDDhhmm[[CC]YY][.ss]
154 *
155 * Some basic checking for valid values is done, but this will not catch
156 * all possible error conditions.
157 */
Mike Frysingerbdbc1302010-10-20 07:17:23 -0400158int mk_date (const char *datestr, struct rtc_time *tmp)
wdenk38635852002-08-27 05:55:31 +0000159{
160 int len, val;
161 char *ptr;
162
163 ptr = strchr (datestr,'.');
164 len = strlen (datestr);
165
166 /* Set seconds */
167 if (ptr) {
168 int sec;
169
170 *ptr++ = '\0';
171 if ((len - (ptr - datestr)) != 2)
172 return (-1);
173
174 len = strlen (datestr);
175
176 if (cnvrt2 (ptr, &sec))
177 return (-1);
178
179 tmp->tm_sec = sec;
180 } else {
181 tmp->tm_sec = 0;
182 }
183
184 if (len == 12) { /* MMDDhhmmCCYY */
185 int year, century;
186
187 if (cnvrt2 (datestr+ 8, &century) ||
188 cnvrt2 (datestr+10, &year) ) {
189 return (-1);
190 }
191 tmp->tm_year = 100 * century + year;
192 } else if (len == 10) { /* MMDDhhmmYY */
193 int year, century;
194
195 century = tmp->tm_year / 100;
196 if (cnvrt2 (datestr+ 8, &year))
197 return (-1);
198 tmp->tm_year = 100 * century + year;
199 }
200
201 switch (len) {
202 case 8: /* MMDDhhmm */
203 /* fall thru */
204 case 10: /* MMDDhhmmYY */
205 /* fall thru */
206 case 12: /* MMDDhhmmCCYY */
207 if (cnvrt2 (datestr+0, &val) ||
208 val > 12) {
209 break;
210 }
211 tmp->tm_mon = val;
212 if (cnvrt2 (datestr+2, &val) ||
213 val > ((tmp->tm_mon==2) ? 29 : 31)) {
214 break;
215 }
216 tmp->tm_mday = val;
217
218 if (cnvrt2 (datestr+4, &val) ||
219 val > 23) {
220 break;
221 }
222 tmp->tm_hour = val;
223
224 if (cnvrt2 (datestr+6, &val) ||
225 val > 59) {
226 break;
227 }
228 tmp->tm_min = val;
229
230 /* calculate day of week */
Simon Glass199e87c2015-04-20 12:37:17 -0600231 rtc_calc_weekday(tmp);
wdenk38635852002-08-27 05:55:31 +0000232
233 return (0);
234 default:
235 break;
236 }
237
238 return (-1);
239}
240
wdenk8bde7f72003-06-27 21:31:46 +0000241/***************************************************/
242
wdenk0d498392003-07-01 21:06:45 +0000243U_BOOT_CMD(
244 date, 2, 1, do_date,
Peter Tyser2fb26042009-01-27 18:03:12 -0600245 "get/set/reset date & time",
wdenk8bde7f72003-06-27 21:31:46 +0000246 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
247 " - without arguments: print date & time\n"
248 " - with numeric argument: set the system date & time\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200249 " - with 'reset' argument: reset the RTC"
wdenk8bde7f72003-06-27 21:31:46 +0000250);