16 #define JTAGSPI_MAX_TIMEOUT 3000
38 LOG_ERROR(
"no memory for flash bank info");
55 uint8_t buf[4] = { 0 };
65 static void flip_u8(
const uint8_t *in, uint8_t *out,
unsigned int len)
67 for (
unsigned int i = 0; i < len; i++)
72 uint8_t *write_buffer,
unsigned int write_len, uint8_t *data_buffer,
int data_len)
74 assert(write_buffer || write_len == 0);
75 assert(data_buffer || data_len == 0);
79 LOG_DEBUG(
"cmd=0x%02x write_len=%d data_len=%d",
cmd, write_len, data_len);
82 const bool is_read = (data_len < 0);
87 const uint8_t marker = 1;
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;
110 flip_u8(write_buffer, write_buffer, write_len);
111 fields[n].
num_bits = write_len * CHAR_BIT;
127 flip_u8(data_buffer, data_buffer, data_len);
131 fields[n].
num_bits = data_len * CHAR_BIT;
142 flip_u8(data_buffer, data_buffer, data_len);
152 unsigned int index = 1;
172 bank->num_sectors = 0;
176 info->always_4byte =
false;
177 info->probed =
false;
179 memset(&
info->dev, 0,
sizeof(
info->dev));
182 info->devname[
sizeof(
info->devname) - 1] =
'\0';
185 info->dev.size_in_bytes = temp;
186 if ((temp & (temp - 1)) || (temp < (1UL << 8))) {
192 info->dev.pagesize = temp;
193 if (
info->dev.pagesize == 0)
195 if ((temp & (temp - 1)) || (temp >
info->dev.size_in_bytes)) {
201 if ((
info->dev.read_cmd != 0x03) &&
202 (
info->dev.read_cmd != 0x13)) {
210 if ((
info->dev.pprog_cmd != 0x02) &&
211 (
info->dev.pprog_cmd != 0x12)) {
220 info->dev.chip_erase_cmd = 0x00;
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))) {
239 info->dev.erase_cmd = 0x00;
240 info->dev.sectorsize =
info->dev.size_in_bytes;
249 bank->size =
info->dev.size_in_bytes;
252 if (
bank->size <= (1UL << 8))
254 else if (
bank->size <= (1UL << 16))
256 else if (
bank->size <= (1UL << 24))
260 LOG_WARNING(
"4-byte addresses needed, might need extra command to enable");
265 info->dev.size_in_bytes /
info->dev.sectorsize;
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;
279 bank->sectors = sectors;
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);
285 LOG_INFO(
"flash \'%s\' id = unknown\nflash size = %" PRIu32
" bytes",
286 info->dev.name,
info->dev.size_in_bytes);
295 unsigned int index = 1;
297 uint8_t num_write, num_read, write_buffer[max], read_buffer[1 << CHAR_BIT];
299 char temp[4],
output[(2 + max + (1 << CHAR_BIT)) * 3 + 8];
310 if (num_write > max) {
311 LOG_ERROR(
"at most %d bytes may be send", max);
322 for (ptr = &write_buffer[0] ; index <
CMD_ARGC; index++) {
325 snprintf(temp,
sizeof(temp),
"%02" PRIx8
" ", data);
331 ptr = &read_buffer[0];
332 jtagspi_cmd(
bank, write_buffer[0], &write_buffer[1], num_write - 1, ptr, -num_read);
336 for ( ; num_read > 0; num_read--) {
337 snprintf(temp,
sizeof(temp),
"%02" PRIx8
" ", *ptr++);
382 info->probed =
false;
384 if (!
bank->target->tap) {
394 memset(&
info->dev, 0,
sizeof(
info->dev));
397 memcpy(&
info->dev, p,
sizeof(
info->dev));
402 LOG_ERROR(
"Unknown flash device (ID 0x%06" PRIx32
")",
id & 0xFFFFFF);
406 LOG_INFO(
"Found flash device \'%s\' (ID 0x%06" PRIx32
")",
407 info->dev.name,
info->dev.device_id & 0xFFFFFF);
410 bank->size =
info->dev.size_in_bytes;
413 if (
bank->size <= (1UL << 8))
415 else if (
bank->size <= (1UL << 16))
417 else if (
bank->size <= (1UL << 24))
421 LOG_WARNING(
"4-byte addresses needed, might need extra command to enable");
426 info->dev.sectorsize :
info->dev.size_in_bytes;
436 for (
unsigned int sector = 0; sector <
bank->num_sectors; sector++) {
443 bank->sectors = sectors;
476 uint32_t
status = (uint32_t)-1;
486 }
while (dt <= timeout_ms);
496 uint32_t
status = (uint32_t)-1;
502 LOG_ERROR(
"Cannot enable write to flash. Status=0x%02" PRIx32,
status);
514 if (
info->dev.chip_erase_cmd == 0x00)
544 uint8_t
addr[
sizeof(uint32_t)];
570 LOG_DEBUG(
"erase from sector %u to sector %u", first, last);
572 if ((last < first) || (last >=
bank->num_sectors)) {
577 if (!(
info->probed)) {
582 for (
unsigned int sector = first; sector <= last; sector++) {
583 if (
bank->sectors[sector].is_protected) {
584 LOG_ERROR(
"Flash sector %u protected", sector);
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) {
597 LOG_WARNING(
"Bulk flash erase failed. Falling back to sector erase.");
600 if (
info->dev.erase_cmd == 0x00)
603 for (
unsigned int sector = first; sector <= last; sector++) {
617 for (
unsigned int sector = first; sector <= last; sector++)
618 bank->sectors[sector].is_protected = set;
625 uint32_t pagesize, currsize;
626 uint8_t
addr[
sizeof(uint32_t)];
629 if (!(
info->probed)) {
635 pagesize =
info->dev.sectorsize ?
info->dev.sectorsize :
info->dev.pagesize;
641 unsigned int addr_len = ((
info->dev.read_cmd != 0x03) &&
info->always_4byte) ? 4 :
info->addr_len;
645 currsize = ((
offset + pagesize) & ~(pagesize - 1)) -
offset;
647 currsize = (
count < currsize) ?
count : currsize;
666 uint8_t
addr[
sizeof(uint32_t)];
674 unsigned int addr_len = ((
info->dev.read_cmd != 0x03) &&
info->always_4byte) ? 4 :
info->addr_len;
686 uint32_t pagesize, currsize;
689 if (!(
info->probed)) {
699 currsize = ((
offset + pagesize) & ~(pagesize - 1)) -
offset;
701 currsize = (
count < currsize) ?
count : currsize;
720 if (!(
info->probed)) {
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,
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);
746 .handler = jtagspi_handle_set,
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.",
754 .handler = jtagspi_handle_cmd,
756 .usage =
"bank_id num_resp cmd_byte ...",
757 .help =
"Send low-level command cmd_byte and following bytes, read num_bytes.",
760 .name =
"always_4byte",
761 .handler = jtagspi_handle_always_4byte,
763 .usage =
"bank_id [ on | off ]",
764 .help =
"Use always 4-byte address except for basic 0x03.",
774 .help =
"jtagspi command group",
784 .flash_bank_command = jtagspi_flash_bank_command,
uint32_t flip_u32(uint32_t value, unsigned int num)
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.
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
void command_print(struct command_invocation *cmd, const char *format,...)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
#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...
#define COMMAND_PARSE_BOOL(in, out, on, off)
Parse the string as a binary parameter, storing the boolean value in out.
#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...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_FLASH_OPER_UNSUPPORTED
#define ERROR_FLASH_SECTOR_INVALID
#define ERROR_FLASH_BANK_NOT_PROBED
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.
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
unsigned jtag_tap_count_enabled(void)
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.
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.
The JTAG interface can be implemented with a software or hardware fifo.
void alive_sleep(uint64_t ms)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
char id[RTT_CB_MAX_ID_LENGTH]
Control block identifier.
const struct flash_device flash_devices[]
#define SPIFLASH_READ_STATUS
#define SPIFLASH_WRITE_ENABLE
#define SPIFLASH_DEF_PAGESIZE
When run_command is called, a new instance will be created on the stack, filled with the proper value...
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...
Describes the geometry and status of a single flash sector within a flash bank.
int is_erased
Indication of erasure status: 0 = not erased, 1 = erased, other = unknown.
uint32_t offset
Bus offset from start of the flash chip (in bytes).
int is_protected
Indication of protection status: 0 = unprotected/unlocked, 1 = protected/locked, other = unknown.
uint32_t size
Number of bytes in this flash sector.
This structure defines a single scan field in the scan.
int num_bits
The number of bits this field specifies.
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
const uint8_t * out_value
A pointer to value to be scanned into the device.
static void h_u32_to_be(uint8_t *buf, uint32_t val)
static uint32_t le_to_h_u24(const uint8_t *buf)
static struct ublast_lowlevel_priv info