summaryrefslogtreecommitdiff
path: root/src/serial.c
blob: 720d45412113139ab1d838666afc62c667d02aa5 (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
#define COM1_BASE 0x3f8
#define COM2_BASE 0x2f8
#define COM3_BASE 0x3e8
#define COM4_BASE 0x2e8

#include "uart_16550.h"

static inline void outb(unsigned char value, unsigned short port)
{
    asm volatile("outb %0,%1" : : "a" (value), "dN" (port));
}

static inline unsigned char inb(unsigned short port)
{
    unsigned char value;

    asm volatile("inb %1,%0" : "=a" (value) : "dN" (port));

    return value;
}

int serial_putchar(unsigned char c)
{
    outb(c, COM1_BASE);

    return 1;
}

int serial_init(void)
{
    /* set the communication parameters */
    unsigned char lcr_value = 0x0;

    lcr_value = DATA_WORD_LEN_8
        | STOP_BIT_1
        | PARITY_NONE
        | BREAK_SIGNAL_DISABLED
        | DLAB_ACCESSIBLE_DLL_DLM;

    outb(lcr_value, COM1_BASE + 3);

    unsigned char dll_value = 0x03;
    unsigned char dlm_value = 0x00;

    outb(dll_value, COM1_BASE + 0);
    outb(dlm_value, COM1_BASE + 1);

    lcr_value = DATA_WORD_LEN_8
        | STOP_BIT_1
        | PARITY_NONE
        | BREAK_SIGNAL_DISABLED
        | DLAB_ACCESSIBLE_RBR_THR_IER;

    outb(lcr_value, COM1_BASE + 3);

    return 0;
}