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 | |
Mike Frysinger | 59e1472 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 23 | int mk_date (const char *, struct rtc_time *); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 24 | |
Marek Vasut | d45c1b8 | 2015-04-10 01:11:54 +0200 | [diff] [blame] | 25 | static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 }; |
| 26 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 27 | static int do_date(struct cmd_tbl *cmdtp, int flag, int argc, |
| 28 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 29 | { |
| 30 | struct rtc_time tm; |
| 31 | int rcode = 0; |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 32 | int old_bus __maybe_unused; |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 33 | |
| 34 | /* switch to correct I2C bus */ |
Bin Meng | 8755d2c | 2015-06-23 12:18:42 +0800 | [diff] [blame] | 35 | #ifdef CONFIG_DM_RTC |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 36 | struct udevice *dev; |
| 37 | |
Michal Simek | 452359e | 2021-07-29 12:59:12 +0200 | [diff] [blame] | 38 | rcode = uclass_get_device_by_seq(UCLASS_RTC, 0, &dev); |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 39 | if (rcode) { |
Michal Simek | 452359e | 2021-07-29 12:59:12 +0200 | [diff] [blame] | 40 | rcode = uclass_get_device(UCLASS_RTC, 0, &dev); |
| 41 | if (rcode) { |
| 42 | printf("Cannot find RTC: err=%d\n", rcode); |
| 43 | return CMD_RET_FAILURE; |
| 44 | } |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 45 | } |
Tom Rini | 52b2e26 | 2021-08-18 23:12:24 -0400 | [diff] [blame] | 46 | #elif CONFIG_IS_ENABLED(SYS_I2C_LEGACY) |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 47 | old_bus = i2c_get_bus_num(); |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 48 | i2c_set_bus_num(CFG_SYS_RTC_BUS_NUM); |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 49 | #else |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 50 | old_bus = I2C_GET_BUS(); |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 51 | I2C_SET_BUS(CFG_SYS_RTC_BUS_NUM); |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 52 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 53 | |
| 54 | switch (argc) { |
| 55 | case 2: /* set date & time */ |
| 56 | if (strcmp(argv[1],"reset") == 0) { |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 57 | puts ("Reset RTC...\n"); |
Bin Meng | 8755d2c | 2015-06-23 12:18:42 +0800 | [diff] [blame] | 58 | #ifdef CONFIG_DM_RTC |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 59 | rcode = dm_rtc_reset(dev); |
| 60 | if (!rcode) |
| 61 | rcode = dm_rtc_set(dev, &default_tm); |
| 62 | #else |
| 63 | rtc_reset(); |
Marek Vasut | d45c1b8 | 2015-04-10 01:11:54 +0200 | [diff] [blame] | 64 | rcode = rtc_set(&default_tm); |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 65 | #endif |
Marek Vasut | d45c1b8 | 2015-04-10 01:11:54 +0200 | [diff] [blame] | 66 | if (rcode) |
| 67 | puts("## Failed to set date after RTC reset\n"); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 68 | } else { |
| 69 | /* initialize tm with current time */ |
Bin Meng | 8755d2c | 2015-06-23 12:18:42 +0800 | [diff] [blame] | 70 | #ifdef CONFIG_DM_RTC |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 71 | rcode = dm_rtc_get(dev, &tm); |
| 72 | #else |
| 73 | rcode = rtc_get(&tm); |
| 74 | #endif |
| 75 | if (!rcode) { |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 76 | /* insert new date & time */ |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 77 | if (mk_date(argv[1], &tm) != 0) { |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 78 | puts ("## Bad date format\n"); |
| 79 | break; |
| 80 | } |
| 81 | /* and write to RTC */ |
Bin Meng | 8755d2c | 2015-06-23 12:18:42 +0800 | [diff] [blame] | 82 | #ifdef CONFIG_DM_RTC |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 83 | rcode = dm_rtc_set(dev, &tm); |
| 84 | #else |
| 85 | rcode = rtc_set(&tm); |
| 86 | #endif |
| 87 | if (rcode) { |
| 88 | printf("## Set date failed: err=%d\n", |
| 89 | rcode); |
| 90 | } |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 91 | } else { |
Magnus Lilja | 5599236 | 2009-11-11 19:56:36 +0100 | [diff] [blame] | 92 | puts("## Get date failed\n"); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 93 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 94 | } |
Heinrich Schuchardt | 5d520cf | 2023-04-01 09:14:11 +0200 | [diff] [blame] | 95 | fallthrough; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 96 | case 1: /* get date & time */ |
Bin Meng | 8755d2c | 2015-06-23 12:18:42 +0800 | [diff] [blame] | 97 | #ifdef CONFIG_DM_RTC |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 98 | rcode = dm_rtc_get(dev, &tm); |
| 99 | #else |
| 100 | rcode = rtc_get(&tm); |
| 101 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 102 | if (rcode) { |
Magnus Lilja | 5599236 | 2009-11-11 19:56:36 +0100 | [diff] [blame] | 103 | puts("## Get date failed\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 104 | break; |
| 105 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 106 | |
| 107 | printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n", |
| 108 | tm.tm_year, tm.tm_mon, tm.tm_mday, |
| 109 | (tm.tm_wday<0 || tm.tm_wday>6) ? |
Marek Vasut | b3c29df | 2023-09-06 23:29:51 +0200 | [diff] [blame^] | 110 | "unknown " : weekdays[tm.tm_wday], |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 111 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 112 | |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 113 | break; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 114 | default: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 115 | rcode = CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 116 | } |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 117 | |
| 118 | /* switch back to original I2C bus */ |
Tom Rini | 52b2e26 | 2021-08-18 23:12:24 -0400 | [diff] [blame] | 119 | #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY) |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 120 | i2c_set_bus_num(old_bus); |
Bin Meng | 8755d2c | 2015-06-23 12:18:42 +0800 | [diff] [blame] | 121 | #elif !defined(CONFIG_DM_RTC) |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 122 | I2C_SET_BUS(old_bus); |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 123 | #endif |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 124 | |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 125 | return rcode ? CMD_RET_FAILURE : 0; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* |
| 129 | * simple conversion of two-digit string with error checking |
| 130 | */ |
Mike Frysinger | 59e1472 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 131 | static int cnvrt2 (const char *str, int *valp) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 132 | { |
| 133 | int val; |
| 134 | |
| 135 | if ((*str < '0') || (*str > '9')) |
| 136 | return (-1); |
| 137 | |
| 138 | val = *str - '0'; |
| 139 | |
| 140 | ++str; |
| 141 | |
| 142 | if ((*str < '0') || (*str > '9')) |
| 143 | return (-1); |
| 144 | |
| 145 | *valp = 10 * val + (*str - '0'); |
| 146 | |
| 147 | return (0); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Convert date string: MMDDhhmm[[CC]YY][.ss] |
| 152 | * |
| 153 | * Some basic checking for valid values is done, but this will not catch |
| 154 | * all possible error conditions. |
| 155 | */ |
Mike Frysinger | 59e1472 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 156 | int mk_date (const char *datestr, struct rtc_time *tmp) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 157 | { |
| 158 | int len, val; |
| 159 | char *ptr; |
| 160 | |
Roman Kapl | 0a0dbd4 | 2019-02-08 10:01:02 +0100 | [diff] [blame] | 161 | ptr = strchr(datestr, '.'); |
| 162 | len = strlen(datestr); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 163 | |
| 164 | /* Set seconds */ |
| 165 | if (ptr) { |
| 166 | int sec; |
| 167 | |
Roman Kapl | 0a0dbd4 | 2019-02-08 10:01:02 +0100 | [diff] [blame] | 168 | ptr++; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 169 | if ((len - (ptr - datestr)) != 2) |
| 170 | return (-1); |
| 171 | |
Roman Kapl | 0a0dbd4 | 2019-02-08 10:01:02 +0100 | [diff] [blame] | 172 | len -= 3; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 173 | |
| 174 | if (cnvrt2 (ptr, &sec)) |
| 175 | return (-1); |
| 176 | |
| 177 | tmp->tm_sec = sec; |
| 178 | } else { |
| 179 | tmp->tm_sec = 0; |
| 180 | } |
| 181 | |
| 182 | if (len == 12) { /* MMDDhhmmCCYY */ |
| 183 | int year, century; |
| 184 | |
| 185 | if (cnvrt2 (datestr+ 8, ¢ury) || |
| 186 | cnvrt2 (datestr+10, &year) ) { |
| 187 | return (-1); |
| 188 | } |
| 189 | tmp->tm_year = 100 * century + year; |
| 190 | } else if (len == 10) { /* MMDDhhmmYY */ |
| 191 | int year, century; |
| 192 | |
| 193 | century = tmp->tm_year / 100; |
| 194 | if (cnvrt2 (datestr+ 8, &year)) |
| 195 | return (-1); |
| 196 | tmp->tm_year = 100 * century + year; |
| 197 | } |
| 198 | |
| 199 | switch (len) { |
| 200 | case 8: /* MMDDhhmm */ |
| 201 | /* fall thru */ |
| 202 | case 10: /* MMDDhhmmYY */ |
| 203 | /* fall thru */ |
| 204 | case 12: /* MMDDhhmmCCYY */ |
| 205 | if (cnvrt2 (datestr+0, &val) || |
| 206 | val > 12) { |
| 207 | break; |
| 208 | } |
| 209 | tmp->tm_mon = val; |
| 210 | if (cnvrt2 (datestr+2, &val) || |
| 211 | val > ((tmp->tm_mon==2) ? 29 : 31)) { |
| 212 | break; |
| 213 | } |
| 214 | tmp->tm_mday = val; |
| 215 | |
| 216 | if (cnvrt2 (datestr+4, &val) || |
| 217 | val > 23) { |
| 218 | break; |
| 219 | } |
| 220 | tmp->tm_hour = val; |
| 221 | |
| 222 | if (cnvrt2 (datestr+6, &val) || |
| 223 | val > 59) { |
| 224 | break; |
| 225 | } |
| 226 | tmp->tm_min = val; |
| 227 | |
| 228 | /* calculate day of week */ |
Simon Glass | 2d937bb | 2015-04-20 12:37:17 -0600 | [diff] [blame] | 229 | rtc_calc_weekday(tmp); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 230 | |
| 231 | return (0); |
| 232 | default: |
| 233 | break; |
| 234 | } |
| 235 | |
| 236 | return (-1); |
| 237 | } |
| 238 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 239 | /***************************************************/ |
| 240 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 241 | U_BOOT_CMD( |
| 242 | date, 2, 1, do_date, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 243 | "get/set/reset date & time", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 244 | "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n" |
| 245 | " - without arguments: print date & time\n" |
| 246 | " - with numeric argument: set the system date & time\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 247 | " - with 'reset' argument: reset the RTC" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 248 | ); |