#include <dt-bindings/pinctrl/stm32-pinfunc.h>
#include <dt-bindings/clock/stm32mp1-clksrc.h>
#include <dt-bindings/soc/st,stm32-etzpc.h>
#include "stm32mp15-mx.dtsi"
#include "stm32mp157.dtsi"
#include "stm32mp15xf.dtsi"
#include "stm32mp15xxaa-pinctrl.dtsi"
#include "stm32mp15-ddr.dtsi"
/dts-v1/;

/* USER CODE BEGIN includes */
#include <dt-bindings/power/stm32mp1-power.h>
/* USER CODE END includes */

/{
	model = "STMicroelectronics STM32MP157F-EV1 STM32CubeMX board";
	compatible = "st,stm32mp157f-board_157f-ev1-mx", "st,stm32mp157";

	/* USER CODE BEGIN root */

	memory@c0000000{
		device_type = "memory";
		reg = <0xC0000000 0x40000000>;
	};

	vin:vin{
		compatible = "regulator-fixed";
		regulator-name = "vin";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		regulator-always-on;
	};

	chosen{
		stdout-path = "serial0:115200n8";
	};

	aliases{
		serial0 = &uart4;
		serial1 = &usart3;
	};
	/* USER CODE END root */

	clocks{

		clk_lse:clk-lse{
			st,drive = < LSEDRV_MEDIUM_HIGH >;
		};

		clk_hse:clk-hse{
			st,digbypass;
		};
	};
};

&pinctrl{

	fmc_pins_mx:fmc_mx-0{

		pins1{
			pinmux = <STM32_PINMUX('D', 0, AF12)>, <STM32_PINMUX('D', 1, AF12)>, <STM32_PINMUX('D', 4, AF12)>, <STM32_PINMUX('D', 5, AF12)>, <STM32_PINMUX('D', 11, AF12)>, <STM32_PINMUX('D', 12, AF12)>, <STM32_PINMUX('D', 14, AF12)>, <STM32_PINMUX('D', 15, AF12)>, <STM32_PINMUX('E', 7, AF12)>, <STM32_PINMUX('E', 8, AF12)>, <STM32_PINMUX('E', 9, AF12)>, <STM32_PINMUX('E', 10, AF12)>, <STM32_PINMUX('G', 9, AF12)>;
			bias-disable;
			drive-push-pull;
			slew-rate = <1>;
		};

		pins2{
			pinmux = <STM32_PINMUX('D', 6, AF12)>;
			bias-pull-up;
		};
	};

	quadspi_pins_mx:quadspi_mx-0{

		pins1{
			pinmux = <STM32_PINMUX('B', 6, AF10)>, <STM32_PINMUX('C', 0, AF10)>;
			bias-pull-up;
			drive-push-pull;
			slew-rate = <1>;
		};

		pins2{
			pinmux = <STM32_PINMUX('F', 10, AF9)>;
			bias-disable;
			drive-push-pull;
			slew-rate = <3>;
		};

		pins3{
			pinmux = <STM32_PINMUX('F', 6, AF9)>, <STM32_PINMUX('F', 7, AF9)>, <STM32_PINMUX('F', 8, AF10)>, <STM32_PINMUX('F', 9, AF10)>, <STM32_PINMUX('G', 7, AF11)>, <STM32_PINMUX('G', 10, AF11)>, <STM32_PINMUX('H', 2, AF9)>, <STM32_PINMUX('H', 3, AF9)>;
			bias-disable;
			drive-push-pull;
			slew-rate = <1>;
		};
	};

	sdmmc1_pins_mx:sdmmc1_mx-0{

		pins1{
			pinmux = <STM32_PINMUX('B', 9, AF11)>, <STM32_PINMUX('C', 7, AF8)>, <STM32_PINMUX('F', 2, AF11)>;
			bias-pull-up;
			drive-push-pull;
			slew-rate = <1>;
		};

		pins2{
			pinmux = <STM32_PINMUX('C', 8, AF12)>, <STM32_PINMUX('C', 9, AF12)>, <STM32_PINMUX('C', 10, AF12)>, <STM32_PINMUX('C', 11, AF12)>, <STM32_PINMUX('D', 2, AF12)>;
			bias-disable;
			drive-push-pull;
			slew-rate = <1>;
		};

		pins3{
			pinmux = <STM32_PINMUX('C', 12, AF12)>;
			bias-disable;
			drive-push-pull;
			slew-rate = <2>;
		};

		pins4{
			pinmux = <STM32_PINMUX('E', 4, AF8)>;
			bias-pull-up;
		};
	};

	sdmmc2_pins_mx:sdmmc2_mx-0{

		pins1{
			pinmux = <STM32_PINMUX('A', 8, AF9)>, <STM32_PINMUX('A', 9, AF10)>, <STM32_PINMUX('B', 3, AF9)>, <STM32_PINMUX('B', 4, AF9)>, <STM32_PINMUX('B', 14, AF9)>, <STM32_PINMUX('B', 15, AF9)>, <STM32_PINMUX('D', 3, AF9)>, <STM32_PINMUX('E', 5, AF9)>, <STM32_PINMUX('G', 6, AF10)>;
			bias-pull-up;
			drive-push-pull;
			slew-rate = <1>;
		};

		pins2{
			pinmux = <STM32_PINMUX('E', 3, AF9)>;
			bias-pull-up;
			drive-push-pull;
			slew-rate = <2>;
		};
	};

	uart4_pins_mx:uart4_mx-0{

		pins1{
			pinmux = <STM32_PINMUX('B', 2, AF8)>;
			bias-disable;
		};

		pins2{
			pinmux = <STM32_PINMUX('G', 11, AF6)>;
			bias-disable;
			drive-push-pull;
			slew-rate = <0>;
		};
	};

	usb_otg_hs_pins_mx:usb_otg_hs_mx-0{

		pins{
			pinmux = <STM32_PINMUX('A', 10, ANALOG)>;
		};
	};
};

&pinctrl_z{

	i2c4_pins_z_mx:i2c4_mx-0{

		pins{
			pinmux = <STM32_PINMUX('Z', 4, AF6)>, <STM32_PINMUX('Z', 5, AF6)>;
			bias-disable;
			drive-open-drain;
			slew-rate = <0>;
		};
	};
};

&rcc{
	st,csi-cal;
	st,hsi-cal;
	st,cal-sec = <60>;
	st,clksrc = <
		CLK_MPU_PLL1P
		CLK_AXI_PLL2P
		CLK_MCU_PLL3P
		CLK_PLL12_HSE
		CLK_PLL3_HSE
		CLK_PLL4_HSE
		CLK_RTC_LSE
		CLK_MCO1_DISABLED
		CLK_MCO2_DISABLED
	>;
	st,clkdiv = <
		1 		
		0 		
		0 		
		1 		
		1 		
		1 		
		1 		
		2 		
		23 		
		0 		
		0 		
	>;
	st,pkcs = <
		CLK_CKPER_DISABLED
		CLK_FMC_ACLK
		CLK_QSPI_ACLK
		CLK_ETH_PLL4P
		CLK_SDMMC12_PLL4P
		CLK_DSI_DSIPLL
		CLK_STGEN_HSE
		CLK_USBPHY_HSE
		CLK_SPI2S1_DISABLED
		CLK_SPI2S23_DISABLED
		CLK_SPI45_DISABLED
		CLK_SPI6_DISABLED
		CLK_I2C46_HSI
		CLK_SDMMC3_DISABLED
		CLK_USBO_USBPHY
		CLK_ADC_DISABLED
		CLK_CEC_LSE
		CLK_I2C12_HSI
		CLK_I2C35_PCLK1
		CLK_UART1_DISABLED
		CLK_UART24_HSI
		CLK_UART35_DISABLED
		CLK_UART6_DISABLED
		CLK_UART78_DISABLED
		CLK_SPDIF_PLL4P
		CLK_FDCAN_PLL4R
		CLK_SAI1_PLL3Q
		CLK_SAI2_PLL3Q
		CLK_SAI3_DISABLED
		CLK_SAI4_PLL3Q
		CLK_RNG1_LSI
		CLK_LPTIM1_DISABLED
		CLK_LPTIM23_DISABLED
		CLK_LPTIM45_DISABLED
	>;
	status = "okay";
	secure-status = "okay";

	pll2:st,pll@1{
		compatible = "st,stm32mp1-pll";
		reg = <1>;
		cfg = < 2 65 1 0 0 PQR(1,1,1) >;
		frac = < 0x1400 >;
	};

	pll3:st,pll@2{
		compatible = "st,stm32mp1-pll";
		reg = <2>;
		cfg = < 1 33 1 16 36 PQR(1,1,0) >;
		frac = < 0x1a04 >;
	};

	pll4:st,pll@3{
		compatible = "st,stm32mp1-pll";
		reg = <3>;
		cfg = < 3 98 5 7 7 PQR(1,1,1) >;
	};
};

&bsec{
	status = "okay";
	secure-status = "okay";

	/* USER CODE BEGIN bsec */

	board_id:board_id@ec{
		reg = <0xec 0x4>;
		st,non-secure-otp;
	};
	/* USER CODE END bsec */
};

&cryp1{
	status = "okay";
};

&etzpc{
	st,decprot = <
	
	DECPROT(STM32MP1_ETZPC_CRYP1_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_DCMI_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_DFSDM_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_DMA1_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_DMA2_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_DMAMUX_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_ETH_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_TT_FDCAN_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_FMC_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_CEC_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_I2C2_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_I2C5_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_QSPI_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_DLYBQ_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_SAI2_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_SAI4_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_SPDIFRX_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_TIM6_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_UART4_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_OTG_ID, DECPROT_NS_RW, DECPROT_UNLOCK)
	
	DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_S_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_S_RW, DECPROT_UNLOCK)
	DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_UNLOCK)

	/* USER CODE BEGIN etzpc_decprot */
		/*STM32CubeMX generates a basic and standard configuration for ETZPC.
		Additional device configurations can be added here if needed.
		"etzpc" node could be also overloaded in "addons" User-Section.*/

	DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_UNLOCK)

	/* USER CODE END etzpc_decprot */		
	>;
	secure-status = "okay";
};

&fmc{
	pinctrl-names = "default";
	pinctrl-0 = <&fmc_pins_mx>;
	status = "okay";

	/* USER CODE BEGIN fmc */

	nand-controller@4,0{
		status = "okay";

		nand@0{
			reg = <0>;
			nand-on-flash-bbt;
			#address-cells = <1>;
			#size-cells = <1>;
		};
	};
	/* USER CODE END fmc */
};

&hash1{
	status = "okay";
};

&i2c4{
	pinctrl-names = "default";
	pinctrl-0 = <&i2c4_pins_z_mx>;
	status = "okay";
	secure-status = "okay";

	/* USER CODE BEGIN i2c4 */
	i2c-scl-rising-time-ns = <185>;
	i2c-scl-falling-time-ns = <20>;
	clock-frequency = <400000>;

	pmic:stpmic@33{
		compatible = "st,stpmic1";
		reg = <0x33>;
		interrupts-extended = <&exti_pwr 55 IRQ_TYPE_EDGE_FALLING>;
		interrupt-controller;
		#interrupt-cells = <2>;
		status = "okay";
		secure-status = "okay";

		regulators{
			compatible = "st,stpmic1-regulators";
			buck1-supply = <&vin>;
			buck2-supply = <&vin>;
			buck3-supply = <&vin>;
			buck4-supply = <&vin>;
			ldo1-supply = <&v3v3>;
			ldo2-supply = <&v3v3>;
			ldo3-supply = <&vdd_ddr>;
			ldo4-supply = <&vin>;
			ldo5-supply = <&v3v3>;
			ldo6-supply = <&v3v3>;
			vref_ddr-supply = <&vin>;
			boost-supply = <&vin>;
			pwr_sw1-supply = <&bst_out>;
			pwr_sw2-supply = <&bst_out>;

			vddcore:buck1{
				regulator-name = "vddcore";
				regulator-min-microvolt = <1200000>;
				regulator-max-microvolt = <1350000>;
				regulator-always-on;
				regulator-initial-mode = <0>;
				regulator-over-current-protection;

				lp-stop{
					regulator-on-in-suspend;
					regulator-suspend-microvolt = <1200000>;
				};

				lplv-stop{
					regulator-on-in-suspend;
					regulator-suspend-microvolt = <900000>;
				};

				standby-ddr-sr{
					regulator-off-in-suspend;
				};

				standby-ddr-off{
					regulator-off-in-suspend;
				};
			};

			vdd_ddr:buck2{
				regulator-name = "vdd_ddr";
				regulator-min-microvolt = <1350000>;
				regulator-max-microvolt = <1350000>;
				regulator-always-on;
				regulator-initial-mode = <0>;
				regulator-over-current-protection;

				lp-stop{
					regulator-suspend-microvolt = <1350000>;
					regulator-on-in-suspend;
				};

				lplv-stop{
					regulator-suspend-microvolt = <1350000>;
					regulator-on-in-suspend;
				};

				standby-ddr-sr{
					regulator-suspend-microvolt = <1350000>;
					regulator-on-in-suspend;
				};

				standby-ddr-off{
					regulator-off-in-suspend;
				};
			};

			vdd:buck3{
				regulator-name = "vdd";
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-always-on;
				st,mask-reset;
				regulator-initial-mode = <0>;
				regulator-over-current-protection;

				lp-stop{
					regulator-suspend-microvolt = <3300000>;
					regulator-on-in-suspend;
				};

				lplv-stop{
					regulator-suspend-microvolt = <3300000>;
					regulator-on-in-suspend;
				};

				standby-ddr-sr{
					regulator-suspend-microvolt = <3300000>;
					regulator-on-in-suspend;
				};

				standby-ddr-off{
					regulator-suspend-microvolt = <3300000>;
					regulator-on-in-suspend;
				};
			};

			v3v3:buck4{
				regulator-name = "v3v3";
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-always-on;
				regulator-over-current-protection;
				regulator-initial-mode = <0>;

				standby-ddr-sr{
					regulator-off-in-suspend;
				};

				standby-ddr-off{
					regulator-off-in-suspend;
				};
			};

			vdda:ldo1{
				regulator-name = "vdda";
				regulator-min-microvolt = <2900000>;
				regulator-max-microvolt = <2900000>;

				standby-ddr-sr{
					regulator-off-in-suspend;
				};

				standby-ddr-off{
					regulator-off-in-suspend;
				};
			};

			v2v8:ldo2{
				regulator-name = "v2v8";
				regulator-min-microvolt = <2800000>;
				regulator-max-microvolt = <2800000>;

				standby-ddr-sr{
					regulator-off-in-suspend;
				};

				standby-ddr-off{
					regulator-off-in-suspend;
				};
			};

			vtt_ddr:ldo3{
				regulator-name = "vtt_ddr";
				regulator-min-microvolt = <500000>;
				regulator-max-microvolt = <750000>;
				regulator-always-on;
				regulator-over-current-protection;

				lp-stop{
					regulator-off-in-suspend;
				};

				lplv-stop{
					regulator-off-in-suspend;
				};

				standby-ddr-sr{
					regulator-off-in-suspend;
				};

				standby-ddr-off{
					regulator-off-in-suspend;
				};
			};

			vdd_usb:ldo4{
				regulator-name = "vdd_usb";
				regulator-min-microvolt = <3300000>;
				regulator-max-microvolt = <3300000>;
				regulator-always-on;

				standby-ddr-sr{
					regulator-off-in-suspend;
				};

				standby-ddr-off{
					regulator-off-in-suspend;
				};
			};

			vdd_sd:ldo5{
				regulator-name = "vdd_sd";
				regulator-min-microvolt = <2900000>;
				regulator-max-microvolt = <2900000>;
				regulator-boot-on;

				standby-ddr-sr{
					regulator-off-in-suspend;
				};

				standby-ddr-off{
					regulator-off-in-suspend;
				};
			};

			v1v8:ldo6{
				regulator-name = "v1v8";
				regulator-min-microvolt = <1800000>;
				regulator-max-microvolt = <1800000>;
				regulator-enable-ramp-delay = <300000>;

				standby-ddr-sr{
					regulator-off-in-suspend;
				};

				standby-ddr-off{
					regulator-off-in-suspend;
				};
			};

			vref_ddr:vref_ddr{
				regulator-name = "vref_ddr";
				regulator-always-on;
				regulator-over-current-protection;

				lp-stop{
					regulator-on-in-suspend;
				};

				lplv-stop{
					regulator-on-in-suspend;
				};

				standby-ddr-sr{
					regulator-on-in-suspend;
				};

				standby-ddr-off{
					regulator-off-in-suspend;
				};
			};

			bst_out:boost{
				regulator-name = "bst_out";
			};

			vbus_otg:pwr_sw1{
				regulator-name = "vbus_otg";
			};

			vbus_sw:pwr_sw2{
				regulator-name = "vbus_sw";
				regulator-active-discharge = <1>;
			};
		};
	};
	/* USER CODE END i2c4 */
};

&iwdg2{
	status = "okay";
	secure-status = "okay";

	/* USER CODE BEGIN iwdg2 */
	timeout-sec = <32>;
	/* USER CODE END iwdg2 */
};

&pwr_regulators{
	status = "okay";
	secure-status = "okay";

	/* USER CODE BEGIN pwr_regulators */
	system_suspend_supported_soc_modes = <
		STM32_PM_CSLEEP_RUN
		STM32_PM_CSTOP_ALLOW_LP_STOP
		STM32_PM_CSTOP_ALLOW_LPLV_STOP
		STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR
	>;
	system_off_soc_mode = <STM32_PM_CSTOP_ALLOW_STANDBY_DDR_OFF>;
	vdd-supply = <&vdd>;
	vdd_3v3_usbfs-supply = <&vdd_usb>;
	/* USER CODE END pwr_regulators */
};

&qspi{
	pinctrl-names = "default";
	pinctrl-0 = <&quadspi_pins_mx>;
	status = "okay";

	/* USER CODE BEGIN qspi */
	reg = <0x58003000 0x1000>, <0x70000000 0x4000000>;
	#address-cells = <1>;
	#size-cells = <0>;

	flash0:mx66l51235l@0{
		compatible = "jedec,spi-nor";
		reg = <0>;
		spi-rx-bus-width = <4>;
		spi-max-frequency = <108000000>;
		#address-cells = <1>;
		#size-cells = <1>;
	};
	/* USER CODE END qspi */
};

&rng1{
	status = "okay";
	secure-status = "okay";
};

&rtc{
	status = "okay";
	secure-status = "okay";
};

&sdmmc1{
	pinctrl-names = "default";
	pinctrl-0 = <&sdmmc1_pins_mx>;
	status = "okay";

	/* USER CODE BEGIN sdmmc1 */
	disable-wp;
	st,sig-dir;
	st,neg-edge;
	st,use-ckin;
	bus-width = <4>;
	vmmc-supply = <&vdd_sd>;
	sd-uhs-sdr12;
	sd-uhs-sdr25;
	sd-uhs-sdr50;
	sd-uhs-ddr50;
	sd-uhs-sdr104;
	/* USER CODE END sdmmc1 */
};

&sdmmc2{
	pinctrl-names = "default";
	pinctrl-0 = <&sdmmc2_pins_mx>;
	status = "okay";

	/* USER CODE BEGIN sdmmc2 */
	non-removable;
	no-sd;
	no-sdio;
	st,neg-edge;
	bus-width = <8>;
	vmmc-supply = <&v3v3>;
	vqmmc-supply = <&vdd>;
	mmc-ddr-3_3v;
	/* USER CODE END sdmmc2 */
};

&tamp{
	status = "okay";
	secure-status = "okay";
};

&timers15{
	secure-status = "okay";

	/* USER CODE BEGIN timers15 */
	st,hsi-cal-input = <7>;
	st,csi-cal-input = <8>;
	/* USER CODE END timers15 */
};

&uart4{
	pinctrl-names = "default";
	pinctrl-0 = <&uart4_pins_mx>;
	status = "okay";
};

&usbotg_hs{
	pinctrl-names = "default";
	pinctrl-0 = <&usb_otg_hs_pins_mx>;
	status = "okay";

	/* USER CODE BEGIN usbotg_hs */
	vbus-supply = <&vbus_otg>;
	phys = <&usbphyc_port1 0>;
	phy-names = "usb2-phy";
	/* USER CODE END usbotg_hs */
};

&usbphyc{
	status = "okay";
};

&usbphyc_port0{
	status = "okay";

	/* USER CODE BEGIN usbphyc_port0 */
	phy-supply = <&vdd_usb>;
	/* USER CODE END usbphyc_port0 */
};

&usbphyc_port1{
	status = "okay";

	/* USER CODE BEGIN usbphyc_port1 */
	phy-supply = <&vdd_usb>;
	/* USER CODE END usbphyc_port1 */
};

/* USER CODE BEGIN addons */

&cpu0{
	cpu-supply = <&vddcore>;
};

&nvmem_layout{
	nvmem-cells = <&cfg0_otp>, <&part_number_otp>, <&monotonic_otp>, <&nand_otp>, <&uid_otp>, <&package_otp>, <&hw2_otp>, <&pkh_otp>, <&board_id>;
	nvmem-cell-names = "cfg0_otp", "part_number_otp", "monotonic_otp", "nand_otp", "uid_otp", "package_otp", "hw2_otp", "pkh_otp", "board_id";
};

&cpu1{
	cpu-supply = <&vddcore>;
};
/* USER CODE END addons */
