From patchwork Fri Jan 12 17:05:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?In=C3=A8s_Varhol?= X-Patchwork-Id: 1886188 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=telecom-paris.fr header.i=@telecom-paris.fr header.a=rsa-sha256 header.s=A35C7578-1106-11E5-A17F-C303FDDA8F2E header.b=ms5wDZfh; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TBSdq0Bltz1yPf for ; Sat, 13 Jan 2024 04:07:43 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rOKze-0005K0-6Y; Fri, 12 Jan 2024 12:06:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rOKzc-0005Ir-HS; Fri, 12 Jan 2024 12:06:52 -0500 Received: from zproxy2.enst.fr ([137.194.2.221]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rOKzY-00050Z-3y; Fri, 12 Jan 2024 12:06:52 -0500 Received: from localhost (localhost [IPv6:::1]) by zproxy2.enst.fr (Postfix) with ESMTP id A6E9880689; Fri, 12 Jan 2024 18:06:42 +0100 (CET) Received: from zproxy2.enst.fr ([IPv6:::1]) by localhost (zproxy2.enst.fr [IPv6:::1]) (amavis, port 10032) with ESMTP id gWrdwKkOY9M1; Fri, 12 Jan 2024 18:06:41 +0100 (CET) Received: from localhost (localhost [IPv6:::1]) by zproxy2.enst.fr (Postfix) with ESMTP id 4FCC08066D; Fri, 12 Jan 2024 18:06:41 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.10.3 zproxy2.enst.fr 4FCC08066D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telecom-paris.fr; s=A35C7578-1106-11E5-A17F-C303FDDA8F2E; t=1705079201; bh=e8CaFPt3t7WNykfq9lSxgZInyTaxN+5SCk6ipY+Z7Fk=; h=From:To:Date:Message-ID:MIME-Version; b=ms5wDZfh8qiP8RqQviu5TnQvV3jQO8Xp8B56oxRwGaaaz4+4jrRgXEJuNFsRyCVuO 1+P1Lh2sOBo9oTi4WVfd4qyAnJD0hdIBmxv5EbI4U7cLJyIAtWOtxoRDTvTWI/N5ZK J3sTrWeG7YMBuUUCSdMCSYbvWXGEhqSlOlvQwA/8= X-Virus-Scanned: amavis at enst.fr Received: from zproxy2.enst.fr ([IPv6:::1]) by localhost (zproxy2.enst.fr [IPv6:::1]) (amavis, port 10026) with ESMTP id 7eOnXL6mgMr2; Fri, 12 Jan 2024 18:06:41 +0100 (CET) Received: from inesv-Inspiron-3501.enst.fr (unknown [IPv6:2a04:8ec0:0:240:fa97:9da:79c1:e167]) by zproxy2.enst.fr (Postfix) with ESMTPSA id 093C380689; Fri, 12 Jan 2024 18:06:41 +0100 (CET) From: =?utf-8?q?In=C3=A8s_Varhol?= To: qemu-devel@nongnu.org Cc: Samuel Tardieu , Arnaud Minier , =?utf-8?q?In=C3=A8s_Varhol?= , Thomas Huth , Peter Maydell , qemu-arm@nongnu.org, Paolo Bonzini , Alistair Francis , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Laurent Vivier Subject: [RFC 1/3] hw/gpio: Implement STM32L4x5 GPIO Date: Fri, 12 Jan 2024 18:05:38 +0100 Message-ID: <20240112170635.303226-2-ines.varhol@telecom-paris.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240112170635.303226-1-ines.varhol@telecom-paris.fr> References: <20240112170635.303226-1-ines.varhol@telecom-paris.fr> MIME-Version: 1.0 Received-SPF: pass client-ip=137.194.2.221; envelope-from=ines.varhol@telecom-paris.fr; helo=zproxy2.enst.fr X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01, WEIRD_QUOTING=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Arnaud Minier Signed-off-by: Inès Varhol --- docs/system/arm/b-l475e-iot01a.rst | 2 +- hw/gpio/Kconfig | 3 + hw/gpio/meson.build | 1 + hw/gpio/stm32l4x5_gpio.c | 520 +++++++++++++++++++++++++++++ hw/gpio/trace-events | 6 + include/hw/gpio/stm32l4x5_gpio.h | 79 +++++ 6 files changed, 610 insertions(+), 1 deletion(-) create mode 100644 hw/gpio/stm32l4x5_gpio.c create mode 100644 include/hw/gpio/stm32l4x5_gpio.h diff --git a/docs/system/arm/b-l475e-iot01a.rst b/docs/system/arm/b-l475e-iot01a.rst index 1a021b306a..fe1143e05e 100644 --- a/docs/system/arm/b-l475e-iot01a.rst +++ b/docs/system/arm/b-l475e-iot01a.rst @@ -17,6 +17,7 @@ Currently B-L475E-IOT01A machine's only supports the following devices: - Cortex-M4F based STM32L4x5 SoC - STM32L4x5 EXTI (Extended interrupts and events controller) - STM32L4x5 SYSCFG (System configuration controller) +- STM32L4x5 GPIOs (General-purpose I/Os) Missing devices """"""""""""""" @@ -25,7 +26,6 @@ The B-L475E-IOT01A does *not* support the following devices: - Reset and clock control (RCC) - Serial ports (UART) -- General-purpose I/Os (GPIO) - Analog to Digital Converter (ADC) - SPI controller - Timer controller (TIMER) diff --git a/hw/gpio/Kconfig b/hw/gpio/Kconfig index d2cf3accc8..712940b8e0 100644 --- a/hw/gpio/Kconfig +++ b/hw/gpio/Kconfig @@ -16,3 +16,6 @@ config GPIO_PWR config SIFIVE_GPIO bool + +config STM32L4X5_GPIO + bool diff --git a/hw/gpio/meson.build b/hw/gpio/meson.build index 066ea96480..8470ca1639 100644 --- a/hw/gpio/meson.build +++ b/hw/gpio/meson.build @@ -9,6 +9,7 @@ system_ss.add(when: 'CONFIG_IMX', if_true: files('imx_gpio.c')) system_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_gpio.c')) system_ss.add(when: 'CONFIG_NRF51_SOC', if_true: files('nrf51_gpio.c')) system_ss.add(when: 'CONFIG_OMAP', if_true: files('omap_gpio.c')) +system_ss.add(when: 'CONFIG_STM32L4X5_SOC', if_true: files('stm32l4x5_gpio.c')) system_ss.add(when: 'CONFIG_RASPI', if_true: files('bcm2835_gpio.c')) system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_gpio.c')) system_ss.add(when: 'CONFIG_SIFIVE_GPIO', if_true: files('sifive_gpio.c')) diff --git a/hw/gpio/stm32l4x5_gpio.c b/hw/gpio/stm32l4x5_gpio.c new file mode 100644 index 0000000000..2199d12c76 --- /dev/null +++ b/hw/gpio/stm32l4x5_gpio.c @@ -0,0 +1,520 @@ +/* + * STM32L4x5 GPIO (General Purpose Input/Ouput) + * + * Copyright (c) 2023 Arnaud Minier + * Copyright (c) 2023 Inès Varhol + * + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +/* + * The reference used is the STMicroElectronics RM0351 Reference manual + * for STM32L4x5 and STM32L4x6 advanced Arm ® -based 32-bit MCUs. + * https://www.st.com/en/microcontrollers-microprocessors/stm32l4x5/documentation.html + */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "hw/gpio/stm32l4x5_gpio.h" +#include "hw/irq.h" +#include "migration/vmstate.h" +#include "trace.h" + +#define GPIO_MODER 0x00 +#define GPIO_OTYPER 0x04 +#define GPIO_OSPEEDR 0x08 +#define GPIO_PUPDR 0x0C +#define GPIO_IDR 0x10 +#define GPIO_ODR 0x14 +#define GPIO_BSRR 0x18 +#define GPIO_LCKR 0x1C +#define GPIO_AFRL 0x20 +#define GPIO_AFRH 0x24 +#define GPIO_BRR 0x28 +#define GPIO_ASCR 0x2C +/* + * DISCONNECTED_PINS isn't actually a GPIO register. + * It exists to ensure that : + * - push-pull output pins can't be set externally + * - open-drain output pins can only be externally set to 0 + * + * This field is accessed for test purposes. + */ +#define GPIO_DISCONNECTED_PINS 0x30 + +/* 0b11111111_11111111_00000000_00000000 */ +#define RESERVED_BITS_MASK 0xFFFF0000 + +static void update_gpio_idr(Stm32l4x5GpioState *s); + +static void stm32l4x5_gpio_reset_hold(Object *obj) +{ + Stm32l4x5GpioState *s = STM32L4X5_GPIO(obj); + Stm32l4x5GpioClass *sc = STM32L4X5_GPIO_GET_CLASS(obj); + + s->moder = sc->moder_reset; + s->otyper = 0x00000000; + s->ospeedr = sc->ospeedr_reset; + s->pupdr = sc->pupdr_reset; + s->idr = 0x00000000; + s->odr = 0x00000000; + s->lckr = 0x00000000; + s->afrl = 0x00000000; + s->afrh = 0x00000000; + s->ascr = 0x00000000; + + s->disconnected_pins = 0xFFFF; + s->pins_connected_high = 0x0000; + update_gpio_idr(s); +} + +static void stm32l4x5_gpio_set(void *opaque, int line, int level) +{ + Stm32l4x5GpioState *s = opaque; + /* + * The pin isn't set if line is configured in output mode + * except if level is 0 and the output is open-drain. + * This way there will be no short-circuit prone situations. + */ + if ((extract32(s->moder, 2 * line, 2) == 1) && + !((extract32(s->otyper, line, 1) == 1) && + (level == 0))) { + qemu_log_mask(LOG_GUEST_ERROR, "Line %d can't be driven externally\n", + line); + return; + } + + s->disconnected_pins &= ~(1 << line); + if (level) { + s->pins_connected_high |= (1 << line); + } else { + s->pins_connected_high &= ~(1 << line); + } + trace_stm32l4x5_gpio_pins(s->disconnected_pins, + s->pins_connected_high); + update_gpio_idr(s); +} + + +static void update_gpio_idr(Stm32l4x5GpioState *s) +{ + uint32_t new_idr_mask = 0; + uint32_t new_idr = s->odr; + + for (int i = 0; i < 16; i++) { + /* output mode */ + if (extract32(s->moder, 2 * i, 2) == 1) { + if (extract32(s->otyper, i, 1) == 0) { + /* push-pull */ + new_idr_mask |= (1 << i); + } else if (!(s->odr & (1 << i))) { + /* open-drain ODR 0 */ + new_idr_mask |= (1 << i); + } else if ((s->disconnected_pins & (1 << i)) && + (extract32(s->pupdr, 2 * i, 2) == 1)) { + /* open-drain pull-up ODR 1 with floating pin */ + new_idr_mask |= (1 << i); + } else if ((s->disconnected_pins & (1 << i)) && + (extract32(s->pupdr, 2 * i, 2) == 2)) { + /* open-drain pull-down ODR 1 with floating pin */ + new_idr_mask |= (1 << i); + new_idr &= ~(1 << i); + } else if (!(s->pins_connected_high & (1 << i))) { + /* open-drain ODR 1 with pin connected low */ + new_idr_mask |= (1 << i); + new_idr &= ~(1 << i); + } else { + /* open-drain ODR 1 with pin connected high */ + assert(false); + } + /* input or analog mode with connected pin */ + } else if (!(s->disconnected_pins & (1 << i))) { + if (s->pins_connected_high & (1 << i)) { + /* pin high */ + new_idr_mask |= (1 << i); + new_idr |= (1 << i); + } else { + /* pin low */ + new_idr_mask |= (1 << i); + new_idr &= ~(1 << i); + } + /* input or analog mode with disconnected pin */ + } else { + if (extract32(s->pupdr, 2 * i, 2) == 1) { + /* pull-up */ + new_idr_mask |= (1 << i); + new_idr |= (1 << i); + } else if (extract32(s->pupdr, 2 * i, 2) == 2) { + /* pull-down */ + new_idr_mask |= (1 << i); + new_idr &= ~(1 << i); + } + } + } + + uint32_t old_idr = s->idr; + s->idr = (old_idr & ~new_idr_mask) | (new_idr & new_idr_mask); + trace_stm32l4x5_gpio_update_idr(old_idr, s->idr); + + for (int i = 0; i < 16; i++) { + if (new_idr_mask & (1 << i)) { + if ((new_idr & (1 << i)) > (old_idr & (1 << i))) { + qemu_irq_raise(s->pin[i]); + } else if ((new_idr & (1 << i)) < (old_idr & (1 << i))) { + qemu_irq_lower(s->pin[i]); + } + } + } +} + +/* + * Return pins both configured in output mode + * and externally driven (except pins in open-drain + * mode externally set to 0). + */ +static uint32_t get_gpio_pins_to_disconnect(Stm32l4x5GpioState *s) +{ + uint32_t pins_to_disconnect = 0; + for (int i = 0; i < 16; i++) { + /* for each connected pin in output mode */ + if ((~s->disconnected_pins & (1 << i)) & + (extract32(s->moder, 2 * i, 2) == 1)) { + /* if either push-pull or high level */ + if ((extract32(s->otyper, i, 1) == 0) || + (extract16(s->pins_connected_high, i, 1)) == 1) { + pins_to_disconnect |= (1 << i); + qemu_log_mask(LOG_GUEST_ERROR, + "Line %d can't be driven externally\n", + i); + } + } + } + return pins_to_disconnect; +} + +/* + * Set field `disconnected_pins` and call `update_gpio_idr()` + */ +static void disconnect_gpio_pins(Stm32l4x5GpioState *s, uint16_t lines) +{ + s->disconnected_pins |= lines; + trace_stm32l4x5_gpio_pins(s->disconnected_pins, + s->pins_connected_high); + update_gpio_idr(s); +} + +static void stm32l4x5_gpio_write(void *opaque, hwaddr addr, + uint64_t val64, unsigned int size) +{ + Stm32l4x5GpioState *s = opaque; + + uint32_t value = val64; + trace_stm32l4x5_gpio_write(addr, val64); + + switch (addr) { + case GPIO_MODER: + s->moder = value; + disconnect_gpio_pins(s, get_gpio_pins_to_disconnect(s)); + qemu_log_mask(LOG_UNIMP, + "%s: Alternate function mode isn't supported\n\ + Setting alternate function mode sets analog mode\n", + __func__); + return; + case GPIO_OTYPER: + s->otyper = value & ~RESERVED_BITS_MASK; + disconnect_gpio_pins(s, get_gpio_pins_to_disconnect(s)); + return; + case GPIO_OSPEEDR: + qemu_log_mask(LOG_UNIMP, + "%s: Changing the I/O output speed isn't supported\n", + __func__); + s->ospeedr = value; + return; + case GPIO_PUPDR: + s->pupdr = value; + update_gpio_idr(s); + return; + case GPIO_IDR: + qemu_log_mask(LOG_UNIMP, + "%s: GPIO->IDR is read-only\n", + __func__); + return; + case GPIO_ODR: + s->odr = value & ~RESERVED_BITS_MASK; + update_gpio_idr(s); + return; + case GPIO_BSRR: { + uint32_t bits_to_reset = (value & RESERVED_BITS_MASK) >> 16; + uint32_t bits_to_set = value & ~RESERVED_BITS_MASK; + /* If both BSx and BRx are set, BSx has priority.*/ + s->odr &= ~bits_to_reset; + s->odr |= bits_to_set; + update_gpio_idr(s); + return; + } + case GPIO_LCKR: + qemu_log_mask(LOG_UNIMP, + "%s: Locking port bits configuration isn't supported\n", + __func__); + s->lckr = value & ~RESERVED_BITS_MASK; + return; + case GPIO_AFRL: + qemu_log_mask(LOG_UNIMP, + "%s: Alternate functions aren't supported\n", + __func__); + s->afrl = value; + return; + case GPIO_AFRH: + qemu_log_mask(LOG_UNIMP, + "%s: Alternate functions aren't supported\n", + __func__); + s->afrh = value; + return; + case GPIO_BRR: { + uint32_t bits_to_reset = value & ~RESERVED_BITS_MASK; + s->odr &= ~bits_to_reset; + update_gpio_idr(s); + return; + } + case GPIO_ASCR: + qemu_log_mask(LOG_UNIMP, + "%s: ADC function isn't supported\n", + __func__); + s->ascr = value & ~RESERVED_BITS_MASK; + return; + /* a tweak to enable the qtest checking disconnected pins */ + case GPIO_DISCONNECTED_PINS: + disconnect_gpio_pins(s, value); + return; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr); + } +} + +static uint64_t stm32l4x5_gpio_read(void *opaque, hwaddr addr, + unsigned int size) +{ + Stm32l4x5GpioState *s = opaque; + + trace_stm32l4x5_gpio_read(addr); + + switch (addr) { + case GPIO_MODER: + return s->moder; + case GPIO_OTYPER: + return s->otyper; + case GPIO_OSPEEDR: + return s->ospeedr; + case GPIO_PUPDR: + return s->pupdr; + case GPIO_IDR: + return s->idr; + case GPIO_ODR: + return s->odr; + case GPIO_BSRR: + return 0; + case GPIO_LCKR: + return s->lckr; + case GPIO_AFRL: + return s->afrl; + case GPIO_AFRH: + return s->afrh; + case GPIO_BRR: + return 0; + case GPIO_ASCR: + return s->ascr; + /* a tweak to enable the qtest checking disconnected pins */ + case GPIO_DISCONNECTED_PINS: + return s->disconnected_pins; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr); + return 0; + } +} + +static const MemoryRegionOps stm32l4x5_gpio_ops = { + .read = stm32l4x5_gpio_read, + .write = stm32l4x5_gpio_write, + .endianness = DEVICE_NATIVE_ENDIAN, + .impl = { + .min_access_size = 4, + .max_access_size = 4, + .unaligned = false, + }, + .valid = { + .min_access_size = 4, + .max_access_size = 4, + .unaligned = false, + }, +}; + +static void stm32l4x5_gpio_init(Object *obj) +{ + Stm32l4x5GpioState *s = STM32L4X5_GPIO(obj); + int i; + + for (i = 0; i < GPIO_NUM_PINS; i++) { + sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->pin[i]); + } + + memory_region_init_io(&s->mmio, obj, &stm32l4x5_gpio_ops, s, + TYPE_STM32L4X5_GPIO, 0x400); + + sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio); + + qdev_init_gpio_out(DEVICE(obj), s->pin, GPIO_NUM_PINS); + qdev_init_gpio_in(DEVICE(obj), stm32l4x5_gpio_set, GPIO_NUM_PINS); +} + +static const VMStateDescription vmstate_stm32l4x5_gpio = { + .name = TYPE_STM32L4X5_GPIO, + .version_id = 1, + .minimum_version_id = 1, + .fields = (VMStateField[]){ + VMSTATE_UINT32(moder, Stm32l4x5GpioState), + VMSTATE_UINT32(otyper, Stm32l4x5GpioState), + VMSTATE_UINT32(ospeedr, Stm32l4x5GpioState), + VMSTATE_UINT32(pupdr, Stm32l4x5GpioState), + VMSTATE_UINT32(idr, Stm32l4x5GpioState), + VMSTATE_UINT32(odr, Stm32l4x5GpioState), + VMSTATE_UINT32(lckr, Stm32l4x5GpioState), + VMSTATE_UINT32(afrl, Stm32l4x5GpioState), + VMSTATE_UINT32(afrh, Stm32l4x5GpioState), + VMSTATE_UINT32(ascr, Stm32l4x5GpioState), + VMSTATE_UINT16(disconnected_pins, Stm32l4x5GpioState), + VMSTATE_UINT16(pins_connected_high, Stm32l4x5GpioState), + VMSTATE_END_OF_LIST() + } +}; + +static void stm32l4x5_gpio_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + ResettableClass *rc = RESETTABLE_CLASS(klass); + + dc->vmsd = &vmstate_stm32l4x5_gpio; + rc->phases.hold = stm32l4x5_gpio_reset_hold; +} + +static void stm32l4x5_gpio_a_class_init(ObjectClass *klass, void *data) +{ + Stm32l4x5GpioClass *sc = STM32L4X5_GPIO_CLASS(klass); + + sc->moder_reset = 0xABFFFFFF; + sc->ospeedr_reset = 0x0C000000; + sc->pupdr_reset = 0x64000000; +} + +static void stm32l4x5_gpio_b_class_init(ObjectClass *klass, void *data) +{ + Stm32l4x5GpioClass *sc = STM32L4X5_GPIO_CLASS(klass); + + sc->moder_reset = 0xFFFFFEBF; + sc->ospeedr_reset = 0x00000000; + sc->pupdr_reset = 0x00000100; +} + +static void stm32l4x5_gpio_c_class_init(ObjectClass *klass, void *data) +{ + Stm32l4x5GpioClass *sc = STM32L4X5_GPIO_CLASS(klass); + + sc->moder_reset = 0xFFFFFFFF; + sc->ospeedr_reset = 0x00000000; + sc->pupdr_reset = 0x00000000; +} + +static void stm32l4x5_gpio_d_class_init(ObjectClass *klass, void *data) +{ + Stm32l4x5GpioClass *sc = STM32L4X5_GPIO_CLASS(klass); + + sc->moder_reset = 0xFFFFFFFF; + sc->ospeedr_reset = 0x00000000; + sc->pupdr_reset = 0x00000000; +} + +static void stm32l4x5_gpio_e_class_init(ObjectClass *klass, void *data) +{ + Stm32l4x5GpioClass *sc = STM32L4X5_GPIO_CLASS(klass); + + sc->moder_reset = 0xFFFFFFFF; + sc->ospeedr_reset = 0x00000000; + sc->pupdr_reset = 0x00000000; +} + +static void stm32l4x5_gpio_f_class_init(ObjectClass *klass, void *data) +{ + Stm32l4x5GpioClass *sc = STM32L4X5_GPIO_CLASS(klass); + + sc->moder_reset = 0xFFFFFFFF; + sc->ospeedr_reset = 0x00000000; + sc->pupdr_reset = 0x00000000; +} + +static void stm32l4x5_gpio_g_class_init(ObjectClass *klass, void *data) +{ + Stm32l4x5GpioClass *sc = STM32L4X5_GPIO_CLASS(klass); + + sc->moder_reset = 0xFFFFFFFF; + sc->ospeedr_reset = 0x00000000; + sc->pupdr_reset = 0x00000000; +} + +static void stm32l4x5_gpio_h_class_init(ObjectClass *klass, void *data) +{ + Stm32l4x5GpioClass *sc = STM32L4X5_GPIO_CLASS(klass); + + sc->moder_reset = 0x0000000F; + sc->ospeedr_reset = 0x00000000; + sc->pupdr_reset = 0x00000000; +} + +static const TypeInfo stm32l4x5_gpio_types[] = { + { + .name = TYPE_STM32L4X5_GPIO, + .parent = TYPE_SYS_BUS_DEVICE, + .instance_size = sizeof(Stm32l4x5GpioState), + .instance_init = stm32l4x5_gpio_init, + .class_size = sizeof(Stm32l4x5GpioClass), + .class_init = stm32l4x5_gpio_class_init, + .abstract = true, + }, { + .name = TYPE_STM32L4X5_GPIO_A, + .parent = TYPE_STM32L4X5_GPIO, + .class_init = stm32l4x5_gpio_a_class_init, + }, { + .name = TYPE_STM32L4X5_GPIO_B, + .parent = TYPE_STM32L4X5_GPIO, + .class_init = stm32l4x5_gpio_b_class_init, + }, { + .name = TYPE_STM32L4X5_GPIO_C, + .parent = TYPE_STM32L4X5_GPIO, + .class_init = stm32l4x5_gpio_c_class_init, + }, { + .name = TYPE_STM32L4X5_GPIO_D, + .parent = TYPE_STM32L4X5_GPIO, + .class_init = stm32l4x5_gpio_d_class_init, + }, { + .name = TYPE_STM32L4X5_GPIO_E, + .parent = TYPE_STM32L4X5_GPIO, + .class_init = stm32l4x5_gpio_e_class_init, + }, { + .name = TYPE_STM32L4X5_GPIO_F, + .parent = TYPE_STM32L4X5_GPIO, + .class_init = stm32l4x5_gpio_f_class_init, + }, { + .name = TYPE_STM32L4X5_GPIO_G, + .parent = TYPE_STM32L4X5_GPIO, + .class_init = stm32l4x5_gpio_g_class_init, + }, { + .name = TYPE_STM32L4X5_GPIO_H, + .parent = TYPE_STM32L4X5_GPIO, + .class_init = stm32l4x5_gpio_h_class_init, + }, +}; + +DEFINE_TYPES(stm32l4x5_gpio_types) diff --git a/hw/gpio/trace-events b/hw/gpio/trace-events index 9736b362ac..8cbf75a897 100644 --- a/hw/gpio/trace-events +++ b/hw/gpio/trace-events @@ -31,3 +31,9 @@ sifive_gpio_update_output_irq(int64_t line, int64_t value) "line %" PRIi64 " val # aspeed_gpio.c aspeed_gpio_read(uint64_t offset, uint64_t value) "offset: 0x%" PRIx64 " value 0x%" PRIx64 aspeed_gpio_write(uint64_t offset, uint64_t value) "offset: 0x%" PRIx64 " value 0x%" PRIx64 + +# stm32l4x5_gpio.c +stm32l4x5_gpio_read(uint64_t addr) "reg read: addr: 0x%" PRIx64 " " +stm32l4x5_gpio_write(uint64_t addr, uint64_t data) "reg write: addr: 0x%" PRIx64 " val: 0x%" PRIx64 "" +stm32l4x5_gpio_update_idr(uint32_t old_idr, uint32_t new_idr) "previous idr: 0x%x new idr: 0x%x" +stm32l4x5_gpio_pins(uint16_t disconnected, uint16_t high) "disconnected pins: 0x%x levels: 0x%x" diff --git a/include/hw/gpio/stm32l4x5_gpio.h b/include/hw/gpio/stm32l4x5_gpio.h new file mode 100644 index 0000000000..7d6c16f6b9 --- /dev/null +++ b/include/hw/gpio/stm32l4x5_gpio.h @@ -0,0 +1,79 @@ +/* + * STM32L4x5 GPIO (General Purpose Input/Ouput) + * + * Copyright (c) 2023 Arnaud Minier + * Copyright (c) 2023 Inès Varhol + * + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +/* + * The reference used is the STMicroElectronics RM0351 Reference manual + * for STM32L4x5 and STM32L4x6 advanced Arm ® -based 32-bit MCUs. + * https://www.st.com/en/microcontrollers-microprocessors/stm32l4x5/documentation.html + */ + +#ifndef HW_STM32L4X5_GPIO_H +#define HW_STM32L4X5_GPIO_H + +#include "hw/sysbus.h" +#include "qom/object.h" + +#define TYPE_STM32L4X5_GPIO "stm32l4x5-gpio" +#define TYPE_STM32L4X5_GPIO_A "stm32l4x5-gpio-a" +#define TYPE_STM32L4X5_GPIO_B "stm32l4x5-gpio-b" +#define TYPE_STM32L4X5_GPIO_C "stm32l4x5-gpio-c" +#define TYPE_STM32L4X5_GPIO_D "stm32l4x5-gpio-d" +#define TYPE_STM32L4X5_GPIO_E "stm32l4x5-gpio-e" +#define TYPE_STM32L4X5_GPIO_F "stm32l4x5-gpio-f" +#define TYPE_STM32L4X5_GPIO_G "stm32l4x5-gpio-g" +#define TYPE_STM32L4X5_GPIO_H "stm32l4x5-gpio-h" +OBJECT_DECLARE_TYPE(Stm32l4x5GpioState, Stm32l4x5GpioClass, STM32L4X5_GPIO) + +#define GPIO_NUM_PINS 16 + +struct Stm32l4x5GpioState { + SysBusDevice parent_obj; + + MemoryRegion mmio; + + /* GPIO registers */ + uint32_t moder; + uint32_t otyper; + uint32_t ospeedr; + uint32_t pupdr; + uint32_t idr; + uint32_t odr; + uint32_t lckr; + uint32_t afrl; + uint32_t afrh; + uint32_t ascr; + + /* + * External driving of pins. + * The pins can be set externally through the device + * anonymous input GPIOs lines under certain conditions. + * The pin must not be in push-pull output mode, + * and can't be set high in open-drain mode. + * Pins driven externally and configured to + * output mode will in general be "disconnected" + * (see `get_gpio_pins_to_disconnect()`) + */ + uint16_t disconnected_pins; + uint16_t pins_connected_high; + + qemu_irq pin[GPIO_NUM_PINS]; +}; + +struct Stm32l4x5GpioClass { + SysBusDeviceClass parent_class; + + uint32_t moder_reset; + uint32_t ospeedr_reset; + uint32_t pupdr_reset; +}; + +#endif From patchwork Fri Jan 12 17:05:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?In=C3=A8s_Varhol?= X-Patchwork-Id: 1886189 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=telecom-paris.fr header.i=@telecom-paris.fr header.a=rsa-sha256 header.s=A35C7578-1106-11E5-A17F-C303FDDA8F2E header.b=gLXSl3Od; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TBSdw4Pkdz1yPf for ; Sat, 13 Jan 2024 04:07:48 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rOKze-0005KE-Ap; Fri, 12 Jan 2024 12:06:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rOKzc-0005IV-4U; Fri, 12 Jan 2024 12:06:52 -0500 Received: from zproxy2.enst.fr ([2001:660:330f:2::dd]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rOKzY-000502-4G; Fri, 12 Jan 2024 12:06:51 -0500 Received: from localhost (localhost [IPv6:::1]) by zproxy2.enst.fr (Postfix) with ESMTP id 04B5880671; Fri, 12 Jan 2024 18:06:42 +0100 (CET) Received: from zproxy2.enst.fr ([IPv6:::1]) by localhost (zproxy2.enst.fr [IPv6:::1]) (amavis, port 10032) with ESMTP id LmHRbGSOLoUQ; Fri, 12 Jan 2024 18:06:41 +0100 (CET) Received: from localhost (localhost [IPv6:::1]) by zproxy2.enst.fr (Postfix) with ESMTP id 66D758068A; Fri, 12 Jan 2024 18:06:41 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.10.3 zproxy2.enst.fr 66D758068A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telecom-paris.fr; s=A35C7578-1106-11E5-A17F-C303FDDA8F2E; t=1705079201; bh=cn7ApSHhwczTz1se066/yK/Z00PDt222NUbyzKhmpJw=; h=From:To:Date:Message-ID:MIME-Version; b=gLXSl3OdGGgOfxW/CYqSHdUb0GWdXBY+G32xgAGyGeh8jKUBkshdf0mAXnO3onsT8 TGtWlskt7Nko7HjYAh2birWrbEnVJUxtox9eqMedWU4bWm/zvsA1ZzhEVlxJKY6ULz QSi2jyy5ZAUNpDCbTZA522YPowhKu3KkoY3pNp6Q= X-Virus-Scanned: amavis at enst.fr Received: from zproxy2.enst.fr ([IPv6:::1]) by localhost (zproxy2.enst.fr [IPv6:::1]) (amavis, port 10026) with ESMTP id CSsiRtljtgRT; Fri, 12 Jan 2024 18:06:41 +0100 (CET) Received: from inesv-Inspiron-3501.enst.fr (unknown [IPv6:2a04:8ec0:0:240:fa97:9da:79c1:e167]) by zproxy2.enst.fr (Postfix) with ESMTPSA id 30A7A806B7; Fri, 12 Jan 2024 18:06:41 +0100 (CET) From: =?utf-8?q?In=C3=A8s_Varhol?= To: qemu-devel@nongnu.org Cc: Samuel Tardieu , Arnaud Minier , =?utf-8?q?In=C3=A8s_Varhol?= , Thomas Huth , Peter Maydell , qemu-arm@nongnu.org, Paolo Bonzini , Alistair Francis , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Laurent Vivier Subject: [RFC 2/3] hw/arm: Connect STM32L4x5 GPIO to STM32L4x5 SoC Date: Fri, 12 Jan 2024 18:05:39 +0100 Message-ID: <20240112170635.303226-3-ines.varhol@telecom-paris.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240112170635.303226-1-ines.varhol@telecom-paris.fr> References: <20240112170635.303226-1-ines.varhol@telecom-paris.fr> MIME-Version: 1.0 Received-SPF: pass client-ip=2001:660:330f:2::dd; envelope-from=ines.varhol@telecom-paris.fr; helo=zproxy2.enst.fr X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Arnaud Minier Signed-off-by: Inès Varhol --- hw/arm/Kconfig | 3 +- hw/arm/stm32l4x5_soc.c | 62 +++++++++++++++++++++++++++------- include/hw/arm/stm32l4x5_soc.h | 9 +++++ 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig index bb4693bfbb..cb05147b64 100644 --- a/hw/arm/Kconfig +++ b/hw/arm/Kconfig @@ -459,8 +459,9 @@ config STM32L4X5_SOC bool select ARM_V7M select OR_IRQ - select STM32L4X5_SYSCFG select STM32L4X5_EXTI + select STM32L4X5_SYSCFG + select STM32L4X5_GPIO config XLNX_ZYNQMP_ARM bool diff --git a/hw/arm/stm32l4x5_soc.c b/hw/arm/stm32l4x5_soc.c index 431f982caf..56a9a6affb 100644 --- a/hw/arm/stm32l4x5_soc.c +++ b/hw/arm/stm32l4x5_soc.c @@ -76,6 +76,17 @@ static const int exti_irq[NUM_EXTI_IRQ] = { 78 /* LCD wakeup, Direct */ }; +static const uint32_t gpio_addr[] = { + 0x48000000, + 0x48000400, + 0x48000800, + 0x48000C00, + 0x48001000, + 0x48001400, + 0x48001800, + 0x48001C00, +}; + static void stm32l4x5_soc_initfn(Object *obj) { Stm32l4x5SocState *s = STM32L4X5_SOC(obj); @@ -83,6 +94,15 @@ static void stm32l4x5_soc_initfn(Object *obj) object_initialize_child(obj, "exti", &s->exti, TYPE_STM32L4X5_EXTI); object_initialize_child(obj, "syscfg", &s->syscfg, TYPE_STM32L4X5_SYSCFG); + object_initialize_child(obj, "gpioa", &s->gpioa, TYPE_STM32L4X5_GPIO_A); + object_initialize_child(obj, "gpiob", &s->gpiob, TYPE_STM32L4X5_GPIO_B); + object_initialize_child(obj, "gpioc", &s->gpioc, TYPE_STM32L4X5_GPIO_C); + object_initialize_child(obj, "gpiod", &s->gpiod, TYPE_STM32L4X5_GPIO_D); + object_initialize_child(obj, "gpioe", &s->gpioe, TYPE_STM32L4X5_GPIO_E); + object_initialize_child(obj, "gpiof", &s->gpiof, TYPE_STM32L4X5_GPIO_F); + object_initialize_child(obj, "gpiog", &s->gpiog, TYPE_STM32L4X5_GPIO_G); + object_initialize_child(obj, "gpioh", &s->gpioh, TYPE_STM32L4X5_GPIO_H); + s->sysclk = qdev_init_clock_in(DEVICE(s), "sysclk", NULL, NULL, 0); s->refclk = qdev_init_clock_in(DEVICE(s), "refclk", NULL, NULL, 0); } @@ -95,6 +115,7 @@ static void stm32l4x5_soc_realize(DeviceState *dev_soc, Error **errp) MemoryRegion *system_memory = get_system_memory(); DeviceState *armv7m; SysBusDevice *busdev; + uint32_t pin_index; /* * We use s->refclk internally and only define it with qdev_init_clock_in() @@ -156,17 +177,40 @@ static void stm32l4x5_soc_realize(DeviceState *dev_soc, Error **errp) return; } + /* GPIOs */ + const Stm32l4x5GpioState *gpios[] = { + &s->gpioa, + &s->gpiob, + &s->gpioc, + &s->gpiod, + &s->gpioe, + &s->gpiof, + &s->gpiog, + &s->gpioh, + }; + for (unsigned i = 0; i < NUM_GPIOS; i++) { + busdev = SYS_BUS_DEVICE(gpios[i]); + if (!sysbus_realize(busdev, errp)) { + return; + } + sysbus_mmio_map(busdev, 0, gpio_addr[i]); + } + /* System configuration controller */ busdev = SYS_BUS_DEVICE(&s->syscfg); if (!sysbus_realize(busdev, errp)) { return; } sysbus_mmio_map(busdev, 0, SYSCFG_ADDR); - /* - * TODO: when the GPIO device is implemented, connect it - * to SYCFG using `qdev_connect_gpio_out`, NUM_GPIOS and - * GPIO_NUM_PINS. - */ + + for (unsigned i = 0; i < NUM_GPIOS; i++) { + for (unsigned j = 0; j < GPIO_NUM_PINS; j++) { + pin_index = GPIO_NUM_PINS * i + j; + qdev_connect_gpio_out(DEVICE(gpios[i]), j, + qdev_get_gpio_in(DEVICE(&s->syscfg), + pin_index)); + } + } /* EXTI device */ busdev = SYS_BUS_DEVICE(&s->exti); @@ -256,14 +300,6 @@ static void stm32l4x5_soc_realize(DeviceState *dev_soc, Error **errp) /* RESERVED: 0x40024400, 0x7FDBC00 */ /* AHB2 BUS */ - create_unimplemented_device("GPIOA", 0x48000000, 0x400); - create_unimplemented_device("GPIOB", 0x48000400, 0x400); - create_unimplemented_device("GPIOC", 0x48000800, 0x400); - create_unimplemented_device("GPIOD", 0x48000C00, 0x400); - create_unimplemented_device("GPIOE", 0x48001000, 0x400); - create_unimplemented_device("GPIOF", 0x48001400, 0x400); - create_unimplemented_device("GPIOG", 0x48001800, 0x400); - create_unimplemented_device("GPIOH", 0x48001C00, 0x400); /* RESERVED: 0x48002000, 0x7FDBC00 */ create_unimplemented_device("OTG_FS", 0x50000000, 0x40000); create_unimplemented_device("ADC", 0x50040000, 0x400); diff --git a/include/hw/arm/stm32l4x5_soc.h b/include/hw/arm/stm32l4x5_soc.h index baf70410b5..c8aff07b6d 100644 --- a/include/hw/arm/stm32l4x5_soc.h +++ b/include/hw/arm/stm32l4x5_soc.h @@ -28,6 +28,7 @@ #include "hw/arm/armv7m.h" #include "hw/misc/stm32l4x5_syscfg.h" #include "hw/misc/stm32l4x5_exti.h" +#include "hw/gpio/stm32l4x5_gpio.h" #include "qom/object.h" #define TYPE_STM32L4X5_SOC "stm32l4x5-soc" @@ -43,6 +44,14 @@ struct Stm32l4x5SocState { Stm32l4x5ExtiState exti; Stm32l4x5SyscfgState syscfg; + Stm32l4x5GpioState gpioa; + Stm32l4x5GpioState gpiob; + Stm32l4x5GpioState gpioc; + Stm32l4x5GpioState gpiod; + Stm32l4x5GpioState gpioe; + Stm32l4x5GpioState gpiof; + Stm32l4x5GpioState gpiog; + Stm32l4x5GpioState gpioh; MemoryRegion sram1; MemoryRegion sram2; From patchwork Fri Jan 12 17:05:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?In=C3=A8s_Varhol?= X-Patchwork-Id: 1886190 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=telecom-paris.fr header.i=@telecom-paris.fr header.a=rsa-sha256 header.s=A35C7578-1106-11E5-A17F-C303FDDA8F2E header.b=FrVp735u; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TBSfD67hsz1yPf for ; Sat, 13 Jan 2024 04:08:04 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rOKzd-0005Jt-Hz; Fri, 12 Jan 2024 12:06:53 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rOKzb-0005IN-MZ; Fri, 12 Jan 2024 12:06:51 -0500 Received: from zproxy2.enst.fr ([137.194.2.221]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rOKzY-000506-41; Fri, 12 Jan 2024 12:06:51 -0500 Received: from localhost (localhost [IPv6:::1]) by zproxy2.enst.fr (Postfix) with ESMTP id 5C5AD80691; Fri, 12 Jan 2024 18:06:42 +0100 (CET) Received: from zproxy2.enst.fr ([IPv6:::1]) by localhost (zproxy2.enst.fr [IPv6:::1]) (amavis, port 10032) with ESMTP id i98td2gUyzt1; Fri, 12 Jan 2024 18:06:41 +0100 (CET) Received: from localhost (localhost [IPv6:::1]) by zproxy2.enst.fr (Postfix) with ESMTP id 9D45980689; Fri, 12 Jan 2024 18:06:41 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.10.3 zproxy2.enst.fr 9D45980689 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telecom-paris.fr; s=A35C7578-1106-11E5-A17F-C303FDDA8F2E; t=1705079201; bh=P6WJopXc7gLArim+Ur0KHnmkk8r4TzVNbo6tKdriGlU=; h=From:To:Date:Message-ID:MIME-Version; b=FrVp735u4n9WOdavWXQfcFV8o7gZbss0HMLHWc8cKoaHCnqzWfAxdUlfj+ax+KiYO wzM2jNB+c9RnsMv4rJqu74SUJhDF3zUD9A1FWHOM53pHjm0gXZL449ZY7H2Vn08keW nb2ipYRz1M6CVKZZVsXHxlR4UNerCeuitqgEWM+o= X-Virus-Scanned: amavis at enst.fr Received: from zproxy2.enst.fr ([IPv6:::1]) by localhost (zproxy2.enst.fr [IPv6:::1]) (amavis, port 10026) with ESMTP id DJX_QNfrwGhR; Fri, 12 Jan 2024 18:06:41 +0100 (CET) Received: from inesv-Inspiron-3501.enst.fr (unknown [IPv6:2a04:8ec0:0:240:fa97:9da:79c1:e167]) by zproxy2.enst.fr (Postfix) with ESMTPSA id 5411980671; Fri, 12 Jan 2024 18:06:41 +0100 (CET) From: =?utf-8?q?In=C3=A8s_Varhol?= To: qemu-devel@nongnu.org Cc: Samuel Tardieu , Arnaud Minier , =?utf-8?q?In=C3=A8s_Varhol?= , Thomas Huth , Peter Maydell , qemu-arm@nongnu.org, Paolo Bonzini , Alistair Francis , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Laurent Vivier Subject: [RFC 3/3] tests/qtest: Add STM32L4x5 GPIO QTest testcase Date: Fri, 12 Jan 2024 18:05:40 +0100 Message-ID: <20240112170635.303226-4-ines.varhol@telecom-paris.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240112170635.303226-1-ines.varhol@telecom-paris.fr> References: <20240112170635.303226-1-ines.varhol@telecom-paris.fr> MIME-Version: 1.0 Received-SPF: pass client-ip=137.194.2.221; envelope-from=ines.varhol@telecom-paris.fr; helo=zproxy2.enst.fr X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Arnaud Minier Signed-off-by: Inès Varhol Acked-by: Thomas Huth --- tests/qtest/meson.build | 3 +- tests/qtest/stm32l4x5_gpio-test.c | 319 ++++++++++++++++++++++++++++++ 2 files changed, 321 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/stm32l4x5_gpio-test.c diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index a926af92f6..a80552c012 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -197,7 +197,8 @@ qtests_aspeed = \ qtests_stm32l4x5 = \ ['stm32l4x5_exti-test', - 'stm32l4x5_syscfg-test'] + 'stm32l4x5_syscfg-test', + 'stm32l4x5_gpio-test'] qtests_arm = \ (config_all_devices.has_key('CONFIG_MPS2') ? ['sse-timer-test'] : []) + \ diff --git a/tests/qtest/stm32l4x5_gpio-test.c b/tests/qtest/stm32l4x5_gpio-test.c new file mode 100644 index 0000000000..189083bc4f --- /dev/null +++ b/tests/qtest/stm32l4x5_gpio-test.c @@ -0,0 +1,319 @@ +/* + * QTest testcase for STM32L4x5_EXTI + * + * Copyright (c) 2023 Arnaud Minier + * Copyright (c) 2023 Inès Varhol + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "libqtest-single.h" + +#define GPIO_A_BASE_ADDR 0x48000000 +#define GPIO_B_BASE_ADDR 0x48000400 +#define GPIO_C_BASE_ADDR 0x48000800 +#define GPIO_D_BASE_ADDR 0x48000C00 +#define GPIO_E_BASE_ADDR 0x48001000 +#define GPIO_F_BASE_ADDR 0x48001400 +#define GPIO_G_BASE_ADDR 0x48001800 +#define GPIO_H_BASE_ADDR 0x48001C00 +#define GPIO_MODER 0x00 +#define GPIO_OTYPER 0x04 +#define GPIO_OSPEEDR 0x08 +#define GPIO_PUPDR 0x0C +#define GPIO_IDR 0x10 +#define GPIO_ODR 0x14 +#define GPIO_BSRR 0x18 +#define GPIO_LCKR 0x1C +#define GPIO_AFRL 0x20 +#define GPIO_AFRH 0x24 +#define GPIO_BRR 0x28 +#define GPIO_ASCR 0x2C +#define GPIO_DISCONNECTED_PINS 0x30 + +static uint32_t gpio_a_readl(unsigned int offset) +{ + return readl(GPIO_A_BASE_ADDR + offset); +} + +static uint32_t gpio_b_readl(unsigned int offset) +{ + return readl(GPIO_B_BASE_ADDR + offset); +} + +static uint32_t gpio_c_readl(unsigned int offset) +{ + return readl(GPIO_C_BASE_ADDR + offset); +} + +static uint32_t gpio_h_readl(unsigned int offset) +{ + return readl(GPIO_H_BASE_ADDR + offset); +} + +static void gpio_a_writel(unsigned int offset, uint32_t value) +{ + writel(GPIO_A_BASE_ADDR + offset, value); +} + +static void gpio_a_set_irq(int num, int level) +{ + qtest_set_irq_in(global_qtest, "/machine/soc/gpioa", + NULL, num, level); +} + +static void test_idr_reset_value(void) +{ + /* + * Check that IDR register as well as other registers + * determining its value have the exepcted value + * GPIOA->IDR value after reset is not identical + * to the real one ad Alternate Functions aren't + * implemented + */ + uint32_t moder = gpio_a_readl(GPIO_MODER); + uint32_t odr = gpio_a_readl(GPIO_ODR); + uint32_t otyper = gpio_a_readl(GPIO_OTYPER); + uint32_t pupdr = gpio_a_readl(GPIO_PUPDR); + uint32_t idr = gpio_a_readl(GPIO_IDR); + /* 15: AF, 14: AF, 13: AF, 12: Analog ... */ + /* here AF is the same as Analog */ + g_assert_cmpint(moder, ==, 0xABFFFFFF); + g_assert_cmpint(odr, ==, 0x00000000); + g_assert_cmpint(otyper, ==, 0x00000000); + /* 15: pull-up, 14: pull-down, 13: pull-up, 12: neither ... */ + g_assert_cmpint(pupdr, ==, 0x64000000); + /* 15 : 1, 14: 0, 13: 1, 12 : reset value ... */ + g_assert_cmpint(idr, ==, 0x0000A000); + + moder = gpio_b_readl(GPIO_MODER); + odr = gpio_b_readl(GPIO_ODR); + otyper = gpio_b_readl(GPIO_OTYPER); + pupdr = gpio_b_readl(GPIO_PUPDR); + idr = gpio_b_readl(GPIO_IDR); + /* ... 5: Analog, 4: AF, 3: AF, 2: Analog ... */ + /* here AF is the same as Analog */ + g_assert_cmpint(moder, ==, 0xFFFFFEBF); + g_assert_cmpint(odr, ==, 0x00000000); + g_assert_cmpint(otyper, ==, 0x00000000); + /* ... 5: neither, 4: pull-up, 3: neither ... */ + g_assert_cmpint(pupdr, ==, 0x00000100); + /* ... 5 : reset value, 4 : 1, 3 : reset value ... */ + g_assert_cmpint(idr, ==, 0x00000010); + + moder = gpio_c_readl(GPIO_MODER); + odr = gpio_c_readl(GPIO_ODR); + otyper = gpio_c_readl(GPIO_OTYPER); + pupdr = gpio_c_readl(GPIO_PUPDR); + idr = gpio_c_readl(GPIO_IDR); + /* Analog */ + g_assert_cmpint(moder, ==, 0xFFFFFFFF); + g_assert_cmpint(odr, ==, 0x00000000); + g_assert_cmpint(otyper, ==, 0x00000000); + /* no pull-up or pull-down */ + g_assert_cmpint(pupdr, ==, 0x00000000); + /* reset value */ + g_assert_cmpint(idr, ==, 0x00000000); + + moder = gpio_h_readl(GPIO_MODER); + odr = gpio_h_readl(GPIO_ODR); + otyper = gpio_h_readl(GPIO_OTYPER); + pupdr = gpio_h_readl(GPIO_PUPDR); + idr = gpio_h_readl(GPIO_IDR); + /* Analog */ + g_assert_cmpint(moder, ==, 0x0000000F); + g_assert_cmpint(odr, ==, 0x00000000); + g_assert_cmpint(otyper, ==, 0x00000000); + /* no pull-up or pull-down */ + g_assert_cmpint(pupdr, ==, 0x00000000); + /* reset value */ + g_assert_cmpint(idr, ==, 0x00000000); + +} + +static void test_gpio_output_mode(void) +{ + /* + * Test that setting and resetting a bit in ODR sends signal + * to SYSCFG when this bit is configured in output mode + * (even if output mode if configured after the bit in ODR is set) + */ + qtest_irq_intercept_in(global_qtest, "/machine/soc/syscfg"); + + /* Set bit 0 in ODR */ + gpio_a_writel(GPIO_ODR, 0x00000001); + + /* Check that IDR wasn't updated */ + g_assert_cmpuint(gpio_a_readl(GPIO_IDR), ==, 0x0000A000); + + /* Check no signal was transmitted to syscfg */ + g_assert_false(get_irq(0)); + + /* Configure GPIOA line 0 as output */ + gpio_a_writel(GPIO_MODER, 0xABFFFFFD); + + /* Check that IDR was updated */ + g_assert_cmpuint(gpio_a_readl(GPIO_IDR), ==, 0x0000A001); + + /* Check that the signal was transmitted to syscfg */ + g_assert_true(get_irq(0)); + + /* Clean the test */ + gpio_a_writel(GPIO_ODR, 0x00000000); + gpio_a_writel(GPIO_MODER, 0xABFFFFFF); +} + +static void test_gpio_input_mode(void) +{ + /* + * Test that configuring a line in input mode allows to send + * a signal to SYSCFG when raising and lowering the line + */ + qtest_irq_intercept_in(global_qtest, + "/machine/soc/syscfg"); + + /* Configure GPIOA line 0 as input */ + gpio_a_writel(GPIO_MODER, 0x00000000); + + /* Raise line 0 */ + gpio_a_set_irq(0, 1); + + /* Check that IDR was updated */ + g_assert_cmpuint(gpio_a_readl(GPIO_IDR), ==, 0x0000A001); + + /* Check that the signal was transmitted to syscfg */ + g_assert_true(get_irq(0)); + + /* Lower line 0 */ + gpio_a_set_irq(0, 0); + + /* Check that IDR was updated */ + g_assert_cmpuint(gpio_a_readl(GPIO_IDR), ==, 0x0000A000); + + /* Check that the signal was transmitted to syscfg */ + g_assert_false(get_irq(0)); + + /* Clean the test */ + gpio_a_writel(GPIO_ODR, 0x00000000); + gpio_a_writel(GPIO_MODER, 0xABFFFFFF); + gpio_a_writel(GPIO_DISCONNECTED_PINS, 0xFFFF); +} + +static void test_pull_up_pull_down(void) +{ + /* + * Test that configuring a line in input mode allows to send + * a signal to SYSCFG just by changing pull-up and pull-down + */ + qtest_irq_intercept_in(global_qtest, + "/machine/soc/syscfg"); + + /* Configure GPIOA line 0 as input */ + gpio_a_writel(GPIO_MODER, 0x00000000); + + /* Configure pull-up for GPIOA line 0 */ + gpio_a_writel(GPIO_PUPDR, 0x00000001); + + /* Check that IDR was updated */ + g_assert_cmpuint(gpio_a_readl(GPIO_IDR), ==, 0x0000A001); + + /* Check that the signal was transmitted to syscfg */ + g_assert_true(get_irq(0)); + + /* Configure pull-down for GPIOA line 0 */ + gpio_a_writel(GPIO_PUPDR, 0x00000002); + + /* Check that IDR was updated */ + g_assert_cmpuint(gpio_a_readl(GPIO_IDR), ==, 0x0000A000); + + /* Check that the signal was transmitted to syscfg */ + g_assert_false(get_irq(0)); + + /* Clean the test */ + gpio_a_writel(GPIO_ODR, 0x00000000); + gpio_a_writel(GPIO_MODER, 0xABFFFFFF); +} + +static void test_no_short_circuit(void) +{ + /* + * Test that configuring a line in output mode + * disconnects the pin, that the pin can't be set or reset + * in push-pull mode, and that it can only be reset + * in open-drain mode + */ + qtest_irq_intercept_in(global_qtest, + "/machine/soc/syscfg"); + + gpio_a_set_irq(0, 1); + + /* Configuring pin 0 in push-pull output mode */ + gpio_a_writel(GPIO_MODER, 0x00000001); + + /* Checking that the pin is disconnected */ + g_assert_cmpuint(gpio_a_readl(GPIO_DISCONNECTED_PINS), ==, 0xFFFF); + + /* Checking that IDR was updated accordingly */ + g_assert_cmpuint(gpio_a_readl(GPIO_IDR), ==, 0x0000A000); + + /* Trying to set and reset the pin and checking it doesn't work */ + gpio_a_set_irq(0, 1); + + g_assert_cmpuint(gpio_a_readl(GPIO_DISCONNECTED_PINS), ==, 0xFFFF); + + g_assert_cmpuint(gpio_a_readl(GPIO_IDR), ==, 0x0000A000); + + gpio_a_writel(GPIO_ODR, 0x00000001); + + g_assert_cmpuint(gpio_a_readl(GPIO_IDR), ==, 0x0000A001); + + gpio_a_set_irq(0, 0); + + g_assert_cmpuint(gpio_a_readl(GPIO_DISCONNECTED_PINS), ==, 0xFFFF); + + g_assert_cmpuint(gpio_a_readl(GPIO_IDR), ==, 0x0000A001); + + /* Configuring pin 0 in open-drain output mode */ + gpio_a_writel(GPIO_OTYPER, 0x00000001); + + /* Trying to set the pin and checking it doesn't work */ + gpio_a_set_irq(0, 1); + + g_assert_cmpuint(gpio_a_readl(GPIO_DISCONNECTED_PINS), ==, 0xFFFF); + + /* Resetting the pin and checking it works */ + gpio_a_set_irq(0, 0); + + g_assert_cmpuint(gpio_a_readl(GPIO_DISCONNECTED_PINS), ==, 0xFFFE); + + g_assert_cmpuint(gpio_a_readl(GPIO_IDR), ==, 0x0000A000); + + /* Cleaning the test */ + gpio_a_writel(GPIO_DISCONNECTED_PINS, 0xFFFF); +} + +int main(int argc, char **argv) +{ + int ret; + + g_test_init(&argc, &argv, NULL); + g_test_set_nonfatal_assertions(); + qtest_add_func("stm32l4x5/gpio/test_idr_reset_value", + test_idr_reset_value); + qtest_add_func("stm32l4x5/gpio/test_gpio_output_mode", + test_gpio_output_mode); + qtest_add_func("stm32l4x5/gpio/test_gpio_input_mode", + test_gpio_input_mode); + qtest_add_func("stm32l4x5/gpio/test_pull_up_pull_down", + test_pull_up_pull_down); + qtest_add_func("stm32l4x5/gpio/test_no_short_circuit", + test_no_short_circuit); + + qtest_start("-machine b-l475e-iot01a"); + ret = g_test_run(); + qtest_end(); + + return ret; +}