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 
16 #define JTAGSPI_MAX_TIMEOUT 3000
17 
18 
20  struct jtag_tap *tap;
21  struct flash_device dev;
22  char devname[32];
23  bool probed;
24  bool always_4byte; /* use always 4-byte address except for basic read 0x03 */
25  uint32_t ir;
26  unsigned int addr_len; /* address length in bytes */
27 };
28 
29 FLASH_BANK_COMMAND_HANDLER(jtagspi_flash_bank_command)
30 {
31  struct jtagspi_flash_bank *info;
32 
33  if (CMD_ARGC < 7)
35 
36  info = malloc(sizeof(struct jtagspi_flash_bank));
37  if (!info) {
38  LOG_ERROR("no memory for flash bank info");
39  return ERROR_FAIL;
40  }
41  bank->sectors = NULL;
42  bank->driver_priv = info;
43 
44  info->tap = NULL;
45  info->probed = false;
46  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], info->ir);
47 
48  return ERROR_OK;
49 }
50 
51 static void jtagspi_set_ir(struct flash_bank *bank)
52 {
53  struct jtagspi_flash_bank *info = bank->driver_priv;
54  struct scan_field field;
55  uint8_t buf[4] = { 0 };
56 
57  LOG_DEBUG("loading jtagspi ir");
58  buf_set_u32(buf, 0, info->tap->ir_length, info->ir);
59  field.num_bits = info->tap->ir_length;
60  field.out_value = buf;
61  field.in_value = NULL;
62  jtag_add_ir_scan(info->tap, &field, TAP_IDLE);
63 }
64 
65 static void flip_u8(const uint8_t *in, uint8_t *out, unsigned int len)
66 {
67  for (unsigned int i = 0; i < len; i++)
68  out[i] = flip_u32(in[i], 8);
69 }
70 
71 static int jtagspi_cmd(struct flash_bank *bank, uint8_t cmd,
72  uint8_t *write_buffer, unsigned int write_len, uint8_t *data_buffer, int data_len)
73 {
74  assert(write_buffer || write_len == 0);
75  assert(data_buffer || data_len == 0);
76 
77  struct scan_field fields[6];
78 
79  LOG_DEBUG("cmd=0x%02x write_len=%d data_len=%d", cmd, write_len, data_len);
80 
81  /* negative data_len == read operation */
82  const bool is_read = (data_len < 0);
83  if (is_read)
84  data_len = -data_len;
85 
86  int n = 0;
87  const uint8_t marker = 1;
88  fields[n].num_bits = 1;
89  fields[n].out_value = &marker;
90  fields[n].in_value = NULL;
91  n++;
92 
93  /* transfer length = cmd + address + read/write,
94  * -1 due to the counter implementation */
95  uint8_t xfer_bits[4];
96  h_u32_to_be(xfer_bits, ((sizeof(cmd) + write_len + data_len) * CHAR_BIT) - 1);
97  flip_u8(xfer_bits, xfer_bits, sizeof(xfer_bits));
98  fields[n].num_bits = sizeof(xfer_bits) * CHAR_BIT;
99  fields[n].out_value = xfer_bits;
100  fields[n].in_value = NULL;
101  n++;
102 
103  flip_u8(&cmd, &cmd, sizeof(cmd));
104  fields[n].num_bits = sizeof(cmd) * CHAR_BIT;
105  fields[n].out_value = &cmd;
106  fields[n].in_value = NULL;
107  n++;
108 
109  if (write_len) {
110  flip_u8(write_buffer, write_buffer, write_len);
111  fields[n].num_bits = write_len * CHAR_BIT;
112  fields[n].out_value = write_buffer;
113  fields[n].in_value = NULL;
114  n++;
115  }
116 
117  if (data_len > 0) {
118  if (is_read) {
119  fields[n].num_bits = jtag_tap_count_enabled();
120  fields[n].out_value = NULL;
121  fields[n].in_value = NULL;
122  n++;
123 
124  fields[n].out_value = NULL;
125  fields[n].in_value = data_buffer;
126  } else {
127  flip_u8(data_buffer, data_buffer, data_len);
128  fields[n].out_value = data_buffer;
129  fields[n].in_value = NULL;
130  }
131  fields[n].num_bits = data_len * CHAR_BIT;
132  n++;
133  }
134 
136  /* passing from an IR scan to SHIFT-DR clears BYPASS registers */
137  struct jtagspi_flash_bank *info = bank->driver_priv;
138  jtag_add_dr_scan(info->tap, n, fields, TAP_IDLE);
139  int retval = jtag_execute_queue();
140 
141  if (is_read)
142  flip_u8(data_buffer, data_buffer, data_len);
143  return retval;
144 }
145 
146 COMMAND_HANDLER(jtagspi_handle_set)
147 {
148  struct flash_bank *bank = NULL;
149  struct jtagspi_flash_bank *info = NULL;
150  struct flash_sector *sectors = NULL;
151  uint32_t temp;
152  unsigned int index = 1;
153  int retval;
154 
155  LOG_DEBUG("%s", __func__);
156 
157  /* there are 6 mandatory arguments:
158  * devname, size_in_bytes, pagesize, read_cmd, unused, pprog_cmd */
159  if (index + 6 > CMD_ARGC) {
160  command_print(CMD, "jtagspi: not enough arguments");
162  }
163 
164  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
165  if (ERROR_OK != retval)
166  return retval;
167  info = bank->driver_priv;
168 
169  /* invalidate all old info */
170  if (info->probed) {
171  bank->size = 0;
172  bank->num_sectors = 0;
173  if (bank->sectors)
174  free(bank->sectors);
175  bank->sectors = NULL;
176  info->always_4byte = false;
177  info->probed = false;
178  }
179  memset(&info->dev, 0, sizeof(info->dev));
180 
181  strncpy(info->devname, CMD_ARGV[index++], sizeof(info->devname) - 1);
182  info->devname[sizeof(info->devname) - 1] = '\0';
183 
184  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], temp);
185  info->dev.size_in_bytes = temp;
186  if ((temp & (temp - 1)) || (temp < (1UL << 8))) {
187  command_print(CMD, "jtagspi: device size must be 2^n with n >= 8");
189  }
190 
191  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], temp);
192  info->dev.pagesize = temp;
193  if (info->dev.pagesize == 0)
194  info->dev.pagesize = SPIFLASH_DEF_PAGESIZE;
195  if ((temp & (temp - 1)) || (temp > info->dev.size_in_bytes)) {
196  command_print(CMD, "jtagspi: page size must be 2^n and <= device size");
198  }
199 
200  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.read_cmd);
201  if ((info->dev.read_cmd != 0x03) &&
202  (info->dev.read_cmd != 0x13)) {
203  command_print(CMD, "jtagspi: only 0x03/0x13 READ allowed");
205  }
206 
207  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.qread_cmd);
208 
209  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.pprog_cmd);
210  if ((info->dev.pprog_cmd != 0x02) &&
211  (info->dev.pprog_cmd != 0x12)) {
212  command_print(CMD, "jtagspi: only 0x02/0x12 PPRG allowed");
214  }
215 
216  /* remaining params are optional */
217  if (index < CMD_ARGC)
218  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.chip_erase_cmd);
219  else
220  info->dev.chip_erase_cmd = 0x00;
221 
222  if (index < CMD_ARGC) {
223  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], temp);
224  info->dev.sectorsize = temp;
225  if ((info->dev.sectorsize > info->dev.size_in_bytes) ||
226  (info->dev.sectorsize < info->dev.pagesize) || (temp & (temp - 1))) {
227  command_print(CMD, "jtagspi: sector size must be 2^n and <= device size");
229  }
230 
231  if (index < CMD_ARGC)
232  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.erase_cmd);
233  else {
234  command_print(CMD, "jtagspi: erase command missing");
236  }
237  } else {
238  /* no sector size / sector erase cmd given, treat whole bank as a single sector */
239  info->dev.erase_cmd = 0x00;
240  info->dev.sectorsize = info->dev.size_in_bytes;
241  }
242 
243  if (index < CMD_ARGC) {
244  command_print(CMD, "jtagspi: extra arguments");
246  }
247 
248  /* set correct size value */
249  bank->size = info->dev.size_in_bytes;
250 
251  /* calculate address length in bytes */
252  if (bank->size <= (1UL << 8))
253  info->addr_len = 1;
254  else if (bank->size <= (1UL << 16))
255  info->addr_len = 2;
256  else if (bank->size <= (1UL << 24))
257  info->addr_len = 3;
258  else {
259  info->addr_len = 4;
260  LOG_WARNING("4-byte addresses needed, might need extra command to enable");
261  }
262 
263  /* create and fill sectors array */
264  bank->num_sectors =
265  info->dev.size_in_bytes / info->dev.sectorsize;
266  sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
267  if (!sectors) {
268  LOG_ERROR("Not enough memory");
269  return ERROR_FAIL;
270  }
271 
272  for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
273  sectors[sector].offset = sector * (info->dev.sectorsize);
274  sectors[sector].size = info->dev.sectorsize;
275  sectors[sector].is_erased = -1;
276  sectors[sector].is_protected = 0;
277  }
278 
279  bank->sectors = sectors;
280  info->dev.name = info->devname;
281  if (info->dev.size_in_bytes / 4096)
282  LOG_INFO("flash \'%s\' id = unknown\nflash size = %" PRIu32 " kbytes",
283  info->dev.name, info->dev.size_in_bytes / 1024);
284  else
285  LOG_INFO("flash \'%s\' id = unknown\nflash size = %" PRIu32 " bytes",
286  info->dev.name, info->dev.size_in_bytes);
287  info->probed = true;
288 
289  return ERROR_OK;
290 }
291 
292 COMMAND_HANDLER(jtagspi_handle_cmd)
293 {
294  struct flash_bank *bank;
295  unsigned int index = 1;
296  const int max = 21;
297  uint8_t num_write, num_read, write_buffer[max], read_buffer[1 << CHAR_BIT];
298  uint8_t data, *ptr;
299  char temp[4], output[(2 + max + (1 << CHAR_BIT)) * 3 + 8];
300  int retval;
301 
302  LOG_DEBUG("%s", __func__);
303 
304  if (CMD_ARGC < 3) {
305  command_print(CMD, "jtagspi: not enough arguments");
307  }
308 
309  num_write = CMD_ARGC - 2;
310  if (num_write > max) {
311  LOG_ERROR("at most %d bytes may be send", max);
313  }
314 
315  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
316  if (ERROR_OK != retval)
317  return retval;
318 
319  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], num_read);
320 
321  snprintf(output, sizeof(output), "spi: ");
322  for (ptr = &write_buffer[0] ; index < CMD_ARGC; index++) {
323  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index], data);
324  *ptr++ = data;
325  snprintf(temp, sizeof(temp), "%02" PRIx8 " ", data);
326  strncat(output, temp, sizeof(output) - strlen(output) - 1);
327  }
328  strncat(output, "-> ", sizeof(output) - strlen(output) - 1);
329 
330  /* process command */
331  ptr = &read_buffer[0];
332  jtagspi_cmd(bank, write_buffer[0], &write_buffer[1], num_write - 1, ptr, -num_read);
333  if (retval != ERROR_OK)
334  return retval;
335 
336  for ( ; num_read > 0; num_read--) {
337  snprintf(temp, sizeof(temp), "%02" PRIx8 " ", *ptr++);
338  strncat(output, temp, sizeof(output) - strlen(output) - 1);
339  }
340  command_print(CMD, "%s", output);
341 
342  return ERROR_OK;
343 }
344 
345 COMMAND_HANDLER(jtagspi_handle_always_4byte)
346 {
347  struct flash_bank *bank;
349  int retval;
350 
351  LOG_DEBUG("%s", __func__);
352 
353  if ((CMD_ARGC != 1) && (CMD_ARGC != 2))
355 
356  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
357  if (ERROR_OK != retval)
358  return retval;
359 
360  jtagspi_info = bank->driver_priv;
361 
362  if (CMD_ARGC == 1)
363  command_print(CMD, jtagspi_info->always_4byte ? "on" : "off");
364  else
365  COMMAND_PARSE_BOOL(CMD_ARGV[1], jtagspi_info->always_4byte, "on", "off");
366 
367  return ERROR_OK;
368 }
369 
370 static int jtagspi_probe(struct flash_bank *bank)
371 {
372  struct jtagspi_flash_bank *info = bank->driver_priv;
373  struct flash_sector *sectors;
374  const struct flash_device *p;
375  uint8_t in_buf[3];
376  uint32_t id, sectorsize;
377 
378  if (bank->sectors) {
379  free(bank->sectors);
380  bank->sectors = NULL;
381  }
382  info->probed = false;
383 
384  if (!bank->target->tap) {
385  LOG_ERROR("Target has no JTAG tap");
386  return ERROR_FAIL;
387  }
388  info->tap = bank->target->tap;
389 
390  jtagspi_cmd(bank, SPIFLASH_READ_ID, NULL, 0, in_buf, -3);
391  /* the table in spi.c has the manufacturer byte (first) as the lsb */
392  id = le_to_h_u24(in_buf);
393 
394  memset(&info->dev, 0, sizeof(info->dev));
395  for (p = flash_devices; p->name ; p++)
396  if (p->device_id == id) {
397  memcpy(&info->dev, p, sizeof(info->dev));
398  break;
399  }
400 
401  if (!(p->name)) {
402  LOG_ERROR("Unknown flash device (ID 0x%06" PRIx32 ")", id & 0xFFFFFF);
403  return ERROR_FAIL;
404  }
405 
406  LOG_INFO("Found flash device \'%s\' (ID 0x%06" PRIx32 ")",
407  info->dev.name, info->dev.device_id & 0xFFFFFF);
408 
409  /* Set correct size value */
410  bank->size = info->dev.size_in_bytes;
411 
412  /* calculate address length in bytes */
413  if (bank->size <= (1UL << 8))
414  info->addr_len = 1;
415  else if (bank->size <= (1UL << 16))
416  info->addr_len = 2;
417  else if (bank->size <= (1UL << 24))
418  info->addr_len = 3;
419  else {
420  info->addr_len = 4;
421  LOG_WARNING("4-byte addresses needed, might need extra command to enable");
422  }
423 
424  /* if no sectors, treat whole bank as single sector */
425  sectorsize = info->dev.sectorsize ?
426  info->dev.sectorsize : info->dev.size_in_bytes;
427 
428  /* create and fill sectors array */
429  bank->num_sectors = info->dev.size_in_bytes / sectorsize;
430  sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
431  if (!sectors) {
432  LOG_ERROR("not enough memory");
433  return ERROR_FAIL;
434  }
435 
436  for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
437  sectors[sector].offset = sector * sectorsize;
438  sectors[sector].size = sectorsize;
439  sectors[sector].is_erased = -1;
440  sectors[sector].is_protected = 0;
441  }
442 
443  bank->sectors = sectors;
444  info->probed = true;
445  return ERROR_OK;
446 }
447 
448 static int jtagspi_auto_probe(struct flash_bank *bank)
449 {
450  struct jtagspi_flash_bank *info = bank->driver_priv;
451 
452  if (info->probed)
453  return ERROR_OK;
454  return jtagspi_probe(bank);
455 }
456 
457 static int jtagspi_read_status(struct flash_bank *bank, uint32_t *status)
458 {
459  uint8_t buf;
460  int err = jtagspi_cmd(bank, SPIFLASH_READ_STATUS, NULL, 0, &buf, -1);
461  if (err == ERROR_OK) {
462  *status = buf;
463  LOG_DEBUG("status=0x%02" PRIx32, *status);
464  }
465  return err;
466 }
467 
468 static int jtagspi_wait(struct flash_bank *bank, int timeout_ms)
469 {
470  int64_t t0 = timeval_ms();
471  int64_t dt;
472 
473  do {
474  dt = timeval_ms() - t0;
475 
476  uint32_t status = (uint32_t)-1;
477  int retval = jtagspi_read_status(bank, &status);
478  if (retval != ERROR_OK)
479  return retval;
480 
481  if ((status & SPIFLASH_BSY_BIT) == 0) {
482  LOG_DEBUG("waited %" PRId64 " ms", dt);
483  return ERROR_OK;
484  }
485  alive_sleep(1);
486  } while (dt <= timeout_ms);
487 
488  LOG_ERROR("timeout, device still busy");
489  return ERROR_FAIL;
490 }
491 
493 {
495 
496  uint32_t status = (uint32_t)-1;
497  int retval = jtagspi_read_status(bank, &status);
498  if (retval != ERROR_OK)
499  return retval;
500 
501  if ((status & SPIFLASH_WE_BIT) == 0) {
502  LOG_ERROR("Cannot enable write to flash. Status=0x%02" PRIx32, status);
503  return ERROR_FAIL;
504  }
505  return ERROR_OK;
506 }
507 
508 static int jtagspi_bulk_erase(struct flash_bank *bank)
509 {
510  struct jtagspi_flash_bank *info = bank->driver_priv;
511  int retval;
512  int64_t t0 = timeval_ms();
513 
514  if (info->dev.chip_erase_cmd == 0x00)
516 
517  retval = jtagspi_write_enable(bank);
518  if (retval != ERROR_OK)
519  return retval;
520 
521  jtagspi_cmd(bank, info->dev.chip_erase_cmd, NULL, 0, NULL, 0);
522  if (retval != ERROR_OK)
523  return retval;
524 
525  retval = jtagspi_wait(bank, bank->num_sectors * JTAGSPI_MAX_TIMEOUT);
526  LOG_INFO("took %" PRId64 " ms", timeval_ms() - t0);
527  return retval;
528 }
529 
530 static uint8_t *fill_addr(uint32_t addr, unsigned int addr_len, uint8_t *buffer)
531 {
532  for (buffer += addr_len; addr_len > 0; --addr_len) {
533  *--buffer = addr;
534  addr >>= 8;
535  }
536 
537  return buffer;
538 }
539 
540 static int jtagspi_sector_erase(struct flash_bank *bank, unsigned int sector)
541 {
542  struct jtagspi_flash_bank *info = bank->driver_priv;
543  int retval;
544  uint8_t addr[sizeof(uint32_t)];
545  int64_t t0 = timeval_ms();
546 
547  retval = jtagspi_write_enable(bank);
548  if (retval != ERROR_OK)
549  return retval;
550 
551  /* ATXP032/064/128 use always 4-byte addresses except for 0x03 read */
552  unsigned int addr_len = info->always_4byte ? 4 : info->addr_len;
553 
554  retval = jtagspi_cmd(bank, info->dev.erase_cmd, fill_addr(bank->sectors[sector].offset, addr_len, addr),
555  addr_len, NULL, 0);
556  if (retval != ERROR_OK)
557  return retval;
558 
560  LOG_INFO("sector %u took %" PRId64 " ms", sector, timeval_ms() - t0);
561  return retval;
562 }
563 
564 static int jtagspi_erase(struct flash_bank *bank, unsigned int first,
565  unsigned int last)
566 {
567  struct jtagspi_flash_bank *info = bank->driver_priv;
568  int retval = ERROR_OK;
569 
570  LOG_DEBUG("erase from sector %u to sector %u", first, last);
571 
572  if ((last < first) || (last >= bank->num_sectors)) {
573  LOG_ERROR("Flash sector invalid");
575  }
576 
577  if (!(info->probed)) {
578  LOG_ERROR("Flash bank not probed");
580  }
581 
582  for (unsigned int sector = first; sector <= last; sector++) {
583  if (bank->sectors[sector].is_protected) {
584  LOG_ERROR("Flash sector %u protected", sector);
585  return ERROR_FAIL;
586  }
587  }
588 
589  if (first == 0 && last == (bank->num_sectors - 1) &&
590  info->dev.chip_erase_cmd != 0x00 &&
591  info->dev.chip_erase_cmd != info->dev.erase_cmd) {
592  LOG_DEBUG("Trying bulk erase.");
593  retval = jtagspi_bulk_erase(bank);
594  if (retval == ERROR_OK)
595  return retval;
596  else
597  LOG_WARNING("Bulk flash erase failed. Falling back to sector erase.");
598  }
599 
600  if (info->dev.erase_cmd == 0x00)
602 
603  for (unsigned int sector = first; sector <= last; sector++) {
604  retval = jtagspi_sector_erase(bank, sector);
605  if (retval != ERROR_OK) {
606  LOG_ERROR("Sector erase failed.");
607  break;
608  }
609  }
610 
611  return retval;
612 }
613 
614 static int jtagspi_protect(struct flash_bank *bank, int set, unsigned int first,
615  unsigned int last)
616 {
617  for (unsigned int sector = first; sector <= last; sector++)
618  bank->sectors[sector].is_protected = set;
619  return ERROR_OK;
620 }
621 
622 static int jtagspi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
623 {
624  struct jtagspi_flash_bank *info = bank->driver_priv;
625  uint32_t pagesize, currsize;
626  uint8_t addr[sizeof(uint32_t)];
627  int retval;
628 
629  if (!(info->probed)) {
630  LOG_ERROR("Flash bank not probed.");
632  }
633 
634  /* if no sectorsize, use reasonable default */
635  pagesize = info->dev.sectorsize ? info->dev.sectorsize : info->dev.pagesize;
636  if (pagesize == 0)
637  pagesize = (info->dev.size_in_bytes <= SPIFLASH_DEF_PAGESIZE) ?
638  info->dev.size_in_bytes : SPIFLASH_DEF_PAGESIZE;
639 
640  /* ATXP032/064/128 use always 4-byte addresses except for 0x03 read */
641  unsigned int addr_len = ((info->dev.read_cmd != 0x03) && info->always_4byte) ? 4 : info->addr_len;
642 
643  while (count > 0) {
644  /* length up to end of current page */
645  currsize = ((offset + pagesize) & ~(pagesize - 1)) - offset;
646  /* but no more than remaining size */
647  currsize = (count < currsize) ? count : currsize;
648 
649  retval = jtagspi_cmd(bank, info->dev.read_cmd, fill_addr(offset, addr_len, addr),
650  addr_len, buffer, -currsize);
651  if (retval != ERROR_OK) {
652  LOG_ERROR("page read error");
653  return retval;
654  }
655  LOG_DEBUG("read page at 0x%08" PRIx32, offset);
656  offset += currsize;
657  buffer += currsize;
658  count -= currsize;
659  }
660  return ERROR_OK;
661 }
662 
663 static int jtagspi_page_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
664 {
665  struct jtagspi_flash_bank *info = bank->driver_priv;
666  uint8_t addr[sizeof(uint32_t)];
667  int retval;
668 
669  retval = jtagspi_write_enable(bank);
670  if (retval != ERROR_OK)
671  return retval;
672 
673  /* ATXP032/064/128 use always 4-byte addresses except for 0x03 read */
674  unsigned int addr_len = ((info->dev.read_cmd != 0x03) && info->always_4byte) ? 4 : info->addr_len;
675 
676  retval = jtagspi_cmd(bank, info->dev.pprog_cmd, fill_addr(offset, addr_len, addr),
677  addr_len, (uint8_t *) buffer, count);
678  if (retval != ERROR_OK)
679  return retval;
681 }
682 
683 static int jtagspi_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
684 {
685  struct jtagspi_flash_bank *info = bank->driver_priv;
686  uint32_t pagesize, currsize;
687  int retval;
688 
689  if (!(info->probed)) {
690  LOG_ERROR("Flash bank not probed.");
692  }
693 
694  /* if no write pagesize, use reasonable default */
695  pagesize = info->dev.pagesize ? info->dev.pagesize : SPIFLASH_DEF_PAGESIZE;
696 
697  while (count > 0) {
698  /* length up to end of current page */
699  currsize = ((offset + pagesize) & ~(pagesize - 1)) - offset;
700  /* but no more than remaining size */
701  currsize = (count < currsize) ? count : currsize;
702 
703  retval = jtagspi_page_write(bank, buffer, offset, currsize);
704  if (retval != ERROR_OK) {
705  LOG_ERROR("page write error");
706  return retval;
707  }
708  LOG_DEBUG("wrote page at 0x%08" PRIx32, offset);
709  offset += currsize;
710  buffer += currsize;
711  count -= currsize;
712  }
713  return ERROR_OK;
714 }
715 
716 static int jtagspi_info(struct flash_bank *bank, struct command_invocation *cmd)
717 {
718  struct jtagspi_flash_bank *info = bank->driver_priv;
719 
720  if (!(info->probed)) {
721  command_print_sameline(cmd, "\nJTAGSPI flash bank not probed yet\n");
722  return ERROR_OK;
723  }
724 
725  command_print_sameline(cmd, "flash \'%s\', device id = 0x%06" PRIx32
726  ", flash size = %" PRIu32 " %sbytes\n(page size = %" PRIu32
727  ", read = 0x%02" PRIx8 ", qread = 0x%02" PRIx8
728  ", pprog = 0x%02" PRIx8 ", mass_erase = 0x%02" PRIx8
729  ", sector size = %" PRIu32 " %sbytes, sector_erase = 0x%02" PRIx8 ")",
730  info->dev.name, info->dev.device_id & 0xFFFFFF,
731  bank->size / 4096 ? bank->size / 1024 : bank->size,
732  bank->size / 4096 ? "k" : "", info->dev.pagesize,
733  info->dev.read_cmd, info->dev.qread_cmd,
734  info->dev.pprog_cmd, info->dev.chip_erase_cmd,
735  info->dev.sectorsize / 4096 ?
736  info->dev.sectorsize / 1024 : info->dev.sectorsize,
737  info->dev.sectorsize / 4096 ? "k" : "",
738  info->dev.erase_cmd);
739 
740  return ERROR_OK;
741 }
742 
743 static const struct command_registration jtagspi_exec_command_handlers[] = {
744  {
745  .name = "set",
746  .handler = jtagspi_handle_set,
747  .mode = COMMAND_EXEC,
748  .usage = "bank_id name chip_size page_size read_cmd unused pprg_cmd "
749  "[ mass_erase_cmd ] [ sector_size sector_erase_cmd ]",
750  .help = "Set device parameters if not autodetected.",
751  },
752  {
753  .name = "cmd",
754  .handler = jtagspi_handle_cmd,
755  .mode = COMMAND_EXEC,
756  .usage = "bank_id num_resp cmd_byte ...",
757  .help = "Send low-level command cmd_byte and following bytes, read num_bytes.",
758  },
759  {
760  .name = "always_4byte",
761  .handler = jtagspi_handle_always_4byte,
762  .mode = COMMAND_EXEC,
763  .usage = "bank_id [ on | off ]",
764  .help = "Use always 4-byte address except for basic 0x03.",
765  },
766 
768 };
769 
770 static const struct command_registration jtagspi_command_handlers[] = {
771  {
772  .name = "jtagspi",
773  .mode = COMMAND_ANY,
774  .help = "jtagspi command group",
775  .usage = "",
777  },
779 };
780 
781 const struct flash_driver jtagspi_flash = {
782  .name = "jtagspi",
783  .commands = jtagspi_command_handlers,
784  .flash_bank_command = jtagspi_flash_bank_command,
785  .erase = jtagspi_erase,
786  .protect = jtagspi_protect,
787  .write = jtagspi_write,
788  .read = jtagspi_read,
789  .probe = jtagspi_probe,
790  .auto_probe = jtagspi_auto_probe,
791  .erase_check = default_flash_blank_check,
792  .info = jtagspi_info,
793  .free_driver_priv = default_flash_free_driver_priv,
794 };
uint32_t flip_u32(uint32_t value, unsigned int num)
Definition: binarybuffer.c:166
static void buf_set_u32(uint8_t *_buffer, unsigned first, unsigned num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:30
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:450
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:473
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:140
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:117
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:155
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:385
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:150
#define COMMAND_PARSE_BOOL(in, out, on, off)
Parse the string as a binary parameter, storing the boolean value in out.
Definition: command.h:486
#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:425
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:247
@ 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.
static uint16_t output
Definition: ftdi.c:118
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
unsigned jtag_tap_count_enabled(void)
Definition: jtag/core.c:200
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_IDLE
Definition: jtag.h:52
static int jtagspi_auto_probe(struct flash_bank *bank)
Definition: jtagspi.c:448
static int jtagspi_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: jtagspi.c:614
#define JTAGSPI_MAX_TIMEOUT
Definition: jtagspi.c:16
static int jtagspi_write_enable(struct flash_bank *bank)
Definition: jtagspi.c:492
static void flip_u8(const uint8_t *in, uint8_t *out, unsigned int len)
Definition: jtagspi.c:65
static const struct command_registration jtagspi_command_handlers[]
Definition: jtagspi.c:770
static int jtagspi_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: jtagspi.c:564
static int jtagspi_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: jtagspi.c:683
const struct flash_driver jtagspi_flash
Definition: jtagspi.c:781
static void jtagspi_set_ir(struct flash_bank *bank)
Definition: jtagspi.c:51
FLASH_BANK_COMMAND_HANDLER(jtagspi_flash_bank_command)
Definition: jtagspi.c:29
static int jtagspi_sector_erase(struct flash_bank *bank, unsigned int sector)
Definition: jtagspi.c:540
static int jtagspi_wait(struct flash_bank *bank, int timeout_ms)
Definition: jtagspi.c:468
static uint8_t * fill_addr(uint32_t addr, unsigned int addr_len, uint8_t *buffer)
Definition: jtagspi.c:530
static int jtagspi_page_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: jtagspi.c:663
COMMAND_HANDLER(jtagspi_handle_set)
Definition: jtagspi.c:146
static const struct command_registration jtagspi_exec_command_handlers[]
Definition: jtagspi.c:743
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:71
static int jtagspi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: jtagspi.c:622
static int jtagspi_probe(struct flash_bank *bank)
Definition: jtagspi.c:370
static int jtagspi_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: jtagspi.c:716
static int jtagspi_read_status(struct flash_bank *bank, uint32_t *status)
Definition: jtagspi.c:457
static int jtagspi_bulk_erase(struct flash_bank *bank)
Definition: jtagspi.c:508
void alive_sleep(uint64_t ms)
Definition: log.c:460
#define LOG_WARNING(expr ...)
Definition: log.h:120
#define ERROR_FAIL
Definition: log.h:161
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define LOG_INFO(expr ...)
Definition: log.h:117
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:155
uint32_t addr
Definition: nuttx.c:65
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:229
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:100
unsigned int addr_len
Definition: jtagspi.c:26
uint32_t ir
Definition: jtagspi.c:25
struct jtag_tap * tap
Definition: jtagspi.c:20
char devname[32]
Definition: jtagspi.c:22
struct flash_device dev
Definition: jtagspi.c:21
This structure defines a single scan field in the scan.
Definition: jtag.h:86
int num_bits
The number of bits this field specifies.
Definition: jtag.h:88
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:92
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:90
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