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