38 #define SIM_SRSID 0x40048000
39 #define ICS_C1 0x40064000
40 #define ICS_C2 0x40064001
41 #define ICS_C3 0x40064002
42 #define ICS_C4 0x40064003
43 #define ICS_S 0x40064004
44 #define SIM_BUSDIV 0x40048018
45 #define SIM_CLKDIV_KE06 0x40048024
46 #define SIM_CLKDIV_KE04_44_64_80 0x40048024
47 #define SIM_CLKDIV_KE04_16_20_24 0x4004801C
48 #define WDOG_CS1 0x40052000
50 #define ICS_C2_BDIV_MASK 0xE0
51 #define ICS_C2_BDIV_SHIFT 5
52 #define ICS_C2_BDIV(x) (((uint8_t)(((uint8_t)(x))<<ICS_C2_BDIV_SHIFT))&ICS_C2_BDIV_MASK)
53 #define ICS_S_LOCK_MASK 0x40
54 #define ICS_C4_SCFTRIM_MASK 0x1
55 #define SIM_CLKDIV_OUTDIV2_MASK 0x1000000
56 #define FTMRX_FCLKDIV_FDIV_MASK 0x3F
57 #define FTMRX_FCLKDIV_FDIV_SHIFT 0
58 #define FTMRX_FCLKDIV_FDIV(x) (((uint8_t)(((uint8_t)(x))<<FTMRX_FCLKDIV_FDIV_SHIFT))&FTMRX_FCLKDIV_FDIV_MASK)
59 #define FTMRX_FCLKDIV_FDIVLCK_MASK 0x40
60 #define FTMRX_FCLKDIV_FDIVLCK_SHIFT 6
61 #define FTMRX_FCLKDIV_FDIVLD_MASK 0x80
62 #define FTMRX_FCLKDIV_FDIVLD_SHIFT 7
63 #define FTMRX_FSTAT_CCIF_MASK 0x80
64 #define FTMRX_FSTAT_MGSTAT0_MASK 0x01
65 #define FTMRX_FSTAT_MGSTAT1_MASK 0x02
68 #define FTMRX_CMD_ALLERASED 0x01
69 #define FTMRX_CMD_BLOCKERASED 0x02
70 #define FTMRX_CMD_SECTIONERASED 0x03
71 #define FTMRX_CMD_READONCE 0x04
72 #define FTMRX_CMD_PROGFLASH 0x06
73 #define FTMRX_CMD_PROGONCE 0x07
74 #define FTMRX_CMD_ERASEALL 0x08
75 #define FTMRX_CMD_ERASEBLOCK 0x09
76 #define FTMRX_CMD_ERASESECTOR 0x0A
77 #define FTMRX_CMD_UNSECURE 0x0B
78 #define FTMRX_CMD_VERIFYACCESS 0x0C
79 #define FTMRX_CMD_SETMARGINLVL 0x0D
80 #define FTMRX_CMD_SETFACTORYLVL 0x0E
81 #define FTMRX_CMD_CONFIGNVM 0x0F
84 #define FTMRX_ERROR_ACCERR 0x20
85 #define FTMRX_ERROR_FPVIOL 0x10
87 #define KINETIS_KE_SRSID_FAMID(x) ((x >> 28) & 0x0F)
88 #define KINETIS_KE_SRSID_SUBFAMID(x) ((x >> 24) & 0x0F)
89 #define KINETIS_KE_SRSID_PINCOUNT(x) ((x >> 16) & 0x0F)
91 #define KINETIS_KE_SRSID_KEX2 0x02
92 #define KINETIS_KE_SRSID_KEX4 0x04
93 #define KINETIS_KE_SRSID_KEX6 0x06
108 #define MDM_REG_STAT 0x00
109 #define MDM_REG_CTRL 0x04
110 #define MDM_REG_ID 0xfc
112 #define MDM_STAT_FMEACK (1<<0)
113 #define MDM_STAT_FREADY (1<<1)
114 #define MDM_STAT_SYSSEC (1<<2)
115 #define MDM_STAT_SYSRES (1<<3)
116 #define MDM_STAT_FMEEN (1<<5)
117 #define MDM_STAT_BACKDOOREN (1<<6)
118 #define MDM_STAT_LPEN (1<<7)
119 #define MDM_STAT_VLPEN (1<<8)
120 #define MDM_STAT_LLSMODEXIT (1<<9)
121 #define MDM_STAT_VLLSXMODEXIT (1<<10)
122 #define MDM_STAT_CORE_HALTED (1<<16)
123 #define MDM_STAT_CORE_SLEEPDEEP (1<<17)
124 #define MDM_STAT_CORESLEEPING (1<<18)
126 #define MEM_CTRL_FMEIP (1<<0)
127 #define MEM_CTRL_DBG_DIS (1<<1)
128 #define MEM_CTRL_DBG_REQ (1<<2)
129 #define MEM_CTRL_SYS_RES_REQ (1<<3)
130 #define MEM_CTRL_CORE_HOLD_RES (1<<4)
131 #define MEM_CTRL_VLLSX_DBG_REQ (1<<5)
132 #define MEM_CTRL_VLLSX_DBG_ACK (1<<6)
133 #define MEM_CTRL_VLLSX_STAT_ACK (1<<7)
135 #define MDM_ACCESS_TIMEOUT 3000
149 LOG_DEBUG(
"MDM: failed to queue a write request");
174 LOG_DEBUG(
"MDM: failed to queue a read request");
212 uint8_t c2, c3, c4, s = 0;
213 uint16_t trim_value = 0;
215 uint32_t bus_clock = 0;
216 uint32_t bus_reg_val = 0;
217 uint32_t bus_reg_addr = 0;
218 uint32_t flash_clk_div;
301 bus_clock = 20000000;
336 bus_clock = 24000000;
342 bus_clock = 24000000;
407 flash_clk_div = bus_clock / 1000000L - 1;
418 LOG_WARNING(
"Flash clock was already set and contains an invalid value.");
431 LOG_WARNING(
"Flash clock register is locked and contains an invalid value.");
448 static const uint8_t watchdog_code[] = {
449 #include "../../../contrib/loaders/flash/kinetis_ke/kinetis_ke_watchdog.inc"
469 LOG_WARNING(
"No working area available for watchdog algorithm");
474 sizeof(watchdog_code), watchdog_code);
482 watchdog_algorithm->
address, 0, 100000, &armv7m_info);
484 LOG_ERROR(
"Error executing Kinetis KE watchdog algorithm");
511 LOG_ERROR(
"Cannot perform mass erase with a high-level adapter");
588 LOG_WARNING(
"Cannot check flash security status with a high-level adapter");
601 LOG_ERROR(
"MDM: failed to read ID register");
636 LOG_ERROR(
"MDM: failed to read MDM_REG_STAT");
643 LOG_WARNING(
"*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
645 LOG_WARNING(
"**** Your Kinetis MCU is in secured state, which means that, ****");
646 LOG_WARNING(
"**** with exception for very basic communication, JTAG/SWD ****");
647 LOG_WARNING(
"**** interface will NOT work. In order to restore its ****");
648 LOG_WARNING(
"**** functionality please issue 'kinetis_ke mdm mass_erase' ****");
649 LOG_WARNING(
"**** command, power cycle the MCU and restart OpenOCD. ****");
651 LOG_WARNING(
"*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
653 LOG_INFO(
"MDM: Chip is unsecured. Continuing.");
660 LOG_ERROR(
"MDM: Failed to check security status of the MCU. Cannot proceed further");
678 bank->driver_priv = bank_info;
685 #include "../../../contrib/loaders/flash/kinetis_ke/kinetis_ke_flash.inc"
689 uint32_t
offset, uint32_t words)
693 uint32_t ram_buffer_size = 512 + 16;
700 uint32_t flash_code_size;
702 LOG_INFO(
"Kinetis KE: FLASH Write ...");
707 LOG_WARNING(
"no working area available, can't do block memory writes");
728 LOG_WARNING(
"No large enough working area available, can't do block memory writes");
772 unsigned int first,
unsigned int last)
774 LOG_WARNING(
"kinetis_ke_protect not supported yet");
796 uint8_t fpopen, fpldis, fphdis;
798 uint32_t lprot_size = 0, hprot_size = 0;
799 uint32_t lprot_to = 0, hprot_from = 0;
807 fpopen = fprot & 0x80;
808 fpldis = fprot & 0x04;
809 fphdis = fprot & 0x20;
810 fphs = (fprot >> 3) & 0x03;
814 if (fpopen && fpldis && fphdis) {
817 for (
unsigned int i = 0; i <
bank->num_sectors; i++)
818 bank->sectors[i].is_protected = 0;
822 LOG_WARNING(
"Flash protected. FPOPEN=%i FPLDIS=%i FPHDIS=%i FPLS=%i FPHS=%i",
823 fpopen ? 1 : 0, fpldis ? 1 : 0, fphdis ? 1 : 0, fpls, fphs);
846 hprot_from = (0x8000 - hprot_size) / kinfo->
sector_size;
848 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
851 if (
bank->sectors[i].offset < 0x4000) {
853 if (lprot_to && (i < lprot_to))
854 bank->sectors[i].is_protected = 1;
856 bank->sectors[i].is_protected = 0;
860 }
else if (
bank->sectors[i].offset < 0x6000 ||
bank->sectors[i].offset >= 0x8000) {
863 bank->sectors[i].is_protected = 0;
865 bank->sectors[i].is_protected = 1;
868 }
else if (
bank->sectors[i].offset < 0x8000) {
869 if (hprot_from && (i > hprot_from))
870 bank->sectors[i].is_protected = 1;
872 bank->sectors[i].is_protected = 0;
881 uint8_t *FCCOBIX, uint8_t *FCCOBHI, uint8_t *FCCOBLO, uint8_t *fstat)
894 for (i = 0; i <
count; i++) {
943 uint8_t FCCOBIX[2], FCCOBHI[2], FCCOBLO[2], fstat;
944 bool fcf_erased =
false;
951 if ((first >
bank->num_sectors) || (last >
bank->num_sectors))
958 for (
unsigned int i = first; i <= last; i++) {
961 FCCOBLO[0] = (
bank->base +
bank->sectors[i].offset) >> 16;
964 FCCOBHI[1] = (
bank->base +
bank->sectors[i].offset) >> 8;
965 FCCOBLO[1] = (
bank->base +
bank->sectors[i].offset);
980 (
"flash configuration field erased, please reset the device");
990 uint8_t *new_buffer =
NULL;
991 uint32_t words =
count / 4;
1015 uint32_t old_count =
count;
1016 count = (old_count | 3) + 1;
1017 new_buffer = malloc(
count);
1019 LOG_ERROR(
"odd number of bytes to write and no memory "
1020 "for padding buffer");
1024 LOG_INFO(
"odd number of bytes to write (%" PRIu32
"), extending to %" PRIu32
" "
1025 "and padding with 0xff", old_count,
count);
1027 memset(new_buffer, 0xff,
count);
1076 bank->base = 0x00000000;
1083 bank->size = 0x00010000;
1084 bank->num_sectors = 128;
1101 bank->size = 0x00020000;
1102 bank->num_sectors = 256;
1117 free(
bank->sectors);
1119 assert(
bank->num_sectors > 0);
1122 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
1126 bank->sectors[i].is_erased = -1;
1127 bank->sectors[i].is_protected = 1;
1153 uint8_t FCCOBIX[3], FCCOBHI[3], FCCOBLO[3], fstat;
1154 uint16_t longwords = 0;
1177 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
1180 FCCOBLO[0] = (
bank->base +
bank->sectors[i].offset) >> 16;
1183 FCCOBHI[1] = (
bank->base +
bank->sectors[i].offset) >> 8;
1184 FCCOBLO[1] = (
bank->base +
bank->sectors[i].offset);
1189 FCCOBHI[2] = longwords >> 8;
1190 FCCOBLO[2] = longwords;
1197 LOG_DEBUG(
"Ignoring error on PFlash sector blank-check");
1198 bank->sectors[i].is_erased = -1;
1203 for (
unsigned int i = 0; i <
bank->num_sectors; i++)
1204 bank->sectors[i].is_erased = 1;
1212 .
name =
"check_security",
1214 .help =
"Check status of device security lock",
1216 .handler = kinetis_ke_check_flash_security_status,
1219 .name =
"mass_erase",
1221 .help =
"Issue a complete flash erase via the MDM-AP",
1223 .handler = kinetis_ke_mdm_mass_erase,
1232 .help =
"MDM-AP command group",
1237 .
name =
"disable_wdog",
1239 .help =
"Disable the watchdog timer",
1241 .handler = kinetis_ke_disable_wdog_handler,
1248 .
name =
"kinetis_ke",
1250 .help =
"Kinetis KE flash controller commands",
1258 .
name =
"kinetis_ke",
1260 .flash_bank_command = kinetis_ke_flash_bank_command,
void init_reg_param(struct reg_param *param, char *reg_name, uint32_t size, enum param_direction direction)
void destroy_reg_param(struct reg_param *param)
struct adiv5_ap * dap_get_ap(struct adiv5_dap *dap, uint64_t ap_num)
int dap_put_ap(struct adiv5_ap *ap)
This defines formats and data structures used to talk to ADIv5 entities.
static int dap_queue_ap_read(struct adiv5_ap *ap, unsigned int reg, uint32_t *data)
Queue an AP register read.
static int dap_queue_ap_write(struct adiv5_ap *ap, unsigned int reg, uint32_t data)
Queue an AP register write.
static int dap_run(struct adiv5_dap *dap)
Perform all queued DAP operations, and clear any errors posted in the CTRL_STAT register when they ar...
#define ARMV7M_COMMON_MAGIC
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
static struct cortex_m_common * target_to_cm(struct target *target)
#define ERROR_FLASH_OPER_UNSUPPORTED
#define ERROR_FLASH_BANK_INVALID
#define ERROR_FLASH_OPERATION_FAILED
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
void jtag_poll_set_enabled(bool value)
Assign flag reporting whether JTAG polling is disallowed.
int adapter_deassert_reset(void)
enum reset_types jtag_get_reset_config(void)
int adapter_assert_reset(void)
COMMAND_HANDLER(kinetis_ke_disable_wdog_handler)
#define KINETIS_KE_SRSID_KEX6
static int kinetis_ke_ftmrx_command(struct flash_bank *bank, uint8_t count, uint8_t *FCCOBIX, uint8_t *FCCOBHI, uint8_t *FCCOBLO, uint8_t *fstat)
static int kinetis_ke_mdm_read_register(struct adiv5_dap *dap, unsigned int reg, uint32_t *result)
static int kinetis_ke_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
static int kinetis_ke_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
#define FTMRX_FCLKDIV_FDIVLD_MASK
static int kinetis_ke_auto_probe(struct flash_bank *bank)
static int kinetis_ke_blank_check(struct flash_bank *bank)
#define KINETIS_KE_SRSID_FAMID(x)
static const uint32_t kinetis_ke_known_mdm_ids[]
#define FTMRX_ERROR_FPVIOL
#define FTMRX_FCLKDIV_FDIV(x)
#define FTMRX_FSTAT_CCIF_MASK
static const struct command_registration kinetis_ke_exec_command_handlers[]
static int kinetis_ke_protect_check(struct flash_bank *bank)
FLASH_BANK_COMMAND_HANDLER(kinetis_ke_flash_bank_command)
#define FTMRX_FCLKDIV_FDIV_MASK
#define FTMRX_ERROR_ACCERR
#define ICS_C4_SCFTRIM_MASK
static int kinetis_ke_mdm_poll_register(struct adiv5_dap *dap, unsigned int reg, uint32_t mask, uint32_t value)
#define FTMRX_CMD_ALLERASED
#define MEM_CTRL_SYS_RES_REQ
static const struct command_registration kinetis_ke_security_command_handlers[]
static int kinetis_ke_stop_watchdog(struct target *target)
#define FTMRX_FSTAT_MGSTAT1_MASK
static int kinetis_ke_prepare_flash(struct flash_bank *bank)
const struct flash_driver kinetis_ke_flash
#define FTMRX_CMD_ERASESECTOR
#define FTMRX_FSTAT_MGSTAT0_MASK
static uint8_t kinetis_ke_flash_write_code[]
#define SIM_CLKDIV_KE04_44_64_80
#define KINETIS_KE_SRSID_PINCOUNT(x)
static const struct command_registration kinetis_ke_command_handler[]
#define KINETIS_KE_SRSID_SUBFAMID(x)
#define MDM_ACCESS_TIMEOUT
#define KINETIS_KE_SRSID_KEX2
static int kinetis_ke_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
#define FTMRX_FCLKDIV_FDIVLCK_MASK
#define KINETIS_KE_SRSID_KEX4
static int kinetis_ke_mdm_write_register(struct adiv5_dap *dap, unsigned int reg, uint32_t value)
#define SIM_CLKDIV_KE04_16_20_24
static int kinetis_ke_write_words(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t words)
static int kinetis_ke_probe(struct flash_bank *bank)
#define FTMRX_CMD_SECTIONERASED
#define SIM_CLKDIV_OUTDIV2_MASK
static int kinetis_ke_info(struct flash_bank *bank, struct command_invocation *cmd)
void alive_sleep(uint64_t ms)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
This represents an ARM Debug Interface (v5) Access Port (AP).
struct adiv5_dap * dap
DAP this AP belongs to.
This represents an ARM Debug Interface (v5) Debug Access Port (DAP).
struct adiv5_dap * dap
For targets conforming to ARM Debug Interface v5, this handle references the Debug Access Port (DAP) ...
unsigned int common_magic
When run_command is called, a new instance will be created on the stack, filled with the proper value...
struct armv7m_common armv7m
Provides details of a flash bank, available either on-chip or through a major interface.
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Describes the geometry and status of a single flash sector within a flash bank.
uint32_t ftmrx_fccobhi_addr
uint32_t ftmrx_fprot_addr
uint32_t ftmrx_fstat_addr
uint32_t ftmrx_fclkdiv_addr
uint32_t ftmrx_fccobix_addr
uint32_t ftmrx_fccoblo_addr
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
int target_run_flash_async_algorithm(struct target *target, const uint8_t *buffer, uint32_t count, int block_size, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, uint32_t buffer_start, uint32_t buffer_size, uint32_t entry_point, uint32_t exit_point, void *arch_info)
Streams data to a circular buffer on target intended for consumption by code running asynchronously o...
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
struct target * get_current_target(struct command_context *cmd_ctx)
#define ERROR_TARGET_NOT_HALTED
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.