Serenity Operating System
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
27#include <Kernel/Arch/i386/CPU.h>
28#include <Kernel/Interrupts/PIC.h>
29#include <Kernel/Scheduler.h>
30#include <Kernel/Thread.h>
31#include <Kernel/Time/PIT.h>
32#include <Kernel/Time/TimeManagement.h>
33#include <LibBareMetal/IO.h>
34
35#define IRQ_TIMER 0
36namespace Kernel {
37
38NonnullRefPtr<PIT> PIT::initialize(Function<void(const RegisterState&)> callback)
39{
40 return adopt(*new PIT(move(callback)));
41}
42
43inline static void reset_countdown(u16 timer_reload)
44{
45 IO::out8(PIT_CTL, TIMER0_SELECT | WRITE_WORD | MODE_COUNTDOWN);
46 IO::out8(TIMER0_CTL, LSB(timer_reload));
47 IO::out8(TIMER0_CTL, MSB(timer_reload));
48}
49
50PIT::PIT(Function<void(const RegisterState&)> callback)
51 : HardwareTimer(IRQ_TIMER, move(callback))
52 , m_periodic(true)
53{
54 IO::out8(PIT_CTL, TIMER0_SELECT | WRITE_WORD | MODE_SQUARE_WAVE);
55
56 klog() << "PIT: " << OPTIMAL_TICKS_PER_SECOND_RATE << " Hz, square wave (" << String::format("%x", BASE_FREQUENCY / OPTIMAL_TICKS_PER_SECOND_RATE) << ")";
57 reset_to_default_ticks_per_second();
58 enable_irq();
59}
60
61size_t PIT::ticks_per_second() const
62{
63 return m_frequency;
64}
65
66void PIT::set_periodic()
67{
68 // FIXME: Implement it...
69 ASSERT_NOT_REACHED();
70}
71void PIT::set_non_periodic()
72{
73 // FIXME: Implement it...
74 ASSERT_NOT_REACHED();
75}
76
77void PIT::reset_to_default_ticks_per_second()
78{
79 InterruptDisabler disabler;
80 bool success = try_to_set_frequency(OPTIMAL_TICKS_PER_SECOND_RATE);
81 ASSERT(success);
82}
83
84bool PIT::try_to_set_frequency(size_t frequency)
85{
86 InterruptDisabler disabler;
87 if (!is_capable_of_frequency(frequency))
88 return false;
89 disable_irq();
90 size_t reload_value = BASE_FREQUENCY / frequency;
91 IO::out8(TIMER0_CTL, LSB(reload_value));
92 IO::out8(TIMER0_CTL, MSB(reload_value));
93 m_frequency = frequency;
94 enable_irq();
95 return true;
96}
97bool PIT::is_capable_of_frequency(size_t frequency) const
98{
99 ASSERT(frequency != 0);
100 return frequency <= BASE_FREQUENCY;
101}
102size_t PIT::calculate_nearest_possible_frequency(size_t frequency) const
103{
104 ASSERT(frequency != 0);
105 return frequency;
106}
107
108}