RTC.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <AK/Time.h>
  29. #include <Kernel/CMOS.h>
  30. #include <Kernel/RTC.h>
  31. namespace RTC {
  32. static time_t s_boot_time;
  33. void initialize()
  34. {
  35. s_boot_time = now();
  36. }
  37. time_t boot_time()
  38. {
  39. return s_boot_time;
  40. }
  41. static bool update_in_progress()
  42. {
  43. return CMOS::read(0x0a) & 0x80;
  44. }
  45. static u8 bcd_to_binary(u8 bcd)
  46. {
  47. return (bcd & 0x0F) + ((bcd >> 4) * 10);
  48. }
  49. void read_registers(unsigned& year, unsigned& month, unsigned& day, unsigned& hour, unsigned& minute, unsigned& second)
  50. {
  51. while (update_in_progress())
  52. ;
  53. u8 status_b = CMOS::read(0x0b);
  54. second = CMOS::read(0x00);
  55. minute = CMOS::read(0x02);
  56. hour = CMOS::read(0x04);
  57. day = CMOS::read(0x07);
  58. month = CMOS::read(0x08);
  59. year = CMOS::read(0x09);
  60. bool is_pm = hour & 0x80;
  61. if (!(status_b & 0x04)) {
  62. second = bcd_to_binary(second);
  63. minute = bcd_to_binary(minute);
  64. hour = bcd_to_binary(hour & 0x7F);
  65. day = bcd_to_binary(day);
  66. month = bcd_to_binary(month);
  67. year = bcd_to_binary(year);
  68. }
  69. if (!(status_b & 0x02)) {
  70. // In the 12 hour clock, midnight and noon are 12, not 0. Map it to 0.
  71. hour %= 12;
  72. if (is_pm)
  73. hour += 12;
  74. }
  75. year += 2000;
  76. }
  77. time_t now()
  78. {
  79. // FIXME: We should probably do something more robust here.
  80. // Perhaps read all the values twice and verify that they were identical.
  81. // We don't want to be caught in the middle of an RTC register update.
  82. while (update_in_progress())
  83. ;
  84. unsigned year, month, day, hour, minute, second;
  85. read_registers(year, month, day, hour, minute, second);
  86. klog() << "RTC: Year: " << year << ", month: " << month << ", day: " << day << ", hour: " << hour << ", minute: " << minute << ", second: " << second;
  87. time_t days_since_epoch = years_to_days_since_epoch(year) + day_of_year(year, month, day);
  88. return ((days_since_epoch * 24 + hour) * 60 + minute) * 60 + second;
  89. }
  90. }