2 * QEMU model of the Xilinx Zynq SPI controller
4 * Copyright (c) 2012 Peter A. G. Crosthwaite
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "hw/sysbus.h"
27 #include "sysemu/sysemu.h"
28 #include "hw/ptimer.h"
30 #include "qemu/bitops.h"
31 #include "hw/ssi/xilinx_spips.h"
32 #include "qapi/error.h"
33 #include "hw/register.h"
34 #include "sysemu/dma.h"
35 #include "migration/blocker.h"
37 #ifndef XILINX_SPIPS_ERR_DEBUG
38 #define XILINX_SPIPS_ERR_DEBUG 0
41 #define DB_PRINT_L(level, ...) do { \
42 if (XILINX_SPIPS_ERR_DEBUG > (level)) { \
43 fprintf(stderr, ": %s: ", __func__); \
44 fprintf(stderr, ## __VA_ARGS__); \
49 #define R_CONFIG (0x00 / 4)
50 #define IFMODE (1U << 31)
51 #define R_CONFIG_ENDIAN (1 << 26)
52 #define MODEFAIL_GEN_EN (1 << 17)
53 #define MAN_START_COM (1 << 16)
54 #define MAN_START_EN (1 << 15)
55 #define MANUAL_CS (1 << 14)
56 #define CS (0xF << 10)
58 #define PERI_SEL (1 << 9)
59 #define REF_CLK (1 << 8)
60 #define FIFO_WIDTH (3 << 6)
61 #define BAUD_RATE_DIV (7 << 3)
62 #define CLK_PH (1 << 2)
63 #define CLK_POL (1 << 1)
64 #define MODE_SEL (1 << 0)
65 #define R_CONFIG_RSVD (0x7bf40000)
67 /* interrupt mechanism */
68 #define R_INTR_STATUS (0x04 / 4)
69 #define R_INTR_STATUS_RESET (0x104)
70 #define R_INTR_EN (0x08 / 4)
71 #define R_INTR_DIS (0x0C / 4)
72 #define R_INTR_MASK (0x10 / 4)
73 #define IXR_TX_FIFO_UNDERFLOW (1 << 6)
74 /* Poll timeout not implemented */
75 #define IXR_RX_FIFO_EMPTY (1 << 11)
76 #define IXR_GENERIC_FIFO_FULL (1 << 10)
77 #define IXR_GENERIC_FIFO_NOT_FULL (1 << 9)
78 #define IXR_TX_FIFO_EMPTY (1 << 8)
79 #define IXR_GENERIC_FIFO_EMPTY (1 << 7)
80 #define IXR_RX_FIFO_FULL (1 << 5)
81 #define IXR_RX_FIFO_NOT_EMPTY (1 << 4)
82 #define IXR_TX_FIFO_FULL (1 << 3)
83 #define IXR_TX_FIFO_NOT_FULL (1 << 2)
84 #define IXR_TX_FIFO_MODE_FAIL (1 << 1)
85 #define IXR_RX_FIFO_OVERFLOW (1 << 0)
86 #define IXR_ALL ((1 << 13) - 1)
87 #define GQSPI_IXR_MASK 0xFBE
88 #define IXR_SELF_CLEAR \
89 (IXR_GENERIC_FIFO_EMPTY \
90 | IXR_GENERIC_FIFO_FULL \
91 | IXR_GENERIC_FIFO_NOT_FULL \
94 | IXR_TX_FIFO_NOT_FULL \
97 | IXR_RX_FIFO_NOT_EMPTY)
99 #define R_EN (0x14 / 4)
100 #define R_DELAY (0x18 / 4)
101 #define R_TX_DATA (0x1C / 4)
102 #define R_RX_DATA (0x20 / 4)
103 #define R_SLAVE_IDLE_COUNT (0x24 / 4)
104 #define R_TX_THRES (0x28 / 4)
105 #define R_RX_THRES (0x2C / 4)
106 #define R_GPIO (0x30 / 4)
107 #define R_LPBK_DLY_ADJ (0x38 / 4)
108 #define R_LPBK_DLY_ADJ_RESET (0x33)
109 #define R_TXD1 (0x80 / 4)
110 #define R_TXD2 (0x84 / 4)
111 #define R_TXD3 (0x88 / 4)
113 #define R_LQSPI_CFG (0xa0 / 4)
114 #define R_LQSPI_CFG_RESET 0x03A002EB
115 #define LQSPI_CFG_LQ_MODE (1U << 31)
116 #define LQSPI_CFG_TWO_MEM (1 << 30)
117 #define LQSPI_CFG_SEP_BUS (1 << 29)
118 #define LQSPI_CFG_U_PAGE (1 << 28)
119 #define LQSPI_CFG_ADDR4 (1 << 27)
120 #define LQSPI_CFG_MODE_EN (1 << 25)
121 #define LQSPI_CFG_MODE_WIDTH 8
122 #define LQSPI_CFG_MODE_SHIFT 16
123 #define LQSPI_CFG_DUMMY_WIDTH 3
124 #define LQSPI_CFG_DUMMY_SHIFT 8
125 #define LQSPI_CFG_INST_CODE 0xFF
127 #define R_CMND (0xc0 / 4)
128 #define R_CMND_RXFIFO_DRAIN (1 << 19)
129 FIELD(CMND
, PARTIAL_BYTE_LEN
, 16, 3)
130 #define R_CMND_EXT_ADD (1 << 15)
131 FIELD(CMND
, RX_DISCARD
, 8, 7)
132 FIELD(CMND
, DUMMY_CYCLES
, 2, 6)
133 #define R_CMND_DMA_EN (1 << 1)
134 #define R_CMND_PUSH_WAIT (1 << 0)
135 #define R_TRANSFER_SIZE (0xc4 / 4)
136 #define R_LQSPI_STS (0xA4 / 4)
137 #define LQSPI_STS_WR_RECVD (1 << 1)
139 #define R_MOD_ID (0xFC / 4)
141 #define R_GQSPI_SELECT (0x144 / 4)
142 FIELD(GQSPI_SELECT
, GENERIC_QSPI_EN
, 0, 1)
143 #define R_GQSPI_ISR (0x104 / 4)
144 #define R_GQSPI_IER (0x108 / 4)
145 #define R_GQSPI_IDR (0x10c / 4)
146 #define R_GQSPI_IMR (0x110 / 4)
147 #define R_GQSPI_IMR_RESET (0xfbe)
148 #define R_GQSPI_TX_THRESH (0x128 / 4)
149 #define R_GQSPI_RX_THRESH (0x12c / 4)
150 #define R_GQSPI_GPIO (0x130 / 4)
151 #define R_GQSPI_LPBK_DLY_ADJ (0x138 / 4)
152 #define R_GQSPI_LPBK_DLY_ADJ_RESET (0x33)
153 #define R_GQSPI_CNFG (0x100 / 4)
154 FIELD(GQSPI_CNFG
, MODE_EN
, 30, 2)
155 FIELD(GQSPI_CNFG
, GEN_FIFO_START_MODE
, 29, 1)
156 FIELD(GQSPI_CNFG
, GEN_FIFO_START
, 28, 1)
157 FIELD(GQSPI_CNFG
, ENDIAN
, 26, 1)
158 /* Poll timeout not implemented */
159 FIELD(GQSPI_CNFG
, EN_POLL_TIMEOUT
, 20, 1)
160 /* QEMU doesnt care about any of these last three */
161 FIELD(GQSPI_CNFG
, BR
, 3, 3)
162 FIELD(GQSPI_CNFG
, CPH
, 2, 1)
163 FIELD(GQSPI_CNFG
, CPL
, 1, 1)
164 #define R_GQSPI_GEN_FIFO (0x140 / 4)
165 #define R_GQSPI_TXD (0x11c / 4)
166 #define R_GQSPI_RXD (0x120 / 4)
167 #define R_GQSPI_FIFO_CTRL (0x14c / 4)
168 FIELD(GQSPI_FIFO_CTRL
, RX_FIFO_RESET
, 2, 1)
169 FIELD(GQSPI_FIFO_CTRL
, TX_FIFO_RESET
, 1, 1)
170 FIELD(GQSPI_FIFO_CTRL
, GENERIC_FIFO_RESET
, 0, 1)
171 #define R_GQSPI_GFIFO_THRESH (0x150 / 4)
172 #define R_GQSPI_DATA_STS (0x15c / 4)
173 /* We use the snapshot register to hold the core state for the currently
174 * or most recently executed command. So the generic fifo format is defined
175 * for the snapshot register
177 #define R_GQSPI_GF_SNAPSHOT (0x160 / 4)
178 FIELD(GQSPI_GF_SNAPSHOT
, POLL
, 19, 1)
179 FIELD(GQSPI_GF_SNAPSHOT
, STRIPE
, 18, 1)
180 FIELD(GQSPI_GF_SNAPSHOT
, RECIEVE
, 17, 1)
181 FIELD(GQSPI_GF_SNAPSHOT
, TRANSMIT
, 16, 1)
182 FIELD(GQSPI_GF_SNAPSHOT
, DATA_BUS_SELECT
, 14, 2)
183 FIELD(GQSPI_GF_SNAPSHOT
, CHIP_SELECT
, 12, 2)
184 FIELD(GQSPI_GF_SNAPSHOT
, SPI_MODE
, 10, 2)
185 FIELD(GQSPI_GF_SNAPSHOT
, EXPONENT
, 9, 1)
186 FIELD(GQSPI_GF_SNAPSHOT
, DATA_XFER
, 8, 1)
187 FIELD(GQSPI_GF_SNAPSHOT
, IMMEDIATE_DATA
, 0, 8)
188 #define R_GQSPI_MOD_ID (0x1fc / 4)
189 #define R_GQSPI_MOD_ID_RESET (0x10a0000)
191 #define R_QSPIDMA_DST_CTRL (0x80c / 4)
192 #define R_QSPIDMA_DST_CTRL_RESET (0x803ffa00)
193 #define R_QSPIDMA_DST_I_MASK (0x820 / 4)
194 #define R_QSPIDMA_DST_I_MASK_RESET (0xfe)
195 #define R_QSPIDMA_DST_CTRL2 (0x824 / 4)
196 #define R_QSPIDMA_DST_CTRL2_RESET (0x081bfff8)
198 /* size of TXRX FIFOs */
202 #define RXFF_A_Q (64 * 4)
203 #define TXFF_A_Q (64 * 4)
205 /* 16MB per linear region */
206 #define LQSPI_ADDRESS_BITS 24
208 #define SNOOP_CHECKING 0xFF
209 #define SNOOP_ADDR 0xF0
210 #define SNOOP_NONE 0xEE
211 #define SNOOP_STRIPING 0
213 #define MIN_NUM_BUSSES 1
214 #define MAX_NUM_BUSSES 2
216 static inline int num_effective_busses(XilinxSPIPS
*s
)
218 return (s
->regs
[R_LQSPI_CFG
] & LQSPI_CFG_SEP_BUS
&&
219 s
->regs
[R_LQSPI_CFG
] & LQSPI_CFG_TWO_MEM
) ? s
->num_busses
: 1;
222 static void xilinx_spips_update_cs(XilinxSPIPS
*s
, int field
)
226 for (i
= 0; i
< s
->num_cs
; i
++) {
227 bool old_state
= s
->cs_lines_state
[i
];
228 bool new_state
= field
& (1 << i
);
230 if (old_state
!= new_state
) {
231 s
->cs_lines_state
[i
] = new_state
;
232 s
->rx_discard
= ARRAY_FIELD_EX32(s
->regs
, CMND
, RX_DISCARD
);
233 DB_PRINT_L(1, "%sselecting slave %d\n", new_state
? "" : "de", i
);
235 qemu_set_irq(s
->cs_lines
[i
], !new_state
);
237 if (!(field
& ((1 << s
->num_cs
) - 1))) {
238 s
->snoop_state
= SNOOP_CHECKING
;
241 s
->link_state_next
= 1;
242 s
->link_state_next_when
= 0;
243 DB_PRINT_L(1, "moving to snoop check state\n");
247 static void xlnx_zynqmp_qspips_update_cs_lines(XlnxZynqMPQSPIPS
*s
)
249 if (s
->regs
[R_GQSPI_GF_SNAPSHOT
]) {
250 int field
= ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, CHIP_SELECT
);
251 xilinx_spips_update_cs(XILINX_SPIPS(s
), field
);
255 static void xilinx_spips_update_cs_lines(XilinxSPIPS
*s
)
257 int field
= ~((s
->regs
[R_CONFIG
] & CS
) >> CS_SHIFT
);
259 /* In dual parallel, mirror low CS to both */
260 if (num_effective_busses(s
) == 2) {
261 /* Single bit chip-select for qspi */
264 /* Dual stack U-Page */
265 } else if (s
->regs
[R_LQSPI_CFG
] & LQSPI_CFG_TWO_MEM
&&
266 s
->regs
[R_LQSPI_STS
] & LQSPI_CFG_U_PAGE
) {
267 /* Single bit chip-select for qspi */
269 /* change from CS0 to CS1 */
273 if (!(s
->regs
[R_CONFIG
] & MANUAL_CS
) &&
274 fifo8_is_empty(&s
->tx_fifo
)) {
277 xilinx_spips_update_cs(s
, field
);
280 static void xilinx_spips_update_ixr(XilinxSPIPS
*s
)
282 if (!(s
->regs
[R_LQSPI_CFG
] & LQSPI_CFG_LQ_MODE
)) {
283 s
->regs
[R_INTR_STATUS
] &= ~IXR_SELF_CLEAR
;
284 s
->regs
[R_INTR_STATUS
] |=
285 (fifo8_is_full(&s
->rx_fifo
) ? IXR_RX_FIFO_FULL
: 0) |
286 (s
->rx_fifo
.num
>= s
->regs
[R_RX_THRES
] ?
287 IXR_RX_FIFO_NOT_EMPTY
: 0) |
288 (fifo8_is_full(&s
->tx_fifo
) ? IXR_TX_FIFO_FULL
: 0) |
289 (fifo8_is_empty(&s
->tx_fifo
) ? IXR_TX_FIFO_EMPTY
: 0) |
290 (s
->tx_fifo
.num
< s
->regs
[R_TX_THRES
] ? IXR_TX_FIFO_NOT_FULL
: 0);
292 int new_irqline
= !!(s
->regs
[R_INTR_MASK
] & s
->regs
[R_INTR_STATUS
] &
294 if (new_irqline
!= s
->irqline
) {
295 s
->irqline
= new_irqline
;
296 qemu_set_irq(s
->irq
, s
->irqline
);
300 static void xlnx_zynqmp_qspips_update_ixr(XlnxZynqMPQSPIPS
*s
)
305 s
->regs
[R_GQSPI_ISR
] &= ~IXR_SELF_CLEAR
;
306 s
->regs
[R_GQSPI_ISR
] |=
307 (fifo32_is_empty(&s
->fifo_g
) ? IXR_GENERIC_FIFO_EMPTY
: 0) |
308 (fifo32_is_full(&s
->fifo_g
) ? IXR_GENERIC_FIFO_FULL
: 0) |
309 (s
->fifo_g
.fifo
.num
< s
->regs
[R_GQSPI_GFIFO_THRESH
] ?
310 IXR_GENERIC_FIFO_NOT_FULL
: 0) |
311 (fifo8_is_empty(&s
->rx_fifo_g
) ? IXR_RX_FIFO_EMPTY
: 0) |
312 (fifo8_is_full(&s
->rx_fifo_g
) ? IXR_RX_FIFO_FULL
: 0) |
313 (s
->rx_fifo_g
.num
>= s
->regs
[R_GQSPI_RX_THRESH
] ?
314 IXR_RX_FIFO_NOT_EMPTY
: 0) |
315 (fifo8_is_empty(&s
->tx_fifo_g
) ? IXR_TX_FIFO_EMPTY
: 0) |
316 (fifo8_is_full(&s
->tx_fifo_g
) ? IXR_TX_FIFO_FULL
: 0) |
317 (s
->tx_fifo_g
.num
< s
->regs
[R_GQSPI_TX_THRESH
] ?
318 IXR_TX_FIFO_NOT_FULL
: 0);
320 /* GQSPI Interrupt Trigger Status */
321 gqspi_int
= (~s
->regs
[R_GQSPI_IMR
]) & s
->regs
[R_GQSPI_ISR
] & GQSPI_IXR_MASK
;
322 new_irqline
= !!(gqspi_int
& IXR_ALL
);
324 /* drive external interrupt pin */
325 if (new_irqline
!= s
->gqspi_irqline
) {
326 s
->gqspi_irqline
= new_irqline
;
327 qemu_set_irq(XILINX_SPIPS(s
)->irq
, s
->gqspi_irqline
);
331 static void xilinx_spips_reset(DeviceState
*d
)
333 XilinxSPIPS
*s
= XILINX_SPIPS(d
);
335 memset(s
->regs
, 0, sizeof(s
->regs
));
337 fifo8_reset(&s
->rx_fifo
);
338 fifo8_reset(&s
->rx_fifo
);
339 /* non zero resets */
340 s
->regs
[R_CONFIG
] |= MODEFAIL_GEN_EN
;
341 s
->regs
[R_SLAVE_IDLE_COUNT
] = 0xFF;
342 s
->regs
[R_TX_THRES
] = 1;
343 s
->regs
[R_RX_THRES
] = 1;
344 /* FIXME: move magic number definition somewhere sensible */
345 s
->regs
[R_MOD_ID
] = 0x01090106;
346 s
->regs
[R_LQSPI_CFG
] = R_LQSPI_CFG_RESET
;
348 s
->link_state_next
= 1;
349 s
->link_state_next_when
= 0;
350 s
->snoop_state
= SNOOP_CHECKING
;
352 s
->man_start_com
= false;
353 xilinx_spips_update_ixr(s
);
354 xilinx_spips_update_cs_lines(s
);
357 static void xlnx_zynqmp_qspips_reset(DeviceState
*d
)
359 XlnxZynqMPQSPIPS
*s
= XLNX_ZYNQMP_QSPIPS(d
);
361 xilinx_spips_reset(d
);
363 memset(s
->regs
, 0, sizeof(s
->regs
));
365 fifo8_reset(&s
->rx_fifo_g
);
366 fifo8_reset(&s
->rx_fifo_g
);
367 fifo32_reset(&s
->fifo_g
);
368 s
->regs
[R_INTR_STATUS
] = R_INTR_STATUS_RESET
;
370 s
->regs
[R_LPBK_DLY_ADJ
] = R_LPBK_DLY_ADJ_RESET
;
371 s
->regs
[R_GQSPI_GFIFO_THRESH
] = 0x10;
372 s
->regs
[R_MOD_ID
] = 0x01090101;
373 s
->regs
[R_GQSPI_IMR
] = R_GQSPI_IMR_RESET
;
374 s
->regs
[R_GQSPI_TX_THRESH
] = 1;
375 s
->regs
[R_GQSPI_RX_THRESH
] = 1;
376 s
->regs
[R_GQSPI_GPIO
] = 1;
377 s
->regs
[R_GQSPI_LPBK_DLY_ADJ
] = R_GQSPI_LPBK_DLY_ADJ_RESET
;
378 s
->regs
[R_GQSPI_MOD_ID
] = R_GQSPI_MOD_ID_RESET
;
379 s
->regs
[R_QSPIDMA_DST_CTRL
] = R_QSPIDMA_DST_CTRL_RESET
;
380 s
->regs
[R_QSPIDMA_DST_I_MASK
] = R_QSPIDMA_DST_I_MASK_RESET
;
381 s
->regs
[R_QSPIDMA_DST_CTRL2
] = R_QSPIDMA_DST_CTRL2_RESET
;
382 s
->man_start_com_g
= false;
383 s
->gqspi_irqline
= 0;
384 xlnx_zynqmp_qspips_update_ixr(s
);
387 /* N way (num) in place bit striper. Lay out row wise bits (MSB to LSB)
388 * column wise (from element 0 to N-1). num is the length of x, and dir
389 * reverses the direction of the transform. Best illustrated by example:
390 * Each digit in the below array is a single bit (num == 3):
392 * {{ 76543210, } ----- stripe (dir == false) -----> {{ 741gdaFC, }
393 * { hgfedcba, } { 630fcHEB, }
394 * { HGFEDCBA, }} <---- upstripe (dir == true) ----- { 52hebGDA, }}
397 static inline void stripe8(uint8_t *x
, int num
, bool dir
)
400 memset(r
, 0, sizeof(uint8_t) * num
);
405 for (idx
[0] = 0; idx
[0] < num
; ++idx
[0]) {
406 for (bit
[0] = 7; bit
[0] >= 0; bit
[0]--) {
407 r
[idx
[!d
]] |= x
[idx
[d
]] & 1 << bit
[d
] ? 1 << bit
[!d
] : 0;
408 idx
[1] = (idx
[1] + 1) % num
;
414 memcpy(x
, r
, sizeof(uint8_t) * num
);
417 static void xlnx_zynqmp_qspips_flush_fifo_g(XlnxZynqMPQSPIPS
*s
)
419 while (s
->regs
[R_GQSPI_DATA_STS
] || !fifo32_is_empty(&s
->fifo_g
)) {
420 uint8_t tx_rx
[2] = { 0 };
425 if (!s
->regs
[R_GQSPI_DATA_STS
]) {
428 s
->regs
[R_GQSPI_GF_SNAPSHOT
] = fifo32_pop(&s
->fifo_g
);
429 DB_PRINT_L(0, "GQSPI command: %x\n", s
->regs
[R_GQSPI_GF_SNAPSHOT
]);
430 if (!s
->regs
[R_GQSPI_GF_SNAPSHOT
]) {
431 DB_PRINT_L(0, "Dummy GQSPI Delay Command Entry, Do nothing");
434 xlnx_zynqmp_qspips_update_cs_lines(s
);
436 imm
= ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, IMMEDIATE_DATA
);
437 if (!ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, DATA_XFER
)) {
438 /* immedate transfer */
439 if (ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, TRANSMIT
) ||
440 ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, RECIEVE
)) {
441 s
->regs
[R_GQSPI_DATA_STS
] = 1;
442 /* CS setup/hold - do nothing */
444 s
->regs
[R_GQSPI_DATA_STS
] = 0;
446 } else if (ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, EXPONENT
)) {
448 qemu_log_mask(LOG_UNIMP
, "QSPI exponential transfer too"
449 " long - 2 ^ %" PRId8
" requested\n", imm
);
451 s
->regs
[R_GQSPI_DATA_STS
] = 1ul << imm
;
453 s
->regs
[R_GQSPI_DATA_STS
] = imm
;
456 /* Zero length transfer check */
457 if (!s
->regs
[R_GQSPI_DATA_STS
]) {
460 if (ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, RECIEVE
) &&
461 fifo8_is_full(&s
->rx_fifo_g
)) {
462 /* No space in RX fifo for transfer - try again later */
465 if (ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, STRIPE
) &&
466 (ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, TRANSMIT
) ||
467 ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, RECIEVE
))) {
470 if (!ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, DATA_XFER
)) {
471 tx_rx
[0] = ARRAY_FIELD_EX32(s
->regs
,
472 GQSPI_GF_SNAPSHOT
, IMMEDIATE_DATA
);
473 } else if (ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, TRANSMIT
)) {
474 for (i
= 0; i
< num_stripes
; ++i
) {
475 if (!fifo8_is_empty(&s
->tx_fifo_g
)) {
476 tx_rx
[i
] = fifo8_pop(&s
->tx_fifo_g
);
477 s
->tx_fifo_g_align
++;
483 if (num_stripes
== 1) {
487 busses
= ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, DATA_BUS_SELECT
);
488 for (i
= 0; i
< 2; ++i
) {
489 DB_PRINT_L(1, "bus %d tx = %02x\n", i
, tx_rx
[i
]);
490 tx_rx
[i
] = ssi_transfer(XILINX_SPIPS(s
)->spi
[i
], tx_rx
[i
]);
491 DB_PRINT_L(1, "bus %d rx = %02x\n", i
, tx_rx
[i
]);
493 if (s
->regs
[R_GQSPI_DATA_STS
] > 1 &&
494 busses
== 0x3 && num_stripes
== 2) {
495 s
->regs
[R_GQSPI_DATA_STS
] -= 2;
496 } else if (s
->regs
[R_GQSPI_DATA_STS
] > 0) {
497 s
->regs
[R_GQSPI_DATA_STS
]--;
499 if (ARRAY_FIELD_EX32(s
->regs
, GQSPI_GF_SNAPSHOT
, RECIEVE
)) {
500 for (i
= 0; i
< 2; ++i
) {
501 if (busses
& (1 << i
)) {
502 DB_PRINT_L(1, "bus %d push_byte = %02x\n", i
, tx_rx
[i
]);
503 fifo8_push(&s
->rx_fifo_g
, tx_rx
[i
]);
504 s
->rx_fifo_g_align
++;
508 if (!s
->regs
[R_GQSPI_DATA_STS
]) {
509 for (; s
->tx_fifo_g_align
% 4; s
->tx_fifo_g_align
++) {
510 fifo8_pop(&s
->tx_fifo_g
);
512 for (; s
->rx_fifo_g_align
% 4; s
->rx_fifo_g_align
++) {
513 fifo8_push(&s
->rx_fifo_g
, 0);
519 static int xilinx_spips_num_dummies(XilinxQSPIPS
*qs
, uint8_t command
)
522 /* The SPI device is not a QSPI device */
526 switch (command
) { /* check for dummies */
527 case READ
: /* no dummy bytes/cycles */
553 static inline uint8_t get_addr_length(XilinxSPIPS
*s
, uint8_t cmd
)
566 return (s
->regs
[R_CMND
] & R_CMND_EXT_ADD
) ? 4 : 3;
570 static void xilinx_spips_flush_txfifo(XilinxSPIPS
*s
)
573 XilinxQSPIPS
*q
= (XilinxQSPIPS
*) object_dynamic_cast(OBJECT(s
),
579 uint8_t tx_rx
[MAX_NUM_BUSSES
] = { 0 };
580 uint8_t dummy_cycles
= 0;
583 if (fifo8_is_empty(&s
->tx_fifo
)) {
584 xilinx_spips_update_ixr(s
);
586 } else if (s
->snoop_state
== SNOOP_STRIPING
) {
587 for (i
= 0; i
< num_effective_busses(s
); ++i
) {
588 tx_rx
[i
] = fifo8_pop(&s
->tx_fifo
);
590 stripe8(tx_rx
, num_effective_busses(s
), false);
591 } else if (s
->snoop_state
>= SNOOP_ADDR
) {
592 tx
= fifo8_pop(&s
->tx_fifo
);
593 for (i
= 0; i
< num_effective_busses(s
); ++i
) {
597 /* Extract a dummy byte and generate dummy cycles according to the
599 tx
= fifo8_pop(&s
->tx_fifo
);
600 dummy_cycles
= 8 / s
->link_state
;
603 for (i
= 0; i
< num_effective_busses(s
); ++i
) {
604 int bus
= num_effective_busses(s
) - 1 - i
;
607 for (d
= 0; d
< dummy_cycles
; ++d
) {
608 tx_rx
[0] = ssi_transfer(s
->spi
[bus
], (uint32_t)tx_rx
[0]);
611 DB_PRINT_L(debug_level
, "tx = %02x\n", tx_rx
[i
]);
612 tx_rx
[i
] = ssi_transfer(s
->spi
[bus
], (uint32_t)tx_rx
[i
]);
613 DB_PRINT_L(debug_level
, "rx = %02x\n", tx_rx
[i
]);
617 if (s
->regs
[R_CMND
] & R_CMND_RXFIFO_DRAIN
) {
618 DB_PRINT_L(debug_level
, "dircarding drained rx byte\n");
620 } else if (s
->rx_discard
) {
621 DB_PRINT_L(debug_level
, "dircarding discarded rx byte\n");
622 s
->rx_discard
-= 8 / s
->link_state
;
623 } else if (fifo8_is_full(&s
->rx_fifo
)) {
624 s
->regs
[R_INTR_STATUS
] |= IXR_RX_FIFO_OVERFLOW
;
625 DB_PRINT_L(0, "rx FIFO overflow");
626 } else if (s
->snoop_state
== SNOOP_STRIPING
) {
627 stripe8(tx_rx
, num_effective_busses(s
), true);
628 for (i
= 0; i
< num_effective_busses(s
); ++i
) {
629 fifo8_push(&s
->rx_fifo
, (uint8_t)tx_rx
[i
]);
630 DB_PRINT_L(debug_level
, "pushing striped rx byte\n");
633 DB_PRINT_L(debug_level
, "pushing unstriped rx byte\n");
634 fifo8_push(&s
->rx_fifo
, (uint8_t)tx_rx
[0]);
637 if (s
->link_state_next_when
) {
638 s
->link_state_next_when
--;
639 if (!s
->link_state_next_when
) {
640 s
->link_state
= s
->link_state_next
;
644 DB_PRINT_L(debug_level
, "initial snoop state: %x\n",
645 (unsigned)s
->snoop_state
);
646 switch (s
->snoop_state
) {
647 case (SNOOP_CHECKING
):
648 /* Store the count of dummy bytes in the txfifo */
649 s
->cmd_dummies
= xilinx_spips_num_dummies(q
, tx
);
650 addr_length
= get_addr_length(s
, tx
);
651 if (s
->cmd_dummies
< 0) {
652 s
->snoop_state
= SNOOP_NONE
;
654 s
->snoop_state
= SNOOP_ADDR
+ addr_length
- 1;
660 s
->link_state_next
= 2;
661 s
->link_state_next_when
= addr_length
+ s
->cmd_dummies
;
667 s
->link_state_next
= 4;
668 s
->link_state_next_when
= addr_length
+ s
->cmd_dummies
;
681 /* Address has been transmitted, transmit dummy cycles now if
683 if (s
->cmd_dummies
< 0) {
684 s
->snoop_state
= SNOOP_NONE
;
686 s
->snoop_state
= s
->cmd_dummies
;
689 case (SNOOP_STRIPING
):
691 /* Once we hit the boring stuff - squelch debug noise */
693 DB_PRINT_L(0, "squelching debug info ....\n");
700 DB_PRINT_L(debug_level
, "final snoop state: %x\n",
701 (unsigned)s
->snoop_state
);
705 static inline void tx_data_bytes(Fifo8
*fifo
, uint32_t value
, int num
, bool be
)
708 for (i
= 0; i
< num
&& !fifo8_is_full(fifo
); ++i
) {
710 fifo8_push(fifo
, (uint8_t)(value
>> 24));
713 fifo8_push(fifo
, (uint8_t)value
);
719 static void xilinx_spips_check_zero_pump(XilinxSPIPS
*s
)
721 if (!s
->regs
[R_TRANSFER_SIZE
]) {
724 if (!fifo8_is_empty(&s
->tx_fifo
) && s
->regs
[R_CMND
] & R_CMND_PUSH_WAIT
) {
728 * The zero pump must never fill tx fifo such that rx overflow is
731 while (s
->regs
[R_TRANSFER_SIZE
] &&
732 s
->rx_fifo
.num
+ s
->tx_fifo
.num
< RXFF_A_Q
- 3) {
733 /* endianess just doesn't matter when zero pumping */
734 tx_data_bytes(&s
->tx_fifo
, 0, 4, false);
735 s
->regs
[R_TRANSFER_SIZE
] &= ~0x03ull
;
736 s
->regs
[R_TRANSFER_SIZE
] -= 4;
740 static void xilinx_spips_check_flush(XilinxSPIPS
*s
)
742 if (s
->man_start_com
||
743 (!fifo8_is_empty(&s
->tx_fifo
) &&
744 !(s
->regs
[R_CONFIG
] & MAN_START_EN
))) {
745 xilinx_spips_check_zero_pump(s
);
746 xilinx_spips_flush_txfifo(s
);
748 if (fifo8_is_empty(&s
->tx_fifo
) && !s
->regs
[R_TRANSFER_SIZE
]) {
749 s
->man_start_com
= false;
751 xilinx_spips_update_ixr(s
);
754 static void xlnx_zynqmp_qspips_check_flush(XlnxZynqMPQSPIPS
*s
)
756 bool gqspi_has_work
= s
->regs
[R_GQSPI_DATA_STS
] ||
757 !fifo32_is_empty(&s
->fifo_g
);
759 if (ARRAY_FIELD_EX32(s
->regs
, GQSPI_SELECT
, GENERIC_QSPI_EN
)) {
760 if (s
->man_start_com_g
|| (gqspi_has_work
&&
761 !ARRAY_FIELD_EX32(s
->regs
, GQSPI_CNFG
, GEN_FIFO_START_MODE
))) {
762 xlnx_zynqmp_qspips_flush_fifo_g(s
);
765 xilinx_spips_check_flush(XILINX_SPIPS(s
));
767 if (!gqspi_has_work
) {
768 s
->man_start_com_g
= false;
770 xlnx_zynqmp_qspips_update_ixr(s
);
773 static inline int rx_data_bytes(Fifo8
*fifo
, uint8_t *value
, int max
)
777 for (i
= 0; i
< max
&& !fifo8_is_empty(fifo
); ++i
) {
778 value
[i
] = fifo8_pop(fifo
);
783 static const void *pop_buf(Fifo8
*fifo
, uint32_t max
, uint32_t *num
)
787 if (max
== 0 || max
> fifo
->num
) {
790 *num
= MIN(fifo
->capacity
- fifo
->head
, max
);
791 ret
= &fifo
->data
[fifo
->head
];
793 fifo
->head
%= fifo
->capacity
;
798 static void xlnx_zynqmp_qspips_notify(void *opaque
)
800 XlnxZynqMPQSPIPS
*rq
= XLNX_ZYNQMP_QSPIPS(opaque
);
801 XilinxSPIPS
*s
= XILINX_SPIPS(rq
);
804 if (ARRAY_FIELD_EX32(rq
->regs
, GQSPI_SELECT
, GENERIC_QSPI_EN
)) {
805 if (!(ARRAY_FIELD_EX32(rq
->regs
, GQSPI_CNFG
, MODE_EN
) == 2)) {
808 recv_fifo
= &rq
->rx_fifo_g
;
810 if (!(s
->regs
[R_CMND
] & R_CMND_DMA_EN
)) {
813 recv_fifo
= &s
->rx_fifo
;
815 while (recv_fifo
->num
>= 4
816 && stream_can_push(rq
->dma
, xlnx_zynqmp_qspips_notify
, rq
))
820 const void *rxd
= pop_buf(recv_fifo
, 4, &num
);
822 memcpy(rq
->dma_buf
, rxd
, num
);
824 ret
= stream_push(rq
->dma
, rq
->dma_buf
, 4);
826 xlnx_zynqmp_qspips_check_flush(rq
);
830 static uint64_t xilinx_spips_read(void *opaque
, hwaddr addr
,
833 XilinxSPIPS
*s
= opaque
;
842 mask
= ~(R_CONFIG_RSVD
| MAN_START_COM
);
845 ret
= s
->regs
[addr
] & IXR_ALL
;
847 DB_PRINT_L(0, "addr=" TARGET_FMT_plx
" = %x\n", addr
* 4, ret
);
848 xilinx_spips_update_ixr(s
);
856 case R_SLAVE_IDLE_COUNT
:
868 memset(rx_buf
, 0, sizeof(rx_buf
));
869 shortfall
= rx_data_bytes(&s
->rx_fifo
, rx_buf
, s
->num_txrx_bytes
);
870 ret
= s
->regs
[R_CONFIG
] & R_CONFIG_ENDIAN
?
871 cpu_to_be32(*(uint32_t *)rx_buf
) :
872 cpu_to_le32(*(uint32_t *)rx_buf
);
873 if (!(s
->regs
[R_CONFIG
] & R_CONFIG_ENDIAN
)) {
874 ret
<<= 8 * shortfall
;
876 DB_PRINT_L(0, "addr=" TARGET_FMT_plx
" = %x\n", addr
* 4, ret
);
877 xilinx_spips_check_flush(s
);
878 xilinx_spips_update_ixr(s
);
881 DB_PRINT_L(0, "addr=" TARGET_FMT_plx
" = %x\n", addr
* 4,
882 s
->regs
[addr
] & mask
);
883 return s
->regs
[addr
] & mask
;
887 static uint64_t xlnx_zynqmp_qspips_read(void *opaque
,
888 hwaddr addr
, unsigned size
)
890 XlnxZynqMPQSPIPS
*s
= XLNX_ZYNQMP_QSPIPS(opaque
);
891 uint32_t reg
= addr
/ 4;
896 if (reg
<= R_MOD_ID
) {
897 return xilinx_spips_read(opaque
, addr
, size
);
901 if (fifo8_is_empty(&s
->rx_fifo_g
)) {
902 qemu_log_mask(LOG_GUEST_ERROR
,
903 "Read from empty GQSPI RX FIFO\n");
906 memset(rx_buf
, 0, sizeof(rx_buf
));
907 shortfall
= rx_data_bytes(&s
->rx_fifo_g
, rx_buf
,
908 XILINX_SPIPS(s
)->num_txrx_bytes
);
909 ret
= ARRAY_FIELD_EX32(s
->regs
, GQSPI_CNFG
, ENDIAN
) ?
910 cpu_to_be32(*(uint32_t *)rx_buf
) :
911 cpu_to_le32(*(uint32_t *)rx_buf
);
912 if (!ARRAY_FIELD_EX32(s
->regs
, GQSPI_CNFG
, ENDIAN
)) {
913 ret
<<= 8 * shortfall
;
915 xlnx_zynqmp_qspips_check_flush(s
);
916 xlnx_zynqmp_qspips_update_ixr(s
);
924 static void xilinx_spips_write(void *opaque
, hwaddr addr
,
925 uint64_t value
, unsigned size
)
928 XilinxSPIPS
*s
= opaque
;
930 DB_PRINT_L(0, "addr=" TARGET_FMT_plx
" = %x\n", addr
, (unsigned)value
);
934 mask
= ~(R_CONFIG_RSVD
| MAN_START_COM
);
935 if ((value
& MAN_START_COM
) && (s
->regs
[R_CONFIG
] & MAN_START_EN
)) {
936 s
->man_start_com
= true;
941 s
->regs
[R_INTR_STATUS
] &= ~(mask
& value
);
945 s
->regs
[R_INTR_MASK
] &= ~(mask
& value
);
949 s
->regs
[R_INTR_MASK
] |= mask
& value
;
954 case R_SLAVE_IDLE_COUNT
:
963 tx_data_bytes(&s
->tx_fifo
, (uint32_t)value
, s
->num_txrx_bytes
,
964 s
->regs
[R_CONFIG
] & R_CONFIG_ENDIAN
);
967 tx_data_bytes(&s
->tx_fifo
, (uint32_t)value
, 1,
968 s
->regs
[R_CONFIG
] & R_CONFIG_ENDIAN
);
971 tx_data_bytes(&s
->tx_fifo
, (uint32_t)value
, 2,
972 s
->regs
[R_CONFIG
] & R_CONFIG_ENDIAN
);
975 tx_data_bytes(&s
->tx_fifo
, (uint32_t)value
, 3,
976 s
->regs
[R_CONFIG
] & R_CONFIG_ENDIAN
);
979 s
->regs
[addr
] = (s
->regs
[addr
] & ~mask
) | (value
& mask
);
981 xilinx_spips_update_cs_lines(s
);
982 xilinx_spips_check_flush(s
);
983 xilinx_spips_update_cs_lines(s
);
984 xilinx_spips_update_ixr(s
);
987 static const MemoryRegionOps spips_ops
= {
988 .read
= xilinx_spips_read
,
989 .write
= xilinx_spips_write
,
990 .endianness
= DEVICE_LITTLE_ENDIAN
,
993 static void xilinx_qspips_invalidate_mmio_ptr(XilinxQSPIPS
*q
)
995 XilinxSPIPS
*s
= &q
->parent_obj
;
997 if ((q
->mmio_execution_enabled
) && (q
->lqspi_cached_addr
!= ~0ULL)) {
998 /* Invalidate the current mapped mmio */
999 memory_region_invalidate_mmio_ptr(&s
->mmlqspi
, q
->lqspi_cached_addr
,
1003 q
->lqspi_cached_addr
= ~0ULL;
1006 static void xilinx_qspips_write(void *opaque
, hwaddr addr
,
1007 uint64_t value
, unsigned size
)
1009 XilinxQSPIPS
*q
= XILINX_QSPIPS(opaque
);
1010 XilinxSPIPS
*s
= XILINX_SPIPS(opaque
);
1012 xilinx_spips_write(opaque
, addr
, value
, size
);
1015 if (addr
== R_LQSPI_CFG
) {
1016 xilinx_qspips_invalidate_mmio_ptr(q
);
1018 if (s
->regs
[R_CMND
] & R_CMND_RXFIFO_DRAIN
) {
1019 fifo8_reset(&s
->rx_fifo
);
1023 static void xlnx_zynqmp_qspips_write(void *opaque
, hwaddr addr
,
1024 uint64_t value
, unsigned size
)
1026 XlnxZynqMPQSPIPS
*s
= XLNX_ZYNQMP_QSPIPS(opaque
);
1027 uint32_t reg
= addr
/ 4;
1029 if (reg
<= R_MOD_ID
) {
1030 xilinx_qspips_write(opaque
, addr
, value
, size
);
1034 if (FIELD_EX32(value
, GQSPI_CNFG
, GEN_FIFO_START
) &&
1035 ARRAY_FIELD_EX32(s
->regs
, GQSPI_CNFG
, GEN_FIFO_START_MODE
)) {
1036 s
->man_start_com_g
= true;
1038 s
->regs
[reg
] = value
& ~(R_GQSPI_CNFG_GEN_FIFO_START_MASK
);
1040 case R_GQSPI_GEN_FIFO
:
1041 if (!fifo32_is_full(&s
->fifo_g
)) {
1042 fifo32_push(&s
->fifo_g
, value
);
1046 tx_data_bytes(&s
->tx_fifo_g
, (uint32_t)value
, 4,
1047 ARRAY_FIELD_EX32(s
->regs
, GQSPI_CNFG
, ENDIAN
));
1049 case R_GQSPI_FIFO_CTRL
:
1050 if (FIELD_EX32(value
, GQSPI_FIFO_CTRL
, GENERIC_FIFO_RESET
)) {
1051 fifo32_reset(&s
->fifo_g
);
1053 if (FIELD_EX32(value
, GQSPI_FIFO_CTRL
, TX_FIFO_RESET
)) {
1054 fifo8_reset(&s
->tx_fifo_g
);
1056 if (FIELD_EX32(value
, GQSPI_FIFO_CTRL
, RX_FIFO_RESET
)) {
1057 fifo8_reset(&s
->rx_fifo_g
);
1061 s
->regs
[R_GQSPI_IMR
] |= value
;
1064 s
->regs
[R_GQSPI_IMR
] &= ~value
;
1067 s
->regs
[R_GQSPI_ISR
] &= ~value
;
1071 case R_GQSPI_GF_SNAPSHOT
:
1072 case R_GQSPI_MOD_ID
:
1075 s
->regs
[reg
] = value
;
1078 xlnx_zynqmp_qspips_update_cs_lines(s
);
1079 xlnx_zynqmp_qspips_check_flush(s
);
1080 xlnx_zynqmp_qspips_update_cs_lines(s
);
1081 xlnx_zynqmp_qspips_update_ixr(s
);
1083 xlnx_zynqmp_qspips_notify(s
);
1086 static const MemoryRegionOps qspips_ops
= {
1087 .read
= xilinx_spips_read
,
1088 .write
= xilinx_qspips_write
,
1089 .endianness
= DEVICE_LITTLE_ENDIAN
,
1092 static const MemoryRegionOps xlnx_zynqmp_qspips_ops
= {
1093 .read
= xlnx_zynqmp_qspips_read
,
1094 .write
= xlnx_zynqmp_qspips_write
,
1095 .endianness
= DEVICE_LITTLE_ENDIAN
,
1098 #define LQSPI_CACHE_SIZE 1024
1100 static void lqspi_load_cache(void *opaque
, hwaddr addr
)
1102 XilinxQSPIPS
*q
= opaque
;
1103 XilinxSPIPS
*s
= opaque
;
1105 int flash_addr
= ((addr
& ~(LQSPI_CACHE_SIZE
- 1))
1106 / num_effective_busses(s
));
1107 int slave
= flash_addr
>> LQSPI_ADDRESS_BITS
;
1108 int cache_entry
= 0;
1109 uint32_t u_page_save
= s
->regs
[R_LQSPI_STS
] & ~LQSPI_CFG_U_PAGE
;
1111 if (addr
< q
->lqspi_cached_addr
||
1112 addr
> q
->lqspi_cached_addr
+ LQSPI_CACHE_SIZE
- 4) {
1113 xilinx_qspips_invalidate_mmio_ptr(q
);
1114 s
->regs
[R_LQSPI_STS
] &= ~LQSPI_CFG_U_PAGE
;
1115 s
->regs
[R_LQSPI_STS
] |= slave
? LQSPI_CFG_U_PAGE
: 0;
1117 DB_PRINT_L(0, "config reg status: %08x\n", s
->regs
[R_LQSPI_CFG
]);
1119 fifo8_reset(&s
->tx_fifo
);
1120 fifo8_reset(&s
->rx_fifo
);
1123 DB_PRINT_L(0, "pushing read instruction: %02x\n",
1124 (unsigned)(uint8_t)(s
->regs
[R_LQSPI_CFG
] &
1125 LQSPI_CFG_INST_CODE
));
1126 fifo8_push(&s
->tx_fifo
, s
->regs
[R_LQSPI_CFG
] & LQSPI_CFG_INST_CODE
);
1128 DB_PRINT_L(0, "pushing read address %06x\n", flash_addr
);
1129 if (s
->regs
[R_LQSPI_CFG
] & LQSPI_CFG_ADDR4
) {
1130 fifo8_push(&s
->tx_fifo
, (uint8_t)(flash_addr
>> 24));
1132 fifo8_push(&s
->tx_fifo
, (uint8_t)(flash_addr
>> 16));
1133 fifo8_push(&s
->tx_fifo
, (uint8_t)(flash_addr
>> 8));
1134 fifo8_push(&s
->tx_fifo
, (uint8_t)flash_addr
);
1136 if (s
->regs
[R_LQSPI_CFG
] & LQSPI_CFG_MODE_EN
) {
1137 fifo8_push(&s
->tx_fifo
, extract32(s
->regs
[R_LQSPI_CFG
],
1138 LQSPI_CFG_MODE_SHIFT
,
1139 LQSPI_CFG_MODE_WIDTH
));
1142 for (i
= 0; i
< (extract32(s
->regs
[R_LQSPI_CFG
], LQSPI_CFG_DUMMY_SHIFT
,
1143 LQSPI_CFG_DUMMY_WIDTH
)); ++i
) {
1144 DB_PRINT_L(0, "pushing dummy byte\n");
1145 fifo8_push(&s
->tx_fifo
, 0);
1147 xilinx_spips_update_cs_lines(s
);
1148 xilinx_spips_flush_txfifo(s
);
1149 fifo8_reset(&s
->rx_fifo
);
1151 DB_PRINT_L(0, "starting QSPI data read\n");
1153 while (cache_entry
< LQSPI_CACHE_SIZE
) {
1154 for (i
= 0; i
< 64; ++i
) {
1155 tx_data_bytes(&s
->tx_fifo
, 0, 1, false);
1157 xilinx_spips_flush_txfifo(s
);
1158 for (i
= 0; i
< 64; ++i
) {
1159 rx_data_bytes(&s
->rx_fifo
, &q
->lqspi_buf
[cache_entry
++], 1);
1163 s
->regs
[R_LQSPI_STS
] &= ~LQSPI_CFG_U_PAGE
;
1164 s
->regs
[R_LQSPI_STS
] |= u_page_save
;
1165 xilinx_spips_update_cs_lines(s
);
1167 q
->lqspi_cached_addr
= flash_addr
* num_effective_busses(s
);
1171 static void *lqspi_request_mmio_ptr(void *opaque
, hwaddr addr
, unsigned *size
,
1174 XilinxQSPIPS
*q
= opaque
;
1175 hwaddr offset_within_the_region
;
1177 if (!q
->mmio_execution_enabled
) {
1181 offset_within_the_region
= addr
& ~(LQSPI_CACHE_SIZE
- 1);
1182 lqspi_load_cache(opaque
, offset_within_the_region
);
1183 *size
= LQSPI_CACHE_SIZE
;
1184 *offset
= offset_within_the_region
;
1185 return q
->lqspi_buf
;
1189 lqspi_read(void *opaque
, hwaddr addr
, unsigned int size
)
1191 XilinxQSPIPS
*q
= opaque
;
1194 if (addr
>= q
->lqspi_cached_addr
&&
1195 addr
<= q
->lqspi_cached_addr
+ LQSPI_CACHE_SIZE
- 4) {
1196 uint8_t *retp
= &q
->lqspi_buf
[addr
- q
->lqspi_cached_addr
];
1197 ret
= cpu_to_le32(*(uint32_t *)retp
);
1198 DB_PRINT_L(1, "addr: %08x, data: %08x\n", (unsigned)addr
,
1202 lqspi_load_cache(opaque
, addr
);
1203 return lqspi_read(opaque
, addr
, size
);
1207 static const MemoryRegionOps lqspi_ops
= {
1209 .request_ptr
= lqspi_request_mmio_ptr
,
1210 .endianness
= DEVICE_NATIVE_ENDIAN
,
1212 .min_access_size
= 1,
1213 .max_access_size
= 4
1217 static void xilinx_spips_realize(DeviceState
*dev
, Error
**errp
)
1219 XilinxSPIPS
*s
= XILINX_SPIPS(dev
);
1220 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1221 XilinxSPIPSClass
*xsc
= XILINX_SPIPS_GET_CLASS(s
);
1225 DB_PRINT_L(0, "realized spips\n");
1227 if (s
->num_busses
> MAX_NUM_BUSSES
) {
1229 "requested number of SPI busses %u exceeds maximum %d",
1230 s
->num_busses
, MAX_NUM_BUSSES
);
1233 if (s
->num_busses
< MIN_NUM_BUSSES
) {
1235 "requested number of SPI busses %u is below minimum %d",
1236 s
->num_busses
, MIN_NUM_BUSSES
);
1240 s
->spi
= g_new(SSIBus
*, s
->num_busses
);
1241 for (i
= 0; i
< s
->num_busses
; ++i
) {
1243 snprintf(bus_name
, 16, "spi%d", i
);
1244 s
->spi
[i
] = ssi_create_bus(dev
, bus_name
);
1247 s
->cs_lines
= g_new0(qemu_irq
, s
->num_cs
* s
->num_busses
);
1248 s
->cs_lines_state
= g_new0(bool, s
->num_cs
* s
->num_busses
);
1249 for (i
= 0, cs
= s
->cs_lines
; i
< s
->num_busses
; ++i
, cs
+= s
->num_cs
) {
1250 ssi_auto_connect_slaves(DEVICE(s
), cs
, s
->spi
[i
]);
1253 sysbus_init_irq(sbd
, &s
->irq
);
1254 for (i
= 0; i
< s
->num_cs
* s
->num_busses
; ++i
) {
1255 sysbus_init_irq(sbd
, &s
->cs_lines
[i
]);
1258 memory_region_init_io(&s
->iomem
, OBJECT(s
), xsc
->reg_ops
, s
,
1259 "spi", XLNX_ZYNQMP_SPIPS_R_MAX
* 4);
1260 sysbus_init_mmio(sbd
, &s
->iomem
);
1264 fifo8_create(&s
->rx_fifo
, xsc
->rx_fifo_size
);
1265 fifo8_create(&s
->tx_fifo
, xsc
->tx_fifo_size
);
1268 static void xilinx_qspips_realize(DeviceState
*dev
, Error
**errp
)
1270 XilinxSPIPS
*s
= XILINX_SPIPS(dev
);
1271 XilinxQSPIPS
*q
= XILINX_QSPIPS(dev
);
1272 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1274 DB_PRINT_L(0, "realized qspips\n");
1278 s
->num_txrx_bytes
= 4;
1280 xilinx_spips_realize(dev
, errp
);
1281 memory_region_init_io(&s
->mmlqspi
, OBJECT(s
), &lqspi_ops
, s
, "lqspi",
1282 (1 << LQSPI_ADDRESS_BITS
) * 2);
1283 sysbus_init_mmio(sbd
, &s
->mmlqspi
);
1285 q
->lqspi_cached_addr
= ~0ULL;
1287 /* mmio_execution breaks migration better aborting than having strange
1290 if (q
->mmio_execution_enabled
) {
1291 error_setg(&q
->migration_blocker
,
1292 "enabling mmio_execution breaks migration");
1293 migrate_add_blocker(q
->migration_blocker
, &error_fatal
);
1297 static void xlnx_zynqmp_qspips_realize(DeviceState
*dev
, Error
**errp
)
1299 XlnxZynqMPQSPIPS
*s
= XLNX_ZYNQMP_QSPIPS(dev
);
1300 XilinxSPIPSClass
*xsc
= XILINX_SPIPS_GET_CLASS(s
);
1302 xilinx_qspips_realize(dev
, errp
);
1303 fifo8_create(&s
->rx_fifo_g
, xsc
->rx_fifo_size
);
1304 fifo8_create(&s
->tx_fifo_g
, xsc
->tx_fifo_size
);
1305 fifo32_create(&s
->fifo_g
, 32);
1308 static void xlnx_zynqmp_qspips_init(Object
*obj
)
1310 XlnxZynqMPQSPIPS
*rq
= XLNX_ZYNQMP_QSPIPS(obj
);
1312 object_property_add_link(obj
, "stream-connected-dma", TYPE_STREAM_SLAVE
,
1313 (Object
**)&rq
->dma
,
1314 object_property_allow_set_link
,
1315 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1319 static int xilinx_spips_post_load(void *opaque
, int version_id
)
1321 xilinx_spips_update_ixr((XilinxSPIPS
*)opaque
);
1322 xilinx_spips_update_cs_lines((XilinxSPIPS
*)opaque
);
1326 static const VMStateDescription vmstate_xilinx_spips
= {
1327 .name
= "xilinx_spips",
1329 .minimum_version_id
= 2,
1330 .post_load
= xilinx_spips_post_load
,
1331 .fields
= (VMStateField
[]) {
1332 VMSTATE_FIFO8(tx_fifo
, XilinxSPIPS
),
1333 VMSTATE_FIFO8(rx_fifo
, XilinxSPIPS
),
1334 VMSTATE_UINT32_ARRAY(regs
, XilinxSPIPS
, XLNX_SPIPS_R_MAX
),
1335 VMSTATE_UINT8(snoop_state
, XilinxSPIPS
),
1336 VMSTATE_END_OF_LIST()
1340 static int xlnx_zynqmp_qspips_post_load(void *opaque
, int version_id
)
1342 XlnxZynqMPQSPIPS
*s
= (XlnxZynqMPQSPIPS
*)opaque
;
1343 XilinxSPIPS
*qs
= XILINX_SPIPS(s
);
1345 if (ARRAY_FIELD_EX32(s
->regs
, GQSPI_SELECT
, GENERIC_QSPI_EN
) &&
1346 fifo8_is_empty(&qs
->rx_fifo
) && fifo8_is_empty(&qs
->tx_fifo
)) {
1347 xlnx_zynqmp_qspips_update_ixr(s
);
1348 xlnx_zynqmp_qspips_update_cs_lines(s
);
1353 static const VMStateDescription vmstate_xilinx_qspips
= {
1354 .name
= "xilinx_qspips",
1356 .minimum_version_id
= 1,
1357 .fields
= (VMStateField
[]) {
1358 VMSTATE_STRUCT(parent_obj
, XilinxQSPIPS
, 0,
1359 vmstate_xilinx_spips
, XilinxSPIPS
),
1360 VMSTATE_END_OF_LIST()
1364 static const VMStateDescription vmstate_xlnx_zynqmp_qspips
= {
1365 .name
= "xlnx_zynqmp_qspips",
1367 .minimum_version_id
= 1,
1368 .post_load
= xlnx_zynqmp_qspips_post_load
,
1369 .fields
= (VMStateField
[]) {
1370 VMSTATE_STRUCT(parent_obj
, XlnxZynqMPQSPIPS
, 0,
1371 vmstate_xilinx_qspips
, XilinxQSPIPS
),
1372 VMSTATE_FIFO8(tx_fifo_g
, XlnxZynqMPQSPIPS
),
1373 VMSTATE_FIFO8(rx_fifo_g
, XlnxZynqMPQSPIPS
),
1374 VMSTATE_FIFO32(fifo_g
, XlnxZynqMPQSPIPS
),
1375 VMSTATE_UINT32_ARRAY(regs
, XlnxZynqMPQSPIPS
, XLNX_ZYNQMP_SPIPS_R_MAX
),
1376 VMSTATE_END_OF_LIST()
1380 static Property xilinx_qspips_properties
[] = {
1381 /* We had to turn this off for 2.10 as it is not compatible with migration.
1382 * It can be enabled but will prevent the device to be migrated.
1383 * This will go aways when a fix will be released.
1385 DEFINE_PROP_BOOL("x-mmio-exec", XilinxQSPIPS
, mmio_execution_enabled
,
1387 DEFINE_PROP_END_OF_LIST(),
1390 static Property xilinx_spips_properties
[] = {
1391 DEFINE_PROP_UINT8("num-busses", XilinxSPIPS
, num_busses
, 1),
1392 DEFINE_PROP_UINT8("num-ss-bits", XilinxSPIPS
, num_cs
, 4),
1393 DEFINE_PROP_UINT8("num-txrx-bytes", XilinxSPIPS
, num_txrx_bytes
, 1),
1394 DEFINE_PROP_END_OF_LIST(),
1397 static void xilinx_qspips_class_init(ObjectClass
*klass
, void * data
)
1399 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1400 XilinxSPIPSClass
*xsc
= XILINX_SPIPS_CLASS(klass
);
1402 dc
->realize
= xilinx_qspips_realize
;
1403 dc
->props
= xilinx_qspips_properties
;
1404 xsc
->reg_ops
= &qspips_ops
;
1405 xsc
->rx_fifo_size
= RXFF_A_Q
;
1406 xsc
->tx_fifo_size
= TXFF_A_Q
;
1409 static void xilinx_spips_class_init(ObjectClass
*klass
, void *data
)
1411 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1412 XilinxSPIPSClass
*xsc
= XILINX_SPIPS_CLASS(klass
);
1414 dc
->realize
= xilinx_spips_realize
;
1415 dc
->reset
= xilinx_spips_reset
;
1416 dc
->props
= xilinx_spips_properties
;
1417 dc
->vmsd
= &vmstate_xilinx_spips
;
1419 xsc
->reg_ops
= &spips_ops
;
1420 xsc
->rx_fifo_size
= RXFF_A
;
1421 xsc
->tx_fifo_size
= TXFF_A
;
1424 static void xlnx_zynqmp_qspips_class_init(ObjectClass
*klass
, void * data
)
1426 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1427 XilinxSPIPSClass
*xsc
= XILINX_SPIPS_CLASS(klass
);
1429 dc
->realize
= xlnx_zynqmp_qspips_realize
;
1430 dc
->reset
= xlnx_zynqmp_qspips_reset
;
1431 dc
->vmsd
= &vmstate_xlnx_zynqmp_qspips
;
1432 xsc
->reg_ops
= &xlnx_zynqmp_qspips_ops
;
1433 xsc
->rx_fifo_size
= RXFF_A_Q
;
1434 xsc
->tx_fifo_size
= TXFF_A_Q
;
1437 static const TypeInfo xilinx_spips_info
= {
1438 .name
= TYPE_XILINX_SPIPS
,
1439 .parent
= TYPE_SYS_BUS_DEVICE
,
1440 .instance_size
= sizeof(XilinxSPIPS
),
1441 .class_init
= xilinx_spips_class_init
,
1442 .class_size
= sizeof(XilinxSPIPSClass
),
1445 static const TypeInfo xilinx_qspips_info
= {
1446 .name
= TYPE_XILINX_QSPIPS
,
1447 .parent
= TYPE_XILINX_SPIPS
,
1448 .instance_size
= sizeof(XilinxQSPIPS
),
1449 .class_init
= xilinx_qspips_class_init
,
1452 static const TypeInfo xlnx_zynqmp_qspips_info
= {
1453 .name
= TYPE_XLNX_ZYNQMP_QSPIPS
,
1454 .parent
= TYPE_XILINX_QSPIPS
,
1455 .instance_size
= sizeof(XlnxZynqMPQSPIPS
),
1456 .instance_init
= xlnx_zynqmp_qspips_init
,
1457 .class_init
= xlnx_zynqmp_qspips_class_init
,
1460 static void xilinx_spips_register_types(void)
1462 type_register_static(&xilinx_spips_info
);
1463 type_register_static(&xilinx_qspips_info
);
1464 type_register_static(&xlnx_zynqmp_qspips_info
);
1467 type_init(xilinx_spips_register_types
)