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.
 
 
 
 
 
 

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