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 (ERROR_OK != retval)
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 (ERROR_OK != retval)
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(struct flash_bank *bank, int timeout_ms)
529 {
530  int64_t t0 = timeval_ms();
531  int64_t dt;
532 
533  do {
534  dt = timeval_ms() - t0;
535 
536  uint32_t status = (uint32_t)-1;
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  return ERROR_OK;
544  }
545  alive_sleep(1);
546  } while (dt <= timeout_ms);
547 
548  LOG_ERROR("timeout, device still busy");
549  return ERROR_FAIL;
550 }
551 
553 {
555 
556  uint32_t status = (uint32_t)-1;
557  int retval = jtagspi_read_status(bank, &status);
558  if (retval != ERROR_OK)
559  return retval;
560 
561  if ((status & SPIFLASH_WE_BIT) == 0) {
562  LOG_ERROR("Cannot enable write to flash. Status=0x%02" PRIx32, status);
563  return ERROR_FAIL;
564  }
565  return ERROR_OK;
566 }
567 
568 static int jtagspi_bulk_erase(struct flash_bank *bank)
569 {
570  struct jtagspi_flash_bank *info = bank->driver_priv;
571  int retval;
572  int64_t t0 = timeval_ms();
573 
574  if (info->dev.chip_erase_cmd == 0x00)
576 
577  retval = jtagspi_write_enable(bank);
578  if (retval != ERROR_OK)
579  return retval;
580 
581  retval = jtagspi_cmd(bank, info->dev.chip_erase_cmd, NULL, 0, NULL, 0);
582  if (retval != ERROR_OK)
583  return retval;
584 
585  retval = jtagspi_wait(bank, bank->num_sectors * JTAGSPI_MAX_TIMEOUT);
586  LOG_INFO("took %" PRId64 " ms", timeval_ms() - t0);
587  return retval;
588 }
589 
590 static uint8_t *fill_addr(uint32_t addr, unsigned int addr_len, uint8_t *buffer)
591 {
592  for (buffer += addr_len; addr_len > 0; --addr_len) {
593  *--buffer = addr;
594  addr >>= 8;
595  }
596 
597  return buffer;
598 }
599 
600 static int jtagspi_sector_erase(struct flash_bank *bank, unsigned int sector)
601 {
602  struct jtagspi_flash_bank *info = bank->driver_priv;
603  int retval;
604  uint8_t addr[sizeof(uint32_t)];
605  int64_t t0 = timeval_ms();
606 
607  retval = jtagspi_write_enable(bank);
608  if (retval != ERROR_OK)
609  return retval;
610 
611  /* ATXP032/064/128 use always 4-byte addresses except for 0x03 read */
612  unsigned int addr_len = info->always_4byte ? 4 : info->addr_len;
613 
614  retval = jtagspi_cmd(bank, info->dev.erase_cmd, fill_addr(bank->sectors[sector].offset, addr_len, addr),
615  addr_len, NULL, 0);
616  if (retval != ERROR_OK)
617  return retval;
618 
620  LOG_INFO("sector %u took %" PRId64 " ms", sector, timeval_ms() - t0);
621  return retval;
622 }
623 
624 static int jtagspi_erase(struct flash_bank *bank, unsigned int first,
625  unsigned int last)
626 {
627  struct jtagspi_flash_bank *info = bank->driver_priv;
628  int retval = ERROR_OK;
629 
630  LOG_DEBUG("erase from sector %u to sector %u", first, last);
631 
632  if ((last < first) || (last >= bank->num_sectors)) {
633  LOG_ERROR("Flash sector invalid");
635  }
636 
637  if (!(info->probed)) {
638  LOG_ERROR("Flash bank not probed");
640  }
641 
642  for (unsigned int sector = first; sector <= last; sector++) {
643  if (bank->sectors[sector].is_protected) {
644  LOG_ERROR("Flash sector %u protected", sector);
645  return ERROR_FAIL;
646  }
647  }
648 
649  if (first == 0 && last == (bank->num_sectors - 1) &&
650  info->dev.chip_erase_cmd != 0x00 &&
651  info->dev.chip_erase_cmd != info->dev.erase_cmd) {
652  LOG_DEBUG("Trying bulk erase.");
653  retval = jtagspi_bulk_erase(bank);
654  if (retval == ERROR_OK)
655  return retval;
656  else
657  LOG_WARNING("Bulk flash erase failed. Falling back to sector erase.");
658  }
659 
660  if (info->dev.erase_cmd == 0x00)
662 
663  for (unsigned int sector = first; sector <= last; sector++) {
664  retval = jtagspi_sector_erase(bank, sector);
665  if (retval != ERROR_OK) {
666  LOG_ERROR("Sector erase failed.");
667  break;
668  }
669  }
670 
671  return retval;
672 }
673 
674 static int jtagspi_protect(struct flash_bank *bank, int set, unsigned int first,
675  unsigned int last)
676 {
677  for (unsigned int sector = first; sector <= last; sector++)
678  bank->sectors[sector].is_protected = set;
679  return ERROR_OK;
680 }
681 
682 static int jtagspi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
683 {
684  struct jtagspi_flash_bank *info = bank->driver_priv;
685  uint32_t pagesize, currsize;
686  uint8_t addr[sizeof(uint32_t)];
687  int retval;
688 
689  if (!(info->probed)) {
690  LOG_ERROR("Flash bank not probed.");
692  }
693 
694  /* if no sectorsize, use reasonable default */
695  pagesize = info->dev.sectorsize ? info->dev.sectorsize : info->dev.pagesize;
696  if (pagesize == 0)
697  pagesize = (info->dev.size_in_bytes <= SPIFLASH_DEF_PAGESIZE) ?
698  info->dev.size_in_bytes : SPIFLASH_DEF_PAGESIZE;
699 
700  /* ATXP032/064/128 use always 4-byte addresses except for 0x03 read */
701  unsigned int addr_len = ((info->dev.read_cmd != 0x03) && info->always_4byte) ? 4 : info->addr_len;
702 
703  while (count > 0) {
704  /* length up to end of current page */
705  currsize = ((offset + pagesize) & ~(pagesize - 1)) - offset;
706  /* but no more than remaining size */
707  currsize = (count < currsize) ? count : currsize;
708 
709  retval = jtagspi_cmd(bank, info->dev.read_cmd, fill_addr(offset, addr_len, addr),
710  addr_len, buffer, -currsize);
711  if (retval != ERROR_OK) {
712  LOG_ERROR("page read error");
713  return retval;
714  }
715  LOG_DEBUG("read page at 0x%08" PRIx32, offset);
716  offset += currsize;
717  buffer += currsize;
718  count -= currsize;
719  }
720  return ERROR_OK;
721 }
722 
723 static int jtagspi_page_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
724 {
725  struct jtagspi_flash_bank *info = bank->driver_priv;
726  uint8_t addr[sizeof(uint32_t)];
727  int retval;
728 
729  retval = jtagspi_write_enable(bank);
730  if (retval != ERROR_OK)
731  return retval;
732 
733  /* ATXP032/064/128 use always 4-byte addresses except for 0x03 read */
734  unsigned int addr_len = ((info->dev.read_cmd != 0x03) && info->always_4byte) ? 4 : info->addr_len;
735 
736  retval = jtagspi_cmd(bank, info->dev.pprog_cmd, fill_addr(offset, addr_len, addr),
737  addr_len, (uint8_t *) buffer, count);
738  if (retval != ERROR_OK)
739  return retval;
741 }
742 
743 static int jtagspi_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
744 {
745  struct jtagspi_flash_bank *info = bank->driver_priv;
746  uint32_t pagesize, currsize;
747  int retval;
748 
749  if (!(info->probed)) {
750  LOG_ERROR("Flash bank not probed.");
752  }
753 
754  /* if no write pagesize, use reasonable default */
755  pagesize = info->dev.pagesize ? info->dev.pagesize : SPIFLASH_DEF_PAGESIZE;
756 
757  while (count > 0) {
758  /* length up to end of current page */
759  currsize = ((offset + pagesize) & ~(pagesize - 1)) - offset;
760  /* but no more than remaining size */
761  currsize = (count < currsize) ? count : currsize;
762 
763  retval = jtagspi_page_write(bank, buffer, offset, currsize);
764  if (retval != ERROR_OK) {
765  LOG_ERROR("page write error");
766  return retval;
767  }
768  LOG_DEBUG("wrote page at 0x%08" PRIx32, offset);
769  offset += currsize;
770  buffer += currsize;
771  count -= currsize;
772  }
773  return ERROR_OK;
774 }
775 
776 static int jtagspi_info(struct flash_bank *bank, struct command_invocation *cmd)
777 {
778  struct jtagspi_flash_bank *info = bank->driver_priv;
779 
780  if (!(info->probed)) {
781  command_print_sameline(cmd, "\nJTAGSPI flash bank not probed yet\n");
782  return ERROR_OK;
783  }
784 
785  command_print_sameline(cmd, "flash \'%s\', device id = 0x%06" PRIx32
786  ", flash size = %" PRIu32 " %sbytes\n(page size = %" PRIu32
787  ", read = 0x%02" PRIx8 ", qread = 0x%02" PRIx8
788  ", pprog = 0x%02" PRIx8 ", mass_erase = 0x%02" PRIx8
789  ", sector size = %" PRIu32 " %sbytes, sector_erase = 0x%02" PRIx8 ")",
790  info->dev.name, info->dev.device_id & 0xFFFFFF,
791  bank->size / 4096 ? bank->size / 1024 : bank->size,
792  bank->size / 4096 ? "k" : "", info->dev.pagesize,
793  info->dev.read_cmd, info->dev.qread_cmd,
794  info->dev.pprog_cmd, info->dev.chip_erase_cmd,
795  info->dev.sectorsize / 4096 ?
796  info->dev.sectorsize / 1024 : info->dev.sectorsize,
797  info->dev.sectorsize / 4096 ? "k" : "",
798  info->dev.erase_cmd);
799 
800  return ERROR_OK;
801 }
802 
803 static const struct command_registration jtagspi_exec_command_handlers[] = {
804  {
805  .name = "set",
806  .handler = jtagspi_handle_set,
807  .mode = COMMAND_EXEC,
808  .usage = "bank_id name chip_size page_size read_cmd unused pprg_cmd "
809  "[ mass_erase_cmd ] [ sector_size sector_erase_cmd ]",
810  .help = "Set device parameters if not autodetected.",
811  },
812  {
813  .name = "cmd",
814  .handler = jtagspi_handle_cmd,
815  .mode = COMMAND_EXEC,
816  .usage = "bank_id num_resp cmd_byte ...",
817  .help = "Send low-level command cmd_byte and following bytes, read num_bytes.",
818  },
819  {
820  .name = "always_4byte",
821  .handler = jtagspi_handle_always_4byte,
822  .mode = COMMAND_EXEC,
823  .usage = "bank_id [ on | off ]",
824  .help = "Use always 4-byte address except for basic 0x03.",
825  },
826 
828 };
829 
830 static const struct command_registration jtagspi_command_handlers[] = {
831  {
832  .name = "jtagspi",
833  .mode = COMMAND_ANY,
834  .help = "jtagspi command group",
835  .usage = "",
837  },
839 };
840 
841 const struct flash_driver jtagspi_flash = {
842  .name = "jtagspi",
843  .commands = jtagspi_command_handlers,
844  .flash_bank_command = jtagspi_flash_bank_command,
845  .erase = jtagspi_erase,
846  .protect = jtagspi_protect,
847  .write = jtagspi_write,
848  .read = jtagspi_read,
849  .probe = jtagspi_probe,
850  .auto_probe = jtagspi_auto_probe,
851  .erase_check = default_flash_blank_check,
852  .info = jtagspi_info,
853  .free_driver_priv = default_flash_free_driver_priv,
854 };
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:420
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:443
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#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_PARSE_BOOL(in, out, on, off)
Parse the string as a binary parameter, storing the boolean value in out.
Definition: command.h:503
#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:442
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:404
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
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
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:206
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1043
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:380
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:457
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:674
#define JTAGSPI_MAX_TIMEOUT
Definition: jtagspi.c:17
static int jtagspi_write_enable(struct flash_bank *bank)
Definition: jtagspi.c:552
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:830
static int jtagspi_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: jtagspi.c:624
static int jtagspi_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: jtagspi.c:743
const struct flash_driver jtagspi_flash
Definition: jtagspi.c:841
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:600
static int jtagspi_wait(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:590
static int jtagspi_page_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: jtagspi.c:723
COMMAND_HANDLER(jtagspi_handle_set)
Definition: jtagspi.c:209
static const struct command_registration jtagspi_exec_command_handlers[]
Definition: jtagspi.c:803
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:682
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:776
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:568
void alive_sleep(uint64_t ms)
Definition: log.c:456
#define LOG_WARNING(expr ...)
Definition: log.h:129
#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
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
char id[RTT_CB_MAX_ID_LENGTH]
Control block identifier.
Definition: rtt/rtt.c:32
const struct flash_device flash_devices[]
Definition: spi.c:24
#define SPIFLASH_READ_STATUS
Definition: spi.h:74
#define SPIFLASH_READ_ID
Definition: spi.h:72
#define SPIFLASH_WRITE_ENABLE
Definition: spi.h:75
#define SPIFLASH_WE_BIT
Definition: spi.h:69
#define SPIFLASH_DEF_PAGESIZE
Definition: spi.h:82
#define SPIFLASH_BSY_BIT
Definition: spi.h:67
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
uint32_t sectorsize
Definition: spi.h:29
uint32_t device_id
Definition: spi.h:27
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