// SPDX-License-Identifier: (GPL-2.0 OR MIT)
/*
 * Copyright (C) 2026 MediaTek Inc.
 * Author: Macpaul Lin <macpaul.lin@mediatek.com>
 */

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

#include "mt8189.dtsi"
#include "mt8189-pinfunc.h"
#include "mt6359.dtsi"

/ {
	aliases {
		serial0 = &uart0;
	};

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

	usb_p0_vbus: regulator@0 {
		compatible = "regulator-fixed";
		regulator-name = "p0_vbus";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		gpio = <&pio 82 0>;
		enable-active-high;
	};

	usb_p1_vbus: regulator@1 {
		compatible = "regulator-fixed";
		regulator-name = "p1_vbus";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		gpio = <&pio 84 0>;
		enable-active-high;
	};

	usb_p2_vbus: regulator@2 {
		compatible = "regulator-fixed";
		regulator-name = "p2_vbus";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		gpio = <&pio 85 0>;
		enable-active-high;
	};

	usb_p3_vbus: regulator@3 {
		compatible = "regulator-fixed";
		regulator-name = "p3_vbus";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		gpio = <&pio 86 0>;
		enable-active-high;
	};

	usb_p4_vbus: regulator@4 {
		compatible = "regulator-fixed";
		regulator-name = "p4_vbus";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		gpio = <&pio 87 0>;
		enable-active-high;
	};

	firmware {
		optee {
			compatible = "linaro,optee-tz";
			method = "smc";
		};

		psci {
			compatible = "arm,psci-1.0";
			method = "smc";
		};
	};

	memory@40000000 {
		/* 8GB */
		device_type = "memory";
		reg = <0 0x40000000 0x2 0x00000000>;
	};

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

		/* 2 MiB reserved for ARM Trusted Firmware (BL31) */
		bl31_secmon_reserved: secmon@54600000 {
			no-map;
			reg = <0 0x54600000 0x0 0x200000>;
		};

		/* 12 MiB reserved for OP-TEE (BL32)
		 * +-----------------------+ 0x43e0_0000
		 * |      SHMEM 2MiB       |
		 * +-----------------------+ 0x43c0_0000
		 * |        | TA_RAM  8MiB |
		 * + TZDRAM +--------------+ 0x4340_0000
		 * |        | TEE_RAM 2MiB |
		 * +-----------------------+ 0x4320_0000
		 */
		optee_reserved: optee@43200000 {
			no-map;
			reg = <0 0x43200000 0 0x00c00000>;
		};

		dsi_reserved: dsi@60000000 {
			reg = <0 0x60000000 0 0x02000000>;
			no-map;
		};
	};
};

&watchdog {
	status = "okay";
};

&uart0 {
	pinctrl-0 = <&uart0_pins>;
	pinctrl-names = "default";
	status = "okay";
};

&mmc0 {
	bus-width = <8>;
	max-frequency = <200000000>;
	cap-mmc-highspeed;
	mmc-hs200-1_8v;
	mmc-hs400-1_8v;
	hs400-ds-delay = <0x1481b>;
	cap-mmc-hw-reset;
	vmmc-supply = <&mt6359_vemc_1_ldo_reg>;
	vqmmc-supply = <&mt6359_vufs_ldo_reg>;
	non-removable;
	pinctrl-names = "default", "state_uhs";
	pinctrl-0 = <&mmc0_default_pins>;
	pinctrl-1 = <&mmc0_uhs_pins>;
	status = "okay";
};

&mmc1 {
	bus-width = <4>;
	cap-sd-highspeed;
	cd-gpios = <&pio 2 GPIO_ACTIVE_HIGH>;
	max-frequency = <200000000>;
	no-mmc;
	no-sdio;
	sd-uhs-sdr50;
	sd-uhs-sdr104;
	pinctrl-names = "default", "state_uhs";
	pinctrl-0 = <&mmc1_default_pins>;
	pinctrl-1 = <&mmc1_uhs_pins>;
	vmmc-supply = <&mt6359_vpa_buck_reg>;
	vqmmc-supply = <&mt6359_vsim1_ldo_reg>;
	status = "okay";
};

&mt6359_vufs_ldo_reg {
	regulator-always-on;
};

&nor_flash {
	pinctrl-names = "default";
	pinctrl-0 = <&nor_pins>;

	status = "okay";

	flash@0 {
		compatible = "jedec,spi-nor";
		reg = <0>;
		spi-max-frequency = <52000000>;
		spi-rx-bus-width = <2>;
		spi-tx-bus-width = <2>;
	};
};

&pio {
	mmc0_default_pins: mmc0-default-pins {
		pins-clk {
			pinmux = <PINMUX_GPIO162__FUNC_MSDC0_CLK>;
			drive-strength = <6>;
			bias-pull-down = <MTK_PUPD_SET_R1R0_10>;
		};

		pins-cmd-dat {
			pinmux = <PINMUX_GPIO166__FUNC_MSDC0_DAT0>,
				 <PINMUX_GPIO165__FUNC_MSDC0_DAT1>,
				 <PINMUX_GPIO164__FUNC_MSDC0_DAT2>,
				 <PINMUX_GPIO163__FUNC_MSDC0_DAT3>,
				 <PINMUX_GPIO159__FUNC_MSDC0_DAT4>,
				 <PINMUX_GPIO158__FUNC_MSDC0_DAT5>,
				 <PINMUX_GPIO157__FUNC_MSDC0_DAT6>,
				 <PINMUX_GPIO156__FUNC_MSDC0_DAT7>,
				 <PINMUX_GPIO161__FUNC_MSDC0_CMD>;
			input-enable;
			drive-strength = <6>;
			bias-pull-up = <MTK_PUPD_SET_R1R0_01>;
		};

		pins-rst {
			pinmux = <PINMUX_GPIO160__FUNC_MSDC0_RSTB>;
			drive-strength = <6>;
			bias-pull-up = <MTK_PUPD_SET_R1R0_00>;
		};
	};

	mmc0_uhs_pins: mmc0-uhs-pins {
		pins-clk {
			pinmux = <PINMUX_GPIO162__FUNC_MSDC0_CLK>;
			drive-strength = <8>;
			bias-pull-down = <MTK_PUPD_SET_R1R0_10>;
		};

		pins-cmd-dat {
			pinmux = <PINMUX_GPIO166__FUNC_MSDC0_DAT0>,
				 <PINMUX_GPIO165__FUNC_MSDC0_DAT1>,
				 <PINMUX_GPIO164__FUNC_MSDC0_DAT2>,
				 <PINMUX_GPIO163__FUNC_MSDC0_DAT3>,
				 <PINMUX_GPIO159__FUNC_MSDC0_DAT4>,
				 <PINMUX_GPIO158__FUNC_MSDC0_DAT5>,
				 <PINMUX_GPIO157__FUNC_MSDC0_DAT6>,
				 <PINMUX_GPIO156__FUNC_MSDC0_DAT7>,
				 <PINMUX_GPIO161__FUNC_MSDC0_CMD>;
			input-enable;
			drive-strength = <8>;
			bias-pull-up = <MTK_PUPD_SET_R1R0_01>;
		};

		pins-ds {
			pinmux = <PINMUX_GPIO167__FUNC_MSDC0_DSL>;
			drive-strength = <8>;
			bias-pull-down = <MTK_PUPD_SET_R1R0_10>;
		};

		pins-rst {
			pinmux = <PINMUX_GPIO160__FUNC_MSDC0_RSTB>;
			bias-pull-up = <MTK_PUPD_SET_R1R0_00>;
		};
	};

	mmc1_default_pins: mmc1-default-pins {
		pins-clk {
			pinmux = <PINMUX_GPIO169__FUNC_MSDC1_CLK>;
			drive-strength = <6>;
			bias-pull-down = <MTK_PUPD_SET_R1R0_10>;
		};

		pins-cmd-dat {
			pinmux = <PINMUX_GPIO170__FUNC_MSDC1_DAT0>,
				 <PINMUX_GPIO171__FUNC_MSDC1_DAT1>,
				 <PINMUX_GPIO172__FUNC_MSDC1_DAT2>,
				 <PINMUX_GPIO173__FUNC_MSDC1_DAT3>,
				 <PINMUX_GPIO168__FUNC_MSDC1_CMD>;
			input-enable;
			drive-strength = <6>;
			bias-pull-up = <MTK_PUPD_SET_R1R0_01>;
		};

		pins-insert {
			pinmux = <PINMUX_GPIO2__FUNC_GPIO2>;
			bias-pull-up;
		};
	};

	mmc1_uhs_pins: mmc1-uhs-pins {
		pins-clk {
			pinmux = <PINMUX_GPIO169__FUNC_MSDC1_CLK>;
			drive-strength = <8>;
			bias-pull-down = <MTK_PUPD_SET_R1R0_10>;
		};

		pins-cmd-dat {
			pinmux = <PINMUX_GPIO170__FUNC_MSDC1_DAT0>,
				 <PINMUX_GPIO171__FUNC_MSDC1_DAT1>,
				 <PINMUX_GPIO172__FUNC_MSDC1_DAT2>,
				 <PINMUX_GPIO173__FUNC_MSDC1_DAT3>,
				 <PINMUX_GPIO168__FUNC_MSDC1_CMD>;
			input-enable;
			drive-strength = <8>;
			bias-pull-up = <MTK_PUPD_SET_R1R0_01>;
		};

		pins-insert {
			pinmux = <PINMUX_GPIO2__FUNC_GPIO2>;
			bias-pull-up;
		};
	};

	nor_pins: nor-pins {
		pins-ck-io {
			pinmux = <PINMUX_GPIO150__FUNC_SPINOR_CK>,
				 <PINMUX_GPIO152__FUNC_SPINOR_IO0>,
				 <PINMUX_GPIO153__FUNC_SPINOR_IO1>;
			drive-strength = <8>;
			bias-pull-down;
		};

		pins-cs {
			pinmux = <PINMUX_GPIO151__FUNC_SPINOR_CS>;
			drive-strength = <8>;
			bias-pull-up;
		};
	};

	uart0_pins: uart0-pins {
		pins {
			pinmux = <PINMUX_GPIO31__FUNC_UTXD0>,
				 <PINMUX_GPIO32__FUNC_URXD0>;
			bias-pull-up;
		};
	};

	eth_default_pins: eth-default-pins {
		txd-pins {
			pinmux = <PINMUX_GPIO119__FUNC_GBE_TXD3>,
				 <PINMUX_GPIO120__FUNC_GBE_TXD2>,
				 <PINMUX_GPIO121__FUNC_GBE_TXD1>,
				 <PINMUX_GPIO122__FUNC_GBE_TXD0>;
			drive-strength = <MTK_DRIVE_8mA>;
		};
		cc-pins {
			pinmux = <PINMUX_GPIO127__FUNC_GBE_TXC>,
				 <PINMUX_GPIO130__FUNC_GBE_TXEN>,
				 <PINMUX_GPIO129__FUNC_GBE_RXDV>,
				 <PINMUX_GPIO128__FUNC_GBE_RXC>;
			drive-strength = <MTK_DRIVE_8mA>;
		};
		rxd-pins {
			pinmux = <PINMUX_GPIO123__FUNC_GBE_RXD3>,
				 <PINMUX_GPIO124__FUNC_GBE_RXD2>,
				 <PINMUX_GPIO125__FUNC_GBE_RXD1>,
				 <PINMUX_GPIO126__FUNC_GBE_RXD0>;
			drive-strength = <MTK_DRIVE_8mA>;
		};
		mdio-pins {
			pinmux = <PINMUX_GPIO131__FUNC_GBE_MDC>,
				 <PINMUX_GPIO132__FUNC_GBE_MDIO>;
			drive-strength = <MTK_DRIVE_8mA>;
			input-enable;
		};
		power-pins {
			pinmux = <PINMUX_GPIO133__FUNC_GPIO133>,
				 <PINMUX_GPIO134__FUNC_GPIO134>;
			output-high;
		};
	};

	eth_sleep_pins: eth-sleep-pins {
		txd-pins {
			pinmux = <PINMUX_GPIO119__FUNC_GPIO119>,
				 <PINMUX_GPIO120__FUNC_GPIO120>,
				 <PINMUX_GPIO121__FUNC_GPIO121>,
				 <PINMUX_GPIO122__FUNC_GPIO122>;
		};
		cc-pins {
			pinmux = <PINMUX_GPIO127__FUNC_GPIO127>,
				 <PINMUX_GPIO130__FUNC_GPIO130>,
				 <PINMUX_GPIO129__FUNC_GPIO129>,
				 <PINMUX_GPIO128__FUNC_GPIO128>;
		};
		rxd-pins {
			pinmux = <PINMUX_GPIO123__FUNC_GPIO123>,
				 <PINMUX_GPIO124__FUNC_GPIO124>,
				 <PINMUX_GPIO125__FUNC_GPIO125>,
				 <PINMUX_GPIO126__FUNC_GPIO126>;
		};
		mdio-pins {
			pinmux = <PINMUX_GPIO131__FUNC_GPIO131>,
				 <PINMUX_GPIO132__FUNC_GPIO132>;
			input-disable;
			bias-disable;
		};
	};
};

&pmic {
	interrupts-extended = <&pio 194 IRQ_TYPE_LEVEL_HIGH>;
};

&xhci0{
	vbus-supply = <&usb_p0_vbus>;
	status = "okay";
};

&xhci1{
	vbus-supply = <&usb_p1_vbus>;
	status = "okay";
};

&xhci2{
	vbus-supply = <&usb_p2_vbus>;
	status = "okay";
};

&xhci3{
	vbus-supply = <&usb_p3_vbus>;
	status = "okay";
};

&xhci4{
	vbus-supply = <&usb_p4_vbus>;
	status = "okay";
};

&eth {
	/*
	 * TX clock is provided by MAC
	 */
	phy-mode = "rgmii-rxid";
	phy-handle = <&phy>;
	pinctrl-names = "default", "sleep";
	pinctrl-0 = <&eth_default_pins>;
	pinctrl-1 = <&eth_sleep_pins>;
	status = "okay";
	mdio {
		compatible = "snps,dwmac-mdio";
		#address-cells = <1>;
		#size-cells = <0>;
		phy: phy@0 {
			compatible = "ethernet-phy-idc0ff.0421";
			reg = <0>;
		};
	};
};
