Add initial LFS support for IMXRT

This commit is contained in:
robert committed 2026-12-04 17:46:41 +02:00
1 parent f3189519d8
commit 010fd33858
14 files changed
+208 -26

No files matched your search

+43 -1
View File
@@ -411,7 +411,11 @@ struct mg_flexspi_nor_driver_interface {
#endif
#if MG_OTA <= MG_OTA_RT1064
#if MG_OTA == MG_OTA_RT1064
#define MG_FLEXSPI_BASE 0x402A4000
#else
#define MG_FLEXSPI_BASE 0x402A8000
#endif
#define flexspi_nor \
(*((struct mg_flexspi_nor_driver_interface **) (*(uint32_t *) 0x0020001c + \
16)))
@@ -576,9 +580,45 @@ MG_IRAM static bool mg_imxrt_erase(void *addr) {
}
#endif
MG_IRAM static void flash_sync(void *addr, size_t len) {
// Include any sectors erased while programming their first page
size_t ss = s_mg_flash_imxrt.secsz, es = ss;
#if MG_IMXRT_PARAM_SECTOR_COUNT > 0
ss = flash_sector_size(addr);
es = flash_sector_size((void *) ((uintptr_t) addr + len - 1));
#endif
uintptr_t start = MG_ROUND_DOWN((uintptr_t) addr, ss);
uintptr_t end = MG_ROUND_UP((uintptr_t) addr + len, es);
MG_DSB();
// Reset the FlexSPI AHB read buffers and IP FIFOs, even with CPU caches off
while ((MG_REG(MG_FLEXSPI_BASE + 0xe0) & 3U) != 3U) (void) 0; // STS0
MG_REG(MG_FLEXSPI_BASE) |= MG_BIT(0); // MCR0 SWRESET
while (MG_REG(MG_FLEXSPI_BASE) & MG_BIT(0)) (void) 0;
#if MG_OTA == MG_OTA_RT1180
uintptr_t cache = 0x44400000; // XCACHE_PC (CM33)
if (MG_REG(cache) & MG_BIT(0)) { // CCR ENCACHE
// CLCR: invalidate by physical address. Cache lines are 16 bytes
MG_SET_BITS(MG_REG(cache + 4), 3U << 24, MG_BIT(24) | MG_BIT(26));
for (; start < end; start += 16) {
MG_REG(cache + 8) = (uint32_t) start | MG_BIT(0); // CSAR: address | LGO
while (MG_REG(cache + 8) & MG_BIT(0)) (void) 0;
}
}
#else
if (MG_REG(0xe000ed14) & MG_BIT(16)) { // CM7 SCB CCR: D-cache enabled
for (; start < end; start += 32) {
MG_REG(0xe000ef5c) = (uint32_t) start; // DCIMVAC
}
}
#endif
MG_DSB();
}
MG_IRAM static bool mg_imxrt_write(void *addr, const void *buf, size_t len) {
struct mg_flexspi_nor_config config, *config_ptr = &config;
bool ok = false;
bool ok = false, sync = false;
// Interrupts must be disabled before calls to ROM API in RT1020 and 1060
MG_ARM_DISABLE_IRQ();
if (flexspi_nor_get_config(&config_ptr) != 0) goto fwxit;
@@ -599,6 +639,7 @@ MG_IRAM static bool mg_imxrt_write(void *addr, const void *buf, size_t len) {
ok = true;
while (ok && src < end) {
sync = true; // Erase/program may modify flash even on failure
if (flash_page_start(dst) && flash_erase(config_ptr, dst) == false) {
ok = false;
break;
@@ -640,6 +681,7 @@ MG_IRAM static bool mg_imxrt_write(void *addr, const void *buf, size_t len) {
}
MG_DEBUG(("Flash write %lu bytes @ %p: %s.", len, dst, ok ? "ok" : "fail"));
fwxit:
if (sync) flash_sync(addr, len);
if (!s_flash_irq_disabled) MG_ARM_ENABLE_IRQ();
return ok;
}