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 "sysemu/sysemu.h"
14 #include "qemu/bitops.h"
16 #include "qemu/module.h"
17 #include "arm-powerctl.h"
20 #ifndef DEBUG_IMX6_SRC
21 #define DEBUG_IMX6_SRC 0
24 #define DPRINTF(fmt, args...) \
26 if (DEBUG_IMX6_SRC) { \
27 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX6_SRC, \
32 static const char *imx6_src_reg_name(uint32_t reg
)
34 static char unknown
[20];
70 sprintf(unknown
, "%d ?", reg
);
75 static const VMStateDescription vmstate_imx6_src
= {
76 .name
= TYPE_IMX6_SRC
,
78 .minimum_version_id
= 1,
79 .fields
= (VMStateField
[]) {
80 VMSTATE_UINT32_ARRAY(regs
, IMX6SRCState
, SRC_MAX
),
85 static void imx6_src_reset(DeviceState
*dev
)
87 IMX6SRCState
*s
= IMX6_SRC(dev
);
91 memset(s
->regs
, 0, sizeof(s
->regs
));
93 /* Set reset values */
94 s
->regs
[SRC_SCR
] = 0x521;
95 s
->regs
[SRC_SRSR
] = 0x1;
96 s
->regs
[SRC_SIMR
] = 0x1F;
99 static uint64_t imx6_src_read(void *opaque
, hwaddr offset
, unsigned size
)
102 IMX6SRCState
*s
= (IMX6SRCState
*)opaque
;
103 uint32_t index
= offset
>> 2;
105 if (index
< SRC_MAX
) {
106 value
= s
->regs
[index
];
108 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
109 HWADDR_PRIx
"\n", TYPE_IMX6_SRC
, __func__
, offset
);
113 DPRINTF("reg[%s] => 0x%" PRIx32
"\n", imx6_src_reg_name(index
), value
);
119 /* The reset is asynchronous so we need to defer clearing the reset
120 * bit until the work is completed.
123 struct SRCSCRResetInfo
{
128 static void imx6_clear_reset_bit(CPUState
*cpu
, run_on_cpu_data data
)
130 struct SRCSCRResetInfo
*ri
= data
.host_ptr
;
131 IMX6SRCState
*s
= ri
->s
;
133 assert(qemu_mutex_iothread_locked());
135 s
->regs
[SRC_SCR
] = deposit32(s
->regs
[SRC_SCR
], ri
->reset_bit
, 1, 0);
136 DPRINTF("reg[%s] <= 0x%" PRIx32
"\n",
137 imx6_src_reg_name(SRC_SCR
), s
->regs
[SRC_SCR
]);
142 static void imx6_defer_clear_reset_bit(int cpuid
,
144 unsigned long reset_shift
)
146 struct SRCSCRResetInfo
*ri
;
147 CPUState
*cpu
= arm_get_cpu_by_id(cpuid
);
153 ri
= g_malloc(sizeof(struct SRCSCRResetInfo
));
155 ri
->reset_bit
= reset_shift
;
157 async_run_on_cpu(cpu
, imx6_clear_reset_bit
, RUN_ON_CPU_HOST_PTR(ri
));
161 static void imx6_src_write(void *opaque
, hwaddr offset
, uint64_t value
,
164 IMX6SRCState
*s
= (IMX6SRCState
*)opaque
;
165 uint32_t index
= offset
>> 2;
166 unsigned long change_mask
;
167 unsigned long current_value
= value
;
169 if (index
>= SRC_MAX
) {
170 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
171 HWADDR_PRIx
"\n", TYPE_IMX6_SRC
, __func__
, offset
);
175 DPRINTF("reg[%s] <= 0x%" PRIx32
"\n", imx6_src_reg_name(index
),
176 (uint32_t)current_value
);
178 change_mask
= s
->regs
[index
] ^ (uint32_t)current_value
;
183 * On real hardware when the system reset controller starts a
184 * secondary CPU it runs through some boot ROM code which reads
185 * the SRC_GPRX registers controlling the start address and branches
187 * Here we are taking a short cut and branching directly to the
188 * requested address (we don't want to run the boot ROM code inside
191 if (EXTRACT(change_mask
, CORE3_ENABLE
)) {
192 if (EXTRACT(current_value
, CORE3_ENABLE
)) {
193 /* CORE 3 is brought up */
194 arm_set_cpu_on(3, s
->regs
[SRC_GPR7
], s
->regs
[SRC_GPR8
],
197 /* CORE 3 is shut down */
200 /* We clear the reset bits as the processor changed state */
201 imx6_defer_clear_reset_bit(3, s
, CORE3_RST_SHIFT
);
202 clear_bit(CORE3_RST_SHIFT
, &change_mask
);
204 if (EXTRACT(change_mask
, CORE2_ENABLE
)) {
205 if (EXTRACT(current_value
, CORE2_ENABLE
)) {
206 /* CORE 2 is brought up */
207 arm_set_cpu_on(2, s
->regs
[SRC_GPR5
], s
->regs
[SRC_GPR6
],
210 /* CORE 2 is shut down */
213 /* We clear the reset bits as the processor changed state */
214 imx6_defer_clear_reset_bit(2, s
, CORE2_RST_SHIFT
);
215 clear_bit(CORE2_RST_SHIFT
, &change_mask
);
217 if (EXTRACT(change_mask
, CORE1_ENABLE
)) {
218 if (EXTRACT(current_value
, CORE1_ENABLE
)) {
219 /* CORE 1 is brought up */
220 arm_set_cpu_on(1, s
->regs
[SRC_GPR3
], s
->regs
[SRC_GPR4
],
223 /* CORE 1 is shut down */
226 /* We clear the reset bits as the processor changed state */
227 imx6_defer_clear_reset_bit(1, s
, CORE1_RST_SHIFT
);
228 clear_bit(CORE1_RST_SHIFT
, &change_mask
);
230 if (EXTRACT(change_mask
, CORE0_RST
)) {
232 imx6_defer_clear_reset_bit(0, s
, CORE0_RST_SHIFT
);
234 if (EXTRACT(change_mask
, CORE1_RST
)) {
236 imx6_defer_clear_reset_bit(1, s
, CORE1_RST_SHIFT
);
238 if (EXTRACT(change_mask
, CORE2_RST
)) {
240 imx6_defer_clear_reset_bit(2, s
, CORE2_RST_SHIFT
);
242 if (EXTRACT(change_mask
, CORE3_RST
)) {
244 imx6_defer_clear_reset_bit(3, s
, CORE3_RST_SHIFT
);
246 if (EXTRACT(change_mask
, SW_IPU2_RST
)) {
247 /* We pretend the IPU2 is reset */
248 clear_bit(SW_IPU2_RST_SHIFT
, ¤t_value
);
250 if (EXTRACT(change_mask
, SW_IPU1_RST
)) {
251 /* We pretend the IPU1 is reset */
252 clear_bit(SW_IPU1_RST_SHIFT
, ¤t_value
);
254 s
->regs
[index
] = current_value
;
257 s
->regs
[index
] = current_value
;
262 static const struct MemoryRegionOps imx6_src_ops
= {
263 .read
= imx6_src_read
,
264 .write
= imx6_src_write
,
265 .endianness
= DEVICE_NATIVE_ENDIAN
,
268 * Our device would not work correctly if the guest was doing
269 * unaligned access. This might not be a limitation on the real
270 * device but in practice there is no reason for a guest to access
271 * this device unaligned.
273 .min_access_size
= 4,
274 .max_access_size
= 4,
279 static void imx6_src_realize(DeviceState
*dev
, Error
**errp
)
281 IMX6SRCState
*s
= IMX6_SRC(dev
);
283 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &imx6_src_ops
, s
,
284 TYPE_IMX6_SRC
, 0x1000);
285 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &s
->iomem
);
288 static void imx6_src_class_init(ObjectClass
*klass
, void *data
)
290 DeviceClass
*dc
= DEVICE_CLASS(klass
);
292 dc
->realize
= imx6_src_realize
;
293 dc
->reset
= imx6_src_reset
;
294 dc
->vmsd
= &vmstate_imx6_src
;
295 dc
->desc
= "i.MX6 System Reset Controller";
298 static const TypeInfo imx6_src_info
= {
299 .name
= TYPE_IMX6_SRC
,
300 .parent
= TYPE_SYS_BUS_DEVICE
,
301 .instance_size
= sizeof(IMX6SRCState
),
302 .class_init
= imx6_src_class_init
,
305 static void imx6_src_register_types(void)
307 type_register_static(&imx6_src_info
);
310 type_init(imx6_src_register_types
)