2 * QEMU model of the Clock-Reset-LPD (CRL).
4 * Copyright (c) 2022 Advanced Micro Devices, Inc.
5 * SPDX-License-Identifier: GPL-2.0-or-later
7 * Written by Edgar E. Iglesias <edgar.iglesias@amd.com>
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
13 #include "qemu/bitops.h"
14 #include "migration/vmstate.h"
15 #include "hw/qdev-properties.h"
16 #include "hw/sysbus.h"
18 #include "hw/register.h"
19 #include "hw/resettable.h"
21 #include "target/arm/arm-powerctl.h"
22 #include "hw/misc/xlnx-versal-crl.h"
24 #ifndef XLNX_VERSAL_CRL_ERR_DEBUG
25 #define XLNX_VERSAL_CRL_ERR_DEBUG 0
28 static void crl_update_irq(XlnxVersalCRL
*s
)
30 bool pending
= s
->regs
[R_IR_STATUS
] & ~s
->regs
[R_IR_MASK
];
31 qemu_set_irq(s
->irq
, pending
);
34 static void crl_status_postw(RegisterInfo
*reg
, uint64_t val64
)
36 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(reg
->opaque
);
40 static uint64_t crl_enable_prew(RegisterInfo
*reg
, uint64_t val64
)
42 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(reg
->opaque
);
45 s
->regs
[R_IR_MASK
] &= ~val
;
50 static uint64_t crl_disable_prew(RegisterInfo
*reg
, uint64_t val64
)
52 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(reg
->opaque
);
55 s
->regs
[R_IR_MASK
] |= val
;
60 static void crl_reset_dev(XlnxVersalCRL
*s
, DeviceState
*dev
,
61 bool rst_old
, bool rst_new
)
63 device_cold_reset(dev
);
66 static void crl_reset_cpu(XlnxVersalCRL
*s
, ARMCPU
*armcpu
,
67 bool rst_old
, bool rst_new
)
70 arm_set_cpu_off(armcpu
->mp_affinity
);
72 arm_set_cpu_on_and_reset(armcpu
->mp_affinity
);
76 #define REGFIELD_RESET(type, s, reg, f, new_val, dev) { \
77 bool old_f = ARRAY_FIELD_EX32((s)->regs, reg, f); \
78 bool new_f = FIELD_EX32(new_val, reg, f); \
81 if (dev && old_f != new_f) { \
82 crl_reset_ ## type(s, dev, old_f, new_f); \
86 static uint64_t crl_rst_r5_prew(RegisterInfo
*reg
, uint64_t val64
)
88 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(reg
->opaque
);
90 REGFIELD_RESET(cpu
, s
, RST_CPU_R5
, RESET_CPU0
, val64
, s
->cfg
.cpu_r5
[0]);
91 REGFIELD_RESET(cpu
, s
, RST_CPU_R5
, RESET_CPU1
, val64
, s
->cfg
.cpu_r5
[1]);
95 static uint64_t crl_rst_adma_prew(RegisterInfo
*reg
, uint64_t val64
)
97 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(reg
->opaque
);
100 /* A single register fans out to all ADMA reset inputs. */
101 for (i
= 0; i
< ARRAY_SIZE(s
->cfg
.adma
); i
++) {
102 REGFIELD_RESET(dev
, s
, RST_ADMA
, RESET
, val64
, s
->cfg
.adma
[i
]);
107 static uint64_t crl_rst_uart0_prew(RegisterInfo
*reg
, uint64_t val64
)
109 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(reg
->opaque
);
111 REGFIELD_RESET(dev
, s
, RST_UART0
, RESET
, val64
, s
->cfg
.uart
[0]);
115 static uint64_t crl_rst_uart1_prew(RegisterInfo
*reg
, uint64_t val64
)
117 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(reg
->opaque
);
119 REGFIELD_RESET(dev
, s
, RST_UART1
, RESET
, val64
, s
->cfg
.uart
[1]);
123 static uint64_t crl_rst_gem0_prew(RegisterInfo
*reg
, uint64_t val64
)
125 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(reg
->opaque
);
127 REGFIELD_RESET(dev
, s
, RST_GEM0
, RESET
, val64
, s
->cfg
.gem
[0]);
131 static uint64_t crl_rst_gem1_prew(RegisterInfo
*reg
, uint64_t val64
)
133 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(reg
->opaque
);
135 REGFIELD_RESET(dev
, s
, RST_GEM1
, RESET
, val64
, s
->cfg
.gem
[1]);
139 static uint64_t crl_rst_usb_prew(RegisterInfo
*reg
, uint64_t val64
)
141 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(reg
->opaque
);
143 REGFIELD_RESET(dev
, s
, RST_USB0
, RESET
, val64
, s
->cfg
.usb
);
147 static const RegisterAccessInfo crl_regs_info
[] = {
148 { .name
= "ERR_CTRL", .addr
= A_ERR_CTRL
,
149 },{ .name
= "IR_STATUS", .addr
= A_IR_STATUS
,
151 .post_write
= crl_status_postw
,
152 },{ .name
= "IR_MASK", .addr
= A_IR_MASK
,
155 },{ .name
= "IR_ENABLE", .addr
= A_IR_ENABLE
,
156 .pre_write
= crl_enable_prew
,
157 },{ .name
= "IR_DISABLE", .addr
= A_IR_DISABLE
,
158 .pre_write
= crl_disable_prew
,
159 },{ .name
= "WPROT", .addr
= A_WPROT
,
160 },{ .name
= "PLL_CLK_OTHER_DMN", .addr
= A_PLL_CLK_OTHER_DMN
,
163 },{ .name
= "RPLL_CTRL", .addr
= A_RPLL_CTRL
,
166 },{ .name
= "RPLL_CFG", .addr
= A_RPLL_CFG
,
169 },{ .name
= "RPLL_FRAC_CFG", .addr
= A_RPLL_FRAC_CFG
,
171 },{ .name
= "PLL_STATUS", .addr
= A_PLL_STATUS
,
172 .reset
= R_PLL_STATUS_RPLL_STABLE_MASK
|
173 R_PLL_STATUS_RPLL_LOCK_MASK
,
176 },{ .name
= "RPLL_TO_XPD_CTRL", .addr
= A_RPLL_TO_XPD_CTRL
,
179 },{ .name
= "LPD_TOP_SWITCH_CTRL", .addr
= A_LPD_TOP_SWITCH_CTRL
,
182 },{ .name
= "LPD_LSBUS_CTRL", .addr
= A_LPD_LSBUS_CTRL
,
185 },{ .name
= "CPU_R5_CTRL", .addr
= A_CPU_R5_CTRL
,
188 },{ .name
= "IOU_SWITCH_CTRL", .addr
= A_IOU_SWITCH_CTRL
,
191 },{ .name
= "GEM0_REF_CTRL", .addr
= A_GEM0_REF_CTRL
,
194 },{ .name
= "GEM1_REF_CTRL", .addr
= A_GEM1_REF_CTRL
,
197 },{ .name
= "GEM_TSU_REF_CTRL", .addr
= A_GEM_TSU_REF_CTRL
,
200 },{ .name
= "USB0_BUS_REF_CTRL", .addr
= A_USB0_BUS_REF_CTRL
,
203 },{ .name
= "UART0_REF_CTRL", .addr
= A_UART0_REF_CTRL
,
206 },{ .name
= "UART1_REF_CTRL", .addr
= A_UART1_REF_CTRL
,
209 },{ .name
= "SPI0_REF_CTRL", .addr
= A_SPI0_REF_CTRL
,
212 },{ .name
= "SPI1_REF_CTRL", .addr
= A_SPI1_REF_CTRL
,
215 },{ .name
= "CAN0_REF_CTRL", .addr
= A_CAN0_REF_CTRL
,
218 },{ .name
= "CAN1_REF_CTRL", .addr
= A_CAN1_REF_CTRL
,
221 },{ .name
= "I2C0_REF_CTRL", .addr
= A_I2C0_REF_CTRL
,
224 },{ .name
= "I2C1_REF_CTRL", .addr
= A_I2C1_REF_CTRL
,
227 },{ .name
= "DBG_LPD_CTRL", .addr
= A_DBG_LPD_CTRL
,
230 },{ .name
= "TIMESTAMP_REF_CTRL", .addr
= A_TIMESTAMP_REF_CTRL
,
233 },{ .name
= "CRL_SAFETY_CHK", .addr
= A_CRL_SAFETY_CHK
,
234 },{ .name
= "PSM_REF_CTRL", .addr
= A_PSM_REF_CTRL
,
237 },{ .name
= "DBG_TSTMP_CTRL", .addr
= A_DBG_TSTMP_CTRL
,
240 },{ .name
= "CPM_TOPSW_REF_CTRL", .addr
= A_CPM_TOPSW_REF_CTRL
,
243 },{ .name
= "USB3_DUAL_REF_CTRL", .addr
= A_USB3_DUAL_REF_CTRL
,
246 },{ .name
= "RST_CPU_R5", .addr
= A_RST_CPU_R5
,
249 .pre_write
= crl_rst_r5_prew
,
250 },{ .name
= "RST_ADMA", .addr
= A_RST_ADMA
,
252 .pre_write
= crl_rst_adma_prew
,
253 },{ .name
= "RST_GEM0", .addr
= A_RST_GEM0
,
255 .pre_write
= crl_rst_gem0_prew
,
256 },{ .name
= "RST_GEM1", .addr
= A_RST_GEM1
,
258 .pre_write
= crl_rst_gem1_prew
,
259 },{ .name
= "RST_SPARE", .addr
= A_RST_SPARE
,
261 },{ .name
= "RST_USB0", .addr
= A_RST_USB0
,
263 .pre_write
= crl_rst_usb_prew
,
264 },{ .name
= "RST_UART0", .addr
= A_RST_UART0
,
266 .pre_write
= crl_rst_uart0_prew
,
267 },{ .name
= "RST_UART1", .addr
= A_RST_UART1
,
269 .pre_write
= crl_rst_uart1_prew
,
270 },{ .name
= "RST_SPI0", .addr
= A_RST_SPI0
,
272 },{ .name
= "RST_SPI1", .addr
= A_RST_SPI1
,
274 },{ .name
= "RST_CAN0", .addr
= A_RST_CAN0
,
276 },{ .name
= "RST_CAN1", .addr
= A_RST_CAN1
,
278 },{ .name
= "RST_I2C0", .addr
= A_RST_I2C0
,
280 },{ .name
= "RST_I2C1", .addr
= A_RST_I2C1
,
282 },{ .name
= "RST_DBG_LPD", .addr
= A_RST_DBG_LPD
,
285 },{ .name
= "RST_GPIO", .addr
= A_RST_GPIO
,
287 },{ .name
= "RST_TTC", .addr
= A_RST_TTC
,
289 },{ .name
= "RST_TIMESTAMP", .addr
= A_RST_TIMESTAMP
,
291 },{ .name
= "RST_SWDT", .addr
= A_RST_SWDT
,
293 },{ .name
= "RST_OCM", .addr
= A_RST_OCM
,
294 },{ .name
= "RST_IPI", .addr
= A_RST_IPI
,
295 },{ .name
= "RST_FPD", .addr
= A_RST_FPD
,
297 },{ .name
= "PSM_RST_MODE", .addr
= A_PSM_RST_MODE
,
303 static void crl_reset_enter(Object
*obj
, ResetType type
)
305 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(obj
);
308 for (i
= 0; i
< ARRAY_SIZE(s
->regs_info
); ++i
) {
309 register_reset(&s
->regs_info
[i
]);
313 static void crl_reset_hold(Object
*obj
)
315 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(obj
);
320 static const MemoryRegionOps crl_ops
= {
321 .read
= register_read_memory
,
322 .write
= register_write_memory
,
323 .endianness
= DEVICE_LITTLE_ENDIAN
,
325 .min_access_size
= 4,
326 .max_access_size
= 4,
330 static void crl_init(Object
*obj
)
332 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(obj
);
333 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
337 register_init_block32(DEVICE(obj
), crl_regs_info
,
338 ARRAY_SIZE(crl_regs_info
),
339 s
->regs_info
, s
->regs
,
341 XLNX_VERSAL_CRL_ERR_DEBUG
,
343 sysbus_init_mmio(sbd
, &s
->reg_array
->mem
);
344 sysbus_init_irq(sbd
, &s
->irq
);
346 for (i
= 0; i
< ARRAY_SIZE(s
->cfg
.cpu_r5
); ++i
) {
347 object_property_add_link(obj
, "cpu_r5[*]", TYPE_ARM_CPU
,
348 (Object
**)&s
->cfg
.cpu_r5
[i
],
349 qdev_prop_allow_set_link_before_realize
,
350 OBJ_PROP_LINK_STRONG
);
353 for (i
= 0; i
< ARRAY_SIZE(s
->cfg
.adma
); ++i
) {
354 object_property_add_link(obj
, "adma[*]", TYPE_DEVICE
,
355 (Object
**)&s
->cfg
.adma
[i
],
356 qdev_prop_allow_set_link_before_realize
,
357 OBJ_PROP_LINK_STRONG
);
360 for (i
= 0; i
< ARRAY_SIZE(s
->cfg
.uart
); ++i
) {
361 object_property_add_link(obj
, "uart[*]", TYPE_DEVICE
,
362 (Object
**)&s
->cfg
.uart
[i
],
363 qdev_prop_allow_set_link_before_realize
,
364 OBJ_PROP_LINK_STRONG
);
367 for (i
= 0; i
< ARRAY_SIZE(s
->cfg
.gem
); ++i
) {
368 object_property_add_link(obj
, "gem[*]", TYPE_DEVICE
,
369 (Object
**)&s
->cfg
.gem
[i
],
370 qdev_prop_allow_set_link_before_realize
,
371 OBJ_PROP_LINK_STRONG
);
374 object_property_add_link(obj
, "usb", TYPE_DEVICE
,
375 (Object
**)&s
->cfg
.gem
[i
],
376 qdev_prop_allow_set_link_before_realize
,
377 OBJ_PROP_LINK_STRONG
);
380 static void crl_finalize(Object
*obj
)
382 XlnxVersalCRL
*s
= XLNX_VERSAL_CRL(obj
);
383 register_finalize_block(s
->reg_array
);
386 static const VMStateDescription vmstate_crl
= {
387 .name
= TYPE_XLNX_VERSAL_CRL
,
389 .minimum_version_id
= 1,
390 .fields
= (VMStateField
[]) {
391 VMSTATE_UINT32_ARRAY(regs
, XlnxVersalCRL
, CRL_R_MAX
),
392 VMSTATE_END_OF_LIST(),
396 static void crl_class_init(ObjectClass
*klass
, void *data
)
398 ResettableClass
*rc
= RESETTABLE_CLASS(klass
);
399 DeviceClass
*dc
= DEVICE_CLASS(klass
);
401 dc
->vmsd
= &vmstate_crl
;
403 rc
->phases
.enter
= crl_reset_enter
;
404 rc
->phases
.hold
= crl_reset_hold
;
407 static const TypeInfo crl_info
= {
408 .name
= TYPE_XLNX_VERSAL_CRL
,
409 .parent
= TYPE_SYS_BUS_DEVICE
,
410 .instance_size
= sizeof(XlnxVersalCRL
),
411 .class_init
= crl_class_init
,
412 .instance_init
= crl_init
,
413 .instance_finalize
= crl_finalize
,
416 static void crl_register_types(void)
418 type_register_static(&crl_info
);
421 type_init(crl_register_types
)