PIT.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <Kernel/Arch/i386/CPU.h>
  27. #include <Kernel/Devices/PIT.h>
  28. #include <Kernel/Interrupts/PIC.h>
  29. #include <Kernel/Scheduler.h>
  30. #include <Kernel/Thread.h>
  31. #include <LibBareMetal/IO.h>
  32. #define IRQ_TIMER 0
  33. namespace Kernel {
  34. //#define PIT_DEBUG
  35. static PIT* s_the;
  36. void PIT::initialize()
  37. {
  38. if (s_the == nullptr) {
  39. s_the = new PIT();
  40. }
  41. }
  42. PIT& PIT::the()
  43. {
  44. ASSERT(s_the != nullptr);
  45. return *s_the;
  46. }
  47. inline static void reset_countdown(u16 timer_reload)
  48. {
  49. IO::out8(PIT_CTL, TIMER0_SELECT | WRITE_WORD | MODE_COUNTDOWN);
  50. IO::out8(TIMER0_CTL, LSB(timer_reload));
  51. IO::out8(TIMER0_CTL, MSB(timer_reload));
  52. }
  53. void PIT::handle_irq(RegisterState& regs)
  54. {
  55. #ifdef PIT_DEBUG
  56. dbg() << "PIT: Debugging Interrupt.";
  57. #endif
  58. if (++m_ticks_this_second >= TICKS_PER_SECOND) {
  59. // FIXME: Synchronize with the RTC somehow to prevent drifting apart.
  60. ++m_seconds_since_boot;
  61. m_ticks_this_second = 0;
  62. }
  63. Scheduler::timer_tick(regs);
  64. }
  65. u32 PIT::ticks_this_second() const
  66. {
  67. return m_ticks_this_second;
  68. }
  69. PIT::PIT()
  70. : HardwareTimer(IRQ_TIMER)
  71. , m_default_timer_reload(BASE_FREQUENCY / TICKS_PER_SECOND)
  72. , m_ticks_this_second(0)
  73. {
  74. IO::out8(PIT_CTL, TIMER0_SELECT | WRITE_WORD | MODE_SQUARE_WAVE);
  75. klog() << "PIT: " << TICKS_PER_SECOND << " Hz, square wave (" << String::format("%x", m_default_timer_reload) << ")";
  76. IO::out8(TIMER0_CTL, LSB(m_default_timer_reload));
  77. IO::out8(TIMER0_CTL, MSB(m_default_timer_reload));
  78. enable_irq();
  79. }
  80. }