From: Sven Eckelmann <sven.eckelmann@open-mesh.com>
Date: Fri, 8 Sep 2017 15:45:05 +0200
Subject: [PATCH] ar71xx: Add basic support for TP-Link CP210 V2.0
---
 target/linux/ar71xx/base-files/etc/board.d/01_leds |   1 +
 .../linux/ar71xx/base-files/etc/board.d/02_network |   1 +
 target/linux/ar71xx/base-files/lib/ar71xx.sh       |   4 +
 .../ar71xx/base-files/lib/upgrade/platform.sh      |   1 +
 target/linux/ar71xx/config-4.4                     |   1 +
 .../ar71xx/files/arch/mips/ath79/Kconfig.openwrt   |  10 ++
 target/linux/ar71xx/files/arch/mips/ath79/Makefile |   1 +
 .../ar71xx/files/arch/mips/ath79/mach-cpe210-v2.c  | 132 +++++++++++++++++++++
 .../linux/ar71xx/files/arch/mips/ath79/machtypes.h |   3 +-
 target/linux/ar71xx/image/tp-link.mk               |  17 ++-
 target/linux/ar71xx/mikrotik/config-default        |   1 +
 target/linux/ar71xx/nand/config-default            |   1 +
 tools/firmware-utils/src/tplink-safeloader.c       |  33 ++++++
 13 files changed, 204 insertions(+), 2 deletions(-)
 create mode 100644 target/linux/ar71xx/files/arch/mips/ath79/mach-cpe210-v2.c

diff --git a/target/linux/ar71xx/base-files/etc/board.d/01_leds b/target/linux/ar71xx/base-files/etc/board.d/01_leds
index 4132e919e433c1dcb2a7e76959d03ba1781be55e..b0eb7a28f2862daf72479d78d77e8c19eff6dc2b 100755
--- a/target/linux/ar71xx/base-files/etc/board.d/01_leds
+++ b/target/linux/ar71xx/base-files/etc/board.d/01_leds
@@ -195,6 +195,7 @@ cf-e530n)
 	ucidef_set_led_netdev "wan" "WAN" "$board:blue:wan" "eth1"
 	;;
 cpe210|\
+cpe210-v2|\
 cpe510|\
 wbs210|\
 wbs510)
diff --git a/target/linux/ar71xx/base-files/etc/board.d/02_network b/target/linux/ar71xx/base-files/etc/board.d/02_network
index ba54d89b8fc447d1df32a441ac7d8d2939e69dc7..00702f6681bec54809c85a5512c05bbb28aabd39 100755
--- a/target/linux/ar71xx/base-files/etc/board.d/02_network
+++ b/target/linux/ar71xx/base-files/etc/board.d/02_network
@@ -251,6 +251,7 @@ ar71xx_setup_interfaces()
 		ucidef_set_interface_lan "eth0" "dhcp"
 		;;
 	cpe210|\
+	cpe210-v2|\
 	cpe510|\
 	wbs210|\
 	wbs510)
diff --git a/target/linux/ar71xx/base-files/lib/ar71xx.sh b/target/linux/ar71xx/base-files/lib/ar71xx.sh
index 7fd5fb452281dfde79f5c28907fb32f402aa5626..f72abf8ed8efb8f4086c648dbb34458347be553a 100755
--- a/target/linux/ar71xx/base-files/lib/ar71xx.sh
+++ b/target/linux/ar71xx/base-files/lib/ar71xx.sh
@@ -523,6 +523,10 @@ ar71xx_board_detect() {
 		name="cpe210"
 		tplink_pharos_board_detect
 		;;
+	*"CPE210-V2")
+		name="cpe210-v2"
+		tplink_pharos_board_detect
+		;;
 	*"CPE505N")
 		name="cpe505n"
 		;;
diff --git a/target/linux/ar71xx/base-files/lib/upgrade/platform.sh b/target/linux/ar71xx/base-files/lib/upgrade/platform.sh
index 05b5634aa51dfeae0f53ba274fd64dde611fd5a7..d4367dc6f349cf80f41c9fe81df5dbe38ed748a3 100755
--- a/target/linux/ar71xx/base-files/lib/upgrade/platform.sh
+++ b/target/linux/ar71xx/base-files/lib/upgrade/platform.sh
@@ -537,6 +537,7 @@ platform_check_image() {
 		nand_do_platform_check $board $1
 		return $?
 		;;
+	cpe210-v2|\
 	cpe210|\
 	cpe510|\
 	eap120|\
diff --git a/target/linux/ar71xx/config-4.4 b/target/linux/ar71xx/config-4.4
index 70793af3a357f17d3b6918b07237df5fbb510cbf..c3b03ab0b7b3f03b94cbbad8f5963ec8996972a9 100644
--- a/target/linux/ar71xx/config-4.4
+++ b/target/linux/ar71xx/config-4.4
@@ -74,6 +74,7 @@ CONFIG_ATH79_MACH_CF_E380AC_V2=y
 CONFIG_ATH79_MACH_CF_E520N=y
 CONFIG_ATH79_MACH_CF_E530N=y
 CONFIG_ATH79_MACH_CPE505N=y
+CONFIG_ATH79_MACH_CPE210_V2=y
 CONFIG_ATH79_MACH_CPE510=y
 CONFIG_ATH79_MACH_CPE830=y
 CONFIG_ATH79_MACH_CPE870=y
diff --git a/target/linux/ar71xx/files/arch/mips/ath79/Kconfig.openwrt b/target/linux/ar71xx/files/arch/mips/ath79/Kconfig.openwrt
index ff9c3037a1424af8224f96b88a8024098ba05ee0..976d695157c33ec903dceed25c1710ca71d37d67 100644
--- a/target/linux/ar71xx/files/arch/mips/ath79/Kconfig.openwrt
+++ b/target/linux/ar71xx/files/arch/mips/ath79/Kconfig.openwrt
@@ -1389,6 +1389,16 @@ config ATH79_MACH_CPE505N
 	select ATH79_DEV_M25P80
 	select ATH79_DEV_WMAC
 
+config ATH79_MACH_CPE210_V2
+	bool "TP-LINK CPE210 V2 support"
+	select SOC_QCA953X
+	select ATH79_DEV_GPIO_BUTTONS
+	select ATH79_DEV_LEDS_GPIO
+	select ATH79_DEV_SPI
+	select ATH79_DEV_WMAC
+	select ATH79_DEV_ETH
+	select ATH79_DEV_M25P80
+
 config ATH79_MACH_CPE510
 	bool "TP-LINK CPE510 support"
 	select SOC_AR934X
diff --git a/target/linux/ar71xx/files/arch/mips/ath79/Makefile b/target/linux/ar71xx/files/arch/mips/ath79/Makefile
index 469b6a794c669d1db0daa49f8c829e8d3fe81c24..c77e3ee3e37c465a93a7acd3266460bf7b33371b 100644
--- a/target/linux/ar71xx/files/arch/mips/ath79/Makefile
+++ b/target/linux/ar71xx/files/arch/mips/ath79/Makefile
@@ -79,6 +79,7 @@ obj-$(CONFIG_ATH79_MACH_CF_E380AC_V2)		+= mach-cf-e316n-v2.o
 obj-$(CONFIG_ATH79_MACH_CF_E520N)		+= mach-cf-e316n-v2.o
 obj-$(CONFIG_ATH79_MACH_CF_E530N)		+= mach-cf-e316n-v2.o
 obj-$(CONFIG_ATH79_MACH_CPE505N)		+= mach-r602n.o
+obj-$(CONFIG_ATH79_MACH_CPE210_V2)		+= mach-cpe210-v2.o
 obj-$(CONFIG_ATH79_MACH_CPE510)			+= mach-cpe510.o
 obj-$(CONFIG_ATH79_MACH_CPE830)			+= mach-ap90q.o
 obj-$(CONFIG_ATH79_MACH_CPE870)			+= mach-cpe870.o
diff --git a/target/linux/ar71xx/files/arch/mips/ath79/mach-cpe210-v2.c b/target/linux/ar71xx/files/arch/mips/ath79/mach-cpe210-v2.c
new file mode 100644
index 0000000000000000000000000000000000000000..33270a315e0914aaa57e2aa5768b71f0eb88cece
--- /dev/null
+++ b/target/linux/ar71xx/files/arch/mips/ath79/mach-cpe210-v2.c
@@ -0,0 +1,132 @@
+/*
+ * TP-LINK CPE210-V2 board support
+ *
+ *  Copyright (C) 2017 Sven Eckelmann <sven@narfation.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify it
+ *  under the terms of the GNU General Public License version 2 as published
+ *  by the Free Software Foundation.
+ */
+
+#include <linux/platform_device.h>
+#include <linux/ath9k_platform.h>
+#include <linux/ar8216_platform.h>
+
+#include <asm/mach-ath79/ar71xx_regs.h>
+
+#include "common.h"
+#include "dev-eth.h"
+#include "dev-gpio-buttons.h"
+#include "dev-leds-gpio.h"
+#include "dev-m25p80.h"
+#include "dev-spi.h"
+#include "dev-usb.h"
+#include "dev-wmac.h"
+#include "machtypes.h"
+
+#define CPE210_V2_GPIO_LED_WLAN		12
+#define CPE210_V2_GPIO_LED_WPS		13
+#define CPE210_V2_GPIO_LED_STATUS		13
+
+#define CPE210_V2_GPIO_LED_WAN		4
+#define CPE210_V2_GPIO_LED_LAN1		16
+#define CPE210_V2_GPIO_LED_LAN2		15
+#define CPE210_V2_GPIO_LED_LAN3		14
+#define CPE210_V2_GPIO_LED_LAN4		11
+
+#define CPE210_V2_GPIO_BTN_WPS		17
+
+#define CPE210_V2_KEYS_POLL_INTERVAL	20	/* msecs */
+#define CPE210_V2_KEYS_DEBOUNCE_INTERVAL	(3 * CPE210_V2_KEYS_POLL_INTERVAL)
+
+#define CPE210_V2_MAC0_OFFSET		0
+#define CPE210_V2_MAC1_OFFSET		6
+#define CPE210_V2_WMAC_CALDATA_OFFSET	0x1000
+
+/*
+static struct gpio_led cpe210_v2_leds_gpio[] __initdata = {
+	{
+		.name		= "cpe210_v2:green:status",
+		.gpio		= CPE210_V2_GPIO_LED_STATUS,
+		.active_low	= 1,
+	},
+	{
+		.name		= "cpe210_v2:green:wlan",
+		.gpio		= CPE210_V2_GPIO_LED_WLAN,
+		.active_low	= 1,
+	}
+};
+
+static struct gpio_keys_button cpe210_v2_gpio_keys[] __initdata = {
+	{
+		.desc		= "WPS button",
+		.type		= EV_KEY,
+		.code		= KEY_WPS_BUTTON,
+		.debounce_interval = CPE210_V2_KEYS_DEBOUNCE_INTERVAL,
+		.gpio		= CPE210_V2_GPIO_BTN_WPS,
+		.active_low	= 1,
+	},
+};
+
+static void __init cpe210_v2_gpio_led_setup(void)
+{
+	ath79_gpio_direction_select(CPE210_V2_GPIO_LED_WAN, true);
+	ath79_gpio_direction_select(CPE210_V2_GPIO_LED_LAN1, true);
+	ath79_gpio_direction_select(CPE210_V2_GPIO_LED_LAN2, true);
+	ath79_gpio_direction_select(CPE210_V2_GPIO_LED_LAN3, true);
+	ath79_gpio_direction_select(CPE210_V2_GPIO_LED_LAN4, true);
+
+	ath79_gpio_output_select(CPE210_V2_GPIO_LED_WAN,
+			QCA953X_GPIO_OUT_MUX_LED_LINK5);
+	ath79_gpio_output_select(CPE210_V2_GPIO_LED_LAN1,
+			QCA953X_GPIO_OUT_MUX_LED_LINK1);
+	ath79_gpio_output_select(CPE210_V2_GPIO_LED_LAN2,
+			QCA953X_GPIO_OUT_MUX_LED_LINK2);
+	ath79_gpio_output_select(CPE210_V2_GPIO_LED_LAN3,
+			QCA953X_GPIO_OUT_MUX_LED_LINK3);
+	ath79_gpio_output_select(CPE210_V2_GPIO_LED_LAN4,
+			QCA953X_GPIO_OUT_MUX_LED_LINK4);
+
+	ath79_register_leds_gpio(-1, ARRAY_SIZE(cpe210_v2_leds_gpio),
+			cpe210_v2_leds_gpio);
+	ath79_register_gpio_keys_polled(-1, CPE210_V2_KEYS_POLL_INTERVAL,
+			ARRAY_SIZE(cpe210_v2_gpio_keys),
+			cpe210_v2_gpio_keys);
+}
+*/
+
+static void __init cpe210_v2_setup(void)
+{
+	u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);
+
+	ath79_register_m25p80(NULL);
+
+	//cpe210_v2_gpio_led_setup();
+
+	ath79_wmac_set_led_pin(CPE210_V2_GPIO_LED_WLAN);
+	ath79_register_wmac(art + CPE210_V2_WMAC_CALDATA_OFFSET, NULL);
+
+	ath79_register_mdio(0, 0x0);
+	ath79_register_mdio(1, 0x0);
+
+	ath79_init_mac(ath79_eth0_data.mac_addr, art + CPE210_V2_MAC0_OFFSET, 0);
+	ath79_init_mac(ath79_eth1_data.mac_addr, art + CPE210_V2_MAC1_OFFSET, 0);
+
+	/* WAN port */
+	ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_MII;
+	ath79_eth0_data.speed = SPEED_100;
+	ath79_eth0_data.duplex = DUPLEX_FULL;
+	ath79_eth0_data.phy_mask = BIT(4);
+	ath79_register_eth(0);
+
+	/* LAN ports */
+	ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
+	ath79_eth1_data.speed = SPEED_1000;
+	ath79_eth1_data.duplex = DUPLEX_FULL;
+	ath79_switch_data.phy_poll_mask |= BIT(4);
+	ath79_switch_data.phy4_mii_en = 1;
+	ath79_register_eth(1);
+}
+
+MIPS_MACHINE(ATH79_MACH_CPE210_V2, "CPE210-V2", "TP-LINK CPE210 V2",
+	     cpe210_v2_setup);
diff --git a/target/linux/ar71xx/files/arch/mips/ath79/machtypes.h b/target/linux/ar71xx/files/arch/mips/ath79/machtypes.h
index 5bc50a42ffd718433e60f178e8f451bfcbb2ffd0..0f6829bcc13ee6ee7a5c796c39c35f188a6e72b8 100644
--- a/target/linux/ar71xx/files/arch/mips/ath79/machtypes.h
+++ b/target/linux/ar71xx/files/arch/mips/ath79/machtypes.h
@@ -64,7 +64,8 @@ enum ath79_mach_type {
 	ATH79_MACH_CF_E380AC_V2,		/* COMFAST CF-E380AC v2 */
 	ATH79_MACH_CF_E520N,			/* COMFAST CF-E520N */
 	ATH79_MACH_CF_E530N,			/* COMFAST CF-E530N */
-	ATH79_MACH_CPE210,			/* TP-LINK CPE210 */
+	ATH79_MACH_CPE210,			/* TP-LINK CPE210 V1.x */
+	ATH79_MACH_CPE210_V2,			/* TP-LINK CPE210 V2.0 */
 	ATH79_MACH_CPE505N,			/* P&W CPE505N */
 	ATH79_MACH_CPE510,			/* TP-LINK CPE510 */
 	ATH79_MACH_CPE830,			/* YunCore CPE830 */
diff --git a/target/linux/ar71xx/image/tp-link.mk b/target/linux/ar71xx/image/tp-link.mk
index 35d07ee40492ade8eaf2fbe8b510d53bd3e63bcf..58987893a08863c857135dfe3772bb8ec309f437 100644
--- a/target/linux/ar71xx/image/tp-link.mk
+++ b/target/linux/ar71xx/image/tp-link.mk
@@ -224,6 +224,21 @@ define Device/cpe210-220-v1
   TPLINK_BOARD_ID := CPE210
 endef
 
+define Device/cpe210-v2
+  DEVICE_TITLE := TP-LINK CPE210 v2
+  DEVICE_PACKAGES := rssileds
+  MTDPARTS := spi0.0:128k(u-boot)ro,64k(pation-table)ro,64k(product-info)ro,1536k(kernel),6144k(rootfs),192k(config)ro,64k(ART)ro,7680k@0x40000(firmware)
+  IMAGE_SIZE := 7680k
+  BOARDNAME := CPE210-V2
+  TPLINK_BOARD_ID := CPE210
+  DEVICE_PROFILE := CPE210-V2
+  LOADER_TYPE := elf
+  KERNEL := kernel-bin | patch-cmdline | lzma | loader-kernel
+  IMAGES := sysupgrade.bin factory.bin
+  IMAGE/sysupgrade.bin := append-rootfs | tplink-safeloader sysupgrade
+  IMAGE/factory.bin := append-rootfs | tplink-safeloader factory
+endef
+
 define Device/wbs210-v1
   $(Device/cpe510-520-v1)
   DEVICE_TITLE := TP-LINK WBS210 v1
@@ -237,7 +252,7 @@ define Device/wbs510-v1
   BOARDNAME := WBS510
   TPLINK_BOARD_ID := WBS510
 endef
-TARGET_DEVICES += cpe210-220-v1 cpe510-520-v1 wbs210-v1 wbs510-v1
+TARGET_DEVICES += cpe210-220-v1 cpe210-v2 cpe510-520-v1 wbs210-v1 wbs510-v1
 
 define Device/eap120-v1
   DEVICE_TITLE := TP-LINK EAP120 v1
diff --git a/target/linux/ar71xx/mikrotik/config-default b/target/linux/ar71xx/mikrotik/config-default
index 34c2fc1f0fd3c6a75deb70801352a5c3b710e260..7edd71963c65012ea2052ea5b64eb0edc0dac80c 100644
--- a/target/linux/ar71xx/mikrotik/config-default
+++ b/target/linux/ar71xx/mikrotik/config-default
@@ -39,6 +39,7 @@
 # CONFIG_ATH79_MACH_CF_E520N is not set
 # CONFIG_ATH79_MACH_CF_E530N is not set
 # CONFIG_ATH79_MACH_CPE505N is not set
+# CONFIG_ATH79_MACH_CPE210_V2 is not set
 # CONFIG_ATH79_MACH_CPE510 is not set
 # CONFIG_ATH79_MACH_CPE830 is not set
 # CONFIG_ATH79_MACH_CPE870 is not set
diff --git a/target/linux/ar71xx/nand/config-default b/target/linux/ar71xx/nand/config-default
index 57318b38e8bf051063482264ee4a00ad19a57d05..c0ae23645286c87d1a0f18680c38947dc7603add 100644
--- a/target/linux/ar71xx/nand/config-default
+++ b/target/linux/ar71xx/nand/config-default
@@ -39,6 +39,7 @@ CONFIG_ATH79_MACH_C60=y
 # CONFIG_ATH79_MACH_CF_E520N is not set
 # CONFIG_ATH79_MACH_CF_E530N is not set
 # CONFIG_ATH79_MACH_CPE505N is not set
+# CONFIG_ATH79_MACH_CPE210_V2 is not set
 # CONFIG_ATH79_MACH_CPE510 is not set
 # CONFIG_ATH79_MACH_CPE830 is not set
 # CONFIG_ATH79_MACH_CPE870 is not set
diff --git a/tools/firmware-utils/src/tplink-safeloader.c b/tools/firmware-utils/src/tplink-safeloader.c
index dceba75da56d8c39eb654c68b1ec2277ed2c872e..fc3b08f0610bec5cbeacd46cbf8b56580d0af4e0 100644
--- a/tools/firmware-utils/src/tplink-safeloader.c
+++ b/tools/firmware-utils/src/tplink-safeloader.c
@@ -154,6 +154,39 @@ static struct device_info boards[] = {
 		.last_sysupgrade_partition = "file-system",
 	},
 
+	/** Firmware layout for the CPE210 V2 */
+	{
+		.id     = "CPE210v2",
+		.vendor = "CPE210(TP-LINK|EU|N300-2):2.0",
+		.support_list =
+			"SupportList:\r\n"
+			"CPE210(TP-LINK|EU|N300-2|45550000):2.0\r\n",
+		.support_trail = '\xff',
+		.soft_ver = NULL,
+
+		.partitions = {
+			{"fs-uboot", 0x00000, 0x20000},
+			{"partition-table", 0x20000, 0x02000},
+			{"default-mac", 0x30000, 0x00020},
+			{"product-info", 0x31100, 0x00100},
+			{"device-info",0x31400, 0x00400},
+			{"signature", 0x32000, 0x00400},
+			{"device-id", 0x33000, 0x00100},
+			{"os-image", 0x40000, 0x170000},
+			{"soft-version", 0x1b0000, 0x00100},
+			{"support-list", 0x1b1000, 0x01000},
+			{"file-system", 0x1c0000, 0x600000},
+			{"user-config", 0x7c0000, 0x10000},
+			{"default-config", 0x7d0000, 0x10000},
+			{"log", 0x7e0000, 0x10000},
+			{"radio", 0x7f0000, 0x10000},
+			{NULL, 0, 0}
+		},
+
+		.first_sysupgrade_partition = "os-image",
+		.last_sysupgrade_partition = "file-system",
+	},
+
 	/** Firmware layout for the CPE510/520 */
 	{
 		.id	= "CPE510",
