flash/nor: consolidate flash protect/protect_check
[openocd.git] / src / flash / nor / psoc5lp.c
index ae8e3d3bc17a5370fed494fdd302cf8fb00a1570..d8e1c15682e4d8b510657f4b753988176b33d85d 100644 (file)
@@ -234,7 +234,8 @@ static void psoc5lp_get_part_number(const struct psoc5lp_device *dev, char *str)
        }
 
        /* Package does not matter. */
-       strncpy(str + 8, "xx", 2);
+       str[8] = 'x';
+       str[9] = 'x';
 
        /* Temperate range cannot uniquely be identified. */
        str[10] = 'x';
@@ -752,16 +753,6 @@ static int psoc5lp_nvl_write(struct flash_bank *bank,
        return ERROR_OK;
 }
 
-static int psoc5lp_nvl_protect_check(struct flash_bank *bank)
-{
-       int i;
-
-       for (i = 0; i < bank->num_sectors; i++)
-               bank->sectors[i].is_protected = -1;
-
-       return ERROR_OK;
-}
-
 static int psoc5lp_nvl_get_info_command(struct flash_bank *bank,
        char *buf, int buf_size)
 {
@@ -854,11 +845,11 @@ struct flash_driver psoc5lp_nvl_flash = {
        .info = psoc5lp_nvl_get_info_command,
        .probe = psoc5lp_nvl_probe,
        .auto_probe = psoc5lp_nvl_auto_probe,
-       .protect_check = psoc5lp_nvl_protect_check,
        .read = psoc5lp_nvl_read,
        .erase = psoc5lp_nvl_erase,
        .erase_check = psoc5lp_nvl_erase_check,
        .write = psoc5lp_nvl_write,
+       .free_driver_priv = default_flash_free_driver_priv,
 };
 
 /*
@@ -943,16 +934,6 @@ static int psoc5lp_eeprom_write(struct flash_bank *bank,
        return ERROR_OK;
 }
 
-static int psoc5lp_eeprom_protect_check(struct flash_bank *bank)
-{
-       int i;
-
-       for (i = 0; i < bank->num_sectors; i++)
-               bank->sectors[i].is_protected = -1;
-
-       return ERROR_OK;
-}
-
 static int psoc5lp_eeprom_get_info_command(struct flash_bank *bank, char *buf, int buf_size)
 {
        struct psoc5lp_eeprom_flash_bank *psoc_eeprom_bank = bank->driver_priv;
@@ -1062,11 +1043,11 @@ struct flash_driver psoc5lp_eeprom_flash = {
        .info = psoc5lp_eeprom_get_info_command,
        .probe = psoc5lp_eeprom_probe,
        .auto_probe = psoc5lp_eeprom_auto_probe,
-       .protect_check = psoc5lp_eeprom_protect_check,
        .read = default_flash_read,
        .erase = psoc5lp_eeprom_erase,
        .erase_check = default_flash_blank_check,
        .write = psoc5lp_eeprom_write,
+       .free_driver_priv = default_flash_free_driver_priv,
 };
 
 /*
@@ -1077,6 +1058,10 @@ struct psoc5lp_flash_bank {
        bool probed;
        const struct psoc5lp_device *device;
        bool ecc_enabled;
+       /* If ecc is disabled, num_sectors counts both std and ecc sectors.
+        * If ecc is enabled, num_sectors indicates just the number of std sectors.
+        * However ecc sector descriptors bank->sector[num_sectors..2*num_sectors-1]
+        * are used for driver private flash operations */
 };
 
 static int psoc5lp_erase(struct flash_bank *bank, int first, int last)
@@ -1121,21 +1106,25 @@ static int psoc5lp_erase_check(struct flash_bank *bank)
                return ERROR_TARGET_NOT_HALTED;
        }
 
+       int num_sectors = bank->num_sectors;
+       if (psoc_bank->ecc_enabled)
+               num_sectors *= 2;       /* count both std and ecc sector always */
+
        struct target_memory_check_block *block_array;
-       block_array = malloc(bank->num_sectors * sizeof(struct target_memory_check_block));
+       block_array = malloc(num_sectors * sizeof(struct target_memory_check_block));
        if (block_array == NULL)
                return ERROR_FAIL;
 
-       for (i = 0; i < bank->num_sectors; i++) {
+       for (i = 0; i < num_sectors; i++) {
                block_array[i].address = bank->base + bank->sectors[i].offset;
                block_array[i].size = bank->sectors[i].size;
                block_array[i].result = UINT32_MAX; /* erase state unknown */
        }
 
        bool fast_check = true;
-       for (i = 0; i < bank->num_sectors; ) {
+       for (i = 0; i < num_sectors; ) {
                retval = armv7m_blank_check_memory(target,
-                                       block_array + i, bank->num_sectors - i,
+                                       block_array + i, num_sectors - i,
                                        bank->erased_value);
                if (retval < 1) {
                        /* Run slow fallback if the first run gives no result
@@ -1148,15 +1137,15 @@ static int psoc5lp_erase_check(struct flash_bank *bank)
        }
 
        if (fast_check) {
-               if (!psoc_bank->ecc_enabled) {
-                       int half_sectors = bank->num_sectors / 2;
-                       for (i = 0; i < half_sectors / 2; i++)
+               if (psoc_bank->ecc_enabled) {
+                       for (i = 0; i < bank->num_sectors; i++)
                                bank->sectors[i].is_erased =
                                        (block_array[i].result != 1)
-                                       ? block_array[i + half_sectors].result
-                                       : block_array[i].result;
+                                       ? block_array[i].result
+                                       : block_array[i + bank->num_sectors].result;
+                               /* if std sector is erased, use status of ecc sector */
                } else {
-                       for (i = 0; i < bank->num_sectors; i++)
+                       for (i = 0; i < num_sectors; i++)
                                bank->sectors[i].is_erased = block_array[i].result;
                }
                retval = ERROR_OK;
@@ -1571,4 +1560,5 @@ struct flash_driver psoc5lp_flash = {
        .erase = psoc5lp_erase,
        .erase_check = psoc5lp_erase_check,
        .write = psoc5lp_write,
+       .free_driver_priv = default_flash_free_driver_priv,
 };

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)