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