rtc.c 4.3 KB

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