2 * ADC registers for Xilinx Zynq Platform
4 * Copyright (c) 2015 Guenter Roeck
5 * Based on hw/misc/zynq_slcr.c, written by Michal Simek
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, see <http://www.gnu.org/licenses/>.
16 #include "qemu/osdep.h"
18 #include "hw/misc/zynq-xadc.h"
19 #include "qemu/timer.h"
20 #include "sysemu/sysemu.h"
22 #include "qemu/module.h"
34 #define CFG_ENABLE BIT(31)
35 #define CFG_CFIFOTH_SHIFT 20
36 #define CFG_CFIFOTH_LENGTH 4
37 #define CFG_DFIFOTH_SHIFT 16
38 #define CFG_DFIFOTH_LENGTH 4
39 #define CFG_WEDGE BIT(13)
40 #define CFG_REDGE BIT(12)
41 #define CFG_TCKRATE_SHIFT 8
42 #define CFG_TCKRATE_LENGTH 2
44 #define CFG_TCKRATE_DIV(x) (0x1 << (x - 1))
46 #define CFG_IGAP_SHIFT 0
47 #define CFG_IGAP_LENGTH 5
49 #define INT_CFIFO_LTH BIT(9)
50 #define INT_DFIFO_GTH BIT(8)
52 #define INT_ALM_SHIFT 0
53 #define INT_ALM_LENGTH 7
54 #define INT_ALM_MASK (((1 << INT_ALM_LENGTH) - 1) << INT_ALM_SHIFT)
56 #define INT_ALL (INT_CFIFO_LTH | INT_DFIFO_GTH | INT_OT | INT_ALM_MASK)
58 #define MSTS_CFIFO_LVL_SHIFT 16
59 #define MSTS_CFIFO_LVL_LENGTH 4
60 #define MSTS_DFIFO_LVL_SHIFT 12
61 #define MSTS_DFIFO_LVL_LENGTH 4
62 #define MSTS_CFIFOF BIT(11)
63 #define MSTS_CFIFOE BIT(10)
64 #define MSTS_DFIFOF BIT(9)
65 #define MSTS_DFIFOE BIT(8)
66 #define MSTS_OT BIT(7)
67 #define MSTS_ALM_SHIFT 0
68 #define MSTS_ALM_LENGTH 7
70 #define MCTL_RESET BIT(4)
74 #define CMD_WRITE 0x02
76 static void zynq_xadc_update_ints(ZynqXADCState
*s
)
79 /* We are fast, commands are actioned instantly so the CFIFO is always
80 * empty (and below threshold).
82 s
->regs
[INT_STS
] |= INT_CFIFO_LTH
;
84 if (s
->xadc_dfifo_entries
>
85 extract32(s
->regs
[CFG
], CFG_DFIFOTH_SHIFT
, CFG_DFIFOTH_LENGTH
)) {
86 s
->regs
[INT_STS
] |= INT_DFIFO_GTH
;
89 qemu_set_irq(s
->qemu_irq
, !!(s
->regs
[INT_STS
] & ~s
->regs
[INT_MASK
]));
92 static void zynq_xadc_reset(DeviceState
*d
)
94 ZynqXADCState
*s
= ZYNQ_XADC(d
);
96 s
->regs
[CFG
] = 0x14 << CFG_IGAP_SHIFT
|
97 CFG_TCKRATE_DIV(4) << CFG_TCKRATE_SHIFT
| CFG_REDGE
;
98 s
->regs
[INT_STS
] = INT_CFIFO_LTH
;
99 s
->regs
[INT_MASK
] = 0xffffffff;
100 s
->regs
[CMDFIFO
] = 0;
102 s
->regs
[MCTL
] = MCTL_RESET
;
104 memset(s
->xadc_regs
, 0, sizeof(s
->xadc_regs
));
105 memset(s
->xadc_dfifo
, 0, sizeof(s
->xadc_dfifo
));
106 s
->xadc_dfifo_entries
= 0;
108 zynq_xadc_update_ints(s
);
111 static uint16_t xadc_pop_dfifo(ZynqXADCState
*s
)
113 uint16_t rv
= s
->xadc_dfifo
[0];
116 if (s
->xadc_dfifo_entries
> 0) {
117 s
->xadc_dfifo_entries
--;
119 for (i
= 0; i
< s
->xadc_dfifo_entries
; i
++) {
120 s
->xadc_dfifo
[i
] = s
->xadc_dfifo
[i
+ 1];
122 s
->xadc_dfifo
[s
->xadc_dfifo_entries
] = 0;
123 zynq_xadc_update_ints(s
);
127 static void xadc_push_dfifo(ZynqXADCState
*s
, uint16_t regval
)
129 if (s
->xadc_dfifo_entries
< ZYNQ_XADC_FIFO_DEPTH
) {
130 s
->xadc_dfifo
[s
->xadc_dfifo_entries
++] = s
->xadc_read_reg_previous
;
132 s
->xadc_read_reg_previous
= regval
;
133 zynq_xadc_update_ints(s
);
136 static bool zynq_xadc_check_offset(hwaddr offset
, bool rnw
)
146 return rnw
; /* read only */
148 return !rnw
; /* write only */
154 static uint64_t zynq_xadc_read(void *opaque
, hwaddr offset
, unsigned size
)
156 ZynqXADCState
*s
= opaque
;
157 int reg
= offset
/ 4;
160 if (!zynq_xadc_check_offset(reg
, true)) {
161 qemu_log_mask(LOG_GUEST_ERROR
, "zynq_xadc: Invalid read access to "
162 "addr %" HWADDR_PRIx
"\n", offset
);
175 rv
|= s
->xadc_dfifo_entries
<< MSTS_DFIFO_LVL_SHIFT
;
176 if (!s
->xadc_dfifo_entries
) {
178 } else if (s
->xadc_dfifo_entries
== ZYNQ_XADC_FIFO_DEPTH
) {
183 rv
= xadc_pop_dfifo(s
);
189 static void zynq_xadc_write(void *opaque
, hwaddr offset
, uint64_t val
,
192 ZynqXADCState
*s
= (ZynqXADCState
*)opaque
;
193 int reg
= offset
/ 4;
198 if (!zynq_xadc_check_offset(reg
, false)) {
199 qemu_log_mask(LOG_GUEST_ERROR
, "zynq_xadc: Invalid write access "
200 "to addr %" HWADDR_PRIx
"\n", offset
);
209 s
->regs
[INT_STS
] &= ~val
;
212 s
->regs
[INT_MASK
] = val
& INT_ALL
;
215 xadc_cmd
= extract32(val
, 26, 4);
216 xadc_reg
= extract32(val
, 16, 10);
217 xadc_data
= extract32(val
, 0, 16);
219 if (s
->regs
[MCTL
] & MCTL_RESET
) {
220 qemu_log_mask(LOG_GUEST_ERROR
, "zynq_xadc: Sending command "
221 "while comm channel held in reset: %" PRIx32
"\n",
226 if (xadc_reg
>= ZYNQ_XADC_NUM_ADC_REGS
&& xadc_cmd
!= CMD_NOP
) {
227 qemu_log_mask(LOG_GUEST_ERROR
, "read/write op to invalid xadc "
228 "reg 0x%x\n", xadc_reg
);
234 xadc_push_dfifo(s
, s
->xadc_regs
[xadc_reg
]);
237 s
->xadc_regs
[xadc_reg
] = xadc_data
;
240 xadc_push_dfifo(s
, 0);
245 s
->regs
[MCTL
] = val
& 0x00fffeff;
248 zynq_xadc_update_ints(s
);
251 static const MemoryRegionOps xadc_ops
= {
252 .read
= zynq_xadc_read
,
253 .write
= zynq_xadc_write
,
254 .endianness
= DEVICE_NATIVE_ENDIAN
,
257 static void zynq_xadc_init(Object
*obj
)
259 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
260 ZynqXADCState
*s
= ZYNQ_XADC(obj
);
262 memory_region_init_io(&s
->iomem
, obj
, &xadc_ops
, s
, "zynq-xadc",
263 ZYNQ_XADC_MMIO_SIZE
);
264 sysbus_init_mmio(sbd
, &s
->iomem
);
265 sysbus_init_irq(sbd
, &s
->qemu_irq
);
268 static const VMStateDescription vmstate_zynq_xadc
= {
271 .minimum_version_id
= 1,
272 .fields
= (VMStateField
[]) {
273 VMSTATE_UINT32_ARRAY(regs
, ZynqXADCState
, ZYNQ_XADC_NUM_IO_REGS
),
274 VMSTATE_UINT16_ARRAY(xadc_regs
, ZynqXADCState
,
275 ZYNQ_XADC_NUM_ADC_REGS
),
276 VMSTATE_UINT16_ARRAY(xadc_dfifo
, ZynqXADCState
,
277 ZYNQ_XADC_FIFO_DEPTH
),
278 VMSTATE_UINT16(xadc_read_reg_previous
, ZynqXADCState
),
279 VMSTATE_UINT16(xadc_dfifo_entries
, ZynqXADCState
),
280 VMSTATE_END_OF_LIST()
284 static void zynq_xadc_class_init(ObjectClass
*klass
, void *data
)
286 DeviceClass
*dc
= DEVICE_CLASS(klass
);
288 dc
->vmsd
= &vmstate_zynq_xadc
;
289 dc
->reset
= zynq_xadc_reset
;
292 static const TypeInfo zynq_xadc_info
= {
293 .class_init
= zynq_xadc_class_init
,
294 .name
= TYPE_ZYNQ_XADC
,
295 .parent
= TYPE_SYS_BUS_DEVICE
,
296 .instance_size
= sizeof(ZynqXADCState
),
297 .instance_init
= zynq_xadc_init
,
300 static void zynq_xadc_register_types(void)
302 type_register_static(&zynq_xadc_info
);
305 type_init(zynq_xadc_register_types
)