use COMMAND_REGISTER macro
[fw/openocd] / src / flash / lpc2900.c
index 26ca67f5fa70ec5175032c394940d8c737293de1..c7f1b3a23d24e0bb398df6c19ac7ea832f68ee9c 100644 (file)
 
 
 #include "image.h"
-
-#include "lpc2900.h"
+#include "flash.h"
 #include "binarybuffer.h"
 #include "armv4_5.h"
+#include "algorithm.h"
 
 
 /* 1024 bytes */
 /**
  * Private data for \c lpc2900 flash driver.
  */
-typedef struct lpc2900_flash_bank_s
+struct lpc2900_flash_bank
 {
        /**
         * Holds the value read from CHIPID register.
@@ -168,32 +168,17 @@ typedef struct lpc2900_flash_bank_s
         */
        uint32_t max_ram_block;
 
-} lpc2900_flash_bank_t;
-
-
-
+};
 
-static int lpc2900_register_commands(struct command_context_s *cmd_ctx);
-static int lpc2900_flash_bank_command(struct command_context_s *cmd_ctx,
-                                      char *cmd, char **args, int argc,
-                                      struct flash_bank_s *bank);
-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_write(struct flash_bank_s *bank,
-                         uint8_t *buffer, uint32_t offset, uint32_t count);
-static int lpc2900_probe(struct flash_bank_s *bank);
-static int lpc2900_erase_check(struct flash_bank_s *bank);
-static int lpc2900_protect_check(struct flash_bank_s *bank);
-static int lpc2900_info(struct flash_bank_s *bank, char *buf, int buf_size);
 
-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 );
 
 
@@ -209,12 +194,12 @@ 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 )
 {
        uint32_t int_status;
-       target_t *target = bank->target;
+       struct target *target = bank->target;
 
 
        do
@@ -243,10 +228,10 @@ 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;
-       lpc2900_flash_bank_t *lpc2900_info = bank->driver_priv;
+       struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
 
 
        /* Power up the flash block */
@@ -266,9 +251,9 @@ 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 )
 {
-       lpc2900_flash_bank_t *lpc2900_info = bank->driver_priv;
+       struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
 
        if( lpc2900_info->chipid != EXPECTED_CHIPID )
        {
@@ -290,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 )
@@ -298,7 +283,7 @@ static uint32_t lpc2900_read_security_status( struct flash_bank_s *bank )
                return status;
        }
 
-       target_t *target = bank->target;
+       struct target *target = bank->target;
 
        /* Enable ISS access */
        target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB | FCTR_FS_ISS);
@@ -371,12 +356,12 @@ 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] )
 {
-       target_t *target = bank->target;
+       struct target *target = bank->target;
 
        /* Clear END_OF_MISR interrupt status */
        target_write_u32( target, INT_CLR_STATUS, INTSRC_END_OF_MISR );
@@ -408,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;
@@ -439,19 +424,19 @@ 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] )
 {
        /* Only pages 4...7 are user writable */
-       if( (pagenum < 4) || (pagenum > 7) )
+       if ((pagenum < 4) || (pagenum > 7))
        {
-               LOG_ERROR( "Refuse to burn index sector page %" PRIu32, pagenum );
+               LOG_ERROR("Refuse to burn index sector page %d", pagenum);
                return ERROR_COMMAND_ARGUMENT_INVALID;
        }
 
        /* Get target, and check if it's halted */
-       target_t *target = bank->target;
+       struct target *target = bank->target;
        if( target->state != TARGET_HALTED )
        {
                LOG_ERROR( "Target not halted" );
@@ -459,7 +444,7 @@ static int lpc2900_write_index_page( struct flash_bank_s *bank,
        }
 
        /* Private info */
-       lpc2900_flash_bank_t *lpc2900_info = bank->driver_priv;
+       struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
 
        /* Enable flash block and set the correct CRA clock of 66 kHz */
        lpc2900_setup( bank );
@@ -479,7 +464,7 @@ static int lpc2900_write_index_page( struct flash_bank_s *bank,
                                 bank->base + pagenum * FLASH_PAGE_SIZE,
                                 4, FLASH_PAGE_SIZE / 4, (uint8_t *)page) != ERROR_OK )
        {
-               LOG_ERROR( "Index sector write failed @ page %" PRIu32, pagenum );
+               LOG_ERROR("Index sector write failed @ page %d", pagenum);
                target_write_u32( target, FCTR, FCTR_FS_CS | FCTR_FS_WEB );
 
                return ERROR_FLASH_OPERATION_FAILED;
@@ -501,10 +486,10 @@ static int lpc2900_write_index_page( struct flash_bank_s *bank,
        /* Wait for the end of the write operation. If it's not over after one
         * second, something went dreadfully wrong... :-(
         */
-       if( lpc2900_wait_status( bank, INTSRC_END_OF_BURN, 1000 ) != ERROR_OK )
+       if (lpc2900_wait_status(bank, INTSRC_END_OF_BURN, 1000) != ERROR_OK)
        {
-               LOG_ERROR( "Index sector write failed @ page %" PRIu32, pagenum );
-               target_write_u32( target, FCTR, FCTR_FS_CS | FCTR_FS_WEB );
+               LOG_ERROR("Index sector write failed @ page %d", pagenum);
+               target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
 
                return ERROR_FLASH_OPERATION_FAILED;
        }
@@ -528,7 +513,7 @@ static uint32_t lpc2900_calc_tr( uint32_t clock, uint32_t time )
         * FPTR.TR = -------------------------------
         *                         512
         *
-        * The result is the 
+        * The result is the
         */
 
        uint32_t tr_val = (uint32_t)((((time / 1e6) * clock) + 511.0) / 512.0);
@@ -545,33 +530,23 @@ static uint32_t lpc2900_calc_tr( uint32_t clock, uint32_t time )
  *
  * Uses the Built-In-Self-Test (BIST) to generate a 128-bit hash value
  * of the flash content.
- *
- * @param cmd_ctx
- * @param cmd
- * @param args
- * @param argc
  */
-static int lpc2900_handle_signature_command( struct command_context_s *cmd_ctx,
-                                             char *cmd, char **args, int argc )
+COMMAND_HANDLER(lpc2900_handle_signature_command)
 {
-       flash_bank_t *bank;
        uint32_t status;
        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;
        }
 
-       /* Get the bank descriptor */
-       bank = get_flash_bank_by_num( strtoul(args[0], NULL, 0) );
-       if( !bank )
-       {
-               command_print( cmd_ctx, "flash bank '#%s' is out of bounds", args[0] );
-               return ERROR_OK;
-       }
+       struct flash_bank *bank;
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
+       if (ERROR_OK != retval)
+               return retval;
 
        if( bank->target->state != TARGET_HALTED )
        {
@@ -589,7 +564,7 @@ static int lpc2900_handle_signature_command( struct command_context_s *cmd_ctx,
                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,
@@ -605,35 +580,24 @@ static int lpc2900_handle_signature_command( struct command_context_s *cmd_ctx,
  *
  * Read customer info from index sector, and store that block of data into
  * a disk file. The format is binary.
- *
- * @param cmd_ctx
- * @param cmd
- * @param args
- * @param argc
  */
-static int lpc2900_handle_read_custom_command( struct command_context_s *cmd_ctx,
-                                               char *cmd, char **args, int argc )
+COMMAND_HANDLER(lpc2900_handle_read_custom_command)
 {
-       flash_bank_t *bank;
-
-
-       if( argc < 2 )
+       if( CMD_ARGC < 2 )
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       /* Get the bank descriptor */
-       bank = get_flash_bank_by_num( strtoul(args[0], NULL, 0) );
-       if( !bank )
-       {
-               command_print( cmd_ctx, "flash bank '#%s' is out of bounds", args[0] );
-               return ERROR_OK;
-       }
-       lpc2900_flash_bank_t *lpc2900_info = bank->driver_priv;
+       struct flash_bank *bank;
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
+       if (ERROR_OK != retval)
+               return retval;
+
+       struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
        lpc2900_info->risky = 0;
 
        /* Get target, and check if it's halted */
-       target_t *target = bank->target;
+       struct target *target = bank->target;
        if( target->state != TARGET_HALTED )
        {
                LOG_ERROR( "Target not halted" );
@@ -658,8 +622,8 @@ static int lpc2900_handle_read_custom_command( struct command_context_s *cmd_ctx
        target_write_u32( target, FCTR, FCTR_FS_CS | FCTR_FS_WEB );
 
        /* Try and open the file */
-       fileio_t fileio;
-       char *filename = args[1];
+       struct fileio fileio;
+       const char *filename = CMD_ARGV[1];
        int ret = fileio_open( &fileio, filename, FILEIO_WRITE, FILEIO_BINARY );
        if( ret != ERROR_OK )
        {
@@ -667,7 +631,7 @@ static int lpc2900_handle_read_custom_command( struct command_context_s *cmd_ctx
                return ret;
        }
 
-       uint32_t nwritten;
+       size_t nwritten;
        ret = fileio_write( &fileio, sizeof(customer),
                         (const uint8_t *)customer, &nwritten );
        if( ret != ERROR_OK )
@@ -687,43 +651,32 @@ static int lpc2900_handle_read_custom_command( struct command_context_s *cmd_ctx
 
 /**
  * Enter password to enable potentially dangerous options.
- *
- * @param cmd_ctx
- * @param cmd
- * @param args
- * @param argc
  */
-static int lpc2900_handle_password_command(struct command_context_s *cmd_ctx,
-                                           char *cmd, char **args, int argc)
+COMMAND_HANDLER(lpc2900_handle_password_command)
 {
-       flash_bank_t *bank;
-
-
-       if (argc < 2)
+       if (CMD_ARGC < 2)
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       /* Get the bank descriptor */
-       bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
-       if (!bank)
-       {
-               command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
-               return ERROR_OK;
-       }
-       lpc2900_flash_bank_t *lpc2900_info = bank->driver_priv;
+       struct flash_bank *bank;
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
+       if (ERROR_OK != retval)
+               return retval;
+
+       struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
 
 #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;
@@ -733,39 +686,31 @@ static int lpc2900_handle_password_command(struct command_context_s *cmd_ctx,
 
 /**
  * Write customer info from file to the index sector.
- *
- * @param cmd_ctx
- * @param cmd
- * @param args
- * @param argc
  */
-static int lpc2900_handle_write_custom_command( struct command_context_s *cmd_ctx,
-                                                char *cmd, char **args, int argc )
+COMMAND_HANDLER(lpc2900_handle_write_custom_command)
 {
-       if (argc < 2)
+       if (CMD_ARGC < 2)
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       /* Get the bank descriptor */
-       flash_bank_t *bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
-       if (!bank)
-       {
-               command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
-               return ERROR_OK;
-       }
-       lpc2900_flash_bank_t *lpc2900_info = bank->driver_priv;
+       struct flash_bank *bank;
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
+       if (ERROR_OK != retval)
+               return retval;
+
+       struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
 
        /* 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;
 
        /* Get target, and check if it's halted */
-       target_t *target = bank->target;
+       struct target *target = bank->target;
        if (target->state != TARGET_HALTED)
        {
                LOG_ERROR("Target not halted");
@@ -773,14 +718,14 @@ static int lpc2900_handle_write_custom_command( struct command_context_s *cmd_ct
        }
 
        /* The image will always start at offset 0 */
-       image_t image;
+       struct image image;
        image.base_address_set = 1;
        image.base_address = 0;
        image.start_address_set = 0;
 
-       char *filename = args[1];
-       char *type = (argc >= 3) ? args[2] : NULL;
-       int retval = image_open(&image, filename, type);
+       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)
        {
                return retval;
@@ -796,7 +741,8 @@ static int lpc2900_handle_write_custom_command( struct command_context_s *cmd_ct
        if( (image.sections[0].base_address != 0) ||
         (image.sections[0].size != ISS_CUSTOMER_SIZE) )
        {
-               LOG_ERROR("Incorrect image file size. Expected %" PRIu32 ", got %" PRIu32,
+               LOG_ERROR("Incorrect image file size. Expected %d, "
+                       "got %" PRIu32,
                    ISS_CUSTOMER_SIZE, image.sections[0].size);
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
@@ -809,7 +755,7 @@ static int lpc2900_handle_write_custom_command( struct command_context_s *cmd_ct
        /* 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 )
@@ -850,52 +796,45 @@ static int lpc2900_handle_write_custom_command( struct command_context_s *cmd_ct
 
 /**
  * Activate 'sector security' for a range of sectors.
- *
- * @param cmd_ctx
- * @param cmd
- * @param args
- * @param argc
  */
-static int lpc2900_handle_secure_sector_command(struct command_context_s *cmd_ctx,
-                                                char *cmd, char **args, int argc)
+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 = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
-       if (!bank)
-       {
-               command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
-               return ERROR_OK;
-       }
-       lpc2900_flash_bank_t *lpc2900_info = bank->driver_priv;
+       struct flash_bank *bank;
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
+       if (ERROR_OK != retval)
+               return retval;
+
+       struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
 
        /* 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;
        }
        lpc2900_info->risky = 0;
 
        /* Read sector range, and do a sanity check. */
-       int first = strtoul(args[1], NULL, 0);
-       int last = strtoul(args[2], NULL, 0);
+       int first, 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;
        }
 
        uint8_t page[FLASH_PAGE_SIZE];
        int sector;
-       int retval;
 
        /* Sectors in page 6 */
        if( (first <= 4) || (last >= 8) )
@@ -939,7 +878,7 @@ static int lpc2900_handle_secure_sector_command(struct command_context_s *cmd_ct
                }
        }
 
-       command_print( cmd_ctx,
+       command_print( CMD_CTX,
                "Sectors security will become effective after next power cycle");
 
        /* Update the sector security status */
@@ -956,33 +895,26 @@ static int lpc2900_handle_secure_sector_command(struct command_context_s *cmd_ct
 
 /**
  * Activate JTAG protection.
- *
- * @param cmd_ctx
- * @param cmd
- * @param args
- * @param argc
  */
-static int lpc2900_handle_secure_jtag_command(struct command_context_s *cmd_ctx,
-                                              char *cmd, char **args, int argc)
+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 = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
-       if (!bank)
-       {
-               command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
-               return ERROR_OK;
-       }
-       lpc2900_flash_bank_t *lpc2900_info = bank->driver_priv;
+       struct flash_bank *bank;
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
+       if (ERROR_OK != retval)
+               return retval;
+
+       struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
 
        /* 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;
        }
@@ -1000,7 +932,6 @@ static int lpc2900_handle_secure_jtag_command(struct command_context_s *cmd_ctx,
        page[0x30 +  3] = 0x7F;
 
        /* Write to page 5 */
-       int retval;
        if( (retval = lpc2900_write_index_page( bank, 5, &page ))
                        != ERROR_OK )
        {
@@ -1020,65 +951,39 @@ static int lpc2900_handle_secure_jtag_command(struct command_context_s *cmd_ctx,
 
 /**
  * Register private command handlers.
- *
- * @param cmd_ctx
  */
-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 = COMMAND_REGISTER(cmd_ctx, NULL, "lpc2900",
                                                  NULL, COMMAND_ANY, NULL);
 
-       register_command(
-           cmd_ctx,
-           lpc2900_cmd,
-           "signature",
-           lpc2900_handle_signature_command,
-           COMMAND_EXEC,
+       COMMAND_REGISTER(cmd_ctx, lpc2900_cmd, "signature",
+           &lpc2900_handle_signature_command, COMMAND_EXEC,
            "<bank> | "
-            "print device signature of flash bank");
-
-       register_command(
-           cmd_ctx,
-           lpc2900_cmd,
-           "read_custom",
-           lpc2900_handle_read_custom_command,
-           COMMAND_EXEC,
+           "print device signature of flash bank");
+
+       COMMAND_REGISTER(cmd_ctx, lpc2900_cmd, "read_custom",
+           &lpc2900_handle_read_custom_command, COMMAND_EXEC,
            "<bank> <filename> | "
             "read customer information from index sector to file");
 
-       register_command(
-           cmd_ctx,
-           lpc2900_cmd,
-           "password",
-           lpc2900_handle_password_command,
-           COMMAND_EXEC,
+       COMMAND_REGISTER(cmd_ctx, lpc2900_cmd, "password",
+           &lpc2900_handle_password_command, COMMAND_EXEC,
            "<bank> <password> | "
             "enter password to enable 'dangerous' options");
 
-       register_command(
-           cmd_ctx,
-           lpc2900_cmd,
-           "write_custom",
-           lpc2900_handle_write_custom_command,
-           COMMAND_EXEC,
+       COMMAND_REGISTER(cmd_ctx, lpc2900_cmd, "write_custom",
+           &lpc2900_handle_write_custom_command, COMMAND_EXEC,
            "<bank> <filename> [<type>] | "
             "write customer info from file to index sector");
 
-       register_command(
-           cmd_ctx,
-           lpc2900_cmd,
-           "secure_sector",
-           lpc2900_handle_secure_sector_command,
-           COMMAND_EXEC,
+       COMMAND_REGISTER(cmd_ctx, lpc2900_cmd, "secure_sector",
+           &lpc2900_handle_secure_sector_command, COMMAND_EXEC,
            "<bank> <first> <last> | "
             "activate sector security for a range of sectors");
 
-       register_command(
-           cmd_ctx,
-           lpc2900_cmd,
-           "secure_jtag",
-           lpc2900_handle_secure_jtag_command,
-           COMMAND_EXEC,
+       COMMAND_REGISTER(cmd_ctx, lpc2900_cmd, "secure_jtag",
+           &lpc2900_handle_secure_jtag_command, COMMAND_EXEC,
            "<bank> <level> | "
             "activate JTAG security");
 
@@ -1086,37 +991,27 @@ static int lpc2900_register_commands(struct command_context_s *cmd_ctx)
 }
 
 
-/**
- * Evaluate flash bank command.
- *
- * Syntax: flash bank lpc2900 0 0 0 0 target# system_base_clock
- *
- * @param cmd_ctx
- * @param cmd
- * @param args
- * @param argc
- * @param bank Pointer to the flash bank descriptor
- */
-static int lpc2900_flash_bank_command(struct command_context_s *cmd_ctx,
-                                      char *cmd, char **args, int argc,
-                                      struct flash_bank_s *bank)
+/// Evaluate flash bank command.
+FLASH_BANK_COMMAND_HANDLER(lpc2900_flash_bank_command)
 {
-       lpc2900_flash_bank_t *lpc2900_info;
+       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;
        }
 
-       lpc2900_info = malloc(sizeof(lpc2900_flash_bank_t));
+       lpc2900_info = malloc(sizeof(struct lpc2900_flash_bank));
        bank->driver_priv = lpc2900_info;
 
        /* Get flash clock.
         * Reject it if we can't meet the requirements for program time
         * (if clock too slow), or for erase time (clock too fast).
         */
-       lpc2900_info->clk_sys_fmc = strtoul(args[6], NULL, 0) * 1000;
+       uint32_t 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;
        /* Check program time limit */
@@ -1151,13 +1046,13 @@ static int lpc2900_flash_bank_command(struct command_context_s *cmd_ctx,
  * @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;
        int last_unsecured_sector;
-       target_t *target = bank->target;
-       lpc2900_flash_bank_t *lpc2900_info = bank->driver_priv;
+       struct target *target = bank->target;
+       struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
 
 
        status = lpc2900_is_ready(bank);
@@ -1258,7 +1153,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
@@ -1280,14 +1175,14 @@ 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];
        uint32_t status;
        uint32_t num_bytes;
-       target_t *target = bank->target;
-       lpc2900_flash_bank_t *lpc2900_info = bank->driver_priv;
+       struct target *target = bank->target;
+       struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
        int sector;
        int retval;
 
@@ -1375,7 +1270,7 @@ static int lpc2900_write(struct flash_bank_s *bank, uint8_t *buffer,
 
        /* Try working area allocation. Start with a large buffer, and try with
           reduced size if that fails. */
-       working_area_t *warea;
+       struct working_area *warea;
        uint32_t buffer_size = lpc2900_info->max_ram_block - 1 * KiB;
        while( (retval = target_alloc_working_area(target,
                                                   buffer_size + target_code_size,
@@ -1394,8 +1289,8 @@ static int lpc2900_write(struct flash_bank_s *bank, uint8_t *buffer,
 
        if( warea )
        {
-               reg_param_t reg_params[5];
-               armv4_5_algorithm_t armv4_5_info;
+               struct reg_param reg_params[5];
+               struct armv4_5_algorithm armv4_5_info;
 
                /* We can use target mode. Download the algorithm. */
                retval = target_write_buffer( target,
@@ -1477,12 +1372,13 @@ static int lpc2900_write(struct flash_bank_s *bank, uint8_t *buffer,
                        }
 
                        /* Skip the current sector if it is secured */
-                       if( bank->sectors[start_sector].is_protected )
+                       if (bank->sectors[start_sector].is_protected)
                        {
-                               LOG_DEBUG( "Skip secured sector %" PRIu32, start_sector );
+                               LOG_DEBUG("Skip secured sector %d",
+                                               start_sector);
 
                                /* Stop if this is the last sector */
-                               if( start_sector == bank->num_sectors - 1 )
+                               if (start_sector == bank->num_sectors - 1)
                                {
                                        break;
                                }
@@ -1628,10 +1524,10 @@ 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)
 {
-       lpc2900_flash_bank_t *lpc2900_info = bank->driver_priv;
-       target_t *target = bank->target;
+       struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
+       struct target *target = bank->target;
        int i = 0;
        uint32_t offset;
 
@@ -1763,9 +1659,9 @@ static int lpc2900_probe(struct flash_bank_s *bank)
        }
 
        /* Show detected device */
-       LOG_INFO("Flash bank %" PRIu32
+       LOG_INFO("Flash bank %d"
                 ": Device %s, %" PRIu32
-                " KiB in %" PRIu32 " sectors",
+                " KiB in %d sectors",
                 bank->bank_number,
                 lpc2900_info->target_name, bank->size / KiB,
                 bank->num_sectors);
@@ -1782,7 +1678,7 @@ static int lpc2900_probe(struct flash_bank_s *bank)
         * the logical flash number are translated into the physical flash numbers
         * of the device.
         */
-       bank->sectors = malloc(sizeof(flash_sector_t) * bank->num_sectors);
+       bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
 
        offset = 0;
        for (i = 0; i < bank->num_sectors; i++)
@@ -1805,7 +1701,7 @@ static int lpc2900_probe(struct flash_bank_s *bank)
                         * that has more than 19 sectors. Politely ask for a fix then.
                         */
                        bank->sectors[i].size = 0;
-                       LOG_ERROR("Never heard about sector %" PRIu32 " (FIXME please)", i);
+                       LOG_ERROR("Never heard about sector %d", i);
                }
 
                offset += bank->sectors[i].size;
@@ -1831,7 +1727,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)
@@ -1889,7 +1785,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);
 }
@@ -1902,7 +1798,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");
 
@@ -1910,7 +1806,7 @@ static int lpc2900_info(struct flash_bank_s *bank, char *buf, int buf_size)
 }
 
 
-flash_driver_t lpc2900_flash =
+struct flash_driver lpc2900_flash =
 {
        .name               = "lpc2900",
        .register_commands  = lpc2900_register_commands,