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"
15 #include "arm-powerctl.h"
17 #ifndef DEBUG_IMX6_SRC
18 #define DEBUG_IMX6_SRC 0
21 #define DPRINTF(fmt, args...) \
23 if (DEBUG_IMX6_SRC) { \
24 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX6_SRC, \
29 static char const *imx6_src_reg_name(uint32_t reg
)
31 static char unknown
[20];
67 sprintf(unknown
, "%d ?", reg
);
72 static const VMStateDescription vmstate_imx6_src
= {
73 .name
= TYPE_IMX6_SRC
,
75 .minimum_version_id
= 1,
76 .fields
= (VMStateField
[]) {
77 VMSTATE_UINT32_ARRAY(regs
, IMX6SRCState
, SRC_MAX
),
82 static void imx6_src_reset(DeviceState
*dev
)
84 IMX6SRCState
*s
= IMX6_SRC(dev
);
88 memset(s
->regs
, 0, sizeof(s
->regs
));
90 /* Set reset values */
91 s
->regs
[SRC_SCR
] = 0x521;
92 s
->regs
[SRC_SRSR
] = 0x1;
93 s
->regs
[SRC_SIMR
] = 0x1F;
96 static uint64_t imx6_src_read(void *opaque
, hwaddr offset
, unsigned size
)
99 IMX6SRCState
*s
= (IMX6SRCState
*)opaque
;
100 uint32_t index
= offset
>> 2;
102 if (index
< SRC_MAX
) {
103 value
= s
->regs
[index
];
105 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
106 HWADDR_PRIx
"\n", TYPE_IMX6_SRC
, __func__
, offset
);
110 DPRINTF("reg[%s] => 0x%" PRIx32
"\n", imx6_src_reg_name(index
), value
);
115 static void imx6_src_write(void *opaque
, hwaddr offset
, uint64_t value
,
118 IMX6SRCState
*s
= (IMX6SRCState
*)opaque
;
119 uint32_t index
= offset
>> 2;
120 unsigned long change_mask
;
121 unsigned long current_value
= value
;
123 if (index
>= SRC_MAX
) {
124 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
125 HWADDR_PRIx
"\n", TYPE_IMX6_SRC
, __func__
, offset
);
129 DPRINTF("reg[%s] <= 0x%" PRIx32
"\n", imx6_src_reg_name(index
),
130 (uint32_t)current_value
);
132 change_mask
= s
->regs
[index
] ^ (uint32_t)current_value
;
137 * On real hardware when the system reset controller starts a
138 * secondary CPU it runs through some boot ROM code which reads
139 * the SRC_GPRX registers controlling the start address and branches
141 * Here we are taking a short cut and branching directly to the
142 * requested address (we don't want to run the boot ROM code inside
145 if (EXTRACT(change_mask
, CORE3_ENABLE
)) {
146 if (EXTRACT(current_value
, CORE3_ENABLE
)) {
147 /* CORE 3 is brought up */
148 arm_set_cpu_on(3, s
->regs
[SRC_GPR7
], s
->regs
[SRC_GPR8
],
151 /* CORE 3 is shut down */
154 /* We clear the reset bits as the processor changed state */
155 clear_bit(CORE3_RST_SHIFT
, ¤t_value
);
156 clear_bit(CORE3_RST_SHIFT
, &change_mask
);
158 if (EXTRACT(change_mask
, CORE2_ENABLE
)) {
159 if (EXTRACT(current_value
, CORE2_ENABLE
)) {
160 /* CORE 2 is brought up */
161 arm_set_cpu_on(2, s
->regs
[SRC_GPR5
], s
->regs
[SRC_GPR6
],
164 /* CORE 3 is shut down */
167 /* We clear the reset bits as the processor changed state */
168 clear_bit(CORE2_RST_SHIFT
, ¤t_value
);
169 clear_bit(CORE2_RST_SHIFT
, &change_mask
);
171 if (EXTRACT(change_mask
, CORE1_ENABLE
)) {
172 if (EXTRACT(current_value
, CORE1_ENABLE
)) {
173 /* CORE 1 is brought up */
174 arm_set_cpu_on(1, s
->regs
[SRC_GPR3
], s
->regs
[SRC_GPR4
],
177 /* CORE 3 is shut down */
180 /* We clear the reset bits as the processor changed state */
181 clear_bit(CORE1_RST_SHIFT
, ¤t_value
);
182 clear_bit(CORE1_RST_SHIFT
, &change_mask
);
184 if (EXTRACT(change_mask
, CORE0_RST
)) {
186 clear_bit(CORE0_RST_SHIFT
, ¤t_value
);
188 if (EXTRACT(change_mask
, CORE1_RST
)) {
190 clear_bit(CORE1_RST_SHIFT
, ¤t_value
);
192 if (EXTRACT(change_mask
, CORE2_RST
)) {
194 clear_bit(CORE2_RST_SHIFT
, ¤t_value
);
196 if (EXTRACT(change_mask
, CORE3_RST
)) {
198 clear_bit(CORE3_RST_SHIFT
, ¤t_value
);
200 if (EXTRACT(change_mask
, SW_IPU2_RST
)) {
201 /* We pretend the IPU2 is reset */
202 clear_bit(SW_IPU2_RST_SHIFT
, ¤t_value
);
204 if (EXTRACT(change_mask
, SW_IPU1_RST
)) {
205 /* We pretend the IPU1 is reset */
206 clear_bit(SW_IPU1_RST_SHIFT
, ¤t_value
);
208 s
->regs
[index
] = current_value
;
211 s
->regs
[index
] = current_value
;
216 static const struct MemoryRegionOps imx6_src_ops
= {
217 .read
= imx6_src_read
,
218 .write
= imx6_src_write
,
219 .endianness
= DEVICE_NATIVE_ENDIAN
,
222 * Our device would not work correctly if the guest was doing
223 * unaligned access. This might not be a limitation on the real
224 * device but in practice there is no reason for a guest to access
225 * this device unaligned.
227 .min_access_size
= 4,
228 .max_access_size
= 4,
233 static void imx6_src_realize(DeviceState
*dev
, Error
**errp
)
235 IMX6SRCState
*s
= IMX6_SRC(dev
);
237 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &imx6_src_ops
, s
,
238 TYPE_IMX6_SRC
, 0x1000);
239 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &s
->iomem
);
242 static void imx6_src_class_init(ObjectClass
*klass
, void *data
)
244 DeviceClass
*dc
= DEVICE_CLASS(klass
);
246 dc
->realize
= imx6_src_realize
;
247 dc
->reset
= imx6_src_reset
;
248 dc
->vmsd
= &vmstate_imx6_src
;
249 dc
->desc
= "i.MX6 System Reset Controller";
252 static const TypeInfo imx6_src_info
= {
253 .name
= TYPE_IMX6_SRC
,
254 .parent
= TYPE_SYS_BUS_DEVICE
,
255 .instance_size
= sizeof(IMX6SRCState
),
256 .class_init
= imx6_src_class_init
,
259 static void imx6_src_register_types(void)
261 type_register_static(&imx6_src_info
);
264 type_init(imx6_src_register_types
)