]>
Commit | Line | Data |
---|---|---|
1d5d0ae9 | 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 | #include <Library/SerialPortLib.h> | |
19 | #include <Library/IoLib.h> | |
20 | #include <Drivers/PL011Uart.h> | |
21 | #include <ArmPlatform.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 | SerialPortInitialize ( | |
33 | VOID | |
34 | ) | |
35 | { | |
36 | if (PL011_CONSOLE_UART_SPEED == 115200) { | |
37 | // Initialize baud rate generator | |
38 | MmioWrite32 (PL011_CONSOLE_UART_BASE + UARTIBRD, UART_115200_IDIV); | |
39 | MmioWrite32 (PL011_CONSOLE_UART_BASE + UARTFBRD, UART_115200_FDIV); | |
40 | } else if (PL011_CONSOLE_UART_SPEED == 38400) { | |
41 | // Initialize baud rate generator | |
42 | MmioWrite32 (PL011_CONSOLE_UART_BASE + UARTIBRD, UART_38400_IDIV); | |
43 | MmioWrite32 (PL011_CONSOLE_UART_BASE + UARTFBRD, UART_38400_FDIV); | |
44 | } else if (PL011_CONSOLE_UART_SPEED == 19200) { | |
45 | // Initialize baud rate generator | |
46 | MmioWrite32 (PL011_CONSOLE_UART_BASE + UARTIBRD, UART_19200_IDIV); | |
47 | MmioWrite32 (PL011_CONSOLE_UART_BASE + UARTFBRD, UART_19200_FDIV); | |
48 | } else { | |
49 | return EFI_INVALID_PARAMETER; | |
50 | } | |
51 | ||
52 | // No parity, 1 stop, no fifo, 8 data bits | |
53 | MmioWrite32 (PL011_CONSOLE_UART_BASE + UARTLCR_H, 0x60); | |
54 | ||
55 | // Clear any pending errors | |
56 | MmioWrite32 (PL011_CONSOLE_UART_BASE + UARTECR, 0); | |
57 | ||
58 | // enable tx, rx, and uart overall | |
59 | MmioWrite32 (PL011_CONSOLE_UART_BASE + UARTCR, 0x301); | |
60 | ||
61 | return EFI_SUCCESS; | |
62 | } | |
63 | ||
64 | /** | |
65 | Write data to serial device. | |
66 | ||
67 | @param Buffer Point of data buffer which need to be writed. | |
68 | @param NumberOfBytes Number of output bytes which are cached in Buffer. | |
69 | ||
70 | @retval 0 Write data failed. | |
71 | @retval !0 Actual number of bytes writed to serial device. | |
72 | ||
73 | **/ | |
74 | UINTN | |
75 | EFIAPI | |
76 | SerialPortWrite ( | |
77 | IN UINT8 *Buffer, | |
78 | IN UINTN NumberOfBytes | |
79 | ) | |
80 | { | |
81 | UINTN Count; | |
82 | ||
83 | for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) { | |
84 | while ((MmioRead32 (PL011_CONSOLE_UART_BASE + UARTFR) & UART_TX_EMPTY_FLAG_MASK) == 0); | |
85 | MmioWrite8 (PL011_CONSOLE_UART_BASE + UARTDR, *Buffer); | |
86 | } | |
87 | ||
88 | return NumberOfBytes; | |
89 | } | |
90 | ||
91 | /** | |
92 | Read data from serial device and save the datas in buffer. | |
93 | ||
94 | @param Buffer Point of data buffer which need to be writed. | |
95 | @param NumberOfBytes Number of output bytes which are cached in Buffer. | |
96 | ||
97 | @retval 0 Read data failed. | |
98 | @retval !0 Aactual number of bytes read from serial device. | |
99 | ||
100 | **/ | |
101 | UINTN | |
102 | EFIAPI | |
103 | SerialPortRead ( | |
104 | OUT UINT8 *Buffer, | |
105 | IN UINTN NumberOfBytes | |
106 | ) | |
107 | { | |
108 | UINTN Count; | |
109 | ||
110 | for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) { | |
111 | while ((MmioRead32 (PL011_CONSOLE_UART_BASE + UARTFR) & UART_RX_EMPTY_FLAG_MASK) != 0); | |
112 | *Buffer = MmioRead8 (PL011_CONSOLE_UART_BASE + UARTDR); | |
113 | } | |
114 | ||
115 | return NumberOfBytes; | |
116 | } | |
117 | ||
118 | /** | |
119 | Check to see if any data is avaiable to be read from the debug device. | |
120 | ||
121 | @retval EFI_SUCCESS At least one byte of data is avaiable to be read | |
122 | @retval EFI_NOT_READY No data is avaiable to be read | |
123 | @retval EFI_DEVICE_ERROR The serial device is not functioning properly | |
124 | ||
125 | **/ | |
126 | BOOLEAN | |
127 | EFIAPI | |
128 | SerialPortPoll ( | |
129 | VOID | |
130 | ) | |
131 | { | |
132 | return ((MmioRead32 (PL011_CONSOLE_UART_BASE + UARTFR) & UART_RX_EMPTY_FLAG_MASK) == 0); | |
133 | } |