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.
 
 
 
 
 
 

816 lines
23 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2008 by Spencer Oliver *
  6. * spen@spen-soft.co.uk *
  7. * *
  8. * Copyright (C) 2010 Øyvind Harboe *
  9. * oyvind.harboe@zylin.com *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  23. ***************************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "imp.h"
  28. #include <target/arm.h>
  29. #include <helper/binarybuffer.h>
  30. #include <target/algorithm.h>
  31. /* Flash registers */
  32. #define FLASH_CR0 0x00000000
  33. #define FLASH_CR1 0x00000004
  34. #define FLASH_DR0 0x00000008
  35. #define FLASH_DR1 0x0000000C
  36. #define FLASH_AR 0x00000010
  37. #define FLASH_ER 0x00000014
  38. #define FLASH_NVWPAR 0x0000DFB0
  39. #define FLASH_NVAPR0 0x0000DFB8
  40. #define FLASH_NVAPR1 0x0000DFBC
  41. /* FLASH_CR0 register bits */
  42. #define FLASH_WMS 0x80000000
  43. #define FLASH_SUSP 0x40000000
  44. #define FLASH_WPG 0x20000000
  45. #define FLASH_DWPG 0x10000000
  46. #define FLASH_SER 0x08000000
  47. #define FLASH_SPR 0x01000000
  48. #define FLASH_BER 0x04000000
  49. #define FLASH_MER 0x02000000
  50. #define FLASH_LOCK 0x00000010
  51. #define FLASH_BSYA1 0x00000004
  52. #define FLASH_BSYA0 0x00000002
  53. /* FLASH_CR1 register bits */
  54. #define FLASH_B1S 0x02000000
  55. #define FLASH_B0S 0x01000000
  56. #define FLASH_B1F1 0x00020000
  57. #define FLASH_B1F0 0x00010000
  58. #define FLASH_B0F7 0x00000080
  59. #define FLASH_B0F6 0x00000040
  60. #define FLASH_B0F5 0x00000020
  61. #define FLASH_B0F4 0x00000010
  62. #define FLASH_B0F3 0x00000008
  63. #define FLASH_B0F2 0x00000004
  64. #define FLASH_B0F1 0x00000002
  65. #define FLASH_B0F0 0x00000001
  66. /* FLASH_ER register bits */
  67. #define FLASH_WPF 0x00000100
  68. #define FLASH_RESER 0x00000080
  69. #define FLASH_SEQER 0x00000040
  70. #define FLASH_10ER 0x00000008
  71. #define FLASH_PGER 0x00000004
  72. #define FLASH_ERER 0x00000002
  73. #define FLASH_ERR 0x00000001
  74. struct str7x_flash_bank {
  75. uint32_t *sector_bits;
  76. uint32_t disable_bit;
  77. uint32_t busy_bits;
  78. uint32_t register_base;
  79. };
  80. struct str7x_mem_layout {
  81. uint32_t sector_start;
  82. uint32_t sector_size;
  83. uint32_t sector_bit;
  84. };
  85. enum str7x_status_codes {
  86. STR7X_CMD_SUCCESS = 0,
  87. STR7X_INVALID_COMMAND = 1,
  88. STR7X_SRC_ADDR_ERROR = 2,
  89. STR7X_DST_ADDR_ERROR = 3,
  90. STR7X_SRC_ADDR_NOT_MAPPED = 4,
  91. STR7X_DST_ADDR_NOT_MAPPED = 5,
  92. STR7X_COUNT_ERROR = 6,
  93. STR7X_INVALID_SECTOR = 7,
  94. STR7X_SECTOR_NOT_BLANK = 8,
  95. STR7X_SECTOR_NOT_PREPARED = 9,
  96. STR7X_COMPARE_ERROR = 10,
  97. STR7X_BUSY = 11
  98. };
  99. static const struct str7x_mem_layout mem_layout_str7bank0[] = {
  100. {0x00000000, 0x02000, 0x01},
  101. {0x00002000, 0x02000, 0x02},
  102. {0x00004000, 0x02000, 0x04},
  103. {0x00006000, 0x02000, 0x08},
  104. {0x00008000, 0x08000, 0x10},
  105. {0x00010000, 0x10000, 0x20},
  106. {0x00020000, 0x10000, 0x40},
  107. {0x00030000, 0x10000, 0x80}
  108. };
  109. static const struct str7x_mem_layout mem_layout_str7bank1[] = {
  110. {0x00000000, 0x02000, 0x10000},
  111. {0x00002000, 0x02000, 0x20000}
  112. };
  113. static int str7x_get_flash_adr(struct flash_bank *bank, uint32_t reg)
  114. {
  115. struct str7x_flash_bank *str7x_info = bank->driver_priv;
  116. return str7x_info->register_base | reg;
  117. }
  118. static int str7x_build_block_list(struct flash_bank *bank)
  119. {
  120. struct str7x_flash_bank *str7x_info = bank->driver_priv;
  121. int i;
  122. int num_sectors;
  123. int b0_sectors = 0, b1_sectors = 0;
  124. switch (bank->size) {
  125. case 16 * 1024:
  126. b1_sectors = 2;
  127. break;
  128. case 64 * 1024:
  129. b0_sectors = 5;
  130. break;
  131. case 128 * 1024:
  132. b0_sectors = 6;
  133. break;
  134. case 256 * 1024:
  135. b0_sectors = 8;
  136. break;
  137. default:
  138. LOG_ERROR("BUG: unknown bank->size encountered");
  139. exit(-1);
  140. }
  141. num_sectors = b0_sectors + b1_sectors;
  142. bank->num_sectors = num_sectors;
  143. bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
  144. str7x_info->sector_bits = malloc(sizeof(uint32_t) * num_sectors);
  145. num_sectors = 0;
  146. for (i = 0; i < b0_sectors; i++) {
  147. bank->sectors[num_sectors].offset = mem_layout_str7bank0[i].sector_start;
  148. bank->sectors[num_sectors].size = mem_layout_str7bank0[i].sector_size;
  149. bank->sectors[num_sectors].is_erased = -1;
  150. /* the reset_init handler marks all the sectors unprotected,
  151. * matching hardware after reset; keep the driver in sync
  152. */
  153. bank->sectors[num_sectors].is_protected = 0;
  154. str7x_info->sector_bits[num_sectors++] = mem_layout_str7bank0[i].sector_bit;
  155. }
  156. for (i = 0; i < b1_sectors; i++) {
  157. bank->sectors[num_sectors].offset = mem_layout_str7bank1[i].sector_start;
  158. bank->sectors[num_sectors].size = mem_layout_str7bank1[i].sector_size;
  159. bank->sectors[num_sectors].is_erased = -1;
  160. /* the reset_init handler marks all the sectors unprotected,
  161. * matching hardware after reset; keep the driver in sync
  162. */
  163. bank->sectors[num_sectors].is_protected = 0;
  164. str7x_info->sector_bits[num_sectors++] = mem_layout_str7bank1[i].sector_bit;
  165. }
  166. return ERROR_OK;
  167. }
  168. /* flash bank str7x <base> <size> 0 0 <target#> <str71_variant>
  169. */
  170. FLASH_BANK_COMMAND_HANDLER(str7x_flash_bank_command)
  171. {
  172. struct str7x_flash_bank *str7x_info;
  173. if (CMD_ARGC < 7)
  174. return ERROR_COMMAND_SYNTAX_ERROR;
  175. str7x_info = malloc(sizeof(struct str7x_flash_bank));
  176. bank->driver_priv = str7x_info;
  177. /* set default bits for str71x flash */
  178. str7x_info->busy_bits = (FLASH_LOCK | FLASH_BSYA1 | FLASH_BSYA0);
  179. str7x_info->disable_bit = (1 << 1);
  180. if (strcmp(CMD_ARGV[6], "STR71x") == 0)
  181. str7x_info->register_base = 0x40100000;
  182. else if (strcmp(CMD_ARGV[6], "STR73x") == 0) {
  183. str7x_info->register_base = 0x80100000;
  184. str7x_info->busy_bits = (FLASH_LOCK | FLASH_BSYA0);
  185. } else if (strcmp(CMD_ARGV[6], "STR75x") == 0) {
  186. str7x_info->register_base = 0x20100000;
  187. str7x_info->disable_bit = (1 << 0);
  188. } else {
  189. LOG_ERROR("unknown STR7x variant: '%s'", CMD_ARGV[6]);
  190. free(str7x_info);
  191. return ERROR_FLASH_BANK_INVALID;
  192. }
  193. str7x_build_block_list(bank);
  194. return ERROR_OK;
  195. }
  196. /* wait for flash to become idle or report errors.
  197. FIX!!! what's the maximum timeout??? The documentation doesn't
  198. state any maximum time.... by inspection it seems > 1000ms is to be
  199. expected.
  200. 10000ms is long enough that it should cover anything, yet not
  201. quite be equivalent to an infinite loop.
  202. */
  203. static int str7x_waitbusy(struct flash_bank *bank)
  204. {
  205. int err;
  206. int i;
  207. struct target *target = bank->target;
  208. struct str7x_flash_bank *str7x_info = bank->driver_priv;
  209. for (i = 0 ; i < 10000; i++) {
  210. uint32_t retval;
  211. err = target_read_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), &retval);
  212. if (err != ERROR_OK)
  213. return err;
  214. if ((retval & str7x_info->busy_bits) == 0)
  215. return ERROR_OK;
  216. alive_sleep(1);
  217. }
  218. LOG_ERROR("Timed out waiting for str7x flash");
  219. return ERROR_FAIL;
  220. }
  221. static int str7x_result(struct flash_bank *bank)
  222. {
  223. struct target *target = bank->target;
  224. uint32_t flash_flags;
  225. int retval;
  226. retval = target_read_u32(target, str7x_get_flash_adr(bank, FLASH_ER), &flash_flags);
  227. if (retval != ERROR_OK)
  228. return retval;
  229. if (flash_flags & FLASH_WPF) {
  230. LOG_ERROR("str7x hw write protection set");
  231. retval = ERROR_FAIL;
  232. }
  233. if (flash_flags & FLASH_RESER) {
  234. LOG_ERROR("str7x suspended program erase not resumed");
  235. retval = ERROR_FAIL;
  236. }
  237. if (flash_flags & FLASH_10ER) {
  238. LOG_ERROR("str7x trying to set bit to 1 when it is already 0");
  239. retval = ERROR_FAIL;
  240. }
  241. if (flash_flags & FLASH_PGER) {
  242. LOG_ERROR("str7x program error");
  243. retval = ERROR_FAIL;
  244. }
  245. if (flash_flags & FLASH_ERER) {
  246. LOG_ERROR("str7x erase error");
  247. retval = ERROR_FAIL;
  248. }
  249. if (retval == ERROR_OK) {
  250. if (flash_flags & FLASH_ERR) {
  251. /* this should always be set if one of the others are set... */
  252. LOG_ERROR("str7x write operation failed / bad setup");
  253. retval = ERROR_FAIL;
  254. }
  255. }
  256. return retval;
  257. }
  258. static int str7x_protect_check(struct flash_bank *bank)
  259. {
  260. struct str7x_flash_bank *str7x_info = bank->driver_priv;
  261. struct target *target = bank->target;
  262. int i;
  263. uint32_t flash_flags;
  264. if (bank->target->state != TARGET_HALTED) {
  265. LOG_ERROR("Target not halted");
  266. return ERROR_TARGET_NOT_HALTED;
  267. }
  268. int retval;
  269. retval = target_read_u32(target, str7x_get_flash_adr(bank, FLASH_NVWPAR), &flash_flags);
  270. if (retval != ERROR_OK)
  271. return retval;
  272. for (i = 0; i < bank->num_sectors; i++) {
  273. if (flash_flags & str7x_info->sector_bits[i])
  274. bank->sectors[i].is_protected = 0;
  275. else
  276. bank->sectors[i].is_protected = 1;
  277. }
  278. return ERROR_OK;
  279. }
  280. static int str7x_erase(struct flash_bank *bank, int first, int last)
  281. {
  282. struct str7x_flash_bank *str7x_info = bank->driver_priv;
  283. struct target *target = bank->target;
  284. int i;
  285. uint32_t cmd;
  286. uint32_t sectors = 0;
  287. int err;
  288. if (bank->target->state != TARGET_HALTED) {
  289. LOG_ERROR("Target not halted");
  290. return ERROR_TARGET_NOT_HALTED;
  291. }
  292. for (i = first; i <= last; i++)
  293. sectors |= str7x_info->sector_bits[i];
  294. LOG_DEBUG("sectors: 0x%" PRIx32 "", sectors);
  295. /* clear FLASH_ER register */
  296. err = target_write_u32(target, str7x_get_flash_adr(bank, FLASH_ER), 0x0);
  297. if (err != ERROR_OK)
  298. return err;
  299. cmd = FLASH_SER;
  300. err = target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), cmd);
  301. if (err != ERROR_OK)
  302. return err;
  303. cmd = sectors;
  304. err = target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR1), cmd);
  305. if (err != ERROR_OK)
  306. return err;
  307. cmd = FLASH_SER | FLASH_WMS;
  308. err = target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), cmd);
  309. if (err != ERROR_OK)
  310. return err;
  311. err = str7x_waitbusy(bank);
  312. if (err != ERROR_OK)
  313. return err;
  314. err = str7x_result(bank);
  315. if (err != ERROR_OK)
  316. return err;
  317. for (i = first; i <= last; i++)
  318. bank->sectors[i].is_erased = 1;
  319. return ERROR_OK;
  320. }
  321. static int str7x_protect(struct flash_bank *bank, int set, int first, int last)
  322. {
  323. struct str7x_flash_bank *str7x_info = bank->driver_priv;
  324. struct target *target = bank->target;
  325. int i;
  326. uint32_t cmd;
  327. uint32_t protect_blocks;
  328. if (bank->target->state != TARGET_HALTED) {
  329. LOG_ERROR("Target not halted");
  330. return ERROR_TARGET_NOT_HALTED;
  331. }
  332. protect_blocks = 0xFFFFFFFF;
  333. if (set) {
  334. for (i = first; i <= last; i++)
  335. protect_blocks &= ~(str7x_info->sector_bits[i]);
  336. }
  337. /* clear FLASH_ER register */
  338. int err;
  339. err = target_write_u32(target, str7x_get_flash_adr(bank, FLASH_ER), 0x0);
  340. if (err != ERROR_OK)
  341. return err;
  342. cmd = FLASH_SPR;
  343. err = target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), cmd);
  344. if (err != ERROR_OK)
  345. return err;
  346. cmd = str7x_get_flash_adr(bank, FLASH_NVWPAR);
  347. err = target_write_u32(target, str7x_get_flash_adr(bank, FLASH_AR), cmd);
  348. if (err != ERROR_OK)
  349. return err;
  350. cmd = protect_blocks;
  351. err = target_write_u32(target, str7x_get_flash_adr(bank, FLASH_DR0), cmd);
  352. if (err != ERROR_OK)
  353. return err;
  354. cmd = FLASH_SPR | FLASH_WMS;
  355. err = target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), cmd);
  356. if (err != ERROR_OK)
  357. return err;
  358. err = str7x_waitbusy(bank);
  359. if (err != ERROR_OK)
  360. return err;
  361. err = str7x_result(bank);
  362. if (err != ERROR_OK)
  363. return err;
  364. return ERROR_OK;
  365. }
  366. static int str7x_write_block(struct flash_bank *bank, const uint8_t *buffer,
  367. uint32_t offset, uint32_t count)
  368. {
  369. struct str7x_flash_bank *str7x_info = bank->driver_priv;
  370. struct target *target = bank->target;
  371. uint32_t buffer_size = 32768;
  372. struct working_area *write_algorithm;
  373. struct working_area *source;
  374. uint32_t address = bank->base + offset;
  375. struct reg_param reg_params[6];
  376. struct arm_algorithm arm_algo;
  377. int retval = ERROR_OK;
  378. /* see contib/loaders/flash/str7x.s for src */
  379. static const uint32_t str7x_flash_write_code[] = {
  380. /* write: */
  381. 0xe3a04201, /* mov r4, #0x10000000 */
  382. 0xe5824000, /* str r4, [r2, #0x0] */
  383. 0xe5821010, /* str r1, [r2, #0x10] */
  384. 0xe4904004, /* ldr r4, [r0], #4 */
  385. 0xe5824008, /* str r4, [r2, #0x8] */
  386. 0xe4904004, /* ldr r4, [r0], #4 */
  387. 0xe582400c, /* str r4, [r2, #0xc] */
  388. 0xe3a04209, /* mov r4, #0x90000000 */
  389. 0xe5824000, /* str r4, [r2, #0x0] */
  390. /* busy: */
  391. 0xe5924000, /* ldr r4, [r2, #0x0] */
  392. 0xe1140005, /* tst r4, r5 */
  393. 0x1afffffc, /* bne busy */
  394. 0xe5924014, /* ldr r4, [r2, #0x14] */
  395. 0xe31400ff, /* tst r4, #0xff */
  396. 0x03140c01, /* tsteq r4, #0x100 */
  397. 0x1a000002, /* bne exit */
  398. 0xe2811008, /* add r1, r1, #0x8 */
  399. 0xe2533001, /* subs r3, r3, #1 */
  400. 0x1affffec, /* bne write */
  401. /* exit: */
  402. 0xeafffffe, /* b exit */
  403. };
  404. /* flash write code */
  405. if (target_alloc_working_area_try(target, sizeof(str7x_flash_write_code),
  406. &write_algorithm) != ERROR_OK) {
  407. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  408. }
  409. uint8_t code[sizeof(str7x_flash_write_code)];
  410. target_buffer_set_u32_array(target, code, ARRAY_SIZE(str7x_flash_write_code),
  411. str7x_flash_write_code);
  412. target_write_buffer(target, write_algorithm->address, sizeof(code), code);
  413. /* memory buffer */
  414. while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK) {
  415. buffer_size /= 2;
  416. if (buffer_size <= 256) {
  417. /* we already allocated the writing code, but failed to get a
  418. * buffer, free the algorithm */
  419. target_free_working_area(target, write_algorithm);
  420. LOG_WARNING("no large enough working area available, can't do block memory writes");
  421. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  422. }
  423. }
  424. arm_algo.common_magic = ARM_COMMON_MAGIC;
  425. arm_algo.core_mode = ARM_MODE_SVC;
  426. arm_algo.core_state = ARM_STATE_ARM;
  427. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  428. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  429. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
  430. init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
  431. init_reg_param(&reg_params[4], "r4", 32, PARAM_IN);
  432. init_reg_param(&reg_params[5], "r5", 32, PARAM_OUT);
  433. while (count > 0) {
  434. uint32_t thisrun_count = (count > (buffer_size / 8)) ? (buffer_size / 8) : count;
  435. target_write_buffer(target, source->address, thisrun_count * 8, buffer);
  436. buf_set_u32(reg_params[0].value, 0, 32, source->address);
  437. buf_set_u32(reg_params[1].value, 0, 32, address);
  438. buf_set_u32(reg_params[2].value, 0, 32, str7x_get_flash_adr(bank, FLASH_CR0));
  439. buf_set_u32(reg_params[3].value, 0, 32, thisrun_count);
  440. buf_set_u32(reg_params[5].value, 0, 32, str7x_info->busy_bits);
  441. retval = target_run_algorithm(target, 0, NULL, 6, reg_params,
  442. write_algorithm->address,
  443. write_algorithm->address + (sizeof(str7x_flash_write_code) - 4),
  444. 10000, &arm_algo);
  445. if (retval != ERROR_OK)
  446. break;
  447. if (buf_get_u32(reg_params[4].value, 0, 32) != 0x00) {
  448. retval = str7x_result(bank);
  449. break;
  450. }
  451. buffer += thisrun_count * 8;
  452. address += thisrun_count * 8;
  453. count -= thisrun_count;
  454. }
  455. target_free_working_area(target, source);
  456. target_free_working_area(target, write_algorithm);
  457. destroy_reg_param(&reg_params[0]);
  458. destroy_reg_param(&reg_params[1]);
  459. destroy_reg_param(&reg_params[2]);
  460. destroy_reg_param(&reg_params[3]);
  461. destroy_reg_param(&reg_params[4]);
  462. destroy_reg_param(&reg_params[5]);
  463. return retval;
  464. }
  465. static int str7x_write(struct flash_bank *bank, const uint8_t *buffer,
  466. uint32_t offset, uint32_t count)
  467. {
  468. struct target *target = bank->target;
  469. uint32_t dwords_remaining = (count / 8);
  470. uint32_t bytes_remaining = (count & 0x00000007);
  471. uint32_t address = bank->base + offset;
  472. uint32_t bytes_written = 0;
  473. uint32_t cmd;
  474. int retval;
  475. uint32_t check_address = offset;
  476. int i;
  477. if (bank->target->state != TARGET_HALTED) {
  478. LOG_ERROR("Target not halted");
  479. return ERROR_TARGET_NOT_HALTED;
  480. }
  481. if (offset & 0x7) {
  482. LOG_WARNING("offset 0x%" PRIx32 " breaks required 8-byte alignment", offset);
  483. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  484. }
  485. for (i = 0; i < bank->num_sectors; i++) {
  486. uint32_t sec_start = bank->sectors[i].offset;
  487. uint32_t sec_end = sec_start + bank->sectors[i].size;
  488. /* check if destination falls within the current sector */
  489. if ((check_address >= sec_start) && (check_address < sec_end)) {
  490. /* check if destination ends in the current sector */
  491. if (offset + count < sec_end)
  492. check_address = offset + count;
  493. else
  494. check_address = sec_end;
  495. }
  496. }
  497. if (check_address != offset + count)
  498. return ERROR_FLASH_DST_OUT_OF_BANK;
  499. /* clear FLASH_ER register */
  500. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_ER), 0x0);
  501. /* multiple dwords (8-byte) to be programmed? */
  502. if (dwords_remaining > 0) {
  503. /* try using a block write */
  504. retval = str7x_write_block(bank, buffer, offset, dwords_remaining);
  505. if (retval != ERROR_OK) {
  506. if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
  507. /* if block write failed (no sufficient working area),
  508. * we use normal (slow) single dword accesses */
  509. LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
  510. } else {
  511. return retval;
  512. }
  513. } else {
  514. buffer += dwords_remaining * 8;
  515. address += dwords_remaining * 8;
  516. dwords_remaining = 0;
  517. }
  518. }
  519. while (dwords_remaining > 0) {
  520. /* command */
  521. cmd = FLASH_DWPG;
  522. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), cmd);
  523. /* address */
  524. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_AR), address);
  525. /* data word 1 */
  526. target_write_memory(target, str7x_get_flash_adr(bank, FLASH_DR0),
  527. 4, 1, buffer + bytes_written);
  528. bytes_written += 4;
  529. /* data word 2 */
  530. target_write_memory(target, str7x_get_flash_adr(bank, FLASH_DR1),
  531. 4, 1, buffer + bytes_written);
  532. bytes_written += 4;
  533. /* start programming cycle */
  534. cmd = FLASH_DWPG | FLASH_WMS;
  535. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), cmd);
  536. int err;
  537. err = str7x_waitbusy(bank);
  538. if (err != ERROR_OK)
  539. return err;
  540. err = str7x_result(bank);
  541. if (err != ERROR_OK)
  542. return err;
  543. dwords_remaining--;
  544. address += 8;
  545. }
  546. if (bytes_remaining) {
  547. uint8_t last_dword[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  548. /* copy the last remaining bytes into the write buffer */
  549. memcpy(last_dword, buffer+bytes_written, bytes_remaining);
  550. /* command */
  551. cmd = FLASH_DWPG;
  552. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), cmd);
  553. /* address */
  554. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_AR), address);
  555. /* data word 1 */
  556. target_write_memory(target, str7x_get_flash_adr(bank, FLASH_DR0),
  557. 4, 1, last_dword);
  558. /* data word 2 */
  559. target_write_memory(target, str7x_get_flash_adr(bank, FLASH_DR1),
  560. 4, 1, last_dword + 4);
  561. /* start programming cycle */
  562. cmd = FLASH_DWPG | FLASH_WMS;
  563. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), cmd);
  564. int err;
  565. err = str7x_waitbusy(bank);
  566. if (err != ERROR_OK)
  567. return err;
  568. err = str7x_result(bank);
  569. if (err != ERROR_OK)
  570. return err;
  571. }
  572. return ERROR_OK;
  573. }
  574. static int str7x_probe(struct flash_bank *bank)
  575. {
  576. return ERROR_OK;
  577. }
  578. #if 0
  579. COMMAND_HANDLER(str7x_handle_part_id_command)
  580. {
  581. return ERROR_OK;
  582. }
  583. #endif
  584. static int get_str7x_info(struct flash_bank *bank, char *buf, int buf_size)
  585. {
  586. /* Setting the write protection on a sector is a permanent change but it
  587. * can be disabled temporarily. FLASH_NVWPAR reflects the permanent
  588. * protection state of the sectors, not the temporary.
  589. */
  590. snprintf(buf, buf_size, "STR7x flash protection info is only valid after a power cycle, "
  591. "clearing the protection is only temporary and may not be reflected in the current "
  592. "info returned.");
  593. return ERROR_OK;
  594. }
  595. COMMAND_HANDLER(str7x_handle_disable_jtag_command)
  596. {
  597. struct target *target = NULL;
  598. struct str7x_flash_bank *str7x_info = NULL;
  599. uint32_t flash_cmd;
  600. uint16_t ProtectionLevel = 0;
  601. uint16_t ProtectionRegs;
  602. if (CMD_ARGC < 1)
  603. return ERROR_COMMAND_SYNTAX_ERROR;
  604. struct flash_bank *bank;
  605. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  606. if (ERROR_OK != retval)
  607. return retval;
  608. str7x_info = bank->driver_priv;
  609. target = bank->target;
  610. if (target->state != TARGET_HALTED) {
  611. LOG_ERROR("Target not halted");
  612. return ERROR_TARGET_NOT_HALTED;
  613. }
  614. /* first we get protection status */
  615. uint32_t reg;
  616. target_read_u32(target, str7x_get_flash_adr(bank, FLASH_NVAPR0), &reg);
  617. if (!(reg & str7x_info->disable_bit))
  618. ProtectionLevel = 1;
  619. target_read_u32(target, str7x_get_flash_adr(bank, FLASH_NVAPR1), &reg);
  620. ProtectionRegs = ~(reg >> 16);
  621. while (((ProtectionRegs) != 0) && (ProtectionLevel < 16)) {
  622. ProtectionRegs >>= 1;
  623. ProtectionLevel++;
  624. }
  625. if (ProtectionLevel == 0) {
  626. flash_cmd = FLASH_SPR;
  627. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), flash_cmd);
  628. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_AR), 0x4010DFB8);
  629. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_DR0), 0xFFFFFFFD);
  630. flash_cmd = FLASH_SPR | FLASH_WMS;
  631. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), flash_cmd);
  632. } else {
  633. flash_cmd = FLASH_SPR;
  634. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), flash_cmd);
  635. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_AR), 0x4010DFBC);
  636. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_DR0),
  637. ~(1 << (15 + ProtectionLevel)));
  638. flash_cmd = FLASH_SPR | FLASH_WMS;
  639. target_write_u32(target, str7x_get_flash_adr(bank, FLASH_CR0), flash_cmd);
  640. }
  641. return ERROR_OK;
  642. }
  643. static const struct command_registration str7x_exec_command_handlers[] = {
  644. {
  645. .name = "disable_jtag",
  646. .usage = "<bank>",
  647. .handler = str7x_handle_disable_jtag_command,
  648. .mode = COMMAND_EXEC,
  649. .help = "disable jtag access",
  650. },
  651. COMMAND_REGISTRATION_DONE
  652. };
  653. static const struct command_registration str7x_command_handlers[] = {
  654. {
  655. .name = "str7x",
  656. .mode = COMMAND_ANY,
  657. .help = "str7x flash command group",
  658. .usage = "",
  659. .chain = str7x_exec_command_handlers,
  660. },
  661. COMMAND_REGISTRATION_DONE
  662. };
  663. struct flash_driver str7x_flash = {
  664. .name = "str7x",
  665. .commands = str7x_command_handlers,
  666. .flash_bank_command = str7x_flash_bank_command,
  667. .erase = str7x_erase,
  668. .protect = str7x_protect,
  669. .write = str7x_write,
  670. .read = default_flash_read,
  671. .probe = str7x_probe,
  672. .auto_probe = str7x_probe,
  673. .erase_check = default_flash_blank_check,
  674. .protect_check = str7x_protect_check,
  675. .info = get_str7x_info,
  676. };