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