2 * IMX7 System Reset Controller
4 * Copyright (c) 2023 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/misc/imx7_src.h"
13 #include "migration/vmstate.h"
14 #include "qemu/bitops.h"
16 #include "qemu/main-loop.h"
17 #include "qemu/module.h"
18 #include "target/arm/arm-powerctl.h"
19 #include "hw/core/cpu.h"
20 #include "hw/registerfields.h"
24 static const char *imx7_src_reg_name(uint32_t reg
)
26 static char unknown
[20];
40 return "SRC_HSICPHY_RCR";
41 case SRC_USBOPHY1_RCR
:
42 return "SRC_USBOPHY1_RCR";
43 case SRC_USBOPHY2_RCR
:
44 return "SRC_USBOPHY2_RCR";
46 return "SRC_PCIEPHY_RCR";
78 snprintf(unknown
, sizeof(unknown
), "%u ?", reg
);
83 static const VMStateDescription vmstate_imx7_src
= {
84 .name
= TYPE_IMX7_SRC
,
86 .minimum_version_id
= 1,
87 .fields
= (const VMStateField
[]) {
88 VMSTATE_UINT32_ARRAY(regs
, IMX7SRCState
, SRC_MAX
),
93 static void imx7_src_reset(DeviceState
*dev
)
95 IMX7SRCState
*s
= IMX7_SRC(dev
);
97 memset(s
->regs
, 0, sizeof(s
->regs
));
99 /* Set reset values */
100 s
->regs
[SRC_SCR
] = 0xA0;
101 s
->regs
[SRC_SRSR
] = 0x1;
102 s
->regs
[SRC_SIMR
] = 0x1F;
105 static uint64_t imx7_src_read(void *opaque
, hwaddr offset
, unsigned size
)
108 IMX7SRCState
*s
= (IMX7SRCState
*)opaque
;
109 uint32_t index
= offset
>> 2;
111 if (index
< SRC_MAX
) {
112 value
= s
->regs
[index
];
114 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
115 HWADDR_PRIx
"\n", TYPE_IMX7_SRC
, __func__
, offset
);
118 trace_imx7_src_read(imx7_src_reg_name(index
), value
);
125 * The reset is asynchronous so we need to defer clearing the reset
126 * bit until the work is completed.
129 struct SRCSCRResetInfo
{
134 static void imx7_clear_reset_bit(CPUState
*cpu
, run_on_cpu_data data
)
136 struct SRCSCRResetInfo
*ri
= data
.host_ptr
;
137 IMX7SRCState
*s
= ri
->s
;
139 assert(bql_locked());
141 s
->regs
[SRC_A7RCR0
] = deposit32(s
->regs
[SRC_A7RCR0
], ri
->reset_bit
, 1, 0);
143 trace_imx7_src_write(imx7_src_reg_name(SRC_A7RCR0
), s
->regs
[SRC_A7RCR0
]);
148 static void imx7_defer_clear_reset_bit(uint32_t cpuid
,
150 uint32_t reset_shift
)
152 struct SRCSCRResetInfo
*ri
;
153 CPUState
*cpu
= arm_get_cpu_by_id(cpuid
);
159 ri
= g_new(struct SRCSCRResetInfo
, 1);
161 ri
->reset_bit
= reset_shift
;
163 async_run_on_cpu(cpu
, imx7_clear_reset_bit
, RUN_ON_CPU_HOST_PTR(ri
));
167 static void imx7_src_write(void *opaque
, hwaddr offset
, uint64_t value
,
170 IMX7SRCState
*s
= (IMX7SRCState
*)opaque
;
171 uint32_t index
= offset
>> 2;
172 long unsigned int change_mask
;
173 uint32_t current_value
= value
;
175 if (index
>= SRC_MAX
) {
176 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
177 HWADDR_PRIx
"\n", TYPE_IMX7_SRC
, __func__
, offset
);
181 trace_imx7_src_write(imx7_src_reg_name(SRC_A7RCR0
), s
->regs
[SRC_A7RCR0
]);
183 change_mask
= s
->regs
[index
] ^ (uint32_t)current_value
;
187 if (FIELD_EX32(change_mask
, CORE0
, RST
)) {
189 imx7_defer_clear_reset_bit(0, s
, R_CORE0_RST_SHIFT
);
191 if (FIELD_EX32(change_mask
, CORE1
, RST
)) {
193 imx7_defer_clear_reset_bit(1, s
, R_CORE1_RST_SHIFT
);
195 s
->regs
[index
] = current_value
;
199 * On real hardware when the system reset controller starts a
200 * secondary CPU it runs through some boot ROM code which reads
201 * the SRC_GPRX registers controlling the start address and branches
203 * Here we are taking a short cut and branching directly to the
204 * requested address (we don't want to run the boot ROM code inside
207 if (FIELD_EX32(change_mask
, CORE1
, ENABLE
)) {
208 if (FIELD_EX32(current_value
, CORE1
, ENABLE
)) {
209 /* CORE 1 is brought up */
210 arm_set_cpu_on(1, s
->regs
[SRC_GPR3
], s
->regs
[SRC_GPR4
],
213 /* CORE 1 is shut down */
216 /* We clear the reset bits as the processor changed state */
217 imx7_defer_clear_reset_bit(1, s
, R_CORE1_RST_SHIFT
);
218 clear_bit(R_CORE1_RST_SHIFT
, &change_mask
);
220 s
->regs
[index
] = current_value
;
223 s
->regs
[index
] = current_value
;
228 static const struct MemoryRegionOps imx7_src_ops
= {
229 .read
= imx7_src_read
,
230 .write
= imx7_src_write
,
231 .endianness
= DEVICE_NATIVE_ENDIAN
,
234 * Our device would not work correctly if the guest was doing
235 * unaligned access. This might not be a limitation on the real
236 * device but in practice there is no reason for a guest to access
237 * this device unaligned.
239 .min_access_size
= 4,
240 .max_access_size
= 4,
245 static void imx7_src_realize(DeviceState
*dev
, Error
**errp
)
247 IMX7SRCState
*s
= IMX7_SRC(dev
);
249 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &imx7_src_ops
, s
,
250 TYPE_IMX7_SRC
, 0x1000);
251 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &s
->iomem
);
254 static void imx7_src_class_init(ObjectClass
*klass
, void *data
)
256 DeviceClass
*dc
= DEVICE_CLASS(klass
);
258 dc
->realize
= imx7_src_realize
;
259 device_class_set_legacy_reset(dc
, imx7_src_reset
);
260 dc
->vmsd
= &vmstate_imx7_src
;
261 dc
->desc
= "i.MX6 System Reset Controller";
264 static const TypeInfo imx7_src_info
= {
265 .name
= TYPE_IMX7_SRC
,
266 .parent
= TYPE_SYS_BUS_DEVICE
,
267 .instance_size
= sizeof(IMX7SRCState
),
268 .class_init
= imx7_src_class_init
,
271 static void imx7_src_register_types(void)
273 type_register_static(&imx7_src_info
);
276 type_init(imx7_src_register_types
)