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