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.
 
 
 
 
 
 

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