date.c 4.9 KB

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