17 #define JTAGSPI_MAX_TIMEOUT 3000
39 if (strcmp(
CMD_ARGV[6],
"-pld") == 0) {
44 bool has_jtagspi_instruction =
false;
48 if (!has_jtagspi_instruction) {
64 LOG_ERROR(
"no memory for flash bank info");
70 if (!
bank->target->tap) {
86 uint8_t buf[4] = { 0 };
96 static void flip_u8(
const uint8_t *in, uint8_t *out,
unsigned int len)
98 for (
unsigned int i = 0; i < len; i++)
103 uint8_t *write_buffer,
unsigned int write_len, uint8_t *data_buffer,
int data_len)
105 assert(write_buffer || write_len == 0);
106 assert(data_buffer || data_len == 0);
111 LOG_DEBUG(
"cmd=0x%02x write_len=%d data_len=%d",
cmd, write_len, data_len);
114 const bool is_read = (data_len < 0);
116 data_len = -data_len;
118 unsigned int facing_read_bits = 0;
119 unsigned int trailing_write_bits = 0;
121 if (
info->pld_device) {
128 const uint8_t marker = 1;
129 uint8_t xfer_bits[4];
130 if (!
info->pld_device) {
139 h_u32_to_be(xfer_bits, ((
sizeof(
cmd) + write_len + data_len) * CHAR_BIT) - 1);
140 flip_u8(xfer_bits, xfer_bits,
sizeof(xfer_bits));
141 fields[n].
num_bits =
sizeof(xfer_bits) * CHAR_BIT;
154 flip_u8(write_buffer, write_buffer, write_len);
155 fields[n].
num_bits = write_len * CHAR_BIT;
163 if (facing_read_bits) {
164 fields[n].
num_bits = facing_read_bits;
173 flip_u8(data_buffer, data_buffer, data_len);
177 fields[n].
num_bits = data_len * CHAR_BIT;
180 if (!is_read && trailing_write_bits) {
181 fields[n].
num_bits = trailing_write_bits;
187 if (
info->pld_device) {
202 flip_u8(data_buffer, data_buffer, data_len);
204 if (
info->pld_device)
215 unsigned int index = 1;
240 bank->num_sectors = 0;
244 info->always_4byte =
false;
245 info->probed =
false;
247 memset(&
info->dev, 0,
sizeof(
info->dev));
250 info->devname[
sizeof(
info->devname) - 1] =
'\0';
253 info->dev.size_in_bytes = temp;
254 if ((temp & (temp - 1)) || (temp < (1UL << 8))) {
260 info->dev.pagesize = temp;
261 if (
info->dev.pagesize == 0)
263 if ((temp & (temp - 1)) || (temp >
info->dev.size_in_bytes)) {
269 if ((
info->dev.read_cmd != 0x03) &&
270 (
info->dev.read_cmd != 0x13)) {
278 if ((
info->dev.pprog_cmd != 0x02) &&
279 (
info->dev.pprog_cmd != 0x12)) {
288 info->dev.chip_erase_cmd = 0x00;
292 info->dev.sectorsize = temp;
293 if ((
info->dev.sectorsize >
info->dev.size_in_bytes) ||
294 (
info->dev.sectorsize <
info->dev.pagesize) || (temp & (temp - 1))) {
307 info->dev.erase_cmd = 0x00;
308 info->dev.sectorsize =
info->dev.size_in_bytes;
317 bank->size =
info->dev.size_in_bytes;
320 if (
bank->size <= (1UL << 8))
322 else if (
bank->size <= (1UL << 16))
324 else if (
bank->size <= (1UL << 24))
328 LOG_WARNING(
"4-byte addresses needed, might need extra command to enable");
333 info->dev.size_in_bytes /
info->dev.sectorsize;
340 for (
unsigned int sector = 0; sector <
bank->num_sectors; sector++) {
341 sectors[sector].
offset = sector * (
info->dev.sectorsize);
342 sectors[sector].
size =
info->dev.sectorsize;
347 bank->sectors = sectors;
349 if (
info->dev.size_in_bytes / 4096)
350 LOG_INFO(
"flash \'%s\' id = unknown\nflash size = %" PRIu32
" kbytes",
351 info->dev.name,
info->dev.size_in_bytes / 1024);
353 LOG_INFO(
"flash \'%s\' id = unknown\nflash size = %" PRIu32
" bytes",
354 info->dev.name,
info->dev.size_in_bytes);
363 const unsigned int max = 20;
364 uint8_t cmd_byte, num_read, write_buffer[max], read_buffer[1 << CHAR_BIT];
372 if (num_write > max) {
390 for (
unsigned int i = 0; i < num_write; i++)
394 retval =
jtagspi_cmd(
bank, cmd_byte, write_buffer, num_write, read_buffer, -num_read);
400 for (
unsigned int i = 0; i < num_write; i++)
405 for (
unsigned int i = 0; i < num_read; i++)
448 info->probed =
false;
454 memset(&
info->dev, 0,
sizeof(
info->dev));
457 memcpy(&
info->dev, p,
sizeof(
info->dev));
462 LOG_ERROR(
"Unknown flash device (ID 0x%06" PRIx32
")",
id & 0xFFFFFF);
466 LOG_INFO(
"Found flash device \'%s\' (ID 0x%06" PRIx32
")",
467 info->dev.name,
info->dev.device_id & 0xFFFFFF);
470 bank->size =
info->dev.size_in_bytes;
473 if (
bank->size <= (1UL << 8))
475 else if (
bank->size <= (1UL << 16))
477 else if (
bank->size <= (1UL << 24))
481 LOG_WARNING(
"4-byte addresses needed, might need extra command to enable");
486 info->dev.sectorsize :
info->dev.size_in_bytes;
496 for (
unsigned int sector = 0; sector <
bank->num_sectors; sector++) {
503 bank->sectors = sectors;
536 uint32_t
status = (uint32_t)-1;
546 }
while (dt <= timeout_ms);
556 uint32_t
status = (uint32_t)-1;
562 LOG_ERROR(
"Cannot enable write to flash. Status=0x%02" PRIx32,
status);
574 if (
info->dev.chip_erase_cmd == 0x00)
604 uint8_t
addr[
sizeof(uint32_t)];
630 LOG_DEBUG(
"erase from sector %u to sector %u", first, last);
632 if ((last < first) || (last >=
bank->num_sectors)) {
637 if (!(
info->probed)) {
642 for (
unsigned int sector = first; sector <= last; sector++) {
643 if (
bank->sectors[sector].is_protected) {
644 LOG_ERROR(
"Flash sector %u protected", sector);
649 if (first == 0 && last == (
bank->num_sectors - 1) &&
650 info->dev.chip_erase_cmd != 0x00 &&
651 info->dev.chip_erase_cmd !=
info->dev.erase_cmd) {
657 LOG_WARNING(
"Bulk flash erase failed. Falling back to sector erase.");
660 if (
info->dev.erase_cmd == 0x00)
663 for (
unsigned int sector = first; sector <= last; sector++) {
677 for (
unsigned int sector = first; sector <= last; sector++)
678 bank->sectors[sector].is_protected = set;
685 uint32_t pagesize, currsize;
686 uint8_t
addr[
sizeof(uint32_t)];
689 if (!(
info->probed)) {
695 pagesize =
info->dev.sectorsize ?
info->dev.sectorsize :
info->dev.pagesize;
701 unsigned int addr_len = ((
info->dev.read_cmd != 0x03) &&
info->always_4byte) ? 4 :
info->addr_len;
705 currsize = ((
offset + pagesize) & ~(pagesize - 1)) -
offset;
707 currsize = (
count < currsize) ?
count : currsize;
726 uint8_t
addr[
sizeof(uint32_t)];
734 unsigned int addr_len = ((
info->dev.read_cmd != 0x03) &&
info->always_4byte) ? 4 :
info->addr_len;
746 uint32_t pagesize, currsize;
749 if (!(
info->probed)) {
759 currsize = ((
offset + pagesize) & ~(pagesize - 1)) -
offset;
761 currsize = (
count < currsize) ?
count : currsize;
780 if (!(
info->probed)) {
786 ", flash size = %" PRIu32
" %sbytes\n(page size = %" PRIu32
787 ", read = 0x%02" PRIx8
", qread = 0x%02" PRIx8
788 ", pprog = 0x%02" PRIx8
", mass_erase = 0x%02" PRIx8
789 ", sector size = %" PRIu32
" %sbytes, sector_erase = 0x%02" PRIx8
")",
790 info->dev.name,
info->dev.device_id & 0xFFFFFF,
792 bank->size / 4096 ?
"k" :
"",
info->dev.pagesize,
793 info->dev.read_cmd,
info->dev.qread_cmd,
794 info->dev.pprog_cmd,
info->dev.chip_erase_cmd,
795 info->dev.sectorsize / 4096 ?
796 info->dev.sectorsize / 1024 :
info->dev.sectorsize,
797 info->dev.sectorsize / 4096 ?
"k" :
"",
798 info->dev.erase_cmd);
806 .handler = jtagspi_handle_set,
808 .usage =
"bank_id name chip_size page_size read_cmd unused pprg_cmd "
809 "[ mass_erase_cmd ] [ sector_size sector_erase_cmd ]",
810 .help =
"Set device parameters if not autodetected.",
814 .handler = jtagspi_handle_cmd,
816 .usage =
"bank_id num_resp cmd_byte ...",
817 .help =
"Send low-level command cmd_byte and following bytes, read num_bytes.",
820 .name =
"always_4byte",
821 .handler = jtagspi_handle_always_4byte,
823 .usage =
"bank_id [ on | off ]",
824 .help =
"Use always 4-byte address except for basic 0x03.",
834 .help =
"jtagspi command group",
844 .flash_bank_command = jtagspi_flash_bank_command,
static const struct device_t * device
uint32_t flip_u32(uint32_t value, unsigned int num)
Inverts the ordering of bits inside a 32-bit word (e.g.
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.
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_COMMAND_ARGUMENT_INVALID
#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.
unsigned int jtag_tap_count_enabled(void)
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
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 ...)
int pld_has_jtagspi_instruction(struct pld_device *pld_device, bool *has_instruction)
struct pld_device * get_pld_device_by_name_or_numstr(const char *str)
int pld_get_jtagspi_stuff_bits(struct pld_device *pld_device, unsigned int *facing_read_bits, unsigned int *trailing_write_bits)
int pld_get_jtagspi_userircode(struct pld_device *pld_device, unsigned int *ir)
int pld_connect_spi_to_jtag(struct pld_device *pld_device)
int pld_disconnect_spi_from_jtag(struct pld_device *pld_device)
target_addr_t addr
Start address to search for the control block.
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.
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.
unsigned int num_bits
The number of bits this field specifies.
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