xref: /qemu/hw/arm/b-l475e-iot01a.c (revision 06b40d250ecfa1633209c2e431a7a38acfd03a98)
141581f13SInès Varhol /*
241581f13SInès Varhol  * B-L475E-IOT01A Discovery Kit machine
341581f13SInès Varhol  * (B-L475E-IOT01A IoT Node)
441581f13SInès Varhol  *
54c3308c6SInès Varhol  * Copyright (c) 2023-2024 Arnaud Minier <arnaud.minier@telecom-paris.fr>
64c3308c6SInès Varhol  * Copyright (c) 2023-2024 Inès Varhol <ines.varhol@telecom-paris.fr>
741581f13SInès Varhol  *
841581f13SInès Varhol  * SPDX-License-Identifier: GPL-2.0-or-later
941581f13SInès Varhol  *
1041581f13SInès Varhol  * This work is licensed under the terms of the GNU GPL, version 2 or later.
1141581f13SInès Varhol  * See the COPYING file in the top-level directory.
1241581f13SInès Varhol  *
1341581f13SInès Varhol  * This work is heavily inspired by the netduinoplus2 by Alistair Francis.
1441581f13SInès Varhol  * Original code is licensed under the MIT License:
1541581f13SInès Varhol  *
1641581f13SInès Varhol  * Copyright (c) 2014 Alistair Francis <alistair@alistair23.me>
1741581f13SInès Varhol  */
1841581f13SInès Varhol 
1941581f13SInès Varhol /*
2041581f13SInès Varhol  * The reference used is the STMicroElectronics UM2153 User manual
2141581f13SInès Varhol  * Discovery kit for IoT node, multi-channel communication with STM32L4.
2241581f13SInès Varhol  * https://www.st.com/en/evaluation-tools/b-l475e-iot01a.html#documentation
2341581f13SInès Varhol  */
2441581f13SInès Varhol 
2541581f13SInès Varhol #include "qemu/osdep.h"
2641581f13SInès Varhol #include "qapi/error.h"
2741581f13SInès Varhol #include "hw/boards.h"
2841581f13SInès Varhol #include "hw/qdev-properties.h"
2941581f13SInès Varhol #include "qemu/error-report.h"
3041581f13SInès Varhol #include "hw/arm/boot.h"
3149157207SInès Varhol #include "hw/core/split-irq.h"
3249157207SInès Varhol #include "hw/arm/stm32l4x5_soc.h"
3349157207SInès Varhol #include "hw/gpio/stm32l4x5_gpio.h"
3449157207SInès Varhol #include "hw/display/dm163.h"
3541581f13SInès Varhol 
3649157207SInès Varhol /* B-L475E-IOT01A implementation is inspired from netduinoplus2 and arduino */
3749157207SInès Varhol 
3849157207SInès Varhol /*
3949157207SInès Varhol  * There are actually 14 input pins in the DM163 device.
4049157207SInès Varhol  * Here the DM163 input pin EN isn't connected to the STM32L4x5
4149157207SInès Varhol  * GPIOs as the IM120417002 colors shield doesn't actually use
4249157207SInès Varhol  * this pin to drive the RGB matrix.
4349157207SInès Varhol  */
4449157207SInès Varhol #define NUM_DM163_INPUTS 13
4549157207SInès Varhol 
4649157207SInès Varhol static const unsigned dm163_input[NUM_DM163_INPUTS] = {
4749157207SInès Varhol     1 * GPIO_NUM_PINS + 2,  /* ROW0  PB2       */
4849157207SInès Varhol     0 * GPIO_NUM_PINS + 15, /* ROW1  PA15      */
4949157207SInès Varhol     0 * GPIO_NUM_PINS + 2,  /* ROW2  PA2       */
5049157207SInès Varhol     0 * GPIO_NUM_PINS + 7,  /* ROW3  PA7       */
5149157207SInès Varhol     0 * GPIO_NUM_PINS + 6,  /* ROW4  PA6       */
5249157207SInès Varhol     0 * GPIO_NUM_PINS + 5,  /* ROW5  PA5       */
5349157207SInès Varhol     1 * GPIO_NUM_PINS + 0,  /* ROW6  PB0       */
5449157207SInès Varhol     0 * GPIO_NUM_PINS + 3,  /* ROW7  PA3       */
5549157207SInès Varhol     0 * GPIO_NUM_PINS + 4,  /* SIN (SDA) PA4   */
5649157207SInès Varhol     1 * GPIO_NUM_PINS + 1,  /* DCK (SCK) PB1   */
5749157207SInès Varhol     2 * GPIO_NUM_PINS + 3,  /* RST_B (RST) PC3 */
5849157207SInès Varhol     2 * GPIO_NUM_PINS + 4,  /* LAT_B (LAT) PC4 */
5949157207SInès Varhol     2 * GPIO_NUM_PINS + 5,  /* SELBK (SB)  PC5 */
6049157207SInès Varhol };
6141581f13SInès Varhol 
624c3308c6SInès Varhol #define TYPE_B_L475E_IOT01A MACHINE_TYPE_NAME("b-l475e-iot01a")
634c3308c6SInès Varhol OBJECT_DECLARE_SIMPLE_TYPE(Bl475eMachineState, B_L475E_IOT01A)
644c3308c6SInès Varhol 
654c3308c6SInès Varhol typedef struct Bl475eMachineState {
664c3308c6SInès Varhol     MachineState parent_obj;
674c3308c6SInès Varhol 
684c3308c6SInès Varhol     Stm32l4x5SocState soc;
6949157207SInès Varhol     SplitIRQ gpio_splitters[NUM_DM163_INPUTS];
7049157207SInès Varhol     DM163State dm163;
714c3308c6SInès Varhol } Bl475eMachineState;
724c3308c6SInès Varhol 
bl475e_init(MachineState * machine)734c3308c6SInès Varhol static void bl475e_init(MachineState *machine)
7441581f13SInès Varhol {
754c3308c6SInès Varhol     Bl475eMachineState *s = B_L475E_IOT01A(machine);
7641581f13SInès Varhol     const Stm32l4x5SocClass *sc;
7749157207SInès Varhol     DeviceState *dev, *gpio_out_splitter;
7849157207SInès Varhol     unsigned gpio, pin;
7941581f13SInès Varhol 
804c3308c6SInès Varhol     object_initialize_child(OBJECT(machine), "soc", &s->soc,
814c3308c6SInès Varhol                             TYPE_STM32L4X5XG_SOC);
824c3308c6SInès Varhol     sysbus_realize(SYS_BUS_DEVICE(&s->soc), &error_fatal);
8341581f13SInès Varhol 
844c3308c6SInès Varhol     sc = STM32L4X5_SOC_GET_CLASS(&s->soc);
85deeb9969SPhilippe Mathieu-Daudé     armv7m_load_kernel(s->soc.armv7m.cpu, machine->kernel_filename, 0,
864c3308c6SInès Varhol                        sc->flash_size);
8749157207SInès Varhol 
8849157207SInès Varhol     if (object_class_by_name(TYPE_DM163)) {
8949157207SInès Varhol         object_initialize_child(OBJECT(machine), "dm163",
9049157207SInès Varhol                                 &s->dm163, TYPE_DM163);
9149157207SInès Varhol         dev = DEVICE(&s->dm163);
9249157207SInès Varhol         qdev_realize(dev, NULL, &error_abort);
9349157207SInès Varhol 
9449157207SInès Varhol         for (unsigned i = 0; i < NUM_DM163_INPUTS; i++) {
9549157207SInès Varhol             object_initialize_child(OBJECT(machine), "gpio-out-splitters[*]",
9649157207SInès Varhol                                     &s->gpio_splitters[i], TYPE_SPLIT_IRQ);
9749157207SInès Varhol             gpio_out_splitter = DEVICE(&s->gpio_splitters[i]);
9849157207SInès Varhol             qdev_prop_set_uint32(gpio_out_splitter, "num-lines", 2);
9949157207SInès Varhol             qdev_realize(gpio_out_splitter, NULL, &error_fatal);
10049157207SInès Varhol 
10149157207SInès Varhol             qdev_connect_gpio_out(gpio_out_splitter, 0,
10249157207SInès Varhol                 qdev_get_gpio_in(DEVICE(&s->soc), dm163_input[i]));
10349157207SInès Varhol             qdev_connect_gpio_out(gpio_out_splitter, 1,
10449157207SInès Varhol                 qdev_get_gpio_in(dev, i));
10549157207SInès Varhol             gpio = dm163_input[i] / GPIO_NUM_PINS;
10649157207SInès Varhol             pin = dm163_input[i] % GPIO_NUM_PINS;
10749157207SInès Varhol             qdev_connect_gpio_out(DEVICE(&s->soc.gpio[gpio]), pin,
10849157207SInès Varhol                 qdev_get_gpio_in(DEVICE(gpio_out_splitter), 0));
10949157207SInès Varhol         }
11049157207SInès Varhol     }
11141581f13SInès Varhol }
11241581f13SInès Varhol 
bl475e_machine_init(ObjectClass * oc,const void * data)113*12d1a768SPhilippe Mathieu-Daudé static void bl475e_machine_init(ObjectClass *oc, const void *data)
11441581f13SInès Varhol {
1154c3308c6SInès Varhol     MachineClass *mc = MACHINE_CLASS(oc);
11641581f13SInès Varhol     static const char *machine_valid_cpu_types[] = {
11741581f13SInès Varhol         ARM_CPU_TYPE_NAME("cortex-m4"),
11841581f13SInès Varhol         NULL
11941581f13SInès Varhol     };
12041581f13SInès Varhol     mc->desc = "B-L475E-IOT01A Discovery Kit (Cortex-M4)";
1214c3308c6SInès Varhol     mc->init = bl475e_init;
12241581f13SInès Varhol     mc->valid_cpu_types = machine_valid_cpu_types;
12341581f13SInès Varhol 
12441581f13SInès Varhol     /* SRAM pre-allocated as part of the SoC instantiation */
12541581f13SInès Varhol     mc->default_ram_size = 0;
12641581f13SInès Varhol }
12741581f13SInès Varhol 
1284c3308c6SInès Varhol static const TypeInfo bl475e_machine_type[] = {
1294c3308c6SInès Varhol     {
1304c3308c6SInès Varhol         .name           = TYPE_B_L475E_IOT01A,
1314c3308c6SInès Varhol         .parent         = TYPE_MACHINE,
1324c3308c6SInès Varhol         .instance_size  = sizeof(Bl475eMachineState),
1334c3308c6SInès Varhol         .class_init     = bl475e_machine_init,
1344c3308c6SInès Varhol     }
1354c3308c6SInès Varhol };
1364c3308c6SInès Varhol 
1374c3308c6SInès Varhol DEFINE_TYPES(bl475e_machine_type)
138