2 * IMX6 System Reset Controller
4 * Copyright (c) 2015 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/imx6_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 "arm-powerctl.h"
19 #include "hw/core/cpu.h"
21 #ifndef DEBUG_IMX6_SRC
22 #define DEBUG_IMX6_SRC 0
25 #define DPRINTF(fmt, args...) \
27 if (DEBUG_IMX6_SRC) { \
28 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX6_SRC, \
33 static const char *imx6_src_reg_name(uint32_t reg
)
35 static char unknown
[20];
71 sprintf(unknown
, "%d ?", reg
);
76 static const VMStateDescription vmstate_imx6_src
= {
77 .name
= TYPE_IMX6_SRC
,
79 .minimum_version_id
= 1,
80 .fields
= (VMStateField
[]) {
81 VMSTATE_UINT32_ARRAY(regs
, IMX6SRCState
, SRC_MAX
),
86 static void imx6_src_reset(DeviceState
*dev
)
88 IMX6SRCState
*s
= IMX6_SRC(dev
);
92 memset(s
->regs
, 0, sizeof(s
->regs
));
94 /* Set reset values */
95 s
->regs
[SRC_SCR
] = 0x521;
96 s
->regs
[SRC_SRSR
] = 0x1;
97 s
->regs
[SRC_SIMR
] = 0x1F;
100 static uint64_t imx6_src_read(void *opaque
, hwaddr offset
, unsigned size
)
103 IMX6SRCState
*s
= (IMX6SRCState
*)opaque
;
104 uint32_t index
= offset
>> 2;
106 if (index
< SRC_MAX
) {
107 value
= s
->regs
[index
];
109 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
110 HWADDR_PRIx
"\n", TYPE_IMX6_SRC
, __func__
, offset
);
114 DPRINTF("reg[%s] => 0x%" PRIx32
"\n", imx6_src_reg_name(index
), value
);
120 /* The reset is asynchronous so we need to defer clearing the reset
121 * bit until the work is completed.
124 struct SRCSCRResetInfo
{
129 static void imx6_clear_reset_bit(CPUState
*cpu
, run_on_cpu_data data
)
131 struct SRCSCRResetInfo
*ri
= data
.host_ptr
;
132 IMX6SRCState
*s
= ri
->s
;
134 assert(qemu_mutex_iothread_locked());
136 s
->regs
[SRC_SCR
] = deposit32(s
->regs
[SRC_SCR
], ri
->reset_bit
, 1, 0);
137 DPRINTF("reg[%s] <= 0x%" PRIx32
"\n",
138 imx6_src_reg_name(SRC_SCR
), s
->regs
[SRC_SCR
]);
143 static void imx6_defer_clear_reset_bit(int cpuid
,
145 unsigned long reset_shift
)
147 struct SRCSCRResetInfo
*ri
;
148 CPUState
*cpu
= arm_get_cpu_by_id(cpuid
);
154 ri
= g_malloc(sizeof(struct SRCSCRResetInfo
));
156 ri
->reset_bit
= reset_shift
;
158 async_run_on_cpu(cpu
, imx6_clear_reset_bit
, RUN_ON_CPU_HOST_PTR(ri
));
162 static void imx6_src_write(void *opaque
, hwaddr offset
, uint64_t value
,
165 IMX6SRCState
*s
= (IMX6SRCState
*)opaque
;
166 uint32_t index
= offset
>> 2;
167 unsigned long change_mask
;
168 unsigned long current_value
= value
;
170 if (index
>= SRC_MAX
) {
171 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
172 HWADDR_PRIx
"\n", TYPE_IMX6_SRC
, __func__
, offset
);
176 DPRINTF("reg[%s] <= 0x%" PRIx32
"\n", imx6_src_reg_name(index
),
177 (uint32_t)current_value
);
179 change_mask
= s
->regs
[index
] ^ (uint32_t)current_value
;
184 * On real hardware when the system reset controller starts a
185 * secondary CPU it runs through some boot ROM code which reads
186 * the SRC_GPRX registers controlling the start address and branches
188 * Here we are taking a short cut and branching directly to the
189 * requested address (we don't want to run the boot ROM code inside
192 if (EXTRACT(change_mask
, CORE3_ENABLE
)) {
193 if (EXTRACT(current_value
, CORE3_ENABLE
)) {
194 /* CORE 3 is brought up */
195 arm_set_cpu_on(3, s
->regs
[SRC_GPR7
], s
->regs
[SRC_GPR8
],
198 /* CORE 3 is shut down */
201 /* We clear the reset bits as the processor changed state */
202 imx6_defer_clear_reset_bit(3, s
, CORE3_RST_SHIFT
);
203 clear_bit(CORE3_RST_SHIFT
, &change_mask
);
205 if (EXTRACT(change_mask
, CORE2_ENABLE
)) {
206 if (EXTRACT(current_value
, CORE2_ENABLE
)) {
207 /* CORE 2 is brought up */
208 arm_set_cpu_on(2, s
->regs
[SRC_GPR5
], s
->regs
[SRC_GPR6
],
211 /* CORE 2 is shut down */
214 /* We clear the reset bits as the processor changed state */
215 imx6_defer_clear_reset_bit(2, s
, CORE2_RST_SHIFT
);
216 clear_bit(CORE2_RST_SHIFT
, &change_mask
);
218 if (EXTRACT(change_mask
, CORE1_ENABLE
)) {
219 if (EXTRACT(current_value
, CORE1_ENABLE
)) {
220 /* CORE 1 is brought up */
221 arm_set_cpu_on(1, s
->regs
[SRC_GPR3
], s
->regs
[SRC_GPR4
],
224 /* CORE 1 is shut down */
227 /* We clear the reset bits as the processor changed state */
228 imx6_defer_clear_reset_bit(1, s
, CORE1_RST_SHIFT
);
229 clear_bit(CORE1_RST_SHIFT
, &change_mask
);
231 if (EXTRACT(change_mask
, CORE0_RST
)) {
233 imx6_defer_clear_reset_bit(0, s
, CORE0_RST_SHIFT
);
235 if (EXTRACT(change_mask
, CORE1_RST
)) {
237 imx6_defer_clear_reset_bit(1, s
, CORE1_RST_SHIFT
);
239 if (EXTRACT(change_mask
, CORE2_RST
)) {
241 imx6_defer_clear_reset_bit(2, s
, CORE2_RST_SHIFT
);
243 if (EXTRACT(change_mask
, CORE3_RST
)) {
245 imx6_defer_clear_reset_bit(3, s
, CORE3_RST_SHIFT
);
247 if (EXTRACT(change_mask
, SW_IPU2_RST
)) {
248 /* We pretend the IPU2 is reset */
249 clear_bit(SW_IPU2_RST_SHIFT
, ¤t_value
);
251 if (EXTRACT(change_mask
, SW_IPU1_RST
)) {
252 /* We pretend the IPU1 is reset */
253 clear_bit(SW_IPU1_RST_SHIFT
, ¤t_value
);
255 s
->regs
[index
] = current_value
;
258 s
->regs
[index
] = current_value
;
263 static const struct MemoryRegionOps imx6_src_ops
= {
264 .read
= imx6_src_read
,
265 .write
= imx6_src_write
,
266 .endianness
= DEVICE_NATIVE_ENDIAN
,
269 * Our device would not work correctly if the guest was doing
270 * unaligned access. This might not be a limitation on the real
271 * device but in practice there is no reason for a guest to access
272 * this device unaligned.
274 .min_access_size
= 4,
275 .max_access_size
= 4,
280 static void imx6_src_realize(DeviceState
*dev
, Error
**errp
)
282 IMX6SRCState
*s
= IMX6_SRC(dev
);
284 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &imx6_src_ops
, s
,
285 TYPE_IMX6_SRC
, 0x1000);
286 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &s
->iomem
);
289 static void imx6_src_class_init(ObjectClass
*klass
, void *data
)
291 DeviceClass
*dc
= DEVICE_CLASS(klass
);
293 dc
->realize
= imx6_src_realize
;
294 dc
->reset
= imx6_src_reset
;
295 dc
->vmsd
= &vmstate_imx6_src
;
296 dc
->desc
= "i.MX6 System Reset Controller";
299 static const TypeInfo imx6_src_info
= {
300 .name
= TYPE_IMX6_SRC
,
301 .parent
= TYPE_SYS_BUS_DEVICE
,
302 .instance_size
= sizeof(IMX6SRCState
),
303 .class_init
= imx6_src_class_init
,
306 static void imx6_src_register_types(void)
308 type_register_static(&imx6_src_info
);
311 type_init(imx6_src_register_types
)