From 241e7e3a01f36b41b9a4e5ba60ad77173f96890c Mon Sep 17 00:00:00 2001 From: Nicolas Boichat Date: Fri, 7 Jul 2017 09:03:28 +0800 Subject: [PATCH] chip/stm32/flash-f: Clear option byte write enable/erase operation when done Before 72afc55bd9d3 "stm32: cleanup flash-f by using constant from register.h" lock() function would simply do: STM32_FLASH_CR = FLASH_CR_LOCK; which would clear all other bits in STM32_FLASH_CR, including FLASH_CR_OPTER and FLASH_CR_OPTWRE. This allow preserve_optb to work, as it does: 1. erase_optb a. unlock() b. Set FLASH_CR_OPTER c. lock() (clears FLASH_CR_OPTER!) 2. write_optb a. unlock() b. Set FLASH_CR_OPTPG c. Write option byte d. Clear FLASH_CR_OPTPG e. lock() After the patch, we now have: STM32_FLASH_CR |= FLASH_CR_LOCK; which seems more correct. However, 1.c. does not clear FLASH_CR_OPTER, and 2.b. ends up with both FLASH_CR_OPTPG and FLASH_CR_OPTER set, and the programming operation does not do anything. This patches does 3 things: - Rename FLASH_CR_OPTSTRT to FLASH_CR_OPTER, as that's the correct register name for STM32F0 and STM32F3. - Fix the above by clearing FLASH_CR_OPTER in erase_optb - Also clear FLASH_CR_OPTWRE in lock(). Not strictly necessary, but this seems to be the right thing to do. BRANCH=none BUG=chromium:739608 TEST=On hammer, type flashwp true; reboot; flashwp all; reboot flashinfo => All flash is protected Change-Id: Ic276545ae3c0bdb685c7b117a7f896ec341731bb Reviewed-on: https://chromium-review.googlesource.com/562839 Commit-Ready: Nicolas Boichat Tested-by: Nicolas Boichat Reviewed-by: Wei-Ning Huang Reviewed-by: Vincent Palatin --- chip/stm32/flash-f.c | 9 ++++++++- chip/stm32/registers.h | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/chip/stm32/flash-f.c b/chip/stm32/flash-f.c index bf0b9ab81c..f1e5b4d85f 100644 --- a/chip/stm32/flash-f.c +++ b/chip/stm32/flash-f.c @@ -82,6 +82,10 @@ static int unlock(int locks) static void lock(void) { +#if defined(CHIP_FAMILY_STM32F0) || defined(CHIP_FAMILY_STM32F3) + /* FLASH_CR_OPTWRE was set by writing the keys in unlock(). */ + STM32_FLASH_CR &= ~FLASH_CR_OPTWRE; +#endif STM32_FLASH_CR |= FLASH_CR_LOCK; } @@ -147,10 +151,13 @@ static int erase_optb(void) return rv; /* Must be set in 2 separate lines. */ - STM32_FLASH_CR |= FLASH_CR_OPTSTRT; + STM32_FLASH_CR |= FLASH_CR_OPTER; STM32_FLASH_CR |= FLASH_CR_STRT; rv = wait_busy(); + + STM32_FLASH_CR &= ~FLASH_CR_OPTER; + if (rv) return rv; lock(); diff --git a/chip/stm32/registers.h b/chip/stm32/registers.h index b8035620f0..03a2c3be00 100644 --- a/chip/stm32/registers.h +++ b/chip/stm32/registers.h @@ -1342,7 +1342,7 @@ typedef volatile struct stm32_spi_regs stm32_spi_regs_t; #define FLASH_CR_PG (1 << 0) #define FLASH_CR_PER (1 << 1) #define FLASH_CR_OPTPG (1 << 4) -#define FLASH_CR_OPTSTRT (1 << 5) +#define FLASH_CR_OPTER (1 << 5) #define FLASH_CR_STRT (1 << 6) #define FLASH_CR_LOCK (1 << 7) #define FLASH_CR_OPTWRE (1 << 9)