date.c 4.9 KB

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