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