nrf51: verify that UICR needs erasing before triggering an error about it
[openocd.git] / src / flash / nor / nrf51.c
index 88c318c9d4cca8ac2047020657da78040b43d0e4..bfa199a2ffc415a7b8c5e010427b80b60ff833de 100644 (file)
@@ -73,7 +73,7 @@ enum nrf51_uicr_registers {
        NRF51_UICR_BASE = 0x10001000, /* User Information
                                       * Configuration Regsters */
 
-       NRF51_UICR_SIZE = 252,
+       NRF51_UICR_SIZE = 0x100,
 
 #define NRF51_UICR_REG(offset) (NRF51_UICR_BASE + offset)
 
@@ -203,7 +203,7 @@ static const struct nrf51_device_spec nrf51_known_devices_table[] = {
 
 static int nrf51_bank_is_probed(struct flash_bank *bank)
 {
-       struct nrf51_info *chip = (struct nrf51_info *)bank->driver_priv;
+       struct nrf51_info *chip = bank->driver_priv;
 
        assert(chip != NULL);
 
@@ -218,7 +218,7 @@ static int nrf51_get_probed_chip_if_halted(struct flash_bank *bank, struct nrf51
                return ERROR_TARGET_NOT_HALTED;
        }
 
-       *chip = (struct nrf51_info *)bank->driver_priv;
+       *chip = bank->driver_priv;
 
        int probed = nrf51_bank_is_probed(bank);
        if (probed < 0)
@@ -357,7 +357,7 @@ static int nrf51_protect_check(struct flash_bank *bank)
        if (bank->base == NRF51_UICR_BASE)
                return ERROR_OK;
 
-       struct nrf51_info *chip = (struct nrf51_info *)bank->driver_priv;
+       struct nrf51_info *chip = bank->driver_priv;
 
        assert(chip != NULL);
 
@@ -443,7 +443,7 @@ static int nrf51_probe(struct flash_bank *bank)
 {
        uint32_t hwid;
        int res;
-       struct nrf51_info *chip = (struct nrf51_info *)bank->driver_priv;
+       struct nrf51_info *chip = bank->driver_priv;
 
        res = target_read_u32(chip->target, NRF51_FICR_CONFIGID, &hwid);
        if (res != ERROR_OK) {
@@ -546,7 +546,7 @@ static int nrf51_auto_probe(struct flash_bank *bank)
 
 static struct flash_sector *nrf51_find_sector_by_address(struct flash_bank *bank, uint32_t address)
 {
-       struct nrf51_info *chip = (struct nrf51_info *)bank->driver_priv;
+       struct nrf51_info *chip = bank->driver_priv;
 
        for (int i = 0; i < bank->num_sectors; i++)
                if (bank->sectors[i].offset <= address &&
@@ -562,14 +562,16 @@ static int nrf51_erase_all(struct nrf51_info *chip)
                                        0x00000001);
 }
 
-static int nrf51_erase_page(struct nrf51_info *chip, struct flash_sector *sector)
+static int nrf51_erase_page(struct flash_bank *bank,
+                                                       struct nrf51_info *chip,
+                                                       struct flash_sector *sector)
 {
        int res;
 
        if (sector->is_protected)
                return ERROR_FAIL;
 
-       if (sector->offset == NRF51_UICR_BASE) {
+       if (bank->base == NRF51_UICR_BASE) {
                uint32_t ppfc;
                res = target_read_u32(chip->target, NRF51_FICR_PPFC,
                                      &ppfc);
@@ -579,6 +581,12 @@ static int nrf51_erase_page(struct nrf51_info *chip, struct flash_sector *sector
                }
 
                if ((ppfc & 0xFF) == 0xFF) {
+                       /* We can't erase the UICR.  Double-check to
+                          see if it's already erased before complaining. */
+                       default_flash_blank_check(bank);
+                       if (sector->is_erased == 1)
+                               return ERROR_OK;
+
                        LOG_ERROR("The chip was not pre-programmed with SoftDevice stack and UICR cannot be erased separately. Please issue mass erase before trying to write to this region");
                        return ERROR_FAIL;
                };
@@ -600,22 +608,42 @@ static int nrf51_erase_page(struct nrf51_info *chip, struct flash_sector *sector
        return res;
 }
 
+static int nrf51_ll_flash_write(struct nrf51_info *chip, uint32_t offset, const uint8_t *buffer, uint32_t buffer_size)
+{
+       int res;
+       assert(buffer_size % 4 == 0);
+
+       for (; buffer_size > 0; buffer_size -= 4) {
+               res = target_write_memory(chip->target, offset, 4, 1, buffer);
+               if (res != ERROR_OK)
+                       return res;
+
+               res = nrf51_wait_for_nvmc(chip);
+               if (res != ERROR_OK)
+                       return res;
+
+               offset += 4;
+               buffer += 4;
+       }
+
+       return ERROR_OK;
+}
+
 static int nrf51_write_page(struct flash_bank *bank, uint32_t offset, const uint8_t *buffer)
 {
        assert(offset % 4 == 0);
-
        int res = ERROR_FAIL;
-       struct nrf51_info *chip = (struct nrf51_info *)bank->driver_priv;
+       struct nrf51_info *chip = bank->driver_priv;
        struct flash_sector *sector = nrf51_find_sector_by_address(bank, offset);
 
        if (!sector)
-               goto error;
+               return ERROR_FLASH_SECTOR_INVALID;
 
        if (sector->is_protected)
                goto error;
 
        if (!sector->is_erased) {
-               res = nrf51_erase_page(chip, sector);
+               res = nrf51_erase_page(bank, chip, sector);
                if (res != ERROR_OK) {
                        LOG_ERROR("Failed to erase sector @ 0x%08"PRIx32, sector->offset);
                        goto error;
@@ -627,8 +655,8 @@ static int nrf51_write_page(struct flash_bank *bank, uint32_t offset, const uint
                goto error;
 
        sector->is_erased = 0;
-       res = target_write_memory(bank->target, offset, 4,
-                                 chip->code_page_size / 4, buffer);
+
+       res = nrf51_ll_flash_write(chip, offset, buffer, chip->code_page_size);
        if (res != ERROR_OK)
                goto set_read_only;
 
@@ -652,7 +680,7 @@ static int nrf51_erase(struct flash_bank *bank, int first, int last)
 
        /* For each sector to be erased */
        for (int s = first; s <= last && res == ERROR_OK; s++)
-               res = nrf51_erase_page(chip, &bank->sectors[s]);
+               res = nrf51_erase_page(bank, chip, &bank->sectors[s]);
 
        return res;
 }
@@ -757,7 +785,7 @@ static int nrf51_uicr_flash_write(struct flash_bank *bank,
                return res;
 
        if (!sector->is_erased) {
-               res = nrf51_erase_page(chip, sector);
+               res = nrf51_erase_page(bank, chip, sector);
                if (res != ERROR_OK)
                        return res;
        }
@@ -768,11 +796,7 @@ static int nrf51_uicr_flash_write(struct flash_bank *bank,
 
        memcpy(&uicr[offset], buffer, count);
 
-       res = target_write_memory(bank->target,
-                                  NRF51_UICR_BASE,
-                                  4,
-                                  NRF51_UICR_SIZE / 4,
-                                  uicr);
+       res = nrf51_ll_flash_write(chip, NRF51_UICR_BASE, uicr, NRF51_UICR_SIZE);
        if (res != ERROR_OK) {
                nrf51_nvmc_read_only(chip);
                return res;
@@ -904,8 +928,9 @@ static int nrf51_info(struct flash_bank *bank, char *buf, int buf_size)
        if (res != ERROR_OK)
                return res;
 
-       struct {
-               uint32_t address, value;
+       static struct {
+               const uint32_t address;
+               uint32_t value;
        } ficr[] = {
                { .address = NRF51_FICR_CODEPAGESIZE    },
                { .address = NRF51_FICR_CODESIZE        },

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)