date.c 4.8 KB

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