21#define FRACTION_PART_SIZE_IN_BITS 6
22#define FRACTION_PART_MASK ((1 << FRACTION_PART_SIZE_IN_BITS) - 1)
28STATIC CONST UINT32 mInvalidControlBits = EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE;
68 IN UINT32 UartClkInHz,
69 IN OUT UINT64 *BaudRate,
70 IN OUT UINT32 *ReceiveFifoDepth,
72 IN OUT UINT8 *DataBits,
80 UINT32 HardwareFifoDepth;
83 HardwareFifoDepth =
FixedPcdGet16 (PcdUartDefaultReceiveFifoDepth);
84 if (HardwareFifoDepth == 0) {
86 HardwareFifoDepth = (PL011_UARTPID2_VER (UartPid2) > PL011_VER_R1P4) ? 32 : 16;
92 if ((*ReceiveFifoDepth == 0) || (*ReceiveFifoDepth >= HardwareFifoDepth)) {
94 LineControl = PL011_UARTLCR_H_FEN;
95 *ReceiveFifoDepth = HardwareFifoDepth;
100 *ReceiveFifoDepth = 1;
113 LineControl |= (PL011_UARTLCR_H_PEN | PL011_UARTLCR_H_EPS);
116 LineControl |= PL011_UARTLCR_H_PEN;
119 LineControl |= (PL011_UARTLCR_H_PEN \
120 | PL011_UARTLCR_H_SPS \
121 | PL011_UARTLCR_H_EPS);
124 LineControl |= (PL011_UARTLCR_H_PEN | PL011_UARTLCR_H_SPS);
137 LineControl |= PL011_UARTLCR_H_WLEN_8;
140 LineControl |= PL011_UARTLCR_H_WLEN_7;
143 LineControl |= PL011_UARTLCR_H_WLEN_6;
146 LineControl |= PL011_UARTLCR_H_WLEN_5;
156 case DefaultStopBits:
157 *StopBits = OneStopBit;
162 LineControl |= PL011_UARTLCR_H_STP2;
164 case OneFiveStopBits:
185 if (*BaudRate == 0) {
187 if (*BaudRate == 0) {
192 if (0 == UartClkInHz) {
196 Divisor = (UartClkInHz * 4) / *BaudRate;
197 Integer = Divisor >> FRACTION_PART_SIZE_IN_BITS;
198 Fractional = Divisor & FRACTION_PART_MASK;
205 if (((
MmioRead32 (UartBase + UARTCR) & PL011_UARTCR_UARTEN) != 0) &&
206 (
MmioRead32 (UartBase + UARTLCR_H) == LineControl) &&
207 (
MmioRead32 (UartBase + UARTIBRD) == Integer) &&
208 (
MmioRead32 (UartBase + UARTFBRD) == Fractional))
215 while ((
MmioRead32 (UartBase + UARTFR) & PL011_UARTFR_TXFE) == 0) {
235 PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN
277 if ((Control & mInvalidControlBits) != 0) {
283 if ((Control & EFI_SERIAL_REQUEST_TO_SEND) != 0) {
284 Bits |= PL011_UARTCR_RTS;
286 Bits &= ~PL011_UARTCR_RTS;
289 if ((Control & EFI_SERIAL_DATA_TERMINAL_READY) != 0) {
290 Bits |= PL011_UARTCR_DTR;
292 Bits &= ~PL011_UARTCR_DTR;
295 if ((Control & EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE) != 0) {
296 Bits |= PL011_UARTCR_LBE;
298 Bits &= ~PL011_UARTCR_LBE;
301 if ((Control & EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE) != 0) {
302 Bits |= (PL011_UARTCR_CTSEN | PL011_UARTCR_RTSEN);
304 Bits &= ~(PL011_UARTCR_CTSEN | PL011_UARTCR_RTSEN);
353 UINT32 ControlRegister;
355 FlagRegister =
MmioRead32 (UartBase + UARTFR);
356 ControlRegister =
MmioRead32 (UartBase + UARTCR);
360 if ((FlagRegister & PL011_UARTFR_CTS) == PL011_UARTFR_CTS) {
361 *Control |= EFI_SERIAL_CLEAR_TO_SEND;
364 if ((FlagRegister & PL011_UARTFR_DSR) == PL011_UARTFR_DSR) {
365 *Control |= EFI_SERIAL_DATA_SET_READY;
368 if ((FlagRegister & PL011_UARTFR_RI) == PL011_UARTFR_RI) {
369 *Control |= EFI_SERIAL_RING_INDICATE;
372 if ((FlagRegister & PL011_UARTFR_DCD) == PL011_UARTFR_DCD) {
373 *Control |= EFI_SERIAL_CARRIER_DETECT;
376 if ((ControlRegister & PL011_UARTCR_RTS) == PL011_UARTCR_RTS) {
377 *Control |= EFI_SERIAL_REQUEST_TO_SEND;
380 if ((ControlRegister & PL011_UARTCR_DTR) == PL011_UARTCR_DTR) {
381 *Control |= EFI_SERIAL_DATA_TERMINAL_READY;
384 if ((FlagRegister & PL011_UARTFR_RXFE) == PL011_UARTFR_RXFE) {
385 *Control |= EFI_SERIAL_INPUT_BUFFER_EMPTY;
388 if ((FlagRegister & PL011_UARTFR_TXFE) == PL011_UARTFR_TXFE) {
389 *Control |= EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
392 if ((ControlRegister & (PL011_UARTCR_CTSEN | PL011_UARTCR_RTSEN))
393 == (PL011_UARTCR_CTSEN | PL011_UARTCR_RTSEN))
395 *Control |= EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE;
398 if ((ControlRegister & PL011_UARTCR_LBE) == PL011_UARTCR_LBE) {
399 *Control |= EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE;
423 UINT8 *
CONST Final = &Buffer[NumberOfBytes];
425 while (Buffer < Final) {
427 while ((
MmioRead32 (UartBase + UARTFR) & UART_TX_FULL_FLAG_MASK)) {
433 return NumberOfBytes;
456 for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {
457 while ((
MmioRead32 (UartBase + UARTFR) & UART_RX_EMPTY_FLAG_MASK) != 0) {
463 return NumberOfBytes;
479 return ((
MmioRead32 (UartBase + UARTFR) & UART_RX_EMPTY_FLAG_MASK) == 0);
UINT8 EFIAPI MmioRead8(IN UINTN Address)
UINT8 EFIAPI MmioWrite8(IN UINTN Address, IN UINT8 Value)
UINT32 EFIAPI MmioRead32(IN UINTN Address)
UINT32 EFIAPI MmioWrite32(IN UINTN Address, IN UINT32 Value)
#define RETURN_UNSUPPORTED
#define RETURN_INVALID_PARAMETER
RETURN_STATUS EFIAPI PL011UartInitializePort(IN UINTN UartBase, IN UINT32 UartClkInHz, IN OUT UINT64 *BaudRate, IN OUT UINT32 *ReceiveFifoDepth, IN OUT EFI_PARITY_TYPE *Parity, IN OUT UINT8 *DataBits, IN OUT EFI_STOP_BITS_TYPE *StopBits)
RETURN_STATUS EFIAPI PL011UartSetControl(IN UINTN UartBase, IN UINT32 Control)
UINTN EFIAPI PL011UartWrite(IN UINTN UartBase, IN UINT8 *Buffer, IN UINTN NumberOfBytes)
UINTN EFIAPI PL011UartRead(IN UINTN UartBase, OUT UINT8 *Buffer, IN UINTN NumberOfBytes)
BOOLEAN EFIAPI PL011UartPoll(IN UINTN UartBase)
RETURN_STATUS EFIAPI PL011UartGetControl(IN UINTN UartBase, OUT UINT32 *Control)
#define FixedPcdGet32(TokenName)
#define FixedPcdGet16(TokenName)