summaryrefslogtreecommitdiff
path: root/kernel/drivers/char/pl011.cpp
blob: 7615370209c714fe2acc75b61aaf397c50751287 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// ============================================================================
// drivers/char/pl011.cpp — PL011 UART console driver + device (aarch64)
//
// Stub implementation that compiles but does no real I/O yet.
// TODO: Program IBRD, FBRD, LCR_H, CR registers for actual UART output.
// ============================================================================

#include <kernel/console_driver.h>
#include <kernel/early_driver.h>
#include <kernel/early_device.h>
#include <kernel/platform_bus.h>

// ── PL011Device ───────────────────────────────────────────────────────────

class PL011Device : public Device, public EarlyDevice<PL011Device> {
public:
    explicit PL011Device(uintptr_t base_addr) : base_(base_addr) {}

    const char* name() const override { return "uart0"; }
    uintptr_t base() const { return base_; }

private:
    uintptr_t base_;
};

// ── PL011Console driver ──────────────────────────────────────────────────

class PL011Console : public ConsoleDriver,
                     public EarlyDriver<PL011Console> {
public:
    PL011Console() = default;

    const char* name() const override { return "pl011-uart"; }

    bool init() override {
        auto* dev = static_cast<PL011Device*>(device());
        base_ = dev->base();

        // TODO: Configure UART registers at base_:
        //   UARTIBRD (offset 0x24) — integer baud rate divisor
        //   UARTFBRD (offset 0x28) — fractional baud rate divisor
        //   UARTLCR_H (offset 0x2C) — line control (8N1, FIFO enable)
        //   UARTCR (offset 0x30) — control register (TX enable, UART enable)
        initialized_ = true;
        return true;
    }

    void shutdown() override {
        initialized_ = false;
    }

    void putchar(char c) override {
        if (!initialized_) return;
        // TODO: while (*(volatile uint32_t*)(base_ + 0x18) & (1 << 5)) {}
        //       *(volatile uint32_t*)(base_ + 0x00) = c;
        (void)c;
    }

    void clear() override {
        // UART has no screen to clear
    }

private:
    uintptr_t base_ = 0;
    bool initialized_ = false;
};

// ── Driver init function ──────────────────────────────────────────────────

namespace console {

void init_pl011(uintptr_t base_addr) {
    auto* dev = PL011Device::create(base_addr);
    platform::bus().add_device(dev);

    auto* drv = PL011Console::create();
    drv->bind(dev);
    drv->init();

    console::register_driver(drv);
}

} // namespace console