rtc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. /*
  25. * RTC test
  26. *
  27. * The Real Time Clock (RTC) operation is verified by this test.
  28. * The following features are verified:
  29. * o) Time uniformity
  30. * This is verified by reading RTC in polling within
  31. * a short period of time.
  32. * o) Passing month boundaries
  33. * This is checked by setting RTC to a second before
  34. * a month boundary and reading it after its passing the
  35. * boundary. The test is performed for both leap- and
  36. * nonleap-years.
  37. */
  38. #ifdef CONFIG_POST
  39. #include <post.h>
  40. #include <rtc.h>
  41. #if CONFIG_POST & CFG_POST_RTC
  42. static int rtc_post_skip (ulong * diff)
  43. {
  44. struct rtc_time tm1;
  45. struct rtc_time tm2;
  46. ulong start1;
  47. ulong start2;
  48. rtc_get (&tm1);
  49. start1 = get_timer (0);
  50. while (1) {
  51. rtc_get (&tm2);
  52. start2 = get_timer (0);
  53. if (tm1.tm_sec != tm2.tm_sec)
  54. break;
  55. if (start2 - start1 > 1500)
  56. break;
  57. }
  58. if (tm1.tm_sec != tm2.tm_sec) {
  59. *diff = start2 - start1;
  60. return 0;
  61. } else {
  62. return -1;
  63. }
  64. }
  65. static void rtc_post_restore (struct rtc_time *tm, unsigned int sec)
  66. {
  67. time_t t = mktime (tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour,
  68. tm->tm_min, tm->tm_sec) + sec;
  69. struct rtc_time ntm;
  70. to_tm (t, &ntm);
  71. rtc_set (&ntm);
  72. }
  73. int rtc_post_test (int flags)
  74. {
  75. ulong diff;
  76. unsigned int i;
  77. struct rtc_time svtm;
  78. static unsigned int daysnl[] =
  79. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  80. static unsigned int daysl[] =
  81. { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  82. unsigned int ynl = 1999;
  83. unsigned int yl = 2000;
  84. unsigned int skipped = 0;
  85. /* Time uniformity */
  86. if (rtc_post_skip (&diff) != 0) {
  87. post_log ("Timeout while waiting for a new second !\n");
  88. return -1;
  89. }
  90. for (i = 0; i < 5; i++) {
  91. if (rtc_post_skip (&diff) != 0) {
  92. post_log ("Timeout while waiting for a new second !\n");
  93. return -1;
  94. }
  95. if (diff < 950 || diff > 1050) {
  96. post_log ("Invalid second duration !\n");
  97. return -1;
  98. }
  99. }
  100. /* Passing month boundaries */
  101. if (rtc_post_skip (&diff) != 0) {
  102. post_log ("Timeout while waiting for a new second !\n");
  103. return -1;
  104. }
  105. rtc_get (&svtm);
  106. for (i = 0; i < 12; i++) {
  107. time_t t = mktime (ynl, i + 1, daysnl[i], 23, 59, 59);
  108. struct rtc_time tm;
  109. to_tm (t, &tm);
  110. rtc_set (&tm);
  111. skipped++;
  112. if (rtc_post_skip (&diff) != 0) {
  113. rtc_post_restore (&svtm, skipped);
  114. post_log ("Timeout while waiting for a new second !\n");
  115. return -1;
  116. }
  117. rtc_get (&tm);
  118. if (tm.tm_mon == i + 1) {
  119. rtc_post_restore (&svtm, skipped);
  120. post_log ("Month %d boundary is not passed !\n", i + 1);
  121. return -1;
  122. }
  123. }
  124. for (i = 0; i < 12; i++) {
  125. time_t t = mktime (yl, i + 1, daysl[i], 23, 59, 59);
  126. struct rtc_time tm;
  127. to_tm (t, &tm);
  128. rtc_set (&tm);
  129. skipped++;
  130. if (rtc_post_skip (&diff) != 0) {
  131. rtc_post_restore (&svtm, skipped);
  132. post_log ("Timeout while waiting for a new second !\n");
  133. return -1;
  134. }
  135. rtc_get (&tm);
  136. if (tm.tm_mon == i + 1) {
  137. rtc_post_restore (&svtm, skipped);
  138. post_log ("Month %d boundary is not passed !\n", i + 1);
  139. return -1;
  140. }
  141. }
  142. rtc_post_restore (&svtm, skipped);
  143. return 0;
  144. }
  145. #endif /* CONFIG_POST & CFG_POST_RTC */
  146. #endif /* CONFIG_POST */