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" 15*03dd024fSPaolo Bonzini #include "qemu/log.h" 1619830574SJean-Christophe DUBOIS #include "arm-powerctl.h" 1719830574SJean-Christophe DUBOIS 1819830574SJean-Christophe DUBOIS #ifndef DEBUG_IMX6_SRC 1919830574SJean-Christophe DUBOIS #define DEBUG_IMX6_SRC 0 2019830574SJean-Christophe DUBOIS #endif 2119830574SJean-Christophe DUBOIS 2219830574SJean-Christophe DUBOIS #define DPRINTF(fmt, args...) \ 2319830574SJean-Christophe DUBOIS do { \ 2419830574SJean-Christophe DUBOIS if (DEBUG_IMX6_SRC) { \ 2519830574SJean-Christophe DUBOIS fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX6_SRC, \ 2619830574SJean-Christophe DUBOIS __func__, ##args); \ 2719830574SJean-Christophe DUBOIS } \ 2819830574SJean-Christophe DUBOIS } while (0) 2919830574SJean-Christophe DUBOIS 3019830574SJean-Christophe DUBOIS static char const *imx6_src_reg_name(uint32_t reg) 3119830574SJean-Christophe DUBOIS { 3219830574SJean-Christophe DUBOIS static char unknown[20]; 3319830574SJean-Christophe DUBOIS 3419830574SJean-Christophe DUBOIS switch (reg) { 3519830574SJean-Christophe DUBOIS case SRC_SCR: 3619830574SJean-Christophe DUBOIS return "SRC_SCR"; 3719830574SJean-Christophe DUBOIS case SRC_SBMR1: 3819830574SJean-Christophe DUBOIS return "SRC_SBMR1"; 3919830574SJean-Christophe DUBOIS case SRC_SRSR: 4019830574SJean-Christophe DUBOIS return "SRC_SRSR"; 4119830574SJean-Christophe DUBOIS case SRC_SISR: 4219830574SJean-Christophe DUBOIS return "SRC_SISR"; 4319830574SJean-Christophe DUBOIS case SRC_SIMR: 4419830574SJean-Christophe DUBOIS return "SRC_SIMR"; 4519830574SJean-Christophe DUBOIS case SRC_SBMR2: 4619830574SJean-Christophe DUBOIS return "SRC_SBMR2"; 4719830574SJean-Christophe DUBOIS case SRC_GPR1: 4819830574SJean-Christophe DUBOIS return "SRC_GPR1"; 4919830574SJean-Christophe DUBOIS case SRC_GPR2: 5019830574SJean-Christophe DUBOIS return "SRC_GPR2"; 5119830574SJean-Christophe DUBOIS case SRC_GPR3: 5219830574SJean-Christophe DUBOIS return "SRC_GPR3"; 5319830574SJean-Christophe DUBOIS case SRC_GPR4: 5419830574SJean-Christophe DUBOIS return "SRC_GPR4"; 5519830574SJean-Christophe DUBOIS case SRC_GPR5: 5619830574SJean-Christophe DUBOIS return "SRC_GPR5"; 5719830574SJean-Christophe DUBOIS case SRC_GPR6: 5819830574SJean-Christophe DUBOIS return "SRC_GPR6"; 5919830574SJean-Christophe DUBOIS case SRC_GPR7: 6019830574SJean-Christophe DUBOIS return "SRC_GPR7"; 6119830574SJean-Christophe DUBOIS case SRC_GPR8: 6219830574SJean-Christophe DUBOIS return "SRC_GPR8"; 6319830574SJean-Christophe DUBOIS case SRC_GPR9: 6419830574SJean-Christophe DUBOIS return "SRC_GPR9"; 6519830574SJean-Christophe DUBOIS case SRC_GPR10: 6619830574SJean-Christophe DUBOIS return "SRC_GPR10"; 6719830574SJean-Christophe DUBOIS default: 6819830574SJean-Christophe DUBOIS sprintf(unknown, "%d ?", reg); 6919830574SJean-Christophe DUBOIS return unknown; 7019830574SJean-Christophe DUBOIS } 7119830574SJean-Christophe DUBOIS } 7219830574SJean-Christophe DUBOIS 7319830574SJean-Christophe DUBOIS static const VMStateDescription vmstate_imx6_src = { 7419830574SJean-Christophe DUBOIS .name = TYPE_IMX6_SRC, 7519830574SJean-Christophe DUBOIS .version_id = 1, 7619830574SJean-Christophe DUBOIS .minimum_version_id = 1, 7719830574SJean-Christophe DUBOIS .fields = (VMStateField[]) { 7819830574SJean-Christophe DUBOIS VMSTATE_UINT32_ARRAY(regs, IMX6SRCState, SRC_MAX), 7919830574SJean-Christophe DUBOIS VMSTATE_END_OF_LIST() 8019830574SJean-Christophe DUBOIS }, 8119830574SJean-Christophe DUBOIS }; 8219830574SJean-Christophe DUBOIS 8319830574SJean-Christophe DUBOIS static void imx6_src_reset(DeviceState *dev) 8419830574SJean-Christophe DUBOIS { 8519830574SJean-Christophe DUBOIS IMX6SRCState *s = IMX6_SRC(dev); 8619830574SJean-Christophe DUBOIS 8719830574SJean-Christophe DUBOIS DPRINTF("\n"); 8819830574SJean-Christophe DUBOIS 8919830574SJean-Christophe DUBOIS memset(s->regs, 0, sizeof(s->regs)); 9019830574SJean-Christophe DUBOIS 9119830574SJean-Christophe DUBOIS /* Set reset values */ 9219830574SJean-Christophe DUBOIS s->regs[SRC_SCR] = 0x521; 9319830574SJean-Christophe DUBOIS s->regs[SRC_SRSR] = 0x1; 9419830574SJean-Christophe DUBOIS s->regs[SRC_SIMR] = 0x1F; 9519830574SJean-Christophe DUBOIS } 9619830574SJean-Christophe DUBOIS 9719830574SJean-Christophe DUBOIS static uint64_t imx6_src_read(void *opaque, hwaddr offset, unsigned size) 9819830574SJean-Christophe DUBOIS { 9919830574SJean-Christophe DUBOIS uint32_t value = 0; 10019830574SJean-Christophe DUBOIS IMX6SRCState *s = (IMX6SRCState *)opaque; 10119830574SJean-Christophe DUBOIS uint32_t index = offset >> 2; 10219830574SJean-Christophe DUBOIS 10319830574SJean-Christophe DUBOIS if (index < SRC_MAX) { 10419830574SJean-Christophe DUBOIS value = s->regs[index]; 10519830574SJean-Christophe DUBOIS } else { 10619830574SJean-Christophe DUBOIS qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 10719830574SJean-Christophe DUBOIS HWADDR_PRIx "\n", TYPE_IMX6_SRC, __func__, offset); 10819830574SJean-Christophe DUBOIS 10919830574SJean-Christophe DUBOIS } 11019830574SJean-Christophe DUBOIS 11119830574SJean-Christophe DUBOIS DPRINTF("reg[%s] => 0x%" PRIx32 "\n", imx6_src_reg_name(index), value); 11219830574SJean-Christophe DUBOIS 11319830574SJean-Christophe DUBOIS return value; 11419830574SJean-Christophe DUBOIS } 11519830574SJean-Christophe DUBOIS 11619830574SJean-Christophe DUBOIS static void imx6_src_write(void *opaque, hwaddr offset, uint64_t value, 11719830574SJean-Christophe DUBOIS unsigned size) 11819830574SJean-Christophe DUBOIS { 11919830574SJean-Christophe DUBOIS IMX6SRCState *s = (IMX6SRCState *)opaque; 12019830574SJean-Christophe DUBOIS uint32_t index = offset >> 2; 12119830574SJean-Christophe DUBOIS unsigned long change_mask; 12219830574SJean-Christophe DUBOIS unsigned long current_value = value; 12319830574SJean-Christophe DUBOIS 12419830574SJean-Christophe DUBOIS if (index >= SRC_MAX) { 12519830574SJean-Christophe DUBOIS qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 12619830574SJean-Christophe DUBOIS HWADDR_PRIx "\n", TYPE_IMX6_SRC, __func__, offset); 12719830574SJean-Christophe DUBOIS return; 12819830574SJean-Christophe DUBOIS } 12919830574SJean-Christophe DUBOIS 13019830574SJean-Christophe DUBOIS DPRINTF("reg[%s] <= 0x%" PRIx32 "\n", imx6_src_reg_name(index), 13119830574SJean-Christophe DUBOIS (uint32_t)current_value); 13219830574SJean-Christophe DUBOIS 13319830574SJean-Christophe DUBOIS change_mask = s->regs[index] ^ (uint32_t)current_value; 13419830574SJean-Christophe DUBOIS 13519830574SJean-Christophe DUBOIS switch (index) { 13619830574SJean-Christophe DUBOIS case SRC_SCR: 13719830574SJean-Christophe DUBOIS /* 13819830574SJean-Christophe DUBOIS * On real hardware when the system reset controller starts a 13919830574SJean-Christophe DUBOIS * secondary CPU it runs through some boot ROM code which reads 14019830574SJean-Christophe DUBOIS * the SRC_GPRX registers controlling the start address and branches 14119830574SJean-Christophe DUBOIS * to it. 14219830574SJean-Christophe DUBOIS * Here we are taking a short cut and branching directly to the 14319830574SJean-Christophe DUBOIS * requested address (we don't want to run the boot ROM code inside 14419830574SJean-Christophe DUBOIS * QEMU) 14519830574SJean-Christophe DUBOIS */ 14619830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE3_ENABLE)) { 14719830574SJean-Christophe DUBOIS if (EXTRACT(current_value, CORE3_ENABLE)) { 14819830574SJean-Christophe DUBOIS /* CORE 3 is brought up */ 14919830574SJean-Christophe DUBOIS arm_set_cpu_on(3, s->regs[SRC_GPR7], s->regs[SRC_GPR8], 15019830574SJean-Christophe DUBOIS 3, false); 15119830574SJean-Christophe DUBOIS } else { 15219830574SJean-Christophe DUBOIS /* CORE 3 is shut down */ 15319830574SJean-Christophe DUBOIS arm_set_cpu_off(3); 15419830574SJean-Christophe DUBOIS } 15519830574SJean-Christophe DUBOIS /* We clear the reset bits as the processor changed state */ 15619830574SJean-Christophe DUBOIS clear_bit(CORE3_RST_SHIFT, ¤t_value); 15719830574SJean-Christophe DUBOIS clear_bit(CORE3_RST_SHIFT, &change_mask); 15819830574SJean-Christophe DUBOIS } 15919830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE2_ENABLE)) { 16019830574SJean-Christophe DUBOIS if (EXTRACT(current_value, CORE2_ENABLE)) { 16119830574SJean-Christophe DUBOIS /* CORE 2 is brought up */ 16219830574SJean-Christophe DUBOIS arm_set_cpu_on(2, s->regs[SRC_GPR5], s->regs[SRC_GPR6], 16319830574SJean-Christophe DUBOIS 3, false); 16419830574SJean-Christophe DUBOIS } else { 16519830574SJean-Christophe DUBOIS /* CORE 3 is shut down */ 16619830574SJean-Christophe DUBOIS arm_set_cpu_off(2); 16719830574SJean-Christophe DUBOIS } 16819830574SJean-Christophe DUBOIS /* We clear the reset bits as the processor changed state */ 16919830574SJean-Christophe DUBOIS clear_bit(CORE2_RST_SHIFT, ¤t_value); 17019830574SJean-Christophe DUBOIS clear_bit(CORE2_RST_SHIFT, &change_mask); 17119830574SJean-Christophe DUBOIS } 17219830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE1_ENABLE)) { 17319830574SJean-Christophe DUBOIS if (EXTRACT(current_value, CORE1_ENABLE)) { 17419830574SJean-Christophe DUBOIS /* CORE 1 is brought up */ 17519830574SJean-Christophe DUBOIS arm_set_cpu_on(1, s->regs[SRC_GPR3], s->regs[SRC_GPR4], 17619830574SJean-Christophe DUBOIS 3, false); 17719830574SJean-Christophe DUBOIS } else { 17819830574SJean-Christophe DUBOIS /* CORE 3 is shut down */ 17919830574SJean-Christophe DUBOIS arm_set_cpu_off(1); 18019830574SJean-Christophe DUBOIS } 18119830574SJean-Christophe DUBOIS /* We clear the reset bits as the processor changed state */ 18219830574SJean-Christophe DUBOIS clear_bit(CORE1_RST_SHIFT, ¤t_value); 18319830574SJean-Christophe DUBOIS clear_bit(CORE1_RST_SHIFT, &change_mask); 18419830574SJean-Christophe DUBOIS } 18519830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE0_RST)) { 18619830574SJean-Christophe DUBOIS arm_reset_cpu(0); 18719830574SJean-Christophe DUBOIS clear_bit(CORE0_RST_SHIFT, ¤t_value); 18819830574SJean-Christophe DUBOIS } 18919830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE1_RST)) { 19019830574SJean-Christophe DUBOIS arm_reset_cpu(1); 19119830574SJean-Christophe DUBOIS clear_bit(CORE1_RST_SHIFT, ¤t_value); 19219830574SJean-Christophe DUBOIS } 19319830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE2_RST)) { 19419830574SJean-Christophe DUBOIS arm_reset_cpu(2); 19519830574SJean-Christophe DUBOIS clear_bit(CORE2_RST_SHIFT, ¤t_value); 19619830574SJean-Christophe DUBOIS } 19719830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, CORE3_RST)) { 19819830574SJean-Christophe DUBOIS arm_reset_cpu(3); 19919830574SJean-Christophe DUBOIS clear_bit(CORE3_RST_SHIFT, ¤t_value); 20019830574SJean-Christophe DUBOIS } 20119830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, SW_IPU2_RST)) { 20219830574SJean-Christophe DUBOIS /* We pretend the IPU2 is reset */ 20319830574SJean-Christophe DUBOIS clear_bit(SW_IPU2_RST_SHIFT, ¤t_value); 20419830574SJean-Christophe DUBOIS } 20519830574SJean-Christophe DUBOIS if (EXTRACT(change_mask, SW_IPU1_RST)) { 20619830574SJean-Christophe DUBOIS /* We pretend the IPU1 is reset */ 20719830574SJean-Christophe DUBOIS clear_bit(SW_IPU1_RST_SHIFT, ¤t_value); 20819830574SJean-Christophe DUBOIS } 20919830574SJean-Christophe DUBOIS s->regs[index] = current_value; 21019830574SJean-Christophe DUBOIS break; 21119830574SJean-Christophe DUBOIS default: 21219830574SJean-Christophe DUBOIS s->regs[index] = current_value; 21319830574SJean-Christophe DUBOIS break; 21419830574SJean-Christophe DUBOIS } 21519830574SJean-Christophe DUBOIS } 21619830574SJean-Christophe DUBOIS 21719830574SJean-Christophe DUBOIS static const struct MemoryRegionOps imx6_src_ops = { 21819830574SJean-Christophe DUBOIS .read = imx6_src_read, 21919830574SJean-Christophe DUBOIS .write = imx6_src_write, 22019830574SJean-Christophe DUBOIS .endianness = DEVICE_NATIVE_ENDIAN, 22119830574SJean-Christophe DUBOIS .valid = { 22219830574SJean-Christophe DUBOIS /* 22319830574SJean-Christophe DUBOIS * Our device would not work correctly if the guest was doing 22419830574SJean-Christophe DUBOIS * unaligned access. This might not be a limitation on the real 22519830574SJean-Christophe DUBOIS * device but in practice there is no reason for a guest to access 22619830574SJean-Christophe DUBOIS * this device unaligned. 22719830574SJean-Christophe DUBOIS */ 22819830574SJean-Christophe DUBOIS .min_access_size = 4, 22919830574SJean-Christophe DUBOIS .max_access_size = 4, 23019830574SJean-Christophe DUBOIS .unaligned = false, 23119830574SJean-Christophe DUBOIS }, 23219830574SJean-Christophe DUBOIS }; 23319830574SJean-Christophe DUBOIS 23419830574SJean-Christophe DUBOIS static void imx6_src_realize(DeviceState *dev, Error **errp) 23519830574SJean-Christophe DUBOIS { 23619830574SJean-Christophe DUBOIS IMX6SRCState *s = IMX6_SRC(dev); 23719830574SJean-Christophe DUBOIS 23819830574SJean-Christophe DUBOIS memory_region_init_io(&s->iomem, OBJECT(dev), &imx6_src_ops, s, 23919830574SJean-Christophe DUBOIS TYPE_IMX6_SRC, 0x1000); 24019830574SJean-Christophe DUBOIS sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem); 24119830574SJean-Christophe DUBOIS } 24219830574SJean-Christophe DUBOIS 24319830574SJean-Christophe DUBOIS static void imx6_src_class_init(ObjectClass *klass, void *data) 24419830574SJean-Christophe DUBOIS { 24519830574SJean-Christophe DUBOIS DeviceClass *dc = DEVICE_CLASS(klass); 24619830574SJean-Christophe DUBOIS 24719830574SJean-Christophe DUBOIS dc->realize = imx6_src_realize; 24819830574SJean-Christophe DUBOIS dc->reset = imx6_src_reset; 24919830574SJean-Christophe DUBOIS dc->vmsd = &vmstate_imx6_src; 25019830574SJean-Christophe DUBOIS dc->desc = "i.MX6 System Reset Controller"; 25119830574SJean-Christophe DUBOIS } 25219830574SJean-Christophe DUBOIS 25319830574SJean-Christophe DUBOIS static const TypeInfo imx6_src_info = { 25419830574SJean-Christophe DUBOIS .name = TYPE_IMX6_SRC, 25519830574SJean-Christophe DUBOIS .parent = TYPE_SYS_BUS_DEVICE, 25619830574SJean-Christophe DUBOIS .instance_size = sizeof(IMX6SRCState), 25719830574SJean-Christophe DUBOIS .class_init = imx6_src_class_init, 25819830574SJean-Christophe DUBOIS }; 25919830574SJean-Christophe DUBOIS 26019830574SJean-Christophe DUBOIS static void imx6_src_register_types(void) 26119830574SJean-Christophe DUBOIS { 26219830574SJean-Christophe DUBOIS type_register_static(&imx6_src_info); 26319830574SJean-Christophe DUBOIS } 26419830574SJean-Christophe DUBOIS 26519830574SJean-Christophe DUBOIS type_init(imx6_src_register_types) 266