53 #define BITS_PER_BYTE 8
55 #define ATH79_REG_FS 0
56 #define ATH79_REG_CLOCK 4
57 #define ATH79_REG_WRITE 8
58 #define ATH79_REG_DATA 12
60 #define ATH79_SPI_CS_ALLHI 0x70000
61 #define ATH79_SPI_CS0_HI 0x10000
62 #define ATH79_SPI_CS1_HI 0x20000
63 #define ATH79_SPI_CS2_HI 0x40000
64 #define ATH79_SPI_CE_HI 0x00100
65 #define ATH79_SPI_DO_HI 0x00001
67 #define ATH79_XFER_FINAL 0x00000001
68 #define ATH79_XFER_PARTIAL 0x00000000
71 #define ATH79_MAX_TIMEOUT (3000)
95 {
"ATH79", 0x00000001, 0xbf000000 },
106 const uint32_t *instr,
109 for (
int i = 0; i < n; i++)
115 uint8_t *data,
int len,
121 uint32_t clock_low = cs_low;
122 uint32_t pracc_out = 0;
125 const uint32_t preamble1[] = {
137 const uint32_t pre_deselect[] = {
151 const uint32_t preamble2[] = {
167 for (
int i = 0; i < len; i++) {
172 int bit = ((x >> j) & 1);
210 const uint32_t post_deselect[] = {
230 return pracc_out / 4;
234 uint8_t *data,
int len,
int *transferred)
246 const int pracc_pre_post = 26;
247 const int pracc_loop_byte = 8 * 2 + 2;
253 int to_xfer = len > max_len ? max_len : len;
254 int partial_xfer = len != to_xfer;
255 int padded_len = (to_xfer + 3) & ~3;
256 uint32_t *out = malloc(padded_len);
266 LOG_DEBUG(
"ath79_spi_bitbang_bytes(%p, %08" PRIx32
", %p, %d)",
269 LOG_DEBUG(
"max code %d => max len %d. to_xfer %d",
273 ath79_info, &ctx, data, to_xfer, partial_xfer);
275 LOG_DEBUG(
"Assembled %d instructions, %d stores",
287 int missed_bytes = 4 - (to_xfer & 3);
303 for (
int i = 0; i < to_xfer; i++) {
305 data[i], ((uint8_t *)out)[i]);
308 memcpy(data, out, to_xfer);
309 *transferred = to_xfer;
325 uint8_t *data,
int len, uint32_t flags)
336 bank, data, len, &transferred);
354 if (CMD_ARGC < 6 || CMD_ARGC > 7)
358 if (strcmp(
CMD_ARGV[6],
"cs0") == 0)
360 else if (strcmp(
CMD_ARGV[6],
"cs1") == 0)
362 else if (strcmp(
CMD_ARGV[6],
"cs2") == 0)
377 bank->driver_priv = ath79_info;
391 bank, spi_bytes,
sizeof(spi_bytes),
434 bank, spi_bytes,
sizeof(spi_bytes),
446 LOG_ERROR(
"Cannot enable write to flash. Status=0x%08" PRIx32,
457 uint32_t
offset =
bank->sectors[sector].offset;
459 uint8_t spi_bytes[] = {
469 bank, spi_bytes,
sizeof(spi_bytes),
496 LOG_DEBUG(
"%s: from sector %u to sector %u", __func__, first, last);
503 if ((last < first) || (last >=
bank->num_sectors)) {
508 if (!ath79_info->
probed) {
516 for (
unsigned int sector = first; sector <= last; sector++) {
517 if (
bank->sectors[sector].is_protected) {
518 LOG_ERROR(
"Flash sector %u protected", sector);
523 for (
unsigned int sector = first; sector <= last; sector++) {
536 for (
unsigned int sector = first; sector <= last; sector++)
537 bank->sectors[sector].is_protected = set;
542 uint32_t address, uint32_t len)
545 uint8_t spi_bytes[] = {
552 uint32_t i, pagesize;
558 if (address & 0xff) {
559 LOG_ERROR(
"ath79_write_page: unaligned write address: %08" PRIx32,
564 LOG_ERROR(
"ath79_write_page: page buffer not initialized");
568 LOG_ERROR(
"ath79_write_page: len bigger than page size %" PRIu32
": %" PRIu32,
573 for (i = 0; i < len; i++) {
580 LOG_INFO(
"writing %" PRIu32
" bytes to flash page @0x%08" PRIx32, len, address);
592 bank, spi_bytes,
sizeof(spi_bytes),
604 uint32_t address, uint32_t len)
610 LOG_DEBUG(
"%s: address=0x%08" PRIx32
" len=0x%08" PRIx32,
611 __func__, address, len);
618 int page_len = len > page_size ? page_size : len;
626 address += page_size;
638 LOG_DEBUG(
"%s: offset=0x%08" PRIx32
" count=0x%08" PRIx32,
647 LOG_WARNING(
"Write pasts end of flash. Extra data discarded.");
652 for (
unsigned int sector = 0; sector <
bank->num_sectors; sector++) {
660 LOG_ERROR(
"Flash sector %u protected", sector);
669 uint32_t address, uint32_t len)
671 uint8_t spi_bytes[] = {
679 LOG_DEBUG(
"%s: address=0x%08" PRIx32
" len=0x%08" PRIx32,
680 __func__, address, len);
682 if (address & 0xff) {
683 LOG_ERROR(
"ath79_read_buffer: unaligned read address: %08" PRIx32,
688 LOG_INFO(
"reading %" PRIu32
" bytes from flash @0x%08" PRIx32, len, address);
707 LOG_DEBUG(
"%s: offset=0x%08" PRIx32
" count=0x%08" PRIx32,
716 LOG_WARNING(
"Reads past end of flash. Extra data discarded.");
742 *
id = (spi_bytes[1] << 0)
743 | (spi_bytes[2] << 8)
744 | (spi_bytes[3] << 16);
746 if (*
id == 0xffffff) {
760 uint32_t pagesize, sectorsize;
768 ath79_info->
probed =
false;
774 if (!target_device->
name) {
775 LOG_ERROR(
"Device ID 0x%" PRIx32
" is not known",
791 if (p->device_id ==
id) {
796 if (!ath79_info->
dev) {
797 LOG_ERROR(
"Unknown flash device (ID 0x%08" PRIx32
")",
id);
801 LOG_INFO(
"Found flash device \'%s\' (ID 0x%08" PRIx32
")",
806 if (
bank->size <= (1UL << 16))
807 LOG_WARNING(
"device needs 2-byte addresses - not implemented");
808 if (
bank->size > (1UL << 24))
809 LOG_WARNING(
"device needs paging or 4-byte addresses - not implemented");
833 for (
unsigned int sector = 0; sector <
bank->num_sectors; sector++) {
834 sectors[sector].
offset = sector * sectorsize;
835 sectors[sector].
size = sectorsize;
840 bank->sectors = sectors;
841 ath79_info->
probed =
true;
870 if (!ath79_info->
probed) {
876 " Device \'%s\' (ID 0x%08" PRIx32
")\n",
884 .flash_bank_command = ath79_flash_bank_command,
#define ATH79_MAX_TIMEOUT
static int read_flash_id(struct flash_bank *bank, uint32_t *id)
static int ath79_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
static int ath79_probe(struct flash_bank *bank)
static int ath79_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static int ath79_write_buffer(struct flash_bank *bank, const uint8_t *buffer, uint32_t address, uint32_t len)
static int get_ath79_info(struct flash_bank *bank, struct command_invocation *cmd)
static int ath79_write_enable(struct flash_bank *bank)
static int ath79_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
static int erase_command(struct flash_bank *bank, int sector)
static int ath79_write_page(struct flash_bank *bank, const uint8_t *buffer, uint32_t address, uint32_t len)
static const uint32_t ath79_chipselects[]
static int ath79_read_buffer(struct flash_bank *bank, uint8_t *buffer, uint32_t address, uint32_t len)
static int ath79_flash_blank_check(struct flash_bank *bank)
#define ATH79_SPI_CS_ALLHI
static void ath79_spi_bitbang_prepare(struct flash_bank *bank)
static int read_status_reg(struct flash_bank *bank, uint32_t *status)
static int ath79_protect_check(struct flash_bank *bank)
static int ath79_erase_sector(struct flash_bank *bank, int sector)
static int ath79_spi_bitbang_bytes(struct flash_bank *bank, uint8_t *data, int len, uint32_t flags)
static int ath79_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static const struct ath79_target target_devices[]
const struct flash_driver ath79_flash
static int wait_till_ready(struct flash_bank *bank, int timeout)
static int ath79_auto_probe(struct flash_bank *bank)
static int ath79_spi_bitbang_codegen(struct ath79_flash_bank *ath79_info, struct pracc_queue_info *ctx, uint8_t *data, int len, int partial_xfer)
FLASH_BANK_COMMAND_HANDLER(ath79_flash_bank_command)
static void ath79_pracc_addn(struct pracc_queue_info *ctx, const uint32_t *instr, int n)
#define ATH79_XFER_PARTIAL
static int ath79_spi_bitbang_chunk(struct flash_bank *bank, uint8_t *data, int len, int *transferred)
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
#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 ERROR_FLASH_OPER_UNSUPPORTED
#define ERROR_FLASH_SECTOR_INVALID
#define ERROR_FLASH_BANK_NOT_PROBED
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.
void alive_sleep(uint64_t ms)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_LEVEL_IS(FOO)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
static struct mips32_common * target_to_mips32(struct target *target)
#define MIPS32_XORI(isa, tar, src, val)
#define MIPS32_SW(isa, reg, off, base)
#define MIPS32_LUI(isa, reg, val)
#define MIPS32_ORI(isa, tar, src, val)
#define MIPS32_MFC0(isa, gpr, cpr, sel)
#define MIPS32_B(isa, off)
#define MIPS32_LW(isa, reg, off, base)
void pracc_queue_free(struct pracc_queue_info *ctx)
int mips32_pracc_queue_exec(struct mips_ejtag *ejtag_info, struct pracc_queue_info *ctx, uint32_t *buf, bool check_last)
void pracc_queue_init(struct pracc_queue_info *ctx)
void pracc_add(struct pracc_queue_info *ctx, uint32_t addr, uint32_t instr)
#define PRACC_UPPER_BASE_ADDR
#define MIPS32_PRACC_PARAM_OUT
#define PRACC_MAX_INSTRUCTIONS
static uint32_t bit(uint32_t value, unsigned int b)
const struct flash_device flash_devices[]
#define SPIFLASH_PAGE_PROGRAM
#define SPIFLASH_READ_STATUS
#define SPIFLASH_WRITE_ENABLE
#define SPIFLASH_DEF_PAGESIZE
const struct flash_device * dev
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.
uint32_t idcode
device identification code
struct mips_ejtag ejtag_info
struct mips_ejtag * ejtag_info
void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
#define ERROR_TARGET_NOT_HALTED
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.