flash/nor/stm32: Eliminate working area leak
[fw/openocd] / src / flash / nor / nrf5.c
index 11e57291cf32c33e9e6693dc82abebb0f3fd0537..a7ae6f3b79707d1997d01ed923aed4cd7ca6490f 100644 (file)
@@ -155,6 +155,11 @@ static const struct nrf5_device_spec nrf5_known_devices_table[] = {
        NRF5_DEVICE_DEF(0x0020, "51822", "CEAA", "BA",    256),
        NRF5_DEVICE_DEF(0x002F, "51822", "CEAA", "B0",    256),
 
+       /* Some early nRF51-DK (PCA10028) & nRF51-Dongle (PCA10031) boards
+          with built-in jlink seem to use engineering samples not listed
+          in the nRF51 Series Compatibility Matrix V1.0. */
+       NRF5_DEVICE_DEF(0x0071, "51822", "QFAC", "AB",    256),
+
        /* nRF51822 Devices (IC rev 2). */
        NRF5_DEVICE_DEF(0x002A, "51822", "QFAA", "FA0",   256),
        NRF5_DEVICE_DEF(0x0044, "51822", "QFAA", "GC0",   256),
@@ -168,12 +173,14 @@ static const struct nrf5_device_spec nrf5_known_devices_table[] = {
 
        /* nRF51822 Devices (IC rev 3). */
        NRF5_DEVICE_DEF(0x0072, "51822", "QFAA", "H0",    256),
+       NRF5_DEVICE_DEF(0x00D1, "51822", "QFAA", "H2",    256),
        NRF5_DEVICE_DEF(0x007B, "51822", "QFAB", "C0",    128),
        NRF5_DEVICE_DEF(0x0083, "51822", "QFAC", "A0",    256),
        NRF5_DEVICE_DEF(0x0084, "51822", "QFAC", "A1",    256),
        NRF5_DEVICE_DEF(0x007D, "51822", "CDAB", "A0",    128),
        NRF5_DEVICE_DEF(0x0079, "51822", "CEAA", "E0",    256),
        NRF5_DEVICE_DEF(0x0087, "51822", "CFAC", "A0",    256),
+       NRF5_DEVICE_DEF(0x008F, "51822", "QFAA", "H1",    256),
 
        /* nRF51422 Devices (IC rev 1). */
        NRF5_DEVICE_DEF(0x001E, "51422", "QFAA", "CA",    256),
@@ -197,11 +204,6 @@ static const struct nrf5_device_spec nrf5_known_devices_table[] = {
 
        /* nRF52832 Devices */
        NRF5_DEVICE_DEF(0x00C7, "52832", "QFAA", "B0",    512),
-
-       /* Some early nRF51-DK (PCA10028) & nRF51-Dongle (PCA10031) boards
-          with built-in jlink seem to use engineering samples not listed
-          in the nRF51 Series Compatibility Matrix V1.0. */
-       NRF5_DEVICE_DEF(0x0071, "51822", "QFAC", "AB",    256),
 };
 
 static int nrf5_bank_is_probed(struct flash_bank *bank)
@@ -529,7 +531,6 @@ static int nrf5_probe(struct flash_bank *bank)
                bank->sectors[0].size = bank->size;
                bank->sectors[0].offset = 0;
 
-               /* mark as unknown */
                bank->sectors[0].is_erased = 0;
                bank->sectors[0].is_protected = 0;
 
@@ -551,17 +552,6 @@ static int nrf5_auto_probe(struct flash_bank *bank)
                return nrf5_probe(bank);
 }
 
-static struct flash_sector *nrf5_find_sector_by_address(struct flash_bank *bank, uint32_t address)
-{
-       struct nrf5_info *chip = bank->driver_priv;
-
-       for (int i = 0; i < bank->num_sectors; i++)
-               if (bank->sectors[i].offset <= address &&
-                   address < (bank->sectors[i].offset + chip->code_page_size))
-                       return &bank->sectors[i];
-       return NULL;
-}
-
 static int nrf5_erase_all(struct nrf5_info *chip)
 {
        LOG_DEBUG("Erasing all non-volatile memory");
@@ -613,9 +603,6 @@ static int nrf5_erase_page(struct flash_bank *bank,
                                               sector->offset);
        }
 
-       if (res == ERROR_OK)
-               sector->is_erased = 1;
-
        return res;
 }
 
@@ -741,48 +728,22 @@ static int nrf5_write_pages(struct flash_bank *bank, uint32_t start, uint32_t en
 {
        int res = ERROR_FAIL;
        struct nrf5_info *chip = bank->driver_priv;
-       struct flash_sector *sector;
-       uint32_t offset;
 
        assert(start % chip->code_page_size == 0);
        assert(end % chip->code_page_size == 0);
 
-       /* Erase all sectors */
-       for (offset = start; offset < end; offset += chip->code_page_size) {
-               sector = nrf5_find_sector_by_address(bank, offset);
-               if (!sector) {
-                       LOG_ERROR("Invalid sector @ 0x%08"PRIx32, offset);
-                       return ERROR_FLASH_SECTOR_INVALID;
-               }
-
-               if (sector->is_protected) {
-                       LOG_ERROR("Can't erase protected sector @ 0x%08"PRIx32, offset);
-                       goto error;
-               }
-
-               if (sector->is_erased != 1) {   /* 1 = erased, 0= not erased, -1 = unknown */
-                       res = nrf5_erase_page(bank, chip, sector);
-                       if (res != ERROR_OK) {
-                               LOG_ERROR("Failed to erase sector @ 0x%08"PRIx32, sector->offset);
-                               goto error;
-                       }
-               }
-               sector->is_erased = 0;
-       }
-
        res = nrf5_nvmc_write_enable(chip);
        if (res != ERROR_OK)
                goto error;
 
        res = nrf5_ll_flash_write(chip, start, buffer, (end - start));
        if (res != ERROR_OK)
-               goto set_read_only;
+               goto error;
 
        return nrf5_nvmc_read_only(chip);
 
-set_read_only:
-       nrf5_nvmc_read_only(chip);
 error:
+       nrf5_nvmc_read_only(chip);
        LOG_ERROR("Failed to write to nrf5 flash");
        return res;
 }
@@ -874,11 +835,9 @@ static int nrf5_uicr_flash_write(struct flash_bank *bank,
        if (res != ERROR_OK)
                return res;
 
-       if (sector->is_erased != 1) {
-               res = nrf5_erase_page(bank, chip, sector);
-               if (res != ERROR_OK)
-                       return res;
-       }
+       res = nrf5_erase_page(bank, chip, sector);
+       if (res != ERROR_OK)
+               return res;
 
        res = nrf5_nvmc_write_enable(chip);
        if (res != ERROR_OK)
@@ -990,9 +949,6 @@ COMMAND_HANDLER(nrf5_handle_mass_erase_command)
                return res;
        }
 
-       for (int i = 0; i < bank->num_sectors; i++)
-               bank->sectors[i].is_erased = 1;
-
        res = nrf5_protect_check(bank);
        if (res != ERROR_OK) {
                LOG_ERROR("Failed to check chip's write protection");
@@ -1003,8 +959,6 @@ COMMAND_HANDLER(nrf5_handle_mass_erase_command)
        if (res != ERROR_OK)
                return res;
 
-       bank->sectors[0].is_erased = 1;
-
        return ERROR_OK;
 }