1 /** @file
2   Serial I/O Port library functions with base address discovered from FDT
3 
4   Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5   Copyright (c) 2012 - 2013, ARM Ltd. All rights reserved.<BR>
6   Copyright (c) 2014, Linaro Ltd. All rights reserved.<BR>
7   Copyright (c) 2014, Red Hat, Inc.<BR>
8   Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
9 
10   This program and the accompanying materials
11   are licensed and made available under the terms and conditions of the BSD License
12   which accompanies this distribution.  The full text of the license may be found at
13   http://opensource.org/licenses/bsd-license.php
14 
15   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 
18 **/
19 
20 #include <Base.h>
21 
22 #include <Library/PcdLib.h>
23 #include <Library/SerialPortLib.h>
24 #include <Pi/PiBootMode.h>
25 #include <Uefi/UefiBaseType.h>
26 #include <Uefi/UefiMultiPhase.h>
27 #include <Pi/PiHob.h>
28 #include <Library/HobLib.h>
29 #include <Guid/EarlyPL011BaseAddress.h>
30 
31 #include <Drivers/PL011Uart.h>
32 
33 STATIC UINTN mSerialBaseAddress;
34 
35 RETURN_STATUS
36 EFIAPI
SerialPortInitialize(VOID)37 SerialPortInitialize (
38   VOID
39   )
40 {
41   return RETURN_SUCCESS;
42 }
43 
44 /**
45 
46   Program hardware of Serial port
47 
48   @return    RETURN_NOT_FOUND if no PL011 base address could be found
49              Otherwise, result of PL011UartInitializePort () is returned
50 
51 **/
52 RETURN_STATUS
53 EFIAPI
FdtPL011SerialPortLibInitialize(VOID)54 FdtPL011SerialPortLibInitialize (
55   VOID
56   )
57 {
58   VOID                *Hob;
59   CONST UINT64        *UartBase;
60   UINT64              BaudRate;
61   UINT32              ReceiveFifoDepth;
62   EFI_PARITY_TYPE     Parity;
63   UINT8               DataBits;
64   EFI_STOP_BITS_TYPE  StopBits;
65 
66   Hob = GetFirstGuidHob (&gEarlyPL011BaseAddressGuid);
67   if (Hob == NULL || GET_GUID_HOB_DATA_SIZE (Hob) != sizeof *UartBase) {
68     return RETURN_NOT_FOUND;
69   }
70   UartBase = GET_GUID_HOB_DATA (Hob);
71 
72   mSerialBaseAddress = (UINTN)*UartBase;
73   if (mSerialBaseAddress == 0) {
74     return RETURN_NOT_FOUND;
75   }
76 
77   BaudRate = (UINTN)PcdGet64 (PcdUartDefaultBaudRate);
78   ReceiveFifoDepth = 0; // Use the default value for Fifo depth
79   Parity = (EFI_PARITY_TYPE)PcdGet8 (PcdUartDefaultParity);
80   DataBits = PcdGet8 (PcdUartDefaultDataBits);
81   StopBits = (EFI_STOP_BITS_TYPE) PcdGet8 (PcdUartDefaultStopBits);
82 
83   return PL011UartInitializePort (
84            mSerialBaseAddress, &BaudRate, &ReceiveFifoDepth,
85            &Parity, &DataBits, &StopBits);
86 }
87 
88 /**
89   Write data to serial device.
90 
91   @param  Buffer           Point of data buffer which need to be written.
92   @param  NumberOfBytes    Number of output bytes which are cached in Buffer.
93 
94   @retval 0                Write data failed.
95   @retval !0               Actual number of bytes written to serial device.
96 
97 **/
98 UINTN
99 EFIAPI
SerialPortWrite(IN UINT8 * Buffer,IN UINTN NumberOfBytes)100 SerialPortWrite (
101   IN UINT8     *Buffer,
102   IN UINTN     NumberOfBytes
103   )
104 {
105   if (mSerialBaseAddress != 0) {
106     return PL011UartWrite (mSerialBaseAddress, Buffer, NumberOfBytes);
107   }
108   return 0;
109 }
110 
111 /**
112   Read data from serial device and save the data in buffer.
113 
114   @param  Buffer           Point of data buffer which need to be written.
115   @param  NumberOfBytes    Number of output bytes which are cached in Buffer.
116 
117   @retval 0                Read data failed.
118   @retval !0               Actual number of bytes read from serial device.
119 
120 **/
121 UINTN
122 EFIAPI
SerialPortRead(OUT UINT8 * Buffer,IN UINTN NumberOfBytes)123 SerialPortRead (
124   OUT UINT8     *Buffer,
125   IN  UINTN     NumberOfBytes
126 )
127 {
128   if (mSerialBaseAddress != 0) {
129     return PL011UartRead (mSerialBaseAddress, Buffer, NumberOfBytes);
130   }
131   return 0;
132 }
133 
134 /**
135   Check to see if any data is available to be read from the debug device.
136 
137   @retval TRUE       At least one byte of data is available to be read
138   @retval FALSE      No data is available to be read
139 
140 **/
141 BOOLEAN
142 EFIAPI
SerialPortPoll(VOID)143 SerialPortPoll (
144   VOID
145   )
146 {
147   if (mSerialBaseAddress != 0) {
148     return PL011UartPoll (mSerialBaseAddress);
149   }
150   return FALSE;
151 }
152 
153 /**
154   Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
155   data bits, and stop bits on a serial device.
156 
157   @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
158                             device's default interface speed.
159                             On output, the value actually set.
160   @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
161                             serial interface. A ReceiveFifoDepth value of 0 will use
162                             the device's default FIFO depth.
163                             On output, the value actually set.
164   @param Timeout            The requested time out for a single character in microseconds.
165                             This timeout applies to both the transmit and receive side of the
166                             interface. A Timeout value of 0 will use the device's default time
167                             out value.
168                             On output, the value actually set.
169   @param Parity             The type of parity to use on this serial device. A Parity value of
170                             DefaultParity will use the device's default parity value.
171                             On output, the value actually set.
172   @param DataBits           The number of data bits to use on the serial device. A DataBits
173                             vaule of 0 will use the device's default data bit setting.
174                             On output, the value actually set.
175   @param StopBits           The number of stop bits to use on this serial device. A StopBits
176                             value of DefaultStopBits will use the device's default number of
177                             stop bits.
178                             On output, the value actually set.
179 
180   @retval RETURN_SUCCESS            The new attributes were set on the serial device.
181   @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
182   @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
183   @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
184 
185 **/
186 RETURN_STATUS
187 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)188 SerialPortSetAttributes (
189   IN OUT UINT64             *BaudRate,
190   IN OUT UINT32             *ReceiveFifoDepth,
191   IN OUT UINT32             *Timeout,
192   IN OUT EFI_PARITY_TYPE    *Parity,
193   IN OUT UINT8              *DataBits,
194   IN OUT EFI_STOP_BITS_TYPE *StopBits
195   )
196 {
197   return RETURN_UNSUPPORTED;
198 }
199 
200 /**
201   Sets the control bits on a serial device.
202 
203   @param Control                Sets the bits of Control that are settable.
204 
205   @retval RETURN_SUCCESS        The new control bits were set on the serial device.
206   @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
207   @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
208 
209 **/
210 RETURN_STATUS
211 EFIAPI
SerialPortSetControl(IN UINT32 Control)212 SerialPortSetControl (
213   IN UINT32 Control
214   )
215 {
216   return RETURN_UNSUPPORTED;
217 }
218 
219 /**
220   Retrieve the status of the control bits on a serial device.
221 
222   @param Control                A pointer to return the current control signals from the serial device.
223 
224   @retval RETURN_SUCCESS        The control bits were read from the serial device.
225   @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
226   @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
227 
228 **/
229 RETURN_STATUS
230 EFIAPI
SerialPortGetControl(OUT UINT32 * Control)231 SerialPortGetControl (
232   OUT UINT32 *Control
233   )
234 {
235   return RETURN_UNSUPPORTED;
236 }
237 
238