| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#ifndef SPI_ENGINE_H_ |
| 40 |
#define SPI_ENGINE_H_ |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#define BIT(x) (1UL << (x)) |
| 46 |
|
| 47 |
#define SPI_ENGINE_VERSION_MAJOR(x) ((x >> 16) & 0xff) |
| 48 |
#define SPI_ENGINE_VERSION_MINOR(x) ((x >> 8) & 0xff) |
| 49 |
#define SPI_ENGINE_VERSION_PATCH(x) (x & 0xff) |
| 50 |
|
| 51 |
#define SPI_ENGINE_REG_VERSION 0x00 |
| 52 |
|
| 53 |
#define SPI_ENGINE_REG_RESET 0x40 |
| 54 |
|
| 55 |
#define SPI_ENGINE_REG_INT_ENABLE 0x80 |
| 56 |
#define SPI_ENGINE_REG_INT_PENDING 0x84 |
| 57 |
#define SPI_ENGINE_REG_INT_SOURCE 0x88 |
| 58 |
|
| 59 |
#define SPI_ENGINE_REG_SYNC_ID 0xc0 |
| 60 |
|
| 61 |
#define SPI_ENGINE_REG_CMD_FIFO_ROOM 0xd0 |
| 62 |
#define SPI_ENGINE_REG_SDO_FIFO_ROOM 0xd4 |
| 63 |
#define SPI_ENGINE_REG_SDI_FIFO_LEVEL 0xd8 |
| 64 |
|
| 65 |
#define SPI_ENGINE_REG_CMD_FIFO 0xe0 |
| 66 |
#define SPI_ENGINE_REG_SDO_DATA_FIFO 0xe4 |
| 67 |
#define SPI_ENGINE_REG_SDI_DATA_FIFO 0xe8 |
| 68 |
#define SPI_ENGINE_REG_SDI_DATA_FIFO_PEEK 0xec |
| 69 |
|
| 70 |
#define SPI_ENGINE_REG_OFFLOAD_CTRL(x) (0x100 + (0x20 * x)) |
| 71 |
#define SPI_ENGINE_REG_OFFLOAD_STATUS(x) (0x104 + (0x20 * x)) |
| 72 |
#define SPI_ENGINE_REG_OFFLOAD_RESET(x) (0x108 + (0x20 * x)) |
| 73 |
#define SPI_ENGINE_REG_OFFLOAD_CMD_MEM(x) (0x110 + (0x20 * x)) |
| 74 |
#define SPI_ENGINE_REG_OFFLOAD_SDO_MEM(x) (0x114 + (0x20 * x)) |
| 75 |
|
| 76 |
#define SPI_ENGINE_INT_CMD_ALMOST_EMPTY BIT(0) |
| 77 |
#define SPI_ENGINE_INT_SDO_ALMOST_EMPTY BIT(1) |
| 78 |
#define SPI_ENGINE_INT_SDI_ALMOST_FULL BIT(2) |
| 79 |
#define SPI_ENGINE_INT_SYNC BIT(3) |
| 80 |
|
| 81 |
#define SPI_ENGINE_OFFLOAD_CTRL_ENABLE BIT(0) |
| 82 |
#define SPI_ENGINE_OFFLOAD_STATUS_ENABLED BIT(0) |
| 83 |
|
| 84 |
#define SPI_ENGINE_CONFIG_CPHA BIT(0) |
| 85 |
#define SPI_ENGINE_CONFIG_CPOL BIT(1) |
| 86 |
#define SPI_ENGINE_CONFIG_3WIRE BIT(2) |
| 87 |
|
| 88 |
#define SPI_ENGINE_INST_TRANSFER 0x0 |
| 89 |
#define SPI_ENGINE_INST_ASSERT 0x1 |
| 90 |
#define SPI_ENGINE_INST_WRITE 0x2 |
| 91 |
#define SPI_ENGINE_INST_MISC 0x3 |
| 92 |
|
| 93 |
#define SPI_ENGINE_CMD_REG_CLK_DIV 0x0 |
| 94 |
#define SPI_ENGINE_CMD_REG_CONFIG 0x1 |
| 95 |
|
| 96 |
#define SPI_ENGINE_MISC_SYNC 0x0 |
| 97 |
#define SPI_ENGINE_MISC_SLEEP 0x1 |
| 98 |
|
| 99 |
#define SPI_ENGINE_CMD(inst, arg1, arg2) \ |
| 100 |
(((inst) << 12) | ((arg1) << 8) | (arg2)) |
| 101 |
|
| 102 |
#define SPI_ENGINE_CMD_TRANSFER(write, read, n) \ |
| 103 |
SPI_ENGINE_CMD(SPI_ENGINE_INST_TRANSFER, ((read) << 1 | (write)), (n)) |
| 104 |
#define SPI_ENGINE_CMD_ASSERT(delay, cs) \ |
| 105 |
SPI_ENGINE_CMD(SPI_ENGINE_INST_ASSERT, (delay), (cs)) |
| 106 |
#define SPI_ENGINE_CMD_WRITE(reg, val) \ |
| 107 |
SPI_ENGINE_CMD(SPI_ENGINE_INST_WRITE, (reg), (val)) |
| 108 |
#define SPI_ENGINE_CMD_SLEEP(delay) \ |
| 109 |
SPI_ENGINE_CMD(SPI_ENGINE_INST_MISC, SPI_ENGINE_MISC_SLEEP, (delay)) |
| 110 |
#define SPI_ENGINE_CMD_SYNC(id) \ |
| 111 |
SPI_ENGINE_CMD(SPI_ENGINE_INST_MISC, SPI_ENGINE_MISC_SYNC, (id)) |
| 112 |
|
| 113 |
#define min(x, y) (((x) < (y)) ? (x) : (y)) |
| 114 |
#define max(x, y) (((x) > (y)) ? (x) : (y)) |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
typedef struct { |
| 120 |
uint8_t chip_select; |
| 121 |
uint8_t cs_change; |
| 122 |
uint32_t spi_clk; |
| 123 |
uint32_t spi_config; |
| 124 |
uint8_t tx_length; |
| 125 |
uint8_t *tx_buf; |
| 126 |
uint8_t rx_length; |
| 127 |
uint8_t *rx_buf; |
| 128 |
} spi_message; |
| 129 |
|
| 130 |
typedef struct { |
| 131 |
uint32_t length; |
| 132 |
uint16_t instructions[]; |
| 133 |
} spi_engine_program; |
| 134 |
|
| 135 |
typedef struct { |
| 136 |
uint32_t baseaddr; |
| 137 |
uint32_t spi_ref_clk; |
| 138 |
uint8_t cmd_length; |
| 139 |
uint16_t *cmd_buf; |
| 140 |
uint32_t sync_id; |
| 141 |
uint32_t completed_id; |
| 142 |
uint32_t int_enable; |
| 143 |
spi_message *msg; |
| 144 |
spi_engine_program *p; |
| 145 |
} spi_engine; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
int32_t spi_engine_write(uint32_t reg_addr, |
| 151 |
uint32_t reg_data); |
| 152 |
int32_t spi_engine_setup(uint32_t baseaddr); |
| 153 |
int32_t spi_engine_transfer_one_message(spi_message *msg); |
| 154 |
void spi_engine_offload_enable(uint8_t enable); |
| 155 |
int32_t spi_engine_offload_load_msg(spi_message *msg); |
| 156 |
int32_t spi_engine_write_and_read(uint8_t ss, |
| 157 |
uint8_t *data, |
| 158 |
uint8_t bytes_number); |
| 159 |
#endif |