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.
 
 
 
 
 
 

1992 lines
59 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 <target/target_type.h>
  37. #include <target/algorithm.h>
  38. #include <target/armv7m.h>
  39. #include <target/cortex_m.h>
  40. /*
  41. * Implementation Notes
  42. *
  43. * The persistent memories in the Kinetis chip families K10 through
  44. * K70 are all manipulated with the Flash Memory Module. Some
  45. * variants call this module the FTFE, others call it the FTFL. To
  46. * indicate that both are considered here, we use FTFX.
  47. *
  48. * Within the module, according to the chip variant, the persistent
  49. * memory is divided into what Freescale terms Program Flash, FlexNVM,
  50. * and FlexRAM. All chip variants have Program Flash. Some chip
  51. * variants also have FlexNVM and FlexRAM, which always appear
  52. * together.
  53. *
  54. * A given Kinetis chip may have 1, 2 or 4 blocks of flash. Here we map
  55. * each block to a separate bank. Each block size varies by chip and
  56. * may be determined by the read-only SIM_FCFG1 register. The sector
  57. * size within each bank/block varies by chip, and may be 1, 2 or 4k.
  58. * The sector size may be different for flash and FlexNVM.
  59. *
  60. * The first half of the flash (1 or 2 blocks) is always Program Flash
  61. * and always starts at address 0x00000000. The "PFLSH" flag, bit 23
  62. * of the read-only SIM_FCFG2 register, determines whether the second
  63. * half of the flash is also Program Flash or FlexNVM+FlexRAM. When
  64. * PFLSH is set, the second from the first half. When PFLSH is clear,
  65. * the second half of flash is FlexNVM and always starts at address
  66. * 0x10000000. FlexRAM, which is also present when PFLSH is clear,
  67. * always starts at address 0x14000000.
  68. *
  69. * The Flash Memory Module provides a register set where flash
  70. * commands are loaded to perform flash operations like erase and
  71. * program. Different commands are available depending on whether
  72. * Program Flash or FlexNVM/FlexRAM is being manipulated. Although
  73. * the commands used are quite consistent between flash blocks, the
  74. * parameters they accept differ according to the flash sector size.
  75. *
  76. */
  77. /* Addressess */
  78. #define FLEXRAM 0x14000000
  79. #define FMC_PFB01CR 0x4001f004
  80. #define FTFx_FSTAT 0x40020000
  81. #define FTFx_FCNFG 0x40020001
  82. #define FTFx_FCCOB3 0x40020004
  83. #define FTFx_FPROT3 0x40020010
  84. #define FTFx_FDPROT 0x40020017
  85. #define SIM_SDID 0x40048024
  86. #define SIM_SOPT1 0x40047000
  87. #define SIM_FCFG1 0x4004804c
  88. #define SIM_FCFG2 0x40048050
  89. #define WDOG_STCTRH 0x40052000
  90. #define SMC_PMCTRL 0x4007E001
  91. #define SMC_PMSTAT 0x4007E003
  92. /* Values */
  93. #define PM_STAT_RUN 0x01
  94. #define PM_STAT_VLPR 0x04
  95. #define PM_CTRL_RUNM_RUN 0x00
  96. /* Commands */
  97. #define FTFx_CMD_BLOCKSTAT 0x00
  98. #define FTFx_CMD_SECTSTAT 0x01
  99. #define FTFx_CMD_LWORDPROG 0x06
  100. #define FTFx_CMD_SECTERASE 0x09
  101. #define FTFx_CMD_SECTWRITE 0x0b
  102. #define FTFx_CMD_MASSERASE 0x44
  103. #define FTFx_CMD_PGMPART 0x80
  104. #define FTFx_CMD_SETFLEXRAM 0x81
  105. /* The older Kinetis K series uses the following SDID layout :
  106. * Bit 31-16 : 0
  107. * Bit 15-12 : REVID
  108. * Bit 11-7 : DIEID
  109. * Bit 6-4 : FAMID
  110. * Bit 3-0 : PINID
  111. *
  112. * The newer Kinetis series uses the following SDID layout :
  113. * Bit 31-28 : FAMID
  114. * Bit 27-24 : SUBFAMID
  115. * Bit 23-20 : SERIESID
  116. * Bit 19-16 : SRAMSIZE
  117. * Bit 15-12 : REVID
  118. * Bit 6-4 : Reserved (0)
  119. * Bit 3-0 : PINID
  120. *
  121. * We assume that if bits 31-16 are 0 then it's an older
  122. * K-series MCU.
  123. */
  124. #define KINETIS_SOPT1_RAMSIZE_MASK 0x0000F000
  125. #define KINETIS_SOPT1_RAMSIZE_K24FN1M 0x0000B000
  126. #define KINETIS_SDID_K_SERIES_MASK 0x0000FFFF
  127. #define KINETIS_SDID_DIEID_MASK 0x00000F80
  128. #define KINETIS_SDID_DIEID_K22FN128 0x00000680 /* smaller pflash with FTFA */
  129. #define KINETIS_SDID_DIEID_K22FN256 0x00000A80
  130. #define KINETIS_SDID_DIEID_K22FN512 0x00000E80
  131. #define KINETIS_SDID_DIEID_K24FN256 0x00000700
  132. #define KINETIS_SDID_DIEID_K24FN1M 0x00000300 /* Detect Errata 7534 */
  133. /* We can't rely solely on the FAMID field to determine the MCU
  134. * type since some FAMID values identify multiple MCUs with
  135. * different flash sector sizes (K20 and K22 for instance).
  136. * Therefore we combine it with the DIEID bits which may possibly
  137. * break if Freescale bumps the DIEID for a particular MCU. */
  138. #define KINETIS_K_SDID_TYPE_MASK 0x00000FF0
  139. #define KINETIS_K_SDID_K10_M50 0x00000000
  140. #define KINETIS_K_SDID_K10_M72 0x00000080
  141. #define KINETIS_K_SDID_K10_M100 0x00000100
  142. #define KINETIS_K_SDID_K10_M120 0x00000180
  143. #define KINETIS_K_SDID_K11 0x00000220
  144. #define KINETIS_K_SDID_K12 0x00000200
  145. #define KINETIS_K_SDID_K20_M50 0x00000010
  146. #define KINETIS_K_SDID_K20_M72 0x00000090
  147. #define KINETIS_K_SDID_K20_M100 0x00000110
  148. #define KINETIS_K_SDID_K20_M120 0x00000190
  149. #define KINETIS_K_SDID_K21_M50 0x00000230
  150. #define KINETIS_K_SDID_K21_M120 0x00000330
  151. #define KINETIS_K_SDID_K22_M50 0x00000210
  152. #define KINETIS_K_SDID_K22_M120 0x00000310
  153. #define KINETIS_K_SDID_K30_M72 0x000000A0
  154. #define KINETIS_K_SDID_K30_M100 0x00000120
  155. #define KINETIS_K_SDID_K40_M72 0x000000B0
  156. #define KINETIS_K_SDID_K40_M100 0x00000130
  157. #define KINETIS_K_SDID_K50_M72 0x000000E0
  158. #define KINETIS_K_SDID_K51_M72 0x000000F0
  159. #define KINETIS_K_SDID_K53 0x00000170
  160. #define KINETIS_K_SDID_K60_M100 0x00000140
  161. #define KINETIS_K_SDID_K60_M150 0x000001C0
  162. #define KINETIS_K_SDID_K70_M150 0x000001D0
  163. #define KINETIS_SDID_SERIESID_MASK 0x00F00000
  164. #define KINETIS_SDID_SERIESID_K 0x00000000
  165. #define KINETIS_SDID_SERIESID_KL 0x00100000
  166. #define KINETIS_SDID_SERIESID_KW 0x00500000
  167. #define KINETIS_SDID_SERIESID_KV 0x00600000
  168. #define KINETIS_SDID_SUBFAMID_MASK 0x0F000000
  169. #define KINETIS_SDID_SUBFAMID_KX0 0x00000000
  170. #define KINETIS_SDID_SUBFAMID_KX1 0x01000000
  171. #define KINETIS_SDID_SUBFAMID_KX2 0x02000000
  172. #define KINETIS_SDID_SUBFAMID_KX3 0x03000000
  173. #define KINETIS_SDID_SUBFAMID_KX4 0x04000000
  174. #define KINETIS_SDID_SUBFAMID_KX5 0x05000000
  175. #define KINETIS_SDID_SUBFAMID_KX6 0x06000000
  176. #define KINETIS_SDID_FAMILYID_MASK 0xF0000000
  177. #define KINETIS_SDID_FAMILYID_K0X 0x00000000
  178. #define KINETIS_SDID_FAMILYID_K1X 0x10000000
  179. #define KINETIS_SDID_FAMILYID_K2X 0x20000000
  180. #define KINETIS_SDID_FAMILYID_K3X 0x30000000
  181. #define KINETIS_SDID_FAMILYID_K4X 0x40000000
  182. #define KINETIS_SDID_FAMILYID_K6X 0x60000000
  183. #define KINETIS_SDID_FAMILYID_K7X 0x70000000
  184. struct kinetis_flash_bank {
  185. bool probed;
  186. uint32_t sector_size;
  187. uint32_t max_flash_prog_size;
  188. uint32_t protection_size;
  189. uint32_t prog_base; /* base address for FTFx operations */
  190. /* same as bank->base for pflash, differs for FlexNVM */
  191. uint32_t protection_block; /* number of first protection block in this bank */
  192. uint32_t sim_sdid;
  193. uint32_t sim_fcfg1;
  194. uint32_t sim_fcfg2;
  195. enum {
  196. FC_AUTO = 0,
  197. FC_PFLASH,
  198. FC_FLEX_NVM,
  199. FC_FLEX_RAM,
  200. } flash_class;
  201. enum {
  202. FS_PROGRAM_SECTOR = 1,
  203. FS_PROGRAM_LONGWORD = 2,
  204. FS_PROGRAM_PHRASE = 4, /* Unsupported */
  205. FS_INVALIDATE_CACHE = 8,
  206. } flash_support;
  207. };
  208. #define MDM_REG_STAT 0x00
  209. #define MDM_REG_CTRL 0x04
  210. #define MDM_REG_ID 0xfc
  211. #define MDM_STAT_FMEACK (1<<0)
  212. #define MDM_STAT_FREADY (1<<1)
  213. #define MDM_STAT_SYSSEC (1<<2)
  214. #define MDM_STAT_SYSRES (1<<3)
  215. #define MDM_STAT_FMEEN (1<<5)
  216. #define MDM_STAT_BACKDOOREN (1<<6)
  217. #define MDM_STAT_LPEN (1<<7)
  218. #define MDM_STAT_VLPEN (1<<8)
  219. #define MDM_STAT_LLSMODEXIT (1<<9)
  220. #define MDM_STAT_VLLSXMODEXIT (1<<10)
  221. #define MDM_STAT_CORE_HALTED (1<<16)
  222. #define MDM_STAT_CORE_SLEEPDEEP (1<<17)
  223. #define MDM_STAT_CORESLEEPING (1<<18)
  224. #define MEM_CTRL_FMEIP (1<<0)
  225. #define MEM_CTRL_DBG_DIS (1<<1)
  226. #define MEM_CTRL_DBG_REQ (1<<2)
  227. #define MEM_CTRL_SYS_RES_REQ (1<<3)
  228. #define MEM_CTRL_CORE_HOLD_RES (1<<4)
  229. #define MEM_CTRL_VLLSX_DBG_REQ (1<<5)
  230. #define MEM_CTRL_VLLSX_DBG_ACK (1<<6)
  231. #define MEM_CTRL_VLLSX_STAT_ACK (1<<7)
  232. #define MDM_ACCESS_TIMEOUT 3000 /* iterations */
  233. static int kinetis_mdm_write_register(struct adiv5_dap *dap, unsigned reg, uint32_t value)
  234. {
  235. int retval;
  236. LOG_DEBUG("MDM_REG[0x%02x] <- %08" PRIX32, reg, value);
  237. retval = dap_queue_ap_write(dap_ap(dap, 1), reg, value);
  238. if (retval != ERROR_OK) {
  239. LOG_DEBUG("MDM: failed to queue a write request");
  240. return retval;
  241. }
  242. retval = dap_run(dap);
  243. if (retval != ERROR_OK) {
  244. LOG_DEBUG("MDM: dap_run failed");
  245. return retval;
  246. }
  247. return ERROR_OK;
  248. }
  249. static int kinetis_mdm_read_register(struct adiv5_dap *dap, unsigned reg, uint32_t *result)
  250. {
  251. int retval;
  252. retval = dap_queue_ap_read(dap_ap(dap, 1), reg, result);
  253. if (retval != ERROR_OK) {
  254. LOG_DEBUG("MDM: failed to queue a read request");
  255. return retval;
  256. }
  257. retval = dap_run(dap);
  258. if (retval != ERROR_OK) {
  259. LOG_DEBUG("MDM: dap_run failed");
  260. return retval;
  261. }
  262. LOG_DEBUG("MDM_REG[0x%02x]: %08" PRIX32, reg, *result);
  263. return ERROR_OK;
  264. }
  265. static int kinetis_mdm_poll_register(struct adiv5_dap *dap, unsigned reg, uint32_t mask, uint32_t value)
  266. {
  267. uint32_t val;
  268. int retval;
  269. int timeout = MDM_ACCESS_TIMEOUT;
  270. do {
  271. retval = kinetis_mdm_read_register(dap, reg, &val);
  272. if (retval != ERROR_OK || (val & mask) == value)
  273. return retval;
  274. alive_sleep(1);
  275. } while (timeout--);
  276. LOG_DEBUG("MDM: polling timed out");
  277. return ERROR_FAIL;
  278. }
  279. /*
  280. * This function implements the procedure to mass erase the flash via
  281. * SWD/JTAG on Kinetis K and L series of devices as it is described in
  282. * AN4835 "Production Flash Programming Best Practices for Kinetis K-
  283. * and L-series MCUs" Section 4.2.1
  284. */
  285. COMMAND_HANDLER(kinetis_mdm_mass_erase)
  286. {
  287. struct target *target = get_current_target(CMD_CTX);
  288. struct cortex_m_common *cortex_m = target_to_cm(target);
  289. struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
  290. if (!dap) {
  291. LOG_ERROR("Cannot perform mass erase with a high-level adapter");
  292. return ERROR_FAIL;
  293. }
  294. int retval;
  295. /*
  296. * ... Power on the processor, or if power has already been
  297. * applied, assert the RESET pin to reset the processor. For
  298. * devices that do not have a RESET pin, write the System
  299. * Reset Request bit in the MDM-AP control register after
  300. * establishing communication...
  301. */
  302. /* assert SRST */
  303. if (jtag_get_reset_config() & RESET_HAS_SRST)
  304. adapter_assert_reset();
  305. else
  306. LOG_WARNING("Attempting mass erase without hardware reset. This is not reliable; "
  307. "it's recommended you connect SRST and use ``reset_config srst_only''.");
  308. retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, MEM_CTRL_SYS_RES_REQ);
  309. if (retval != ERROR_OK)
  310. return retval;
  311. /*
  312. * ... Read the MDM-AP status register until the Flash Ready bit sets...
  313. */
  314. retval = kinetis_mdm_poll_register(dap, MDM_REG_STAT,
  315. MDM_STAT_FREADY | MDM_STAT_SYSRES,
  316. MDM_STAT_FREADY);
  317. if (retval != ERROR_OK) {
  318. LOG_ERROR("MDM : flash ready timeout");
  319. return retval;
  320. }
  321. /*
  322. * ... Write the MDM-AP control register to set the Flash Mass
  323. * Erase in Progress bit. This will start the mass erase
  324. * process...
  325. */
  326. retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL,
  327. MEM_CTRL_SYS_RES_REQ | MEM_CTRL_FMEIP);
  328. if (retval != ERROR_OK)
  329. return retval;
  330. /* As a sanity check make sure that device started mass erase procedure */
  331. retval = kinetis_mdm_poll_register(dap, MDM_REG_STAT,
  332. MDM_STAT_FMEACK, MDM_STAT_FMEACK);
  333. if (retval != ERROR_OK)
  334. return retval;
  335. /*
  336. * ... Read the MDM-AP control register until the Flash Mass
  337. * Erase in Progress bit clears...
  338. */
  339. retval = kinetis_mdm_poll_register(dap, MDM_REG_CTRL,
  340. MEM_CTRL_FMEIP,
  341. 0);
  342. if (retval != ERROR_OK)
  343. return retval;
  344. /*
  345. * ... Negate the RESET signal or clear the System Reset Request
  346. * bit in the MDM-AP control register...
  347. */
  348. retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
  349. if (retval != ERROR_OK)
  350. return retval;
  351. if (jtag_get_reset_config() & RESET_HAS_SRST) {
  352. /* halt MCU otherwise it loops in hard fault - WDOG reset cycle */
  353. target->reset_halt = true;
  354. target->type->assert_reset(target);
  355. target->type->deassert_reset(target);
  356. }
  357. return ERROR_OK;
  358. }
  359. static const uint32_t kinetis_known_mdm_ids[] = {
  360. 0x001C0000, /* Kinetis-K Series */
  361. 0x001C0020, /* Kinetis-L/M/V/E Series */
  362. };
  363. /*
  364. * This function implements the procedure to connect to
  365. * SWD/JTAG on Kinetis K and L series of devices as it is described in
  366. * AN4835 "Production Flash Programming Best Practices for Kinetis K-
  367. * and L-series MCUs" Section 4.1.1
  368. */
  369. COMMAND_HANDLER(kinetis_check_flash_security_status)
  370. {
  371. struct target *target = get_current_target(CMD_CTX);
  372. struct cortex_m_common *cortex_m = target_to_cm(target);
  373. struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
  374. if (!dap) {
  375. LOG_WARNING("Cannot check flash security status with a high-level adapter");
  376. return ERROR_OK;
  377. }
  378. uint32_t val;
  379. int retval;
  380. /*
  381. * ... The MDM-AP ID register can be read to verify that the
  382. * connection is working correctly...
  383. */
  384. retval = kinetis_mdm_read_register(dap, MDM_REG_ID, &val);
  385. if (retval != ERROR_OK) {
  386. LOG_ERROR("MDM: failed to read ID register");
  387. goto fail;
  388. }
  389. bool found = false;
  390. for (size_t i = 0; i < ARRAY_SIZE(kinetis_known_mdm_ids); i++) {
  391. if (val == kinetis_known_mdm_ids[i]) {
  392. found = true;
  393. break;
  394. }
  395. }
  396. if (!found)
  397. LOG_WARNING("MDM: unknown ID %08" PRIX32, val);
  398. /*
  399. * ... Read the MDM-AP status register until the Flash Ready bit sets...
  400. */
  401. retval = kinetis_mdm_poll_register(dap, MDM_REG_STAT,
  402. MDM_STAT_FREADY,
  403. MDM_STAT_FREADY);
  404. if (retval != ERROR_OK) {
  405. LOG_ERROR("MDM: flash ready timeout");
  406. goto fail;
  407. }
  408. /*
  409. * ... Read the System Security bit to determine if security is enabled.
  410. * If System Security = 0, then proceed. If System Security = 1, then
  411. * communication with the internals of the processor, including the
  412. * flash, will not be possible without issuing a mass erase command or
  413. * unsecuring the part through other means (backdoor key unlock)...
  414. */
  415. retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &val);
  416. if (retval != ERROR_OK) {
  417. LOG_ERROR("MDM: failed to read MDM_REG_STAT");
  418. goto fail;
  419. }
  420. if ((val & (MDM_STAT_SYSSEC | MDM_STAT_CORE_HALTED)) == MDM_STAT_SYSSEC) {
  421. LOG_WARNING("MDM: Secured MCU state detected however it may be a false alarm");
  422. LOG_WARNING("MDM: Halting target to detect secured state reliably");
  423. retval = target_halt(target);
  424. if (retval == ERROR_OK)
  425. retval = target_wait_state(target, TARGET_HALTED, 100);
  426. if (retval != ERROR_OK) {
  427. LOG_WARNING("MDM: Target not halted, trying reset halt");
  428. target->reset_halt = true;
  429. target->type->assert_reset(target);
  430. target->type->deassert_reset(target);
  431. }
  432. /* re-read status */
  433. retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &val);
  434. if (retval != ERROR_OK) {
  435. LOG_ERROR("MDM: failed to read MDM_REG_STAT");
  436. goto fail;
  437. }
  438. }
  439. if (val & MDM_STAT_SYSSEC) {
  440. jtag_poll_set_enabled(false);
  441. LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
  442. LOG_WARNING("**** ****");
  443. LOG_WARNING("**** Your Kinetis MCU is in secured state, which means that, ****");
  444. LOG_WARNING("**** with exception for very basic communication, JTAG/SWD ****");
  445. LOG_WARNING("**** interface will NOT work. In order to restore its ****");
  446. LOG_WARNING("**** functionality please issue 'kinetis mdm mass_erase' ****");
  447. LOG_WARNING("**** command, power cycle the MCU and restart OpenOCD. ****");
  448. LOG_WARNING("**** ****");
  449. LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
  450. } else {
  451. LOG_INFO("MDM: Chip is unsecured. Continuing.");
  452. jtag_poll_set_enabled(true);
  453. }
  454. return ERROR_OK;
  455. fail:
  456. LOG_ERROR("MDM: Failed to check security status of the MCU. Cannot proceed further");
  457. jtag_poll_set_enabled(false);
  458. return retval;
  459. }
  460. FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
  461. {
  462. struct kinetis_flash_bank *bank_info;
  463. if (CMD_ARGC < 6)
  464. return ERROR_COMMAND_SYNTAX_ERROR;
  465. LOG_INFO("add flash_bank kinetis %s", bank->name);
  466. bank_info = malloc(sizeof(struct kinetis_flash_bank));
  467. memset(bank_info, 0, sizeof(struct kinetis_flash_bank));
  468. bank->driver_priv = bank_info;
  469. return ERROR_OK;
  470. }
  471. /* Disable the watchdog on Kinetis devices */
  472. int kinetis_disable_wdog(struct target *target, uint32_t sim_sdid)
  473. {
  474. struct working_area *wdog_algorithm;
  475. struct armv7m_algorithm armv7m_info;
  476. uint16_t wdog;
  477. int retval;
  478. static const uint8_t kinetis_unlock_wdog_code[] = {
  479. #include "../../../contrib/loaders/watchdog/armv7m_kinetis_wdog.inc"
  480. };
  481. /* Decide whether the connected device needs watchdog disabling.
  482. * Disable for all Kx and KVx devices, return if it is a KLx */
  483. if ((sim_sdid & KINETIS_SDID_SERIESID_MASK) == KINETIS_SDID_SERIESID_KL)
  484. return ERROR_OK;
  485. /* The connected device requires watchdog disabling. */
  486. retval = target_read_u16(target, WDOG_STCTRH, &wdog);
  487. if (retval != ERROR_OK)
  488. return retval;
  489. if ((wdog & 0x1) == 0) {
  490. /* watchdog already disabled */
  491. return ERROR_OK;
  492. }
  493. LOG_INFO("Disabling Kinetis watchdog (initial WDOG_STCTRLH = 0x%x)", wdog);
  494. if (target->state != TARGET_HALTED) {
  495. LOG_ERROR("Target not halted");
  496. return ERROR_TARGET_NOT_HALTED;
  497. }
  498. retval = target_alloc_working_area(target, sizeof(kinetis_unlock_wdog_code), &wdog_algorithm);
  499. if (retval != ERROR_OK)
  500. return retval;
  501. retval = target_write_buffer(target, wdog_algorithm->address,
  502. sizeof(kinetis_unlock_wdog_code), (uint8_t *)kinetis_unlock_wdog_code);
  503. if (retval != ERROR_OK) {
  504. target_free_working_area(target, wdog_algorithm);
  505. return retval;
  506. }
  507. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  508. armv7m_info.core_mode = ARM_MODE_THREAD;
  509. retval = target_run_algorithm(target, 0, NULL, 0, NULL, wdog_algorithm->address,
  510. wdog_algorithm->address + (sizeof(kinetis_unlock_wdog_code) - 2),
  511. 10000, &armv7m_info);
  512. if (retval != ERROR_OK)
  513. LOG_ERROR("error executing kinetis wdog unlock algorithm");
  514. retval = target_read_u16(target, WDOG_STCTRH, &wdog);
  515. if (retval != ERROR_OK)
  516. return retval;
  517. LOG_INFO("WDOG_STCTRLH = 0x%x", wdog);
  518. target_free_working_area(target, wdog_algorithm);
  519. return retval;
  520. }
  521. COMMAND_HANDLER(kinetis_disable_wdog_handler)
  522. {
  523. int result;
  524. uint32_t sim_sdid;
  525. struct target *target = get_current_target(CMD_CTX);
  526. if (CMD_ARGC > 0)
  527. return ERROR_COMMAND_SYNTAX_ERROR;
  528. result = target_read_u32(target, SIM_SDID, &sim_sdid);
  529. if (result != ERROR_OK) {
  530. LOG_ERROR("Failed to read SIMSDID");
  531. return result;
  532. }
  533. result = kinetis_disable_wdog(target, sim_sdid);
  534. return result;
  535. }
  536. /* Kinetis Program-LongWord Microcodes */
  537. static const uint8_t kinetis_flash_write_code[] = {
  538. /* Params:
  539. * r0 - workarea buffer
  540. * r1 - target address
  541. * r2 - wordcount
  542. * Clobbered:
  543. * r4 - tmp
  544. * r5 - tmp
  545. * r6 - tmp
  546. * r7 - tmp
  547. */
  548. /* .L1: */
  549. /* for(register uint32_t i=0;i<wcount;i++){ */
  550. 0x04, 0x1C, /* mov r4, r0 */
  551. 0x00, 0x23, /* mov r3, #0 */
  552. /* .L2: */
  553. 0x0E, 0x1A, /* sub r6, r1, r0 */
  554. 0xA6, 0x19, /* add r6, r4, r6 */
  555. 0x93, 0x42, /* cmp r3, r2 */
  556. 0x16, 0xD0, /* beq .L9 */
  557. /* .L5: */
  558. /* while((FTFx_FSTAT&FTFA_FSTAT_CCIF_MASK) != FTFA_FSTAT_CCIF_MASK){}; */
  559. 0x0B, 0x4D, /* ldr r5, .L10 */
  560. 0x2F, 0x78, /* ldrb r7, [r5] */
  561. 0x7F, 0xB2, /* sxtb r7, r7 */
  562. 0x00, 0x2F, /* cmp r7, #0 */
  563. 0xFA, 0xDA, /* bge .L5 */
  564. /* FTFx_FSTAT = FTFA_FSTAT_ACCERR_MASK|FTFA_FSTAT_FPVIOL_MASK|FTFA_FSTAT_RDCO */
  565. 0x70, 0x27, /* mov r7, #112 */
  566. 0x2F, 0x70, /* strb r7, [r5] */
  567. /* FTFx_FCCOB3 = faddr; */
  568. 0x09, 0x4F, /* ldr r7, .L10+4 */
  569. 0x3E, 0x60, /* str r6, [r7] */
  570. 0x06, 0x27, /* mov r7, #6 */
  571. /* FTFx_FCCOB0 = 0x06; */
  572. 0x08, 0x4E, /* ldr r6, .L10+8 */
  573. 0x37, 0x70, /* strb r7, [r6] */
  574. /* FTFx_FCCOB7 = *pLW; */
  575. 0x80, 0xCC, /* ldmia r4!, {r7} */
  576. 0x08, 0x4E, /* ldr r6, .L10+12 */
  577. 0x37, 0x60, /* str r7, [r6] */
  578. /* FTFx_FSTAT = FTFA_FSTAT_CCIF_MASK; */
  579. 0x80, 0x27, /* mov r7, #128 */
  580. 0x2F, 0x70, /* strb r7, [r5] */
  581. /* .L4: */
  582. /* while((FTFx_FSTAT&FTFA_FSTAT_CCIF_MASK) != FTFA_FSTAT_CCIF_MASK){}; */
  583. 0x2E, 0x78, /* ldrb r6, [r5] */
  584. 0x77, 0xB2, /* sxtb r7, r6 */
  585. 0x00, 0x2F, /* cmp r7, #0 */
  586. 0xFB, 0xDA, /* bge .L4 */
  587. 0x01, 0x33, /* add r3, r3, #1 */
  588. 0xE4, 0xE7, /* b .L2 */
  589. /* .L9: */
  590. 0x00, 0xBE, /* bkpt #0 */
  591. /* .L10: */
  592. 0x00, 0x00, 0x02, 0x40, /* .word 1073872896 */
  593. 0x04, 0x00, 0x02, 0x40, /* .word 1073872900 */
  594. 0x07, 0x00, 0x02, 0x40, /* .word 1073872903 */
  595. 0x08, 0x00, 0x02, 0x40, /* .word 1073872904 */
  596. };
  597. /* Program LongWord Block Write */
  598. static int kinetis_write_block(struct flash_bank *bank, const uint8_t *buffer,
  599. uint32_t offset, uint32_t wcount)
  600. {
  601. struct target *target = bank->target;
  602. uint32_t buffer_size = 2048; /* Default minimum value */
  603. struct working_area *write_algorithm;
  604. struct working_area *source;
  605. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  606. uint32_t address = kinfo->prog_base + offset;
  607. struct reg_param reg_params[3];
  608. struct armv7m_algorithm armv7m_info;
  609. int retval = ERROR_OK;
  610. /* Params:
  611. * r0 - workarea buffer
  612. * r1 - target address
  613. * r2 - wordcount
  614. * Clobbered:
  615. * r4 - tmp
  616. * r5 - tmp
  617. * r6 - tmp
  618. * r7 - tmp
  619. */
  620. /* Increase buffer_size if needed */
  621. if (buffer_size < (target->working_area_size/2))
  622. buffer_size = (target->working_area_size/2);
  623. LOG_INFO("Kinetis: FLASH Write ...");
  624. /* check code alignment */
  625. if (offset & 0x1) {
  626. LOG_WARNING("offset 0x%" PRIx32 " breaks required 2-byte alignment", offset);
  627. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  628. }
  629. /* allocate working area with flash programming code */
  630. if (target_alloc_working_area(target, sizeof(kinetis_flash_write_code),
  631. &write_algorithm) != ERROR_OK) {
  632. LOG_WARNING("no working area available, can't do block memory writes");
  633. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  634. }
  635. retval = target_write_buffer(target, write_algorithm->address,
  636. sizeof(kinetis_flash_write_code), kinetis_flash_write_code);
  637. if (retval != ERROR_OK)
  638. return retval;
  639. /* memory buffer */
  640. while (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK) {
  641. buffer_size /= 4;
  642. if (buffer_size <= 256) {
  643. /* free working area, write algorithm already allocated */
  644. target_free_working_area(target, write_algorithm);
  645. LOG_WARNING("No large enough working area available, can't do block memory writes");
  646. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  647. }
  648. }
  649. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  650. armv7m_info.core_mode = ARM_MODE_THREAD;
  651. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT); /* *pLW (*buffer) */
  652. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT); /* faddr */
  653. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT); /* number of words to program */
  654. /* write code buffer and use Flash programming code within kinetis */
  655. /* Set breakpoint to 0 with time-out of 1000 ms */
  656. while (wcount > 0) {
  657. uint32_t thisrun_count = (wcount > (buffer_size / 4)) ? (buffer_size / 4) : wcount;
  658. retval = target_write_buffer(target, source->address, thisrun_count * 4, buffer);
  659. if (retval != ERROR_OK)
  660. break;
  661. buf_set_u32(reg_params[0].value, 0, 32, source->address);
  662. buf_set_u32(reg_params[1].value, 0, 32, address);
  663. buf_set_u32(reg_params[2].value, 0, 32, thisrun_count);
  664. retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
  665. write_algorithm->address, 0, 100000, &armv7m_info);
  666. if (retval != ERROR_OK) {
  667. LOG_ERROR("Error executing kinetis Flash programming algorithm");
  668. retval = ERROR_FLASH_OPERATION_FAILED;
  669. break;
  670. }
  671. buffer += thisrun_count * 4;
  672. address += thisrun_count * 4;
  673. wcount -= thisrun_count;
  674. }
  675. target_free_working_area(target, source);
  676. target_free_working_area(target, write_algorithm);
  677. destroy_reg_param(&reg_params[0]);
  678. destroy_reg_param(&reg_params[1]);
  679. destroy_reg_param(&reg_params[2]);
  680. return retval;
  681. }
  682. static int kinetis_protect(struct flash_bank *bank, int set, int first, int last)
  683. {
  684. LOG_WARNING("kinetis_protect not supported yet");
  685. /* FIXME: TODO */
  686. if (bank->target->state != TARGET_HALTED) {
  687. LOG_ERROR("Target not halted");
  688. return ERROR_TARGET_NOT_HALTED;
  689. }
  690. return ERROR_FLASH_BANK_INVALID;
  691. }
  692. static int kinetis_protect_check(struct flash_bank *bank)
  693. {
  694. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  695. int result;
  696. int i, b;
  697. uint32_t fprot, psec;
  698. if (bank->target->state != TARGET_HALTED) {
  699. LOG_ERROR("Target not halted");
  700. return ERROR_TARGET_NOT_HALTED;
  701. }
  702. if (kinfo->flash_class == FC_PFLASH) {
  703. uint8_t buffer[4];
  704. /* read protection register */
  705. result = target_read_memory(bank->target, FTFx_FPROT3, 1, 4, buffer);
  706. if (result != ERROR_OK)
  707. return result;
  708. fprot = target_buffer_get_u32(bank->target, buffer);
  709. /* Every bit protects 1/32 of the full flash (not necessarily just this bank) */
  710. } else if (kinfo->flash_class == FC_FLEX_NVM) {
  711. uint8_t fdprot;
  712. /* read protection register */
  713. result = target_read_memory(bank->target, FTFx_FDPROT, 1, 1, &fdprot);
  714. if (result != ERROR_OK)
  715. return result;
  716. fprot = fdprot;
  717. } else {
  718. LOG_ERROR("Protection checks for FlexRAM not supported");
  719. return ERROR_FLASH_BANK_INVALID;
  720. }
  721. b = kinfo->protection_block;
  722. for (psec = 0, i = 0; i < bank->num_sectors; i++) {
  723. if ((fprot >> b) & 1)
  724. bank->sectors[i].is_protected = 0;
  725. else
  726. bank->sectors[i].is_protected = 1;
  727. psec += bank->sectors[i].size;
  728. if (psec >= kinfo->protection_size) {
  729. psec = 0;
  730. b++;
  731. }
  732. }
  733. return ERROR_OK;
  734. }
  735. static int kinetis_ftfx_command(struct target *target, uint8_t fcmd, uint32_t faddr,
  736. uint8_t fccob4, uint8_t fccob5, uint8_t fccob6, uint8_t fccob7,
  737. uint8_t fccob8, uint8_t fccob9, uint8_t fccoba, uint8_t fccobb,
  738. uint8_t *ftfx_fstat)
  739. {
  740. uint8_t command[12] = {faddr & 0xff, (faddr >> 8) & 0xff, (faddr >> 16) & 0xff, fcmd,
  741. fccob7, fccob6, fccob5, fccob4,
  742. fccobb, fccoba, fccob9, fccob8};
  743. int result, i;
  744. uint8_t buffer;
  745. /* wait for done */
  746. for (i = 0; i < 50; i++) {
  747. result =
  748. target_read_memory(target, FTFx_FSTAT, 1, 1, &buffer);
  749. if (result != ERROR_OK)
  750. return result;
  751. if (buffer & 0x80)
  752. break;
  753. buffer = 0x00;
  754. }
  755. if (buffer != 0x80) {
  756. /* reset error flags */
  757. buffer = 0x30;
  758. result =
  759. target_write_memory(target, FTFx_FSTAT, 1, 1, &buffer);
  760. if (result != ERROR_OK)
  761. return result;
  762. }
  763. result = target_write_memory(target, FTFx_FCCOB3, 4, 3, command);
  764. if (result != ERROR_OK)
  765. return result;
  766. /* start command */
  767. buffer = 0x80;
  768. result = target_write_memory(target, FTFx_FSTAT, 1, 1, &buffer);
  769. if (result != ERROR_OK)
  770. return result;
  771. /* wait for done */
  772. for (i = 0; i < 240; i++) { /* Need longtime for "Mass Erase" Command Nemui Changed */
  773. result =
  774. target_read_memory(target, FTFx_FSTAT, 1, 1, ftfx_fstat);
  775. if (result != ERROR_OK)
  776. return result;
  777. if (*ftfx_fstat & 0x80)
  778. break;
  779. }
  780. if ((*ftfx_fstat & 0xf0) != 0x80) {
  781. LOG_ERROR
  782. ("ftfx command failed FSTAT: %02X FCCOB: %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X",
  783. *ftfx_fstat, command[3], command[2], command[1], command[0],
  784. command[7], command[6], command[5], command[4],
  785. command[11], command[10], command[9], command[8]);
  786. return ERROR_FLASH_OPERATION_FAILED;
  787. }
  788. return ERROR_OK;
  789. }
  790. static int kinetis_check_run_mode(struct target *target)
  791. {
  792. int result, i;
  793. uint8_t pmctrl, pmstat;
  794. if (target->state != TARGET_HALTED) {
  795. LOG_ERROR("Target not halted");
  796. return ERROR_TARGET_NOT_HALTED;
  797. }
  798. result = target_read_u8(target, SMC_PMSTAT, &pmstat);
  799. if (result != ERROR_OK)
  800. return result;
  801. if (pmstat == PM_STAT_RUN)
  802. return ERROR_OK;
  803. if (pmstat == PM_STAT_VLPR) {
  804. /* It is safe to switch from VLPR to RUN mode without changing clock */
  805. LOG_INFO("Switching from VLPR to RUN mode.");
  806. pmctrl = PM_CTRL_RUNM_RUN;
  807. result = target_write_u8(target, SMC_PMCTRL, pmctrl);
  808. if (result != ERROR_OK)
  809. return result;
  810. for (i = 100; i; i--) {
  811. result = target_read_u8(target, SMC_PMSTAT, &pmstat);
  812. if (result != ERROR_OK)
  813. return result;
  814. if (pmstat == PM_STAT_RUN)
  815. return ERROR_OK;
  816. }
  817. }
  818. LOG_ERROR("Flash operation not possible in current run mode: SMC_PMSTAT: 0x%x", pmstat);
  819. LOG_ERROR("Issue a 'reset init' command.");
  820. return ERROR_TARGET_NOT_HALTED;
  821. }
  822. static void kinetis_invalidate_flash_cache(struct flash_bank *bank)
  823. {
  824. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  825. uint8_t pfb01cr_byte2 = 0xf0;
  826. if (!(kinfo->flash_support & FS_INVALIDATE_CACHE))
  827. return;
  828. target_write_memory(bank->target, FMC_PFB01CR + 2, 1, 1, &pfb01cr_byte2);
  829. return;
  830. }
  831. static int kinetis_erase(struct flash_bank *bank, int first, int last)
  832. {
  833. int result, i;
  834. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  835. result = kinetis_check_run_mode(bank->target);
  836. if (result != ERROR_OK)
  837. return result;
  838. if ((first > bank->num_sectors) || (last > bank->num_sectors))
  839. return ERROR_FLASH_OPERATION_FAILED;
  840. /*
  841. * FIXME: TODO: use the 'Erase Flash Block' command if the
  842. * requested erase is PFlash or NVM and encompasses the entire
  843. * block. Should be quicker.
  844. */
  845. for (i = first; i <= last; i++) {
  846. uint8_t ftfx_fstat;
  847. /* set command and sector address */
  848. result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTERASE, kinfo->prog_base + bank->sectors[i].offset,
  849. 0, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat);
  850. if (result != ERROR_OK) {
  851. LOG_WARNING("erase sector %d failed", i);
  852. return ERROR_FLASH_OPERATION_FAILED;
  853. }
  854. bank->sectors[i].is_erased = 1;
  855. }
  856. kinetis_invalidate_flash_cache(bank);
  857. if (first == 0) {
  858. LOG_WARNING
  859. ("flash configuration field erased, please reset the device");
  860. }
  861. return ERROR_OK;
  862. }
  863. static int kinetis_make_ram_ready(struct target *target)
  864. {
  865. int result;
  866. uint8_t ftfx_fstat;
  867. uint8_t ftfx_fcnfg;
  868. /* check if ram ready */
  869. result = target_read_memory(target, FTFx_FCNFG, 1, 1, &ftfx_fcnfg);
  870. if (result != ERROR_OK)
  871. return result;
  872. if (ftfx_fcnfg & (1 << 1))
  873. return ERROR_OK; /* ram ready */
  874. /* make flex ram available */
  875. result = kinetis_ftfx_command(target, FTFx_CMD_SETFLEXRAM, 0x00ff0000,
  876. 0, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat);
  877. if (result != ERROR_OK)
  878. return ERROR_FLASH_OPERATION_FAILED;
  879. /* check again */
  880. result = target_read_memory(target, FTFx_FCNFG, 1, 1, &ftfx_fcnfg);
  881. if (result != ERROR_OK)
  882. return result;
  883. if (ftfx_fcnfg & (1 << 1))
  884. return ERROR_OK; /* ram ready */
  885. return ERROR_FLASH_OPERATION_FAILED;
  886. }
  887. static int kinetis_write(struct flash_bank *bank, const uint8_t *buffer,
  888. uint32_t offset, uint32_t count)
  889. {
  890. unsigned int i, result, fallback = 0;
  891. uint32_t wc;
  892. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  893. uint8_t *new_buffer = NULL;
  894. result = kinetis_check_run_mode(bank->target);
  895. if (result != ERROR_OK)
  896. return result;
  897. if (!(kinfo->flash_support & FS_PROGRAM_SECTOR)) {
  898. /* fallback to longword write */
  899. fallback = 1;
  900. LOG_WARNING("This device supports Program Longword execution only.");
  901. } else {
  902. result = kinetis_make_ram_ready(bank->target);
  903. if (result != ERROR_OK) {
  904. fallback = 1;
  905. LOG_WARNING("FlexRAM not ready, fallback to slow longword write.");
  906. }
  907. }
  908. LOG_DEBUG("flash write @08%" PRIX32, offset);
  909. /* program section command */
  910. if (fallback == 0) {
  911. /*
  912. * Kinetis uses different terms for the granularity of
  913. * sector writes, e.g. "phrase" or "128 bits". We use
  914. * the generic term "chunk". The largest possible
  915. * Kinetis "chunk" is 16 bytes (128 bits).
  916. */
  917. unsigned prog_section_chunk_bytes = kinfo->sector_size >> 8;
  918. unsigned prog_size_bytes = kinfo->max_flash_prog_size;
  919. for (i = 0; i < count; i += prog_size_bytes) {
  920. uint8_t residual_buffer[16];
  921. uint8_t ftfx_fstat;
  922. uint32_t section_count = prog_size_bytes / prog_section_chunk_bytes;
  923. uint32_t residual_wc = 0;
  924. /*
  925. * Assume the word count covers an entire
  926. * sector.
  927. */
  928. wc = prog_size_bytes / 4;
  929. /*
  930. * If bytes to be programmed are less than the
  931. * full sector, then determine the number of
  932. * full-words to program, and put together the
  933. * residual buffer so that a full "section"
  934. * may always be programmed.
  935. */
  936. if ((count - i) < prog_size_bytes) {
  937. /* number of bytes to program beyond full section */
  938. unsigned residual_bc = (count-i) % prog_section_chunk_bytes;
  939. /* number of complete words to copy directly from buffer */
  940. wc = (count - i - residual_bc) / 4;
  941. /* number of total sections to write, including residual */
  942. section_count = DIV_ROUND_UP((count-i), prog_section_chunk_bytes);
  943. /* any residual bytes delivers a whole residual section */
  944. residual_wc = (residual_bc ? prog_section_chunk_bytes : 0)/4;
  945. /* clear residual buffer then populate residual bytes */
  946. (void) memset(residual_buffer, 0xff, prog_section_chunk_bytes);
  947. (void) memcpy(residual_buffer, &buffer[i+4*wc], residual_bc);
  948. }
  949. LOG_DEBUG("write section @ %08" PRIX32 " with length %" PRIu32 " bytes",
  950. offset + i, (uint32_t)wc*4);
  951. /* write data to flexram as whole-words */
  952. result = target_write_memory(bank->target, FLEXRAM, 4, wc,
  953. buffer + i);
  954. if (result != ERROR_OK) {
  955. LOG_ERROR("target_write_memory failed");
  956. return result;
  957. }
  958. /* write the residual words to the flexram */
  959. if (residual_wc) {
  960. result = target_write_memory(bank->target,
  961. FLEXRAM+4*wc,
  962. 4, residual_wc,
  963. residual_buffer);
  964. if (result != ERROR_OK) {
  965. LOG_ERROR("target_write_memory failed");
  966. return result;
  967. }
  968. }
  969. /* execute section-write command */
  970. result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTWRITE, kinfo->prog_base + offset + i,
  971. section_count>>8, section_count, 0, 0,
  972. 0, 0, 0, 0, &ftfx_fstat);
  973. if (result != ERROR_OK)
  974. return ERROR_FLASH_OPERATION_FAILED;
  975. }
  976. }
  977. /* program longword command, not supported in "SF3" devices */
  978. else if (kinfo->flash_support & FS_PROGRAM_LONGWORD) {
  979. if (count & 0x3) {
  980. uint32_t old_count = count;
  981. count = (old_count | 3) + 1;
  982. new_buffer = malloc(count);
  983. if (new_buffer == NULL) {
  984. LOG_ERROR("odd number of bytes to write and no memory "
  985. "for padding buffer");
  986. return ERROR_FAIL;
  987. }
  988. LOG_INFO("odd number of bytes to write (%" PRIu32 "), extending to %" PRIu32 " "
  989. "and padding with 0xff", old_count, count);
  990. memset(new_buffer, 0xff, count);
  991. buffer = memcpy(new_buffer, buffer, old_count);
  992. }
  993. uint32_t words_remaining = count / 4;
  994. kinetis_disable_wdog(bank->target, kinfo->sim_sdid);
  995. /* try using a block write */
  996. int retval = kinetis_write_block(bank, buffer, offset, words_remaining);
  997. if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
  998. /* if block write failed (no sufficient working area),
  999. * we use normal (slow) single word accesses */
  1000. LOG_WARNING("couldn't use block writes, falling back to single "
  1001. "memory accesses");
  1002. for (i = 0; i < count; i += 4) {
  1003. uint8_t ftfx_fstat;
  1004. LOG_DEBUG("write longword @ %08" PRIX32, (uint32_t)(offset + i));
  1005. uint8_t padding[4] = {0xff, 0xff, 0xff, 0xff};
  1006. memcpy(padding, buffer + i, MIN(4, count-i));
  1007. result = kinetis_ftfx_command(bank->target, FTFx_CMD_LWORDPROG, kinfo->prog_base + offset + i,
  1008. padding[3], padding[2], padding[1], padding[0],
  1009. 0, 0, 0, 0, &ftfx_fstat);
  1010. if (result != ERROR_OK)
  1011. return ERROR_FLASH_OPERATION_FAILED;
  1012. }
  1013. }
  1014. } else {
  1015. LOG_ERROR("Flash write strategy not implemented");
  1016. return ERROR_FLASH_OPERATION_FAILED;
  1017. }
  1018. kinetis_invalidate_flash_cache(bank);
  1019. return ERROR_OK;
  1020. }
  1021. static int kinetis_probe(struct flash_bank *bank)
  1022. {
  1023. int result, i;
  1024. uint32_t offset = 0;
  1025. uint8_t fcfg1_nvmsize, fcfg1_pfsize, fcfg1_eesize, fcfg1_depart;
  1026. uint8_t fcfg2_maxaddr0, fcfg2_pflsh, fcfg2_maxaddr1;
  1027. uint32_t nvm_size = 0, pf_size = 0, df_size = 0, ee_size = 0;
  1028. unsigned num_blocks = 0, num_pflash_blocks = 0, num_nvm_blocks = 0, first_nvm_bank = 0,
  1029. pflash_sector_size_bytes = 0, nvm_sector_size_bytes = 0;
  1030. struct target *target = bank->target;
  1031. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  1032. kinfo->probed = false;
  1033. result = target_read_u32(target, SIM_SDID, &kinfo->sim_sdid);
  1034. if (result != ERROR_OK)
  1035. return result;
  1036. if ((kinfo->sim_sdid & (~KINETIS_SDID_K_SERIES_MASK)) == 0) {
  1037. /* older K-series MCU */
  1038. uint32_t mcu_type = kinfo->sim_sdid & KINETIS_K_SDID_TYPE_MASK;
  1039. switch (mcu_type) {
  1040. case KINETIS_K_SDID_K10_M50:
  1041. case KINETIS_K_SDID_K20_M50:
  1042. /* 1kB sectors */
  1043. pflash_sector_size_bytes = 1<<10;
  1044. nvm_sector_size_bytes = 1<<10;
  1045. num_blocks = 2;
  1046. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1047. break;
  1048. case KINETIS_K_SDID_K10_M72:
  1049. case KINETIS_K_SDID_K20_M72:
  1050. case KINETIS_K_SDID_K30_M72:
  1051. case KINETIS_K_SDID_K30_M100:
  1052. case KINETIS_K_SDID_K40_M72:
  1053. case KINETIS_K_SDID_K40_M100:
  1054. case KINETIS_K_SDID_K50_M72:
  1055. /* 2kB sectors, 1kB FlexNVM sectors */
  1056. pflash_sector_size_bytes = 2<<10;
  1057. nvm_sector_size_bytes = 1<<10;
  1058. num_blocks = 2;
  1059. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1060. kinfo->max_flash_prog_size = 1<<10;
  1061. break;
  1062. case KINETIS_K_SDID_K10_M100:
  1063. case KINETIS_K_SDID_K20_M100:
  1064. case KINETIS_K_SDID_K11:
  1065. case KINETIS_K_SDID_K12:
  1066. case KINETIS_K_SDID_K21_M50:
  1067. case KINETIS_K_SDID_K22_M50:
  1068. case KINETIS_K_SDID_K51_M72:
  1069. case KINETIS_K_SDID_K53:
  1070. case KINETIS_K_SDID_K60_M100:
  1071. /* 2kB sectors */
  1072. pflash_sector_size_bytes = 2<<10;
  1073. nvm_sector_size_bytes = 2<<10;
  1074. num_blocks = 2;
  1075. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1076. break;
  1077. case KINETIS_K_SDID_K21_M120:
  1078. case KINETIS_K_SDID_K22_M120:
  1079. /* 4kB sectors (MK21FN1M0, MK21FX512, MK22FN1M0, MK22FX512) */
  1080. pflash_sector_size_bytes = 4<<10;
  1081. kinfo->max_flash_prog_size = 1<<10;
  1082. nvm_sector_size_bytes = 4<<10;
  1083. num_blocks = 2;
  1084. kinfo->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1085. break;
  1086. case KINETIS_K_SDID_K10_M120:
  1087. case KINETIS_K_SDID_K20_M120:
  1088. case KINETIS_K_SDID_K60_M150:
  1089. case KINETIS_K_SDID_K70_M150:
  1090. /* 4kB sectors */
  1091. pflash_sector_size_bytes = 4<<10;
  1092. nvm_sector_size_bytes = 4<<10;
  1093. num_blocks = 4;
  1094. kinfo->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1095. break;
  1096. default:
  1097. LOG_ERROR("Unsupported K-family FAMID");
  1098. }
  1099. } else {
  1100. /* Newer K-series or KL series MCU */
  1101. switch (kinfo->sim_sdid & KINETIS_SDID_SERIESID_MASK) {
  1102. case KINETIS_SDID_SERIESID_K:
  1103. switch (kinfo->sim_sdid & (KINETIS_SDID_FAMILYID_MASK | KINETIS_SDID_SUBFAMID_MASK)) {
  1104. case KINETIS_SDID_FAMILYID_K0X | KINETIS_SDID_SUBFAMID_KX2:
  1105. /* K02FN64, K02FN128: FTFA, 2kB sectors */
  1106. pflash_sector_size_bytes = 2<<10;
  1107. num_blocks = 1;
  1108. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE;
  1109. break;
  1110. case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX2: {
  1111. /* MK24FN1M reports as K22, this should detect it (according to errata note 1N83J) */
  1112. uint32_t sopt1;
  1113. result = target_read_u32(target, SIM_SOPT1, &sopt1);
  1114. if (result != ERROR_OK)
  1115. return result;
  1116. if (((kinfo->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K24FN1M) &&
  1117. ((sopt1 & KINETIS_SOPT1_RAMSIZE_MASK) == KINETIS_SOPT1_RAMSIZE_K24FN1M)) {
  1118. /* MK24FN1M */
  1119. pflash_sector_size_bytes = 4<<10;
  1120. num_blocks = 2;
  1121. kinfo->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1122. kinfo->max_flash_prog_size = 1<<10;
  1123. break;
  1124. }
  1125. if ((kinfo->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K22FN128
  1126. || (kinfo->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K22FN256
  1127. || (kinfo->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K22FN512) {
  1128. /* K22 with new-style SDID - smaller pflash with FTFA, 2kB sectors */
  1129. pflash_sector_size_bytes = 2<<10;
  1130. /* autodetect 1 or 2 blocks */
  1131. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE;
  1132. break;
  1133. }
  1134. LOG_ERROR("Unsupported Kinetis K22 DIEID");
  1135. break;
  1136. }
  1137. case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX4:
  1138. pflash_sector_size_bytes = 4<<10;
  1139. if ((kinfo->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K24FN256) {
  1140. /* K24FN256 - smaller pflash with FTFA */
  1141. num_blocks = 1;
  1142. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE;
  1143. break;
  1144. }
  1145. /* K24FN1M without errata 7534 */
  1146. num_blocks = 2;
  1147. kinfo->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1148. kinfo->max_flash_prog_size = 1<<10;
  1149. break;
  1150. case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX3:
  1151. case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX1: /* errata 7534 - should be K63 */
  1152. /* K63FN1M0 */
  1153. case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX4:
  1154. case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX2: /* errata 7534 - should be K64 */
  1155. /* K64FN1M0, K64FX512 */
  1156. pflash_sector_size_bytes = 4<<10;
  1157. nvm_sector_size_bytes = 4<<10;
  1158. kinfo->max_flash_prog_size = 1<<10;
  1159. num_blocks = 2;
  1160. kinfo->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1161. break;
  1162. case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX6:
  1163. /* K26FN2M0 */
  1164. case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX6:
  1165. /* K66FN2M0, K66FX1M0 */
  1166. pflash_sector_size_bytes = 4<<10;
  1167. nvm_sector_size_bytes = 4<<10;
  1168. kinfo->max_flash_prog_size = 1<<10;
  1169. num_blocks = 4;
  1170. kinfo->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE;
  1171. break;
  1172. default:
  1173. LOG_ERROR("Unsupported Kinetis FAMILYID SUBFAMID");
  1174. }
  1175. break;
  1176. case KINETIS_SDID_SERIESID_KL:
  1177. /* KL-series */
  1178. pflash_sector_size_bytes = 1<<10;
  1179. nvm_sector_size_bytes = 1<<10;
  1180. /* autodetect 1 or 2 blocks */
  1181. kinfo->flash_support = FS_PROGRAM_LONGWORD;
  1182. break;
  1183. case KINETIS_SDID_SERIESID_KV:
  1184. /* KV-series */
  1185. switch (kinfo->sim_sdid & (KINETIS_SDID_FAMILYID_MASK | KINETIS_SDID_SUBFAMID_MASK)) {
  1186. case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX0:
  1187. /* KV10: FTFA, 1kB sectors */
  1188. pflash_sector_size_bytes = 1<<10;
  1189. num_blocks = 1;
  1190. kinfo->flash_support = FS_PROGRAM_LONGWORD;
  1191. break;
  1192. case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX1:
  1193. /* KV11: FTFA, 2kB sectors */
  1194. pflash_sector_size_bytes = 2<<10;
  1195. num_blocks = 1;
  1196. kinfo->flash_support = FS_PROGRAM_LONGWORD;
  1197. break;
  1198. case KINETIS_SDID_FAMILYID_K3X | KINETIS_SDID_SUBFAMID_KX0:
  1199. /* KV30: FTFA, 2kB sectors, 1 block */
  1200. case KINETIS_SDID_FAMILYID_K3X | KINETIS_SDID_SUBFAMID_KX1:
  1201. /* KV31: FTFA, 2kB sectors, 2 blocks */
  1202. pflash_sector_size_bytes = 2<<10;
  1203. /* autodetect 1 or 2 blocks */
  1204. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE;
  1205. break;
  1206. case KINETIS_SDID_FAMILYID_K4X | KINETIS_SDID_SUBFAMID_KX2:
  1207. case KINETIS_SDID_FAMILYID_K4X | KINETIS_SDID_SUBFAMID_KX4:
  1208. case KINETIS_SDID_FAMILYID_K4X | KINETIS_SDID_SUBFAMID_KX6:
  1209. /* KV4x: FTFA, 4kB sectors */
  1210. pflash_sector_size_bytes = 4<<10;
  1211. num_blocks = 1;
  1212. kinfo->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE;
  1213. break;
  1214. default:
  1215. LOG_ERROR("Unsupported KV FAMILYID SUBFAMID");
  1216. }
  1217. break;
  1218. default:
  1219. LOG_ERROR("Unsupported K-series");
  1220. }
  1221. }
  1222. if (pflash_sector_size_bytes == 0) {
  1223. LOG_ERROR("MCU is unsupported, SDID 0x%08" PRIx32, kinfo->sim_sdid);
  1224. return ERROR_FLASH_OPER_UNSUPPORTED;
  1225. }
  1226. result = target_read_u32(target, SIM_FCFG1, &kinfo->sim_fcfg1);
  1227. if (result != ERROR_OK)
  1228. return result;
  1229. result = target_read_u32(target, SIM_FCFG2, &kinfo->sim_fcfg2);
  1230. if (result != ERROR_OK)
  1231. return result;
  1232. LOG_DEBUG("SDID: 0x%08" PRIX32 " FCFG1: 0x%08" PRIX32 " FCFG2: 0x%08" PRIX32, kinfo->sim_sdid,
  1233. kinfo->sim_fcfg1, kinfo->sim_fcfg2);
  1234. fcfg1_nvmsize = (uint8_t)((kinfo->sim_fcfg1 >> 28) & 0x0f);
  1235. fcfg1_pfsize = (uint8_t)((kinfo->sim_fcfg1 >> 24) & 0x0f);
  1236. fcfg1_eesize = (uint8_t)((kinfo->sim_fcfg1 >> 16) & 0x0f);
  1237. fcfg1_depart = (uint8_t)((kinfo->sim_fcfg1 >> 8) & 0x0f);
  1238. fcfg2_pflsh = (uint8_t)((kinfo->sim_fcfg2 >> 23) & 0x01);
  1239. fcfg2_maxaddr0 = (uint8_t)((kinfo->sim_fcfg2 >> 24) & 0x7f);
  1240. fcfg2_maxaddr1 = (uint8_t)((kinfo->sim_fcfg2 >> 16) & 0x7f);
  1241. if (num_blocks == 0)
  1242. num_blocks = fcfg2_maxaddr1 ? 2 : 1;
  1243. else if (fcfg2_maxaddr1 == 0 && num_blocks >= 2) {
  1244. num_blocks = 1;
  1245. LOG_WARNING("MAXADDR1 is zero, number of flash banks adjusted to 1");
  1246. } else if (fcfg2_maxaddr1 != 0 && num_blocks == 1) {
  1247. num_blocks = 2;
  1248. LOG_WARNING("MAXADDR1 is non zero, number of flash banks adjusted to 2");
  1249. }
  1250. /* when the PFLSH bit is set, there is no FlexNVM/FlexRAM */
  1251. if (!fcfg2_pflsh) {
  1252. switch (fcfg1_nvmsize) {
  1253. case 0x03:
  1254. case 0x05:
  1255. case 0x07:
  1256. case 0x09:
  1257. case 0x0b:
  1258. nvm_size = 1 << (14 + (fcfg1_nvmsize >> 1));
  1259. break;
  1260. case 0x0f:
  1261. if (pflash_sector_size_bytes >= 4<<10)
  1262. nvm_size = 512<<10;
  1263. else
  1264. /* K20_100 */
  1265. nvm_size = 256<<10;
  1266. break;
  1267. default:
  1268. nvm_size = 0;
  1269. break;
  1270. }
  1271. switch (fcfg1_eesize) {
  1272. case 0x00:
  1273. case 0x01:
  1274. case 0x02:
  1275. case 0x03:
  1276. case 0x04:
  1277. case 0x05:
  1278. case 0x06:
  1279. case 0x07:
  1280. case 0x08:
  1281. case 0x09:
  1282. ee_size = (16 << (10 - fcfg1_eesize));
  1283. break;
  1284. default:
  1285. ee_size = 0;
  1286. break;
  1287. }
  1288. switch (fcfg1_depart) {
  1289. case 0x01:
  1290. case 0x02:
  1291. case 0x03:
  1292. case 0x04:
  1293. case 0x05:
  1294. case 0x06:
  1295. df_size = nvm_size - (4096 << fcfg1_depart);
  1296. break;
  1297. case 0x08:
  1298. df_size = 0;
  1299. break;
  1300. case 0x09:
  1301. case 0x0a:
  1302. case 0x0b:
  1303. case 0x0c:
  1304. case 0x0d:
  1305. df_size = 4096 << (fcfg1_depart & 0x7);
  1306. break;
  1307. default:
  1308. df_size = nvm_size;
  1309. break;
  1310. }
  1311. }
  1312. switch (fcfg1_pfsize) {
  1313. case 0x03:
  1314. case 0x05:
  1315. case 0x07:
  1316. case 0x09:
  1317. case 0x0b:
  1318. case 0x0d:
  1319. pf_size = 1 << (14 + (fcfg1_pfsize >> 1));
  1320. break;
  1321. case 0x0f:
  1322. /* a peculiar case: Freescale states different sizes for 0xf
  1323. * K02P64M100SFARM 128 KB ... duplicate of code 0x7
  1324. * K22P121M120SF8RM 256 KB ... duplicate of code 0x9
  1325. * K22P121M120SF7RM 512 KB ... duplicate of code 0xb
  1326. * K22P100M120SF5RM 1024 KB ... duplicate of code 0xd
  1327. * K26P169M180SF5RM 2048 KB ... the only unique value
  1328. * fcfg2_maxaddr0 seems to be the only clue to pf_size
  1329. * Checking fcfg2_maxaddr0 later in this routine is pointless then
  1330. */
  1331. if (fcfg2_pflsh)
  1332. pf_size = ((uint32_t)fcfg2_maxaddr0 << 13) * num_blocks;
  1333. else
  1334. pf_size = ((uint32_t)fcfg2_maxaddr0 << 13) * num_blocks / 2;
  1335. if (pf_size != 2048<<10)
  1336. LOG_WARNING("SIM_FCFG1 PFSIZE = 0xf: please check if pflash is %u KB", pf_size>>10);
  1337. break;
  1338. default:
  1339. pf_size = 0;
  1340. break;
  1341. }
  1342. LOG_DEBUG("FlexNVM: %" PRIu32 " PFlash: %" PRIu32 " FlexRAM: %" PRIu32 " PFLSH: %d",
  1343. nvm_size, pf_size, ee_size, fcfg2_pflsh);
  1344. num_pflash_blocks = num_blocks / (2 - fcfg2_pflsh);
  1345. first_nvm_bank = num_pflash_blocks;
  1346. num_nvm_blocks = num_blocks - num_pflash_blocks;
  1347. LOG_DEBUG("%d blocks total: %d PFlash, %d FlexNVM",
  1348. num_blocks, num_pflash_blocks, num_nvm_blocks);
  1349. LOG_INFO("Probing flash info for bank %d", bank->bank_number);
  1350. if ((unsigned)bank->bank_number < num_pflash_blocks) {
  1351. /* pflash, banks start at address zero */
  1352. kinfo->flash_class = FC_PFLASH;
  1353. bank->size = (pf_size / num_pflash_blocks);
  1354. bank->base = 0x00000000 + bank->size * bank->bank_number;
  1355. kinfo->prog_base = bank->base;
  1356. kinfo->sector_size = pflash_sector_size_bytes;
  1357. /* pflash is divided into 32 protection areas for
  1358. * parts with more than 32K of PFlash. For parts with
  1359. * less the protection unit is set to 1024 bytes */
  1360. kinfo->protection_size = MAX(pf_size / 32, 1024);
  1361. kinfo->protection_block = (32 / num_pflash_blocks) * bank->bank_number;
  1362. } else if ((unsigned)bank->bank_number < num_blocks) {
  1363. /* nvm, banks start at address 0x10000000 */
  1364. unsigned nvm_ord = bank->bank_number - first_nvm_bank;
  1365. uint32_t limit;
  1366. kinfo->flash_class = FC_FLEX_NVM;
  1367. bank->size = (nvm_size / num_nvm_blocks);
  1368. bank->base = 0x10000000 + bank->size * nvm_ord;
  1369. kinfo->prog_base = 0x00800000 + bank->size * nvm_ord;
  1370. kinfo->sector_size = nvm_sector_size_bytes;
  1371. if (df_size == 0) {
  1372. kinfo->protection_size = 0;
  1373. } else {
  1374. for (i = df_size; ~i & 1; i >>= 1)
  1375. ;
  1376. if (i == 1)
  1377. kinfo->protection_size = df_size / 8; /* data flash size = 2^^n */
  1378. else
  1379. kinfo->protection_size = nvm_size / 8; /* TODO: verify on SF1, not documented in RM */
  1380. }
  1381. kinfo->protection_block = (8 / num_nvm_blocks) * nvm_ord;
  1382. /* EEPROM backup part of FlexNVM is not accessible, use df_size as a limit */
  1383. if (df_size > bank->size * nvm_ord)
  1384. limit = df_size - bank->size * nvm_ord;
  1385. else
  1386. limit = 0;
  1387. if (bank->size > limit) {
  1388. bank->size = limit;
  1389. LOG_DEBUG("FlexNVM bank %d limited to 0x%08" PRIx32 " due to active EEPROM backup",
  1390. bank->bank_number, limit);
  1391. }
  1392. } else if ((unsigned)bank->bank_number == num_blocks) {
  1393. LOG_ERROR("FlexRAM support not yet implemented");
  1394. return ERROR_FLASH_OPER_UNSUPPORTED;
  1395. } else {
  1396. LOG_ERROR("Cannot determine parameters for bank %d, only %d banks on device",
  1397. bank->bank_number, num_blocks);
  1398. return ERROR_FLASH_BANK_INVALID;
  1399. }
  1400. if (bank->bank_number == 0 && ((uint32_t)fcfg2_maxaddr0 << 13) != bank->size)
  1401. LOG_WARNING("MAXADDR0 0x%02" PRIx8 " check failed,"
  1402. " please report to OpenOCD mailing list", fcfg2_maxaddr0);
  1403. if (fcfg2_pflsh) {
  1404. if (bank->bank_number == 1 && ((uint32_t)fcfg2_maxaddr1 << 13) != bank->size)
  1405. LOG_WARNING("MAXADDR1 0x%02" PRIx8 " check failed,"
  1406. " please report to OpenOCD mailing list", fcfg2_maxaddr1);
  1407. } else {
  1408. if ((unsigned)bank->bank_number == first_nvm_bank
  1409. && ((uint32_t)fcfg2_maxaddr1 << 13) != df_size)
  1410. LOG_WARNING("FlexNVM MAXADDR1 0x%02" PRIx8 " check failed,"
  1411. " please report to OpenOCD mailing list", fcfg2_maxaddr1);
  1412. }
  1413. if (bank->sectors) {
  1414. free(bank->sectors);
  1415. bank->sectors = NULL;
  1416. }
  1417. if (kinfo->sector_size == 0) {
  1418. LOG_ERROR("Unknown sector size for bank %d", bank->bank_number);
  1419. return ERROR_FLASH_BANK_INVALID;
  1420. }
  1421. if (kinfo->flash_support & FS_PROGRAM_SECTOR
  1422. && kinfo->max_flash_prog_size == 0) {
  1423. kinfo->max_flash_prog_size = kinfo->sector_size;
  1424. /* Program section size is equal to sector size by default */
  1425. }
  1426. bank->num_sectors = bank->size / kinfo->sector_size;
  1427. if (bank->num_sectors > 0) {
  1428. /* FlexNVM bank can be used for EEPROM backup therefore zero sized */
  1429. bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
  1430. for (i = 0; i < bank->num_sectors; i++) {
  1431. bank->sectors[i].offset = offset;
  1432. bank->sectors[i].size = kinfo->sector_size;
  1433. offset += kinfo->sector_size;
  1434. bank->sectors[i].is_erased = -1;
  1435. bank->sectors[i].is_protected = 1;
  1436. }
  1437. }
  1438. kinfo->probed = true;
  1439. return ERROR_OK;
  1440. }
  1441. static int kinetis_auto_probe(struct flash_bank *bank)
  1442. {
  1443. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  1444. if (kinfo && kinfo->probed)
  1445. return ERROR_OK;
  1446. return kinetis_probe(bank);
  1447. }
  1448. static int kinetis_info(struct flash_bank *bank, char *buf, int buf_size)
  1449. {
  1450. const char *bank_class_names[] = {
  1451. "(ANY)", "PFlash", "FlexNVM", "FlexRAM"
  1452. };
  1453. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  1454. (void) snprintf(buf, buf_size,
  1455. "%s driver for %s flash bank %s at 0x%8.8" PRIx32 "",
  1456. bank->driver->name, bank_class_names[kinfo->flash_class],
  1457. bank->name, bank->base);
  1458. return ERROR_OK;
  1459. }
  1460. static int kinetis_blank_check(struct flash_bank *bank)
  1461. {
  1462. struct kinetis_flash_bank *kinfo = bank->driver_priv;
  1463. int result;
  1464. /* suprisingly blank check does not work in VLPR and HSRUN modes */
  1465. result = kinetis_check_run_mode(bank->target);
  1466. if (result != ERROR_OK)
  1467. return result;
  1468. if (kinfo->flash_class == FC_PFLASH || kinfo->flash_class == FC_FLEX_NVM) {
  1469. bool block_dirty = false;
  1470. uint8_t ftfx_fstat;
  1471. if (kinfo->flash_class == FC_FLEX_NVM) {
  1472. uint8_t fcfg1_depart = (uint8_t)((kinfo->sim_fcfg1 >> 8) & 0x0f);
  1473. /* block operation cannot be used on FlexNVM when EEPROM backup partition is set */
  1474. if (fcfg1_depart != 0xf && fcfg1_depart != 0)
  1475. block_dirty = true;
  1476. }
  1477. if (!block_dirty) {
  1478. /* check if whole bank is blank */
  1479. result = kinetis_ftfx_command(bank->target, FTFx_CMD_BLOCKSTAT, kinfo->prog_base,
  1480. 0, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat);
  1481. if (result != ERROR_OK || (ftfx_fstat & 0x01))
  1482. block_dirty = true;
  1483. }
  1484. if (block_dirty) {
  1485. /* the whole bank is not erased, check sector-by-sector */
  1486. int i;
  1487. for (i = 0; i < bank->num_sectors; i++) {
  1488. /* normal margin */
  1489. result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTSTAT,
  1490. kinfo->prog_base + bank->sectors[i].offset,
  1491. 1, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat);
  1492. if (result == ERROR_OK) {
  1493. bank->sectors[i].is_erased = !(ftfx_fstat & 0x01);
  1494. } else {
  1495. LOG_DEBUG("Ignoring errored PFlash sector blank-check");
  1496. bank->sectors[i].is_erased = -1;
  1497. }
  1498. }
  1499. } else {
  1500. /* the whole bank is erased, update all sectors */
  1501. int i;
  1502. for (i = 0; i < bank->num_sectors; i++)
  1503. bank->sectors[i].is_erased = 1;
  1504. }
  1505. } else {
  1506. LOG_WARNING("kinetis_blank_check not supported yet for FlexRAM");
  1507. return ERROR_FLASH_OPERATION_FAILED;
  1508. }
  1509. return ERROR_OK;
  1510. }
  1511. COMMAND_HANDLER(kinetis_nvm_partition)
  1512. {
  1513. int result, i;
  1514. unsigned long par, log2 = 0, ee1 = 0, ee2 = 0;
  1515. enum { SHOW_INFO, DF_SIZE, EEBKP_SIZE } sz_type = SHOW_INFO;
  1516. bool enable;
  1517. uint8_t ftfx_fstat;
  1518. uint8_t load_flex_ram = 1;
  1519. uint8_t ee_size_code = 0x3f;
  1520. uint8_t flex_nvm_partition_code = 0;
  1521. uint8_t ee_split = 3;
  1522. struct target *target = get_current_target(CMD_CTX);
  1523. struct flash_bank *bank;
  1524. struct kinetis_flash_bank *kinfo;
  1525. uint32_t sim_fcfg1;
  1526. if (CMD_ARGC >= 2) {
  1527. if (strcmp(CMD_ARGV[0], "dataflash") == 0)
  1528. sz_type = DF_SIZE;
  1529. else if (strcmp(CMD_ARGV[0], "eebkp") == 0)
  1530. sz_type = EEBKP_SIZE;
  1531. par = strtoul(CMD_ARGV[1], NULL, 10);
  1532. while (par >> (log2 + 3))
  1533. log2++;
  1534. }
  1535. switch (sz_type) {
  1536. case SHOW_INFO:
  1537. result = target_read_u32(target, SIM_FCFG1, &sim_fcfg1);
  1538. if (result != ERROR_OK)
  1539. return result;
  1540. flex_nvm_partition_code = (uint8_t)((sim_fcfg1 >> 8) & 0x0f);
  1541. switch (flex_nvm_partition_code) {
  1542. case 0:
  1543. command_print(CMD_CTX, "No EEPROM backup, data flash only");
  1544. break;
  1545. case 1:
  1546. case 2:
  1547. case 3:
  1548. case 4:
  1549. case 5:
  1550. case 6:
  1551. command_print(CMD_CTX, "EEPROM backup %d KB", 4 << flex_nvm_partition_code);
  1552. break;
  1553. case 8:
  1554. command_print(CMD_CTX, "No data flash, EEPROM backup only");
  1555. break;
  1556. case 0x9:
  1557. case 0xA:
  1558. case 0xB:
  1559. case 0xC:
  1560. case 0xD:
  1561. case 0xE:
  1562. command_print(CMD_CTX, "data flash %d KB", 4 << (flex_nvm_partition_code & 7));
  1563. break;
  1564. case 0xf:
  1565. command_print(CMD_CTX, "No EEPROM backup, data flash only (DEPART not set)");
  1566. break;
  1567. default:
  1568. command_print(CMD_CTX, "Unsupported EEPROM backup size code 0x%02" PRIx8, flex_nvm_partition_code);
  1569. }
  1570. return ERROR_OK;
  1571. case DF_SIZE:
  1572. flex_nvm_partition_code = 0x8 | log2;
  1573. break;
  1574. case EEBKP_SIZE:
  1575. flex_nvm_partition_code = log2;
  1576. break;
  1577. }
  1578. if (CMD_ARGC == 3)
  1579. ee1 = ee2 = strtoul(CMD_ARGV[2], NULL, 10) / 2;
  1580. else if (CMD_ARGC >= 4) {
  1581. ee1 = strtoul(CMD_ARGV[2], NULL, 10);
  1582. ee2 = strtoul(CMD_ARGV[3], NULL, 10);
  1583. }
  1584. enable = ee1 + ee2 > 0;
  1585. if (enable) {
  1586. for (log2 = 2; ; log2++) {
  1587. if (ee1 + ee2 == (16u << 10) >> log2)
  1588. break;
  1589. if (ee1 + ee2 > (16u << 10) >> log2 || log2 >= 9) {
  1590. LOG_ERROR("Unsupported EEPROM size");
  1591. return ERROR_FLASH_OPERATION_FAILED;
  1592. }
  1593. }
  1594. if (ee1 * 3 == ee2)
  1595. ee_split = 1;
  1596. else if (ee1 * 7 == ee2)
  1597. ee_split = 0;
  1598. else if (ee1 != ee2) {
  1599. LOG_ERROR("Unsupported EEPROM sizes ratio");
  1600. return ERROR_FLASH_OPERATION_FAILED;
  1601. }
  1602. ee_size_code = log2 | ee_split << 4;
  1603. }
  1604. if (CMD_ARGC >= 5)
  1605. COMMAND_PARSE_ON_OFF(CMD_ARGV[4], enable);
  1606. if (enable)
  1607. load_flex_ram = 0;
  1608. LOG_INFO("DEPART 0x%" PRIx8 ", EEPROM size code 0x%" PRIx8,
  1609. flex_nvm_partition_code, ee_size_code);
  1610. result = kinetis_check_run_mode(target);
  1611. if (result != ERROR_OK)
  1612. return result;
  1613. result = kinetis_ftfx_command(target, FTFx_CMD_PGMPART, load_flex_ram,
  1614. ee_size_code, flex_nvm_partition_code, 0, 0,
  1615. 0, 0, 0, 0, &ftfx_fstat);
  1616. if (result != ERROR_OK)
  1617. return result;
  1618. command_print(CMD_CTX, "FlexNVM partition set. Please reset MCU.");
  1619. for (i = 1; i < 4; i++) {
  1620. bank = get_flash_bank_by_num_noprobe(i);
  1621. if (bank == NULL)
  1622. break;
  1623. kinfo = bank->driver_priv;
  1624. if (kinfo && kinfo->flash_class == FC_FLEX_NVM)
  1625. kinfo->probed = false; /* re-probe before next use */
  1626. }
  1627. command_print(CMD_CTX, "FlexNVM banks will be re-probed to set new data flash size.");
  1628. return ERROR_OK;
  1629. }
  1630. static const struct command_registration kinetis_security_command_handlers[] = {
  1631. {
  1632. .name = "check_security",
  1633. .mode = COMMAND_EXEC,
  1634. .help = "",
  1635. .usage = "",
  1636. .handler = kinetis_check_flash_security_status,
  1637. },
  1638. {
  1639. .name = "mass_erase",
  1640. .mode = COMMAND_EXEC,
  1641. .help = "",
  1642. .usage = "",
  1643. .handler = kinetis_mdm_mass_erase,
  1644. },
  1645. COMMAND_REGISTRATION_DONE
  1646. };
  1647. static const struct command_registration kinetis_exec_command_handlers[] = {
  1648. {
  1649. .name = "mdm",
  1650. .mode = COMMAND_ANY,
  1651. .help = "",
  1652. .usage = "",
  1653. .chain = kinetis_security_command_handlers,
  1654. },
  1655. {
  1656. .name = "disable_wdog",
  1657. .mode = COMMAND_EXEC,
  1658. .help = "Disable the watchdog timer",
  1659. .usage = "",
  1660. .handler = kinetis_disable_wdog_handler,
  1661. },
  1662. {
  1663. .name = "nvm_partition",
  1664. .mode = COMMAND_EXEC,
  1665. .help = "Show/set data flash or EEPROM backup size in kilobytes,"
  1666. " set two EEPROM sizes in bytes and FlexRAM loading during reset",
  1667. .usage = "('info'|'dataflash' size|'eebkp' size) [eesize1 eesize2] ['on'|'off']",
  1668. .handler = kinetis_nvm_partition,
  1669. },
  1670. COMMAND_REGISTRATION_DONE
  1671. };
  1672. static const struct command_registration kinetis_command_handler[] = {
  1673. {
  1674. .name = "kinetis",
  1675. .mode = COMMAND_ANY,
  1676. .help = "kinetis flash controller commands",
  1677. .usage = "",
  1678. .chain = kinetis_exec_command_handlers,
  1679. },
  1680. COMMAND_REGISTRATION_DONE
  1681. };
  1682. struct flash_driver kinetis_flash = {
  1683. .name = "kinetis",
  1684. .commands = kinetis_command_handler,
  1685. .flash_bank_command = kinetis_flash_bank_command,
  1686. .erase = kinetis_erase,
  1687. .protect = kinetis_protect,
  1688. .write = kinetis_write,
  1689. .read = default_flash_read,
  1690. .probe = kinetis_probe,
  1691. .auto_probe = kinetis_auto_probe,
  1692. .erase_check = kinetis_blank_check,
  1693. .protect_check = kinetis_protect_check,
  1694. .info = kinetis_info,
  1695. };