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
55 #define BL602_DEFAULT_TIMEOUT_MS 5000
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,
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,
106 .idcode = 0x20000e05,
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,
118 uint32_t arg_data[],
unsigned int n_args, uint32_t *return_data,
unsigned int timeout_ms)
120 static const char *
const reg_names[] = {
"a0",
"a1",
"a2",
"a3",
"a4",
"a5" };
122 unsigned int n_reg_params = 0;
128 uint32_t trampoline_code[] = {
133 &trampoline_algorithm);
135 LOG_ERROR(
"No working area available, can't do trampoline");
140 sizeof(trampoline_code), (uint8_t *)trampoline_code);
142 LOG_ERROR(
"Writing trampoline code failed, can't do trampoline");
147 if (!return_data || n_args > 0) {
150 buf_set_u32(reg_params[0].value, 0, 32, arg_data[0]);
156 for (
unsigned int i = 1; i < n_args; ++i) {
158 buf_set_u32(reg_params[i].value, 0, 32, arg_data[i]);
168 n_reg_params, reg_params,
175 trampoline_algorithm->
address, retval);
180 *return_data =
buf_get_u32(reg_params[0].value, 0, 32);
183 for (
unsigned int i = 0; i < n_reg_params; i++)
191 uint32_t arg_data[],
unsigned int n_args, uint32_t *return_data,
unsigned int timeout_ms)
209 if (avail_pages == 0) {
210 LOG_ERROR(
"Not enough space for bounce buffer. Can't continue");
216 unsigned int chunk_size =
MIN(
MAX(avail_pages - 1, 1) *
priv->dev->pagesize,
count);
219 LOG_ERROR(
"Could not allocate bounce buffer for flash manipulation. Can't continue");
237 LOG_ERROR(
"No working area available, can't alloc sflash cfg");
242 sizeof(
priv->sflash_cfg),
priv->sflash_cfg);
254 uint32_t flash_pin_cfg = 0;
260 uint32_t device_info;
262 if (retval !=
ERROR_OK || device_info == 0x0) {
265 LOG_ERROR(
"Failed to read device info from eFuse");
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;
273 flash_pin_cfg = (flash_cfg << 2) | sf_swap_cfg;
275 flash_pin_cfg = sf_swap_cfg + (sf_reverse_cfg != 0 ? 5 : 1);
281 if (retval !=
ERROR_OK || sw_usage == 0x0) {
284 LOG_ERROR(
"Failed to read sw usage from eFuse");
289 uint32_t sf_swap_cfg = (sw_usage >> 16) & 0x03;
290 flash_pin_cfg = sf_swap_cfg;
302 LOG_ERROR(
"Failed to invoke spi flash gpio init function");
310 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00
317 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
318 0xe1, 0xfd, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00,
319 0x00, 0x00, 0x01, 0x21,
334 const uint8_t *sflash_ctrl_cfg =
NULL;
335 uint16_t sflash_ctrl_cfg_size = 0;
337 switch (part_info->
series) {
348 LOG_ERROR(
"No SFlash Control Config available for this chip");
354 &sflash_ctrl_cfg_area);
356 LOG_WARNING(
"No working area available, can't init SFlash");
361 sflash_ctrl_cfg_size, sflash_ctrl_cfg);
363 LOG_ERROR(
"Failed to write SFlash Control CFG into working area");
374 LOG_ERROR(
"Failed to invoke SFlash init function");
393 LOG_ERROR(
"No working area available, can't read flash id");
400 LOG_ERROR(
"Failed to write data required for flash id read");
412 LOG_ERROR(
"Failed to invoke get jedec id function");
418 LOG_ERROR(
"Failed to read flash id from target");
422 *jedec_id &= 0x00FFFFFF;
447 uint32_t return_value;
449 for (
unsigned int sector = first; sector <= last; sector++) {
450 LOG_INFO(
"Erasing sector %d", sector);
458 LOG_ERROR(
"Failed to invoke flash erase code on target");
461 if (return_value != 0) {
462 LOG_ERROR(
"Erase flash function returned wrong value: %02X", return_value);
481 LOG_DEBUG(
"bank->size=0x%x offset=0x%08" PRIx32
" count=0x%08" PRIx32,
490 LOG_WARNING(
"Write past end of flash. Extra data discarded");
502 unsigned int chunk_size = bounce_area->
size;
503 uint32_t return_value;
506 uint32_t write_size =
count > chunk_size ? chunk_size :
count;
507 LOG_INFO(
"Writing %d bytes to offset 0x%" PRIx32, write_size,
offset);
510 LOG_ERROR(
"Could not load data into target bounce buffer");
523 LOG_ERROR(
"Failed to invoke flash programming code on target");
526 if (return_value != 0) {
527 LOG_ERROR(
"Write flash function returned wrong value: %02X", return_value);
571 unsigned int chunk_size = bounce_area->
size;
574 uint32_t read_size =
count > chunk_size ? chunk_size :
count;
584 uint32_t return_value;
588 LOG_ERROR(
"Failed to invoke flash read code on target");
591 if (return_value != 0) {
592 LOG_ERROR(
"Read flash function returned wrong value: %02X", return_value);
599 LOG_ERROR(
"Could not load data from target bounce buffer");
625 priv->probed =
false;
631 LOG_ERROR(
"TAP IDCODE is not valid for selected chip type");
634 priv->part_info = part_info;
639 if (!
priv->part_info) {
640 LOG_ERROR(
"Cannot identify target as an BL602 family device");
646 LOG_ERROR(
"Initialization of flash failed");
653 LOG_ERROR(
"Cannot identify flash JEDEC ID");
659 if (p->device_id == jedec_id) {
666 LOG_ERROR(
"Unknown flash device (ID 0x%08" PRIx32
")", jedec_id);
669 LOG_INFO(
"Found flash device '%s' (ID 0x%08" PRIx32
")",
priv->dev->name,
670 priv->dev->device_id);
673 bank->size =
priv->dev->size_in_bytes;
675 bank->write_start_alignment = 8;
676 bank->write_end_alignment = 8;
714 priv->probed =
false;
717 if (strcmp(
CMD_ARGV[6],
"bl602") == 0) {
719 }
else if (strcmp(
CMD_ARGV[6],
"bl702") == 0) {
721 }
else if (strcmp(
CMD_ARGV[6],
"bl702l") == 0) {
752 .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))
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
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.