X-Git-Url: https://review.openocd.org/gitweb?a=blobdiff_plain;f=src%2Fflash%2Fat91sam7.c;h=c0b516c3f1da53585bf6e37f2bf5f8526466be59;hb=632fd663a821047df9a9b965ec1c35d6b034ebba;hp=04790617fafb2252177b3dabcd74ad1662aed375;hpb=2e779198535580515dfa9c8bfe1f3fe08abdb84b;p=openocd.git diff --git a/src/flash/at91sam7.c b/src/flash/at91sam7.c index 04790617fa..c0b516c3f1 100644 --- a/src/flash/at91sam7.c +++ b/src/flash/at91sam7.c @@ -20,20 +20,30 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ****************************************************************************/ -/*************************************************************************************************************************************************************************************** +/*************************************************************************** * * New flash setup command: * -* flash bank [ ] +* flash bank +* [ +* +* +* ] * * - MUST be used if clock is from external source, -* CAN be used if main oscillator frequency is known (recomended) +* CAN be used if main oscillator frequency is known (recommended) * Examples: -* flash bank at91sam7 0x00100000 0 0 4 0 0 AT91SAM7XC256 1 16 64 256 3 25000 ==== RECOMENDED ============ -* flash bank at91sam7 0 0 0 0 0 0 0 0 0 0 0 0 25000 (auto-detection, except for clock) ==== RECOMENDED ============ -* flash bank at91sam7 0x00100000 0 0 4 0 0 AT91SAM7XC256 1 16 64 256 3 0 ==== NOT RECOMENDED !!! ==== -* flash bank at91sam7 0 0 0 0 0 (old style, full auto-detection) ==== NOT RECOMENDED !!! ==== -****************************************************************************************************************************************************************************************/ +* ==== RECOMMENDED (covers clock speed) ============ +* flash bank at91sam7 0x00100000 0 0 4 $_TARGETNAME AT91SAM7XC256 1 16 64 256 3 25000 +* (if auto-detect fails; provides clock spec) +* flash bank at91sam7 0 0 0 0 $_TARGETNAME 0 0 0 0 0 0 25000 +* (auto-detect everything except the clock) +* ==== NOT RECOMMENDED !!! (clock speed is not configured) ==== +* flash bank at91sam7 0x00100000 0 0 4 $_TARGETNAME AT91SAM7XC256 1 16 64 256 3 0 +* (if auto-detect fails) +* flash bank at91sam7 0 0 0 0 $_TARGETNAME +* (old style, auto-detect everything) +****************************************************************************/ #ifdef HAVE_CONFIG_H #include "config.h" @@ -42,38 +52,13 @@ #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 }; @@ -102,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 set | clear, set or clear one gpnvm bit"); - return ERROR_OK; -} static uint32_t at91sam7_get_flash_status(target_t *target, int bank_number) { @@ -122,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; @@ -202,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)) @@ -277,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; @@ -306,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; @@ -557,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 */ @@ -574,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; @@ -689,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) { @@ -734,25 +711,15 @@ static int at91sam7_protect_check(struct flash_bank_s *bank) return ERROR_OK; } -/*************************************************************************************************************************************************************************************** -# flash bank [ ] -# - MUST be used if clock is from external source -# CAN be used if main oscillator frequency is known -# Examples: -# flash bank at91sam7 0x00100000 0 0 4 0 0 AT91SAM7XC256 1 16 64 256 3 25000 ==== RECOMENDED ============ -# flash bank at91sam7 0 0 0 0 0 0 0 0 0 0 0 0 25000 (auto-detection, except for clock) ==== RECOMENDED ============ -# flash bank at91sam7 0x00100000 0 0 4 0 0 AT91SAM7XC256 1 16 64 256 3 0 ==== NOT RECOMENDED !!! ==== -# flash bank at91sam7 0 0 0 0 0 (old style, full auto-detection) ==== NOT RECOMENDED !!! ==== -****************************************************************************************************************************************************************************************/ -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 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, + };