iPXE
Data Structures | Macros | Enumerations | Functions
uart.h File Reference

16550-compatible UART More...

#include <stdint.h>
#include <bits/uart.h>

Go to the source code of this file.

Data Structures

struct  uart
 A 16550-compatible UART. More...
 

Macros

#define UART_THR   0x00
 Transmitter holding register. More...
 
#define UART_RBR   0x00
 Receiver buffer register. More...
 
#define UART_IER   0x01
 Interrupt enable register. More...
 
#define UART_FCR   0x02
 FIFO control register. More...
 
#define UART_FCR_FE   0x01
 FIFO enable. More...
 
#define UART_LCR   0x03
 Line control register. More...
 
#define UART_LCR_WLS0   0x01
 Word length select bit 0. More...
 
#define UART_LCR_WLS1   0x02
 Word length select bit 1. More...
 
#define UART_LCR_STB   0x04
 Number of stop bits. More...
 
#define UART_LCR_PEN   0x08
 Parity enable. More...
 
#define UART_LCR_EPS   0x10
 Even parity select. More...
 
#define UART_LCR_DLAB   0x80
 Divisor latch access bit. More...
 
#define UART_LCR_WORD_LEN(x)   ( ( (x) - 5 ) << 0 )
 Word length. More...
 
#define UART_LCR_STOP_BITS(x)   ( ( (x) - 1 ) << 2 )
 Stop bits. More...
 
#define UART_LCR_PARITY(x)   ( ( (x) - 0 ) << 3 )
 Parity. More...
 
#define UART_LCR_WPS(word_len, parity, stop_bits)
 Calculate line control register value. More...
 
#define UART_LCR_8N1   UART_LCR_WPS ( 8, 0, 1 )
 Default LCR value: 8 data bits, no parity, one stop bit. More...
 
#define UART_MCR   0x04
 Modem control register. More...
 
#define UART_MCR_DTR   0x01
 Data terminal ready. More...
 
#define UART_MCR_RTS   0x02
 Request to send. More...
 
#define UART_LSR   0x05
 Line status register. More...
 
#define UART_LSR_DR   0x01
 Data ready. More...
 
#define UART_LSR_THRE   0x20
 Transmitter holding register empty. More...
 
#define UART_LSR_TEMT   0x40
 Transmitter empty. More...
 
#define UART_SCR   0x07
 Scratch register. More...
 
#define UART_DLL   0x00
 Divisor latch (least significant byte) More...
 
#define UART_DLM   0x01
 Divisor latch (most significant byte) More...
 
#define UART_MAX_BAUD   115200
 Maximum baud rate. More...
 

Enumerations

enum  uart_port { COM1 = 1, COM2 = 2, COM3 = 3, COM4 = 4 }
 Symbolic names for port indexes. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
void uart_write (struct uart *uart, unsigned int addr, uint8_t data)
 
uint8_t uart_read (struct uart *uart, unsigned int addr)
 
int uart_select (struct uart *uart, unsigned int port)
 Select UART port. More...
 
static int uart_data_ready (struct uart *uart)
 Check if received data is ready. More...
 
static uint8_t uart_receive (struct uart *uart)
 Receive data. More...
 
void uart_transmit (struct uart *uart, uint8_t data)
 Transmit data. More...
 
void uart_flush (struct uart *uart)
 Flush data. More...
 
int uart_exists (struct uart *uart)
 Check for existence of UART. More...
 
int uart_init (struct uart *uart, unsigned int baud, uint8_t lcr)
 Initialise UART. More...
 

Detailed Description

16550-compatible UART

Definition in file uart.h.

Macro Definition Documentation

◆ UART_THR

#define UART_THR   0x00

Transmitter holding register.

Definition at line 15 of file uart.h.

◆ UART_RBR

#define UART_RBR   0x00

Receiver buffer register.

Definition at line 18 of file uart.h.

◆ UART_IER

#define UART_IER   0x01

Interrupt enable register.

Definition at line 21 of file uart.h.

◆ UART_FCR

#define UART_FCR   0x02

FIFO control register.

Definition at line 24 of file uart.h.

◆ UART_FCR_FE

#define UART_FCR_FE   0x01

FIFO enable.

Definition at line 25 of file uart.h.

◆ UART_LCR

#define UART_LCR   0x03

Line control register.

Definition at line 28 of file uart.h.

◆ UART_LCR_WLS0

#define UART_LCR_WLS0   0x01

Word length select bit 0.

Definition at line 29 of file uart.h.

◆ UART_LCR_WLS1

#define UART_LCR_WLS1   0x02

Word length select bit 1.

Definition at line 30 of file uart.h.

◆ UART_LCR_STB

#define UART_LCR_STB   0x04

Number of stop bits.

Definition at line 31 of file uart.h.

◆ UART_LCR_PEN

#define UART_LCR_PEN   0x08

Parity enable.

Definition at line 32 of file uart.h.

◆ UART_LCR_EPS

#define UART_LCR_EPS   0x10

Even parity select.

Definition at line 33 of file uart.h.

◆ UART_LCR_DLAB

#define UART_LCR_DLAB   0x80

Divisor latch access bit.

Definition at line 34 of file uart.h.

◆ UART_LCR_WORD_LEN

#define UART_LCR_WORD_LEN (   x)    ( ( (x) - 5 ) << 0 )

Word length.

Definition at line 36 of file uart.h.

◆ UART_LCR_STOP_BITS

#define UART_LCR_STOP_BITS (   x)    ( ( (x) - 1 ) << 2 )

Stop bits.

Definition at line 37 of file uart.h.

◆ UART_LCR_PARITY

#define UART_LCR_PARITY (   x)    ( ( (x) - 0 ) << 3 )

Parity.

Definition at line 38 of file uart.h.

◆ UART_LCR_WPS

#define UART_LCR_WPS (   word_len,
  parity,
  stop_bits 
)
Value:
( UART_LCR_WORD_LEN ( (word_len) ) | \
UART_LCR_PARITY ( (parity) ) | \
UART_LCR_STOP_BITS ( (stop_bits) ) )
#define UART_LCR_WORD_LEN(x)
Word length.
Definition: uart.h:36

Calculate line control register value.

Parameters
word_lenWord length (5-8)
parityParity (0=none, 1=odd, 3=even)
stop_bitsStop bits (1-2)
Return values
lcrLine control register value

Definition at line 48 of file uart.h.

◆ UART_LCR_8N1

#define UART_LCR_8N1   UART_LCR_WPS ( 8, 0, 1 )

Default LCR value: 8 data bits, no parity, one stop bit.

Definition at line 54 of file uart.h.

◆ UART_MCR

#define UART_MCR   0x04

Modem control register.

Definition at line 57 of file uart.h.

◆ UART_MCR_DTR

#define UART_MCR_DTR   0x01

Data terminal ready.

Definition at line 58 of file uart.h.

◆ UART_MCR_RTS

#define UART_MCR_RTS   0x02

Request to send.

Definition at line 59 of file uart.h.

◆ UART_LSR

#define UART_LSR   0x05

Line status register.

Definition at line 62 of file uart.h.

◆ UART_LSR_DR

#define UART_LSR_DR   0x01

Data ready.

Definition at line 63 of file uart.h.

◆ UART_LSR_THRE

#define UART_LSR_THRE   0x20

Transmitter holding register empty.

Definition at line 64 of file uart.h.

◆ UART_LSR_TEMT

#define UART_LSR_TEMT   0x40

Transmitter empty.

Definition at line 65 of file uart.h.

◆ UART_SCR

#define UART_SCR   0x07

Scratch register.

Definition at line 68 of file uart.h.

◆ UART_DLL

#define UART_DLL   0x00

Divisor latch (least significant byte)

Definition at line 71 of file uart.h.

◆ UART_DLM

#define UART_DLM   0x01

Divisor latch (most significant byte)

Definition at line 74 of file uart.h.

◆ UART_MAX_BAUD

#define UART_MAX_BAUD   115200

Maximum baud rate.

Definition at line 77 of file uart.h.

Enumeration Type Documentation

◆ uart_port

enum uart_port

Symbolic names for port indexes.

Enumerator
COM1 
COM2 
COM3 
COM4 

Definition at line 90 of file uart.h.

90  {
91  COM1 = 1,
92  COM2 = 2,
93  COM3 = 3,
94  COM4 = 4,
95 };
Definition: uart.h:92
Definition: uart.h:91
Definition: uart.h:94
Definition: uart.h:93

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ uart_write()

void uart_write ( struct uart uart,
unsigned int  addr,
uint8_t  data 
)

◆ uart_read()

uint8_t uart_read ( struct uart uart,
unsigned int  addr 
)

◆ uart_select()

int uart_select ( struct uart uart,
unsigned int  port 
)

Select UART port.

Parameters
uartUART
portPort number, or 0 to disable
Return values
rcReturn status code

Definition at line 50 of file x86_uart.c.

50  {
51  int rc;
52 
53  /* Set new UART base */
54  if ( port >= ( sizeof ( uart_base ) / sizeof ( uart_base[0] ) ) ) {
55  rc = -ENODEV;
56  goto err;
57  }
58  uart->base = ( ( void * ) ( intptr_t ) uart_base[port] );
59 
60  /* Check that UART exists */
61  if ( ( rc = uart_exists ( uart ) ) != 0 )
62  goto err;
63 
64  return 0;
65 
66  err:
67  uart->base = NULL;
68  return rc;
69 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int uart_exists(struct uart *uart)
Check for existence of UART.
Definition: uart.c:88
A 16550-compatible UART.
Definition: uart.h:80
void * base
I/O port base address.
Definition: uart.h:82
unsigned long intptr_t
Definition: stdint.h:21
u8 port
Port number.
Definition: CIB_PRM.h:31
#define ENODEV
No such device.
Definition: errno.h:509
static uint16_t uart_base[]
UART port bases.
Definition: x86_uart.c:36
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

◆ uart_data_ready()

static int uart_data_ready ( struct uart uart)
inlinestatic

Check if received data is ready.

Parameters
uartUART
Return values
readyData is ready

Definition at line 109 of file uart.h.

109  {
110  uint8_t lsr;
111 
112  lsr = uart_read ( uart, UART_LSR );
113  return ( lsr & UART_LSR_DR );
114 }
A 16550-compatible UART.
Definition: uart.h:80
#define UART_LSR
Line status register.
Definition: uart.h:62
uint8_t uart_read(struct uart *uart, unsigned int addr)
unsigned char uint8_t
Definition: stdint.h:10
#define UART_LSR_DR
Data ready.
Definition: uart.h:63

References UART_LSR, UART_LSR_DR, and uart_read().

Referenced by gdbserial_recv(), serial_getchar(), and serial_iskey().

◆ uart_receive()

static uint8_t uart_receive ( struct uart uart)
inlinestatic

Receive data.

Parameters
uartUART
Return values
dataData

Definition at line 122 of file uart.h.

122  {
123 
124  return uart_read ( uart, UART_RBR );
125 }
A 16550-compatible UART.
Definition: uart.h:80
#define UART_RBR
Receiver buffer register.
Definition: uart.h:18
uint8_t uart_read(struct uart *uart, unsigned int addr)

References UART_RBR, and uart_read().

Referenced by gdbserial_recv(), and serial_getchar().

◆ uart_transmit()

void uart_transmit ( struct uart uart,
uint8_t  data 
)

Transmit data.

Parameters
uartUART
dataData

Definition at line 48 of file uart.c.

48  {
49  unsigned int i;
50  uint8_t lsr;
51 
52  /* Wait for transmitter holding register to become empty */
53  for ( i = 0 ; i < UART_THRE_TIMEOUT_MS ; i++ ) {
54  lsr = uart_read ( uart, UART_LSR );
55  if ( lsr & UART_LSR_THRE )
56  break;
57  mdelay ( 1 );
58  }
59 
60  /* Transmit data (even if we timed out) */
62 }
A 16550-compatible UART.
Definition: uart.h:80
#define UART_LSR
Line status register.
Definition: uart.h:62
#define UART_LSR_THRE
Transmitter holding register empty.
Definition: uart.h:64
#define UART_THR
Transmitter holding register.
Definition: uart.h:15
void uart_write(struct uart *uart, unsigned int addr, uint8_t data)
uint8_t uart_read(struct uart *uart, unsigned int addr)
unsigned char uint8_t
Definition: stdint.h:10
#define UART_THRE_TIMEOUT_MS
Timeout for transmit holding register to become empty.
Definition: uart.c:37
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition: timer.c:78
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

References data, mdelay(), UART_LSR, UART_LSR_THRE, uart_read(), UART_THR, UART_THRE_TIMEOUT_MS, and uart_write().

Referenced by gdbserial_send(), int21(), and serial_putchar().

◆ uart_flush()

void uart_flush ( struct uart uart)

Flush data.

Parameters
uartUART

Definition at line 69 of file uart.c.

69  {
70  unsigned int i;
71  uint8_t lsr;
72 
73  /* Wait for transmitter and receiver to become empty */
74  for ( i = 0 ; i < UART_TEMT_TIMEOUT_MS ; i++ ) {
75  uart_read ( uart, UART_RBR );
76  lsr = uart_read ( uart, UART_LSR );
77  if ( ( lsr & UART_LSR_TEMT ) && ! ( lsr & UART_LSR_DR ) )
78  break;
79  }
80 }
A 16550-compatible UART.
Definition: uart.h:80
#define UART_LSR
Line status register.
Definition: uart.h:62
#define UART_RBR
Receiver buffer register.
Definition: uart.h:18
uint8_t uart_read(struct uart *uart, unsigned int addr)
unsigned char uint8_t
Definition: stdint.h:10
#define UART_LSR_TEMT
Transmitter empty.
Definition: uart.h:65
#define UART_TEMT_TIMEOUT_MS
Timeout for transmitter to become empty.
Definition: uart.c:40
#define UART_LSR_DR
Data ready.
Definition: uart.h:63

References UART_LSR, UART_LSR_DR, UART_LSR_TEMT, UART_RBR, uart_read(), and UART_TEMT_TIMEOUT_MS.

Referenced by serial_shutdown(), and uart_init().

◆ uart_exists()

int uart_exists ( struct uart uart)

Check for existence of UART.

Parameters
uartUART
Return values
rcReturn status code

Definition at line 88 of file uart.c.

88  {
89 
90  /* Fail if no UART port is defined */
91  if ( ! uart->base )
92  return -ENODEV;
93 
94  /* Fail if UART scratch register seems not to be present */
95  uart_write ( uart, UART_SCR, 0x18 );
96  if ( uart_read ( uart, UART_SCR ) != 0x18 )
97  return -ENODEV;
98  uart_write ( uart, UART_SCR, 0xae );
99  if ( uart_read ( uart, UART_SCR ) != 0xae )
100  return -ENODEV;
101 
102  return 0;
103 }
A 16550-compatible UART.
Definition: uart.h:80
void * base
I/O port base address.
Definition: uart.h:82
void uart_write(struct uart *uart, unsigned int addr, uint8_t data)
uint8_t uart_read(struct uart *uart, unsigned int addr)
#define ENODEV
No such device.
Definition: errno.h:509
#define UART_SCR
Scratch register.
Definition: uart.h:68

References uart::base, ENODEV, uart_read(), UART_SCR, and uart_write().

Referenced by uart_init(), and uart_select().

◆ uart_init()

int uart_init ( struct uart uart,
unsigned int  baud,
uint8_t  lcr 
)

Initialise UART.

Parameters
uartUART
baudBaud rate, or zero to leave unchanged
lcrLine control register value, or zero to leave unchanged
Return values
rcReturn status code

Definition at line 113 of file uart.c.

113  {
114  uint8_t dlm;
115  uint8_t dll;
116  int rc;
117 
118  /* Check for existence of UART */
119  if ( ( rc = uart_exists ( uart ) ) != 0 )
120  return rc;
121 
122  /* Configure divisor and line control register, if applicable */
123  if ( ! lcr )
124  lcr = uart_read ( uart, UART_LCR );
125  uart->lcr = lcr;
126  uart_write ( uart, UART_LCR, ( lcr | UART_LCR_DLAB ) );
127  if ( baud ) {
128  uart->divisor = ( UART_MAX_BAUD / baud );
129  dlm = ( ( uart->divisor >> 8 ) & 0xff );
130  dll = ( ( uart->divisor >> 0 ) & 0xff );
131  uart_write ( uart, UART_DLM, dlm );
132  uart_write ( uart, UART_DLL, dll );
133  } else {
134  dlm = uart_read ( uart, UART_DLM );
135  dll = uart_read ( uart, UART_DLL );
136  uart->divisor = ( ( dlm << 8 ) | dll );
137  }
138  uart_write ( uart, UART_LCR, ( lcr & ~UART_LCR_DLAB ) );
139 
140  /* Disable interrupts */
141  uart_write ( uart, UART_IER, 0 );
142 
143  /* Enable FIFOs */
145 
146  /* Assert DTR and RTS */
148 
149  /* Flush any stale data */
150  uart_flush ( uart );
151 
152  return 0;
153 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void uart_flush(struct uart *uart)
Flush data.
Definition: uart.c:69
int uart_exists(struct uart *uart)
Check for existence of UART.
Definition: uart.c:88
uint16_t divisor
Baud rate divisor.
Definition: uart.h:84
A 16550-compatible UART.
Definition: uart.h:80
uint8_t lcr
Line control register.
Definition: uart.h:86
#define UART_DLL
Divisor latch (least significant byte)
Definition: uart.h:71
#define UART_MAX_BAUD
Maximum baud rate.
Definition: uart.h:77
#define UART_DLM
Divisor latch (most significant byte)
Definition: uart.h:74
void uart_write(struct uart *uart, unsigned int addr, uint8_t data)
uint8_t uart_read(struct uart *uart, unsigned int addr)
unsigned char uint8_t
Definition: stdint.h:10
#define UART_FCR
FIFO control register.
Definition: uart.h:24
#define UART_MCR
Modem control register.
Definition: uart.h:57
#define UART_IER
Interrupt enable register.
Definition: uart.h:21
#define UART_MCR_RTS
Request to send.
Definition: uart.h:59
#define UART_LCR
Line control register.
Definition: uart.h:28
#define UART_FCR_FE
FIFO enable.
Definition: uart.h:25
#define UART_MCR_DTR
Data terminal ready.
Definition: uart.h:58
#define UART_LCR_DLAB
Divisor latch access bit.
Definition: uart.h:34

References uart::divisor, uart::lcr, rc, UART_DLL, UART_DLM, uart_exists(), UART_FCR, UART_FCR_FE, uart_flush(), UART_IER, UART_LCR, UART_LCR_DLAB, UART_MAX_BAUD, UART_MCR, UART_MCR_DTR, UART_MCR_RTS, uart_read(), and uart_write().

Referenced by gdbserial_configure(), and serial_init().