]> git.proxmox.com Git - mirror_edk2.git/blame - ArmEbPkg/Library/SerialPortLib/SerialPortLib.c
Started working on an ArmEb package. GIC is ported. SEC is a start. Still missing...
[mirror_edk2.git] / ArmEbPkg / Library / SerialPortLib / SerialPortLib.c
CommitLineData
1fde2f61 1/** @file\r
2 Serial I/O Port library functions with no library constructor/destructor\r
3\r
4\r
5 Copyright (c) 2008-2010, Apple Inc. All rights reserved.\r
6 \r
7 All rights reserved. This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include <Base.h>\r
18#include <Library/DebugLib.h>\r
19#include <Library/SerialPortLib.h>\r
20#include <Library/PcdLib.h>\r
21#include <Library/IoLib.h>\r
22#include <ArmEbUart.h>\r
23\r
24/*\r
25\r
26 Programmed hardware of Serial port.\r
27\r
28 @return Always return EFI_UNSUPPORTED.\r
29\r
30**/\r
31RETURN_STATUS\r
32EFIAPI\r
33SerialPortInitialize (\r
34 VOID\r
35 )\r
36{\r
37 UINT32 Base = PcdGet32 (PcdConsoleUart);\r
38 \r
39 // initialize baud rate generator to 115200 based on EB clock REFCLK24MHZ\r
40 MmioWrite32 (Base + UARTIBRD, UART_115200_IDIV);\r
41 MmioWrite32 (Base + UARTFBRD, UART_115200_FDIV);\r
42\r
43 // no parity, 1 stop, no fifo, 8 data bits\r
44 MmioWrite32 (Base + UARTLCR_H, 0x60);\r
45\r
46 // clear any pending errors\r
47 MmioWrite32 (Base + UARTECR, 0);\r
48\r
49 // enable tx, rx, and uart overall\r
50 MmioWrite32 (Base + UARTCR, 0x301);\r
51\r
52 return RETURN_SUCCESS;\r
53}\r
54\r
55/**\r
56 Write data to serial device.\r
57\r
58 @param Buffer Point of data buffer which need to be writed.\r
59 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
60\r
61 @retval 0 Write data failed.\r
62 @retval !0 Actual number of bytes writed to serial device.\r
63\r
64**/\r
65UINTN\r
66EFIAPI\r
67SerialPortWrite (\r
68 IN UINT8 *Buffer,\r
69 IN UINTN NumberOfBytes\r
70)\r
71{\r
72 UINT32 FR = PcdGet32(PcdConsoleUart) + UARTFR;\r
73 UINT32 DR = PcdGet32(PcdConsoleUart) + UARTDR;\r
74 UINTN Count;\r
75 \r
76 for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {\r
77 while ((MmioRead32 (FR) & UART_TX_EMPTY_FLAG_MASK) != 0);\r
78 MmioWrite8 (DR, *Buffer);\r
79 }\r
80\r
81 return NumberOfBytes;\r
82}\r
83\r
84\r
85/**\r
86 Read data from serial device and save the datas in buffer.\r
87\r
88 @param Buffer Point of data buffer which need to be writed.\r
89 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
90\r
91 @retval 0 Read data failed.\r
92 @retval !0 Aactual number of bytes read from serial device.\r
93\r
94**/\r
95UINTN\r
96EFIAPI\r
97SerialPortRead (\r
98 OUT UINT8 *Buffer,\r
99 IN UINTN NumberOfBytes\r
100)\r
101{\r
102 UINT32 FR = PcdGet32(PcdConsoleUart) + UARTFR;\r
103 UINT32 DR = PcdGet32(PcdConsoleUart) + UARTDR;\r
104 UINTN Count;\r
105 \r
106 for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {\r
107 while ((MmioRead32 (FR) & UART_RX_EMPTY_FLAG_MASK) == 0);\r
108 *Buffer = MmioRead8 (DR);\r
109 }\r
110\r
111 return NumberOfBytes;\r
112}\r
113\r
114\r
115/**\r
116 Check to see if any data is avaiable to be read from the debug device.\r
117\r
118 @retval EFI_SUCCESS At least one byte of data is avaiable to be read\r
119 @retval EFI_NOT_READY No data is avaiable to be read\r
120 @retval EFI_DEVICE_ERROR The serial device is not functioning properly\r
121\r
122**/\r
123BOOLEAN\r
124EFIAPI\r
125SerialPortPoll (\r
126 VOID\r
127 )\r
128{\r
129 UINT32 FR = PcdGet32(PcdConsoleUart) + UARTFR;\r
130\r
131 if ((MmioRead32 (FR) & UART_RX_EMPTY_FLAG_MASK) == 0) {\r
132 return TRUE;\r
133 } else {\r
134 return FALSE;\r
135 }\r
136}\r
137\r