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.
 
 
 
 
 
 

818 lines
24 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * LPC1700 support Copyright (C) 2009 by Audrius Urmanavicius *
  6. * didele.deze@gmail.com *
  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 "lpc2000.h"
  27. #include "armv4_5.h"
  28. #include "armv7m.h"
  29. #include "binarybuffer.h"
  30. /* flash programming support for NXP LPC17xx and LPC2xxx devices
  31. * currently supported devices:
  32. * variant 1 (lpc2000_v1):
  33. * - 2104 | 5 | 6
  34. * - 2114 | 9
  35. * - 2124 | 9
  36. * - 2194
  37. * - 2212 | 4
  38. * - 2292 | 4
  39. *
  40. * variant 2 (lpc2000_v2):
  41. * - 213x
  42. * - 214x
  43. * - 2101 | 2 | 3
  44. * - 2364 | 6 | 8
  45. * - 2378
  46. *
  47. * lpc1700:
  48. * - 175x
  49. * - 176x (tested with LPC1768)
  50. */
  51. static int lpc2000_register_commands(struct command_context_s *cmd_ctx);
  52. static int lpc2000_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
  53. static int lpc2000_erase(struct flash_bank_s *bank, int first, int last);
  54. static int lpc2000_protect(struct flash_bank_s *bank, int set, int first, int last);
  55. static int lpc2000_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
  56. static int lpc2000_probe(struct flash_bank_s *bank);
  57. static int lpc2000_erase_check(struct flash_bank_s *bank);
  58. static int lpc2000_protect_check(struct flash_bank_s *bank);
  59. static int lpc2000_info(struct flash_bank_s *bank, char *buf, int buf_size);
  60. static int lpc2000_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  61. flash_driver_t lpc2000_flash =
  62. {
  63. .name = "lpc2000",
  64. .register_commands = lpc2000_register_commands,
  65. .flash_bank_command = lpc2000_flash_bank_command,
  66. .erase = lpc2000_erase,
  67. .protect = lpc2000_protect,
  68. .write = lpc2000_write,
  69. .probe = lpc2000_probe,
  70. .auto_probe = lpc2000_probe,
  71. .erase_check = lpc2000_erase_check,
  72. .protect_check = lpc2000_protect_check,
  73. .info = lpc2000_info
  74. };
  75. static int lpc2000_register_commands(struct command_context_s *cmd_ctx)
  76. {
  77. command_t *lpc2000_cmd = register_command(cmd_ctx, NULL, "lpc2000", NULL, COMMAND_ANY, NULL);
  78. register_command(cmd_ctx, lpc2000_cmd, "part_id", lpc2000_handle_part_id_command, COMMAND_EXEC,
  79. "print part id of lpc2000 flash bank <num>");
  80. return ERROR_OK;
  81. }
  82. static int lpc2000_build_sector_list(struct flash_bank_s *bank)
  83. {
  84. lpc2000_flash_bank_t *lpc2000_info = bank->driver_priv;
  85. int i;
  86. uint32_t offset = 0;
  87. /* default to a 4096 write buffer */
  88. lpc2000_info->cmd51_max_buffer = 4096;
  89. if (lpc2000_info->variant == lpc2000_v1)
  90. {
  91. /* variant 1 has different layout for 128kb and 256kb flashes */
  92. if (bank->size == 128 * 1024)
  93. {
  94. bank->num_sectors = 16;
  95. bank->sectors = malloc(sizeof(flash_sector_t) * 16);
  96. for (i = 0; i < 16; i++)
  97. {
  98. bank->sectors[i].offset = offset;
  99. bank->sectors[i].size = 8 * 1024;
  100. offset += bank->sectors[i].size;
  101. bank->sectors[i].is_erased = -1;
  102. bank->sectors[i].is_protected = 1;
  103. }
  104. }
  105. else if (bank->size == 256 * 1024)
  106. {
  107. bank->num_sectors = 18;
  108. bank->sectors = malloc(sizeof(flash_sector_t) * 18);
  109. for (i = 0; i < 8; i++)
  110. {
  111. bank->sectors[i].offset = offset;
  112. bank->sectors[i].size = 8 * 1024;
  113. offset += bank->sectors[i].size;
  114. bank->sectors[i].is_erased = -1;
  115. bank->sectors[i].is_protected = 1;
  116. }
  117. for (i = 8; i < 10; i++)
  118. {
  119. bank->sectors[i].offset = offset;
  120. bank->sectors[i].size = 64 * 1024;
  121. offset += bank->sectors[i].size;
  122. bank->sectors[i].is_erased = -1;
  123. bank->sectors[i].is_protected = 1;
  124. }
  125. for (i = 10; i < 18; i++)
  126. {
  127. bank->sectors[i].offset = offset;
  128. bank->sectors[i].size = 8 * 1024;
  129. offset += bank->sectors[i].size;
  130. bank->sectors[i].is_erased = -1;
  131. bank->sectors[i].is_protected = 1;
  132. }
  133. }
  134. else
  135. {
  136. LOG_ERROR("BUG: unknown bank->size encountered");
  137. exit(-1);
  138. }
  139. }
  140. else if (lpc2000_info->variant == lpc2000_v2)
  141. {
  142. /* variant 2 has a uniform layout, only number of sectors differs */
  143. switch (bank->size)
  144. {
  145. case 4 * 1024:
  146. lpc2000_info->cmd51_max_buffer = 1024;
  147. bank->num_sectors = 1;
  148. break;
  149. case 8 * 1024:
  150. lpc2000_info->cmd51_max_buffer = 1024;
  151. bank->num_sectors = 2;
  152. break;
  153. case 16 * 1024:
  154. bank->num_sectors = 4;
  155. break;
  156. case 32 * 1024:
  157. bank->num_sectors = 8;
  158. break;
  159. case 64 * 1024:
  160. bank->num_sectors = 9;
  161. break;
  162. case 128 * 1024:
  163. bank->num_sectors = 11;
  164. break;
  165. case 256 * 1024:
  166. bank->num_sectors = 15;
  167. break;
  168. case 512 * 1024:
  169. case 500 * 1024:
  170. bank->num_sectors = 27;
  171. break;
  172. default:
  173. LOG_ERROR("BUG: unknown bank->size encountered");
  174. exit(-1);
  175. break;
  176. }
  177. bank->sectors = malloc(sizeof(flash_sector_t) * bank->num_sectors);
  178. for (i = 0; i < bank->num_sectors; i++)
  179. {
  180. if ((i >= 0) && (i < 8))
  181. {
  182. bank->sectors[i].offset = offset;
  183. bank->sectors[i].size = 4 * 1024;
  184. offset += bank->sectors[i].size;
  185. bank->sectors[i].is_erased = -1;
  186. bank->sectors[i].is_protected = 1;
  187. }
  188. if ((i >= 8) && (i < 22))
  189. {
  190. bank->sectors[i].offset = offset;
  191. bank->sectors[i].size = 32 * 1024;
  192. offset += bank->sectors[i].size;
  193. bank->sectors[i].is_erased = -1;
  194. bank->sectors[i].is_protected = 1;
  195. }
  196. if ((i >= 22) && (i < 27))
  197. {
  198. bank->sectors[i].offset = offset;
  199. bank->sectors[i].size = 4 * 1024;
  200. offset += bank->sectors[i].size;
  201. bank->sectors[i].is_erased = -1;
  202. bank->sectors[i].is_protected = 1;
  203. }
  204. }
  205. }
  206. else if (lpc2000_info->variant == lpc1700)
  207. {
  208. switch(bank->size)
  209. {
  210. case 32 * 1024:
  211. bank->num_sectors = 8;
  212. break;
  213. case 64 * 1024:
  214. bank->num_sectors = 16;
  215. break;
  216. case 128 * 1024:
  217. bank->num_sectors = 18;
  218. break;
  219. case 256 * 1024:
  220. bank->num_sectors = 22;
  221. break;
  222. case 512 * 1024:
  223. bank->num_sectors = 30;
  224. break;
  225. default:
  226. LOG_ERROR("BUG: unknown bank->size encountered");
  227. exit(-1);
  228. }
  229. bank->sectors = malloc(sizeof(flash_sector_t) * bank->num_sectors);
  230. for(i = 0; i < bank->num_sectors; i++)
  231. {
  232. bank->sectors[i].offset = offset;
  233. /* sectors 0-15 are 4kB-sized, 16 and above are 32kB-sized for LPC17xx devices */
  234. bank->sectors[i].size = (i < 16)? 4 * 1024 : 32 * 1024;
  235. offset += bank->sectors[i].size;
  236. bank->sectors[i].is_erased = -1;
  237. bank->sectors[i].is_protected = 1;
  238. }
  239. }
  240. else
  241. {
  242. LOG_ERROR("BUG: unknown lpc2000_info->variant encountered");
  243. exit(-1);
  244. }
  245. return ERROR_OK;
  246. }
  247. /* call LPC1700/LPC2000 IAP function
  248. * uses 180 bytes working area
  249. * 0x0 to 0x7: jump gate (BX to thumb state, b -2 to wait)
  250. * 0x8 to 0x1f: command parameter table (1+5 words)
  251. * 0x20 to 0x33: command result table (1+4 words)
  252. * 0x34 to 0xb3: stack (only 128b needed)
  253. */
  254. static int lpc2000_iap_call(flash_bank_t *bank, int code, uint32_t param_table[5], uint32_t result_table[4])
  255. {
  256. int retval;
  257. lpc2000_flash_bank_t *lpc2000_info = bank->driver_priv;
  258. target_t *target = bank->target;
  259. mem_param_t mem_params[2];
  260. reg_param_t reg_params[5];
  261. armv4_5_algorithm_t armv4_5_info; /* for LPC2000 */
  262. armv7m_algorithm_t armv7m_info; /* for LPC1700 */
  263. uint32_t status_code;
  264. uint32_t iap_entry_point = 0; /* to make compiler happier */
  265. /* regrab previously allocated working_area, or allocate a new one */
  266. if (!lpc2000_info->iap_working_area)
  267. {
  268. uint8_t jump_gate[8];
  269. /* make sure we have a working area */
  270. if (target_alloc_working_area(target, 180, &lpc2000_info->iap_working_area) != ERROR_OK)
  271. {
  272. LOG_ERROR("no working area specified, can't write LPC2000 internal flash");
  273. return ERROR_FLASH_OPERATION_FAILED;
  274. }
  275. /* write IAP code to working area */
  276. switch(lpc2000_info->variant)
  277. {
  278. case lpc1700:
  279. target_buffer_set_u32(target, jump_gate, ARMV7M_T_BX(12));
  280. target_buffer_set_u32(target, jump_gate + 4, ARMV7M_T_B(0xfffffe));
  281. break;
  282. case lpc2000_v1:
  283. case lpc2000_v2:
  284. target_buffer_set_u32(target, jump_gate, ARMV4_5_BX(12));
  285. target_buffer_set_u32(target, jump_gate + 4, ARMV4_5_B(0xfffffe, 0));
  286. break;
  287. default:
  288. LOG_ERROR("BUG: unknown bank->size encountered");
  289. exit(-1);
  290. }
  291. if ((retval = target_write_memory(target, lpc2000_info->iap_working_area->address, 4, 2, jump_gate)) != ERROR_OK)
  292. {
  293. LOG_ERROR("Write memory at address 0x%8.8" PRIx32 " failed (check work_area definition)", lpc2000_info->iap_working_area->address);
  294. return retval;
  295. }
  296. }
  297. switch(lpc2000_info->variant)
  298. {
  299. case lpc1700:
  300. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  301. armv7m_info.core_mode = ARMV7M_MODE_ANY;
  302. iap_entry_point = 0x1fff1ff1;
  303. break;
  304. case lpc2000_v1:
  305. case lpc2000_v2:
  306. armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
  307. armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
  308. armv4_5_info.core_state = ARMV4_5_STATE_ARM;
  309. iap_entry_point = 0x7ffffff1;
  310. break;
  311. default:
  312. LOG_ERROR("BUG: unknown lpc2000->variant encountered");
  313. exit(-1);
  314. }
  315. /* command parameter table */
  316. init_mem_param(&mem_params[0], lpc2000_info->iap_working_area->address + 8, 6 * 4, PARAM_OUT);
  317. target_buffer_set_u32(target, mem_params[0].value, code);
  318. target_buffer_set_u32(target, mem_params[0].value + 0x04, param_table[0]);
  319. target_buffer_set_u32(target, mem_params[0].value + 0x08, param_table[1]);
  320. target_buffer_set_u32(target, mem_params[0].value + 0x0c, param_table[2]);
  321. target_buffer_set_u32(target, mem_params[0].value + 0x10, param_table[3]);
  322. target_buffer_set_u32(target, mem_params[0].value + 0x14, param_table[4]);
  323. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  324. buf_set_u32(reg_params[0].value, 0, 32, lpc2000_info->iap_working_area->address + 0x08);
  325. /* command result table */
  326. init_mem_param(&mem_params[1], lpc2000_info->iap_working_area->address + 0x20, 5 * 4, PARAM_IN);
  327. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  328. buf_set_u32(reg_params[1].value, 0, 32, lpc2000_info->iap_working_area->address + 0x20);
  329. /* IAP entry point */
  330. init_reg_param(&reg_params[2], "r12", 32, PARAM_OUT);
  331. buf_set_u32(reg_params[2].value, 0, 32, iap_entry_point);
  332. switch(lpc2000_info->variant)
  333. {
  334. case lpc1700:
  335. /* IAP stack */
  336. init_reg_param(&reg_params[3], "sp", 32, PARAM_OUT);
  337. buf_set_u32(reg_params[3].value, 0, 32, lpc2000_info->iap_working_area->address + 0xb4);
  338. /* return address */
  339. init_reg_param(&reg_params[4], "lr", 32, PARAM_OUT);
  340. buf_set_u32(reg_params[4].value, 0, 32, (lpc2000_info->iap_working_area->address + 0x04) | 1); /* bit0 of LR = 1 to return in Thumb mode */
  341. target_run_algorithm(target, 2, mem_params, 5, reg_params, lpc2000_info->iap_working_area->address, lpc2000_info->iap_working_area->address + 0x4, 10000, &armv7m_info);
  342. break;
  343. case lpc2000_v1:
  344. case lpc2000_v2:
  345. /* IAP stack */
  346. init_reg_param(&reg_params[3], "r13_svc", 32, PARAM_OUT);
  347. buf_set_u32(reg_params[3].value, 0, 32, lpc2000_info->iap_working_area->address + 0xb4);
  348. /* return address */
  349. init_reg_param(&reg_params[4], "lr_svc", 32, PARAM_OUT);
  350. buf_set_u32(reg_params[4].value, 0, 32, lpc2000_info->iap_working_area->address + 0x04);
  351. target_run_algorithm(target, 2, mem_params, 5, reg_params, lpc2000_info->iap_working_area->address, lpc2000_info->iap_working_area->address + 0x4, 10000, &armv4_5_info);
  352. break;
  353. default:
  354. LOG_ERROR("BUG: unknown lpc2000->variant encountered");
  355. exit(-1);
  356. }
  357. status_code = target_buffer_get_u32(target, mem_params[1].value);
  358. result_table[0] = target_buffer_get_u32(target, mem_params[1].value + 0x04);
  359. result_table[1] = target_buffer_get_u32(target, mem_params[1].value + 0x08);
  360. result_table[2] = target_buffer_get_u32(target, mem_params[1].value + 0x0c);
  361. result_table[3] = target_buffer_get_u32(target, mem_params[1].value + 0x10);
  362. LOG_DEBUG("IAP command = %i (0x%8.8" PRIx32", 0x%8.8" PRIx32", 0x%8.8" PRIx32", 0x%8.8" PRIx32", 0x%8.8" PRIx32") completed with result = %8.8" PRIx32,
  363. code, param_table[0], param_table[1], param_table[2], param_table[3], param_table[4], status_code);
  364. destroy_mem_param(&mem_params[0]);
  365. destroy_mem_param(&mem_params[1]);
  366. destroy_reg_param(&reg_params[0]);
  367. destroy_reg_param(&reg_params[1]);
  368. destroy_reg_param(&reg_params[2]);
  369. destroy_reg_param(&reg_params[3]);
  370. destroy_reg_param(&reg_params[4]);
  371. return status_code;
  372. }
  373. static int lpc2000_iap_blank_check(struct flash_bank_s *bank, int first, int last)
  374. {
  375. uint32_t param_table[5];
  376. uint32_t result_table[4];
  377. int status_code;
  378. int i;
  379. if ((first < 0) || (last >= bank->num_sectors))
  380. return ERROR_FLASH_SECTOR_INVALID;
  381. for (i = first; i <= last; i++)
  382. {
  383. /* check single sector */
  384. param_table[0] = param_table[1] = i;
  385. status_code = lpc2000_iap_call(bank, 53, param_table, result_table);
  386. switch (status_code)
  387. {
  388. case ERROR_FLASH_OPERATION_FAILED:
  389. return ERROR_FLASH_OPERATION_FAILED;
  390. case LPC2000_CMD_SUCCESS:
  391. bank->sectors[i].is_erased = 1;
  392. break;
  393. case LPC2000_SECTOR_NOT_BLANK:
  394. bank->sectors[i].is_erased = 0;
  395. break;
  396. case LPC2000_INVALID_SECTOR:
  397. bank->sectors[i].is_erased = 0;
  398. break;
  399. case LPC2000_BUSY:
  400. return ERROR_FLASH_BUSY;
  401. break;
  402. default:
  403. LOG_ERROR("BUG: unknown LPC2000 status code %i", status_code);
  404. exit(-1);
  405. }
  406. }
  407. return ERROR_OK;
  408. }
  409. /*
  410. * flash bank lpc2000 <base> <size> 0 0 <target#> <lpc_variant> <cclk> [calc_checksum]
  411. */
  412. static int lpc2000_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
  413. {
  414. lpc2000_flash_bank_t *lpc2000_info;
  415. if (argc < 8)
  416. {
  417. LOG_WARNING("incomplete flash_bank lpc2000 configuration");
  418. return ERROR_FLASH_BANK_INVALID;
  419. }
  420. lpc2000_info = malloc(sizeof(lpc2000_flash_bank_t));
  421. bank->driver_priv = lpc2000_info;
  422. if (strcmp(args[6], "lpc2000_v1") == 0)
  423. {
  424. lpc2000_info->variant = lpc2000_v1;
  425. lpc2000_info->cmd51_dst_boundary = 512;
  426. lpc2000_info->cmd51_can_256b = 0;
  427. lpc2000_info->cmd51_can_8192b = 1;
  428. lpc2000_info->checksum_vector = 5;
  429. }
  430. else if (strcmp(args[6], "lpc2000_v2") == 0)
  431. {
  432. lpc2000_info->variant = lpc2000_v2;
  433. lpc2000_info->cmd51_dst_boundary = 256;
  434. lpc2000_info->cmd51_can_256b = 1;
  435. lpc2000_info->cmd51_can_8192b = 0;
  436. lpc2000_info->checksum_vector = 5;
  437. }
  438. else if (strcmp(args[6], "lpc1700") == 0)
  439. {
  440. lpc2000_info->variant = lpc1700;
  441. lpc2000_info->cmd51_dst_boundary = 256;
  442. lpc2000_info->cmd51_can_256b = 1;
  443. lpc2000_info->cmd51_can_8192b = 0;
  444. lpc2000_info->checksum_vector = 7;
  445. }
  446. else
  447. {
  448. LOG_ERROR("unknown LPC2000 variant: %s", args[6]);
  449. free(lpc2000_info);
  450. return ERROR_FLASH_BANK_INVALID;
  451. }
  452. lpc2000_info->iap_working_area = NULL;
  453. lpc2000_info->cclk = strtoul(args[7], NULL, 0);
  454. lpc2000_info->calc_checksum = 0;
  455. lpc2000_build_sector_list(bank);
  456. if (argc >= 9)
  457. {
  458. if (strcmp(args[8], "calc_checksum") == 0)
  459. lpc2000_info->calc_checksum = 1;
  460. }
  461. return ERROR_OK;
  462. }
  463. static int lpc2000_erase(struct flash_bank_s *bank, int first, int last)
  464. {
  465. lpc2000_flash_bank_t *lpc2000_info = bank->driver_priv;
  466. uint32_t param_table[5];
  467. uint32_t result_table[4];
  468. int status_code;
  469. if (bank->target->state != TARGET_HALTED)
  470. {
  471. LOG_ERROR("Target not halted");
  472. return ERROR_TARGET_NOT_HALTED;
  473. }
  474. param_table[0] = first;
  475. param_table[1] = last;
  476. param_table[2] = lpc2000_info->cclk;
  477. /* Prepare sectors */
  478. status_code = lpc2000_iap_call(bank, 50, param_table, result_table);
  479. switch (status_code)
  480. {
  481. case ERROR_FLASH_OPERATION_FAILED:
  482. return ERROR_FLASH_OPERATION_FAILED;
  483. case LPC2000_CMD_SUCCESS:
  484. break;
  485. case LPC2000_INVALID_SECTOR:
  486. return ERROR_FLASH_SECTOR_INVALID;
  487. break;
  488. default:
  489. LOG_WARNING("lpc2000 prepare sectors returned %i", status_code);
  490. return ERROR_FLASH_OPERATION_FAILED;
  491. }
  492. /* Erase sectors */
  493. status_code = lpc2000_iap_call(bank, 52, param_table, result_table);
  494. switch (status_code)
  495. {
  496. case ERROR_FLASH_OPERATION_FAILED:
  497. return ERROR_FLASH_OPERATION_FAILED;
  498. case LPC2000_CMD_SUCCESS:
  499. break;
  500. case LPC2000_INVALID_SECTOR:
  501. return ERROR_FLASH_SECTOR_INVALID;
  502. break;
  503. default:
  504. LOG_WARNING("lpc2000 erase sectors returned %i", status_code);
  505. return ERROR_FLASH_OPERATION_FAILED;
  506. }
  507. return ERROR_OK;
  508. }
  509. static int lpc2000_protect(struct flash_bank_s *bank, int set, int first, int last)
  510. {
  511. /* can't protect/unprotect on the lpc2000 */
  512. return ERROR_OK;
  513. }
  514. static int lpc2000_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  515. {
  516. lpc2000_flash_bank_t *lpc2000_info = bank->driver_priv;
  517. target_t *target = bank->target;
  518. uint32_t dst_min_alignment;
  519. uint32_t bytes_remaining = count;
  520. uint32_t bytes_written = 0;
  521. int first_sector = 0;
  522. int last_sector = 0;
  523. uint32_t param_table[5];
  524. uint32_t result_table[4];
  525. int status_code;
  526. int i;
  527. working_area_t *download_area;
  528. int retval = ERROR_OK;
  529. if (bank->target->state != TARGET_HALTED)
  530. {
  531. LOG_ERROR("Target not halted");
  532. return ERROR_TARGET_NOT_HALTED;
  533. }
  534. if (offset + count > bank->size)
  535. return ERROR_FLASH_DST_OUT_OF_BANK;
  536. dst_min_alignment = lpc2000_info->cmd51_dst_boundary;
  537. if (offset % dst_min_alignment)
  538. {
  539. LOG_WARNING("offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32, offset, dst_min_alignment);
  540. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  541. }
  542. for (i = 0; i < bank->num_sectors; i++)
  543. {
  544. if (offset >= bank->sectors[i].offset)
  545. first_sector = i;
  546. if (offset + CEIL(count, dst_min_alignment) * dst_min_alignment > bank->sectors[i].offset)
  547. last_sector = i;
  548. }
  549. LOG_DEBUG("first_sector: %i, last_sector: %i", first_sector, last_sector);
  550. /* check if exception vectors should be flashed */
  551. if ((offset == 0) && (count >= 0x20) && lpc2000_info->calc_checksum)
  552. {
  553. uint32_t checksum = 0;
  554. int i;
  555. for (i = 0; i < 8; i++)
  556. {
  557. LOG_DEBUG("Vector 0x%2.2x: 0x%8.8" PRIx32, i * 4, buf_get_u32(buffer + (i * 4), 0, 32));
  558. if (i != lpc2000_info->checksum_vector)
  559. checksum += buf_get_u32(buffer + (i * 4), 0, 32);
  560. }
  561. checksum = 0 - checksum;
  562. LOG_DEBUG("checksum: 0x%8.8" PRIx32, checksum);
  563. uint32_t original_value = buf_get_u32(buffer + (lpc2000_info->checksum_vector * 4), 0, 32);
  564. if (original_value != checksum)
  565. {
  566. LOG_WARNING("Verification will fail since checksum in image (0x%8.8" PRIx32 ") to be written to flash is different from calculated vector checksum (0x%8.8" PRIx32 ").",
  567. original_value, checksum);
  568. LOG_WARNING("To remove this warning modify build tools on developer PC to inject correct LPC vector checksum.");
  569. }
  570. buf_set_u32(buffer + (lpc2000_info->checksum_vector * 4), 0, 32, checksum);
  571. }
  572. /* allocate a working area */
  573. if (target_alloc_working_area(target, lpc2000_info->cmd51_max_buffer, &download_area) != ERROR_OK)
  574. {
  575. LOG_ERROR("no working area specified, can't write LPC2000 internal flash");
  576. return ERROR_FLASH_OPERATION_FAILED;
  577. }
  578. while (bytes_remaining > 0)
  579. {
  580. uint32_t thisrun_bytes;
  581. if (bytes_remaining >= lpc2000_info->cmd51_max_buffer)
  582. thisrun_bytes = lpc2000_info->cmd51_max_buffer;
  583. else if (bytes_remaining >= 1024)
  584. thisrun_bytes = 1024;
  585. else if ((bytes_remaining >= 512) || (!lpc2000_info->cmd51_can_256b))
  586. thisrun_bytes = 512;
  587. else
  588. thisrun_bytes = 256;
  589. /* Prepare sectors */
  590. param_table[0] = first_sector;
  591. param_table[1] = last_sector;
  592. status_code = lpc2000_iap_call(bank, 50, param_table, result_table);
  593. switch (status_code)
  594. {
  595. case ERROR_FLASH_OPERATION_FAILED:
  596. retval = ERROR_FLASH_OPERATION_FAILED;
  597. break;
  598. case LPC2000_CMD_SUCCESS:
  599. break;
  600. case LPC2000_INVALID_SECTOR:
  601. retval = ERROR_FLASH_SECTOR_INVALID;
  602. break;
  603. default:
  604. LOG_WARNING("lpc2000 prepare sectors returned %i", status_code);
  605. retval = ERROR_FLASH_OPERATION_FAILED;
  606. break;
  607. }
  608. /* Exit if error occured */
  609. if (retval != ERROR_OK)
  610. break;
  611. if (bytes_remaining >= thisrun_bytes)
  612. {
  613. if ((retval = target_write_buffer(bank->target, download_area->address, thisrun_bytes, buffer + bytes_written)) != ERROR_OK)
  614. {
  615. retval = ERROR_FLASH_OPERATION_FAILED;
  616. break;
  617. }
  618. }
  619. else
  620. {
  621. uint8_t *last_buffer = malloc(thisrun_bytes);
  622. memcpy(last_buffer, buffer + bytes_written, bytes_remaining);
  623. memset(last_buffer + bytes_remaining, 0xff, thisrun_bytes - bytes_remaining);
  624. target_write_buffer(bank->target, download_area->address, thisrun_bytes, last_buffer);
  625. free(last_buffer);
  626. }
  627. LOG_DEBUG("writing 0x%" PRIx32 " bytes to address 0x%" PRIx32 , thisrun_bytes, bank->base + offset + bytes_written);
  628. /* Write data */
  629. param_table[0] = bank->base + offset + bytes_written;
  630. param_table[1] = download_area->address;
  631. param_table[2] = thisrun_bytes;
  632. param_table[3] = lpc2000_info->cclk;
  633. status_code = lpc2000_iap_call(bank, 51, param_table, result_table);
  634. switch (status_code)
  635. {
  636. case ERROR_FLASH_OPERATION_FAILED:
  637. retval = ERROR_FLASH_OPERATION_FAILED;
  638. break;
  639. case LPC2000_CMD_SUCCESS:
  640. break;
  641. case LPC2000_INVALID_SECTOR:
  642. retval = ERROR_FLASH_SECTOR_INVALID;
  643. break;
  644. default:
  645. LOG_WARNING("lpc2000 returned %i", status_code);
  646. retval = ERROR_FLASH_OPERATION_FAILED;
  647. break;
  648. }
  649. /* Exit if error occured */
  650. if (retval != ERROR_OK)
  651. break;
  652. if (bytes_remaining > thisrun_bytes)
  653. bytes_remaining -= thisrun_bytes;
  654. else
  655. bytes_remaining = 0;
  656. bytes_written += thisrun_bytes;
  657. }
  658. target_free_working_area(target, download_area);
  659. return retval;
  660. }
  661. static int lpc2000_probe(struct flash_bank_s *bank)
  662. {
  663. /* we can't probe on an lpc2000
  664. * if this is an lpc2xxx, it has the configured flash
  665. */
  666. return ERROR_OK;
  667. }
  668. static int lpc2000_erase_check(struct flash_bank_s *bank)
  669. {
  670. if (bank->target->state != TARGET_HALTED)
  671. {
  672. LOG_ERROR("Target not halted");
  673. return ERROR_TARGET_NOT_HALTED;
  674. }
  675. return lpc2000_iap_blank_check(bank, 0, bank->num_sectors - 1);
  676. }
  677. static int lpc2000_protect_check(struct flash_bank_s *bank)
  678. {
  679. /* sectors are always protected */
  680. return ERROR_OK;
  681. }
  682. static int lpc2000_info(struct flash_bank_s *bank, char *buf, int buf_size)
  683. {
  684. lpc2000_flash_bank_t *lpc2000_info = bank->driver_priv;
  685. snprintf(buf, buf_size, "lpc2000 flash driver variant: %i, clk: %" PRIi32 "kHz" , lpc2000_info->variant, lpc2000_info->cclk);
  686. return ERROR_OK;
  687. }
  688. static int lpc2000_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  689. {
  690. flash_bank_t *bank;
  691. uint32_t param_table[5];
  692. uint32_t result_table[4];
  693. int status_code;
  694. if (argc < 1)
  695. {
  696. return ERROR_COMMAND_SYNTAX_ERROR;
  697. }
  698. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  699. if (!bank)
  700. {
  701. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  702. return ERROR_OK;
  703. }
  704. if (bank->target->state != TARGET_HALTED)
  705. {
  706. LOG_ERROR("Target not halted");
  707. return ERROR_TARGET_NOT_HALTED;
  708. }
  709. if ((status_code = lpc2000_iap_call(bank, 54, param_table, result_table)) != 0x0)
  710. {
  711. if (status_code == ERROR_FLASH_OPERATION_FAILED)
  712. {
  713. command_print(cmd_ctx, "no sufficient working area specified, can't access LPC2000 IAP interface");
  714. return ERROR_OK;
  715. }
  716. command_print(cmd_ctx, "lpc2000 IAP returned status code %i", status_code);
  717. }
  718. else
  719. {
  720. command_print(cmd_ctx, "lpc2000 part id: 0x%8.8" PRIx32 , result_table[0]);
  721. }
  722. return ERROR_OK;
  723. }