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.
 
 
 
 
 
 

946 lines
26 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2008 by Spencer Oliver *
  6. * spen@spen-soft.co.uk *
  7. * *
  8. * Copyright (C) 2008 by John McCarthy *
  9. * jgmcc@magma.ca *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "pic32mx.h"
  30. #include "mips32.h"
  31. static
  32. struct pic32mx_devs_s {
  33. uint8_t devid;
  34. char *name;
  35. uint32_t pfm_size;
  36. } pic32mx_devs[] = {
  37. { 0x78, "460F512L USB", 512 },
  38. { 0x74, "460F256L USB", 256 },
  39. { 0x6D, "440F128L USB", 128 },
  40. { 0x56, "440F512H USB", 512 },
  41. { 0x52, "440F256H USB", 256 },
  42. { 0x4D, "440F128H USB", 128 },
  43. { 0x42, "420F032H USB", 32 },
  44. { 0x38, "360F512L", 512 },
  45. { 0x34, "360F256L", 256 },
  46. { 0x2D, "340F128L", 128 },
  47. { 0x2A, "320F128L", 128 },
  48. { 0x16, "340F512H", 512 },
  49. { 0x12, "340F256H", 256 },
  50. { 0x0D, "340F128H", 128 },
  51. { 0x0A, "320F128H", 128 },
  52. { 0x06, "320F064H", 64 },
  53. { 0x02, "320F032H", 32 },
  54. { 0x00, NULL, 0 }
  55. };
  56. static int pic32mx_register_commands(struct command_context_s *cmd_ctx);
  57. static int pic32mx_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
  58. static int pic32mx_erase(struct flash_bank_s *bank, int first, int last);
  59. static int pic32mx_protect(struct flash_bank_s *bank, int set, int first, int last);
  60. static int pic32mx_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
  61. static int pic32mx_write_row(struct flash_bank_s *bank, uint32_t address, uint32_t srcaddr);
  62. static int pic32mx_write_word(struct flash_bank_s *bank, uint32_t address, uint32_t word);
  63. static int pic32mx_probe(struct flash_bank_s *bank);
  64. static int pic32mx_auto_probe(struct flash_bank_s *bank);
  65. //static int pic32mx_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  66. static int pic32mx_protect_check(struct flash_bank_s *bank);
  67. static int pic32mx_info(struct flash_bank_s *bank, char *buf, int buf_size);
  68. #if 0
  69. int pic32mx_handle_lock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  70. int pic32mx_handle_unlock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  71. #endif
  72. static int pic32mx_handle_chip_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  73. static int pic32mx_handle_pgm_word_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  74. //static int pic32mx_chip_erase(struct flash_bank_s *bank);
  75. flash_driver_t pic32mx_flash =
  76. {
  77. .name = "pic32mx",
  78. .register_commands = pic32mx_register_commands,
  79. .flash_bank_command = pic32mx_flash_bank_command,
  80. .erase = pic32mx_erase,
  81. .protect = pic32mx_protect,
  82. .write = pic32mx_write,
  83. .probe = pic32mx_probe,
  84. .auto_probe = pic32mx_auto_probe,
  85. .erase_check = default_flash_mem_blank_check,
  86. .protect_check = pic32mx_protect_check,
  87. .info = pic32mx_info
  88. };
  89. static int pic32mx_register_commands(struct command_context_s *cmd_ctx)
  90. {
  91. command_t *pic32mx_cmd = register_command(cmd_ctx, NULL, "pic32mx", NULL, COMMAND_ANY, "pic32mx flash specific commands");
  92. #if 0
  93. register_command(cmd_ctx, pic32mx_cmd, "lock", pic32mx_handle_lock_command, COMMAND_EXEC,
  94. "lock device");
  95. register_command(cmd_ctx, pic32mx_cmd, "unlock", pic32mx_handle_unlock_command, COMMAND_EXEC,
  96. "unlock protected device");
  97. #endif
  98. register_command(cmd_ctx, pic32mx_cmd, "chip_erase", pic32mx_handle_chip_erase_command, COMMAND_EXEC,
  99. "erase device");
  100. register_command(cmd_ctx, pic32mx_cmd, "pgm_word", pic32mx_handle_pgm_word_command, COMMAND_EXEC,
  101. "program a word");
  102. return ERROR_OK;
  103. }
  104. /* flash bank pic32mx <base> <size> 0 0 <target#>
  105. */
  106. static int pic32mx_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
  107. {
  108. pic32mx_flash_bank_t *pic32mx_info;
  109. if (argc < 6)
  110. {
  111. LOG_WARNING("incomplete flash_bank pic32mx configuration");
  112. return ERROR_FLASH_BANK_INVALID;
  113. }
  114. pic32mx_info = malloc(sizeof(pic32mx_flash_bank_t));
  115. bank->driver_priv = pic32mx_info;
  116. pic32mx_info->write_algorithm = NULL;
  117. pic32mx_info->probed = 0;
  118. return ERROR_OK;
  119. }
  120. static uint32_t pic32mx_get_flash_status(flash_bank_t *bank)
  121. {
  122. target_t *target = bank->target;
  123. uint32_t status;
  124. target_read_u32(target, PIC32MX_NVMCON, &status);
  125. return status;
  126. }
  127. static uint32_t pic32mx_wait_status_busy(flash_bank_t *bank, int timeout)
  128. {
  129. uint32_t status;
  130. /* wait for busy to clear */
  131. while (((status = pic32mx_get_flash_status(bank)) & NVMCON_NVMWR) && (timeout-- > 0))
  132. {
  133. LOG_DEBUG("status: 0x%" PRIx32, status);
  134. alive_sleep(1);
  135. }
  136. if (timeout <= 0)
  137. LOG_DEBUG("timeout: status: 0x%" PRIx32, status);
  138. return status;
  139. }
  140. static int pic32mx_nvm_exec(struct flash_bank_s *bank, uint32_t op, uint32_t timeout)
  141. {
  142. target_t *target = bank->target;
  143. uint32_t status;
  144. target_write_u32(target, PIC32MX_NVMCON, NVMCON_NVMWREN | op);
  145. /* unlock flash registers */
  146. target_write_u32(target, PIC32MX_NVMKEY, NVMKEY1);
  147. target_write_u32(target, PIC32MX_NVMKEY, NVMKEY2);
  148. /* start operation */
  149. target_write_u32(target, PIC32MX_NVMCONSET, NVMCON_NVMWR);
  150. status = pic32mx_wait_status_busy(bank, timeout);
  151. /* lock flash registers */
  152. target_write_u32(target, PIC32MX_NVMCONCLR, NVMCON_NVMWREN);
  153. return status;
  154. }
  155. static int pic32mx_protect_check(struct flash_bank_s *bank)
  156. {
  157. target_t *target = bank->target;
  158. uint32_t devcfg0;
  159. int s;
  160. int num_pages;
  161. if (target->state != TARGET_HALTED)
  162. {
  163. LOG_ERROR("Target not halted");
  164. return ERROR_TARGET_NOT_HALTED;
  165. }
  166. target_read_u32(target, PIC32MX_DEVCFG0, &devcfg0);
  167. if ((devcfg0 & (1 << 28)) == 0) /* code protect bit */
  168. num_pages = 0xffff; /* All pages protected */
  169. else if (bank->base == PIC32MX_KSEG1_BOOT_FLASH)
  170. {
  171. if (devcfg0 & (1 << 24))
  172. num_pages = 0; /* All pages unprotected */
  173. else
  174. num_pages = 0xffff; /* All pages protected */
  175. }
  176. else /* pgm flash */
  177. num_pages = (~devcfg0 >> 12) & 0xff;
  178. for (s = 0; s < bank->num_sectors && s < num_pages; s++)
  179. bank->sectors[s].is_protected = 1;
  180. for (; s < bank->num_sectors; s++)
  181. bank->sectors[s].is_protected = 0;
  182. return ERROR_OK;
  183. }
  184. static int pic32mx_erase(struct flash_bank_s *bank, int first, int last)
  185. {
  186. target_t *target = bank->target;
  187. int i;
  188. uint32_t status;
  189. if (bank->target->state != TARGET_HALTED)
  190. {
  191. LOG_ERROR("Target not halted");
  192. return ERROR_TARGET_NOT_HALTED;
  193. }
  194. if ((first == 0) && (last == (bank->num_sectors - 1)) && (bank->base == PIC32MX_KSEG0_PGM_FLASH || bank->base == PIC32MX_KSEG1_PGM_FLASH))
  195. {
  196. LOG_DEBUG("Erasing entire program flash");
  197. status = pic32mx_nvm_exec(bank, NVMCON_OP_PFM_ERASE, 50);
  198. if (status & NVMCON_NVMERR)
  199. return ERROR_FLASH_OPERATION_FAILED;
  200. if (status & NVMCON_LVDERR)
  201. return ERROR_FLASH_OPERATION_FAILED;
  202. return ERROR_OK;
  203. }
  204. for (i = first; i <= last; i++)
  205. {
  206. if (bank->base >= PIC32MX_KSEG1_PGM_FLASH)
  207. target_write_u32(target, PIC32MX_NVMADDR, KS1Virt2Phys(bank->base + bank->sectors[i].offset));
  208. else
  209. target_write_u32(target, PIC32MX_NVMADDR, KS0Virt2Phys(bank->base + bank->sectors[i].offset));
  210. status = pic32mx_nvm_exec(bank, NVMCON_OP_PAGE_ERASE, 10);
  211. if (status & NVMCON_NVMERR)
  212. return ERROR_FLASH_OPERATION_FAILED;
  213. if (status & NVMCON_LVDERR)
  214. return ERROR_FLASH_OPERATION_FAILED;
  215. bank->sectors[i].is_erased = 1;
  216. }
  217. return ERROR_OK;
  218. }
  219. static int pic32mx_protect(struct flash_bank_s *bank, int set, int first, int last)
  220. {
  221. pic32mx_flash_bank_t *pic32mx_info = NULL;
  222. target_t *target = bank->target;
  223. #if 0
  224. uint16_t prot_reg[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
  225. int i, reg, bit;
  226. int status;
  227. uint32_t protection;
  228. #endif
  229. pic32mx_info = bank->driver_priv;
  230. if (target->state != TARGET_HALTED)
  231. {
  232. LOG_ERROR("Target not halted");
  233. return ERROR_TARGET_NOT_HALTED;
  234. }
  235. #if 0
  236. if ((first && (first % pic32mx_info->ppage_size)) || ((last + 1) && (last + 1) % pic32mx_info->ppage_size))
  237. {
  238. LOG_WARNING("sector start/end incorrect - stm32 has %dK sector protection", pic32mx_info->ppage_size);
  239. return ERROR_FLASH_SECTOR_INVALID;
  240. }
  241. /* medium density - each bit refers to a 4bank protection
  242. * high density - each bit refers to a 2bank protection */
  243. target_read_u32(target, PIC32MX_FLASH_WRPR, &protection);
  244. prot_reg[0] = (uint16_t)protection;
  245. prot_reg[1] = (uint16_t)(protection >> 8);
  246. prot_reg[2] = (uint16_t)(protection >> 16);
  247. prot_reg[3] = (uint16_t)(protection >> 24);
  248. if (pic32mx_info->ppage_size == 2)
  249. {
  250. /* high density flash */
  251. /* bit 7 controls sector 62 - 255 protection */
  252. if (last > 61)
  253. {
  254. if (set)
  255. prot_reg[3] &= ~(1 << 7);
  256. else
  257. prot_reg[3] |= (1 << 7);
  258. }
  259. if (first > 61)
  260. first = 62;
  261. if (last > 61)
  262. last = 61;
  263. for (i = first; i <= last; i++)
  264. {
  265. reg = (i / pic32mx_info->ppage_size) / 8;
  266. bit = (i / pic32mx_info->ppage_size) - (reg * 8);
  267. if (set)
  268. prot_reg[reg] &= ~(1 << bit);
  269. else
  270. prot_reg[reg] |= (1 << bit);
  271. }
  272. }
  273. else
  274. {
  275. /* medium density flash */
  276. for (i = first; i <= last; i++)
  277. {
  278. reg = (i / pic32mx_info->ppage_size) / 8;
  279. bit = (i / pic32mx_info->ppage_size) - (reg * 8);
  280. if (set)
  281. prot_reg[reg] &= ~(1 << bit);
  282. else
  283. prot_reg[reg] |= (1 << bit);
  284. }
  285. }
  286. if ((status = pic32mx_erase_options(bank)) != ERROR_OK)
  287. return status;
  288. pic32mx_info->option_bytes.protection[0] = prot_reg[0];
  289. pic32mx_info->option_bytes.protection[1] = prot_reg[1];
  290. pic32mx_info->option_bytes.protection[2] = prot_reg[2];
  291. pic32mx_info->option_bytes.protection[3] = prot_reg[3];
  292. return pic32mx_write_options(bank);
  293. #else
  294. return ERROR_OK;
  295. #endif
  296. }
  297. static int pic32mx_write_block(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  298. {
  299. target_t *target = bank->target;
  300. uint32_t buffer_size = 512;
  301. working_area_t *source;
  302. uint32_t address = bank->base + offset;
  303. int retval = ERROR_OK;
  304. #if 0
  305. pic32mx_flash_bank_t *pic32mx_info = bank->driver_priv;
  306. armv7m_algorithm_t armv7m_info;
  307. uint8_t pic32mx_flash_write_code[] = {
  308. /* write: */
  309. 0xDF, 0xF8, 0x24, 0x40, /* ldr r4, PIC32MX_FLASH_CR */
  310. 0x09, 0x4D, /* ldr r5, PIC32MX_FLASH_SR */
  311. 0x4F, 0xF0, 0x01, 0x03, /* mov r3, #1 */
  312. 0x23, 0x60, /* str r3, [r4, #0] */
  313. 0x30, 0xF8, 0x02, 0x3B, /* ldrh r3, [r0], #2 */
  314. 0x21, 0xF8, 0x02, 0x3B, /* strh r3, [r1], #2 */
  315. /* busy: */
  316. 0x2B, 0x68, /* ldr r3, [r5, #0] */
  317. 0x13, 0xF0, 0x01, 0x0F, /* tst r3, #0x01 */
  318. 0xFB, 0xD0, /* beq busy */
  319. 0x13, 0xF0, 0x14, 0x0F, /* tst r3, #0x14 */
  320. 0x01, 0xD1, /* bne exit */
  321. 0x01, 0x3A, /* subs r2, r2, #1 */
  322. 0xED, 0xD1, /* bne write */
  323. /* exit: */
  324. 0xFE, 0xE7, /* b exit */
  325. 0x10, 0x20, 0x02, 0x40, /* PIC32MX_FLASH_CR: .word 0x40022010 */
  326. 0x0C, 0x20, 0x02, 0x40 /* PIC32MX_FLASH_SR: .word 0x4002200C */
  327. };
  328. /* flash write code */
  329. if (target_alloc_working_area(target, sizeof(pic32mx_flash_write_code), &pic32mx_info->write_algorithm) != ERROR_OK)
  330. {
  331. LOG_WARNING("no working area available, can't do block memory writes");
  332. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  333. };
  334. if ((retval = target_write_buffer(target, pic32mx_info->write_algorithm->address, sizeof(pic32mx_flash_write_code), pic32mx_flash_write_code)) != ERROR_OK)
  335. return retval;
  336. #endif
  337. /* memory buffer */
  338. if (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK)
  339. {
  340. #if 0
  341. /* if we already allocated the writing code, but failed to get a buffer, free the algorithm */
  342. if (pic32mx_info->write_algorithm)
  343. target_free_working_area(target, pic32mx_info->write_algorithm);
  344. #endif
  345. LOG_WARNING("no large enough working area available, can't do block memory writes");
  346. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  347. }
  348. while (count >= buffer_size/4)
  349. {
  350. uint32_t status;
  351. if ((retval = target_write_buffer(target, source->address, buffer_size, buffer)) != ERROR_OK) {
  352. LOG_ERROR("Failed to write row buffer (%d words) to RAM", (int)(buffer_size/4));
  353. break;
  354. }
  355. #if 0
  356. buf_set_u32(reg_params[0].value, 0, 32, source->address);
  357. buf_set_u32(reg_params[1].value, 0, 32, address);
  358. buf_set_u32(reg_params[2].value, 0, 32, buffer_size/4);
  359. if ((retval = target_run_algorithm(target, 0, NULL, 4, reg_params, pic32mx_info->write_algorithm->address, \
  360. pic32mx_info->write_algorithm->address + (sizeof(pic32mx_flash_write_code) - 10), 10000, &armv7m_info)) != ERROR_OK)
  361. {
  362. LOG_ERROR("error executing pic32mx flash write algorithm");
  363. retval = ERROR_FLASH_OPERATION_FAILED;
  364. break;
  365. }
  366. if (buf_get_u32(reg_params[3].value, 0, 32) & 0x14)
  367. {
  368. retval = ERROR_FLASH_OPERATION_FAILED;
  369. break;
  370. }
  371. #endif
  372. status = pic32mx_write_row(bank, address, source->address);
  373. if (status & NVMCON_NVMERR) {
  374. LOG_ERROR("Flash write error NVMERR (status = 0x%08" PRIx32 ")", status);
  375. retval = ERROR_FLASH_OPERATION_FAILED;
  376. break;
  377. }
  378. if (status & NVMCON_LVDERR) {
  379. LOG_ERROR("Flash write error LVDERR (status = 0x%08" PRIx32 ")", status);
  380. retval = ERROR_FLASH_OPERATION_FAILED;
  381. break;
  382. }
  383. buffer += buffer_size;
  384. address += buffer_size;
  385. count -= buffer_size/4;
  386. }
  387. target_free_working_area(target, source);
  388. while (count > 0)
  389. {
  390. uint32_t value;
  391. memcpy(&value, buffer, sizeof(uint32_t));
  392. uint32_t status = pic32mx_write_word(bank, address, value);
  393. if (status & NVMCON_NVMERR) {
  394. LOG_ERROR("Flash write error NVMERR (status = 0x%08" PRIx32 ")", status);
  395. retval = ERROR_FLASH_OPERATION_FAILED;
  396. break;
  397. }
  398. if (status & NVMCON_LVDERR) {
  399. LOG_ERROR("Flash write error LVDERR (status = 0x%08" PRIx32 ")", status);
  400. retval = ERROR_FLASH_OPERATION_FAILED;
  401. break;
  402. }
  403. buffer += 4;
  404. address += 4;
  405. count--;
  406. }
  407. return retval;
  408. }
  409. static int pic32mx_write_word(struct flash_bank_s *bank, uint32_t address, uint32_t word)
  410. {
  411. target_t *target = bank->target;
  412. if (bank->base >= PIC32MX_KSEG1_PGM_FLASH)
  413. target_write_u32(target, PIC32MX_NVMADDR, KS1Virt2Phys(address));
  414. else
  415. target_write_u32(target, PIC32MX_NVMADDR, KS0Virt2Phys(address));
  416. target_write_u32(target, PIC32MX_NVMDATA, word);
  417. return pic32mx_nvm_exec(bank, NVMCON_OP_WORD_PROG, 5);
  418. }
  419. /*
  420. * Write a 128 word (512 byte) row to flash address from RAM srcaddr.
  421. */
  422. static int pic32mx_write_row(struct flash_bank_s *bank, uint32_t address, uint32_t srcaddr)
  423. {
  424. target_t *target = bank->target;
  425. LOG_DEBUG("addr: 0x%08" PRIx32 " srcaddr: 0x%08" PRIx32 "", address, srcaddr);
  426. if (address >= PIC32MX_KSEG1_PGM_FLASH)
  427. target_write_u32(target, PIC32MX_NVMADDR, KS1Virt2Phys(address));
  428. else
  429. target_write_u32(target, PIC32MX_NVMADDR, KS0Virt2Phys(address));
  430. if (srcaddr >= PIC32MX_KSEG1_RAM)
  431. target_write_u32(target, PIC32MX_NVMSRCADDR, KS1Virt2Phys(srcaddr));
  432. else
  433. target_write_u32(target, PIC32MX_NVMSRCADDR, KS0Virt2Phys(srcaddr));
  434. return pic32mx_nvm_exec(bank, NVMCON_OP_ROW_PROG, 100);
  435. }
  436. static int pic32mx_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  437. {
  438. uint32_t words_remaining = (count / 4);
  439. uint32_t bytes_remaining = (count & 0x00000003);
  440. uint32_t address = bank->base + offset;
  441. uint32_t bytes_written = 0;
  442. uint32_t status;
  443. int retval;
  444. if (bank->target->state != TARGET_HALTED)
  445. {
  446. LOG_ERROR("Target not halted");
  447. return ERROR_TARGET_NOT_HALTED;
  448. }
  449. if (offset & 0x3)
  450. {
  451. LOG_WARNING("offset 0x%" PRIx32 "breaks required 4-byte alignment", offset);
  452. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  453. }
  454. /* multiple words (4-byte) to be programmed? */
  455. if (words_remaining > 0)
  456. {
  457. /* try using a block write */
  458. if ((retval = pic32mx_write_block(bank, buffer, offset, words_remaining)) != ERROR_OK)
  459. {
  460. if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
  461. {
  462. /* if block write failed (no sufficient working area),
  463. * we use normal (slow) single dword accesses */
  464. LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
  465. }
  466. else if (retval == ERROR_FLASH_OPERATION_FAILED)
  467. {
  468. LOG_ERROR("flash writing failed with error code: 0x%x", retval);
  469. return ERROR_FLASH_OPERATION_FAILED;
  470. }
  471. }
  472. else
  473. {
  474. buffer += words_remaining * 4;
  475. address += words_remaining * 4;
  476. words_remaining = 0;
  477. }
  478. }
  479. while (words_remaining > 0)
  480. {
  481. uint32_t value;
  482. memcpy(&value, buffer + bytes_written, sizeof(uint32_t));
  483. status = pic32mx_write_word(bank, address, value);
  484. if (status & NVMCON_NVMERR)
  485. return ERROR_FLASH_OPERATION_FAILED;
  486. if (status & NVMCON_LVDERR)
  487. return ERROR_FLASH_OPERATION_FAILED;
  488. bytes_written += 4;
  489. words_remaining--;
  490. address += 4;
  491. }
  492. if (bytes_remaining)
  493. {
  494. uint32_t value = 0xffffffff;
  495. memcpy(&value, buffer + bytes_written, bytes_remaining);
  496. status = pic32mx_write_word(bank, address, value);
  497. if (status & NVMCON_NVMERR)
  498. return ERROR_FLASH_OPERATION_FAILED;
  499. if (status & NVMCON_LVDERR)
  500. return ERROR_FLASH_OPERATION_FAILED;
  501. }
  502. return ERROR_OK;
  503. }
  504. static int pic32mx_probe(struct flash_bank_s *bank)
  505. {
  506. target_t *target = bank->target;
  507. pic32mx_flash_bank_t *pic32mx_info = bank->driver_priv;
  508. mips32_common_t *mips32 = target->arch_info;
  509. mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
  510. int i;
  511. uint16_t num_pages = 0;
  512. uint32_t device_id;
  513. int page_size;
  514. pic32mx_info->probed = 0;
  515. device_id = ejtag_info->idcode;
  516. LOG_INFO("device id = 0x%08" PRIx32 " (manuf 0x%03x dev 0x%02x, ver 0x%03x)",
  517. device_id,
  518. (unsigned)((device_id >> 1)&0x7ff),
  519. (unsigned)((device_id >> 12)&0xff),
  520. (unsigned)((device_id >> 20)&0xfff));
  521. if (((device_id >> 1)&0x7ff) != PIC32MX_MANUF_ID) {
  522. LOG_WARNING("Cannot identify target as a PIC32MX family.");
  523. return ERROR_FLASH_OPERATION_FAILED;
  524. }
  525. page_size = 4096;
  526. if (bank->base == PIC32MX_KSEG1_BOOT_FLASH || bank->base == 1) {
  527. /* 0xBFC00000: Boot flash size fixed at 12k */
  528. num_pages = 12;
  529. } else {
  530. /* 0xBD000000: Program flash size varies with device */
  531. for (i = 0; pic32mx_devs[i].name != NULL; i++)
  532. if (pic32mx_devs[i].devid == ((device_id >> 12) & 0xff)) {
  533. num_pages = pic32mx_devs[i].pfm_size;
  534. break;
  535. }
  536. if (pic32mx_devs[i].name == NULL) {
  537. LOG_WARNING("Cannot identify target as a PIC32MX family.");
  538. return ERROR_FLASH_OPERATION_FAILED;
  539. }
  540. }
  541. #if 0
  542. if (bank->target->state != TARGET_HALTED)
  543. {
  544. LOG_ERROR("Target not halted");
  545. return ERROR_TARGET_NOT_HALTED;
  546. }
  547. /* get flash size from target */
  548. if (target_read_u16(target, 0x1FFFF7E0, &num_pages) != ERROR_OK)
  549. {
  550. /* failed reading flash size, default to max target family */
  551. num_pages = 0xffff;
  552. }
  553. #endif
  554. LOG_INFO("flash size = %dkbytes", num_pages);
  555. /* calculate numbers of pages */
  556. num_pages /= (page_size / 1024);
  557. if (bank->base == 0) bank->base = PIC32MX_KSEG1_PGM_FLASH;
  558. if (bank->base == 1) bank->base = PIC32MX_KSEG1_BOOT_FLASH;
  559. bank->size = (num_pages * page_size);
  560. bank->num_sectors = num_pages;
  561. bank->chip_width = 4;
  562. bank->bus_width = 4;
  563. bank->sectors = malloc(sizeof(flash_sector_t) * num_pages);
  564. for (i = 0; i < num_pages; i++)
  565. {
  566. bank->sectors[i].offset = i * page_size;
  567. bank->sectors[i].size = page_size;
  568. bank->sectors[i].is_erased = -1;
  569. bank->sectors[i].is_protected = 1;
  570. }
  571. pic32mx_info->probed = 1;
  572. return ERROR_OK;
  573. }
  574. static int pic32mx_auto_probe(struct flash_bank_s *bank)
  575. {
  576. pic32mx_flash_bank_t *pic32mx_info = bank->driver_priv;
  577. if (pic32mx_info->probed)
  578. return ERROR_OK;
  579. return pic32mx_probe(bank);
  580. }
  581. #if 0
  582. static int pic32mx_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  583. {
  584. return ERROR_OK;
  585. }
  586. #endif
  587. static int pic32mx_info(struct flash_bank_s *bank, char *buf, int buf_size)
  588. {
  589. target_t *target = bank->target;
  590. mips32_common_t *mips32 = target->arch_info;
  591. mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
  592. uint32_t device_id;
  593. int printed = 0, i;
  594. device_id = ejtag_info->idcode;
  595. if (((device_id >> 1)&0x7ff) != PIC32MX_MANUF_ID) {
  596. snprintf(buf, buf_size,
  597. "Cannot identify target as a PIC32MX family (manufacturer 0x%03d != 0x%03d)\n",
  598. (unsigned)((device_id >> 1)&0x7ff),
  599. PIC32MX_MANUF_ID);
  600. return ERROR_FLASH_OPERATION_FAILED;
  601. }
  602. for (i = 0; pic32mx_devs[i].name != NULL; i++)
  603. if (pic32mx_devs[i].devid == ((device_id >> 12) & 0xff)) {
  604. printed = snprintf(buf, buf_size, "PIC32MX%s", pic32mx_devs[i].name);
  605. break;
  606. }
  607. if (pic32mx_devs[i].name == NULL) {
  608. snprintf(buf, buf_size, "Cannot identify target as a PIC32MX family\n");
  609. return ERROR_FLASH_OPERATION_FAILED;
  610. }
  611. buf += printed;
  612. buf_size -= printed;
  613. printed = snprintf(buf, buf_size, " Ver: 0x%03x",
  614. (unsigned)((device_id >> 20)&0xfff));
  615. return ERROR_OK;
  616. }
  617. #if 0
  618. int pic32mx_handle_lock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  619. {
  620. flash_bank_t *bank;
  621. target_t *target = NULL;
  622. pic32mx_flash_bank_t *pic32mx_info = NULL;
  623. if (argc < 1)
  624. {
  625. command_print(cmd_ctx, "pic32mx lock <bank>");
  626. return ERROR_OK;
  627. }
  628. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  629. if (!bank)
  630. {
  631. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  632. return ERROR_OK;
  633. }
  634. pic32mx_info = bank->driver_priv;
  635. target = bank->target;
  636. if (target->state != TARGET_HALTED)
  637. {
  638. LOG_ERROR("Target not halted");
  639. return ERROR_TARGET_NOT_HALTED;
  640. }
  641. if (pic32mx_erase_options(bank) != ERROR_OK)
  642. {
  643. command_print(cmd_ctx, "pic32mx failed to erase options");
  644. return ERROR_OK;
  645. }
  646. /* set readout protection */
  647. pic32mx_info->option_bytes.RDP = 0;
  648. if (pic32mx_write_options(bank) != ERROR_OK)
  649. {
  650. command_print(cmd_ctx, "pic32mx failed to lock device");
  651. return ERROR_OK;
  652. }
  653. command_print(cmd_ctx, "pic32mx locked");
  654. return ERROR_OK;
  655. }
  656. int pic32mx_handle_unlock_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  657. {
  658. flash_bank_t *bank;
  659. target_t *target = NULL;
  660. pic32mx_flash_bank_t *pic32mx_info = NULL;
  661. if (argc < 1)
  662. {
  663. command_print(cmd_ctx, "pic32mx unlock <bank>");
  664. return ERROR_OK;
  665. }
  666. bank = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  667. if (!bank)
  668. {
  669. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  670. return ERROR_OK;
  671. }
  672. pic32mx_info = bank->driver_priv;
  673. target = bank->target;
  674. if (target->state != TARGET_HALTED)
  675. {
  676. LOG_ERROR("Target not halted");
  677. return ERROR_TARGET_NOT_HALTED;
  678. }
  679. if (pic32mx_erase_options(bank) != ERROR_OK)
  680. {
  681. command_print(cmd_ctx, "pic32mx failed to unlock device");
  682. return ERROR_OK;
  683. }
  684. if (pic32mx_write_options(bank) != ERROR_OK)
  685. {
  686. command_print(cmd_ctx, "pic32mx failed to lock device");
  687. return ERROR_OK;
  688. }
  689. command_print(cmd_ctx, "pic32mx unlocked");
  690. return ERROR_OK;
  691. }
  692. #endif
  693. #if 0
  694. static int pic32mx_chip_erase(struct flash_bank_s *bank)
  695. {
  696. target_t *target = bank->target;
  697. #if 0
  698. uint32_t status;
  699. #endif
  700. if (target->state != TARGET_HALTED)
  701. {
  702. LOG_ERROR("Target not halted");
  703. return ERROR_TARGET_NOT_HALTED;
  704. }
  705. LOG_INFO("PIC32MX chip erase called");
  706. #if 0
  707. /* unlock option flash registers */
  708. target_write_u32(target, PIC32MX_FLASH_KEYR, KEY1);
  709. target_write_u32(target, PIC32MX_FLASH_KEYR, KEY2);
  710. /* chip erase flash memory */
  711. target_write_u32(target, PIC32MX_FLASH_CR, FLASH_MER);
  712. target_write_u32(target, PIC32MX_FLASH_CR, FLASH_MER | FLASH_STRT);
  713. status = pic32mx_wait_status_busy(bank, 10);
  714. target_write_u32(target, PIC32MX_FLASH_CR, FLASH_LOCK);
  715. if (status & FLASH_WRPRTERR)
  716. {
  717. LOG_ERROR("pic32mx device protected");
  718. return ERROR_OK;
  719. }
  720. if (status & FLASH_PGERR)
  721. {
  722. LOG_ERROR("pic32mx device programming failed");
  723. return ERROR_OK;
  724. }
  725. #endif
  726. return ERROR_OK;
  727. }
  728. #endif
  729. static int pic32mx_handle_chip_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  730. {
  731. #if 0
  732. flash_bank_t *bank;
  733. int i;
  734. if (argc != 0)
  735. {
  736. command_print(cmd_ctx, "pic32mx chip_erase");
  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. if (pic32mx_chip_erase(bank) == ERROR_OK)
  746. {
  747. /* set all sectors as erased */
  748. for (i = 0; i < bank->num_sectors; i++)
  749. {
  750. bank->sectors[i].is_erased = 1;
  751. }
  752. command_print(cmd_ctx, "pic32mx chip erase complete");
  753. }
  754. else
  755. {
  756. command_print(cmd_ctx, "pic32mx chip erase failed");
  757. }
  758. #endif
  759. return ERROR_OK;
  760. }
  761. static int pic32mx_handle_pgm_word_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  762. {
  763. flash_bank_t *bank;
  764. uint32_t address, value;
  765. int status, res;
  766. if (argc != 3)
  767. {
  768. command_print(cmd_ctx, "pic32mx pgm_word <addr> <value> <bank>");
  769. return ERROR_OK;
  770. }
  771. address = strtoul(args[0], NULL, 0);
  772. value = strtoul(args[1], NULL, 0);
  773. bank = get_flash_bank_by_num(strtoul(args[2], NULL, 0));
  774. if (!bank)
  775. {
  776. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[2]);
  777. return ERROR_OK;
  778. }
  779. if (address < bank->base || address >= (bank->base + bank->size))
  780. {
  781. command_print(cmd_ctx, "flash address '%s' is out of bounds", args[0]);
  782. return ERROR_OK;
  783. }
  784. res = ERROR_OK;
  785. status = pic32mx_write_word(bank, address, value);
  786. if (status & NVMCON_NVMERR)
  787. res = ERROR_FLASH_OPERATION_FAILED;
  788. if (status & NVMCON_LVDERR)
  789. res = ERROR_FLASH_OPERATION_FAILED;
  790. if (res == ERROR_OK)
  791. command_print(cmd_ctx, "pic32mx pgm word complete");
  792. else
  793. command_print(cmd_ctx, "pic32mx pgm word failed (status = 0x%x)", status);
  794. return ERROR_OK;
  795. }