]> git.sur5r.net Git - openocd/blobdiff - src/flash/at91sam7.c
arm_jtag_t -> struct arm_jtag
[openocd] / src / flash / at91sam7.c
index 315a3f3ffd195e1ef7dd441f9902eb87cda5c66f..c0b516c3f1da53585bf6e37f2bf5f8526466be59 100644 (file)
 #include "at91sam7.h"
 #include "binarybuffer.h"
 
-
-static int at91sam7_register_commands(struct command_context_s *cmd_ctx);
-static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
-static int at91sam7_erase(struct flash_bank_s *bank, int first, int last);
-static int at91sam7_protect(struct flash_bank_s *bank, int set, int first, int last);
-static int at91sam7_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
-static int at91sam7_probe(struct flash_bank_s *bank);
-//static int at91sam7_auto_probe(struct flash_bank_s *bank);
-static int at91sam7_erase_check(struct flash_bank_s *bank);
 static int at91sam7_protect_check(struct flash_bank_s *bank);
-static int at91sam7_info(struct flash_bank_s *bank, char *buf, int buf_size);
+static int at91sam7_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
 
 static uint32_t at91sam7_get_flash_status(target_t *target, int bank_number);
 static void at91sam7_set_flash_mode(flash_bank_t *bank, int mode);
 static uint32_t at91sam7_wait_status_busy(flash_bank_t *bank, uint32_t waitbits, int timeout);
 static int at91sam7_flash_command(struct flash_bank_s *bank, uint8_t cmd, uint16_t pagen);
-static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-
-flash_driver_t at91sam7_flash =
-{
-       .name = "at91sam7",
-       .register_commands = at91sam7_register_commands,
-       .flash_bank_command = at91sam7_flash_bank_command,
-       .erase = at91sam7_erase,
-       .protect = at91sam7_protect,
-       .write = at91sam7_write,
-       .probe = at91sam7_probe,
-       .auto_probe = at91sam7_probe,
-       .erase_check = at91sam7_erase_check,
-       .protect_check = at91sam7_protect_check,
-       .info = at91sam7_info
-};
 
 static uint32_t MC_FMR[4] = { 0xFFFFFF60, 0xFFFFFF70, 0xFFFFFF80, 0xFFFFFF90 };
 static uint32_t MC_FCR[4] = { 0xFFFFFF64, 0xFFFFFF74, 0xFFFFFF84, 0xFFFFFF94 };
@@ -112,14 +87,6 @@ static long SRAMSIZ[16] = {
 };
 #endif
 
-static int at91sam7_register_commands(struct command_context_s *cmd_ctx)
-{
-       command_t *at91sam7_cmd = register_command(cmd_ctx, NULL, "at91sam7", NULL, COMMAND_ANY, NULL);
-
-       register_command(cmd_ctx, at91sam7_cmd, "gpnvm", at91sam7_handle_gpnvm_command, COMMAND_EXEC,
-                                       "at91sam7 gpnvm <bit> set | clear, set or clear one gpnvm bit");
-       return ERROR_OK;
-}
 
 static uint32_t at91sam7_get_flash_status(target_t *target, int bank_number)
 {
@@ -132,7 +99,7 @@ static uint32_t at91sam7_get_flash_status(target_t *target, int bank_number)
 /* Read clock configuration and set at91sam7_info->mck_freq */
 static void at91sam7_read_clock_info(flash_bank_t *bank)
 {
-       at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
+       struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
        target_t *target = bank->target;
        uint32_t mckr, mcfr, pllr, mor;
        unsigned long tmp = 0, mainfreq;
@@ -212,7 +179,7 @@ static void at91sam7_read_clock_info(flash_bank_t *bank)
 static void at91sam7_set_flash_mode(flash_bank_t *bank, int mode)
 {
        uint32_t fmr, fmcn = 0, fws = 0;
-       at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
+       struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
        target_t *target = bank->target;
 
        if (mode && (mode != at91sam7_info->flashmode))
@@ -287,7 +254,7 @@ static uint32_t at91sam7_wait_status_busy(flash_bank_t *bank, uint32_t waitbits,
 static int at91sam7_flash_command(struct flash_bank_s *bank, uint8_t cmd, uint16_t pagen)
 {
        uint32_t fcr;
-       at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
+       struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
        target_t *target = bank->target;
 
        fcr = (0x5A << 24) | ((pagen&0x3FF) << 8) | cmd;
@@ -316,7 +283,7 @@ static int at91sam7_flash_command(struct flash_bank_s *bank, uint8_t cmd, uint16
 static int at91sam7_read_part_info(struct flash_bank_s *bank)
 {
        flash_bank_t *t_bank = bank;
-       at91sam7_flash_bank_t *at91sam7_info;
+       struct at91sam7_flash_bank *at91sam7_info;
        target_t *target = t_bank->target;
 
        uint16_t bnk, sec;
@@ -567,8 +534,8 @@ static int at91sam7_read_part_info(struct flash_bank_s *bank)
                        /* create a new flash bank element */
                        flash_bank_t *fb = malloc(sizeof(flash_bank_t));
                        fb->target = target;
-                       fb->driver = &at91sam7_flash;
-                       fb->driver_priv = malloc(sizeof(at91sam7_flash_bank_t));
+                       fb->driver = bank->driver;
+                       fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
                        fb->next = NULL;
 
                        /* link created bank in 'flash_banks' list and redirect t_bank */
@@ -584,7 +551,7 @@ static int at91sam7_read_part_info(struct flash_bank_s *bank)
                t_bank->num_sectors = sectors_num;
 
                /* allocate sectors */
-               t_bank->sectors = malloc(sectors_num * sizeof(flash_sector_t));
+               t_bank->sectors = malloc(sectors_num * sizeof(struct flash_sector));
                for (sec = 0; sec < sectors_num; sec++)
                {
                        t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
@@ -699,7 +666,7 @@ static int at91sam7_protect_check(struct flash_bank_s *bank)
        uint8_t lock_pos, gpnvm_pos;
        uint32_t status;
 
-       at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
+       struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
 
        if (at91sam7_info->cidr == 0)
        {
@@ -744,15 +711,15 @@ static int at91sam7_protect_check(struct flash_bank_s *bank)
        return ERROR_OK;
 }
 
-static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
+FLASH_BANK_COMMAND_HANDLER(at91sam7_flash_bank_command)
 {
        flash_bank_t *t_bank = bank;
-       at91sam7_flash_bank_t *at91sam7_info;
+       struct at91sam7_flash_bank *at91sam7_info;
        target_t *target = t_bank->target;
 
        uint32_t base_address;
        uint32_t bank_size;
-       uint32_t ext_freq;
+       uint32_t ext_freq = 0;
 
        int chip_width;
        int bus_width;
@@ -767,7 +734,7 @@ static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *
 
        int bnk, sec;
 
-       at91sam7_info = malloc(sizeof(at91sam7_flash_bank_t));
+       at91sam7_info = malloc(sizeof(struct at91sam7_flash_bank));
        t_bank->driver_priv = at91sam7_info;
 
        /* part wasn't probed for info yet */
@@ -776,33 +743,37 @@ static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *
        at91sam7_info->ext_freq = 0;
        at91sam7_info->flash_autodetection = 0;
 
-       if (argc == 14)
+       if (argc < 13)
        {
-               ext_freq = atol(args[13]) * 1000;
+               at91sam7_info->flash_autodetection = 1;
+               return ERROR_OK;
+       }
+
+       COMMAND_PARSE_NUMBER(u32, args[1], base_address);
+
+       COMMAND_PARSE_NUMBER(int, args[3], chip_width);
+       COMMAND_PARSE_NUMBER(int, args[4], bus_width);
+
+       COMMAND_PARSE_NUMBER(int, args[8], banks_num);
+       COMMAND_PARSE_NUMBER(int, args[9], num_sectors);
+       COMMAND_PARSE_NUMBER(u16, args[10], pages_per_sector);
+       COMMAND_PARSE_NUMBER(u16, args[11], page_size);
+       COMMAND_PARSE_NUMBER(u16, args[12], num_nvmbits);
+
+       if (argc == 14) {
+               unsigned long freq;
+               COMMAND_PARSE_NUMBER(ulong, args[13], freq);
+               ext_freq = freq * 1000;
                at91sam7_info->ext_freq = ext_freq;
        }
 
-       if ((argc != 14)                ||
-               (atoi(args[4]) == 0)        ||  /* bus width */
-               (atoi(args[8]) == 0)        ||  /* banks number */
-               (atoi(args[9]) == 0)        ||  /* sectors per bank */
-               (atoi(args[10]) == 0)       ||  /* pages per sector */
-               (atoi(args[11]) == 0)       ||  /* page size */
-               (atoi(args[12]) == 0))          /* nvmbits number */
+       if ((bus_width == 0) || (banks_num == 0) || (num_sectors == 0) ||
+               (pages_per_sector == 0) || (page_size == 0) || (num_nvmbits == 0))
        {
                at91sam7_info->flash_autodetection = 1;
                return ERROR_OK;
        }
 
-       base_address = strtoul(args[1], NULL, 0);
-       chip_width = atoi(args[3]);
-       bus_width = atoi(args[4]);
-       banks_num = atoi(args[8]);
-       num_sectors = atoi(args[9]);
-       pages_per_sector = atoi(args[10]);
-       page_size = atoi(args[11]);
-       num_nvmbits = atoi(args[12]);
-
        target_name = calloc(strlen(args[7]) + 1, sizeof(char));
        strcpy(target_name, args[7]);
 
@@ -816,8 +787,8 @@ static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *
                        /* create a new bank element */
                        flash_bank_t *fb = malloc(sizeof(flash_bank_t));
                        fb->target = target;
-                       fb->driver = &at91sam7_flash;
-                       fb->driver_priv = malloc(sizeof(at91sam7_flash_bank_t));
+                       fb->driver = bank->driver;
+                       fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
                        fb->next = NULL;
 
                        /* link created bank in 'flash_banks' list and redirect t_bank */
@@ -833,7 +804,7 @@ static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *
                t_bank->num_sectors = num_sectors;
 
                /* allocate sectors */
-               t_bank->sectors = malloc(num_sectors * sizeof(flash_sector_t));
+               t_bank->sectors = malloc(num_sectors * sizeof(struct flash_sector));
                for (sec = 0; sec < num_sectors; sec++)
                {
                        t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
@@ -858,7 +829,7 @@ static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *
 
 static int at91sam7_erase(struct flash_bank_s *bank, int first, int last)
 {
-       at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
+       struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
        int sec;
        uint32_t nbytes, pos;
        uint8_t *buffer;
@@ -930,7 +901,7 @@ static int at91sam7_protect(struct flash_bank_s *bank, int set, int first, int l
        int sector;
        uint32_t pagen;
 
-       at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
+       struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
 
        if (at91sam7_info->cidr == 0)
        {
@@ -977,7 +948,7 @@ static int at91sam7_protect(struct flash_bank_s *bank, int set, int first, int l
 static int at91sam7_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
 {
        int retval;
-       at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
+       struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
        target_t *target = bank->target;
        uint32_t dst_min_alignment, wcount, bytes_remaining = count;
        uint32_t first_page, last_page, pagen, buffer_pos;
@@ -1065,7 +1036,7 @@ static int at91sam7_probe(struct flash_bank_s *bank)
 static int at91sam7_info(struct flash_bank_s *bank, char *buf, int buf_size)
 {
        int printed;
-       at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
+       struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
 
        if (at91sam7_info->cidr == 0)
        {
@@ -1127,13 +1098,13 @@ static int at91sam7_info(struct flash_bank_s *bank, char *buf, int buf_size)
 *   The maximum number of write/erase cycles for Non volatile Memory bits is 100. this includes
 *   Lock Bits (LOCKx), General Purpose NVM bits (GPNVMx) and the Security Bit.
 */
-static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+COMMAND_HANDLER(at91sam7_handle_gpnvm_command)
 {
        flash_bank_t *bank;
        int bit;
        uint8_t  flashcmd;
        uint32_t status;
-       at91sam7_flash_bank_t *at91sam7_info;
+       struct at91sam7_flash_bank *at91sam7_info;
        int retval;
 
        if (argc != 2)
@@ -1147,7 +1118,7 @@ static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char
        {
                return ERROR_FLASH_BANK_INVALID;
        }
-       if (bank->driver != &at91sam7_flash)
+       if (strcmp(bank->driver->name, "at91sam7"))
        {
                command_print(cmd_ctx, "not an at91sam7 flash bank '%s'", args[0]);
                return ERROR_FLASH_BANK_INVALID;
@@ -1181,7 +1152,7 @@ static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char
                }
        }
 
-       bit = atoi(args[0]);
+       COMMAND_PARSE_NUMBER(int, args[0], bit);
        if ((bit < 0) || (bit >= at91sam7_info->num_nvmbits))
        {
                command_print(cmd_ctx, "gpnvm bit '#%s' is out of bounds for target %s", args[0], at91sam7_info->target_name);
@@ -1206,3 +1177,30 @@ static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char
 
        return ERROR_OK;
 }
+
+static int at91sam7_register_commands(struct command_context_s *cmd_ctx)
+{
+       command_t *at91sam7_cmd = register_command(cmd_ctx, NULL, "at91sam7",
+                       NULL, COMMAND_ANY, NULL);
+
+       register_command(cmd_ctx, at91sam7_cmd, "gpnvm",
+                       at91sam7_handle_gpnvm_command, COMMAND_EXEC,
+                       "at91sam7 gpnvm <bit> set | clear, "
+                       "set or clear one gpnvm bit");
+
+       return ERROR_OK;
+}
+
+struct flash_driver at91sam7_flash = {
+               .name = "at91sam7",
+               .register_commands = &at91sam7_register_commands,
+               .flash_bank_command = &at91sam7_flash_bank_command,
+               .erase = &at91sam7_erase,
+               .protect = &at91sam7_protect,
+               .write = &at91sam7_write,
+               .probe = &at91sam7_probe,
+               .auto_probe = &at91sam7_probe,
+               .erase_check = &at91sam7_erase_check,
+               .protect_check = &at91sam7_protect_check,
+               .info = &at91sam7_info,
+       };