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