date.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. /*
  7. * RTC, Date & Time support: get and set date & time
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <dm.h>
  12. #include <rtc.h>
  13. #include <i2c.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. static const char * const weekdays[] = {
  16. "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
  17. };
  18. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  19. #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
  20. #else
  21. #define RELOC(a) a
  22. #endif
  23. int mk_date (const char *, struct rtc_time *);
  24. static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
  25. static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  26. {
  27. struct rtc_time tm;
  28. int rcode = 0;
  29. int old_bus __maybe_unused;
  30. /* switch to correct I2C bus */
  31. #ifdef CONFIG_DM_RTC
  32. struct udevice *dev;
  33. rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
  34. if (rcode) {
  35. printf("Cannot find RTC: err=%d\n", rcode);
  36. return CMD_RET_FAILURE;
  37. }
  38. #elif defined(CONFIG_SYS_I2C)
  39. old_bus = i2c_get_bus_num();
  40. i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
  41. #else
  42. old_bus = I2C_GET_BUS();
  43. I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
  44. #endif
  45. switch (argc) {
  46. case 2: /* set date & time */
  47. if (strcmp(argv[1],"reset") == 0) {
  48. puts ("Reset RTC...\n");
  49. #ifdef CONFIG_DM_RTC
  50. rcode = dm_rtc_reset(dev);
  51. if (!rcode)
  52. rcode = dm_rtc_set(dev, &default_tm);
  53. #else
  54. rtc_reset();
  55. rcode = rtc_set(&default_tm);
  56. #endif
  57. if (rcode)
  58. puts("## Failed to set date after RTC reset\n");
  59. } else {
  60. /* initialize tm with current time */
  61. #ifdef CONFIG_DM_RTC
  62. rcode = dm_rtc_get(dev, &tm);
  63. #else
  64. rcode = rtc_get(&tm);
  65. #endif
  66. if (!rcode) {
  67. /* insert new date & time */
  68. if (mk_date(argv[1], &tm) != 0) {
  69. puts ("## Bad date format\n");
  70. break;
  71. }
  72. /* and write to RTC */
  73. #ifdef CONFIG_DM_RTC
  74. rcode = dm_rtc_set(dev, &tm);
  75. #else
  76. rcode = rtc_set(&tm);
  77. #endif
  78. if (rcode) {
  79. printf("## Set date failed: err=%d\n",
  80. rcode);
  81. }
  82. } else {
  83. puts("## Get date failed\n");
  84. }
  85. }
  86. /* FALL TROUGH */
  87. case 1: /* get date & time */
  88. #ifdef CONFIG_DM_RTC
  89. rcode = dm_rtc_get(dev, &tm);
  90. #else
  91. rcode = rtc_get(&tm);
  92. #endif
  93. if (rcode) {
  94. puts("## Get date failed\n");
  95. break;
  96. }
  97. printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
  98. tm.tm_year, tm.tm_mon, tm.tm_mday,
  99. (tm.tm_wday<0 || tm.tm_wday>6) ?
  100. "unknown " : RELOC(weekdays[tm.tm_wday]),
  101. tm.tm_hour, tm.tm_min, tm.tm_sec);
  102. break;
  103. default:
  104. rcode = CMD_RET_USAGE;
  105. }
  106. /* switch back to original I2C bus */
  107. #ifdef CONFIG_SYS_I2C
  108. i2c_set_bus_num(old_bus);
  109. #elif !defined(CONFIG_DM_RTC)
  110. I2C_SET_BUS(old_bus);
  111. #endif
  112. return rcode ? CMD_RET_FAILURE : 0;
  113. }
  114. /*
  115. * simple conversion of two-digit string with error checking
  116. */
  117. static int cnvrt2 (const char *str, int *valp)
  118. {
  119. int val;
  120. if ((*str < '0') || (*str > '9'))
  121. return (-1);
  122. val = *str - '0';
  123. ++str;
  124. if ((*str < '0') || (*str > '9'))
  125. return (-1);
  126. *valp = 10 * val + (*str - '0');
  127. return (0);
  128. }
  129. /*
  130. * Convert date string: MMDDhhmm[[CC]YY][.ss]
  131. *
  132. * Some basic checking for valid values is done, but this will not catch
  133. * all possible error conditions.
  134. */
  135. int mk_date (const char *datestr, struct rtc_time *tmp)
  136. {
  137. int len, val;
  138. char *ptr;
  139. ptr = strchr(datestr, '.');
  140. len = strlen(datestr);
  141. /* Set seconds */
  142. if (ptr) {
  143. int sec;
  144. ptr++;
  145. if ((len - (ptr - datestr)) != 2)
  146. return (-1);
  147. len -= 3;
  148. if (cnvrt2 (ptr, &sec))
  149. return (-1);
  150. tmp->tm_sec = sec;
  151. } else {
  152. tmp->tm_sec = 0;
  153. }
  154. if (len == 12) { /* MMDDhhmmCCYY */
  155. int year, century;
  156. if (cnvrt2 (datestr+ 8, &century) ||
  157. cnvrt2 (datestr+10, &year) ) {
  158. return (-1);
  159. }
  160. tmp->tm_year = 100 * century + year;
  161. } else if (len == 10) { /* MMDDhhmmYY */
  162. int year, century;
  163. century = tmp->tm_year / 100;
  164. if (cnvrt2 (datestr+ 8, &year))
  165. return (-1);
  166. tmp->tm_year = 100 * century + year;
  167. }
  168. switch (len) {
  169. case 8: /* MMDDhhmm */
  170. /* fall thru */
  171. case 10: /* MMDDhhmmYY */
  172. /* fall thru */
  173. case 12: /* MMDDhhmmCCYY */
  174. if (cnvrt2 (datestr+0, &val) ||
  175. val > 12) {
  176. break;
  177. }
  178. tmp->tm_mon = val;
  179. if (cnvrt2 (datestr+2, &val) ||
  180. val > ((tmp->tm_mon==2) ? 29 : 31)) {
  181. break;
  182. }
  183. tmp->tm_mday = val;
  184. if (cnvrt2 (datestr+4, &val) ||
  185. val > 23) {
  186. break;
  187. }
  188. tmp->tm_hour = val;
  189. if (cnvrt2 (datestr+6, &val) ||
  190. val > 59) {
  191. break;
  192. }
  193. tmp->tm_min = val;
  194. /* calculate day of week */
  195. rtc_calc_weekday(tmp);
  196. return (0);
  197. default:
  198. break;
  199. }
  200. return (-1);
  201. }
  202. /***************************************************/
  203. U_BOOT_CMD(
  204. date, 2, 1, do_date,
  205. "get/set/reset date & time",
  206. "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
  207. " - without arguments: print date & time\n"
  208. " - with numeric argument: set the system date & time\n"
  209. " - with 'reset' argument: reset the RTC"
  210. );