SpiFlashCommon.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /** @file
  2. Wrap EFI_SPI_PROTOCOL to provide some library level interfaces
  3. for module use.
  4. Copyright (c) 2019 Intel Corporation. All rights reserved. <BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <Library/SpiFlashCommonLib.h>
  8. #include <Library/IoLib.h>
  9. #include <Library/PciLib.h>
  10. #include <Protocol/Spi.h>
  11. EFI_SPI_PROTOCOL *mSpiProtocol;
  12. //
  13. // FlashAreaBaseAddress and Size for boottime and runtime usage.
  14. //
  15. UINTN mFlashAreaBaseAddress = 0;
  16. UINTN mFlashAreaSize = 0;
  17. /**
  18. Enable block protection on the Serial Flash device.
  19. @retval EFI_SUCCESS Opertion is successful.
  20. @retval EFI_DEVICE_ERROR If there is any device errors.
  21. **/
  22. EFI_STATUS
  23. EFIAPI
  24. SpiFlashLock (
  25. VOID
  26. )
  27. {
  28. return EFI_SUCCESS;
  29. }
  30. /**
  31. Read NumBytes bytes of data from the address specified by
  32. PAddress into Buffer.
  33. @param[in] Address The starting physical address of the read.
  34. @param[in,out] NumBytes On input, the number of bytes to read. On output, the number
  35. of bytes actually read.
  36. @param[out] Buffer The destination data buffer for the read.
  37. @retval EFI_SUCCESS Opertion is successful.
  38. @retval EFI_DEVICE_ERROR If there is any device errors.
  39. **/
  40. EFI_STATUS
  41. EFIAPI
  42. SpiFlashRead (
  43. IN UINTN Address,
  44. IN OUT UINT32 *NumBytes,
  45. OUT UINT8 *Buffer
  46. )
  47. {
  48. ASSERT ((NumBytes != NULL) && (Buffer != NULL));
  49. if ((NumBytes == NULL) || (Buffer == NULL)) {
  50. return EFI_INVALID_PARAMETER;
  51. }
  52. //
  53. // This function is implemented specifically for those platforms
  54. // at which the SPI device is memory mapped for read. So this
  55. // function just do a memory copy for Spi Flash Read.
  56. //
  57. CopyMem (Buffer, (VOID *) Address, *NumBytes);
  58. return EFI_SUCCESS;
  59. }
  60. /**
  61. Write NumBytes bytes of data from Buffer to the address specified by
  62. PAddresss.
  63. @param[in] Address The starting physical address of the write.
  64. @param[in,out] NumBytes On input, the number of bytes to write. On output,
  65. the actual number of bytes written.
  66. @param[in] Buffer The source data buffer for the write.
  67. @retval EFI_SUCCESS Opertion is successful.
  68. @retval EFI_DEVICE_ERROR If there is any device errors.
  69. **/
  70. EFI_STATUS
  71. EFIAPI
  72. SpiFlashWrite (
  73. IN UINTN Address,
  74. IN OUT UINT32 *NumBytes,
  75. IN UINT8 *Buffer
  76. )
  77. {
  78. EFI_STATUS Status;
  79. UINTN Offset;
  80. UINT32 Length;
  81. UINT32 RemainingBytes;
  82. ASSERT ((NumBytes != NULL) && (Buffer != NULL));
  83. if ((NumBytes == NULL) || (Buffer == NULL)) {
  84. return EFI_INVALID_PARAMETER;
  85. }
  86. ASSERT (Address >= mFlashAreaBaseAddress);
  87. Offset = Address - mFlashAreaBaseAddress;
  88. ASSERT ((*NumBytes + Offset) <= mFlashAreaSize);
  89. Status = EFI_SUCCESS;
  90. RemainingBytes = *NumBytes;
  91. while (RemainingBytes > 0) {
  92. if (RemainingBytes > SECTOR_SIZE_4KB) {
  93. Length = SECTOR_SIZE_4KB;
  94. } else {
  95. Length = RemainingBytes;
  96. }
  97. Status = mSpiProtocol->FlashWrite (
  98. mSpiProtocol,
  99. FlashRegionBios,
  100. (UINT32) Offset,
  101. Length,
  102. Buffer
  103. );
  104. if (EFI_ERROR (Status)) {
  105. break;
  106. }
  107. RemainingBytes -= Length;
  108. Offset += Length;
  109. Buffer += Length;
  110. }
  111. //
  112. // Actual number of bytes written
  113. //
  114. *NumBytes -= RemainingBytes;
  115. return Status;
  116. }
  117. /**
  118. Erase the block starting at Address.
  119. @param[in] Address The starting physical address of the block to be erased.
  120. This library assume that caller garantee that the PAddress
  121. is at the starting address of this block.
  122. @param[in] NumBytes On input, the number of bytes of the logical block to be erased.
  123. On output, the actual number of bytes erased.
  124. @retval EFI_SUCCESS. Opertion is successful.
  125. @retval EFI_DEVICE_ERROR If there is any device errors.
  126. **/
  127. EFI_STATUS
  128. EFIAPI
  129. SpiFlashBlockErase (
  130. IN UINTN Address,
  131. IN UINTN *NumBytes
  132. )
  133. {
  134. EFI_STATUS Status;
  135. UINTN Offset;
  136. UINTN RemainingBytes;
  137. ASSERT (NumBytes != NULL);
  138. if (NumBytes == NULL) {
  139. return EFI_INVALID_PARAMETER;
  140. }
  141. ASSERT (Address >= mFlashAreaBaseAddress);
  142. Offset = Address - mFlashAreaBaseAddress;
  143. ASSERT ((*NumBytes % SECTOR_SIZE_4KB) == 0);
  144. ASSERT ((*NumBytes + Offset) <= mFlashAreaSize);
  145. Status = EFI_SUCCESS;
  146. RemainingBytes = *NumBytes;
  147. Status = mSpiProtocol->FlashErase (
  148. mSpiProtocol,
  149. FlashRegionBios,
  150. (UINT32) Offset,
  151. (UINT32) RemainingBytes
  152. );
  153. return Status;
  154. }