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.
 
 
 
 
 
 

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