RTC.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Time.h>
  7. #include <Kernel/CMOS.h>
  8. #include <Kernel/IO.h>
  9. #include <Kernel/RTC.h>
  10. namespace RTC {
  11. static time_t s_boot_time;
  12. void initialize()
  13. {
  14. s_boot_time = now();
  15. }
  16. time_t boot_time()
  17. {
  18. return s_boot_time;
  19. }
  20. static bool update_in_progress()
  21. {
  22. return CMOS::read(0x0a) & 0x80;
  23. }
  24. static u8 bcd_to_binary(u8 bcd)
  25. {
  26. return (bcd & 0x0F) + ((bcd >> 4) * 10);
  27. }
  28. static bool try_to_read_registers(unsigned& year, unsigned& month, unsigned& day, unsigned& hour, unsigned& minute, unsigned& second)
  29. {
  30. // Note: Let's wait 0.01 seconds until we stop trying to query the RTC CMOS
  31. size_t time_passed_in_milliseconds = 0;
  32. bool update_in_progress_ended_successfully = false;
  33. while (time_passed_in_milliseconds < 100) {
  34. if (!update_in_progress()) {
  35. update_in_progress_ended_successfully = true;
  36. break;
  37. }
  38. IO::delay(1000);
  39. time_passed_in_milliseconds++;
  40. }
  41. if (!update_in_progress_ended_successfully) {
  42. year = 1970;
  43. month = 1;
  44. day = 1;
  45. hour = 0;
  46. minute = 0;
  47. second = 0;
  48. return false;
  49. }
  50. u8 status_b = CMOS::read(0x0b);
  51. second = CMOS::read(0x00);
  52. minute = CMOS::read(0x02);
  53. hour = CMOS::read(0x04);
  54. day = CMOS::read(0x07);
  55. month = CMOS::read(0x08);
  56. year = CMOS::read(0x09);
  57. bool is_pm = hour & 0x80;
  58. if (!(status_b & 0x04)) {
  59. second = bcd_to_binary(second);
  60. minute = bcd_to_binary(minute);
  61. hour = bcd_to_binary(hour & 0x7F);
  62. day = bcd_to_binary(day);
  63. month = bcd_to_binary(month);
  64. year = bcd_to_binary(year);
  65. }
  66. if (!(status_b & 0x02)) {
  67. // In the 12 hour clock, midnight and noon are 12, not 0. Map it to 0.
  68. hour %= 12;
  69. if (is_pm)
  70. hour += 12;
  71. }
  72. year += 2000;
  73. return true;
  74. }
  75. time_t now()
  76. {
  77. auto check_registers_against_preloaded_values = [](unsigned year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second) {
  78. unsigned checked_year, checked_month, checked_day, checked_hour, checked_minute, checked_second;
  79. if (!try_to_read_registers(checked_year, checked_month, checked_day, checked_hour, checked_minute, checked_second))
  80. return false;
  81. return checked_year == year && checked_month == month && checked_day == day && checked_hour == hour && checked_minute == minute && checked_second == second;
  82. };
  83. unsigned year, month, day, hour, minute, second;
  84. bool did_read_rtc_sucessfully = false;
  85. for (size_t attempt = 0; attempt < 5; attempt++) {
  86. if (!try_to_read_registers(year, month, day, hour, minute, second))
  87. break;
  88. if (check_registers_against_preloaded_values(year, month, day, hour, minute, second)) {
  89. did_read_rtc_sucessfully = true;
  90. break;
  91. }
  92. }
  93. dmesgln("RTC: {} Year: {}, month: {}, day: {}, hour: {}, minute: {}, second: {}", (did_read_rtc_sucessfully ? "" : "(failed to read)"), year, month, day, hour, minute, second);
  94. time_t days_since_epoch = years_to_days_since_epoch(year) + day_of_year(year, month, day);
  95. return ((days_since_epoch * 24 + hour) * 60 + minute) * 60 + second;
  96. }
  97. }