aboutsummaryrefslogtreecommitdiff
path: root/devices/uart_16550.cpp
blob: 96209813644fbcf82190b4821bc720f4624ecc97 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "uart.hpp"
#include <sys/io.hpp>

template <UART port> struct Uart16550 : public kIoDevice, private Port<unsigned short, port> {
  using Base = Port<unsigned short, port>;
  using Ports = UART;
  using PortOffset = UARTPortOffset;

  // Line Control
  // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
  // |dla|   | parity    | s | data  |
  enum LineControl : unsigned char {
    d5bit = 0x00, // 0000 0000  data bits
    d6bit = 0x01, // 0000 0001
    d7bit = 0x02, // 0000 0010
    d8bit = 0x03, // 0000 0011
    none = 0x00,  // 0000 0000  parity bits
    odd = 0x08,   // 0000 1000
    even = 0x18,  // 0001 1000
    mark = 0x28,  // 0010 1000
    space = 0x38, // 0011 1000
    s1bit = 0x00, // 0000 0000  stop bits
    s2bit = 0x04, // 0000 0100  1.5 for 5bit data; 2 otherwise
    dlab = 0x80   // 1000 0000  divisor latch access bit
  };

  // Line Status Register
  enum LineStatus : unsigned char {
    DR = (1 << 0),   // data ready: see if there is data to read
    OE = (1 << 1),   // overrun error: see if there has been data lost
    PE = (1 << 2),   // parity error: see if there was error in transmission
    FE = (1 << 3),   // framing error: see if a stop bit was missing
    BI = (1 << 4),   // break indicator: see if there is a break in data input
    THRE = (1 << 5), // transmitter holding register empty: see if transmission buffer is empty
    TEMT = (1 << 6), // transmitter empty: see if transmitter is not doing anything
    ERRO = (1 << 7), // impending error: see if there is an error with a word in the input buffer
  };

  [[nodiscard]] static bool
  test()
  {
    Base::out(0x00, 1); // Disable all interrupts
    Base::out(0x80, 3); // Enable DLAB (set baud rate divisor)
    Base::out(0x03, 0); // Set divisor to 3 (lo byte) 38400 baud
    Base::out(0x00, 1); //                  (hi byte)
    Base::out(0x03, 3); // 8 bits, no parity, one stop bit
    Base::out(0xc7, 2); // Enable FIFO, clear them, with 14-byte threshold
    Base::out(0x0b, 4); // IRQs enabled, RTS/DSR set
    Base::out(0x1e, 4); // Set in loopback mode, test the serial chip
    Base::out(0xae, 0); // Test serial chip (send byte 0xAE and check if serial
                        // returns same byte)

    // Check if serial is faulty (i.e: not same byte as sent)
    if (Base::in() != 0xae) { return false; }

    // If serial is not faulty set it in normal operation mode
    // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
    Base::out(0x0f, 4);
    return true;
  }

  [[nodiscard]] static bool
  thre()
  {
    return Base::in(PortOffset::LineStatus)&THRE;
  }

  void
  putc(char a) override
  {
    while (!thre()) {}
    Base::out(a);
    if (a == '\n') putc('\r');
  }

  int
  puts(const char *string, int length) override
  {
    int written = 0;

    if (length == -1)
      while (*string != '\0') {
        putc(*string);
        ++string;
        ++written;
      }

    else {
      for (int i = 0; i < length; ++i) putc(string[i]);
      written += length;
    }

    return written;
  }

  void
  flush() override
  {
  }
};

static_assert(sizeof(Uart16550<COM1>) == sizeof(void *));

template <>
FILE *
uart_init<COM1>()
{
  static Uart16550<COM1> device;
  return &device;
}