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