Merge tag 'nvme-next-pull-request' of https://gitlab.com/birkelund/qemu into staging
[qemu/ar7.git] / hw / misc / imx7_src.c
blobd19f0450d4aa2c172a2adc8ba1496c090e196fa9
1 /*
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.
9 */
11 #include "qemu/osdep.h"
12 #include "hw/misc/imx7_src.h"
13 #include "migration/vmstate.h"
14 #include "qemu/bitops.h"
15 #include "qemu/log.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"
22 #include "trace.h"
24 static const char *imx7_src_reg_name(uint32_t reg)
26 static char unknown[20];
28 switch (reg) {
29 case SRC_SCR:
30 return "SRC_SCR";
31 case SRC_A7RCR0:
32 return "SRC_A7RCR0";
33 case SRC_A7RCR1:
34 return "SRC_A7RCR1";
35 case SRC_M4RCR:
36 return "SRC_M4RCR";
37 case SRC_ERCR:
38 return "SRC_ERCR";
39 case SRC_HSICPHY_RCR:
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";
45 case SRC_PCIEPHY_RCR:
46 return "SRC_PCIEPHY_RCR";
47 case SRC_SBMR1:
48 return "SRC_SBMR1";
49 case SRC_SRSR:
50 return "SRC_SRSR";
51 case SRC_SISR:
52 return "SRC_SISR";
53 case SRC_SIMR:
54 return "SRC_SIMR";
55 case SRC_SBMR2:
56 return "SRC_SBMR2";
57 case SRC_GPR1:
58 return "SRC_GPR1";
59 case SRC_GPR2:
60 return "SRC_GPR2";
61 case SRC_GPR3:
62 return "SRC_GPR3";
63 case SRC_GPR4:
64 return "SRC_GPR4";
65 case SRC_GPR5:
66 return "SRC_GPR5";
67 case SRC_GPR6:
68 return "SRC_GPR6";
69 case SRC_GPR7:
70 return "SRC_GPR7";
71 case SRC_GPR8:
72 return "SRC_GPR8";
73 case SRC_GPR9:
74 return "SRC_GPR9";
75 case SRC_GPR10:
76 return "SRC_GPR10";
77 default:
78 snprintf(unknown, sizeof(unknown), "%u ?", reg);
79 return unknown;
83 static const VMStateDescription vmstate_imx7_src = {
84 .name = TYPE_IMX7_SRC,
85 .version_id = 1,
86 .minimum_version_id = 1,
87 .fields = (const VMStateField[]) {
88 VMSTATE_UINT32_ARRAY(regs, IMX7SRCState, SRC_MAX),
89 VMSTATE_END_OF_LIST()
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)
107 uint32_t value = 0;
108 IMX7SRCState *s = (IMX7SRCState *)opaque;
109 uint32_t index = offset >> 2;
111 if (index < SRC_MAX) {
112 value = s->regs[index];
113 } else {
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);
120 return value;
125 * The reset is asynchronous so we need to defer clearing the reset
126 * bit until the work is completed.
129 struct SRCSCRResetInfo {
130 IMX7SRCState *s;
131 uint32_t reset_bit;
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]);
145 g_free(ri);
148 static void imx7_defer_clear_reset_bit(uint32_t cpuid,
149 IMX7SRCState *s,
150 uint32_t reset_shift)
152 struct SRCSCRResetInfo *ri;
153 CPUState *cpu = arm_get_cpu_by_id(cpuid);
155 if (!cpu) {
156 return;
159 ri = g_new(struct SRCSCRResetInfo, 1);
160 ri->s = s;
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,
168 unsigned size)
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);
178 return;
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;
185 switch (index) {
186 case SRC_A7RCR0:
187 if (FIELD_EX32(change_mask, CORE0, RST)) {
188 arm_reset_cpu(0);
189 imx7_defer_clear_reset_bit(0, s, R_CORE0_RST_SHIFT);
191 if (FIELD_EX32(change_mask, CORE1, RST)) {
192 arm_reset_cpu(1);
193 imx7_defer_clear_reset_bit(1, s, R_CORE1_RST_SHIFT);
195 s->regs[index] = current_value;
196 break;
197 case SRC_A7RCR1:
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
202 * to it.
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
205 * QEMU)
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],
211 3, false);
212 } else {
213 /* CORE 1 is shut down */
214 arm_set_cpu_off(1);
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;
221 break;
222 default:
223 s->regs[index] = current_value;
224 break;
228 static const struct MemoryRegionOps imx7_src_ops = {
229 .read = imx7_src_read,
230 .write = imx7_src_write,
231 .endianness = DEVICE_NATIVE_ENDIAN,
232 .valid = {
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,
241 .unaligned = false,
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 dc->reset = 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)