This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 414b4914adfa06f3a777c0b2b1a8bb76b2b204ba Author: raiden00pl <[email protected]> AuthorDate: Thu Nov 19 13:42:36 2026 +0100 arch/arm/stm32n6: Add USB host support. USB1 or USB2 OTG HS runs the common OTG v2 host driver at full and low speed on its embedded HS PHY, which stm32_otg_phyif.c powers and clocks. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: raiden00pl <[email protected]> --- Documentation/guides/chip-specific/stm32_ports.rst | 1 + Documentation/platforms/arm/stm32n6/index.rst | 4 +- arch/arm/src/common/stm32/Kconfig.have | 10 ++ arch/arm/src/common/stm32/Kconfig.usb | 19 +++- arch/arm/src/common/stm32/hardware/stm32_otg_v2.h | 2 + arch/arm/src/common/stm32/stm32_otg_v2.h | 15 ++- arch/arm/src/common/stm32/stm32_otghost_v2.c | 63 +++++++++--- arch/arm/src/stm32h7/Kconfig | 1 + arch/arm/src/stm32h7/stm32_otg_phyif.h | 17 ++++ arch/arm/src/stm32n6/CMakeLists.txt | 4 + arch/arm/src/stm32n6/Kconfig | 20 ++++ arch/arm/src/stm32n6/Make.defs | 4 + arch/arm/src/stm32n6/hardware/stm32n6xxx_rifsc.h | 2 + arch/arm/src/stm32n6/hardware/stm32n6xxx_usbphyc.h | 68 +++++++++++++ arch/arm/src/stm32n6/stm32_otg_phyif.c | 109 +++++++++++++++++++++ arch/arm/src/stm32n6/stm32_otg_phyif.h | 99 +++++++++++++++++++ arch/arm/src/stm32n6/stm32_rif.c | 12 +++ arch/arm/src/stm32n6/stm32n6xx_rcc.c | 10 ++ 18 files changed, 433 insertions(+), 27 deletions(-) diff --git a/Documentation/guides/chip-specific/stm32_ports.rst b/Documentation/guides/chip-specific/stm32_ports.rst index c4ddaa22fc2..0ca9ed61f63 100644 --- a/Documentation/guides/chip-specific/stm32_ports.rst +++ b/Documentation/guides/chip-specific/stm32_ports.rst @@ -790,6 +790,7 @@ SPI/I2S v5 arch/arm/src/common/stm32/stm32_spi_v5.c TIM v3 arch/arm/src/common/stm32/stm32_tim.c USART/LPUART M33M55 v3 arch/arm/src/common/stm32/stm32_serial_m33m55_v3.c USB device not supported +USB host OTG v2 arch/arm/src/common/stm32/stm32_otghost_v2.c WWDG v3 arch/arm/src/common/stm32/stm32_wwdg.c XSPI to be done not supported ============ ============ ================================================== diff --git a/Documentation/platforms/arm/stm32n6/index.rst b/Documentation/platforms/arm/stm32n6/index.rst index 75da5de92ee..23bbe3ba859 100644 --- a/Documentation/platforms/arm/stm32n6/index.rst +++ b/Documentation/platforms/arm/stm32n6/index.rst @@ -46,6 +46,8 @@ SPI Yes SPI1-6 through the common SPI v5 driver, master mode, optional GPDMA. USART Yes USART1-3, UART4-5, USART6, UART7-9, USART10 and LPUART1 through the common U(S)ART driver. +USB Yes USB1 or USB2 OTG HS host through the common OTG v2 + driver, full and low speed on the embedded HS PHY. ADC No DCMIPP No @@ -55,7 +57,7 @@ MPU No NPU No SAI No TIM No -USB No +USB device No XSPI No ========== ======= =============================================== diff --git a/arch/arm/src/common/stm32/Kconfig.have b/arch/arm/src/common/stm32/Kconfig.have index a18bed42484..9bc0909aed7 100644 --- a/arch/arm/src/common/stm32/Kconfig.have +++ b/arch/arm/src/common/stm32/Kconfig.have @@ -1270,6 +1270,16 @@ config STM32_HAVE_IP_OTGHS_M3M4_V1 config STM32_HAVE_IP_OTG_V2 bool +config STM32_HAVE_OTGHS_USBDEV + bool + ---help--- + The OTG v2 driver supports the OTG HS device role (STM32H7). + +config STM32_HAVE_OTGHS_HOST + bool + ---help--- + The OTG v2 driver supports the OTG HS host role (STM32N6). + config STM32_HAVE_COMMON_USBHOST_DEBUG bool default y if STM32_COMMON_LEGACY diff --git a/arch/arm/src/common/stm32/Kconfig.usb b/arch/arm/src/common/stm32/Kconfig.usb index 7f4c969114e..c47cc224f6b 100644 --- a/arch/arm/src/common/stm32/Kconfig.usb +++ b/arch/arm/src/common/stm32/Kconfig.usb @@ -114,10 +114,10 @@ endchoice # "ULPI Selection" endmenu # OTG_HS Configuration menu "OTG Configuration" - depends on ARCH_CHIP_STM32H7 && (STM32_OTGFS || STM32_OTGHS) + depends on STM32_HAVE_IP_OTG_V2 && (STM32_OTGFS || STM32_OTGHS) choice - prompt "STM32H7 OTGFS role" + prompt "OTGFS role" depends on STM32_OTGFS default STM32_OTGFS_USBDEV if USBDEV default STM32_OTGFS_HOST if !USBDEV && USBHOST @@ -130,18 +130,27 @@ config STM32_OTGFS_HOST bool "OTGFS as HOST" depends on USBHOST -endchoice # "STM32H7 OTGFS role" +endchoice # "OTGFS role" choice - prompt "STM32H7 OTGHS role (only USBDEV supported for now)" + prompt "OTGHS role" depends on STM32_OTGHS default STM32_OTGHS_USBDEV if USBDEV + default STM32_OTGHS_HOST if !USBDEV && USBHOST config STM32_OTGHS_USBDEV bool "OTGHS as USBDEV" depends on USBDEV + depends on STM32_HAVE_OTGHS_USBDEV -endchoice # "STM32H7 OTGHS role" +config STM32_OTGHS_HOST + bool "OTGHS as HOST" + depends on USBHOST + depends on STM32_HAVE_OTGHS_HOST + ---help--- + The host driver runs the OTG HS core at full and low speed. + +endchoice # "OTGHS role" endmenu # OTG Configuration diff --git a/arch/arm/src/common/stm32/hardware/stm32_otg_v2.h b/arch/arm/src/common/stm32/hardware/stm32_otg_v2.h index 0353981f53c..ad40d791ac3 100644 --- a/arch/arm/src/common/stm32/hardware/stm32_otg_v2.h +++ b/arch/arm/src/common/stm32/hardware/stm32_otg_v2.h @@ -457,6 +457,7 @@ #define OTG_GCCFG_PDEN (1 << 19) /* Bit 19: Primary detection (PD) mode enable */ #define OTG_GCCFG_SDEN (1 << 20) /* Bit 20: Secondary detection (SD) mode enable */ #define OTG_GCCFG_VBDEN (1 << 21) /* Bit 21: USB VBUS detection enable */ +#define OTG_GCCFG_PULLDOWNEN (1 << 25) /* Bit 25: Host pull-down enable (embedded HS PHY) */ /* Bits 22-31: Reserved */ /* TODO: OTG core LPM configuration register */ @@ -482,6 +483,7 @@ #define OTG_HCFG_FSLSPCS_SHIFT (0) /* Bits 0-1: FS/LS PHY clock select */ #define OTG_HCFG_FSLSPCS_MASK (3 << OTG_HCFG_FSLSPCS_SHIFT) +# define OTG_HCFG_FSLSPCS_HS3060MHz (0 << OTG_HCFG_FSLSPCS_SHIFT) /* PHY clock is running at 30/60 MHz (UTMI+ PHY) */ # define OTG_HCFG_FSLSPCS_FS48MHz (1 << OTG_HCFG_FSLSPCS_SHIFT) /* FS host mode, PHY clock is running at 48 MHz */ # define OTG_HCFG_FSLSPCS_LS48MHz (1 << OTG_HCFG_FSLSPCS_SHIFT) /* LS host mode, Select 48 MHz PHY clock frequency */ # define OTG_HCFG_FSLSPCS_LS6MHz (2 << OTG_HCFG_FSLSPCS_SHIFT) /* LS host mode, Select 6 MHz PHY clock frequency */ diff --git a/arch/arm/src/common/stm32/stm32_otg_v2.h b/arch/arm/src/common/stm32/stm32_otg_v2.h index 045d4489e32..fab8c65bcea 100644 --- a/arch/arm/src/common/stm32/stm32_otg_v2.h +++ b/arch/arm/src/common/stm32/stm32_otg_v2.h @@ -39,12 +39,6 @@ #if defined(CONFIG_STM32_OTGFS) || defined(CONFIG_STM32_OTGHS) -#if (STM32_RCC_D2CCIP2R_USBSRC == RCC_D2CCIP2R_USBSEL_HSI48) && \ - !defined(CONFIG_STM32_HSI48) -# error board.h selected HSI48 as USB clock source, but HSI48 is not \ - enabled. Enable STM32_HSI48 -#endif - #if defined(CONFIG_STM32_OTGHS) && !defined(CONFIG_STM32_OTGHS_FS) && \ defined(CONFIG_STM32_OTGHS_NO_ULPI) # error OTG HS selected but no ULPI enabled @@ -79,10 +73,11 @@ extern "C" #endif /**************************************************************************** - * Name: stm32_otgfshost_initialize + * Name: stm32_otgfshost_initialize / stm32_otghshost_initialize * * Description: - * Initialize USB host device controller hardware. + * Initialize USB host device controller hardware of the OTG FS or the + * OTG HS instance, as selected by the host role. * * Input Parameters: * controller -- If the device supports more than USB host controller, then @@ -105,7 +100,11 @@ extern "C" #ifdef CONFIG_USBHOST struct usbhost_connection_s; +# ifdef CONFIG_STM32_OTGHS_HOST +struct usbhost_connection_s *stm32_otghshost_initialize(int controller); +# else struct usbhost_connection_s *stm32_otgfshost_initialize(int controller); +# endif #endif /**************************************************************************** diff --git a/arch/arm/src/common/stm32/stm32_otghost_v2.c b/arch/arm/src/common/stm32/stm32_otghost_v2.c index 946817e263e..27681f0a823 100644 --- a/arch/arm/src/common/stm32/stm32_otghost_v2.c +++ b/arch/arm/src/common/stm32/stm32_otghost_v2.c @@ -60,7 +60,8 @@ #include "stm32_otg_phyif.h" #include "stm32_usbhost.h" -#if defined(CONFIG_USBHOST) && defined(CONFIG_STM32_OTGFS) +#if defined(CONFIG_USBHOST) && \ + (defined(CONFIG_STM32_OTGFS) || defined(CONFIG_STM32_OTGHS)) /**************************************************************************** * Pre-processor Definitions @@ -77,7 +78,6 @@ # define GPIO_OTG_SOF GPIO_OTGFS_SOF # define STM32_OTG_FIFO_SIZE 4096 #elif defined(CONFIG_STM32_OTGHS_HOST) -# error OTGHS HOST role not supported yet # define STM32_IRQ_OTG STM32_IRQ_OTGHS # define STM32_OTG_BASE STM32_OTGHS_BASE # define GPIO_OTG_DM GPIO_OTGHS_DM @@ -86,7 +86,7 @@ # define GPIO_OTG_SOF GPIO_OTGHS_SOF # define STM32_OTG_FIFO_SIZE 4096 #else -# error Not selected USBDEV peripheral +# error No OTG host role selected #endif #if defined(CONFIG_STM32_OTGFS_HOST) && defined(CONFIG_STM32_OTGHS_HOST) @@ -101,7 +101,8 @@ * * CONFIG_USBHOST - Enable general USB host support * CONFIG_STM32_OTGFS - Enable the STM32 USB OTG FS block - * CONFIG_STM32_SYSCFG - Needed + * or + * CONFIG_STM32_OTGHS - Enable the STM32 USB OTG HS block * * Options: * @@ -122,10 +123,6 @@ /* Pre-requisites (partial) */ -#ifndef CONFIG_STM32_SYSCFG -# error "CONFIG_STM32_SYSCFG is required" -#endif - /* Default RxFIFO size */ #ifndef CONFIG_STM32_OTG_RXFIFO_SIZE @@ -3372,7 +3369,9 @@ static inline void stm32_gint_hprtisr(struct stm32_usbhost_s *priv) { uint32_t hprt; uint32_t newhprt; +#ifndef STM32_OTG_EMBEDDED_HSPHY uint32_t hcfg; +#endif usbhost_vtrace1(OTG_VTRACE1_GINT_HPRT, 0); @@ -3431,6 +3430,7 @@ static inline void stm32_gint_hprtisr(struct stm32_usbhost_s *priv) stm32_gint_connected(priv); +#ifndef STM32_OTG_EMBEDDED_HSPHY /* Check the Host ConFiGuration register (HCFG) */ hcfg = stm32_getreg(STM32_OTG_HCFG); @@ -3485,6 +3485,7 @@ static inline void stm32_gint_hprtisr(struct stm32_usbhost_s *priv) stm32_portreset(priv); } } +#endif } } @@ -5155,17 +5156,28 @@ static void stm32_host_initialize(struct stm32_usbhost_s *priv) regval = stm32_getreg(STM32_OTG_HCFG); regval &= ~OTG_HCFG_FSLSPCS_MASK; +#ifdef STM32_OTG_EMBEDDED_HSPHY + regval |= OTG_HCFG_FSLSPCS_HS3060MHz; +#else regval |= OTG_HCFG_FSLSPCS_FS48MHz; +#endif stm32_putreg(STM32_OTG_HCFG, regval); /* Reset the host port */ stm32_portreset(priv); - /* Clear the FS-/LS-only support bit in the HCFG register */ + /* The driver supports full and low speed only. The embedded HS PHY is + * limited to them with the FS-/LS-only support bit; otherwise the bit is + * cleared. + */ regval = stm32_getreg(STM32_OTG_HCFG); +#ifdef STM32_OTG_EMBEDDED_HSPHY + regval |= OTG_HCFG_FSLSS; +#else regval &= ~OTG_HCFG_FSLSS; +#endif stm32_putreg(STM32_OTG_HCFG, regval); /* Carve up FIFO memory for the Rx FIFO and the periodic and non-periodic @@ -5317,6 +5329,15 @@ static inline int stm32_hw_initialize(struct stm32_usbhost_s *priv) uint32_t regval; unsigned long timeout; +#ifdef STM32_OTG_EMBEDDED_HSPHY + /* Clear the PHYSEL bit in the GUSBCFG register to select the embedded + * high speed PHY on the UTMI+ interface. + */ + + regval = stm32_getreg(STM32_OTG_GUSBCFG); + regval &= ~OTG_GUSBCFG_PHYSEL; + stm32_putreg(STM32_OTG_GUSBCFG, regval); +#else /* Set the PHYSEL bit in the GUSBCFG register to select the OTG FS serial * transceiver: "This bit is always 1 with write-only access" */ @@ -5324,6 +5345,7 @@ static inline int stm32_hw_initialize(struct stm32_usbhost_s *priv) regval = stm32_getreg(STM32_OTG_GUSBCFG); regval |= OTG_GUSBCFG_PHYSEL; stm32_putreg(STM32_OTG_GUSBCFG, regval); +#endif /* Reset after a PHY select and set Host mode. First, wait for AHB master * IDLE state. @@ -5355,9 +5377,17 @@ static inline int stm32_hw_initialize(struct stm32_usbhost_s *priv) up_udelay(3); +#ifdef STM32_OTG_EMBEDDED_HSPHY + /* The embedded HS PHY is powered through its PHY controller. Enable the + * host pull-down resistors on DP and DM. + */ + + regval = OTG_GCCFG_PULLDOWNEN; +#else /* Deactivate the power down */ regval = (OTG_GCCFG_PWRDWN | OTG_GCCFG_VBDEN); +#endif stm32_putreg(STM32_OTG_GCCFG, regval); up_mdelay(20); @@ -5384,10 +5414,11 @@ static inline int stm32_hw_initialize(struct stm32_usbhost_s *priv) ****************************************************************************/ /**************************************************************************** - * Name: stm32_otgfshost_initialize + * Name: stm32_otgfshost_initialize / stm32_otghshost_initialize * * Description: - * Initialize USB host device controller hardware. + * Initialize USB host device controller hardware of the OTG FS or the + * OTG HS instance, as selected by the host role. * * Input Parameters: * controller -- If the device supports more than USB host controller, then @@ -5408,7 +5439,11 @@ static inline int stm32_hw_initialize(struct stm32_usbhost_s *priv) * ****************************************************************************/ +#ifdef CONFIG_STM32_OTGHS_HOST +struct usbhost_connection_s *stm32_otghshost_initialize(int controller) +#else struct usbhost_connection_s *stm32_otgfshost_initialize(int controller) +#endif { /* At present, there is only support for a single OTG FS host. Hence it is * pre-allocated as g_usbhost. However, in most code, the private data @@ -5462,14 +5497,16 @@ struct usbhost_connection_s *stm32_otgfshost_initialize(int controller) * *Pins may vary from device-to-device. */ +#ifndef STM32_OTG_EMBEDDED_HSPHY stm32_configgpio(GPIO_OTG_DM); stm32_configgpio(GPIO_OTG_DP); stm32_configgpio(GPIO_OTG_ID); /* Only needed for OTG */ /* SOF output pin configuration is configurable */ -#ifdef CONFIG_STM32_OTG_SOFOUTPUT +# ifdef CONFIG_STM32_OTG_SOFOUTPUT stm32_configgpio(GPIO_OTG_SOF); +# endif #endif /* Initialize the USB OTG FS core */ @@ -5494,4 +5531,4 @@ struct usbhost_connection_s *stm32_otgfshost_initialize(int controller) return &g_usbconn; } -#endif /* CONFIG_USBHOST && CONFIG_STM32_OTGFS */ +#endif /* CONFIG_USBHOST && (CONFIG_STM32_OTGFS || CONFIG_STM32_OTGHS) */ diff --git a/arch/arm/src/stm32h7/Kconfig b/arch/arm/src/stm32h7/Kconfig index c6160f7763c..b5be2e59e49 100644 --- a/arch/arm/src/stm32h7/Kconfig +++ b/arch/arm/src/stm32h7/Kconfig @@ -110,6 +110,7 @@ config STM32_H7_PERIPHERALS select STM32_HAVE_OTG_H7 select STM32_HAVE_OTGHS select STM32_HAVE_IP_OTG_V2 + select STM32_HAVE_OTGHS_USBDEV select STM32_HAVE_USART_RXFIFO_THRESHOLD select STM32_HAVE_FDCAN_H7 select STM32_HAVE_ETHERNET diff --git a/arch/arm/src/stm32h7/stm32_otg_phyif.h b/arch/arm/src/stm32h7/stm32_otg_phyif.h index 9093a7fb8d1..5332a3e6195 100644 --- a/arch/arm/src/stm32h7/stm32_otg_phyif.h +++ b/arch/arm/src/stm32h7/stm32_otg_phyif.h @@ -29,8 +29,25 @@ #include <nuttx/config.h> +#include <arch/board/board.h> + #include "arm_internal.h" #include "hardware/stm32_pwr.h" +#include "stm32_rcc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#if (STM32_RCC_D2CCIP2R_USBSRC == RCC_D2CCIP2R_USBSEL_HSI48) && \ + !defined(CONFIG_STM32_HSI48) +# error board.h selected HSI48 as USB clock source, but HSI48 is not \ + enabled. Enable STM32_HSI48 +#endif + +#if defined(CONFIG_USBHOST) && !defined(CONFIG_STM32_SYSCFG) +# error "CONFIG_STM32_SYSCFG is required" +#endif /**************************************************************************** * Inline Functions diff --git a/arch/arm/src/stm32n6/CMakeLists.txt b/arch/arm/src/stm32n6/CMakeLists.txt index 5f6a7adb7a9..c7aa8b96868 100644 --- a/arch/arm/src/stm32n6/CMakeLists.txt +++ b/arch/arm/src/stm32n6/CMakeLists.txt @@ -39,6 +39,10 @@ if(NOT CONFIG_ARCH_IDLE_CUSTOM) list(APPEND SRCS stm32_idle.c) endif() +if(CONFIG_STM32_OTGHS) + list(APPEND SRCS stm32_otg_phyif.c) +endif() + # Chip-specific RCC if(CONFIG_STM32_STM32N6XXXX) diff --git a/arch/arm/src/stm32n6/Kconfig b/arch/arm/src/stm32n6/Kconfig index 05433fb844d..5e1ba120b3d 100644 --- a/arch/arm/src/stm32n6/Kconfig +++ b/arch/arm/src/stm32n6/Kconfig @@ -41,6 +41,7 @@ config STM32_N6_PERIPHERALS select STM32_HAVE_IP_DBGMCU_V8 select STM32_HAVE_IP_LPTIM_V2 select STM32_HAVE_IP_LTDC_V2 + select STM32_HAVE_IP_OTG_V2 select STM32_HAVE_IP_RNG_V1 select STM32_HAVE_IP_RTCC_M33M55_V1 select STM32_HAVE_IP_SDMMC_V2 @@ -63,6 +64,8 @@ config STM32_N6_PERIPHERALS select STM32_HAVE_LPTIM4 select STM32_HAVE_LPTIM5 select STM32_HAVE_LTDC + select STM32_HAVE_OTGHS + select STM32_HAVE_OTGHS_HOST select STM32_HAVE_RNG select STM32_HAVE_RTC select STM32_HAVE_RTC_MAGIC @@ -131,6 +134,23 @@ config ARCH_CHIP_STM32N657X0 endchoice +choice + prompt "USB OTG HS instance" + default STM32N6_OTGHS_USB1 + depends on STM32_OTGHS + +config STM32N6_OTGHS_USB1 + bool "USB1" + ---help--- + Use USB1 OTG HS and its embedded high speed PHY. + +config STM32N6_OTGHS_USB2 + bool "USB2" + ---help--- + Use USB2 OTG HS and its embedded high speed PHY. + +endchoice + config STM32N6_DEBUG bool "Enable debugger access after boot" default n diff --git a/arch/arm/src/stm32n6/Make.defs b/arch/arm/src/stm32n6/Make.defs index dcbbc7b90df..d4338e1a13f 100644 --- a/arch/arm/src/stm32n6/Make.defs +++ b/arch/arm/src/stm32n6/Make.defs @@ -39,6 +39,10 @@ ifneq ($(CONFIG_ARCH_IDLE_CUSTOM),y) CHIP_CSRCS += stm32_idle.c endif +ifeq ($(CONFIG_STM32_OTGHS),y) +CHIP_CSRCS += stm32_otg_phyif.c +endif + # Chip-specific RCC CHIP_CSRCS += stm32n6xx_rcc.c diff --git a/arch/arm/src/stm32n6/hardware/stm32n6xxx_rifsc.h b/arch/arm/src/stm32n6/hardware/stm32n6xxx_rifsc.h index e9a9124d81b..12a936ff8d7 100644 --- a/arch/arm/src/stm32n6/hardware/stm32n6xxx_rifsc.h +++ b/arch/arm/src/stm32n6/hardware/stm32n6xxx_rifsc.h @@ -90,6 +90,8 @@ #define RIFSC_PERIPH_SDMMC1 (1 * 32 + 21) #define RIFSC_PERIPH_SDMMC2 (1 * 32 + 22) +#define RIFSC_PERIPH_OTG1HS (1 * 32 + 24) +#define RIFSC_PERIPH_OTG2HS (1 * 32 + 25) #define RIFSC_PERIPH_ETH1 (1 * 32 + 28) #define RIFSC_PERIPH_LTDC (3 * 32 + 6) #define RIFSC_PERIPH_LTDCL1 (3 * 32 + 7) diff --git a/arch/arm/src/stm32n6/hardware/stm32n6xxx_usbphyc.h b/arch/arm/src/stm32n6/hardware/stm32n6xxx_usbphyc.h new file mode 100644 index 00000000000..4a12fbcfae7 --- /dev/null +++ b/arch/arm/src/stm32n6/hardware/stm32n6xxx_usbphyc.h @@ -0,0 +1,68 @@ +/**************************************************************************** + * arch/arm/src/stm32n6/hardware/stm32n6xxx_usbphyc.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_ARM_SRC_STM32N6_HARDWARE_STM32N6XXX_USBPHYC_H +#define __ARCH_ARM_SRC_STM32N6_HARDWARE_STM32N6XXX_USBPHYC_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "hardware/stm32n6xxx_memorymap.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Register Offsets *********************************************************/ + +#define STM32_USBPHYC_CR_OFFSET 0x0000 /* USB HS PHY control register */ +#define STM32_USBPHYC_TRIM1CR_OFFSET 0x0004 /* USB HS PHY trimming 1 */ +#define STM32_USBPHYC_TRIM2CR_OFFSET 0x0008 /* USB HS PHY trimming 2 */ + +/* Register Addresses *******************************************************/ + +#define STM32_USB1_HS_PHYC_CR (STM32_USB1_HS_PHYC_BASE + \ + STM32_USBPHYC_CR_OFFSET) +#define STM32_USB2_HS_PHYC_CR (STM32_USB2_HS_PHYC_BASE + \ + STM32_USBPHYC_CR_OFFSET) + +/* Register Bitfield Definitions ********************************************/ + +/* USB HS PHY control register (USBPHYC_CR) */ + +#define USBPHYC_CR_RETENABLEN1 (1 << 0) /* Bit 0: Retention mode enable */ +#define USBPHYC_CR_AUTORSMENB1 (1 << 1) /* Bit 1: Auto-resume mode enable */ +#define USBPHYC_CR_CMN (1 << 2) /* Bit 2: Power down of analog + * blocks in Suspend and Sleep */ +#define USBPHYC_CR_FSEL_SHIFT (4) /* Bits 4-6: Reference clock + * 19.2, 20 or 24 MHz */ +#define USBPHYC_CR_FSEL_MASK (7 << USBPHYC_CR_FSEL_SHIFT) +# define USBPHYC_CR_FSEL_19_2MHZ (0 << USBPHYC_CR_FSEL_SHIFT) +# define USBPHYC_CR_FSEL_20MHZ (1 << USBPHYC_CR_FSEL_SHIFT) +# define USBPHYC_CR_FSEL_24MHZ (2 << USBPHYC_CR_FSEL_SHIFT) +#define USBPHYC_CR_OTGDISABLE0 (1 << 16) /* Bit 16: OTG block disable */ +#define USBPHYC_CR_DRVVBUS0 (1 << 17) /* Bit 17: Drive VBUS */ +#define USBPHYC_CR_SELOTGDBG (1 << 31) /* Bit 31: Select HS PHY debug + * port */ + +#endif /* __ARCH_ARM_SRC_STM32N6_HARDWARE_STM32N6XXX_USBPHYC_H */ diff --git a/arch/arm/src/stm32n6/stm32_otg_phyif.c b/arch/arm/src/stm32n6/stm32_otg_phyif.c new file mode 100644 index 00000000000..dd593ea1149 --- /dev/null +++ b/arch/arm/src/stm32n6/stm32_otg_phyif.c @@ -0,0 +1,109 @@ +/**************************************************************************** + * arch/arm/src/stm32n6/stm32_otg_phyif.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <stdint.h> + +#include <nuttx/arch.h> + +#include "arm_internal.h" +#include "stm32_otg_phyif.h" +#include "hardware/stm32n6xxx_pwr.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* VDD33USB is a board supply: give it up to 100 ms to be detected */ + +#define USB33RDY_TIMEOUT_US 100000 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_otg_phyinit + * + * Description: + * Power up and configure the embedded high speed PHY of the selected + * USB OTG HS instance. + * The OTG and PHY clocks are already enabled by the RCC set-up. The + * sequence follows the STM32CubeN6 USB examples: mark VDD33USB valid, + * hold the core and the PHY in reset, program the PHY reference clock in + * the PHY controller, then release the PHY and the core. + * + ****************************************************************************/ + +int stm32_otg_phyinit(void) +{ + uint32_t regval; + int timeout; + + /* Enable the VDD33USB monitor, wait until the supply is ready and mark it + * valid to remove the USB33 isolation. + */ + + modifyreg32(STM32_PWR_SVMCR3, 0, PWR_SVMCR3_USB33VMEN); + for (timeout = USB33RDY_TIMEOUT_US; + (getreg32(STM32_PWR_SVMCR3) & PWR_SVMCR3_USB33RDY) == 0; + timeout--) + { + if (timeout == 0) + { + return -ETIMEDOUT; + } + + up_udelay(1); + } + + modifyreg32(STM32_PWR_SVMCR3, 0, PWR_SVMCR3_USB33SV); + + /* Hold the core and the PHY in reset, release the PHY controller */ + + putreg32(STM32_OTGHS_RST | STM32_OTGHS_PHYRST | STM32_OTGHS_PHYCTLRST, + STM32_RCC_AHB5RSTSR); + putreg32(STM32_OTGHS_PHYCTLRST, STM32_RCC_AHB5RSTCR); + up_mdelay(1); + + /* Reference clock frequency, OTG block of the PHY disabled */ + + regval = getreg32(STM32_OTGHS_PHYC_CR); + regval &= ~USBPHYC_CR_FSEL_MASK; + regval |= STM32_OTGHS_PHY_FSEL | USBPHYC_CR_OTGDISABLE0 | USBPHYC_CR_CMN | + USBPHYC_CR_RETENABLEN1; + putreg32(regval, STM32_OTGHS_PHYC_CR); + + /* Release the PHY, then the core */ + + putreg32(STM32_OTGHS_PHYRST, STM32_RCC_AHB5RSTCR); + up_mdelay(1); + putreg32(STM32_OTGHS_RST, STM32_RCC_AHB5RSTCR); + + return OK; +} diff --git a/arch/arm/src/stm32n6/stm32_otg_phyif.h b/arch/arm/src/stm32n6/stm32_otg_phyif.h new file mode 100644 index 00000000000..db3a00f5e07 --- /dev/null +++ b/arch/arm/src/stm32n6/stm32_otg_phyif.h @@ -0,0 +1,99 @@ +/**************************************************************************** + * arch/arm/src/stm32n6/stm32_otg_phyif.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_ARM_SRC_STM32N6_STM32_OTG_PHYIF_H +#define __ARCH_ARM_SRC_STM32N6_STM32_OTG_PHYIF_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <arch/board/board.h> + +#include "hardware/stm32n6xxx_memorymap.h" +#include "hardware/stm32n6xxx_rcc.h" +#include "hardware/stm32n6xxx_usbphyc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The common OTG driver drives USB1 or USB2 OTG HS as its OTG HS instance. + * Each core is wired to its own embedded high speed PHY on the UTMI+ + * interface and has no alternate function pins. + */ + +#ifdef CONFIG_STM32N6_OTGHS_USB2 +# define STM32_OTGHS_BASE STM32_USB2_OTG_HS_BASE +# define STM32_IRQ_OTGHS STM32_IRQ_USB2_OTG_HS +# define STM32_OTGHS_PHYC_CR STM32_USB2_HS_PHYC_CR +# define STM32_OTGHS_RST RCC_AHB5RSTR_OTG2RST +# define STM32_OTGHS_PHYRST RCC_AHB5RSTR_OTGPHY2RST +# define STM32_OTGHS_PHYCTLRST RCC_AHB5RSTR_OTG2PHYCTLRST +# define STM32_OTGHS_PHY_FREQUENCY STM32_OTGPHY2_FREQUENCY +#else +# define STM32_OTGHS_BASE STM32_USB1_OTG_HS_BASE +# define STM32_IRQ_OTGHS STM32_IRQ_USB1_OTG_HS +# define STM32_OTGHS_PHYC_CR STM32_USB1_HS_PHYC_CR +# define STM32_OTGHS_RST RCC_AHB5RSTR_OTG1RST +# define STM32_OTGHS_PHYRST RCC_AHB5RSTR_OTGPHY1RST +# define STM32_OTGHS_PHYCTLRST RCC_AHB5RSTR_OTG1PHYCTLRST +# define STM32_OTGHS_PHY_FREQUENCY STM32_OTGPHY1_FREQUENCY +#endif + +#define STM32_OTG_EMBEDDED_HSPHY 1 + +/* The PHY reference clock comes from board.h (RCC CCIPR6 OTGPHYxSEL) */ + +#ifndef STM32_OTGHS_PHY_FREQUENCY +# error "board.h must provide STM32_OTGPHY1/2_FREQUENCY" +#elif STM32_OTGHS_PHY_FREQUENCY == 19200000 +# define STM32_OTGHS_PHY_FSEL USBPHYC_CR_FSEL_19_2MHZ +#elif STM32_OTGHS_PHY_FREQUENCY == 20000000 +# define STM32_OTGHS_PHY_FSEL USBPHYC_CR_FSEL_20MHZ +#elif STM32_OTGHS_PHY_FREQUENCY == 24000000 +# define STM32_OTGHS_PHY_FSEL USBPHYC_CR_FSEL_24MHZ +#else +# error "Unsupported OTG PHY reference clock (19.2, 20 or 24 MHz)" +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_otg_phyinit + * + * Description: + * Power up and configure the embedded high speed PHY of the selected + * USB OTG HS instance. + * + * Returned Value: + * OK on success, -ETIMEDOUT if VDD33USB does not become ready. + * + ****************************************************************************/ + +int stm32_otg_phyinit(void); + +#endif /* __ARCH_ARM_SRC_STM32N6_STM32_OTG_PHYIF_H */ diff --git a/arch/arm/src/stm32n6/stm32_rif.c b/arch/arm/src/stm32n6/stm32_rif.c index 16a607de9e8..d4ca686a7d1 100644 --- a/arch/arm/src/stm32n6/stm32_rif.c +++ b/arch/arm/src/stm32n6/stm32_rif.c @@ -111,4 +111,16 @@ void stm32_rifconfig(void) rif_setmaster(RIFSC_MASTER_SDMMC2, RIF_MASTER_CID1_SEC_PRIV); rif_setslave_secpriv(RIFSC_PERIPH_SDMMC2); #endif + +#if defined(CONFIG_STM32_OTGHS) && defined(CONFIG_STM32N6_OTGHS_USB2) + /* USB2 OTG HS is a bus master (its DMA mode is not used by the driver) */ + + rif_setmaster(RIFSC_MASTER_OTG2, RIF_MASTER_CID1_SEC_PRIV); + rif_setslave_secpriv(RIFSC_PERIPH_OTG2HS); +#elif defined(CONFIG_STM32_OTGHS) + /* USB1 OTG HS is a bus master (its DMA mode is not used by the driver) */ + + rif_setmaster(RIFSC_MASTER_OTG1, RIF_MASTER_CID1_SEC_PRIV); + rif_setslave_secpriv(RIFSC_PERIPH_OTG1HS); +#endif } diff --git a/arch/arm/src/stm32n6/stm32n6xx_rcc.c b/arch/arm/src/stm32n6/stm32n6xx_rcc.c index 164e97399f7..6d2bb21b5b9 100644 --- a/arch/arm/src/stm32n6/stm32n6xx_rcc.c +++ b/arch/arm/src/stm32n6/stm32n6xx_rcc.c @@ -333,6 +333,16 @@ static inline void rcc_enableahb5(void) RCC_AHB5ENR_ETH1TXEN | RCC_AHB5ENR_ETH1RXEN; #endif +#if defined(CONFIG_STM32_OTGHS) && defined(CONFIG_STM32N6_OTGHS_USB2) + /* USB2 OTG HS core and its embedded high speed PHY */ + + regval |= RCC_AHB5ENR_OTG2EN | RCC_AHB5ENR_OTGPHY2EN; +#elif defined(CONFIG_STM32_OTGHS) + /* USB1 OTG HS core and its embedded high speed PHY */ + + regval |= RCC_AHB5ENR_OTG1EN | RCC_AHB5ENR_OTGPHY1EN; +#endif + if (regval != 0) { putreg32(regval, STM32_RCC_AHB5ENSR);
