// SPDX-License-Identifier: (GPL-2.0 OR MIT)

/dts-v1/;
#include <dt-bindings/input/input.h>
#include <dt-bindings/leds/common.h>
#include <dt-bindings/gpio/gpio.h>

#include "mt7986a.dtsi"

/ {
	model = "ZyXEL EX5700 (Telenor)";
	compatible = "zyxel,ex5700-telenor", "mediatek,mt7986a";

	aliases {
		serial0 = &uart0;
		ethernet0 = &gmac0;
		led-boot = &led_status_green;
		led-failsafe = &led_status_green;
		led-running = &led_status_green;
		led-upgrade = &led_status_amber;
	};

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

		// Stock U-Boot crashes unless /chosen/bootargs exists
		bootargs = "earlycon=uart8250,mmio32,0x11002000 console=ttyS0,115200n8";
	};

	memory {
		reg = <0 0x40000000 0 0x40000000>;
	};

	reg_3p3v: regulator-3p3v {
		compatible = "regulator-fixed";
		regulator-name = "fixed-3.3V";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-boot-on;
		regulator-always-on;
	};

	reg_5v: regulator-5v {
		compatible = "regulator-fixed";
		regulator-name = "fixed-5V";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		regulator-boot-on;
		regulator-always-on;
	};


	keys {
		compatible = "gpio-keys";
		poll-interval = <20>;

		reset-button {
			label = "reset";
			gpios = <&pio 9 GPIO_ACTIVE_LOW>;
			linux,code = <KEY_RESTART>;
		};

		wps-button {
			label = "wps";
			gpios = <&pio 10 GPIO_ACTIVE_LOW>;
			linux,code = <KEY_WPS_BUTTON>;
		};
	};

	leds {
		compatible = "gpio-leds";

		red1 {
			label = "red:net";
			gpios = <&pio 23 GPIO_ACTIVE_HIGH>;
			default-state = "off";
		};

		green1 {
			label = "green:net";
			gpios = <&pio 25 GPIO_ACTIVE_HIGH>;
			default-state = "off";
		};

		amber1 {
			label = "amber:net";
			gpios = <&pio 29 GPIO_ACTIVE_HIGH>;
			default-state = "off";
		};

		white2 {
			function = LED_FUNCTION_STATUS;
			color = <LED_COLOR_ID_WHITE>;
			gpios = <&pio 16 GPIO_ACTIVE_HIGH>;
			default-state = "off";
		};

		red2 {
			function = LED_FUNCTION_STATUS;
			color = <LED_COLOR_ID_RED>;
			gpios = <&pio 17 GPIO_ACTIVE_HIGH>;
			default-state = "off";
		};

		led_status_green: green2 {
			function = LED_FUNCTION_STATUS;
			color = <LED_COLOR_ID_GREEN>;
			gpios = <&pio 31 GPIO_ACTIVE_HIGH>;
			default-state = "off";
		};

		led_status_amber: amber2 {
			function = LED_FUNCTION_STATUS;
			color = <LED_COLOR_ID_AMBER>;
			gpios = <&pio 18 GPIO_ACTIVE_HIGH>;
			default-state = "off";
		};
	};

};

&eth {
	status = "okay";
	pinctrl-names = "default";
	pinctrl-0 = <&eth_pins>;

	gmac0: mac@0 {
		compatible = "mediatek,eth-mac";
		reg = <0>;
		phy-mode = "2500base-x";

		fixed-link {
			speed = <2500>;
			full-duplex;
			pause;
		};
	};

	mac@1 {
		compatible = "mediatek,eth-mac";
		reg = <1>;
		label = "wan";
		phy-mode = "2500base-x";
		phy-handle = <&phy6>;
	};

	mdio: mdio-bus {
		#address-cells = <1>;
		#size-cells = <0>;
	};
};

&mdio {
	reset-gpios = <&pio 6 GPIO_ACTIVE_LOW>;
	reset-delay-us = <50000>;
	reset-post-delay-us = <20000>;

	phy5: phy@5 {
		compatible = "ethernet-phy-ieee802.3-c45";
		reg = <5>;
	};

	phy6: phy@6 {
		compatible = "ethernet-phy-ieee802.3-c45";
		reg = <6>;
	};

	switch: switch@1f {
		compatible = "mediatek,mt7531";
		reg = <31>;
		reset-gpios = <&pio 5 GPIO_ACTIVE_HIGH>;
		interrupt-controller;
		#interrupt-cells = <1>;
		interrupt-parent = <&pio>;
		interrupts = <66 IRQ_TYPE_LEVEL_HIGH>;
	};
};

&switch {
	ports {
		#address-cells = <1>;
		#size-cells = <0>;

		port@0 {
			reg = <0>;
			label = "lan3";
		};

		port@1 {
			reg = <1>;
			label = "lan2";
		};

		port@2 {
			reg = <2>;
			label = "lan1";
		};

		port@5 {
			reg = <5>;
			label = "lan4";
			phy-mode = "2500base-x";
			phy-handle = <&phy5>;
		};

		port@6 {
			reg = <6>;
			ethernet = <&gmac0>;
			phy-mode = "2500base-x";

			fixed-link {
				speed = <2500>;
				full-duplex;
				pause;
			};
		};
	};
};

&crypto {
	status = "okay";
};

&pcie {
	pinctrl-names = "default";
	pinctrl-0 = <&pcie_pins>;
	status = "okay";

	pcie@0,0 {
		reg = <0x0000 0 0 0 0>;

		wifi@0,0 {
			compatible = "mediatek,mt76";
			reg = <0x0000 0 0 0 0>;
			mediatek,mtd-eeprom = <&factory 0xa0000>;
		};
	};
};

&pcie_phy {
	status = "okay";
};

&watchdog {
	status = "okay";
};

&wifi {
	status = "okay";
	pinctrl-names = "default";
	pinctrl-0 = <&wf_5g_pins>;

	mediatek,mtd-eeprom = <&factory 0x0>;
};

&pio {
	eth_pins: eth-pins {
		mux {
			function = "eth";
			groups = "switch_int", "mdc_mdio";
		};
	};

	pcie_pins: pcie-pins {
		mux {
			function = "pcie";
			groups = "pcie_pereset"; // "pcie_clk" and "pcie_wake" is unused?
		};
	};

	spi_flash_pins: spi-flash-pins-33-to-38 {
		mux {
			function = "spi";
			groups = "spi0", "spi0_wp_hold";
		};
		conf-pu {
			pins = "SPI2_CS", "SPI2_HOLD", "SPI2_WP";
			drive-strength = <8>;
			mediatek,pull-up-adv = <0>; /* bias-disable */
		};
		conf-pd {
			pins = "SPI2_CLK", "SPI2_MOSI", "SPI2_MISO";
			drive-strength = <8>;
			mediatek,pull-down-adv = <0>; /* bias-disable */
		};
	};

	wf_5g_pins: wf_5g-pins {
		mux {
			function = "wifi";
			groups = "wf_5g";
		};
		conf {
			pins = "WF1_HB1", "WF1_HB2", "WF1_HB3", "WF1_HB4",
			       "WF1_HB0", "WF1_HB5", "WF1_HB6", "WF1_HB7",
			       "WF1_HB8", "WF1_TOP_CLK", "WF1_TOP_DATA";
			drive-strength = <4>;
		};
	};

};

&spi0 {
	pinctrl-names = "default";
	pinctrl-0 = <&spi_flash_pins>;
	cs-gpios = <0>, <0>;
	status = "okay";

	flash@0 {
		compatible = "jedec,spi-nor";
		reg = <0>;
		spi-max-frequency = <20000000>;
	};

	flash@1 {
		compatible = "spi-nand";
		reg = <1>;

		mediatek,nmbm;
		mediatek,bmt-max-ratio = <1>;
		mediatek,bmt-max-reserved-blocks = <64>;

		spi-max-frequency = <20000000>;
		spi-tx-bus-width = <4>;
		spi-rx-bus-width = <4>;

		partitions {
			compatible = "fixed-partitions";
			#address-cells = <1>;
			#size-cells = <1>;

			partition@0 {
				label = "BL2";
				reg = <0x000000 0x100000>;
				read-only;
			};
			partition@100000 {
				label = "u-boot-env";
				reg = <0x100000 0x80000>;
			};
			factory: partition@180000 {
				label = "Factory";
				reg = <0x180000 0x200000>;
				read-only;
			};
			partition@380000 {
				label = "FIP";
				reg = <0x380000 0x200000>;
				read-only;
			};
			partition@580000 {
				label = "ubi";
				reg = <0x580000 0x1da80000>;
			};
		};
	};
};

&ssusb {
	vusb33-supply = <&reg_3p3v>;
	vbus-supply = <&reg_5v>;
	status = "okay";
};

&trng {
	status = "okay";
};

&uart0 {
	status = "okay";
};

&usb_phy {
	status = "okay";
};