From b3c057ed667964f814b8093fdf25378bffd025f9 Mon Sep 17 00:00:00 2001
From: John Crispin <john@phrozen.org>
Date: Fri, 7 Jan 2022 10:58:08 +0100
Subject: [PATCH] ramips: add support for actiontec web7200

Signed-off-by: John Crispin <john@phrozen.org>
---
 package/boot/uboot-envtools/files/ramips      |   3 +
 .../ramips/dts/mt7621_actiontec_web7200.dts   | 138 ++++++++++++++++++
 target/linux/ramips/image/mt7621.mk           |  33 +++++
 .../mt7621/base-files/etc/board.d/02_network  |   6 +
 .../mt7621/base-files/lib/upgrade/platform.sh |   1 +
 5 files changed, 181 insertions(+)
 create mode 100644 target/linux/ramips/dts/mt7621_actiontec_web7200.dts

diff --git a/package/boot/uboot-envtools/files/ramips b/package/boot/uboot-envtools/files/ramips
index 7c1385cdcc..3e16888c0c 100644
--- a/package/boot/uboot-envtools/files/ramips
+++ b/package/boot/uboot-envtools/files/ramips
@@ -12,6 +12,9 @@ touch /etc/config/ubootenv
 board=$(board_name)
 
 case "$board" in
+actiontec,web7200)
+	ubootenv_add_uci_config "/dev/mtd2" "0x0" "0x20000" "0x20000"
+	;;
 alfa-network,ac1200rm|\
 alfa-network,awusfree1|\
 alfa-network,quad-e4g|\
diff --git a/target/linux/ramips/dts/mt7621_actiontec_web7200.dts b/target/linux/ramips/dts/mt7621_actiontec_web7200.dts
new file mode 100644
index 0000000000..eb7dbdd316
--- /dev/null
+++ b/target/linux/ramips/dts/mt7621_actiontec_web7200.dts
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: GPL-2.0-or-later OR MIT
+
+#include "mt7621.dtsi"
+
+#include <dt-bindings/gpio/gpio.h>
+#include <dt-bindings/input/input.h>
+
+/ {
+	compatible = "actiontec,web7200", "mediatek,mt7621-soc";
+	model = "Actiontec WEB7200";
+
+	aliases {
+/*		led-boot = &led_white;
+		led-failsafe = &led_white;
+		led-running = &led_blue;
+		led-upgrade = &led_blue;
+		label-mac-device = &gmac0;*/
+	};
+
+	chosen {
+		bootargs = "console=ttyS0,115200";
+	};
+
+	leds {
+		compatible = "gpio-leds";
+
+		wan_green {
+			label = "wan:green";
+			gpios = <&gpio 14 GPIO_ACTIVE_HIGH>;
+		};
+
+		lan_green {
+			label = "lan:green";
+			gpios = <&gpio 16 GPIO_ACTIVE_HIGH>;
+		};
+	};
+
+	keys {
+		compatible = "gpio-keys";
+
+		reset {
+			label = "reset";
+			gpios = <&gpio 6 GPIO_ACTIVE_LOW>;
+			linux,code = <KEY_RESTART>;
+		};
+
+		wps {
+			label = "wps";
+			gpios = <&gpio 8 GPIO_ACTIVE_LOW>;
+			linux,code = <KEY_WPS_BUTTON>;
+		};
+	};
+
+};
+
+&nand {
+	status = "okay";
+
+	mediatek,nmbm;
+	mediatek,bmt-remap-range =
+		<0x0 0x580000>,
+		<0x3180000 0x3580000>;
+
+	partitions {
+		compatible = "fixed-partitions";
+		#address-cells = <1>;
+		#size-cells = <1>;
+
+		partition@0 {
+			label = "u-boot";
+			reg = <0x00 0x80000>;
+			read-only;
+		};
+
+		partition@80000 {
+			label = "u-boot-env";
+			reg = <0x80000 0x80000>;
+		};
+
+		factory: partition@100000 {
+			label = "factory";
+			reg = <0x100000 0x80000>;
+		};
+
+		partition@180000 {
+			compatible = "denx,fit";
+			label = "firmware";
+			openwrt,cmdline-match = "bootselect=0";
+			reg = <0x180000 0x3000000>;
+		};
+
+		partition@3180000 {
+			compatible = "denx,fit";
+			label = "firmware2";
+			openwrt,cmdline-match = "bootselect=1";
+			reg = <0x3180000 0x3000000>;
+		};
+
+		partition@6980000 {
+			label = "aei_data";
+			reg = <0x6980000 0x400000>;
+		};
+
+		partition@6180000 {
+			label = "aei_data_ext";
+			reg = <0x6180000 0x800000>;
+		};
+	};
+};
+
+&pcie {
+	status = "okay";
+};
+
+&pcie1 {
+	wlan_5g: wifi@0,0 {
+		compatible = "mediatek,mt76";
+		reg = <0x0 0 0 0 0>;
+		mediatek,mtd-eeprom = <&factory 0x0>;
+	};
+};
+
+&i2c {
+	status = "okay";
+};
+
+&switch0 {
+	ports {
+		port@0 {
+			status = "okay";
+			label = "lan";
+		};
+		port@1 {
+			status = "okay";
+			label = "wan";
+		};
+	};
+};
diff --git a/target/linux/ramips/image/mt7621.mk b/target/linux/ramips/image/mt7621.mk
index 2386ca52fc..4721dcb170 100755
--- a/target/linux/ramips/image/mt7621.mk
+++ b/target/linux/ramips/image/mt7621.mk
@@ -251,6 +251,21 @@ define Build/zyxel-nwa-fit
 	@mv $@.new $@
 endef
 
+RELOCATE_LOADADDR = 0x81000000
+
+define Build/fit-relocate
+	$(TOPDIR)/scripts/mkits.sh \
+		-D $(DEVICE_NAME) -o $@.its -k $@ \
+		$(if $(word 2,$(1)),-d $(word 2,$(1))) -C $(word 1,$(1)) \
+		$(if $(word 3,$(1)),-r $(IMAGE_ROOTFS) -f $(subst _,$(comma),$(DEVICE_NAME))) \
+		-a $(RELOCATE_LOADADDR) -e $(RELOCATE_LOADADDR) \
+		$(if $(DEVICE_FDT_NUM),-n $(DEVICE_FDT_NUM)) \
+		-c $(if $(DEVICE_DTS_CONFIG),$(DEVICE_DTS_CONFIG),"config-1") \
+		-A $(LINUX_KARCH) -v $(LINUX_VERSION)
+	PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage $(if $(word 3,$(1)),-E -B 0x1000 -p 0x1000) -f $@.its $@.new
+	@mv $@.new $@
+endef
+
 define Device/dsa-migration
   DEVICE_COMPAT_VERSION := 1.1
   DEVICE_COMPAT_MESSAGE := Config cannot be migrated from swconfig to DSA
@@ -265,6 +280,24 @@ define Device/nand
   IMAGE/sysupgrade.bin := sysupgrade-tar | append-metadata
 endef
 
+define Device/actiontec_web7200
+  $(Device/dsa-migration)
+  DEVICE_VENDOR := Actiontec
+  DEVICE_MODEL := EB7200
+  DEVICE_PACKAGES += kmod-mt7603 kmod-mt7915e kmod-usb3 uboot-envtools kmod-i2c-core
+  LOADER_TYPE := bin
+  KERNEL_SIZE := 4096k
+  BLOCKSIZE := 128k
+  PAGESIZE := 2048
+  UBINIZE_OPTS := -E 5
+  KERNEL_INITRAMFS := kernel-bin | lzma | loader-kernel | gzip | fit-relocate gzip $$(KDIR)/image-$$(firstword $$(DEVICE_DTS)).dtb
+  KERNEL := kernel-bin | relocate-kernel | lzma | fit-relocate lzma $$(KDIR)/image-$$(firstword $$(DEVICE_DTS)).dtb
+  IMAGES += factory.bin
+  IMAGE/sysupgrade.bin := sysupgrade-tar | append-metadata
+  IMAGE/factory.bin := append-kernel | pad-to $$(KERNEL_SIZE) | append-ubi
+endef
+TARGET_DEVICES += actiontec_web7200
+
 define Device/adslr_g7
   $(Device/dsa-migration)
   IMAGE_SIZE := 16064k
diff --git a/target/linux/ramips/mt7621/base-files/etc/board.d/02_network b/target/linux/ramips/mt7621/base-files/etc/board.d/02_network
index f70b2cc74f..2e294389c9 100644
--- a/target/linux/ramips/mt7621/base-files/etc/board.d/02_network
+++ b/target/linux/ramips/mt7621/base-files/etc/board.d/02_network
@@ -67,6 +67,7 @@ ramips_setup_interfaces()
 	zyxel,nwa55axe)
 		ucidef_set_interface_lan "lan"
 		;;
+	actiontec,web7200|\
 	asiarf,ap7621-001|\
 	dna,valokuitu-plus-ex400|\
 	humax,e10|\
@@ -210,6 +211,11 @@ ramips_setup_macs()
 	zbtlink,zbt-wg3526-32m)
 		label_mac=$(mtd_get_mac_binary factory 0x4)
 		;;
+	actiontec,web7200)
+		wan_mac=$(mtd_get_mac_ascii u-boot-env "AeiBaseMACAddr")
+		label_mac=$wan_mac
+		lan_mac=$(macaddr_add "$wan_mac" 1)
+		;;
 	ampedwireless,ally-00x19k)
 		lan_mac=$(mtd_get_mac_ascii hwconfig HW.LAN.MAC.Address)
 		label_mac=$lan_mac
diff --git a/target/linux/ramips/mt7621/base-files/lib/upgrade/platform.sh b/target/linux/ramips/mt7621/base-files/lib/upgrade/platform.sh
index 33d8a14cd0..3fdbaa845c 100755
--- a/target/linux/ramips/mt7621/base-files/lib/upgrade/platform.sh
+++ b/target/linux/ramips/mt7621/base-files/lib/upgrade/platform.sh
@@ -67,6 +67,7 @@ platform_do_upgrade() {
 	esac
 
 	case "$board" in
+	actiontec,web7200|\
 	ampedwireless,ally-00x19k|\
 	ampedwireless,ally-r1900k|\
 	arcadyan,we420223-99|\
-- 
2.30.2