NorFlash.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /** @file NorFlash.h
  2. Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #ifndef __NOR_FLASH_H__
  6. #define __NOR_FLASH_H__
  7. #include <Base.h>
  8. #include <PiDxe.h>
  9. #include <Guid/EventGroup.h>
  10. #include <Protocol/BlockIo.h>
  11. #include <Protocol/DiskIo.h>
  12. #include <Protocol/FirmwareVolumeBlock.h>
  13. #include <Library/DebugLib.h>
  14. #include <Library/IoLib.h>
  15. #include <Library/NorFlashPlatformLib.h>
  16. #include <Library/UefiLib.h>
  17. #include <Library/UefiRuntimeLib.h>
  18. #define NOR_FLASH_ERASE_RETRY 10
  19. // Device access macros
  20. // These are necessary because we use 2 x 16bit parts to make up 32bit data
  21. #define HIGH_16_BITS 0xFFFF0000
  22. #define LOW_16_BITS 0x0000FFFF
  23. #define LOW_8_BITS 0x000000FF
  24. #define FOLD_32BIT_INTO_16BIT(value) ( ( value >> 16 ) | ( value & LOW_16_BITS ) )
  25. #define GET_LOW_BYTE(value) ( value & LOW_8_BITS )
  26. #define GET_HIGH_BYTE(value) ( GET_LOW_BYTE( value >> 16 ) )
  27. // Each command must be sent simultaneously to both chips,
  28. // i.e. at the lower 16 bits AND at the higher 16 bits
  29. #define CREATE_NOR_ADDRESS(BaseAddr, OffsetAddr) ((BaseAddr) + ((OffsetAddr) << 2))
  30. #define CREATE_DUAL_CMD(Cmd) ( ( Cmd << 16) | ( Cmd & LOW_16_BITS) )
  31. #define SEND_NOR_COMMAND(BaseAddr, Offset, Cmd) MmioWrite32 (CREATE_NOR_ADDRESS(BaseAddr,Offset), CREATE_DUAL_CMD(Cmd))
  32. #define GET_NOR_BLOCK_ADDRESS(BaseAddr, Lba, LbaSize) ( BaseAddr + (UINTN)((Lba) * LbaSize) )
  33. // Status Register Bits
  34. #define P30_SR_BIT_WRITE (BIT7 << 16 | BIT7)
  35. #define P30_SR_BIT_ERASE_SUSPEND (BIT6 << 16 | BIT6)
  36. #define P30_SR_BIT_ERASE (BIT5 << 16 | BIT5)
  37. #define P30_SR_BIT_PROGRAM (BIT4 << 16 | BIT4)
  38. #define P30_SR_BIT_VPP (BIT3 << 16 | BIT3)
  39. #define P30_SR_BIT_PROGRAM_SUSPEND (BIT2 << 16 | BIT2)
  40. #define P30_SR_BIT_BLOCK_LOCKED (BIT1 << 16 | BIT1)
  41. #define P30_SR_BIT_BEFP (BIT0 << 16 | BIT0)
  42. // Device Commands for Intel StrataFlash(R) Embedded Memory (P30) Family
  43. // On chip buffer size for buffered programming operations
  44. // There are 2 chips, each chip can buffer up to 32 (16-bit)words, and each word is 2 bytes.
  45. // Therefore the total size of the buffer is 2 x 32 x 2 = 128 bytes
  46. #define P30_MAX_BUFFER_SIZE_IN_BYTES ((UINTN)128)
  47. #define P30_MAX_BUFFER_SIZE_IN_WORDS (P30_MAX_BUFFER_SIZE_IN_BYTES/((UINTN)4))
  48. #define MAX_BUFFERED_PROG_ITERATIONS 10000000
  49. #define BOUNDARY_OF_32_WORDS 0x7F
  50. // CFI Addresses
  51. #define P30_CFI_ADDR_QUERY_UNIQUE_QRY 0x10
  52. #define P30_CFI_ADDR_VENDOR_ID 0x13
  53. // CFI Data
  54. #define CFI_QRY 0x00595251
  55. // READ Commands
  56. #define P30_CMD_READ_DEVICE_ID 0x0090
  57. #define P30_CMD_READ_STATUS_REGISTER 0x0070
  58. #define P30_CMD_CLEAR_STATUS_REGISTER 0x0050
  59. #define P30_CMD_READ_ARRAY 0x00FF
  60. #define P30_CMD_READ_CFI_QUERY 0x0098
  61. // WRITE Commands
  62. #define P30_CMD_WORD_PROGRAM_SETUP 0x0040
  63. #define P30_CMD_ALTERNATE_WORD_PROGRAM_SETUP 0x0010
  64. #define P30_CMD_BUFFERED_PROGRAM_SETUP 0x00E8
  65. #define P30_CMD_BUFFERED_PROGRAM_CONFIRM 0x00D0
  66. #define P30_CMD_BEFP_SETUP 0x0080
  67. #define P30_CMD_BEFP_CONFIRM 0x00D0
  68. // ERASE Commands
  69. #define P30_CMD_BLOCK_ERASE_SETUP 0x0020
  70. #define P30_CMD_BLOCK_ERASE_CONFIRM 0x00D0
  71. // SUSPEND Commands
  72. #define P30_CMD_PROGRAM_OR_ERASE_SUSPEND 0x00B0
  73. #define P30_CMD_SUSPEND_RESUME 0x00D0
  74. // BLOCK LOCKING / UNLOCKING Commands
  75. #define P30_CMD_LOCK_BLOCK_SETUP 0x0060
  76. #define P30_CMD_LOCK_BLOCK 0x0001
  77. #define P30_CMD_UNLOCK_BLOCK 0x00D0
  78. #define P30_CMD_LOCK_DOWN_BLOCK 0x002F
  79. // PROTECTION Commands
  80. #define P30_CMD_PROGRAM_PROTECTION_REGISTER_SETUP 0x00C0
  81. // CONFIGURATION Commands
  82. #define P30_CMD_READ_CONFIGURATION_REGISTER_SETUP 0x0060
  83. #define P30_CMD_READ_CONFIGURATION_REGISTER 0x0003
  84. #define NOR_FLASH_SIGNATURE SIGNATURE_32('n', 'o', 'r', '0')
  85. #define INSTANCE_FROM_FVB_THIS(a) CR(a, NOR_FLASH_INSTANCE, FvbProtocol, NOR_FLASH_SIGNATURE)
  86. #define INSTANCE_FROM_BLKIO_THIS(a) CR(a, NOR_FLASH_INSTANCE, BlockIoProtocol, NOR_FLASH_SIGNATURE)
  87. #define INSTANCE_FROM_DISKIO_THIS(a) CR(a, NOR_FLASH_INSTANCE, DiskIoProtocol, NOR_FLASH_SIGNATURE)
  88. typedef struct _NOR_FLASH_INSTANCE NOR_FLASH_INSTANCE;
  89. #pragma pack (1)
  90. typedef struct {
  91. VENDOR_DEVICE_PATH Vendor;
  92. UINT8 Index;
  93. EFI_DEVICE_PATH_PROTOCOL End;
  94. } NOR_FLASH_DEVICE_PATH;
  95. #pragma pack ()
  96. struct _NOR_FLASH_INSTANCE {
  97. UINT32 Signature;
  98. EFI_HANDLE Handle;
  99. UINTN DeviceBaseAddress;
  100. UINTN RegionBaseAddress;
  101. UINTN Size;
  102. EFI_LBA StartLba;
  103. EFI_BLOCK_IO_PROTOCOL BlockIoProtocol;
  104. EFI_BLOCK_IO_MEDIA Media;
  105. EFI_DISK_IO_PROTOCOL DiskIoProtocol;
  106. EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL FvbProtocol;
  107. VOID *ShadowBuffer;
  108. NOR_FLASH_DEVICE_PATH DevicePath;
  109. };
  110. EFI_STATUS
  111. NorFlashReadCfiData (
  112. IN UINTN DeviceBaseAddress,
  113. IN UINTN CFI_Offset,
  114. IN UINT32 NumberOfBytes,
  115. OUT UINT32 *Data
  116. );
  117. EFI_STATUS
  118. NorFlashWriteBuffer (
  119. IN NOR_FLASH_INSTANCE *Instance,
  120. IN UINTN TargetAddress,
  121. IN UINTN BufferSizeInBytes,
  122. IN UINT32 *Buffer
  123. );
  124. //
  125. // BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.Reset
  126. //
  127. EFI_STATUS
  128. EFIAPI
  129. NorFlashBlockIoReset (
  130. IN EFI_BLOCK_IO_PROTOCOL *This,
  131. IN BOOLEAN ExtendedVerification
  132. );
  133. //
  134. // BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.ReadBlocks
  135. //
  136. EFI_STATUS
  137. EFIAPI
  138. NorFlashBlockIoReadBlocks (
  139. IN EFI_BLOCK_IO_PROTOCOL *This,
  140. IN UINT32 MediaId,
  141. IN EFI_LBA Lba,
  142. IN UINTN BufferSizeInBytes,
  143. OUT VOID *Buffer
  144. );
  145. //
  146. // BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.WriteBlocks
  147. //
  148. EFI_STATUS
  149. EFIAPI
  150. NorFlashBlockIoWriteBlocks (
  151. IN EFI_BLOCK_IO_PROTOCOL *This,
  152. IN UINT32 MediaId,
  153. IN EFI_LBA Lba,
  154. IN UINTN BufferSizeInBytes,
  155. IN VOID *Buffer
  156. );
  157. //
  158. // BlockIO Protocol function EFI_BLOCK_IO_PROTOCOL.FlushBlocks
  159. //
  160. EFI_STATUS
  161. EFIAPI
  162. NorFlashBlockIoFlushBlocks (
  163. IN EFI_BLOCK_IO_PROTOCOL *This
  164. );
  165. //
  166. // DiskIO Protocol function EFI_DISK_IO_PROTOCOL.ReadDisk
  167. //
  168. EFI_STATUS
  169. EFIAPI
  170. NorFlashDiskIoReadDisk (
  171. IN EFI_DISK_IO_PROTOCOL *This,
  172. IN UINT32 MediaId,
  173. IN UINT64 Offset,
  174. IN UINTN BufferSize,
  175. OUT VOID *Buffer
  176. );
  177. //
  178. // DiskIO Protocol function EFI_DISK_IO_PROTOCOL.WriteDisk
  179. //
  180. EFI_STATUS
  181. EFIAPI
  182. NorFlashDiskIoWriteDisk (
  183. IN EFI_DISK_IO_PROTOCOL *This,
  184. IN UINT32 MediaId,
  185. IN UINT64 Offset,
  186. IN UINTN BufferSize,
  187. IN VOID *Buffer
  188. );
  189. //
  190. // NorFlashFvbDxe.c
  191. //
  192. EFI_STATUS
  193. EFIAPI
  194. FvbGetAttributes (
  195. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  196. OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  197. );
  198. EFI_STATUS
  199. EFIAPI
  200. FvbSetAttributes (
  201. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  202. IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
  203. );
  204. EFI_STATUS
  205. EFIAPI
  206. FvbGetPhysicalAddress (
  207. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  208. OUT EFI_PHYSICAL_ADDRESS *Address
  209. );
  210. EFI_STATUS
  211. EFIAPI
  212. FvbGetBlockSize (
  213. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  214. IN EFI_LBA Lba,
  215. OUT UINTN *BlockSize,
  216. OUT UINTN *NumberOfBlocks
  217. );
  218. EFI_STATUS
  219. EFIAPI
  220. FvbRead (
  221. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  222. IN EFI_LBA Lba,
  223. IN UINTN Offset,
  224. IN OUT UINTN *NumBytes,
  225. IN OUT UINT8 *Buffer
  226. );
  227. EFI_STATUS
  228. EFIAPI
  229. FvbWrite (
  230. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  231. IN EFI_LBA Lba,
  232. IN UINTN Offset,
  233. IN OUT UINTN *NumBytes,
  234. IN UINT8 *Buffer
  235. );
  236. EFI_STATUS
  237. EFIAPI
  238. FvbEraseBlocks (
  239. IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,
  240. ...
  241. );
  242. EFI_STATUS
  243. ValidateFvHeader (
  244. IN NOR_FLASH_INSTANCE *Instance
  245. );
  246. EFI_STATUS
  247. InitializeFvAndVariableStoreHeaders (
  248. IN NOR_FLASH_INSTANCE *Instance
  249. );
  250. VOID
  251. EFIAPI
  252. FvbVirtualNotifyEvent (
  253. IN EFI_EVENT Event,
  254. IN VOID *Context
  255. );
  256. //
  257. // NorFlashDxe.c
  258. //
  259. EFI_STATUS
  260. NorFlashWriteFullBlock (
  261. IN NOR_FLASH_INSTANCE *Instance,
  262. IN EFI_LBA Lba,
  263. IN UINT32 *DataBuffer,
  264. IN UINT32 BlockSizeInWords
  265. );
  266. EFI_STATUS
  267. NorFlashUnlockAndEraseSingleBlock (
  268. IN NOR_FLASH_INSTANCE *Instance,
  269. IN UINTN BlockAddress
  270. );
  271. EFI_STATUS
  272. NorFlashCreateInstance (
  273. IN UINTN NorFlashDeviceBase,
  274. IN UINTN NorFlashRegionBase,
  275. IN UINTN NorFlashSize,
  276. IN UINT32 Index,
  277. IN UINT32 BlockSize,
  278. IN BOOLEAN SupportFvb,
  279. OUT NOR_FLASH_INSTANCE **NorFlashInstance
  280. );
  281. EFI_STATUS
  282. EFIAPI
  283. NorFlashFvbInitialize (
  284. IN NOR_FLASH_INSTANCE *Instance
  285. );
  286. //
  287. // NorFlash.c
  288. //
  289. EFI_STATUS
  290. NorFlashWriteSingleBlock (
  291. IN NOR_FLASH_INSTANCE *Instance,
  292. IN EFI_LBA Lba,
  293. IN UINTN Offset,
  294. IN OUT UINTN *NumBytes,
  295. IN UINT8 *Buffer
  296. );
  297. EFI_STATUS
  298. NorFlashWriteBlocks (
  299. IN NOR_FLASH_INSTANCE *Instance,
  300. IN EFI_LBA Lba,
  301. IN UINTN BufferSizeInBytes,
  302. IN VOID *Buffer
  303. );
  304. EFI_STATUS
  305. NorFlashReadBlocks (
  306. IN NOR_FLASH_INSTANCE *Instance,
  307. IN EFI_LBA Lba,
  308. IN UINTN BufferSizeInBytes,
  309. OUT VOID *Buffer
  310. );
  311. EFI_STATUS
  312. NorFlashRead (
  313. IN NOR_FLASH_INSTANCE *Instance,
  314. IN EFI_LBA Lba,
  315. IN UINTN Offset,
  316. IN UINTN BufferSizeInBytes,
  317. OUT VOID *Buffer
  318. );
  319. EFI_STATUS
  320. NorFlashWrite (
  321. IN NOR_FLASH_INSTANCE *Instance,
  322. IN EFI_LBA Lba,
  323. IN UINTN Offset,
  324. IN OUT UINTN *NumBytes,
  325. IN UINT8 *Buffer
  326. );
  327. EFI_STATUS
  328. NorFlashReset (
  329. IN NOR_FLASH_INSTANCE *Instance
  330. );
  331. EFI_STATUS
  332. NorFlashEraseSingleBlock (
  333. IN NOR_FLASH_INSTANCE *Instance,
  334. IN UINTN BlockAddress
  335. );
  336. EFI_STATUS
  337. NorFlashUnlockSingleBlockIfNecessary (
  338. IN NOR_FLASH_INSTANCE *Instance,
  339. IN UINTN BlockAddress
  340. );
  341. EFI_STATUS
  342. NorFlashWriteSingleWord (
  343. IN NOR_FLASH_INSTANCE *Instance,
  344. IN UINTN WordAddress,
  345. IN UINT32 WriteData
  346. );
  347. VOID
  348. EFIAPI
  349. NorFlashVirtualNotifyEvent (
  350. IN EFI_EVENT Event,
  351. IN VOID *Context
  352. );
  353. #endif /* __NOR_FLASH_H__ */