blob: 0e1189475313d16eb9572c5d4de1068d0a1a92cd [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>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.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
Simon Glass09140112020-05-10 11:40:03 -060033static int do_date(struct cmd_tbl *cmdtp, int flag, int argc,
34 char *const argv[])
wdenk38635852002-08-27 05:55:31 +000035{
36 struct rtc_time tm;
37 int rcode = 0;
Simon Glassf9951ea2015-04-20 12:37:26 -060038 int old_bus __maybe_unused;
Stefan Roese0dc018e2007-02-20 10:51:26 +010039
40 /* switch to correct I2C bus */
Bin Mengffe38792015-06-23 12:18:42 +080041#ifdef CONFIG_DM_RTC
Simon Glassf9951ea2015-04-20 12:37:26 -060042 struct udevice *dev;
43
44 rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
45 if (rcode) {
46 printf("Cannot find RTC: err=%d\n", rcode);
47 return CMD_RET_FAILURE;
48 }
49#elif defined(CONFIG_SYS_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +000050 old_bus = i2c_get_bus_num();
51 i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
52#else
Stefan Roese0dc018e2007-02-20 10:51:26 +010053 old_bus = I2C_GET_BUS();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020054 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
Heiko Schocher3f4978c2012-01-16 21:12:24 +000055#endif
wdenk38635852002-08-27 05:55:31 +000056
57 switch (argc) {
58 case 2: /* set date & time */
59 if (strcmp(argv[1],"reset") == 0) {
wdenk4b9206e2004-03-23 22:14:11 +000060 puts ("Reset RTC...\n");
Bin Mengffe38792015-06-23 12:18:42 +080061#ifdef CONFIG_DM_RTC
Simon Glassf9951ea2015-04-20 12:37:26 -060062 rcode = dm_rtc_reset(dev);
63 if (!rcode)
64 rcode = dm_rtc_set(dev, &default_tm);
65#else
66 rtc_reset();
Marek Vasut1a1fa242015-04-10 01:11:54 +020067 rcode = rtc_set(&default_tm);
Simon Glassf9951ea2015-04-20 12:37:26 -060068#endif
Marek Vasut1a1fa242015-04-10 01:11:54 +020069 if (rcode)
70 puts("## Failed to set date after RTC reset\n");
wdenk38635852002-08-27 05:55:31 +000071 } else {
72 /* initialize tm with current time */
Bin Mengffe38792015-06-23 12:18:42 +080073#ifdef CONFIG_DM_RTC
Simon Glassf9951ea2015-04-20 12:37:26 -060074 rcode = dm_rtc_get(dev, &tm);
75#else
76 rcode = rtc_get(&tm);
77#endif
78 if (!rcode) {
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020079 /* insert new date & time */
Simon Glassf9951ea2015-04-20 12:37:26 -060080 if (mk_date(argv[1], &tm) != 0) {
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020081 puts ("## Bad date format\n");
82 break;
83 }
84 /* and write to RTC */
Bin Mengffe38792015-06-23 12:18:42 +080085#ifdef CONFIG_DM_RTC
Simon Glassf9951ea2015-04-20 12:37:26 -060086 rcode = dm_rtc_set(dev, &tm);
87#else
88 rcode = rtc_set(&tm);
89#endif
90 if (rcode) {
91 printf("## Set date failed: err=%d\n",
92 rcode);
93 }
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020094 } else {
Magnus Liljad52e3e02009-11-11 19:56:36 +010095 puts("## Get date failed\n");
wdenk38635852002-08-27 05:55:31 +000096 }
wdenk38635852002-08-27 05:55:31 +000097 }
98 /* FALL TROUGH */
99 case 1: /* get date & time */
Bin Mengffe38792015-06-23 12:18:42 +0800100#ifdef CONFIG_DM_RTC
Simon Glassf9951ea2015-04-20 12:37:26 -0600101 rcode = dm_rtc_get(dev, &tm);
102#else
103 rcode = rtc_get(&tm);
104#endif
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200105 if (rcode) {
Magnus Liljad52e3e02009-11-11 19:56:36 +0100106 puts("## Get date failed\n");
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200107 break;
108 }
wdenk38635852002-08-27 05:55:31 +0000109
110 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
111 tm.tm_year, tm.tm_mon, tm.tm_mday,
112 (tm.tm_wday<0 || tm.tm_wday>6) ?
wdenk3e386912003-04-05 00:53:31 +0000113 "unknown " : RELOC(weekdays[tm.tm_wday]),
wdenk38635852002-08-27 05:55:31 +0000114 tm.tm_hour, tm.tm_min, tm.tm_sec);
115
Stefan Roese0dc018e2007-02-20 10:51:26 +0100116 break;
wdenk38635852002-08-27 05:55:31 +0000117 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000118 rcode = CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000119 }
Stefan Roese0dc018e2007-02-20 10:51:26 +0100120
121 /* switch back to original I2C bus */
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000122#ifdef CONFIG_SYS_I2C
123 i2c_set_bus_num(old_bus);
Bin Mengffe38792015-06-23 12:18:42 +0800124#elif !defined(CONFIG_DM_RTC)
Stefan Roese0dc018e2007-02-20 10:51:26 +0100125 I2C_SET_BUS(old_bus);
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000126#endif
Stefan Roese0dc018e2007-02-20 10:51:26 +0100127
Simon Glassf9951ea2015-04-20 12:37:26 -0600128 return rcode ? CMD_RET_FAILURE : 0;
wdenk38635852002-08-27 05:55:31 +0000129}
130
131/*
132 * simple conversion of two-digit string with error checking
133 */
Mike Frysingerbdbc1302010-10-20 07:17:23 -0400134static int cnvrt2 (const char *str, int *valp)
wdenk38635852002-08-27 05:55:31 +0000135{
136 int val;
137
138 if ((*str < '0') || (*str > '9'))
139 return (-1);
140
141 val = *str - '0';
142
143 ++str;
144
145 if ((*str < '0') || (*str > '9'))
146 return (-1);
147
148 *valp = 10 * val + (*str - '0');
149
150 return (0);
151}
152
153/*
154 * Convert date string: MMDDhhmm[[CC]YY][.ss]
155 *
156 * Some basic checking for valid values is done, but this will not catch
157 * all possible error conditions.
158 */
Mike Frysingerbdbc1302010-10-20 07:17:23 -0400159int mk_date (const char *datestr, struct rtc_time *tmp)
wdenk38635852002-08-27 05:55:31 +0000160{
161 int len, val;
162 char *ptr;
163
Roman Kapl44ac80e2019-02-08 10:01:02 +0100164 ptr = strchr(datestr, '.');
165 len = strlen(datestr);
wdenk38635852002-08-27 05:55:31 +0000166
167 /* Set seconds */
168 if (ptr) {
169 int sec;
170
Roman Kapl44ac80e2019-02-08 10:01:02 +0100171 ptr++;
wdenk38635852002-08-27 05:55:31 +0000172 if ((len - (ptr - datestr)) != 2)
173 return (-1);
174
Roman Kapl44ac80e2019-02-08 10:01:02 +0100175 len -= 3;
wdenk38635852002-08-27 05:55:31 +0000176
177 if (cnvrt2 (ptr, &sec))
178 return (-1);
179
180 tmp->tm_sec = sec;
181 } else {
182 tmp->tm_sec = 0;
183 }
184
185 if (len == 12) { /* MMDDhhmmCCYY */
186 int year, century;
187
188 if (cnvrt2 (datestr+ 8, &century) ||
189 cnvrt2 (datestr+10, &year) ) {
190 return (-1);
191 }
192 tmp->tm_year = 100 * century + year;
193 } else if (len == 10) { /* MMDDhhmmYY */
194 int year, century;
195
196 century = tmp->tm_year / 100;
197 if (cnvrt2 (datestr+ 8, &year))
198 return (-1);
199 tmp->tm_year = 100 * century + year;
200 }
201
202 switch (len) {
203 case 8: /* MMDDhhmm */
204 /* fall thru */
205 case 10: /* MMDDhhmmYY */
206 /* fall thru */
207 case 12: /* MMDDhhmmCCYY */
208 if (cnvrt2 (datestr+0, &val) ||
209 val > 12) {
210 break;
211 }
212 tmp->tm_mon = val;
213 if (cnvrt2 (datestr+2, &val) ||
214 val > ((tmp->tm_mon==2) ? 29 : 31)) {
215 break;
216 }
217 tmp->tm_mday = val;
218
219 if (cnvrt2 (datestr+4, &val) ||
220 val > 23) {
221 break;
222 }
223 tmp->tm_hour = val;
224
225 if (cnvrt2 (datestr+6, &val) ||
226 val > 59) {
227 break;
228 }
229 tmp->tm_min = val;
230
231 /* calculate day of week */
Simon Glass199e87c2015-04-20 12:37:17 -0600232 rtc_calc_weekday(tmp);
wdenk38635852002-08-27 05:55:31 +0000233
234 return (0);
235 default:
236 break;
237 }
238
239 return (-1);
240}
241
wdenk8bde7f72003-06-27 21:31:46 +0000242/***************************************************/
243
wdenk0d498392003-07-01 21:06:45 +0000244U_BOOT_CMD(
245 date, 2, 1, do_date,
Peter Tyser2fb26042009-01-27 18:03:12 -0600246 "get/set/reset date & time",
wdenk8bde7f72003-06-27 21:31:46 +0000247 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
248 " - without arguments: print date & time\n"
249 " - with numeric argument: set the system date & time\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200250 " - with 'reset' argument: reset the RTC"
wdenk8bde7f72003-06-27 21:31:46 +0000251);