1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include <mos/types.h>
6
7typedef enum
8{
9 OFFSET_INTERRUPT_ENABLE = 1, // Interrupt Enable Register
10 OFFSET_INTERRUPT_ID_FIFO = 2, // Interrupt ID Register and FIFO Control Register
11 OFFSET_LINE_CONTROL = 3, // Line Control Register
12 OFFSET_MODEM_CONTROL = 4, // Modem Control Register
13 OFFSET_LINE_STATUS = 5, // Line Status Register
14 OFFSET_MODEM_STATUS = 6, // Modem Status Register
15 OFFSET_SCRATCH = 7, // Scratch Register
16
17 OFFSET_DLAB_DIVISOR_LSB = 0, // With DLAB set to 1, this is the least significant byte of the divisor value for setting the baud rate.
18 OFFSET_DLAB_DIVISOR_MSB = 1, // With DLAB set to 1, this is the most significant byte of the divisor value.
19} serial_register_t;
20
21typedef enum
22{
23 CHAR_LENGTH_5 = 0x0,
24 CHAR_LENGTH_6 = 0x1,
25 CHAR_LENGTH_7 = 0x2,
26 CHAR_LENGTH_8 = 0x3,
27} serial_charlength_t;
28
29typedef enum
30{
31 STOP_BITS_1, // 1 stop bit
32 STOP_BITS_15_OR_2, // 1.5 or 2 stop bits
33} serial_stopbits_t;
34
35typedef enum
36{
37 PARITY_NONE = 0, // No parity
38 PARITY_ODD = 1, // Odd parity
39 PARITY_EVEN = 2, // Even parity
40 PARITY_MARK = 3, // Parity bit always 1
41 PARITY_SPACE = 4, // Parity bit always 0
42} serial_parity_t;
43
44typedef enum
45{
46 INTERRUPT_DATA_AVAILABLE = 1 << 0, // Data ready interrupt
47 INTERRUPT_TRANSMITTER_EMPTY = 1 << 1, // Transmitter empty interrupt
48 INTERRUPT_BREAK_ERROR = 1 << 2, // Break error interrupt
49 INTERRUPT_STATUS_CHANGE = 1 << 3, // Status change interrupt
50 INTERRUPT_NONE = 0, // No interrupts
51 INTERRUPT_ALL = INTERRUPT_DATA_AVAILABLE | INTERRUPT_TRANSMITTER_EMPTY | INTERRUPT_BREAK_ERROR | INTERRUPT_STATUS_CHANGE,
52} serial_interrupt_t;
53
54typedef enum
55{
56 BAUD_RATE_115200 = 1,
57 BAUD_RATE_57600 = 2,
58 BAUD_RATE_38400 = 3,
59 BAUD_RATE_19200 = 4,
60 BAUD_RATE_9600 = 5,
61 BAUD_RATE_4800 = 6,
62 BAUD_RATE_2400 = 7,
63 BAUD_RATE_1200 = 8,
64 BAUD_RATE_600 = 9,
65 BAUD_RATE_300 = 10,
66 BAUD_RATE_110 = 11,
67} serial_baudrate_t;
68
69typedef struct _serial_device serial_device_t;
70
71typedef struct _serial_driver
72{
73 u8 (*read_data)(serial_device_t *dev);
74 void (*write_data)(serial_device_t *dev, u8 data);
75
76 u8 (*read_register)(serial_device_t *dev, serial_register_t offset);
77 void (*write_register)(serial_device_t *dev, serial_register_t offset, u8 value);
78} serial_driver_t;
79
80typedef struct _serial_device
81{
82 const serial_driver_t *driver;
83 void *driver_data;
84
85 serial_baudrate_t baudrate_divisor;
86 serial_charlength_t char_length;
87 serial_stopbits_t stop_bits;
88 serial_parity_t parity;
89} serial_device_t;
90
91bool serial_device_setup(serial_device_t *device);
92
93int serial_device_read(serial_device_t *device, char *data, size_t length);
94
95int serial_device_write(serial_device_t *device, const char *data, size_t length);
96
97bool serial_dev_get_data_ready(serial_device_t *device);
98