2021-05-25 16:56:02 +08:00
|
|
|
#ifndef __FEL_H__
|
|
|
|
#define __FEL_H__
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-05-25 23:44:28 +08:00
|
|
|
#include <x.h>
|
2023-07-11 21:54:54 +08:00
|
|
|
#include <misc.h>
|
2021-06-04 17:11:28 +08:00
|
|
|
#include <progress.h>
|
2021-05-25 16:56:02 +08:00
|
|
|
|
2021-05-25 23:44:28 +08:00
|
|
|
struct xfel_ctx_t;
|
|
|
|
struct chip_t;
|
2021-05-25 16:56:02 +08:00
|
|
|
|
|
|
|
struct xfel_ctx_t {
|
|
|
|
libusb_device_handle * hdl;
|
|
|
|
int epout;
|
|
|
|
int epin;
|
2021-05-25 23:44:28 +08:00
|
|
|
struct {
|
2021-05-26 17:22:48 +08:00
|
|
|
char magic[8];
|
2021-05-25 23:44:28 +08:00
|
|
|
uint32_t id;
|
2021-05-26 17:22:48 +08:00
|
|
|
uint32_t firmware;
|
2021-05-25 23:44:28 +08:00
|
|
|
uint16_t protocol;
|
2021-05-26 17:22:48 +08:00
|
|
|
uint8_t dflag;
|
|
|
|
uint8_t dlength;
|
2021-05-25 23:44:28 +08:00
|
|
|
uint32_t scratchpad;
|
2021-05-26 17:22:48 +08:00
|
|
|
uint8_t pad[8];
|
2021-05-25 23:44:28 +08:00
|
|
|
} version;
|
2021-05-25 16:56:02 +08:00
|
|
|
struct chip_t * chip;
|
|
|
|
};
|
|
|
|
|
2021-05-25 23:44:28 +08:00
|
|
|
struct chip_t {
|
|
|
|
char * name;
|
2021-12-31 16:40:44 +08:00
|
|
|
int (*detect)(struct xfel_ctx_t * ctx, uint32_t id);
|
2021-05-25 23:44:28 +08:00
|
|
|
int (*reset)(struct xfel_ctx_t * ctx);
|
2021-06-25 14:16:57 +08:00
|
|
|
int (*sid)(struct xfel_ctx_t * ctx, char * sid);
|
2021-05-25 23:44:28 +08:00
|
|
|
int (*jtag)(struct xfel_ctx_t * ctx);
|
2021-05-26 11:26:25 +08:00
|
|
|
int (*ddr)(struct xfel_ctx_t * ctx, const char * type);
|
2021-06-13 01:29:25 +08:00
|
|
|
int (*spi_init)(struct xfel_ctx_t * ctx, uint32_t * swapbuf, uint32_t * swaplen, uint32_t * cmdlen);
|
2021-06-13 01:04:33 +08:00
|
|
|
int (*spi_run)(struct xfel_ctx_t * ctx, uint8_t * cbuf, uint32_t clen);
|
2023-05-16 16:55:39 +08:00
|
|
|
int (*extra)(struct xfel_ctx_t * ctx, int argc, char * argv[]);
|
2021-06-12 14:09:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2021-06-12 20:16:15 +08:00
|
|
|
SPI_CMD_END = 0x00,
|
|
|
|
SPI_CMD_INIT = 0x01,
|
|
|
|
SPI_CMD_SELECT = 0x02,
|
|
|
|
SPI_CMD_DESELECT = 0x03,
|
2021-06-12 21:02:21 +08:00
|
|
|
SPI_CMD_FAST = 0x04,
|
|
|
|
SPI_CMD_TXBUF = 0x05,
|
|
|
|
SPI_CMD_RXBUF = 0x06,
|
|
|
|
SPI_CMD_SPINOR_WAIT = 0x07,
|
|
|
|
SPI_CMD_SPINAND_WAIT = 0x08,
|
2021-05-25 23:44:28 +08:00
|
|
|
};
|
|
|
|
|
2021-06-08 16:58:27 +08:00
|
|
|
/*
|
|
|
|
* This R32 and W32 macro can only be used for byte access address, Don't used for address
|
|
|
|
* that can only support word access. Because the fel protocol can only support
|
|
|
|
* byte operation, VERY IMPORTANT !!!
|
|
|
|
*/
|
2021-06-05 15:03:19 +08:00
|
|
|
#define R32(reg) fel_read32(ctx, reg)
|
2021-05-26 10:49:30 +08:00
|
|
|
#define W32(reg, val) fel_write32(ctx, reg, val)
|
2021-05-25 23:44:28 +08:00
|
|
|
|
2021-12-31 16:40:44 +08:00
|
|
|
static inline int fel_chip_detect(struct xfel_ctx_t * ctx, uint32_t id)
|
|
|
|
{
|
2023-05-16 16:55:39 +08:00
|
|
|
if(ctx->chip->detect)
|
|
|
|
return ctx->chip->detect(ctx, id);
|
|
|
|
return 0;
|
2021-12-31 16:40:44 +08:00
|
|
|
}
|
|
|
|
|
2021-06-10 18:16:11 +08:00
|
|
|
static inline int fel_chip_reset(struct xfel_ctx_t * ctx)
|
|
|
|
{
|
2023-05-16 16:55:39 +08:00
|
|
|
if(ctx->chip->reset)
|
|
|
|
return ctx->chip->reset(ctx);
|
|
|
|
return 0;
|
2021-06-10 18:16:11 +08:00
|
|
|
}
|
|
|
|
|
2021-06-25 14:16:57 +08:00
|
|
|
static inline int fel_chip_sid(struct xfel_ctx_t * ctx, char * sid)
|
2021-06-10 18:16:11 +08:00
|
|
|
{
|
2023-05-16 16:55:39 +08:00
|
|
|
if(ctx->chip->sid)
|
|
|
|
return ctx->chip->sid(ctx, sid);
|
|
|
|
return 0;
|
2021-06-10 18:16:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fel_chip_jtag(struct xfel_ctx_t * ctx)
|
|
|
|
{
|
2023-05-16 16:55:39 +08:00
|
|
|
if(ctx->chip->jtag)
|
|
|
|
return ctx->chip->jtag(ctx);
|
|
|
|
return 0;
|
2021-06-10 18:16:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fel_chip_ddr(struct xfel_ctx_t * ctx, const char * type)
|
|
|
|
{
|
2023-05-16 16:55:39 +08:00
|
|
|
if(ctx->chip->ddr)
|
|
|
|
return ctx->chip->ddr(ctx, type);
|
|
|
|
return 0;
|
2021-06-10 18:16:11 +08:00
|
|
|
}
|
|
|
|
|
2021-06-13 01:29:25 +08:00
|
|
|
static inline int fel_chip_spi_init(struct xfel_ctx_t * ctx, uint32_t * swapbuf, uint32_t * swaplen, uint32_t * cmdlen)
|
2021-06-10 18:16:11 +08:00
|
|
|
{
|
2023-05-16 16:55:39 +08:00
|
|
|
if(ctx->chip->spi_init)
|
|
|
|
return ctx->chip->spi_init(ctx, swapbuf, swaplen, cmdlen);
|
|
|
|
return 0;
|
2021-06-10 18:16:11 +08:00
|
|
|
}
|
|
|
|
|
2021-06-13 01:04:33 +08:00
|
|
|
static inline int fel_chip_spi_run(struct xfel_ctx_t * ctx, uint8_t * cbuf, uint32_t clen)
|
2021-06-10 18:16:11 +08:00
|
|
|
{
|
2023-05-16 16:55:39 +08:00
|
|
|
if(ctx->chip->spi_run)
|
|
|
|
return ctx->chip->spi_run(ctx, cbuf, clen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fel_chip_extra(struct xfel_ctx_t * ctx, int argc, char * argv[])
|
|
|
|
{
|
|
|
|
if(ctx->chip->extra)
|
|
|
|
return ctx->chip->extra(ctx, argc, argv);
|
|
|
|
return 0;
|
2021-06-10 18:16:11 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 10:03:58 +08:00
|
|
|
int fel_init(struct xfel_ctx_t * ctx);
|
2021-05-25 23:44:28 +08:00
|
|
|
void fel_exec(struct xfel_ctx_t * ctx, uint32_t addr);
|
2021-06-05 15:03:19 +08:00
|
|
|
uint32_t fel_read32(struct xfel_ctx_t * ctx, uint32_t addr);
|
2021-05-25 23:44:28 +08:00
|
|
|
void fel_write32(struct xfel_ctx_t * ctx, uint32_t addr, uint32_t val);
|
2021-06-05 15:03:19 +08:00
|
|
|
void fel_read(struct xfel_ctx_t * ctx, uint32_t addr, void * buf, size_t len);
|
|
|
|
void fel_write(struct xfel_ctx_t * ctx, uint32_t addr, void * buf, size_t len);
|
|
|
|
void fel_read_progress(struct xfel_ctx_t * ctx, uint32_t addr, void * buf, size_t len);
|
|
|
|
void fel_write_progress(struct xfel_ctx_t * ctx, uint32_t addr, void * buf, size_t len);
|
2021-06-13 01:29:25 +08:00
|
|
|
int fel_spi_init(struct xfel_ctx_t * ctx, uint32_t * swapbuf, uint32_t * swaplen, uint32_t * cmdlen);
|
|
|
|
int fel_spi_xfer(struct xfel_ctx_t * ctx, uint32_t swapbuf, uint32_t swaplen, uint32_t cmdlen, void * txbuf, uint32_t txlen, void * rxbuf, uint32_t rxlen);
|
2021-05-25 23:44:28 +08:00
|
|
|
|
2021-05-25 16:56:02 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* __FEL_H__ */
|