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.
 
 
 
 
 
 

1241 lines
32 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. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "stm32x.h"
  27. #include "armv7m.h"
  28. #include "binarybuffer.h"
  29. static int stm32x_register_commands(struct command_context_s *cmd_ctx);
  30. static int stm32x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
  31. static int stm32x_erase(struct flash_bank_s *bank, int first, int last);
  32. static int stm32x_protect(struct flash_bank_s *bank, int set, int first, int last);
  33. static int stm32x_write(struct flash_bank_s *bank, uint8_t *buffer, u32 offset, u32 count);
  34. static int stm32x_probe(struct flash_bank_s *bank);
  35. static int stm32x_auto_probe(struct flash_bank_s *bank);
  36. //static int stm32x_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  37. static int stm32x_protect_check(struct flash_bank_s *bank);
  38. static int stm32x_info(struct flash_bank_s *bank, char *buf, int buf_size);
  39. static int stm32x_handle_lock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  40. static int stm32x_handle_unlock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  41. static int stm32x_handle_options_read_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  42. static int stm32x_handle_options_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  43. static int stm32x_handle_mass_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  44. static int stm32x_mass_erase(struct flash_bank_s *bank);
  45. flash_driver_t stm32x_flash =
  46. {
  47. .name = "stm32x",
  48. .register_commands = stm32x_register_commands,
  49. .flash_bank_command = stm32x_flash_bank_command,
  50. .erase = stm32x_erase,
  51. .protect = stm32x_protect,
  52. .write = stm32x_write,
  53. .probe = stm32x_probe,
  54. .auto_probe = stm32x_auto_probe,
  55. .erase_check = default_flash_mem_blank_check,
  56. .protect_check = stm32x_protect_check,
  57. .info = stm32x_info
  58. };
  59. static int stm32x_register_commands(struct command_context_s *cmd_ctx)
  60. {
  61. command_t *stm32x_cmd = register_command(cmd_ctx, NULL, "stm32x", NULL, COMMAND_ANY, "stm32x flash specific commands");
  62. register_command(cmd_ctx, stm32x_cmd, "lock", stm32x_handle_lock_command, COMMAND_EXEC,
  63. "lock device");
  64. register_command(cmd_ctx, stm32x_cmd, "unlock", stm32x_handle_unlock_command, COMMAND_EXEC,
  65. "unlock protected device");
  66. register_command(cmd_ctx, stm32x_cmd, "mass_erase", stm32x_handle_mass_erase_command, COMMAND_EXEC,
  67. "mass erase device");
  68. register_command(cmd_ctx, stm32x_cmd, "options_read", stm32x_handle_options_read_command, COMMAND_EXEC,
  69. "read device option bytes");
  70. register_command(cmd_ctx, stm32x_cmd, "options_write", stm32x_handle_options_write_command, COMMAND_EXEC,
  71. "write device option bytes");
  72. return ERROR_OK;
  73. }
  74. /* flash bank stm32x <base> <size> 0 0 <target#>
  75. */
  76. static int stm32x_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
  77. {
  78. stm32x_flash_bank_t *stm32x_info;
  79. if (argc < 6)
  80. {
  81. LOG_WARNING("incomplete flash_bank stm32x configuration");
  82. return ERROR_FLASH_BANK_INVALID;
  83. }
  84. stm32x_info = malloc(sizeof(stm32x_flash_bank_t));
  85. bank->driver_priv = stm32x_info;
  86. stm32x_info->write_algorithm = NULL;
  87. stm32x_info->probed = 0;
  88. return ERROR_OK;
  89. }
  90. static u32 stm32x_get_flash_status(flash_bank_t *bank)
  91. {
  92. target_t *target = bank->target;
  93. u32 status;
  94. target_read_u32(target, STM32_FLASH_SR, &status);
  95. return status;
  96. }
  97. static u32 stm32x_wait_status_busy(flash_bank_t *bank, int timeout)
  98. {
  99. target_t *target = bank->target;
  100. u32 status;
  101. /* wait for busy to clear */
  102. while (((status = stm32x_get_flash_status(bank)) & FLASH_BSY) && (timeout-- > 0))
  103. {
  104. LOG_DEBUG("status: 0x%x", status);
  105. alive_sleep(1);
  106. }
  107. /* Clear but report errors */
  108. if (status & (FLASH_WRPRTERR|FLASH_PGERR))
  109. {
  110. target_write_u32(target, STM32_FLASH_SR, FLASH_WRPRTERR|FLASH_PGERR);
  111. }
  112. return status;
  113. }
  114. static int stm32x_read_options(struct flash_bank_s *bank)
  115. {
  116. u32 optiondata;
  117. stm32x_flash_bank_t *stm32x_info = NULL;
  118. target_t *target = bank->target;
  119. stm32x_info = bank->driver_priv;
  120. /* read current option bytes */
  121. target_read_u32(target, STM32_FLASH_OBR, &optiondata);
  122. stm32x_info->option_bytes.user_options = (uint16_t)0xFFF8|((optiondata >> 2) & 0x07);
  123. stm32x_info->option_bytes.RDP = (optiondata & (1 << OPT_READOUT)) ? 0xFFFF : 0x5AA5;
  124. if (optiondata & (1 << OPT_READOUT))
  125. LOG_INFO("Device Security Bit Set");
  126. /* each bit refers to a 4bank protection */
  127. target_read_u32(target, STM32_FLASH_WRPR, &optiondata);
  128. stm32x_info->option_bytes.protection[0] = (uint16_t)optiondata;
  129. stm32x_info->option_bytes.protection[1] = (uint16_t)(optiondata >> 8);
  130. stm32x_info->option_bytes.protection[2] = (uint16_t)(optiondata >> 16);
  131. stm32x_info->option_bytes.protection[3] = (uint16_t)(optiondata >> 24);
  132. return ERROR_OK;
  133. }
  134. static int stm32x_erase_options(struct flash_bank_s *bank)
  135. {
  136. stm32x_flash_bank_t *stm32x_info = NULL;
  137. target_t *target = bank->target;
  138. u32 status;
  139. stm32x_info = bank->driver_priv;
  140. /* read current options */
  141. stm32x_read_options(bank);
  142. /* unlock flash registers */
  143. target_write_u32(target, STM32_FLASH_KEYR, KEY1);
  144. target_write_u32(target, STM32_FLASH_KEYR, KEY2);
  145. /* unlock option flash registers */
  146. target_write_u32(target, STM32_FLASH_OPTKEYR, KEY1);
  147. target_write_u32(target, STM32_FLASH_OPTKEYR, KEY2);
  148. /* erase option bytes */
  149. target_write_u32(target, STM32_FLASH_CR, FLASH_OPTER|FLASH_OPTWRE);
  150. target_write_u32(target, STM32_FLASH_CR, FLASH_OPTER|FLASH_STRT|FLASH_OPTWRE);
  151. status = stm32x_wait_status_busy(bank, 10);
  152. if( status & FLASH_WRPRTERR )
  153. return ERROR_FLASH_OPERATION_FAILED;
  154. if( status & FLASH_PGERR )
  155. return ERROR_FLASH_OPERATION_FAILED;
  156. /* clear readout protection and complementary option bytes
  157. * this will also force a device unlock if set */
  158. stm32x_info->option_bytes.RDP = 0x5AA5;
  159. return ERROR_OK;
  160. }
  161. static int stm32x_write_options(struct flash_bank_s *bank)
  162. {
  163. stm32x_flash_bank_t *stm32x_info = NULL;
  164. target_t *target = bank->target;
  165. u32 status;
  166. stm32x_info = bank->driver_priv;
  167. /* unlock flash registers */
  168. target_write_u32(target, STM32_FLASH_KEYR, KEY1);
  169. target_write_u32(target, STM32_FLASH_KEYR, KEY2);
  170. /* unlock option flash registers */
  171. target_write_u32(target, STM32_FLASH_OPTKEYR, KEY1);
  172. target_write_u32(target, STM32_FLASH_OPTKEYR, KEY2);
  173. /* program option bytes */
  174. target_write_u32(target, STM32_FLASH_CR, FLASH_OPTPG|FLASH_OPTWRE);
  175. /* write user option byte */
  176. target_write_u16(target, STM32_OB_USER, stm32x_info->option_bytes.user_options);
  177. status = stm32x_wait_status_busy(bank, 10);
  178. if( status & FLASH_WRPRTERR )
  179. return ERROR_FLASH_OPERATION_FAILED;
  180. if( status & FLASH_PGERR )
  181. return ERROR_FLASH_OPERATION_FAILED;
  182. /* write protection byte 1 */
  183. target_write_u16(target, STM32_OB_WRP0, stm32x_info->option_bytes.protection[0]);
  184. status = stm32x_wait_status_busy(bank, 10);
  185. if( status & FLASH_WRPRTERR )
  186. return ERROR_FLASH_OPERATION_FAILED;
  187. if( status & FLASH_PGERR )
  188. return ERROR_FLASH_OPERATION_FAILED;
  189. /* write protection byte 2 */
  190. target_write_u16(target, STM32_OB_WRP1, stm32x_info->option_bytes.protection[1]);
  191. status = stm32x_wait_status_busy(bank, 10);
  192. if( status & FLASH_WRPRTERR )
  193. return ERROR_FLASH_OPERATION_FAILED;
  194. if( status & FLASH_PGERR )
  195. return ERROR_FLASH_OPERATION_FAILED;
  196. /* write protection byte 3 */
  197. target_write_u16(target, STM32_OB_WRP2, stm32x_info->option_bytes.protection[2]);
  198. status = stm32x_wait_status_busy(bank, 10);
  199. if( status & FLASH_WRPRTERR )
  200. return ERROR_FLASH_OPERATION_FAILED;
  201. if( status & FLASH_PGERR )
  202. return ERROR_FLASH_OPERATION_FAILED;
  203. /* write protection byte 4 */
  204. target_write_u16(target, STM32_OB_WRP3, stm32x_info->option_bytes.protection[3]);
  205. status = stm32x_wait_status_busy(bank, 10);
  206. if( status & FLASH_WRPRTERR )
  207. return ERROR_FLASH_OPERATION_FAILED;
  208. if( status & FLASH_PGERR )
  209. return ERROR_FLASH_OPERATION_FAILED;
  210. /* write readout protection bit */
  211. target_write_u16(target, STM32_OB_RDP, stm32x_info->option_bytes.RDP);
  212. status = stm32x_wait_status_busy(bank, 10);
  213. if( status & FLASH_WRPRTERR )
  214. return ERROR_FLASH_OPERATION_FAILED;
  215. if( status & FLASH_PGERR )
  216. return ERROR_FLASH_OPERATION_FAILED;
  217. target_write_u32(target, STM32_FLASH_CR, FLASH_LOCK);
  218. return ERROR_OK;
  219. }
  220. static int stm32x_protect_check(struct flash_bank_s *bank)
  221. {
  222. target_t *target = bank->target;
  223. stm32x_flash_bank_t *stm32x_info = bank->driver_priv;
  224. u32 protection;
  225. int i, s;
  226. int num_bits;
  227. int set;
  228. if (target->state != TARGET_HALTED)
  229. {
  230. LOG_ERROR("Target not halted");
  231. return ERROR_TARGET_NOT_HALTED;
  232. }
  233. /* medium density - each bit refers to a 4bank protection
  234. * high density - each bit refers to a 2bank protection */
  235. target_read_u32(target, STM32_FLASH_WRPR, &protection);
  236. /* medium density - each protection bit is for 4 * 1K pages
  237. * high density - each protection bit is for 2 * 2K pages */
  238. num_bits = (bank->num_sectors / stm32x_info->ppage_size);
  239. if (stm32x_info->ppage_size == 2)
  240. {
  241. /* high density flash */
  242. set = 1;
  243. if (protection & (1 << 31))
  244. set = 0;
  245. /* bit 31 controls sector 62 - 255 protection */
  246. for (s = 62; s < bank->num_sectors; s++)
  247. {
  248. bank->sectors[s].is_protected = set;
  249. }
  250. if (bank->num_sectors > 61)
  251. num_bits = 31;
  252. for (i = 0; i < num_bits; i++)
  253. {
  254. set = 1;
  255. if (protection & (1 << i))
  256. set = 0;
  257. for (s = 0; s < stm32x_info->ppage_size; s++)
  258. bank->sectors[(i * stm32x_info->ppage_size) + s].is_protected = set;
  259. }
  260. }
  261. else
  262. {
  263. /* medium density flash */
  264. for (i = 0; i < num_bits; i++)
  265. {
  266. set = 1;
  267. if( protection & (1 << i))
  268. set = 0;
  269. for (s = 0; s < stm32x_info->ppage_size; s++)
  270. bank->sectors[(i * stm32x_info->ppage_size) + s].is_protected = set;
  271. }
  272. }
  273. return ERROR_OK;
  274. }
  275. static int stm32x_erase(struct flash_bank_s *bank, int first, int last)
  276. {
  277. target_t *target = bank->target;
  278. int i;
  279. u32 status;
  280. if (bank->target->state != TARGET_HALTED)
  281. {
  282. LOG_ERROR("Target not halted");
  283. return ERROR_TARGET_NOT_HALTED;
  284. }
  285. if ((first == 0) && (last == (bank->num_sectors - 1)))
  286. {
  287. return stm32x_mass_erase(bank);
  288. }
  289. /* unlock flash registers */
  290. target_write_u32(target, STM32_FLASH_KEYR, KEY1);
  291. target_write_u32(target, STM32_FLASH_KEYR, KEY2);
  292. for (i = first; i <= last; i++)
  293. {
  294. target_write_u32(target, STM32_FLASH_CR, FLASH_PER);
  295. target_write_u32(target, STM32_FLASH_AR, bank->base + bank->sectors[i].offset);
  296. target_write_u32(target, STM32_FLASH_CR, FLASH_PER|FLASH_STRT);
  297. status = stm32x_wait_status_busy(bank, 10);
  298. if( status & FLASH_WRPRTERR )
  299. return ERROR_FLASH_OPERATION_FAILED;
  300. if( status & FLASH_PGERR )
  301. return ERROR_FLASH_OPERATION_FAILED;
  302. bank->sectors[i].is_erased = 1;
  303. }
  304. target_write_u32(target, STM32_FLASH_CR, FLASH_LOCK);
  305. return ERROR_OK;
  306. }
  307. static int stm32x_protect(struct flash_bank_s *bank, int set, int first, int last)
  308. {
  309. stm32x_flash_bank_t *stm32x_info = NULL;
  310. target_t *target = bank->target;
  311. uint16_t prot_reg[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
  312. int i, reg, bit;
  313. int status;
  314. u32 protection;
  315. stm32x_info = bank->driver_priv;
  316. if (target->state != TARGET_HALTED)
  317. {
  318. LOG_ERROR("Target not halted");
  319. return ERROR_TARGET_NOT_HALTED;
  320. }
  321. if ((first && (first % stm32x_info->ppage_size)) || ((last + 1) && (last + 1) % stm32x_info->ppage_size))
  322. {
  323. LOG_WARNING("sector start/end incorrect - stm32 has %dK sector protection", stm32x_info->ppage_size);
  324. return ERROR_FLASH_SECTOR_INVALID;
  325. }
  326. /* medium density - each bit refers to a 4bank protection
  327. * high density - each bit refers to a 2bank protection */
  328. target_read_u32(target, STM32_FLASH_WRPR, &protection);
  329. prot_reg[0] = (uint16_t)protection;
  330. prot_reg[1] = (uint16_t)(protection >> 8);
  331. prot_reg[2] = (uint16_t)(protection >> 16);
  332. prot_reg[3] = (uint16_t)(protection >> 24);
  333. if (stm32x_info->ppage_size == 2)
  334. {
  335. /* high density flash */
  336. /* bit 7 controls sector 62 - 255 protection */
  337. if (last > 61)
  338. {
  339. if (set)
  340. prot_reg[3] &= ~(1 << 7);
  341. else
  342. prot_reg[3] |= (1 << 7);
  343. }
  344. if (first > 61)
  345. first = 62;
  346. if (last > 61)
  347. last = 61;
  348. for (i = first; i <= last; i++)
  349. {
  350. reg = (i / stm32x_info->ppage_size) / 8;
  351. bit = (i / stm32x_info->ppage_size) - (reg * 8);
  352. if( set )
  353. prot_reg[reg] &= ~(1 << bit);
  354. else
  355. prot_reg[reg] |= (1 << bit);
  356. }
  357. }
  358. else
  359. {
  360. /* medium density flash */
  361. for (i = first; i <= last; i++)
  362. {
  363. reg = (i / stm32x_info->ppage_size) / 8;
  364. bit = (i / stm32x_info->ppage_size) - (reg * 8);
  365. if( set )
  366. prot_reg[reg] &= ~(1 << bit);
  367. else
  368. prot_reg[reg] |= (1 << bit);
  369. }
  370. }
  371. if ((status = stm32x_erase_options(bank)) != ERROR_OK)
  372. return status;
  373. stm32x_info->option_bytes.protection[0] = prot_reg[0];
  374. stm32x_info->option_bytes.protection[1] = prot_reg[1];
  375. stm32x_info->option_bytes.protection[2] = prot_reg[2];
  376. stm32x_info->option_bytes.protection[3] = prot_reg[3];
  377. return stm32x_write_options(bank);
  378. }
  379. static int stm32x_write_block(struct flash_bank_s *bank, uint8_t *buffer, u32 offset, u32 count)
  380. {
  381. stm32x_flash_bank_t *stm32x_info = bank->driver_priv;
  382. target_t *target = bank->target;
  383. u32 buffer_size = 16384;
  384. working_area_t *source;
  385. u32 address = bank->base + offset;
  386. reg_param_t reg_params[4];
  387. armv7m_algorithm_t armv7m_info;
  388. int retval = ERROR_OK;
  389. uint8_t stm32x_flash_write_code[] = {
  390. /* write: */
  391. 0xDF, 0xF8, 0x24, 0x40, /* ldr r4, STM32_FLASH_CR */
  392. 0x09, 0x4D, /* ldr r5, STM32_FLASH_SR */
  393. 0x4F, 0xF0, 0x01, 0x03, /* mov r3, #1 */
  394. 0x23, 0x60, /* str r3, [r4, #0] */
  395. 0x30, 0xF8, 0x02, 0x3B, /* ldrh r3, [r0], #2 */
  396. 0x21, 0xF8, 0x02, 0x3B, /* strh r3, [r1], #2 */
  397. /* busy: */
  398. 0x2B, 0x68, /* ldr r3, [r5, #0] */
  399. 0x13, 0xF0, 0x01, 0x0F, /* tst r3, #0x01 */
  400. 0xFB, 0xD0, /* beq busy */
  401. 0x13, 0xF0, 0x14, 0x0F, /* tst r3, #0x14 */
  402. 0x01, 0xD1, /* bne exit */
  403. 0x01, 0x3A, /* subs r2, r2, #1 */
  404. 0xED, 0xD1, /* bne write */
  405. /* exit: */
  406. 0xFE, 0xE7, /* b exit */
  407. 0x10, 0x20, 0x02, 0x40, /* STM32_FLASH_CR: .word 0x40022010 */
  408. 0x0C, 0x20, 0x02, 0x40 /* STM32_FLASH_SR: .word 0x4002200C */
  409. };
  410. /* flash write code */
  411. if (target_alloc_working_area(target, sizeof(stm32x_flash_write_code), &stm32x_info->write_algorithm) != ERROR_OK)
  412. {
  413. LOG_WARNING("no working area available, can't do block memory writes");
  414. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  415. };
  416. if ((retval=target_write_buffer(target, stm32x_info->write_algorithm->address, sizeof(stm32x_flash_write_code), stm32x_flash_write_code))!=ERROR_OK)
  417. return retval;
  418. /* memory buffer */
  419. while (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK)
  420. {
  421. buffer_size /= 2;
  422. if (buffer_size <= 256)
  423. {
  424. /* if we already allocated the writing code, but failed to get a buffer, free the algorithm */
  425. if (stm32x_info->write_algorithm)
  426. target_free_working_area(target, stm32x_info->write_algorithm);
  427. LOG_WARNING("no large enough working area available, can't do block memory writes");
  428. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  429. }
  430. };
  431. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  432. armv7m_info.core_mode = ARMV7M_MODE_ANY;
  433. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  434. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  435. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
  436. init_reg_param(&reg_params[3], "r3", 32, PARAM_IN);
  437. while (count > 0)
  438. {
  439. u32 thisrun_count = (count > (buffer_size / 2)) ? (buffer_size / 2) : count;
  440. if ((retval = target_write_buffer(target, source->address, thisrun_count * 2, buffer))!=ERROR_OK)
  441. break;
  442. buf_set_u32(reg_params[0].value, 0, 32, source->address);
  443. buf_set_u32(reg_params[1].value, 0, 32, address);
  444. buf_set_u32(reg_params[2].value, 0, 32, thisrun_count);
  445. if ((retval = target_run_algorithm(target, 0, NULL, 4, reg_params, stm32x_info->write_algorithm->address, \
  446. stm32x_info->write_algorithm->address + (sizeof(stm32x_flash_write_code) - 10), 10000, &armv7m_info)) != ERROR_OK)
  447. {
  448. LOG_ERROR("error executing stm32x flash write algorithm");
  449. retval = ERROR_FLASH_OPERATION_FAILED;
  450. break;
  451. }
  452. if (buf_get_u32(reg_params[3].value, 0, 32) & FLASH_PGERR)
  453. {
  454. LOG_ERROR("flash memory not erased before writing");
  455. /* Clear but report errors */
  456. target_write_u32(target, STM32_FLASH_SR, FLASH_PGERR);
  457. retval = ERROR_FLASH_OPERATION_FAILED;
  458. break;
  459. }
  460. if (buf_get_u32(reg_params[3].value, 0, 32) & FLASH_WRPRTERR)
  461. {
  462. LOG_ERROR("flash memory write protected");
  463. /* Clear but report errors */
  464. target_write_u32(target, STM32_FLASH_SR, FLASH_WRPRTERR);
  465. retval = ERROR_FLASH_OPERATION_FAILED;
  466. break;
  467. }
  468. buffer += thisrun_count * 2;
  469. address += thisrun_count * 2;
  470. count -= thisrun_count;
  471. }
  472. target_free_working_area(target, source);
  473. target_free_working_area(target, stm32x_info->write_algorithm);
  474. destroy_reg_param(&reg_params[0]);
  475. destroy_reg_param(&reg_params[1]);
  476. destroy_reg_param(&reg_params[2]);
  477. destroy_reg_param(&reg_params[3]);
  478. return retval;
  479. }
  480. static int stm32x_write(struct flash_bank_s *bank, uint8_t *buffer, u32 offset, u32 count)
  481. {
  482. target_t *target = bank->target;
  483. u32 words_remaining = (count / 2);
  484. u32 bytes_remaining = (count & 0x00000001);
  485. u32 address = bank->base + offset;
  486. u32 bytes_written = 0;
  487. uint8_t status;
  488. int retval;
  489. if (bank->target->state != TARGET_HALTED)
  490. {
  491. LOG_ERROR("Target not halted");
  492. return ERROR_TARGET_NOT_HALTED;
  493. }
  494. if (offset & 0x1)
  495. {
  496. LOG_WARNING("offset 0x%x breaks required 2-byte alignment", offset);
  497. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  498. }
  499. /* unlock flash registers */
  500. target_write_u32(target, STM32_FLASH_KEYR, KEY1);
  501. target_write_u32(target, STM32_FLASH_KEYR, KEY2);
  502. /* multiple half words (2-byte) to be programmed? */
  503. if (words_remaining > 0)
  504. {
  505. /* try using a block write */
  506. if ((retval = stm32x_write_block(bank, buffer, offset, words_remaining)) != ERROR_OK)
  507. {
  508. if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
  509. {
  510. /* if block write failed (no sufficient working area),
  511. * we use normal (slow) single dword accesses */
  512. LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
  513. }
  514. else if (retval == ERROR_FLASH_OPERATION_FAILED)
  515. {
  516. LOG_ERROR("flash writing failed with error code: 0x%x", retval);
  517. return ERROR_FLASH_OPERATION_FAILED;
  518. }
  519. }
  520. else
  521. {
  522. buffer += words_remaining * 2;
  523. address += words_remaining * 2;
  524. words_remaining = 0;
  525. }
  526. }
  527. while (words_remaining > 0)
  528. {
  529. uint16_t value;
  530. memcpy(&value, buffer + bytes_written, sizeof(uint16_t));
  531. target_write_u32(target, STM32_FLASH_CR, FLASH_PG);
  532. target_write_u16(target, address, value);
  533. status = stm32x_wait_status_busy(bank, 5);
  534. if( status & FLASH_WRPRTERR )
  535. {
  536. LOG_ERROR("flash memory not erased before writing");
  537. return ERROR_FLASH_OPERATION_FAILED;
  538. }
  539. if( status & FLASH_PGERR )
  540. {
  541. LOG_ERROR("flash memory write protected");
  542. return ERROR_FLASH_OPERATION_FAILED;
  543. }
  544. bytes_written += 2;
  545. words_remaining--;
  546. address += 2;
  547. }
  548. if (bytes_remaining)
  549. {
  550. uint16_t value = 0xffff;
  551. memcpy(&value, buffer + bytes_written, bytes_remaining);
  552. target_write_u32(target, STM32_FLASH_CR, FLASH_PG);
  553. target_write_u16(target, address, value);
  554. status = stm32x_wait_status_busy(bank, 5);
  555. if( status & FLASH_WRPRTERR )
  556. {
  557. LOG_ERROR("flash memory not erased before writing");
  558. return ERROR_FLASH_OPERATION_FAILED;
  559. }
  560. if( status & FLASH_PGERR )
  561. {
  562. LOG_ERROR("flash memory write protected");
  563. return ERROR_FLASH_OPERATION_FAILED;
  564. }
  565. }
  566. target_write_u32(target, STM32_FLASH_CR, FLASH_LOCK);
  567. return ERROR_OK;
  568. }
  569. static int stm32x_probe(struct flash_bank_s *bank)
  570. {
  571. target_t *target = bank->target;
  572. stm32x_flash_bank_t *stm32x_info = bank->driver_priv;
  573. int i;
  574. uint16_t num_pages;
  575. u32 device_id;
  576. int page_size;
  577. if (bank->target->state != TARGET_HALTED)
  578. {
  579. LOG_ERROR("Target not halted");
  580. return ERROR_TARGET_NOT_HALTED;
  581. }
  582. stm32x_info->probed = 0;
  583. /* read stm32 device id register */
  584. target_read_u32(target, 0xE0042000, &device_id);
  585. LOG_INFO( "device id = 0x%08x", device_id );
  586. /* get flash size from target */
  587. if (target_read_u16(target, 0x1FFFF7E0, &num_pages) != ERROR_OK)
  588. {
  589. /* failed reading flash size, default to max target family */
  590. num_pages = 0xffff;
  591. }
  592. if ((device_id & 0x7ff) == 0x410)
  593. {
  594. /* medium density - we have 1k pages
  595. * 4 pages for a protection area */
  596. page_size = 1024;
  597. stm32x_info->ppage_size = 4;
  598. /* check for early silicon */
  599. if (num_pages == 0xffff)
  600. {
  601. /* number of sectors incorrect on revA */
  602. LOG_WARNING( "STM32 flash size failed, probe inaccurate - assuming 128k flash" );
  603. num_pages = 128;
  604. }
  605. }
  606. else if ((device_id & 0x7ff) == 0x412)
  607. {
  608. /* low density - we have 1k pages
  609. * 4 pages for a protection area */
  610. page_size = 1024;
  611. stm32x_info->ppage_size = 4;
  612. /* check for early silicon */
  613. if (num_pages == 0xffff)
  614. {
  615. /* number of sectors incorrect on revA */
  616. LOG_WARNING( "STM32 flash size failed, probe inaccurate - assuming 32k flash" );
  617. num_pages = 32;
  618. }
  619. }
  620. else if ((device_id & 0x7ff) == 0x414)
  621. {
  622. /* high density - we have 2k pages
  623. * 2 pages for a protection area */
  624. page_size = 2048;
  625. stm32x_info->ppage_size = 2;
  626. /* check for early silicon */
  627. if (num_pages == 0xffff)
  628. {
  629. /* number of sectors incorrect on revZ */
  630. LOG_WARNING( "STM32 flash size failed, probe inaccurate - assuming 512k flash" );
  631. num_pages = 512;
  632. }
  633. }
  634. else if ((device_id & 0x7ff) == 0x418)
  635. {
  636. /* connectivity line density - we have 1k pages
  637. * 4 pages for a protection area */
  638. page_size = 1024;
  639. stm32x_info->ppage_size = 4;
  640. /* check for early silicon */
  641. if (num_pages == 0xffff)
  642. {
  643. /* number of sectors incorrect on revZ */
  644. LOG_WARNING( "STM32 flash size failed, probe inaccurate - assuming 256k flash" );
  645. num_pages = 256;
  646. }
  647. }
  648. else
  649. {
  650. LOG_WARNING( "Cannot identify target as a STM32 family." );
  651. return ERROR_FLASH_OPERATION_FAILED;
  652. }
  653. LOG_INFO( "flash size = %dkbytes", num_pages );
  654. /* calculate numbers of pages */
  655. num_pages /= (page_size / 1024);
  656. bank->base = 0x08000000;
  657. bank->size = (num_pages * page_size);
  658. bank->num_sectors = num_pages;
  659. bank->sectors = malloc(sizeof(flash_sector_t) * num_pages);
  660. for (i = 0; i < num_pages; i++)
  661. {
  662. bank->sectors[i].offset = i * page_size;
  663. bank->sectors[i].size = page_size;
  664. bank->sectors[i].is_erased = -1;
  665. bank->sectors[i].is_protected = 1;
  666. }
  667. stm32x_info->probed = 1;
  668. return ERROR_OK;
  669. }
  670. static int stm32x_auto_probe(struct flash_bank_s *bank)
  671. {
  672. stm32x_flash_bank_t *stm32x_info = bank->driver_priv;
  673. if (stm32x_info->probed)
  674. return ERROR_OK;
  675. return stm32x_probe(bank);
  676. }
  677. #if 0
  678. static int stm32x_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  679. {
  680. return ERROR_OK;
  681. }
  682. #endif
  683. static int stm32x_info(struct flash_bank_s *bank, char *buf, int buf_size)
  684. {
  685. target_t *target = bank->target;
  686. u32 device_id;
  687. int printed;
  688. /* read stm32 device id register */
  689. target_read_u32(target, 0xE0042000, &device_id);
  690. if ((device_id & 0x7ff) == 0x410)
  691. {
  692. printed = snprintf(buf, buf_size, "stm32x (Medium Density) - Rev: ");
  693. buf += printed;
  694. buf_size -= printed;
  695. switch(device_id >> 16)
  696. {
  697. case 0x0000:
  698. snprintf(buf, buf_size, "A");
  699. break;
  700. case 0x2000:
  701. snprintf(buf, buf_size, "B");
  702. break;
  703. case 0x2001:
  704. snprintf(buf, buf_size, "Z");
  705. break;
  706. case 0x2003:
  707. snprintf(buf, buf_size, "Y");
  708. break;
  709. default:
  710. snprintf(buf, buf_size, "unknown");
  711. break;
  712. }
  713. }
  714. else if ((device_id & 0x7ff) == 0x412)
  715. {
  716. printed = snprintf(buf, buf_size, "stm32x (Low Density) - Rev: ");
  717. buf += printed;
  718. buf_size -= printed;
  719. switch(device_id >> 16)
  720. {
  721. case 0x1000:
  722. snprintf(buf, buf_size, "A");
  723. break;
  724. default:
  725. snprintf(buf, buf_size, "unknown");
  726. break;
  727. }
  728. }
  729. else if ((device_id & 0x7ff) == 0x414)
  730. {
  731. printed = snprintf(buf, buf_size, "stm32x (High Density) - Rev: ");
  732. buf += printed;
  733. buf_size -= printed;
  734. switch(device_id >> 16)
  735. {
  736. case 0x1000:
  737. snprintf(buf, buf_size, "A");
  738. break;
  739. case 0x1001:
  740. snprintf(buf, buf_size, "Z");
  741. break;
  742. default:
  743. snprintf(buf, buf_size, "unknown");
  744. break;
  745. }
  746. }
  747. else if ((device_id & 0x7ff) == 0x418)
  748. {
  749. printed = snprintf(buf, buf_size, "stm32x (Connectivity) - Rev: ");
  750. buf += printed;
  751. buf_size -= printed;
  752. switch(device_id >> 16)
  753. {
  754. case 0x1000:
  755. snprintf(buf, buf_size, "A");
  756. break;
  757. default:
  758. snprintf(buf, buf_size, "unknown");
  759. break;
  760. }
  761. }
  762. else
  763. {
  764. snprintf(buf, buf_size, "Cannot identify target as a stm32x\n");
  765. return ERROR_FLASH_OPERATION_FAILED;
  766. }
  767. return ERROR_OK;
  768. }
  769. static int stm32x_handle_lock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  770. {
  771. flash_bank_t *bank;
  772. target_t *target = NULL;
  773. stm32x_flash_bank_t *stm32x_info = NULL;
  774. if (argc < 1)
  775. {
  776. command_print(cmd_ctx, "stm32x lock <bank>");
  777. return ERROR_OK;
  778. }
  779. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  780. if (!bank)
  781. {
  782. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  783. return ERROR_OK;
  784. }
  785. stm32x_info = bank->driver_priv;
  786. target = bank->target;
  787. if (target->state != TARGET_HALTED)
  788. {
  789. LOG_ERROR("Target not halted");
  790. return ERROR_TARGET_NOT_HALTED;
  791. }
  792. if (stm32x_erase_options(bank) != ERROR_OK)
  793. {
  794. command_print(cmd_ctx, "stm32x failed to erase options");
  795. return ERROR_OK;
  796. }
  797. /* set readout protection */
  798. stm32x_info->option_bytes.RDP = 0;
  799. if (stm32x_write_options(bank) != ERROR_OK)
  800. {
  801. command_print(cmd_ctx, "stm32x failed to lock device");
  802. return ERROR_OK;
  803. }
  804. command_print(cmd_ctx, "stm32x locked");
  805. return ERROR_OK;
  806. }
  807. static int stm32x_handle_unlock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  808. {
  809. flash_bank_t *bank;
  810. target_t *target = NULL;
  811. stm32x_flash_bank_t *stm32x_info = NULL;
  812. if (argc < 1)
  813. {
  814. command_print(cmd_ctx, "stm32x unlock <bank>");
  815. return ERROR_OK;
  816. }
  817. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  818. if (!bank)
  819. {
  820. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  821. return ERROR_OK;
  822. }
  823. stm32x_info = bank->driver_priv;
  824. target = bank->target;
  825. if (target->state != TARGET_HALTED)
  826. {
  827. LOG_ERROR("Target not halted");
  828. return ERROR_TARGET_NOT_HALTED;
  829. }
  830. if (stm32x_erase_options(bank) != ERROR_OK)
  831. {
  832. command_print(cmd_ctx, "stm32x failed to unlock device");
  833. return ERROR_OK;
  834. }
  835. if (stm32x_write_options(bank) != ERROR_OK)
  836. {
  837. command_print(cmd_ctx, "stm32x failed to lock device");
  838. return ERROR_OK;
  839. }
  840. command_print(cmd_ctx, "stm32x unlocked");
  841. return ERROR_OK;
  842. }
  843. static int stm32x_handle_options_read_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  844. {
  845. flash_bank_t *bank;
  846. u32 optionbyte;
  847. target_t *target = NULL;
  848. stm32x_flash_bank_t *stm32x_info = NULL;
  849. if (argc < 1)
  850. {
  851. command_print(cmd_ctx, "stm32x options_read <bank>");
  852. return ERROR_OK;
  853. }
  854. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  855. if (!bank)
  856. {
  857. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  858. return ERROR_OK;
  859. }
  860. stm32x_info = bank->driver_priv;
  861. target = bank->target;
  862. if (target->state != TARGET_HALTED)
  863. {
  864. LOG_ERROR("Target not halted");
  865. return ERROR_TARGET_NOT_HALTED;
  866. }
  867. target_read_u32(target, STM32_FLASH_OBR, &optionbyte);
  868. command_print(cmd_ctx, "Option Byte: 0x%x", optionbyte);
  869. if (buf_get_u32((uint8_t*)&optionbyte, OPT_ERROR, 1))
  870. command_print(cmd_ctx, "Option Byte Complement Error");
  871. if (buf_get_u32((uint8_t*)&optionbyte, OPT_READOUT, 1))
  872. command_print(cmd_ctx, "Readout Protection On");
  873. else
  874. command_print(cmd_ctx, "Readout Protection Off");
  875. if (buf_get_u32((uint8_t*)&optionbyte, OPT_RDWDGSW, 1))
  876. command_print(cmd_ctx, "Software Watchdog");
  877. else
  878. command_print(cmd_ctx, "Hardware Watchdog");
  879. if (buf_get_u32((uint8_t*)&optionbyte, OPT_RDRSTSTOP, 1))
  880. command_print(cmd_ctx, "Stop: No reset generated");
  881. else
  882. command_print(cmd_ctx, "Stop: Reset generated");
  883. if (buf_get_u32((uint8_t*)&optionbyte, OPT_RDRSTSTDBY, 1))
  884. command_print(cmd_ctx, "Standby: No reset generated");
  885. else
  886. command_print(cmd_ctx, "Standby: Reset generated");
  887. return ERROR_OK;
  888. }
  889. static int stm32x_handle_options_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  890. {
  891. flash_bank_t *bank;
  892. target_t *target = NULL;
  893. stm32x_flash_bank_t *stm32x_info = NULL;
  894. uint16_t optionbyte = 0xF8;
  895. if (argc < 4)
  896. {
  897. command_print(cmd_ctx, "stm32x options_write <bank> <SWWDG|HWWDG> <RSTSTNDBY|NORSTSTNDBY> <RSTSTOP|NORSTSTOP>");
  898. return ERROR_OK;
  899. }
  900. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  901. if (!bank)
  902. {
  903. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  904. return ERROR_OK;
  905. }
  906. stm32x_info = bank->driver_priv;
  907. target = bank->target;
  908. if (target->state != TARGET_HALTED)
  909. {
  910. LOG_ERROR("Target not halted");
  911. return ERROR_TARGET_NOT_HALTED;
  912. }
  913. if (strcmp(args[1], "SWWDG") == 0)
  914. {
  915. optionbyte |= (1<<0);
  916. }
  917. else
  918. {
  919. optionbyte &= ~(1<<0);
  920. }
  921. if (strcmp(args[2], "NORSTSTNDBY") == 0)
  922. {
  923. optionbyte |= (1<<1);
  924. }
  925. else
  926. {
  927. optionbyte &= ~(1<<1);
  928. }
  929. if (strcmp(args[3], "NORSTSTOP") == 0)
  930. {
  931. optionbyte |= (1<<2);
  932. }
  933. else
  934. {
  935. optionbyte &= ~(1<<2);
  936. }
  937. if (stm32x_erase_options(bank) != ERROR_OK)
  938. {
  939. command_print(cmd_ctx, "stm32x failed to erase options");
  940. return ERROR_OK;
  941. }
  942. stm32x_info->option_bytes.user_options = optionbyte;
  943. if (stm32x_write_options(bank) != ERROR_OK)
  944. {
  945. command_print(cmd_ctx, "stm32x failed to write options");
  946. return ERROR_OK;
  947. }
  948. command_print(cmd_ctx, "stm32x write options complete");
  949. return ERROR_OK;
  950. }
  951. static int stm32x_mass_erase(struct flash_bank_s *bank)
  952. {
  953. target_t *target = bank->target;
  954. u32 status;
  955. if (target->state != TARGET_HALTED)
  956. {
  957. LOG_ERROR("Target not halted");
  958. return ERROR_TARGET_NOT_HALTED;
  959. }
  960. /* unlock option flash registers */
  961. target_write_u32(target, STM32_FLASH_KEYR, KEY1);
  962. target_write_u32(target, STM32_FLASH_KEYR, KEY2);
  963. /* mass erase flash memory */
  964. target_write_u32(target, STM32_FLASH_CR, FLASH_MER);
  965. target_write_u32(target, STM32_FLASH_CR, FLASH_MER|FLASH_STRT);
  966. status = stm32x_wait_status_busy(bank, 10);
  967. target_write_u32(target, STM32_FLASH_CR, FLASH_LOCK);
  968. if( status & FLASH_WRPRTERR )
  969. {
  970. LOG_ERROR("stm32x device protected");
  971. return ERROR_OK;
  972. }
  973. if( status & FLASH_PGERR )
  974. {
  975. LOG_ERROR("stm32x device programming failed");
  976. return ERROR_OK;
  977. }
  978. return ERROR_OK;
  979. }
  980. static int stm32x_handle_mass_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  981. {
  982. flash_bank_t *bank;
  983. int i;
  984. if (argc < 1)
  985. {
  986. command_print(cmd_ctx, "stm32x mass_erase <bank>");
  987. return ERROR_OK;
  988. }
  989. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  990. if (!bank)
  991. {
  992. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  993. return ERROR_OK;
  994. }
  995. if (stm32x_mass_erase(bank) == ERROR_OK)
  996. {
  997. /* set all sectors as erased */
  998. for (i = 0; i < bank->num_sectors; i++)
  999. {
  1000. bank->sectors[i].is_erased = 1;
  1001. }
  1002. command_print(cmd_ctx, "stm32x mass erase complete");
  1003. }
  1004. else
  1005. {
  1006. command_print(cmd_ctx, "stm32x mass erase failed");
  1007. }
  1008. return ERROR_OK;
  1009. }