4 * Copyright (c) 2016 Jean-Christophe Dubois <jcd@tribudubois.net>
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
11 #include "qemu/osdep.h"
12 #include "hw/ssi/imx_spi.h"
13 #include "sysemu/sysemu.h"
17 #define DEBUG_IMX_SPI 0
20 #define DPRINTF(fmt, args...) \
22 if (DEBUG_IMX_SPI) { \
23 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_SPI, \
28 static char const *imx_spi_reg_name(uint32_t reg
)
30 static char unknown
[20];
34 return "ECSPI_RXDATA";
36 return "ECSPI_TXDATA";
38 return "ECSPI_CONREG";
40 return "ECSPI_CONFIGREG";
42 return "ECSPI_INTREG";
44 return "ECSPI_DMAREG";
46 return "ECSPI_STATREG";
48 return "ECSPI_PERIODREG";
50 return "ECSPI_TESTREG";
52 return "ECSPI_MSGDATA";
54 sprintf(unknown
, "%d ?", reg
);
59 static const VMStateDescription vmstate_imx_spi
= {
62 .minimum_version_id
= 1,
63 .fields
= (VMStateField
[]) {
64 VMSTATE_FIFO32(tx_fifo
, IMXSPIState
),
65 VMSTATE_FIFO32(rx_fifo
, IMXSPIState
),
66 VMSTATE_INT16(burst_length
, IMXSPIState
),
67 VMSTATE_UINT32_ARRAY(regs
, IMXSPIState
, ECSPI_MAX
),
72 static void imx_spi_txfifo_reset(IMXSPIState
*s
)
74 fifo32_reset(&s
->tx_fifo
);
75 s
->regs
[ECSPI_STATREG
] |= ECSPI_STATREG_TE
;
76 s
->regs
[ECSPI_STATREG
] &= ~ECSPI_STATREG_TF
;
79 static void imx_spi_rxfifo_reset(IMXSPIState
*s
)
81 fifo32_reset(&s
->rx_fifo
);
82 s
->regs
[ECSPI_STATREG
] &= ~ECSPI_STATREG_RR
;
83 s
->regs
[ECSPI_STATREG
] &= ~ECSPI_STATREG_RF
;
84 s
->regs
[ECSPI_STATREG
] &= ~ECSPI_STATREG_RO
;
87 static void imx_spi_update_irq(IMXSPIState
*s
)
91 if (fifo32_is_empty(&s
->rx_fifo
)) {
92 s
->regs
[ECSPI_STATREG
] &= ~ECSPI_STATREG_RR
;
94 s
->regs
[ECSPI_STATREG
] |= ECSPI_STATREG_RR
;
97 if (fifo32_is_full(&s
->rx_fifo
)) {
98 s
->regs
[ECSPI_STATREG
] |= ECSPI_STATREG_RF
;
100 s
->regs
[ECSPI_STATREG
] &= ~ECSPI_STATREG_RF
;
103 if (fifo32_is_empty(&s
->tx_fifo
)) {
104 s
->regs
[ECSPI_STATREG
] |= ECSPI_STATREG_TE
;
106 s
->regs
[ECSPI_STATREG
] &= ~ECSPI_STATREG_TE
;
109 if (fifo32_is_full(&s
->tx_fifo
)) {
110 s
->regs
[ECSPI_STATREG
] |= ECSPI_STATREG_TF
;
112 s
->regs
[ECSPI_STATREG
] &= ~ECSPI_STATREG_TF
;
115 level
= s
->regs
[ECSPI_STATREG
] & s
->regs
[ECSPI_INTREG
] ? 1 : 0;
117 qemu_set_irq(s
->irq
, level
);
119 DPRINTF("IRQ level is %d\n", level
);
122 static uint8_t imx_spi_selected_channel(IMXSPIState
*s
)
124 return EXTRACT(s
->regs
[ECSPI_CONREG
], ECSPI_CONREG_CHANNEL_SELECT
);
127 static uint32_t imx_spi_burst_length(IMXSPIState
*s
)
129 return EXTRACT(s
->regs
[ECSPI_CONREG
], ECSPI_CONREG_BURST_LENGTH
) + 1;
132 static bool imx_spi_is_enabled(IMXSPIState
*s
)
134 return s
->regs
[ECSPI_CONREG
] & ECSPI_CONREG_EN
;
137 static bool imx_spi_channel_is_master(IMXSPIState
*s
)
139 uint8_t mode
= EXTRACT(s
->regs
[ECSPI_CONREG
], ECSPI_CONREG_CHANNEL_MODE
);
141 return (mode
& (1 << imx_spi_selected_channel(s
))) ? true : false;
144 static bool imx_spi_is_multiple_master_burst(IMXSPIState
*s
)
146 uint8_t wave
= EXTRACT(s
->regs
[ECSPI_CONFIGREG
], ECSPI_CONFIGREG_SS_CTL
);
148 return imx_spi_channel_is_master(s
) &&
149 !(s
->regs
[ECSPI_CONREG
] & ECSPI_CONREG_SMC
) &&
150 ((wave
& (1 << imx_spi_selected_channel(s
))) ? true : false);
153 static void imx_spi_flush_txfifo(IMXSPIState
*s
)
158 DPRINTF("Begin: TX Fifo Size = %d, RX Fifo Size = %d\n",
159 fifo32_num_used(&s
->tx_fifo
), fifo32_num_used(&s
->rx_fifo
));
161 while (!fifo32_is_empty(&s
->tx_fifo
)) {
165 if (s
->burst_length
<= 0) {
166 s
->burst_length
= imx_spi_burst_length(s
);
168 DPRINTF("Burst length = %d\n", s
->burst_length
);
170 if (imx_spi_is_multiple_master_burst(s
)) {
171 s
->regs
[ECSPI_CONREG
] |= ECSPI_CONREG_XCH
;
175 tx
= fifo32_pop(&s
->tx_fifo
);
177 DPRINTF("data tx:0x%08x\n", tx
);
179 tx_burst
= MIN(s
->burst_length
, 32);
184 uint8_t byte
= tx
& 0xff;
186 DPRINTF("writing 0x%02x\n", (uint32_t)byte
);
188 /* We need to write one byte at a time */
189 byte
= ssi_transfer(s
->bus
, byte
);
191 DPRINTF("0x%02x read\n", (uint32_t)byte
);
194 rx
|= (byte
<< (index
* 8));
196 /* Remove 8 bits from the actual burst */
198 s
->burst_length
-= 8;
202 DPRINTF("data rx:0x%08x\n", rx
);
204 if (fifo32_is_full(&s
->rx_fifo
)) {
205 s
->regs
[ECSPI_STATREG
] |= ECSPI_STATREG_RO
;
207 fifo32_push(&s
->rx_fifo
, (uint8_t)rx
);
210 if (s
->burst_length
<= 0) {
211 s
->regs
[ECSPI_CONREG
] &= ~ECSPI_CONREG_XCH
;
213 if (!imx_spi_is_multiple_master_burst(s
)) {
214 s
->regs
[ECSPI_STATREG
] |= ECSPI_STATREG_TC
;
220 if (fifo32_is_empty(&s
->tx_fifo
)) {
221 s
->regs
[ECSPI_STATREG
] |= ECSPI_STATREG_TC
;
224 /* TODO: We should also use TDR and RDR bits */
226 DPRINTF("End: TX Fifo Size = %d, RX Fifo Size = %d\n",
227 fifo32_num_used(&s
->tx_fifo
), fifo32_num_used(&s
->rx_fifo
));
230 static void imx_spi_reset(DeviceState
*dev
)
232 IMXSPIState
*s
= IMX_SPI(dev
);
236 memset(s
->regs
, 0, sizeof(s
->regs
));
238 s
->regs
[ECSPI_STATREG
] = 0x00000003;
240 imx_spi_rxfifo_reset(s
);
241 imx_spi_txfifo_reset(s
);
243 imx_spi_update_irq(s
);
248 static uint64_t imx_spi_read(void *opaque
, hwaddr offset
, unsigned size
)
251 IMXSPIState
*s
= opaque
;
252 uint32_t index
= offset
>> 2;
254 if (index
>= ECSPI_MAX
) {
255 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
256 HWADDR_PRIx
"\n", TYPE_IMX_SPI
, __func__
, offset
);
262 if (!imx_spi_is_enabled(s
)) {
264 } else if (fifo32_is_empty(&s
->rx_fifo
)) {
265 /* value is undefined */
268 /* read from the RX FIFO */
269 value
= fifo32_pop(&s
->rx_fifo
);
274 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Trying to read from TX FIFO\n",
275 TYPE_IMX_SPI
, __func__
);
277 /* Reading from TXDATA gives 0 */
281 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Trying to read from MSG FIFO\n",
282 TYPE_IMX_SPI
, __func__
);
284 /* Reading from MSGDATA gives 0 */
288 value
= s
->regs
[index
];
292 DPRINTF("reg[%s] => 0x%" PRIx32
"\n", imx_spi_reg_name(index
), value
);
294 imx_spi_update_irq(s
);
296 return (uint64_t)value
;
299 static void imx_spi_write(void *opaque
, hwaddr offset
, uint64_t value
,
302 IMXSPIState
*s
= opaque
;
303 uint32_t index
= offset
>> 2;
304 uint32_t change_mask
;
306 if (index
>= ECSPI_MAX
) {
307 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
308 HWADDR_PRIx
"\n", TYPE_IMX_SPI
, __func__
, offset
);
312 DPRINTF("reg[%s] <= 0x%" PRIx32
"\n", imx_spi_reg_name(index
),
315 change_mask
= s
->regs
[index
] ^ value
;
319 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Trying to write to RX FIFO\n",
320 TYPE_IMX_SPI
, __func__
);
324 /* Is there any difference between TXDATA and MSGDATA ? */
325 /* I'll have to look in the linux driver */
326 if (!imx_spi_is_enabled(s
)) {
327 /* Ignore writes if device is disabled */
329 } else if (fifo32_is_full(&s
->tx_fifo
)) {
330 /* Ignore writes if queue is full */
334 fifo32_push(&s
->tx_fifo
, (uint32_t)value
);
336 if (imx_spi_channel_is_master(s
) &&
337 (s
->regs
[ECSPI_CONREG
] & ECSPI_CONREG_SMC
)) {
339 * Start emitting if current channel is master and SMC bit is
342 imx_spi_flush_txfifo(s
);
347 /* the RO and TC bits are write-one-to-clear */
348 value
&= ECSPI_STATREG_RO
| ECSPI_STATREG_TC
;
349 s
->regs
[ECSPI_STATREG
] &= ~value
;
353 s
->regs
[ECSPI_CONREG
] = value
;
355 if (!imx_spi_is_enabled(s
)) {
356 /* device is disabled, so this is a reset */
357 imx_spi_reset(DEVICE(s
));
361 if (imx_spi_channel_is_master(s
)) {
364 /* We are in master mode */
366 for (i
= 0; i
< 4; i
++) {
367 qemu_set_irq(s
->cs_lines
[i
],
368 i
== imx_spi_selected_channel(s
) ? 0 : 1);
371 if ((value
& change_mask
& ECSPI_CONREG_SMC
) &&
372 !fifo32_is_empty(&s
->tx_fifo
)) {
373 /* SMC bit is set and TX FIFO has some slots filled in */
374 imx_spi_flush_txfifo(s
);
375 } else if ((value
& change_mask
& ECSPI_CONREG_XCH
) &&
376 !(value
& ECSPI_CONREG_SMC
)) {
377 /* This is a request to start emitting */
378 imx_spi_flush_txfifo(s
);
384 s
->regs
[index
] = value
;
389 imx_spi_update_irq(s
);
392 static const struct MemoryRegionOps imx_spi_ops
= {
393 .read
= imx_spi_read
,
394 .write
= imx_spi_write
,
395 .endianness
= DEVICE_NATIVE_ENDIAN
,
398 * Our device would not work correctly if the guest was doing
399 * unaligned access. This might not be a limitation on the real
400 * device but in practice there is no reason for a guest to access
401 * this device unaligned.
403 .min_access_size
= 4,
404 .max_access_size
= 4,
409 static void imx_spi_realize(DeviceState
*dev
, Error
**errp
)
411 IMXSPIState
*s
= IMX_SPI(dev
);
414 s
->bus
= ssi_create_bus(dev
, "spi");
416 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &imx_spi_ops
, s
,
417 TYPE_IMX_SPI
, 0x1000);
418 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &s
->iomem
);
419 sysbus_init_irq(SYS_BUS_DEVICE(dev
), &s
->irq
);
421 ssi_auto_connect_slaves(dev
, s
->cs_lines
, s
->bus
);
423 for (i
= 0; i
< 4; ++i
) {
424 sysbus_init_irq(SYS_BUS_DEVICE(dev
), &s
->cs_lines
[i
]);
429 fifo32_create(&s
->tx_fifo
, ECSPI_FIFO_SIZE
);
430 fifo32_create(&s
->rx_fifo
, ECSPI_FIFO_SIZE
);
433 static void imx_spi_class_init(ObjectClass
*klass
, void *data
)
435 DeviceClass
*dc
= DEVICE_CLASS(klass
);
437 dc
->realize
= imx_spi_realize
;
438 dc
->vmsd
= &vmstate_imx_spi
;
439 dc
->reset
= imx_spi_reset
;
440 dc
->desc
= "i.MX SPI Controller";
443 static const TypeInfo imx_spi_info
= {
444 .name
= TYPE_IMX_SPI
,
445 .parent
= TYPE_SYS_BUS_DEVICE
,
446 .instance_size
= sizeof(IMXSPIState
),
447 .class_init
= imx_spi_class_init
,
450 static void imx_spi_register_types(void)
452 type_register_static(&imx_spi_info
);
455 type_init(imx_spi_register_types
)