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