You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2366 lines
69 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2011 by Mathias Kuester *
  3. * kesmtp@freenet.de *
  4. * *
  5. * Copyright (C) 2011 sleep(5) ltd *
  6. * tomas@sleepfive.com *
  7. * *
  8. * Copyright (C) 2012 by Christopher D. Kilgour *
  9. * techie at whiterocker.com *
  10. * *
  11. * Copyright (C) 2013 Nemui Trinomius *
  12. * nemuisan_kawausogasuki@live.jp *
  13. * *
  14. * Copyright (C) 2015 Tomas Vanek *
  15. * vanekt@fbl.cz *
  16. * *
  17. * This program is free software; you can redistribute it and/or modify *
  18. * it under the terms of the GNU General Public License as published by *
  19. * the Free Software Foundation; either version 2 of the License, or *
  20. * (at your option) any later version. *
  21. * *
  22. * This program is distributed in the hope that it will be useful, *
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  25. * GNU General Public License for more details. *
  26. * *
  27. * You should have received a copy of the GNU General Public License *
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  29. ***************************************************************************/
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include "jtag/interface.h"
  34. #include "imp.h"
  35. #include <helper/binarybuffer.h>
  36. #include <helper/time_support.h>
  37. #include <target/target_type.h>
  38. #include <target/algorithm.h>
  39. #include <target/armv7m.h>
  40. #include <target/cortex_m.h>
  41. /*
  42. * Implementation Notes
  43. *
  44. * The persistent memories in the Kinetis chip families K10 through
  45. * K70 are all manipulated with the Flash Memory Module. Some
  46. * variants call this module the FTFE, others call it the FTFL. To
  47. * indicate that both are considered here, we use FTFX.
  48. *
  49. * Within the module, according to the chip variant, the persistent
  50. * memory is divided into what Freescale terms Program Flash, FlexNVM,
  51. * and FlexRAM. All chip variants have Program Flash. Some chip
  52. * variants also have FlexNVM and FlexRAM, which always appear
  53. * together.
  54. *
  55. * A given Kinetis chip may have 1, 2 or 4 blocks of flash. Here we map
  56. * each block to a separate bank. Each block size varies by chip and
  57. * may be determined by the read-only SIM_FCFG1 register. The sector
  58. * size within each bank/block varies by chip, and may be 1, 2 or 4k.
  59. * The sector size may be different for flash and FlexNVM.
  60. *
  61. * The first half of the flash (1 or 2 blocks) is always Program Flash
  62. * and always starts at address 0x00000000. The "PFLSH" flag, bit 23
  63. * of the read-only SIM_FCFG2 register, determines whether the second
  64. * half of the flash is also Program Flash or FlexNVM+FlexRAM. When
  65. * PFLSH is set, the second from the first half. When PFLSH is clear,
  66. * the second half of flash is FlexNVM and always starts at address
  67. * 0x10000000. FlexRAM, which is also present when PFLSH is clear,
  68. * always starts at address 0x14000000.
  69. *
  70. * The Flash Memory Module provides a register set where flash
  71. * commands are loaded to perform flash operations like erase and
  72. * program. Different commands are available depending on whether
  73. * Program Flash or FlexNVM/FlexRAM is being manipulated. Although
  74. * the commands used are quite consistent between flash blocks, the
  75. * parameters they accept differ according to the flash sector size.
  76. *
  77. */
  78. /* Addressess */
  79. #define FCF_ADDRESS 0x00000400
  80. #define FCF_FPROT 0x8
  81. #define FCF_FSEC 0xc
  82. #define FCF_FOPT 0xd
  83. #define FCF_FDPROT 0xf
  84. #define FCF_SIZE 0x10
  85. #define FLEXRAM 0x14000000
  86. #define FMC_PFB01CR 0x4001f004
  87. #define FTFx_FSTAT 0x40020000
  88. #define FTFx_FCNFG 0x40020001
  89. #define FTFx_FCCOB3 0x40020004
  90. #define FTFx_FPROT3 0x40020010
  91. #define FTFx_FDPROT 0x40020017
  92. #define SIM_SDID 0x40048024
  93. #define SIM_SOPT1 0x40047000
  94. #define SIM_FCFG1 0x4004804c
  95. #define SIM_FCFG2 0x40048050
  96. #define WDOG_STCTRH 0x40052000
  97. #define SMC_PMCTRL 0x4007E001
  98. #define SMC_PMSTAT 0x4007E003
  99. /* Values */
  100. #define PM_STAT_RUN 0x01
  101. #define PM_STAT_VLPR 0x04
  102. #define PM_CTRL_RUNM_RUN 0x00
  103. /* Commands */
  104. #define FTFx_CMD_BLOCKSTAT 0x00
  105. #define FTFx_CMD_SECTSTAT 0x01
  106. #define FTFx_CMD_LWORDPROG 0x06
  107. #define FTFx_CMD_SECTERASE 0x09
  108. #define FTFx_CMD_SECTWRITE 0x0b
  109. #define FTFx_CMD_MASSERASE 0x44
  110. #define FTFx_CMD_PGMPART 0x80
  111. #define FTFx_CMD_SETFLEXRAM 0x81
  112. /* The older Kinetis K series uses the following SDID layout :
  113. * Bit 31-16 : 0
  114. * Bit 15-12 : REVID
  115. * Bit 11-7 : DIEID
  116. * Bit 6-4 : FAMID
  117. * Bit 3-0 : PINID
  118. *
  119. * The newer Kinetis series uses the following SDID layout :
  120. * Bit 31-28 : FAMID
  121. * Bit 27-24 : SUBFAMID
  122. * Bit 23-20 : SERIESID
  123. * Bit 19-16 : SRAMSIZE
  124. * Bit 15-12 : REVID
  125. * Bit 6-4 : Reserved (0)
  126. * Bit 3-0 : PINID
  127. *
  128. * We assume that if bits 31-16 are 0 then it's an older
  129. * K-series MCU.
  130. */
  131. #define KINETIS_SOPT1_RAMSIZE_MASK 0x0000F000
  132. #define KINETIS_SOPT1_RAMSIZE_K24FN1M 0x0000B000
  133. #define KINETIS_SDID_K_SERIES_MASK 0x0000FFFF
  134. #define KINETIS_SDID_DIEID_MASK 0x00000F80
  135. #define KINETIS_SDID_DIEID_K22FN128 0x00000680 /* smaller pflash with FTFA */
  136. #define KINETIS_SDID_DIEID_K22FN256 0x00000A80
  137. #define KINETIS_SDID_DIEID_K22FN512 0x00000E80
  138. #define KINETIS_SDID_DIEID_K24FN256 0x00000700
  139. #define KINETIS_SDID_DIEID_K24FN1M 0x00000300 /* Detect Errata 7534 */
  140. /* We can't rely solely on the FAMID field to determine the MCU
  141. * type since some FAMID values identify multiple MCUs with
  142. * different flash sector sizes (K20 and K22 for instance).
  143. * Therefore we combine it with the DIEID bits which may possibly
  144. * break if Freescale bumps the DIEID for a particular MCU. */
  145. #define KINETIS_K_SDID_TYPE_MASK 0x00000FF0
  146. #define KINETIS_K_SDID_K10_M50 0x00000000
  147. #define KINETIS_K_SDID_K10_M72 0x00000080
  148. #define KINETIS_K_SDID_K10_M100 0x00000100
  149. #define KINETIS_K_SDID_K10_M120 0x00000180
  150. #define KINETIS_K_SDID_K11 0x00000220
  151. #define KINETIS_K_SDID_K12 0x00000200
  152. #define KINETIS_K_SDID_K20_M50 0x00000010
  153. #define KINETIS_K_SDID_K20_M72 0x00000090
  154. #define KINETIS_K_SDID_K20_M100 0x00000110
  155. #define KINETIS_K_SDID_K20_M120 0x00000190
  156. #define KINETIS_K_SDID_K21_M50 0x00000230
  157. #define KINETIS_K_SDID_K21_M120 0x00000330
  158. #define KINETIS_K_SDID_K22_M50 0x00000210
  159. #define KINETIS_K_SDID_K22_M120 0x00000310
  160. #define KINETIS_K_SDID_K30_M72 0x000000A0
  161. #define KINETIS_K_SDID_K30_M100 0x00000120
  162. #define KINETIS_K_SDID_K40_M72 0x000000B0
  163. #define KINETIS_K_SDID_K40_M100 0x00000130
  164. #define KINETIS_K_SDID_K50_M72 0x000000E0
  165. #define KINETIS_K_SDID_K51_M72 0x000000F0
  166. #define KINETIS_K_SDID_K53 0x00000170
  167. #define KINETIS_K_SDID_K60_M100 0x00000140
  168. #define KINETIS_K_SDID_K60_M150 0x000001C0
  169. #define KINETIS_K_SDID_K70_M150 0x000001D0
  170. #define KINETIS_SDID_SERIESID_MASK 0x00F00000
  171. #define KINETIS_SDID_SERIESID_K 0x00000000
  172. #define KINETIS_SDID_SERIESID_KL 0x00100000
  173. #define KINETIS_SDID_SERIESID_KW 0x00500000
  174. #define KINETIS_SDID_SERIESID_KV 0x00600000
  175. #define KINETIS_SDID_SUBFAMID_MASK 0x0F000000
  176. #define KINETIS_SDID_SUBFAMID_KX0 0x00000000
  177. #define KINETIS_SDID_SUBFAMID_KX1 0x01000000
  178. #define KINETIS_SDID_SUBFAMID_KX2 0x02000000
  179. #define KINETIS_SDID_SUBFAMID_KX3 0x03000000
  180. #define KINETIS_SDID_SUBFAMID_KX4 0x04000000
  181. #define KINETIS_SDID_SUBFAMID_KX5 0x05000000
  182. #define KINETIS_SDID_SUBFAMID_KX6 0x06000000
  183. #define KINETIS_SDID_FAMILYID_MASK 0xF0000000
  184. #define KINETIS_SDID_FAMILYID_K0X 0x00000000
  185. #define KINETIS_SDID_FAMILYID_K1X 0x10000000
  186. #define KINETIS_SDID_FAMILYID_K2X 0x20000000
  187. #define KINETIS_SDID_FAMILYID_K3X 0x30000000
  188. #define KINETIS_SDID_FAMILYID_K4X 0x40000000
  189. #define KINETIS_SDID_FAMILYID_K6X 0x60000000
  190. #define KINETIS_SDID_FAMILYID_K7X 0x70000000
  191. struct kinetis_flash_bank {
  192. bool probed;
  193. uint32_t sector_size;
  194. uint32_t max_flash_prog_size;
  195. uint32_t protection_size;
  196. uint32_t prog_base; /* base address for FTFx operations */
  197. /* same as bank->base for pflash, differs for FlexNVM */
  198. uint32_t protection_block; /* number of first protection block in this bank */
  199. uint32_t sim_sdid;
  200. uint32_t sim_fcfg1;
  201. uint32_t sim_fcfg2;
  202. enum {
  203. FC_AUTO = 0,
  204. FC_PFLASH,
  205. FC_FLEX_NVM,
  206. FC_FLEX_RAM,
  207. } flash_class;
  208. enum {
  209. FS_PROGRAM_SECTOR = 1,
  210. FS_PROGRAM_LONGWORD = 2,
  211. FS_PROGRAM_PHRASE = 4, /* Unsupported */
  212. FS_INVALIDATE_CACHE = 8,
  213. } flash_support;
  214. };
  215. #define MDM_AP 1
  216. #define MDM_REG_STAT 0x00
  217. #define MDM_REG_CTRL 0x04
  218. #define MDM_REG_ID 0xfc
  219. #define MDM_STAT_FMEACK (1<<0)
  220. #define MDM_STAT_FREADY (1<<1)
  221. #define MDM_STAT_SYSSEC (1<<2)
  222. #define MDM_STAT_SYSRES (1<<3)
  223. #define MDM_STAT_FMEEN (1<<5)
  224. #define MDM_STAT_BACKDOOREN (1<<6)
  225. #define MDM_STAT_LPEN (1<<7)
  226. #define MDM_STAT_VLPEN (1<<8)
  227. #define MDM_STAT_LLSMODEXIT (1<<9)
  228. #define MDM_STAT_VLLSXMODEXIT (1<<10)
  229. #define MDM_STAT_CORE_HALTED (1<<16)
  230. #define MDM_STAT_CORE_SLEEPDEEP (1<<17)
  231. #define MDM_STAT_CORESLEEPING (1<<18)
  232. #define MDM_CTRL_FMEIP (1<<0)
  233. #define MDM_CTRL_DBG_DIS (1<<1)
  234. #define MDM_CTRL_DBG_REQ (1<<2)
  235. #define MDM_CTRL_SYS_RES_REQ (1<<3)
  236. #define MDM_CTRL_CORE_HOLD_RES (1<<4)
  237. #define MDM_CTRL_VLLSX_DBG_REQ (1<<5)
  238. #define MDM_CTRL_VLLSX_DBG_ACK (1<<6)
  239. #define MDM_CTRL_VLLSX_STAT_ACK (1<<7)
  240. #define MDM_ACCESS_TIMEOUT 500 /* msec */
  241. static bool allow_fcf_writes;
  242. static uint8_t fcf_fopt = 0xff;
  243. struct flash_driver kinetis_flash;
  244. static int kinetis_write_inner(struct flash_bank *bank, const uint8_t *buffer,
  245. uint32_t offset, uint32_t count);
  246. static int kinetis_auto_probe(struct flash_bank *bank);
  247. static int kinetis_mdm_write_register(struct adiv5_dap *dap, unsigned reg, uint32_t value)
  248. {
  249. int retval;
  250. LOG_DEBUG("MDM_REG[0x%02x] <- %08" PRIX32, reg, value);
  251. retval = dap_queue_ap_write(dap_ap(dap, MDM_AP), reg, value);
  252. if (retval != ERROR_OK) {
  253. LOG_DEBUG("MDM: failed to queue a write request");
  254. return retval;
  255. }
  256. retval = dap_run(dap);
  257. if (retval != ERROR_OK) {
  258. LOG_DEBUG("MDM: dap_run failed");
  259. return retval;
  260. }
  261. return ERROR_OK;
  262. }
  263. static int kinetis_mdm_read_register(struct adiv5_dap *dap, unsigned reg, uint32_t *result)
  264. {
  265. int retval;
  266. retval = dap_queue_ap_read(dap_ap(dap, MDM_AP), reg, result);
  267. if (retval != ERROR_OK) {
  268. LOG_DEBUG("MDM: failed to queue a read request");
  269. return retval;
  270. }
  271. retval = dap_run(dap);
  272. if (retval != ERROR_OK) {
  273. LOG_DEBUG("MDM: dap_run failed");
  274. return retval;
  275. }
  276. LOG_DEBUG("MDM_REG[0x%02x]: %08" PRIX32, reg, *result);
  277. return ERROR_OK;
  278. }
  279. static int kinetis_mdm_poll_register(struct adiv5_dap *dap, unsigned reg,
  280. uint32_t mask, uint32_t value, uint32_t timeout_ms)
  281. {
  282. uint32_t val;
  283. int retval;
  284. int64_t ms_timeout = timeval_ms() + timeout_ms;
  285. do {
  286. retval = kinetis_mdm_read_register(dap, reg, &val);
  287. if (retval != ERROR_OK || (val & mask) == value)
  288. return retval;
  289. alive_sleep(1);
  290. } while (timeval_ms() < ms_timeout);
  291. LOG_DEBUG("MDM: polling timed out");
  292. return ERROR_FAIL;
  293. }
  294. /*
  295. * This command can be used to break a watchdog reset loop when
  296. * connecting to an unsecured target. Unlike other commands, halt will
  297. * automatically retry as it does not know how far into the boot process
  298. * it is when the command is called.
  299. */
  300. COMMAND_HANDLER(kinetis_mdm_halt)
  301. {
  302. struct target *target = get_current_target(CMD_CTX);
  303. struct cortex_m_common *cortex_m = target_to_cm(target);
  304. struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
  305. int retval;
  306. int tries = 0;
  307. uint32_t stat;
  308. int64_t ms_timeout = timeval_ms() + MDM_ACCESS_TIMEOUT;
  309. if (!dap) {
  310. LOG_ERROR("Cannot perform halt with a high-level adapter");
  311. return ERROR_FAIL;
  312. }
  313. while (true) {
  314. tries++;
  315. kinetis_mdm_write_register(dap, MDM_REG_CTRL, MDM_CTRL_CORE_HOLD_RES);
  316. alive_sleep(1);
  317. retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &stat);
  318. if (retval != ERROR_OK) {
  319. LOG_DEBUG("MDM: failed to read MDM_REG_STAT");
  320. continue;
  321. }
  322. /* Repeat setting MDM_CTRL_CORE_HOLD_RES until system is out of
  323. * reset with flash ready and without security
  324. */
  325. if ((stat & (MDM_STAT_FREADY | MDM_STAT_SYSSEC | MDM_STAT_SYSRES))
  326. == (MDM_STAT_FREADY | MDM_STAT_SYSRES))
  327. break;
  328. if (timeval_ms() >= ms_timeout) {
  329. LOG_ERROR("MDM: halt timed out");
  330. return ERROR_FAIL;
  331. }
  332. }
  333. LOG_DEBUG("MDM: halt succeded after %d attempts.", tries);
  334. target_poll(target);
  335. /* enable polling in case kinetis_check_flash_security_status disabled it */
  336. jtag_poll_set_enabled(true);
  337. alive_sleep(100);
  338. target->reset_halt = true;
  339. target->type->assert_reset(target);
  340. retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
  341. if (retval != ERROR_OK) {
  342. LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
  343. return retval;
  344. }
  345. target->type->deassert_reset(target);
  346. return ERROR_OK;
  347. }
  348. COMMAND_HANDLER(kinetis_mdm_reset)
  349. {
  350. struct target *target = get_current_target(CMD_CTX);
  351. struct cortex_m_common *cortex_m = target_to_cm(target);
  352. struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
  353. int retval;
  354. if (!dap) {
  355. LOG_ERROR("Cannot perform reset with a high-level adapter");
  356. return ERROR_FAIL;
  357. }
  358. retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, MDM_CTRL_SYS_RES_REQ);
  359. if (retval != ERROR_OK) {
  360. LOG_ERROR("MDM: failed to write MDM_REG_CTRL");
  361. return retval;
  362. }
  363. retval = kinetis_mdm_poll_register(dap, MDM_REG_STAT, MDM_STAT_SYSRES, 0, 500);
  364. if (retval != ERROR_OK) {
  365. LOG_ERROR("MDM: failed to assert reset");
  366. return retval;
  367. }
  368. retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
  369. if (retval != ERROR_OK) {
  370. LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
  371. return retval;
  372. }
  373. return ERROR_OK;
  374. }
  375. /*
  376. * This function implements the procedure to mass erase the flash via
  377. * SWD/JTAG on Kinetis K and L series of devices as it is described in
  378. * AN4835 "Production Flash Programming Best Practices for Kinetis K-
  379. * and L-series MCUs" Section 4.2.1. To prevent a watchdog reset loop,
  380. * the core remains halted after this function completes as suggested
  381. * by the application note.
  382. */
  383. COMMAND_HANDLER(kinetis_mdm_mass_erase)
  384. {
  385. struct target *target = get_current_target(CMD_CTX);
  386. struct cortex_m_common *cortex_m = target_to_cm(target);
  387. struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
  388. if (!dap) {
  389. LOG_ERROR("Cannot perform mass erase with a high-level adapter");
  390. return ERROR_FAIL;
  391. }
  392. int retval;
  393. /*
  394. * ... Power on the processor, or if power has already been
  395. * applied, assert the RESET pin to reset the processor. For
  396. * devices that do not have a RESET pin, write the System
  397. * Reset Request bit in the MDM-AP control register after
  398. * establishing communication...
  399. */
  400. /* assert SRST if configured */
  401. bool has_srst = jtag_get_reset_config() & RESET_HAS_SRST;
  402. if (has_srst)
  403. adapter_assert_reset();
  404. retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, MDM_CTRL_SYS_RES_REQ);
  405. if (retval != ERROR_OK && !has_srst) {
  406. LOG_ERROR("MDM: failed to assert reset");
  407. goto deassert_reset_and_exit;
  408. }
  409. /*
  410. * ... Read the MDM-AP status register repeatedly and wait for
  411. * stable conditions suitable for mass erase:
  412. * - mass erase is enabled
  413. * - flash is ready
  414. * - reset is finished
  415. *
  416. * Mass erase is started as soon as all conditions are met in 32
  417. * subsequent status reads.
  418. *
  419. * In case of not stable conditions (RESET/WDOG loop in secured device)
  420. * the user is asked for manual pressing of RESET button
  421. * as a last resort.
  422. */
  423. int cnt_mass_erase_disabled = 0;
  424. int cnt_ready = 0;
  425. int64_t ms_start = timeval_ms();
  426. bool man_reset_requested = false;
  427. do {
  428. uint32_t stat = 0;
  429. int64_t ms_elapsed = timeval_ms() - ms_start;
  430. if (!man_reset_requested && ms_elapsed > 100) {
  431. LOG_INFO("MDM: Press RESET button now if possible.");
  432. man_reset_requested = true;
  433. }
  434. if (ms_elapsed > 3000) {
  435. LOG_ERROR("MDM: waiting for mass erase conditions timed out.");
  436. LOG_INFO("Mass erase of a secured MCU is not possible without hardware reset.");
  437. LOG_INFO("Connect SRST, use 'reset_config srst_only' and retry.");
  438. goto deassert_reset_and_exit;
  439. }
  440. retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &stat);
  441. if (retval != ERROR_OK) {
  442. cnt_ready = 0;
  443. continue;
  444. }
  445. if (!(stat & MDM_STAT_FMEEN)) {
  446. cnt_ready = 0;
  447. cnt_mass_erase_disabled++;
  448. if (cnt_mass_erase_disabled > 10) {
  449. LOG_ERROR("MDM: mass erase is disabled");
  450. goto deassert_reset_and_exit;
  451. }
  452. continue;
  453. }
  454. if ((stat & (MDM_STAT_FREADY | MDM_STAT_SYSRES)) == MDM_STAT_FREADY)
  455. cnt_ready++;
  456. else
  457. cnt_ready = 0;
  458. } while (cnt_ready < 32);
  459. /*
  460. * ... Write the MDM-AP control register to set the Flash Mass
  461. * Erase in Progress bit. This will start the mass erase
  462. * process...
  463. */
  464. retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, MDM_CTRL_SYS_RES_REQ | MDM_CTRL_FMEIP);
  465. if (retval != ERROR_OK) {
  466. LOG_ERROR("MDM: failed to start mass erase");
  467. goto deassert_reset_and_exit;
  468. }
  469. /*
  470. * ... Read the MDM-AP control register until the Flash Mass
  471. * Erase in Progress bit clears...
  472. * Data sheed defines erase time <3.6 sec/512kB flash block.
  473. * The biggest device has 4 pflash blocks => timeout 16 sec.
  474. */
  475. retval = kinetis_mdm_poll_register(dap, MDM_REG_CTRL, MDM_CTRL_FMEIP, 0, 16000);
  476. if (retval != ERROR_OK) {
  477. LOG_ERROR("MDM: mass erase timeout");
  478. goto deassert_reset_and_exit;
  479. }
  480. target_poll(target);
  481. /* enable polling in case kinetis_check_flash_security_status disabled it */
  482. jtag_poll_set_enabled(true);
  483. alive_sleep(100);
  484. target->reset_halt = true;
  485. target->type->assert_reset(target);
  486. /*
  487. * ... Negate the RESET signal or clear the System Reset Request
  488. * bit in the MDM-AP control register.
  489. */
  490. retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
  491. if (retval != ERROR_OK)
  492. LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
  493. target->type->deassert_reset(target);
  494. return retval;
  495. deassert_reset_and_exit:
  496. kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
  497. if (has_srst)
  498. adapter_deassert_reset();
  499. return retval;
  500. }
  501. static const uint32_t kinetis_known_mdm_ids[] = {
  502. 0x001C0000, /* Kinetis-K Series */
  503. 0x001C0020, /* Kinetis-L/M/V/E Series */
  504. };
  505. /*
  506. * This function implements the procedure to connect to
  507. * SWD/JTAG on Kinetis K and L series of devices as it is described in
  508. * AN4835 "Production Flash Programming Best Practices for Kinetis K-
  509. * and L-series MCUs" Section 4.1.1
  510. */
  511. COMMAND_HANDLER(kinetis_check_flash_security_status)
  512. {
  513. struct target *target = get_current_target(CMD_CTX);
  514. struct cortex_m_common *cortex_m = target_to_cm(target);
  515. struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
  516. if (!dap) {
  517. LOG_WARNING("Cannot check flash security status with a high-level adapter");
  518. return ERROR_OK;
  519. }
  520. if (!dap->ops)
  521. return ERROR_OK; /* too early to check, in JTAG mode ops may not be initialised */
  522. uint32_t val;
  523. int retval;
  524. /*
  525. * ... The MDM-AP ID register can be read to verify that the
  526. * connection is working correctly...
  527. */
  528. retval = kinetis_mdm_read_register(dap, MDM_REG_ID, &val);
  529. if (retval != ERROR_OK) {
  530. LOG_ERROR("MDM: failed to read ID register");
  531. return ERROR_OK;
  532. }
  533. if (val == 0)
  534. return ERROR_OK; /* dap not yet initialised */
  535. bool found = false;
  536. for (size_t i = 0; i < ARRAY_SIZE(kinetis_known_mdm_ids); i++) {
  537. if (val == kinetis_known_mdm_ids[i]) {
  538. found = true;
  539. break;
  540. }
  541. }
  542. if (!found)
  543. LOG_WARNING("MDM: unknown ID %08" PRIX32, val);
  544. /*
  545. * ... Read the System Security bit to determine if security is enabled.
  546. * If System Security = 0, then proceed. If System Security = 1, then
  547. * communication with the internals of the processor, including the
  548. * flash, will not be possible without issuing a mass erase command or
  549. * unsecuring the part through other means (backdoor key unlock)...
  550. */
  551. retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &val);
  552. if (retval != ERROR_OK) {
  553. LOG_ERROR("MDM: failed to read MDM_REG_STAT");
  554. return ERROR_OK;
  555. }
  556. /*
  557. * System Security bit is also active for short time during reset.
  558. * If a MCU has blank flash and runs in RESET/WDOG loop,
  559. * System Security bit is active most of time!
  560. * We should observe Flash Ready bit and read status several times
  561. * to avoid false detection of secured MCU
  562. */
  563. int secured_score = 0, flash_not_ready_score = 0;
  564. if ((val & (MDM_STAT_SYSSEC | MDM_STAT_FREADY)) != MDM_STAT_FREADY) {
  565. uint32_t stats[32];
  566. int i;
  567. for (i = 0; i < 32; i++) {
  568. stats[i] = MDM_STAT_FREADY;
  569. dap_queue_ap_read(dap_ap(dap, MDM_AP), MDM_REG_STAT, &stats[i]);
  570. }
  571. retval = dap_run(dap);
  572. if (retval != ERROR_OK) {
  573. LOG_DEBUG("MDM: dap_run failed when validating secured state");
  574. return ERROR_OK;
  575. }
  576. for (i = 0; i < 32; i++) {
  577. if (stats[i] & MDM_STAT_SYSSEC)
  578. secured_score++;
  579. if (!(stats[i] & MDM_STAT_FREADY))
  580. flash_not_ready_score++;
  581. }
  582. }
  583. if (flash_not_ready_score <= 8 && secured_score > 24) {
  584. jtag_poll_set_enabled(false);
  585. LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
  586. LOG_WARNING("**** ****");
  587. LOG_WARNING("**** Your Kinetis MCU is in secured state, which means that, ****");
  588. LOG_WARNING("**** with exception for very basic communication, JTAG/SWD ****");
  589. LOG_WARNING("**** interface will NOT work. In order to restore its ****");
  590. LOG_WARNING("**** functionality please issue 'kinetis mdm mass_erase' ****");
  591. LOG_WARNING("**** command, power cycle the MCU and restart OpenOCD. ****");
  592. LOG_WARNING("**** ****");
  593. LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
  594. } else if (flash_not_ready_score > 24) {
  595. jtag_poll_set_enabled(false);
  596. LOG_WARNING("**** Your Kinetis MCU is probably locked-up in RESET/WDOG loop. ****");
  597. LOG_WARNING("**** Common reason is a blank flash (at least a reset vector). ****");
  598. LOG_WARNING("**** Issue 'kinetis mdm halt' command or if SRST is connected ****");
  599. LOG_WARNING("**** and configured, use 'reset halt' ****");
  600. LOG_WARNING("**** If MCU cannot be halted, it is likely secured and running ****");
  601. LOG_WARNING("**** in RESET/WDOG loop. Issue 'kinetis mdm mass_erase' ****");
  602. } else {
  603. LOG_INFO("MDM: Chip is unsecured. Continuing.");
  604. jtag_poll_set_enabled(true);
  605. }
  606. return ERROR_OK;
  607. }
  608. FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
  609. {
  610. struct kinetis_flash_bank *bank_info;
  611. if (CMD_ARGC < 6)
  612. return ERROR_COMMAND_SYNTAX_ERROR;
  613. LOG_INFO("add flash_bank kinetis %s", bank->name);
  614. bank_info = malloc(sizeof(struct kinetis_flash_bank));
  615. memset(bank_info, 0, sizeof(struct kinetis_flash_bank));
  616. bank->driver_priv = bank_info;
  617. return ERROR_OK;
  618. }
  619. /* Disable the watchdog on Kinetis devices */
  620. int kinetis_disable_wdog(struct target *target, uint32_t sim_sdid)
  621. {
  622. struct working_area *wdog_algorithm;
  623. struct armv7m_algorithm armv7m_info;
  624. uint16_t wdog;
  625. int retval;
  626. static const uint8_t kinetis_unlock_wdog_code[] = {
  627. #include "../../../contrib/loaders/watchdog/armv7m_kinetis_wdog.inc"
  628. };
  629. /* Decide whether the connected device needs watchdog disabling.
  630. * Disable for all Kx and KVx devices, return if it is a KLx */
  631. if ((sim_sdid & KINETIS_SDID_SERIESID_MASK) == KINETIS_SDID_SERIESID_KL)
  632. return ERROR_OK;
  633. /* The connected device requires watchdog disabling. */
  634. retval = target_read_u16(target, WDOG_STCTRH, &wdog);
  635. if (retval != ERROR_OK)
  636. return retval;
  637. if ((wdog & 0x1) == 0) {
  638. /* watchdog already disabled */
  639. return ERROR_OK;
  640. }
  641. LOG_INFO("Disabling Kinetis watchdog (initial WDOG_STCTRLH = 0x%x)", wdog);
  642. if (target->state != TARGET_HALTED) {
  643. LOG_ERROR("Target not halted");
  644. return ERROR_TARGET_NOT_HALTED;
  645. }
  646. retval = target_alloc_working_area(target, sizeof(kinetis_unlock_wdog_code), &wdog_algorithm);
  647. if (retval != ERROR_OK)
  648. return retval;
  649. retval = target_write_buffer(target, wdog_algorithm->address,
  650. sizeof(kinetis_unlock_wdog_code), (uint8_t *)kinetis_unlock_wdog_code);
  651. if (retval != ERROR_OK) {
  652. target_free_working_area(target, wdog_algorithm);
  653. return retval;
  654. }
  655. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  656. armv7m_info.core_mode = ARM_MODE_THREAD;
  657. retval = target_run_algorithm(target, 0, NULL, 0, NULL, wdog_algorithm->address,
  658. wdog_algorithm->address + (sizeof(kinetis_unlock_wdog_code) - 2),
  659. 10000, &armv7m_info);
  660. if (retval != ERROR_OK)
  661. LOG_ERROR("error executing kinetis wdog unlock algorithm");
  662. retval = target_read_u16(target, WDOG_STCTRH, &wdog);
  663. if (retval != ERROR_OK)
  664. return retval;
  665. LOG_INFO("WDOG_STCTRLH = 0x%x", wdog);
  666. target_free_working_area(target, wdog_algorithm);
  667. return retval;
  668. }
  669. COMMAND_HANDLER(kinetis_disable_wdog_handler)
  670. {
  671. int result;
  672. uint32_t sim_sdid;
  673. struct target *target = get_current_target(CMD_CTX);
  674. if (CMD_ARGC > 0)
  675. return ERROR_COMMAND_SYNTAX_ERROR;
  676. result = target_read_u32(target, SIM_SDID, &sim_sdid);
  677. if (result != ERROR_OK) {
  678. LOG_ERROR("Failed to read SIMSDID");
  679. return result;
  680. }
  681. result = kinetis_disable_wdog(target, sim_sdid);
  682. return result;
  683. }
  684. static int kinetis_ftfx_decode_error(uint8_t fstat)
  685. {
  686. if (fstat & 0x20) {
  687. LOG_ERROR("Flash operation failed, illegal command");
  688. return ERROR_FLASH_OPER_UNSUPPORTED;
  689. } else if (fstat & 0x10)
  690. LOG_ERROR("Flash operation failed, protection violated");
  691. else if (fstat & 0x40)
  692. LOG_ERROR("Flash operation failed, read collision");
  693. else if (fstat & 0x80)
  694. return ERROR_OK;
  695. else
  696. LOG_ERROR("Flash operation timed out");
  697. return ERROR_FLASH_OPERATION_FAILED;
  698. }
  699. static int kinetis_ftfx_prepare(struct target *target)
  700. {
  701. int result, i;
  702. uint8_t fstat;
  703. /* wait until busy */
  704. for (i = 0; i < 50; i++) {
  705. result = target_read_u8(target, FTFx_FSTAT, &fstat);
  706. if (result != ERROR_OK)
  707. return result;
  708. if (fstat & 0x80)
  709. break;
  710. }
  711. if ((fstat & 0x80) == 0) {
  712. LOG_ERROR("Flash controller is busy");
  713. return ERROR_FLASH_OPERATION_FAILED;
  714. }
  715. if (fstat != 0x80) {
  716. /* reset error flags */
  717. result = target_write_u8(target, FTFx_FSTAT, 0x70);
  718. }
  719. return result;
  720. }
  721. /* Kinetis Program-LongWord Microcodes */
  722. static const uint8_t kinetis_flash_write_code[] = {
  723. #include "../../../contrib/loaders/flash/kinetis/kinetis_flash.inc"
  724. };
  725. /* Program LongWord Block Write */
  726. static int kinetis_write_block(struct flash_bank *bank, const uint8_t *buffer,
  727. uint32_t offset, uint32_t wcount)
  728. {
  729. struct target *target = bank->target;
  730. uint32_t buffer_size = 2048; /* Default minimum value */
  731. struct working_area *write_algorithm;
  732. struct working_area *source;
  733. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  734. uint32_t address = kinfo->prog_base + offset;
  735. uint32_t end_address;
  736. struct reg_param reg_params[5];
  737. struct armv7m_algorithm armv7m_info;
  738. int retval;
  739. uint8_t fstat;
  740. /* Increase buffer_size if needed */
  741. if (buffer_size < (target->working_area_size/2))
  742. buffer_size = (target->working_area_size/2);
  743. /* allocate working area with flash programming code */
  744. if (target_alloc_working_area(target, sizeof(kinetis_flash_write_code),
  745. &write_algorithm) != ERROR_OK) {
  746. LOG_WARNING("no working area available, can't do block memory writes");
  747. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  748. }
  749. retval = target_write_buffer(target, write_algorithm->address,
  750. sizeof(kinetis_flash_write_code), kinetis_flash_write_code);
  751. if (retval != ERROR_OK)
  752. return retval;
  753. /* memory buffer */
  754. while (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK) {
  755. buffer_size /= 4;
  756. if (buffer_size <= 256) {
  757. /* free working area, write algorithm already allocated */
  758. target_free_working_area(target, write_algorithm);
  759. LOG_WARNING("No large enough working area available, can't do block memory writes");
  760. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  761. }
  762. }
  763. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  764. armv7m_info.core_mode = ARM_MODE_THREAD;
  765. init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT); /* address */
  766. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT); /* word count */
  767. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
  768. init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
  769. init_reg_param(&reg_params[4], "r4", 32, PARAM_OUT);
  770. buf_set_u32(reg_params[0].value, 0, 32, address);
  771. buf_set_u32(reg_params[1].value, 0, 32, wcount);
  772. buf_set_u32(reg_params[2].value, 0, 32, source->address);
  773. buf_set_u32(reg_params[3].value, 0, 32, source->address + source->size);
  774. buf_set_u32(reg_params[4].value, 0, 32, FTFx_FSTAT);
  775. retval = target_run_flash_async_algorithm(target, buffer, wcount, 4,
  776. 0, NULL,
  777. 5, reg_params,
  778. source->address, source->size,
  779. write_algorithm->address, 0,
  780. &armv7m_info);
  781. if (retval == ERROR_FLASH_OPERATION_FAILED) {
  782. end_address = buf_get_u32(reg_params[0].value, 0, 32);
  783. LOG_ERROR("Error writing flash at %08" PRIx32, end_address);
  784. retval = target_read_u8(target, FTFx_FSTAT, &fstat);
  785. if (retval == ERROR_OK) {
  786. retval = kinetis_ftfx_decode_error(fstat);
  787. /* reset error flags */
  788. target_write_u8(target, FTFx_FSTAT, 0x70);
  789. }
  790. } else if (retval != ERROR_OK)
  791. LOG_ERROR("Error executing kinetis Flash programming algorithm");
  792. target_free_working_area(target, source);
  793. target_free_working_area(target, write_algorithm);
  794. destroy_reg_param(&reg_params[0]);
  795. destroy_reg_param(&reg_params[1]);
  796. destroy_reg_param(&reg_params[2]);
  797. destroy_reg_param(&reg_params[3]);
  798. destroy_reg_param(&reg_params[4]);
  799. return retval;
  800. }
  801. static int kinetis_protect(struct flash_bank *bank, int set, int first, int last)
  802. {
  803. int i;
  804. if (allow_fcf_writes) {
  805. LOG_ERROR("Protection setting is possible with 'kinetis fcf_source protection' only!");
  806. return ERROR_FAIL;
  807. }
  808. if (!bank->prot_blocks || bank->num_prot_blocks == 0) {
  809. LOG_ERROR("No protection possible for current bank!");
  810. return ERROR_FLASH_BANK_INVALID;
  811. }
  812. for (i = first; i < bank->num_prot_blocks && i <= last; i++)
  813. bank->prot_blocks[i].is_protected = set;
  814. LOG_INFO("Protection bits will be written at the next FCF sector erase or write.");
  815. LOG_INFO("Do not issue 'flash info' command until protection is written,");
  816. LOG_INFO("doing so would re-read protection status from MCU.");
  817. return ERROR_OK;
  818. }
  819. static int kinetis_protect_check(struct flash_bank *bank)
  820. {
  821. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  822. int result;
  823. int i, b;
  824. uint32_t fprot;
  825. if (kinfo->flash_class == FC_PFLASH) {
  826. /* read protection register */
  827. result = target_read_u32(bank->target, FTFx_FPROT3, &fprot);
  828. if (result != ERROR_OK)
  829. return result;
  830. /* Every bit protects 1/32 of the full flash (not necessarily just this bank) */
  831. } else if (kinfo->flash_class == FC_FLEX_NVM) {
  832. uint8_t fdprot;
  833. /* read protection register */
  834. result = target_read_u8(bank->target, FTFx_FDPROT, &fdprot);
  835. if (result != ERROR_OK)
  836. return result;
  837. fprot = fdprot;
  838. } else {
  839. LOG_ERROR("Protection checks for FlexRAM not supported");
  840. return ERROR_FLASH_BANK_INVALID;
  841. }
  842. b = kinfo->protection_block;
  843. for (i = 0; i < bank->num_prot_blocks; i++) {
  844. if ((fprot >> b) & 1)
  845. bank->prot_blocks[i].is_protected = 0;
  846. else
  847. bank->prot_blocks[i].is_protected = 1;
  848. b++;
  849. }
  850. return ERROR_OK;
  851. }
  852. static int kinetis_fill_fcf(struct flash_bank *bank, uint8_t *fcf)
  853. {
  854. uint32_t fprot = 0xffffffff;
  855. uint8_t fsec = 0xfe; /* set MCU unsecure */
  856. uint8_t fdprot = 0xff;
  857. int i;
  858. uint32_t pflash_bit;
  859. uint8_t dflash_bit;
  860. struct flash_bank *bank_iter;
  861. struct kinetis_flash_bank *kinfo;
  862. memset(fcf, 0xff, FCF_SIZE);
  863. pflash_bit = 1;
  864. dflash_bit = 1;
  865. /* iterate over all kinetis banks */
  866. /* current bank is bank 0, it contains FCF */
  867. for (bank_iter = bank; bank_iter; bank_iter = bank_iter->next) {
  868. if (bank_iter->driver != &kinetis_flash
  869. || bank_iter->target != bank->target)
  870. continue;
  871. kinetis_auto_probe(bank_iter);
  872. kinfo = bank->driver_priv;
  873. if (!kinfo)
  874. continue;
  875. if (kinfo->flash_class == FC_PFLASH) {
  876. for (i = 0; i < bank_iter->num_prot_blocks; i++) {
  877. if (bank_iter->prot_blocks[i].is_protected == 1)
  878. fprot &= ~pflash_bit;
  879. pflash_bit <<= 1;
  880. }
  881. } else if (kinfo->flash_class == FC_FLEX_NVM) {
  882. for (i = 0; i < bank_iter->num_prot_blocks; i++) {
  883. if (bank_iter->prot_blocks[i].is_protected == 1)
  884. fdprot &= ~dflash_bit;
  885. dflash_bit <<= 1;
  886. }
  887. }
  888. }
  889. target_buffer_set_u32(bank->target, fcf + FCF_FPROT, fprot);
  890. fcf[FCF_FSEC] = fsec;
  891. fcf[FCF_FOPT] = fcf_fopt;
  892. fcf[FCF_FDPROT] = fdprot;
  893. return ERROR_OK;
  894. }
  895. static int kinetis_ftfx_command(struct target *target, uint8_t fcmd, uint32_t faddr,
  896. uint8_t fccob4, uint8_t fccob5, uint8_t fccob6, uint8_t fccob7,
  897. uint8_t fccob8, uint8_t fccob9, uint8_t fccoba, uint8_t fccobb,
  898. uint8_t *ftfx_fstat)
  899. {
  900. uint8_t command[12] = {faddr & 0xff, (faddr >> 8) & 0xff, (faddr >> 16) & 0xff, fcmd,
  901. fccob7, fccob6, fccob5, fccob4,
  902. fccobb, fccoba, fccob9, fccob8};
  903. int result;
  904. uint8_t fstat;
  905. int64_t ms_timeout = timeval_ms() + 250;
  906. result = target_write_memory(target, FTFx_FCCOB3, 4, 3, command);
  907. if (result != ERROR_OK)
  908. return result;
  909. /* start command */
  910. result = target_write_u8(target, FTFx_FSTAT, 0x80);
  911. if (result != ERROR_OK)
  912. return result;
  913. /* wait for done */
  914. do {
  915. result = target_read_u8(target, FTFx_FSTAT, &fstat);
  916. if (result != ERROR_OK)
  917. return result;
  918. if (fstat & 0x80)
  919. break;
  920. } while (timeval_ms() < ms_timeout);
  921. if (ftfx_fstat)
  922. *ftfx_fstat = fstat;
  923. if ((fstat & 0xf0) != 0x80) {
  924. LOG_DEBUG("ftfx command failed FSTAT: %02X FCCOB: %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X",
  925. fstat, command[3], command[2], command[1], command[0],
  926. command[7], command[6], command[5], command[4],
  927. command[11], command[10], command[9], command[8]);
  928. return kinetis_ftfx_decode_error(fstat);
  929. }
  930. return ERROR_OK;
  931. }
  932. static int kinetis_check_run_mode(struct target *target)
  933. {
  934. int result, i;
  935. uint8_t pmctrl, pmstat;
  936. if (target->state != TARGET_HALTED) {
  937. LOG_ERROR("Target not halted");
  938. return ERROR_TARGET_NOT_HALTED;
  939. }
  940. result = target_read_u8(target, SMC_PMSTAT, &pmstat);
  941. if (result != ERROR_OK)
  942. return result;
  943. if (pmstat == PM_STAT_RUN)
  944. return ERROR_OK;
  945. if (pmstat == PM_STAT_VLPR) {
  946. /* It is safe to switch from VLPR to RUN mode without changing clock */
  947. LOG_INFO("Switching from VLPR to RUN mode.");
  948. pmctrl = PM_CTRL_RUNM_RUN;
  949. result = target_write_u8(target, SMC_PMCTRL, pmctrl);
  950. if (result != ERROR_OK)
  951. return result;
  952. for (i = 100; i; i--) {
  953. result = target_read_u8(target, SMC_PMSTAT, &pmstat);
  954. if (result != ERROR_OK)
  955. return result;
  956. if (pmstat == PM_STAT_RUN)
  957. return ERROR_OK;
  958. }
  959. }
  960. LOG_ERROR("Flash operation not possible in current run mode: SMC_PMSTAT: 0x%x", pmstat);
  961. LOG_ERROR("Issue a 'reset init' command.");
  962. return ERROR_TARGET_NOT_HALTED;
  963. }
  964. static void kinetis_invalidate_flash_cache(struct flash_bank *bank)
  965. {
  966. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  967. uint8_t pfb01cr_byte2 = 0xf0;
  968. if (!(kinfo->flash_support & FS_INVALIDATE_CACHE))
  969. return;
  970. target_write_memory(bank->target, FMC_PFB01CR + 2, 1, 1, &pfb01cr_byte2);
  971. return;
  972. }
  973. static int kinetis_erase(struct flash_bank *bank, int first, int last)
  974. {
  975. int result, i;
  976. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  977. result = kinetis_check_run_mode(bank->target);
  978. if (result != ERROR_OK)
  979. return result;
  980. /* reset error flags */
  981. result = kinetis_ftfx_prepare(bank->target);
  982. if (result != ERROR_OK)
  983. return result;
  984. if ((first > bank->num_sectors) || (last > bank->num_sectors))
  985. return ERROR_FLASH_OPERATION_FAILED;
  986. /*
  987. * FIXME: TODO: use the 'Erase Flash Block' command if the
  988. * requested erase is PFlash or NVM and encompasses the entire
  989. * block. Should be quicker.
  990. */
  991. for (i = first; i <= last; i++) {
  992. /* set command and sector address */
  993. result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTERASE, kinfo->prog_base + bank->sectors[i].offset,
  994. 0, 0, 0, 0, 0, 0, 0, 0, NULL);
  995. if (result != ERROR_OK) {
  996. LOG_WARNING("erase sector %d failed", i);
  997. return ERROR_FLASH_OPERATION_FAILED;
  998. }
  999. bank->sectors[i].is_erased = 1;
  1000. if (bank->base == 0
  1001. && bank->sectors[i].offset <= FCF_ADDRESS
  1002. && bank->sectors[i].offset + bank->sectors[i].size > FCF_ADDRESS + FCF_SIZE) {
  1003. if (allow_fcf_writes) {
  1004. LOG_WARNING("Flash Configuration Field erased, DO NOT reset or power off the device");
  1005. LOG_WARNING("until correct FCF is programmed or MCU gets security lock.");
  1006. } else {
  1007. uint8_t fcf_buffer[FCF_SIZE];
  1008. kinetis_fill_fcf(bank, fcf_buffer);
  1009. result = kinetis_write_inner(bank, fcf_buffer, FCF_ADDRESS, FCF_SIZE);
  1010. if (result != ERROR_OK)
  1011. LOG_WARNING("Flash Configuration Field write failed");
  1012. bank->sectors[i].is_erased = 0;
  1013. }
  1014. }
  1015. }
  1016. kinetis_invalidate_flash_cache(bank);
  1017. return ERROR_OK;
  1018. }
  1019. static int kinetis_make_ram_ready(struct target *target)
  1020. {
  1021. int result;
  1022. uint8_t ftfx_fcnfg;
  1023. /* check if ram ready */
  1024. result = target_read_u8(target, FTFx_FCNFG, &ftfx_fcnfg);
  1025. if (result != ERROR_OK)
  1026. return result;
  1027. if (ftfx_fcnfg & (1 << 1))
  1028. return ERROR_OK; /* ram ready */
  1029. /* make flex ram available */
  1030. result = kinetis_ftfx_command(target, FTFx_CMD_SETFLEXRAM, 0x00ff0000,
  1031. 0, 0, 0, 0, 0, 0, 0, 0, NULL);
  1032. if (result != ERROR_OK)
  1033. return ERROR_FLASH_OPERATION_FAILED;
  1034. /* check again */
  1035. result = target_read_u8(target, FTFx_FCNFG, &ftfx_fcnfg);
  1036. if (result != ERROR_OK)
  1037. return result;
  1038. if (ftfx_fcnfg & (1 << 1))
  1039. return ERROR_OK; /* ram ready */
  1040. return ERROR_FLASH_OPERATION_FAILED;
  1041. }
  1042. static int kinetis_write_sections(struct flash_bank *bank, const uint8_t *buffer,
  1043. uint32_t offset, uint32_t count)
  1044. {
  1045. int result = ERROR_OK;
  1046. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  1047. uint8_t *buffer_aligned = NULL;
  1048. /*
  1049. * Kinetis uses different terms for the granularity of
  1050. * sector writes, e.g. "phrase" or "128 bits". We use
  1051. * the generic term "chunk". The largest possible
  1052. * Kinetis "chunk" is 16 bytes (128 bits).
  1053. */
  1054. uint32_t prog_section_chunk_bytes = kinfo->sector_size >> 8;
  1055. uint32_t prog_size_bytes = kinfo->max_flash_prog_size;
  1056. while (count > 0) {
  1057. uint32_t size = prog_size_bytes - offset % prog_size_bytes;
  1058. uint32_t align_begin = offset % prog_section_chunk_bytes;
  1059. uint32_t align_end;
  1060. uint32_t size_aligned;
  1061. uint16_t chunk_count;
  1062. uint8_t ftfx_fstat;
  1063. if (size > count)
  1064. size = count;
  1065. align_end = (align_begin + size) % prog_section_chunk_bytes;
  1066. if (align_end)
  1067. align_end = prog_section_chunk_bytes - align_end;
  1068. size_aligned = align_begin + size + align_end;
  1069. chunk_count = size_aligned / prog_section_chunk_bytes;
  1070. if (size != size_aligned) {
  1071. /* aligned section: the first, the last or the only */
  1072. if (!buffer_aligned)
  1073. buffer_aligned = malloc(prog_size_bytes);
  1074. memset(buffer_aligned, 0xff, size_aligned);
  1075. memcpy(buffer_aligned + align_begin, buffer, size);
  1076. result = target_write_memory(bank->target, FLEXRAM,
  1077. 4, size_aligned / 4, buffer_aligned);
  1078. LOG_DEBUG("section @ %08" PRIx32 " aligned begin %" PRIu32 ", end %" PRIu32,
  1079. bank->base + offset, align_begin, align_end);
  1080. } else
  1081. result = target_write_memory(bank->target, FLEXRAM,
  1082. 4, size_aligned / 4, buffer);
  1083. LOG_DEBUG("write section @ %08" PRIx32 " with length %" PRIu32 " bytes",
  1084. bank->base + offset, size);
  1085. if (result != ERROR_OK) {
  1086. LOG_ERROR("target_write_memory failed");
  1087. break;
  1088. }
  1089. /* execute section-write command */
  1090. result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTWRITE,
  1091. kinfo->prog_base + offset - align_begin,
  1092. chunk_count>>8, chunk_count, 0, 0,
  1093. 0, 0, 0, 0, &ftfx_fstat);
  1094. if (result != ERROR_OK) {
  1095. LOG_ERROR("Error writing section at %08" PRIx32, bank->base + offset);
  1096. break;
  1097. }
  1098. if (ftfx_fstat & 0x01)
  1099. LOG_ERROR("Flash write error at %08" PRIx32, bank->base + offset);
  1100. buffer += size;
  1101. offset += size;
  1102. count -= size;
  1103. }
  1104. free(buffer_aligned);
  1105. return result;
  1106. }
  1107. static int kinetis_write_inner(struct flash_bank *bank, const uint8_t *buffer,
  1108. uint32_t offset, uint32_t count)
  1109. {
  1110. int result, fallback = 0;
  1111. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  1112. if (!(kinfo->flash_support & FS_PROGRAM_SECTOR)) {
  1113. /* fallback to longword write */
  1114. fallback = 1;
  1115. LOG_WARNING("This device supports Program Longword execution only.");
  1116. } else {
  1117. result = kinetis_make_ram_ready(bank->target);
  1118. if (result != ERROR_OK) {
  1119. fallback = 1;
  1120. LOG_WARNING("FlexRAM not ready, fallback to slow longword write.");
  1121. }
  1122. }
  1123. LOG_DEBUG("flash write @08%" PRIx32, bank->base + offset);
  1124. if (fallback == 0) {
  1125. /* program section command */
  1126. kinetis_write_sections(bank, buffer, offset, count);
  1127. }
  1128. else if (kinfo->flash_support & FS_PROGRAM_LONGWORD) {
  1129. /* program longword command, not supported in FTFE */
  1130. uint8_t *new_buffer = NULL;
  1131. /* check word alignment */
  1132. if (offset & 0x3) {
  1133. LOG_ERROR("offset 0x%" PRIx32 " breaks the required alignment", offset);
  1134. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  1135. }
  1136. if (count & 0x3) {
  1137. uint32_t old_count = count;
  1138. count = (old_count | 3) + 1;
  1139. new_buffer = malloc(count);
  1140. if (new_buffer == NULL) {
  1141. LOG_ERROR("odd number of bytes to write and no memory "
  1142. "for padding buffer");
  1143. return ERROR_FAIL;
  1144. }
  1145. LOG_INFO("odd number of bytes to write (%" PRIu32 "), extending to %" PRIu32 " "
  1146. "and padding with 0xff", old_count, count);
  1147. memset(new_buffer + old_count, 0xff, count - old_count);
  1148. buffer = memcpy(new_buffer, buffer, old_count);
  1149. }
  1150. uint32_t words_remaining = count / 4;
  1151. kinetis_disable_wdog(bank->target, kinfo->sim_sdid);
  1152. /* try using a block write */
  1153. result = kinetis_write_block(bank, buffer, offset, words_remaining);
  1154. if (result == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
  1155. /* if block write failed (no sufficient working area),
  1156. * we use normal (slow) single word accesses */
  1157. LOG_WARNING("couldn't use block writes, falling back to single "
  1158. "memory accesses");
  1159. while (words_remaining) {
  1160. uint8_t ftfx_fstat;
  1161. LOG_DEBUG("write longword @ %08" PRIx32, (uint32_t)(bank->base + offset));
  1162. result = kinetis_ftfx_command(bank->target, FTFx_CMD_LWORDPROG, kinfo->prog_base + offset,
  1163. buffer[3], buffer[2], buffer[1], buffer[0],
  1164. 0, 0, 0, 0, &ftfx_fstat);
  1165. if (result != ERROR_OK) {
  1166. LOG_ERROR("Error writing longword at %08" PRIx32, bank->base + offset);
  1167. break;
  1168. }
  1169. if (ftfx_fstat & 0x01)
  1170. LOG_ERROR("Flash write error at %08" PRIx32, bank->base + offset);
  1171. buffer += 4;
  1172. offset += 4;
  1173. words_remaining--;
  1174. }
  1175. }
  1176. free(new_buffer);
  1177. } else {
  1178. LOG_ERROR("Flash write strategy not implemented");
  1179. return ERROR_FLASH_OPERATION_FAILED;
  1180. }
  1181. kinetis_invalidate_flash_cache(bank);
  1182. return result;
  1183. }
  1184. static int kinetis_write(struct flash_bank *bank, const uint8_t *buffer,
  1185. uint32_t offset, uint32_t count)
  1186. {
  1187. int result;
  1188. bool set_fcf = false;
  1189. int sect = 0;
  1190. result = kinetis_check_run_mode(bank->target);
  1191. if (result != ERROR_OK)
  1192. return result;
  1193. /* reset error flags */
  1194. result = kinetis_ftfx_prepare(bank->target);
  1195. if (result != ERROR_OK)
  1196. return result;
  1197. if (bank->base == 0 && !allow_fcf_writes) {
  1198. if (bank->sectors[1].offset <= FCF_ADDRESS)
  1199. sect = 1; /* 1kb sector, FCF in 2nd sector */
  1200. if (offset < bank->sectors[sect].offset + bank->sectors[sect].size
  1201. && offset + count > bank->sectors[sect].offset)
  1202. set_fcf = true; /* write to any part of sector with FCF */
  1203. }
  1204. if (set_fcf) {
  1205. uint8_t fcf_buffer[FCF_SIZE];
  1206. uint8_t fcf_current[FCF_SIZE];
  1207. kinetis_fill_fcf(bank, fcf_buffer);
  1208. if (offset < FCF_ADDRESS) {
  1209. /* write part preceding FCF */
  1210. result = kinetis_write_inner(bank, buffer, offset, FCF_ADDRESS - offset);
  1211. if (result != ERROR_OK)
  1212. return result;
  1213. }
  1214. result = target_read_memory(bank->target, FCF_ADDRESS, 4, FCF_SIZE / 4, fcf_current);
  1215. if (result == ERROR_OK && memcmp(fcf_current, fcf_buffer, FCF_SIZE) == 0)
  1216. set_fcf = false;
  1217. if (set_fcf) {
  1218. /* write FCF if differs from flash - eliminate multiple writes */
  1219. result = kinetis_write_inner(bank, fcf_buffer, FCF_ADDRESS, FCF_SIZE);
  1220. if (result != ERROR_OK)
  1221. return result;
  1222. }
  1223. LOG_WARNING("Flash Configuration Field written.");
  1224. LOG_WARNING("Reset or power off the device to make settings effective.");
  1225. if (offset + count > FCF_ADDRESS + FCF_SIZE) {
  1226. uint32_t delta = FCF_ADDRESS + FCF_SIZE - offset;
  1227. /* write part after FCF */
  1228. result = kinetis_write_inner(bank, buffer + delta, FCF_ADDRESS + FCF_SIZE, count - delta);
  1229. }
  1230. return result;
  1231. } else
  1232. /* no FCF fiddling, normal write */
  1233. return kinetis_write_inner(bank, buffer, offset, count);
  1234. }
  1235. static int kinetis_probe(struct flash_bank *bank)
  1236. {
  1237. int result, i;
  1238. uint8_t fcfg1_nvmsize, fcfg1_pfsize, fcfg1_eesize, fcfg1_depart;
  1239. uint8_t fcfg2_maxaddr0, fcfg2_pflsh, fcfg2_maxaddr1;
  1240. uint32_t nvm_size = 0, pf_size = 0, df_size = 0, ee_size = 0;
  1241. unsigned num_blocks = 0, num_pflash_blocks = 0, num_nvm_blocks = 0, first_nvm_bank = 0,
  1242. pflash_sector_size_bytes = 0, nvm_sector_size_bytes = 0;
  1243. struct target *target = bank->target;
  1244. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  1245. kinfo->probed = false;
  1246. result = target_read_u32(target, SIM_SDID, &kinfo->sim_sdid);
  1247. if (result != ERROR_OK)
  1248. return result;
  1249. if ((kinfo->sim_sdid & (~KINETIS_SDID_K_SERIES_MASK)) == 0) {
  1250. /* older K-series MCU */
  1251. uint32_t mcu_type = kinfo->sim_sdid & KINETIS_K_SDID_TYPE_MASK;
  1252. switch (mcu_type) {
  1253. case KINETIS_K_SDID_K10_M50:
  1254. case KINETIS_K_SDID_K20_M50:
  1255. /* 1kB sectors */
  1256. pflash_sector_size_bytes = 1<<10;
  1257. nvm_sector_size_bytes = 1<<10;
  1258. num_blocks = 2;
  1259. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1260. break;
  1261. case KINETIS_K_SDID_K10_M72:
  1262. case KINETIS_K_SDID_K20_M72:
  1263. case KINETIS_K_SDID_K30_M72:
  1264. case KINETIS_K_SDID_K30_M100:
  1265. case KINETIS_K_SDID_K40_M72:
  1266. case KINETIS_K_SDID_K40_M100:
  1267. case KINETIS_K_SDID_K50_M72:
  1268. /* 2kB sectors, 1kB FlexNVM sectors */
  1269. pflash_sector_size_bytes = 2<<10;
  1270. nvm_sector_size_bytes = 1<<10;
  1271. num_blocks = 2;
  1272. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1273. kinfo->max_flash_prog_size = 1<<10;
  1274. break;
  1275. case KINETIS_K_SDID_K10_M100:
  1276. case KINETIS_K_SDID_K20_M100:
  1277. case KINETIS_K_SDID_K11:
  1278. case KINETIS_K_SDID_K12:
  1279. case KINETIS_K_SDID_K21_M50:
  1280. case KINETIS_K_SDID_K22_M50:
  1281. case KINETIS_K_SDID_K51_M72:
  1282. case KINETIS_K_SDID_K53:
  1283. case KINETIS_K_SDID_K60_M100:
  1284. /* 2kB sectors */
  1285. pflash_sector_size_bytes = 2<<10;
  1286. nvm_sector_size_bytes = 2<<10;
  1287. num_blocks = 2;
  1288. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1289. break;
  1290. case KINETIS_K_SDID_K21_M120:
  1291. case KINETIS_K_SDID_K22_M120:
  1292. /* 4kB sectors (MK21FN1M0, MK21FX512, MK22FN1M0, MK22FX512) */
  1293. pflash_sector_size_bytes = 4<<10;
  1294. kinfo->max_flash_prog_size = 1<<10;
  1295. nvm_sector_size_bytes = 4<<10;
  1296. num_blocks = 2;
  1297. kinfo->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1298. break;
  1299. case KINETIS_K_SDID_K10_M120:
  1300. case KINETIS_K_SDID_K20_M120:
  1301. case KINETIS_K_SDID_K60_M150:
  1302. case KINETIS_K_SDID_K70_M150:
  1303. /* 4kB sectors */
  1304. pflash_sector_size_bytes = 4<<10;
  1305. nvm_sector_size_bytes = 4<<10;
  1306. num_blocks = 4;
  1307. kinfo->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1308. break;
  1309. default:
  1310. LOG_ERROR("Unsupported K-family FAMID");
  1311. }
  1312. } else {
  1313. /* Newer K-series or KL series MCU */
  1314. switch (kinfo->sim_sdid & KINETIS_SDID_SERIESID_MASK) {
  1315. case KINETIS_SDID_SERIESID_K:
  1316. switch (kinfo->sim_sdid & (KINETIS_SDID_FAMILYID_MASK | KINETIS_SDID_SUBFAMID_MASK)) {
  1317. case KINETIS_SDID_FAMILYID_K0X | KINETIS_SDID_SUBFAMID_KX2:
  1318. /* K02FN64, K02FN128: FTFA, 2kB sectors */
  1319. pflash_sector_size_bytes = 2<<10;
  1320. num_blocks = 1;
  1321. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE;
  1322. break;
  1323. case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX2: {
  1324. /* MK24FN1M reports as K22, this should detect it (according to errata note 1N83J) */
  1325. uint32_t sopt1;
  1326. result = target_read_u32(target, SIM_SOPT1, &sopt1);
  1327. if (result != ERROR_OK)
  1328. return result;
  1329. if (((kinfo->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K24FN1M) &&
  1330. ((sopt1 & KINETIS_SOPT1_RAMSIZE_MASK) == KINETIS_SOPT1_RAMSIZE_K24FN1M)) {
  1331. /* MK24FN1M */
  1332. pflash_sector_size_bytes = 4<<10;
  1333. num_blocks = 2;
  1334. kinfo->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1335. kinfo->max_flash_prog_size = 1<<10;
  1336. break;
  1337. }
  1338. if ((kinfo->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K22FN128
  1339. || (kinfo->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K22FN256
  1340. || (kinfo->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K22FN512) {
  1341. /* K22 with new-style SDID - smaller pflash with FTFA, 2kB sectors */
  1342. pflash_sector_size_bytes = 2<<10;
  1343. /* autodetect 1 or 2 blocks */
  1344. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE;
  1345. break;
  1346. }
  1347. LOG_ERROR("Unsupported Kinetis K22 DIEID");
  1348. break;
  1349. }
  1350. case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX4:
  1351. pflash_sector_size_bytes = 4<<10;
  1352. if ((kinfo->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K24FN256) {
  1353. /* K24FN256 - smaller pflash with FTFA */
  1354. num_blocks = 1;
  1355. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE;
  1356. break;
  1357. }
  1358. /* K24FN1M without errata 7534 */
  1359. num_blocks = 2;
  1360. kinfo->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1361. kinfo->max_flash_prog_size = 1<<10;
  1362. break;
  1363. case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX3:
  1364. case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX1: /* errata 7534 - should be K63 */
  1365. /* K63FN1M0 */
  1366. case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX4:
  1367. case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX2: /* errata 7534 - should be K64 */
  1368. /* K64FN1M0, K64FX512 */
  1369. pflash_sector_size_bytes = 4<<10;
  1370. nvm_sector_size_bytes = 4<<10;
  1371. kinfo->max_flash_prog_size = 1<<10;
  1372. num_blocks = 2;
  1373. kinfo->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1374. break;
  1375. case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX6:
  1376. /* K26FN2M0 */
  1377. case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX6:
  1378. /* K66FN2M0, K66FX1M0 */
  1379. pflash_sector_size_bytes = 4<<10;
  1380. nvm_sector_size_bytes = 4<<10;
  1381. kinfo->max_flash_prog_size = 1<<10;
  1382. num_blocks = 4;
  1383. kinfo->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1384. break;
  1385. default:
  1386. LOG_ERROR("Unsupported Kinetis FAMILYID SUBFAMID");
  1387. }
  1388. break;
  1389. case KINETIS_SDID_SERIESID_KL:
  1390. /* KL-series */
  1391. pflash_sector_size_bytes = 1<<10;
  1392. nvm_sector_size_bytes = 1<<10;
  1393. /* autodetect 1 or 2 blocks */
  1394. kinfo->flash_support = FS_PROGRAM_LONGWORD;
  1395. break;
  1396. case KINETIS_SDID_SERIESID_KV:
  1397. /* KV-series */
  1398. switch (kinfo->sim_sdid & (KINETIS_SDID_FAMILYID_MASK | KINETIS_SDID_SUBFAMID_MASK)) {
  1399. case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX0:
  1400. /* KV10: FTFA, 1kB sectors */
  1401. pflash_sector_size_bytes = 1<<10;
  1402. num_blocks = 1;
  1403. kinfo->flash_support = FS_PROGRAM_LONGWORD;
  1404. break;
  1405. case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX1:
  1406. /* KV11: FTFA, 2kB sectors */
  1407. pflash_sector_size_bytes = 2<<10;
  1408. num_blocks = 1;
  1409. kinfo->flash_support = FS_PROGRAM_LONGWORD;
  1410. break;
  1411. case KINETIS_SDID_FAMILYID_K3X | KINETIS_SDID_SUBFAMID_KX0:
  1412. /* KV30: FTFA, 2kB sectors, 1 block */
  1413. case KINETIS_SDID_FAMILYID_K3X | KINETIS_SDID_SUBFAMID_KX1:
  1414. /* KV31: FTFA, 2kB sectors, 2 blocks */
  1415. pflash_sector_size_bytes = 2<<10;
  1416. /* autodetect 1 or 2 blocks */
  1417. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE;
  1418. break;
  1419. case KINETIS_SDID_FAMILYID_K4X | KINETIS_SDID_SUBFAMID_KX2:
  1420. case KINETIS_SDID_FAMILYID_K4X | KINETIS_SDID_SUBFAMID_KX4:
  1421. case KINETIS_SDID_FAMILYID_K4X | KINETIS_SDID_SUBFAMID_KX6:
  1422. /* KV4x: FTFA, 4kB sectors */
  1423. pflash_sector_size_bytes = 4<<10;
  1424. num_blocks = 1;
  1425. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE;
  1426. break;
  1427. default:
  1428. LOG_ERROR("Unsupported KV FAMILYID SUBFAMID");
  1429. }
  1430. break;
  1431. default:
  1432. LOG_ERROR("Unsupported K-series");
  1433. }
  1434. }
  1435. if (pflash_sector_size_bytes == 0) {
  1436. LOG_ERROR("MCU is unsupported, SDID 0x%08" PRIx32, kinfo->sim_sdid);
  1437. return ERROR_FLASH_OPER_UNSUPPORTED;
  1438. }
  1439. result = target_read_u32(target, SIM_FCFG1, &kinfo->sim_fcfg1);
  1440. if (result != ERROR_OK)
  1441. return result;
  1442. result = target_read_u32(target, SIM_FCFG2, &kinfo->sim_fcfg2);
  1443. if (result != ERROR_OK)
  1444. return result;
  1445. LOG_DEBUG("SDID: 0x%08" PRIX32 " FCFG1: 0x%08" PRIX32 " FCFG2: 0x%08" PRIX32, kinfo->sim_sdid,
  1446. kinfo->sim_fcfg1, kinfo->sim_fcfg2);
  1447. fcfg1_nvmsize = (uint8_t)((kinfo->sim_fcfg1 >> 28) & 0x0f);
  1448. fcfg1_pfsize = (uint8_t)((kinfo->sim_fcfg1 >> 24) & 0x0f);
  1449. fcfg1_eesize = (uint8_t)((kinfo->sim_fcfg1 >> 16) & 0x0f);
  1450. fcfg1_depart = (uint8_t)((kinfo->sim_fcfg1 >> 8) & 0x0f);
  1451. fcfg2_pflsh = (uint8_t)((kinfo->sim_fcfg2 >> 23) & 0x01);
  1452. fcfg2_maxaddr0 = (uint8_t)((kinfo->sim_fcfg2 >> 24) & 0x7f);
  1453. fcfg2_maxaddr1 = (uint8_t)((kinfo->sim_fcfg2 >> 16) & 0x7f);
  1454. if (num_blocks == 0)
  1455. num_blocks = fcfg2_maxaddr1 ? 2 : 1;
  1456. else if (fcfg2_maxaddr1 == 0 && num_blocks >= 2) {
  1457. num_blocks = 1;
  1458. LOG_WARNING("MAXADDR1 is zero, number of flash banks adjusted to 1");
  1459. } else if (fcfg2_maxaddr1 != 0 && num_blocks == 1) {
  1460. num_blocks = 2;
  1461. LOG_WARNING("MAXADDR1 is non zero, number of flash banks adjusted to 2");
  1462. }
  1463. /* when the PFLSH bit is set, there is no FlexNVM/FlexRAM */
  1464. if (!fcfg2_pflsh) {
  1465. switch (fcfg1_nvmsize) {
  1466. case 0x03:
  1467. case 0x05:
  1468. case 0x07:
  1469. case 0x09:
  1470. case 0x0b:
  1471. nvm_size = 1 << (14 + (fcfg1_nvmsize >> 1));
  1472. break;
  1473. case 0x0f:
  1474. if (pflash_sector_size_bytes >= 4<<10)
  1475. nvm_size = 512<<10;
  1476. else
  1477. /* K20_100 */
  1478. nvm_size = 256<<10;
  1479. break;
  1480. default:
  1481. nvm_size = 0;
  1482. break;
  1483. }
  1484. switch (fcfg1_eesize) {
  1485. case 0x00:
  1486. case 0x01:
  1487. case 0x02:
  1488. case 0x03:
  1489. case 0x04:
  1490. case 0x05:
  1491. case 0x06:
  1492. case 0x07:
  1493. case 0x08:
  1494. case 0x09:
  1495. ee_size = (16 << (10 - fcfg1_eesize));
  1496. break;
  1497. default:
  1498. ee_size = 0;
  1499. break;
  1500. }
  1501. switch (fcfg1_depart) {
  1502. case 0x01:
  1503. case 0x02:
  1504. case 0x03:
  1505. case 0x04:
  1506. case 0x05:
  1507. case 0x06:
  1508. df_size = nvm_size - (4096 << fcfg1_depart);
  1509. break;
  1510. case 0x08:
  1511. df_size = 0;
  1512. break;
  1513. case 0x09:
  1514. case 0x0a:
  1515. case 0x0b:
  1516. case 0x0c:
  1517. case 0x0d:
  1518. df_size = 4096 << (fcfg1_depart & 0x7);
  1519. break;
  1520. default:
  1521. df_size = nvm_size;
  1522. break;
  1523. }
  1524. }
  1525. switch (fcfg1_pfsize) {
  1526. case 0x03:
  1527. case 0x05:
  1528. case 0x07:
  1529. case 0x09:
  1530. case 0x0b:
  1531. case 0x0d:
  1532. pf_size = 1 << (14 + (fcfg1_pfsize >> 1));
  1533. break;
  1534. case 0x0f:
  1535. /* a peculiar case: Freescale states different sizes for 0xf
  1536. * K02P64M100SFARM 128 KB ... duplicate of code 0x7
  1537. * K22P121M120SF8RM 256 KB ... duplicate of code 0x9
  1538. * K22P121M120SF7RM 512 KB ... duplicate of code 0xb
  1539. * K22P100M120SF5RM 1024 KB ... duplicate of code 0xd
  1540. * K26P169M180SF5RM 2048 KB ... the only unique value
  1541. * fcfg2_maxaddr0 seems to be the only clue to pf_size
  1542. * Checking fcfg2_maxaddr0 later in this routine is pointless then
  1543. */
  1544. if (fcfg2_pflsh)
  1545. pf_size = ((uint32_t)fcfg2_maxaddr0 << 13) * num_blocks;
  1546. else
  1547. pf_size = ((uint32_t)fcfg2_maxaddr0 << 13) * num_blocks / 2;
  1548. if (pf_size != 2048<<10)
  1549. LOG_WARNING("SIM_FCFG1 PFSIZE = 0xf: please check if pflash is %u KB", pf_size>>10);
  1550. break;
  1551. default:
  1552. pf_size = 0;
  1553. break;
  1554. }
  1555. LOG_DEBUG("FlexNVM: %" PRIu32 " PFlash: %" PRIu32 " FlexRAM: %" PRIu32 " PFLSH: %d",
  1556. nvm_size, pf_size, ee_size, fcfg2_pflsh);
  1557. num_pflash_blocks = num_blocks / (2 - fcfg2_pflsh);
  1558. first_nvm_bank = num_pflash_blocks;
  1559. num_nvm_blocks = num_blocks - num_pflash_blocks;
  1560. LOG_DEBUG("%d blocks total: %d PFlash, %d FlexNVM",
  1561. num_blocks, num_pflash_blocks, num_nvm_blocks);
  1562. LOG_INFO("Probing flash info for bank %d", bank->bank_number);
  1563. if ((unsigned)bank->bank_number < num_pflash_blocks) {
  1564. /* pflash, banks start at address zero */
  1565. kinfo->flash_class = FC_PFLASH;
  1566. bank->size = (pf_size / num_pflash_blocks);
  1567. bank->base = 0x00000000 + bank->size * bank->bank_number;
  1568. kinfo->prog_base = bank->base;
  1569. kinfo->sector_size = pflash_sector_size_bytes;
  1570. /* pflash is divided into 32 protection areas for
  1571. * parts with more than 32K of PFlash. For parts with
  1572. * less the protection unit is set to 1024 bytes */
  1573. kinfo->protection_size = MAX(pf_size / 32, 1024);
  1574. bank->num_prot_blocks = 32 / num_pflash_blocks;
  1575. kinfo->protection_block = bank->num_prot_blocks * bank->bank_number;
  1576. } else if ((unsigned)bank->bank_number < num_blocks) {
  1577. /* nvm, banks start at address 0x10000000 */
  1578. unsigned nvm_ord = bank->bank_number - first_nvm_bank;
  1579. uint32_t limit;
  1580. kinfo->flash_class = FC_FLEX_NVM;
  1581. bank->size = (nvm_size / num_nvm_blocks);
  1582. bank->base = 0x10000000 + bank->size * nvm_ord;
  1583. kinfo->prog_base = 0x00800000 + bank->size * nvm_ord;
  1584. kinfo->sector_size = nvm_sector_size_bytes;
  1585. if (df_size == 0) {
  1586. kinfo->protection_size = 0;
  1587. } else {
  1588. for (i = df_size; ~i & 1; i >>= 1)
  1589. ;
  1590. if (i == 1)
  1591. kinfo->protection_size = df_size / 8; /* data flash size = 2^^n */
  1592. else
  1593. kinfo->protection_size = nvm_size / 8; /* TODO: verify on SF1, not documented in RM */
  1594. }
  1595. bank->num_prot_blocks = 8 / num_nvm_blocks;
  1596. kinfo->protection_block = bank->num_prot_blocks * nvm_ord;
  1597. /* EEPROM backup part of FlexNVM is not accessible, use df_size as a limit */
  1598. if (df_size > bank->size * nvm_ord)
  1599. limit = df_size - bank->size * nvm_ord;
  1600. else
  1601. limit = 0;
  1602. if (bank->size > limit) {
  1603. bank->size = limit;
  1604. LOG_DEBUG("FlexNVM bank %d limited to 0x%08" PRIx32 " due to active EEPROM backup",
  1605. bank->bank_number, limit);
  1606. }
  1607. } else if ((unsigned)bank->bank_number == num_blocks) {
  1608. LOG_ERROR("FlexRAM support not yet implemented");
  1609. return ERROR_FLASH_OPER_UNSUPPORTED;
  1610. } else {
  1611. LOG_ERROR("Cannot determine parameters for bank %d, only %d banks on device",
  1612. bank->bank_number, num_blocks);
  1613. return ERROR_FLASH_BANK_INVALID;
  1614. }
  1615. if (bank->bank_number == 0 && ((uint32_t)fcfg2_maxaddr0 << 13) != bank->size)
  1616. LOG_WARNING("MAXADDR0 0x%02" PRIx8 " check failed,"
  1617. " please report to OpenOCD mailing list", fcfg2_maxaddr0);
  1618. if (fcfg2_pflsh) {
  1619. if (bank->bank_number == 1 && ((uint32_t)fcfg2_maxaddr1 << 13) != bank->size)
  1620. LOG_WARNING("MAXADDR1 0x%02" PRIx8 " check failed,"
  1621. " please report to OpenOCD mailing list", fcfg2_maxaddr1);
  1622. } else {
  1623. if ((unsigned)bank->bank_number == first_nvm_bank
  1624. && ((uint32_t)fcfg2_maxaddr1 << 13) != df_size)
  1625. LOG_WARNING("FlexNVM MAXADDR1 0x%02" PRIx8 " check failed,"
  1626. " please report to OpenOCD mailing list", fcfg2_maxaddr1);
  1627. }
  1628. if (bank->sectors) {
  1629. free(bank->sectors);
  1630. bank->sectors = NULL;
  1631. }
  1632. if (bank->prot_blocks) {
  1633. free(bank->prot_blocks);
  1634. bank->prot_blocks = NULL;
  1635. }
  1636. if (kinfo->sector_size == 0) {
  1637. LOG_ERROR("Unknown sector size for bank %d", bank->bank_number);
  1638. return ERROR_FLASH_BANK_INVALID;
  1639. }
  1640. if (kinfo->flash_support & FS_PROGRAM_SECTOR
  1641. && kinfo->max_flash_prog_size == 0) {
  1642. kinfo->max_flash_prog_size = kinfo->sector_size;
  1643. /* Program section size is equal to sector size by default */
  1644. }
  1645. bank->num_sectors = bank->size / kinfo->sector_size;
  1646. if (bank->num_sectors > 0) {
  1647. /* FlexNVM bank can be used for EEPROM backup therefore zero sized */
  1648. bank->sectors = alloc_block_array(0, kinfo->sector_size, bank->num_sectors);
  1649. if (!bank->sectors)
  1650. return ERROR_FAIL;
  1651. bank->prot_blocks = alloc_block_array(0, kinfo->protection_size, bank->num_prot_blocks);
  1652. if (!bank->prot_blocks)
  1653. return ERROR_FAIL;
  1654. } else {
  1655. bank->num_prot_blocks = 0;
  1656. }
  1657. kinfo->probed = true;
  1658. return ERROR_OK;
  1659. }
  1660. static int kinetis_auto_probe(struct flash_bank *bank)
  1661. {
  1662. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  1663. if (kinfo && kinfo->probed)
  1664. return ERROR_OK;
  1665. return kinetis_probe(bank);
  1666. }
  1667. static int kinetis_info(struct flash_bank *bank, char *buf, int buf_size)
  1668. {
  1669. const char *bank_class_names[] = {
  1670. "(ANY)", "PFlash", "FlexNVM", "FlexRAM"
  1671. };
  1672. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  1673. (void) snprintf(buf, buf_size,
  1674. "%s driver for %s flash bank %s at 0x%8.8" PRIx32 "",
  1675. bank->driver->name, bank_class_names[kinfo->flash_class],
  1676. bank->name, bank->base);
  1677. return ERROR_OK;
  1678. }
  1679. static int kinetis_blank_check(struct flash_bank *bank)
  1680. {
  1681. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  1682. int result;
  1683. /* suprisingly blank check does not work in VLPR and HSRUN modes */
  1684. result = kinetis_check_run_mode(bank->target);
  1685. if (result != ERROR_OK)
  1686. return result;
  1687. /* reset error flags */
  1688. result = kinetis_ftfx_prepare(bank->target);
  1689. if (result != ERROR_OK)
  1690. return result;
  1691. if (kinfo->flash_class == FC_PFLASH || kinfo->flash_class == FC_FLEX_NVM) {
  1692. bool block_dirty = false;
  1693. uint8_t ftfx_fstat;
  1694. if (kinfo->flash_class == FC_FLEX_NVM) {
  1695. uint8_t fcfg1_depart = (uint8_t)((kinfo->sim_fcfg1 >> 8) & 0x0f);
  1696. /* block operation cannot be used on FlexNVM when EEPROM backup partition is set */
  1697. if (fcfg1_depart != 0xf && fcfg1_depart != 0)
  1698. block_dirty = true;
  1699. }
  1700. if (!block_dirty) {
  1701. /* check if whole bank is blank */
  1702. result = kinetis_ftfx_command(bank->target, FTFx_CMD_BLOCKSTAT, kinfo->prog_base,
  1703. 0, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat);
  1704. if (result != ERROR_OK || (ftfx_fstat & 0x01))
  1705. block_dirty = true;
  1706. }
  1707. if (block_dirty) {
  1708. /* the whole bank is not erased, check sector-by-sector */
  1709. int i;
  1710. for (i = 0; i < bank->num_sectors; i++) {
  1711. /* normal margin */
  1712. result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTSTAT,
  1713. kinfo->prog_base + bank->sectors[i].offset,
  1714. 1, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat);
  1715. if (result == ERROR_OK) {
  1716. bank->sectors[i].is_erased = !(ftfx_fstat & 0x01);
  1717. } else {
  1718. LOG_DEBUG("Ignoring errored PFlash sector blank-check");
  1719. bank->sectors[i].is_erased = -1;
  1720. }
  1721. }
  1722. } else {
  1723. /* the whole bank is erased, update all sectors */
  1724. int i;
  1725. for (i = 0; i < bank->num_sectors; i++)
  1726. bank->sectors[i].is_erased = 1;
  1727. }
  1728. } else {
  1729. LOG_WARNING("kinetis_blank_check not supported yet for FlexRAM");
  1730. return ERROR_FLASH_OPERATION_FAILED;
  1731. }
  1732. return ERROR_OK;
  1733. }
  1734. COMMAND_HANDLER(kinetis_nvm_partition)
  1735. {
  1736. int result, i;
  1737. unsigned long par, log2 = 0, ee1 = 0, ee2 = 0;
  1738. enum { SHOW_INFO, DF_SIZE, EEBKP_SIZE } sz_type = SHOW_INFO;
  1739. bool enable;
  1740. uint8_t load_flex_ram = 1;
  1741. uint8_t ee_size_code = 0x3f;
  1742. uint8_t flex_nvm_partition_code = 0;
  1743. uint8_t ee_split = 3;
  1744. struct target *target = get_current_target(CMD_CTX);
  1745. struct flash_bank *bank;
  1746. struct kinetis_flash_bank *kinfo;
  1747. uint32_t sim_fcfg1;
  1748. if (CMD_ARGC >= 2) {
  1749. if (strcmp(CMD_ARGV[0], "dataflash") == 0)
  1750. sz_type = DF_SIZE;
  1751. else if (strcmp(CMD_ARGV[0], "eebkp") == 0)
  1752. sz_type = EEBKP_SIZE;
  1753. par = strtoul(CMD_ARGV[1], NULL, 10);
  1754. while (par >> (log2 + 3))
  1755. log2++;
  1756. }
  1757. switch (sz_type) {
  1758. case SHOW_INFO:
  1759. result = target_read_u32(target, SIM_FCFG1, &sim_fcfg1);
  1760. if (result != ERROR_OK)
  1761. return result;
  1762. flex_nvm_partition_code = (uint8_t)((sim_fcfg1 >> 8) & 0x0f);
  1763. switch (flex_nvm_partition_code) {
  1764. case 0:
  1765. command_print(CMD_CTX, "No EEPROM backup, data flash only");
  1766. break;
  1767. case 1:
  1768. case 2:
  1769. case 3:
  1770. case 4:
  1771. case 5:
  1772. case 6:
  1773. command_print(CMD_CTX, "EEPROM backup %d KB", 4 << flex_nvm_partition_code);
  1774. break;
  1775. case 8:
  1776. command_print(CMD_CTX, "No data flash, EEPROM backup only");
  1777. break;
  1778. case 0x9:
  1779. case 0xA:
  1780. case 0xB:
  1781. case 0xC:
  1782. case 0xD:
  1783. case 0xE:
  1784. command_print(CMD_CTX, "data flash %d KB", 4 << (flex_nvm_partition_code & 7));
  1785. break;
  1786. case 0xf:
  1787. command_print(CMD_CTX, "No EEPROM backup, data flash only (DEPART not set)");
  1788. break;
  1789. default:
  1790. command_print(CMD_CTX, "Unsupported EEPROM backup size code 0x%02" PRIx8, flex_nvm_partition_code);
  1791. }
  1792. return ERROR_OK;
  1793. case DF_SIZE:
  1794. flex_nvm_partition_code = 0x8 | log2;
  1795. break;
  1796. case EEBKP_SIZE:
  1797. flex_nvm_partition_code = log2;
  1798. break;
  1799. }
  1800. if (CMD_ARGC == 3)
  1801. ee1 = ee2 = strtoul(CMD_ARGV[2], NULL, 10) / 2;
  1802. else if (CMD_ARGC >= 4) {
  1803. ee1 = strtoul(CMD_ARGV[2], NULL, 10);
  1804. ee2 = strtoul(CMD_ARGV[3], NULL, 10);
  1805. }
  1806. enable = ee1 + ee2 > 0;
  1807. if (enable) {
  1808. for (log2 = 2; ; log2++) {
  1809. if (ee1 + ee2 == (16u << 10) >> log2)
  1810. break;
  1811. if (ee1 + ee2 > (16u << 10) >> log2 || log2 >= 9) {
  1812. LOG_ERROR("Unsupported EEPROM size");
  1813. return ERROR_FLASH_OPERATION_FAILED;
  1814. }
  1815. }
  1816. if (ee1 * 3 == ee2)
  1817. ee_split = 1;
  1818. else if (ee1 * 7 == ee2)
  1819. ee_split = 0;
  1820. else if (ee1 != ee2) {
  1821. LOG_ERROR("Unsupported EEPROM sizes ratio");
  1822. return ERROR_FLASH_OPERATION_FAILED;
  1823. }
  1824. ee_size_code = log2 | ee_split << 4;
  1825. }
  1826. if (CMD_ARGC >= 5)
  1827. COMMAND_PARSE_ON_OFF(CMD_ARGV[4], enable);
  1828. if (enable)
  1829. load_flex_ram = 0;
  1830. LOG_INFO("DEPART 0x%" PRIx8 ", EEPROM size code 0x%" PRIx8,
  1831. flex_nvm_partition_code, ee_size_code);
  1832. result = kinetis_check_run_mode(target);
  1833. if (result != ERROR_OK)
  1834. return result;
  1835. /* reset error flags */
  1836. result = kinetis_ftfx_prepare(target);
  1837. if (result != ERROR_OK)
  1838. return result;
  1839. result = kinetis_ftfx_command(target, FTFx_CMD_PGMPART, load_flex_ram,
  1840. ee_size_code, flex_nvm_partition_code, 0, 0,
  1841. 0, 0, 0, 0, NULL);
  1842. if (result != ERROR_OK)
  1843. return result;
  1844. command_print(CMD_CTX, "FlexNVM partition set. Please reset MCU.");
  1845. for (i = 1; i < 4; i++) {
  1846. bank = get_flash_bank_by_num_noprobe(i);
  1847. if (bank == NULL)
  1848. break;
  1849. kinfo = bank->driver_priv;
  1850. if (kinfo && kinfo->flash_class == FC_FLEX_NVM)
  1851. kinfo->probed = false; /* re-probe before next use */
  1852. }
  1853. command_print(CMD_CTX, "FlexNVM banks will be re-probed to set new data flash size.");
  1854. return ERROR_OK;
  1855. }
  1856. COMMAND_HANDLER(kinetis_fcf_source_handler)
  1857. {
  1858. if (CMD_ARGC > 1)
  1859. return ERROR_COMMAND_SYNTAX_ERROR;
  1860. if (CMD_ARGC == 1) {
  1861. if (strcmp(CMD_ARGV[0], "write") == 0)
  1862. allow_fcf_writes = true;
  1863. else if (strcmp(CMD_ARGV[0], "protection") == 0)
  1864. allow_fcf_writes = false;
  1865. else
  1866. return ERROR_COMMAND_SYNTAX_ERROR;
  1867. }
  1868. if (allow_fcf_writes) {
  1869. command_print(CMD_CTX, "Arbitrary Flash Configuration Field writes enabled.");
  1870. command_print(CMD_CTX, "Protection info writes to FCF disabled.");
  1871. LOG_WARNING("BEWARE: incorrect flash configuration may permanently lock the device.");
  1872. } else {
  1873. command_print(CMD_CTX, "Protection info writes to Flash Configuration Field enabled.");
  1874. command_print(CMD_CTX, "Arbitrary FCF writes disabled. Mode safe from unwanted locking of the device.");
  1875. }
  1876. return ERROR_OK;
  1877. }
  1878. COMMAND_HANDLER(kinetis_fopt_handler)
  1879. {
  1880. if (CMD_ARGC > 1)
  1881. return ERROR_COMMAND_SYNTAX_ERROR;
  1882. if (CMD_ARGC == 1)
  1883. fcf_fopt = (uint8_t)strtoul(CMD_ARGV[0], NULL, 0);
  1884. else
  1885. command_print(CMD_CTX, "FCF_FOPT 0x%02" PRIx8, fcf_fopt);
  1886. return ERROR_OK;
  1887. }
  1888. static const struct command_registration kinetis_security_command_handlers[] = {
  1889. {
  1890. .name = "check_security",
  1891. .mode = COMMAND_EXEC,
  1892. .help = "Check status of device security lock",
  1893. .usage = "",
  1894. .handler = kinetis_check_flash_security_status,
  1895. },
  1896. {
  1897. .name = "halt",
  1898. .mode = COMMAND_EXEC,
  1899. .help = "Issue a halt via the MDM-AP",
  1900. .usage = "",
  1901. .handler = kinetis_mdm_halt,
  1902. },
  1903. {
  1904. .name = "mass_erase",
  1905. .mode = COMMAND_EXEC,
  1906. .help = "Issue a complete flash erase via the MDM-AP",
  1907. .usage = "",
  1908. .handler = kinetis_mdm_mass_erase,
  1909. },
  1910. { .name = "reset",
  1911. .mode = COMMAND_EXEC,
  1912. .help = "Issue a reset via the MDM-AP",
  1913. .usage = "",
  1914. .handler = kinetis_mdm_reset,
  1915. },
  1916. COMMAND_REGISTRATION_DONE
  1917. };
  1918. static const struct command_registration kinetis_exec_command_handlers[] = {
  1919. {
  1920. .name = "mdm",
  1921. .mode = COMMAND_ANY,
  1922. .help = "MDM-AP command group",
  1923. .usage = "",
  1924. .chain = kinetis_security_command_handlers,
  1925. },
  1926. {
  1927. .name = "disable_wdog",
  1928. .mode = COMMAND_EXEC,
  1929. .help = "Disable the watchdog timer",
  1930. .usage = "",
  1931. .handler = kinetis_disable_wdog_handler,
  1932. },
  1933. {
  1934. .name = "nvm_partition",
  1935. .mode = COMMAND_EXEC,
  1936. .help = "Show/set data flash or EEPROM backup size in kilobytes,"
  1937. " set two EEPROM sizes in bytes and FlexRAM loading during reset",
  1938. .usage = "('info'|'dataflash' size|'eebkp' size) [eesize1 eesize2] ['on'|'off']",
  1939. .handler = kinetis_nvm_partition,
  1940. },
  1941. {
  1942. .name = "fcf_source",
  1943. .mode = COMMAND_EXEC,
  1944. .help = "Use protection as a source for Flash Configuration Field or allow writing arbitrary values to the FCF"
  1945. " Mode 'protection' is safe from unwanted locking of the device.",
  1946. .usage = "['protection'|'write']",
  1947. .handler = kinetis_fcf_source_handler,
  1948. },
  1949. {
  1950. .name = "fopt",
  1951. .mode = COMMAND_EXEC,
  1952. .help = "FCF_FOPT value source in 'kinetis fcf_source protection' mode",
  1953. .usage = "[num]",
  1954. .handler = kinetis_fopt_handler,
  1955. },
  1956. COMMAND_REGISTRATION_DONE
  1957. };
  1958. static const struct command_registration kinetis_command_handler[] = {
  1959. {
  1960. .name = "kinetis",
  1961. .mode = COMMAND_ANY,
  1962. .help = "Kinetis flash controller commands",
  1963. .usage = "",
  1964. .chain = kinetis_exec_command_handlers,
  1965. },
  1966. COMMAND_REGISTRATION_DONE
  1967. };
  1968. struct flash_driver kinetis_flash = {
  1969. .name = "kinetis",
  1970. .commands = kinetis_command_handler,
  1971. .flash_bank_command = kinetis_flash_bank_command,
  1972. .erase = kinetis_erase,
  1973. .protect = kinetis_protect,
  1974. .write = kinetis_write,
  1975. .read = default_flash_read,
  1976. .probe = kinetis_probe,
  1977. .auto_probe = kinetis_auto_probe,
  1978. .erase_check = kinetis_blank_check,
  1979. .protect_check = kinetis_protect_check,
  1980. .info = kinetis_info,
  1981. };