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.
 
 
 
 
 
 

709 lines
20 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "lpc2000.h"
  24. #include "flash.h"
  25. #include "target.h"
  26. #include "log.h"
  27. #include "armv4_5.h"
  28. #include "algorithm.h"
  29. #include "binarybuffer.h"
  30. #include <stdlib.h>
  31. #include <string.h>
  32. /* flash programming support for Philips LPC2xxx devices
  33. * currently supported devices:
  34. * variant 1 (lpc2000_v1):
  35. * - 2104|5|6
  36. * - 2114|9
  37. * - 2124|9
  38. * - 2194
  39. * - 2212|4
  40. * - 2292|4
  41. *
  42. * variant 2 (lpc2000_v2):
  43. * - 213x
  44. * - 214x
  45. * - 2101|2|3
  46. * - 2364|6|8
  47. * - 2378
  48. */
  49. int lpc2000_register_commands(struct command_context_s *cmd_ctx);
  50. int lpc2000_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
  51. int lpc2000_erase(struct flash_bank_s *bank, int first, int last);
  52. int lpc2000_protect(struct flash_bank_s *bank, int set, int first, int last);
  53. int lpc2000_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
  54. int lpc2000_probe(struct flash_bank_s *bank);
  55. int lpc2000_erase_check(struct flash_bank_s *bank);
  56. int lpc2000_protect_check(struct flash_bank_s *bank);
  57. int lpc2000_info(struct flash_bank_s *bank, char *buf, int buf_size);
  58. int lpc2000_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  59. flash_driver_t lpc2000_flash =
  60. {
  61. .name = "lpc2000",
  62. .register_commands = lpc2000_register_commands,
  63. .flash_bank_command = lpc2000_flash_bank_command,
  64. .erase = lpc2000_erase,
  65. .protect = lpc2000_protect,
  66. .write = lpc2000_write,
  67. .probe = lpc2000_probe,
  68. .auto_probe = lpc2000_probe,
  69. .erase_check = lpc2000_erase_check,
  70. .protect_check = lpc2000_protect_check,
  71. .info = lpc2000_info
  72. };
  73. int lpc2000_register_commands(struct command_context_s *cmd_ctx)
  74. {
  75. command_t *lpc2000_cmd = register_command(cmd_ctx, NULL, "lpc2000", NULL, COMMAND_ANY, NULL);
  76. register_command(cmd_ctx, lpc2000_cmd, "part_id", lpc2000_handle_part_id_command, COMMAND_EXEC,
  77. "print part id of lpc2000 flash bank <num>");
  78. return ERROR_OK;
  79. }
  80. int lpc2000_build_sector_list(struct flash_bank_s *bank)
  81. {
  82. lpc2000_flash_bank_t *lpc2000_info = bank->driver_priv;
  83. /* default to a 4096 write buffer */
  84. lpc2000_info->cmd51_max_buffer = 4096;
  85. if (lpc2000_info->variant == 1)
  86. {
  87. int i = 0;
  88. u32 offset = 0;
  89. /* variant 1 has different layout for 128kb and 256kb flashes */
  90. if (bank->size == 128 * 1024)
  91. {
  92. bank->num_sectors = 16;
  93. bank->sectors = malloc(sizeof(flash_sector_t) * 16);
  94. for (i = 0; i < 16; i++)
  95. {
  96. bank->sectors[i].offset = offset;
  97. bank->sectors[i].size = 8 * 1024;
  98. offset += bank->sectors[i].size;
  99. bank->sectors[i].is_erased = -1;
  100. bank->sectors[i].is_protected = 1;
  101. }
  102. }
  103. else if (bank->size == 256 * 1024)
  104. {
  105. bank->num_sectors = 18;
  106. bank->sectors = malloc(sizeof(flash_sector_t) * 18);
  107. for (i = 0; i < 8; i++)
  108. {
  109. bank->sectors[i].offset = offset;
  110. bank->sectors[i].size = 8 * 1024;
  111. offset += bank->sectors[i].size;
  112. bank->sectors[i].is_erased = -1;
  113. bank->sectors[i].is_protected = 1;
  114. }
  115. for (i = 8; i < 10; i++)
  116. {
  117. bank->sectors[i].offset = offset;
  118. bank->sectors[i].size = 64 * 1024;
  119. offset += bank->sectors[i].size;
  120. bank->sectors[i].is_erased = -1;
  121. bank->sectors[i].is_protected = 1;
  122. }
  123. for (i = 10; i < 18; i++)
  124. {
  125. bank->sectors[i].offset = offset;
  126. bank->sectors[i].size = 8 * 1024;
  127. offset += bank->sectors[i].size;
  128. bank->sectors[i].is_erased = -1;
  129. bank->sectors[i].is_protected = 1;
  130. }
  131. }
  132. else
  133. {
  134. LOG_ERROR("BUG: unknown bank->size encountered");
  135. exit(-1);
  136. }
  137. }
  138. else if (lpc2000_info->variant == 2)
  139. {
  140. int num_sectors;
  141. int i;
  142. u32 offset = 0;
  143. /* variant 2 has a uniform layout, only number of sectors differs */
  144. switch (bank->size)
  145. {
  146. case 4 * 1024:
  147. lpc2000_info->cmd51_max_buffer = 1024;
  148. num_sectors = 1;
  149. break;
  150. case 8 * 1024:
  151. lpc2000_info->cmd51_max_buffer = 1024;
  152. num_sectors = 2;
  153. break;
  154. case 16 * 1024:
  155. num_sectors = 4;
  156. break;
  157. case 32 * 1024:
  158. num_sectors = 8;
  159. break;
  160. case 64 * 1024:
  161. num_sectors = 9;
  162. break;
  163. case 128 * 1024:
  164. num_sectors = 11;
  165. break;
  166. case 256 * 1024:
  167. num_sectors = 15;
  168. break;
  169. case 512 * 1024:
  170. case 500 * 1024:
  171. num_sectors = 27;
  172. break;
  173. default:
  174. LOG_ERROR("BUG: unknown bank->size encountered");
  175. exit(-1);
  176. break;
  177. }
  178. bank->num_sectors = num_sectors;
  179. bank->sectors = malloc(sizeof(flash_sector_t) * num_sectors);
  180. for (i = 0; i < num_sectors; i++)
  181. {
  182. if ((i >= 0) && (i < 8))
  183. {
  184. bank->sectors[i].offset = offset;
  185. bank->sectors[i].size = 4 * 1024;
  186. offset += bank->sectors[i].size;
  187. bank->sectors[i].is_erased = -1;
  188. bank->sectors[i].is_protected = 1;
  189. }
  190. if ((i >= 8) && (i < 22))
  191. {
  192. bank->sectors[i].offset = offset;
  193. bank->sectors[i].size = 32 * 1024;
  194. offset += bank->sectors[i].size;
  195. bank->sectors[i].is_erased = -1;
  196. bank->sectors[i].is_protected = 1;
  197. }
  198. if ((i >= 22) && (i < 27))
  199. {
  200. bank->sectors[i].offset = offset;
  201. bank->sectors[i].size = 4 * 1024;
  202. offset += bank->sectors[i].size;
  203. bank->sectors[i].is_erased = -1;
  204. bank->sectors[i].is_protected = 1;
  205. }
  206. }
  207. }
  208. else
  209. {
  210. LOG_ERROR("BUG: unknown lpc2000_info->variant encountered");
  211. exit(-1);
  212. }
  213. return ERROR_OK;
  214. }
  215. /* call LPC2000 IAP function
  216. * uses 172 bytes working area
  217. * 0x0 to 0x7: jump gate (BX to thumb state, b -2 to wait)
  218. * 0x8 to 0x1f: command parameter table
  219. * 0x20 to 0x2b: command result table
  220. * 0x2c to 0xac: stack (only 128b needed)
  221. */
  222. int lpc2000_iap_call(flash_bank_t *bank, int code, u32 param_table[5], u32 result_table[2])
  223. {
  224. lpc2000_flash_bank_t *lpc2000_info = bank->driver_priv;
  225. target_t *target = bank->target;
  226. mem_param_t mem_params[2];
  227. reg_param_t reg_params[5];
  228. armv4_5_algorithm_t armv4_5_info;
  229. u32 status_code;
  230. /* regrab previously allocated working_area, or allocate a new one */
  231. if (!lpc2000_info->iap_working_area)
  232. {
  233. u8 jump_gate[8];
  234. /* make sure we have a working area */
  235. if (target_alloc_working_area(target, 172, &lpc2000_info->iap_working_area) != ERROR_OK)
  236. {
  237. LOG_ERROR("no working area specified, can't write LPC2000 internal flash");
  238. return ERROR_FLASH_OPERATION_FAILED;
  239. }
  240. /* write IAP code to working area */
  241. target_buffer_set_u32(target, jump_gate, ARMV4_5_BX(12));
  242. target_buffer_set_u32(target, jump_gate + 4, ARMV4_5_B(0xfffffe, 0));
  243. target->type->write_memory(target, lpc2000_info->iap_working_area->address, 4, 2, jump_gate);
  244. }
  245. armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
  246. armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
  247. armv4_5_info.core_state = ARMV4_5_STATE_ARM;
  248. /* command parameter table */
  249. init_mem_param(&mem_params[0], lpc2000_info->iap_working_area->address + 8, 4 * 6, PARAM_OUT);
  250. target_buffer_set_u32(target, mem_params[0].value, code);
  251. target_buffer_set_u32(target, mem_params[0].value + 0x4, param_table[0]);
  252. target_buffer_set_u32(target, mem_params[0].value + 0x8, param_table[1]);
  253. target_buffer_set_u32(target, mem_params[0].value + 0xc, param_table[2]);
  254. target_buffer_set_u32(target, mem_params[0].value + 0x10, param_table[3]);
  255. target_buffer_set_u32(target, mem_params[0].value + 0x14, param_table[4]);
  256. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  257. buf_set_u32(reg_params[0].value, 0, 32, lpc2000_info->iap_working_area->address + 0x8);
  258. /* command result table */
  259. init_mem_param(&mem_params[1], lpc2000_info->iap_working_area->address + 0x20, 4 * 3, PARAM_IN);
  260. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  261. buf_set_u32(reg_params[1].value, 0, 32, lpc2000_info->iap_working_area->address + 0x20);
  262. /* IAP entry point */
  263. init_reg_param(&reg_params[2], "r12", 32, PARAM_OUT);
  264. buf_set_u32(reg_params[2].value, 0, 32, 0x7ffffff1);
  265. /* IAP stack */
  266. init_reg_param(&reg_params[3], "r13_svc", 32, PARAM_OUT);
  267. buf_set_u32(reg_params[3].value, 0, 32, lpc2000_info->iap_working_area->address + 0xac);
  268. /* return address */
  269. init_reg_param(&reg_params[4], "lr_svc", 32, PARAM_OUT);
  270. buf_set_u32(reg_params[4].value, 0, 32, lpc2000_info->iap_working_area->address + 0x4);
  271. target->type->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);
  272. status_code = buf_get_u32(mem_params[1].value, 0, 32);
  273. result_table[0] = target_buffer_get_u32(target, mem_params[1].value);
  274. result_table[1] = target_buffer_get_u32(target, mem_params[1].value + 4);
  275. destroy_mem_param(&mem_params[0]);
  276. destroy_mem_param(&mem_params[1]);
  277. destroy_reg_param(&reg_params[0]);
  278. destroy_reg_param(&reg_params[1]);
  279. destroy_reg_param(&reg_params[2]);
  280. destroy_reg_param(&reg_params[3]);
  281. destroy_reg_param(&reg_params[4]);
  282. return status_code;
  283. }
  284. int lpc2000_iap_blank_check(struct flash_bank_s *bank, int first, int last)
  285. {
  286. u32 param_table[5];
  287. u32 result_table[2];
  288. int status_code;
  289. int i;
  290. if ((first < 0) || (last > bank->num_sectors))
  291. return ERROR_FLASH_SECTOR_INVALID;
  292. for (i = first; i <= last; i++)
  293. {
  294. /* check single sector */
  295. param_table[0] = param_table[1] = i;
  296. status_code = lpc2000_iap_call(bank, 53, param_table, result_table);
  297. switch (status_code)
  298. {
  299. case ERROR_FLASH_OPERATION_FAILED:
  300. return ERROR_FLASH_OPERATION_FAILED;
  301. case LPC2000_CMD_SUCCESS:
  302. bank->sectors[i].is_erased = 1;
  303. break;
  304. case LPC2000_SECTOR_NOT_BLANK:
  305. bank->sectors[i].is_erased = 0;
  306. break;
  307. case LPC2000_INVALID_SECTOR:
  308. bank->sectors[i].is_erased = 0;
  309. break;
  310. case LPC2000_BUSY:
  311. return ERROR_FLASH_BUSY;
  312. break;
  313. default:
  314. LOG_ERROR("BUG: unknown LPC2000 status code");
  315. exit(-1);
  316. }
  317. }
  318. return ERROR_OK;
  319. }
  320. /* flash bank lpc2000 <base> <size> 0 0 <target#> <lpc_variant> <cclk> [calc_checksum]
  321. */
  322. int lpc2000_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
  323. {
  324. lpc2000_flash_bank_t *lpc2000_info;
  325. if (argc < 8)
  326. {
  327. LOG_WARNING("incomplete flash_bank lpc2000 configuration");
  328. return ERROR_FLASH_BANK_INVALID;
  329. }
  330. lpc2000_info = malloc(sizeof(lpc2000_flash_bank_t));
  331. bank->driver_priv = lpc2000_info;
  332. if (strcmp(args[6], "lpc2000_v1") == 0)
  333. {
  334. lpc2000_info->variant = 1;
  335. lpc2000_info->cmd51_dst_boundary = 512;
  336. lpc2000_info->cmd51_can_256b = 0;
  337. lpc2000_info->cmd51_can_8192b = 1;
  338. }
  339. else if (strcmp(args[6], "lpc2000_v2") == 0)
  340. {
  341. lpc2000_info->variant = 2;
  342. lpc2000_info->cmd51_dst_boundary = 256;
  343. lpc2000_info->cmd51_can_256b = 1;
  344. lpc2000_info->cmd51_can_8192b = 0;
  345. }
  346. else
  347. {
  348. LOG_ERROR("unknown LPC2000 variant");
  349. free(lpc2000_info);
  350. return ERROR_FLASH_BANK_INVALID;
  351. }
  352. lpc2000_info->iap_working_area = NULL;
  353. lpc2000_info->cclk = strtoul(args[7], NULL, 0);
  354. lpc2000_info->calc_checksum = 0;
  355. lpc2000_build_sector_list(bank);
  356. if (argc >= 9)
  357. {
  358. if (strcmp(args[8], "calc_checksum") == 0)
  359. lpc2000_info->calc_checksum = 1;
  360. }
  361. return ERROR_OK;
  362. }
  363. int lpc2000_erase(struct flash_bank_s *bank, int first, int last)
  364. {
  365. lpc2000_flash_bank_t *lpc2000_info = bank->driver_priv;
  366. u32 param_table[5];
  367. u32 result_table[2];
  368. int status_code;
  369. if (bank->target->state != TARGET_HALTED)
  370. {
  371. LOG_ERROR("Target not halted");
  372. return ERROR_TARGET_NOT_HALTED;
  373. }
  374. param_table[0] = first;
  375. param_table[1] = last;
  376. param_table[2] = lpc2000_info->cclk;
  377. /* Prepare sectors */
  378. status_code = lpc2000_iap_call(bank, 50, param_table, result_table);
  379. switch (status_code)
  380. {
  381. case ERROR_FLASH_OPERATION_FAILED:
  382. return ERROR_FLASH_OPERATION_FAILED;
  383. case LPC2000_CMD_SUCCESS:
  384. break;
  385. case LPC2000_INVALID_SECTOR:
  386. return ERROR_FLASH_SECTOR_INVALID;
  387. break;
  388. default:
  389. LOG_WARNING("lpc2000 prepare sectors returned %i", status_code);
  390. return ERROR_FLASH_OPERATION_FAILED;
  391. }
  392. /* Erase sectors */
  393. status_code = lpc2000_iap_call(bank, 52, param_table, result_table);
  394. switch (status_code)
  395. {
  396. case ERROR_FLASH_OPERATION_FAILED:
  397. return ERROR_FLASH_OPERATION_FAILED;
  398. case LPC2000_CMD_SUCCESS:
  399. break;
  400. case LPC2000_INVALID_SECTOR:
  401. return ERROR_FLASH_SECTOR_INVALID;
  402. break;
  403. default:
  404. LOG_WARNING("lpc2000 erase sectors returned %i", status_code);
  405. return ERROR_FLASH_OPERATION_FAILED;
  406. }
  407. return ERROR_OK;
  408. }
  409. int lpc2000_protect(struct flash_bank_s *bank, int set, int first, int last)
  410. {
  411. /* can't protect/unprotect on the lpc2000 */
  412. return ERROR_OK;
  413. }
  414. int lpc2000_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
  415. {
  416. lpc2000_flash_bank_t *lpc2000_info = bank->driver_priv;
  417. target_t *target = bank->target;
  418. u32 dst_min_alignment;
  419. u32 bytes_remaining = count;
  420. u32 bytes_written = 0;
  421. int first_sector = 0;
  422. int last_sector = 0;
  423. u32 param_table[5];
  424. u32 result_table[2];
  425. int status_code;
  426. int i;
  427. working_area_t *download_area;
  428. int retval = ERROR_OK;
  429. if (bank->target->state != TARGET_HALTED)
  430. {
  431. LOG_ERROR("Target not halted");
  432. return ERROR_TARGET_NOT_HALTED;
  433. }
  434. if (offset + count > bank->size)
  435. return ERROR_FLASH_DST_OUT_OF_BANK;
  436. if (lpc2000_info->cmd51_can_256b)
  437. dst_min_alignment = 256;
  438. else
  439. dst_min_alignment = 512;
  440. if (offset % dst_min_alignment)
  441. {
  442. LOG_WARNING("offset 0x%x breaks required alignment 0x%x", offset, dst_min_alignment);
  443. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  444. }
  445. for (i = 0; i < bank->num_sectors; i++)
  446. {
  447. if (offset >= bank->sectors[i].offset)
  448. first_sector = i;
  449. if (offset + CEIL(count, dst_min_alignment) * dst_min_alignment > bank->sectors[i].offset)
  450. last_sector = i;
  451. }
  452. LOG_DEBUG("first_sector: %i, last_sector: %i", first_sector, last_sector);
  453. /* check if exception vectors should be flashed */
  454. if ((offset == 0) && (count >= 0x20) && lpc2000_info->calc_checksum)
  455. {
  456. u32 checksum = 0;
  457. int i = 0;
  458. for (i = 0; i < 8; i++)
  459. {
  460. LOG_DEBUG("0x%2.2x: 0x%8.8x", i * 4, buf_get_u32(buffer + (i * 4), 0, 32));
  461. if (i != 5)
  462. checksum += buf_get_u32(buffer + (i * 4), 0, 32);
  463. }
  464. checksum = 0 - checksum;
  465. LOG_DEBUG("checksum: 0x%8.8x", checksum);
  466. buf_set_u32(buffer + 0x14, 0, 32, checksum);
  467. }
  468. /* allocate a working area */
  469. if (target_alloc_working_area(target, lpc2000_info->cmd51_max_buffer, &download_area) != ERROR_OK)
  470. {
  471. LOG_ERROR("no working area specified, can't write LPC2000 internal flash");
  472. return ERROR_FLASH_OPERATION_FAILED;
  473. }
  474. while (bytes_remaining > 0)
  475. {
  476. u32 thisrun_bytes;
  477. if (bytes_remaining >= lpc2000_info->cmd51_max_buffer)
  478. thisrun_bytes = lpc2000_info->cmd51_max_buffer;
  479. else if (bytes_remaining >= 1024)
  480. thisrun_bytes = 1024;
  481. else if ((bytes_remaining >= 512) || (!lpc2000_info->cmd51_can_256b))
  482. thisrun_bytes = 512;
  483. else
  484. thisrun_bytes = 256;
  485. /* Prepare sectors */
  486. param_table[0] = first_sector;
  487. param_table[1] = last_sector;
  488. status_code = lpc2000_iap_call(bank, 50, param_table, result_table);
  489. switch (status_code)
  490. {
  491. case ERROR_FLASH_OPERATION_FAILED:
  492. retval = ERROR_FLASH_OPERATION_FAILED;
  493. break;
  494. case LPC2000_CMD_SUCCESS:
  495. break;
  496. case LPC2000_INVALID_SECTOR:
  497. retval = ERROR_FLASH_SECTOR_INVALID;
  498. break;
  499. default:
  500. LOG_WARNING("lpc2000 prepare sectors returned %i", status_code);
  501. retval = ERROR_FLASH_OPERATION_FAILED;
  502. break;
  503. }
  504. /* Exit if error occured */
  505. if (retval != ERROR_OK)
  506. break;
  507. if (bytes_remaining >= thisrun_bytes)
  508. {
  509. if ((retval = target_write_buffer(bank->target, download_area->address, thisrun_bytes, buffer + bytes_written)) != ERROR_OK)
  510. {
  511. retval = ERROR_FLASH_OPERATION_FAILED;
  512. break;
  513. }
  514. }
  515. else
  516. {
  517. u8 *last_buffer = malloc(thisrun_bytes);
  518. int i;
  519. memcpy(last_buffer, buffer + bytes_written, bytes_remaining);
  520. for (i = bytes_remaining; i < thisrun_bytes; i++)
  521. last_buffer[i] = 0xff;
  522. target_write_buffer(bank->target, download_area->address, thisrun_bytes, last_buffer);
  523. free(last_buffer);
  524. }
  525. LOG_DEBUG("writing 0x%x bytes to address 0x%x", thisrun_bytes, bank->base + offset + bytes_written);
  526. /* Write data */
  527. param_table[0] = bank->base + offset + bytes_written;
  528. param_table[1] = download_area->address;
  529. param_table[2] = thisrun_bytes;
  530. param_table[3] = lpc2000_info->cclk;
  531. status_code = lpc2000_iap_call(bank, 51, param_table, result_table);
  532. switch (status_code)
  533. {
  534. case ERROR_FLASH_OPERATION_FAILED:
  535. retval = ERROR_FLASH_OPERATION_FAILED;
  536. break;
  537. case LPC2000_CMD_SUCCESS:
  538. break;
  539. case LPC2000_INVALID_SECTOR:
  540. retval = ERROR_FLASH_SECTOR_INVALID;
  541. break;
  542. default:
  543. LOG_WARNING("lpc2000 returned %i", status_code);
  544. retval = ERROR_FLASH_OPERATION_FAILED;
  545. break;
  546. }
  547. /* Exit if error occured */
  548. if (retval != ERROR_OK)
  549. break;
  550. if (bytes_remaining > thisrun_bytes)
  551. bytes_remaining -= thisrun_bytes;
  552. else
  553. bytes_remaining = 0;
  554. bytes_written += thisrun_bytes;
  555. }
  556. target_free_working_area(target, download_area);
  557. return retval;
  558. }
  559. int lpc2000_probe(struct flash_bank_s *bank)
  560. {
  561. /* we can't probe on an lpc2000
  562. * if this is an lpc2xxx, it has the configured flash
  563. */
  564. return ERROR_OK;
  565. }
  566. int lpc2000_erase_check(struct flash_bank_s *bank)
  567. {
  568. if (bank->target->state != TARGET_HALTED)
  569. {
  570. LOG_ERROR("Target not halted");
  571. return ERROR_TARGET_NOT_HALTED;
  572. }
  573. return lpc2000_iap_blank_check(bank, 0, bank->num_sectors - 1);
  574. }
  575. int lpc2000_protect_check(struct flash_bank_s *bank)
  576. {
  577. /* sectors are always protected */
  578. return ERROR_OK;
  579. }
  580. int lpc2000_info(struct flash_bank_s *bank, char *buf, int buf_size)
  581. {
  582. lpc2000_flash_bank_t *lpc2000_info = bank->driver_priv;
  583. snprintf(buf, buf_size, "lpc2000 flash driver variant: %i, clk: %i", lpc2000_info->variant, lpc2000_info->cclk);
  584. return ERROR_OK;
  585. }
  586. int lpc2000_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  587. {
  588. flash_bank_t *bank;
  589. u32 param_table[5];
  590. u32 result_table[2];
  591. int status_code;
  592. if (argc < 1)
  593. {
  594. return ERROR_COMMAND_SYNTAX_ERROR;
  595. }
  596. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  597. if (!bank)
  598. {
  599. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  600. return ERROR_OK;
  601. }
  602. if (bank->target->state != TARGET_HALTED)
  603. {
  604. LOG_ERROR("Target not halted");
  605. return ERROR_TARGET_NOT_HALTED;
  606. }
  607. if ((status_code = lpc2000_iap_call(bank, 54, param_table, result_table)) != 0x0)
  608. {
  609. if (status_code == ERROR_FLASH_OPERATION_FAILED)
  610. {
  611. command_print(cmd_ctx, "no sufficient working area specified, can't access LPC2000 IAP interface");
  612. return ERROR_OK;
  613. }
  614. command_print(cmd_ctx, "lpc2000 IAP returned status code %i", status_code);
  615. }
  616. else
  617. {
  618. command_print(cmd_ctx, "lpc2000 part id: 0x%8.8x", result_table[0]);
  619. }
  620. return ERROR_OK;
  621. }