0509deb8abe448eb86fdb3140a2a5d610ea67714
[mirror_edk2.git] / Omap35xxPkg / Library / SerialPortLib / SerialPortLib.c
1 /** @file
2 Serial I/O Port library functions with no library constructor/destructor
3
4
5 Copyright (c) 2008-2009, 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 <Library/OmapLib.h>
23 #include <Omap3530/Omap3530.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 // assume assembly code at reset vector has setup UART
39 return RETURN_SUCCESS;
40 }
41
42 /**
43 Write data to serial device.
44
45 @param Buffer Point of data buffer which need to be writed.
46 @param NumberOfBytes Number of output bytes which are cached in Buffer.
47
48 @retval 0 Write data failed.
49 @retval !0 Actual number of bytes writed to serial device.
50
51 **/
52 UINTN
53 EFIAPI
54 SerialPortWrite (
55 IN UINT8 *Buffer,
56 IN UINTN NumberOfBytes
57 )
58 {
59 UINT32 LSR = UartBase(PcdGet32(PcdBeagleConsoleUart)) + UART_LSR_REG;
60 UINT32 THR = UartBase(PcdGet32(PcdBeagleConsoleUart)) + UART_THR_REG;
61 UINTN Count;
62
63 for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {
64 while ((MmioRead8(LSR) & UART_LSR_TX_FIFO_E_MASK) == UART_LSR_TX_FIFO_E_NOT_EMPTY);
65 MmioWrite8(THR, *Buffer);
66 }
67
68 return NumberOfBytes;
69 }
70
71
72 /**
73 Read data from serial device and save the datas in buffer.
74
75 @param Buffer Point of data buffer which need to be writed.
76 @param NumberOfBytes Number of output bytes which are cached in Buffer.
77
78 @retval 0 Read data failed.
79 @retval !0 Aactual number of bytes read from serial device.
80
81 **/
82 UINTN
83 EFIAPI
84 SerialPortRead (
85 OUT UINT8 *Buffer,
86 IN UINTN NumberOfBytes
87 )
88 {
89 UINT32 LSR = UartBase(PcdGet32(PcdBeagleConsoleUart)) + UART_LSR_REG;
90 UINT32 RBR = UartBase(PcdGet32(PcdBeagleConsoleUart)) + UART_RBR_REG;
91 UINTN Count;
92
93 for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {
94 while ((MmioRead8(LSR) & UART_LSR_RX_FIFO_E_MASK) == UART_LSR_RX_FIFO_E_EMPTY);
95 *Buffer = MmioRead8(RBR);
96 }
97
98 return NumberOfBytes;
99 }
100
101
102 /**
103 Check to see if any data is avaiable to be read from the debug device.
104
105 @retval EFI_SUCCESS At least one byte of data is avaiable to be read
106 @retval EFI_NOT_READY No data is avaiable to be read
107 @retval EFI_DEVICE_ERROR The serial device is not functioning properly
108
109 **/
110 BOOLEAN
111 EFIAPI
112 SerialPortPoll (
113 VOID
114 )
115 {
116 UINT32 LSR = UartBase(PcdGet32(PcdBeagleConsoleUart)) + UART_LSR_REG;
117
118 if ((MmioRead8(LSR) & UART_LSR_RX_FIFO_E_MASK) == UART_LSR_RX_FIFO_E_NOT_EMPTY) {
119 return TRUE;
120 } else {
121 return FALSE;
122 }
123 }
124