119830574SJean-Christophe DUBOIS /*
219830574SJean-Christophe DUBOIS * IMX6 System Reset Controller
319830574SJean-Christophe DUBOIS *
419830574SJean-Christophe DUBOIS * Copyright (c) 2015 Jean-Christophe Dubois <jcd@tribudubois.net>
519830574SJean-Christophe DUBOIS *
619830574SJean-Christophe DUBOIS * This work is licensed under the terms of the GNU GPL, version 2 or later.
719830574SJean-Christophe DUBOIS * See the COPYING file in the top-level directory.
819830574SJean-Christophe DUBOIS *
919830574SJean-Christophe DUBOIS */
1019830574SJean-Christophe DUBOIS
1119830574SJean-Christophe DUBOIS #include "qemu/osdep.h"
1219830574SJean-Christophe DUBOIS #include "hw/misc/imx6_src.h"
13d6454270SMarkus Armbruster #include "migration/vmstate.h"
1419830574SJean-Christophe DUBOIS #include "qemu/bitops.h"
1503dd024fSPaolo Bonzini #include "qemu/log.h"
16db725815SMarkus Armbruster #include "qemu/main-loop.h"
170b8fa32fSMarkus Armbruster #include "qemu/module.h"
183d81e8cfSThomas Huth #include "target/arm/arm-powerctl.h"
192e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
2019830574SJean-Christophe DUBOIS
2119830574SJean-Christophe DUBOIS #ifndef DEBUG_IMX6_SRC
2219830574SJean-Christophe DUBOIS #define DEBUG_IMX6_SRC 0
2319830574SJean-Christophe DUBOIS #endif
2419830574SJean-Christophe DUBOIS
2519830574SJean-Christophe DUBOIS #define DPRINTF(fmt, args...) \
2619830574SJean-Christophe DUBOIS do { \
2719830574SJean-Christophe DUBOIS if (DEBUG_IMX6_SRC) { \
2819830574SJean-Christophe DUBOIS fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX6_SRC, \
2919830574SJean-Christophe DUBOIS __func__, ##args); \
3019830574SJean-Christophe DUBOIS } \
3119830574SJean-Christophe DUBOIS } while (0)
3219830574SJean-Christophe DUBOIS
imx6_src_reg_name(uint32_t reg)33d675765aSPeter Maydell static const char *imx6_src_reg_name(uint32_t reg)
3419830574SJean-Christophe DUBOIS {
3519830574SJean-Christophe DUBOIS static char unknown[20];
3619830574SJean-Christophe DUBOIS
3719830574SJean-Christophe DUBOIS switch (reg) {
3819830574SJean-Christophe DUBOIS case SRC_SCR:
3919830574SJean-Christophe DUBOIS return "SRC_SCR";
4019830574SJean-Christophe DUBOIS case SRC_SBMR1:
4119830574SJean-Christophe DUBOIS return "SRC_SBMR1";
4219830574SJean-Christophe DUBOIS case SRC_SRSR:
4319830574SJean-Christophe DUBOIS return "SRC_SRSR";
4419830574SJean-Christophe DUBOIS case SRC_SISR:
4519830574SJean-Christophe DUBOIS return "SRC_SISR";
4619830574SJean-Christophe DUBOIS case SRC_SIMR:
4719830574SJean-Christophe DUBOIS return "SRC_SIMR";
4819830574SJean-Christophe DUBOIS case SRC_SBMR2:
4919830574SJean-Christophe DUBOIS return "SRC_SBMR2";
5019830574SJean-Christophe DUBOIS case SRC_GPR1:
5119830574SJean-Christophe DUBOIS return "SRC_GPR1";
5219830574SJean-Christophe DUBOIS case SRC_GPR2:
5319830574SJean-Christophe DUBOIS return "SRC_GPR2";
5419830574SJean-Christophe DUBOIS case SRC_GPR3:
5519830574SJean-Christophe DUBOIS return "SRC_GPR3";
5619830574SJean-Christophe DUBOIS case SRC_GPR4:
5719830574SJean-Christophe DUBOIS return "SRC_GPR4";
5819830574SJean-Christophe DUBOIS case SRC_GPR5:
5919830574SJean-Christophe DUBOIS return "SRC_GPR5";
6019830574SJean-Christophe DUBOIS case SRC_GPR6:
6119830574SJean-Christophe DUBOIS return "SRC_GPR6";
6219830574SJean-Christophe DUBOIS case SRC_GPR7:
6319830574SJean-Christophe DUBOIS return "SRC_GPR7";
6419830574SJean-Christophe DUBOIS case SRC_GPR8:
6519830574SJean-Christophe DUBOIS return "SRC_GPR8";
6619830574SJean-Christophe DUBOIS case SRC_GPR9:
6719830574SJean-Christophe DUBOIS return "SRC_GPR9";
6819830574SJean-Christophe DUBOIS case SRC_GPR10:
6919830574SJean-Christophe DUBOIS return "SRC_GPR10";
7019830574SJean-Christophe DUBOIS default:
71ca4af17cSPhilippe Mathieu-Daudé snprintf(unknown, sizeof(unknown), "%u ?", reg);
7219830574SJean-Christophe DUBOIS return unknown;
7319830574SJean-Christophe DUBOIS }
7419830574SJean-Christophe DUBOIS }
7519830574SJean-Christophe DUBOIS
7619830574SJean-Christophe DUBOIS static const VMStateDescription vmstate_imx6_src = {
7719830574SJean-Christophe DUBOIS .name = TYPE_IMX6_SRC,
7819830574SJean-Christophe DUBOIS .version_id = 1,
7919830574SJean-Christophe DUBOIS .minimum_version_id = 1,
80e4ea952fSRichard Henderson .fields = (const VMStateField[]) {
8119830574SJean-Christophe DUBOIS VMSTATE_UINT32_ARRAY(regs, IMX6SRCState, SRC_MAX),
8219830574SJean-Christophe DUBOIS VMSTATE_END_OF_LIST()
8319830574SJean-Christophe DUBOIS },
8419830574SJean-Christophe DUBOIS };
8519830574SJean-Christophe DUBOIS
imx6_src_reset(DeviceState * dev)8619830574SJean-Christophe DUBOIS static void imx6_src_reset(DeviceState *dev)
8719830574SJean-Christophe DUBOIS {
8819830574SJean-Christophe DUBOIS IMX6SRCState *s = IMX6_SRC(dev);
8919830574SJean-Christophe DUBOIS
9019830574SJean-Christophe DUBOIS DPRINTF("\n");
9119830574SJean-Christophe DUBOIS
9219830574SJean-Christophe DUBOIS memset(s->regs, 0, sizeof(s->regs));
9319830574SJean-Christophe DUBOIS
9419830574SJean-Christophe DUBOIS /* Set reset values */
9519830574SJean-Christophe DUBOIS s->regs[SRC_SCR] = 0x521;
9619830574SJean-Christophe DUBOIS s->regs[SRC_SRSR] = 0x1;
9719830574SJean-Christophe DUBOIS s->regs[SRC_SIMR] = 0x1F;
9819830574SJean-Christophe DUBOIS }
9919830574SJean-Christophe DUBOIS
imx6_src_read(void * opaque,hwaddr offset,unsigned size)10019830574SJean-Christophe DUBOIS static uint64_t imx6_src_read(void *opaque, hwaddr offset, unsigned size)
10119830574SJean-Christophe DUBOIS {
10219830574SJean-Christophe DUBOIS uint32_t value = 0;
10319830574SJean-Christophe DUBOIS IMX6SRCState *s = (IMX6SRCState *)opaque;
10419830574SJean-Christophe DUBOIS uint32_t index = offset >> 2;
10519830574SJean-Christophe DUBOIS
10619830574SJean-Christophe DUBOIS if (index < SRC_MAX) {
10719830574SJean-Christophe DUBOIS value = s->regs[index];
10819830574SJean-Christophe DUBOIS } else {
10919830574SJean-Christophe DUBOIS qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
11019830574SJean-Christophe DUBOIS HWADDR_PRIx "\n", TYPE_IMX6_SRC, __func__, offset);
11119830574SJean-Christophe DUBOIS
11219830574SJean-Christophe DUBOIS }
11319830574SJean-Christophe DUBOIS
11419830574SJean-Christophe DUBOIS DPRINTF("reg[%s] => 0x%" PRIx32 "\n", imx6_src_reg_name(index), value);
11519830574SJean-Christophe DUBOIS
11619830574SJean-Christophe DUBOIS return value;
11719830574SJean-Christophe DUBOIS }
11819830574SJean-Christophe DUBOIS
1194881658aSAlex Bennée
1204881658aSAlex Bennée /* The reset is asynchronous so we need to defer clearing the reset
1214881658aSAlex Bennée * bit until the work is completed.
1224881658aSAlex Bennée */
1234881658aSAlex Bennée
1244881658aSAlex Bennée struct SRCSCRResetInfo {
1254881658aSAlex Bennée IMX6SRCState *s;
1264881658aSAlex Bennée int reset_bit;
1274881658aSAlex Bennée };
1284881658aSAlex Bennée
imx6_clear_reset_bit(CPUState * cpu,run_on_cpu_data data)1294881658aSAlex Bennée static void imx6_clear_reset_bit(CPUState *cpu, run_on_cpu_data data)
1304881658aSAlex Bennée {
1314881658aSAlex Bennée struct SRCSCRResetInfo *ri = data.host_ptr;
1324881658aSAlex Bennée IMX6SRCState *s = ri->s;
1334881658aSAlex Bennée
134195801d7SStefan Hajnoczi assert(bql_locked());
1354881658aSAlex Bennée
1364881658aSAlex Bennée s->regs[SRC_SCR] = deposit32(s->regs[SRC_SCR], ri->reset_bit, 1, 0);
1374881658aSAlex Bennée DPRINTF("reg[%s] <= 0x%" PRIx32 "\n",
1384881658aSAlex Bennée imx6_src_reg_name(SRC_SCR), s->regs[SRC_SCR]);
1394881658aSAlex Bennée
1404881658aSAlex Bennée g_free(ri);
1414881658aSAlex Bennée }
1424881658aSAlex Bennée
imx6_defer_clear_reset_bit(int cpuid,IMX6SRCState * s,unsigned long reset_shift)1434881658aSAlex Bennée static void imx6_defer_clear_reset_bit(int cpuid,
1444881658aSAlex Bennée IMX6SRCState *s,
1454881658aSAlex Bennée unsigned long reset_shift)
1464881658aSAlex Bennée {
1474881658aSAlex Bennée struct SRCSCRResetInfo *ri;
1485e2fb7c5SPeter Maydell CPUState *cpu = arm_get_cpu_by_id(cpuid);
1495e2fb7c5SPeter Maydell
1505e2fb7c5SPeter Maydell if (!cpu) {
1515e2fb7c5SPeter Maydell return;
1525e2fb7c5SPeter Maydell }
1534881658aSAlex Bennée
154b21e2380SMarkus Armbruster ri = g_new(struct SRCSCRResetInfo, 1);
1554881658aSAlex Bennée ri->s = s;
1564881658aSAlex Bennée ri->reset_bit = reset_shift;
1574881658aSAlex Bennée
1585e2fb7c5SPeter Maydell async_run_on_cpu(cpu, imx6_clear_reset_bit, RUN_ON_CPU_HOST_PTR(ri));
1594881658aSAlex Bennée }
1604881658aSAlex Bennée
1614881658aSAlex Bennée
imx6_src_write(void * opaque,hwaddr offset,uint64_t value,unsigned size)16219830574SJean-Christophe DUBOIS static void imx6_src_write(void *opaque, hwaddr offset, uint64_t value,
16319830574SJean-Christophe DUBOIS unsigned size)
16419830574SJean-Christophe DUBOIS {
16519830574SJean-Christophe DUBOIS IMX6SRCState *s = (IMX6SRCState *)opaque;
16619830574SJean-Christophe DUBOIS uint32_t index = offset >> 2;
16719830574SJean-Christophe DUBOIS unsigned long change_mask;
16819830574SJean-Christophe DUBOIS unsigned long current_value = value;
16919830574SJean-Christophe DUBOIS
17019830574SJean-Christophe DUBOIS if (index >= SRC_MAX) {
17119830574SJean-Christophe DUBOIS qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
17219830574SJean-Christophe DUBOIS HWADDR_PRIx "\n", TYPE_IMX6_SRC, __func__, offset);
17319830574SJean-Christophe DUBOIS return;
17419830574SJean-Christophe DUBOIS }
17519830574SJean-Christophe DUBOIS
17619830574SJean-Christophe DUBOIS DPRINTF("reg[%s] <= 0x%" PRIx32 "\n", imx6_src_reg_name(index),
17719830574SJean-Christophe DUBOIS (uint32_t)current_value);
17819830574SJean-Christophe DUBOIS
17919830574SJean-Christophe DUBOIS change_mask = s->regs[index] ^ (uint32_t)current_value;
18019830574SJean-Christophe DUBOIS
18119830574SJean-Christophe DUBOIS switch (index) {
18219830574SJean-Christophe DUBOIS case SRC_SCR:
18319830574SJean-Christophe DUBOIS /*
18419830574SJean-Christophe DUBOIS * On real hardware when the system reset controller starts a
18519830574SJean-Christophe DUBOIS * secondary CPU it runs through some boot ROM code which reads
18619830574SJean-Christophe DUBOIS * the SRC_GPRX registers controlling the start address and branches
18719830574SJean-Christophe DUBOIS * to it.
18819830574SJean-Christophe DUBOIS * Here we are taking a short cut and branching directly to the
18919830574SJean-Christophe DUBOIS * requested address (we don't want to run the boot ROM code inside
19019830574SJean-Christophe DUBOIS * QEMU)
19119830574SJean-Christophe DUBOIS */
19219830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE3_ENABLE)) {
19319830574SJean-Christophe DUBOIS if (EXTRACT(current_value, CORE3_ENABLE)) {
19419830574SJean-Christophe DUBOIS /* CORE 3 is brought up */
19519830574SJean-Christophe DUBOIS arm_set_cpu_on(3, s->regs[SRC_GPR7], s->regs[SRC_GPR8],
19619830574SJean-Christophe DUBOIS 3, false);
19719830574SJean-Christophe DUBOIS } else {
19819830574SJean-Christophe DUBOIS /* CORE 3 is shut down */
19919830574SJean-Christophe DUBOIS arm_set_cpu_off(3);
20019830574SJean-Christophe DUBOIS }
20119830574SJean-Christophe DUBOIS /* We clear the reset bits as the processor changed state */
2024881658aSAlex Bennée imx6_defer_clear_reset_bit(3, s, CORE3_RST_SHIFT);
20319830574SJean-Christophe DUBOIS clear_bit(CORE3_RST_SHIFT, &change_mask);
20419830574SJean-Christophe DUBOIS }
20519830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE2_ENABLE)) {
20619830574SJean-Christophe DUBOIS if (EXTRACT(current_value, CORE2_ENABLE)) {
20719830574SJean-Christophe DUBOIS /* CORE 2 is brought up */
20819830574SJean-Christophe DUBOIS arm_set_cpu_on(2, s->regs[SRC_GPR5], s->regs[SRC_GPR6],
20919830574SJean-Christophe DUBOIS 3, false);
21019830574SJean-Christophe DUBOIS } else {
2114881658aSAlex Bennée /* CORE 2 is shut down */
21219830574SJean-Christophe DUBOIS arm_set_cpu_off(2);
21319830574SJean-Christophe DUBOIS }
21419830574SJean-Christophe DUBOIS /* We clear the reset bits as the processor changed state */
2154881658aSAlex Bennée imx6_defer_clear_reset_bit(2, s, CORE2_RST_SHIFT);
21619830574SJean-Christophe DUBOIS clear_bit(CORE2_RST_SHIFT, &change_mask);
21719830574SJean-Christophe DUBOIS }
21819830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE1_ENABLE)) {
21919830574SJean-Christophe DUBOIS if (EXTRACT(current_value, CORE1_ENABLE)) {
22019830574SJean-Christophe DUBOIS /* CORE 1 is brought up */
22119830574SJean-Christophe DUBOIS arm_set_cpu_on(1, s->regs[SRC_GPR3], s->regs[SRC_GPR4],
22219830574SJean-Christophe DUBOIS 3, false);
22319830574SJean-Christophe DUBOIS } else {
2244881658aSAlex Bennée /* CORE 1 is shut down */
22519830574SJean-Christophe DUBOIS arm_set_cpu_off(1);
22619830574SJean-Christophe DUBOIS }
22719830574SJean-Christophe DUBOIS /* We clear the reset bits as the processor changed state */
2284881658aSAlex Bennée imx6_defer_clear_reset_bit(1, s, CORE1_RST_SHIFT);
22919830574SJean-Christophe DUBOIS clear_bit(CORE1_RST_SHIFT, &change_mask);
23019830574SJean-Christophe DUBOIS }
23119830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE0_RST)) {
23219830574SJean-Christophe DUBOIS arm_reset_cpu(0);
2334881658aSAlex Bennée imx6_defer_clear_reset_bit(0, s, CORE0_RST_SHIFT);
23419830574SJean-Christophe DUBOIS }
23519830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE1_RST)) {
23619830574SJean-Christophe DUBOIS arm_reset_cpu(1);
2374881658aSAlex Bennée imx6_defer_clear_reset_bit(1, s, CORE1_RST_SHIFT);
23819830574SJean-Christophe DUBOIS }
23919830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE2_RST)) {
24019830574SJean-Christophe DUBOIS arm_reset_cpu(2);
2414881658aSAlex Bennée imx6_defer_clear_reset_bit(2, s, CORE2_RST_SHIFT);
24219830574SJean-Christophe DUBOIS }
24319830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE3_RST)) {
24419830574SJean-Christophe DUBOIS arm_reset_cpu(3);
2454881658aSAlex Bennée imx6_defer_clear_reset_bit(3, s, CORE3_RST_SHIFT);
24619830574SJean-Christophe DUBOIS }
24719830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, SW_IPU2_RST)) {
24819830574SJean-Christophe DUBOIS /* We pretend the IPU2 is reset */
24919830574SJean-Christophe DUBOIS clear_bit(SW_IPU2_RST_SHIFT, ¤t_value);
25019830574SJean-Christophe DUBOIS }
25119830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, SW_IPU1_RST)) {
25219830574SJean-Christophe DUBOIS /* We pretend the IPU1 is reset */
25319830574SJean-Christophe DUBOIS clear_bit(SW_IPU1_RST_SHIFT, ¤t_value);
25419830574SJean-Christophe DUBOIS }
25519830574SJean-Christophe DUBOIS s->regs[index] = current_value;
25619830574SJean-Christophe DUBOIS break;
25719830574SJean-Christophe DUBOIS default:
25819830574SJean-Christophe DUBOIS s->regs[index] = current_value;
25919830574SJean-Christophe DUBOIS break;
26019830574SJean-Christophe DUBOIS }
26119830574SJean-Christophe DUBOIS }
26219830574SJean-Christophe DUBOIS
26319830574SJean-Christophe DUBOIS static const struct MemoryRegionOps imx6_src_ops = {
26419830574SJean-Christophe DUBOIS .read = imx6_src_read,
26519830574SJean-Christophe DUBOIS .write = imx6_src_write,
26619830574SJean-Christophe DUBOIS .endianness = DEVICE_NATIVE_ENDIAN,
26719830574SJean-Christophe DUBOIS .valid = {
26819830574SJean-Christophe DUBOIS /*
26919830574SJean-Christophe DUBOIS * Our device would not work correctly if the guest was doing
27019830574SJean-Christophe DUBOIS * unaligned access. This might not be a limitation on the real
27119830574SJean-Christophe DUBOIS * device but in practice there is no reason for a guest to access
27219830574SJean-Christophe DUBOIS * this device unaligned.
27319830574SJean-Christophe DUBOIS */
27419830574SJean-Christophe DUBOIS .min_access_size = 4,
27519830574SJean-Christophe DUBOIS .max_access_size = 4,
27619830574SJean-Christophe DUBOIS .unaligned = false,
27719830574SJean-Christophe DUBOIS },
27819830574SJean-Christophe DUBOIS };
27919830574SJean-Christophe DUBOIS
imx6_src_realize(DeviceState * dev,Error ** errp)28019830574SJean-Christophe DUBOIS static void imx6_src_realize(DeviceState *dev, Error **errp)
28119830574SJean-Christophe DUBOIS {
28219830574SJean-Christophe DUBOIS IMX6SRCState *s = IMX6_SRC(dev);
28319830574SJean-Christophe DUBOIS
28419830574SJean-Christophe DUBOIS memory_region_init_io(&s->iomem, OBJECT(dev), &imx6_src_ops, s,
28519830574SJean-Christophe DUBOIS TYPE_IMX6_SRC, 0x1000);
28619830574SJean-Christophe DUBOIS sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
28719830574SJean-Christophe DUBOIS }
28819830574SJean-Christophe DUBOIS
imx6_src_class_init(ObjectClass * klass,void * data)28919830574SJean-Christophe DUBOIS static void imx6_src_class_init(ObjectClass *klass, void *data)
29019830574SJean-Christophe DUBOIS {
29119830574SJean-Christophe DUBOIS DeviceClass *dc = DEVICE_CLASS(klass);
29219830574SJean-Christophe DUBOIS
29319830574SJean-Christophe DUBOIS dc->realize = imx6_src_realize;
294*e3d08143SPeter Maydell device_class_set_legacy_reset(dc, imx6_src_reset);
29519830574SJean-Christophe DUBOIS dc->vmsd = &vmstate_imx6_src;
29619830574SJean-Christophe DUBOIS dc->desc = "i.MX6 System Reset Controller";
29719830574SJean-Christophe DUBOIS }
29819830574SJean-Christophe DUBOIS
29919830574SJean-Christophe DUBOIS static const TypeInfo imx6_src_info = {
30019830574SJean-Christophe DUBOIS .name = TYPE_IMX6_SRC,
30119830574SJean-Christophe DUBOIS .parent = TYPE_SYS_BUS_DEVICE,
30219830574SJean-Christophe DUBOIS .instance_size = sizeof(IMX6SRCState),
30319830574SJean-Christophe DUBOIS .class_init = imx6_src_class_init,
30419830574SJean-Christophe DUBOIS };
30519830574SJean-Christophe DUBOIS
imx6_src_register_types(void)30619830574SJean-Christophe DUBOIS static void imx6_src_register_types(void)
30719830574SJean-Christophe DUBOIS {
30819830574SJean-Christophe DUBOIS type_register_static(&imx6_src_info);
30919830574SJean-Christophe DUBOIS }
31019830574SJean-Christophe DUBOIS
31119830574SJean-Christophe DUBOIS type_init(imx6_src_register_types)
312