split startup.tcl file across modules
[fw/openocd] / src / flash / lpc2900.c
index 49d02598920221230958443cb6c64bc11617e572..1d5abd9d9ea7b7e12f74ddd1ab068da727ba9c56 100644 (file)
 
 
 #include "image.h"
-
-#include "lpc2900.h"
+#include "flash.h"
 #include "binarybuffer.h"
 #include "armv4_5.h"
+#include "algorithm.h"
 
 
 /* 1024 bytes */
@@ -171,14 +171,14 @@ struct lpc2900_flash_bank
 };
 
 
-static uint32_t lpc2900_wait_status(flash_bank_t *bank, uint32_t mask, int timeout);
-static void lpc2900_setup(struct flash_bank_s *bank);
-static uint32_t lpc2900_is_ready(struct flash_bank_s *bank);
-static uint32_t lpc2900_read_security_status(struct flash_bank_s *bank);
-static uint32_t lpc2900_run_bist128(struct flash_bank_s *bank,
+static uint32_t lpc2900_wait_status(struct flash_bank *bank, uint32_t mask, int timeout);
+static void lpc2900_setup(struct flash_bank *bank);
+static uint32_t lpc2900_is_ready(struct flash_bank *bank);
+static uint32_t lpc2900_read_security_status(struct flash_bank *bank);
+static uint32_t lpc2900_run_bist128(struct flash_bank *bank,
                                     uint32_t addr_from, uint32_t addr_to,
                                     uint32_t (*signature)[4] );
-static uint32_t lpc2900_address2sector(struct flash_bank_s *bank, uint32_t offset);
+static uint32_t lpc2900_address2sector(struct flash_bank *bank, uint32_t offset);
 static uint32_t lpc2900_calc_tr( uint32_t clock, uint32_t time );
 
 
@@ -194,7 +194,7 @@ static uint32_t lpc2900_calc_tr( uint32_t clock, uint32_t time );
  * @param[in] mask Mask to be used for INT_STATUS
  * @param[in] timeout Timeout in ms
  */
-static uint32_t lpc2900_wait_status( flash_bank_t *bank,
+static uint32_t lpc2900_wait_status( struct flash_bank *bank,
                                      uint32_t mask,
                                      int timeout )
 {
@@ -228,7 +228,7 @@ static uint32_t lpc2900_wait_status( flash_bank_t *bank,
  *
  * @param bank Pointer to the flash bank descriptor
  */
-static void lpc2900_setup( struct flash_bank_s *bank )
+static void lpc2900_setup( struct flash_bank *bank )
 {
        uint32_t fcra;
        struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
@@ -251,7 +251,7 @@ static void lpc2900_setup( struct flash_bank_s *bank )
  * Must have been successfully probed.
  * Must be halted.
  */
-static uint32_t lpc2900_is_ready( struct flash_bank_s *bank )
+static uint32_t lpc2900_is_ready( struct flash_bank *bank )
 {
        struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
 
@@ -275,7 +275,7 @@ static uint32_t lpc2900_is_ready( struct flash_bank_s *bank )
  *
  * @param bank Pointer to the flash bank descriptor
  */
-static uint32_t lpc2900_read_security_status( struct flash_bank_s *bank )
+static uint32_t lpc2900_read_security_status( struct flash_bank *bank )
 {
        uint32_t status;
        if( (status = lpc2900_is_ready( bank )) != ERROR_OK )
@@ -356,7 +356,7 @@ static uint32_t lpc2900_read_security_status( struct flash_bank_s *bank )
  * @param addr_to
  * @param signature
  */
-static uint32_t lpc2900_run_bist128(struct flash_bank_s *bank,
+static uint32_t lpc2900_run_bist128(struct flash_bank *bank,
                                     uint32_t addr_from,
                                     uint32_t addr_to,
                                     uint32_t (*signature)[4] )
@@ -393,7 +393,7 @@ static uint32_t lpc2900_run_bist128(struct flash_bank_s *bank,
  * @param bank Pointer to the flash bank descriptor
  * @param offset Offset address relative to bank start
  */
-static uint32_t lpc2900_address2sector( struct flash_bank_s *bank,
+static uint32_t lpc2900_address2sector( struct flash_bank *bank,
                                         uint32_t offset )
 {
        uint32_t address = bank->base + offset;
@@ -424,7 +424,7 @@ static uint32_t lpc2900_address2sector( struct flash_bank_s *bank,
  * @param pagenum Page number (0...7)
  * @param page Page array (FLASH_PAGE_SIZE bytes)
  */
-static int lpc2900_write_index_page( struct flash_bank_s *bank,
+static int lpc2900_write_index_page( struct flash_bank *bank,
                                      int pagenum,
                                      uint8_t (*page)[FLASH_PAGE_SIZE] )
 {
@@ -537,14 +537,14 @@ COMMAND_HANDLER(lpc2900_handle_signature_command)
        uint32_t signature[4];
 
 
-       if( argc < 1 )
+       if( CMD_ARGC < 1 )
        {
                LOG_WARNING( "Too few arguments. Call: lpc2900 signature <bank#>" );
                return ERROR_FLASH_BANK_INVALID;
        }
 
-       flash_bank_t *bank;
-       int retval = flash_command_get_bank_by_num(cmd_ctx, args[0], &bank);
+       struct flash_bank *bank;
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &bank);
        if (ERROR_OK != retval)
                return retval;
 
@@ -564,7 +564,7 @@ COMMAND_HANDLER(lpc2900_handle_signature_command)
                return status;
        }
 
-       command_print( cmd_ctx, "signature: 0x%8.8" PRIx32
+       command_print( CMD_CTX, "signature: 0x%8.8" PRIx32
                                          ":0x%8.8" PRIx32
                                          ":0x%8.8" PRIx32
                                          ":0x%8.8" PRIx32,
@@ -583,13 +583,13 @@ COMMAND_HANDLER(lpc2900_handle_signature_command)
  */
 COMMAND_HANDLER(lpc2900_handle_read_custom_command)
 {
-       if( argc < 2 )
+       if( CMD_ARGC < 2 )
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       flash_bank_t *bank;
-       int retval = flash_command_get_bank_by_num(cmd_ctx, args[0], &bank);
+       struct flash_bank *bank;
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &bank);
        if (ERROR_OK != retval)
                return retval;
 
@@ -623,7 +623,7 @@ COMMAND_HANDLER(lpc2900_handle_read_custom_command)
 
        /* Try and open the file */
        struct fileio fileio;
-       const char *filename = args[1];
+       const char *filename = CMD_ARGV[1];
        int ret = fileio_open( &fileio, filename, FILEIO_WRITE, FILEIO_BINARY );
        if( ret != ERROR_OK )
        {
@@ -631,7 +631,7 @@ COMMAND_HANDLER(lpc2900_handle_read_custom_command)
                return ret;
        }
 
-       uint32_t nwritten;
+       size_t nwritten;
        ret = fileio_write( &fileio, sizeof(customer),
                         (const uint8_t *)customer, &nwritten );
        if( ret != ERROR_OK )
@@ -654,13 +654,13 @@ COMMAND_HANDLER(lpc2900_handle_read_custom_command)
  */
 COMMAND_HANDLER(lpc2900_handle_password_command)
 {
-       if (argc < 2)
+       if (CMD_ARGC < 2)
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       flash_bank_t *bank;
-       int retval = flash_command_get_bank_by_num(cmd_ctx, args[0], &bank);
+       struct flash_bank *bank;
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &bank);
        if (ERROR_OK != retval)
                return retval;
 
@@ -668,15 +668,15 @@ COMMAND_HANDLER(lpc2900_handle_password_command)
 
 #define ISS_PASSWORD "I_know_what_I_am_doing"
 
-       lpc2900_info->risky = !strcmp( args[1], ISS_PASSWORD );
+       lpc2900_info->risky = !strcmp( CMD_ARGV[1], ISS_PASSWORD );
 
        if( !lpc2900_info->risky )
        {
-               command_print(cmd_ctx, "Wrong password (use '%s')", ISS_PASSWORD);
+               command_print(CMD_CTX, "Wrong password (use '%s')", ISS_PASSWORD);
                return ERROR_COMMAND_ARGUMENT_INVALID;
        }
 
-       command_print(cmd_ctx,
+       command_print(CMD_CTX,
                   "Potentially dangerous operation allowed in next command!");
 
        return ERROR_OK;
@@ -689,13 +689,13 @@ COMMAND_HANDLER(lpc2900_handle_password_command)
  */
 COMMAND_HANDLER(lpc2900_handle_write_custom_command)
 {
-       if (argc < 2)
+       if (CMD_ARGC < 2)
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       flash_bank_t *bank;
-       int retval = flash_command_get_bank_by_num(cmd_ctx, args[0], &bank);
+       struct flash_bank *bank;
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &bank);
        if (ERROR_OK != retval)
                return retval;
 
@@ -704,7 +704,7 @@ COMMAND_HANDLER(lpc2900_handle_write_custom_command)
        /* Check if command execution is allowed. */
        if( !lpc2900_info->risky )
        {
-               command_print( cmd_ctx, "Command execution not allowed!" );
+               command_print( CMD_CTX, "Command execution not allowed!" );
                return ERROR_COMMAND_ARGUMENT_INVALID;
        }
        lpc2900_info->risky = 0;
@@ -723,8 +723,8 @@ COMMAND_HANDLER(lpc2900_handle_write_custom_command)
        image.base_address = 0;
        image.start_address_set = 0;
 
-       const char *filename = args[1];
-       const char *type = (argc >= 3) ? args[2] : NULL;
+       const char *filename = CMD_ARGV[1];
+       const char *type = (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL;
        retval = image_open(&image, filename, type);
        if (retval != ERROR_OK)
        {
@@ -755,7 +755,7 @@ COMMAND_HANDLER(lpc2900_handle_write_custom_command)
        /* Page 4 */
        uint32_t offset = ISS_CUSTOMER_START1 % FLASH_PAGE_SIZE;
        memset( page, 0xff, FLASH_PAGE_SIZE );
-       uint32_t size_read;
+       size_t size_read;
        retval = image_read_section( &image, 0, 0,
                                     ISS_CUSTOMER_SIZE1, &page[offset], &size_read);
        if( retval != ERROR_OK )
@@ -799,14 +799,14 @@ COMMAND_HANDLER(lpc2900_handle_write_custom_command)
  */
 COMMAND_HANDLER(lpc2900_handle_secure_sector_command)
 {
-       if (argc < 3)
+       if (CMD_ARGC < 3)
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
        /* Get the bank descriptor */
-       flash_bank_t *bank;
-       int retval = flash_command_get_bank_by_num(cmd_ctx, args[0], &bank);
+       struct flash_bank *bank;
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &bank);
        if (ERROR_OK != retval)
                return retval;
 
@@ -815,7 +815,7 @@ COMMAND_HANDLER(lpc2900_handle_secure_sector_command)
        /* Check if command execution is allowed. */
        if( !lpc2900_info->risky )
        {
-               command_print( cmd_ctx, "Command execution not allowed! "
+               command_print( CMD_CTX, "Command execution not allowed! "
                "(use 'password' command first)");
                return ERROR_COMMAND_ARGUMENT_INVALID;
        }
@@ -823,13 +823,13 @@ COMMAND_HANDLER(lpc2900_handle_secure_sector_command)
 
        /* Read sector range, and do a sanity check. */
        int first, last;
-       COMMAND_PARSE_NUMBER(int, args[1], first);
-       COMMAND_PARSE_NUMBER(int, args[2], last);
+       COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], first);
+       COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], last);
        if( (first >= bank->num_sectors) ||
            (last >= bank->num_sectors) ||
            (first > last) )
        {
-               command_print( cmd_ctx, "Illegal sector range" );
+               command_print( CMD_CTX, "Illegal sector range" );
                return ERROR_COMMAND_ARGUMENT_INVALID;
        }
 
@@ -878,7 +878,7 @@ COMMAND_HANDLER(lpc2900_handle_secure_sector_command)
                }
        }
 
-       command_print( cmd_ctx,
+       command_print( CMD_CTX,
                "Sectors security will become effective after next power cycle");
 
        /* Update the sector security status */
@@ -898,14 +898,14 @@ COMMAND_HANDLER(lpc2900_handle_secure_sector_command)
  */
 COMMAND_HANDLER(lpc2900_handle_secure_jtag_command)
 {
-       if (argc < 1)
+       if (CMD_ARGC < 1)
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
        /* Get the bank descriptor */
-       flash_bank_t *bank;
-       int retval = flash_command_get_bank_by_num(cmd_ctx, args[0], &bank);
+       struct flash_bank *bank;
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &bank);
        if (ERROR_OK != retval)
                return retval;
 
@@ -914,7 +914,7 @@ COMMAND_HANDLER(lpc2900_handle_secure_jtag_command)
        /* Check if command execution is allowed. */
        if( !lpc2900_info->risky )
        {
-               command_print( cmd_ctx, "Command execution not allowed! "
+               command_print( CMD_CTX, "Command execution not allowed! "
                                        "(use 'password' command first)");
                return ERROR_COMMAND_ARGUMENT_INVALID;
        }
@@ -952,9 +952,9 @@ COMMAND_HANDLER(lpc2900_handle_secure_jtag_command)
 /**
  * Register private command handlers.
  */
-static int lpc2900_register_commands(struct command_context_s *cmd_ctx)
+static int lpc2900_register_commands(struct command_context *cmd_ctx)
 {
-       command_t *lpc2900_cmd = register_command(cmd_ctx, NULL, "lpc2900",
+       struct command *lpc2900_cmd = register_command(cmd_ctx, NULL, "lpc2900",
                                                  NULL, COMMAND_ANY, NULL);
 
        register_command(
@@ -1020,7 +1020,7 @@ FLASH_BANK_COMMAND_HANDLER(lpc2900_flash_bank_command)
 {
        struct lpc2900_flash_bank *lpc2900_info;
 
-       if (argc < 6)
+       if (CMD_ARGC < 6)
        {
                LOG_WARNING("incomplete flash_bank LPC2900 configuration");
                return ERROR_FLASH_BANK_INVALID;
@@ -1034,7 +1034,7 @@ FLASH_BANK_COMMAND_HANDLER(lpc2900_flash_bank_command)
         * (if clock too slow), or for erase time (clock too fast).
         */
        uint32_t clk_sys_fmc;
-       COMMAND_PARSE_NUMBER(u32, args[6], clk_sys_fmc);
+       COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], clk_sys_fmc);
        lpc2900_info->clk_sys_fmc = clk_sys_fmc * 1000;
 
        uint32_t clock_limit;
@@ -1070,7 +1070,7 @@ FLASH_BANK_COMMAND_HANDLER(lpc2900_flash_bank_command)
  * @param first First sector to be erased
  * @param last Last sector (including) to be erased
  */
-static int lpc2900_erase(struct flash_bank_s *bank, int first, int last)
+static int lpc2900_erase(struct flash_bank *bank, int first, int last)
 {
        uint32_t status;
        int sector;
@@ -1177,7 +1177,7 @@ static int lpc2900_erase(struct flash_bank_s *bank, int first, int last)
 
 
 
-static int lpc2900_protect(struct flash_bank_s *bank, int set, int first, int last)
+static int lpc2900_protect(struct flash_bank *bank, int set, int first, int last)
 {
        /* This command is not supported.
      * "Protection" in LPC2900 terms is handled transparently. Sectors will
@@ -1199,7 +1199,7 @@ static int lpc2900_protect(struct flash_bank_s *bank, int set, int first, int la
  * @param offset Start address (relative to bank start)
  * @param count Number of bytes to be programmed
  */
-static int lpc2900_write(struct flash_bank_s *bank, uint8_t *buffer,
+static int lpc2900_write(struct flash_bank *bank, uint8_t *buffer,
                          uint32_t offset, uint32_t count)
 {
        uint8_t page[FLASH_PAGE_SIZE];
@@ -1548,7 +1548,7 @@ static int lpc2900_write(struct flash_bank_s *bank, uint8_t *buffer,
  *
  * @param bank Pointer to the flash bank descriptor
  */
-static int lpc2900_probe(struct flash_bank_s *bank)
+static int lpc2900_probe(struct flash_bank *bank)
 {
        struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
        struct target *target = bank->target;
@@ -1751,7 +1751,7 @@ static int lpc2900_probe(struct flash_bank_s *bank)
  *
  * @param bank Pointer to the flash bank descriptor
  */
-static int lpc2900_erase_check(struct flash_bank_s *bank)
+static int lpc2900_erase_check(struct flash_bank *bank)
 {
        uint32_t status = lpc2900_is_ready(bank);
        if (status != ERROR_OK)
@@ -1809,7 +1809,7 @@ static int lpc2900_erase_check(struct flash_bank_s *bank)
  *
  * @param bank Pointer to the flash bank descriptor
  */
-static int lpc2900_protect_check(struct flash_bank_s *bank)
+static int lpc2900_protect_check(struct flash_bank *bank)
 {
        return lpc2900_read_security_status(bank);
 }
@@ -1822,7 +1822,7 @@ static int lpc2900_protect_check(struct flash_bank_s *bank)
  * @param buf Buffer to take the string
  * @param buf_size Maximum number of characters that the buffer can take
  */
-static int lpc2900_info(struct flash_bank_s *bank, char *buf, int buf_size)
+static int lpc2900_info(struct flash_bank *bank, char *buf, int buf_size)
 {
        snprintf(buf, buf_size, "lpc2900 flash driver");