iPXE
uart.h
Go to the documentation of this file.
1 #ifndef _IPXE_UART_H
2 #define _IPXE_UART_H
3 
4 /** @file
5  *
6  * 16550-compatible UART
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stdint.h>
13 
14 /** Transmitter holding register */
15 #define UART_THR 0x00
16 
17 /** Receiver buffer register */
18 #define UART_RBR 0x00
19 
20 /** Interrupt enable register */
21 #define UART_IER 0x01
22 
23 /** FIFO control register */
24 #define UART_FCR 0x02
25 #define UART_FCR_FE 0x01 /**< FIFO enable */
26 
27 /** Line control register */
28 #define UART_LCR 0x03
29 #define UART_LCR_WLS0 0x01 /**< Word length select bit 0 */
30 #define UART_LCR_WLS1 0x02 /**< Word length select bit 1 */
31 #define UART_LCR_STB 0x04 /**< Number of stop bits */
32 #define UART_LCR_PEN 0x08 /**< Parity enable */
33 #define UART_LCR_EPS 0x10 /**< Even parity select */
34 #define UART_LCR_DLAB 0x80 /**< Divisor latch access bit */
35 
36 #define UART_LCR_WORD_LEN(x) ( ( (x) - 5 ) << 0 ) /**< Word length */
37 #define UART_LCR_STOP_BITS(x) ( ( (x) - 1 ) << 2 ) /**< Stop bits */
38 #define UART_LCR_PARITY(x) ( ( (x) - 0 ) << 3 ) /**< Parity */
39 
40 /**
41  * Calculate line control register value
42  *
43  * @v word_len Word length (5-8)
44  * @v parity Parity (0=none, 1=odd, 3=even)
45  * @v stop_bits Stop bits (1-2)
46  * @ret lcr Line control register value
47  */
48 #define UART_LCR_WPS( word_len, parity, stop_bits ) \
49  ( UART_LCR_WORD_LEN ( (word_len) ) | \
50  UART_LCR_PARITY ( (parity) ) | \
51  UART_LCR_STOP_BITS ( (stop_bits) ) )
52 
53 /** Default LCR value: 8 data bits, no parity, one stop bit */
54 #define UART_LCR_8N1 UART_LCR_WPS ( 8, 0, 1 )
55 
56 /** Modem control register */
57 #define UART_MCR 0x04
58 #define UART_MCR_DTR 0x01 /**< Data terminal ready */
59 #define UART_MCR_RTS 0x02 /**< Request to send */
60 
61 /** Line status register */
62 #define UART_LSR 0x05
63 #define UART_LSR_DR 0x01 /**< Data ready */
64 #define UART_LSR_THRE 0x20 /**< Transmitter holding register empty */
65 #define UART_LSR_TEMT 0x40 /**< Transmitter empty */
66 
67 /** Scratch register */
68 #define UART_SCR 0x07
69 
70 /** Divisor latch (least significant byte) */
71 #define UART_DLL 0x00
72 
73 /** Divisor latch (most significant byte) */
74 #define UART_DLM 0x01
75 
76 /** Maximum baud rate */
77 #define UART_MAX_BAUD 115200
78 
79 /** A 16550-compatible UART */
80 struct uart {
81  /** I/O port base address */
82  void *base;
83  /** Baud rate divisor */
85  /** Line control register */
87 };
88 
89 /** Symbolic names for port indexes */
90 enum uart_port {
91  COM1 = 1,
92  COM2 = 2,
93  COM3 = 3,
94  COM4 = 4,
95 };
96 
97 #include <bits/uart.h>
98 
99 void uart_write ( struct uart *uart, unsigned int addr, uint8_t data );
100 uint8_t uart_read ( struct uart *uart, unsigned int addr );
101 int uart_select ( struct uart *uart, unsigned int port );
102 
103 /**
104  * Check if received data is ready
105  *
106  * @v uart UART
107  * @ret ready Data is ready
108  */
109 static inline int uart_data_ready ( struct uart *uart ) {
110  uint8_t lsr;
111 
112  lsr = uart_read ( uart, UART_LSR );
113  return ( lsr & UART_LSR_DR );
114 }
115 
116 /**
117  * Receive data
118  *
119  * @v uart UART
120  * @ret data Data
121  */
122 static inline uint8_t uart_receive ( struct uart *uart ) {
123 
124  return uart_read ( uart, UART_RBR );
125 }
126 
127 extern void uart_transmit ( struct uart *uart, uint8_t data );
128 extern void uart_flush ( struct uart *uart );
129 extern int uart_exists ( struct uart *uart );
130 extern int uart_init ( struct uart *uart, unsigned int baud, uint8_t lcr );
131 
132 #endif /* _IPXE_UART_H */
static unsigned int addr
Definition: uart.h:23
unsigned short uint16_t
Definition: stdint.h:11
uint16_t divisor
Baud rate divisor.
Definition: uart.h:84
Definition: uart.h:92
A 16550-compatible UART.
Definition: uart.h:80
static int uart_data_ready(struct uart *uart)
Check if received data is ready.
Definition: uart.h:109
#define UART_LSR
Line status register.
Definition: uart.h:62
uart_port
Symbolic names for port indexes.
Definition: uart.h:90
uint8_t lcr
Line control register.
Definition: uart.h:86
void * base
I/O port base address.
Definition: uart.h:82
void uart_flush(struct uart *uart)
Flush data.
Definition: uart.c:69
static uint8_t uart_receive(struct uart *uart)
Receive data.
Definition: uart.h:122
int uart_init(struct uart *uart, unsigned int baud, uint8_t lcr)
Initialise UART.
Definition: uart.c:113
static unsigned int uint8_t data
Definition: uart.h:23
Definition: uart.h:91
int uart_select(struct uart *uart, unsigned int port)
Select UART port.
Definition: x86_uart.c:50
u8 port
Port number.
Definition: CIB_PRM.h:31
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define UART_RBR
Receiver buffer register.
Definition: uart.h:18
void uart_write(struct uart *uart, unsigned int addr, uint8_t data)
uint8_t uart_read(struct uart *uart, unsigned int addr)
Definition: uart.h:94
unsigned char uint8_t
Definition: stdint.h:10
Definition: uart.h:93
void uart_transmit(struct uart *uart, uint8_t data)
Transmit data.
Definition: uart.c:48
16550-compatible UART
int uart_exists(struct uart *uart)
Check for existence of UART.
Definition: uart.c:88
#define UART_LSR_DR
Data ready.
Definition: uart.h:63