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
56 #define BL602_DEFAULT_TIMEOUT_MS 5000
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,
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,
107 .idcode = 0x20000e05,
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,
119 uint32_t arg_data[],
unsigned int n_args, uint32_t *return_data,
unsigned int timeout_ms)
121 static const char *
const reg_names[] = {
"a0",
"a1",
"a2",
"a3",
"a4",
"a5" };
123 unsigned int n_reg_params = 0;
129 uint32_t trampoline_code[] = {
134 &trampoline_algorithm);
136 LOG_ERROR(
"No working area available, can't do trampoline");
141 sizeof(trampoline_code), (uint8_t *)trampoline_code);
143 LOG_ERROR(
"Writing trampoline code failed, can't do trampoline");
148 if (!return_data || n_args > 0) {
151 buf_set_u32(reg_params[0].value, 0, 32, arg_data[0]);
157 for (
unsigned int i = 1; i < n_args; ++i) {
159 buf_set_u32(reg_params[i].value, 0, 32, arg_data[i]);
172 n_reg_params, reg_params,
175 timeout_ms, &algorithm_info);
179 trampoline_algorithm->
address, retval);
184 *return_data =
buf_get_u32(reg_params[0].value, 0, 32);
187 for (
unsigned int i = 0; i < n_reg_params; i++)
195 uint32_t arg_data[],
unsigned int n_args, uint32_t *return_data,
unsigned int timeout_ms)
213 if (avail_pages == 0) {
214 LOG_ERROR(
"Not enough space for bounce buffer. Can't continue");
220 unsigned int chunk_size = MIN(MAX(avail_pages - 1, 1) *
priv->dev->pagesize,
count);
223 LOG_ERROR(
"Could not allocate bounce buffer for flash manipulation. Can't continue");
241 LOG_ERROR(
"No working area available, can't alloc sflash cfg");
246 sizeof(
priv->sflash_cfg),
priv->sflash_cfg);
258 uint32_t flash_pin_cfg = 0;
264 uint32_t device_info;
266 if (retval !=
ERROR_OK || device_info == 0x0) {
269 LOG_ERROR(
"Failed to read device info from eFuse");
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;
277 flash_pin_cfg = (flash_cfg << 2) | sf_swap_cfg;
279 flash_pin_cfg = sf_swap_cfg + (sf_reverse_cfg != 0 ? 5 : 1);
285 if (retval !=
ERROR_OK || sw_usage == 0x0) {
288 LOG_ERROR(
"Failed to read sw usage from eFuse");
293 uint32_t sf_swap_cfg = (sw_usage >> 16) & 0x03;
294 flash_pin_cfg = sf_swap_cfg;
306 LOG_ERROR(
"Failed to invoke spi flash gpio init function");
314 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00
321 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
322 0xe1, 0xfd, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00,
323 0x00, 0x00, 0x01, 0x21,
338 const uint8_t *sflash_ctrl_cfg =
NULL;
339 uint16_t sflash_ctrl_cfg_size = 0;
341 switch (part_info->
series) {
352 LOG_ERROR(
"No SFlash Control Config available for this chip");
358 &sflash_ctrl_cfg_area);
360 LOG_WARNING(
"No working area available, can't init SFlash");
365 sflash_ctrl_cfg_size, sflash_ctrl_cfg);
367 LOG_ERROR(
"Failed to write SFlash Control CFG into working area");
378 LOG_ERROR(
"Failed to invoke SFlash init function");
397 LOG_ERROR(
"No working area available, can't read flash id");
404 LOG_ERROR(
"Failed to write data required for flash id read");
416 LOG_ERROR(
"Failed to invoke get jedec id function");
422 LOG_ERROR(
"Failed to read flash id from target");
426 *jedec_id &= 0x00FFFFFF;
451 uint32_t return_value;
453 for (
unsigned int sector = first; sector <= last; sector++) {
454 LOG_INFO(
"Erasing sector %d", sector);
462 LOG_ERROR(
"Failed to invoke flash erase code on target");
465 if (return_value != 0) {
466 LOG_ERROR(
"Erase flash function returned wrong value: %02X", return_value);
485 LOG_DEBUG(
"bank->size=0x%x offset=0x%08" PRIx32
" count=0x%08" PRIx32,
494 LOG_WARNING(
"Write past end of flash. Extra data discarded");
506 unsigned int chunk_size = bounce_area->
size;
507 uint32_t return_value;
510 uint32_t write_size =
count > chunk_size ? chunk_size :
count;
511 LOG_INFO(
"Writing %d bytes to offset 0x%" PRIx32, write_size,
offset);
514 LOG_ERROR(
"Could not load data into target bounce buffer");
527 LOG_ERROR(
"Failed to invoke flash programming code on target");
530 if (return_value != 0) {
531 LOG_ERROR(
"Write flash function returned wrong value: %02X", return_value);
575 unsigned int chunk_size = bounce_area->
size;
578 uint32_t read_size =
count > chunk_size ? chunk_size :
count;
588 uint32_t return_value;
592 LOG_ERROR(
"Failed to invoke flash read code on target");
595 if (return_value != 0) {
596 LOG_ERROR(
"Read flash function returned wrong value: %02X", return_value);
603 LOG_ERROR(
"Could not load data from target bounce buffer");
629 priv->probed =
false;
635 LOG_ERROR(
"TAP IDCODE is not valid for selected chip type");
638 priv->part_info = part_info;
643 if (!
priv->part_info) {
644 LOG_ERROR(
"Cannot identify target as an BL602 family device");
650 LOG_ERROR(
"Initialization of flash failed");
657 LOG_ERROR(
"Cannot identify flash JEDEC ID");
663 if (p->device_id == jedec_id) {
670 LOG_ERROR(
"Unknown flash device (ID 0x%08" PRIx32
")", jedec_id);
673 LOG_INFO(
"Found flash device '%s' (ID 0x%08" PRIx32
")",
priv->dev->name,
674 priv->dev->device_id);
677 bank->size =
priv->dev->size_in_bytes;
679 bank->write_start_alignment = 8;
680 bank->write_end_alignment = 8;
718 priv->probed =
false;
721 if (strcmp(
CMD_ARGV[6],
"bl602") == 0) {
723 }
else if (strcmp(
CMD_ARGV[6],
"bl702") == 0) {
725 }
else if (strcmp(
CMD_ARGV[6],
"bl702l") == 0) {
756 .flash_bank_command = bl602_flash_bank_command,
void init_reg_param(struct reg_param *param, const char *reg_name, uint32_t size, enum param_direction direction)
void destroy_reg_param(struct reg_param *param)
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.
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.
static int bl602_flash_gpio_init(struct flash_bank *bank)
#define SFLASH_CFG_SECTOR_SIZE_POS
static int bl602_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
#define SFLASH_CFG_BUSY_READ_REG_LEN_POS
#define SFLASH_CFG_FAST_READ_CMD_POS
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)
static int bl602_alloc_sflash_cfg(struct flash_bank *bank, struct working_area **working_area)
#define BL602_DEFAULT_TIMEOUT_MS
static int bl602_flash_read_id(struct flash_bank *bank, uint32_t *jedec_id)
static const uint8_t bl602_sflash_ctrl_cfg[]
#define SFLASH_CFG_READ_STATUS_REG1_POS
static int bl602_alloc_bounce_buffer(struct flash_bank *bank, struct working_area **working_area, uint32_t count)
#define SFLASH_CFG_JEDEC_ID_CMD_POS
static const uint8_t bl702l_sflash_ctrl_cfg[]
#define SFLASH_CFG_PAGE_SIZE_POS
#define SFLASH_CFG_WRITE_ENABLE_BIT_POS
static int bl602_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
#define SFLASH_CFG_JEDEC_ID_CMD_DMY_CLK_POS
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)
static int bl602_auto_probe(struct flash_bank *bank)
#define SFLASH_CFG_WRITE_ENABLE_CMD_POS
#define SFLASH_CFG_PAGE_PROGRAM_CMD_POS
static int bl602_flash_init(struct flash_bank *bank)
#define SFLASH_CFG_TIME_PAGE_PGM_POS
#define SFLASH_CFG_SECTOR_ERASE_CMD_POS
#define SFLASH_CFG_WRITE_ENABLE_READ_REG_LEN_POS
#define SFLASH_CFG_WRITE_ENABLE_INDEX_POS
static int bl602_probe(struct flash_bank *bank)
static int bl602_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
FLASH_BANK_COMMAND_HANDLER(bl602_flash_bank_command)
static const struct bl602_part_info bl602_parts[]
#define SFLASH_CFG_TIME_ERASE_SECTOR_POS
const struct flash_driver bl602_flash
#define SFLASH_CFG_BUSY_INDEX_POS
#define SFLASH_CFG_BUSY_BIT_POS
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
uint64_t buffer
Pointer to data buffer to send over SPI.
static struct esp_usb_jtag * priv
#define ERROR_FLASH_BANK_INVALID
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
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
static uint32_t ebreak(void) __attribute__((unused))
#define RISCV_COMMON_MAGIC
const struct flash_device flash_devices[]
const struct bl602_part_info * part_info
uint8_t sflash_cfg[SFLASH_CFG_SIZE]
const struct flash_device * dev
uint32_t romapi_sflash_init
uint32_t romapi_sflash_init_gpio
uint32_t romapi_sflash_erase_sector
uint32_t romapi_get_jedec_id
uint32_t romapi_sflash_read
uint32_t romapi_sflash_program
Provides details of a flash bank, available either on-chip or through a major interface.
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
uint32_t idcode
device identification code
enum param_direction direction
Architecture info for RISC-V algorithms.
unsigned int common_magic
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
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.
uint32_t target_get_working_area_avail(struct target *target)
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
#define ERROR_TARGET_NOT_HALTED
static bool target_was_examined(const struct target *target)
#define ERROR_TARGET_NOT_EXAMINED
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.