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

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

#include "mt7981b.dtsi"

/ {
	model = "COMFAST CF-XR186";
	compatible = "comfast,cf-xr186", "mediatek,mt7981";

	aliases {
		label-mac-device = &gmac1;
		serial0 = &uart0;
		led-boot = &led_wifi_red;
		led-failsafe = &led_wifi_red;
		led-upgrade = &led_wifi_red;
		led-running = &led_wifi_red;
	};

	chosen {
		bootargs-override = "console=ttyS0,115200n8";
		stdout-path = "serial0:115200n8";
	};

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

	gpio-keys {
		compatible = "gpio-keys";

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

	leds {
		compatible = "gpio-leds";

		led_wifi_blue: wifi-blue {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_WLAN;
			gpios = <&pio 6 GPIO_ACTIVE_LOW>;
		};

		led_wifi_green: wifi-green {
			color = <LED_COLOR_ID_GREEN>;
			function = LED_FUNCTION_WLAN;
			gpios = <&pio 35 GPIO_ACTIVE_LOW>;
		};

		led_wifi_red: wifi-red {
			color = <LED_COLOR_ID_RED>;
			function = LED_FUNCTION_WLAN;
			gpios = <&pio 34 GPIO_ACTIVE_LOW>;
		};

		led_lan: lan-blue {
			color = <LED_COLOR_ID_BLUE>;
			function = LED_FUNCTION_LAN;
			gpios = <&pio 4 GPIO_ACTIVE_LOW>;
		};
	};
};

&crypto {
	status = "okay";
};

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

	gmac1: mac@1 {
		compatible = "mediatek,eth-mac";
		reg = <1>;
		phy-mode = "gmii";
		phy-handle = <&int_gbe_phy>;
		nvmem-cells = <&macaddr_factory_e000 0>;
		nvmem-cell-names = "mac-address";
	};
};

&pio {
	spi0_flash_pins: spi0-pins {
		mux {
			function = "spi";
			groups = "spi0", "spi0_wp_hold";
		};
		conf-pu {
			pins = "SPI0_CS", "SPI0_HOLD", "SPI0_WP";
			drive-strength = <MTK_DRIVE_8mA>;
			bias-pull-up = <MTK_PUPD_SET_R1R0_11>;
		};
		conf-pd {
			pins = "SPI0_CLK", "SPI0_MOSI", "SPI0_MISO";
			drive-strength = <MTK_DRIVE_8mA>;
			bias-pull-down = <MTK_PUPD_SET_R1R0_11>;
		};
	};
};

&spi0 {
	pinctrl-names = "default";
	pinctrl-0 = <&spi0_flash_pins>;
	status = "okay";

	spi_nand: spi-nand@0 {
		compatible = "spi-nand";
		reg = <0>;
		spi-max-frequency = <52000000>;

		spi-cal-enable;
		spi-cal-mode = "read-data";
		spi-cal-datalen = <7>;
		spi-cal-data = /bits/ 8 <0x53 0x50 0x49 0x4E 0x41 0x4E 0x44>;
		spi-cal-addrlen = <5>;
		spi-cal-addr = /bits/ 32 <0x0 0x0 0x0 0x0 0x0>;

		spi-tx-bus-width = <4>;
		spi-rx-bus-width = <4>;
		mediatek,nmbm;
		mediatek,bmt-max-ratio = <1>;
		mediatek,bmt-max-reserved-blocks = <64>;

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

			partition@0 {
				label = "BL2";
				reg = <0x00000 0x0100000>;
				read-only;
			};

			partition@100000 {
				label = "u-boot-env";
				reg = <0x0100000 0x0080000>;
				read-only;
			};

			partition@180000 {
				label = "Factory";
				reg = <0x180000 0x0200000>;
				read-only;

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

					eeprom_factory_0: eeprom@0 {
						reg = <0x0 0x1000>;
					};

					macaddr_factory_4: macaddr@4 {
						compatible = "mac-base";
						reg = <0x4 0x6>;
						#nvmem-cell-cells = <1>;
					};

					macaddr_factory_8000: macaddr@8000 {
						compatible = "mac-base";
						reg = <0x8000 0x6>;
						#nvmem-cell-cells = <1>;
					};

					macaddr_factory_e000: macaddr@e000 {
						compatible = "mac-base";
						reg = <0xe000 0x6>;
						#nvmem-cell-cells = <1>;
					};
				};
			};

			partition@380000 {
				label = "FIP";
				reg = <0x380000 0x0200000>;
				read-only;
			};

			partition@580000 {
				label = "ubi";
				reg = <0x580000 0x4000000>;
				compatible = "linux,ubi";
			};
		};
	};
};

&uart0 {
	status = "okay";
};

&watchdog {
	status = "okay";
};

&wifi {
	#address-cells = <1>;
	#size-cells = <0>;
	status = "okay";
	nvmem-cells = <&eeprom_factory_0>;
	nvmem-cell-names = "eeprom";

	band@0 {
		reg = <0>;
		nvmem-cells = <&macaddr_factory_4 0>;
		nvmem-cell-names = "mac-address";
	};

	band@1 {
		reg = <1>;
		nvmem-cells = <&macaddr_factory_8000 0>;
		nvmem-cell-names = "mac-address";
	};
};
