iPXE
Macros | Functions
uart.c File Reference

16550-compatible UART More...

#include <unistd.h>
#include <errno.h>
#include <ipxe/uart.h>

Go to the source code of this file.

Macros

#define UART_THRE_TIMEOUT_MS   100
 Timeout for transmit holding register to become empty. More...
 
#define UART_TEMT_TIMEOUT_MS   1000
 Timeout for transmitter to become empty. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
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.c.

Macro Definition Documentation

◆ UART_THRE_TIMEOUT_MS

#define UART_THRE_TIMEOUT_MS   100

Timeout for transmit holding register to become empty.

Definition at line 37 of file uart.c.

◆ UART_TEMT_TIMEOUT_MS

#define UART_TEMT_TIMEOUT_MS   1000

Timeout for transmitter to become empty.

Definition at line 40 of file uart.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ 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().