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
32 #ifdef XILINX_SPIPS_ERR_DEBUG
33 #define DB_PRINT(...) do { \
34 fprintf(stderr, ": %s: ", __func__); \
35 fprintf(stderr, ## __VA_ARGS__); \
42 #define R_CONFIG (0x00 / 4)
43 #define MODEFAIL_GEN_EN (1 << 17)
44 #define MAN_START_COM (1 << 16)
45 #define MAN_START_EN (1 << 15)
46 #define MANUAL_CS (1 << 14)
47 #define CS (0xF << 10)
49 #define PERI_SEL (1 << 9)
50 #define REF_CLK (1 << 8)
51 #define FIFO_WIDTH (3 << 6)
52 #define BAUD_RATE_DIV (7 << 3)
53 #define CLK_PH (1 << 2)
54 #define CLK_POL (1 << 1)
55 #define MODE_SEL (1 << 0)
57 /* interrupt mechanism */
58 #define R_INTR_STATUS (0x04 / 4)
59 #define R_INTR_EN (0x08 / 4)
60 #define R_INTR_DIS (0x0C / 4)
61 #define R_INTR_MASK (0x10 / 4)
62 #define IXR_TX_FIFO_UNDERFLOW (1 << 6)
63 #define IXR_RX_FIFO_FULL (1 << 5)
64 #define IXR_RX_FIFO_NOT_EMPTY (1 << 4)
65 #define IXR_TX_FIFO_FULL (1 << 3)
66 #define IXR_TX_FIFO_NOT_FULL (1 << 2)
67 #define IXR_TX_FIFO_MODE_FAIL (1 << 1)
68 #define IXR_RX_FIFO_OVERFLOW (1 << 0)
69 #define IXR_ALL ((IXR_TX_FIFO_UNDERFLOW<<1)-1)
71 #define R_EN (0x14 / 4)
72 #define R_DELAY (0x18 / 4)
73 #define R_TX_DATA (0x1C / 4)
74 #define R_RX_DATA (0x20 / 4)
75 #define R_SLAVE_IDLE_COUNT (0x24 / 4)
76 #define R_TX_THRES (0x28 / 4)
77 #define R_RX_THRES (0x2C / 4)
78 #define R_MOD_ID (0xFC / 4)
80 #define R_MAX (R_MOD_ID+1)
82 /* size of TXRX FIFOs */
83 #define NUM_CS_LINES 4
93 qemu_irq cs_lines
[NUM_CS_LINES
];
102 static void xilinx_spips_update_cs_lines(XilinxSPIPS
*s
)
106 int field
= s
->regs
[R_CONFIG
] >> CS_SHIFT
;
108 for (i
= 0; i
< NUM_CS_LINES
; i
++) {
109 if (~field
& (1 << i
) && !found
) {
111 DB_PRINT("selecting slave %d\n", i
);
112 qemu_set_irq(s
->cs_lines
[i
], 0);
114 qemu_set_irq(s
->cs_lines
[i
], 1);
119 static void xilinx_spips_update_ixr(XilinxSPIPS
*s
)
121 /* These are set/cleared as they occur */
122 s
->regs
[R_INTR_STATUS
] &= (IXR_TX_FIFO_UNDERFLOW
| IXR_RX_FIFO_OVERFLOW
|
123 IXR_TX_FIFO_MODE_FAIL
);
124 /* these are pure functions of fifo state, set them here */
125 s
->regs
[R_INTR_STATUS
] |=
126 (fifo8_is_full(&s
->rx_fifo
) ? IXR_RX_FIFO_FULL
: 0) |
127 (s
->rx_fifo
.num
>= s
->regs
[R_RX_THRES
] ? IXR_RX_FIFO_NOT_EMPTY
: 0) |
128 (fifo8_is_full(&s
->tx_fifo
) ? IXR_TX_FIFO_FULL
: 0) |
129 (s
->tx_fifo
.num
< s
->regs
[R_TX_THRES
] ? IXR_TX_FIFO_NOT_FULL
: 0);
130 /* drive external interrupt pin */
131 int new_irqline
= !!(s
->regs
[R_INTR_MASK
] & s
->regs
[R_INTR_STATUS
] &
133 if (new_irqline
!= s
->irqline
) {
134 s
->irqline
= new_irqline
;
135 qemu_set_irq(s
->irq
, s
->irqline
);
139 static void xilinx_spips_reset(DeviceState
*d
)
141 XilinxSPIPS
*s
= DO_UPCAST(XilinxSPIPS
, busdev
.qdev
, d
);
144 for (i
= 0; i
< R_MAX
; i
++) {
148 fifo8_reset(&s
->rx_fifo
);
149 fifo8_reset(&s
->rx_fifo
);
150 /* non zero resets */
151 s
->regs
[R_CONFIG
] |= MODEFAIL_GEN_EN
;
152 s
->regs
[R_SLAVE_IDLE_COUNT
] = 0xFF;
153 s
->regs
[R_TX_THRES
] = 1;
154 s
->regs
[R_RX_THRES
] = 1;
155 /* FIXME: move magic number definition somewhere sensible */
156 s
->regs
[R_MOD_ID
] = 0x01090106;
157 xilinx_spips_update_ixr(s
);
158 xilinx_spips_update_cs_lines(s
);
161 static void xilinx_spips_flush_txfifo(XilinxSPIPS
*s
)
167 if (fifo8_is_empty(&s
->tx_fifo
)) {
168 s
->regs
[R_INTR_STATUS
] |= IXR_TX_FIFO_UNDERFLOW
;
171 value
= fifo8_pop(&s
->tx_fifo
);
174 r
= ssi_transfer(s
->spi
, (uint32_t)value
);
175 DB_PRINT("tx = %02x rx = %02x\n", value
, r
);
176 if (fifo8_is_full(&s
->rx_fifo
)) {
177 s
->regs
[R_INTR_STATUS
] |= IXR_RX_FIFO_OVERFLOW
;
178 DB_PRINT("rx FIFO overflow");
180 fifo8_push(&s
->rx_fifo
, (uint8_t)r
);
183 xilinx_spips_update_ixr(s
);
186 static uint64_t xilinx_spips_read(void *opaque
, target_phys_addr_t addr
,
189 XilinxSPIPS
*s
= opaque
;
205 case R_SLAVE_IDLE_COUNT
:
217 ret
= (uint32_t)fifo8_pop(&s
->rx_fifo
);
218 DB_PRINT("addr=" TARGET_FMT_plx
" = %x\n", addr
* 4, ret
);
219 xilinx_spips_update_ixr(s
);
222 DB_PRINT("addr=" TARGET_FMT_plx
" = %x\n", addr
* 4, s
->regs
[addr
] & mask
);
223 return s
->regs
[addr
] & mask
;
227 static void xilinx_spips_write(void *opaque
, target_phys_addr_t addr
,
228 uint64_t value
, unsigned size
)
231 int man_start_com
= 0;
232 XilinxSPIPS
*s
= opaque
;
234 DB_PRINT("addr=" TARGET_FMT_plx
" = %x\n", addr
, (unsigned)value
);
239 if (value
& MAN_START_COM
) {
245 s
->regs
[R_INTR_STATUS
] &= ~(mask
& value
);
249 s
->regs
[R_INTR_MASK
] &= ~(mask
& value
);
253 s
->regs
[R_INTR_MASK
] |= mask
& value
;
258 case R_SLAVE_IDLE_COUNT
:
267 fifo8_push(&s
->tx_fifo
, (uint8_t)value
);
270 s
->regs
[addr
] = (s
->regs
[addr
] & ~mask
) | (value
& mask
);
273 xilinx_spips_flush_txfifo(s
);
275 xilinx_spips_update_ixr(s
);
276 xilinx_spips_update_cs_lines(s
);
279 static const MemoryRegionOps spips_ops
= {
280 .read
= xilinx_spips_read
,
281 .write
= xilinx_spips_write
,
282 .endianness
= DEVICE_LITTLE_ENDIAN
,
285 static int xilinx_spips_init(SysBusDevice
*dev
)
287 XilinxSPIPS
*s
= FROM_SYSBUS(typeof(*s
), dev
);
290 DB_PRINT("inited device model\n");
292 s
->spi
= ssi_create_bus(&dev
->qdev
, "spi");
294 ssi_auto_connect_slaves(DEVICE(s
), s
->cs_lines
, s
->spi
);
295 sysbus_init_irq(dev
, &s
->irq
);
296 for (i
= 0; i
< NUM_CS_LINES
; ++i
) {
297 sysbus_init_irq(dev
, &s
->cs_lines
[i
]);
300 memory_region_init_io(&s
->iomem
, &spips_ops
, s
, "spi", R_MAX
*4);
301 sysbus_init_mmio(dev
, &s
->iomem
);
305 fifo8_create(&s
->rx_fifo
, RXFF_A
);
306 fifo8_create(&s
->tx_fifo
, TXFF_A
);
311 static int xilinx_spips_post_load(void *opaque
, int version_id
)
313 xilinx_spips_update_ixr((XilinxSPIPS
*)opaque
);
314 xilinx_spips_update_cs_lines((XilinxSPIPS
*)opaque
);
318 static const VMStateDescription vmstate_xilinx_spips
= {
319 .name
= "xilinx_spips",
321 .minimum_version_id
= 1,
322 .minimum_version_id_old
= 1,
323 .post_load
= xilinx_spips_post_load
,
324 .fields
= (VMStateField
[]) {
325 VMSTATE_FIFO8(tx_fifo
, XilinxSPIPS
),
326 VMSTATE_FIFO8(rx_fifo
, XilinxSPIPS
),
327 VMSTATE_UINT32_ARRAY(regs
, XilinxSPIPS
, R_MAX
),
328 VMSTATE_END_OF_LIST()
332 static void xilinx_spips_class_init(ObjectClass
*klass
, void *data
)
334 DeviceClass
*dc
= DEVICE_CLASS(klass
);
335 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
337 sdc
->init
= xilinx_spips_init
;
338 dc
->reset
= xilinx_spips_reset
;
339 dc
->vmsd
= &vmstate_xilinx_spips
;
342 static const TypeInfo xilinx_spips_info
= {
343 .name
= "xilinx,spips",
344 .parent
= TYPE_SYS_BUS_DEVICE
,
345 .instance_size
= sizeof(XilinxSPIPS
),
346 .class_init
= xilinx_spips_class_init
,
349 static void xilinx_spips_register_types(void)
351 type_register_static(&xilinx_spips_info
);
354 type_init(xilinx_spips_register_types
)