RTC.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  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 <Kernel/Arch/x86/CPU.h>
  27. #include <Kernel/CMOS.h>
  28. #include <Kernel/IO.h>
  29. #include <Kernel/Time/RTC.h>
  30. #include <Kernel/Time/TimeManagement.h>
  31. namespace Kernel {
  32. #define IRQ_TIMER 8
  33. #define MAX_FREQUENCY 8000
  34. NonnullRefPtr<RealTimeClock> RealTimeClock::create(Function<void(const RegisterState&)> callback)
  35. {
  36. return adopt(*new RealTimeClock(move(callback)));
  37. }
  38. RealTimeClock::RealTimeClock(Function<void(const RegisterState&)> callback)
  39. : HardwareTimer(IRQ_TIMER, move(callback))
  40. {
  41. InterruptDisabler disabler;
  42. NonMaskableInterruptDisabler nmi_disabler;
  43. enable_irq();
  44. CMOS::write(0x8B, CMOS::read(0xB) | 0x40);
  45. reset_to_default_ticks_per_second();
  46. }
  47. void RealTimeClock::handle_irq(const RegisterState& regs)
  48. {
  49. HardwareTimer::handle_irq(regs);
  50. CMOS::read(0x8C);
  51. }
  52. size_t RealTimeClock::ticks_per_second() const
  53. {
  54. return m_frequency;
  55. }
  56. void RealTimeClock::reset_to_default_ticks_per_second()
  57. {
  58. InterruptDisabler disabler;
  59. bool success = try_to_set_frequency(1024);
  60. VERIFY(success);
  61. }
  62. // FIXME: This is a quick & dirty log base 2 with a parameter. Please provide something better in the future.
  63. static int quick_log2(size_t number)
  64. {
  65. int count = 0;
  66. while (number >>= 1)
  67. count++;
  68. return count;
  69. }
  70. bool RealTimeClock::try_to_set_frequency(size_t frequency)
  71. {
  72. InterruptDisabler disabler;
  73. if (!is_capable_of_frequency(frequency))
  74. return false;
  75. disable_irq();
  76. u8 previous_rate = CMOS::read(0x8A);
  77. u8 rate = quick_log2(32768 / frequency) + 1;
  78. dbgln("RTC: Set rate to {}", rate);
  79. CMOS::write(0x8A, (previous_rate & 0xF0) | rate);
  80. m_frequency = frequency;
  81. dbgln("RTC: Set frequency to {} Hz", frequency);
  82. enable_irq();
  83. return true;
  84. }
  85. bool RealTimeClock::is_capable_of_frequency(size_t frequency) const
  86. {
  87. VERIFY(frequency != 0);
  88. if (frequency > MAX_FREQUENCY)
  89. return false;
  90. if (32768 % frequency)
  91. return false;
  92. u16 divider = 32768 / frequency;
  93. return (divider <= 16384 && divider >= 4); // Frequency can be in range of 2 Hz to 8 KHz
  94. }
  95. size_t RealTimeClock::calculate_nearest_possible_frequency(size_t frequency) const
  96. {
  97. VERIFY(frequency != 0);
  98. return frequency;
  99. }
  100. }