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"
31*49157207SInès Varhol #include "hw/core/split-irq.h"
32*49157207SInès Varhol #include "hw/arm/stm32l4x5_soc.h"
33*49157207SInès Varhol #include "hw/gpio/stm32l4x5_gpio.h"
34*49157207SInès Varhol #include "hw/display/dm163.h"
3541581f13SInès Varhol
36*49157207SInès Varhol /* B-L475E-IOT01A implementation is inspired from netduinoplus2 and arduino */
37*49157207SInès Varhol
38*49157207SInès Varhol /*
39*49157207SInès Varhol * There are actually 14 input pins in the DM163 device.
40*49157207SInès Varhol * Here the DM163 input pin EN isn't connected to the STM32L4x5
41*49157207SInès Varhol * GPIOs as the IM120417002 colors shield doesn't actually use
42*49157207SInès Varhol * this pin to drive the RGB matrix.
43*49157207SInès Varhol */
44*49157207SInès Varhol #define NUM_DM163_INPUTS 13
45*49157207SInès Varhol
46*49157207SInès Varhol static const unsigned dm163_input[NUM_DM163_INPUTS] = {
47*49157207SInès Varhol 1 * GPIO_NUM_PINS + 2, /* ROW0 PB2 */
48*49157207SInès Varhol 0 * GPIO_NUM_PINS + 15, /* ROW1 PA15 */
49*49157207SInès Varhol 0 * GPIO_NUM_PINS + 2, /* ROW2 PA2 */
50*49157207SInès Varhol 0 * GPIO_NUM_PINS + 7, /* ROW3 PA7 */
51*49157207SInès Varhol 0 * GPIO_NUM_PINS + 6, /* ROW4 PA6 */
52*49157207SInès Varhol 0 * GPIO_NUM_PINS + 5, /* ROW5 PA5 */
53*49157207SInès Varhol 1 * GPIO_NUM_PINS + 0, /* ROW6 PB0 */
54*49157207SInès Varhol 0 * GPIO_NUM_PINS + 3, /* ROW7 PA3 */
55*49157207SInès Varhol 0 * GPIO_NUM_PINS + 4, /* SIN (SDA) PA4 */
56*49157207SInès Varhol 1 * GPIO_NUM_PINS + 1, /* DCK (SCK) PB1 */
57*49157207SInès Varhol 2 * GPIO_NUM_PINS + 3, /* RST_B (RST) PC3 */
58*49157207SInès Varhol 2 * GPIO_NUM_PINS + 4, /* LAT_B (LAT) PC4 */
59*49157207SInès Varhol 2 * GPIO_NUM_PINS + 5, /* SELBK (SB) PC5 */
60*49157207SInè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;
69*49157207SInès Varhol SplitIRQ gpio_splitters[NUM_DM163_INPUTS];
70*49157207SInè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;
77*49157207SInès Varhol DeviceState *dev, *gpio_out_splitter;
78*49157207SInè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);
854c3308c6SInès Varhol armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename, 0,
864c3308c6SInès Varhol sc->flash_size);
87*49157207SInès Varhol
88*49157207SInès Varhol if (object_class_by_name(TYPE_DM163)) {
89*49157207SInès Varhol object_initialize_child(OBJECT(machine), "dm163",
90*49157207SInès Varhol &s->dm163, TYPE_DM163);
91*49157207SInès Varhol dev = DEVICE(&s->dm163);
92*49157207SInès Varhol qdev_realize(dev, NULL, &error_abort);
93*49157207SInès Varhol
94*49157207SInès Varhol for (unsigned i = 0; i < NUM_DM163_INPUTS; i++) {
95*49157207SInès Varhol object_initialize_child(OBJECT(machine), "gpio-out-splitters[*]",
96*49157207SInès Varhol &s->gpio_splitters[i], TYPE_SPLIT_IRQ);
97*49157207SInès Varhol gpio_out_splitter = DEVICE(&s->gpio_splitters[i]);
98*49157207SInès Varhol qdev_prop_set_uint32(gpio_out_splitter, "num-lines", 2);
99*49157207SInès Varhol qdev_realize(gpio_out_splitter, NULL, &error_fatal);
100*49157207SInès Varhol
101*49157207SInès Varhol qdev_connect_gpio_out(gpio_out_splitter, 0,
102*49157207SInès Varhol qdev_get_gpio_in(DEVICE(&s->soc), dm163_input[i]));
103*49157207SInès Varhol qdev_connect_gpio_out(gpio_out_splitter, 1,
104*49157207SInès Varhol qdev_get_gpio_in(dev, i));
105*49157207SInès Varhol gpio = dm163_input[i] / GPIO_NUM_PINS;
106*49157207SInès Varhol pin = dm163_input[i] % GPIO_NUM_PINS;
107*49157207SInès Varhol qdev_connect_gpio_out(DEVICE(&s->soc.gpio[gpio]), pin,
108*49157207SInès Varhol qdev_get_gpio_in(DEVICE(gpio_out_splitter), 0));
109*49157207SInès Varhol }
110*49157207SInès Varhol }
11141581f13SInès Varhol }
11241581f13SInès Varhol
bl475e_machine_init(ObjectClass * oc,void * data)1134c3308c6SInès Varhol static void bl475e_machine_init(ObjectClass *oc, 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