blob: 8614f0227614c2ae602271e2d8235c6403b3b41e [file] [log] [blame]
Tom Rini10e47792018-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 */
wdenk38635852002-08-27 05:55:31 +000010#include <command.h>
Simon Glassa8efb4a2015-04-20 12:37:26 -060011#include <dm.h>
wdenk38635852002-08-27 05:55:31 +000012#include <rtc.h>
Stefan Roese096cc9b2007-02-20 10:51:26 +010013#include <i2c.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
wdenk38635852002-08-27 05:55:31 +000015
Wolfgang Denk6405a152006-03-31 18:32:53 +020016DECLARE_GLOBAL_DATA_PTR;
17
Mike Frysinger59e14722010-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
Mike Frysinger59e14722010-10-20 07:17:23 -040022int mk_date (const char *, struct rtc_time *);
wdenk38635852002-08-27 05:55:31 +000023
Marek Vasutd45c1b82015-04-10 01:11:54 +020024static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
25
Simon Glassed38aef2020-05-10 11:40:03 -060026static int do_date(struct cmd_tbl *cmdtp, int flag, int argc,
27 char *const argv[])
wdenk38635852002-08-27 05:55:31 +000028{
29 struct rtc_time tm;
30 int rcode = 0;
Simon Glassa8efb4a2015-04-20 12:37:26 -060031 int old_bus __maybe_unused;
Stefan Roese096cc9b2007-02-20 10:51:26 +010032
33 /* switch to correct I2C bus */
Simon Glassa8efb4a2015-04-20 12:37:26 -060034 struct udevice *dev;
35
Michal Simek452359e2021-07-29 12:59:12 +020036 rcode = uclass_get_device_by_seq(UCLASS_RTC, 0, &dev);
Simon Glassa8efb4a2015-04-20 12:37:26 -060037 if (rcode) {
Michal Simek452359e2021-07-29 12:59:12 +020038 rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
39 if (rcode) {
40 printf("Cannot find RTC: err=%d\n", rcode);
41 return CMD_RET_FAILURE;
42 }
Simon Glassa8efb4a2015-04-20 12:37:26 -060043 }
wdenk38635852002-08-27 05:55:31 +000044
45 switch (argc) {
46 case 2: /* set date & time */
47 if (strcmp(argv[1],"reset") == 0) {
wdenk42c05472004-03-23 22:14:11 +000048 puts ("Reset RTC...\n");
Simon Glassa8efb4a2015-04-20 12:37:26 -060049 rcode = dm_rtc_reset(dev);
50 if (!rcode)
51 rcode = dm_rtc_set(dev, &default_tm);
Marek Vasutd45c1b82015-04-10 01:11:54 +020052 if (rcode)
53 puts("## Failed to set date after RTC reset\n");
wdenk38635852002-08-27 05:55:31 +000054 } else {
55 /* initialize tm with current time */
Simon Glassa8efb4a2015-04-20 12:37:26 -060056 rcode = dm_rtc_get(dev, &tm);
Simon Glassa8efb4a2015-04-20 12:37:26 -060057 if (!rcode) {
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020058 /* insert new date & time */
Simon Glassa8efb4a2015-04-20 12:37:26 -060059 if (mk_date(argv[1], &tm) != 0) {
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020060 puts ("## Bad date format\n");
61 break;
62 }
63 /* and write to RTC */
Simon Glassa8efb4a2015-04-20 12:37:26 -060064 rcode = dm_rtc_set(dev, &tm);
Simon Glassa8efb4a2015-04-20 12:37:26 -060065 if (rcode) {
66 printf("## Set date failed: err=%d\n",
67 rcode);
68 }
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020069 } else {
Magnus Lilja55992362009-11-11 19:56:36 +010070 puts("## Get date failed\n");
wdenk38635852002-08-27 05:55:31 +000071 }
wdenk38635852002-08-27 05:55:31 +000072 }
Heinrich Schuchardt5d520cf2023-04-01 09:14:11 +020073 fallthrough;
wdenk38635852002-08-27 05:55:31 +000074 case 1: /* get date & time */
Simon Glassa8efb4a2015-04-20 12:37:26 -060075 rcode = dm_rtc_get(dev, &tm);
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020076 if (rcode) {
Magnus Lilja55992362009-11-11 19:56:36 +010077 puts("## Get date failed\n");
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020078 break;
79 }
wdenk38635852002-08-27 05:55:31 +000080
81 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
82 tm.tm_year, tm.tm_mon, tm.tm_mday,
83 (tm.tm_wday<0 || tm.tm_wday>6) ?
Marek Vasutb3c29df2023-09-06 23:29:51 +020084 "unknown " : weekdays[tm.tm_wday],
wdenk38635852002-08-27 05:55:31 +000085 tm.tm_hour, tm.tm_min, tm.tm_sec);
86
Stefan Roese096cc9b2007-02-20 10:51:26 +010087 break;
wdenk38635852002-08-27 05:55:31 +000088 default:
Simon Glassa06dfc72011-12-10 08:44:01 +000089 rcode = CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +000090 }
Stefan Roese096cc9b2007-02-20 10:51:26 +010091
Simon Glassa8efb4a2015-04-20 12:37:26 -060092 return rcode ? CMD_RET_FAILURE : 0;
wdenk38635852002-08-27 05:55:31 +000093}
94
95/*
96 * simple conversion of two-digit string with error checking
97 */
Mike Frysinger59e14722010-10-20 07:17:23 -040098static int cnvrt2 (const char *str, int *valp)
wdenk38635852002-08-27 05:55:31 +000099{
100 int val;
101
102 if ((*str < '0') || (*str > '9'))
103 return (-1);
104
105 val = *str - '0';
106
107 ++str;
108
109 if ((*str < '0') || (*str > '9'))
110 return (-1);
111
112 *valp = 10 * val + (*str - '0');
113
114 return (0);
115}
116
117/*
118 * Convert date string: MMDDhhmm[[CC]YY][.ss]
119 *
120 * Some basic checking for valid values is done, but this will not catch
121 * all possible error conditions.
122 */
Mike Frysinger59e14722010-10-20 07:17:23 -0400123int mk_date (const char *datestr, struct rtc_time *tmp)
wdenk38635852002-08-27 05:55:31 +0000124{
125 int len, val;
126 char *ptr;
127
Roman Kapl0a0dbd42019-02-08 10:01:02 +0100128 ptr = strchr(datestr, '.');
129 len = strlen(datestr);
wdenk38635852002-08-27 05:55:31 +0000130
131 /* Set seconds */
132 if (ptr) {
133 int sec;
134
Roman Kapl0a0dbd42019-02-08 10:01:02 +0100135 ptr++;
wdenk38635852002-08-27 05:55:31 +0000136 if ((len - (ptr - datestr)) != 2)
137 return (-1);
138
Roman Kapl0a0dbd42019-02-08 10:01:02 +0100139 len -= 3;
wdenk38635852002-08-27 05:55:31 +0000140
141 if (cnvrt2 (ptr, &sec))
142 return (-1);
143
144 tmp->tm_sec = sec;
145 } else {
146 tmp->tm_sec = 0;
147 }
148
149 if (len == 12) { /* MMDDhhmmCCYY */
150 int year, century;
151
152 if (cnvrt2 (datestr+ 8, &century) ||
153 cnvrt2 (datestr+10, &year) ) {
154 return (-1);
155 }
156 tmp->tm_year = 100 * century + year;
157 } else if (len == 10) { /* MMDDhhmmYY */
158 int year, century;
159
160 century = tmp->tm_year / 100;
161 if (cnvrt2 (datestr+ 8, &year))
162 return (-1);
163 tmp->tm_year = 100 * century + year;
164 }
165
166 switch (len) {
167 case 8: /* MMDDhhmm */
168 /* fall thru */
169 case 10: /* MMDDhhmmYY */
170 /* fall thru */
171 case 12: /* MMDDhhmmCCYY */
172 if (cnvrt2 (datestr+0, &val) ||
173 val > 12) {
174 break;
175 }
176 tmp->tm_mon = val;
177 if (cnvrt2 (datestr+2, &val) ||
178 val > ((tmp->tm_mon==2) ? 29 : 31)) {
179 break;
180 }
181 tmp->tm_mday = val;
182
183 if (cnvrt2 (datestr+4, &val) ||
184 val > 23) {
185 break;
186 }
187 tmp->tm_hour = val;
188
189 if (cnvrt2 (datestr+6, &val) ||
190 val > 59) {
191 break;
192 }
193 tmp->tm_min = val;
194
195 /* calculate day of week */
Simon Glass2d937bb2015-04-20 12:37:17 -0600196 rtc_calc_weekday(tmp);
wdenk38635852002-08-27 05:55:31 +0000197
198 return (0);
199 default:
200 break;
201 }
202
203 return (-1);
204}
205
wdenk57b2d802003-06-27 21:31:46 +0000206/***************************************************/
207
wdenkf287a242003-07-01 21:06:45 +0000208U_BOOT_CMD(
209 date, 2, 1, do_date,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600210 "get/set/reset date & time",
wdenk57b2d802003-06-27 21:31:46 +0000211 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
212 " - without arguments: print date & time\n"
213 " - with numeric argument: set the system date & time\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200214 " - with 'reset' argument: reset the RTC"
wdenk57b2d802003-06-27 21:31:46 +0000215);