1 /** @file
2   Serial I/O Port library functions with no library constructor/destructor
3 
4   Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5   Copyright (c) 2012 - 2014, ARM Ltd. All rights reserved.<BR>
6   Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
7 
8   This program and the accompanying materials
9   are licensed and made available under the terms and conditions of the BSD License
10   which accompanies this distribution.  The full text of the license may be found at
11   http://opensource.org/licenses/bsd-license.php
12 
13   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 
16 **/
17 
18 #include <Base.h>
19 
20 #include <Library/IoLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/SerialPortLib.h>
23 
24 #include <Drivers/PL011Uart.h>
25 
26 
27 /**
28 
29   Programmed hardware of Serial port.
30 
31   @return    Always return RETURN_UNSUPPORTED.
32 
33 **/
34 RETURN_STATUS
35 EFIAPI
SerialPortInitialize(VOID)36 SerialPortInitialize (
37   VOID
38   )
39 {
40   UINT64              BaudRate;
41   UINT32              ReceiveFifoDepth;
42   EFI_PARITY_TYPE     Parity;
43   UINT8               DataBits;
44   EFI_STOP_BITS_TYPE  StopBits;
45 
46   BaudRate = (UINTN)PcdGet64 (PcdUartDefaultBaudRate);
47   ReceiveFifoDepth = 0; // Use the default value for Fifo depth
48   Parity = (EFI_PARITY_TYPE)PcdGet8 (PcdUartDefaultParity);
49   DataBits = PcdGet8 (PcdUartDefaultDataBits);
50   StopBits = (EFI_STOP_BITS_TYPE) PcdGet8 (PcdUartDefaultStopBits);
51 
52   return PL011UartInitializePort (
53       (UINTN)PcdGet64 (PcdSerialRegisterBase),
54       &BaudRate, &ReceiveFifoDepth, &Parity, &DataBits, &StopBits);
55 }
56 
57 /**
58   Write data to serial device.
59 
60   @param  Buffer           Point of data buffer which need to be written.
61   @param  NumberOfBytes    Number of output bytes which are cached in Buffer.
62 
63   @retval 0                Write data failed.
64   @retval !0               Actual number of bytes written to serial device.
65 
66 **/
67 UINTN
68 EFIAPI
SerialPortWrite(IN UINT8 * Buffer,IN UINTN NumberOfBytes)69 SerialPortWrite (
70   IN UINT8     *Buffer,
71   IN UINTN     NumberOfBytes
72   )
73 {
74   return PL011UartWrite ((UINTN)PcdGet64 (PcdSerialRegisterBase), Buffer, NumberOfBytes);
75 }
76 
77 /**
78   Read data from serial device and save the data in buffer.
79 
80   @param  Buffer           Point of data buffer which need to be written.
81   @param  NumberOfBytes    Number of output bytes which are cached in Buffer.
82 
83   @retval 0                Read data failed.
84   @retval !0               Actual number of bytes read from serial device.
85 
86 **/
87 UINTN
88 EFIAPI
SerialPortRead(OUT UINT8 * Buffer,IN UINTN NumberOfBytes)89 SerialPortRead (
90   OUT UINT8     *Buffer,
91   IN  UINTN     NumberOfBytes
92 )
93 {
94   return PL011UartRead ((UINTN)PcdGet64 (PcdSerialRegisterBase), Buffer, NumberOfBytes);
95 }
96 
97 /**
98   Check to see if any data is available to be read from the debug device.
99 
100   @retval EFI_SUCCESS       At least one byte of data is available to be read
101   @retval EFI_NOT_READY     No data is available to be read
102   @retval EFI_DEVICE_ERROR  The serial device is not functioning properly
103 
104 **/
105 BOOLEAN
106 EFIAPI
SerialPortPoll(VOID)107 SerialPortPoll (
108   VOID
109   )
110 {
111   return PL011UartPoll ((UINTN)PcdGet64 (PcdSerialRegisterBase));
112 }
113 /**
114   Set new attributes to PL011.
115 
116   @param  BaudRate                The baud rate of the serial device. If the baud rate is not supported,
117                                   the speed will be reduced down to the nearest supported one and the
118                                   variable's value will be updated accordingly.
119   @param  ReceiveFifoDepth        The number of characters the device will buffer on input. If the specified
120                                   value is not supported, the variable's value will be reduced down to the
121                                   nearest supported one.
122   @param  Timeout                 If applicable, the number of microseconds the device will wait
123                                   before timing out a Read or a Write operation.
124   @param  Parity                  If applicable, this is the EFI_PARITY_TYPE that is computed or checked
125                                   as each character is transmitted or received. If the device does not
126                                   support parity, the value is the default parity value.
127   @param  DataBits                The number of data bits in each character
128   @param  StopBits                If applicable, the EFI_STOP_BITS_TYPE number of stop bits per character.
129                                   If the device does not support stop bits, the value is the default stop
130                                   bit value.
131 
132   @retval EFI_SUCCESS             All attributes were set correctly on the serial device.
133   @retval EFI_INVALID_PARAMETERS  One or more of the attributes has an unsupported value.
134 
135 **/
136 RETURN_STATUS
137 EFIAPI
SerialPortSetAttributes(IN OUT UINT64 * BaudRate,IN OUT UINT32 * ReceiveFifoDepth,IN OUT UINT32 * Timeout,IN OUT EFI_PARITY_TYPE * Parity,IN OUT UINT8 * DataBits,IN OUT EFI_STOP_BITS_TYPE * StopBits)138 SerialPortSetAttributes (
139   IN OUT UINT64              *BaudRate,
140   IN OUT UINT32              *ReceiveFifoDepth,
141   IN OUT UINT32              *Timeout,
142   IN OUT EFI_PARITY_TYPE     *Parity,
143   IN OUT UINT8               *DataBits,
144   IN OUT EFI_STOP_BITS_TYPE  *StopBits
145   )
146 {
147   return PL011UartInitializePort (
148         (UINTN)PcdGet64 (PcdSerialRegisterBase),
149         BaudRate,
150         ReceiveFifoDepth,
151         Parity,
152         DataBits,
153         StopBits);
154 }
155 
156 /**
157 
158   Assert or deassert the control signals on a serial port.
159   The following control signals are set according their bit settings :
160   . Request to Send
161   . Data Terminal Ready
162 
163   @param[in]  Control  The following bits are taken into account :
164                        . EFI_SERIAL_REQUEST_TO_SEND : assert/deassert the
165                          "Request To Send" control signal if this bit is
166                          equal to one/zero.
167                        . EFI_SERIAL_DATA_TERMINAL_READY : assert/deassert
168                          the "Data Terminal Ready" control signal if this
169                          bit is equal to one/zero.
170                        . EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE : enable/disable
171                          the hardware loopback if this bit is equal to
172                          one/zero.
173                        . EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE : not supported.
174                        . EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE : enable/
175                          disable the hardware flow control based on CTS (Clear
176                          To Send) and RTS (Ready To Send) control signals.
177 
178   @retval  RETURN_SUCCESS      The new control bits were set on the serial device.
179   @retval  RETURN_UNSUPPORTED  The serial device does not support this operation.
180 
181 **/
182 RETURN_STATUS
183 EFIAPI
SerialPortSetControl(IN UINT32 Control)184 SerialPortSetControl (
185   IN UINT32  Control
186   )
187 {
188   return PL011UartSetControl ((UINTN)PcdGet64 (PcdSerialRegisterBase), Control);
189 }
190 
191 /**
192 
193   Retrieve the status of the control bits on a serial device.
194 
195   @param[out]  Control  Status of the control bits on a serial device :
196 
197                         . EFI_SERIAL_DATA_CLEAR_TO_SEND, EFI_SERIAL_DATA_SET_READY,
198                           EFI_SERIAL_RING_INDICATE, EFI_SERIAL_CARRIER_DETECT,
199                           EFI_SERIAL_REQUEST_TO_SEND, EFI_SERIAL_DATA_TERMINAL_READY
200                           are all related to the DTE (Data Terminal Equipment) and
201                           DCE (Data Communication Equipment) modes of operation of
202                           the serial device.
203                         . EFI_SERIAL_INPUT_BUFFER_EMPTY : equal to one if the receive
204                           buffer is empty, 0 otherwise.
205                         . EFI_SERIAL_OUTPUT_BUFFER_EMPTY : equal to one if the transmit
206                           buffer is empty, 0 otherwise.
207                         . EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE : equal to one if the
208                           hardware loopback is enabled (the output feeds the receive
209                           buffer), 0 otherwise.
210                         . EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE : equal to one if a
211                           loopback is accomplished by software, 0 otherwise.
212                         . EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE : equal to one if the
213                           hardware flow control based on CTS (Clear To Send) and RTS
214                           (Ready To Send) control signals is enabled, 0 otherwise.
215 
216   @retval RETURN_SUCCESS  The control bits were read from the serial device.
217 
218 **/
219 RETURN_STATUS
220 EFIAPI
SerialPortGetControl(OUT UINT32 * Control)221 SerialPortGetControl (
222   OUT UINT32  *Control
223   )
224 {
225   return PL011UartGetControl ((UINTN)PcdGet64 (PcdSerialRegisterBase), Control);
226 }
227