flash/nor: Use proper data types in driver API
[openocd.git] / src / flash / nor / psoc5lp.c
index 0b21ed6494295affd09271a5d2a12867c18ec63c..17cc6d80856e7c08db3438704b24b9a6b2f4c089 100644 (file)
@@ -657,7 +657,8 @@ static int psoc5lp_nvl_read(struct flash_bank *bank,
        return ERROR_OK;
 }
 
-static int psoc5lp_nvl_erase(struct flash_bank *bank, int first, int last)
+static int psoc5lp_nvl_erase(struct flash_bank *bank, unsigned int first,
+               unsigned int last)
 {
        LOG_WARNING("There is no erase operation for NV Latches");
        return ERROR_FLASH_OPER_UNSUPPORTED;
@@ -665,9 +666,7 @@ static int psoc5lp_nvl_erase(struct flash_bank *bank, int first, int last)
 
 static int psoc5lp_nvl_erase_check(struct flash_bank *bank)
 {
-       int i;
-
-       for (i = 0; i < bank->num_sectors; i++)
+       for (unsigned int i = 0; i < bank->num_sectors; i++)
                bank->sectors[i].is_erased = 0;
 
        return ERROR_OK;
@@ -753,16 +752,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)
 {
@@ -848,18 +837,18 @@ static const struct command_registration psoc5lp_nvl_command_handlers[] = {
        COMMAND_REGISTRATION_DONE
 };
 
-struct flash_driver psoc5lp_nvl_flash = {
+const struct flash_driver psoc5lp_nvl_flash = {
        .name = "psoc5lp_nvl",
        .commands = psoc5lp_nvl_command_handlers,
        .flash_bank_command = psoc5lp_nvl_flash_bank_command,
        .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,
 };
 
 /*
@@ -871,11 +860,12 @@ struct psoc5lp_eeprom_flash_bank {
        const struct psoc5lp_device *device;
 };
 
-static int psoc5lp_eeprom_erase(struct flash_bank *bank, int first, int last)
+static int psoc5lp_eeprom_erase(struct flash_bank *bank, unsigned int first,
+               unsigned int last)
 {
-       int i, retval;
+       int retval;
 
-       for (i = first; i <= last; i++) {
+       for (unsigned int i = first; i <= last; i++) {
                retval = psoc5lp_spc_erase_sector(bank->target,
                                SPC_ARRAY_EEPROM, i);
                if (retval != ERROR_OK)
@@ -944,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;
@@ -971,7 +951,7 @@ static int psoc5lp_eeprom_probe(struct flash_bank *bank)
        struct psoc5lp_eeprom_flash_bank *psoc_eeprom_bank = bank->driver_priv;
        uint32_t flash_addr = bank->base;
        uint32_t val;
-       int i, retval;
+       int retval;
 
        if (psoc_eeprom_bank->probed)
                return ERROR_OK;
@@ -999,7 +979,7 @@ static int psoc5lp_eeprom_probe(struct flash_bank *bank)
        bank->num_sectors = DIV_ROUND_UP(bank->size, EEPROM_SECTOR_SIZE);
        bank->sectors = calloc(bank->num_sectors,
                               sizeof(struct flash_sector));
-       for (i = 0; i < bank->num_sectors; i++) {
+       for (unsigned int i = 0; i < bank->num_sectors; i++) {
                bank->sectors[i].size = EEPROM_SECTOR_SIZE;
                bank->sectors[i].offset = flash_addr - bank->base;
                bank->sectors[i].is_erased = -1;
@@ -1056,18 +1036,18 @@ static const struct command_registration psoc5lp_eeprom_command_handlers[] = {
        COMMAND_REGISTRATION_DONE
 };
 
-struct flash_driver psoc5lp_eeprom_flash = {
+const struct flash_driver psoc5lp_eeprom_flash = {
        .name = "psoc5lp_eeprom",
        .commands = psoc5lp_eeprom_command_handlers,
        .flash_bank_command = psoc5lp_eeprom_flash_bank_command,
        .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,
 };
 
 /*
@@ -1078,29 +1058,34 @@ 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)
+static int psoc5lp_erase(struct flash_bank *bank, unsigned int first,
+               unsigned int last)
 {
        struct psoc5lp_flash_bank *psoc_bank = bank->driver_priv;
-       int i, retval;
+       int retval;
 
        if (!psoc_bank->ecc_enabled) {
                /* Silently avoid erasing sectors twice */
                if (last >= first + bank->num_sectors / 2) {
-                       LOG_DEBUG("Skipping duplicate erase of sectors %d to %d",
+                       LOG_DEBUG("Skipping duplicate erase of sectors %u to %u",
                                first + bank->num_sectors / 2, last);
                        last = first + (bank->num_sectors / 2) - 1;
                }
                /* Check for any remaining ECC sectors */
                if (last >= bank->num_sectors / 2) {
-                       LOG_WARNING("Skipping erase of ECC region sectors %d to %d",
+                       LOG_WARNING("Skipping erase of ECC region sectors %u to %u",
                                bank->num_sectors / 2, last);
                        last = (bank->num_sectors / 2) - 1;
                }
        }
 
-       for (i = first; i <= last; i++) {
+       for (unsigned int i = first; i <= last; i++) {
                retval = psoc5lp_spc_erase_sector(bank->target,
                                i / SECTORS_PER_BLOCK, i % SECTORS_PER_BLOCK);
                if (retval != ERROR_OK)
@@ -1115,28 +1100,32 @@ static int psoc5lp_erase_check(struct flash_bank *bank)
 {
        struct psoc5lp_flash_bank *psoc_bank = bank->driver_priv;
        struct target *target = bank->target;
-       int i, retval;
+       int retval;
 
        if (target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
 
+       unsigned 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 (unsigned int 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 (unsigned int 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
@@ -1149,15 +1138,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 (unsigned int 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 (unsigned int i = 0; i < num_sectors; i++)
                                bank->sectors[i].is_erased = block_array[i].result;
                }
                retval = ERROR_OK;
@@ -1239,7 +1228,7 @@ static int psoc5lp_write(struct flash_bank *bank, const uint8_t *buffer,
                        struct working_area *data_area = even_row ? even_row_area : odd_row_area;
                        unsigned len = MIN(ROW_SIZE, byte_count);
 
-                       LOG_DEBUG("Writing load command for array %u row %u at 0x%08" TARGET_PRIxADDR,
+                       LOG_DEBUG("Writing load command for array %u row %u at " TARGET_ADDR_FMT,
                                array_id, row, data_area->address);
 
                        psoc5lp_spc_write_opcode_buffer(target, buf, SPC_LOAD_ROW);
@@ -1362,7 +1351,7 @@ static int psoc5lp_protect_check(struct flash_bank *bank)
        struct psoc5lp_flash_bank *psoc_bank = bank->driver_priv;
        uint8_t row_data[ROW_SIZE];
        const unsigned protection_bytes_per_sector = ROWS_PER_SECTOR * 2 / 8;
-       unsigned i, j, k, num_sectors;
+       unsigned i, k, num_sectors;
        int retval;
 
        if (bank->target->state != TARGET_HALTED) {
@@ -1382,7 +1371,7 @@ static int psoc5lp_protect_check(struct flash_bank *bank)
                else
                        num_sectors = SECTORS_PER_BLOCK;
 
-               for (j = 0; j < num_sectors; j++) {
+               for (unsigned int j = 0; j < num_sectors; j++) {
                        int sector_nr = i * SECTORS_PER_BLOCK + j;
                        struct flash_sector *sector = &bank->sectors[sector_nr];
                        struct flash_sector *ecc_sector;
@@ -1428,7 +1417,7 @@ static int psoc5lp_probe(struct flash_bank *bank)
        struct psoc5lp_flash_bank *psoc_bank = bank->driver_priv;
        uint32_t flash_addr = bank->base;
        uint8_t nvl[4], temp[2];
-       int i, retval;
+       int retval;
 
        if (target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
@@ -1459,7 +1448,7 @@ static int psoc5lp_probe(struct flash_bank *bank)
 
                bank->sectors = calloc(bank->num_sectors * 2,
                                       sizeof(struct flash_sector));
-               for (i = 0; i < bank->num_sectors; i++) {
+               for (unsigned int i = 0; i < bank->num_sectors; i++) {
                        bank->sectors[i].size = SECTOR_SIZE;
                        bank->sectors[i].offset = flash_addr - bank->base;
                        bank->sectors[i].is_erased = -1;
@@ -1468,7 +1457,7 @@ static int psoc5lp_probe(struct flash_bank *bank)
                        flash_addr += bank->sectors[i].size;
                }
                flash_addr = 0x48000000;
-               for (i = bank->num_sectors; i < bank->num_sectors * 2; i++) {
+               for (unsigned int i = bank->num_sectors; i < bank->num_sectors * 2; i++) {
                        bank->sectors[i].size = ROWS_PER_SECTOR * ROW_ECC_SIZE;
                        bank->sectors[i].offset = flash_addr - bank->base;
                        bank->sectors[i].is_erased = -1;
@@ -1513,9 +1502,9 @@ COMMAND_HANDLER(psoc5lp_handle_mass_erase_command)
 
        retval = psoc5lp_spc_erase_all(bank->target);
        if (retval == ERROR_OK)
-               command_print(CMD_CTX, "PSoC 5LP erase succeeded");
+               command_print(CMD, "PSoC 5LP erase succeeded");
        else
-               command_print(CMD_CTX, "PSoC 5LP erase failed");
+               command_print(CMD, "PSoC 5LP erase failed");
 
        return retval;
 }
@@ -1560,7 +1549,7 @@ static const struct command_registration psoc5lp_command_handlers[] = {
        COMMAND_REGISTRATION_DONE
 };
 
-struct flash_driver psoc5lp_flash = {
+const struct flash_driver psoc5lp_flash = {
        .name = "psoc5lp",
        .commands = psoc5lp_command_handlers,
        .flash_bank_command = psoc5lp_flash_bank_command,
@@ -1572,4 +1561,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)