xref: /qemu/hw/misc/imx6_src.c (revision 12d1a768bdfea6e27a3a829228840d72507613a1)
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"
202eabc498SBernhard Beschow #include "trace.h"
2119830574SJean-Christophe DUBOIS 
22d675765aSPeter Maydell static const char *imx6_src_reg_name(uint32_t reg)
2319830574SJean-Christophe DUBOIS {
2419830574SJean-Christophe DUBOIS     static char unknown[20];
2519830574SJean-Christophe DUBOIS 
2619830574SJean-Christophe DUBOIS     switch (reg) {
2719830574SJean-Christophe DUBOIS     case SRC_SCR:
2819830574SJean-Christophe DUBOIS         return "SRC_SCR";
2919830574SJean-Christophe DUBOIS     case SRC_SBMR1:
3019830574SJean-Christophe DUBOIS         return "SRC_SBMR1";
3119830574SJean-Christophe DUBOIS     case SRC_SRSR:
3219830574SJean-Christophe DUBOIS         return "SRC_SRSR";
3319830574SJean-Christophe DUBOIS     case SRC_SISR:
3419830574SJean-Christophe DUBOIS         return "SRC_SISR";
3519830574SJean-Christophe DUBOIS     case SRC_SIMR:
3619830574SJean-Christophe DUBOIS         return "SRC_SIMR";
3719830574SJean-Christophe DUBOIS     case SRC_SBMR2:
3819830574SJean-Christophe DUBOIS         return "SRC_SBMR2";
3919830574SJean-Christophe DUBOIS     case SRC_GPR1:
4019830574SJean-Christophe DUBOIS         return "SRC_GPR1";
4119830574SJean-Christophe DUBOIS     case SRC_GPR2:
4219830574SJean-Christophe DUBOIS         return "SRC_GPR2";
4319830574SJean-Christophe DUBOIS     case SRC_GPR3:
4419830574SJean-Christophe DUBOIS         return "SRC_GPR3";
4519830574SJean-Christophe DUBOIS     case SRC_GPR4:
4619830574SJean-Christophe DUBOIS         return "SRC_GPR4";
4719830574SJean-Christophe DUBOIS     case SRC_GPR5:
4819830574SJean-Christophe DUBOIS         return "SRC_GPR5";
4919830574SJean-Christophe DUBOIS     case SRC_GPR6:
5019830574SJean-Christophe DUBOIS         return "SRC_GPR6";
5119830574SJean-Christophe DUBOIS     case SRC_GPR7:
5219830574SJean-Christophe DUBOIS         return "SRC_GPR7";
5319830574SJean-Christophe DUBOIS     case SRC_GPR8:
5419830574SJean-Christophe DUBOIS         return "SRC_GPR8";
5519830574SJean-Christophe DUBOIS     case SRC_GPR9:
5619830574SJean-Christophe DUBOIS         return "SRC_GPR9";
5719830574SJean-Christophe DUBOIS     case SRC_GPR10:
5819830574SJean-Christophe DUBOIS         return "SRC_GPR10";
5919830574SJean-Christophe DUBOIS     default:
60ca4af17cSPhilippe Mathieu-Daudé         snprintf(unknown, sizeof(unknown), "%u ?", reg);
6119830574SJean-Christophe DUBOIS         return unknown;
6219830574SJean-Christophe DUBOIS     }
6319830574SJean-Christophe DUBOIS }
6419830574SJean-Christophe DUBOIS 
6519830574SJean-Christophe DUBOIS static const VMStateDescription vmstate_imx6_src = {
6619830574SJean-Christophe DUBOIS     .name = TYPE_IMX6_SRC,
6719830574SJean-Christophe DUBOIS     .version_id = 1,
6819830574SJean-Christophe DUBOIS     .minimum_version_id = 1,
69e4ea952fSRichard Henderson     .fields = (const VMStateField[]) {
7019830574SJean-Christophe DUBOIS         VMSTATE_UINT32_ARRAY(regs, IMX6SRCState, SRC_MAX),
7119830574SJean-Christophe DUBOIS         VMSTATE_END_OF_LIST()
7219830574SJean-Christophe DUBOIS     },
7319830574SJean-Christophe DUBOIS };
7419830574SJean-Christophe DUBOIS 
7519830574SJean-Christophe DUBOIS static void imx6_src_reset(DeviceState *dev)
7619830574SJean-Christophe DUBOIS {
7719830574SJean-Christophe DUBOIS     IMX6SRCState *s = IMX6_SRC(dev);
7819830574SJean-Christophe DUBOIS 
792eabc498SBernhard Beschow     trace_imx6_src_reset();
8019830574SJean-Christophe DUBOIS 
8119830574SJean-Christophe DUBOIS     memset(s->regs, 0, sizeof(s->regs));
8219830574SJean-Christophe DUBOIS 
8319830574SJean-Christophe DUBOIS     /* Set reset values */
8419830574SJean-Christophe DUBOIS     s->regs[SRC_SCR] = 0x521;
8519830574SJean-Christophe DUBOIS     s->regs[SRC_SRSR] = 0x1;
8619830574SJean-Christophe DUBOIS     s->regs[SRC_SIMR] = 0x1F;
8719830574SJean-Christophe DUBOIS }
8819830574SJean-Christophe DUBOIS 
8919830574SJean-Christophe DUBOIS static uint64_t imx6_src_read(void *opaque, hwaddr offset, unsigned size)
9019830574SJean-Christophe DUBOIS {
9119830574SJean-Christophe DUBOIS     uint32_t value = 0;
9219830574SJean-Christophe DUBOIS     IMX6SRCState *s = (IMX6SRCState *)opaque;
9319830574SJean-Christophe DUBOIS     uint32_t index = offset >> 2;
9419830574SJean-Christophe DUBOIS 
9519830574SJean-Christophe DUBOIS     if (index < SRC_MAX) {
9619830574SJean-Christophe DUBOIS         value = s->regs[index];
9719830574SJean-Christophe DUBOIS     } else {
9819830574SJean-Christophe DUBOIS         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
9919830574SJean-Christophe DUBOIS                       HWADDR_PRIx "\n", TYPE_IMX6_SRC, __func__, offset);
10019830574SJean-Christophe DUBOIS 
10119830574SJean-Christophe DUBOIS     }
10219830574SJean-Christophe DUBOIS 
1032eabc498SBernhard Beschow     trace_imx6_src_read(imx6_src_reg_name(index), value);
10419830574SJean-Christophe DUBOIS 
10519830574SJean-Christophe DUBOIS     return value;
10619830574SJean-Christophe DUBOIS }
10719830574SJean-Christophe DUBOIS 
1084881658aSAlex Bennée 
1094881658aSAlex Bennée /* The reset is asynchronous so we need to defer clearing the reset
1104881658aSAlex Bennée  * bit until the work is completed.
1114881658aSAlex Bennée  */
1124881658aSAlex Bennée 
1134881658aSAlex Bennée struct SRCSCRResetInfo {
1144881658aSAlex Bennée     IMX6SRCState *s;
1154881658aSAlex Bennée     int reset_bit;
1164881658aSAlex Bennée };
1174881658aSAlex Bennée 
1184881658aSAlex Bennée static void imx6_clear_reset_bit(CPUState *cpu, run_on_cpu_data data)
1194881658aSAlex Bennée {
1204881658aSAlex Bennée     struct SRCSCRResetInfo *ri = data.host_ptr;
1214881658aSAlex Bennée     IMX6SRCState *s = ri->s;
1224881658aSAlex Bennée 
123195801d7SStefan Hajnoczi     assert(bql_locked());
1244881658aSAlex Bennée 
1254881658aSAlex Bennée     s->regs[SRC_SCR] = deposit32(s->regs[SRC_SCR], ri->reset_bit, 1, 0);
1262eabc498SBernhard Beschow     trace_imx6_clear_reset_bit(imx6_src_reg_name(SRC_SCR), s->regs[SRC_SCR]);
1274881658aSAlex Bennée 
1284881658aSAlex Bennée     g_free(ri);
1294881658aSAlex Bennée }
1304881658aSAlex Bennée 
1314881658aSAlex Bennée static void imx6_defer_clear_reset_bit(int cpuid,
1324881658aSAlex Bennée                                        IMX6SRCState *s,
1334881658aSAlex Bennée                                        unsigned long reset_shift)
1344881658aSAlex Bennée {
1354881658aSAlex Bennée     struct SRCSCRResetInfo *ri;
1365e2fb7c5SPeter Maydell     CPUState *cpu = arm_get_cpu_by_id(cpuid);
1375e2fb7c5SPeter Maydell 
1385e2fb7c5SPeter Maydell     if (!cpu) {
1395e2fb7c5SPeter Maydell         return;
1405e2fb7c5SPeter Maydell     }
1414881658aSAlex Bennée 
142b21e2380SMarkus Armbruster     ri = g_new(struct SRCSCRResetInfo, 1);
1434881658aSAlex Bennée     ri->s = s;
1444881658aSAlex Bennée     ri->reset_bit = reset_shift;
1454881658aSAlex Bennée 
1465e2fb7c5SPeter Maydell     async_run_on_cpu(cpu, imx6_clear_reset_bit, RUN_ON_CPU_HOST_PTR(ri));
1474881658aSAlex Bennée }
1484881658aSAlex Bennée 
1494881658aSAlex Bennée 
15019830574SJean-Christophe DUBOIS static void imx6_src_write(void *opaque, hwaddr offset, uint64_t value,
15119830574SJean-Christophe DUBOIS                            unsigned size)
15219830574SJean-Christophe DUBOIS {
15319830574SJean-Christophe DUBOIS     IMX6SRCState *s = (IMX6SRCState *)opaque;
15419830574SJean-Christophe DUBOIS     uint32_t index = offset >> 2;
15519830574SJean-Christophe DUBOIS     unsigned long change_mask;
15619830574SJean-Christophe DUBOIS     unsigned long current_value = value;
15719830574SJean-Christophe DUBOIS 
15819830574SJean-Christophe DUBOIS     if (index >=  SRC_MAX) {
15919830574SJean-Christophe DUBOIS         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
16019830574SJean-Christophe DUBOIS                       HWADDR_PRIx "\n", TYPE_IMX6_SRC, __func__, offset);
16119830574SJean-Christophe DUBOIS         return;
16219830574SJean-Christophe DUBOIS     }
16319830574SJean-Christophe DUBOIS 
1642eabc498SBernhard Beschow     trace_imx6_src_write(imx6_src_reg_name(index), value);
16519830574SJean-Christophe DUBOIS 
16619830574SJean-Christophe DUBOIS     change_mask = s->regs[index] ^ (uint32_t)current_value;
16719830574SJean-Christophe DUBOIS 
16819830574SJean-Christophe DUBOIS     switch (index) {
16919830574SJean-Christophe DUBOIS     case SRC_SCR:
17019830574SJean-Christophe DUBOIS         /*
17119830574SJean-Christophe DUBOIS          * On real hardware when the system reset controller starts a
17219830574SJean-Christophe DUBOIS          * secondary CPU it runs through some boot ROM code which reads
17319830574SJean-Christophe DUBOIS          * the SRC_GPRX registers controlling the start address and branches
17419830574SJean-Christophe DUBOIS          * to it.
17519830574SJean-Christophe DUBOIS          * Here we are taking a short cut and branching directly to the
17619830574SJean-Christophe DUBOIS          * requested address (we don't want to run the boot ROM code inside
17719830574SJean-Christophe DUBOIS          * QEMU)
17819830574SJean-Christophe DUBOIS          */
17919830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE3_ENABLE)) {
18019830574SJean-Christophe DUBOIS             if (EXTRACT(current_value, CORE3_ENABLE)) {
18119830574SJean-Christophe DUBOIS                 /* CORE 3 is brought up */
18219830574SJean-Christophe DUBOIS                 arm_set_cpu_on(3, s->regs[SRC_GPR7], s->regs[SRC_GPR8],
18319830574SJean-Christophe DUBOIS                                3, false);
18419830574SJean-Christophe DUBOIS             } else {
18519830574SJean-Christophe DUBOIS                 /* CORE 3 is shut down */
18619830574SJean-Christophe DUBOIS                 arm_set_cpu_off(3);
18719830574SJean-Christophe DUBOIS             }
18819830574SJean-Christophe DUBOIS             /* We clear the reset bits as the processor changed state */
1894881658aSAlex Bennée             imx6_defer_clear_reset_bit(3, s, CORE3_RST_SHIFT);
19019830574SJean-Christophe DUBOIS             clear_bit(CORE3_RST_SHIFT, &change_mask);
19119830574SJean-Christophe DUBOIS         }
19219830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE2_ENABLE)) {
19319830574SJean-Christophe DUBOIS             if (EXTRACT(current_value, CORE2_ENABLE)) {
19419830574SJean-Christophe DUBOIS                 /* CORE 2 is brought up */
19519830574SJean-Christophe DUBOIS                 arm_set_cpu_on(2, s->regs[SRC_GPR5], s->regs[SRC_GPR6],
19619830574SJean-Christophe DUBOIS                                3, false);
19719830574SJean-Christophe DUBOIS             } else {
1984881658aSAlex Bennée                 /* CORE 2 is shut down */
19919830574SJean-Christophe DUBOIS                 arm_set_cpu_off(2);
20019830574SJean-Christophe DUBOIS             }
20119830574SJean-Christophe DUBOIS             /* We clear the reset bits as the processor changed state */
2024881658aSAlex Bennée             imx6_defer_clear_reset_bit(2, s, CORE2_RST_SHIFT);
20319830574SJean-Christophe DUBOIS             clear_bit(CORE2_RST_SHIFT, &change_mask);
20419830574SJean-Christophe DUBOIS         }
20519830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE1_ENABLE)) {
20619830574SJean-Christophe DUBOIS             if (EXTRACT(current_value, CORE1_ENABLE)) {
20719830574SJean-Christophe DUBOIS                 /* CORE 1 is brought up */
20819830574SJean-Christophe DUBOIS                 arm_set_cpu_on(1, s->regs[SRC_GPR3], s->regs[SRC_GPR4],
20919830574SJean-Christophe DUBOIS                                3, false);
21019830574SJean-Christophe DUBOIS             } else {
2114881658aSAlex Bennée                 /* CORE 1 is shut down */
21219830574SJean-Christophe DUBOIS                 arm_set_cpu_off(1);
21319830574SJean-Christophe DUBOIS             }
21419830574SJean-Christophe DUBOIS             /* We clear the reset bits as the processor changed state */
2154881658aSAlex Bennée             imx6_defer_clear_reset_bit(1, s, CORE1_RST_SHIFT);
21619830574SJean-Christophe DUBOIS             clear_bit(CORE1_RST_SHIFT, &change_mask);
21719830574SJean-Christophe DUBOIS         }
21819830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE0_RST)) {
21919830574SJean-Christophe DUBOIS             arm_reset_cpu(0);
2204881658aSAlex Bennée             imx6_defer_clear_reset_bit(0, s, CORE0_RST_SHIFT);
22119830574SJean-Christophe DUBOIS         }
22219830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE1_RST)) {
22319830574SJean-Christophe DUBOIS             arm_reset_cpu(1);
2244881658aSAlex Bennée             imx6_defer_clear_reset_bit(1, s, CORE1_RST_SHIFT);
22519830574SJean-Christophe DUBOIS         }
22619830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE2_RST)) {
22719830574SJean-Christophe DUBOIS             arm_reset_cpu(2);
2284881658aSAlex Bennée             imx6_defer_clear_reset_bit(2, s, CORE2_RST_SHIFT);
22919830574SJean-Christophe DUBOIS         }
23019830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE3_RST)) {
23119830574SJean-Christophe DUBOIS             arm_reset_cpu(3);
2324881658aSAlex Bennée             imx6_defer_clear_reset_bit(3, s, CORE3_RST_SHIFT);
23319830574SJean-Christophe DUBOIS         }
23419830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, SW_IPU2_RST)) {
23519830574SJean-Christophe DUBOIS             /* We pretend the IPU2 is reset */
23619830574SJean-Christophe DUBOIS             clear_bit(SW_IPU2_RST_SHIFT, &current_value);
23719830574SJean-Christophe DUBOIS         }
23819830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, SW_IPU1_RST)) {
23919830574SJean-Christophe DUBOIS             /* We pretend the IPU1 is reset */
24019830574SJean-Christophe DUBOIS             clear_bit(SW_IPU1_RST_SHIFT, &current_value);
24119830574SJean-Christophe DUBOIS         }
24219830574SJean-Christophe DUBOIS         s->regs[index] = current_value;
24319830574SJean-Christophe DUBOIS         break;
24419830574SJean-Christophe DUBOIS     default:
24519830574SJean-Christophe DUBOIS         s->regs[index] = current_value;
24619830574SJean-Christophe DUBOIS         break;
24719830574SJean-Christophe DUBOIS     }
24819830574SJean-Christophe DUBOIS }
24919830574SJean-Christophe DUBOIS 
25019830574SJean-Christophe DUBOIS static const struct MemoryRegionOps imx6_src_ops = {
25119830574SJean-Christophe DUBOIS     .read = imx6_src_read,
25219830574SJean-Christophe DUBOIS     .write = imx6_src_write,
25319830574SJean-Christophe DUBOIS     .endianness = DEVICE_NATIVE_ENDIAN,
25419830574SJean-Christophe DUBOIS     .valid = {
25519830574SJean-Christophe DUBOIS         /*
25619830574SJean-Christophe DUBOIS          * Our device would not work correctly if the guest was doing
25719830574SJean-Christophe DUBOIS          * unaligned access. This might not be a limitation on the real
25819830574SJean-Christophe DUBOIS          * device but in practice there is no reason for a guest to access
25919830574SJean-Christophe DUBOIS          * this device unaligned.
26019830574SJean-Christophe DUBOIS          */
26119830574SJean-Christophe DUBOIS         .min_access_size = 4,
26219830574SJean-Christophe DUBOIS         .max_access_size = 4,
26319830574SJean-Christophe DUBOIS         .unaligned = false,
26419830574SJean-Christophe DUBOIS     },
26519830574SJean-Christophe DUBOIS };
26619830574SJean-Christophe DUBOIS 
26719830574SJean-Christophe DUBOIS static void imx6_src_realize(DeviceState *dev, Error **errp)
26819830574SJean-Christophe DUBOIS {
26919830574SJean-Christophe DUBOIS     IMX6SRCState *s = IMX6_SRC(dev);
27019830574SJean-Christophe DUBOIS 
27119830574SJean-Christophe DUBOIS     memory_region_init_io(&s->iomem, OBJECT(dev), &imx6_src_ops, s,
27219830574SJean-Christophe DUBOIS                           TYPE_IMX6_SRC, 0x1000);
27319830574SJean-Christophe DUBOIS     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
27419830574SJean-Christophe DUBOIS }
27519830574SJean-Christophe DUBOIS 
276*12d1a768SPhilippe Mathieu-Daudé static void imx6_src_class_init(ObjectClass *klass, const void *data)
27719830574SJean-Christophe DUBOIS {
27819830574SJean-Christophe DUBOIS     DeviceClass *dc = DEVICE_CLASS(klass);
27919830574SJean-Christophe DUBOIS 
28019830574SJean-Christophe DUBOIS     dc->realize = imx6_src_realize;
281e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, imx6_src_reset);
28219830574SJean-Christophe DUBOIS     dc->vmsd = &vmstate_imx6_src;
28319830574SJean-Christophe DUBOIS     dc->desc = "i.MX6 System Reset Controller";
28419830574SJean-Christophe DUBOIS }
28519830574SJean-Christophe DUBOIS 
28619830574SJean-Christophe DUBOIS static const TypeInfo imx6_src_info = {
28719830574SJean-Christophe DUBOIS     .name          = TYPE_IMX6_SRC,
28819830574SJean-Christophe DUBOIS     .parent        = TYPE_SYS_BUS_DEVICE,
28919830574SJean-Christophe DUBOIS     .instance_size = sizeof(IMX6SRCState),
29019830574SJean-Christophe DUBOIS     .class_init    = imx6_src_class_init,
29119830574SJean-Christophe DUBOIS };
29219830574SJean-Christophe DUBOIS 
29319830574SJean-Christophe DUBOIS static void imx6_src_register_types(void)
29419830574SJean-Christophe DUBOIS {
29519830574SJean-Christophe DUBOIS     type_register_static(&imx6_src_info);
29619830574SJean-Christophe DUBOIS }
29719830574SJean-Christophe DUBOIS 
29819830574SJean-Christophe DUBOIS type_init(imx6_src_register_types)
299