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 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * RTC, Date & Time support: get and set date & time |
| 26 | */ |
| 27 | #include <common.h> |
| 28 | #include <command.h> |
| 29 | #include <rtc.h> |
Stefan Roese | 0dc018e | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 30 | #include <i2c.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 31 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
Mike Frysinger | bdbc130 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 34 | static const char * const weekdays[] = { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 35 | "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", |
| 36 | }; |
| 37 | |
Wolfgang Denk | 2e5167c | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 38 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 39 | #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off)) |
Wolfgang Denk | 2e5167c | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 40 | #else |
| 41 | #define RELOC(a) a |
Peter Tyser | 521af04 | 2009-09-21 11:20:36 -0500 | [diff] [blame] | 42 | #endif |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 43 | |
Mike Frysinger | bdbc130 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 44 | int mk_date (const char *, struct rtc_time *); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 45 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 46 | 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] | 47 | { |
| 48 | struct rtc_time tm; |
| 49 | int rcode = 0; |
Stefan Roese | 0dc018e | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 50 | int old_bus; |
| 51 | |
| 52 | /* switch to correct I2C bus */ |
| 53 | old_bus = I2C_GET_BUS(); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 54 | I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 55 | |
| 56 | switch (argc) { |
| 57 | case 2: /* set date & time */ |
| 58 | if (strcmp(argv[1],"reset") == 0) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 59 | puts ("Reset RTC...\n"); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 60 | rtc_reset (); |
| 61 | } else { |
| 62 | /* initialize tm with current time */ |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 63 | rcode = rtc_get (&tm); |
| 64 | |
| 65 | if(!rcode) { |
| 66 | /* insert new date & time */ |
| 67 | if (mk_date (argv[1], &tm) != 0) { |
| 68 | puts ("## Bad date format\n"); |
| 69 | break; |
| 70 | } |
| 71 | /* and write to RTC */ |
| 72 | rcode = rtc_set (&tm); |
| 73 | if(rcode) |
Magnus Lilja | d52e3e0 | 2009-11-11 19:56:36 +0100 | [diff] [blame] | 74 | puts("## Set date failed\n"); |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 75 | } else { |
Magnus Lilja | d52e3e0 | 2009-11-11 19:56:36 +0100 | [diff] [blame] | 76 | puts("## Get date failed\n"); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 77 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 78 | } |
| 79 | /* FALL TROUGH */ |
| 80 | case 1: /* get date & time */ |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 81 | rcode = rtc_get (&tm); |
| 82 | |
| 83 | if (rcode) { |
Magnus Lilja | d52e3e0 | 2009-11-11 19:56:36 +0100 | [diff] [blame] | 84 | puts("## Get date failed\n"); |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 85 | break; |
| 86 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 87 | |
| 88 | printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n", |
| 89 | tm.tm_year, tm.tm_mon, tm.tm_mday, |
| 90 | (tm.tm_wday<0 || tm.tm_wday>6) ? |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 91 | "unknown " : RELOC(weekdays[tm.tm_wday]), |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 92 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 93 | |
Stefan Roese | 0dc018e | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 94 | break; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 95 | default: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 96 | rcode = CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 97 | } |
Stefan Roese | 0dc018e | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 98 | |
| 99 | /* switch back to original I2C bus */ |
| 100 | I2C_SET_BUS(old_bus); |
| 101 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 102 | return rcode; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * simple conversion of two-digit string with error checking |
| 107 | */ |
Mike Frysinger | bdbc130 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 108 | static int cnvrt2 (const char *str, int *valp) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 109 | { |
| 110 | int val; |
| 111 | |
| 112 | if ((*str < '0') || (*str > '9')) |
| 113 | return (-1); |
| 114 | |
| 115 | val = *str - '0'; |
| 116 | |
| 117 | ++str; |
| 118 | |
| 119 | if ((*str < '0') || (*str > '9')) |
| 120 | return (-1); |
| 121 | |
| 122 | *valp = 10 * val + (*str - '0'); |
| 123 | |
| 124 | return (0); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Convert date string: MMDDhhmm[[CC]YY][.ss] |
| 129 | * |
| 130 | * Some basic checking for valid values is done, but this will not catch |
| 131 | * all possible error conditions. |
| 132 | */ |
Mike Frysinger | bdbc130 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 133 | int mk_date (const char *datestr, struct rtc_time *tmp) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 134 | { |
| 135 | int len, val; |
| 136 | char *ptr; |
| 137 | |
| 138 | ptr = strchr (datestr,'.'); |
| 139 | len = strlen (datestr); |
| 140 | |
| 141 | /* Set seconds */ |
| 142 | if (ptr) { |
| 143 | int sec; |
| 144 | |
| 145 | *ptr++ = '\0'; |
| 146 | if ((len - (ptr - datestr)) != 2) |
| 147 | return (-1); |
| 148 | |
| 149 | len = strlen (datestr); |
| 150 | |
| 151 | if (cnvrt2 (ptr, &sec)) |
| 152 | return (-1); |
| 153 | |
| 154 | tmp->tm_sec = sec; |
| 155 | } else { |
| 156 | tmp->tm_sec = 0; |
| 157 | } |
| 158 | |
| 159 | if (len == 12) { /* MMDDhhmmCCYY */ |
| 160 | int year, century; |
| 161 | |
| 162 | if (cnvrt2 (datestr+ 8, ¢ury) || |
| 163 | cnvrt2 (datestr+10, &year) ) { |
| 164 | return (-1); |
| 165 | } |
| 166 | tmp->tm_year = 100 * century + year; |
| 167 | } else if (len == 10) { /* MMDDhhmmYY */ |
| 168 | int year, century; |
| 169 | |
| 170 | century = tmp->tm_year / 100; |
| 171 | if (cnvrt2 (datestr+ 8, &year)) |
| 172 | return (-1); |
| 173 | tmp->tm_year = 100 * century + year; |
| 174 | } |
| 175 | |
| 176 | switch (len) { |
| 177 | case 8: /* MMDDhhmm */ |
| 178 | /* fall thru */ |
| 179 | case 10: /* MMDDhhmmYY */ |
| 180 | /* fall thru */ |
| 181 | case 12: /* MMDDhhmmCCYY */ |
| 182 | if (cnvrt2 (datestr+0, &val) || |
| 183 | val > 12) { |
| 184 | break; |
| 185 | } |
| 186 | tmp->tm_mon = val; |
| 187 | if (cnvrt2 (datestr+2, &val) || |
| 188 | val > ((tmp->tm_mon==2) ? 29 : 31)) { |
| 189 | break; |
| 190 | } |
| 191 | tmp->tm_mday = val; |
| 192 | |
| 193 | if (cnvrt2 (datestr+4, &val) || |
| 194 | val > 23) { |
| 195 | break; |
| 196 | } |
| 197 | tmp->tm_hour = val; |
| 198 | |
| 199 | if (cnvrt2 (datestr+6, &val) || |
| 200 | val > 59) { |
| 201 | break; |
| 202 | } |
| 203 | tmp->tm_min = val; |
| 204 | |
| 205 | /* calculate day of week */ |
| 206 | GregorianDay (tmp); |
| 207 | |
| 208 | return (0); |
| 209 | default: |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | return (-1); |
| 214 | } |
| 215 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 216 | /***************************************************/ |
| 217 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 218 | U_BOOT_CMD( |
| 219 | date, 2, 1, do_date, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 220 | "get/set/reset date & time", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 221 | "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n" |
| 222 | " - without arguments: print date & time\n" |
| 223 | " - with numeric argument: set the system date & time\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 224 | " - with 'reset' argument: reset the RTC" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 225 | ); |