// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/*
 * Device Tree Source for the RZ/G2L Solidrun SoM
 *
 * Copyright 2023 SolidRun Ltd.
 * Copyright 2025 Josua Mayer <josua@solid-run.com>
 */

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/rzg2l-pinctrl.h>

/ {
	aliases {
		ethernet0 = &eth0;
		ethernet1 = &eth1;
		i2c0 = &i2c0;
		i2c1 = &i2c1;
		i2c2 = &i2c3;
		mmc0 = &sdhi0;
		mmc1 = &sdhi1;
		rtc0 = &pmic;
		serial0 = &scif0;
		serial1 = &scif1;
		serial2 = &scif2;
	};

	chosen {
		stdout-path = "serial0:115200n8";
	};

	memory@48000000 {
		/*
		 * Physical RAM starts at 0x40000000, but Renesas RZ/G2 BSPs
		 * reserve and hide the first 128MB for TrustZone by default.
		 *
		 * Minimum size for this SoM is 1GB, and the bootloader patches
		 * the dtb according to the actual RAM size and reservations.
		 */
		reg = <0x0 0x48000000 0x0 0x38000000>;
		device_type = "memory";
	};

	sdhi0_mux: mux-controller-0 {
		compatible = "gpio-mux";
		#mux-control-cells = <0>;
		#mux-state-cells = <1>;
		/*
		 * Mux switches SD0_DATA[0-3], SD0_CMD & SD0_CLK between
		 * on-SoM eMMC and board-to-board connector using one gpio:
		 * Low = connector, High = eMMC.
		 * Define active-low for logical mux state 0 = eMMC, 1 = connector.
		 */
		mux-gpios = <&pinctrl RZG2L_GPIO(22, 1) GPIO_ACTIVE_LOW>;
	};

	reg_pmic_buck1: regulator-pmic-buck1 {
		compatible = "regulator-fixed";
		regulator-name = "pmic-buck1";
		regulator-always-on;
		regulator-boot-on;
		regulator-max-microvolt = <1100000>;
		regulator-min-microvolt = <1100000>;
	};

	reg_pmic_buck3: regulator-pmic-buck3 {
		compatible = "regulator-fixed";
		regulator-name = "pmic-buck3";
		regulator-always-on;
		regulator-boot-on;
		regulator-max-microvolt = <1800000>;
		regulator-min-microvolt = <1800000>;
	};

	reg_pmic_buck4: regulator-pmic-buck4 {
		compatible = "regulator-fixed";
		regulator-name = "pmic-buck4";
		regulator-always-on;
		regulator-boot-on;
		regulator-max-microvolt = <3300000>;
		regulator-min-microvolt = <3300000>;
	};

	reg_pmic_ldo1: regulator-pmic-ldo1 {
		compatible = "regulator-gpio";
		regulator-name = "pmic-ldo1";
		gpios = <&pinctrl RZG2L_GPIO(39, 0) GPIO_ACTIVE_HIGH>;
		regulator-always-on;
		regulator-boot-on;
		regulator-max-microvolt = <3300000>;
		regulator-min-microvolt = <1800000>;
		states = <3300000 1>, <1800000 0>;
	};

	reg_pmic_ldo2: regulator-pmic-ldo2 {
		compatible = "regulator-fixed";
		regulator-name = "pmic-ldo2";
		/*
		 * This ldo can switch mmc host controller io voltage between
		 * 1.8V and 3.3V by assembly option of pull-up / pull-dow.
		 * Default assembly is 3.3V.
		 */
		regulator-min-microvolt = <3300000>;
		regulator-always-on;
		regulator-boot-on;
		regulator-max-microvolt = <3300000>;
	};

	reserved-memory {
		ranges;
		#address-cells = <2>;
		#size-cells = <2>;

		global_cma: linux,cma@58000000 {
			compatible = "shared-dma-pool";
			reg = <0x0 0x58000000 0x0 0x10000000>;
			reusable;
			linux,cma-default;
		};
	};

	sdhi1_pwrseq: sdhi1-pwrseq {
		compatible = "mmc-pwrseq-simple";
		reset-gpios = <&pinctrl RZG2L_GPIO(23, 1) GPIO_ACTIVE_LOW>;
	};

	/* 32.768kHz crystal */
	x2: x2-clock {
		compatible = "fixed-clock";
		#clock-cells = <0>;
		clock-frequency = <32768>;
	};
};

&ehci0 {
	status = "okay";
};

&ehci1 {
	status = "okay";
};

&eth0 {
	phy-handle = <&phy0>;
	pinctrl-0 = <&eth0_pins>;
	pinctrl-names = "default";
	/*
	 * ravb driver does not configure mac internal delays for RZ/G2L(C),
	 * instead delays are added by the MxL86110 phy driver.
	 */
	phy-mode = "rgmii-id";
	status = "okay";

	phy0: ethernet-phy@0 {
		reg = <0>;
		/* Level interrupts are typical for phy but not supported on RZ/G2L gpios. */
		interrupts-extended = <&pinctrl RZG2L_GPIO(27, 0) IRQ_TYPE_EDGE_FALLING>;
	};
};

&eth1 {
	phy-handle = <&phy1>;
	pinctrl-0 = <&eth1_pins>;
	pinctrl-names = "default";
	/*
	 * ravb driver does not configure mac internal delays for RZ/G2L(C),
	 * instead delays are added by the MxL86110 phy driver.
	 */
	phy-mode = "rgmii-id";
	status = "okay";

	phy1: ethernet-phy@4 {
		reg = <4>;
		/* Level interrupts are typical for phy but not supported on RZ/G2L gpios. */
		interrupts-extended = <&pinctrl RZG2L_GPIO(42, 4) IRQ_TYPE_EDGE_FALLING>;
	};
};

&extal_clk {
	clock-frequency = <24000000>;
};

&gpu {
	mali-supply = <&reg_pmic_buck1>;
};

&i2c0 {
	clock-frequency = <400000>;
	pinctrl-0 = <&i2c0_pins>;
	pinctrl-names = "default";
	status = "okay";
};

&i2c1 {
	clock-frequency = <400000>;
	pinctrl-0 = <&i2c1_pins>;
	pinctrl-names = "default";
	status = "okay";

	eeprom: eeprom@50 {
		compatible = "atmel,24c01";
		reg = <0x50>;
		pagesize = <16>;
	};
};

&i2c3 {
	clock-frequency = <400000>;
	pinctrl-0 = <&i2c3_pins>;
	pinctrl-names = "default";
	status = "okay";

	pmic: pmic@12 {
		compatible = "renesas,raa215300";
		reg = <0x12>, <0x6f>;
		reg-names = "main", "rtc";
		clocks = <&x2>;
		clock-names = "xin";
	};
};

&ohci0 {
	status = "okay";
};

&ohci1 {
	status = "okay";
};

&ostm1 {
	status = "okay";
};

&ostm2 {
	status = "okay";
};

&phyrst {
	status = "okay";
};

&pinctrl {
	eth0_pins: eth0 {
		pinmux = <RZG2L_PORT_PINMUX(28, 1, 1)>, /* ET0_LINKSTA */
			 <RZG2L_PORT_PINMUX(27, 1, 1)>, /* ET0_MDC */
			 <RZG2L_PORT_PINMUX(28, 0, 1)>, /* ET0_MDIO */
			 <RZG2L_PORT_PINMUX(20, 0, 1)>, /* ET0_TXC */
			 <RZG2L_PORT_PINMUX(20, 1, 1)>, /* ET0_TX_CTL */
			 <RZG2L_PORT_PINMUX(20, 2, 1)>, /* ET0_TXD0 */
			 <RZG2L_PORT_PINMUX(21, 0, 1)>, /* ET0_TXD1 */
			 <RZG2L_PORT_PINMUX(21, 1, 1)>, /* ET0_TXD2 */
			 <RZG2L_PORT_PINMUX(22, 0, 1)>, /* ET0_TXD3 */
			 <RZG2L_PORT_PINMUX(24, 0, 1)>, /* ET0_RXC */
			 <RZG2L_PORT_PINMUX(24, 1, 1)>, /* ET0_RX_CTL */
			 <RZG2L_PORT_PINMUX(25, 0, 1)>, /* ET0_RXD0 */
			 <RZG2L_PORT_PINMUX(25, 1, 1)>, /* ET0_RXD1 */
			 <RZG2L_PORT_PINMUX(26, 0, 1)>, /* ET0_RXD2 */
			 <RZG2L_PORT_PINMUX(26, 1, 1)>; /* ET0_RXD3 */
	};

	eth1_pins: eth1 {
		pinmux = <RZG2L_PORT_PINMUX(37, 2, 1)>, /* ET1_LINKSTA */
			 <RZG2L_PORT_PINMUX(37, 0, 1)>, /* ET1_MDC */
			 <RZG2L_PORT_PINMUX(37, 1, 1)>, /* ET1_MDIO */
			 <RZG2L_PORT_PINMUX(29, 0, 1)>, /* ET1_TXC */
			 <RZG2L_PORT_PINMUX(29, 1, 1)>, /* ET1_TX_CTL */
			 <RZG2L_PORT_PINMUX(30, 0, 1)>, /* ET1_TXD0 */
			 <RZG2L_PORT_PINMUX(30, 1, 1)>, /* ET1_TXD1 */
			 <RZG2L_PORT_PINMUX(31, 0, 1)>, /* ET1_TXD2 */
			 <RZG2L_PORT_PINMUX(31, 1, 1)>, /* ET1_TXD3 */
			 <RZG2L_PORT_PINMUX(33, 1, 1)>, /* ET1_RXC */
			 <RZG2L_PORT_PINMUX(34, 0, 1)>, /* ET1_RX_CTL */
			 <RZG2L_PORT_PINMUX(34, 1, 1)>, /* ET1_RXD0 */
			 <RZG2L_PORT_PINMUX(35, 0, 1)>, /* ET1_RXD1 */
			 <RZG2L_PORT_PINMUX(35, 1, 1)>, /* ET1_RXD2 */
			 <RZG2L_PORT_PINMUX(36, 0, 1)>; /* ET1_RXD3 */
	};

	i2c0_pins: i2c0 {
		input-enable;
		pins = "RIIC0_SDA", "RIIC0_SCL";
	};

	i2c1_pins: i2c1 {
		input-enable;
		pins = "RIIC1_SDA", "RIIC1_SCL";
	};

	i2c3_pins: i2c3 {
		pinmux = <RZG2L_PORT_PINMUX(18, 0, 3)>, /* RIIC3_SDA */
			 <RZG2L_PORT_PINMUX(18, 1, 3)>; /* RIIC3_SCL */
	};

	qspi0_pins: qspi0 {
		pins = "QSPI0_IO0", "QSPI0_IO1", "QSPI0_IO2", "QSPI0_IO3",
		       "QSPI0_SPCLK", "QSPI0_SSL";
		power-source = <1800>;
	};

	scif0_pins: scif0 {
		pinmux = <RZG2L_PORT_PINMUX(38, 0, 1)>, /* SCIF0_TXD */
			 <RZG2L_PORT_PINMUX(38, 1, 1)>; /* SCIF0_RXD */
	};

	scif2_pins: scif2 {
		pinmux = <RZG2L_PORT_PINMUX(48, 0, 1)>, /* SCIF2_TXD */
			 <RZG2L_PORT_PINMUX(48, 1, 1)>, /* SCIF2_RXD */
			 <RZG2L_PORT_PINMUX(48, 3, 1)>, /* SCIF2_CTS# */
			 <RZG2L_PORT_PINMUX(48, 4, 1)>; /* SCIF2_RTS# */
	};

	sdhi0_4bit_pins: sdhi0-4bit {
		sd0-ctrl-dat03 {
			pins = "SD0_DATA0", "SD0_DATA1", "SD0_DATA2", "SD0_DATA3",
			       "SD0_CLK", "SD0_CMD";
			power-source = <3300>;
		};

		/*
		 * Pins 4-7 are hard-wired to eMMC with 1.8V IO voltage,
		 * with all pins sharing a single voltage domain.
		 *
		 * These pins are fixed-function lacking independently configurable
		 * pull-up, pull-down or output-disable.
		 *
		 * While unused, disable the input buffers to avoid sampling the still
		 * physically connected but unused eMMC lines.
		 * For output direction rely on correct controller bus-width configuration
		 * to avoid over-voltage to eMMC e.g. when operating at 3.3V for a 4-bit microSD.
		 */
		sd0-dat47 {
			input-disable;
			pins = "SD0_DATA4", "SD0_DATA5", "SD0_DATA6", "SD0_DATA7";
		};
	};

	sdhi0_4bit_uhs_pins: sdhi0-4bit-uhs {
		sd0-ctrl-dat03 {
			pins = "SD0_DATA0", "SD0_DATA1", "SD0_DATA2", "SD0_DATA3",
			       "SD0_CLK", "SD0_CMD";
			power-source = <1800>;
		};

		/*
		 * Pins 4-7 are hard-wired to eMMC with 1.8V IO voltage,
		 * with all pins sharing a single voltage domain.
		 *
		 * These pins are fixed-function, lacking configurable pull-up, pull-down or
		 * output-disable, while sharing the voltage domain with all SD0_* pins.
		 *
		 * While unused, disable the input buffers to avoid sampling the still
		 * physically connected but unused eMMC lines.
		 * For output direction rely on correct controller bus-width configuration
		 * to avoid over-voltage to eMMC e.g. when operating at 3.3V for a 4-bit microSD.
		 */
		sd0-dat47 {
			input-disable;
			pins = "SD0_DATA4", "SD0_DATA5", "SD0_DATA6", "SD0_DATA7";
		};
	};

	sdhi0_8bit_uhs_pins: sdhi0-8bit-uhs {
		sd0-ctrl {
			pins = "SD0_CLK", "SD0_CMD";
			power-source = <1800>;
		};

		sd0-dat {
			input-enable;
			pins = "SD0_DATA0", "SD0_DATA1", "SD0_DATA2", "SD0_DATA3",
			       "SD0_DATA4", "SD0_DATA5", "SD0_DATA6", "SD0_DATA7";
		};
	};

	sdhi0_cd_pins: sdhi0-cd {
		pinmux = <RZG2L_PORT_PINMUX(47, 0, 2)>; /* SD0_CD */
	};

	/* SD0_RST is only routed to eMMC which uses fixed 1.8V IO voltage */
	sdhi0_rst_pins: sdhi0-rst {
		pins = "SD0_RST#";
		power-source = <1800>;
	};

	sdhi1_pins: sdhi1 {
		pins = "SD1_DATA0", "SD1_DATA1", "SD1_DATA2", "SD1_DATA3",
		       "SD1_CLK", "SD1_CMD";
		power-source = <3300>;
	};

	spi1_pins: spi1 {
		pinmux = <RZG2L_PORT_PINMUX(44, 2, 1)>, /* RSPI1_MISO */
			 <RZG2L_PORT_PINMUX(44, 1, 1)>, /* RSPI1_MOSI */
			 <RZG2L_PORT_PINMUX(44, 0, 1)>; /* RSPI1_CK */
	};

	spi1_cs_pins: spi1-cs {
		pinmux = <RZG2L_PORT_PINMUX(44, 3, 1)>; /* RSPI1_SSL */
	};
};

&sbc {
	pinctrl-0 = <&qspi0_pins>;
	pinctrl-names = "default";
	status = "okay";

	flash@0 {
		compatible = "winbond,w25q80bl", "jedec,spi-nor";
		reg = <0>;
		spi-max-frequency = <50000000>;
		m25p,fast-read;
	};
};

&scif0 {
	pinctrl-0 = <&scif0_pins>;
	pinctrl-names = "default";
	status = "okay";
};

&scif2 {
	pinctrl-0 = <&scif2_pins>;
	pinctrl-names = "default";
	uart-has-rtscts;
	status = "okay";

	bluetooth {
		/*
		 * The BT is based on BCM4345C0. Murata 1MW variant seems to
		 * require driving RTS on open.
		 * Use compatible bcm43438 to accomplish this.
		 */
		compatible = "brcm,bcm43438-bt";
		max-speed = <115200>;
		shutdown-gpios = <&pinctrl RZG2L_GPIO(23, 0) GPIO_ACTIVE_HIGH>;
		vbat-supply = <&reg_pmic_buck4>;
		vddio-supply = <&reg_pmic_buck4>;
	};
};

/* WiFi */
&sdhi1 {
	/* Murata 1MW max rate is 50MHz */
	max-frequency = <50000000>;
	bus-width = <4>;
	cap-sdio-irq;
	mmc-pwrseq = <&sdhi1_pwrseq>;
	non-removable;
	no-1-8-v;
	no-sd;
	pinctrl-0 = <&sdhi1_pins>;
	pinctrl-names = "default";
	vmmc-supply = <&reg_pmic_buck4>;
	/*
	 * Host controller IO voltage is provided from reg_pmic_ldo2,
	 * WiFi module IO voltage from reg_pmic_buck4.
	 * Neither is configurable at run-time so either can be set here.
	 */
	vqmmc-supply = <&reg_pmic_ldo2>;
	status = "okay";
};

&usb2_phy0 {
	vbus-supply = <&usb0_vbus_otg>;
	status = "okay";
};

&usb2_phy1 {
	status = "okay";
};

&wdt0 {
	status = "okay";
};