PchSpi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /** @file
  2. PCH SPI PEI Library implements the SPI Host Controller Compatibility Interface.
  3. @copyright
  4. Copyright 2004 - 2021 Intel Corporation. <BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <Library/IoLib.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/MemoryAllocationLib.h>
  10. #include <Library/PeiServicesLib.h>
  11. #include <Ppi/Spi.h>
  12. #include <Ppi/PchPolicy.h>
  13. #include <Private/Library/PchSpiCommonLib.h>
  14. #include <Library/PchMultiPchBase.h>
  15. typedef struct {
  16. EFI_PEI_PPI_DESCRIPTOR PpiDescriptor;
  17. SPI_INSTANCE SpiInstance;
  18. } PEI_SPI_INSTANCE;
  19. /**
  20. Read data from the flash part.
  21. @param[in] This Pointer to the PCH_SPI_PROTOCOL instance.
  22. @param[in] FlashRegionType The Flash Region type for flash cycle which is listed in the Descriptor.
  23. @param[in] Address The Flash Linear Address must fall within a region for which BIOS has access permissions.
  24. @param[in] ByteCount Number of bytes in the data portion of the SPI cycle.
  25. @param[out] Buffer The Pointer to caller-allocated buffer containing the dada received.
  26. It is the caller's responsibility to make sure Buffer is large enough for the total number of bytes read.
  27. @retval EFI_SUCCESS Command succeed.
  28. @retval EFI_INVALID_PARAMETER The parameters specified are not valid.
  29. @retval EFI_DEVICE_ERROR Device error, command aborts abnormally.
  30. **/
  31. EFI_STATUS
  32. EFIAPI
  33. SpiProtocolFlashRead (
  34. IN PCH_SPI_PROTOCOL *This,
  35. IN FLASH_REGION_TYPE FlashRegionType,
  36. IN UINT32 Address,
  37. IN UINT32 ByteCount,
  38. OUT UINT8 *Buffer
  39. )
  40. {
  41. return EFI_UNSUPPORTED;
  42. }
  43. /**
  44. Write data to the flash part.
  45. @param[in] This Pointer to the PCH_SPI_PROTOCOL instance.
  46. @param[in] FlashRegionType The Flash Region type for flash cycle which is listed in the Descriptor.
  47. @param[in] Address The Flash Linear Address must fall within a region for which BIOS has access permissions.
  48. @param[in] ByteCount Number of bytes in the data portion of the SPI cycle.
  49. @param[in] Buffer Pointer to caller-allocated buffer containing the data sent during the SPI cycle.
  50. @retval EFI_SUCCESS Command succeed.
  51. @retval EFI_INVALID_PARAMETER The parameters specified are not valid.
  52. @retval EFI_DEVICE_ERROR Device error, command aborts abnormally.
  53. **/
  54. EFI_STATUS
  55. EFIAPI
  56. SpiProtocolFlashWrite (
  57. IN PCH_SPI_PROTOCOL *This,
  58. IN FLASH_REGION_TYPE FlashRegionType,
  59. IN UINT32 Address,
  60. IN UINT32 ByteCount,
  61. IN UINT8 *Buffer
  62. )
  63. {
  64. return EFI_UNSUPPORTED;
  65. }
  66. /**
  67. Erase some area on the flash part.
  68. @param[in] This Pointer to the PCH_SPI_PROTOCOL instance.
  69. @param[in] FlashRegionType The Flash Region type for flash cycle which is listed in the Descriptor.
  70. @param[in] Address The Flash Linear Address must fall within a region for which BIOS has access permissions.
  71. @param[in] ByteCount Number of bytes in the data portion of the SPI cycle.
  72. @retval EFI_SUCCESS Command succeed.
  73. @retval EFI_INVALID_PARAMETER The parameters specified are not valid.
  74. @retval EFI_DEVICE_ERROR Device error, command aborts abnormally.
  75. **/
  76. EFI_STATUS
  77. EFIAPI
  78. SpiProtocolFlashErase (
  79. IN PCH_SPI_PROTOCOL *This,
  80. IN FLASH_REGION_TYPE FlashRegionType,
  81. IN UINT32 Address,
  82. IN UINT32 ByteCount
  83. )
  84. {
  85. return EFI_UNSUPPORTED;
  86. }
  87. /**
  88. Read SFDP data from the flash part.
  89. @param[in] This Pointer to the PCH_SPI_PROTOCOL instance.
  90. @param[in] ComponentNumber The Componen Number for chip select
  91. @param[in] Address The starting byte address for SFDP data read.
  92. @param[in] ByteCount Number of bytes in SFDP data portion of the SPI cycle
  93. @param[out] SfdpData The Pointer to caller-allocated buffer containing the SFDP data received
  94. It is the caller's responsibility to make sure Buffer is large enough for the total number of bytes read
  95. @retval EFI_SUCCESS Command succeed.
  96. @retval EFI_INVALID_PARAMETER The parameters specified are not valid.
  97. @retval EFI_DEVICE_ERROR Device error, command aborts abnormally.
  98. **/
  99. EFI_STATUS
  100. EFIAPI
  101. SpiProtocolFlashReadSfdp (
  102. IN PCH_SPI_PROTOCOL *This,
  103. IN UINT8 ComponentNumber,
  104. IN UINT32 Address,
  105. IN UINT32 ByteCount,
  106. OUT UINT8 *SfdpData
  107. )
  108. {
  109. return EFI_UNSUPPORTED;
  110. }
  111. /**
  112. Read Jedec Id from the flash part.
  113. @param[in] This Pointer to the PCH_SPI_PROTOCOL instance.
  114. @param[in] ComponentNumber The Componen Number for chip select
  115. @param[in] ByteCount Number of bytes in JedecId data portion of the SPI cycle, the data size is 3 typically
  116. @param[out] JedecId The Pointer to caller-allocated buffer containing JEDEC ID received
  117. It is the caller's responsibility to make sure Buffer is large enough for the total number of bytes read.
  118. @retval EFI_SUCCESS Command succeed.
  119. @retval EFI_INVALID_PARAMETER The parameters specified are not valid.
  120. @retval EFI_DEVICE_ERROR Device error, command aborts abnormally.
  121. **/
  122. EFI_STATUS
  123. EFIAPI
  124. SpiProtocolFlashReadJedecId (
  125. IN PCH_SPI_PROTOCOL *This,
  126. IN UINT8 ComponentNumber,
  127. IN UINT32 ByteCount,
  128. OUT UINT8 *JedecId
  129. )
  130. {
  131. return EFI_UNSUPPORTED;
  132. }
  133. /**
  134. Write the status register in the flash part.
  135. @param[in] This Pointer to the PCH_SPI_PROTOCOL instance.
  136. @param[in] ByteCount Number of bytes in Status data portion of the SPI cycle, the data size is 1 typically
  137. @param[in] StatusValue The Pointer to caller-allocated buffer containing the value of Status register writing
  138. @retval EFI_SUCCESS Command succeed.
  139. @retval EFI_INVALID_PARAMETER The parameters specified are not valid.
  140. @retval EFI_DEVICE_ERROR Device error, command aborts abnormally.
  141. **/
  142. EFI_STATUS
  143. EFIAPI
  144. SpiProtocolFlashWriteStatus (
  145. IN PCH_SPI_PROTOCOL *This,
  146. IN UINT32 ByteCount,
  147. IN UINT8 *StatusValue
  148. )
  149. {
  150. return EFI_UNSUPPORTED;
  151. }
  152. /**
  153. Read status register in the flash part.
  154. @param[in] This Pointer to the PCH_SPI_PROTOCOL instance.
  155. @param[in] ByteCount Number of bytes in Status data portion of the SPI cycle, the data size is 1 typically
  156. @param[out] StatusValue The Pointer to caller-allocated buffer containing the value of Status register received.
  157. @retval EFI_SUCCESS Command succeed.
  158. @retval EFI_INVALID_PARAMETER The parameters specified are not valid.
  159. @retval EFI_DEVICE_ERROR Device error, command aborts abnormally.
  160. **/
  161. EFI_STATUS
  162. EFIAPI
  163. SpiProtocolFlashReadStatus (
  164. IN PCH_SPI_PROTOCOL *This,
  165. IN UINT32 ByteCount,
  166. OUT UINT8 *StatusValue
  167. )
  168. {
  169. return EFI_UNSUPPORTED;
  170. }
  171. /**
  172. Get the SPI region base and size, based on the enum type
  173. @param[in] This Pointer to the PCH_SPI_PROTOCOL instance.
  174. @param[in] FlashRegionType The Flash Region type for for the base address which is listed in the Descriptor.
  175. @param[out] BaseAddress The Flash Linear Address for the Region 'n' Base
  176. @param[out] RegionSize The size for the Region 'n'
  177. @retval EFI_SUCCESS Read success
  178. @retval EFI_INVALID_PARAMETER Invalid region type given
  179. @retval EFI_DEVICE_ERROR The region is not used
  180. **/
  181. EFI_STATUS
  182. EFIAPI
  183. SpiProtocolGetRegionAddress (
  184. IN PCH_SPI_PROTOCOL *This,
  185. IN FLASH_REGION_TYPE FlashRegionType,
  186. OUT UINT32 *BaseAddress,
  187. OUT UINT32 *RegionSize
  188. )
  189. {
  190. return EFI_UNSUPPORTED;
  191. }
  192. /**
  193. Read PCH Soft Strap Values
  194. @param[in] This Pointer to the PCH_SPI_PROTOCOL instance.
  195. @param[in] SoftStrapAddr PCH Soft Strap address offset from FPSBA.
  196. @param[in] ByteCount Number of bytes in SoftStrap data portion of the SPI cycle
  197. @param[out] SoftStrapValue The Pointer to caller-allocated buffer containing PCH Soft Strap Value.
  198. If the value of ByteCount is 0, the data type of SoftStrapValue should be UINT16 and SoftStrapValue will be PCH Soft Strap Length
  199. It is the caller's responsibility to make sure Buffer is large enough for the total number of bytes read.
  200. @retval EFI_SUCCESS Command succeed.
  201. @retval EFI_INVALID_PARAMETER The parameters specified are not valid.
  202. @retval EFI_DEVICE_ERROR Device error, command aborts abnormally.
  203. **/
  204. EFI_STATUS
  205. EFIAPI
  206. SpiProtocolReadPchSoftStrap (
  207. IN PCH_SPI_PROTOCOL *This,
  208. IN UINT32 SoftStrapAddr,
  209. IN UINT32 ByteCount,
  210. OUT VOID *SoftStrapValue
  211. )
  212. {
  213. return EFI_UNSUPPORTED;
  214. }
  215. /**
  216. Read CPU Soft Strap Values
  217. @param[in] This Pointer to the PCH_SPI_PROTOCOL instance.
  218. @param[in] SoftStrapAddr CPU Soft Strap address offset from FCPUSBA.
  219. @param[in] ByteCount Number of bytes in SoftStrap data portion of the SPI cycle.
  220. @param[out] SoftStrapValue The Pointer to caller-allocated buffer containing CPU Soft Strap Value.
  221. If the value of ByteCount is 0, the data type of SoftStrapValue should be UINT16 and SoftStrapValue will be PCH Soft Strap Length
  222. It is the caller's responsibility to make sure Buffer is large enough for the total number of bytes read.
  223. @retval EFI_SUCCESS Command succeed.
  224. @retval EFI_INVALID_PARAMETER The parameters specified are not valid.
  225. @retval EFI_DEVICE_ERROR Device error, command aborts abnormally.
  226. **/
  227. EFI_STATUS
  228. EFIAPI
  229. SpiProtocolReadCpuSoftStrap (
  230. IN PCH_SPI_PROTOCOL *This,
  231. IN UINT32 SoftStrapAddr,
  232. IN UINT32 ByteCount,
  233. OUT VOID *SoftStrapValue
  234. )
  235. {
  236. return EFI_UNSUPPORTED;
  237. }
  238. /**
  239. Initialize an SPI protocol instance.
  240. @param[in] PchId The PCH Id (0 - Legacy PCH, 1 ... n - Non-Legacy PCH)
  241. @param[in] SpiInstance Pointer to SpiInstance to initialize
  242. @retval EFI_SUCCESS The protocol instance was properly initialized
  243. @exception EFI_UNSUPPORTED The PCH is not supported by this module
  244. **/
  245. EFI_STATUS
  246. SpiProtocolConstructor (
  247. IN UINT8 PchId,
  248. IN SPI_INSTANCE *SpiInstance
  249. )
  250. {
  251. //
  252. // Initialize the SPI protocol instance
  253. //
  254. SpiInstance->Signature = PCH_SPI_PRIVATE_DATA_SIGNATURE;
  255. SpiInstance->Handle = NULL;
  256. SpiInstance->SpiProtocol.Revision = PCH_SPI_SERVICES_REVISION;
  257. SpiInstance->SpiProtocol.FlashRead = SpiProtocolFlashRead;
  258. SpiInstance->SpiProtocol.FlashWrite = SpiProtocolFlashWrite;
  259. SpiInstance->SpiProtocol.FlashErase = SpiProtocolFlashErase;
  260. SpiInstance->SpiProtocol.FlashReadSfdp = SpiProtocolFlashReadSfdp;
  261. SpiInstance->SpiProtocol.FlashReadJedecId = SpiProtocolFlashReadJedecId;
  262. SpiInstance->SpiProtocol.FlashWriteStatus = SpiProtocolFlashWriteStatus;
  263. SpiInstance->SpiProtocol.FlashReadStatus = SpiProtocolFlashReadStatus;
  264. SpiInstance->SpiProtocol.GetRegionAddress = SpiProtocolGetRegionAddress;
  265. SpiInstance->SpiProtocol.ReadPchSoftStrap = SpiProtocolReadPchSoftStrap;
  266. SpiInstance->SpiProtocol.ReadCpuSoftStrap = SpiProtocolReadCpuSoftStrap;
  267. return EFI_SUCCESS;
  268. }
  269. /**
  270. Installs PCH SPI PPI
  271. @retval EFI_SUCCESS PCH SPI PPI is installed successfully
  272. @retval EFI_OUT_OF_RESOURCES Can't allocate pool
  273. **/
  274. EFI_STATUS
  275. EFIAPI
  276. InstallPchSpi (
  277. VOID
  278. )
  279. {
  280. EFI_STATUS Status;
  281. PEI_SPI_INSTANCE *PeiSpiInstance;
  282. SPI_INSTANCE *SpiInstance;
  283. DEBUG ((DEBUG_INFO, "InstallPchSpi() Start\n"));
  284. //
  285. // PCI Enumeratuion is not done till later in DXE
  286. // Initlialize SPI BAR0 to a default value till enumeration is done
  287. // also enable memory space decoding for SPI
  288. //
  289. PeiSpiInstance = (PEI_SPI_INSTANCE *) AllocateZeroPool (sizeof (PEI_SPI_INSTANCE));
  290. if (NULL == PeiSpiInstance) {
  291. return EFI_OUT_OF_RESOURCES;
  292. }
  293. SpiInstance = &(PeiSpiInstance->SpiInstance);
  294. SpiProtocolConstructor (PCH_LEGACY_ID, SpiInstance);
  295. PeiSpiInstance->PpiDescriptor.Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
  296. PeiSpiInstance->PpiDescriptor.Guid = &gPchSpiPpiGuid;
  297. PeiSpiInstance->PpiDescriptor.Ppi = &(SpiInstance->SpiProtocol);
  298. ///
  299. /// Install the SPI PPI
  300. ///
  301. Status = PeiServicesInstallPpi (&PeiSpiInstance->PpiDescriptor);
  302. ASSERT_EFI_ERROR (Status);
  303. DEBUG ((DEBUG_INFO, "SPI PPI Installed\n"));
  304. DEBUG ((DEBUG_INFO, "InstallPchSpi() End\n"));
  305. return Status;
  306. }
  307. /**
  308. Board Install Dummy SPI Ppi entry point.
  309. @param FileHandle Handle of the file being invoked.
  310. Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().
  311. @param PeiServices General purpose services available to every PEIM.
  312. @retval EFI_SUCCESS
  313. **/
  314. EFI_STATUS
  315. EFIAPI
  316. DummySpiPpiEntryPoint (
  317. IN EFI_PEI_FILE_HANDLE FileHandle,
  318. IN CONST EFI_PEI_SERVICES **PeiServices
  319. )
  320. {
  321. InstallPchSpi ();
  322. return EFI_SUCCESS;
  323. }