xref: /openbmc/qemu/hw/misc/stm32l4x5_syscfg.c (revision f774a677507966222624a9b2859f06ede7608100)
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"
29*b91b8fe7SInès Varhol #include "hw/clock.h"
30*b91b8fe7SInès Varhol #include "hw/qdev-clock.h"
31*b91b8fe7SInès Varhol #include "qapi/error.h"
3220936684SInès Varhol #include "hw/misc/stm32l4x5_syscfg.h"
331c38129dSInès Varhol #include "hw/gpio/stm32l4x5_gpio.h"
3420936684SInès Varhol 
3520936684SInès Varhol #define SYSCFG_MEMRMP 0x00
3620936684SInès Varhol #define SYSCFG_CFGR1 0x04
3720936684SInès Varhol #define SYSCFG_EXTICR1 0x08
3820936684SInès Varhol #define SYSCFG_EXTICR2 0x0C
3920936684SInès Varhol #define SYSCFG_EXTICR3 0x10
4020936684SInès Varhol #define SYSCFG_EXTICR4 0x14
4120936684SInès Varhol #define SYSCFG_SCSR 0x18
4220936684SInès Varhol #define SYSCFG_CFGR2 0x1C
4320936684SInès Varhol #define SYSCFG_SWPR 0x20
4420936684SInès Varhol #define SYSCFG_SKR 0x24
4520936684SInès Varhol #define SYSCFG_SWPR2 0x28
4620936684SInès Varhol 
4720936684SInès Varhol /* 00000000_00000000_00000001_00000111 */
4820936684SInès Varhol #define ACTIVABLE_BITS_MEMRP 0x00000107
4920936684SInès Varhol 
5020936684SInès Varhol /* 11111100_11111111_00000001_00000000 */
5120936684SInès Varhol #define ACTIVABLE_BITS_CFGR1 0xFCFF0100
5220936684SInès Varhol /* 00000000_00000000_00000000_00000001 */
5320936684SInès Varhol #define FIREWALL_DISABLE_CFGR1 0x00000001
5420936684SInès Varhol 
5520936684SInès Varhol /* 00000000_00000000_11111111_11111111 */
5620936684SInès Varhol #define ACTIVABLE_BITS_EXTICR 0x0000FFFF
5720936684SInès Varhol 
5820936684SInès Varhol /* 00000000_00000000_00000000_00000011 */
5920936684SInès Varhol /* #define ACTIVABLE_BITS_SCSR 0x00000003 */
6020936684SInès Varhol 
6120936684SInès Varhol /* 00000000_00000000_00000000_00001111 */
6220936684SInès Varhol #define ECC_LOCK_CFGR2 0x0000000F
6320936684SInès Varhol /* 00000000_00000000_00000001_00000000 */
6420936684SInès Varhol #define SRAM2_PARITY_ERROR_FLAG_CFGR2 0x00000100
6520936684SInès Varhol 
6620936684SInès Varhol /* 00000000_00000000_00000000_11111111 */
6720936684SInès Varhol #define ACTIVABLE_BITS_SKR 0x000000FF
6820936684SInès Varhol 
6920936684SInès Varhol #define NUM_LINES_PER_EXTICR_REG 4
7020936684SInès Varhol 
stm32l4x5_syscfg_hold_reset(Object * obj,ResetType type)71ad80e367SPeter Maydell static void stm32l4x5_syscfg_hold_reset(Object *obj, ResetType type)
7220936684SInès Varhol {
7320936684SInès Varhol     Stm32l4x5SyscfgState *s = STM32L4X5_SYSCFG(obj);
7420936684SInès Varhol 
7520936684SInès Varhol     s->memrmp = 0x00000000;
7620936684SInès Varhol     s->cfgr1 = 0x7C000001;
7720936684SInès Varhol     s->exticr[0] = 0x00000000;
7820936684SInès Varhol     s->exticr[1] = 0x00000000;
7920936684SInès Varhol     s->exticr[2] = 0x00000000;
8020936684SInès Varhol     s->exticr[3] = 0x00000000;
8120936684SInès Varhol     s->scsr = 0x00000000;
8220936684SInès Varhol     s->cfgr2 = 0x00000000;
8320936684SInès Varhol     s->swpr = 0x00000000;
8420936684SInès Varhol     s->skr = 0x00000000;
8520936684SInès Varhol     s->swpr2 = 0x00000000;
8620936684SInès Varhol }
8720936684SInès Varhol 
stm32l4x5_syscfg_set_irq(void * opaque,int irq,int level)8820936684SInès Varhol static void stm32l4x5_syscfg_set_irq(void *opaque, int irq, int level)
8920936684SInès Varhol {
9020936684SInès Varhol     Stm32l4x5SyscfgState *s = opaque;
9120936684SInès Varhol     const uint8_t gpio = irq / GPIO_NUM_PINS;
9220936684SInès Varhol     const int line = irq % GPIO_NUM_PINS;
9320936684SInès Varhol 
9420936684SInès Varhol     const int exticr_reg = line / NUM_LINES_PER_EXTICR_REG;
9520936684SInès Varhol     const int startbit = (line % NUM_LINES_PER_EXTICR_REG) * 4;
9620936684SInès Varhol 
9720936684SInès Varhol     g_assert(gpio < NUM_GPIOS);
9820936684SInès Varhol     trace_stm32l4x5_syscfg_set_irq(gpio, line, level);
9920936684SInès Varhol 
10020936684SInès Varhol     if (extract32(s->exticr[exticr_reg], startbit, 4) == gpio) {
10120936684SInès Varhol         trace_stm32l4x5_syscfg_forward_exti(line);
10220936684SInès Varhol         qemu_set_irq(s->gpio_out[line], level);
10320936684SInès Varhol     }
10420936684SInès Varhol }
10520936684SInès Varhol 
stm32l4x5_syscfg_read(void * opaque,hwaddr addr,unsigned int size)10620936684SInès Varhol static uint64_t stm32l4x5_syscfg_read(void *opaque, hwaddr addr,
10720936684SInès Varhol                                       unsigned int size)
10820936684SInès Varhol {
10920936684SInès Varhol     Stm32l4x5SyscfgState *s = opaque;
11020936684SInès Varhol 
11120936684SInès Varhol     trace_stm32l4x5_syscfg_read(addr);
11220936684SInès Varhol 
11320936684SInès Varhol     switch (addr) {
11420936684SInès Varhol     case SYSCFG_MEMRMP:
11520936684SInès Varhol         return s->memrmp;
11620936684SInès Varhol     case SYSCFG_CFGR1:
11720936684SInès Varhol         return s->cfgr1;
11820936684SInès Varhol     case SYSCFG_EXTICR1...SYSCFG_EXTICR4:
11920936684SInès Varhol         return s->exticr[(addr - SYSCFG_EXTICR1) / 4];
12020936684SInès Varhol     case SYSCFG_SCSR:
12120936684SInès Varhol         return s->scsr;
12220936684SInès Varhol     case SYSCFG_CFGR2:
12320936684SInès Varhol         return s->cfgr2;
12420936684SInès Varhol     case SYSCFG_SWPR:
12520936684SInès Varhol         return s->swpr;
12620936684SInès Varhol     case SYSCFG_SKR:
12720936684SInès Varhol         return s->skr;
12820936684SInès Varhol     case SYSCFG_SWPR2:
12920936684SInès Varhol         return s->swpr2;
13020936684SInès Varhol     default:
13120936684SInès Varhol         qemu_log_mask(LOG_GUEST_ERROR,
13220936684SInès Varhol                       "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr);
13320936684SInès Varhol         return 0;
13420936684SInès Varhol     }
13520936684SInès Varhol }
stm32l4x5_syscfg_write(void * opaque,hwaddr addr,uint64_t value,unsigned int size)13620936684SInès Varhol static void stm32l4x5_syscfg_write(void *opaque, hwaddr addr,
13720936684SInès Varhol                                    uint64_t value, unsigned int size)
13820936684SInès Varhol {
13920936684SInès Varhol     Stm32l4x5SyscfgState *s = opaque;
14020936684SInès Varhol 
14120936684SInès Varhol     trace_stm32l4x5_syscfg_write(addr, value);
14220936684SInès Varhol 
14320936684SInès Varhol     switch (addr) {
14420936684SInès Varhol     case SYSCFG_MEMRMP:
14520936684SInès Varhol         qemu_log_mask(LOG_UNIMP,
14620936684SInès Varhol                       "%s: Changing the memory mapping isn't supported\n",
14720936684SInès Varhol                       __func__);
14820936684SInès Varhol         s->memrmp = value & ACTIVABLE_BITS_MEMRP;
14920936684SInès Varhol         return;
15020936684SInès Varhol     case SYSCFG_CFGR1:
15120936684SInès Varhol         qemu_log_mask(LOG_UNIMP,
15220936684SInès Varhol                       "%s: Functions in CFGRx aren't supported\n",
15320936684SInès Varhol                       __func__);
15420936684SInès Varhol         /* bit 0 (firewall dis.) is cleared by software, set only by reset. */
15520936684SInès Varhol         s->cfgr1 = (s->cfgr1 & value & FIREWALL_DISABLE_CFGR1) |
15620936684SInès Varhol                    (value & ACTIVABLE_BITS_CFGR1);
15720936684SInès Varhol         return;
15820936684SInès Varhol     case SYSCFG_EXTICR1...SYSCFG_EXTICR4:
15920936684SInès Varhol         s->exticr[(addr - SYSCFG_EXTICR1) / 4] =
16020936684SInès Varhol                 (value & ACTIVABLE_BITS_EXTICR);
16120936684SInès Varhol         return;
16220936684SInès Varhol     case SYSCFG_SCSR:
16320936684SInès Varhol         qemu_log_mask(LOG_UNIMP,
16420936684SInès Varhol                       "%s: Erasing SRAM2 isn't supported\n",
16520936684SInès Varhol                       __func__);
16620936684SInès Varhol         /*
16720936684SInès Varhol          * only non reserved bits are :
16820936684SInès Varhol          * bit 0 (write-protected by a passkey), bit 1 (meant to be read)
16920936684SInès Varhol          * so it serves no purpose yet to add :
17020936684SInès Varhol          * s->scsr = value & 0x3;
17120936684SInès Varhol          */
17220936684SInès Varhol         return;
17320936684SInès Varhol     case SYSCFG_CFGR2:
17420936684SInès Varhol         qemu_log_mask(LOG_UNIMP,
17520936684SInès Varhol                       "%s: Functions in CFGRx aren't supported\n",
17620936684SInès Varhol                       __func__);
17720936684SInès Varhol         /* bit 8 (SRAM2 PEF) is cleared by software by writing a '1'.*/
17820936684SInès Varhol         /* bits[3:0] (ECC Lock) are set by software, cleared only by reset.*/
17920936684SInès Varhol         s->cfgr2 = (s->cfgr2 | (value & ECC_LOCK_CFGR2)) &
18020936684SInès Varhol                    ~(value & SRAM2_PARITY_ERROR_FLAG_CFGR2);
18120936684SInès Varhol         return;
18220936684SInès Varhol     case SYSCFG_SWPR:
18320936684SInès Varhol         qemu_log_mask(LOG_UNIMP,
18420936684SInès Varhol                       "%s: Write protecting SRAM2 isn't supported\n",
18520936684SInès Varhol                       __func__);
18620936684SInès Varhol         /* These bits are set by software and cleared only by reset.*/
18720936684SInès Varhol         s->swpr |= value;
18820936684SInès Varhol         return;
18920936684SInès Varhol     case SYSCFG_SKR:
19020936684SInès Varhol         qemu_log_mask(LOG_UNIMP,
19120936684SInès Varhol                       "%s: Erasing SRAM2 isn't supported\n",
19220936684SInès Varhol                       __func__);
19320936684SInès Varhol         s->skr = value & ACTIVABLE_BITS_SKR;
19420936684SInès Varhol         return;
19520936684SInès Varhol     case SYSCFG_SWPR2:
19620936684SInès Varhol         qemu_log_mask(LOG_UNIMP,
19720936684SInès Varhol                       "%s: Write protecting SRAM2 isn't supported\n",
19820936684SInès Varhol                       __func__);
19920936684SInès Varhol         /* These bits are set by software and cleared only by reset.*/
20020936684SInès Varhol         s->swpr2 |= value;
20120936684SInès Varhol         return;
20220936684SInès Varhol     default:
20320936684SInès Varhol         qemu_log_mask(LOG_GUEST_ERROR,
20420936684SInès Varhol                       "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr);
20520936684SInès Varhol     }
20620936684SInès Varhol }
20720936684SInès Varhol 
20820936684SInès Varhol static const MemoryRegionOps stm32l4x5_syscfg_ops = {
20920936684SInès Varhol     .read = stm32l4x5_syscfg_read,
21020936684SInès Varhol     .write = stm32l4x5_syscfg_write,
21120936684SInès Varhol     .endianness = DEVICE_NATIVE_ENDIAN,
21220936684SInès Varhol     .impl.min_access_size = 4,
21320936684SInès Varhol     .impl.max_access_size = 4,
21420936684SInès Varhol     .impl.unaligned = false,
21520936684SInès Varhol     .valid.min_access_size = 4,
21620936684SInès Varhol     .valid.max_access_size = 4,
21720936684SInès Varhol     .valid.unaligned = false,
21820936684SInès Varhol };
21920936684SInès Varhol 
stm32l4x5_syscfg_init(Object * obj)22020936684SInès Varhol static void stm32l4x5_syscfg_init(Object *obj)
22120936684SInès Varhol {
22220936684SInès Varhol     Stm32l4x5SyscfgState *s = STM32L4X5_SYSCFG(obj);
22320936684SInès Varhol 
22420936684SInès Varhol     memory_region_init_io(&s->mmio, obj, &stm32l4x5_syscfg_ops, s,
22520936684SInès Varhol                           TYPE_STM32L4X5_SYSCFG, 0x400);
22620936684SInès Varhol     sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
22720936684SInès Varhol 
22820936684SInès Varhol     qdev_init_gpio_in(DEVICE(obj), stm32l4x5_syscfg_set_irq,
22920936684SInès Varhol                       GPIO_NUM_PINS * NUM_GPIOS);
23020936684SInès Varhol     qdev_init_gpio_out(DEVICE(obj), s->gpio_out, GPIO_NUM_PINS);
231*b91b8fe7SInès Varhol     s->clk = qdev_init_clock_in(DEVICE(s), "clk", NULL, s, 0);
232*b91b8fe7SInès Varhol }
233*b91b8fe7SInès Varhol 
stm32l4x5_syscfg_realize(DeviceState * dev,Error ** errp)234*b91b8fe7SInès Varhol static void stm32l4x5_syscfg_realize(DeviceState *dev, Error **errp)
235*b91b8fe7SInès Varhol {
236*b91b8fe7SInès Varhol     Stm32l4x5SyscfgState *s = STM32L4X5_SYSCFG(dev);
237*b91b8fe7SInès Varhol     if (!clock_has_source(s->clk)) {
238*b91b8fe7SInès Varhol         error_setg(errp, "SYSCFG: clk input must be connected");
239*b91b8fe7SInès Varhol         return;
240*b91b8fe7SInès Varhol     }
24120936684SInès Varhol }
24220936684SInès Varhol 
24320936684SInès Varhol static const VMStateDescription vmstate_stm32l4x5_syscfg = {
24420936684SInès Varhol     .name = TYPE_STM32L4X5_SYSCFG,
245*b91b8fe7SInès Varhol     .version_id = 2,
246*b91b8fe7SInès Varhol     .minimum_version_id = 2,
24720936684SInès Varhol     .fields = (VMStateField[]) {
24820936684SInès Varhol         VMSTATE_UINT32(memrmp, Stm32l4x5SyscfgState),
24920936684SInès Varhol         VMSTATE_UINT32(cfgr1, Stm32l4x5SyscfgState),
25020936684SInès Varhol         VMSTATE_UINT32_ARRAY(exticr, Stm32l4x5SyscfgState,
25120936684SInès Varhol                              SYSCFG_NUM_EXTICR),
25220936684SInès Varhol         VMSTATE_UINT32(scsr, Stm32l4x5SyscfgState),
25320936684SInès Varhol         VMSTATE_UINT32(cfgr2, Stm32l4x5SyscfgState),
25420936684SInès Varhol         VMSTATE_UINT32(swpr, Stm32l4x5SyscfgState),
25520936684SInès Varhol         VMSTATE_UINT32(skr, Stm32l4x5SyscfgState),
25620936684SInès Varhol         VMSTATE_UINT32(swpr2, Stm32l4x5SyscfgState),
257*b91b8fe7SInès Varhol         VMSTATE_CLOCK(clk, Stm32l4x5SyscfgState),
25820936684SInès Varhol         VMSTATE_END_OF_LIST()
25920936684SInès Varhol     }
26020936684SInès Varhol };
26120936684SInès Varhol 
stm32l4x5_syscfg_class_init(ObjectClass * klass,void * data)26220936684SInès Varhol static void stm32l4x5_syscfg_class_init(ObjectClass *klass, void *data)
26320936684SInès Varhol {
26420936684SInès Varhol     DeviceClass *dc = DEVICE_CLASS(klass);
26520936684SInès Varhol     ResettableClass *rc = RESETTABLE_CLASS(klass);
26620936684SInès Varhol 
26720936684SInès Varhol     dc->vmsd = &vmstate_stm32l4x5_syscfg;
268*b91b8fe7SInès Varhol     dc->realize = stm32l4x5_syscfg_realize;
26920936684SInès Varhol     rc->phases.hold = stm32l4x5_syscfg_hold_reset;
27020936684SInès Varhol }
27120936684SInès Varhol 
27220936684SInès Varhol static const TypeInfo stm32l4x5_syscfg_info[] = {
27320936684SInès Varhol     {
27420936684SInès Varhol         .name          = TYPE_STM32L4X5_SYSCFG,
27520936684SInès Varhol         .parent        = TYPE_SYS_BUS_DEVICE,
27620936684SInès Varhol         .instance_size = sizeof(Stm32l4x5SyscfgState),
27720936684SInès Varhol         .instance_init = stm32l4x5_syscfg_init,
27820936684SInès Varhol         .class_init    = stm32l4x5_syscfg_class_init,
27920936684SInès Varhol     }
28020936684SInès Varhol };
28120936684SInès Varhol 
28220936684SInès Varhol DEFINE_TYPES(stm32l4x5_syscfg_info)
283