Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * RTC, Date & Time support: get and set date & time |
| 9 | */ |
| 10 | #include <common.h> |
| 11 | #include <command.h> |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 12 | #include <dm.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 13 | #include <rtc.h> |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 14 | #include <i2c.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 16 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Mike Frysinger | 59e1472 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 19 | static const char * const weekdays[] = { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 20 | "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", |
| 21 | }; |
| 22 | |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 23 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 24 | #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off)) |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 25 | #else |
| 26 | #define RELOC(a) a |
Peter Tyser | 9057cbf | 2009-09-21 11:20:36 -0500 | [diff] [blame] | 27 | #endif |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 28 | |
Mike Frysinger | 59e1472 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 29 | int mk_date (const char *, struct rtc_time *); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 30 | |
Marek Vasut | d45c1b8 | 2015-04-10 01:11:54 +0200 | [diff] [blame] | 31 | static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 }; |
| 32 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 33 | static int do_date(struct cmd_tbl *cmdtp, int flag, int argc, |
| 34 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 35 | { |
| 36 | struct rtc_time tm; |
| 37 | int rcode = 0; |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 38 | int old_bus __maybe_unused; |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 39 | |
| 40 | /* switch to correct I2C bus */ |
Bin Meng | 8755d2c | 2015-06-23 12:18:42 +0800 | [diff] [blame] | 41 | #ifdef CONFIG_DM_RTC |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 42 | 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 | } |
Simon Glass | 0529b59 | 2021-07-10 21:14:32 -0600 | [diff] [blame] | 49 | #elif defined(CONFIG_SYS_I2C_LEGACY) |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 50 | old_bus = i2c_get_bus_num(); |
| 51 | i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM); |
| 52 | #else |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 53 | old_bus = I2C_GET_BUS(); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 54 | I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM); |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 55 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 56 | |
| 57 | switch (argc) { |
| 58 | case 2: /* set date & time */ |
| 59 | if (strcmp(argv[1],"reset") == 0) { |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 60 | puts ("Reset RTC...\n"); |
Bin Meng | 8755d2c | 2015-06-23 12:18:42 +0800 | [diff] [blame] | 61 | #ifdef CONFIG_DM_RTC |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 62 | rcode = dm_rtc_reset(dev); |
| 63 | if (!rcode) |
| 64 | rcode = dm_rtc_set(dev, &default_tm); |
| 65 | #else |
| 66 | rtc_reset(); |
Marek Vasut | d45c1b8 | 2015-04-10 01:11:54 +0200 | [diff] [blame] | 67 | rcode = rtc_set(&default_tm); |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 68 | #endif |
Marek Vasut | d45c1b8 | 2015-04-10 01:11:54 +0200 | [diff] [blame] | 69 | if (rcode) |
| 70 | puts("## Failed to set date after RTC reset\n"); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 71 | } else { |
| 72 | /* initialize tm with current time */ |
Bin Meng | 8755d2c | 2015-06-23 12:18:42 +0800 | [diff] [blame] | 73 | #ifdef CONFIG_DM_RTC |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 74 | rcode = dm_rtc_get(dev, &tm); |
| 75 | #else |
| 76 | rcode = rtc_get(&tm); |
| 77 | #endif |
| 78 | if (!rcode) { |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 79 | /* insert new date & time */ |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 80 | if (mk_date(argv[1], &tm) != 0) { |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 81 | puts ("## Bad date format\n"); |
| 82 | break; |
| 83 | } |
| 84 | /* and write to RTC */ |
Bin Meng | 8755d2c | 2015-06-23 12:18:42 +0800 | [diff] [blame] | 85 | #ifdef CONFIG_DM_RTC |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 86 | 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-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 94 | } else { |
Magnus Lilja | 5599236 | 2009-11-11 19:56:36 +0100 | [diff] [blame] | 95 | puts("## Get date failed\n"); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 96 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 97 | } |
| 98 | /* FALL TROUGH */ |
| 99 | case 1: /* get date & time */ |
Bin Meng | 8755d2c | 2015-06-23 12:18:42 +0800 | [diff] [blame] | 100 | #ifdef CONFIG_DM_RTC |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 101 | rcode = dm_rtc_get(dev, &tm); |
| 102 | #else |
| 103 | rcode = rtc_get(&tm); |
| 104 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 105 | if (rcode) { |
Magnus Lilja | 5599236 | 2009-11-11 19:56:36 +0100 | [diff] [blame] | 106 | puts("## Get date failed\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 107 | break; |
| 108 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 109 | |
| 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) ? |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 113 | "unknown " : RELOC(weekdays[tm.tm_wday]), |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 114 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 115 | |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 116 | break; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 117 | default: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 118 | rcode = CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 119 | } |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 120 | |
| 121 | /* switch back to original I2C bus */ |
Simon Glass | 0529b59 | 2021-07-10 21:14:32 -0600 | [diff] [blame] | 122 | #ifdef CONFIG_SYS_I2C_LEGACY |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 123 | i2c_set_bus_num(old_bus); |
Bin Meng | 8755d2c | 2015-06-23 12:18:42 +0800 | [diff] [blame] | 124 | #elif !defined(CONFIG_DM_RTC) |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 125 | I2C_SET_BUS(old_bus); |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 126 | #endif |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 127 | |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 128 | return rcode ? CMD_RET_FAILURE : 0; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * simple conversion of two-digit string with error checking |
| 133 | */ |
Mike Frysinger | 59e1472 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 134 | static int cnvrt2 (const char *str, int *valp) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 135 | { |
| 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 Frysinger | 59e1472 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 159 | int mk_date (const char *datestr, struct rtc_time *tmp) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 160 | { |
| 161 | int len, val; |
| 162 | char *ptr; |
| 163 | |
Roman Kapl | 0a0dbd4 | 2019-02-08 10:01:02 +0100 | [diff] [blame] | 164 | ptr = strchr(datestr, '.'); |
| 165 | len = strlen(datestr); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 166 | |
| 167 | /* Set seconds */ |
| 168 | if (ptr) { |
| 169 | int sec; |
| 170 | |
Roman Kapl | 0a0dbd4 | 2019-02-08 10:01:02 +0100 | [diff] [blame] | 171 | ptr++; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 172 | if ((len - (ptr - datestr)) != 2) |
| 173 | return (-1); |
| 174 | |
Roman Kapl | 0a0dbd4 | 2019-02-08 10:01:02 +0100 | [diff] [blame] | 175 | len -= 3; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 176 | |
| 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, ¢ury) || |
| 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 Glass | 2d937bb | 2015-04-20 12:37:17 -0600 | [diff] [blame] | 232 | rtc_calc_weekday(tmp); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 233 | |
| 234 | return (0); |
| 235 | default: |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | return (-1); |
| 240 | } |
| 241 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 242 | /***************************************************/ |
| 243 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 244 | U_BOOT_CMD( |
| 245 | date, 2, 1, do_date, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 246 | "get/set/reset date & time", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 247 | "[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 Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 250 | " - with 'reset' argument: reset the RTC" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 251 | ); |