Add target_write_memory wrapper:
[openocd.git] / src / flash / str9x.c
index 30c4e980833e742cb0bb90c31735bc434f3c5243..7de98b12309fac092c48110b0d672ced7b4df85b 100644 (file)
@@ -2,6 +2,12 @@
  *   Copyright (C) 2005 by Dominic Rath                                    *
  *   Dominic.Rath@gmx.de                                                   *
  *                                                                         *
+ *   Copyright (C) 2008 by Spencer Oliver                                  *
+ *   spen@spen-soft.co.uk                                                  *
+ *
+ *   Copyright (C) 2008 by Oyvind Harboe                                   *
+ *   oyvind.harboe@zylin.com                                               *
+ *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   the Free Software Foundation; either version 2 of the License, or     *
 #include "config.h"
 #endif
 
-#include "replacements.h"
-
 #include "str9x.h"
-#include "flash.h"
-#include "target.h"
-#include "log.h"
-#include "armv4_5.h"
 #include "arm966e.h"
-#include "algorithm.h"
-#include "binarybuffer.h"
 
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
 
 static u32 bank1start = 0x00080000;
 
-int str9x_register_commands(struct command_context_s *cmd_ctx);
-int str9x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
-int str9x_erase(struct flash_bank_s *bank, int first, int last);
-int str9x_protect(struct flash_bank_s *bank, int set, int first, int last);
-int str9x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
-int str9x_probe(struct flash_bank_s *bank);
-int str9x_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-int str9x_protect_check(struct flash_bank_s *bank);
-int str9x_info(struct flash_bank_s *bank, char *buf, int buf_size);
+static int str9x_register_commands(struct command_context_s *cmd_ctx);
+static int str9x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
+static int str9x_erase(struct flash_bank_s *bank, int first, int last);
+static int str9x_protect(struct flash_bank_s *bank, int set, int first, int last);
+static int str9x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
+static int str9x_probe(struct flash_bank_s *bank);
+//static int str9x_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
+static int str9x_protect_check(struct flash_bank_s *bank);
+static int str9x_info(struct flash_bank_s *bank, char *buf, int buf_size);
 
-int str9x_handle_flash_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
+static int str9x_handle_flash_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 
 flash_driver_t str9x_flash =
 {
@@ -65,7 +60,7 @@ flash_driver_t str9x_flash =
        .info = str9x_info
 };
 
-int str9x_register_commands(struct command_context_s *cmd_ctx)
+static int str9x_register_commands(struct command_context_s *cmd_ctx)
 {
        command_t *str9x_cmd = register_command(cmd_ctx, NULL, "str9x", NULL, COMMAND_ANY, NULL);
        
@@ -75,7 +70,7 @@ int str9x_register_commands(struct command_context_s *cmd_ctx)
        return ERROR_OK;
 }
 
-int str9x_build_block_list(struct flash_bank_s *bank)
+static int str9x_build_block_list(struct flash_bank_s *bank)
 {
        str9x_flash_bank_t *str9x_info = bank->driver_priv;
        
@@ -158,7 +153,8 @@ int str9x_build_block_list(struct flash_bank_s *bank)
 
 /* flash bank str9x <base> <size> 0 0 <target#>
  */
-int str9x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
+static int str9x_flash_bank_command(struct command_context_s *cmd_ctx,
+               char *cmd, char **args, int argc, struct flash_bank_s *bank)
 {
        str9x_flash_bank_t *str9x_info;
        
@@ -178,17 +174,20 @@ int str9x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char
        return ERROR_OK;
 }
 
-int str9x_protect_check(struct flash_bank_s *bank)
+static int str9x_protect_check(struct flash_bank_s *bank)
 {
+       int retval;
        str9x_flash_bank_t *str9x_info = bank->driver_priv;
        target_t *target = bank->target;
        
        int i;
        u32 adr;
        u32 status = 0;
+       u16 hstatus = 0;
 
        if (bank->target->state != TARGET_HALTED)
        {
+               LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
 
@@ -199,24 +198,48 @@ int str9x_protect_check(struct flash_bank_s *bank)
                if (str9x_info->bank1)
                {
                        adr = bank1start + 0x18;
-                       target_write_u16(target, adr, 0x90);
-                       target_read_u16(target, adr, (u16*)&status);
+                       if ((retval=target_write_u16(target, adr, 0x90))!=ERROR_OK)
+                       {
+                               return retval;
+                       }
+                       if ((retval=target_read_u16(target, adr, &hstatus))!=ERROR_OK)
+                       {
+                               return retval;
+                       }
+                       status = hstatus;
                }
                else
                {
                        adr = bank1start + 0x14;
-                       target_write_u16(target, adr, 0x90);
-                       target_read_u32(target, adr, &status);
+                       if ((retval=target_write_u16(target, adr, 0x90))!=ERROR_OK)
+                       {
+                               return retval;
+                       }
+                       if ((retval=target_read_u32(target, adr, &status))!=ERROR_OK)
+                       {
+                               return retval;
+                       }
                }
        }
        else
        {
                adr = bank1start + 0x10;
-               target_write_u16(target, adr, 0x90);
-               target_read_u16(target, adr, (u16*)&status);
+               if ((retval=target_write_u16(target, adr, 0x90))!=ERROR_OK)
+               {
+                       return retval;
+               }
+               if ((retval=target_read_u16(target, adr, &hstatus))!=ERROR_OK)
+               {
+                       return retval;
+               }
+               status = hstatus;
        }
        
-       target_write_u16(target, adr, 0xFF);
+       /* read array command */
+       if ((retval=target_write_u16(target, adr, 0xFF))!=ERROR_OK)
+       {
+               return retval;
+       }
        
        for (i = 0; i < bank->num_sectors; i++)
        {
@@ -229,47 +252,90 @@ int str9x_protect_check(struct flash_bank_s *bank)
        return ERROR_OK;
 }
 
-int str9x_erase(struct flash_bank_s *bank, int first, int last)
+static int str9x_erase(struct flash_bank_s *bank, int first, int last)
 {
        target_t *target = bank->target;
        int i;
        u32 adr;
        u8 status;
+       u8 erase_cmd;
        
        if (bank->target->state != TARGET_HALTED)
        {
+               LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
 
+       /* Check if we erase whole bank */
+       if ((first == 0) && (last == (bank->num_sectors - 1)))
+       {
+               /* Optimize to run erase bank command instead of sector */
+               erase_cmd = 0x80;
+       }
+       else
+       {
+               /* Erase sector command */
+               erase_cmd = 0x20;
+       }
+       
        for (i = first; i <= last; i++)
        {
+               int retval;
                adr = bank->base + bank->sectors[i].offset;
                
                /* erase sectors */
-               target_write_u16(target, adr, 0x20);
-               target_write_u16(target, adr, 0xD0);
+               if ((retval=target_write_u16(target, adr, erase_cmd))!=ERROR_OK)
+               {
+                       return retval;
+               }
+               if ((retval=target_write_u16(target, adr, 0xD0))!=ERROR_OK)
+               {
+                       return retval;
+               }
                
                /* get status */
-               target_write_u16(target, adr, 0x70);
-               
-               while (1) {
-                       target_read_u8(target, adr, &status);
+               if ((retval=target_write_u16(target, adr, 0x70))!=ERROR_OK)
+               {
+                       return retval;
+               }
+
+               int timeout;            
+               for (timeout=0; timeout<1000; timeout++) {
+                       if ((retval=target_read_u8(target, adr, &status))!=ERROR_OK)
+                       {
+                               return retval;
+                       }
                        if( status & 0x80 )
                                break;
-                       usleep(1000);
+                       alive_sleep(1);
+               }
+               if (timeout==1000)
+               {
+                       LOG_ERROR("erase timed out");
+                       return ERROR_FAIL;
                }
                
                /* clear status, also clear read array */
-               target_write_u16(target, adr, 0x50);
+               if ((retval=target_write_u16(target, adr, 0x50))!=ERROR_OK)
+               {
+                       return retval;
+               }
                
                /* read array command */
-               target_write_u16(target, adr, 0xFF);
+               if ((retval=target_write_u16(target, adr, 0xFF))!=ERROR_OK)
+               {
+                       return retval;
+               }
                
                if( status & 0x22 )
                {
                        LOG_ERROR("error erasing flash bank, status: 0x%x", status);
                        return ERROR_FLASH_OPERATION_FAILED;
                }
+               
+               /* If we ran erase bank command, we are finished */
+               if (erase_cmd == 0x80)
+                       break;
        }
        
        for (i = first; i <= last; i++)
@@ -278,7 +344,8 @@ int str9x_erase(struct flash_bank_s *bank, int first, int last)
        return ERROR_OK;
 }
 
-int str9x_protect(struct flash_bank_s *bank, int set, int first, int last)
+static int str9x_protect(struct flash_bank_s *bank,
+               int set, int first, int last)
 {
        target_t *target = bank->target;
        int i;
@@ -287,6 +354,7 @@ int str9x_protect(struct flash_bank_s *bank, int set, int first, int last)
        
        if (bank->target->state != TARGET_HALTED)
        {
+               LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
        
@@ -304,12 +372,19 @@ int str9x_protect(struct flash_bank_s *bank, int set, int first, int last)
                
                /* query status */
                target_read_u8(target, adr, &status);
+               
+               /* clear status, also clear read array */
+               target_write_u16(target, adr, 0x50);
+               
+               /* read array command */
+               target_write_u16(target, adr, 0xFF);
        }
        
        return ERROR_OK;
 }
 
-int str9x_write_block(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
+static int str9x_write_block(struct flash_bank_s *bank,
+               u8 *buffer, u32 offset, u32 count)
 {
        str9x_flash_bank_t *str9x_info = bank->driver_priv;
        target_t *target = bank->target;
@@ -318,7 +393,7 @@ int str9x_write_block(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 cou
        u32 address = bank->base + offset;
        reg_param_t reg_params[4];
        armv4_5_algorithm_t armv4_5_info;
-       int retval;
+       int retval = ERROR_OK;
        
        u32 str9x_flash_write_code[] = {
                                        /* write:                               */
@@ -390,15 +465,15 @@ int str9x_write_block(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 cou
 
                if ((retval = target->type->run_algorithm(target, 0, NULL, 4, reg_params, str9x_info->write_algorithm->address, str9x_info->write_algorithm->address + (18 * 4), 10000, &armv4_5_info)) != ERROR_OK)
                {
-                       target_free_working_area(target, source);
-                       target_free_working_area(target, str9x_info->write_algorithm);
                        LOG_ERROR("error executing str9x flash write algorithm");
-                       return ERROR_FLASH_OPERATION_FAILED;
+                       retval = ERROR_FLASH_OPERATION_FAILED;
+                       break;
                }
        
                if (buf_get_u32(reg_params[3].value, 0, 32) != 0x80)
                {
-                       return ERROR_FLASH_OPERATION_FAILED;
+                       retval = ERROR_FLASH_OPERATION_FAILED;
+                       break;
                }
                
                buffer += thisrun_count * 2;
@@ -414,10 +489,11 @@ int str9x_write_block(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 cou
        destroy_reg_param(&reg_params[2]);
        destroy_reg_param(&reg_params[3]);
        
-       return ERROR_OK;
+       return retval;
 }
 
-int str9x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
+static int str9x_write(struct flash_bank_s *bank,
+               u8 *buffer, u32 offset, u32 count)
 {
        target_t *target = bank->target;
        u32 words_remaining = (count / 2);
@@ -425,13 +501,14 @@ int str9x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
        u32 address = bank->base + offset;
        u32 bytes_written = 0;
        u8 status;
-       u32 retval;
+       int retval;
        u32 check_address = offset;
        u32 bank_adr;
        int i;
        
        if (bank->target->state != TARGET_HALTED)
        {
+               LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
 
@@ -492,16 +569,23 @@ int str9x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                
                /* write data command */
                target_write_u16(target, bank_adr, 0x40);
-               target->type->write_memory(target, address, 2, 1, buffer + bytes_written);
+               target_write_memory(target, address, 2, 1, buffer + bytes_written);
                
                /* get status command */
                target_write_u16(target, bank_adr, 0x70);
-               
-               while (1) {
+       
+               int timeout;
+               for (timeout=0; timeout<1000; timeout++)
+               {       
                        target_read_u8(target, bank_adr, &status);
                        if( status & 0x80 )
                                break;
-                       usleep(1000);
+                       alive_sleep(1);
+               }
+               if (timeout==1000)
+               {
+                       LOG_ERROR("write timed out");
+                       return ERROR_FAIL;
                }
                
                /* clear status reg and read array */
@@ -534,16 +618,23 @@ int str9x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
                
                /* write data comamnd */
                target_write_u16(target, bank_adr, 0x40);
-               target->type->write_memory(target, address, 2, 1, last_halfword);
+               target_write_memory(target, address, 2, 1, last_halfword);
                
                /* query status command */
                target_write_u16(target, bank_adr, 0x70);
                
-               while (1) {
+               int timeout;
+               for (timeout=0; timeout<1000; timeout++)
+               {       
                        target_read_u8(target, bank_adr, &status);
                        if( status & 0x80 )
                                break;
-                       usleep(1000);
+                       alive_sleep(1);
+               }
+               if (timeout==1000)
+               {
+                       LOG_ERROR("write timed out");
+                       return ERROR_FAIL;
                }
                
                /* clear status reg and read array */
@@ -559,23 +650,27 @@ int str9x_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
        return ERROR_OK;
 }
 
-int str9x_probe(struct flash_bank_s *bank)
+static int str9x_probe(struct flash_bank_s *bank)
 {
        return ERROR_OK;
 }
 
-int str9x_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+#if 0
+static int str9x_handle_part_id_command(struct command_context_s *cmd_ctx,
+               char *cmd, char **args, int argc)
 {
        return ERROR_OK;
 }
+#endif
 
-int str9x_info(struct flash_bank_s *bank, char *buf, int buf_size)
+static int str9x_info(struct flash_bank_s *bank, char *buf, int buf_size)
 {
        snprintf(buf, buf_size, "str9x flash driver info" );
        return ERROR_OK;
 }
 
-int str9x_handle_flash_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+static int str9x_handle_flash_config_command(struct command_context_s *cmd_ctx,
+               char *cmd, char **args, int argc)
 {
        str9x_flash_bank_t *str9x_info;
        flash_bank_t *bank;
@@ -599,6 +694,7 @@ int str9x_handle_flash_config_command(struct command_context_s *cmd_ctx, char *c
        
        if (bank->target->state != TARGET_HALTED)
        {
+               LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
        

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)