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