12#ifndef EFI_STORAGE_INT_FLASH_DRIVER
13#define EFI_STORAGE_INT_FLASH_DRIVER TRUE
16#if defined(EFI_BOOTLOADER) || EFI_STORAGE_INT_FLASH_DRIVER
23 #define FLASH_CR FLASH->CR2
24 #define FLASH_SR FLASH->SR2
25 #define FLASH_KEYR FLASH->KEYR2
28 #define FLASH_CR_STRT FLASH_CR_START
32 #define FLASH_BASE 0x08100000
35 #define intFlashWaitWhileBusy() do { __DSB(); } while (FLASH_SR & FLASH_SR_QW);
37 #define FLASH_CR FLASH->CR
38 #define FLASH_SR FLASH->SR
39 #define FLASH_KEYR FLASH->KEYR
42 #define intFlashWaitWhileBusy() do { __DSB(); } while (FLASH->SR & FLASH_SR_BSY);
67 FLASH->CCR2 = 0xffffffff;
69 FLASH_SR = 0x0000ffff;
74 uint32_t sr = FLASH_SR;
77 if (sr & FLASH_SR_OPERR)
78 return FLASH_RETURN_OPERROR;
80 if (sr & FLASH_SR_WRPERR)
81 return FLASH_RETURN_WPERROR;
83 if (sr & FLASH_SR_PGAERR)
84 return FLASH_RETURN_ALIGNERROR;
87 if (sr & FLASH_SR_PGPERR)
88 return FLASH_RETURN_PPARALLERROR;
91 if (sr & FLASH_SR_ERSERR)
92 return FLASH_RETURN_ESEQERROR;
95 if (sr & FLASH_SR_PGSERR)
96 return FLASH_RETURN_PSEQERROR;
109 if (!(FLASH_CR & FLASH_CR_LOCK))
113 FLASH_KEYR = 0x45670123;
114 FLASH_KEYR = 0xCDEF89AB;
117 if (FLASH_CR & FLASH_CR_LOCK)
125#define intFlashLock() { FLASH_CR |= FLASH_CR_LOCK; }
129#ifdef FLASH_OPTCR_nDBANK
131 return (FLASH->OPTCR & FLASH_OPTCR_nDBANK) == 0;
150 uint8_t sectorRegIdx = sector;
161 sectorRegIdx |= 0x10;
167 return FLASH_RETURN_NO_PERMISSION;
170 intFlashWaitWhileBusy();
176 FLASH_CR &= ~FLASH_CR_PSIZE_MASK;
177 FLASH_CR |= FLASH_CR_PSIZE_VALUE;
189 FLASH_CR &= ~FLASH_CR_SNB_Msk;
190 FLASH_CR |= (sectorRegIdx << FLASH_CR_SNB_Pos) & FLASH_CR_SNB_Msk;
192 FLASH_CR |= FLASH_CR_SER;
194 FLASH_CR |= FLASH_CR_STRT;
197 intFlashWaitWhileBusy();
200 FLASH_CR &= ~FLASH_CR_SER;
212 return FLASH_RETURN_BAD_FLASH;
220 while (address <= endAddress) {
236 SCB_InvalidateDCache_by_Addr((uint32_t*)address,
size);
249 if (*(
char*) address != 0xFF)
269 if (*(
volatile char*) address != *
buffer)
283 SCB_InvalidateDCache_by_Addr((uint32_t*)source,
size);
286 memcpy(destination, (
char*) source,
size);
294 return FLASH_RETURN_NO_PERMISSION;
297 intFlashWaitWhileBusy();
300 FLASH_CR &= ~FLASH_CR_PSIZE_MASK;
301 FLASH_CR |= FLASH_CR_PSIZE_VALUE;
304 size_t flashWordCount = (
size - 1) / 32 + 1;
310 for (
size_t word = 0; word < flashWordCount; word++) {
312 FLASH_CR |= FLASH_CR_PG;
319 for (
size_t i = 0; i < 8; i++) {
320 *pWrite++ = *pRead++;
328 intFlashWaitWhileBusy();
331 FLASH_CR &= ~FLASH_CR_PG;
350 FLASH->CR |= FLASH_CR_PG;
362 intFlashWaitWhileBusy();
365 FLASH->CR &= ~FLASH_CR_PG;
375 return FLASH_RETURN_NO_PERMISSION;
378 intFlashWaitWhileBusy();
381 FLASH->CR &= ~FLASH_CR_PSIZE_MASK;
382 FLASH->CR |= FLASH_CR_PSIZE_VALUE;
385 size_t alignOffset = address %
sizeof(
flashdata_t);
387 if (alignOffset != 0) {
392 flashaddr_t alignedFlashAddress = address - alignOffset;
398 size_t chunkSize =
sizeof(
flashdata_t) - alignOffset;
399 if (chunkSize >
size)
403 memcpy((
char*) &tmp + alignOffset,
buffer, chunkSize);
411 address += chunkSize;
return FLASH_RETURN_SUCCESS
int intFlashWrite(flashaddr_t address, const char *buffer, size_t size)
Copy data from a buffer to the flash memory.
bool intFlashIsErased(flashaddr_t address, size_t size)
Check if the size bytes of flash memory starting at address are erased.
int intFlashRead(flashaddr_t source, char *destination, size_t size)
Copy data from the flash memory to a destination.
bool intFlashCompare(flashaddr_t address, const char *buffer, size_t size)
Check if the data in buffer are identical to the one in flash memory.
int intFlashErase(flashaddr_t address, size_t size)
Erase the sectors containing the span of size bytes starting at address.
uintptr_t flashaddr_t
Address in the flash memory.
size_t flashSectorSize(flashsector_t sector)
Get the size of sector.
uint8_t flashsector_t
Index of a sector.
flashsector_t intFlashSectorAt(flashaddr_t address)
static bool isDualBank(void)
static int intFlashCheckErrors()
static int intFlashSectorErase(flashsector_t sector)
Erase the flash sector.
flashaddr_t intFlashSectorEnd(flashsector_t sector)
static void intFlashClearErrors()
flashaddr_t intFlashSectorBegin(flashsector_t sector)
static bool intFlashUnlock(void)
Unlock the flash memory for write access.
static int intFlashWriteData(flashaddr_t address, const flashdata_t data)
static BigBufferHandle buffer