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.
 
 
 
 
 
 

975 lines
25 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) 2011 by Erik Botö
  9. * erik.boto@pelagicore.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, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "imp.h"
  30. #include <helper/binarybuffer.h>
  31. #include <target/algorithm.h>
  32. #include <target/armv7m.h>
  33. /* em357 register locations */
  34. #define EM357_FLASH_ACR 0x40008000
  35. #define EM357_FLASH_KEYR 0x40008004
  36. #define EM357_FLASH_OPTKEYR 0x40008008
  37. #define EM357_FLASH_SR 0x4000800C
  38. #define EM357_FLASH_CR 0x40008010
  39. #define EM357_FLASH_AR 0x40008014
  40. #define EM357_FLASH_OBR 0x4000801C
  41. #define EM357_FLASH_WRPR 0x40008020
  42. #define EM357_FPEC_CLK 0x4000402c
  43. /* option byte location */
  44. #define EM357_OB_RDP 0x08040800
  45. #define EM357_OB_WRP0 0x08040808
  46. #define EM357_OB_WRP1 0x0804080A
  47. #define EM357_OB_WRP2 0x0804080C
  48. /* FLASH_CR register bits */
  49. #define FLASH_PG (1 << 0)
  50. #define FLASH_PER (1 << 1)
  51. #define FLASH_MER (1 << 2)
  52. #define FLASH_OPTPG (1 << 4)
  53. #define FLASH_OPTER (1 << 5)
  54. #define FLASH_STRT (1 << 6)
  55. #define FLASH_LOCK (1 << 7)
  56. #define FLASH_OPTWRE (1 << 9)
  57. /* FLASH_SR register bits */
  58. #define FLASH_BSY (1 << 0)
  59. #define FLASH_PGERR (1 << 2)
  60. #define FLASH_WRPRTERR (1 << 4)
  61. #define FLASH_EOP (1 << 5)
  62. /* EM357_FLASH_OBR bit definitions (reading) */
  63. #define OPT_ERROR 0
  64. #define OPT_READOUT 1
  65. /* register unlock keys */
  66. #define KEY1 0x45670123
  67. #define KEY2 0xCDEF89AB
  68. struct em357_options
  69. {
  70. uint16_t RDP;
  71. uint16_t user_options;
  72. uint16_t protection[3];
  73. };
  74. struct em357_flash_bank
  75. {
  76. struct em357_options option_bytes;
  77. struct working_area *write_algorithm;
  78. int ppage_size;
  79. int probed;
  80. };
  81. static int em357_mass_erase(struct flash_bank *bank);
  82. /* flash bank em357 <base> <size> 0 0 <target#>
  83. */
  84. FLASH_BANK_COMMAND_HANDLER(em357_flash_bank_command)
  85. {
  86. struct em357_flash_bank *em357_info;
  87. if (CMD_ARGC < 6)
  88. {
  89. LOG_WARNING("incomplete flash_bank em357 configuration");
  90. return ERROR_FLASH_BANK_INVALID;
  91. }
  92. em357_info = malloc(sizeof(struct em357_flash_bank));
  93. bank->driver_priv = em357_info;
  94. em357_info->write_algorithm = NULL;
  95. em357_info->probed = 0;
  96. return ERROR_OK;
  97. }
  98. static inline int em357_get_flash_status(struct flash_bank *bank, uint32_t *status)
  99. {
  100. struct target *target = bank->target;
  101. return target_read_u32(target, EM357_FLASH_SR, status);
  102. }
  103. static int em357_wait_status_busy(struct flash_bank *bank, int timeout)
  104. {
  105. struct target *target = bank->target;
  106. uint32_t status;
  107. int retval = ERROR_OK;
  108. /* wait for busy to clear */
  109. for (;;)
  110. {
  111. retval = em357_get_flash_status(bank, &status);
  112. if (retval != ERROR_OK)
  113. return retval;
  114. LOG_DEBUG("status: 0x%" PRIx32 "", status);
  115. if ((status & FLASH_BSY) == 0)
  116. break;
  117. if (timeout-- <= 0)
  118. {
  119. LOG_ERROR("timed out waiting for flash");
  120. return ERROR_FAIL;
  121. }
  122. alive_sleep(1);
  123. }
  124. if (status & FLASH_WRPRTERR)
  125. {
  126. LOG_ERROR("em357 device protected");
  127. retval = ERROR_FAIL;
  128. }
  129. if (status & FLASH_PGERR)
  130. {
  131. LOG_ERROR("em357 device programming failed");
  132. retval = ERROR_FAIL;
  133. }
  134. /* Clear but report errors */
  135. if (status & (FLASH_WRPRTERR | FLASH_PGERR))
  136. {
  137. /* If this operation fails, we ignore it and report the original
  138. * retval
  139. */
  140. target_write_u32(target, EM357_FLASH_SR, FLASH_WRPRTERR | FLASH_PGERR);
  141. }
  142. return retval;
  143. }
  144. static int em357_read_options(struct flash_bank *bank)
  145. {
  146. uint32_t optiondata;
  147. struct em357_flash_bank *em357_info = NULL;
  148. struct target *target = bank->target;
  149. em357_info = bank->driver_priv;
  150. /* read current option bytes */
  151. int retval = target_read_u32(target, EM357_FLASH_OBR, &optiondata);
  152. if (retval != ERROR_OK)
  153. return retval;
  154. em357_info->option_bytes.user_options = (uint16_t)0xFFFC | ((optiondata >> 2) & 0x03);
  155. em357_info->option_bytes.RDP = (optiondata & (1 << OPT_READOUT)) ? 0xFFFF : 0x5AA5;
  156. if (optiondata & (1 << OPT_READOUT))
  157. LOG_INFO("Device Security Bit Set");
  158. /* each bit refers to a 4bank protection */
  159. retval = target_read_u32(target, EM357_FLASH_WRPR, &optiondata);
  160. if (retval != ERROR_OK)
  161. return retval;
  162. em357_info->option_bytes.protection[0] = (uint16_t)optiondata;
  163. em357_info->option_bytes.protection[1] = (uint16_t)(optiondata >> 8);
  164. em357_info->option_bytes.protection[2] = (uint16_t)(optiondata >> 16);
  165. return ERROR_OK;
  166. }
  167. static int em357_erase_options(struct flash_bank *bank)
  168. {
  169. struct em357_flash_bank *em357_info = NULL;
  170. struct target *target = bank->target;
  171. em357_info = bank->driver_priv;
  172. /* read current options */
  173. em357_read_options(bank);
  174. /* unlock flash registers */
  175. int retval = target_write_u32(target, EM357_FLASH_KEYR, KEY1);
  176. if (retval != ERROR_OK)
  177. return retval;
  178. retval = target_write_u32(target, EM357_FLASH_KEYR, KEY2);
  179. if (retval != ERROR_OK)
  180. return retval;
  181. /* unlock option flash registers */
  182. retval = target_write_u32(target, EM357_FLASH_OPTKEYR, KEY1);
  183. if (retval != ERROR_OK)
  184. return retval;
  185. retval = target_write_u32(target, EM357_FLASH_OPTKEYR, KEY2);
  186. if (retval != ERROR_OK)
  187. return retval;
  188. /* erase option bytes */
  189. retval = target_write_u32(target, EM357_FLASH_CR, FLASH_OPTER | FLASH_OPTWRE);
  190. if (retval != ERROR_OK)
  191. return retval;
  192. retval = target_write_u32(target, EM357_FLASH_CR, FLASH_OPTER | FLASH_STRT | FLASH_OPTWRE);
  193. if (retval != ERROR_OK)
  194. return retval;
  195. retval = em357_wait_status_busy(bank, 10);
  196. if (retval != ERROR_OK)
  197. return retval;
  198. /* clear readout protection and complementary option bytes
  199. * this will also force a device unlock if set */
  200. em357_info->option_bytes.RDP = 0x5AA5;
  201. return ERROR_OK;
  202. }
  203. static int em357_write_options(struct flash_bank *bank)
  204. {
  205. struct em357_flash_bank *em357_info = NULL;
  206. struct target *target = bank->target;
  207. em357_info = bank->driver_priv;
  208. /* unlock flash registers */
  209. int retval = target_write_u32(target, EM357_FLASH_KEYR, KEY1);
  210. if (retval != ERROR_OK)
  211. return retval;
  212. retval = target_write_u32(target, EM357_FLASH_KEYR, KEY2);
  213. if (retval != ERROR_OK)
  214. return retval;
  215. /* unlock option flash registers */
  216. retval = target_write_u32(target, EM357_FLASH_OPTKEYR, KEY1);
  217. if (retval != ERROR_OK)
  218. return retval;
  219. retval = target_write_u32(target, EM357_FLASH_OPTKEYR, KEY2);
  220. if (retval != ERROR_OK)
  221. return retval;
  222. /* program option bytes */
  223. retval = target_write_u32(target, EM357_FLASH_CR, FLASH_OPTPG | FLASH_OPTWRE);
  224. if (retval != ERROR_OK)
  225. return retval;
  226. retval = em357_wait_status_busy(bank, 10);
  227. if (retval != ERROR_OK)
  228. return retval;
  229. /* write protection byte 1 */
  230. retval = target_write_u16(target, EM357_OB_WRP0, em357_info->option_bytes.protection[0]);
  231. if (retval != ERROR_OK)
  232. return retval;
  233. retval = em357_wait_status_busy(bank, 10);
  234. if (retval != ERROR_OK)
  235. return retval;
  236. /* write protection byte 2 */
  237. retval = target_write_u16(target, EM357_OB_WRP1, em357_info->option_bytes.protection[1]);
  238. if (retval != ERROR_OK)
  239. return retval;
  240. retval = em357_wait_status_busy(bank, 10);
  241. if (retval != ERROR_OK)
  242. return retval;
  243. /* write protection byte 3 */
  244. retval = target_write_u16(target, EM357_OB_WRP2, em357_info->option_bytes.protection[2]);
  245. if (retval != ERROR_OK)
  246. return retval;
  247. retval = em357_wait_status_busy(bank, 10);
  248. if (retval != ERROR_OK)
  249. return retval;
  250. /* write readout protection bit */
  251. retval = target_write_u16(target, EM357_OB_RDP, em357_info->option_bytes.RDP);
  252. if (retval != ERROR_OK)
  253. return retval;
  254. retval = em357_wait_status_busy(bank, 10);
  255. if (retval != ERROR_OK)
  256. return retval;
  257. retval = target_write_u32(target, EM357_FLASH_CR, FLASH_LOCK);
  258. if (retval != ERROR_OK)
  259. return retval;
  260. return ERROR_OK;
  261. }
  262. static int em357_protect_check(struct flash_bank *bank)
  263. {
  264. struct target *target = bank->target;
  265. struct em357_flash_bank *em357_info = bank->driver_priv;
  266. uint32_t protection;
  267. int i, s;
  268. int num_bits;
  269. int set;
  270. if (target->state != TARGET_HALTED)
  271. {
  272. LOG_ERROR("Target not halted");
  273. return ERROR_TARGET_NOT_HALTED;
  274. }
  275. /* each bit refers to a 4bank protection (bit 0-23) */
  276. int retval = target_read_u32(target, EM357_FLASH_WRPR, &protection);
  277. if (retval != ERROR_OK)
  278. return retval;
  279. /* each protection bit is for 4 * 2K pages */
  280. num_bits = (bank->num_sectors / em357_info->ppage_size);
  281. for (i = 0; i < num_bits; i++)
  282. {
  283. set = 1;
  284. if (protection & (1 << i))
  285. set = 0;
  286. for (s = 0; s < em357_info->ppage_size; s++)
  287. bank->sectors[(i * em357_info->ppage_size) + s].is_protected = set;
  288. }
  289. return ERROR_OK;
  290. }
  291. static int em357_erase(struct flash_bank *bank, int first, int last)
  292. {
  293. struct target *target = bank->target;
  294. int i;
  295. if (bank->target->state != TARGET_HALTED)
  296. {
  297. LOG_ERROR("Target not halted");
  298. return ERROR_TARGET_NOT_HALTED;
  299. }
  300. if ((first == 0) && (last == (bank->num_sectors - 1)))
  301. {
  302. return em357_mass_erase(bank);
  303. }
  304. /* unlock flash registers */
  305. int retval = target_write_u32(target, EM357_FLASH_KEYR, KEY1);
  306. if (retval != ERROR_OK)
  307. return retval;
  308. retval = target_write_u32(target, EM357_FLASH_KEYR, KEY2);
  309. if (retval != ERROR_OK)
  310. return retval;
  311. for (i = first; i <= last; i++)
  312. {
  313. retval = target_write_u32(target, EM357_FLASH_CR, FLASH_PER);
  314. if (retval != ERROR_OK)
  315. return retval;
  316. retval = target_write_u32(target, EM357_FLASH_AR,
  317. bank->base + bank->sectors[i].offset);
  318. if (retval != ERROR_OK)
  319. return retval;
  320. retval = target_write_u32(target, EM357_FLASH_CR, FLASH_PER | FLASH_STRT);
  321. if (retval != ERROR_OK)
  322. return retval;
  323. retval = em357_wait_status_busy(bank, 100);
  324. if (retval != ERROR_OK)
  325. return retval;
  326. bank->sectors[i].is_erased = 1;
  327. }
  328. retval = target_write_u32(target, EM357_FLASH_CR, FLASH_LOCK);
  329. if (retval != ERROR_OK)
  330. return retval;
  331. return ERROR_OK;
  332. }
  333. static int em357_protect(struct flash_bank *bank, int set, int first, int last)
  334. {
  335. struct em357_flash_bank *em357_info = NULL;
  336. struct target *target = bank->target;
  337. uint16_t prot_reg[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
  338. int i, reg, bit;
  339. int status;
  340. uint32_t protection;
  341. em357_info = bank->driver_priv;
  342. if (target->state != TARGET_HALTED)
  343. {
  344. LOG_ERROR("Target not halted");
  345. return ERROR_TARGET_NOT_HALTED;
  346. }
  347. if ((first % em357_info->ppage_size) != 0)
  348. {
  349. LOG_WARNING("aligned start protect sector to a %d sector boundary",
  350. em357_info->ppage_size);
  351. first = first - (first % em357_info->ppage_size);
  352. }
  353. if (((last + 1) % em357_info->ppage_size) != 0)
  354. {
  355. LOG_WARNING("aligned end protect sector to a %d sector boundary",
  356. em357_info->ppage_size);
  357. last++;
  358. last = last - (last % em357_info->ppage_size);
  359. last--;
  360. }
  361. /* each bit refers to a 4bank protection */
  362. int retval = target_read_u32(target, EM357_FLASH_WRPR, &protection);
  363. if (retval != ERROR_OK)
  364. return retval;
  365. prot_reg[0] = (uint16_t)protection;
  366. prot_reg[1] = (uint16_t)(protection >> 8);
  367. prot_reg[2] = (uint16_t)(protection >> 16);
  368. for (i = first; i <= last; i++)
  369. {
  370. reg = (i / em357_info->ppage_size) / 8;
  371. bit = (i / em357_info->ppage_size) - (reg * 8);
  372. LOG_WARNING("reg, bit: %d, %d", reg, bit);
  373. if (set)
  374. prot_reg[reg] &= ~(1 << bit);
  375. else
  376. prot_reg[reg] |= (1 << bit);
  377. }
  378. if ((status = em357_erase_options(bank)) != ERROR_OK)
  379. return status;
  380. em357_info->option_bytes.protection[0] = prot_reg[0];
  381. em357_info->option_bytes.protection[1] = prot_reg[1];
  382. em357_info->option_bytes.protection[2] = prot_reg[2];
  383. return em357_write_options(bank);
  384. }
  385. static int em357_write_block(struct flash_bank *bank, uint8_t *buffer,
  386. uint32_t offset, uint32_t count)
  387. {
  388. struct em357_flash_bank *em357_info = bank->driver_priv;
  389. struct target *target = bank->target;
  390. uint32_t buffer_size = 16384;
  391. struct working_area *source;
  392. uint32_t address = bank->base + offset;
  393. struct reg_param reg_params[4];
  394. struct armv7m_algorithm armv7m_info;
  395. int retval = ERROR_OK;
  396. /* see contib/loaders/flash/stm32x.s for src, the same is used here except for
  397. * a modified *_FLASH_BASE */
  398. static const uint8_t em357_flash_write_code[] = {
  399. /* #define EM357_FLASH_CR_OFFSET 0x10 */
  400. /* #define EM357_FLASH_SR_OFFSET 0x0C */
  401. /* write: */
  402. 0x08, 0x4c, /* ldr r4, EM357_FLASH_BASE */
  403. 0x1c, 0x44, /* add r4, r3 */
  404. /* write_half_word: */
  405. 0x01, 0x23, /* movs r3, #0x01 */
  406. 0x23, 0x61, /* str r3, [r4, #EM357_FLASH_CR_OFFSET] */
  407. 0x30, 0xf8, 0x02, 0x3b, /* ldrh r3, [r0], #0x02 */
  408. 0x21, 0xf8, 0x02, 0x3b, /* strh r3, [r1], #0x02 */
  409. /* busy: */
  410. 0xe3, 0x68, /* ldr r3, [r4, #EM357_FLASH_SR_OFFSET] */
  411. 0x13, 0xf0, 0x01, 0x0f, /* tst r3, #0x01 */
  412. 0xfb, 0xd0, /* beq busy */
  413. 0x13, 0xf0, 0x14, 0x0f, /* tst r3, #0x14 */
  414. 0x01, 0xd1, /* bne exit */
  415. 0x01, 0x3a, /* subs r2, r2, #0x01 */
  416. 0xf0, 0xd1, /* bne write_half_word */
  417. /* exit: */
  418. 0x00, 0xbe, /* bkpt #0x00 */
  419. 0x00, 0x80, 0x00, 0x40, /* EM357_FLASH_BASE: .word 0x40008000 */
  420. };
  421. /* flash write code */
  422. if (target_alloc_working_area(target, sizeof(em357_flash_write_code),
  423. &em357_info->write_algorithm) != ERROR_OK)
  424. {
  425. LOG_WARNING("no working area available, can't do block memory writes");
  426. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  427. };
  428. if ((retval = target_write_buffer(target, em357_info->write_algorithm->address,
  429. sizeof(em357_flash_write_code),
  430. (uint8_t*)em357_flash_write_code)) != ERROR_OK)
  431. return retval;
  432. /* memory buffer */
  433. while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK)
  434. {
  435. buffer_size /= 2;
  436. if (buffer_size <= 256)
  437. {
  438. /* if we already allocated the writing code, but failed to get a
  439. * buffer, free the algorithm */
  440. if (em357_info->write_algorithm)
  441. target_free_working_area(target, em357_info->write_algorithm);
  442. LOG_WARNING("no large enough working area available, can't do block memory writes");
  443. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  444. }
  445. };
  446. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  447. armv7m_info.core_mode = ARMV7M_MODE_ANY;
  448. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  449. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  450. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
  451. init_reg_param(&reg_params[3], "r3", 32, PARAM_IN_OUT);
  452. while (count > 0)
  453. {
  454. uint32_t thisrun_count = (count > (buffer_size / 2)) ?
  455. (buffer_size / 2) : count;
  456. if ((retval = target_write_buffer(target, source->address,
  457. thisrun_count * 2, buffer)) != ERROR_OK)
  458. break;
  459. buf_set_u32(reg_params[0].value, 0, 32, source->address);
  460. buf_set_u32(reg_params[1].value, 0, 32, address);
  461. buf_set_u32(reg_params[2].value, 0, 32, thisrun_count);
  462. buf_set_u32(reg_params[3].value, 0, 32, 0);
  463. if ((retval = target_run_algorithm(target, 0, NULL, 4, reg_params,
  464. em357_info->write_algorithm->address,
  465. 0,
  466. 10000, &armv7m_info)) != ERROR_OK)
  467. {
  468. LOG_ERROR("error executing em357 flash write algorithm");
  469. break;
  470. }
  471. if (buf_get_u32(reg_params[3].value, 0, 32) & FLASH_PGERR)
  472. {
  473. LOG_ERROR("flash memory not erased before writing");
  474. /* Clear but report errors */
  475. target_write_u32(target, EM357_FLASH_SR, FLASH_PGERR);
  476. retval = ERROR_FAIL;
  477. break;
  478. }
  479. if (buf_get_u32(reg_params[3].value, 0, 32) & FLASH_WRPRTERR)
  480. {
  481. LOG_ERROR("flash memory write protected");
  482. /* Clear but report errors */
  483. target_write_u32(target, EM357_FLASH_SR, FLASH_WRPRTERR);
  484. retval = ERROR_FAIL;
  485. break;
  486. }
  487. buffer += thisrun_count * 2;
  488. address += thisrun_count * 2;
  489. count -= thisrun_count;
  490. }
  491. target_free_working_area(target, source);
  492. target_free_working_area(target, em357_info->write_algorithm);
  493. destroy_reg_param(&reg_params[0]);
  494. destroy_reg_param(&reg_params[1]);
  495. destroy_reg_param(&reg_params[2]);
  496. destroy_reg_param(&reg_params[3]);
  497. return retval;
  498. }
  499. static int em357_write(struct flash_bank *bank, uint8_t *buffer,
  500. uint32_t offset, uint32_t count)
  501. {
  502. struct target *target = bank->target;
  503. uint32_t words_remaining = (count / 2);
  504. uint32_t bytes_remaining = (count & 0x00000001);
  505. uint32_t address = bank->base + offset;
  506. uint32_t bytes_written = 0;
  507. int retval;
  508. if (bank->target->state != TARGET_HALTED)
  509. {
  510. LOG_ERROR("Target not halted");
  511. return ERROR_TARGET_NOT_HALTED;
  512. }
  513. if (offset & 0x1)
  514. {
  515. LOG_WARNING("offset 0x%" PRIx32 " breaks required 2-byte alignment", offset);
  516. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  517. }
  518. /* unlock flash registers */
  519. retval = target_write_u32(target, EM357_FLASH_KEYR, KEY1);
  520. if (retval != ERROR_OK)
  521. return retval;
  522. retval = target_write_u32(target, EM357_FLASH_KEYR, KEY2);
  523. if (retval != ERROR_OK)
  524. return retval;
  525. /* multiple half words (2-byte) to be programmed? */
  526. if (words_remaining > 0)
  527. {
  528. /* try using a block write */
  529. if ((retval = em357_write_block(bank, buffer, offset, words_remaining)) != ERROR_OK)
  530. {
  531. if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
  532. {
  533. /* if block write failed (no sufficient working area),
  534. * we use normal (slow) single dword accesses */
  535. LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
  536. }
  537. }
  538. else
  539. {
  540. buffer += words_remaining * 2;
  541. address += words_remaining * 2;
  542. words_remaining = 0;
  543. }
  544. }
  545. if ((retval != ERROR_OK) && (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE))
  546. return retval;
  547. while (words_remaining > 0)
  548. {
  549. uint16_t value;
  550. memcpy(&value, buffer + bytes_written, sizeof(uint16_t));
  551. retval = target_write_u32(target, EM357_FLASH_CR, FLASH_PG);
  552. if (retval != ERROR_OK)
  553. return retval;
  554. retval = target_write_u16(target, address, value);
  555. if (retval != ERROR_OK)
  556. return retval;
  557. retval = em357_wait_status_busy(bank, 5);
  558. if (retval != ERROR_OK)
  559. return retval;
  560. bytes_written += 2;
  561. words_remaining--;
  562. address += 2;
  563. }
  564. if (bytes_remaining)
  565. {
  566. uint16_t value = 0xffff;
  567. memcpy(&value, buffer + bytes_written, bytes_remaining);
  568. retval = target_write_u32(target, EM357_FLASH_CR, FLASH_PG);
  569. if (retval != ERROR_OK)
  570. return retval;
  571. retval = target_write_u16(target, address, value);
  572. if (retval != ERROR_OK)
  573. return retval;
  574. retval = em357_wait_status_busy(bank, 5);
  575. if (retval != ERROR_OK)
  576. return retval;
  577. }
  578. return target_write_u32(target, EM357_FLASH_CR, FLASH_LOCK);
  579. }
  580. static int em357_probe(struct flash_bank *bank)
  581. {
  582. struct target *target = bank->target;
  583. struct em357_flash_bank *em357_info = bank->driver_priv;
  584. int i;
  585. uint16_t num_pages;
  586. int page_size;
  587. uint32_t base_address = 0x08000000;
  588. em357_info->probed = 0;
  589. /* Enable FPEC CLK */
  590. int retval = target_write_u32(target, EM357_FPEC_CLK, 0x00000001);
  591. if (retval != ERROR_OK)
  592. return retval;
  593. page_size = 2048;
  594. em357_info->ppage_size = 4;
  595. num_pages = 96;
  596. LOG_INFO("flash size = %dkbytes", num_pages*page_size/1024);
  597. if (bank->sectors)
  598. {
  599. free(bank->sectors);
  600. bank->sectors = NULL;
  601. }
  602. bank->base = base_address;
  603. bank->size = (num_pages * page_size);
  604. bank->num_sectors = num_pages;
  605. bank->sectors = malloc(sizeof(struct flash_sector) * num_pages);
  606. for (i = 0; i < num_pages; i++)
  607. {
  608. bank->sectors[i].offset = i * page_size;
  609. bank->sectors[i].size = page_size;
  610. bank->sectors[i].is_erased = -1;
  611. bank->sectors[i].is_protected = 1;
  612. }
  613. em357_info->probed = 1;
  614. return ERROR_OK;
  615. }
  616. static int em357_auto_probe(struct flash_bank *bank)
  617. {
  618. struct em357_flash_bank *em357_info = bank->driver_priv;
  619. if (em357_info->probed)
  620. return ERROR_OK;
  621. return em357_probe(bank);
  622. }
  623. static int get_em357_info(struct flash_bank *bank, char *buf, int buf_size)
  624. {
  625. int printed;
  626. printed = snprintf(buf, buf_size, "em357\n");
  627. buf += printed;
  628. buf_size -= printed;
  629. return ERROR_OK;
  630. }
  631. COMMAND_HANDLER(em357_handle_lock_command)
  632. {
  633. struct target *target = NULL;
  634. struct em357_flash_bank *em357_info = NULL;
  635. if (CMD_ARGC < 1)
  636. {
  637. command_print(CMD_CTX, "em357 lock <bank>");
  638. return ERROR_OK;
  639. }
  640. struct flash_bank *bank;
  641. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  642. if (ERROR_OK != retval)
  643. return retval;
  644. em357_info = bank->driver_priv;
  645. target = bank->target;
  646. if (target->state != TARGET_HALTED)
  647. {
  648. LOG_ERROR("Target not halted");
  649. return ERROR_TARGET_NOT_HALTED;
  650. }
  651. if (em357_erase_options(bank) != ERROR_OK)
  652. {
  653. command_print(CMD_CTX, "em357 failed to erase options");
  654. return ERROR_OK;
  655. }
  656. /* set readout protection */
  657. em357_info->option_bytes.RDP = 0;
  658. if (em357_write_options(bank) != ERROR_OK)
  659. {
  660. command_print(CMD_CTX, "em357 failed to lock device");
  661. return ERROR_OK;
  662. }
  663. command_print(CMD_CTX, "em357 locked");
  664. return ERROR_OK;
  665. }
  666. COMMAND_HANDLER(em357_handle_unlock_command)
  667. {
  668. struct target *target = NULL;
  669. if (CMD_ARGC < 1)
  670. {
  671. command_print(CMD_CTX, "em357 unlock <bank>");
  672. return ERROR_OK;
  673. }
  674. struct flash_bank *bank;
  675. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  676. if (ERROR_OK != retval)
  677. return retval;
  678. target = bank->target;
  679. if (target->state != TARGET_HALTED)
  680. {
  681. LOG_ERROR("Target not halted");
  682. return ERROR_TARGET_NOT_HALTED;
  683. }
  684. if (em357_erase_options(bank) != ERROR_OK)
  685. {
  686. command_print(CMD_CTX, "em357 failed to unlock device");
  687. return ERROR_OK;
  688. }
  689. if (em357_write_options(bank) != ERROR_OK)
  690. {
  691. command_print(CMD_CTX, "em357 failed to lock device");
  692. return ERROR_OK;
  693. }
  694. command_print(CMD_CTX, "em357 unlocked.\n"
  695. "INFO: a reset or power cycle is required "
  696. "for the new settings to take effect.");
  697. return ERROR_OK;
  698. }
  699. static int em357_mass_erase(struct flash_bank *bank)
  700. {
  701. struct target *target = bank->target;
  702. if (target->state != TARGET_HALTED)
  703. {
  704. LOG_ERROR("Target not halted");
  705. return ERROR_TARGET_NOT_HALTED;
  706. }
  707. /* unlock option flash registers */
  708. int retval = target_write_u32(target, EM357_FLASH_KEYR, KEY1);
  709. if (retval != ERROR_OK)
  710. return retval;
  711. retval = target_write_u32(target, EM357_FLASH_KEYR, KEY2);
  712. if (retval != ERROR_OK)
  713. return retval;
  714. /* mass erase flash memory */
  715. retval = target_write_u32(target, EM357_FLASH_CR, FLASH_MER);
  716. if (retval != ERROR_OK)
  717. return retval;
  718. retval = target_write_u32(target, EM357_FLASH_CR, FLASH_MER | FLASH_STRT);
  719. if (retval != ERROR_OK)
  720. return retval;
  721. retval = em357_wait_status_busy(bank, 100);
  722. if (retval != ERROR_OK)
  723. return retval;
  724. retval = target_write_u32(target, EM357_FLASH_CR, FLASH_LOCK);
  725. if (retval != ERROR_OK)
  726. return retval;
  727. return ERROR_OK;
  728. }
  729. COMMAND_HANDLER(em357_handle_mass_erase_command)
  730. {
  731. int i;
  732. if (CMD_ARGC < 1)
  733. {
  734. command_print(CMD_CTX, "em357 mass_erase <bank>");
  735. return ERROR_OK;
  736. }
  737. struct flash_bank *bank;
  738. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  739. if (ERROR_OK != retval)
  740. return retval;
  741. retval = em357_mass_erase(bank);
  742. if (retval == ERROR_OK)
  743. {
  744. /* set all sectors as erased */
  745. for (i = 0; i < bank->num_sectors; i++)
  746. {
  747. bank->sectors[i].is_erased = 1;
  748. }
  749. command_print(CMD_CTX, "em357 mass erase complete");
  750. }
  751. else
  752. {
  753. command_print(CMD_CTX, "em357 mass erase failed");
  754. }
  755. return retval;
  756. }
  757. static const struct command_registration em357_exec_command_handlers[] = {
  758. {
  759. .name = "lock",
  760. .handler = em357_handle_lock_command,
  761. .mode = COMMAND_EXEC,
  762. .usage = "bank_id",
  763. .help = "Lock entire flash device.",
  764. },
  765. {
  766. .name = "unlock",
  767. .handler = em357_handle_unlock_command,
  768. .mode = COMMAND_EXEC,
  769. .usage = "bank_id",
  770. .help = "Unlock entire protected flash device.",
  771. },
  772. {
  773. .name = "mass_erase",
  774. .handler = em357_handle_mass_erase_command,
  775. .mode = COMMAND_EXEC,
  776. .usage = "bank_id",
  777. .help = "Erase entire flash device.",
  778. },
  779. COMMAND_REGISTRATION_DONE
  780. };
  781. static const struct command_registration em357_command_handlers[] = {
  782. {
  783. .name = "em357",
  784. .mode = COMMAND_ANY,
  785. .help = "em357 flash command group",
  786. .chain = em357_exec_command_handlers,
  787. },
  788. COMMAND_REGISTRATION_DONE
  789. };
  790. struct flash_driver em357_flash = {
  791. .name = "em357",
  792. .commands = em357_command_handlers,
  793. .flash_bank_command = em357_flash_bank_command,
  794. .erase = em357_erase,
  795. .protect = em357_protect,
  796. .write = em357_write,
  797. .read = default_flash_read,
  798. .probe = em357_probe,
  799. .auto_probe = em357_auto_probe,
  800. .erase_check = default_flash_mem_blank_check,
  801. .protect_check = em357_protect_check,
  802. .info = get_em357_info,
  803. };