]> git.proxmox.com Git - mirror_edk2.git/blame - ArmEbPkg/Library/SerialPortLib/SerialPortLib.c
A better template, with some build scripts, for ArmEbPkg. New libraries are just...
[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
cf748a1a 5 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
1fde2f61 6 \r
cf748a1a 7 This program and the accompanying materials\r
1fde2f61 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
b76848cb 22\r
23#include <ArmEb/ArmEb.h>\r
1fde2f61 24\r
25/*\r
26\r
27 Programmed hardware of Serial port.\r
28\r
29 @return Always return EFI_UNSUPPORTED.\r
30\r
31**/\r
32RETURN_STATUS\r
33EFIAPI\r
34SerialPortInitialize (\r
35 VOID\r
36 )\r
37{\r
38 UINT32 Base = PcdGet32 (PcdConsoleUart);\r
39 \r
40 // initialize baud rate generator to 115200 based on EB clock REFCLK24MHZ\r
41 MmioWrite32 (Base + UARTIBRD, UART_115200_IDIV);\r
42 MmioWrite32 (Base + UARTFBRD, UART_115200_FDIV);\r
43\r
44 // no parity, 1 stop, no fifo, 8 data bits\r
45 MmioWrite32 (Base + UARTLCR_H, 0x60);\r
46\r
47 // clear any pending errors\r
48 MmioWrite32 (Base + UARTECR, 0);\r
49\r
50 // enable tx, rx, and uart overall\r
51 MmioWrite32 (Base + UARTCR, 0x301);\r
52\r
53 return RETURN_SUCCESS;\r
54}\r
55\r
56/**\r
57 Write data to serial device.\r
58\r
59 @param Buffer Point of data buffer which need to be writed.\r
60 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
61\r
62 @retval 0 Write data failed.\r
63 @retval !0 Actual number of bytes writed to serial device.\r
64\r
65**/\r
66UINTN\r
67EFIAPI\r
68SerialPortWrite (\r
69 IN UINT8 *Buffer,\r
70 IN UINTN NumberOfBytes\r
71)\r
72{\r
73 UINT32 FR = PcdGet32(PcdConsoleUart) + UARTFR;\r
74 UINT32 DR = PcdGet32(PcdConsoleUart) + UARTDR;\r
75 UINTN Count;\r
76 \r
77 for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {\r
78 while ((MmioRead32 (FR) & UART_TX_EMPTY_FLAG_MASK) != 0);\r
79 MmioWrite8 (DR, *Buffer);\r
80 }\r
81\r
82 return NumberOfBytes;\r
83}\r
84\r
85\r
86/**\r
87 Read data from serial device and save the datas in buffer.\r
88\r
89 @param Buffer Point of data buffer which need to be writed.\r
90 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
91\r
92 @retval 0 Read data failed.\r
93 @retval !0 Aactual number of bytes read from serial device.\r
94\r
95**/\r
96UINTN\r
97EFIAPI\r
98SerialPortRead (\r
99 OUT UINT8 *Buffer,\r
100 IN UINTN NumberOfBytes\r
101)\r
102{\r
103 UINT32 FR = PcdGet32(PcdConsoleUart) + UARTFR;\r
104 UINT32 DR = PcdGet32(PcdConsoleUart) + UARTDR;\r
105 UINTN Count;\r
106 \r
107 for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {\r
108 while ((MmioRead32 (FR) & UART_RX_EMPTY_FLAG_MASK) == 0);\r
109 *Buffer = MmioRead8 (DR);\r
110 }\r
111\r
112 return NumberOfBytes;\r
113}\r
114\r
115\r
116/**\r
117 Check to see if any data is avaiable to be read from the debug device.\r
118\r
119 @retval EFI_SUCCESS At least one byte of data is avaiable to be read\r
120 @retval EFI_NOT_READY No data is avaiable to be read\r
121 @retval EFI_DEVICE_ERROR The serial device is not functioning properly\r
122\r
123**/\r
124BOOLEAN\r
125EFIAPI\r
126SerialPortPoll (\r
127 VOID\r
128 )\r
129{\r
130 UINT32 FR = PcdGet32(PcdConsoleUart) + UARTFR;\r
131\r
132 if ((MmioRead32 (FR) & UART_RX_EMPTY_FLAG_MASK) == 0) {\r
133 return TRUE;\r
134 } else {\r
135 return FALSE;\r
136 }\r
137}\r
138\r