OpenOCD
jtagspi.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2015 Robert Jordens <jordens@gmail.com> *
5  ***************************************************************************/
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include "imp.h"
12 #include <jtag/jtag.h>
13 #include <flash/nor/spi.h>
14 #include <helper/time_support.h>
15 #include <pld/pld.h>
16 
17 #define JTAGSPI_MAX_TIMEOUT 3000
18 
19 
21  struct jtag_tap *tap;
22  struct flash_device dev;
23  char devname[32];
24  bool probed;
25  bool always_4byte; /* use always 4-byte address except for basic read 0x03 */
26  unsigned int addr_len; /* address length in bytes */
27  struct pld_device *pld_device; /* if not NULL, the PLD has special instructions for JTAGSPI */
28  uint32_t ir; /* when !pld_device, this instruction code is used in
29  jtagspi_set_user_ir to connect through a proxy bitstream */
30 };
31 
32 FLASH_BANK_COMMAND_HANDLER(jtagspi_flash_bank_command)
33 {
34  if (CMD_ARGC < 7)
36 
37  unsigned int ir = 0;
38  struct pld_device *device = NULL;
39  if (strcmp(CMD_ARGV[6], "-pld") == 0) {
40  if (CMD_ARGC < 8)
43  if (device) {
44  bool has_jtagspi_instruction = false;
45  int retval = pld_has_jtagspi_instruction(device, &has_jtagspi_instruction);
46  if (retval != ERROR_OK)
47  return retval;
48  if (!has_jtagspi_instruction) {
49  retval = pld_get_jtagspi_userircode(device, &ir);
50  if (retval != ERROR_OK)
51  return retval;
52  device = NULL;
53  }
54  } else {
55  LOG_ERROR("pld device '#%s' is out of bounds or unknown", CMD_ARGV[7]);
56  return ERROR_FAIL;
57  }
58  } else {
59  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[6], ir);
60  }
61 
62  struct jtagspi_flash_bank *info = calloc(1, sizeof(struct jtagspi_flash_bank));
63  if (!info) {
64  LOG_ERROR("no memory for flash bank info");
65  return ERROR_FAIL;
66  }
67  bank->sectors = NULL;
68  bank->driver_priv = info;
69 
70  if (!bank->target->tap) {
71  LOG_ERROR("Target has no JTAG tap");
72  return ERROR_FAIL;
73  }
74  info->tap = bank->target->tap;
75  info->probed = false;
76 
77  info->ir = ir;
78  info->pld_device = device;
79 
80  return ERROR_OK;
81 }
82 
84 {
85  struct scan_field field;
86  uint8_t buf[4] = { 0 };
87 
88  LOG_DEBUG("loading jtagspi ir(0x%" PRIx32 ")", info->ir);
89  buf_set_u32(buf, 0, info->tap->ir_length, info->ir);
90  field.num_bits = info->tap->ir_length;
91  field.out_value = buf;
92  field.in_value = NULL;
93  jtag_add_ir_scan(info->tap, &field, TAP_IDLE);
94 }
95 
96 static void flip_u8(const uint8_t *in, uint8_t *out, unsigned int len)
97 {
98  for (unsigned int i = 0; i < len; i++)
99  out[i] = flip_u32(in[i], 8);
100 }
101 
102 static int jtagspi_cmd(struct flash_bank *bank, uint8_t cmd,
103  uint8_t *write_buffer, unsigned int write_len, uint8_t *data_buffer, int data_len)
104 {
105  assert(write_buffer || write_len == 0);
106  assert(data_buffer || data_len == 0);
107 
108  struct scan_field fields[6];
109  struct jtagspi_flash_bank *info = bank->driver_priv;
110 
111  LOG_DEBUG("cmd=0x%02x write_len=%d data_len=%d", cmd, write_len, data_len);
112 
113  /* negative data_len == read operation */
114  const bool is_read = (data_len < 0);
115  if (is_read)
116  data_len = -data_len;
117 
118  unsigned int facing_read_bits = 0;
119  unsigned int trailing_write_bits = 0;
120 
121  if (info->pld_device) {
122  int retval = pld_get_jtagspi_stuff_bits(info->pld_device, &facing_read_bits, &trailing_write_bits);
123  if (retval != ERROR_OK)
124  return retval;
125  }
126 
127  int n = 0;
128  const uint8_t marker = 1;
129  uint8_t xfer_bits[4];
130  if (!info->pld_device) { /* mode == JTAGSPI_MODE_PROXY_BITSTREAM */
131  facing_read_bits = jtag_tap_count_enabled();
132  fields[n].num_bits = 1;
133  fields[n].out_value = &marker;
134  fields[n].in_value = NULL;
135  n++;
136 
137  /* transfer length = cmd + address + read/write,
138  * -1 due to the counter implementation */
139  h_u32_to_be(xfer_bits, ((sizeof(cmd) + write_len + data_len) * CHAR_BIT) - 1);
140  flip_u8(xfer_bits, xfer_bits, sizeof(xfer_bits));
141  fields[n].num_bits = sizeof(xfer_bits) * CHAR_BIT;
142  fields[n].out_value = xfer_bits;
143  fields[n].in_value = NULL;
144  n++;
145  }
146 
147  flip_u8(&cmd, &cmd, sizeof(cmd));
148  fields[n].num_bits = sizeof(cmd) * CHAR_BIT;
149  fields[n].out_value = &cmd;
150  fields[n].in_value = NULL;
151  n++;
152 
153  if (write_len) {
154  flip_u8(write_buffer, write_buffer, write_len);
155  fields[n].num_bits = write_len * CHAR_BIT;
156  fields[n].out_value = write_buffer;
157  fields[n].in_value = NULL;
158  n++;
159  }
160 
161  if (data_len > 0) {
162  if (is_read) {
163  if (facing_read_bits) {
164  fields[n].num_bits = facing_read_bits;
165  fields[n].out_value = NULL;
166  fields[n].in_value = NULL;
167  n++;
168  }
169 
170  fields[n].out_value = NULL;
171  fields[n].in_value = data_buffer;
172  } else {
173  flip_u8(data_buffer, data_buffer, data_len);
174  fields[n].out_value = data_buffer;
175  fields[n].in_value = NULL;
176  }
177  fields[n].num_bits = data_len * CHAR_BIT;
178  n++;
179  }
180  if (!is_read && trailing_write_bits) {
181  fields[n].num_bits = trailing_write_bits;
182  fields[n].out_value = NULL;
183  fields[n].in_value = NULL;
184  n++;
185  }
186 
187  if (info->pld_device) {
188  int retval = pld_connect_spi_to_jtag(info->pld_device);
189  if (retval != ERROR_OK)
190  return retval;
191  } else {
193  }
194 
195  /* passing from an IR scan to SHIFT-DR clears BYPASS registers */
196  jtag_add_dr_scan(info->tap, n, fields, TAP_IDLE);
197  int retval = jtag_execute_queue();
198  if (retval != ERROR_OK)
199  return retval;
200 
201  if (is_read)
202  flip_u8(data_buffer, data_buffer, data_len);
203 
204  if (info->pld_device)
205  return pld_disconnect_spi_from_jtag(info->pld_device);
206  return ERROR_OK;
207 }
208 
209 COMMAND_HANDLER(jtagspi_handle_set)
210 {
211  struct flash_bank *bank = NULL;
212  struct jtagspi_flash_bank *info = NULL;
213  struct flash_sector *sectors = NULL;
214  uint32_t temp;
215  unsigned int index = 1;
216  int retval;
217 
218  LOG_DEBUG("%s", __func__);
219 
220  /* there are 6 mandatory arguments:
221  * devname, size_in_bytes, pagesize, read_cmd, unused, pprog_cmd */
222  if (index + 6 > CMD_ARGC) {
223  command_print(CMD, "jtagspi: not enough arguments");
225  }
226 
227  /* calling flash_command_get_bank without probing because handle_set is used
228  to set device parameters if not autodetected. So probing would fail
229  anyhow.
230  */
231  retval = CALL_COMMAND_HANDLER(flash_command_get_bank_probe_optional, 0,
232  &bank, false);
233  if (retval != ERROR_OK)
234  return retval;
235  info = bank->driver_priv;
236 
237  /* invalidate all old info */
238  if (info->probed) {
239  bank->size = 0;
240  bank->num_sectors = 0;
241  if (bank->sectors)
242  free(bank->sectors);
243  bank->sectors = NULL;
244  info->always_4byte = false;
245  info->probed = false;
246  }
247  memset(&info->dev, 0, sizeof(info->dev));
248 
249  strncpy(info->devname, CMD_ARGV[index++], sizeof(info->devname) - 1);
250  info->devname[sizeof(info->devname) - 1] = '\0';
251 
252  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], temp);
253  info->dev.size_in_bytes = temp;
254  if ((temp & (temp - 1)) || (temp < (1UL << 8))) {
255  command_print(CMD, "jtagspi: device size must be 2^n with n >= 8");
257  }
258 
259  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], temp);
260  info->dev.pagesize = temp;
261  if (info->dev.pagesize == 0)
262  info->dev.pagesize = SPIFLASH_DEF_PAGESIZE;
263  if ((temp & (temp - 1)) || (temp > info->dev.size_in_bytes)) {
264  command_print(CMD, "jtagspi: page size must be 2^n and <= device size");
266  }
267 
268  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.read_cmd);
269  if ((info->dev.read_cmd != 0x03) &&
270  (info->dev.read_cmd != 0x13)) {
271  command_print(CMD, "jtagspi: only 0x03/0x13 READ allowed");
273  }
274 
275  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.qread_cmd);
276 
277  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.pprog_cmd);
278  if ((info->dev.pprog_cmd != 0x02) &&
279  (info->dev.pprog_cmd != 0x12)) {
280  command_print(CMD, "jtagspi: only 0x02/0x12 PPRG allowed");
282  }
283 
284  /* remaining params are optional */
285  if (index < CMD_ARGC)
286  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.chip_erase_cmd);
287  else
288  info->dev.chip_erase_cmd = 0x00;
289 
290  if (index < CMD_ARGC) {
291  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], temp);
292  info->dev.sectorsize = temp;
293  if ((info->dev.sectorsize > info->dev.size_in_bytes) ||
294  (info->dev.sectorsize < info->dev.pagesize) || (temp & (temp - 1))) {
295  command_print(CMD, "jtagspi: sector size must be 2^n and <= device size");
297  }
298 
299  if (index < CMD_ARGC)
300  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.erase_cmd);
301  else {
302  command_print(CMD, "jtagspi: erase command missing");
304  }
305  } else {
306  /* no sector size / sector erase cmd given, treat whole bank as a single sector */
307  info->dev.erase_cmd = 0x00;
308  info->dev.sectorsize = info->dev.size_in_bytes;
309  }
310 
311  if (index < CMD_ARGC) {
312  command_print(CMD, "jtagspi: extra arguments");
314  }
315 
316  /* set correct size value */
317  bank->size = info->dev.size_in_bytes;
318 
319  /* calculate address length in bytes */
320  if (bank->size <= (1UL << 8))
321  info->addr_len = 1;
322  else if (bank->size <= (1UL << 16))
323  info->addr_len = 2;
324  else if (bank->size <= (1UL << 24))
325  info->addr_len = 3;
326  else {
327  info->addr_len = 4;
328  LOG_WARNING("4-byte addresses needed, might need extra command to enable");
329  }
330 
331  /* create and fill sectors array */
332  bank->num_sectors =
333  info->dev.size_in_bytes / info->dev.sectorsize;
334  sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
335  if (!sectors) {
336  LOG_ERROR("Not enough memory");
337  return ERROR_FAIL;
338  }
339 
340  for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
341  sectors[sector].offset = sector * (info->dev.sectorsize);
342  sectors[sector].size = info->dev.sectorsize;
343  sectors[sector].is_erased = -1;
344  sectors[sector].is_protected = 0;
345  }
346 
347  bank->sectors = sectors;
348  info->dev.name = info->devname;
349  if (info->dev.size_in_bytes / 4096)
350  LOG_INFO("flash \'%s\' id = unknown\nflash size = %" PRIu32 " kbytes",
351  info->dev.name, info->dev.size_in_bytes / 1024);
352  else
353  LOG_INFO("flash \'%s\' id = unknown\nflash size = %" PRIu32 " bytes",
354  info->dev.name, info->dev.size_in_bytes);
355  info->probed = true;
356 
357  return ERROR_OK;
358 }
359 
360 COMMAND_HANDLER(jtagspi_handle_cmd)
361 {
362  struct flash_bank *bank;
363  const unsigned int max = 20;
364  uint8_t cmd_byte, num_read, write_buffer[max], read_buffer[1 << CHAR_BIT];
365 
366  LOG_DEBUG("%s", __func__);
367 
368  if (CMD_ARGC < 3)
370 
371  uint8_t num_write = CMD_ARGC - 3;
372  if (num_write > max) {
373  command_print(CMD, "at most %d bytes may be send", max);
375  }
376 
377  /* calling flash_command_get_bank without probing because we like to be
378  able to send commands before auto-probing occurred. For example sending
379  "release from power down" is needed before probing when flash is in
380  power down mode.
381  */
382  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_probe_optional, 0,
383  &bank, false);
384  if (retval != ERROR_OK)
385  return retval;
386 
387  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], num_read);
388  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[2], cmd_byte);
389 
390  for (unsigned int i = 0; i < num_write; i++)
391  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[i + 3], write_buffer[i]);
392 
393  /* process command */
394  retval = jtagspi_cmd(bank, cmd_byte, write_buffer, num_write, read_buffer, -num_read);
395  if (retval != ERROR_OK)
396  return retval;
397 
398  command_print_sameline(CMD, "spi: %02" PRIx8, cmd_byte);
399 
400  for (unsigned int i = 0; i < num_write; i++)
401  command_print_sameline(CMD, " %02" PRIx8, write_buffer[i]);
402 
403  command_print_sameline(CMD, " ->");
404 
405  for (unsigned int i = 0; i < num_read; i++)
406  command_print_sameline(CMD, " %02" PRIx8, read_buffer[i]);
407 
408  return ERROR_OK;
409 }
410 
411 COMMAND_HANDLER(jtagspi_handle_always_4byte)
412 {
413  struct flash_bank *bank;
415  int retval;
416 
417  LOG_DEBUG("%s", __func__);
418 
419  if ((CMD_ARGC != 1) && (CMD_ARGC != 2))
421 
422  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
423  if (retval != ERROR_OK)
424  return retval;
425 
426  jtagspi_info = bank->driver_priv;
427 
428  if (CMD_ARGC == 1)
429  command_print(CMD, jtagspi_info->always_4byte ? "on" : "off");
430  else
431  COMMAND_PARSE_BOOL(CMD_ARGV[1], jtagspi_info->always_4byte, "on", "off");
432 
433  return ERROR_OK;
434 }
435 
436 static int jtagspi_probe(struct flash_bank *bank)
437 {
438  struct jtagspi_flash_bank *info = bank->driver_priv;
439  struct flash_sector *sectors;
440  const struct flash_device *p;
441  uint8_t in_buf[3];
442  uint32_t id, sectorsize;
443 
444  if (bank->sectors) {
445  free(bank->sectors);
446  bank->sectors = NULL;
447  }
448  info->probed = false;
449 
450  jtagspi_cmd(bank, SPIFLASH_READ_ID, NULL, 0, in_buf, -3);
451  /* the table in spi.c has the manufacturer byte (first) as the lsb */
452  id = le_to_h_u24(in_buf);
453 
454  memset(&info->dev, 0, sizeof(info->dev));
455  for (p = flash_devices; p->name ; p++)
456  if (p->device_id == id) {
457  memcpy(&info->dev, p, sizeof(info->dev));
458  break;
459  }
460 
461  if (!(p->name)) {
462  LOG_ERROR("Unknown flash device (ID 0x%06" PRIx32 ")", id & 0xFFFFFF);
463  return ERROR_FAIL;
464  }
465 
466  LOG_INFO("Found flash device \'%s\' (ID 0x%06" PRIx32 ")",
467  info->dev.name, info->dev.device_id & 0xFFFFFF);
468 
469  /* Set correct size value */
470  bank->size = info->dev.size_in_bytes;
471 
472  /* calculate address length in bytes */
473  if (bank->size <= (1UL << 8))
474  info->addr_len = 1;
475  else if (bank->size <= (1UL << 16))
476  info->addr_len = 2;
477  else if (bank->size <= (1UL << 24))
478  info->addr_len = 3;
479  else {
480  info->addr_len = 4;
481  LOG_WARNING("4-byte addresses needed, might need extra command to enable");
482  }
483 
484  /* if no sectors, treat whole bank as single sector */
485  sectorsize = info->dev.sectorsize ?
486  info->dev.sectorsize : info->dev.size_in_bytes;
487 
488  /* create and fill sectors array */
489  bank->num_sectors = info->dev.size_in_bytes / sectorsize;
490  sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
491  if (!sectors) {
492  LOG_ERROR("not enough memory");
493  return ERROR_FAIL;
494  }
495 
496  for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
497  sectors[sector].offset = sector * sectorsize;
498  sectors[sector].size = sectorsize;
499  sectors[sector].is_erased = -1;
500  sectors[sector].is_protected = 0;
501  }
502 
503  bank->sectors = sectors;
504  info->probed = true;
505  return ERROR_OK;
506 }
507 
508 static int jtagspi_auto_probe(struct flash_bank *bank)
509 {
510  struct jtagspi_flash_bank *info = bank->driver_priv;
511 
512  if (info->probed)
513  return ERROR_OK;
514  return jtagspi_probe(bank);
515 }
516 
517 static int jtagspi_read_status(struct flash_bank *bank, uint32_t *status)
518 {
519  uint8_t buf;
520  int err = jtagspi_cmd(bank, SPIFLASH_READ_STATUS, NULL, 0, &buf, -1);
521  if (err == ERROR_OK) {
522  *status = buf;
523  LOG_DEBUG("status=0x%02" PRIx32, *status);
524  }
525  return err;
526 }
527 
528 static int jtagspi_wait_check_we(struct flash_bank *bank, int timeout_ms)
529 {
530  int64_t t0 = timeval_ms();
531  int64_t dt;
532  uint32_t status = (uint32_t)-1;
533 
534  do {
535  dt = timeval_ms() - t0;
536 
537  int retval = jtagspi_read_status(bank, &status);
538  if (retval != ERROR_OK)
539  return retval;
540 
541  if ((status & SPIFLASH_BSY_BIT) == 0) {
542  LOG_DEBUG("waited %" PRId64 " ms", dt);
543  break;
544  }
545  alive_sleep(1);
546  } while (dt <= timeout_ms);
547 
548  if (status & SPIFLASH_BSY_BIT) {
549  LOG_ERROR("timeout, device still busy");
550  return ERROR_FAIL;
551  }
552 
553  /* The write enable latch is cleared by the flash when a program or
554  * erase operation completes. If it is still set, the device did not
555  * execute the command (e.g. it was sent with an address length the
556  * device does not accept) or the operation failed. The status
557  * register has just been read to poll BUSY, so the check is free. */
558  if (status & SPIFLASH_WE_BIT) {
559  LOG_ERROR("flash did not execute the operation, WEL is still set");
560  /* drop the pending write enable so that the next command is not
561  * misled by a stale latch */
564  }
565 
566  return ERROR_OK;
567 }
568 
570 {
572 
573  uint32_t status = (uint32_t)-1;
574  int retval = jtagspi_read_status(bank, &status);
575  if (retval != ERROR_OK)
576  return retval;
577 
578  if ((status & SPIFLASH_WE_BIT) == 0) {
579  LOG_ERROR("Cannot enable write to flash. Status=0x%02" PRIx32, status);
580  return ERROR_FAIL;
581  }
582  return ERROR_OK;
583 }
584 
585 static int jtagspi_bulk_erase(struct flash_bank *bank)
586 {
587  struct jtagspi_flash_bank *info = bank->driver_priv;
588  int retval;
589  int64_t t0 = timeval_ms();
590 
591  if (info->dev.chip_erase_cmd == 0x00)
593 
594  retval = jtagspi_write_enable(bank);
595  if (retval != ERROR_OK)
596  return retval;
597 
598  retval = jtagspi_cmd(bank, info->dev.chip_erase_cmd, NULL, 0, NULL, 0);
599  if (retval != ERROR_OK)
600  return retval;
601 
602  retval = jtagspi_wait_check_we(bank, bank->num_sectors * JTAGSPI_MAX_TIMEOUT);
603  LOG_INFO("took %" PRId64 " ms", timeval_ms() - t0);
604  return retval;
605 }
606 
607 static uint8_t *fill_addr(uint32_t addr, unsigned int addr_len, uint8_t *buffer)
608 {
609  for (buffer += addr_len; addr_len > 0; --addr_len) {
610  *--buffer = addr;
611  addr >>= 8;
612  }
613 
614  return buffer;
615 }
616 
617 static int jtagspi_sector_erase(struct flash_bank *bank, unsigned int sector)
618 {
619  struct jtagspi_flash_bank *info = bank->driver_priv;
620  int retval;
621  uint8_t addr[sizeof(uint32_t)];
622  int64_t t0 = timeval_ms();
623 
624  retval = jtagspi_write_enable(bank);
625  if (retval != ERROR_OK)
626  return retval;
627 
628  /* ATXP032/064/128 use always 4-byte addresses except for 0x03 read */
629  unsigned int addr_len = info->always_4byte ? 4 : info->addr_len;
630 
631  retval = jtagspi_cmd(bank, info->dev.erase_cmd, fill_addr(bank->sectors[sector].offset, addr_len, addr),
632  addr_len, NULL, 0);
633  if (retval != ERROR_OK)
634  return retval;
635 
637  LOG_INFO("sector %u took %" PRId64 " ms", sector, timeval_ms() - t0);
638  return retval;
639 }
640 
641 static int jtagspi_erase(struct flash_bank *bank, unsigned int first,
642  unsigned int last)
643 {
644  struct jtagspi_flash_bank *info = bank->driver_priv;
645  int retval = ERROR_OK;
646 
647  LOG_DEBUG("erase from sector %u to sector %u", first, last);
648 
649  if ((last < first) || (last >= bank->num_sectors)) {
650  LOG_ERROR("Flash sector invalid");
652  }
653 
654  if (!(info->probed)) {
655  LOG_ERROR("Flash bank not probed");
657  }
658 
659  for (unsigned int sector = first; sector <= last; sector++) {
660  if (bank->sectors[sector].is_protected) {
661  LOG_ERROR("Flash sector %u protected", sector);
662  return ERROR_FAIL;
663  }
664  }
665 
666  if (first == 0 && last == (bank->num_sectors - 1) &&
667  info->dev.chip_erase_cmd != 0x00 &&
668  info->dev.chip_erase_cmd != info->dev.erase_cmd) {
669  LOG_DEBUG("Trying bulk erase.");
670  retval = jtagspi_bulk_erase(bank);
671  if (retval == ERROR_OK)
672  return retval;
673  else
674  LOG_WARNING("Bulk flash erase failed. Falling back to sector erase.");
675  }
676 
677  if (info->dev.erase_cmd == 0x00)
679 
680  for (unsigned int sector = first; sector <= last; sector++) {
681  retval = jtagspi_sector_erase(bank, sector);
682  if (retval != ERROR_OK) {
683  LOG_ERROR("Sector erase failed.");
684  break;
685  }
686  }
687 
688  return retval;
689 }
690 
691 static int jtagspi_protect(struct flash_bank *bank, int set, unsigned int first,
692  unsigned int last)
693 {
694  for (unsigned int sector = first; sector <= last; sector++)
695  bank->sectors[sector].is_protected = set;
696  return ERROR_OK;
697 }
698 
699 static int jtagspi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
700 {
701  struct jtagspi_flash_bank *info = bank->driver_priv;
702  uint32_t pagesize, currsize;
703  uint8_t addr[sizeof(uint32_t)];
704  int retval;
705 
706  if (!(info->probed)) {
707  LOG_ERROR("Flash bank not probed.");
709  }
710 
711  /* if no sectorsize, use reasonable default */
712  pagesize = info->dev.sectorsize ? info->dev.sectorsize : info->dev.pagesize;
713  if (pagesize == 0)
714  pagesize = (info->dev.size_in_bytes <= SPIFLASH_DEF_PAGESIZE) ?
715  info->dev.size_in_bytes : SPIFLASH_DEF_PAGESIZE;
716 
717  /* ATXP032/064/128 use always 4-byte addresses except for 0x03 read */
718  unsigned int addr_len = ((info->dev.read_cmd != 0x03) && info->always_4byte) ? 4 : info->addr_len;
719 
720  while (count > 0) {
721  /* length up to end of current page */
722  currsize = ((offset + pagesize) & ~(pagesize - 1)) - offset;
723  /* but no more than remaining size */
724  currsize = (count < currsize) ? count : currsize;
725 
726  retval = jtagspi_cmd(bank, info->dev.read_cmd, fill_addr(offset, addr_len, addr),
727  addr_len, buffer, -currsize);
728  if (retval != ERROR_OK) {
729  LOG_ERROR("page read error");
730  return retval;
731  }
732  LOG_DEBUG("read page at 0x%08" PRIx32, offset);
733  offset += currsize;
734  buffer += currsize;
735  count -= currsize;
736  }
737  return ERROR_OK;
738 }
739 
740 static int jtagspi_page_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
741 {
742  struct jtagspi_flash_bank *info = bank->driver_priv;
743  uint8_t addr[sizeof(uint32_t)];
744  int retval;
745 
746  retval = jtagspi_write_enable(bank);
747  if (retval != ERROR_OK)
748  return retval;
749 
750  /* ATXP032/064/128 use always 4-byte addresses except for 0x03 read */
751  unsigned int addr_len = ((info->dev.read_cmd != 0x03) && info->always_4byte) ? 4 : info->addr_len;
752 
753  retval = jtagspi_cmd(bank, info->dev.pprog_cmd, fill_addr(offset, addr_len, addr),
754  addr_len, (uint8_t *) buffer, count);
755  if (retval != ERROR_OK)
756  return retval;
758 }
759 
760 static int jtagspi_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
761 {
762  struct jtagspi_flash_bank *info = bank->driver_priv;
763  uint32_t pagesize, currsize;
764  int retval;
765 
766  if (!(info->probed)) {
767  LOG_ERROR("Flash bank not probed.");
769  }
770 
771  /* if no write pagesize, use reasonable default */
772  pagesize = info->dev.pagesize ? info->dev.pagesize : SPIFLASH_DEF_PAGESIZE;
773 
774  while (count > 0) {
775  /* length up to end of current page */
776  currsize = ((offset + pagesize) & ~(pagesize - 1)) - offset;
777  /* but no more than remaining size */
778  currsize = (count < currsize) ? count : currsize;
779 
780  retval = jtagspi_page_write(bank, buffer, offset, currsize);
781  if (retval != ERROR_OK) {
782  LOG_ERROR("page write error");
783  return retval;
784  }
785  LOG_DEBUG("wrote page at 0x%08" PRIx32, offset);
786  offset += currsize;
787  buffer += currsize;
788  count -= currsize;
789  }
790  return ERROR_OK;
791 }
792 
793 static int jtagspi_info(struct flash_bank *bank, struct command_invocation *cmd)
794 {
795  struct jtagspi_flash_bank *info = bank->driver_priv;
796 
797  if (!(info->probed)) {
798  command_print_sameline(cmd, "\nJTAGSPI flash bank not probed yet\n");
799  return ERROR_OK;
800  }
801 
802  command_print_sameline(cmd, "flash \'%s\', device id = 0x%06" PRIx32
803  ", flash size = %" PRIu32 " %sbytes\n(page size = %" PRIu32
804  ", read = 0x%02" PRIx8 ", qread = 0x%02" PRIx8
805  ", pprog = 0x%02" PRIx8 ", mass_erase = 0x%02" PRIx8
806  ", sector size = %" PRIu32 " %sbytes, sector_erase = 0x%02" PRIx8 ")",
807  info->dev.name, info->dev.device_id & 0xFFFFFF,
808  bank->size / 4096 ? bank->size / 1024 : bank->size,
809  bank->size / 4096 ? "k" : "", info->dev.pagesize,
810  info->dev.read_cmd, info->dev.qread_cmd,
811  info->dev.pprog_cmd, info->dev.chip_erase_cmd,
812  info->dev.sectorsize / 4096 ?
813  info->dev.sectorsize / 1024 : info->dev.sectorsize,
814  info->dev.sectorsize / 4096 ? "k" : "",
815  info->dev.erase_cmd);
816 
817  return ERROR_OK;
818 }
819 
820 static const struct command_registration jtagspi_exec_command_handlers[] = {
821  {
822  .name = "set",
823  .handler = jtagspi_handle_set,
824  .mode = COMMAND_EXEC,
825  .usage = "bank_id name chip_size page_size read_cmd unused pprg_cmd "
826  "[ mass_erase_cmd ] [ sector_size sector_erase_cmd ]",
827  .help = "Set device parameters if not autodetected.",
828  },
829  {
830  .name = "cmd",
831  .handler = jtagspi_handle_cmd,
832  .mode = COMMAND_EXEC,
833  .usage = "bank_id num_resp cmd_byte ...",
834  .help = "Send low-level command cmd_byte and following bytes, read num_bytes.",
835  },
836  {
837  .name = "always_4byte",
838  .handler = jtagspi_handle_always_4byte,
839  .mode = COMMAND_EXEC,
840  .usage = "bank_id [ on | off ]",
841  .help = "Use always 4-byte address except for basic 0x03.",
842  },
843 
845 };
846 
847 static const struct command_registration jtagspi_command_handlers[] = {
848  {
849  .name = "jtagspi",
850  .mode = COMMAND_ANY,
851  .help = "jtagspi command group",
852  .usage = "",
854  },
856 };
857 
858 const struct flash_driver jtagspi_flash = {
859  .name = "jtagspi",
860  .commands = jtagspi_command_handlers,
861  .flash_bank_command = jtagspi_flash_bank_command,
862  .erase = jtagspi_erase,
863  .protect = jtagspi_protect,
864  .write = jtagspi_write,
865  .read = jtagspi_read,
866  .probe = jtagspi_probe,
867  .auto_probe = jtagspi_auto_probe,
868  .erase_check = default_flash_blank_check,
869  .info = jtagspi_info,
870  .free_driver_priv = default_flash_free_driver_priv,
871 };
static const struct device_t * device
Definition: at91rm9200.c:94
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
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:378
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_BOOL(in, out, on, off)
Parse the string as a binary parameter, storing the boolean value in out.
Definition: command.h:506
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint8_t bank
Definition: esirisc.c:135
#define ERROR_FLASH_OPER_UNSUPPORTED
Definition: flash/common.h:36
#define ERROR_FLASH_SECTOR_INVALID
Definition: flash/common.h:29
#define ERROR_FLASH_BANK_NOT_PROBED
Definition: flash/common.h:35
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
unsigned int jtag_tap_count_enabled(void)
Definition: jtag/core.c:201
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1033
void jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, enum tap_state state)
Generate a DR SCAN using the fields passed to the function.
Definition: jtag/core.c:438
void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, enum tap_state state)
Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
Definition: jtag/core.c:361
The JTAG interface can be implemented with a software or hardware fifo.
@ TAP_IDLE
Definition: jtag.h:53
static int jtagspi_auto_probe(struct flash_bank *bank)
Definition: jtagspi.c:508
static int jtagspi_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: jtagspi.c:691
#define JTAGSPI_MAX_TIMEOUT
Definition: jtagspi.c:17
static int jtagspi_write_enable(struct flash_bank *bank)
Definition: jtagspi.c:569
static void flip_u8(const uint8_t *in, uint8_t *out, unsigned int len)
Definition: jtagspi.c:96
static const struct command_registration jtagspi_command_handlers[]
Definition: jtagspi.c:847
static int jtagspi_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: jtagspi.c:641
static int jtagspi_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: jtagspi.c:760
const struct flash_driver jtagspi_flash
Definition: jtagspi.c:858
FLASH_BANK_COMMAND_HANDLER(jtagspi_flash_bank_command)
Definition: jtagspi.c:32
static int jtagspi_sector_erase(struct flash_bank *bank, unsigned int sector)
Definition: jtagspi.c:617
static int jtagspi_wait_check_we(struct flash_bank *bank, int timeout_ms)
Definition: jtagspi.c:528
static uint8_t * fill_addr(uint32_t addr, unsigned int addr_len, uint8_t *buffer)
Definition: jtagspi.c:607
static int jtagspi_page_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: jtagspi.c:740
COMMAND_HANDLER(jtagspi_handle_set)
Definition: jtagspi.c:209
static const struct command_registration jtagspi_exec_command_handlers[]
Definition: jtagspi.c:820
static void jtagspi_set_user_ir(struct jtagspi_flash_bank *info)
Definition: jtagspi.c:83
static int jtagspi_cmd(struct flash_bank *bank, uint8_t cmd, uint8_t *write_buffer, unsigned int write_len, uint8_t *data_buffer, int data_len)
Definition: jtagspi.c:102
static int jtagspi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: jtagspi.c:699
static int jtagspi_probe(struct flash_bank *bank)
Definition: jtagspi.c:436
static int jtagspi_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: jtagspi.c:793
static int jtagspi_read_status(struct flash_bank *bank, uint32_t *status)
Definition: jtagspi.c:517
static int jtagspi_bulk_erase(struct flash_bank *bank)
Definition: jtagspi.c:585
void alive_sleep(uint64_t ms)
Definition: log.c:478
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
#define t0
Definition: mips32.c:192
uint32_t id
Definition: nrf54.c:43
int pld_has_jtagspi_instruction(struct pld_device *pld_device, bool *has_instruction)
Definition: pld.c:73
struct pld_device * get_pld_device_by_name_or_numstr(const char *str)
Definition: pld.c:56
int pld_get_jtagspi_stuff_bits(struct pld_device *pld_device, unsigned int *facing_read_bits, unsigned int *trailing_write_bits)
Definition: pld.c:109
int pld_get_jtagspi_userircode(struct pld_device *pld_device, unsigned int *ir)
Definition: pld.c:92
int pld_connect_spi_to_jtag(struct pld_device *pld_device)
Definition: pld.c:127
int pld_disconnect_spi_from_jtag(struct pld_device *pld_device)
Definition: pld.c:144
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
const struct flash_device flash_devices[]
Definition: spi.c:24
#define SPIFLASH_READ_STATUS
Definition: spi.h:77
#define SPIFLASH_WRITE_DISABLE
Definition: spi.h:79
#define SPIFLASH_READ_ID
Definition: spi.h:75
#define SPIFLASH_WRITE_ENABLE
Definition: spi.h:78
#define SPIFLASH_WE_BIT
Definition: spi.h:72
#define SPIFLASH_DEF_PAGESIZE
Definition: spi.h:86
#define SPIFLASH_BSY_BIT
Definition: spi.h:70
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:239
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
uint32_t sectorsize
Definition: spi.h:32
uint32_t device_id
Definition: spi.h:30
const char * name
Definition: spi.h:21
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
int is_erased
Indication of erasure status: 0 = not erased, 1 = erased, other = unknown.
Definition: nor/core.h:42
uint32_t offset
Bus offset from start of the flash chip (in bytes).
Definition: nor/core.h:30
int is_protected
Indication of protection status: 0 = unprotected/unlocked, 1 = protected/locked, other = unknown.
Definition: nor/core.h:55
uint32_t size
Number of bytes in this flash sector.
Definition: nor/core.h:32
Definition: jtag.h:101
unsigned int addr_len
Definition: jtagspi.c:26
uint32_t ir
Definition: jtagspi.c:28
struct jtag_tap * tap
Definition: jtagspi.c:21
char devname[32]
Definition: jtagspi.c:23
struct flash_device dev
Definition: jtagspi.c:22
struct pld_device * pld_device
Definition: jtagspi.c:27
Definition: pld.h:48
This structure defines a single scan field in the scan.
Definition: jtag.h:87
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
unsigned int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
int64_t timeval_ms(void)
static void h_u32_to_be(uint8_t *buf, uint32_t val)
Definition: types.h:186
static uint32_t le_to_h_u24(const uint8_t *buf)
Definition: types.h:117
static struct ublast_lowlevel_priv info
#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