blob: 7fa950a902682093bf8d694840c1d740b1c03e11 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00002/*
3 * (C) Copyright 2001
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk38635852002-08-27 05:55:31 +00005 */
6
7/*
8 * RTC, Date & Time support: get and set date & time
9 */
10#include <common.h>
11#include <command.h>
Simon Glassf9951ea2015-04-20 12:37:26 -060012#include <dm.h>
wdenk38635852002-08-27 05:55:31 +000013#include <rtc.h>
Stefan Roese0dc018e2007-02-20 10:51:26 +010014#include <i2c.h>
wdenk38635852002-08-27 05:55:31 +000015
Wolfgang Denkd87080b2006-03-31 18:32:53 +020016DECLARE_GLOBAL_DATA_PTR;
17
Mike Frysingerbdbc1302010-10-20 07:17:23 -040018static const char * const weekdays[] = {
wdenk38635852002-08-27 05:55:31 +000019 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
20};
21
Wolfgang Denk2e5167c2010-10-28 20:00:11 +020022#ifdef CONFIG_NEEDS_MANUAL_RELOC
wdenk3e386912003-04-05 00:53:31 +000023#define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
Wolfgang Denk2e5167c2010-10-28 20:00:11 +020024#else
25#define RELOC(a) a
Peter Tyser521af042009-09-21 11:20:36 -050026#endif
wdenk3e386912003-04-05 00:53:31 +000027
Mike Frysingerbdbc1302010-10-20 07:17:23 -040028int mk_date (const char *, struct rtc_time *);
wdenk38635852002-08-27 05:55:31 +000029
Marek Vasut1a1fa242015-04-10 01:11:54 +020030static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
31
Kim Phillips088f1b12012-10-29 13:34:31 +000032static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000033{
34 struct rtc_time tm;
35 int rcode = 0;
Simon Glassf9951ea2015-04-20 12:37:26 -060036 int old_bus __maybe_unused;
Stefan Roese0dc018e2007-02-20 10:51:26 +010037
38 /* switch to correct I2C bus */
Bin Mengffe38792015-06-23 12:18:42 +080039#ifdef CONFIG_DM_RTC
Simon Glassf9951ea2015-04-20 12:37:26 -060040 struct udevice *dev;
41
42 rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
43 if (rcode) {
44 printf("Cannot find RTC: err=%d\n", rcode);
45 return CMD_RET_FAILURE;
46 }
47#elif defined(CONFIG_SYS_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +000048 old_bus = i2c_get_bus_num();
49 i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
50#else
Stefan Roese0dc018e2007-02-20 10:51:26 +010051 old_bus = I2C_GET_BUS();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020052 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
Heiko Schocher3f4978c2012-01-16 21:12:24 +000053#endif
wdenk38635852002-08-27 05:55:31 +000054
55 switch (argc) {
56 case 2: /* set date & time */
57 if (strcmp(argv[1],"reset") == 0) {
wdenk4b9206e2004-03-23 22:14:11 +000058 puts ("Reset RTC...\n");
Bin Mengffe38792015-06-23 12:18:42 +080059#ifdef CONFIG_DM_RTC
Simon Glassf9951ea2015-04-20 12:37:26 -060060 rcode = dm_rtc_reset(dev);
61 if (!rcode)
62 rcode = dm_rtc_set(dev, &default_tm);
63#else
64 rtc_reset();
Marek Vasut1a1fa242015-04-10 01:11:54 +020065 rcode = rtc_set(&default_tm);
Simon Glassf9951ea2015-04-20 12:37:26 -060066#endif
Marek Vasut1a1fa242015-04-10 01:11:54 +020067 if (rcode)
68 puts("## Failed to set date after RTC reset\n");
wdenk38635852002-08-27 05:55:31 +000069 } else {
70 /* initialize tm with current time */
Bin Mengffe38792015-06-23 12:18:42 +080071#ifdef CONFIG_DM_RTC
Simon Glassf9951ea2015-04-20 12:37:26 -060072 rcode = dm_rtc_get(dev, &tm);
73#else
74 rcode = rtc_get(&tm);
75#endif
76 if (!rcode) {
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020077 /* insert new date & time */
Simon Glassf9951ea2015-04-20 12:37:26 -060078 if (mk_date(argv[1], &tm) != 0) {
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020079 puts ("## Bad date format\n");
80 break;
81 }
82 /* and write to RTC */
Bin Mengffe38792015-06-23 12:18:42 +080083#ifdef CONFIG_DM_RTC
Simon Glassf9951ea2015-04-20 12:37:26 -060084 rcode = dm_rtc_set(dev, &tm);
85#else
86 rcode = rtc_set(&tm);
87#endif
88 if (rcode) {
89 printf("## Set date failed: err=%d\n",
90 rcode);
91 }
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020092 } else {
Magnus Liljad52e3e02009-11-11 19:56:36 +010093 puts("## Get date failed\n");
wdenk38635852002-08-27 05:55:31 +000094 }
wdenk38635852002-08-27 05:55:31 +000095 }
96 /* FALL TROUGH */
97 case 1: /* get date & time */
Bin Mengffe38792015-06-23 12:18:42 +080098#ifdef CONFIG_DM_RTC
Simon Glassf9951ea2015-04-20 12:37:26 -060099 rcode = dm_rtc_get(dev, &tm);
100#else
101 rcode = rtc_get(&tm);
102#endif
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200103 if (rcode) {
Magnus Liljad52e3e02009-11-11 19:56:36 +0100104 puts("## Get date failed\n");
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200105 break;
106 }
wdenk38635852002-08-27 05:55:31 +0000107
108 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
109 tm.tm_year, tm.tm_mon, tm.tm_mday,
110 (tm.tm_wday<0 || tm.tm_wday>6) ?
wdenk3e386912003-04-05 00:53:31 +0000111 "unknown " : RELOC(weekdays[tm.tm_wday]),
wdenk38635852002-08-27 05:55:31 +0000112 tm.tm_hour, tm.tm_min, tm.tm_sec);
113
Stefan Roese0dc018e2007-02-20 10:51:26 +0100114 break;
wdenk38635852002-08-27 05:55:31 +0000115 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000116 rcode = CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000117 }
Stefan Roese0dc018e2007-02-20 10:51:26 +0100118
119 /* switch back to original I2C bus */
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000120#ifdef CONFIG_SYS_I2C
121 i2c_set_bus_num(old_bus);
Bin Mengffe38792015-06-23 12:18:42 +0800122#elif !defined(CONFIG_DM_RTC)
Stefan Roese0dc018e2007-02-20 10:51:26 +0100123 I2C_SET_BUS(old_bus);
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000124#endif
Stefan Roese0dc018e2007-02-20 10:51:26 +0100125
Simon Glassf9951ea2015-04-20 12:37:26 -0600126 return rcode ? CMD_RET_FAILURE : 0;
wdenk38635852002-08-27 05:55:31 +0000127}
128
129/*
130 * simple conversion of two-digit string with error checking
131 */
Mike Frysingerbdbc1302010-10-20 07:17:23 -0400132static int cnvrt2 (const char *str, int *valp)
wdenk38635852002-08-27 05:55:31 +0000133{
134 int val;
135
136 if ((*str < '0') || (*str > '9'))
137 return (-1);
138
139 val = *str - '0';
140
141 ++str;
142
143 if ((*str < '0') || (*str > '9'))
144 return (-1);
145
146 *valp = 10 * val + (*str - '0');
147
148 return (0);
149}
150
151/*
152 * Convert date string: MMDDhhmm[[CC]YY][.ss]
153 *
154 * Some basic checking for valid values is done, but this will not catch
155 * all possible error conditions.
156 */
Mike Frysingerbdbc1302010-10-20 07:17:23 -0400157int mk_date (const char *datestr, struct rtc_time *tmp)
wdenk38635852002-08-27 05:55:31 +0000158{
159 int len, val;
160 char *ptr;
161
Roman Kapl44ac80e2019-02-08 10:01:02 +0100162 ptr = strchr(datestr, '.');
163 len = strlen(datestr);
wdenk38635852002-08-27 05:55:31 +0000164
165 /* Set seconds */
166 if (ptr) {
167 int sec;
168
Roman Kapl44ac80e2019-02-08 10:01:02 +0100169 ptr++;
wdenk38635852002-08-27 05:55:31 +0000170 if ((len - (ptr - datestr)) != 2)
171 return (-1);
172
Roman Kapl44ac80e2019-02-08 10:01:02 +0100173 len -= 3;
wdenk38635852002-08-27 05:55:31 +0000174
175 if (cnvrt2 (ptr, &sec))
176 return (-1);
177
178 tmp->tm_sec = sec;
179 } else {
180 tmp->tm_sec = 0;
181 }
182
183 if (len == 12) { /* MMDDhhmmCCYY */
184 int year, century;
185
186 if (cnvrt2 (datestr+ 8, &century) ||
187 cnvrt2 (datestr+10, &year) ) {
188 return (-1);
189 }
190 tmp->tm_year = 100 * century + year;
191 } else if (len == 10) { /* MMDDhhmmYY */
192 int year, century;
193
194 century = tmp->tm_year / 100;
195 if (cnvrt2 (datestr+ 8, &year))
196 return (-1);
197 tmp->tm_year = 100 * century + year;
198 }
199
200 switch (len) {
201 case 8: /* MMDDhhmm */
202 /* fall thru */
203 case 10: /* MMDDhhmmYY */
204 /* fall thru */
205 case 12: /* MMDDhhmmCCYY */
206 if (cnvrt2 (datestr+0, &val) ||
207 val > 12) {
208 break;
209 }
210 tmp->tm_mon = val;
211 if (cnvrt2 (datestr+2, &val) ||
212 val > ((tmp->tm_mon==2) ? 29 : 31)) {
213 break;
214 }
215 tmp->tm_mday = val;
216
217 if (cnvrt2 (datestr+4, &val) ||
218 val > 23) {
219 break;
220 }
221 tmp->tm_hour = val;
222
223 if (cnvrt2 (datestr+6, &val) ||
224 val > 59) {
225 break;
226 }
227 tmp->tm_min = val;
228
229 /* calculate day of week */
Simon Glass199e87c2015-04-20 12:37:17 -0600230 rtc_calc_weekday(tmp);
wdenk38635852002-08-27 05:55:31 +0000231
232 return (0);
233 default:
234 break;
235 }
236
237 return (-1);
238}
239
wdenk8bde7f72003-06-27 21:31:46 +0000240/***************************************************/
241
wdenk0d498392003-07-01 21:06:45 +0000242U_BOOT_CMD(
243 date, 2, 1, do_date,
Peter Tyser2fb26042009-01-27 18:03:12 -0600244 "get/set/reset date & time",
wdenk8bde7f72003-06-27 21:31:46 +0000245 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
246 " - without arguments: print date & time\n"
247 " - with numeric argument: set the system date & time\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200248 " - with 'reset' argument: reset the RTC"
wdenk8bde7f72003-06-27 21:31:46 +0000249);