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