PeiSerialPortLibSpiFlash.c 10 KB

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