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.
 
 
 
 
 
 

902 lines
24 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2016 by Uladzimir Pylinski aka barthess *
  3. * barthess@yandex.ru *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <string.h>
  22. #include "imp.h"
  23. #include <jtag/jtag.h>
  24. #include <helper/time_support.h>
  25. /*
  26. ******************************************************************************
  27. * DEFINES
  28. ******************************************************************************
  29. */
  30. #define SECTOR_ERASE_TIMEOUT_MS (35 * 1000)
  31. #define XCF_PAGE_SIZE 32
  32. #define XCF_DATA_SECTOR_SIZE (1024 * 1024)
  33. #define ID_XCF01S 0x05044093
  34. #define ID_XCF02S 0x05045093
  35. #define ID_XCF04S 0x05046093
  36. #define ID_XCF08P 0x05057093
  37. #define ID_XCF16P 0x05058093
  38. #define ID_XCF32P 0x05059093
  39. #define ID_MEANINGFUL_MASK 0x0FFFFFFF
  40. static const char * const xcf_name_list[] = {
  41. "XCF08P",
  42. "XCF16P",
  43. "XCF32P",
  44. "unknown"
  45. };
  46. struct xcf_priv {
  47. bool probed;
  48. };
  49. struct xcf_status {
  50. bool isc_error; /* false == OK, true == error */
  51. bool prog_error; /* false == OK, true == error */
  52. bool prog_busy; /* false == idle, true == busy */
  53. bool isc_mode; /* false == normal mode, true == ISC mode */
  54. };
  55. /*
  56. ******************************************************************************
  57. * GLOBAL VARIABLES
  58. ******************************************************************************
  59. */
  60. static const uint8_t cmd_bypass[2] = {0xFF, 0xFF};
  61. static const uint8_t cmd_isc_address_shift[2] = {0xEB, 0x00};
  62. static const uint8_t cmd_isc_data_shift[2] = {0xED, 0x00};
  63. static const uint8_t cmd_isc_disable[2] = {0xF0, 0x00};
  64. static const uint8_t cmd_isc_enable[2] = {0xE8, 0x00};
  65. static const uint8_t cmd_isc_erase[2] = {0xEC, 0x00};
  66. static const uint8_t cmd_isc_program[2] = {0xEA, 0x00};
  67. static const uint8_t cmd_xsc_blank_check[2] = {0x0D, 0x00};
  68. static const uint8_t cmd_xsc_config[2] = {0xEE, 0x00};
  69. static const uint8_t cmd_xsc_data_btc[2] = {0xF2, 0x00};
  70. static const uint8_t cmd_xsc_data_ccb[2] = {0x0C, 0x00};
  71. static const uint8_t cmd_xsc_data_done[2] = {0x09, 0x00};
  72. static const uint8_t cmd_xsc_data_sucr[2] = {0x0E, 0x00};
  73. static const uint8_t cmd_xsc_data_wrpt[2] = {0xF7, 0x00};
  74. static const uint8_t cmd_xsc_op_status[2] = {0xE3, 0x00};
  75. static const uint8_t cmd_xsc_read[2] = {0xEF, 0x00};
  76. static const uint8_t cmd_xsc_unlock[2] = {0x55, 0xAA};
  77. /*
  78. ******************************************************************************
  79. * LOCAL FUNCTIONS
  80. ******************************************************************************
  81. */
  82. static const char *product_name(const struct flash_bank *bank)
  83. {
  84. switch (bank->target->tap->idcode & ID_MEANINGFUL_MASK) {
  85. case ID_XCF08P:
  86. return xcf_name_list[0];
  87. case ID_XCF16P:
  88. return xcf_name_list[1];
  89. case ID_XCF32P:
  90. return xcf_name_list[2];
  91. default:
  92. return xcf_name_list[3];
  93. }
  94. }
  95. static void fill_sector_table(struct flash_bank *bank)
  96. {
  97. /* Note: is_erased and is_protected fields must be set here to an unknown
  98. * state, they will be correctly filled from other API calls. */
  99. for (unsigned int i = 0; i < bank->num_sectors; i++) {
  100. bank->sectors[i].is_erased = -1;
  101. bank->sectors[i].is_protected = -1;
  102. }
  103. for (unsigned int i = 0; i < bank->num_sectors; i++) {
  104. bank->sectors[i].size = XCF_DATA_SECTOR_SIZE;
  105. bank->sectors[i].offset = i * XCF_DATA_SECTOR_SIZE;
  106. }
  107. bank->size = bank->num_sectors * XCF_DATA_SECTOR_SIZE;
  108. }
  109. static struct xcf_status read_status(struct flash_bank *bank)
  110. {
  111. struct xcf_status ret;
  112. uint8_t irdata[2];
  113. struct scan_field scan;
  114. scan.check_mask = NULL;
  115. scan.check_value = NULL;
  116. scan.num_bits = 16;
  117. scan.out_value = cmd_bypass;
  118. scan.in_value = irdata;
  119. jtag_add_ir_scan(bank->target->tap, &scan, TAP_IDLE);
  120. jtag_execute_queue();
  121. ret.isc_error = ((irdata[0] >> 7) & 3) == 0b01;
  122. ret.prog_error = ((irdata[0] >> 5) & 3) == 0b01;
  123. ret.prog_busy = ((irdata[0] >> 4) & 1) == 0;
  124. ret.isc_mode = ((irdata[0] >> 3) & 1) == 1;
  125. return ret;
  126. }
  127. static int isc_enter(struct flash_bank *bank)
  128. {
  129. struct xcf_status status = read_status(bank);
  130. if (true == status.isc_mode)
  131. return ERROR_OK;
  132. else {
  133. struct scan_field scan;
  134. scan.check_mask = NULL;
  135. scan.check_value = NULL;
  136. scan.num_bits = 16;
  137. scan.out_value = cmd_isc_enable;
  138. scan.in_value = NULL;
  139. jtag_add_ir_scan(bank->target->tap, &scan, TAP_IDLE);
  140. jtag_execute_queue();
  141. status = read_status(bank);
  142. if (!status.isc_mode) {
  143. LOG_ERROR("*** XCF: FAILED to enter ISC mode");
  144. return ERROR_FLASH_OPERATION_FAILED;
  145. }
  146. return ERROR_OK;
  147. }
  148. }
  149. static int isc_leave(struct flash_bank *bank)
  150. {
  151. struct xcf_status status = read_status(bank);
  152. if (!status.isc_mode)
  153. return ERROR_OK;
  154. else {
  155. struct scan_field scan;
  156. scan.check_mask = NULL;
  157. scan.check_value = NULL;
  158. scan.num_bits = 16;
  159. scan.out_value = cmd_isc_disable;
  160. scan.in_value = NULL;
  161. jtag_add_ir_scan(bank->target->tap, &scan, TAP_IDLE);
  162. jtag_execute_queue();
  163. alive_sleep(1); /* device needs 50 uS to leave ISC mode */
  164. status = read_status(bank);
  165. if (status.isc_mode) {
  166. LOG_ERROR("*** XCF: FAILED to leave ISC mode");
  167. return ERROR_FLASH_OPERATION_FAILED;
  168. }
  169. return ERROR_OK;
  170. }
  171. }
  172. static int sector_state(uint8_t wrpt, int sector)
  173. {
  174. if (((wrpt >> sector) & 1) == 1)
  175. return 0;
  176. else
  177. return 1;
  178. }
  179. static uint8_t fill_select_block(unsigned int first, unsigned int last)
  180. {
  181. uint8_t ret = 0;
  182. for (unsigned int i = first; i <= last; i++)
  183. ret |= 1 << i;
  184. return ret;
  185. }
  186. static int isc_read_register(struct flash_bank *bank, const uint8_t *cmd,
  187. uint8_t *data_buf, int num_bits)
  188. {
  189. struct scan_field scan;
  190. scan.check_mask = NULL;
  191. scan.check_value = NULL;
  192. scan.out_value = cmd;
  193. scan.in_value = NULL;
  194. scan.num_bits = 16;
  195. jtag_add_ir_scan(bank->target->tap, &scan, TAP_DRSHIFT);
  196. scan.out_value = NULL;
  197. scan.in_value = data_buf;
  198. scan.num_bits = num_bits;
  199. jtag_add_dr_scan(bank->target->tap, 1, &scan, TAP_IDLE);
  200. return jtag_execute_queue();
  201. }
  202. static int isc_wait_erase_program(struct flash_bank *bank, int64_t timeout_ms)
  203. {
  204. uint8_t isc_default;
  205. int64_t t0 = timeval_ms();
  206. int64_t dt;
  207. do {
  208. isc_read_register(bank, cmd_xsc_op_status, &isc_default, 8);
  209. if (((isc_default >> 2) & 1) == 1)
  210. return ERROR_OK;
  211. dt = timeval_ms() - t0;
  212. } while (dt <= timeout_ms);
  213. return ERROR_FLASH_OPERATION_FAILED;
  214. }
  215. /*
  216. * helper function for procedures without program jtag command at the end
  217. */
  218. static int isc_set_register(struct flash_bank *bank, const uint8_t *cmd,
  219. const uint8_t *data_buf, int num_bits, int64_t timeout_ms)
  220. {
  221. struct scan_field scan;
  222. scan.check_mask = NULL;
  223. scan.check_value = NULL;
  224. scan.num_bits = 16;
  225. scan.out_value = cmd;
  226. scan.in_value = NULL;
  227. jtag_add_ir_scan(bank->target->tap, &scan, TAP_DRSHIFT);
  228. scan.num_bits = num_bits;
  229. scan.out_value = data_buf;
  230. scan.in_value = NULL;
  231. jtag_add_dr_scan(bank->target->tap, 1, &scan, TAP_IDLE);
  232. if (timeout_ms == 0)
  233. return jtag_execute_queue();
  234. else
  235. return isc_wait_erase_program(bank, timeout_ms);
  236. }
  237. /*
  238. * helper function for procedures required program jtag command at the end
  239. */
  240. static int isc_program_register(struct flash_bank *bank, const uint8_t *cmd,
  241. const uint8_t *data_buf, int num_bits, int64_t timeout_ms)
  242. {
  243. struct scan_field scan;
  244. scan.check_mask = NULL;
  245. scan.check_value = NULL;
  246. scan.num_bits = 16;
  247. scan.out_value = cmd;
  248. scan.in_value = NULL;
  249. jtag_add_ir_scan(bank->target->tap, &scan, TAP_DRSHIFT);
  250. scan.num_bits = num_bits;
  251. scan.out_value = data_buf;
  252. scan.in_value = NULL;
  253. jtag_add_dr_scan(bank->target->tap, 1, &scan, TAP_IRSHIFT);
  254. scan.num_bits = 16;
  255. scan.out_value = cmd_isc_program;
  256. scan.in_value = NULL;
  257. jtag_add_ir_scan(bank->target->tap, &scan, TAP_IDLE);
  258. if (timeout_ms == 0)
  259. return jtag_execute_queue();
  260. else
  261. return isc_wait_erase_program(bank, timeout_ms);
  262. }
  263. static int isc_clear_protect(struct flash_bank *bank, unsigned int first,
  264. unsigned int last)
  265. {
  266. uint8_t select_block[3] = {0x0, 0x0, 0x0};
  267. select_block[0] = fill_select_block(first, last);
  268. return isc_set_register(bank, cmd_xsc_unlock, select_block, 24, 0);
  269. }
  270. static int isc_set_protect(struct flash_bank *bank, unsigned int first,
  271. unsigned int last)
  272. {
  273. uint8_t wrpt[2] = {0xFF, 0xFF};
  274. for (unsigned int i = first; i <= last; i++)
  275. wrpt[0] &= ~(1 << i);
  276. return isc_program_register(bank, cmd_xsc_data_wrpt, wrpt, 16, 0);
  277. }
  278. static int isc_erase_sectors(struct flash_bank *bank, unsigned int first,
  279. unsigned int last)
  280. {
  281. uint8_t select_block[3] = {0, 0, 0};
  282. select_block[0] = fill_select_block(first, last);
  283. int64_t timeout = SECTOR_ERASE_TIMEOUT_MS * (last - first + 1);
  284. return isc_set_register(bank, cmd_isc_erase, select_block, 24, timeout);
  285. }
  286. static int isc_adr_shift(struct flash_bank *bank, int adr)
  287. {
  288. uint8_t adr_buf[3];
  289. h_u24_to_le(adr_buf, adr);
  290. return isc_set_register(bank, cmd_isc_address_shift, adr_buf, 24, 0);
  291. }
  292. static int isc_program_data_page(struct flash_bank *bank, const uint8_t *page_buf)
  293. {
  294. return isc_program_register(bank, cmd_isc_data_shift, page_buf, 8 * XCF_PAGE_SIZE, 100);
  295. }
  296. static void isc_data_read_out(struct flash_bank *bank, uint8_t *buffer, uint32_t count)
  297. {
  298. struct scan_field scan;
  299. /* Do not change this code with isc_read_register() call because it needs
  300. * transition to IDLE state before data retrieving. */
  301. scan.check_mask = NULL;
  302. scan.check_value = NULL;
  303. scan.num_bits = 16;
  304. scan.out_value = cmd_xsc_read;
  305. scan.in_value = NULL;
  306. jtag_add_ir_scan(bank->target->tap, &scan, TAP_IDLE);
  307. scan.num_bits = 8 * count;
  308. scan.out_value = NULL;
  309. scan.in_value = buffer;
  310. jtag_add_dr_scan(bank->target->tap, 1, &scan, TAP_IDLE);
  311. jtag_execute_queue();
  312. }
  313. static int isc_set_data_done(struct flash_bank *bank, int sector)
  314. {
  315. uint8_t done = 0xFF;
  316. done &= ~(1 << sector);
  317. return isc_program_register(bank, cmd_xsc_data_done, &done, 8, 100);
  318. }
  319. static void flip_u8(uint8_t *out, const uint8_t *in, int len)
  320. {
  321. for (int i = 0; i < len; i++)
  322. out[i] = flip_u32(in[i], 8);
  323. }
  324. /*
  325. * Xilinx bin file contains simple fixed header for automatic bus width detection:
  326. * 16 bytes of 0xFF
  327. * 4 byte sync word 0xAA995566 or (bit reversed) 0x5599AA66 in MSC file
  328. *
  329. * Function presumes need of bit reversing if it can not exactly detects
  330. * the opposite.
  331. */
  332. static bool need_bit_reverse(const uint8_t *buffer)
  333. {
  334. const size_t L = 20;
  335. uint8_t reference[L];
  336. memset(reference, 0xFF, 16);
  337. reference[16] = 0x55;
  338. reference[17] = 0x99;
  339. reference[18] = 0xAA;
  340. reference[19] = 0x66;
  341. if (memcmp(reference, buffer, L) == 0)
  342. return false;
  343. else
  344. return true;
  345. }
  346. /*
  347. * The page address to be programmed is determined by loading the
  348. * internal ADDRESS Register using an ISC_ADDRESS_SHIFT instruction sequence.
  349. * The page address automatically increments to the next 256-bit
  350. * page address after each programming sequence until the last address
  351. * in the 8 Mb block is reached. To continue programming the next block,
  352. * the next 8 Mb block's starting address must be loaded into the
  353. * internal ADDRESS register.
  354. */
  355. static int read_write_data(struct flash_bank *bank, const uint8_t *w_buffer,
  356. uint8_t *r_buffer, bool write_flag, uint32_t offset, uint32_t count)
  357. {
  358. int dbg_count = count;
  359. int dbg_written = 0;
  360. int ret = ERROR_OK;
  361. uint8_t *page_buf = malloc(XCF_PAGE_SIZE);
  362. bool revbit = true;
  363. isc_enter(bank);
  364. if (offset % XCF_PAGE_SIZE != 0) {
  365. ret = ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  366. goto EXIT;
  367. }
  368. if ((offset + count) > (bank->num_sectors * XCF_DATA_SECTOR_SIZE)) {
  369. ret = ERROR_FLASH_DST_OUT_OF_BANK;
  370. goto EXIT;
  371. }
  372. if ((write_flag) && (offset == 0) && (count >= XCF_PAGE_SIZE))
  373. revbit = need_bit_reverse(w_buffer);
  374. while (count > 0) {
  375. uint32_t sector_num = offset / XCF_DATA_SECTOR_SIZE;
  376. uint32_t sector_offset = offset - sector_num * XCF_DATA_SECTOR_SIZE;
  377. uint32_t sector_bytes = XCF_DATA_SECTOR_SIZE - sector_offset;
  378. if (count < sector_bytes)
  379. sector_bytes = count;
  380. isc_adr_shift(bank, offset);
  381. offset += sector_bytes;
  382. count -= sector_bytes;
  383. if (write_flag) {
  384. while (sector_bytes > 0) {
  385. int len;
  386. if (sector_bytes < XCF_PAGE_SIZE) {
  387. len = sector_bytes;
  388. memset(page_buf, 0xFF, XCF_PAGE_SIZE);
  389. } else
  390. len = XCF_PAGE_SIZE;
  391. if (revbit)
  392. flip_u8(page_buf, w_buffer, len);
  393. else
  394. memcpy(page_buf, w_buffer, len);
  395. w_buffer += len;
  396. sector_bytes -= len;
  397. ret = isc_program_data_page(bank, page_buf);
  398. if (ret != ERROR_OK)
  399. goto EXIT;
  400. else {
  401. LOG_DEBUG("written %d bytes from %d", dbg_written, dbg_count);
  402. dbg_written += len;
  403. }
  404. }
  405. } else {
  406. isc_data_read_out(bank, r_buffer, sector_bytes);
  407. flip_u8(r_buffer, r_buffer, sector_bytes);
  408. r_buffer += sector_bytes;
  409. }
  410. }
  411. /* Set 'done' flags for all data sectors because driver supports
  412. * only single revision. */
  413. if (write_flag) {
  414. for (unsigned int i = 0; i < bank->num_sectors; i++) {
  415. ret = isc_set_data_done(bank, i);
  416. if (ret != ERROR_OK)
  417. goto EXIT;
  418. }
  419. }
  420. EXIT:
  421. free(page_buf);
  422. isc_leave(bank);
  423. return ret;
  424. }
  425. static uint16_t isc_read_ccb(struct flash_bank *bank)
  426. {
  427. uint8_t ccb[2];
  428. isc_read_register(bank, cmd_xsc_data_ccb, ccb, 16);
  429. return le_to_h_u16(ccb);
  430. }
  431. static unsigned int gucr_num(const struct flash_bank *bank)
  432. {
  433. return bank->num_sectors;
  434. }
  435. static unsigned int sucr_num(const struct flash_bank *bank)
  436. {
  437. return bank->num_sectors + 1;
  438. }
  439. static int isc_program_ccb(struct flash_bank *bank, uint16_t ccb)
  440. {
  441. uint8_t buf[2];
  442. h_u16_to_le(buf, ccb);
  443. return isc_program_register(bank, cmd_xsc_data_ccb, buf, 16, 100);
  444. }
  445. static int isc_program_singe_revision_sucr(struct flash_bank *bank)
  446. {
  447. uint8_t sucr[2] = {0xFC, 0xFF};
  448. return isc_program_register(bank, cmd_xsc_data_sucr, sucr, 16, 100);
  449. }
  450. static int isc_program_single_revision_btc(struct flash_bank *bank)
  451. {
  452. uint8_t buf[4];
  453. uint32_t btc = 0xFFFFFFFF;
  454. btc &= ~0b1111;
  455. btc |= ((bank->num_sectors - 1) << 2);
  456. btc &= ~(1 << 4);
  457. h_u32_to_le(buf, btc);
  458. return isc_program_register(bank, cmd_xsc_data_btc, buf, 32, 100);
  459. }
  460. static int fpga_configure(struct flash_bank *bank)
  461. {
  462. struct scan_field scan;
  463. scan.check_mask = NULL;
  464. scan.check_value = NULL;
  465. scan.num_bits = 16;
  466. scan.out_value = cmd_xsc_config;
  467. scan.in_value = NULL;
  468. jtag_add_ir_scan(bank->target->tap, &scan, TAP_IDLE);
  469. jtag_execute_queue();
  470. return ERROR_OK;
  471. }
  472. /*
  473. ******************************************************************************
  474. * EXPORTED FUNCTIONS
  475. ******************************************************************************
  476. */
  477. FLASH_BANK_COMMAND_HANDLER(xcf_flash_bank_command)
  478. {
  479. struct xcf_priv *priv;
  480. priv = malloc(sizeof(struct xcf_priv));
  481. if (!priv) {
  482. LOG_ERROR("no memory for flash bank info");
  483. return ERROR_FAIL;
  484. }
  485. bank->driver_priv = priv;
  486. priv->probed = false;
  487. return ERROR_OK;
  488. }
  489. static int xcf_info(struct flash_bank *bank, struct command_invocation *cmd)
  490. {
  491. const struct xcf_priv *priv = bank->driver_priv;
  492. if (!priv->probed) {
  493. command_print_sameline(cmd, "\nXCF flash bank not probed yet\n");
  494. return ERROR_OK;
  495. }
  496. command_print_sameline(cmd, "%s", product_name(bank));
  497. return ERROR_OK;
  498. }
  499. static int xcf_probe(struct flash_bank *bank)
  500. {
  501. struct xcf_priv *priv = bank->driver_priv;
  502. uint32_t id;
  503. if (priv->probed)
  504. free(bank->sectors);
  505. priv->probed = false;
  506. if (!bank->target->tap) {
  507. LOG_ERROR("Target has no JTAG tap");
  508. return ERROR_FAIL;
  509. }
  510. /* check idcode and alloc memory for sector table */
  511. if (!bank->target->tap->hasidcode)
  512. return ERROR_FLASH_OPERATION_FAILED;
  513. /* guess number of blocks using chip ID */
  514. id = bank->target->tap->idcode;
  515. switch (id & ID_MEANINGFUL_MASK) {
  516. case ID_XCF08P:
  517. bank->num_sectors = 1;
  518. break;
  519. case ID_XCF16P:
  520. bank->num_sectors = 2;
  521. break;
  522. case ID_XCF32P:
  523. bank->num_sectors = 4;
  524. break;
  525. default:
  526. LOG_ERROR("Unknown flash device ID 0x%" PRIX32, id);
  527. return ERROR_FAIL;
  528. }
  529. bank->sectors = malloc(bank->num_sectors * sizeof(struct flash_sector));
  530. if (!bank->sectors) {
  531. LOG_ERROR("No memory for sector table");
  532. return ERROR_FAIL;
  533. }
  534. fill_sector_table(bank);
  535. priv->probed = true;
  536. /* REVISIT: Why is unchanged bank->driver_priv rewritten by same value? */
  537. bank->driver_priv = priv;
  538. LOG_INFO("product name: %s", product_name(bank));
  539. LOG_INFO("device id = 0x%" PRIX32, bank->target->tap->idcode);
  540. LOG_INFO("flash size = %d configuration bits",
  541. bank->num_sectors * XCF_DATA_SECTOR_SIZE * 8);
  542. LOG_INFO("number of sectors = %u", bank->num_sectors);
  543. return ERROR_OK;
  544. }
  545. static int xcf_auto_probe(struct flash_bank *bank)
  546. {
  547. struct xcf_priv *priv = bank->driver_priv;
  548. if (priv->probed)
  549. return ERROR_OK;
  550. else
  551. return xcf_probe(bank);
  552. }
  553. static int xcf_protect_check(struct flash_bank *bank)
  554. {
  555. uint8_t wrpt[2];
  556. isc_enter(bank);
  557. isc_read_register(bank, cmd_xsc_data_wrpt, wrpt, 16);
  558. isc_leave(bank);
  559. for (unsigned int i = 0; i < bank->num_sectors; i++)
  560. bank->sectors[i].is_protected = sector_state(wrpt[0], i);
  561. return ERROR_OK;
  562. }
  563. static int xcf_erase_check(struct flash_bank *bank)
  564. {
  565. uint8_t blankreg;
  566. struct scan_field scan;
  567. isc_enter(bank);
  568. /* Do not change this code with isc_read_register() call because it needs
  569. * transition to IDLE state and pause before data retrieving. */
  570. scan.check_mask = NULL;
  571. scan.check_value = NULL;
  572. scan.num_bits = 16;
  573. scan.out_value = cmd_xsc_blank_check;
  574. scan.in_value = NULL;
  575. jtag_add_ir_scan(bank->target->tap, &scan, TAP_IDLE);
  576. jtag_execute_queue();
  577. alive_sleep(500); /* device needs at least 0.5s to self check */
  578. scan.num_bits = 8;
  579. scan.in_value = &blankreg;
  580. jtag_add_dr_scan(bank->target->tap, 1, &scan, TAP_IDLE);
  581. jtag_execute_queue();
  582. isc_leave(bank);
  583. for (unsigned int i = 0; i < bank->num_sectors; i++)
  584. bank->sectors[i].is_erased = sector_state(blankreg, i);
  585. return ERROR_OK;
  586. }
  587. static int xcf_erase(struct flash_bank *bank, unsigned int first,
  588. unsigned int last)
  589. {
  590. if ((first >= bank->num_sectors)
  591. || (last >= bank->num_sectors)
  592. || (last < first))
  593. return ERROR_FLASH_SECTOR_INVALID;
  594. else {
  595. isc_enter(bank);
  596. isc_clear_protect(bank, first, last);
  597. int ret = isc_erase_sectors(bank, first, last);
  598. isc_leave(bank);
  599. return ret;
  600. }
  601. }
  602. static int xcf_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  603. {
  604. return read_write_data(bank, NULL, buffer, false, offset, count);
  605. }
  606. static int xcf_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset,
  607. uint32_t count)
  608. {
  609. return read_write_data(bank, buffer, NULL, true, offset, count);
  610. }
  611. static int xcf_protect(struct flash_bank *bank, int set, unsigned int first,
  612. unsigned int last)
  613. {
  614. int ret;
  615. isc_enter(bank);
  616. if (set)
  617. ret = isc_set_protect(bank, first, last);
  618. else {
  619. /* write protection may be removed only with following erase */
  620. isc_clear_protect(bank, first, last);
  621. ret = isc_erase_sectors(bank, first, last);
  622. }
  623. isc_leave(bank);
  624. return ret;
  625. }
  626. COMMAND_HANDLER(xcf_handle_ccb_command) {
  627. if (!((CMD_ARGC == 1) || (CMD_ARGC == 5)))
  628. return ERROR_COMMAND_SYNTAX_ERROR;
  629. struct flash_bank *bank;
  630. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  631. if (retval != ERROR_OK)
  632. return retval;
  633. uint16_t ccb = 0xFFFF;
  634. isc_enter(bank);
  635. uint16_t old_ccb = isc_read_ccb(bank);
  636. isc_leave(bank);
  637. if (CMD_ARGC == 1) {
  638. LOG_INFO("current CCB = 0x%X", old_ccb);
  639. return ERROR_OK;
  640. } else {
  641. /* skip over flash bank */
  642. CMD_ARGC--;
  643. CMD_ARGV++;
  644. while (CMD_ARGC) {
  645. if (strcmp("external", CMD_ARGV[0]) == 0)
  646. ccb |= (1 << 0);
  647. else if (strcmp("internal", CMD_ARGV[0]) == 0)
  648. ccb &= ~(1 << 0);
  649. else if (strcmp("serial", CMD_ARGV[0]) == 0)
  650. ccb |= (3 << 1);
  651. else if (strcmp("parallel", CMD_ARGV[0]) == 0)
  652. ccb &= ~(3 << 1);
  653. else if (strcmp("slave", CMD_ARGV[0]) == 0)
  654. ccb |= (1 << 3);
  655. else if (strcmp("master", CMD_ARGV[0]) == 0)
  656. ccb &= ~(1 << 3);
  657. else if (strcmp("40", CMD_ARGV[0]) == 0)
  658. ccb |= (3 << 4);
  659. else if (strcmp("20", CMD_ARGV[0]) == 0)
  660. ccb &= ~(1 << 5);
  661. else
  662. return ERROR_COMMAND_SYNTAX_ERROR;
  663. CMD_ARGC--;
  664. CMD_ARGV++;
  665. }
  666. isc_enter(bank);
  667. int sector;
  668. /* GUCR sector */
  669. sector = gucr_num(bank);
  670. isc_clear_protect(bank, sector, sector);
  671. int ret = isc_erase_sectors(bank, sector, sector);
  672. if (ret != ERROR_OK)
  673. goto EXIT;
  674. ret = isc_program_ccb(bank, ccb);
  675. if (ret != ERROR_OK)
  676. goto EXIT;
  677. ret = isc_program_single_revision_btc(bank);
  678. if (ret != ERROR_OK)
  679. goto EXIT;
  680. ret = isc_set_data_done(bank, sector);
  681. if (ret != ERROR_OK)
  682. goto EXIT;
  683. /* SUCR sector */
  684. sector = sucr_num(bank);
  685. isc_clear_protect(bank, sector, sector);
  686. ret = isc_erase_sectors(bank, sector, sector);
  687. if (ret != ERROR_OK)
  688. goto EXIT;
  689. ret = isc_program_singe_revision_sucr(bank);
  690. if (ret != ERROR_OK)
  691. goto EXIT;
  692. ret = isc_set_data_done(bank, sector);
  693. if (ret != ERROR_OK)
  694. goto EXIT;
  695. EXIT:
  696. isc_leave(bank);
  697. return ret;
  698. }
  699. }
  700. COMMAND_HANDLER(xcf_handle_configure_command) {
  701. if (CMD_ARGC != 1)
  702. return ERROR_COMMAND_SYNTAX_ERROR;
  703. struct flash_bank *bank;
  704. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  705. if (retval != ERROR_OK)
  706. return retval;
  707. return fpga_configure(bank);
  708. }
  709. static const struct command_registration xcf_exec_command_handlers[] = {
  710. {
  711. .name = "configure",
  712. .handler = xcf_handle_configure_command,
  713. .mode = COMMAND_EXEC,
  714. .usage = "bank_id",
  715. .help = "Initiate FPGA loading procedure."
  716. },
  717. {
  718. .name = "ccb",
  719. .handler = xcf_handle_ccb_command,
  720. .mode = COMMAND_EXEC,
  721. .usage = "bank_id [('external'|'internal') "
  722. "('serial'|'parallel') "
  723. "('slave'|'master') "
  724. "('40'|'20')]",
  725. .help = "Write CCB register with supplied options and (silently) BTC "
  726. "register with single revision options. Display current "
  727. "CCB value when only bank_id supplied. "
  728. "Following options available: "
  729. "1) external or internal clock source; "
  730. "2) serial or parallel bus mode; "
  731. "3) slave or master mode; "
  732. "4) clock frequency in MHz for internal clock in master mode;"
  733. },
  734. COMMAND_REGISTRATION_DONE
  735. };
  736. static const struct command_registration xcf_command_handlers[] = {
  737. {
  738. .name = "xcf",
  739. .mode = COMMAND_ANY,
  740. .help = "Xilinx platform flash command group",
  741. .usage = "",
  742. .chain = xcf_exec_command_handlers
  743. },
  744. COMMAND_REGISTRATION_DONE
  745. };
  746. const struct flash_driver xcf_flash = {
  747. .name = "xcf",
  748. .usage = NULL,
  749. .commands = xcf_command_handlers,
  750. .flash_bank_command = xcf_flash_bank_command,
  751. .erase = xcf_erase,
  752. .protect = xcf_protect,
  753. .write = xcf_write,
  754. .read = xcf_read,
  755. .probe = xcf_probe,
  756. .auto_probe = xcf_auto_probe,
  757. .erase_check = xcf_erase_check,
  758. .protect_check = xcf_protect_check,
  759. .info = xcf_info,
  760. .free_driver_priv = default_flash_free_driver_priv,
  761. };