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

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

/ {
	aliases {
		ethernet0 = &eth0;
		i2c0 = &i2c0;
		i2c1 = &i2c1;
		i2c2 = &i2c2;
		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 512MB, and the bootloader patches
		 * the dtb according to the actual RAM size and reservations.
		 */
		reg = <0x0 0x48000000 0x0 0x18000000>;
		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:
		 * High = connector, Low = eMMC.
		 * Define active-high for logical mux state 0 = eMMC, 1 = connector.
		 */
		mux-gpios = <&pinctrl RZG2L_GPIO(2, 1) GPIO_ACTIVE_HIGH>;
	};

	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(13, 2) 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>;
	};

	sdhi1_pwrseq: sdhi1-pwrseq {
		compatible = "mmc-pwrseq-simple";
		reset-gpios = <&pinctrl RZG2L_GPIO(2, 2) 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/G2(UL/L/C),
	 * instead delays are added by the ADIN1200 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(4, 2) IRQ_TYPE_EDGE_FALLING>;
	};
};

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

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

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

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

&i2c2 {
	clock-frequency = <400000>;
	pinctrl-0 = <&i2c2_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(4, 5, 1)>, /* ET0_LINKSTA */
			 <RZG2L_PORT_PINMUX(4, 3, 1)>, /* ET0_MDC */
			 <RZG2L_PORT_PINMUX(4, 4, 1)>, /* ET0_MDIO */
			 <RZG2L_PORT_PINMUX(1, 0, 1)>, /* ET0_TXC */
			 <RZG2L_PORT_PINMUX(1, 1, 1)>, /* ET0_TX_CTL */
			 <RZG2L_PORT_PINMUX(1, 2, 1)>, /* ET0_TXD0 */
			 <RZG2L_PORT_PINMUX(1, 3, 1)>, /* ET0_TXD1 */
			 <RZG2L_PORT_PINMUX(1, 4, 1)>, /* ET0_TXD2 */
			 <RZG2L_PORT_PINMUX(2, 0, 1)>, /* ET0_TXD3 */
			 <RZG2L_PORT_PINMUX(3, 0, 1)>, /* ET0_RXC */
			 <RZG2L_PORT_PINMUX(3, 1, 1)>, /* ET0_RX_CTL */
			 <RZG2L_PORT_PINMUX(3, 2, 1)>, /* ET0_RXD0 */
			 <RZG2L_PORT_PINMUX(3, 3, 1)>, /* ET0_RXD1 */
			 <RZG2L_PORT_PINMUX(4, 0, 1)>, /* ET0_RXD2 */
			 <RZG2L_PORT_PINMUX(4, 1, 1)>; /* ET0_RXD3 */
	};

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

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

	i2c2_pins: i2c2 {
		pinmux = <RZG2L_PORT_PINMUX(6, 3, 1)>, /* RIIC2_SDA */
			 <RZG2L_PORT_PINMUX(6, 4, 1)>; /* RIIC2_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(13, 0, 1)>, /* SCIF0_TXD */
			 <RZG2L_PORT_PINMUX(13, 1, 1)>; /* SCIF0_RXD */
	};

	scif2_pins: scif2 {
		pinmux = <RZG2L_PORT_PINMUX(6, 0, 4)>, /* SCIF2_TXD */
			 <RZG2L_PORT_PINMUX(6, 1, 4)>, /* SCIF2_RXD */
			 <RZG2L_PORT_PINMUX(5, 3, 2)>, /* SCIF2_CTS# */
			 <RZG2L_PORT_PINMUX(5, 4, 2)>; /* 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(0, 0, 1)>; /* 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(17, 2, 1)>, /* RSPI1_MISO */
			 <RZG2L_PORT_PINMUX(17, 1, 1)>, /* RSPI1_MOSI */
			 <RZG2L_PORT_PINMUX(17, 0, 1)>; /* RSPI1_CK */
	};

	spi1_cs_pins: spi1-cs {
		pinmux = <RZG2L_PORT_PINMUX(17, 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 BCM4343A2. Murata 1YN 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(2, 3) GPIO_ACTIVE_HIGH>;
		vbat-supply = <&reg_pmic_buck4>;
		vddio-supply = <&reg_pmic_buck4>;
	};
};

/* WiFi */
&sdhi1 {
	/* Murata 1YN 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";
};