]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Drivers/PL011Uart/PL011Uart.c
ArmPlatformPkg/PL011Uart: Create PL011 UART driver
[mirror_edk2.git] / ArmPlatformPkg / Drivers / PL011Uart / PL011Uart.c
1 /** @file
2 Serial I/O Port library functions with no library constructor/destructor
3
4
5 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include <Include/Uefi.h>
18
19 #include <Library/IoLib.h>
20
21 #include <Drivers/PL011Uart.h>
22
23 /*
24
25 Programmed hardware of Serial port.
26
27 @return Always return EFI_UNSUPPORTED.
28
29 **/
30 RETURN_STATUS
31 EFIAPI
32 PL011UartInitialize (
33 IN UINTN UartBase,
34 IN UINTN BaudRate,
35 IN UINTN LineControl
36 )
37 {
38 if (BaudRate == 115200) {
39 // Initialize baud rate generator
40 MmioWrite32 (UartBase + UARTIBRD, UART_115200_IDIV);
41 MmioWrite32 (UartBase + UARTFBRD, UART_115200_FDIV);
42 } else if (BaudRate == 38400) {
43 // Initialize baud rate generator
44 MmioWrite32 (UartBase + UARTIBRD, UART_38400_IDIV);
45 MmioWrite32 (UartBase + UARTFBRD, UART_38400_FDIV);
46 } else if (BaudRate == 19200) {
47 // Initialize baud rate generator
48 MmioWrite32 (UartBase + UARTIBRD, UART_19200_IDIV);
49 MmioWrite32 (UartBase + UARTFBRD, UART_19200_FDIV);
50 } else {
51 return EFI_INVALID_PARAMETER;
52 }
53
54 // No parity, 1 stop, no fifo, 8 data bits
55 MmioWrite32 (UartBase + UARTLCR_H, LineControl);
56
57 // Clear any pending errors
58 MmioWrite32 (UartBase + UARTECR, 0);
59
60 // Enable tx, rx, and uart overall
61 MmioWrite32 (UartBase + UARTCR, PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN);
62
63 return EFI_SUCCESS;
64 }
65
66 /**
67 Write data to serial device.
68
69 @param Buffer Point of data buffer which need to be written.
70 @param NumberOfBytes Number of output bytes which are cached in Buffer.
71
72 @retval 0 Write data failed.
73 @retval !0 Actual number of bytes written to serial device.
74
75 **/
76 UINTN
77 EFIAPI
78 PL011UartWrite (
79 IN UINTN UartBase,
80 IN UINT8 *Buffer,
81 IN UINTN NumberOfBytes
82 )
83 {
84 UINTN Count;
85
86 for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {
87 while ((MmioRead32 (UartBase + UARTFR) & UART_TX_EMPTY_FLAG_MASK) == 0);
88 MmioWrite8 (UartBase + UARTDR, *Buffer);
89 }
90
91 return NumberOfBytes;
92 }
93
94 /**
95 Read data from serial device and save the data in buffer.
96
97 @param Buffer Point of data buffer which need to be written.
98 @param NumberOfBytes Number of output bytes which are cached in Buffer.
99
100 @retval 0 Read data failed.
101 @retval !0 Actual number of bytes read from serial device.
102
103 **/
104 UINTN
105 EFIAPI
106 PL011UartRead (
107 IN UINTN UartBase,
108 OUT UINT8 *Buffer,
109 IN UINTN NumberOfBytes
110 )
111 {
112 UINTN Count;
113
114 for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {
115 while ((MmioRead32 (UartBase + UARTFR) & UART_RX_EMPTY_FLAG_MASK) != 0);
116 *Buffer = MmioRead8 (UartBase + UARTDR);
117 }
118
119 return NumberOfBytes;
120 }
121
122 /**
123 Check to see if any data is available to be read from the debug device.
124
125 @retval EFI_SUCCESS At least one byte of data is available to be read
126 @retval EFI_NOT_READY No data is available to be read
127 @retval EFI_DEVICE_ERROR The serial device is not functioning properly
128
129 **/
130 BOOLEAN
131 EFIAPI
132 PL011UartPoll (
133 IN UINTN UartBase
134 )
135 {
136 return ((MmioRead32 (UartBase + UARTFR) & UART_RX_EMPTY_FLAG_MASK) == 0);
137 }