RTC.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/LogStream.h>
  28. #include <Kernel/CMOS.h>
  29. #include <Kernel/RTC.h>
  30. namespace RTC {
  31. static time_t s_boot_time;
  32. void initialize()
  33. {
  34. s_boot_time = now();
  35. }
  36. time_t boot_time()
  37. {
  38. return s_boot_time;
  39. }
  40. static bool update_in_progress()
  41. {
  42. return CMOS::read(0x0a) & 0x80;
  43. }
  44. inline bool is_leap_year(unsigned year)
  45. {
  46. return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0));
  47. }
  48. static unsigned days_in_months_since_start_of_year(unsigned month, unsigned year)
  49. {
  50. ASSERT(month <= 11);
  51. unsigned days = 0;
  52. switch (month) {
  53. case 11:
  54. days += 30;
  55. [[fallthrough]];
  56. case 10:
  57. days += 31;
  58. [[fallthrough]];
  59. case 9:
  60. days += 30;
  61. [[fallthrough]];
  62. case 8:
  63. days += 31;
  64. [[fallthrough]];
  65. case 7:
  66. days += 31;
  67. [[fallthrough]];
  68. case 6:
  69. days += 30;
  70. [[fallthrough]];
  71. case 5:
  72. days += 31;
  73. [[fallthrough]];
  74. case 4:
  75. days += 30;
  76. [[fallthrough]];
  77. case 3:
  78. days += 31;
  79. [[fallthrough]];
  80. case 2:
  81. if (is_leap_year(year))
  82. days += 29;
  83. else
  84. days += 28;
  85. [[fallthrough]];
  86. case 1:
  87. days += 31;
  88. }
  89. return days;
  90. }
  91. static unsigned days_in_years_since_epoch(unsigned year)
  92. {
  93. unsigned days = 0;
  94. while (year > 1969) {
  95. days += 365;
  96. if (is_leap_year(year))
  97. ++days;
  98. --year;
  99. }
  100. return days;
  101. }
  102. u8 bcd_to_binary(u8 bcd)
  103. {
  104. return (bcd & 0x0F) + ((bcd >> 4) * 10);
  105. }
  106. void read_registers(unsigned& year, unsigned& month, unsigned& day, unsigned& hour, unsigned& minute, unsigned& second)
  107. {
  108. while (update_in_progress())
  109. ;
  110. u8 status_b = CMOS::read(0x0b);
  111. second = CMOS::read(0x00);
  112. minute = CMOS::read(0x02);
  113. hour = CMOS::read(0x04);
  114. day = CMOS::read(0x07);
  115. month = CMOS::read(0x08);
  116. year = CMOS::read(0x09);
  117. if (!(status_b & 0x04)) {
  118. second = bcd_to_binary(second);
  119. minute = bcd_to_binary(minute);
  120. hour = bcd_to_binary(hour & 0x70);
  121. day = bcd_to_binary(day);
  122. month = bcd_to_binary(month);
  123. year = bcd_to_binary(year);
  124. }
  125. if (!(status_b & 0x02) && (hour & 0x80)) {
  126. hour = ((hour & 0x7F) + 12) % 24;
  127. }
  128. year += 2000;
  129. }
  130. time_t now()
  131. {
  132. // FIXME: We should probably do something more robust here.
  133. // Perhaps read all the values twice and verify that they were identical.
  134. // We don't want to be caught in the middle of an RTC register update.
  135. while (update_in_progress())
  136. ;
  137. unsigned year, month, day, hour, minute, second;
  138. read_registers(year, month, day, hour, minute, second);
  139. klog() << "year: " << year << ", month: " << month << ", day: " << day;
  140. ASSERT(year >= 2018);
  141. return days_in_years_since_epoch(year - 1) * 86400
  142. + days_in_months_since_start_of_year(month - 1, year) * 86400
  143. + (day - 1) * 86400
  144. + hour * 3600
  145. + minute * 60
  146. + second;
  147. }
  148. }