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.
 
 
 
 
 
 

1315 lines
37 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2015 by Ivan Meleca *
  3. * ivan@artekit.eu *
  4. * *
  5. * Modified from kinetis.c *
  6. * *
  7. * Copyright (C) 2011 by Mathias Kuester *
  8. * kesmtp@freenet.de *
  9. * *
  10. * Copyright (C) 2011 sleep(5) ltd *
  11. * tomas@sleepfive.com *
  12. * *
  13. * Copyright (C) 2012 by Christopher D. Kilgour *
  14. * techie at whiterocker.com *
  15. * *
  16. * Copyright (C) 2013 Nemui Trinomius *
  17. * nemuisan_kawausogasuki@live.jp *
  18. * *
  19. * Copyright (C) 2015 Tomas Vanek *
  20. * vanekt@fbl.cz *
  21. * *
  22. * This program is free software; you can redistribute it and/or modify *
  23. * it under the terms of the GNU General Public License as published by *
  24. * the Free Software Foundation; either version 2 of the License, or *
  25. * (at your option) any later version. *
  26. * *
  27. * This program is distributed in the hope that it will be useful, *
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  30. * GNU General Public License for more details. *
  31. * *
  32. * You should have received a copy of the GNU General Public License *
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  34. ***************************************************************************/
  35. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38. #include "jtag/interface.h"
  39. #include "imp.h"
  40. #include <helper/binarybuffer.h>
  41. #include <target/algorithm.h>
  42. #include <target/armv7m.h>
  43. #include <target/cortex_m.h>
  44. /* Addresses */
  45. #define SIM_SRSID 0x40048000
  46. #define ICS_C1 0x40064000
  47. #define ICS_C2 0x40064001
  48. #define ICS_C3 0x40064002
  49. #define ICS_C4 0x40064003
  50. #define ICS_S 0x40064004
  51. #define SIM_BUSDIV 0x40048018
  52. #define SIM_CLKDIV_KE06 0x40048024
  53. #define SIM_CLKDIV_KE04_44_64_80 0x40048024
  54. #define SIM_CLKDIV_KE04_16_20_24 0x4004801C
  55. #define WDOG_CS1 0x40052000
  56. #define ICS_C2_BDIV_MASK 0xE0
  57. #define ICS_C2_BDIV_SHIFT 5
  58. #define ICS_C2_BDIV(x) (((uint8_t)(((uint8_t)(x))<<ICS_C2_BDIV_SHIFT))&ICS_C2_BDIV_MASK)
  59. #define ICS_S_LOCK_MASK 0x40
  60. #define ICS_C4_SCFTRIM_MASK 0x1
  61. #define SIM_CLKDIV_OUTDIV2_MASK 0x1000000
  62. #define FTMRX_FCLKDIV_FDIV_MASK 0x3F
  63. #define FTMRX_FCLKDIV_FDIV_SHIFT 0
  64. #define FTMRX_FCLKDIV_FDIV(x) (((uint8_t)(((uint8_t)(x))<<FTMRX_FCLKDIV_FDIV_SHIFT))&FTMRX_FCLKDIV_FDIV_MASK)
  65. #define FTMRX_FCLKDIV_FDIVLCK_MASK 0x40
  66. #define FTMRX_FCLKDIV_FDIVLCK_SHIFT 6
  67. #define FTMRX_FCLKDIV_FDIVLD_MASK 0x80
  68. #define FTMRX_FCLKDIV_FDIVLD_SHIFT 7
  69. #define FTMRX_FSTAT_CCIF_MASK 0x80
  70. #define FTMRX_FSTAT_MGSTAT0_MASK 0x01
  71. #define FTMRX_FSTAT_MGSTAT1_MASK 0x02
  72. /* Commands */
  73. #define FTMRX_CMD_ALLERASED 0x01
  74. #define FTMRX_CMD_BLOCKERASED 0x02
  75. #define FTMRX_CMD_SECTIONERASED 0x03
  76. #define FTMRX_CMD_READONCE 0x04
  77. #define FTMRX_CMD_PROGFLASH 0x06
  78. #define FTMRX_CMD_PROGONCE 0x07
  79. #define FTMRX_CMD_ERASEALL 0x08
  80. #define FTMRX_CMD_ERASEBLOCK 0x09
  81. #define FTMRX_CMD_ERASESECTOR 0x0A
  82. #define FTMRX_CMD_UNSECURE 0x0B
  83. #define FTMRX_CMD_VERIFYACCESS 0x0C
  84. #define FTMRX_CMD_SETMARGINLVL 0x0D
  85. #define FTMRX_CMD_SETFACTORYLVL 0x0E
  86. #define FTMRX_CMD_CONFIGNVM 0x0F
  87. /* Error codes */
  88. #define FTMRX_ERROR_ACCERR 0x20
  89. #define FTMRX_ERROR_FPVIOL 0x10
  90. #define KINETIS_KE_SRSID_FAMID(x) ((x >> 28) & 0x0F)
  91. #define KINETIS_KE_SRSID_SUBFAMID(x) ((x >> 24) & 0x0F)
  92. #define KINETIS_KE_SRSID_PINCOUNT(x) ((x >> 16) & 0x0F)
  93. #define KINETIS_KE_SRSID_KEX2 0x02
  94. #define KINETIS_KE_SRSID_KEX4 0x04
  95. #define KINETIS_KE_SRSID_KEX6 0x06
  96. struct kinetis_ke_flash_bank {
  97. uint32_t sector_size;
  98. uint32_t protection_size;
  99. uint32_t sim_srsid;
  100. uint32_t ftmrx_fclkdiv_addr;
  101. uint32_t ftmrx_fccobix_addr;
  102. uint32_t ftmrx_fstat_addr;
  103. uint32_t ftmrx_fprot_addr;
  104. uint32_t ftmrx_fccobhi_addr;
  105. uint32_t ftmrx_fccoblo_addr;
  106. };
  107. #define MDM_REG_STAT 0x00
  108. #define MDM_REG_CTRL 0x04
  109. #define MDM_REG_ID 0xfc
  110. #define MDM_STAT_FMEACK (1<<0)
  111. #define MDM_STAT_FREADY (1<<1)
  112. #define MDM_STAT_SYSSEC (1<<2)
  113. #define MDM_STAT_SYSRES (1<<3)
  114. #define MDM_STAT_FMEEN (1<<5)
  115. #define MDM_STAT_BACKDOOREN (1<<6)
  116. #define MDM_STAT_LPEN (1<<7)
  117. #define MDM_STAT_VLPEN (1<<8)
  118. #define MDM_STAT_LLSMODEXIT (1<<9)
  119. #define MDM_STAT_VLLSXMODEXIT (1<<10)
  120. #define MDM_STAT_CORE_HALTED (1<<16)
  121. #define MDM_STAT_CORE_SLEEPDEEP (1<<17)
  122. #define MDM_STAT_CORESLEEPING (1<<18)
  123. #define MEM_CTRL_FMEIP (1<<0)
  124. #define MEM_CTRL_DBG_DIS (1<<1)
  125. #define MEM_CTRL_DBG_REQ (1<<2)
  126. #define MEM_CTRL_SYS_RES_REQ (1<<3)
  127. #define MEM_CTRL_CORE_HOLD_RES (1<<4)
  128. #define MEM_CTRL_VLLSX_DBG_REQ (1<<5)
  129. #define MEM_CTRL_VLLSX_DBG_ACK (1<<6)
  130. #define MEM_CTRL_VLLSX_STAT_ACK (1<<7)
  131. #define MDM_ACCESS_TIMEOUT 3000 /* iterations */
  132. static int kinetis_ke_mdm_write_register(struct adiv5_dap *dap, unsigned reg, uint32_t value)
  133. {
  134. int retval;
  135. LOG_DEBUG("MDM_REG[0x%02x] <- %08" PRIX32, reg, value);
  136. retval = dap_queue_ap_write(dap_ap(dap, 1), reg, value);
  137. if (retval != ERROR_OK) {
  138. LOG_DEBUG("MDM: failed to queue a write request");
  139. return retval;
  140. }
  141. retval = dap_run(dap);
  142. if (retval != ERROR_OK) {
  143. LOG_DEBUG("MDM: dap_run failed");
  144. return retval;
  145. }
  146. return ERROR_OK;
  147. }
  148. static int kinetis_ke_mdm_read_register(struct adiv5_dap *dap, unsigned reg, uint32_t *result)
  149. {
  150. int retval;
  151. retval = dap_queue_ap_read(dap_ap(dap, 1), reg, result);
  152. if (retval != ERROR_OK) {
  153. LOG_DEBUG("MDM: failed to queue a read request");
  154. return retval;
  155. }
  156. retval = dap_run(dap);
  157. if (retval != ERROR_OK) {
  158. LOG_DEBUG("MDM: dap_run failed");
  159. return retval;
  160. }
  161. LOG_DEBUG("MDM_REG[0x%02x]: %08" PRIX32, reg, *result);
  162. return ERROR_OK;
  163. }
  164. static int kinetis_ke_mdm_poll_register(struct adiv5_dap *dap, unsigned reg, uint32_t mask, uint32_t value)
  165. {
  166. uint32_t val;
  167. int retval;
  168. int timeout = MDM_ACCESS_TIMEOUT;
  169. do {
  170. retval = kinetis_ke_mdm_read_register(dap, reg, &val);
  171. if (retval != ERROR_OK || (val & mask) == value)
  172. return retval;
  173. alive_sleep(1);
  174. } while (timeout--);
  175. LOG_DEBUG("MDM: polling timed out");
  176. return ERROR_FAIL;
  177. }
  178. static int kinetis_ke_prepare_flash(struct flash_bank *bank)
  179. {
  180. struct target *target = bank->target;
  181. struct kinetis_ke_flash_bank *kinfo = bank->driver_priv;
  182. uint8_t c2, c3, c4, s = 0;
  183. uint16_t trim_value = 0;
  184. uint16_t timeout = 0;
  185. uint32_t bus_clock = 0;
  186. uint32_t bus_reg_val = 0;
  187. uint32_t bus_reg_addr = 0;
  188. uint32_t flash_clk_div;
  189. uint8_t fclkdiv;
  190. int result;
  191. /*
  192. * The RM states that the flash clock has to be set to 1MHz for writing and
  193. * erasing operations (otherwise it can damage the flash).
  194. * This function configures the entire clock tree to make sure we
  195. * run at the specified clock. We'll set FEI mode running from the ~32KHz
  196. * internal clock. So we need to:
  197. * - Trim internal clock.
  198. * - Configure the divider for ICSOUTCLK (ICS module).
  199. * - Configure the divider to get a bus clock (SIM module).
  200. * - Configure the flash clock that depends on the bus clock.
  201. *
  202. * For MKE02_40 and MKE02_20 we set ICSOUTCLK = 20MHz and bus clock = 20MHz.
  203. * For MKE04 and MKE06 we run at ICSOUTCLK = 48MHz and bus clock = 24MHz.
  204. */
  205. /*
  206. * Trim internal clock
  207. */
  208. switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) {
  209. case KINETIS_KE_SRSID_KEX2:
  210. /* Both KE02_20 and KE02_40 should get the same trim value */
  211. trim_value = 0x4C;
  212. break;
  213. case KINETIS_KE_SRSID_KEX4:
  214. trim_value = 0x54;
  215. break;
  216. case KINETIS_KE_SRSID_KEX6:
  217. trim_value = 0x58;
  218. break;
  219. }
  220. result = target_read_u8(target, ICS_C4, &c4);
  221. if (result != ERROR_OK)
  222. return result;
  223. c3 = trim_value;
  224. c4 = (c4 & ~(ICS_C4_SCFTRIM_MASK)) | ((trim_value >> 8) & 0x01);
  225. result = target_write_u8(target, ICS_C3, c3);
  226. if (result != ERROR_OK)
  227. return result;
  228. result = target_write_u8(target, ICS_C4, c4);
  229. if (result != ERROR_OK)
  230. return result;
  231. result = target_read_u8(target, ICS_S, &s);
  232. if (result != ERROR_OK)
  233. return result;
  234. /* Wait */
  235. while (!(s & ICS_S_LOCK_MASK)) {
  236. if (timeout <= 1000) {
  237. timeout++;
  238. alive_sleep(1);
  239. } else {
  240. return ERROR_FAIL;
  241. }
  242. result = target_read_u8(target, ICS_S, &s);
  243. if (result != ERROR_OK)
  244. return result;
  245. }
  246. /* ... trim done ... */
  247. /*
  248. * Configure SIM (bus clock)
  249. */
  250. switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) {
  251. /* KE02 sub-family operates on SIM_BUSDIV */
  252. case KINETIS_KE_SRSID_KEX2:
  253. bus_reg_val = 0;
  254. bus_reg_addr = SIM_BUSDIV;
  255. bus_clock = 20000000;
  256. break;
  257. /* KE04 and KE06 sub-family operates on SIM_CLKDIV
  258. * Clocks are divided by:
  259. * DIV1 = core clock = 48MHz
  260. * DIV2 = bus clock = 24Mhz
  261. * DIV3 = timer clocks
  262. * So we need to configure SIM_CLKDIV, DIV1 and DIV2 value
  263. */
  264. case KINETIS_KE_SRSID_KEX4:
  265. /* KE04 devices have the SIM_CLKDIV register at a different offset
  266. * depending on the pin count. */
  267. switch (KINETIS_KE_SRSID_PINCOUNT(kinfo->sim_srsid)) {
  268. /* 16, 20 and 24 pins */
  269. case 1:
  270. case 2:
  271. case 3:
  272. bus_reg_addr = SIM_CLKDIV_KE04_16_20_24;
  273. break;
  274. /* 44, 64 and 80 pins */
  275. case 5:
  276. case 7:
  277. case 8:
  278. bus_reg_addr = SIM_CLKDIV_KE04_44_64_80;
  279. break;
  280. default:
  281. LOG_ERROR("KE04 - Unknown pin count");
  282. return ERROR_FAIL;
  283. }
  284. bus_reg_val = SIM_CLKDIV_OUTDIV2_MASK;
  285. bus_clock = 24000000;
  286. break;
  287. case KINETIS_KE_SRSID_KEX6:
  288. bus_reg_val = SIM_CLKDIV_OUTDIV2_MASK;
  289. bus_reg_addr = SIM_CLKDIV_KE06;
  290. bus_clock = 24000000;
  291. break;
  292. }
  293. result = target_write_u32(target, bus_reg_addr, bus_reg_val);
  294. if (result != ERROR_OK)
  295. return result;
  296. /*
  297. * Configure ICS to FEI (internal source)
  298. */
  299. result = target_read_u8(target, ICS_C2, &c2);
  300. if (result != ERROR_OK)
  301. return result;
  302. c2 &= ~ICS_C2_BDIV_MASK;
  303. switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) {
  304. case KINETIS_KE_SRSID_KEX2:
  305. /* Note: since there are two KE02 types, the KE02_40 @ 40MHz and the
  306. * KE02_20 @ 20MHz, we divide here the ~40MHz ICSFLLCLK down to 20MHz,
  307. * for compatibility.
  308. */
  309. c2 |= ICS_C2_BDIV(1);
  310. break;
  311. case KINETIS_KE_SRSID_KEX4:
  312. case KINETIS_KE_SRSID_KEX6:
  313. /* For KE04 and KE06, the ICSFLLCLK can be 48MHz. */
  314. c2 |= ICS_C2_BDIV(0);
  315. break;
  316. }
  317. result = target_write_u8(target, ICS_C2, c2);
  318. if (result != ERROR_OK)
  319. return result;
  320. /* Internal clock as reference (IREFS = 1) */
  321. result = target_write_u8(target, ICS_C1, 4);
  322. if (result != ERROR_OK)
  323. return result;
  324. /* Wait for FLL to lock */
  325. result = target_read_u8(target, ICS_S, &s);
  326. if (result != ERROR_OK)
  327. return result;
  328. while (!(s & ICS_S_LOCK_MASK)) {
  329. if (timeout <= 1000) {
  330. timeout++;
  331. alive_sleep(1);
  332. } else {
  333. return ERROR_FLASH_OPERATION_FAILED;
  334. }
  335. result = target_read_u8(target, ICS_S, &s);
  336. if (result != ERROR_OK)
  337. return result;
  338. }
  339. /*
  340. * Configure flash clock to 1MHz.
  341. */
  342. flash_clk_div = bus_clock / 1000000L - 1;
  343. /* Check if the FCLKDIV register is locked */
  344. result = target_read_u8(target, kinfo->ftmrx_fclkdiv_addr, &fclkdiv);
  345. if (result != ERROR_OK)
  346. return result;
  347. if (!(fclkdiv & FTMRX_FCLKDIV_FDIVLCK_MASK)) {
  348. /* Unlocked. Check if the register was configured, and if so, if it has the right value */
  349. if ((fclkdiv & FTMRX_FCLKDIV_FDIVLD_MASK) &&
  350. ((fclkdiv & FTMRX_FCLKDIV_FDIV_MASK) != FTMRX_FCLKDIV_FDIV(flash_clk_div))) {
  351. LOG_WARNING("Flash clock was already set and contains an invalid value.");
  352. LOG_WARNING("Please reset the target.");
  353. return ERROR_FAIL;
  354. }
  355. /* Finally, configure the flash clock */
  356. fclkdiv = (fclkdiv & ~(FTMRX_FCLKDIV_FDIV_MASK)) | FTMRX_FCLKDIV_FDIV(flash_clk_div);
  357. result = target_write_u8(target, kinfo->ftmrx_fclkdiv_addr, fclkdiv);
  358. if (result != ERROR_OK)
  359. return result;
  360. } else {
  361. /* Locked. Check if the current value is correct. */
  362. if ((fclkdiv & FTMRX_FCLKDIV_FDIV_MASK) != FTMRX_FCLKDIV_FDIV(flash_clk_div)) {
  363. LOG_WARNING("Flash clock register is locked and contains an invalid value.");
  364. LOG_WARNING("Please reset the target.");
  365. return ERROR_FAIL;
  366. }
  367. }
  368. LOG_INFO("Flash clock ready");
  369. return ERROR_OK;
  370. }
  371. int kinetis_ke_stop_watchdog(struct target *target)
  372. {
  373. struct working_area *watchdog_algorithm;
  374. struct armv7m_algorithm armv7m_info;
  375. int retval;
  376. uint8_t cs1;
  377. static const uint8_t watchdog_code[] = {
  378. #include "../../../contrib/loaders/flash/kinetis_ke/kinetis_ke_watchdog.inc"
  379. };
  380. if (target->state != TARGET_HALTED) {
  381. LOG_ERROR("Target not halted");
  382. return ERROR_TARGET_NOT_HALTED;
  383. }
  384. /* Check if the watchdog is enabled */
  385. retval = target_read_u8(target, WDOG_CS1, &cs1);
  386. if (retval != ERROR_OK)
  387. return retval;
  388. if (!(cs1 & 0x80)) {
  389. /* Already stopped */
  390. return ERROR_OK;
  391. }
  392. /* allocate working area with watchdog code */
  393. if (target_alloc_working_area(target, sizeof(watchdog_code), &watchdog_algorithm) != ERROR_OK) {
  394. LOG_WARNING("No working area available for watchdog algorithm");
  395. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  396. }
  397. retval = target_write_buffer(target, watchdog_algorithm->address,
  398. sizeof(watchdog_code), watchdog_code);
  399. if (retval != ERROR_OK)
  400. return retval;
  401. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  402. armv7m_info.core_mode = ARM_MODE_THREAD;
  403. retval = target_run_algorithm(target, 0, NULL, 0, NULL,
  404. watchdog_algorithm->address, 0, 100000, &armv7m_info);
  405. if (retval != ERROR_OK) {
  406. LOG_ERROR("Error executing Kinetis KE watchdog algorithm");
  407. retval = ERROR_FAIL;
  408. } else {
  409. LOG_INFO("Watchdog stopped");
  410. }
  411. target_free_working_area(target, watchdog_algorithm);
  412. return ERROR_OK;
  413. }
  414. COMMAND_HANDLER(kinetis_ke_disable_wdog_handler)
  415. {
  416. struct target *target = get_current_target(CMD_CTX);
  417. if (CMD_ARGC > 0)
  418. return ERROR_COMMAND_SYNTAX_ERROR;
  419. return kinetis_ke_stop_watchdog(target);
  420. }
  421. COMMAND_HANDLER(kinetis_ke_mdm_mass_erase)
  422. {
  423. struct target *target = get_current_target(CMD_CTX);
  424. struct cortex_m_common *cortex_m = target_to_cm(target);
  425. struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
  426. if (!dap) {
  427. LOG_ERROR("Cannot perform mass erase with a high-level adapter");
  428. return ERROR_FAIL;
  429. }
  430. int retval;
  431. /* According to chapter 18.3.7.2 of the KE02 reference manual */
  432. /* assert SRST */
  433. if (jtag_get_reset_config() & RESET_HAS_SRST)
  434. adapter_assert_reset();
  435. /*
  436. * 1. Reset the device by asserting RESET pin or DAP_CTRL[3]
  437. */
  438. retval = kinetis_ke_mdm_write_register(dap, MDM_REG_CTRL, MEM_CTRL_SYS_RES_REQ);
  439. if (retval != ERROR_OK)
  440. return retval;
  441. /*
  442. * ... Read the MDM-AP status register until the Flash Ready bit sets...
  443. */
  444. retval = kinetis_ke_mdm_poll_register(dap, MDM_REG_STAT,
  445. MDM_STAT_FREADY | MDM_STAT_SYSRES,
  446. MDM_STAT_FREADY);
  447. if (retval != ERROR_OK) {
  448. LOG_ERROR("MDM : flash ready timeout");
  449. return retval;
  450. }
  451. /*
  452. * 2. Set DAP_CTRL[0] bit to invoke debug mass erase via SWD
  453. * 3. Release reset by deasserting RESET pin or DAP_CTRL[3] bit via SWD.
  454. */
  455. retval = kinetis_ke_mdm_write_register(dap, MDM_REG_CTRL, MEM_CTRL_FMEIP);
  456. if (retval != ERROR_OK)
  457. return retval;
  458. /* As a sanity check make sure that device started mass erase procedure */
  459. retval = kinetis_ke_mdm_poll_register(dap, MDM_REG_STAT,
  460. MDM_STAT_FMEACK, MDM_STAT_FMEACK);
  461. if (retval != ERROR_OK)
  462. return retval;
  463. /*
  464. * 4. Wait till DAP_CTRL[0] bit is cleared (after mass erase completes,
  465. * DAP_CTRL[0] bit is cleared automatically).
  466. */
  467. retval = kinetis_ke_mdm_poll_register(dap, MDM_REG_CTRL,
  468. MEM_CTRL_FMEIP,
  469. 0);
  470. if (retval != ERROR_OK)
  471. return retval;
  472. if (jtag_get_reset_config() & RESET_HAS_SRST)
  473. adapter_deassert_reset();
  474. return ERROR_OK;
  475. }
  476. static const uint32_t kinetis_ke_known_mdm_ids[] = {
  477. 0x001C0020, /* Kinetis-L/M/V/E/KE Series */
  478. };
  479. /*
  480. * This function implements the procedure to connect to
  481. * SWD/JTAG on Kinetis K and L series of devices as it is described in
  482. * AN4835 "Production Flash Programming Best Practices for Kinetis K-
  483. * and L-series MCUs" Section 4.1.1
  484. */
  485. COMMAND_HANDLER(kinetis_ke_check_flash_security_status)
  486. {
  487. struct target *target = get_current_target(CMD_CTX);
  488. struct cortex_m_common *cortex_m = target_to_cm(target);
  489. struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
  490. if (!dap) {
  491. LOG_WARNING("Cannot check flash security status with a high-level adapter");
  492. return ERROR_OK;
  493. }
  494. uint32_t val;
  495. int retval;
  496. /*
  497. * ... The MDM-AP ID register can be read to verify that the
  498. * connection is working correctly...
  499. */
  500. retval = kinetis_ke_mdm_read_register(dap, MDM_REG_ID, &val);
  501. if (retval != ERROR_OK) {
  502. LOG_ERROR("MDM: failed to read ID register");
  503. goto fail;
  504. }
  505. bool found = false;
  506. for (size_t i = 0; i < ARRAY_SIZE(kinetis_ke_known_mdm_ids); i++) {
  507. if (val == kinetis_ke_known_mdm_ids[i]) {
  508. found = true;
  509. break;
  510. }
  511. }
  512. if (!found)
  513. LOG_WARNING("MDM: unknown ID %08" PRIX32, val);
  514. /*
  515. * ... Read the MDM-AP status register until the Flash Ready bit sets...
  516. */
  517. retval = kinetis_ke_mdm_poll_register(dap, MDM_REG_STAT,
  518. MDM_STAT_FREADY,
  519. MDM_STAT_FREADY);
  520. if (retval != ERROR_OK) {
  521. LOG_ERROR("MDM: flash ready timeout");
  522. goto fail;
  523. }
  524. /*
  525. * ... Read the System Security bit to determine if security is enabled.
  526. * If System Security = 0, then proceed. If System Security = 1, then
  527. * communication with the internals of the processor, including the
  528. * flash, will not be possible without issuing a mass erase command or
  529. * unsecuring the part through other means (backdoor key unlock)...
  530. */
  531. retval = kinetis_ke_mdm_read_register(dap, MDM_REG_STAT, &val);
  532. if (retval != ERROR_OK) {
  533. LOG_ERROR("MDM: failed to read MDM_REG_STAT");
  534. goto fail;
  535. }
  536. if (val & MDM_STAT_SYSSEC) {
  537. jtag_poll_set_enabled(false);
  538. LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
  539. LOG_WARNING("**** ****");
  540. LOG_WARNING("**** Your Kinetis MCU is in secured state, which means that, ****");
  541. LOG_WARNING("**** with exception for very basic communication, JTAG/SWD ****");
  542. LOG_WARNING("**** interface will NOT work. In order to restore its ****");
  543. LOG_WARNING("**** functionality please issue 'kinetis_ke mdm mass_erase' ****");
  544. LOG_WARNING("**** command, power cycle the MCU and restart OpenOCD. ****");
  545. LOG_WARNING("**** ****");
  546. LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
  547. } else {
  548. LOG_INFO("MDM: Chip is unsecured. Continuing.");
  549. jtag_poll_set_enabled(true);
  550. }
  551. return ERROR_OK;
  552. fail:
  553. LOG_ERROR("MDM: Failed to check security status of the MCU. Cannot proceed further");
  554. jtag_poll_set_enabled(false);
  555. return retval;
  556. }
  557. FLASH_BANK_COMMAND_HANDLER(kinetis_ke_flash_bank_command)
  558. {
  559. struct kinetis_ke_flash_bank *bank_info;
  560. if (CMD_ARGC < 6)
  561. return ERROR_COMMAND_SYNTAX_ERROR;
  562. LOG_INFO("add flash_bank kinetis_ke %s", bank->name);
  563. bank_info = malloc(sizeof(struct kinetis_ke_flash_bank));
  564. memset(bank_info, 0, sizeof(struct kinetis_ke_flash_bank));
  565. bank->driver_priv = bank_info;
  566. return ERROR_OK;
  567. }
  568. /* Kinetis Program-LongWord Microcodes */
  569. static uint8_t kinetis_ke_flash_write_code[] = {
  570. #include "../../../contrib/loaders/flash/kinetis_ke/kinetis_ke_flash.inc"
  571. };
  572. static int kinetis_ke_write_words(struct flash_bank *bank, const uint8_t *buffer,
  573. uint32_t offset, uint32_t words)
  574. {
  575. struct kinetis_ke_flash_bank *kinfo = bank->driver_priv;
  576. struct target *target = bank->target;
  577. uint32_t ram_buffer_size = 512 + 16;
  578. struct working_area *write_algorithm;
  579. struct working_area *source;
  580. uint32_t address = bank->base + offset;
  581. struct reg_param reg_params[4];
  582. struct armv7m_algorithm armv7m_info;
  583. int retval = ERROR_OK;
  584. uint32_t flash_code_size;
  585. LOG_INFO("Kinetis KE: FLASH Write ...");
  586. /* allocate working area with flash programming code */
  587. if (target_alloc_working_area(target, sizeof(kinetis_ke_flash_write_code),
  588. &write_algorithm) != ERROR_OK) {
  589. LOG_WARNING("no working area available, can't do block memory writes");
  590. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  591. }
  592. /* Patch the FTMRx registers addresses */
  593. flash_code_size = sizeof(kinetis_ke_flash_write_code);
  594. buf_set_u32(&kinetis_ke_flash_write_code[flash_code_size-16], 0, 32, kinfo->ftmrx_fstat_addr);
  595. buf_set_u32(&kinetis_ke_flash_write_code[flash_code_size-12], 0, 32, kinfo->ftmrx_fccobix_addr);
  596. buf_set_u32(&kinetis_ke_flash_write_code[flash_code_size-8], 0, 32, kinfo->ftmrx_fccobhi_addr);
  597. buf_set_u32(&kinetis_ke_flash_write_code[flash_code_size-4], 0, 32, kinfo->ftmrx_fccoblo_addr);
  598. retval = target_write_buffer(target, write_algorithm->address,
  599. sizeof(kinetis_ke_flash_write_code), kinetis_ke_flash_write_code);
  600. if (retval != ERROR_OK)
  601. return retval;
  602. /* memory buffer */
  603. if (target_alloc_working_area(target, ram_buffer_size, &source) != ERROR_OK) {
  604. /* free working area, write algorithm already allocated */
  605. target_free_working_area(target, write_algorithm);
  606. LOG_WARNING("No large enough working area available, can't do block memory writes");
  607. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  608. }
  609. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  610. armv7m_info.core_mode = ARM_MODE_THREAD;
  611. init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT);
  612. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  613. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
  614. init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
  615. buf_set_u32(reg_params[0].value, 0, 32, address);
  616. buf_set_u32(reg_params[1].value, 0, 32, words);
  617. buf_set_u32(reg_params[2].value, 0, 32, source->address);
  618. buf_set_u32(reg_params[3].value, 0, 32, source->address + source->size);
  619. retval = target_run_flash_async_algorithm(target, buffer, words, 4,
  620. 0, NULL,
  621. 4, reg_params,
  622. source->address, source->size,
  623. write_algorithm->address, 0,
  624. &armv7m_info);
  625. if (retval == ERROR_FLASH_OPERATION_FAILED) {
  626. if (buf_get_u32(reg_params[0].value, 0, 32) & FTMRX_ERROR_ACCERR)
  627. LOG_ERROR("flash access error");
  628. if (buf_get_u32(reg_params[0].value, 0, 32) & FTMRX_ERROR_FPVIOL)
  629. LOG_ERROR("flash protection violation");
  630. }
  631. target_free_working_area(target, source);
  632. target_free_working_area(target, write_algorithm);
  633. destroy_reg_param(&reg_params[0]);
  634. destroy_reg_param(&reg_params[1]);
  635. destroy_reg_param(&reg_params[2]);
  636. destroy_reg_param(&reg_params[3]);
  637. return retval;
  638. }
  639. static int kinetis_ke_protect(struct flash_bank *bank, int set, int first, int last)
  640. {
  641. LOG_WARNING("kinetis_ke_protect not supported yet");
  642. /* FIXME: TODO */
  643. if (bank->target->state != TARGET_HALTED) {
  644. LOG_ERROR("Target not halted");
  645. return ERROR_TARGET_NOT_HALTED;
  646. }
  647. return ERROR_FLASH_BANK_INVALID;
  648. }
  649. static int kinetis_ke_protect_check(struct flash_bank *bank)
  650. {
  651. struct kinetis_ke_flash_bank *kinfo = bank->driver_priv;
  652. if (bank->target->state != TARGET_HALTED) {
  653. LOG_ERROR("Target not halted");
  654. return ERROR_TARGET_NOT_HALTED;
  655. }
  656. int result;
  657. uint8_t fprot;
  658. uint8_t fpopen, fpldis, fphdis;
  659. uint8_t fphs, fpls;
  660. uint32_t lprot_size = 0, hprot_size = 0;
  661. uint32_t lprot_to = 0, hprot_from = 0;
  662. /* read protection register */
  663. result = target_read_u8(bank->target, kinfo->ftmrx_fprot_addr, &fprot);
  664. if (result != ERROR_OK)
  665. return result;
  666. fpopen = fprot & 0x80;
  667. fpldis = fprot & 0x04;
  668. fphdis = fprot & 0x20;
  669. fphs = (fprot >> 3) & 0x03;
  670. fpls = fprot & 0x03;
  671. /* Fully unprotected? */
  672. if (fpopen && fpldis && fphdis) {
  673. LOG_WARNING("No flash protection found.");
  674. for (uint32_t i = 0; i < (uint32_t) bank->num_sectors; i++)
  675. bank->sectors[i].is_protected = 0;
  676. kinfo->protection_size = 0;
  677. } else {
  678. LOG_WARNING("Flash protected. FPOPEN=%i FPLDIS=%i FPHDIS=%i FPLS=%i FPHS=%i", \
  679. fpopen ? 1 : 0, fpldis ? 1 : 0, fphdis ? 1 : 0, fpls, fphs);
  680. /* Retrieve which region is protected and how much */
  681. if (fpopen) {
  682. if (fpldis == 0)
  683. lprot_size = (kinfo->sector_size * 4) << fpls;
  684. if (fphdis == 0)
  685. hprot_size = (kinfo->sector_size * 2) << fphs;
  686. } else {
  687. if (fpldis == 1)
  688. lprot_size = (kinfo->sector_size * 4) << fpls;
  689. if (fphdis == 1)
  690. hprot_size = (kinfo->sector_size * 2) << fphs;
  691. }
  692. kinfo->protection_size = lprot_size + hprot_size;
  693. /* lprot_to indicates up to where the lower region is protected */
  694. lprot_to = lprot_size / kinfo->sector_size;
  695. /* hprot_from indicates from where the upper region is protected */
  696. hprot_from = (0x8000 - hprot_size) / kinfo->sector_size;
  697. for (uint32_t i = 0; i < (uint32_t) bank->num_sectors; i++) {
  698. /* Check if the sector is in the lower region */
  699. if (bank->sectors[i].offset < 0x4000) {
  700. /* Compare the sector start address against lprot_to */
  701. if (lprot_to && (i < lprot_to))
  702. bank->sectors[i].is_protected = 1;
  703. else
  704. bank->sectors[i].is_protected = 0;
  705. /* Check if the sector is between the lower and upper region
  706. * OR after the upper region */
  707. } else if (bank->sectors[i].offset < 0x6000 || bank->sectors[i].offset >= 0x8000) {
  708. /* If fpopen is 1 then these regions are protected */
  709. if (fpopen)
  710. bank->sectors[i].is_protected = 0;
  711. else
  712. bank->sectors[i].is_protected = 1;
  713. /* Check if the sector is in the upper region */
  714. } else if (bank->sectors[i].offset < 0x8000) {
  715. if (hprot_from && (i > hprot_from))
  716. bank->sectors[i].is_protected = 1;
  717. else
  718. bank->sectors[i].is_protected = 0;
  719. }
  720. }
  721. }
  722. return ERROR_OK;
  723. }
  724. static int kinetis_ke_ftmrx_command(struct flash_bank *bank, uint8_t count,
  725. uint8_t *FCCOBIX, uint8_t *FCCOBHI, uint8_t *FCCOBLO, uint8_t *fstat)
  726. {
  727. uint8_t i;
  728. int result;
  729. struct target *target = bank->target;
  730. struct kinetis_ke_flash_bank *kinfo = bank->driver_priv;
  731. uint32_t timeout = 0;
  732. /* Clear error flags */
  733. result = target_write_u8(target, kinfo->ftmrx_fstat_addr, 0x30);
  734. if (result != ERROR_OK)
  735. return result;
  736. for (i = 0; i < count; i++) {
  737. /* Write index */
  738. result = target_write_u8(target, kinfo->ftmrx_fccobix_addr, FCCOBIX[i]);
  739. if (result != ERROR_OK)
  740. return result;
  741. /* Write high part */
  742. result = target_write_u8(target, kinfo->ftmrx_fccobhi_addr, FCCOBHI[i]);
  743. if (result != ERROR_OK)
  744. return result;
  745. /* Write low part (that is not always required) */
  746. if (FCCOBLO) {
  747. result = target_write_u8(target, kinfo->ftmrx_fccoblo_addr, FCCOBLO[i]);
  748. if (result != ERROR_OK)
  749. return result;
  750. }
  751. }
  752. /* Launch the command */
  753. result = target_write_u8(target, kinfo->ftmrx_fstat_addr, 0x80);
  754. if (result != ERROR_OK)
  755. return result;
  756. /* Wait for it to finish */
  757. result = target_read_u8(target, kinfo->ftmrx_fstat_addr, fstat);
  758. if (result != ERROR_OK)
  759. return result;
  760. while (!(*fstat & FTMRX_FSTAT_CCIF_MASK)) {
  761. if (timeout <= 1000) {
  762. timeout++;
  763. alive_sleep(1);
  764. } else {
  765. return ERROR_FLASH_OPERATION_FAILED;
  766. }
  767. result = target_read_u8(target, kinfo->ftmrx_fstat_addr, fstat);
  768. if (result != ERROR_OK)
  769. return result;
  770. }
  771. return ERROR_OK;
  772. }
  773. COMMAND_HANDLER(kinetis_ke_securing_test)
  774. {
  775. int result;
  776. struct target *target = get_current_target(CMD_CTX);
  777. struct flash_bank *bank = NULL;
  778. uint32_t address;
  779. uint8_t FCCOBIX[2], FCCOBHI[2], FCCOBLO[2], fstat;
  780. result = get_flash_bank_by_addr(target, 0x00000000, true, &bank);
  781. if (result != ERROR_OK)
  782. return result;
  783. assert(bank != NULL);
  784. if (target->state != TARGET_HALTED) {
  785. LOG_ERROR("Target not halted");
  786. return ERROR_TARGET_NOT_HALTED;
  787. }
  788. address = bank->base + 0x00000400;
  789. FCCOBIX[0] = 0;
  790. FCCOBHI[0] = FTMRX_CMD_ERASESECTOR;
  791. FCCOBLO[0] = address >> 16;
  792. FCCOBIX[1] = 1;
  793. FCCOBHI[1] = address >> 8;
  794. FCCOBLO[1] = address;
  795. return kinetis_ke_ftmrx_command(bank, 2, FCCOBIX, FCCOBHI, FCCOBLO, &fstat);
  796. }
  797. static int kinetis_ke_erase(struct flash_bank *bank, int first, int last)
  798. {
  799. int result, i;
  800. uint8_t FCCOBIX[2], FCCOBHI[2], FCCOBLO[2], fstat;
  801. bool fcf_erased = false;
  802. if (bank->target->state != TARGET_HALTED) {
  803. LOG_ERROR("Target not halted");
  804. return ERROR_TARGET_NOT_HALTED;
  805. }
  806. if ((first > bank->num_sectors) || (last > bank->num_sectors))
  807. return ERROR_FLASH_OPERATION_FAILED;
  808. result = kinetis_ke_prepare_flash(bank);
  809. if (result != ERROR_OK)
  810. return result;
  811. for (i = first; i <= last; i++) {
  812. FCCOBIX[0] = 0;
  813. FCCOBHI[0] = FTMRX_CMD_ERASESECTOR;
  814. FCCOBLO[0] = (bank->base + bank->sectors[i].offset) >> 16;
  815. FCCOBIX[1] = 1;
  816. FCCOBHI[1] = (bank->base + bank->sectors[i].offset) >> 8;
  817. FCCOBLO[1] = (bank->base + bank->sectors[i].offset);
  818. result = kinetis_ke_ftmrx_command(bank, 2, FCCOBIX, FCCOBHI, FCCOBLO, &fstat);
  819. if (result != ERROR_OK) {
  820. LOG_WARNING("erase sector %d failed", i);
  821. return ERROR_FLASH_OPERATION_FAILED;
  822. }
  823. bank->sectors[i].is_erased = 1;
  824. if (i == 2)
  825. fcf_erased = true;
  826. }
  827. if (fcf_erased) {
  828. LOG_WARNING
  829. ("flash configuration field erased, please reset the device");
  830. }
  831. return ERROR_OK;
  832. }
  833. static int kinetis_ke_write(struct flash_bank *bank, const uint8_t *buffer,
  834. uint32_t offset, uint32_t count)
  835. {
  836. int result;
  837. uint8_t *new_buffer = NULL;
  838. uint32_t words = count / 4;
  839. if (bank->target->state != TARGET_HALTED) {
  840. LOG_ERROR("Target not halted");
  841. return ERROR_TARGET_NOT_HALTED;
  842. }
  843. if (offset > bank->size)
  844. return ERROR_FLASH_BANK_INVALID;
  845. if (offset & 0x3) {
  846. LOG_WARNING("offset 0x%" PRIx32 " breaks the required alignment", offset);
  847. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  848. }
  849. result = kinetis_ke_stop_watchdog(bank->target);
  850. if (result != ERROR_OK)
  851. return result;
  852. result = kinetis_ke_prepare_flash(bank);
  853. if (result != ERROR_OK)
  854. return result;
  855. if (count & 0x3) {
  856. uint32_t old_count = count;
  857. count = (old_count | 3) + 1;
  858. new_buffer = malloc(count);
  859. if (new_buffer == NULL) {
  860. LOG_ERROR("odd number of bytes to write and no memory "
  861. "for padding buffer");
  862. return ERROR_FAIL;
  863. }
  864. LOG_INFO("odd number of bytes to write (%" PRIu32 "), extending to %" PRIu32 " "
  865. "and padding with 0xff", old_count, count);
  866. memset(new_buffer, 0xff, count);
  867. buffer = memcpy(new_buffer, buffer, old_count);
  868. words++;
  869. }
  870. result = kinetis_ke_write_words(bank, buffer, offset, words);
  871. free(new_buffer);
  872. return result;
  873. }
  874. static int kinetis_ke_probe(struct flash_bank *bank)
  875. {
  876. int result, i;
  877. uint32_t offset = 0;
  878. struct target *target = bank->target;
  879. struct kinetis_ke_flash_bank *kinfo = bank->driver_priv;
  880. result = target_read_u32(target, SIM_SRSID, &kinfo->sim_srsid);
  881. if (result != ERROR_OK)
  882. return result;
  883. if (KINETIS_KE_SRSID_FAMID(kinfo->sim_srsid) != 0x00) {
  884. LOG_ERROR("Unsupported KE family");
  885. return ERROR_FLASH_OPER_UNSUPPORTED;
  886. }
  887. switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) {
  888. case KINETIS_KE_SRSID_KEX2:
  889. LOG_INFO("KE02 sub-family");
  890. break;
  891. case KINETIS_KE_SRSID_KEX4:
  892. LOG_INFO("KE04 sub-family");
  893. break;
  894. case KINETIS_KE_SRSID_KEX6:
  895. LOG_INFO("KE06 sub-family");
  896. break;
  897. default:
  898. LOG_ERROR("Unsupported KE sub-family");
  899. return ERROR_FLASH_OPER_UNSUPPORTED;
  900. }
  901. /* We can only retrieve the ke0x part, but there is no way to know
  902. * the flash size, so assume the maximum flash size for the entire
  903. * sub family.
  904. */
  905. bank->base = 0x00000000;
  906. kinfo->sector_size = 512;
  907. switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) {
  908. case KINETIS_KE_SRSID_KEX2:
  909. /* Max. 64KB */
  910. bank->size = 0x00010000;
  911. bank->num_sectors = 128;
  912. /* KE02 uses the FTMRH flash controller,
  913. * and registers have a different offset from the
  914. * FTMRE flash controller. Sort this out here.
  915. */
  916. kinfo->ftmrx_fclkdiv_addr = 0x40020000;
  917. kinfo->ftmrx_fccobix_addr = 0x40020002;
  918. kinfo->ftmrx_fstat_addr = 0x40020006;
  919. kinfo->ftmrx_fprot_addr = 0x40020008;
  920. kinfo->ftmrx_fccobhi_addr = 0x4002000A;
  921. kinfo->ftmrx_fccoblo_addr = 0x4002000B;
  922. break;
  923. case KINETIS_KE_SRSID_KEX6:
  924. case KINETIS_KE_SRSID_KEX4:
  925. /* Max. 128KB */
  926. bank->size = 0x00020000;
  927. bank->num_sectors = 256;
  928. /* KE04 and KE06 use the FTMRE flash controller,
  929. * and registers have a different offset from the
  930. * FTMRH flash controller. Sort this out here.
  931. */
  932. kinfo->ftmrx_fclkdiv_addr = 0x40020003;
  933. kinfo->ftmrx_fccobix_addr = 0x40020001;
  934. kinfo->ftmrx_fstat_addr = 0x40020005;
  935. kinfo->ftmrx_fprot_addr = 0x4002000B;
  936. kinfo->ftmrx_fccobhi_addr = 0x40020009;
  937. kinfo->ftmrx_fccoblo_addr = 0x40020008;
  938. break;
  939. }
  940. if (bank->sectors) {
  941. free(bank->sectors);
  942. bank->sectors = NULL;
  943. }
  944. assert(bank->num_sectors > 0);
  945. bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
  946. for (i = 0; i < bank->num_sectors; i++) {
  947. bank->sectors[i].offset = offset;
  948. bank->sectors[i].size = kinfo->sector_size;
  949. offset += kinfo->sector_size;
  950. bank->sectors[i].is_erased = -1;
  951. bank->sectors[i].is_protected = 1;
  952. }
  953. return ERROR_OK;
  954. }
  955. static int kinetis_ke_auto_probe(struct flash_bank *bank)
  956. {
  957. struct kinetis_ke_flash_bank *kinfo = bank->driver_priv;
  958. if (kinfo->sim_srsid)
  959. return ERROR_OK;
  960. return kinetis_ke_probe(bank);
  961. }
  962. static int kinetis_ke_info(struct flash_bank *bank, char *buf, int buf_size)
  963. {
  964. (void) snprintf(buf, buf_size,
  965. "%s driver for flash bank %s at 0x%8.8" PRIx32 "",
  966. bank->driver->name, bank->name, bank->base);
  967. return ERROR_OK;
  968. }
  969. static int kinetis_ke_blank_check(struct flash_bank *bank)
  970. {
  971. uint8_t FCCOBIX[3], FCCOBHI[3], FCCOBLO[3], fstat;
  972. uint16_t longwords = 0;
  973. int result;
  974. if (bank->target->state != TARGET_HALTED) {
  975. LOG_ERROR("Target not halted");
  976. return ERROR_TARGET_NOT_HALTED;
  977. }
  978. result = kinetis_ke_prepare_flash(bank);
  979. if (result != ERROR_OK)
  980. return result;
  981. /* check if whole bank is blank */
  982. FCCOBIX[0] = 0;
  983. FCCOBHI[0] = FTMRX_CMD_ALLERASED;
  984. result = kinetis_ke_ftmrx_command(bank, 1, FCCOBIX, FCCOBHI, NULL, &fstat);
  985. if (result != ERROR_OK)
  986. return result;
  987. if (fstat & (FTMRX_FSTAT_MGSTAT0_MASK | FTMRX_FSTAT_MGSTAT1_MASK)) {
  988. /* the whole bank is not erased, check sector-by-sector */
  989. int i;
  990. for (i = 0; i < bank->num_sectors; i++) {
  991. FCCOBIX[0] = 0;
  992. FCCOBHI[0] = FTMRX_CMD_SECTIONERASED;
  993. FCCOBLO[0] = (bank->base + bank->sectors[i].offset) >> 16;
  994. FCCOBIX[1] = 1;
  995. FCCOBHI[1] = (bank->base + bank->sectors[i].offset) >> 8;
  996. FCCOBLO[1] = (bank->base + bank->sectors[i].offset);
  997. longwords = 128;
  998. FCCOBIX[2] = 2;
  999. FCCOBHI[2] = longwords >> 8;
  1000. FCCOBLO[2] = longwords;
  1001. result = kinetis_ke_ftmrx_command(bank, 3, FCCOBIX, FCCOBHI, FCCOBLO, &fstat);
  1002. if (result == ERROR_OK) {
  1003. bank->sectors[i].is_erased = !(fstat & (FTMRX_FSTAT_MGSTAT0_MASK | FTMRX_FSTAT_MGSTAT1_MASK));
  1004. } else {
  1005. LOG_DEBUG("Ignoring errored PFlash sector blank-check");
  1006. bank->sectors[i].is_erased = -1;
  1007. }
  1008. }
  1009. } else {
  1010. /* the whole bank is erased, update all sectors */
  1011. int i;
  1012. for (i = 0; i < bank->num_sectors; i++)
  1013. bank->sectors[i].is_erased = 1;
  1014. }
  1015. return ERROR_OK;
  1016. }
  1017. static const struct command_registration kinetis_ke_security_command_handlers[] = {
  1018. {
  1019. .name = "check_security",
  1020. .mode = COMMAND_EXEC,
  1021. .help = "",
  1022. .usage = "",
  1023. .handler = kinetis_ke_check_flash_security_status,
  1024. },
  1025. {
  1026. .name = "mass_erase",
  1027. .mode = COMMAND_EXEC,
  1028. .help = "",
  1029. .usage = "",
  1030. .handler = kinetis_ke_mdm_mass_erase,
  1031. },
  1032. {
  1033. .name = "test_securing",
  1034. .mode = COMMAND_EXEC,
  1035. .help = "",
  1036. .usage = "",
  1037. .handler = kinetis_ke_securing_test,
  1038. },
  1039. COMMAND_REGISTRATION_DONE
  1040. };
  1041. static const struct command_registration kinetis_ke_exec_command_handlers[] = {
  1042. {
  1043. .name = "mdm",
  1044. .mode = COMMAND_ANY,
  1045. .help = "",
  1046. .usage = "",
  1047. .chain = kinetis_ke_security_command_handlers,
  1048. },
  1049. {
  1050. .name = "disable_wdog",
  1051. .mode = COMMAND_EXEC,
  1052. .help = "Disable the watchdog timer",
  1053. .usage = "",
  1054. .handler = kinetis_ke_disable_wdog_handler,
  1055. },
  1056. COMMAND_REGISTRATION_DONE
  1057. };
  1058. static const struct command_registration kinetis_ke_command_handler[] = {
  1059. {
  1060. .name = "kinetis_ke",
  1061. .mode = COMMAND_ANY,
  1062. .help = "Kinetis KE NAND flash controller commands",
  1063. .usage = "",
  1064. .chain = kinetis_ke_exec_command_handlers,
  1065. },
  1066. COMMAND_REGISTRATION_DONE
  1067. };
  1068. struct flash_driver kinetis_ke_flash = {
  1069. .name = "kinetis_ke",
  1070. .commands = kinetis_ke_command_handler,
  1071. .flash_bank_command = kinetis_ke_flash_bank_command,
  1072. .erase = kinetis_ke_erase,
  1073. .protect = kinetis_ke_protect,
  1074. .write = kinetis_ke_write,
  1075. .read = default_flash_read,
  1076. .probe = kinetis_ke_probe,
  1077. .auto_probe = kinetis_ke_auto_probe,
  1078. .erase_check = kinetis_ke_blank_check,
  1079. .protect_check = kinetis_ke_protect_check,
  1080. .info = kinetis_ke_info,
  1081. };