1 /******************************************************************************* 2 Copyright (C) 2016 Marvell International Ltd. 3 4 Marvell BSD License Option 5 6 If you received this File from Marvell, you may opt to use, redistribute and/or 7 modify this File under the following licensing terms. 8 Redistribution and use in source and binary forms, with or without modification, 9 are permitted provided that the following conditions are met: 10 11 * Redistributions of source code must retain the above copyright notice, 12 this list of conditions and the following disclaimer. 13 14 * Redistributions in binary form must reproduce the above copyright 15 notice, this list of conditions and the following disclaimer in the 16 documentation and/or other materials provided with the distribution. 17 18 * Neither the name of Marvell nor the names of its contributors may be 19 used to endorse or promote products derived from this software without 20 specific prior written permission. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 33 *******************************************************************************/ 34 #ifndef __MV_SPI_FLASH_H__ 35 #define __MV_SPI_FLASH_H__ 36 37 #include <Library/IoLib.h> 38 #include <Library/PcdLib.h> 39 #include <Library/UefiLib.h> 40 #include <Library/DebugLib.h> 41 #include <Library/MemoryAllocationLib.h> 42 #include <Uefi/UefiBaseType.h> 43 #include <Library/BaseMemoryLib.h> 44 #include <Library/UefiBootServicesTableLib.h> 45 46 #include <Protocol/Spi.h> 47 #include <Protocol/SpiFlash.h> 48 49 #define SPI_FLASH_SIGNATURE SIGNATURE_32 ('F', 'S', 'P', 'I') 50 51 #define CMD_READ_ID 0x9f 52 #define READ_STATUS_REG_CMD 0x0b 53 #define CMD_WRITE_ENABLE 0x06 54 #define CMD_READ_STATUS 0x05 55 #define CMD_FLAG_STATUS 0x70 56 #define CMD_WRITE_STATUS_REG 0x01 57 #define CMD_READ_ARRAY_FAST 0x0b 58 #define CMD_PAGE_PROGRAM 0x02 59 #define CMD_BANK_WRITE 0xc5 60 #define CMD_ERASE_64K 0xd8 61 #define CMD_4B_ADDR_ENABLE 0xb7 62 63 #define STATUS_REG_POLL_WIP (1 << 0) 64 #define STATUS_REG_POLL_PEC (1 << 7) 65 66 #define SPI_TRANSFER_BEGIN 0x01 // Assert CS before transfer 67 #define SPI_TRANSFER_END 0x02 // Deassert CS after transfers 68 69 #define SPI_FLASH_16MB_BOUN 0x1000000 70 71 typedef enum { 72 SPI_FLASH_READ_ID, 73 SPI_FLASH_READ, // Read from SPI flash with address 74 SPI_FLASH_WRITE, // Write to SPI flash with address 75 SPI_FLASH_ERASE, 76 SPI_FLASH_UPDATE, 77 SPI_COMMAND_MAX 78 } SPI_COMMAND; 79 80 typedef struct { 81 MARVELL_SPI_FLASH_PROTOCOL SpiFlashProtocol; 82 UINTN Signature; 83 EFI_HANDLE Handle; 84 } SPI_FLASH_INSTANCE; 85 86 EFI_STATUS 87 EFIAPI 88 SpiFlashReadId ( 89 IN SPI_DEVICE *SpiDev, 90 IN UINT32 DataByteCount, 91 IN OUT UINT8 *Buffer 92 ); 93 94 EFI_STATUS 95 SpiFlashRead ( 96 IN SPI_DEVICE *Slave, 97 IN UINT32 Offset, 98 IN UINTN Length, 99 IN VOID *Buf 100 ); 101 102 EFI_STATUS 103 SpiFlashWrite ( 104 IN SPI_DEVICE *Slave, 105 IN UINT32 Offset, 106 IN UINTN Length, 107 IN VOID *Buf 108 ); 109 110 EFI_STATUS 111 SpiFlashUpdate ( 112 IN SPI_DEVICE *Slave, 113 IN UINT32 Offset, 114 IN UINTN ByteCount, 115 IN UINT8 *Buf 116 ); 117 118 EFI_STATUS 119 SpiFlashErase ( 120 IN SPI_DEVICE *SpiDev, 121 IN UINTN Offset, 122 IN UINTN Length 123 ); 124 125 EFI_STATUS 126 EFIAPI 127 EfiSpiFlashInit ( 128 IN MARVELL_SPI_FLASH_PROTOCOL *This, 129 IN SPI_DEVICE *Slave 130 ); 131 132 #endif // __MV_SPI_FLASH_H__ 133