X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fflash%2Fnor%2Fxmc4xxx.c;h=aa26693ec6b28b1c7f63f0edb2808f450dd09512;hb=bf346292942868db6ed8a71e2c4c8b8359d6d300;hp=f47995079923b2b4d58c8fc4e15dba402ff34b5c;hpb=ed54838ea61afa647a2b6ce2121b4a5bc11995ce;p=fw%2Fopenocd diff --git a/src/flash/nor/xmc4xxx.c b/src/flash/nor/xmc4xxx.c index f47995079..aa26693ec 100644 --- a/src/flash/nor/xmc4xxx.c +++ b/src/flash/nor/xmc4xxx.c @@ -11,6 +11,8 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * +* You should have received a copy of the GNU General Public License * +* along with this program. If not, see . * ***************************************************************************/ #ifdef HAVE_CONFIG_H @@ -181,6 +183,7 @@ /* Flash controller configuration values */ #define FLASH_ID_XMC4500 0xA2 +#define FLASH_ID_XMC4300_XMC4700_4800 0x92 #define FLASH_ID_XMC4100_4200 0x9C #define FLASH_ID_XMC4400 0x9F @@ -239,18 +242,22 @@ struct xmc4xxx_command_seq { }; /* Sector capacities. See section 8 of xmc4x00_rm */ -static unsigned int sector_capacity_8[] = { +static const unsigned int sector_capacity_8[8] = { 16, 16, 16, 16, 16, 16, 16, 128 }; -static unsigned int sector_capacity_9[] = { +static const unsigned int sector_capacity_9[9] = { 16, 16, 16, 16, 16, 16, 16, 128, 256 }; -static unsigned int sector_capacity_12[] = { +static const unsigned int sector_capacity_12[12] = { 16, 16, 16, 16, 16, 16, 16, 16, 128, 256, 256, 256 }; +static const unsigned int sector_capacity_16[16] = { + 16, 16, 16, 16, 16, 16, 16, 16, 128, 256, 256, 256, 256, 256, 256, 256 +}; + static int xmc4xxx_write_command_sequence(struct flash_bank *bank, struct xmc4xxx_command_seq *seq, int seq_len) @@ -269,11 +276,11 @@ static int xmc4xxx_write_command_sequence(struct flash_bank *bank, static int xmc4xxx_load_bank_layout(struct flash_bank *bank) { - unsigned int *capacity = NULL; + const unsigned int *capacity = NULL; /* At this point, we know which flash controller ID we're * talking to and simply need to fill out the bank structure accordingly */ - LOG_DEBUG("%d sectors", bank->num_sectors); + LOG_DEBUG("%u sectors", bank->num_sectors); switch (bank->num_sectors) { case 8: @@ -285,8 +292,11 @@ static int xmc4xxx_load_bank_layout(struct flash_bank *bank) case 12: capacity = sector_capacity_12; break; + case 16: + capacity = sector_capacity_16; + break; default: - LOG_ERROR("Unexpected number of sectors, %d\n", + LOG_ERROR("Unexpected number of sectors, %u\n", bank->num_sectors); return ERROR_FAIL; } @@ -297,7 +307,7 @@ static int xmc4xxx_load_bank_layout(struct flash_bank *bank) uint32_t total_offset = 0; bank->sectors = calloc(bank->num_sectors, sizeof(struct flash_sector)); - for (int i = 0; i < bank->num_sectors; i++) { + for (unsigned int i = 0; i < bank->num_sectors; i++) { bank->sectors[i].size = capacity[i] * 1024; bank->sectors[i].offset = total_offset; bank->sectors[i].is_erased = -1; @@ -309,8 +319,8 @@ static int xmc4xxx_load_bank_layout(struct flash_bank *bank) } /* This part doesn't follow the typical standard of 0xff - * being the default padding value.*/ - bank->default_padded_value = 0x00; + * being the erased value.*/ + bank->default_padded_value = bank->erased_value = 0x00; return ERROR_OK; } @@ -373,6 +383,10 @@ static int xmc4xxx_probe(struct flash_bank *bank) bank->num_sectors = 12; LOG_DEBUG("XMC4xxx: XMC4500 detected."); break; + case FLASH_ID_XMC4300_XMC4700_4800: + bank->num_sectors = 16; + LOG_DEBUG("XMC4xxx: XMC4700/4800 detected."); + break; default: LOG_ERROR("XMC4xxx: Unexpected flash ID. got %02" PRIx8, flash_id); @@ -394,7 +408,7 @@ static int xmc4xxx_probe(struct flash_bank *bank) } static int xmc4xxx_get_sector_start_addr(struct flash_bank *bank, - int sector, uint32_t *ret_addr) + unsigned int sector, uint32_t *ret_addr) { /* Make sure we understand this sector */ if (sector > bank->num_sectors) @@ -524,7 +538,8 @@ static int xmc4xxx_erase_sector(struct flash_bank *bank, uint32_t address, return res; } -static int xmc4xxx_erase(struct flash_bank *bank, int first, int last) +static int xmc4xxx_erase(struct flash_bank *bank, unsigned int first, + unsigned int last) { struct xmc4xxx_flash_bank *fb = bank->driver_priv; int res; @@ -542,14 +557,14 @@ static int xmc4xxx_erase(struct flash_bank *bank, int first, int last) uint32_t tmp_addr; /* Loop through the sectors and erase each one */ - for (int i = first; i <= last; i++) { + for (unsigned int i = first; i <= last; i++) { res = xmc4xxx_get_sector_start_addr(bank, i, &tmp_addr); if (res != ERROR_OK) { - LOG_ERROR("Invalid sector %d", i); + LOG_ERROR("Invalid sector %u", i); return res; } - LOG_DEBUG("Erasing sector %d @ 0x%08"PRIx32, i, tmp_addr); + LOG_DEBUG("Erasing sector %u @ 0x%08"PRIx32, i, tmp_addr); res = xmc4xxx_erase_sector(bank, tmp_addr, false); if (res != ERROR_OK) { @@ -603,106 +618,6 @@ static int xmc4xxx_enter_page_mode(struct flash_bank *bank) return res; } -/* The logical erase value of an xmc4xxx memory cell is 0x00, - * therefore, we cannot use the built in flash blank check and must - * implement our own */ - -/** Checks whether a memory region is zeroed. */ -int xmc4xxx_blank_check_memory(struct target *target, - uint32_t address, uint32_t count, uint32_t *blank) -{ - struct working_area *erase_check_algorithm; - struct reg_param reg_params[3]; - struct armv7m_algorithm armv7m_info; - int retval; - - /* see contrib/loaders/erase_check/armv7m_0_erase_check.s for src */ - - static const uint8_t erase_check_code[] = { - /* loop: */ - 0x03, 0x78, /* ldrb r3, [r0] */ - 0x01, 0x30, /* adds r0, #1 */ - 0x1A, 0x43, /* orrs r2, r2, r3 */ - 0x01, 0x39, /* subs r1, r1, #1 */ - 0xFA, 0xD1, /* bne loop */ - 0x00, 0xBE /* bkpt #0 */ - }; - - /* make sure we have a working area */ - if (target_alloc_working_area(target, sizeof(erase_check_code), - &erase_check_algorithm) != ERROR_OK) - return ERROR_TARGET_RESOURCE_NOT_AVAILABLE; - - retval = target_write_buffer(target, erase_check_algorithm->address, - sizeof(erase_check_code), (uint8_t *)erase_check_code); - if (retval != ERROR_OK) - return retval; - - armv7m_info.common_magic = ARMV7M_COMMON_MAGIC; - armv7m_info.core_mode = ARM_MODE_THREAD; - - init_reg_param(®_params[0], "r0", 32, PARAM_OUT); - buf_set_u32(reg_params[0].value, 0, 32, address); - - init_reg_param(®_params[1], "r1", 32, PARAM_OUT); - buf_set_u32(reg_params[1].value, 0, 32, count); - - init_reg_param(®_params[2], "r2", 32, PARAM_IN_OUT); - buf_set_u32(reg_params[2].value, 0, 32, 0x00); - - retval = target_run_algorithm(target, - 0, - NULL, - 3, - reg_params, - erase_check_algorithm->address, - erase_check_algorithm->address + (sizeof(erase_check_code) - 2), - 10000, - &armv7m_info); - - if (retval == ERROR_OK) - *blank = buf_get_u32(reg_params[2].value, 0, 32); - - destroy_reg_param(®_params[0]); - destroy_reg_param(®_params[1]); - destroy_reg_param(®_params[2]); - - target_free_working_area(target, erase_check_algorithm); - - return retval; -} - -static int xmc4xxx_flash_blank_check(struct flash_bank *bank) -{ - struct target *target = bank->target; - int i; - int retval; - uint32_t blank; - - if (bank->target->state != TARGET_HALTED) { - LOG_ERROR("Target not halted"); - return ERROR_TARGET_NOT_HALTED; - } - - for (i = 0; i < bank->num_sectors; i++) { - uint32_t address = bank->base + bank->sectors[i].offset; - uint32_t size = bank->sectors[i].size; - - LOG_DEBUG("Erase checking 0x%08"PRIx32, address); - retval = xmc4xxx_blank_check_memory(target, address, size, &blank); - - if (retval != ERROR_OK) - break; - - if (blank == 0x00) - bank->sectors[i].is_erased = 1; - else - bank->sectors[i].is_erased = 0; - } - - return ERROR_OK; -} - static int xmc4xxx_write_page(struct flash_bank *bank, const uint8_t *pg_buf, uint32_t offset, bool user_config) { @@ -864,7 +779,7 @@ static int xmc4xxx_write(struct flash_bank *bank, const uint8_t *buffer, memcpy(&tmp_buf[start_pad], buffer, remaining); if (end_pad) { - LOG_INFO("Padding end of page @%08"PRIx32" by %d bytes", + LOG_INFO("Padding end of page @" TARGET_ADDR_FMT " by %d bytes", bank->base + offset, end_pad); memset(&tmp_buf[256 - end_pad], 0xff, end_pad); } @@ -937,6 +852,14 @@ static int xmc4xxx_get_info_command(struct flash_bank *bank, char *buf, int buf_ break; } break; + case 0x300: + dev_str = "XMC4300"; + + switch (rev_id) { + case 0x1: + rev_str = "AA"; + } + break; case 0x400: dev_str = "XMC4400"; @@ -971,6 +894,24 @@ static int xmc4xxx_get_info_command(struct flash_bank *bank, char *buf, int buf_ break; } break; + case 0x700: + dev_str = "XMC4700"; + + switch (rev_id) { + case 0x1: + rev_str = "EES-AA"; + break; + } + break; + case 0x800: + dev_str = "XMC4800"; + + switch (rev_id) { + case 0x1: + rev_str = "EES-AA"; + break; + } + break; default: snprintf(buf, buf_size, @@ -985,19 +926,19 @@ static int xmc4xxx_get_info_command(struct flash_bank *bank, char *buf, int buf_ snprintf(prot_str, sizeof(prot_str), "\nFlash is read protected"); bool otp_enabled = false; - for (int i = 0; i < bank->num_sectors; i++) + for (unsigned int i = 0; i < bank->num_sectors; i++) if (fb->write_prot_otp[i]) otp_enabled = true; /* If OTP Write protection is enabled (User 2), list each * sector that has it enabled */ - char otp_str[8]; + char otp_str[14]; if (otp_enabled) { strcat(prot_str, "\nOTP Protection is enabled for sectors:\n"); - for (int i = 0; i < bank->num_sectors; i++) { + for (unsigned int i = 0; i < bank->num_sectors; i++) { if (fb->write_prot_otp[i]) { snprintf(otp_str, sizeof(otp_str), "- %d\n", i); - strncat(prot_str, otp_str, ARRAY_SIZE(otp_str)); + strncat(prot_str, otp_str, sizeof(prot_str) - strlen(prot_str) - 1); } } } @@ -1066,11 +1007,6 @@ static int xmc4xxx_flash_unprotect(struct flash_bank *bank, int32_t level) uint32_t addr; int res; - if ((level < 0) || (level > 1)) { - LOG_ERROR("Invalid user level. Must be 0-1"); - return ERROR_FAIL; - } - switch (level) { case 0: addr = UCB0_BASE; @@ -1078,6 +1014,9 @@ static int xmc4xxx_flash_unprotect(struct flash_bank *bank, int32_t level) case 1: addr = UCB1_BASE; break; + default: + LOG_ERROR("Invalid user level. Must be 0-1"); + return ERROR_FAIL; } res = xmc4xxx_erase_sector(bank, addr, true); @@ -1090,7 +1029,7 @@ static int xmc4xxx_flash_unprotect(struct flash_bank *bank, int32_t level) /* Reference: "XMC4500 Flash Protection.pptx" app note */ static int xmc4xxx_flash_protect(struct flash_bank *bank, int level, bool read_protect, - int first, int last) + unsigned int first, unsigned int last) { /* User configuration block buffers */ uint8_t ucp0_buf[8 * sizeof(uint32_t)] = {0}; @@ -1149,7 +1088,7 @@ static int xmc4xxx_flash_protect(struct flash_bank *bank, int level, bool read_p /* We need to fill out the procon register representation * that we will be writing to the device */ - for (int i = first; i <= last; i++) + for (unsigned int i = first; i <= last; i++) procon |= 1 << i; /* If read protection is requested, set the appropriate bit @@ -1206,7 +1145,8 @@ static int xmc4xxx_flash_protect(struct flash_bank *bank, int level, bool read_p return ERROR_OK; } -static int xmc4xxx_protect(struct flash_bank *bank, int set, int first, int last) +static int xmc4xxx_protect(struct flash_bank *bank, int set, unsigned int first, + unsigned int last) { int ret; struct xmc4xxx_flash_bank *fb = bank->driver_priv; @@ -1255,7 +1195,7 @@ static int xmc4xxx_protect_check(struct flash_bank *bank) return ret; } - int sectors = bank->num_sectors; + unsigned int sectors = bank->num_sectors; /* On devices with 12 sectors, sectors 10 & 11 are ptected * together instead of individually */ @@ -1263,7 +1203,7 @@ static int xmc4xxx_protect_check(struct flash_bank *bank) sectors--; /* Clear the protection status */ - for (int i = 0; i < bank->num_sectors; i++) { + for (unsigned int i = 0; i < bank->num_sectors; i++) { bank->sectors[i].is_protected = 0; fb->write_prot_otp[i] = false; } @@ -1276,7 +1216,7 @@ static int xmc4xxx_protect_check(struct flash_bank *bank) /* Check for write protection on every available * sector */ - for (int j = 0; j < sectors; j++) { + for (unsigned int j = 0; j < sectors; j++) { int set = (protection[i] & (1 << j)) ? 1 : 0; bank->sectors[j].is_protected |= set; @@ -1346,9 +1286,9 @@ COMMAND_HANDLER(xmc4xxx_handle_flash_password_command) fb->pw_set = true; - command_print(CMD_CTX, "XMC4xxx flash passwords set to:\n"); - command_print(CMD_CTX, "-0x%08"PRIx32"\n", fb->pw1); - command_print(CMD_CTX, "-0x%08"PRIx32"\n", fb->pw2); + command_print(CMD, "XMC4xxx flash passwords set to:\n"); + command_print(CMD, "-0x%08"PRIx32"\n", fb->pw1); + command_print(CMD, "-0x%08"PRIx32"\n", fb->pw2); return ERROR_OK; } @@ -1391,7 +1331,8 @@ static const struct command_registration xmc4xxx_exec_command_handlers[] = { .usage = "bank_id user_level[0-1]", .help = "Permanently Removes flash protection (read and write) " "for the specified user level", - }, COMMAND_REGISTRATION_DONE + }, + COMMAND_REGISTRATION_DONE }; static const struct command_registration xmc4xxx_command_handlers[] = { @@ -1405,7 +1346,7 @@ static const struct command_registration xmc4xxx_command_handlers[] = { COMMAND_REGISTRATION_DONE }; -struct flash_driver xmc4xxx_flash = { +const struct flash_driver xmc4xxx_flash = { .name = "xmc4xxx", .commands = xmc4xxx_command_handlers, .flash_bank_command = xmc4xxx_flash_bank_command, @@ -1414,8 +1355,9 @@ struct flash_driver xmc4xxx_flash = { .read = default_flash_read, .probe = xmc4xxx_probe, .auto_probe = xmc4xxx_probe, - .erase_check = xmc4xxx_flash_blank_check, + .erase_check = default_flash_blank_check, .info = xmc4xxx_get_info_command, .protect_check = xmc4xxx_protect_check, .protect = xmc4xxx_protect, + .free_driver_priv = default_flash_free_driver_priv, };