xref: /openbmc/qemu/hw/misc/imx6_src.c (revision db725815985654007ade0fd53590d613fd657208)
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 "sysemu/sysemu.h"
1519830574SJean-Christophe DUBOIS #include "qemu/bitops.h"
1603dd024fSPaolo Bonzini #include "qemu/log.h"
17*db725815SMarkus Armbruster #include "qemu/main-loop.h"
180b8fa32fSMarkus Armbruster #include "qemu/module.h"
1919830574SJean-Christophe DUBOIS #include "arm-powerctl.h"
204881658aSAlex Bennée #include "qom/cpu.h"
2119830574SJean-Christophe DUBOIS 
2219830574SJean-Christophe DUBOIS #ifndef DEBUG_IMX6_SRC
2319830574SJean-Christophe DUBOIS #define DEBUG_IMX6_SRC 0
2419830574SJean-Christophe DUBOIS #endif
2519830574SJean-Christophe DUBOIS 
2619830574SJean-Christophe DUBOIS #define DPRINTF(fmt, args...) \
2719830574SJean-Christophe DUBOIS     do { \
2819830574SJean-Christophe DUBOIS         if (DEBUG_IMX6_SRC) { \
2919830574SJean-Christophe DUBOIS             fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX6_SRC, \
3019830574SJean-Christophe DUBOIS                                              __func__, ##args); \
3119830574SJean-Christophe DUBOIS         } \
3219830574SJean-Christophe DUBOIS     } while (0)
3319830574SJean-Christophe DUBOIS 
34d675765aSPeter Maydell static const char *imx6_src_reg_name(uint32_t reg)
3519830574SJean-Christophe DUBOIS {
3619830574SJean-Christophe DUBOIS     static char unknown[20];
3719830574SJean-Christophe DUBOIS 
3819830574SJean-Christophe DUBOIS     switch (reg) {
3919830574SJean-Christophe DUBOIS     case SRC_SCR:
4019830574SJean-Christophe DUBOIS         return "SRC_SCR";
4119830574SJean-Christophe DUBOIS     case SRC_SBMR1:
4219830574SJean-Christophe DUBOIS         return "SRC_SBMR1";
4319830574SJean-Christophe DUBOIS     case SRC_SRSR:
4419830574SJean-Christophe DUBOIS         return "SRC_SRSR";
4519830574SJean-Christophe DUBOIS     case SRC_SISR:
4619830574SJean-Christophe DUBOIS         return "SRC_SISR";
4719830574SJean-Christophe DUBOIS     case SRC_SIMR:
4819830574SJean-Christophe DUBOIS         return "SRC_SIMR";
4919830574SJean-Christophe DUBOIS     case SRC_SBMR2:
5019830574SJean-Christophe DUBOIS         return "SRC_SBMR2";
5119830574SJean-Christophe DUBOIS     case SRC_GPR1:
5219830574SJean-Christophe DUBOIS         return "SRC_GPR1";
5319830574SJean-Christophe DUBOIS     case SRC_GPR2:
5419830574SJean-Christophe DUBOIS         return "SRC_GPR2";
5519830574SJean-Christophe DUBOIS     case SRC_GPR3:
5619830574SJean-Christophe DUBOIS         return "SRC_GPR3";
5719830574SJean-Christophe DUBOIS     case SRC_GPR4:
5819830574SJean-Christophe DUBOIS         return "SRC_GPR4";
5919830574SJean-Christophe DUBOIS     case SRC_GPR5:
6019830574SJean-Christophe DUBOIS         return "SRC_GPR5";
6119830574SJean-Christophe DUBOIS     case SRC_GPR6:
6219830574SJean-Christophe DUBOIS         return "SRC_GPR6";
6319830574SJean-Christophe DUBOIS     case SRC_GPR7:
6419830574SJean-Christophe DUBOIS         return "SRC_GPR7";
6519830574SJean-Christophe DUBOIS     case SRC_GPR8:
6619830574SJean-Christophe DUBOIS         return "SRC_GPR8";
6719830574SJean-Christophe DUBOIS     case SRC_GPR9:
6819830574SJean-Christophe DUBOIS         return "SRC_GPR9";
6919830574SJean-Christophe DUBOIS     case SRC_GPR10:
7019830574SJean-Christophe DUBOIS         return "SRC_GPR10";
7119830574SJean-Christophe DUBOIS     default:
7219830574SJean-Christophe DUBOIS         sprintf(unknown, "%d ?", reg);
7319830574SJean-Christophe DUBOIS         return unknown;
7419830574SJean-Christophe DUBOIS     }
7519830574SJean-Christophe DUBOIS }
7619830574SJean-Christophe DUBOIS 
7719830574SJean-Christophe DUBOIS static const VMStateDescription vmstate_imx6_src = {
7819830574SJean-Christophe DUBOIS     .name = TYPE_IMX6_SRC,
7919830574SJean-Christophe DUBOIS     .version_id = 1,
8019830574SJean-Christophe DUBOIS     .minimum_version_id = 1,
8119830574SJean-Christophe DUBOIS     .fields = (VMStateField[]) {
8219830574SJean-Christophe DUBOIS         VMSTATE_UINT32_ARRAY(regs, IMX6SRCState, SRC_MAX),
8319830574SJean-Christophe DUBOIS         VMSTATE_END_OF_LIST()
8419830574SJean-Christophe DUBOIS     },
8519830574SJean-Christophe DUBOIS };
8619830574SJean-Christophe DUBOIS 
8719830574SJean-Christophe DUBOIS static void imx6_src_reset(DeviceState *dev)
8819830574SJean-Christophe DUBOIS {
8919830574SJean-Christophe DUBOIS     IMX6SRCState *s = IMX6_SRC(dev);
9019830574SJean-Christophe DUBOIS 
9119830574SJean-Christophe DUBOIS     DPRINTF("\n");
9219830574SJean-Christophe DUBOIS 
9319830574SJean-Christophe DUBOIS     memset(s->regs, 0, sizeof(s->regs));
9419830574SJean-Christophe DUBOIS 
9519830574SJean-Christophe DUBOIS     /* Set reset values */
9619830574SJean-Christophe DUBOIS     s->regs[SRC_SCR] = 0x521;
9719830574SJean-Christophe DUBOIS     s->regs[SRC_SRSR] = 0x1;
9819830574SJean-Christophe DUBOIS     s->regs[SRC_SIMR] = 0x1F;
9919830574SJean-Christophe DUBOIS }
10019830574SJean-Christophe DUBOIS 
10119830574SJean-Christophe DUBOIS static uint64_t imx6_src_read(void *opaque, hwaddr offset, unsigned size)
10219830574SJean-Christophe DUBOIS {
10319830574SJean-Christophe DUBOIS     uint32_t value = 0;
10419830574SJean-Christophe DUBOIS     IMX6SRCState *s = (IMX6SRCState *)opaque;
10519830574SJean-Christophe DUBOIS     uint32_t index = offset >> 2;
10619830574SJean-Christophe DUBOIS 
10719830574SJean-Christophe DUBOIS     if (index < SRC_MAX) {
10819830574SJean-Christophe DUBOIS         value = s->regs[index];
10919830574SJean-Christophe DUBOIS     } else {
11019830574SJean-Christophe DUBOIS         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
11119830574SJean-Christophe DUBOIS                       HWADDR_PRIx "\n", TYPE_IMX6_SRC, __func__, offset);
11219830574SJean-Christophe DUBOIS 
11319830574SJean-Christophe DUBOIS     }
11419830574SJean-Christophe DUBOIS 
11519830574SJean-Christophe DUBOIS     DPRINTF("reg[%s] => 0x%" PRIx32 "\n", imx6_src_reg_name(index), value);
11619830574SJean-Christophe DUBOIS 
11719830574SJean-Christophe DUBOIS     return value;
11819830574SJean-Christophe DUBOIS }
11919830574SJean-Christophe DUBOIS 
1204881658aSAlex Bennée 
1214881658aSAlex Bennée /* The reset is asynchronous so we need to defer clearing the reset
1224881658aSAlex Bennée  * bit until the work is completed.
1234881658aSAlex Bennée  */
1244881658aSAlex Bennée 
1254881658aSAlex Bennée struct SRCSCRResetInfo {
1264881658aSAlex Bennée     IMX6SRCState *s;
1274881658aSAlex Bennée     int reset_bit;
1284881658aSAlex Bennée };
1294881658aSAlex Bennée 
1304881658aSAlex Bennée static void imx6_clear_reset_bit(CPUState *cpu, run_on_cpu_data data)
1314881658aSAlex Bennée {
1324881658aSAlex Bennée     struct SRCSCRResetInfo *ri = data.host_ptr;
1334881658aSAlex Bennée     IMX6SRCState *s = ri->s;
1344881658aSAlex Bennée 
1354881658aSAlex Bennée     assert(qemu_mutex_iothread_locked());
1364881658aSAlex Bennée 
1374881658aSAlex Bennée     s->regs[SRC_SCR] = deposit32(s->regs[SRC_SCR], ri->reset_bit, 1, 0);
1384881658aSAlex Bennée     DPRINTF("reg[%s] <= 0x%" PRIx32 "\n",
1394881658aSAlex Bennée             imx6_src_reg_name(SRC_SCR), s->regs[SRC_SCR]);
1404881658aSAlex Bennée 
1414881658aSAlex Bennée     g_free(ri);
1424881658aSAlex Bennée }
1434881658aSAlex Bennée 
1444881658aSAlex Bennée static void imx6_defer_clear_reset_bit(int cpuid,
1454881658aSAlex Bennée                                        IMX6SRCState *s,
1464881658aSAlex Bennée                                        unsigned long reset_shift)
1474881658aSAlex Bennée {
1484881658aSAlex Bennée     struct SRCSCRResetInfo *ri;
1495e2fb7c5SPeter Maydell     CPUState *cpu = arm_get_cpu_by_id(cpuid);
1505e2fb7c5SPeter Maydell 
1515e2fb7c5SPeter Maydell     if (!cpu) {
1525e2fb7c5SPeter Maydell         return;
1535e2fb7c5SPeter Maydell     }
1544881658aSAlex Bennée 
1554881658aSAlex Bennée     ri = g_malloc(sizeof(struct SRCSCRResetInfo));
1564881658aSAlex Bennée     ri->s = s;
1574881658aSAlex Bennée     ri->reset_bit = reset_shift;
1584881658aSAlex Bennée 
1595e2fb7c5SPeter Maydell     async_run_on_cpu(cpu, imx6_clear_reset_bit, RUN_ON_CPU_HOST_PTR(ri));
1604881658aSAlex Bennée }
1614881658aSAlex Bennée 
1624881658aSAlex Bennée 
16319830574SJean-Christophe DUBOIS static void imx6_src_write(void *opaque, hwaddr offset, uint64_t value,
16419830574SJean-Christophe DUBOIS                            unsigned size)
16519830574SJean-Christophe DUBOIS {
16619830574SJean-Christophe DUBOIS     IMX6SRCState *s = (IMX6SRCState *)opaque;
16719830574SJean-Christophe DUBOIS     uint32_t index = offset >> 2;
16819830574SJean-Christophe DUBOIS     unsigned long change_mask;
16919830574SJean-Christophe DUBOIS     unsigned long current_value = value;
17019830574SJean-Christophe DUBOIS 
17119830574SJean-Christophe DUBOIS     if (index >=  SRC_MAX) {
17219830574SJean-Christophe DUBOIS         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
17319830574SJean-Christophe DUBOIS                       HWADDR_PRIx "\n", TYPE_IMX6_SRC, __func__, offset);
17419830574SJean-Christophe DUBOIS         return;
17519830574SJean-Christophe DUBOIS     }
17619830574SJean-Christophe DUBOIS 
17719830574SJean-Christophe DUBOIS     DPRINTF("reg[%s] <= 0x%" PRIx32 "\n", imx6_src_reg_name(index),
17819830574SJean-Christophe DUBOIS             (uint32_t)current_value);
17919830574SJean-Christophe DUBOIS 
18019830574SJean-Christophe DUBOIS     change_mask = s->regs[index] ^ (uint32_t)current_value;
18119830574SJean-Christophe DUBOIS 
18219830574SJean-Christophe DUBOIS     switch (index) {
18319830574SJean-Christophe DUBOIS     case SRC_SCR:
18419830574SJean-Christophe DUBOIS         /*
18519830574SJean-Christophe DUBOIS          * On real hardware when the system reset controller starts a
18619830574SJean-Christophe DUBOIS          * secondary CPU it runs through some boot ROM code which reads
18719830574SJean-Christophe DUBOIS          * the SRC_GPRX registers controlling the start address and branches
18819830574SJean-Christophe DUBOIS          * to it.
18919830574SJean-Christophe DUBOIS          * Here we are taking a short cut and branching directly to the
19019830574SJean-Christophe DUBOIS          * requested address (we don't want to run the boot ROM code inside
19119830574SJean-Christophe DUBOIS          * QEMU)
19219830574SJean-Christophe DUBOIS          */
19319830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE3_ENABLE)) {
19419830574SJean-Christophe DUBOIS             if (EXTRACT(current_value, CORE3_ENABLE)) {
19519830574SJean-Christophe DUBOIS                 /* CORE 3 is brought up */
19619830574SJean-Christophe DUBOIS                 arm_set_cpu_on(3, s->regs[SRC_GPR7], s->regs[SRC_GPR8],
19719830574SJean-Christophe DUBOIS                                3, false);
19819830574SJean-Christophe DUBOIS             } else {
19919830574SJean-Christophe DUBOIS                 /* CORE 3 is shut down */
20019830574SJean-Christophe DUBOIS                 arm_set_cpu_off(3);
20119830574SJean-Christophe DUBOIS             }
20219830574SJean-Christophe DUBOIS             /* We clear the reset bits as the processor changed state */
2034881658aSAlex Bennée             imx6_defer_clear_reset_bit(3, s, CORE3_RST_SHIFT);
20419830574SJean-Christophe DUBOIS             clear_bit(CORE3_RST_SHIFT, &change_mask);
20519830574SJean-Christophe DUBOIS         }
20619830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE2_ENABLE)) {
20719830574SJean-Christophe DUBOIS             if (EXTRACT(current_value, CORE2_ENABLE)) {
20819830574SJean-Christophe DUBOIS                 /* CORE 2 is brought up */
20919830574SJean-Christophe DUBOIS                 arm_set_cpu_on(2, s->regs[SRC_GPR5], s->regs[SRC_GPR6],
21019830574SJean-Christophe DUBOIS                                3, false);
21119830574SJean-Christophe DUBOIS             } else {
2124881658aSAlex Bennée                 /* CORE 2 is shut down */
21319830574SJean-Christophe DUBOIS                 arm_set_cpu_off(2);
21419830574SJean-Christophe DUBOIS             }
21519830574SJean-Christophe DUBOIS             /* We clear the reset bits as the processor changed state */
2164881658aSAlex Bennée             imx6_defer_clear_reset_bit(2, s, CORE2_RST_SHIFT);
21719830574SJean-Christophe DUBOIS             clear_bit(CORE2_RST_SHIFT, &change_mask);
21819830574SJean-Christophe DUBOIS         }
21919830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE1_ENABLE)) {
22019830574SJean-Christophe DUBOIS             if (EXTRACT(current_value, CORE1_ENABLE)) {
22119830574SJean-Christophe DUBOIS                 /* CORE 1 is brought up */
22219830574SJean-Christophe DUBOIS                 arm_set_cpu_on(1, s->regs[SRC_GPR3], s->regs[SRC_GPR4],
22319830574SJean-Christophe DUBOIS                                3, false);
22419830574SJean-Christophe DUBOIS             } else {
2254881658aSAlex Bennée                 /* CORE 1 is shut down */
22619830574SJean-Christophe DUBOIS                 arm_set_cpu_off(1);
22719830574SJean-Christophe DUBOIS             }
22819830574SJean-Christophe DUBOIS             /* We clear the reset bits as the processor changed state */
2294881658aSAlex Bennée             imx6_defer_clear_reset_bit(1, s, CORE1_RST_SHIFT);
23019830574SJean-Christophe DUBOIS             clear_bit(CORE1_RST_SHIFT, &change_mask);
23119830574SJean-Christophe DUBOIS         }
23219830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE0_RST)) {
23319830574SJean-Christophe DUBOIS             arm_reset_cpu(0);
2344881658aSAlex Bennée             imx6_defer_clear_reset_bit(0, s, CORE0_RST_SHIFT);
23519830574SJean-Christophe DUBOIS         }
23619830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE1_RST)) {
23719830574SJean-Christophe DUBOIS             arm_reset_cpu(1);
2384881658aSAlex Bennée             imx6_defer_clear_reset_bit(1, s, CORE1_RST_SHIFT);
23919830574SJean-Christophe DUBOIS         }
24019830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE2_RST)) {
24119830574SJean-Christophe DUBOIS             arm_reset_cpu(2);
2424881658aSAlex Bennée             imx6_defer_clear_reset_bit(2, s, CORE2_RST_SHIFT);
24319830574SJean-Christophe DUBOIS         }
24419830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, CORE3_RST)) {
24519830574SJean-Christophe DUBOIS             arm_reset_cpu(3);
2464881658aSAlex Bennée             imx6_defer_clear_reset_bit(3, s, CORE3_RST_SHIFT);
24719830574SJean-Christophe DUBOIS         }
24819830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, SW_IPU2_RST)) {
24919830574SJean-Christophe DUBOIS             /* We pretend the IPU2 is reset */
25019830574SJean-Christophe DUBOIS             clear_bit(SW_IPU2_RST_SHIFT, &current_value);
25119830574SJean-Christophe DUBOIS         }
25219830574SJean-Christophe DUBOIS         if (EXTRACT(change_mask, SW_IPU1_RST)) {
25319830574SJean-Christophe DUBOIS             /* We pretend the IPU1 is reset */
25419830574SJean-Christophe DUBOIS             clear_bit(SW_IPU1_RST_SHIFT, &current_value);
25519830574SJean-Christophe DUBOIS         }
25619830574SJean-Christophe DUBOIS         s->regs[index] = current_value;
25719830574SJean-Christophe DUBOIS         break;
25819830574SJean-Christophe DUBOIS     default:
25919830574SJean-Christophe DUBOIS         s->regs[index] = current_value;
26019830574SJean-Christophe DUBOIS         break;
26119830574SJean-Christophe DUBOIS     }
26219830574SJean-Christophe DUBOIS }
26319830574SJean-Christophe DUBOIS 
26419830574SJean-Christophe DUBOIS static const struct MemoryRegionOps imx6_src_ops = {
26519830574SJean-Christophe DUBOIS     .read = imx6_src_read,
26619830574SJean-Christophe DUBOIS     .write = imx6_src_write,
26719830574SJean-Christophe DUBOIS     .endianness = DEVICE_NATIVE_ENDIAN,
26819830574SJean-Christophe DUBOIS     .valid = {
26919830574SJean-Christophe DUBOIS         /*
27019830574SJean-Christophe DUBOIS          * Our device would not work correctly if the guest was doing
27119830574SJean-Christophe DUBOIS          * unaligned access. This might not be a limitation on the real
27219830574SJean-Christophe DUBOIS          * device but in practice there is no reason for a guest to access
27319830574SJean-Christophe DUBOIS          * this device unaligned.
27419830574SJean-Christophe DUBOIS          */
27519830574SJean-Christophe DUBOIS         .min_access_size = 4,
27619830574SJean-Christophe DUBOIS         .max_access_size = 4,
27719830574SJean-Christophe DUBOIS         .unaligned = false,
27819830574SJean-Christophe DUBOIS     },
27919830574SJean-Christophe DUBOIS };
28019830574SJean-Christophe DUBOIS 
28119830574SJean-Christophe DUBOIS static void imx6_src_realize(DeviceState *dev, Error **errp)
28219830574SJean-Christophe DUBOIS {
28319830574SJean-Christophe DUBOIS     IMX6SRCState *s = IMX6_SRC(dev);
28419830574SJean-Christophe DUBOIS 
28519830574SJean-Christophe DUBOIS     memory_region_init_io(&s->iomem, OBJECT(dev), &imx6_src_ops, s,
28619830574SJean-Christophe DUBOIS                           TYPE_IMX6_SRC, 0x1000);
28719830574SJean-Christophe DUBOIS     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
28819830574SJean-Christophe DUBOIS }
28919830574SJean-Christophe DUBOIS 
29019830574SJean-Christophe DUBOIS static void imx6_src_class_init(ObjectClass *klass, void *data)
29119830574SJean-Christophe DUBOIS {
29219830574SJean-Christophe DUBOIS     DeviceClass *dc = DEVICE_CLASS(klass);
29319830574SJean-Christophe DUBOIS 
29419830574SJean-Christophe DUBOIS     dc->realize = imx6_src_realize;
29519830574SJean-Christophe DUBOIS     dc->reset = imx6_src_reset;
29619830574SJean-Christophe DUBOIS     dc->vmsd = &vmstate_imx6_src;
29719830574SJean-Christophe DUBOIS     dc->desc = "i.MX6 System Reset Controller";
29819830574SJean-Christophe DUBOIS }
29919830574SJean-Christophe DUBOIS 
30019830574SJean-Christophe DUBOIS static const TypeInfo imx6_src_info = {
30119830574SJean-Christophe DUBOIS     .name          = TYPE_IMX6_SRC,
30219830574SJean-Christophe DUBOIS     .parent        = TYPE_SYS_BUS_DEVICE,
30319830574SJean-Christophe DUBOIS     .instance_size = sizeof(IMX6SRCState),
30419830574SJean-Christophe DUBOIS     .class_init    = imx6_src_class_init,
30519830574SJean-Christophe DUBOIS };
30619830574SJean-Christophe DUBOIS 
30719830574SJean-Christophe DUBOIS static void imx6_src_register_types(void)
30819830574SJean-Christophe DUBOIS {
30919830574SJean-Christophe DUBOIS     type_register_static(&imx6_src_info);
31019830574SJean-Christophe DUBOIS }
31119830574SJean-Christophe DUBOIS 
31219830574SJean-Christophe DUBOIS type_init(imx6_src_register_types)
313