aboutsummaryrefslogtreecommitdiff
path: root/arch
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-04-02 15:07:54 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-04-02 15:07:54 +0200
commitab4c59912c526d515e6e72188c08a7f92e5573e8 (patch)
tree0b6b561efb6fd8ac7a174edc6d4514a98f890861 /arch
parent66ffd2ad8c793c4eea1527848fe4772e42595718 (diff)
downloadteachos-ab4c59912c526d515e6e72188c08a7f92e5573e8.tar.xz
teachos-ab4c59912c526d515e6e72188c08a7f92e5573e8.zip
x86_64: implement legacy PIT driver
Diffstat (limited to 'arch')
-rw-r--r--arch/x86_64/CMakeLists.txt3
-rw-r--r--arch/x86_64/include/arch/devices/legacy_pit.hpp28
-rw-r--r--arch/x86_64/kapi/devices.cpp11
-rw-r--r--arch/x86_64/src/cpu/initialization.cpp2
-rw-r--r--arch/x86_64/src/devices/legacy_pit.cpp54
5 files changed, 97 insertions, 1 deletions
diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt
index 21dceef..83cae0b 100644
--- a/arch/x86_64/CMakeLists.txt
+++ b/arch/x86_64/CMakeLists.txt
@@ -37,6 +37,9 @@ target_sources("x86_64" PRIVATE
# Debug interfaces
"src/debug/qemu_output.cpp"
+ # Devices
+ "src/devices/legacy_pit.cpp"
+
# Memory management
"src/memory/kernel_mapper.cpp"
"src/memory/higher_half_mapper.cpp"
diff --git a/arch/x86_64/include/arch/devices/legacy_pit.hpp b/arch/x86_64/include/arch/devices/legacy_pit.hpp
new file mode 100644
index 0000000..d28e4d6
--- /dev/null
+++ b/arch/x86_64/include/arch/devices/legacy_pit.hpp
@@ -0,0 +1,28 @@
+#ifndef TEACHOS_ARCH_X86_64_DEVICES_LEGACY_PIT_HPP
+#define TEACHOS_ARCH_X86_64_DEVICES_LEGACY_PIT_HPP
+
+#include "kapi/devices/device.hpp"
+#include "kapi/interrupts.hpp"
+
+#include <cstdint>
+
+namespace arch::devices
+{
+
+ struct legacy_pit : kapi::devices::device, kapi::interrupts::handler
+ {
+ explicit legacy_pit(std::uint32_t frequency_in_hz);
+
+ auto init() -> bool override;
+
+ auto handle_interrupt(std::uint32_t irq_number) -> kapi::interrupts::status override;
+
+ private:
+ std::uint32_t m_irq_number{};
+ std::uint32_t m_frequency_in_hz{};
+ std::uint64_t m_ticks{};
+ };
+
+} // namespace arch::devices
+
+#endif \ No newline at end of file
diff --git a/arch/x86_64/kapi/devices.cpp b/arch/x86_64/kapi/devices.cpp
index 25185d6..7aa7090 100644
--- a/arch/x86_64/kapi/devices.cpp
+++ b/arch/x86_64/kapi/devices.cpp
@@ -1,20 +1,31 @@
#include "kapi/devices.hpp"
#include "arch/bus/isa.hpp"
+#include "arch/devices/legacy_pit.hpp"
#include <kstd/memory>
#include <kstd/print>
+#include <cstdint>
#include <utility>
namespace kapi::devices
{
+ namespace
+ {
+ constexpr auto pit_frequency_in_hz = std::uint32_t{100u};
+ }
+
auto init_platform_devices() -> void
{
kstd::println("[x86_64:devices] Initializing ISA bus...");
+
auto isa_bus = kstd::make_unique<arch::bus::isa>();
+ auto pit = kstd::make_unique<arch::devices::legacy_pit>(pit_frequency_in_hz);
+ isa_bus->add_child(std::move(pit));
+
auto & root_bus = get_root_bus();
root_bus.add_child(std::move(isa_bus));
}
diff --git a/arch/x86_64/src/cpu/initialization.cpp b/arch/x86_64/src/cpu/initialization.cpp
index 878fa07..b808c76 100644
--- a/arch/x86_64/src/cpu/initialization.cpp
+++ b/arch/x86_64/src/cpu/initialization.cpp
@@ -139,7 +139,7 @@ namespace arch::cpu
constexpr auto pic_cascade_address = std::uint8_t{0x04};
constexpr auto pic_cascade_slave_identity = std::uint8_t{0x02};
constexpr auto pic_use_8086_mode = std::uint8_t{0x01};
- constexpr auto pic_master_mask = std::uint8_t{0x01};
+ constexpr auto pic_master_mask = std::uint8_t{0x00};
constexpr auto pic_slave_mask = std::uint8_t{0x00};
pic_master_control_port::write(pic_init_command);
diff --git a/arch/x86_64/src/devices/legacy_pit.cpp b/arch/x86_64/src/devices/legacy_pit.cpp
new file mode 100644
index 0000000..f2fb70e
--- /dev/null
+++ b/arch/x86_64/src/devices/legacy_pit.cpp
@@ -0,0 +1,54 @@
+#include "arch/devices/legacy_pit.hpp"
+
+#include "kapi/devices.hpp"
+#include "kapi/devices/device.hpp"
+#include "kapi/interrupts.hpp"
+
+#include "arch/device_io/port_io.hpp"
+
+#include <cstdint>
+
+namespace arch::devices
+{
+
+ namespace
+ {
+ using command_port = io::port<0x43, std::uint8_t, io::port_write>;
+ using channel_0_port = io::port<0x40, std::uint8_t, io::port_write>;
+ using channel_1_port = io::port<0x41, std::uint8_t, io::port_write>;
+ using channel_2_port = io::port<0x42, std::uint8_t, io::port_write>;
+ } // namespace
+
+ legacy_pit::legacy_pit(std::uint32_t frequency_in_hz)
+ : kapi::devices::device{kapi::devices::allocate_major_number(), 0, "legacy_pit"}
+ , m_irq_number{0}
+ , m_frequency_in_hz{frequency_in_hz}
+ {}
+
+ auto legacy_pit::init() -> bool
+ {
+ constexpr auto base_frequency = 1'193'182u;
+ auto divisor = static_cast<std::uint16_t>(base_frequency / m_frequency_in_hz);
+
+ kapi::interrupts::register_handler(m_irq_number, *this);
+
+ command_port::write<std::uint8_t>(0x36); // NOLINT
+ channel_0_port::write<std::uint8_t>(divisor & 0xff); // NOLINT
+ channel_0_port::write<std::uint8_t>(divisor >> 8 & 0xff); // NOLINT
+
+ return true;
+ }
+
+ auto legacy_pit::handle_interrupt(std::uint32_t irq_number) -> kapi::interrupts::status
+ {
+ if (irq_number != m_irq_number)
+ {
+ return kapi::interrupts::status::unhandled;
+ }
+
+ ++m_ticks;
+
+ return kapi::interrupts::status::handled;
+ }
+
+} // namespace arch::devices \ No newline at end of file