// SPDX-License-Identifier: GPL-2.0-or-later OR MIT

/dts-v1/;

#include "armada-7040.dtsi"
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/input/input.h>
#include <dt-bindings/leds/common.h>

/ {
	model = "MikroTik RB5009";
	compatible = "mikrotik,rb5009", "marvell,armada7040",
		     "marvell,armada-ap806-quad", "marvell,armada-ap806";

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

	memory@0 {
		device_type = "memory";
		reg = <0x0 0x0 0x0 0x40000000>;
	};

	aliases {
		led-boot = &led_user;
		led-failsafe = &led_user;
		led-running = &led_user;
		led-upgrade = &led_user;
		label-mac-device = &p1;
	};

	usb3_vbus: regulator-usb3-vbus0 {
		compatible = "regulator-fixed";
		regulator-name = "usb3_vbus";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		gpios = <&cp0_gpio2 23 GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};

	reg_leds: regulator-leds {
		compatible = "regulator-fixed";
		regulator-name = "LED-power";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		gpios = <&cp0_gpio2 27 GPIO_ACTIVE_HIGH>;
		enable-active-high;
		regulator-boot-on;
	};

	output-led-power {
		compatible = "regulator-output";
		vout-supply = <&reg_leds>;
	};

	sfp_i2c: sfp-i2c {
		compatible = "i2c-gpio";
		sda-gpios = <&cp0_gpio1 0 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>;
		scl-gpios = <&cp0_gpio1 1 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>;
	};

	keys {
		compatible = "gpio-keys";

		reset {
			label = "reset";
			gpios = <&cp0_gpio1 28 GPIO_ACTIVE_LOW>;
			linux,code = <KEY_RESTART>;
		};
	};

	leds {
		compatible = "gpio-leds";

		led_user: user {
			function = "user";
			gpios = <&cp0_gpio2 26 GPIO_ACTIVE_LOW>;
			color = <LED_COLOR_ID_GREEN>;
		};

		sfp {
			function = "sfp";
			gpios = <&cp0_gpio2 25 GPIO_ACTIVE_LOW>;
			color = <LED_COLOR_ID_GREEN>;
		};

		hdr1 {
			function = "hdr1";
			gpios = <&cp0_gpio1 4 GPIO_ACTIVE_LOW>;
			color = <LED_COLOR_ID_BLUE>;
		};

		hdr2 {
			function = "hdr2";
			gpios = <&cp0_gpio2 19 GPIO_ACTIVE_LOW>;
			color = <LED_COLOR_ID_BLUE>;
		};
	};

	sfp: sfp {
		compatible = "sff,sfp";
		i2c-bus = <&sfp_i2c>;
		mod-def0-gpios = <&cp0_gpio1 11 GPIO_ACTIVE_LOW>;
		los-gpios = <&cp0_gpio1 2 GPIO_ACTIVE_HIGH>;
		tx-fault-gpios = <&cp0_gpio1 6 GPIO_ACTIVE_HIGH>;
		tx-disable-gpios = <&cp0_gpio1 5 GPIO_ACTIVE_HIGH>;
		rate-select0-gpios = <&cp0_gpio1 3 GPIO_ACTIVE_HIGH>;
	};
};

&uart0 {
	status = "okay";

	pinctrl-0 = <&uart0_pins>;
	pinctrl-names = "default";
};


&spi0 {
	status = "okay";

	flash@0 {
		#address-cells = <1>;
		#size-cells = <1>;
		compatible = "jedec,spi-nor";
		reg = <0>;
		spi-max-frequency = <20000000>;

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

			partition@0 {
				compatible = "mikrotik,routerboot-partitions";
				#address-cells = <1>;
				#size-cells = <1>;
				label = "MikroTik";
				reg = <0x0 0xfe0000>;

				hard_config: hard_config {
					read-only;

					nvmem-layout {
						compatible = "mikrotik,routerboot-nvmem";

						macaddr_hard: base-mac-address {
							#nvmem-cell-cells = <1>;
						};
					};
				};

				soft_config {
				};

				dtb_config {
					read-only;
				};
			};

			partition@b0000 {
				label = "RouterBOOT-primary";
				reg = <0xb0000 0x10000>;
			};

			/* Empty space on NOR repurposed for U-Boot environment */
			partition@fe0000 {
				label = "u-boot-env";
				reg = <0xfe0000 0x20000>;

				nvmem-layout {
					compatible = "u-boot,env";
				};
			};
		};
	};
};

&cp0_nand_controller {
	status = "okay";

	nand@0 {
		reg = <0>;
		nand-rb = <0>;
		nand-ecc-mode = "hw";
		nand-ecc-strength = <4>;
		nand-ecc-step-size = <512>;
		nand-on-flash-bbt;

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

			partition@0 {
				label = "YAFFS";
				reg = <0x0 0x800000>;
			};

			partition@800000 {
				label = "ubi";
				reg = <0x800000 0x3f800000>;
			};
		};
	};
};

&cp0_utmi {
	status = "okay";
};

&cp0_comphy3 {
	connector {
		compatible = "usb-a-connector";
		phy-supply = <&usb3_vbus>;
	};
};

&cp0_usb3_1 {
	status = "okay";
	phys = <&cp0_comphy3 1>, <&cp0_utmi1>;
	phy-names = "cp0-usb3h1-comphy", "utmi";
	dr_mode = "host";
};

&cp0_i2c0 {
	status = "okay";
	clock-frequency = <100000>;
};

&cp0_mdio {
	status = "okay";
};

&cp0_ethernet {
	status = "okay";
};

&cp0_eth0 {
	/* This port is connected to 88E6393X switch */
	status = "okay";
	phy-mode = "10gbase-r";
	phys = <&cp0_comphy2 0>;
	managed = "in-band-status";
	nvmem-cells = <&macaddr_hard 0>;
	nvmem-cell-names = "mac-address";
};

&cp0_mdio {
	status = "okay";

	switch@0 {
		/* Actual device is MV88E6393X */
		compatible = "marvell,mv88e6190";
		#address-cells = <1>;
		#size-cells = <0>;
		reg = <0>;
		/* LED config is lost if switch is reset */
		//reset-gpios = <&cp0_gpio2 2 GPIO_ACTIVE_HIGH>;

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

			port@0 {
				reg = <0>;
				label = "cpu";
				ethernet = <&cp0_eth0>;
				phy-mode = "10gbase-r";
				managed = "in-band-status";
			};

			port@1 {
				reg = <1>;
				label = "p8";
				phy-handle = <&switch0phy1>;
				nvmem-cells = <&macaddr_hard 7>;
				nvmem-cell-names = "mac-address";
			};

			port@2 {
				reg = <2>;
				label = "p7";
				phy-handle = <&switch0phy2>;
				nvmem-cells = <&macaddr_hard 6>;
				nvmem-cell-names = "mac-address";
			};

			port@3 {
				reg = <3>;
				label = "p6";
				phy-handle = <&switch0phy3>;
				nvmem-cells = <&macaddr_hard 5>;
				nvmem-cell-names = "mac-address";
			};

			port@4 {
				reg = <4>;
				label = "p5";
				phy-handle = <&switch0phy4>;
				nvmem-cells = <&macaddr_hard 4>;
				nvmem-cell-names = "mac-address";
			};

			port@5 {
				reg = <5>;
				label = "p4";
				phy-handle = <&switch0phy5>;
				nvmem-cells = <&macaddr_hard 3>;
				nvmem-cell-names = "mac-address";
			};

			port@6 {
				reg = <6>;
				label = "p3";
				phy-handle = <&switch0phy6>;
				nvmem-cells = <&macaddr_hard 2>;
				nvmem-cell-names = "mac-address";
			};

			port@7 {
				reg = <7>;
				label = "p2";
				phy-handle = <&switch0phy7>;
				nvmem-cells = <&macaddr_hard 1>;
				nvmem-cell-names = "mac-address";
			};

			p1: port@9 {
				reg = <9>;
				label = "p1";
				phy-mode = "sgmii";
				phy-handle = <&qca8081>;
				managed = "in-band-status";
				nvmem-cells = <&macaddr_hard 0>;
				nvmem-cell-names = "mac-address";
			};

			port@a {
				reg = <10>;
				label = "sfp";
				phy-mode = "10gbase-r";
				managed = "in-band-status";
				sfp = <&sfp>;
				nvmem-cells = <&macaddr_hard 8>;
				nvmem-cell-names = "mac-address";
			};
		};

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

			switch0phy1: switch0phy1@1 {
				reg = <0x1>;
			};

			switch0phy2: switch0phy2@2 {
				reg = <0x2>;
			};

			switch0phy3: switch0phy3@3 {
				reg = <0x3>;
			};

			switch0phy4: switch0phy4@4 {
				reg = <0x4>;
			};

			switch0phy5: switch0phy5@5 {
				reg = <0x5>;
			};

			switch0phy6: switch0phy6@6 {
				reg = <0x6>;
			};

			switch0phy7: switch0phy7@7 {
				reg = <0x7>;
			};
		};

		mdio1 {
			compatible = "marvell,mv88e6xxx-mdio-external";
			#address-cells = <1>;
			#size-cells = <0>;

			qca8081: qca8081@0 {
				reg = <0>;

				leds {
					#address-cells = <1>;
					#size-cells = <0>;

					led@1 {
						reg = <1>;
						color = <LED_COLOR_ID_GREEN>;
						function = LED_FUNCTION_WAN;
						default-state = "keep";
						active-low;
					};
				};
			};
		};
	};
};
