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.
 
 
 
 
 
 

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