summaryrefslogtreecommitdiff
path: root/Kernel/Prekernel/Arch/aarch64/UART.h
blob: 5255de39af39b9a6bac0138d9ab6668e204c1daa (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
/*
 * Copyright (c) 2021, Nico Weber <thakis@chromium.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Types.h>

namespace Prekernel {

struct UARTRegisters;

// Abstracts the PL011 UART on a Raspberry Pi.
// (The BCM2711 on a Raspberry Pi 4 has five PL011 UARTs; this is always the first of those.)
class UART {
public:
    static UART& the();

    void send(u32 c);
    u32 receive();

    void print_str(const char* s)
    {
        while (*s)
            send(*s++);
    }
    void print_num(u64 n)
    {
        char buf[21];
        int i = 0;
        do {
            buf[i++] = (n % 10) + '0';
            n /= 10;
        } while (n);
        for (i--; i >= 0; i--)
            send(buf[i]);
    }

private:
    UART();

    void set_baud_rate(int baud_rate, int uart_frequency_in_hz);
    void wait_until_we_can_send();
    void wait_until_we_can_receive();

    UARTRegisters volatile* m_registers;
};

}