2 * IMX6 System Reset Controller
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/imx6_src.h"
13 #include "sysemu/sysemu.h"
14 #include "qemu/bitops.h"
16 #include "arm-powerctl.h"
19 #ifndef DEBUG_IMX6_SRC
20 #define DEBUG_IMX6_SRC 0
23 #define DPRINTF(fmt, args...) \
25 if (DEBUG_IMX6_SRC) { \
26 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX6_SRC, \
31 static const char *imx6_src_reg_name(uint32_t reg)
33 static char unknown[20];
69 sprintf(unknown, "%d ?", reg);
74 static const VMStateDescription vmstate_imx6_src = {
75 .name = TYPE_IMX6_SRC,
77 .minimum_version_id = 1,
78 .fields = (VMStateField[]) {
79 VMSTATE_UINT32_ARRAY(regs, IMX6SRCState, SRC_MAX),
84 static void imx6_src_reset(DeviceState *dev)
86 IMX6SRCState *s = IMX6_SRC(dev);
90 memset(s->regs, 0, sizeof(s->regs));
92 /* Set reset values */
93 s->regs[SRC_SCR] = 0x521;
94 s->regs[SRC_SRSR] = 0x1;
95 s->regs[SRC_SIMR] = 0x1F;
98 static uint64_t imx6_src_read(void *opaque, hwaddr offset, unsigned size)
101 IMX6SRCState *s = (IMX6SRCState *)opaque;
102 uint32_t index = offset >> 2;
104 if (index < SRC_MAX) {
105 value = s->regs[index];
107 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
108 HWADDR_PRIx "\n", TYPE_IMX6_SRC, __func__, offset);
112 DPRINTF("reg[%s] => 0x%" PRIx32 "\n", imx6_src_reg_name(index), value);
118 /* The reset is asynchronous so we need to defer clearing the reset
119 * bit until the work is completed.
122 struct SRCSCRResetInfo {
127 static void imx6_clear_reset_bit(CPUState *cpu, run_on_cpu_data data)
129 struct SRCSCRResetInfo *ri = data.host_ptr;
130 IMX6SRCState *s = ri->s;
132 assert(qemu_mutex_iothread_locked());
134 s->regs[SRC_SCR] = deposit32(s->regs[SRC_SCR], ri->reset_bit, 1, 0);
135 DPRINTF("reg[%s] <= 0x%" PRIx32 "\n",
136 imx6_src_reg_name(SRC_SCR), s->regs[SRC_SCR]);
141 static void imx6_defer_clear_reset_bit(int cpuid,
143 unsigned long reset_shift)
145 struct SRCSCRResetInfo *ri;
146 CPUState *cpu = arm_get_cpu_by_id(cpuid);
152 ri = g_malloc(sizeof(struct SRCSCRResetInfo));
154 ri->reset_bit = reset_shift;
156 async_run_on_cpu(cpu, imx6_clear_reset_bit, RUN_ON_CPU_HOST_PTR(ri));
160 static void imx6_src_write(void *opaque, hwaddr offset, uint64_t value,
163 IMX6SRCState *s = (IMX6SRCState *)opaque;
164 uint32_t index = offset >> 2;
165 unsigned long change_mask;
166 unsigned long current_value = value;
168 if (index >= SRC_MAX) {
169 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
170 HWADDR_PRIx "\n", TYPE_IMX6_SRC, __func__, offset);
174 DPRINTF("reg[%s] <= 0x%" PRIx32 "\n", imx6_src_reg_name(index),
175 (uint32_t)current_value);
177 change_mask = s->regs[index] ^ (uint32_t)current_value;
182 * On real hardware when the system reset controller starts a
183 * secondary CPU it runs through some boot ROM code which reads
184 * the SRC_GPRX registers controlling the start address and branches
186 * Here we are taking a short cut and branching directly to the
187 * requested address (we don't want to run the boot ROM code inside
190 if (EXTRACT(change_mask, CORE3_ENABLE)) {
191 if (EXTRACT(current_value, CORE3_ENABLE)) {
192 /* CORE 3 is brought up */
193 arm_set_cpu_on(3, s->regs[SRC_GPR7], s->regs[SRC_GPR8],
196 /* CORE 3 is shut down */
199 /* We clear the reset bits as the processor changed state */
200 imx6_defer_clear_reset_bit(3, s, CORE3_RST_SHIFT);
201 clear_bit(CORE3_RST_SHIFT, &change_mask);
203 if (EXTRACT(change_mask, CORE2_ENABLE)) {
204 if (EXTRACT(current_value, CORE2_ENABLE)) {
205 /* CORE 2 is brought up */
206 arm_set_cpu_on(2, s->regs[SRC_GPR5], s->regs[SRC_GPR6],
209 /* CORE 2 is shut down */
212 /* We clear the reset bits as the processor changed state */
213 imx6_defer_clear_reset_bit(2, s, CORE2_RST_SHIFT);
214 clear_bit(CORE2_RST_SHIFT, &change_mask);
216 if (EXTRACT(change_mask, CORE1_ENABLE)) {
217 if (EXTRACT(current_value, CORE1_ENABLE)) {
218 /* CORE 1 is brought up */
219 arm_set_cpu_on(1, s->regs[SRC_GPR3], s->regs[SRC_GPR4],
222 /* CORE 1 is shut down */
225 /* We clear the reset bits as the processor changed state */
226 imx6_defer_clear_reset_bit(1, s, CORE1_RST_SHIFT);
227 clear_bit(CORE1_RST_SHIFT, &change_mask);
229 if (EXTRACT(change_mask, CORE0_RST)) {
231 imx6_defer_clear_reset_bit(0, s, CORE0_RST_SHIFT);
233 if (EXTRACT(change_mask, CORE1_RST)) {
235 imx6_defer_clear_reset_bit(1, s, CORE1_RST_SHIFT);
237 if (EXTRACT(change_mask, CORE2_RST)) {
239 imx6_defer_clear_reset_bit(2, s, CORE2_RST_SHIFT);
241 if (EXTRACT(change_mask, CORE3_RST)) {
243 imx6_defer_clear_reset_bit(3, s, CORE3_RST_SHIFT);
245 if (EXTRACT(change_mask, SW_IPU2_RST)) {
246 /* We pretend the IPU2 is reset */
247 clear_bit(SW_IPU2_RST_SHIFT, ¤t_value);
249 if (EXTRACT(change_mask, SW_IPU1_RST)) {
250 /* We pretend the IPU1 is reset */
251 clear_bit(SW_IPU1_RST_SHIFT, ¤t_value);
253 s->regs[index] = current_value;
256 s->regs[index] = current_value;
261 static const struct MemoryRegionOps imx6_src_ops = {
262 .read = imx6_src_read,
263 .write = imx6_src_write,
264 .endianness = DEVICE_NATIVE_ENDIAN,
267 * Our device would not work correctly if the guest was doing
268 * unaligned access. This might not be a limitation on the real
269 * device but in practice there is no reason for a guest to access
270 * this device unaligned.
272 .min_access_size = 4,
273 .max_access_size = 4,
278 static void imx6_src_realize(DeviceState *dev, Error **errp)
280 IMX6SRCState *s = IMX6_SRC(dev);
282 memory_region_init_io(&s->iomem, OBJECT(dev), &imx6_src_ops, s,
283 TYPE_IMX6_SRC, 0x1000);
284 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
287 static void imx6_src_class_init(ObjectClass *klass, void *data)
289 DeviceClass *dc = DEVICE_CLASS(klass);
291 dc->realize = imx6_src_realize;
292 dc->reset = imx6_src_reset;
293 dc->vmsd = &vmstate_imx6_src;
294 dc->desc = "i.MX6 System Reset Controller";
297 static const TypeInfo imx6_src_info = {
298 .name = TYPE_IMX6_SRC,
299 .parent = TYPE_SYS_BUS_DEVICE,
300 .instance_size = sizeof(IMX6SRCState),
301 .class_init = imx6_src_class_init,
304 static void imx6_src_register_types(void)
306 type_register_static(&imx6_src_info);
309 type_init(imx6_src_register_types)