X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fflash%2Fat91sam3.c;h=804a35b44be37c80fc863268e345370c426e62f4;hp=449bcae786d67bc3d84c8966d1859f1c10515e2d;hb=57c5c5f46304a785092874a7dc0f6abc84794cc3;hpb=997df70f6e1e51d36c25ed21c4cbb37ab5f732ad diff --git a/src/flash/at91sam3.c b/src/flash/at91sam3.c index 449bcae786..804a35b44b 100644 --- a/src/flash/at91sam3.c +++ b/src/flash/at91sam3.c @@ -554,8 +554,8 @@ static const struct sam3_chip_details all_sam3_details[] = { **********************************************************************/ /* *ATMEL* style code - from the SAM3 driver code */ -/** Get the current status of the EEFC - * +/** + * Get the current status of the EEFC and * the value of some status bits (LOCKE, PROGE). * @param pPrivate - info about the bank * @param v - result goes here @@ -574,7 +574,8 @@ EFC_GetStatus(struct sam3_bank_private *pPrivate, uint32_t *v) return r; } -/** Get the result of the last executed command. +/** + * Get the result of the last executed command. * @param pPrivate - info about the bank * @param v - result goes here */ @@ -683,8 +684,8 @@ EFC_StartCommand(struct sam3_bank_private *pPrivate, return r; } -/** Performs the given command and wait until its completion (or an error). - * +/** + * Performs the given command and wait until its completion (or an error). * @param pPrivate - info about the bank * @param command - Command to perform. * @param argument - Optional command argument. @@ -741,13 +742,11 @@ EFC_PerformCommand(struct sam3_bank_private *pPrivate, -/** Read the unique ID. - * - * \param pPrivate - info about the bank - * +/** + * Read the unique ID. + * @param pPrivate - info about the bank * The unique ID is stored in the 'pPrivate' structure. */ - static int FLASHD_ReadUniqueID (struct sam3_bank_private *pPrivate) { @@ -787,7 +786,8 @@ FLASHD_ReadUniqueID (struct sam3_bank_private *pPrivate) } -/** Erases the entire flash. +/** + * Erases the entire flash. * @param pPrivate - the info about the bank. */ static int @@ -799,11 +799,11 @@ FLASHD_EraseEntireBank(struct sam3_bank_private *pPrivate) -/** Gets current GPNVM state. +/** + * Gets current GPNVM state. * @param pPrivate - info about the bank. * @param gpnvm - GPNVM bit index. * @param puthere - result stored here. - * */ //------------------------------------------------------------------------------ static int @@ -845,10 +845,11 @@ FLASHD_GetGPNVM(struct sam3_bank_private *pPrivate, unsigned gpnvm, unsigned *pu -/** Clears the selected GPNVM bit. - * @param gpnvm GPNVM index. - * - * Returns 0 if successful; otherwise returns an error code. +/** + * Clears the selected GPNVM bit. + * @param pPrivate info about the bank + * @param gpnvm GPNVM index. + * @returns 0 if successful; otherwise returns an error code. */ static int FLASHD_ClrGPNVM(struct sam3_bank_private *pPrivate, unsigned gpnvm) @@ -880,9 +881,10 @@ FLASHD_ClrGPNVM(struct sam3_bank_private *pPrivate, unsigned gpnvm) -/** Sets the selected GPNVM bit. - * @param gpnvm GPNVM index. - * +/** + * Sets the selected GPNVM bit. + * @param pPrivate info about the bank + * @param gpnvm GPNVM index. */ static int FLASHD_SetGPNVM(struct sam3_bank_private *pPrivate, unsigned gpnvm) @@ -916,12 +918,11 @@ FLASHD_SetGPNVM(struct sam3_bank_private *pPrivate, unsigned gpnvm) } -/** Returns a bit field (at most 64) of locked regions within a page. - * @param pPrivate - info about the bank - * @param v - where to store locked bits - * \param end End address of range. +/** + * Returns a bit field (at most 64) of locked regions within a page. + * @param pPrivate info about the bank + * @param v where to store locked bits */ - static int FLASHD_GetLockBits(struct sam3_bank_private *pPrivate, uint32_t *v) { @@ -936,10 +937,11 @@ FLASHD_GetLockBits(struct sam3_bank_private *pPrivate, uint32_t *v) } -/**Unlocks all the regions in the given address range. - * - * \param start_sector - first sector to unlock - * \param end_sector - last (inclusive) to unlock +/** + * Unlocks all the regions in the given address range. + * @param pPrivate info about the bank + * @param start_sector first sector to unlock + * @param end_sector last (inclusive) to unlock */ static int @@ -969,13 +971,12 @@ FLASHD_Unlock(struct sam3_bank_private *pPrivate, } -/** Locks regions - * +/** + * Locks regions + * @param pPrivate - info about the bank * @param start_sector - first sector to lock * @param end_sector - last sector (inclusive) to lock */ - - static int FLASHD_Lock(struct sam3_bank_private *pPrivate, unsigned start_sector, @@ -1307,9 +1308,9 @@ sam3_explain_ckgr_plla(struct sam3_chip *pChip) static void sam3_explain_mckr(struct sam3_chip *pChip) { - uint32_t css, pres,fin; - int pdiv; - const char *cp; + uint32_t css, pres, fin = 0; + int pdiv = 0; + const char *cp = NULL; css = sam3_reg_fieldname(pChip, "CSS", pChip->cfg.PMC_MCKR, 0, 2); switch (css & 3) { @@ -1460,9 +1461,9 @@ get_sam3_bank_private(flash_bank_t *bank) return (struct sam3_bank_private *)(bank->driver_priv); } -/* - * Given a pointer to where it goes in the structure.. - * Determine the register name, address from the all registers table. +/** + * Given a pointer to where it goes in the structure, + * determine the register name, address from the all registers table. */ static const struct sam3_reg_list * sam3_GetReg(struct sam3_chip *pChip, uint32_t *goes_here) @@ -1608,7 +1609,7 @@ static int sam3_protect_check(struct flash_bank_s *bank) { int r; - uint32_t v; + uint32_t v=0; unsigned x; struct sam3_bank_private *pPrivate; @@ -2121,19 +2122,25 @@ sam3_write(struct flash_bank_s *bank, struct sam3_bank_private *pPrivate; uint8_t *pagebuffer; + // incase we bail further below, set this to null + pagebuffer = NULL; + // ignore dumb requests if (count == 0) { - return ERROR_OK; + r = ERROR_OK; + goto done; } if (bank->target->state != TARGET_HALTED) { LOG_ERROR("Target not halted"); - return ERROR_TARGET_NOT_HALTED; + r = ERROR_TARGET_NOT_HALTED; + goto done; } pPrivate = get_sam3_bank_private(bank); if (!(pPrivate->probed)) { - return ERROR_FLASH_BANK_NOT_PROBED; + r = ERROR_FLASH_BANK_NOT_PROBED; + goto done; } @@ -2143,10 +2150,16 @@ sam3_write(struct flash_bank_s *bank, (unsigned int)(offset), (unsigned int)(count), (unsigned int)(pPrivate->size_bytes)); - return ERROR_FAIL; + r = ERROR_FAIL; + goto done; } - pagebuffer = alloca(pPrivate->page_size); + pagebuffer = malloc(pPrivate->page_size); + if( !pagebuffer ){ + LOG_ERROR("No memory for %d Byte page buffer", (int)(pPrivate->page_size)); + r = ERROR_FAIL; + goto done; + } // what page do we start & end in? page_cur = offset / pPrivate->page_size; @@ -2167,7 +2180,7 @@ sam3_write(struct flash_bank_s *bank, LOG_DEBUG("Special case, all in one page"); r = sam3_page_read(pPrivate, page_cur, pagebuffer); if (r != ERROR_OK) { - return r; + goto done; } page_offset = (offset & (pPrivate->page_size-1)); @@ -2177,9 +2190,10 @@ sam3_write(struct flash_bank_s *bank, r = sam3_page_write(pPrivate, page_cur, pagebuffer); if (r != ERROR_OK) { - return r; + goto done; } - return ERROR_OK; + r = ERROR_OK; + goto done; } // non-aligned start @@ -2189,7 +2203,7 @@ sam3_write(struct flash_bank_s *bank, // read the partial r = sam3_page_read(pPrivate, page_cur, pagebuffer); if (r != ERROR_OK) { - return r; + goto done; } // over-write with new data @@ -2200,7 +2214,7 @@ sam3_write(struct flash_bank_s *bank, r = sam3_page_write(pPrivate, page_cur, pagebuffer); if (r != ERROR_OK) { - return r; + goto done; } count -= n; @@ -2219,7 +2233,7 @@ sam3_write(struct flash_bank_s *bank, (count >= pPrivate->page_size)) { r = sam3_page_write(pPrivate, page_cur, buffer); if (r != ERROR_OK) { - return r; + goto done; } count -= pPrivate->page_size; buffer += pPrivate->page_size; @@ -2232,23 +2246,27 @@ sam3_write(struct flash_bank_s *bank, // we have a partial page r = sam3_page_read(pPrivate, page_cur, pagebuffer); if (r != ERROR_OK) { - return r; + goto done; } // data goes at start memcpy(pagebuffer, buffer, count); r = sam3_page_write(pPrivate, page_cur, pagebuffer); if (r != ERROR_OK) { - return r; + goto done; } buffer += count; count -= count; } LOG_DEBUG("Done!"); - return ERROR_OK; + r = ERROR_OK; + done: + if( pagebuffer ){ + free(pagebuffer); + } + return r; } -static int -sam3_handle_info_command(struct command_context_s *cmd_ctx, char *cmd, char **argv, int argc) +COMMAND_HANDLER(sam3_handle_info_command) { struct sam3_chip *pChip; void *vp; @@ -2324,11 +2342,9 @@ sam3_handle_info_command(struct command_context_s *cmd_ctx, char *cmd, char **ar return ERROR_OK; } -static int -sam3_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **argv, int argc) +COMMAND_HANDLER(sam3_handle_gpnvm_command) { unsigned x,v; - uint32_t v32; int r,who; struct sam3_chip *pChip; @@ -2369,22 +2385,20 @@ sam3_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **a who = -1; break; case 2: - if ((0 == strcmp(argv[0], "show")) && (0 == strcmp(argv[1], "all"))) { + if ((0 == strcmp(args[0], "show")) && (0 == strcmp(args[1], "all"))) { who = -1; } else { - r = parse_u32(argv[1], &v32); - if (r != ERROR_OK) { - command_print(cmd_ctx, "Not a number: %s", argv[1]); - return r; - } + uint32_t v32; + COMMAND_PARSE_NUMBER(u32, args[1], v32); who = v32; } break; } - if (0 == strcmp("show", argv[0])) { + if (0 == strcmp("show", args[0])) { if (who == -1) { showall: + r = ERROR_OK; for (x = 0 ; x < pChip->details.n_gpnvms ; x++) { r = FLASHD_GetGPNVM(&(pChip->details.bank[0]), x, &v); if (r != ERROR_OK) { @@ -2409,24 +2423,20 @@ sam3_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **a return ERROR_COMMAND_SYNTAX_ERROR; } - if (0 == strcmp("set", argv[0])) { + if (0 == strcmp("set", args[0])) { r = FLASHD_SetGPNVM(&(pChip->details.bank[0]), who); - } else if ((0 == strcmp("clr", argv[0])) || - (0 == strcmp("clear", argv[0]))) { // quietly accept both + } else if ((0 == strcmp("clr", args[0])) || + (0 == strcmp("clear", args[0]))) { // quietly accept both r = FLASHD_ClrGPNVM(&(pChip->details.bank[0]), who); } else { - command_print(cmd_ctx, "Unkown command: %s", argv[0]); + command_print(cmd_ctx, "Unkown command: %s", args[0]); r = ERROR_COMMAND_SYNTAX_ERROR; } return r; } -static int -sam3_handle_slowclk_command(struct command_context_s *cmd_ctx, char *cmd, char **argv, int argc) +COMMAND_HANDLER(sam3_handle_slowclk_command) { - uint32_t v; - int r; - struct sam3_chip *pChip; pChip = get_current_sam3(cmd_ctx); @@ -2440,8 +2450,10 @@ sam3_handle_slowclk_command(struct command_context_s *cmd_ctx, char *cmd, char * // show break; case 1: + { // set - r = parse_u32(argv[0], &v); + uint32_t v; + COMMAND_PARSE_NUMBER(u32, args[0], v); if (v > 200000) { // absurd slow clock of 200Khz? command_print(cmd_ctx,"Absurd/illegal slow clock freq: %d\n", (int)(v)); @@ -2449,7 +2461,7 @@ sam3_handle_slowclk_command(struct command_context_s *cmd_ctx, char *cmd, char * } pChip->cfg.slow_freq = v; break; - + } default: // error command_print(cmd_ctx,"Too many parameters"); @@ -2493,29 +2505,16 @@ sam3_register_commands(struct command_context_s *cmd_ctx) return ERROR_OK; } - -flash_driver_t at91sam3_flash = -{ - .name = "at91sam3", - .register_commands = sam3_register_commands, - - .flash_bank_command = sam3_flash_bank_command, - .erase = sam3_erase, - .protect = sam3_protect, - .write = sam3_write, - .probe = sam3_probe, - .auto_probe = sam3_auto_probe, - .erase_check = sam3_erase_check, - .protect_check = sam3_protect_check, - .info = sam3_info -}; - - - -/** - * Local Variables: ** - * mode: c ** - * c-basic-offset: 4 ** - * tab-width: 4 ** - * End: ** - */ +flash_driver_t at91sam3_flash = { + .name = "at91sam3", + .register_commands = &sam3_register_commands, + .flash_bank_command = &sam3_flash_bank_command, + .erase = &sam3_erase, + .protect = &sam3_protect, + .write = &sam3_write, + .probe = &sam3_probe, + .auto_probe = &sam3_auto_probe, + .erase_check = &sam3_erase_check, + .protect_check = &sam3_protect_check, + .info = &sam3_info, + };