From dc70ba612a929fdd365275d412e68c61836ed5b8 Mon Sep 17 00:00:00 2001
From: Nick Brassel <nick@tzarc.org>
Date: Thu, 14 Jul 2022 00:41:08 +1000
Subject: Post-bootloader EFL/SPI fixes. (#17661)

* Fixup read address for EFL driver.

* Fixup sequencing of SPI.

* Lock during init of EFL backing store.
---
 platforms/chibios/drivers/spi_master.c                      | 1 -
 platforms/chibios/drivers/wear_leveling/wear_leveling_efl.c | 5 ++++-
 2 files changed, 4 insertions(+), 2 deletions(-)

(limited to 'platforms/chibios')

diff --git a/platforms/chibios/drivers/spi_master.c b/platforms/chibios/drivers/spi_master.c
index de308229ef..c3ab0623f0 100644
--- a/platforms/chibios/drivers/spi_master.c
+++ b/platforms/chibios/drivers/spi_master.c
@@ -46,7 +46,6 @@ __attribute__((weak)) void spi_init(void) {
         palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), SPI_MOSI_FLAGS);
         palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), SPI_MISO_FLAGS);
 #endif
-        spiUnselect(&SPI_DRIVER);
         spiStop(&SPI_DRIVER);
         currentSlavePin = NO_PIN;
     }
diff --git a/platforms/chibios/drivers/wear_leveling/wear_leveling_efl.c b/platforms/chibios/drivers/wear_leveling/wear_leveling_efl.c
index 4b5639ee4a..cdd1e26a7d 100644
--- a/platforms/chibios/drivers/wear_leveling/wear_leveling_efl.c
+++ b/platforms/chibios/drivers/wear_leveling/wear_leveling_efl.c
@@ -43,6 +43,9 @@ bool backing_store_init(void) {
     bs_dprintf("Init\n");
     flash = (BaseFlash *)&EFLD1;
 
+    // Need to re-lock the EFL, as if we've just had the bootloader executing it'll already be unlocked.
+    backing_store_lock();
+
     const flash_descriptor_t *desc    = flashGetDescriptor(flash);
     uint32_t                  counter = 0;
 
@@ -132,7 +135,7 @@ bool backing_store_lock(void) {
 
 bool backing_store_read(uint32_t address, backing_store_int_t *value) {
     uint32_t             offset = (base_offset + address);
-    backing_store_int_t *loc    = (backing_store_int_t *)offset;
+    backing_store_int_t *loc    = (backing_store_int_t *)flashGetOffsetAddress(flash, offset);
     *value                      = ~(*loc);
     bs_dprintf("Read  ");
     wl_dump(offset, value, sizeof(backing_store_int_t));
-- 
cgit v1.2.3