Tom Rini | 10e4779 | 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 | */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 10 | #include <command.h> |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 11 | #include <dm.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 12 | #include <rtc.h> |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 13 | #include <i2c.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 15 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Mike Frysinger | 59e1472 | 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 | |
Mike Frysinger | 59e1472 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 22 | int mk_date (const char *, struct rtc_time *); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 23 | |
Marek Vasut | d45c1b8 | 2015-04-10 01:11:54 +0200 | [diff] [blame] | 24 | static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 }; |
| 25 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 26 | static int do_date(struct cmd_tbl *cmdtp, int flag, int argc, |
| 27 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 28 | { |
| 29 | struct rtc_time tm; |
| 30 | int rcode = 0; |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 31 | int old_bus __maybe_unused; |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 32 | |
| 33 | /* switch to correct I2C bus */ |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 34 | struct udevice *dev; |
| 35 | |
Michal Simek | 452359e | 2021-07-29 12:59:12 +0200 | [diff] [blame] | 36 | rcode = uclass_get_device_by_seq(UCLASS_RTC, 0, &dev); |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 37 | if (rcode) { |
Michal Simek | 452359e | 2021-07-29 12:59:12 +0200 | [diff] [blame] | 38 | rcode = uclass_get_device(UCLASS_RTC, 0, &dev); |
| 39 | if (rcode) { |
| 40 | printf("Cannot find RTC: err=%d\n", rcode); |
| 41 | return CMD_RET_FAILURE; |
| 42 | } |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 43 | } |
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 | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 48 | puts ("Reset RTC...\n"); |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 49 | rcode = dm_rtc_reset(dev); |
| 50 | if (!rcode) |
| 51 | rcode = dm_rtc_set(dev, &default_tm); |
Marek Vasut | d45c1b8 | 2015-04-10 01:11:54 +0200 | [diff] [blame] | 52 | if (rcode) |
| 53 | puts("## Failed to set date after RTC reset\n"); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 54 | } else { |
| 55 | /* initialize tm with current time */ |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 56 | rcode = dm_rtc_get(dev, &tm); |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 57 | if (!rcode) { |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 58 | /* insert new date & time */ |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 59 | if (mk_date(argv[1], &tm) != 0) { |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 60 | puts ("## Bad date format\n"); |
| 61 | break; |
| 62 | } |
| 63 | /* and write to RTC */ |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 64 | rcode = dm_rtc_set(dev, &tm); |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 65 | if (rcode) { |
| 66 | printf("## Set date failed: err=%d\n", |
| 67 | rcode); |
| 68 | } |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 69 | } else { |
Magnus Lilja | 5599236 | 2009-11-11 19:56:36 +0100 | [diff] [blame] | 70 | puts("## Get date failed\n"); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 71 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 72 | } |
Heinrich Schuchardt | 5d520cf | 2023-04-01 09:14:11 +0200 | [diff] [blame] | 73 | fallthrough; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 74 | case 1: /* get date & time */ |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 75 | rcode = dm_rtc_get(dev, &tm); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 76 | if (rcode) { |
Magnus Lilja | 5599236 | 2009-11-11 19:56:36 +0100 | [diff] [blame] | 77 | puts("## Get date failed\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 78 | break; |
| 79 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 80 | |
| 81 | printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n", |
| 82 | tm.tm_year, tm.tm_mon, tm.tm_mday, |
| 83 | (tm.tm_wday<0 || tm.tm_wday>6) ? |
Marek Vasut | b3c29df | 2023-09-06 23:29:51 +0200 | [diff] [blame] | 84 | "unknown " : weekdays[tm.tm_wday], |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 85 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 86 | |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 87 | break; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 88 | default: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 89 | rcode = CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 90 | } |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 91 | |
Simon Glass | a8efb4a | 2015-04-20 12:37:26 -0600 | [diff] [blame] | 92 | return rcode ? CMD_RET_FAILURE : 0; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
| 96 | * simple conversion of two-digit string with error checking |
| 97 | */ |
Mike Frysinger | 59e1472 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 98 | static int cnvrt2 (const char *str, int *valp) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 99 | { |
| 100 | int val; |
| 101 | |
| 102 | if ((*str < '0') || (*str > '9')) |
| 103 | return (-1); |
| 104 | |
| 105 | val = *str - '0'; |
| 106 | |
| 107 | ++str; |
| 108 | |
| 109 | if ((*str < '0') || (*str > '9')) |
| 110 | return (-1); |
| 111 | |
| 112 | *valp = 10 * val + (*str - '0'); |
| 113 | |
| 114 | return (0); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Convert date string: MMDDhhmm[[CC]YY][.ss] |
| 119 | * |
| 120 | * Some basic checking for valid values is done, but this will not catch |
| 121 | * all possible error conditions. |
| 122 | */ |
Mike Frysinger | 59e1472 | 2010-10-20 07:17:23 -0400 | [diff] [blame] | 123 | int mk_date (const char *datestr, struct rtc_time *tmp) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 124 | { |
| 125 | int len, val; |
| 126 | char *ptr; |
| 127 | |
Roman Kapl | 0a0dbd4 | 2019-02-08 10:01:02 +0100 | [diff] [blame] | 128 | ptr = strchr(datestr, '.'); |
| 129 | len = strlen(datestr); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 130 | |
| 131 | /* Set seconds */ |
| 132 | if (ptr) { |
| 133 | int sec; |
| 134 | |
Roman Kapl | 0a0dbd4 | 2019-02-08 10:01:02 +0100 | [diff] [blame] | 135 | ptr++; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 136 | if ((len - (ptr - datestr)) != 2) |
| 137 | return (-1); |
| 138 | |
Roman Kapl | 0a0dbd4 | 2019-02-08 10:01:02 +0100 | [diff] [blame] | 139 | len -= 3; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 140 | |
| 141 | if (cnvrt2 (ptr, &sec)) |
| 142 | return (-1); |
| 143 | |
| 144 | tmp->tm_sec = sec; |
| 145 | } else { |
| 146 | tmp->tm_sec = 0; |
| 147 | } |
| 148 | |
| 149 | if (len == 12) { /* MMDDhhmmCCYY */ |
| 150 | int year, century; |
| 151 | |
| 152 | if (cnvrt2 (datestr+ 8, ¢ury) || |
| 153 | cnvrt2 (datestr+10, &year) ) { |
| 154 | return (-1); |
| 155 | } |
| 156 | tmp->tm_year = 100 * century + year; |
| 157 | } else if (len == 10) { /* MMDDhhmmYY */ |
| 158 | int year, century; |
| 159 | |
| 160 | century = tmp->tm_year / 100; |
| 161 | if (cnvrt2 (datestr+ 8, &year)) |
| 162 | return (-1); |
| 163 | tmp->tm_year = 100 * century + year; |
| 164 | } |
| 165 | |
| 166 | switch (len) { |
| 167 | case 8: /* MMDDhhmm */ |
| 168 | /* fall thru */ |
| 169 | case 10: /* MMDDhhmmYY */ |
| 170 | /* fall thru */ |
| 171 | case 12: /* MMDDhhmmCCYY */ |
| 172 | if (cnvrt2 (datestr+0, &val) || |
| 173 | val > 12) { |
| 174 | break; |
| 175 | } |
| 176 | tmp->tm_mon = val; |
| 177 | if (cnvrt2 (datestr+2, &val) || |
| 178 | val > ((tmp->tm_mon==2) ? 29 : 31)) { |
| 179 | break; |
| 180 | } |
| 181 | tmp->tm_mday = val; |
| 182 | |
| 183 | if (cnvrt2 (datestr+4, &val) || |
| 184 | val > 23) { |
| 185 | break; |
| 186 | } |
| 187 | tmp->tm_hour = val; |
| 188 | |
| 189 | if (cnvrt2 (datestr+6, &val) || |
| 190 | val > 59) { |
| 191 | break; |
| 192 | } |
| 193 | tmp->tm_min = val; |
| 194 | |
| 195 | /* calculate day of week */ |
Simon Glass | 2d937bb | 2015-04-20 12:37:17 -0600 | [diff] [blame] | 196 | rtc_calc_weekday(tmp); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 197 | |
| 198 | return (0); |
| 199 | default: |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | return (-1); |
| 204 | } |
| 205 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 206 | /***************************************************/ |
| 207 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 208 | U_BOOT_CMD( |
| 209 | date, 2, 1, do_date, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 210 | "get/set/reset date & time", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 211 | "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n" |
| 212 | " - without arguments: print date & time\n" |
| 213 | " - with numeric argument: set the system date & time\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 214 | " - with 'reset' argument: reset the RTC" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 215 | ); |