X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fflash%2Fnor%2Fefm32.c;h=117cd8a1b89de88352d506a21678908bec10871d;hp=57566499908e54d74c7946ca136e5e8e7838e2bd;hb=b3cf9a665c00b7a7309a936d117e9f098943ad6b;hpb=7ae9154846f6e268c4fbc0c237c1b2c6301da2ce;ds=sidebyside diff --git a/src/flash/nor/efm32.c b/src/flash/nor/efm32.c index 5756649990..117cd8a1b8 100644 --- a/src/flash/nor/efm32.c +++ b/src/flash/nor/efm32.c @@ -10,7 +10,10 @@ * * * Copyright (C) 2013 by Roman Dmitrienko * * me@iamroman.org * - * + * * + * Copyright (C) 2014 Nemui Trinomius * + * nemuisan_kawausogasuki@live.jp * + * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * @@ -22,9 +25,7 @@ * 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, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * along with this program. If not, see . * ***************************************************************************/ #ifdef HAVE_CONFIG_H @@ -42,6 +43,12 @@ #define EFM_FAMILY_ID_GIANT_GECKO 72 #define EFM_FAMILY_ID_TINY_GECKO 73 #define EFM_FAMILY_ID_LEOPARD_GECKO 74 +#define EFM_FAMILY_ID_WONDER_GECKO 75 +#define EFM_FAMILY_ID_ZERO_GECKO 76 +#define EFM_FAMILY_ID_HAPPY_GECKO 77 +#define EZR_FAMILY_ID_WONDER_GECKO 120 +#define EZR_FAMILY_ID_LEOPARD_GECKO 121 +#define EZR_FAMILY_ID_HAPPY_GECKO 122 #define EFM32_FLASH_ERASE_TMO 100 #define EFM32_FLASH_WDATAREADY_TMO 100 @@ -56,7 +63,7 @@ #define EFM32_MSC_LOCK_BITS (EFM32_MSC_INFO_BASE+0x4000) #define EFM32_MSC_DEV_INFO (EFM32_MSC_INFO_BASE+0x8000) -/* PAGE_SIZE is only present in Leopard and Giant Gecko MCUs */ +/* PAGE_SIZE is only present in Leopard, Giant and Wonder Gecko MCUs */ #define EFM32_MSC_DI_PAGE_SIZE (EFM32_MSC_DEV_INFO+0x1e7) #define EFM32_MSC_DI_FLASH_SZ (EFM32_MSC_DEV_INFO+0x1f8) #define EFM32_MSC_DI_RAM_SZ (EFM32_MSC_DEV_INFO+0x1fa) @@ -85,7 +92,7 @@ struct efm32x_flash_bank { int probed; - uint8_t lb_page[LOCKBITS_PAGE_SZ]; + uint32_t lb_page[LOCKBITS_PAGE_SZ/4]; }; struct efm32_info { @@ -97,7 +104,7 @@ struct efm32_info { uint16_t page_size; }; -static int efm32x_write(struct flash_bank *bank, uint8_t *buffer, +static int efm32x_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count); static int efm32x_get_flash_size(struct flash_bank *bank, uint16_t *flash_sz) @@ -138,9 +145,13 @@ static int efm32x_read_info(struct flash_bank *bank, return ret; if (((cpuid >> 4) & 0xfff) == 0xc23) { - /* Cortex M3 device */ + /* Cortex-M3 device */ + } else if (((cpuid >> 4) & 0xfff) == 0xc24) { + /* Cortex-M4 device (WONDER GECKO) */ + } else if (((cpuid >> 4) & 0xfff) == 0xc60) { + /* Cortex-M0+ device */ } else { - LOG_ERROR("Target is not CortexM3"); + LOG_ERROR("Target is not Cortex-Mx Device"); return ERROR_FAIL; } @@ -167,19 +178,45 @@ static int efm32x_read_info(struct flash_bank *bank, if (EFM_FAMILY_ID_GECKO == efm32_info->part_family || EFM_FAMILY_ID_TINY_GECKO == efm32_info->part_family) efm32_info->page_size = 512; + else if (EFM_FAMILY_ID_ZERO_GECKO == efm32_info->part_family || + EFM_FAMILY_ID_HAPPY_GECKO == efm32_info->part_family || + EZR_FAMILY_ID_HAPPY_GECKO == efm32_info->part_family) + efm32_info->page_size = 1024; else if (EFM_FAMILY_ID_GIANT_GECKO == efm32_info->part_family || EFM_FAMILY_ID_LEOPARD_GECKO == efm32_info->part_family) { - uint8_t pg_size = 0; + if (efm32_info->prod_rev >= 18) { + uint8_t pg_size = 0; + ret = target_read_u8(bank->target, EFM32_MSC_DI_PAGE_SIZE, + &pg_size); + if (ERROR_OK != ret) + return ret; + + efm32_info->page_size = (1 << ((pg_size+10) & 0xff)); + } else { + /* EFM32 GG/LG errata: MEM_INFO_PAGE_SIZE is invalid + for MCUs with PROD_REV < 18 */ + if (efm32_info->flash_sz_kib < 512) + efm32_info->page_size = 2048; + else + efm32_info->page_size = 4096; + } + if ((2048 != efm32_info->page_size) && + (4096 != efm32_info->page_size)) { + LOG_ERROR("Invalid page size %u", efm32_info->page_size); + return ERROR_FAIL; + } + } else if (EFM_FAMILY_ID_WONDER_GECKO == efm32_info->part_family || + EZR_FAMILY_ID_WONDER_GECKO == efm32_info->part_family || + EZR_FAMILY_ID_LEOPARD_GECKO == efm32_info->part_family) { + uint8_t pg_size = 0; ret = target_read_u8(bank->target, EFM32_MSC_DI_PAGE_SIZE, &pg_size); if (ERROR_OK != ret) return ret; efm32_info->page_size = (1 << ((pg_size+10) & 0xff)); - - if ((2048 != efm32_info->page_size) && - (4096 != efm32_info->page_size)) { + if (2048 != efm32_info->page_size) { LOG_ERROR("Invalid page size %u", efm32_info->page_size); return ERROR_FAIL; } @@ -191,6 +228,72 @@ static int efm32x_read_info(struct flash_bank *bank, return ERROR_OK; } +/* + * Helper to create a human friendly string describing a part + */ +static int efm32x_decode_info(struct efm32_info *info, char *buf, int buf_size) +{ + int printed = 0; + + switch (info->part_family) { + case EZR_FAMILY_ID_WONDER_GECKO: + case EZR_FAMILY_ID_LEOPARD_GECKO: + case EZR_FAMILY_ID_HAPPY_GECKO: + printed = snprintf(buf, buf_size, "EZR32 "); + break; + default: + printed = snprintf(buf, buf_size, "EFM32 "); + } + + buf += printed; + buf_size -= printed; + + if (0 >= buf_size) + return ERROR_BUF_TOO_SMALL; + + switch (info->part_family) { + case EFM_FAMILY_ID_GECKO: + printed = snprintf(buf, buf_size, "Gecko"); + break; + case EFM_FAMILY_ID_GIANT_GECKO: + printed = snprintf(buf, buf_size, "Giant Gecko"); + break; + case EFM_FAMILY_ID_TINY_GECKO: + printed = snprintf(buf, buf_size, "Tiny Gecko"); + break; + case EFM_FAMILY_ID_LEOPARD_GECKO: + case EZR_FAMILY_ID_LEOPARD_GECKO: + printed = snprintf(buf, buf_size, "Leopard Gecko"); + break; + case EFM_FAMILY_ID_WONDER_GECKO: + case EZR_FAMILY_ID_WONDER_GECKO: + printed = snprintf(buf, buf_size, "Wonder Gecko"); + break; + case EFM_FAMILY_ID_ZERO_GECKO: + printed = snprintf(buf, buf_size, "Zero Gecko"); + break; + case EFM_FAMILY_ID_HAPPY_GECKO: + case EZR_FAMILY_ID_HAPPY_GECKO: + printed = snprintf(buf, buf_size, "Happy Gecko"); + break; + } + + buf += printed; + buf_size -= printed; + + if (0 >= buf_size) + return ERROR_BUF_TOO_SMALL; + + printed = snprintf(buf, buf_size, " - Rev: %d", info->prod_rev); + buf += printed; + buf_size -= printed; + + if (0 >= buf_size) + return ERROR_BUF_TOO_SMALL; + + return ERROR_OK; +} + /* flash bank efm32 0 0 */ FLASH_BANK_COMMAND_HANDLER(efm32x_flash_bank_command) @@ -284,7 +387,7 @@ static int efm32x_erase_page(struct flash_bank *bank, uint32_t addr) int ret = 0; uint32_t status = 0; - LOG_DEBUG("erasing flash page at 0x%08x", addr); + LOG_DEBUG("erasing flash page at 0x%08" PRIx32, addr); ret = target_write_u32(bank->target, EFM32_MSC_ADDRB, addr); if (ERROR_OK != ret) @@ -299,13 +402,13 @@ static int efm32x_erase_page(struct flash_bank *bank, uint32_t addr) if (ERROR_OK != ret) return ret; - LOG_DEBUG("status 0x%x", status); + LOG_DEBUG("status 0x%" PRIx32, status); if (status & EFM32_MSC_STATUS_LOCKED_MASK) { LOG_ERROR("Page is locked"); return ERROR_FAIL; } else if (status & EFM32_MSC_STATUS_INVADDR_MASK) { - LOG_ERROR("Invalid address 0x%x", addr); + LOG_ERROR("Invalid address 0x%" PRIx32, addr); return ERROR_FAIL; } @@ -357,12 +460,12 @@ static int efm32x_read_lock_data(struct flash_bank *bank) uint32_t *ptr = NULL; int ret = 0; - assert(!(bank->num_sectors & 0x1f)); + assert(bank->num_sectors > 0); - data_size = bank->num_sectors / 8; /* number of data bytes */ - data_size /= 4; /* ...and data dwords */ + /* calculate the number of 32-bit words to read (one lock bit per sector) */ + data_size = (bank->num_sectors + 31) / 32; - ptr = (uint32_t *)efm32x_info->lb_page; + ptr = efm32x_info->lb_page; for (i = 0; i < data_size; i++, ptr++) { ret = target_read_u32(target, EFM32_MSC_LOCK_BITS+i*4, ptr); @@ -375,7 +478,7 @@ static int efm32x_read_lock_data(struct flash_bank *bank) /* also, read ULW, DLW and MLW */ /* ULW, word 126 */ - ptr = ((uint32_t *)efm32x_info->lb_page) + 126; + ptr = efm32x_info->lb_page + 126; ret = target_read_u32(target, EFM32_MSC_LOCK_BITS+126*4, ptr); if (ERROR_OK != ret) { LOG_ERROR("Failed to read ULW"); @@ -383,7 +486,7 @@ static int efm32x_read_lock_data(struct flash_bank *bank) } /* DLW, word 127 */ - ptr = ((uint32_t *)efm32x_info->lb_page) + 127; + ptr = efm32x_info->lb_page + 127; ret = target_read_u32(target, EFM32_MSC_LOCK_BITS+127*4, ptr); if (ERROR_OK != ret) { LOG_ERROR("Failed to read DLW"); @@ -391,7 +494,7 @@ static int efm32x_read_lock_data(struct flash_bank *bank) } /* MLW, word 125, present in GG and LG */ - ptr = ((uint32_t *)efm32x_info->lb_page) + 125; + ptr = efm32x_info->lb_page + 125; ret = target_read_u32(target, EFM32_MSC_LOCK_BITS+125*4, ptr); if (ERROR_OK != ret) { LOG_ERROR("Failed to read MLW"); @@ -412,14 +515,14 @@ static int efm32x_write_lock_data(struct flash_bank *bank) return ret; } - return efm32x_write(bank, efm32x_info->lb_page, EFM32_MSC_LOCK_BITS, + return efm32x_write(bank, (uint8_t *)efm32x_info->lb_page, EFM32_MSC_LOCK_BITS, LOCKBITS_PAGE_SZ); } static int efm32x_get_page_lock(struct flash_bank *bank, size_t page) { struct efm32x_flash_bank *efm32x_info = bank->driver_priv; - uint32_t dw = ((uint32_t *)efm32x_info->lb_page)[page >> 5]; + uint32_t dw = efm32x_info->lb_page[page >> 5]; uint32_t mask = 0; mask = 1 << (page & 0x1f); @@ -430,7 +533,7 @@ static int efm32x_get_page_lock(struct flash_bank *bank, size_t page) static int efm32x_set_page_lock(struct flash_bank *bank, size_t page, int set) { struct efm32x_flash_bank *efm32x_info = bank->driver_priv; - uint32_t *dw = &((uint32_t *)efm32x_info->lb_page)[page >> 5]; + uint32_t *dw = &efm32x_info->lb_page[page >> 5]; uint32_t mask = 0; mask = 1 << (page & 0x1f); @@ -476,7 +579,7 @@ static int efm32x_protect(struct flash_bank *bank, int set, int first, int last) return ERROR_OK; } -static int efm32x_write_block(struct flash_bank *bank, uint8_t *buf, +static int efm32x_write_block(struct flash_bank *bank, const uint8_t *buf, uint32_t offset, uint32_t count) { struct target *target = bank->target; @@ -567,11 +670,10 @@ static int efm32x_write_block(struct flash_bank *bank, uint8_t *buf, &write_algorithm) != ERROR_OK) { LOG_WARNING("no working area available, can't do block memory writes"); return ERROR_TARGET_RESOURCE_NOT_AVAILABLE; - }; + } ret = target_write_buffer(target, write_algorithm->address, - sizeof(efm32x_flash_write_code), - (uint8_t *)efm32x_flash_write_code); + sizeof(efm32x_flash_write_code), efm32x_flash_write_code); if (ret != ERROR_OK) return ret; @@ -587,7 +689,7 @@ static int efm32x_write_block(struct flash_bank *bank, uint8_t *buf, LOG_WARNING("no large enough working area available, can't do block memory writes"); return ERROR_TARGET_RESOURCE_NOT_AVAILABLE; } - }; + } init_reg_param(®_params[0], "r0", 32, PARAM_IN_OUT); /* flash base (in), status (out) */ init_reg_param(®_params[1], "r1", 32, PARAM_OUT); /* count (word-32bit) */ @@ -602,7 +704,7 @@ static int efm32x_write_block(struct flash_bank *bank, uint8_t *buf, buf_set_u32(reg_params[4].value, 0, 32, address); armv7m_info.common_magic = ARMV7M_COMMON_MAGIC; - armv7m_info.core_mode = ARMV7M_MODE_ANY; + armv7m_info.core_mode = ARM_MODE_THREAD; ret = target_run_flash_async_algorithm(target, buf, count, 4, 0, NULL, @@ -673,13 +775,13 @@ static int efm32x_write_word(struct flash_bank *bank, uint32_t addr, if (ERROR_OK != ret) return ret; - LOG_DEBUG("status 0x%x", status); + LOG_DEBUG("status 0x%" PRIx32, status); if (status & EFM32_MSC_STATUS_LOCKED_MASK) { LOG_ERROR("Page is locked"); return ERROR_FAIL; } else if (status & EFM32_MSC_STATUS_INVADDR_MASK) { - LOG_ERROR("Invalid address 0x%x", addr); + LOG_ERROR("Invalid address 0x%" PRIx32, addr); return ERROR_FAIL; } @@ -713,7 +815,7 @@ static int efm32x_write_word(struct flash_bank *bank, uint32_t addr, return ERROR_OK; } -static int efm32x_write(struct flash_bank *bank, uint8_t *buffer, +static int efm32x_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count) { struct target *target = bank->target; @@ -739,9 +841,9 @@ static int efm32x_write(struct flash_bank *bank, uint8_t *buffer, "for padding buffer"); return ERROR_FAIL; } - LOG_INFO("odd number of bytes to write (%d), extending to %d " + LOG_INFO("odd number of bytes to write (%" PRIu32 "), extending to %" PRIu32 " " "and padding with 0xff", old_count, count); - memset(buffer, 0xff, count); + memset(new_buffer, 0xff, count); buffer = memcpy(new_buffer, buffer, old_count); } @@ -797,6 +899,7 @@ static int efm32x_probe(struct flash_bank *bank) int ret; int i; uint32_t base_address = 0x00000000; + char buf[256]; efm32x_info->probed = 0; memset(efm32x_info->lb_page, 0xff, LOCKBITS_PAGE_SZ); @@ -805,25 +908,11 @@ static int efm32x_probe(struct flash_bank *bank) if (ERROR_OK != ret) return ret; - switch (efm32_mcu_info.part_family) { - case EFM_FAMILY_ID_GECKO: - LOG_INFO("Gecko MCU detected"); - break; - case EFM_FAMILY_ID_GIANT_GECKO: - LOG_INFO("Giant Gecko MCU detected"); - break; - case EFM_FAMILY_ID_TINY_GECKO: - LOG_INFO("Tiny Gecko MCU detected"); - break; - case EFM_FAMILY_ID_LEOPARD_GECKO: - LOG_INFO("Leopard Gecko MCU detected"); - break; - default: - LOG_ERROR("Unsupported MCU family %d", - efm32_mcu_info.part_family); - return ERROR_FAIL; - } + ret = efm32x_decode_info(&efm32_mcu_info, buf, sizeof(buf)); + if (ERROR_OK != ret) + return ret; + LOG_INFO("detected part: %s", buf); LOG_INFO("flash size = %dkbytes", efm32_mcu_info.flash_sz_kib); LOG_INFO("flash page size = %dbytes", efm32_mcu_info.page_size); @@ -900,7 +989,6 @@ static int get_efm32x_info(struct flash_bank *bank, char *buf, int buf_size) { struct efm32_info info; int ret = 0; - int printed = 0; ret = efm32x_read_info(bank, &info); if (ERROR_OK != ret) { @@ -908,45 +996,53 @@ static int get_efm32x_info(struct flash_bank *bank, char *buf, int buf_size) return ret; } - printed = snprintf(buf, buf_size, "EFM32 "); - buf += printed; - buf_size -= printed; + return efm32x_decode_info(&info, buf, buf_size); +} - if (0 >= buf_size) - return ERROR_BUF_TOO_SMALL; +COMMAND_HANDLER(efm32x_handle_debuglock_command) +{ + struct target *target = NULL; - switch (info.part_family) { - case EFM_FAMILY_ID_GECKO: - printed = snprintf(buf, buf_size, "Gecko"); - break; - case EFM_FAMILY_ID_GIANT_GECKO: - printed = snprintf(buf, buf_size, "Giant Gecko"); - break; - case EFM_FAMILY_ID_TINY_GECKO: - printed = snprintf(buf, buf_size, "Tiny Gecko"); - break; - case EFM_FAMILY_ID_LEOPARD_GECKO: - printed = snprintf(buf, buf_size, "Leopard Gecko"); - break; - } + if (CMD_ARGC < 1) + return ERROR_COMMAND_SYNTAX_ERROR; - buf += printed; - buf_size -= printed; + struct flash_bank *bank; + int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank); + if (ERROR_OK != retval) + return retval; - if (0 >= buf_size) - return ERROR_BUF_TOO_SMALL; + struct efm32x_flash_bank *efm32x_info = bank->driver_priv; - printed = snprintf(buf, buf_size, " - Rev: %d", info.prod_rev); - buf += printed; - buf_size -= printed; + target = bank->target; - if (0 >= buf_size) - return ERROR_BUF_TOO_SMALL; + if (target->state != TARGET_HALTED) { + LOG_ERROR("Target not halted"); + return ERROR_TARGET_NOT_HALTED; + } + + uint32_t *ptr; + ptr = efm32x_info->lb_page + 127; + *ptr = 0; + + retval = efm32x_write_lock_data(bank); + if (ERROR_OK != retval) { + LOG_ERROR("Failed to write LB page"); + return retval; + } + + command_print(CMD_CTX, "efm32x debug interface locked, reset the device to apply"); return ERROR_OK; } static const struct command_registration efm32x_exec_command_handlers[] = { + { + .name = "debuglock", + .handler = efm32x_handle_debuglock_command, + .mode = COMMAND_EXEC, + .usage = "bank_id", + .help = "Lock the debug interface of the device.", + }, COMMAND_REGISTRATION_DONE };