FdtPL011SerialPortLib.c 7.9 KB

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