change %x and %d to PRIx32 and PRId32 where needed for cygwin
[fw/openocd] / src / flash / nor / str7x.c
index 5767b9300219f1a955850f2c866e1c0de82d50f1..fa1744c5e6e2b88b34d49fae9d1d0df848683464 100644 (file)
@@ -26,7 +26,7 @@
 
 #include "imp.h"
 #include "str7x.h"
-#include <target/armv4_5.h>
+#include <target/arm.h>
 #include <helper/binarybuffer.h>
 #include <target/algorithm.h>
 
@@ -93,7 +93,10 @@ static int str7x_build_block_list(struct flash_bank *bank)
                bank->sectors[num_sectors].offset = mem_layout_str7bank0[i].sector_start;
                bank->sectors[num_sectors].size = mem_layout_str7bank0[i].sector_size;
                bank->sectors[num_sectors].is_erased = -1;
-               bank->sectors[num_sectors].is_protected = 1;
+               /* the reset_init handler marks all the sectors unprotected,
+                * matching hardware after reset; keep the driver in sync
+                */
+               bank->sectors[num_sectors].is_protected = 0;
                str7x_info->sector_bits[num_sectors++] = mem_layout_str7bank0[i].sector_bit;
        }
 
@@ -102,7 +105,10 @@ static int str7x_build_block_list(struct flash_bank *bank)
                bank->sectors[num_sectors].offset = mem_layout_str7bank1[i].sector_start;
                bank->sectors[num_sectors].size = mem_layout_str7bank1[i].sector_size;
                bank->sectors[num_sectors].is_erased = -1;
-               bank->sectors[num_sectors].is_protected = 1;
+               /* the reset_init handler marks all the sectors unprotected,
+                * matching hardware after reset; keep the driver in sync
+                */
+               bank->sectors[num_sectors].is_protected = 0;
                str7x_info->sector_bits[num_sectors++] = mem_layout_str7bank1[i].sector_bit;
        }
 
@@ -310,18 +316,19 @@ static int str7x_protect(struct flash_bank *bank, int set, int first, int last)
        return ERROR_OK;
 }
 
-static int str7x_write_block(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
+static int str7x_write_block(struct flash_bank *bank, uint8_t *buffer,
+               uint32_t offset, uint32_t count)
 {
        struct str7x_flash_bank *str7x_info = bank->driver_priv;
        struct target *target = bank->target;
-       uint32_t buffer_size = 8192;
+       uint32_t buffer_size = 32768;
        struct working_area *source;
        uint32_t address = bank->base + offset;
        struct reg_param reg_params[6];
-       struct armv4_5_algorithm armv4_5_info;
+       struct arm_algorithm armv4_5_info;
        int retval = ERROR_OK;
 
-       uint32_t str7x_flash_write_code[] = {
+       static const uint32_t str7x_flash_write_code[] = {
                                        /* write:                               */
                0xe3a04201, /*  mov r4, #0x10000000     */
                0xe5824000, /*  str r4, [r2, #0x0]      */
@@ -348,13 +355,16 @@ static int str7x_write_block(struct flash_bank *bank, uint8_t *buffer, uint32_t
        };
 
        /* flash write code */
-       if (target_alloc_working_area(target, 4 * 20, &str7x_info->write_algorithm) != ERROR_OK)
+       if (target_alloc_working_area(target, sizeof(str7x_flash_write_code),
+                       &str7x_info->write_algorithm) != ERROR_OK)
        {
                LOG_WARNING("no working area available, can't do block memory writes");
                return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
        };
 
-       target_write_buffer(target, str7x_info->write_algorithm->address, 20 * 4, (uint8_t*)str7x_flash_write_code);
+       target_write_buffer(target, str7x_info->write_algorithm->address,
+                       sizeof(str7x_flash_write_code),
+                       (uint8_t*)str7x_flash_write_code);
 
        /* memory buffer */
        while (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK)
@@ -362,7 +372,8 @@ static int str7x_write_block(struct flash_bank *bank, uint8_t *buffer, uint32_t
                buffer_size /= 2;
                if (buffer_size <= 256)
                {
-                       /* if we already allocated the writing code, but failed to get a buffer, free the algorithm */
+                       /* if we already allocated the writing code, but failed to get a
+                        * buffer, free the algorithm */
                        if (str7x_info->write_algorithm)
                                target_free_working_area(target, str7x_info->write_algorithm);
 
@@ -371,7 +382,7 @@ static int str7x_write_block(struct flash_bank *bank, uint8_t *buffer, uint32_t
                }
        }
 
-       armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
+       armv4_5_info.common_magic = ARM_COMMON_MAGIC;
        armv4_5_info.core_mode = ARM_MODE_SVC;
        armv4_5_info.core_state = ARM_STATE_ARM;
 
@@ -394,7 +405,10 @@ static int str7x_write_block(struct flash_bank *bank, uint8_t *buffer, uint32_t
                buf_set_u32(reg_params[3].value, 0, 32, thisrun_count);
                buf_set_u32(reg_params[5].value, 0, 32, str7x_info->busy_bits);
 
-               if ((retval = target_run_algorithm(target, 0, NULL, 6, reg_params, str7x_info->write_algorithm->address, str7x_info->write_algorithm->address + (19 * 4), 10000, &armv4_5_info)) != ERROR_OK)
+               if ((retval = target_run_algorithm(target, 0, NULL, 6, reg_params,
+                               str7x_info->write_algorithm->address,
+                               str7x_info->write_algorithm->address + (sizeof(str7x_flash_write_code) - 4),
+                               10000, &armv4_5_info)) != ERROR_OK)
                {
                        LOG_ERROR("error executing str7x flash write algorithm");
                        retval = ERROR_FLASH_OPERATION_FAILED;
@@ -425,7 +439,8 @@ static int str7x_write_block(struct flash_bank *bank, uint8_t *buffer, uint32_t
        return retval;
 }
 
-static int str7x_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
+static int str7x_write(struct flash_bank *bank, uint8_t *buffer,
+               uint32_t offset, uint32_t count)
 {
        struct target *target = bank->target;
        struct str7x_flash_bank *str7x_info = bank->driver_priv;
@@ -476,7 +491,8 @@ static int str7x_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset
        if (dwords_remaining > 0)
        {
                /* try using a block write */
-               if ((retval = str7x_write_block(bank, buffer, offset, dwords_remaining)) != ERROR_OK)
+               if ((retval = str7x_write_block(bank, buffer, offset,
+                               dwords_remaining)) != ERROR_OK)
                {
                        if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
                        {
@@ -511,11 +527,13 @@ static int str7x_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset
                target_write_u32(target, str7x_get_flash_adr(bank, FLASH_AR), address);
 
                /* data word 1 */
-               target_write_memory(target, str7x_get_flash_adr(bank, FLASH_DR0), 4, 1, buffer + bytes_written);
+               target_write_memory(target, str7x_get_flash_adr(bank, FLASH_DR0),
+                               4, 1, buffer + bytes_written);
                bytes_written += 4;
 
                /* data word 2 */
-               target_write_memory(target, str7x_get_flash_adr(bank, FLASH_DR1), 4, 1, buffer + bytes_written);
+               target_write_memory(target, str7x_get_flash_adr(bank, FLASH_DR1),
+                               4, 1, buffer + bytes_written);
                bytes_written += 4;
 
                /* start programming cycle */
@@ -558,11 +576,13 @@ static int str7x_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset
                target_write_u32(target, str7x_get_flash_adr(bank, FLASH_AR), address);
 
                /* data word 1 */
-               target_write_memory(target, str7x_get_flash_adr(bank, FLASH_DR0), 4, 1, last_dword);
+               target_write_memory(target, str7x_get_flash_adr(bank, FLASH_DR0),
+                               4, 1, last_dword);
                bytes_written += 4;
 
                /* data word 2 */
-               target_write_memory(target, str7x_get_flash_adr(bank, FLASH_DR1), 4, 1, last_dword + 4);
+               target_write_memory(target, str7x_get_flash_adr(bank, FLASH_DR1),
+                               4, 1, last_dword + 4);
                bytes_written += 4;
 
                /* start programming cycle */
@@ -600,6 +620,12 @@ COMMAND_HANDLER(str7x_handle_part_id_command)
 static int str7x_info(struct flash_bank *bank, char *buf, int buf_size)
 {
        snprintf(buf, buf_size, "str7x flash driver info");
+       /* STR7x flash doesn't support sector protection interrogation.
+        * FLASH_NVWPAR acts as a write only register; its read value
+        * doesn't reflect the actual protection state of the sectors.
+        */
+       LOG_WARNING("STR7x flash lock information might not be correct "
+                       "due to hardware limitations.");
        return ERROR_OK;
 }
 
@@ -665,7 +691,8 @@ COMMAND_HANDLER(str7x_handle_disable_jtag_command)
                flash_cmd = FLASH_SPR;
                target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), flash_cmd);
                target_write_u32(target, str7x_get_flash_adr(bank, FLASH_AR), 0x4010DFBC);
-               target_write_u32(target, str7x_get_flash_adr(bank, FLASH_DR0), ~(1 << (15 + ProtectionLevel)));
+               target_write_u32(target, str7x_get_flash_adr(bank, FLASH_DR0),
+                               ~(1 << (15 + ProtectionLevel)));
                flash_cmd = FLASH_SPR | FLASH_WMS;
                target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), flash_cmd);
        }
@@ -676,12 +703,13 @@ COMMAND_HANDLER(str7x_handle_disable_jtag_command)
 static const struct command_registration str7x_exec_command_handlers[] = {
        {
                .name = "disable_jtag",
-               .handler = &str7x_handle_disable_jtag_command,
+               .handler = str7x_handle_disable_jtag_command,
                .mode = COMMAND_EXEC,
                .help = "disable jtag access",
        },
        COMMAND_REGISTRATION_DONE
 };
+
 static const struct command_registration str7x_command_handlers[] = {
        {
                .name = "str7x",
@@ -693,15 +721,15 @@ static const struct command_registration str7x_command_handlers[] = {
 };
 
 struct flash_driver str7x_flash = {
-               .name = "str7x",
-               .commands = str7x_command_handlers,
-               .flash_bank_command = &str7x_flash_bank_command,
-               .erase = &str7x_erase,
-               .protect = &str7x_protect,
-               .write = &str7x_write,
-               .probe = &str7x_probe,
-               .auto_probe = &str7x_probe,
-               .erase_check = &default_flash_blank_check,
-               .protect_check = &str7x_protect_check,
-               .info = &str7x_info,
-       };
+       .name = "str7x",
+       .commands = str7x_command_handlers,
+       .flash_bank_command = str7x_flash_bank_command,
+       .erase = str7x_erase,
+       .protect = str7x_protect,
+       .write = str7x_write,
+       .probe = str7x_probe,
+       .auto_probe = str7x_probe,
+       .erase_check = default_flash_blank_check,
+       .protect_check = str7x_protect_check,
+       .info = str7x_info,
+};