PeiSerialPortLibSpiFlash.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /** @file
  2. Serial I/O Port library implementation for output to SPI flash
  3. Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  4. Copyright (c) Microsoft Corporation.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <Base.h>
  8. #include <Ppi/Spi2.h>
  9. #include <Library/BaseLib.h>
  10. #include <Library/BaseMemoryLib.h>
  11. #include <Library/HobLib.h>
  12. #include <Library/PcdLib.h>
  13. #include <Library/PeiServicesLib.h>
  14. #include <Library/SerialPortLib.h>
  15. #include <Library/SpiLib.h>
  16. typedef struct {
  17. UINT32 CurrentWriteOffset;
  18. } SPI_FLASH_DEBUG_CONTEXT;
  19. /**
  20. Returns a pointer to the PCH SPI PPI.
  21. @return Pointer to PCH_SPI2_PPI If an instance of the PCH SPI PPI is found
  22. @return NULL If an instance of the PCH SPI PPI is not found
  23. **/
  24. PCH_SPI2_PPI *
  25. GetSpiPpi (
  26. VOID
  27. )
  28. {
  29. EFI_STATUS Status;
  30. PCH_SPI2_PPI *PchSpi2Ppi;
  31. Status = PeiServicesLocatePpi (
  32. &gPchSpi2PpiGuid,
  33. 0,
  34. NULL,
  35. (VOID **) &PchSpi2Ppi
  36. );
  37. if (EFI_ERROR (Status)) {
  38. return NULL;
  39. }
  40. return PchSpi2Ppi;
  41. }
  42. /**
  43. Common function to write trace data to a chosen debug interface like
  44. UART Serial device, USB Serial device or Trace Hub device
  45. @param Buffer Point of data buffer which needs to be written.
  46. @param NumberOfBytes Number of output bytes which are cached in Buffer.
  47. **/
  48. UINTN
  49. EFIAPI
  50. SerialPortWrite (
  51. IN UINT8 *Buffer,
  52. IN UINTN NumberOfBytes
  53. )
  54. {
  55. EFI_STATUS Status;
  56. EFI_HOB_GUID_TYPE *GuidHob;
  57. SPI_FLASH_DEBUG_CONTEXT *Context;
  58. PCH_SPI2_PPI *PchSpi2Ppi;
  59. UINT32 BytesWritten;
  60. UINT32 SourceBufferOffset;
  61. UINT32 NvMessageAreaSize;
  62. UINT32 LinearOffset;
  63. BytesWritten = NumberOfBytes;
  64. SourceBufferOffset = 0;
  65. NvMessageAreaSize = (UINT32) FixedPcdGet32 (PcdFlashNvDebugMessageSize);
  66. if (NumberOfBytes == 0 || NvMessageAreaSize == 0) {
  67. return 0;
  68. }
  69. GuidHob = GetFirstGuidHob (&gSpiFlashDebugHobGuid);
  70. if (GuidHob == NULL) {
  71. return 0;
  72. }
  73. Context = GET_GUID_HOB_DATA (GuidHob);
  74. if (Context == NULL || Context->CurrentWriteOffset >= NvMessageAreaSize) {
  75. return 0;
  76. }
  77. PchSpi2Ppi = GetSpiPpi ();
  78. if (PchSpi2Ppi == NULL) {
  79. return 0;
  80. }
  81. if ((Context->CurrentWriteOffset + NumberOfBytes) / NvMessageAreaSize > 0) {
  82. LinearOffset = (UINT32) (FixedPcdGet32 (PcdFlashNvDebugMessageBase) - FixedPcdGet32 (PcdFlashAreaBaseAddress));
  83. Status = PchSpi2Ppi->FlashErase (
  84. PchSpi2Ppi,
  85. &gFlashRegionBiosGuid,
  86. LinearOffset,
  87. NvMessageAreaSize
  88. );
  89. if (!EFI_ERROR (Status)) {
  90. Context->CurrentWriteOffset = 0;
  91. } else {
  92. return 0;
  93. }
  94. }
  95. if (NumberOfBytes > NvMessageAreaSize) {
  96. BytesWritten = NvMessageAreaSize;
  97. SourceBufferOffset = NumberOfBytes - NvMessageAreaSize;
  98. }
  99. LinearOffset = (FixedPcdGet32 (PcdFlashNvDebugMessageBase) + Context->CurrentWriteOffset) - FixedPcdGet32 (PcdFlashAreaBaseAddress);
  100. Status = PchSpi2Ppi->FlashWrite (
  101. PchSpi2Ppi,
  102. &gFlashRegionBiosGuid,
  103. LinearOffset,
  104. BytesWritten,
  105. (UINT8 *) &Buffer[SourceBufferOffset]
  106. );
  107. if (!EFI_ERROR (Status)) {
  108. Context->CurrentWriteOffset += BytesWritten;
  109. return BytesWritten;
  110. }
  111. return 0;
  112. }
  113. /**
  114. Common function to Read data from UART serial device, USB serial device and save the datas in buffer.
  115. @param Buffer Point of data buffer which need to be writed.
  116. @param NumberOfBytes Number of output bytes which are cached in Buffer.
  117. @retval 0 Read data failed, no data is to be read.
  118. @retval >0 Actual number of bytes read from debug device.
  119. **/
  120. UINTN
  121. EFIAPI
  122. SerialPortRead (
  123. OUT UINT8 *Buffer,
  124. IN UINTN NumberOfBytes
  125. )
  126. {
  127. return 0;
  128. }
  129. /**
  130. Polls a serial device to see if there is any data waiting to be read.
  131. Polls a serial device to see if there is any data waiting to be read.
  132. If there is data waiting to be read from the serial device, then TRUE is returned.
  133. If there is no data waiting to be read from the serial device, then FALSE is returned.
  134. @retval TRUE Data is waiting to be read from the serial device.
  135. @retval FALSE There is no data waiting to be read from the serial device.
  136. **/
  137. BOOLEAN
  138. EFIAPI
  139. SerialPortPoll (
  140. VOID
  141. )
  142. {
  143. return FALSE;
  144. }
  145. /**
  146. Sets the control bits on a serial device.
  147. @param Control Sets the bits of Control that are settable.
  148. @retval RETURN_SUCCESS The new control bits were set on the serial device.
  149. @retval RETURN_UNSUPPORTED The serial device does not support this operation.
  150. @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
  151. **/
  152. RETURN_STATUS
  153. EFIAPI
  154. SerialPortSetControl (
  155. IN UINT32 Control
  156. )
  157. {
  158. return EFI_UNSUPPORTED;
  159. }
  160. /**
  161. Retrieve the status of the control bits on a serial device.
  162. @param Control A pointer to return the current control signals from the serial device.
  163. @retval RETURN_SUCCESS The control bits were read from the serial device.
  164. @retval RETURN_UNSUPPORTED The serial device does not support this operation.
  165. @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
  166. **/
  167. RETURN_STATUS
  168. EFIAPI
  169. SerialPortGetControl (
  170. OUT UINT32 *Control
  171. )
  172. {
  173. return EFI_UNSUPPORTED;
  174. }
  175. /**
  176. Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
  177. data bits, and stop bits on a serial device.
  178. @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
  179. device's default interface speed.
  180. On output, the value actually set.
  181. @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the
  182. serial interface. A ReceiveFifoDepth value of 0 will use
  183. the device's default FIFO depth.
  184. On output, the value actually set.
  185. @param Timeout The requested time out for a single character in microseconds.
  186. This timeout applies to both the transmit and receive side of the
  187. interface. A Timeout value of 0 will use the device's default time
  188. out value.
  189. On output, the value actually set.
  190. @param Parity The type of parity to use on this serial device. A Parity value of
  191. DefaultParity will use the device's default parity value.
  192. On output, the value actually set.
  193. @param DataBits The number of data bits to use on the serial device. A DataBits
  194. vaule of 0 will use the device's default data bit setting.
  195. On output, the value actually set.
  196. @param StopBits The number of stop bits to use on this serial device. A StopBits
  197. value of DefaultStopBits will use the device's default number of
  198. stop bits.
  199. On output, the value actually set.
  200. @retval RETURN_SUCCESS The new attributes were set on the serial device.
  201. @retval RETURN_UNSUPPORTED The serial device does not support this operation.
  202. @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.
  203. @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
  204. **/
  205. RETURN_STATUS
  206. EFIAPI
  207. SerialPortSetAttributes (
  208. IN OUT UINT64 *BaudRate,
  209. IN OUT UINT32 *ReceiveFifoDepth,
  210. IN OUT UINT32 *Timeout,
  211. IN OUT EFI_PARITY_TYPE *Parity,
  212. IN OUT UINT8 *DataBits,
  213. IN OUT EFI_STOP_BITS_TYPE *StopBits
  214. )
  215. {
  216. return EFI_UNSUPPORTED;
  217. }
  218. /**
  219. Initialize the serial device hardware.
  220. If no initialization is required, then return RETURN_SUCCESS.
  221. If the serial device was successfully initialized, then return RETURN_SUCCESS.
  222. If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
  223. @retval RETURN_SUCCESS The serial device was initialized.
  224. @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
  225. **/
  226. RETURN_STATUS
  227. EFIAPI
  228. SerialPortInitialize (
  229. VOID
  230. )
  231. {
  232. EFI_STATUS Status;
  233. EFI_HOB_GUID_TYPE *GuidHob;
  234. SPI_FLASH_DEBUG_CONTEXT *Context;
  235. GuidHob = GetFirstGuidHob (&gSpiFlashDebugHobGuid);
  236. if (GuidHob != NULL) {
  237. // Initialization only needs to occur once
  238. return EFI_SUCCESS;
  239. }
  240. //
  241. // Perform silicon specific initialization required to enable write to SPI flash.
  242. //
  243. Status = SpiServiceInit ();
  244. if (EFI_ERROR (Status)) {
  245. return EFI_DEVICE_ERROR;
  246. }
  247. Context = (SPI_FLASH_DEBUG_CONTEXT *) BuildGuidHob (&gSpiFlashDebugHobGuid, sizeof (SPI_FLASH_DEBUG_CONTEXT));
  248. if (Context == NULL) {
  249. return EFI_DEVICE_ERROR;
  250. }
  251. ZeroMem ((VOID *) Context, sizeof (SPI_FLASH_DEBUG_CONTEXT));
  252. return EFI_SUCCESS;
  253. }