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.
 
 
 
 
 
 

979 lines
28 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, see <http://www.gnu.org/licenses/>. *
  23. ***************************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include <jtag/jtag.h>
  28. #include "imp.h"
  29. #include <target/algorithm.h>
  30. #include <target/mips32.h>
  31. #include <target/mips_m4k.h>
  32. #define PIC32MX_MANUF_ID 0x029
  33. /* pic32mx memory locations */
  34. #define PIC32MX_PHYS_RAM 0x00000000
  35. #define PIC32MX_PHYS_PGM_FLASH 0x1D000000
  36. #define PIC32MX_PHYS_PERIPHERALS 0x1F800000
  37. #define PIC32MX_PHYS_BOOT_FLASH 0x1FC00000
  38. /*
  39. * Translate Virtual and Physical addresses.
  40. * Note: These macros only work for KSEG0/KSEG1 addresses.
  41. */
  42. #define virt2phys(v) ((v) & 0x1FFFFFFF)
  43. /* pic32mx configuration register locations */
  44. #define PIC32MX_DEVCFG0_1XX_2XX 0xBFC00BFC
  45. #define PIC32MX_DEVCFG0 0xBFC02FFC
  46. #define PIC32MX_DEVCFG1 0xBFC02FF8
  47. #define PIC32MX_DEVCFG2 0xBFC02FF4
  48. #define PIC32MX_DEVCFG3 0xBFC02FF0
  49. #define PIC32MX_DEVID 0xBF80F220
  50. #define PIC32MX_BMXPFMSZ 0xBF882060
  51. #define PIC32MX_BMXBOOTSZ 0xBF882070
  52. #define PIC32MX_BMXDRMSZ 0xBF882040
  53. /* pic32mx flash controller register locations */
  54. #define PIC32MX_NVMCON 0xBF80F400
  55. #define PIC32MX_NVMCONCLR 0xBF80F404
  56. #define PIC32MX_NVMCONSET 0xBF80F408
  57. #define PIC32MX_NVMCONINV 0xBF80F40C
  58. #define NVMCON_NVMWR (1 << 15)
  59. #define NVMCON_NVMWREN (1 << 14)
  60. #define NVMCON_NVMERR (1 << 13)
  61. #define NVMCON_LVDERR (1 << 12)
  62. #define NVMCON_LVDSTAT (1 << 11)
  63. #define NVMCON_OP_PFM_ERASE 0x5
  64. #define NVMCON_OP_PAGE_ERASE 0x4
  65. #define NVMCON_OP_ROW_PROG 0x3
  66. #define NVMCON_OP_WORD_PROG 0x1
  67. #define NVMCON_OP_NOP 0x0
  68. #define PIC32MX_NVMKEY 0xBF80F410
  69. #define PIC32MX_NVMADDR 0xBF80F420
  70. #define PIC32MX_NVMADDRCLR 0xBF80F424
  71. #define PIC32MX_NVMADDRSET 0xBF80F428
  72. #define PIC32MX_NVMADDRINV 0xBF80F42C
  73. #define PIC32MX_NVMDATA 0xBF80F430
  74. #define PIC32MX_NVMSRCADDR 0xBF80F440
  75. /* flash unlock keys */
  76. #define NVMKEY1 0xAA996655
  77. #define NVMKEY2 0x556699AA
  78. #define MX_1XX_2XX 1 /* PIC32mx1xx/2xx */
  79. #define MX_17X_27X 2 /* PIC32mx17x/27x */
  80. struct pic32mx_flash_bank {
  81. bool probed;
  82. int dev_type; /* Default 0. 1 for Pic32MX1XX/2XX variant */
  83. };
  84. /*
  85. * DEVID values as per PIC32MX Flash Programming Specification Rev N
  86. */
  87. static const struct pic32mx_devs_s {
  88. uint32_t devid;
  89. const char *name;
  90. } pic32mx_devs[] = {
  91. {0x04A07053, "110F016B"},
  92. {0x04A09053, "110F016C"},
  93. {0x04A0B053, "110F016D"},
  94. {0x04A06053, "120F032B"},
  95. {0x04A08053, "120F032C"},
  96. {0x04A0A053, "120F032D"},
  97. {0x04D07053, "130F064B"},
  98. {0x04D09053, "130F064C"},
  99. {0x04D0B053, "130F064D"},
  100. {0x04D06053, "150F128B"},
  101. {0x04D08053, "150F128C"},
  102. {0x04D0A053, "150F128D"},
  103. {0x06610053, "170F256B"},
  104. {0x0661A053, "170F256D"},
  105. {0x04A01053, "210F016B"},
  106. {0x04A03053, "210F016C"},
  107. {0x04A05053, "210F016D"},
  108. {0x04A00053, "220F032B"},
  109. {0x04A02053, "220F032C"},
  110. {0x04A04053, "220F032D"},
  111. {0x04D01053, "230F064B"},
  112. {0x04D03053, "230F064C"},
  113. {0x04D05053, "230F064D"},
  114. {0x04D00053, "250F128B"},
  115. {0x04D02053, "250F128C"},
  116. {0x04D04053, "250F128D"},
  117. {0x06600053, "270F256B"},
  118. {0x0660A053, "270F256D"},
  119. {0x05600053, "330F064H"},
  120. {0x05601053, "330F064L"},
  121. {0x05602053, "430F064H"},
  122. {0x05603053, "430F064L"},
  123. {0x0570C053, "350F128H"},
  124. {0x0570D053, "350F128L"},
  125. {0x0570E053, "450F128H"},
  126. {0x0570F053, "450F128L"},
  127. {0x05704053, "350F256H"},
  128. {0x05705053, "350F256L"},
  129. {0x05706053, "450F256H"},
  130. {0x05707053, "450F256L"},
  131. {0x05808053, "370F512H"},
  132. {0x05809053, "370F512L"},
  133. {0x0580A053, "470F512H"},
  134. {0x0580B053, "470F512L"},
  135. {0x00938053, "360F512L"},
  136. {0x00934053, "360F256L"},
  137. {0x0092D053, "340F128L"},
  138. {0x0092A053, "320F128L"},
  139. {0x00916053, "340F512H"},
  140. {0x00912053, "340F256H"},
  141. {0x0090D053, "340F128H"},
  142. {0x0090A053, "320F128H"},
  143. {0x00906053, "320F064H"},
  144. {0x00902053, "320F032H"},
  145. {0x00978053, "460F512L"},
  146. {0x00974053, "460F256L"},
  147. {0x0096D053, "440F128L"},
  148. {0x00952053, "440F256H"},
  149. {0x00956053, "440F512H"},
  150. {0x0094D053, "440F128H"},
  151. {0x00942053, "420F032H"},
  152. {0x04307053, "795F512L"},
  153. {0x0430E053, "795F512H"},
  154. {0x04306053, "775F512L"},
  155. {0x0430D053, "775F512H"},
  156. {0x04312053, "775F256L"},
  157. {0x04303053, "775F256H"},
  158. {0x04417053, "764F128L"},
  159. {0x0440B053, "764F128H"},
  160. {0x04341053, "695F512L"},
  161. {0x04325053, "695F512H"},
  162. {0x04311053, "675F512L"},
  163. {0x0430C053, "675F512H"},
  164. {0x04305053, "675F256L"},
  165. {0x0430B053, "675F256H"},
  166. {0x04413053, "664F128L"},
  167. {0x04407053, "664F128H"},
  168. {0x04411053, "664F064L"},
  169. {0x04405053, "664F064H"},
  170. {0x0430F053, "575F512L"},
  171. {0x04309053, "575F512H"},
  172. {0x04333053, "575F256L"},
  173. {0x04317053, "575F256H"},
  174. {0x0440F053, "564F128L"},
  175. {0x04403053, "564F128H"},
  176. {0x0440D053, "564F064L"},
  177. {0x04401053, "564F064H"},
  178. {0x04400053, "534F064H"},
  179. {0x0440C053, "534F064L"},
  180. {0x00000000, NULL}
  181. };
  182. /* flash bank pic32mx <base> <size> 0 0 <target#>
  183. */
  184. FLASH_BANK_COMMAND_HANDLER(pic32mx_flash_bank_command)
  185. {
  186. struct pic32mx_flash_bank *pic32mx_info;
  187. if (CMD_ARGC < 6)
  188. return ERROR_COMMAND_SYNTAX_ERROR;
  189. pic32mx_info = malloc(sizeof(struct pic32mx_flash_bank));
  190. bank->driver_priv = pic32mx_info;
  191. pic32mx_info->probed = false;
  192. pic32mx_info->dev_type = 0;
  193. return ERROR_OK;
  194. }
  195. static uint32_t pic32mx_get_flash_status(struct flash_bank *bank)
  196. {
  197. struct target *target = bank->target;
  198. uint32_t status;
  199. target_read_u32(target, PIC32MX_NVMCON, &status);
  200. return status;
  201. }
  202. static uint32_t pic32mx_wait_status_busy(struct flash_bank *bank, int timeout)
  203. {
  204. uint32_t status;
  205. /* wait for busy to clear */
  206. while (((status = pic32mx_get_flash_status(bank)) & NVMCON_NVMWR) && (timeout-- > 0)) {
  207. LOG_DEBUG("status: 0x%" PRIx32, status);
  208. alive_sleep(1);
  209. }
  210. if (timeout <= 0)
  211. LOG_DEBUG("timeout: status: 0x%" PRIx32, status);
  212. return status;
  213. }
  214. static int pic32mx_nvm_exec(struct flash_bank *bank, uint32_t op, uint32_t timeout)
  215. {
  216. struct target *target = bank->target;
  217. uint32_t status;
  218. target_write_u32(target, PIC32MX_NVMCON, NVMCON_NVMWREN | op);
  219. /* unlock flash registers */
  220. target_write_u32(target, PIC32MX_NVMKEY, NVMKEY1);
  221. target_write_u32(target, PIC32MX_NVMKEY, NVMKEY2);
  222. /* start operation */
  223. target_write_u32(target, PIC32MX_NVMCONSET, NVMCON_NVMWR);
  224. status = pic32mx_wait_status_busy(bank, timeout);
  225. /* lock flash registers */
  226. target_write_u32(target, PIC32MX_NVMCONCLR, NVMCON_NVMWREN);
  227. return status;
  228. }
  229. static int pic32mx_protect_check(struct flash_bank *bank)
  230. {
  231. struct target *target = bank->target;
  232. struct pic32mx_flash_bank *pic32mx_info = bank->driver_priv;
  233. uint32_t config0_address;
  234. uint32_t devcfg0;
  235. unsigned int s, num_pages;
  236. if (target->state != TARGET_HALTED) {
  237. LOG_ERROR("Target not halted");
  238. return ERROR_TARGET_NOT_HALTED;
  239. }
  240. switch (pic32mx_info->dev_type) {
  241. case MX_1XX_2XX:
  242. case MX_17X_27X:
  243. config0_address = PIC32MX_DEVCFG0_1XX_2XX;
  244. break;
  245. default:
  246. config0_address = PIC32MX_DEVCFG0;
  247. break;
  248. }
  249. target_read_u32(target, config0_address, &devcfg0);
  250. if ((devcfg0 & (1 << 28)) == 0) /* code protect bit */
  251. num_pages = 0xffff; /* All pages protected */
  252. else if (virt2phys(bank->base) == PIC32MX_PHYS_BOOT_FLASH) {
  253. if (devcfg0 & (1 << 24))
  254. num_pages = 0; /* All pages unprotected */
  255. else
  256. num_pages = 0xffff; /* All pages protected */
  257. } else {
  258. /* pgm flash */
  259. switch (pic32mx_info->dev_type) {
  260. case MX_1XX_2XX:
  261. num_pages = (~devcfg0 >> 10) & 0x7f;
  262. break;
  263. case MX_17X_27X:
  264. num_pages = (~devcfg0 >> 10) & 0x1ff;
  265. break;
  266. default:
  267. num_pages = (~devcfg0 >> 12) & 0xff;
  268. break;
  269. }
  270. }
  271. for (s = 0; s < bank->num_sectors && s < num_pages; s++)
  272. bank->sectors[s].is_protected = 1;
  273. for (; s < bank->num_sectors; s++)
  274. bank->sectors[s].is_protected = 0;
  275. return ERROR_OK;
  276. }
  277. static int pic32mx_erase(struct flash_bank *bank, unsigned int first,
  278. unsigned int last)
  279. {
  280. struct target *target = bank->target;
  281. uint32_t status;
  282. if (bank->target->state != TARGET_HALTED) {
  283. LOG_ERROR("Target not halted");
  284. return ERROR_TARGET_NOT_HALTED;
  285. }
  286. if ((first == 0) && (last == (bank->num_sectors - 1))
  287. && (virt2phys(bank->base) == PIC32MX_PHYS_PGM_FLASH)) {
  288. /* this will only erase the Program Flash (PFM), not the Boot Flash (BFM)
  289. * we need to use the MTAP to perform a full erase */
  290. LOG_DEBUG("Erasing entire program flash");
  291. status = pic32mx_nvm_exec(bank, NVMCON_OP_PFM_ERASE, 50);
  292. if (status & NVMCON_NVMERR)
  293. return ERROR_FLASH_OPERATION_FAILED;
  294. if (status & NVMCON_LVDERR)
  295. return ERROR_FLASH_OPERATION_FAILED;
  296. return ERROR_OK;
  297. }
  298. for (unsigned int i = first; i <= last; i++) {
  299. target_write_u32(target, PIC32MX_NVMADDR, virt2phys(bank->base + bank->sectors[i].offset));
  300. status = pic32mx_nvm_exec(bank, NVMCON_OP_PAGE_ERASE, 10);
  301. if (status & NVMCON_NVMERR)
  302. return ERROR_FLASH_OPERATION_FAILED;
  303. if (status & NVMCON_LVDERR)
  304. return ERROR_FLASH_OPERATION_FAILED;
  305. bank->sectors[i].is_erased = 1;
  306. }
  307. return ERROR_OK;
  308. }
  309. static int pic32mx_protect(struct flash_bank *bank, int set, unsigned int first,
  310. unsigned int last)
  311. {
  312. struct target *target = bank->target;
  313. if (target->state != TARGET_HALTED) {
  314. LOG_ERROR("Target not halted");
  315. return ERROR_TARGET_NOT_HALTED;
  316. }
  317. return ERROR_OK;
  318. }
  319. /* see contrib/loaders/flash/pic32mx.s for src */
  320. static uint32_t pic32mx_flash_write_code[] = {
  321. /* write: */
  322. 0x3C08AA99, /* lui $t0, 0xaa99 */
  323. 0x35086655, /* ori $t0, 0x6655 */
  324. 0x3C095566, /* lui $t1, 0x5566 */
  325. 0x352999AA, /* ori $t1, 0x99aa */
  326. 0x3C0ABF80, /* lui $t2, 0xbf80 */
  327. 0x354AF400, /* ori $t2, 0xf400 */
  328. 0x340B4003, /* ori $t3, $zero, 0x4003 */
  329. 0x340C8000, /* ori $t4, $zero, 0x8000 */
  330. /* write_row: */
  331. 0x2CD30080, /* sltiu $s3, $a2, 128 */
  332. 0x16600008, /* bne $s3, $zero, write_word */
  333. 0x340D4000, /* ori $t5, $zero, 0x4000 */
  334. 0xAD450020, /* sw $a1, 32($t2) */
  335. 0xAD440040, /* sw $a0, 64($t2) */
  336. 0x04110016, /* bal progflash */
  337. 0x24840200, /* addiu $a0, $a0, 512 */
  338. 0x24A50200, /* addiu $a1, $a1, 512 */
  339. 0x1000FFF7, /* beq $zero, $zero, write_row */
  340. 0x24C6FF80, /* addiu $a2, $a2, -128 */
  341. /* write_word: */
  342. 0x3C15A000, /* lui $s5, 0xa000 */
  343. 0x36B50000, /* ori $s5, $s5, 0x0 */
  344. 0x00952025, /* or $a0, $a0, $s5 */
  345. 0x10000008, /* beq $zero, $zero, next_word */
  346. 0x340B4001, /* ori $t3, $zero, 0x4001 */
  347. /* prog_word: */
  348. 0x8C940000, /* lw $s4, 0($a0) */
  349. 0xAD540030, /* sw $s4, 48($t2) */
  350. 0xAD450020, /* sw $a1, 32($t2) */
  351. 0x04110009, /* bal progflash */
  352. 0x24840004, /* addiu $a0, $a0, 4 */
  353. 0x24A50004, /* addiu $a1, $a1, 4 */
  354. 0x24C6FFFF, /* addiu $a2, $a2, -1 */
  355. /* next_word: */
  356. 0x14C0FFF8, /* bne $a2, $zero, prog_word */
  357. 0x00000000, /* nop */
  358. /* done: */
  359. 0x10000002, /* beq $zero, $zero, exit */
  360. 0x24040000, /* addiu $a0, $zero, 0 */
  361. /* error: */
  362. 0x26240000, /* addiu $a0, $s1, 0 */
  363. /* exit: */
  364. 0x7000003F, /* sdbbp */
  365. /* progflash: */
  366. 0xAD4B0000, /* sw $t3, 0($t2) */
  367. 0xAD480010, /* sw $t0, 16($t2) */
  368. 0xAD490010, /* sw $t1, 16($t2) */
  369. 0xAD4C0008, /* sw $t4, 8($t2) */
  370. /* waitflash: */
  371. 0x8D500000, /* lw $s0, 0($t2) */
  372. 0x020C8024, /* and $s0, $s0, $t4 */
  373. 0x1600FFFD, /* bne $s0, $zero, waitflash */
  374. 0x00000000, /* nop */
  375. 0x00000000, /* nop */
  376. 0x00000000, /* nop */
  377. 0x00000000, /* nop */
  378. 0x00000000, /* nop */
  379. 0x8D510000, /* lw $s1, 0($t2) */
  380. 0x30113000, /* andi $s1, $zero, 0x3000 */
  381. 0x1620FFEF, /* bne $s1, $zero, error */
  382. 0xAD4D0004, /* sw $t5, 4($t2) */
  383. 0x03E00008, /* jr $ra */
  384. 0x00000000 /* nop */
  385. };
  386. static int pic32mx_write_block(struct flash_bank *bank, const uint8_t *buffer,
  387. uint32_t offset, uint32_t count)
  388. {
  389. struct target *target = bank->target;
  390. uint32_t buffer_size = 16384;
  391. struct working_area *write_algorithm;
  392. struct working_area *source;
  393. uint32_t address = bank->base + offset;
  394. struct reg_param reg_params[3];
  395. uint32_t row_size;
  396. int retval = ERROR_OK;
  397. struct pic32mx_flash_bank *pic32mx_info = bank->driver_priv;
  398. struct mips32_algorithm mips32_info;
  399. /* flash write code */
  400. if (target_alloc_working_area(target, sizeof(pic32mx_flash_write_code),
  401. &write_algorithm) != ERROR_OK) {
  402. LOG_WARNING("no working area available, can't do block memory writes");
  403. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  404. }
  405. /* Change values for counters and row size, depending on variant */
  406. switch (pic32mx_info->dev_type) {
  407. case MX_1XX_2XX:
  408. case MX_17X_27X:
  409. /* 128 byte row */
  410. pic32mx_flash_write_code[8] = 0x2CD30020;
  411. pic32mx_flash_write_code[14] = 0x24840080;
  412. pic32mx_flash_write_code[15] = 0x24A50080;
  413. pic32mx_flash_write_code[17] = 0x24C6FFE0;
  414. row_size = 128;
  415. break;
  416. default:
  417. /* 512 byte row */
  418. pic32mx_flash_write_code[8] = 0x2CD30080;
  419. pic32mx_flash_write_code[14] = 0x24840200;
  420. pic32mx_flash_write_code[15] = 0x24A50200;
  421. pic32mx_flash_write_code[17] = 0x24C6FF80;
  422. row_size = 512;
  423. break;
  424. }
  425. uint8_t code[sizeof(pic32mx_flash_write_code)];
  426. target_buffer_set_u32_array(target, code, ARRAY_SIZE(pic32mx_flash_write_code),
  427. pic32mx_flash_write_code);
  428. retval = target_write_buffer(target, write_algorithm->address, sizeof(code), code);
  429. if (retval != ERROR_OK)
  430. return retval;
  431. /* memory buffer */
  432. while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK) {
  433. buffer_size /= 2;
  434. if (buffer_size <= 256) {
  435. /* we already allocated the writing code, but failed to get a
  436. * buffer, free the algorithm */
  437. target_free_working_area(target, write_algorithm);
  438. LOG_WARNING("no large enough working area available, can't do block memory writes");
  439. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  440. }
  441. }
  442. mips32_info.common_magic = MIPS32_COMMON_MAGIC;
  443. mips32_info.isa_mode = MIPS32_ISA_MIPS32;
  444. init_reg_param(&reg_params[0], "r4", 32, PARAM_IN_OUT);
  445. init_reg_param(&reg_params[1], "r5", 32, PARAM_OUT);
  446. init_reg_param(&reg_params[2], "r6", 32, PARAM_OUT);
  447. int row_offset = offset % row_size;
  448. uint8_t *new_buffer = NULL;
  449. if (row_offset && (count >= (row_size / 4))) {
  450. new_buffer = malloc(buffer_size);
  451. if (!new_buffer) {
  452. LOG_ERROR("Out of memory");
  453. return ERROR_FAIL;
  454. }
  455. memset(new_buffer, 0xff, row_offset);
  456. address -= row_offset;
  457. } else
  458. row_offset = 0;
  459. while (count > 0) {
  460. uint32_t status;
  461. uint32_t thisrun_count;
  462. if (row_offset) {
  463. thisrun_count = (count > ((buffer_size - row_offset) / 4)) ?
  464. ((buffer_size - row_offset) / 4) : count;
  465. memcpy(new_buffer + row_offset, buffer, thisrun_count * 4);
  466. retval = target_write_buffer(target, source->address,
  467. row_offset + thisrun_count * 4, new_buffer);
  468. if (retval != ERROR_OK)
  469. break;
  470. } else {
  471. thisrun_count = (count > (buffer_size / 4)) ?
  472. (buffer_size / 4) : count;
  473. retval = target_write_buffer(target, source->address,
  474. thisrun_count * 4, buffer);
  475. if (retval != ERROR_OK)
  476. break;
  477. }
  478. buf_set_u32(reg_params[0].value, 0, 32, virt2phys(source->address));
  479. buf_set_u32(reg_params[1].value, 0, 32, virt2phys(address));
  480. buf_set_u32(reg_params[2].value, 0, 32, thisrun_count + row_offset / 4);
  481. retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
  482. write_algorithm->address,
  483. 0, 10000, &mips32_info);
  484. if (retval != ERROR_OK) {
  485. LOG_ERROR("error executing pic32mx flash write algorithm");
  486. retval = ERROR_FLASH_OPERATION_FAILED;
  487. break;
  488. }
  489. status = buf_get_u32(reg_params[0].value, 0, 32);
  490. if (status & NVMCON_NVMERR) {
  491. LOG_ERROR("Flash write error NVMERR (status = 0x%08" PRIx32 ")", status);
  492. retval = ERROR_FLASH_OPERATION_FAILED;
  493. break;
  494. }
  495. if (status & NVMCON_LVDERR) {
  496. LOG_ERROR("Flash write error LVDERR (status = 0x%08" PRIx32 ")", status);
  497. retval = ERROR_FLASH_OPERATION_FAILED;
  498. break;
  499. }
  500. buffer += thisrun_count * 4;
  501. address += thisrun_count * 4;
  502. count -= thisrun_count;
  503. if (row_offset) {
  504. address += row_offset;
  505. row_offset = 0;
  506. }
  507. }
  508. target_free_working_area(target, source);
  509. target_free_working_area(target, write_algorithm);
  510. destroy_reg_param(&reg_params[0]);
  511. destroy_reg_param(&reg_params[1]);
  512. destroy_reg_param(&reg_params[2]);
  513. free(new_buffer);
  514. return retval;
  515. }
  516. static int pic32mx_write_word(struct flash_bank *bank, uint32_t address, uint32_t word)
  517. {
  518. struct target *target = bank->target;
  519. target_write_u32(target, PIC32MX_NVMADDR, virt2phys(address));
  520. target_write_u32(target, PIC32MX_NVMDATA, word);
  521. return pic32mx_nvm_exec(bank, NVMCON_OP_WORD_PROG, 5);
  522. }
  523. static int pic32mx_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
  524. {
  525. uint32_t words_remaining = (count / 4);
  526. uint32_t bytes_remaining = (count & 0x00000003);
  527. uint32_t address = bank->base + offset;
  528. uint32_t bytes_written = 0;
  529. uint32_t status;
  530. int retval;
  531. if (bank->target->state != TARGET_HALTED) {
  532. LOG_ERROR("Target not halted");
  533. return ERROR_TARGET_NOT_HALTED;
  534. }
  535. LOG_DEBUG("writing to flash at address " TARGET_ADDR_FMT " at offset 0x%8.8" PRIx32
  536. " count: 0x%8.8" PRIx32 "", bank->base, offset, count);
  537. if (offset & 0x3) {
  538. LOG_WARNING("offset 0x%" PRIx32 "breaks required 4-byte alignment", offset);
  539. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  540. }
  541. /* multiple words (4-byte) to be programmed? */
  542. if (words_remaining > 0) {
  543. /* try using a block write */
  544. retval = pic32mx_write_block(bank, buffer, offset, words_remaining);
  545. if (retval != ERROR_OK) {
  546. if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
  547. /* if block write failed (no sufficient working area),
  548. * we use normal (slow) single dword accesses */
  549. LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
  550. } else if (retval == ERROR_FLASH_OPERATION_FAILED) {
  551. LOG_ERROR("flash writing failed");
  552. return retval;
  553. }
  554. } else {
  555. buffer += words_remaining * 4;
  556. address += words_remaining * 4;
  557. words_remaining = 0;
  558. }
  559. }
  560. while (words_remaining > 0) {
  561. uint32_t value;
  562. memcpy(&value, buffer + bytes_written, sizeof(uint32_t));
  563. status = pic32mx_write_word(bank, address, value);
  564. if (status & NVMCON_NVMERR) {
  565. LOG_ERROR("Flash write error NVMERR (status = 0x%08" PRIx32 ")", status);
  566. return ERROR_FLASH_OPERATION_FAILED;
  567. }
  568. if (status & NVMCON_LVDERR) {
  569. LOG_ERROR("Flash write error LVDERR (status = 0x%08" PRIx32 ")", status);
  570. return ERROR_FLASH_OPERATION_FAILED;
  571. }
  572. bytes_written += 4;
  573. words_remaining--;
  574. address += 4;
  575. }
  576. if (bytes_remaining) {
  577. uint32_t value = 0xffffffff;
  578. memcpy(&value, buffer + bytes_written, bytes_remaining);
  579. status = pic32mx_write_word(bank, address, value);
  580. if (status & NVMCON_NVMERR) {
  581. LOG_ERROR("Flash write error NVMERR (status = 0x%08" PRIx32 ")", status);
  582. return ERROR_FLASH_OPERATION_FAILED;
  583. }
  584. if (status & NVMCON_LVDERR) {
  585. LOG_ERROR("Flash write error LVDERR (status = 0x%08" PRIx32 ")", status);
  586. return ERROR_FLASH_OPERATION_FAILED;
  587. }
  588. }
  589. return ERROR_OK;
  590. }
  591. static int pic32mx_probe(struct flash_bank *bank)
  592. {
  593. struct target *target = bank->target;
  594. struct pic32mx_flash_bank *pic32mx_info = bank->driver_priv;
  595. struct mips32_common *mips32 = target->arch_info;
  596. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  597. int i;
  598. uint32_t num_pages = 0;
  599. uint32_t device_id;
  600. int page_size;
  601. pic32mx_info->probed = false;
  602. device_id = ejtag_info->idcode;
  603. LOG_INFO("device id = 0x%08" PRIx32 " (manuf 0x%03x dev 0x%04x, ver 0x%02x)",
  604. device_id,
  605. (unsigned)((device_id >> 1) & 0x7ff),
  606. (unsigned)((device_id >> 12) & 0xffff),
  607. (unsigned)((device_id >> 28) & 0xf));
  608. if (((device_id >> 1) & 0x7ff) != PIC32MX_MANUF_ID) {
  609. LOG_WARNING("Cannot identify target as a PIC32MX family.");
  610. return ERROR_FLASH_OPERATION_FAILED;
  611. }
  612. /* Check for PIC32mx1xx/2xx */
  613. for (i = 0; pic32mx_devs[i].name; i++) {
  614. if (pic32mx_devs[i].devid == (device_id & 0x0fffffff)) {
  615. if ((pic32mx_devs[i].name[0] == '1') || (pic32mx_devs[i].name[0] == '2'))
  616. pic32mx_info->dev_type = (pic32mx_devs[i].name[1] == '7') ? MX_17X_27X : MX_1XX_2XX;
  617. break;
  618. }
  619. }
  620. switch (pic32mx_info->dev_type) {
  621. case MX_1XX_2XX:
  622. case MX_17X_27X:
  623. page_size = 1024;
  624. break;
  625. default:
  626. page_size = 4096;
  627. break;
  628. }
  629. if (virt2phys(bank->base) == PIC32MX_PHYS_BOOT_FLASH) {
  630. /* 0x1FC00000: Boot flash size */
  631. #if 0
  632. /* for some reason this register returns 8k for the boot bank size
  633. * this does not match the docs, so for now set the boot bank at a
  634. * fixed 12k */
  635. if (target_read_u32(target, PIC32MX_BMXBOOTSZ, &num_pages) != ERROR_OK) {
  636. LOG_WARNING("PIC32MX flash size failed, probe inaccurate - assuming 12k flash");
  637. num_pages = (12 * 1024);
  638. }
  639. #else
  640. /* fixed 12k boot bank - see comments above */
  641. switch (pic32mx_info->dev_type) {
  642. case MX_1XX_2XX:
  643. case MX_17X_27X:
  644. num_pages = (3 * 1024);
  645. break;
  646. default:
  647. num_pages = (12 * 1024);
  648. break;
  649. }
  650. #endif
  651. } else {
  652. /* read the flash size from the device */
  653. if (target_read_u32(target, PIC32MX_BMXPFMSZ, &num_pages) != ERROR_OK) {
  654. switch (pic32mx_info->dev_type) {
  655. case MX_1XX_2XX:
  656. case MX_17X_27X:
  657. LOG_WARNING("PIC32MX flash size failed, probe inaccurate - assuming 32k flash");
  658. num_pages = (32 * 1024);
  659. break;
  660. default:
  661. LOG_WARNING("PIC32MX flash size failed, probe inaccurate - assuming 512k flash");
  662. num_pages = (512 * 1024);
  663. break;
  664. }
  665. }
  666. }
  667. LOG_INFO("flash size = %" PRIu32 "kbytes", num_pages / 1024);
  668. free(bank->sectors);
  669. /* calculate numbers of pages */
  670. num_pages /= page_size;
  671. bank->size = (num_pages * page_size);
  672. bank->num_sectors = num_pages;
  673. bank->sectors = malloc(sizeof(struct flash_sector) * num_pages);
  674. for (i = 0; i < (int)num_pages; i++) {
  675. bank->sectors[i].offset = i * page_size;
  676. bank->sectors[i].size = page_size;
  677. bank->sectors[i].is_erased = -1;
  678. bank->sectors[i].is_protected = 1;
  679. }
  680. pic32mx_info->probed = true;
  681. return ERROR_OK;
  682. }
  683. static int pic32mx_auto_probe(struct flash_bank *bank)
  684. {
  685. struct pic32mx_flash_bank *pic32mx_info = bank->driver_priv;
  686. if (pic32mx_info->probed)
  687. return ERROR_OK;
  688. return pic32mx_probe(bank);
  689. }
  690. static int pic32mx_info(struct flash_bank *bank, struct command_invocation *cmd)
  691. {
  692. struct target *target = bank->target;
  693. struct mips32_common *mips32 = target->arch_info;
  694. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  695. uint32_t device_id;
  696. device_id = ejtag_info->idcode;
  697. if (((device_id >> 1) & 0x7ff) != PIC32MX_MANUF_ID) {
  698. command_print_sameline(cmd,
  699. "Cannot identify target as a PIC32MX family (manufacturer 0x%03x != 0x%03x)\n",
  700. (unsigned)((device_id >> 1) & 0x7ff),
  701. PIC32MX_MANUF_ID);
  702. return ERROR_FLASH_OPERATION_FAILED;
  703. }
  704. int i;
  705. for (i = 0; pic32mx_devs[i].name; i++) {
  706. if (pic32mx_devs[i].devid == (device_id & 0x0fffffff)) {
  707. command_print_sameline(cmd, "PIC32MX%s", pic32mx_devs[i].name);
  708. break;
  709. }
  710. }
  711. if (!pic32mx_devs[i].name)
  712. command_print_sameline(cmd, "Unknown");
  713. command_print_sameline(cmd, " Ver: 0x%02x",
  714. (unsigned)((device_id >> 28) & 0xf));
  715. return ERROR_OK;
  716. }
  717. COMMAND_HANDLER(pic32mx_handle_pgm_word_command)
  718. {
  719. uint32_t address, value;
  720. int status, res;
  721. if (CMD_ARGC != 3)
  722. return ERROR_COMMAND_SYNTAX_ERROR;
  723. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
  724. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
  725. struct flash_bank *bank;
  726. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 2, &bank);
  727. if (retval != ERROR_OK)
  728. return retval;
  729. if (address < bank->base || address >= (bank->base + bank->size)) {
  730. command_print(CMD, "flash address '%s' is out of bounds", CMD_ARGV[0]);
  731. return ERROR_OK;
  732. }
  733. res = ERROR_OK;
  734. status = pic32mx_write_word(bank, address, value);
  735. if (status & NVMCON_NVMERR)
  736. res = ERROR_FLASH_OPERATION_FAILED;
  737. if (status & NVMCON_LVDERR)
  738. res = ERROR_FLASH_OPERATION_FAILED;
  739. if (res == ERROR_OK)
  740. command_print(CMD, "pic32mx pgm word complete");
  741. else
  742. command_print(CMD, "pic32mx pgm word failed (status = 0x%x)", status);
  743. return ERROR_OK;
  744. }
  745. COMMAND_HANDLER(pic32mx_handle_unlock_command)
  746. {
  747. struct target *target = NULL;
  748. struct mips_m4k_common *mips_m4k;
  749. struct mips_ejtag *ejtag_info;
  750. int timeout = 10;
  751. if (CMD_ARGC < 1) {
  752. command_print(CMD, "pic32mx unlock <bank>");
  753. return ERROR_COMMAND_SYNTAX_ERROR;
  754. }
  755. struct flash_bank *bank;
  756. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  757. if (retval != ERROR_OK)
  758. return retval;
  759. target = bank->target;
  760. mips_m4k = target_to_m4k(target);
  761. ejtag_info = &mips_m4k->mips32.ejtag_info;
  762. /* we have to use the MTAP to perform a full erase */
  763. mips_ejtag_set_instr(ejtag_info, MTAP_SW_MTAP);
  764. mips_ejtag_set_instr(ejtag_info, MTAP_COMMAND);
  765. /* first check status of device */
  766. uint8_t mchip_cmd = MCHP_STATUS;
  767. mips_ejtag_drscan_8(ejtag_info, &mchip_cmd);
  768. if (mchip_cmd & (1 << 7)) {
  769. /* device is not locked */
  770. command_print(CMD, "pic32mx is already unlocked, erasing anyway");
  771. }
  772. /* unlock/erase device */
  773. mips_ejtag_drscan_8_out(ejtag_info, MCHP_ASERT_RST);
  774. jtag_add_sleep(200);
  775. mips_ejtag_drscan_8_out(ejtag_info, MCHP_ERASE);
  776. do {
  777. mchip_cmd = MCHP_STATUS;
  778. mips_ejtag_drscan_8(ejtag_info, &mchip_cmd);
  779. if (timeout-- == 0) {
  780. LOG_DEBUG("timeout waiting for unlock: 0x%" PRIx8 "", mchip_cmd);
  781. break;
  782. }
  783. alive_sleep(1);
  784. } while ((mchip_cmd & (1 << 2)) || (!(mchip_cmd & (1 << 3))));
  785. mips_ejtag_drscan_8_out(ejtag_info, MCHP_DE_ASSERT_RST);
  786. /* select ejtag tap */
  787. mips_ejtag_set_instr(ejtag_info, MTAP_SW_ETAP);
  788. command_print(CMD, "pic32mx unlocked.\n"
  789. "INFO: a reset or power cycle is required "
  790. "for the new settings to take effect.");
  791. return ERROR_OK;
  792. }
  793. static const struct command_registration pic32mx_exec_command_handlers[] = {
  794. {
  795. .name = "pgm_word",
  796. .usage = "<addr> <value> <bank>",
  797. .handler = pic32mx_handle_pgm_word_command,
  798. .mode = COMMAND_EXEC,
  799. .help = "program a word",
  800. },
  801. {
  802. .name = "unlock",
  803. .handler = pic32mx_handle_unlock_command,
  804. .mode = COMMAND_EXEC,
  805. .usage = "[bank_id]",
  806. .help = "Unlock/Erase entire device.",
  807. },
  808. COMMAND_REGISTRATION_DONE
  809. };
  810. static const struct command_registration pic32mx_command_handlers[] = {
  811. {
  812. .name = "pic32mx",
  813. .mode = COMMAND_ANY,
  814. .help = "pic32mx flash command group",
  815. .usage = "",
  816. .chain = pic32mx_exec_command_handlers,
  817. },
  818. COMMAND_REGISTRATION_DONE
  819. };
  820. const struct flash_driver pic32mx_flash = {
  821. .name = "pic32mx",
  822. .commands = pic32mx_command_handlers,
  823. .flash_bank_command = pic32mx_flash_bank_command,
  824. .erase = pic32mx_erase,
  825. .protect = pic32mx_protect,
  826. .write = pic32mx_write,
  827. .read = default_flash_read,
  828. .probe = pic32mx_probe,
  829. .auto_probe = pic32mx_auto_probe,
  830. .erase_check = default_flash_blank_check,
  831. .protect_check = pic32mx_protect_check,
  832. .info = pic32mx_info,
  833. .free_driver_priv = default_flash_free_driver_priv,
  834. };