OpenOCD
bl602.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2024 by Marek Kraus *
5  * gamelaster@gami.ee *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include "imp.h"
13 #include <jtag/jtag.h>
14 #include <target/algorithm.h>
15 #include "spi.h"
16 #include "target/riscv/opcodes.h"
17 #include "target/riscv/gdb_regs.h"
18 
19 /*
20  * Flash bank driver for Bouffalo chips with BL602-like flash controller
21  *
22  * Supported chips (series): BL602, BL702 and BL702L
23  * Not supported but compatible chips (series): BL616, BL606P and BL808
24  *
25  * SFlash supports both direct access and eXecute In Place (XIP) via L1 cache (L1C).
26  * If XIP is configured and direct access is needed,
27  * then the state needs to be saved and some settings need to be reset for it to work properly.
28  * The only way to do this is to save/restore the entire SFlash core configuration, which has not been tested.
29  * Because of this, on every OpenOCD flash probe, GPIO and SFlash will be reconfigured, resulting in XIP not working.
30  * So it's recommended to reset the device after every OpenOCD flash operation.
31  *
32  * QSPI write is not supported yet.
33  */
34 
35 // SFlash CFG definitions
36 #define SFLASH_CFG_SIZE 84
37 #define SFLASH_CFG_BUSY_BIT_POS 0x2D
38 #define SFLASH_CFG_BUSY_INDEX_POS 0x2A
39 #define SFLASH_CFG_BUSY_READ_REG_LEN_POS 0x33
40 #define SFLASH_CFG_FAST_READ_CMD_POS 0x18
41 #define SFLASH_CFG_JEDEC_ID_CMD_DMY_CLK_POS 0x09
42 #define SFLASH_CFG_JEDEC_ID_CMD_POS 0x08
43 #define SFLASH_CFG_PAGE_PROGRAM_CMD_POS 0x15
44 #define SFLASH_CFG_PAGE_SIZE_POS 0x0E
45 #define SFLASH_CFG_READ_STATUS_REG1_POS 0x34
46 #define SFLASH_CFG_SECTOR_ERASE_CMD_POS 0x11
47 #define SFLASH_CFG_SECTOR_SIZE_POS 0x0C
48 #define SFLASH_CFG_TIME_ERASE_SECTOR_POS 0x48
49 #define SFLASH_CFG_TIME_PAGE_PGM_POS 0x4E
50 #define SFLASH_CFG_WRITE_ENABLE_BIT_POS 0x2B
51 #define SFLASH_CFG_WRITE_ENABLE_CMD_POS 0x14
52 #define SFLASH_CFG_WRITE_ENABLE_INDEX_POS 0x28
53 #define SFLASH_CFG_WRITE_ENABLE_READ_REG_LEN_POS 0x2F
54 
55 #define BL602_DEFAULT_TIMEOUT_MS 5000
56 
61 };
62 
64  uint32_t idcode;
65  enum bflb_series series;
72 };
73 
75  // flag indicating successful flash probe
76  bool probed;
77  enum bflb_series series;
78  const struct bl602_part_info *part_info;
80  // detected model of SPI flash
81  const struct flash_device *dev;
82 };
83 
84 static const struct bl602_part_info bl602_parts[] = {
85  {
86  .idcode = 0x20000c05,
87  .series = BFLB_SERIES_BL602,
88  .romapi_get_jedec_id = 0x210109bc,
89  .romapi_sflash_init_gpio = 0x21010a20,
90  .romapi_sflash_init = 0x21010980,
91  .romapi_sflash_program = 0x210109b4,
92  .romapi_sflash_read = 0x210109fc,
93  .romapi_sflash_erase_sector = 0x210109a4,
94  },
95  {
96  .idcode = 0x20000e05,
97  .series = BFLB_SERIES_BL702,
98  .romapi_get_jedec_id = 0x210189a4,
99  .romapi_sflash_init_gpio = 0x21018a78,
100  .romapi_sflash_init = 0x21018960,
101  .romapi_sflash_program = 0x2101899c,
102  .romapi_sflash_read = 0x210189d8,
103  .romapi_sflash_erase_sector = 0x2101898c,
104  },
105  {
106  .idcode = 0x20000e05,
107  .series = BFLB_SERIES_BL702L,
108  .romapi_get_jedec_id = 0x21010d10,
109  .romapi_sflash_init_gpio = 0x21010c54,
110  .romapi_sflash_init = 0x21010cc8,
111  .romapi_sflash_program = 0x21010d08,
112  .romapi_sflash_read = 0x21010d44,
113  .romapi_sflash_erase_sector = 0x21010cf8,
114  },
115 };
116 
117 static int bl602_call_func(struct target *target, uint32_t func_addr,
118  uint32_t arg_data[], unsigned int n_args, uint32_t *return_data, unsigned int timeout_ms)
119 {
120  static const char * const reg_names[] = { "a0", "a1", "a2", "a3", "a4", "a5" };
121  struct reg_param reg_params[ARRAY_SIZE(reg_names) + 1];
122  unsigned int n_reg_params = 0;
123 
124  assert(n_args <= ARRAY_SIZE(reg_names)); // only allow register arguments
125 
126  struct working_area *trampoline_algorithm;
127 
128  uint32_t trampoline_code[] = {
129  ebreak(),
130  };
131 
132  int retval = target_alloc_working_area(target, sizeof(trampoline_code),
133  &trampoline_algorithm);
134  if (retval != ERROR_OK) {
135  LOG_ERROR("No working area available, can't do trampoline");
136  return retval;
137  }
138 
139  retval = target_write_buffer(target, trampoline_algorithm->address,
140  sizeof(trampoline_code), (uint8_t *)trampoline_code);
141  if (retval != ERROR_OK) {
142  LOG_ERROR("Writing trampoline code failed, can't do trampoline");
143  goto cleanup;
144  }
145 
146  // initialize a0 register, which is used both as arg and also return register.
147  if (!return_data || n_args > 0) {
148  init_reg_param(&reg_params[0], reg_names[0], 32, PARAM_IN);
149  if (n_args > 0) {
150  buf_set_u32(reg_params[0].value, 0, 32, arg_data[0]);
151  reg_params[0].direction = PARAM_IN_OUT;
152  }
153  n_reg_params++;
154  }
155  // initialize rest of registers, if any
156  for (unsigned int i = 1; i < n_args; ++i) {
157  init_reg_param(&reg_params[i], reg_names[i], 32, PARAM_OUT);
158  buf_set_u32(reg_params[i].value, 0, 32, arg_data[i]);
159  n_reg_params++;
160  }
161  // set return address to the ebreak instruction in working area
162  init_reg_param(&reg_params[n_reg_params], "ra", 32, PARAM_OUT);
163  buf_set_u32(reg_params[n_reg_params].value, 0, 32, trampoline_algorithm->address);
164  n_reg_params++;
165 
166  retval = target_run_algorithm(target,
167  0, NULL,
168  n_reg_params, reg_params,
169  func_addr,
170  trampoline_algorithm->address,
171  timeout_ms, NULL);
172 
173  if (retval != ERROR_OK) {
174  LOG_ERROR("Failed to execute algorithm at 0x%" TARGET_PRIxADDR ": %d",
175  trampoline_algorithm->address, retval);
176  goto cleanup;
177  }
178 
179  if (return_data)
180  *return_data = buf_get_u32(reg_params[0].value, 0, 32);
181 
182 cleanup:
183  for (unsigned int i = 0; i < n_reg_params; i++)
184  destroy_reg_param(&reg_params[i]);
185 
186  target_free_working_area(target, trampoline_algorithm);
187  return retval;
188 }
189 
190 static int bl602_call_romapi_func(struct target *target, uint32_t romapi_func_addr,
191  uint32_t arg_data[], unsigned int n_args, uint32_t *return_data, unsigned int timeout_ms)
192 {
193  uint32_t func_addr;
194 
195  int retval = target_read_u32(target, romapi_func_addr, &func_addr);
196  if (retval != ERROR_OK)
197  return retval;
198 
199  return bl602_call_func(target, func_addr, arg_data, n_args, return_data, timeout_ms);
200 }
201 
203  struct working_area **working_area, uint32_t count)
204 {
205  struct bl602_flash_bank *priv = bank->driver_priv;
206  struct target *target = bank->target;
207 
208  unsigned int avail_pages = target_get_working_area_avail(target) / priv->dev->pagesize;
209  if (avail_pages == 0) {
210  LOG_ERROR("Not enough space for bounce buffer. Can't continue");
212  }
213 
214  /* We try to allocate working area rounded down to device page size,
215  * at least 1 page, at most the write data size */
216  unsigned int chunk_size = MIN(MAX(avail_pages - 1, 1) * priv->dev->pagesize, count);
217  int retval = target_alloc_working_area(target, chunk_size, working_area);
218  if (retval != ERROR_OK) {
219  LOG_ERROR("Could not allocate bounce buffer for flash manipulation. Can't continue");
220  return retval;
221  }
222 
223  LOG_DEBUG("Allocated flash bounce buffer @" TARGET_ADDR_FMT, (*working_area)->address);
224 
225  return ERROR_OK;
226 }
227 
229  struct working_area **working_area)
230 {
231  struct bl602_flash_bank *priv = bank->driver_priv;
232  struct target *target = bank->target;
233 
234  int retval = target_alloc_working_area(target, sizeof(priv->sflash_cfg),
235  working_area);
236  if (retval != ERROR_OK) {
237  LOG_ERROR("No working area available, can't alloc sflash cfg");
238  return retval;
239  }
240 
241  retval = target_write_buffer(target, (*working_area)->address,
242  sizeof(priv->sflash_cfg), priv->sflash_cfg);
243  if (retval != ERROR_OK)
245  return retval;
246 }
247 
249 {
250  struct bl602_flash_bank *priv = bank->driver_priv;
251  struct target *target = bank->target;
252  const struct bl602_part_info *part_info = priv->part_info;
253 
254  uint32_t flash_pin_cfg = 0;
255 
256  // Read device info from eFuse
257  if (part_info->series == BFLB_SERIES_BL702 ||
258  part_info->series == BFLB_SERIES_BL702L) {
259  // Read device_info register
260  uint32_t device_info;
261  int retval = target_read_u32(target, 0x40007074, &device_info);
262  if (retval != ERROR_OK || device_info == 0x0) {
263  /* device_info being 0x0 shouldn't happen, since eFuse is loaded in BootROM,
264  * but better to handle it */
265  LOG_ERROR("Failed to read device info from eFuse");
266  return retval;
267  }
268 
269  uint32_t flash_cfg = (device_info >> 26) & 0x03;
270  uint32_t sf_swap_cfg = (device_info >> 22) & 0x03;
271  uint32_t sf_reverse_cfg = (device_info >> 29) & 0x01; // BL702L only
272  if (part_info->series == BFLB_SERIES_BL702) {
273  flash_pin_cfg = (flash_cfg << 2) | sf_swap_cfg;
274  } else { // BL702L
275  flash_pin_cfg = sf_swap_cfg + (sf_reverse_cfg != 0 ? 5 : 1);
276  }
277  } else if (part_info->series == BFLB_SERIES_BL602) {
278  // Structure is not known.
279  uint32_t sw_usage;
280  int retval = target_read_u32(target, 0x40007010, &sw_usage);
281  if (retval != ERROR_OK || sw_usage == 0x0) {
282  /* sw_usage being 0x0 shouldn't happen, since eFuse is loaded in BootROM,
283  * but better to handle it */
284  LOG_ERROR("Failed to read sw usage from eFuse");
285  return retval;
286  }
287 
288  // We only assume it is sf_swap_cfg
289  uint32_t sf_swap_cfg = (sw_usage >> 16) & 0x03;
290  flash_pin_cfg = sf_swap_cfg;
291  }
292 
293  // initialize flash GPIOs
294  uint32_t args[] = {
295  flash_pin_cfg,
296  1, // restoreDefault
297  };
298 
299  int retval = bl602_call_romapi_func(target, part_info->romapi_sflash_init_gpio,
301  if (retval != ERROR_OK) {
302  LOG_ERROR("Failed to invoke spi flash gpio init function");
303  return retval;
304  }
305 
306  return ERROR_OK;
307 }
308 
309 static const uint8_t bl602_sflash_ctrl_cfg[] = {
310  0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00
311 };
312 
313 /* The structure of SF_Ctrl_Cfg_Type is same as on BL602 and BL702,
314  * but BL702L's BootROM was compiled without -fshort-enums flag.
315  * That caused that first three fields (enums) are 4 bytes instead of 1 byte. */
316 static const uint8_t bl702l_sflash_ctrl_cfg[] = {
317  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
318  0xe1, 0xfd, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00,
319  0x00, 0x00, 0x01, 0x21,
320 };
321 
322 static int bl602_flash_init(struct flash_bank *bank)
323 {
324  struct bl602_flash_bank *priv = bank->driver_priv;
325  struct target *target = bank->target;
326  const struct bl602_part_info *part_info = priv->part_info;
327 
328  int retval = bl602_flash_gpio_init(bank);
329  if (retval != ERROR_OK)
330  return retval;
331 
332  // initialize SFlash peripheral
333  // structure for this can be found in official SDK by name SF_Ctrl_Cfg_Type
334  const uint8_t *sflash_ctrl_cfg = NULL;
335  uint16_t sflash_ctrl_cfg_size = 0;
336 
337  switch (part_info->series) {
338  case BFLB_SERIES_BL602:
339  case BFLB_SERIES_BL702:
340  sflash_ctrl_cfg = bl602_sflash_ctrl_cfg;
341  sflash_ctrl_cfg_size = ARRAY_SIZE(bl602_sflash_ctrl_cfg);
342  break;
343  case BFLB_SERIES_BL702L:
344  sflash_ctrl_cfg = bl702l_sflash_ctrl_cfg;
345  sflash_ctrl_cfg_size = ARRAY_SIZE(bl702l_sflash_ctrl_cfg);
346  break;
347  default:
348  LOG_ERROR("No SFlash Control Config available for this chip");
349  return ERROR_NOT_IMPLEMENTED;
350  }
351 
352  struct working_area *sflash_ctrl_cfg_area;
353  retval = target_alloc_working_area(target, sflash_ctrl_cfg_size,
354  &sflash_ctrl_cfg_area);
355  if (retval != ERROR_OK) {
356  LOG_WARNING("No working area available, can't init SFlash");
357  return retval;
358  }
359 
360  retval = target_write_buffer(target, sflash_ctrl_cfg_area->address,
361  sflash_ctrl_cfg_size, sflash_ctrl_cfg);
362  if (retval != ERROR_OK) {
363  LOG_ERROR("Failed to write SFlash Control CFG into working area");
364  goto cleanup;
365  }
366 
367  uint32_t args[] = {
368  sflash_ctrl_cfg_area->address,
369  };
370 
371  retval = bl602_call_romapi_func(target, part_info->romapi_sflash_init,
373  if (retval != ERROR_OK) {
374  LOG_ERROR("Failed to invoke SFlash init function");
375  goto cleanup;
376  }
377 
378 cleanup:
379  target_free_working_area(target, sflash_ctrl_cfg_area);
380  return retval;
381 }
382 
383 static int bl602_flash_read_id(struct flash_bank *bank, uint32_t *jedec_id)
384 {
385  struct bl602_flash_bank *priv = bank->driver_priv;
386  struct target *target = bank->target;
387  const struct bl602_part_info *part_info = priv->part_info;
388 
389  struct working_area *data_area;
390  int retval = target_alloc_working_area(target, sizeof(uint32_t) + SFLASH_CFG_SIZE,
391  &data_area);
392  if (retval != ERROR_OK) {
393  LOG_ERROR("No working area available, can't read flash id");
394  return retval;
395  }
396 
397  retval = target_write_buffer(target, data_area->address + sizeof(uint32_t),
398  SFLASH_CFG_SIZE, priv->sflash_cfg);
399  if (retval != ERROR_OK) {
400  LOG_ERROR("Failed to write data required for flash id read");
401  goto cleanup;
402  }
403 
404  uint32_t args[2] = {
405  data_area->address + 4, // SFlash CFG
406  data_area->address, // JEDEC ID pointer
407  };
408 
411  if (retval != ERROR_OK) {
412  LOG_ERROR("Failed to invoke get jedec id function");
413  goto cleanup;
414  }
415 
416  retval = target_read_u32(target, data_area->address, jedec_id);
417  if (retval != ERROR_OK) {
418  LOG_ERROR("Failed to read flash id from target");
419  goto cleanup;
420  }
421 
422  *jedec_id &= 0x00FFFFFF;
423 
424 cleanup:
425  target_free_working_area(target, data_area);
426 
427  return retval;
428 }
429 
430 static int bl602_erase(struct flash_bank *bank, unsigned int first,
431  unsigned int last)
432 {
433  struct bl602_flash_bank *priv = bank->driver_priv;
434  const struct bl602_part_info *part_info = priv->part_info;
435  struct target *target = bank->target;
436  struct working_area *sflash_cfg_area = NULL;
437 
438  if (target->state != TARGET_HALTED) {
439  LOG_ERROR("Target not halted");
441  }
442 
443  int retval = bl602_alloc_sflash_cfg(bank, &sflash_cfg_area);
444  if (retval != ERROR_OK)
445  return retval;
446 
447  uint32_t return_value;
448 
449  for (unsigned int sector = first; sector <= last; sector++) {
450  LOG_INFO("Erasing sector %d", sector);
451  uint32_t args[] = {
452  sflash_cfg_area->address,
453  sector,
454  };
456  args, ARRAY_SIZE(args), &return_value, BL602_DEFAULT_TIMEOUT_MS);
457  if (retval != ERROR_OK) {
458  LOG_ERROR("Failed to invoke flash erase code on target");
459  break;
460  }
461  if (return_value != 0) {
462  LOG_ERROR("Erase flash function returned wrong value: %02X", return_value);
463  retval = ERROR_FAIL;
464  break;
465  }
466  }
467 
468  target_free_working_area(target, sflash_cfg_area);
469  return retval;
470 }
471 
472 static int bl602_write(struct flash_bank *bank, const uint8_t *buffer,
473  uint32_t offset, uint32_t count)
474 {
475  struct bl602_flash_bank *priv = bank->driver_priv;
476  const struct bl602_part_info *part_info = priv->part_info;
477  struct target *target = bank->target;
478  struct working_area *bounce_area = NULL;
479  struct working_area *sflash_cfg_area = NULL;
480 
481  LOG_DEBUG("bank->size=0x%x offset=0x%08" PRIx32 " count=0x%08" PRIx32,
482  bank->size, offset, count);
483 
484  if (target->state != TARGET_HALTED) {
485  LOG_ERROR("Target not halted");
487  }
488 
489  if (offset + count > priv->dev->size_in_bytes) {
490  LOG_WARNING("Write past end of flash. Extra data discarded");
491  count = priv->dev->size_in_bytes - offset;
492  }
493 
494  int retval = bl602_alloc_sflash_cfg(bank, &sflash_cfg_area);
495  if (retval != ERROR_OK)
496  return retval;
497 
498  retval = bl602_alloc_bounce_buffer(bank, &bounce_area, count);
499  if (retval != ERROR_OK)
500  goto cleanup;
501 
502  unsigned int chunk_size = bounce_area->size;
503  uint32_t return_value;
504 
505  while (count > 0) {
506  uint32_t write_size = count > chunk_size ? chunk_size : count;
507  LOG_INFO("Writing %d bytes to offset 0x%" PRIx32, write_size, offset);
508  retval = target_write_buffer(target, bounce_area->address, write_size, buffer);
509  if (retval != ERROR_OK) {
510  LOG_ERROR("Could not load data into target bounce buffer");
511  goto cleanup;
512  }
513  uint32_t args[] = {
514  sflash_cfg_area->address,
515  0x0, // io_mode
516  offset,
517  bounce_area->address,
518  write_size,
519  };
521  args, ARRAY_SIZE(args), &return_value, BL602_DEFAULT_TIMEOUT_MS);
522  if (retval != ERROR_OK) {
523  LOG_ERROR("Failed to invoke flash programming code on target");
524  goto cleanup;
525  }
526  if (return_value != 0) {
527  LOG_ERROR("Write flash function returned wrong value: %02X", return_value);
528  retval = ERROR_FAIL;
529  goto cleanup;
530  }
531 
532  buffer += write_size;
533  offset += write_size;
534  count -= write_size;
535  }
536 
537 
538 cleanup:
539  target_free_working_area(target, bounce_area);
540  target_free_working_area(target, sflash_cfg_area);
541  return retval;
542 }
543 
544 static int bl602_read(struct flash_bank *bank,
545  uint8_t *buffer, uint32_t offset, uint32_t count)
546 {
547  struct bl602_flash_bank *priv = bank->driver_priv;
548  const struct bl602_part_info *part_info = priv->part_info;
549  struct target *target = bank->target;
550  struct working_area *bounce_area = NULL;
551  struct working_area *sflash_cfg_area = NULL;
552 
553  if (target->state != TARGET_HALTED) {
554  LOG_ERROR("Target not halted");
556  }
557 
558  if (offset + count > priv->dev->size_in_bytes) {
559  LOG_WARNING("Read past end of flash");
560  count = priv->dev->size_in_bytes - offset;
561  }
562 
563  int retval = bl602_alloc_sflash_cfg(bank, &sflash_cfg_area);
564  if (retval != ERROR_OK)
565  return retval;
566 
567  retval = bl602_alloc_bounce_buffer(bank, &bounce_area, count);
568  if (retval != ERROR_OK)
569  goto cleanup;
570 
571  unsigned int chunk_size = bounce_area->size;
572 
573  while (count > 0) {
574  uint32_t read_size = count > chunk_size ? chunk_size : count;
575  LOG_DEBUG("Read %d bytes from offset 0x%" PRIx32, read_size, offset);
576  uint32_t args[] = {
577  sflash_cfg_area->address,
578  0x0, // io_mode
579  false, // continous_read
580  offset,
581  bounce_area->address,
582  read_size,
583  };
584  uint32_t return_value;
585  retval = bl602_call_romapi_func(target, part_info->romapi_sflash_read,
586  args, ARRAY_SIZE(args), &return_value, BL602_DEFAULT_TIMEOUT_MS);
587  if (retval != ERROR_OK) {
588  LOG_ERROR("Failed to invoke flash read code on target");
589  break;
590  }
591  if (return_value != 0) {
592  LOG_ERROR("Read flash function returned wrong value: %02X", return_value);
593  retval = ERROR_FAIL;
594  break;
595  }
596 
597  retval = target_read_buffer(target, bounce_area->address, read_size, buffer);
598  if (retval != ERROR_OK) {
599  LOG_ERROR("Could not load data from target bounce buffer");
600  break;
601  }
602 
603  buffer += read_size;
604  offset += read_size;
605  count -= read_size;
606  }
607 
608 cleanup:
609  target_free_working_area(target, bounce_area);
610  target_free_working_area(target, sflash_cfg_area);
611 
612  return retval;
613 }
614 
615 static int bl602_probe(struct flash_bank *bank)
616 {
617  struct target *target = bank->target;
618  struct bl602_flash_bank *priv = bank->driver_priv;
619 
620  if (!target_was_examined(target)) {
621  LOG_ERROR("Target not examined yet");
623  }
624 
625  priv->probed = false;
626 
627  for (unsigned int n = 0; n < ARRAY_SIZE(bl602_parts); n++) {
628  const struct bl602_part_info *part_info = &bl602_parts[n];
629  if (priv->series == part_info->series) {
630  if (target->tap->idcode != part_info->idcode) {
631  LOG_ERROR("TAP IDCODE is not valid for selected chip type");
632  return ERROR_FAIL;
633  }
634  priv->part_info = part_info;
635  break;
636  }
637  }
638 
639  if (!priv->part_info) {
640  LOG_ERROR("Cannot identify target as an BL602 family device");
641  return ERROR_FAIL;
642  }
643 
644  int retval = bl602_flash_init(bank);
645  if (retval != ERROR_OK) {
646  LOG_ERROR("Initialization of flash failed");
647  return retval;
648  }
649 
650  uint32_t jedec_id;
651  retval = bl602_flash_read_id(bank, &jedec_id);
652  if (retval != ERROR_OK) {
653  LOG_ERROR("Cannot identify flash JEDEC ID");
654  return retval;
655  }
656 
657  priv->dev = NULL;
658  for (const struct flash_device *p = flash_devices; p->name ; p++) {
659  if (p->device_id == jedec_id) {
660  priv->dev = p;
661  break;
662  }
663  }
664 
665  if (!priv->dev) {
666  LOG_ERROR("Unknown flash device (ID 0x%08" PRIx32 ")", jedec_id);
667  return ERROR_FAIL;
668  }
669  LOG_INFO("Found flash device '%s' (ID 0x%08" PRIx32 ")", priv->dev->name,
670  priv->dev->device_id);
671 
672  // set correct size value
673  bank->size = priv->dev->size_in_bytes;
674  bank->num_sectors = bank->size / priv->dev->sectorsize;
675  bank->write_start_alignment = 8;
676  bank->write_end_alignment = 8;
677 
678  bank->sectors = alloc_block_array(0, priv->dev->sectorsize, bank->num_sectors);
679  if (!bank->sectors)
680  return ERROR_FAIL;
681 
682  priv->sflash_cfg[SFLASH_CFG_PAGE_PROGRAM_CMD_POS] = priv->dev->pprog_cmd;
683  priv->sflash_cfg[SFLASH_CFG_PAGE_SIZE_POS] = priv->dev->pagesize & 0xFF;
684  priv->sflash_cfg[SFLASH_CFG_PAGE_SIZE_POS + 1] = (priv->dev->pagesize >> 8) & 0xFF;
685  priv->sflash_cfg[SFLASH_CFG_FAST_READ_CMD_POS] = priv->dev->read_cmd;
686  priv->sflash_cfg[SFLASH_CFG_SECTOR_SIZE_POS] = priv->dev->sectorsize / 1024;
687  priv->sflash_cfg[SFLASH_CFG_SECTOR_ERASE_CMD_POS] = priv->dev->erase_cmd;
688 
689  priv->probed = true;
690 
691  return retval;
692 }
693 
694 static int bl602_auto_probe(struct flash_bank *bank)
695 {
696  struct bl602_flash_bank *priv = bank->driver_priv;
697 
698  if (priv->probed)
699  return ERROR_OK;
700 
701  return bl602_probe(bank);
702 }
703 
704 /*
705  * flash bank bl602 <base> 0 0 0 <target#> <chip_type>
706  */
707 FLASH_BANK_COMMAND_HANDLER(bl602_flash_bank_command)
708 {
709  if (CMD_ARGC < 7)
711 
712  struct bl602_flash_bank *priv;
713  priv = malloc(sizeof(struct bl602_flash_bank));
714  priv->probed = false;
715  priv->part_info = NULL;
716 
717  if (strcmp(CMD_ARGV[6], "bl602") == 0) {
718  priv->series = BFLB_SERIES_BL602;
719  } else if (strcmp(CMD_ARGV[6], "bl702") == 0) {
720  priv->series = BFLB_SERIES_BL702;
721  } else if (strcmp(CMD_ARGV[6], "bl702l") == 0) {
722  priv->series = BFLB_SERIES_BL702L;
723  } else {
724  LOG_ERROR("Unknown BL602 chip type: %s", CMD_ARGV[6]);
725  free(priv);
727  }
728 
729  // initialize default sflash_cfg fields
730  priv->sflash_cfg[SFLASH_CFG_JEDEC_ID_CMD_POS] = 0x9F;
731  priv->sflash_cfg[SFLASH_CFG_JEDEC_ID_CMD_DMY_CLK_POS] = 0x0;
732  priv->sflash_cfg[SFLASH_CFG_TIME_PAGE_PGM_POS] = 200;
733  priv->sflash_cfg[SFLASH_CFG_WRITE_ENABLE_CMD_POS] = 0x06;
734  priv->sflash_cfg[SFLASH_CFG_WRITE_ENABLE_INDEX_POS] = 0;
736  priv->sflash_cfg[SFLASH_CFG_WRITE_ENABLE_BIT_POS] = 1;
737  priv->sflash_cfg[SFLASH_CFG_READ_STATUS_REG1_POS] = 0x05;
738  priv->sflash_cfg[SFLASH_CFG_BUSY_INDEX_POS] = 0;
739  priv->sflash_cfg[SFLASH_CFG_BUSY_READ_REG_LEN_POS] = 1;
740  priv->sflash_cfg[SFLASH_CFG_BUSY_BIT_POS] = 0;
741  priv->sflash_cfg[SFLASH_CFG_TIME_ERASE_SECTOR_POS] = 1000 & 0xFF;
742  priv->sflash_cfg[SFLASH_CFG_TIME_ERASE_SECTOR_POS + 1] = (1000 >> 8) & 0xFF;
743 
744  // set up driver_priv
745  bank->driver_priv = priv;
746 
747  return ERROR_OK;
748 }
749 
750 const struct flash_driver bl602_flash = {
751  .name = "bl602",
752  .flash_bank_command = bl602_flash_bank_command,
753  .erase = bl602_erase,
754  .write = bl602_write,
755  .read = bl602_read,
756  .probe = bl602_probe,
757  .auto_probe = bl602_auto_probe,
758  .erase_check = default_flash_blank_check,
759  .free_driver_priv = default_flash_free_driver_priv
760 };
void init_reg_param(struct reg_param *param, const char *reg_name, uint32_t size, enum param_direction direction)
Definition: algorithm.c:29
void destroy_reg_param(struct reg_param *param)
Definition: algorithm.c:38
@ PARAM_OUT
Definition: algorithm.h:16
@ PARAM_IN
Definition: algorithm.h:15
@ PARAM_IN_OUT
Definition: algorithm.h:17
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
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
static int bl602_flash_gpio_init(struct flash_bank *bank)
Definition: bl602.c:248
#define SFLASH_CFG_SECTOR_SIZE_POS
Definition: bl602.c:47
static int bl602_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: bl602.c:430
#define SFLASH_CFG_BUSY_READ_REG_LEN_POS
Definition: bl602.c:39
#define SFLASH_CFG_FAST_READ_CMD_POS
Definition: bl602.c:40
static int bl602_call_func(struct target *target, uint32_t func_addr, uint32_t arg_data[], unsigned int n_args, uint32_t *return_data, unsigned int timeout_ms)
Definition: bl602.c:117
static int bl602_alloc_sflash_cfg(struct flash_bank *bank, struct working_area **working_area)
Definition: bl602.c:228
#define BL602_DEFAULT_TIMEOUT_MS
Definition: bl602.c:55
#define SFLASH_CFG_SIZE
Definition: bl602.c:36
static int bl602_flash_read_id(struct flash_bank *bank, uint32_t *jedec_id)
Definition: bl602.c:383
static const uint8_t bl602_sflash_ctrl_cfg[]
Definition: bl602.c:309
#define SFLASH_CFG_READ_STATUS_REG1_POS
Definition: bl602.c:45
static int bl602_alloc_bounce_buffer(struct flash_bank *bank, struct working_area **working_area, uint32_t count)
Definition: bl602.c:202
#define SFLASH_CFG_JEDEC_ID_CMD_POS
Definition: bl602.c:42
static const uint8_t bl702l_sflash_ctrl_cfg[]
Definition: bl602.c:316
#define SFLASH_CFG_PAGE_SIZE_POS
Definition: bl602.c:44
#define SFLASH_CFG_WRITE_ENABLE_BIT_POS
Definition: bl602.c:50
static int bl602_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: bl602.c:544
#define SFLASH_CFG_JEDEC_ID_CMD_DMY_CLK_POS
Definition: bl602.c:41
bflb_series
Definition: bl602.c:57
@ BFLB_SERIES_BL702L
Definition: bl602.c:60
@ BFLB_SERIES_BL702
Definition: bl602.c:59
@ BFLB_SERIES_BL602
Definition: bl602.c:58
static int bl602_call_romapi_func(struct target *target, uint32_t romapi_func_addr, uint32_t arg_data[], unsigned int n_args, uint32_t *return_data, unsigned int timeout_ms)
Definition: bl602.c:190
static int bl602_auto_probe(struct flash_bank *bank)
Definition: bl602.c:694
#define SFLASH_CFG_WRITE_ENABLE_CMD_POS
Definition: bl602.c:51
#define SFLASH_CFG_PAGE_PROGRAM_CMD_POS
Definition: bl602.c:43
static int bl602_flash_init(struct flash_bank *bank)
Definition: bl602.c:322
#define SFLASH_CFG_TIME_PAGE_PGM_POS
Definition: bl602.c:49
#define SFLASH_CFG_SECTOR_ERASE_CMD_POS
Definition: bl602.c:46
#define SFLASH_CFG_WRITE_ENABLE_READ_REG_LEN_POS
Definition: bl602.c:53
#define SFLASH_CFG_WRITE_ENABLE_INDEX_POS
Definition: bl602.c:52
static int bl602_probe(struct flash_bank *bank)
Definition: bl602.c:615
static int bl602_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: bl602.c:472
FLASH_BANK_COMMAND_HANDLER(bl602_flash_bank_command)
Definition: bl602.c:707
static const struct bl602_part_info bl602_parts[]
Definition: bl602.c:84
#define SFLASH_CFG_TIME_ERASE_SECTOR_POS
Definition: bl602.c:48
const struct flash_driver bl602_flash
Definition: bl602.c:750
#define SFLASH_CFG_BUSY_INDEX_POS
Definition: bl602.c:38
#define SFLASH_CFG_BUSY_BIT_POS
Definition: bl602.c:37
#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:400
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint8_t bank
Definition: esirisc.c:135
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
#define ERROR_FLASH_BANK_INVALID
Definition: flash/common.h:28
struct flash_sector * alloc_block_array(uint32_t offset, uint32_t size, unsigned int num_blocks)
Allocate and fill an array of sectors or protection blocks.
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.
The JTAG interface can be implemented with a software or hardware fifo.
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:179
#define LOG_WARNING(expr ...)
Definition: log.h:131
#define ERROR_FAIL
Definition: log.h:175
#define LOG_ERROR(expr ...)
Definition: log.h:134
#define LOG_INFO(expr ...)
Definition: log.h:128
#define LOG_DEBUG(expr ...)
Definition: log.h:111
#define ERROR_OK
Definition: log.h:169
static uint32_t ebreak(void) __attribute__((unused))
Definition: opcodes.h:336
#define MIN(a, b)
Definition: replacements.h:22
#define MAX(a, b)
Definition: replacements.h:25
const struct flash_device flash_devices[]
Definition: spi.c:24
bool probed
Definition: bl602.c:76
const struct bl602_part_info * part_info
Definition: bl602.c:78
enum bflb_series series
Definition: bl602.c:77
uint8_t sflash_cfg[SFLASH_CFG_SIZE]
Definition: bl602.c:79
const struct flash_device * dev
Definition: bl602.c:81
uint32_t romapi_sflash_init
Definition: bl602.c:68
uint32_t romapi_sflash_init_gpio
Definition: bl602.c:67
uint32_t romapi_sflash_erase_sector
Definition: bl602.c:71
uint32_t romapi_get_jedec_id
Definition: bl602.c:66
uint32_t romapi_sflash_read
Definition: bl602.c:70
enum bflb_series series
Definition: bl602.c:65
uint32_t idcode
Definition: bl602.c:64
uint32_t romapi_sflash_program
Definition: bl602.c:69
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
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
uint32_t idcode
device identification code
Definition: jtag.h:115
enum param_direction direction
Definition: algorithm.h:31
Definition: target.h:119
struct jtag_tap * tap
Definition: target.h:122
enum target_state state
Definition: target.h:160
uint32_t size
Definition: target.h:90
target_addr_t address
Definition: target.h:89
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2351
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2416
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
Definition: target.c:783
uint32_t target_get_working_area_avail(struct target *target)
Definition: target.c:2174
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2070
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2128
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2546
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:786
static bool target_was_examined(const struct target *target)
Definition: target.h:432
@ TARGET_HALTED
Definition: target.h:58
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:793
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:790
#define TARGET_ADDR_FMT
Definition: types.h:286
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define TARGET_PRIxADDR
Definition: types.h:284
#define NULL
Definition: usb.h:16
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22