rtc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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) RTC Power Fault
  30. * This is verified by analyzing the rtc_get() return status.
  31. * o) Time uniformity
  32. * This is verified by reading RTC in polling within
  33. * a short period of time.
  34. * o) Passing month boundaries
  35. * This is checked by setting RTC to a second before
  36. * a month boundary and reading it after its passing the
  37. * boundary. The test is performed for both leap- and
  38. * nonleap-years.
  39. */
  40. #ifdef CONFIG_POST
  41. #include <post.h>
  42. #include <rtc.h>
  43. #if CONFIG_POST & CFG_POST_RTC
  44. static int rtc_post_skip (ulong * diff)
  45. {
  46. struct rtc_time tm1;
  47. struct rtc_time tm2;
  48. ulong start1;
  49. ulong start2;
  50. rtc_get (&tm1);
  51. start1 = get_timer (0);
  52. while (1) {
  53. rtc_get (&tm2);
  54. start2 = get_timer (0);
  55. if (tm1.tm_sec != tm2.tm_sec)
  56. break;
  57. if (start2 - start1 > 1500)
  58. break;
  59. }
  60. if (tm1.tm_sec != tm2.tm_sec) {
  61. *diff = start2 - start1;
  62. return 0;
  63. } else {
  64. return -1;
  65. }
  66. }
  67. static void rtc_post_restore (struct rtc_time *tm, unsigned int sec)
  68. {
  69. time_t t = mktime (tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour,
  70. tm->tm_min, tm->tm_sec) + sec;
  71. struct rtc_time ntm;
  72. to_tm (t, &ntm);
  73. rtc_set (&ntm);
  74. }
  75. int rtc_post_test (int flags)
  76. {
  77. ulong diff;
  78. unsigned int i;
  79. struct rtc_time svtm;
  80. static unsigned int daysnl[] =
  81. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  82. static unsigned int daysl[] =
  83. { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  84. unsigned int ynl = 1999;
  85. unsigned int yl = 2000;
  86. unsigned int skipped = 0;
  87. int reliable;
  88. /* Time reliability */
  89. reliable = rtc_get (&svtm);
  90. /* Time uniformity */
  91. if (rtc_post_skip (&diff) != 0) {
  92. post_log ("Timeout while waiting for a new second !\n");
  93. return -1;
  94. }
  95. for (i = 0; i < 5; i++) {
  96. if (rtc_post_skip (&diff) != 0) {
  97. post_log ("Timeout while waiting for a new second !\n");
  98. return -1;
  99. }
  100. if (diff < 950 || diff > 1050) {
  101. post_log ("Invalid second duration !\n");
  102. return -1;
  103. }
  104. }
  105. /* Passing month boundaries */
  106. if (rtc_post_skip (&diff) != 0) {
  107. post_log ("Timeout while waiting for a new second !\n");
  108. return -1;
  109. }
  110. rtc_get (&svtm);
  111. for (i = 0; i < 12; i++) {
  112. time_t t = mktime (ynl, i + 1, daysnl[i], 23, 59, 59);
  113. struct rtc_time tm;
  114. to_tm (t, &tm);
  115. rtc_set (&tm);
  116. skipped++;
  117. if (rtc_post_skip (&diff) != 0) {
  118. rtc_post_restore (&svtm, skipped);
  119. post_log ("Timeout while waiting for a new second !\n");
  120. return -1;
  121. }
  122. rtc_get (&tm);
  123. if (tm.tm_mon == i + 1) {
  124. rtc_post_restore (&svtm, skipped);
  125. post_log ("Month %d boundary is not passed !\n", i + 1);
  126. return -1;
  127. }
  128. }
  129. for (i = 0; i < 12; i++) {
  130. time_t t = mktime (yl, i + 1, daysl[i], 23, 59, 59);
  131. struct rtc_time tm;
  132. to_tm (t, &tm);
  133. rtc_set (&tm);
  134. skipped++;
  135. if (rtc_post_skip (&diff) != 0) {
  136. rtc_post_restore (&svtm, skipped);
  137. post_log ("Timeout while waiting for a new second !\n");
  138. return -1;
  139. }
  140. rtc_get (&tm);
  141. if (tm.tm_mon == i + 1) {
  142. rtc_post_restore (&svtm, skipped);
  143. post_log ("Month %d boundary is not passed !\n", i + 1);
  144. return -1;
  145. }
  146. }
  147. rtc_post_restore (&svtm, skipped);
  148. /* If come here, then RTC operates correcty, check the correctness
  149. * of the time it reports.
  150. */
  151. if (reliable < 0) {
  152. post_log ("RTC Time is not reliable! Power fault? \n");
  153. return -1;
  154. }
  155. return 0;
  156. }
  157. #endif /* CONFIG_POST & CFG_POST_RTC */
  158. #endif /* CONFIG_POST */