120936684SInès Varhol /* 220936684SInès Varhol * STM32L4x5 SYSCFG (System Configuration Controller) 320936684SInès Varhol * 420936684SInès Varhol * Copyright (c) 2023 Arnaud Minier <arnaud.minier@telecom-paris.fr> 520936684SInès Varhol * Copyright (c) 2023 Inès Varhol <ines.varhol@telecom-paris.fr> 620936684SInès Varhol * 720936684SInès Varhol * SPDX-License-Identifier: GPL-2.0-or-later 820936684SInès Varhol * 920936684SInès Varhol * This work is licensed under the terms of the GNU GPL, version 2 or later. 1020936684SInès Varhol * See the COPYING file in the top-level directory. 1120936684SInès Varhol * 1220936684SInès Varhol * This work is based on the stm32f4xx_syscfg by Alistair Francis. 1320936684SInès Varhol * Original code is licensed under the MIT License: 1420936684SInès Varhol * 1520936684SInès Varhol * Copyright (c) 2014 Alistair Francis <alistair@alistair23.me> 1620936684SInès Varhol */ 1720936684SInès Varhol 1820936684SInès Varhol /* 1920936684SInès Varhol * The reference used is the STMicroElectronics RM0351 Reference manual 2020936684SInès Varhol * for STM32L4x5 and STM32L4x6 advanced Arm ® -based 32-bit MCUs. 2120936684SInès Varhol * https://www.st.com/en/microcontrollers-microprocessors/stm32l4x5/documentation.html 2220936684SInès Varhol */ 2320936684SInès Varhol 2420936684SInès Varhol #include "qemu/osdep.h" 2520936684SInès Varhol #include "qemu/log.h" 2620936684SInès Varhol #include "trace.h" 2720936684SInès Varhol #include "hw/irq.h" 2820936684SInès Varhol #include "migration/vmstate.h" 2920936684SInès Varhol #include "hw/misc/stm32l4x5_syscfg.h" 301c38129dSInès Varhol #include "hw/gpio/stm32l4x5_gpio.h" 3120936684SInès Varhol 3220936684SInès Varhol #define SYSCFG_MEMRMP 0x00 3320936684SInès Varhol #define SYSCFG_CFGR1 0x04 3420936684SInès Varhol #define SYSCFG_EXTICR1 0x08 3520936684SInès Varhol #define SYSCFG_EXTICR2 0x0C 3620936684SInès Varhol #define SYSCFG_EXTICR3 0x10 3720936684SInès Varhol #define SYSCFG_EXTICR4 0x14 3820936684SInès Varhol #define SYSCFG_SCSR 0x18 3920936684SInès Varhol #define SYSCFG_CFGR2 0x1C 4020936684SInès Varhol #define SYSCFG_SWPR 0x20 4120936684SInès Varhol #define SYSCFG_SKR 0x24 4220936684SInès Varhol #define SYSCFG_SWPR2 0x28 4320936684SInès Varhol 4420936684SInès Varhol /* 00000000_00000000_00000001_00000111 */ 4520936684SInès Varhol #define ACTIVABLE_BITS_MEMRP 0x00000107 4620936684SInès Varhol 4720936684SInès Varhol /* 11111100_11111111_00000001_00000000 */ 4820936684SInès Varhol #define ACTIVABLE_BITS_CFGR1 0xFCFF0100 4920936684SInès Varhol /* 00000000_00000000_00000000_00000001 */ 5020936684SInès Varhol #define FIREWALL_DISABLE_CFGR1 0x00000001 5120936684SInès Varhol 5220936684SInès Varhol /* 00000000_00000000_11111111_11111111 */ 5320936684SInès Varhol #define ACTIVABLE_BITS_EXTICR 0x0000FFFF 5420936684SInès Varhol 5520936684SInès Varhol /* 00000000_00000000_00000000_00000011 */ 5620936684SInès Varhol /* #define ACTIVABLE_BITS_SCSR 0x00000003 */ 5720936684SInès Varhol 5820936684SInès Varhol /* 00000000_00000000_00000000_00001111 */ 5920936684SInès Varhol #define ECC_LOCK_CFGR2 0x0000000F 6020936684SInès Varhol /* 00000000_00000000_00000001_00000000 */ 6120936684SInès Varhol #define SRAM2_PARITY_ERROR_FLAG_CFGR2 0x00000100 6220936684SInès Varhol 6320936684SInès Varhol /* 00000000_00000000_00000000_11111111 */ 6420936684SInès Varhol #define ACTIVABLE_BITS_SKR 0x000000FF 6520936684SInès Varhol 6620936684SInès Varhol #define NUM_LINES_PER_EXTICR_REG 4 6720936684SInès Varhol 68*ad80e367SPeter Maydell static void stm32l4x5_syscfg_hold_reset(Object *obj, ResetType type) 6920936684SInès Varhol { 7020936684SInès Varhol Stm32l4x5SyscfgState *s = STM32L4X5_SYSCFG(obj); 7120936684SInès Varhol 7220936684SInès Varhol s->memrmp = 0x00000000; 7320936684SInès Varhol s->cfgr1 = 0x7C000001; 7420936684SInès Varhol s->exticr[0] = 0x00000000; 7520936684SInès Varhol s->exticr[1] = 0x00000000; 7620936684SInès Varhol s->exticr[2] = 0x00000000; 7720936684SInès Varhol s->exticr[3] = 0x00000000; 7820936684SInès Varhol s->scsr = 0x00000000; 7920936684SInès Varhol s->cfgr2 = 0x00000000; 8020936684SInès Varhol s->swpr = 0x00000000; 8120936684SInès Varhol s->skr = 0x00000000; 8220936684SInès Varhol s->swpr2 = 0x00000000; 8320936684SInès Varhol } 8420936684SInès Varhol 8520936684SInès Varhol static void stm32l4x5_syscfg_set_irq(void *opaque, int irq, int level) 8620936684SInès Varhol { 8720936684SInès Varhol Stm32l4x5SyscfgState *s = opaque; 8820936684SInès Varhol const uint8_t gpio = irq / GPIO_NUM_PINS; 8920936684SInès Varhol const int line = irq % GPIO_NUM_PINS; 9020936684SInès Varhol 9120936684SInès Varhol const int exticr_reg = line / NUM_LINES_PER_EXTICR_REG; 9220936684SInès Varhol const int startbit = (line % NUM_LINES_PER_EXTICR_REG) * 4; 9320936684SInès Varhol 9420936684SInès Varhol g_assert(gpio < NUM_GPIOS); 9520936684SInès Varhol trace_stm32l4x5_syscfg_set_irq(gpio, line, level); 9620936684SInès Varhol 9720936684SInès Varhol if (extract32(s->exticr[exticr_reg], startbit, 4) == gpio) { 9820936684SInès Varhol trace_stm32l4x5_syscfg_forward_exti(line); 9920936684SInès Varhol qemu_set_irq(s->gpio_out[line], level); 10020936684SInès Varhol } 10120936684SInès Varhol } 10220936684SInès Varhol 10320936684SInès Varhol static uint64_t stm32l4x5_syscfg_read(void *opaque, hwaddr addr, 10420936684SInès Varhol unsigned int size) 10520936684SInès Varhol { 10620936684SInès Varhol Stm32l4x5SyscfgState *s = opaque; 10720936684SInès Varhol 10820936684SInès Varhol trace_stm32l4x5_syscfg_read(addr); 10920936684SInès Varhol 11020936684SInès Varhol switch (addr) { 11120936684SInès Varhol case SYSCFG_MEMRMP: 11220936684SInès Varhol return s->memrmp; 11320936684SInès Varhol case SYSCFG_CFGR1: 11420936684SInès Varhol return s->cfgr1; 11520936684SInès Varhol case SYSCFG_EXTICR1...SYSCFG_EXTICR4: 11620936684SInès Varhol return s->exticr[(addr - SYSCFG_EXTICR1) / 4]; 11720936684SInès Varhol case SYSCFG_SCSR: 11820936684SInès Varhol return s->scsr; 11920936684SInès Varhol case SYSCFG_CFGR2: 12020936684SInès Varhol return s->cfgr2; 12120936684SInès Varhol case SYSCFG_SWPR: 12220936684SInès Varhol return s->swpr; 12320936684SInès Varhol case SYSCFG_SKR: 12420936684SInès Varhol return s->skr; 12520936684SInès Varhol case SYSCFG_SWPR2: 12620936684SInès Varhol return s->swpr2; 12720936684SInès Varhol default: 12820936684SInès Varhol qemu_log_mask(LOG_GUEST_ERROR, 12920936684SInès Varhol "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr); 13020936684SInès Varhol return 0; 13120936684SInès Varhol } 13220936684SInès Varhol } 13320936684SInès Varhol static void stm32l4x5_syscfg_write(void *opaque, hwaddr addr, 13420936684SInès Varhol uint64_t value, unsigned int size) 13520936684SInès Varhol { 13620936684SInès Varhol Stm32l4x5SyscfgState *s = opaque; 13720936684SInès Varhol 13820936684SInès Varhol trace_stm32l4x5_syscfg_write(addr, value); 13920936684SInès Varhol 14020936684SInès Varhol switch (addr) { 14120936684SInès Varhol case SYSCFG_MEMRMP: 14220936684SInès Varhol qemu_log_mask(LOG_UNIMP, 14320936684SInès Varhol "%s: Changing the memory mapping isn't supported\n", 14420936684SInès Varhol __func__); 14520936684SInès Varhol s->memrmp = value & ACTIVABLE_BITS_MEMRP; 14620936684SInès Varhol return; 14720936684SInès Varhol case SYSCFG_CFGR1: 14820936684SInès Varhol qemu_log_mask(LOG_UNIMP, 14920936684SInès Varhol "%s: Functions in CFGRx aren't supported\n", 15020936684SInès Varhol __func__); 15120936684SInès Varhol /* bit 0 (firewall dis.) is cleared by software, set only by reset. */ 15220936684SInès Varhol s->cfgr1 = (s->cfgr1 & value & FIREWALL_DISABLE_CFGR1) | 15320936684SInès Varhol (value & ACTIVABLE_BITS_CFGR1); 15420936684SInès Varhol return; 15520936684SInès Varhol case SYSCFG_EXTICR1...SYSCFG_EXTICR4: 15620936684SInès Varhol s->exticr[(addr - SYSCFG_EXTICR1) / 4] = 15720936684SInès Varhol (value & ACTIVABLE_BITS_EXTICR); 15820936684SInès Varhol return; 15920936684SInès Varhol case SYSCFG_SCSR: 16020936684SInès Varhol qemu_log_mask(LOG_UNIMP, 16120936684SInès Varhol "%s: Erasing SRAM2 isn't supported\n", 16220936684SInès Varhol __func__); 16320936684SInès Varhol /* 16420936684SInès Varhol * only non reserved bits are : 16520936684SInès Varhol * bit 0 (write-protected by a passkey), bit 1 (meant to be read) 16620936684SInès Varhol * so it serves no purpose yet to add : 16720936684SInès Varhol * s->scsr = value & 0x3; 16820936684SInès Varhol */ 16920936684SInès Varhol return; 17020936684SInès Varhol case SYSCFG_CFGR2: 17120936684SInès Varhol qemu_log_mask(LOG_UNIMP, 17220936684SInès Varhol "%s: Functions in CFGRx aren't supported\n", 17320936684SInès Varhol __func__); 17420936684SInès Varhol /* bit 8 (SRAM2 PEF) is cleared by software by writing a '1'.*/ 17520936684SInès Varhol /* bits[3:0] (ECC Lock) are set by software, cleared only by reset.*/ 17620936684SInès Varhol s->cfgr2 = (s->cfgr2 | (value & ECC_LOCK_CFGR2)) & 17720936684SInès Varhol ~(value & SRAM2_PARITY_ERROR_FLAG_CFGR2); 17820936684SInès Varhol return; 17920936684SInès Varhol case SYSCFG_SWPR: 18020936684SInès Varhol qemu_log_mask(LOG_UNIMP, 18120936684SInès Varhol "%s: Write protecting SRAM2 isn't supported\n", 18220936684SInès Varhol __func__); 18320936684SInès Varhol /* These bits are set by software and cleared only by reset.*/ 18420936684SInès Varhol s->swpr |= value; 18520936684SInès Varhol return; 18620936684SInès Varhol case SYSCFG_SKR: 18720936684SInès Varhol qemu_log_mask(LOG_UNIMP, 18820936684SInès Varhol "%s: Erasing SRAM2 isn't supported\n", 18920936684SInès Varhol __func__); 19020936684SInès Varhol s->skr = value & ACTIVABLE_BITS_SKR; 19120936684SInès Varhol return; 19220936684SInès Varhol case SYSCFG_SWPR2: 19320936684SInès Varhol qemu_log_mask(LOG_UNIMP, 19420936684SInès Varhol "%s: Write protecting SRAM2 isn't supported\n", 19520936684SInès Varhol __func__); 19620936684SInès Varhol /* These bits are set by software and cleared only by reset.*/ 19720936684SInès Varhol s->swpr2 |= value; 19820936684SInès Varhol return; 19920936684SInès Varhol default: 20020936684SInès Varhol qemu_log_mask(LOG_GUEST_ERROR, 20120936684SInès Varhol "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr); 20220936684SInès Varhol } 20320936684SInès Varhol } 20420936684SInès Varhol 20520936684SInès Varhol static const MemoryRegionOps stm32l4x5_syscfg_ops = { 20620936684SInès Varhol .read = stm32l4x5_syscfg_read, 20720936684SInès Varhol .write = stm32l4x5_syscfg_write, 20820936684SInès Varhol .endianness = DEVICE_NATIVE_ENDIAN, 20920936684SInès Varhol .impl.min_access_size = 4, 21020936684SInès Varhol .impl.max_access_size = 4, 21120936684SInès Varhol .impl.unaligned = false, 21220936684SInès Varhol .valid.min_access_size = 4, 21320936684SInès Varhol .valid.max_access_size = 4, 21420936684SInès Varhol .valid.unaligned = false, 21520936684SInès Varhol }; 21620936684SInès Varhol 21720936684SInès Varhol static void stm32l4x5_syscfg_init(Object *obj) 21820936684SInès Varhol { 21920936684SInès Varhol Stm32l4x5SyscfgState *s = STM32L4X5_SYSCFG(obj); 22020936684SInès Varhol 22120936684SInès Varhol memory_region_init_io(&s->mmio, obj, &stm32l4x5_syscfg_ops, s, 22220936684SInès Varhol TYPE_STM32L4X5_SYSCFG, 0x400); 22320936684SInès Varhol sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio); 22420936684SInès Varhol 22520936684SInès Varhol qdev_init_gpio_in(DEVICE(obj), stm32l4x5_syscfg_set_irq, 22620936684SInès Varhol GPIO_NUM_PINS * NUM_GPIOS); 22720936684SInès Varhol qdev_init_gpio_out(DEVICE(obj), s->gpio_out, GPIO_NUM_PINS); 22820936684SInès Varhol } 22920936684SInès Varhol 23020936684SInès Varhol static const VMStateDescription vmstate_stm32l4x5_syscfg = { 23120936684SInès Varhol .name = TYPE_STM32L4X5_SYSCFG, 23220936684SInès Varhol .version_id = 1, 23320936684SInès Varhol .minimum_version_id = 1, 23420936684SInès Varhol .fields = (VMStateField[]) { 23520936684SInès Varhol VMSTATE_UINT32(memrmp, Stm32l4x5SyscfgState), 23620936684SInès Varhol VMSTATE_UINT32(cfgr1, Stm32l4x5SyscfgState), 23720936684SInès Varhol VMSTATE_UINT32_ARRAY(exticr, Stm32l4x5SyscfgState, 23820936684SInès Varhol SYSCFG_NUM_EXTICR), 23920936684SInès Varhol VMSTATE_UINT32(scsr, Stm32l4x5SyscfgState), 24020936684SInès Varhol VMSTATE_UINT32(cfgr2, Stm32l4x5SyscfgState), 24120936684SInès Varhol VMSTATE_UINT32(swpr, Stm32l4x5SyscfgState), 24220936684SInès Varhol VMSTATE_UINT32(skr, Stm32l4x5SyscfgState), 24320936684SInès Varhol VMSTATE_UINT32(swpr2, Stm32l4x5SyscfgState), 24420936684SInès Varhol VMSTATE_END_OF_LIST() 24520936684SInès Varhol } 24620936684SInès Varhol }; 24720936684SInès Varhol 24820936684SInès Varhol static void stm32l4x5_syscfg_class_init(ObjectClass *klass, void *data) 24920936684SInès Varhol { 25020936684SInès Varhol DeviceClass *dc = DEVICE_CLASS(klass); 25120936684SInès Varhol ResettableClass *rc = RESETTABLE_CLASS(klass); 25220936684SInès Varhol 25320936684SInès Varhol dc->vmsd = &vmstate_stm32l4x5_syscfg; 25420936684SInès Varhol rc->phases.hold = stm32l4x5_syscfg_hold_reset; 25520936684SInès Varhol } 25620936684SInès Varhol 25720936684SInès Varhol static const TypeInfo stm32l4x5_syscfg_info[] = { 25820936684SInès Varhol { 25920936684SInès Varhol .name = TYPE_STM32L4X5_SYSCFG, 26020936684SInès Varhol .parent = TYPE_SYS_BUS_DEVICE, 26120936684SInès Varhol .instance_size = sizeof(Stm32l4x5SyscfgState), 26220936684SInès Varhol .instance_init = stm32l4x5_syscfg_init, 26320936684SInès Varhol .class_init = stm32l4x5_syscfg_class_init, 26420936684SInès Varhol } 26520936684SInès Varhol }; 26620936684SInès Varhol 26720936684SInès Varhol DEFINE_TYPES(stm32l4x5_syscfg_info) 268