From 2c9aff289e71689c07770efb2ce9e5f1b30960f2 Mon Sep 17 00:00:00 2001
From: Felix Fietkau <nbd@nbd.name>
Date: Tue, 27 Jun 2017 23:26:03 +0200
Subject: [PATCH] ar71xx: add support for Nokia WI2A-AC200i

Specifications:
 - SoC: Qualcomm QCA9558 (720 MHz)
 - RAM: 256MB
 - Storage: 1MB NOR, 128 MB NAND flash
 - Ethernet: 1x1000M

Installation:
 1. Connect to serial console on the board
 2. Boot initramfs image over u-boot
 3. Copy image to the device and run sysupgrade

Installation without serial console is not supported at this time

Signed-off-by: Felix Fietkau <nbd@nbd.name>
---
 .../ar71xx/base-files/etc/board.d/01_leds     |   6 +
 .../ar71xx/base-files/etc/board.d/02_network  |   1 +
 target/linux/ar71xx/base-files/etc/diag.sh    |   6 +
 .../etc/hotplug.d/firmware/11-ath10k-caldata  |   3 +
 target/linux/ar71xx/base-files/lib/ar71xx.sh  |   3 +
 .../ar71xx/base-files/lib/upgrade/platform.sh |  19 ++
 target/linux/ar71xx/config-4.4                |   1 +
 target/linux/ar71xx/config-4.9                |   1 +
 .../files/arch/mips/ath79/Kconfig.openwrt     |  11 +
 .../ar71xx/files/arch/mips/ath79/Makefile     |   1 +
 .../files/arch/mips/ath79/mach-wi2a-ac200i.c  | 217 ++++++++++++++++++
 .../ar71xx/files/arch/mips/ath79/machtypes.h  |   1 +
 target/linux/ar71xx/image/nand.mk             |  15 ++
 target/linux/ar71xx/nand/config-default       |   1 +
 14 files changed, 286 insertions(+)
 create mode 100644 target/linux/ar71xx/files/arch/mips/ath79/mach-wi2a-ac200i.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 80d195301b..0694fe954f 100755
--- a/target/linux/ar71xx/base-files/etc/board.d/01_leds
+++ b/target/linux/ar71xx/base-files/etc/board.d/01_leds
@@ -824,6 +824,12 @@ r6100)
 	ucidef_set_led_usbdev "usb" "USB" "netgear:blue:usb" "1-1"
 	ucidef_set_led_wlan "wlan" "WLAN" "netgear:blue:wlan" "phy1tpt"
 	;;
+wi2a-ac200i)
+	ucidef_set_led_default "power" "Power (green)" "nokia:green:power" "1"
+	ucidef_set_led_default "wan" "Ethernet LED (green)" "nokia:green:wan" "1"
+	ucidef_set_led_wlan "wlan5g" "WLAN" "nokia:green:wlan-5g" "phy0tpt"
+	ucidef_set_led_wlan "wlan2g" "WLAN" "nokia:green:wlan-2g" "phy1tpt"
+	;;
 wndr3700v4|\
 wndr4300)
 	ucidef_set_led_switch "wan-green" "WAN (green)" "netgear:green:wan" "switch0" "0x20" "0x08"
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 d83835204a..46b217136d 100755
--- a/target/linux/ar71xx/base-files/etc/board.d/02_network
+++ b/target/linux/ar71xx/base-files/etc/board.d/02_network
@@ -128,6 +128,7 @@ ar71xx_setup_interfaces()
 	tl-wr902ac-v1|\
 	tube2h|\
 	unifiac-lite|\
+	wi2a-ac200i|\
 	wndap360|\
 	wp543)
 		ucidef_set_interface_lan "eth0"
diff --git a/target/linux/ar71xx/base-files/etc/diag.sh b/target/linux/ar71xx/base-files/etc/diag.sh
index ade726f726..7fa4ff0bcc 100644
--- a/target/linux/ar71xx/base-files/etc/diag.sh
+++ b/target/linux/ar71xx/base-files/etc/diag.sh
@@ -459,6 +459,9 @@ get_status_led() {
 	airgatewaypro)
 		status_led="ubnt:white:status"
 		;;
+	wi2a-ac200i)
+		status_led="nokia:green:ctrl"
+		;;
 	whr-g301n|\
 	whr-hp-g300n|\
 	whr-hp-gn|\
@@ -529,6 +532,9 @@ set_state() {
 			local n=$(fw_printenv activeregion | cut -d = -f 2)
 			fw_setenv "image${n}trynum" 0
 			;;
+		wi2a-ac200i)
+			fw_setenv PKRstCnt 0
+			;;
 		esac
 		;;
 	esac
diff --git a/target/linux/ar71xx/base-files/etc/hotplug.d/firmware/11-ath10k-caldata b/target/linux/ar71xx/base-files/etc/hotplug.d/firmware/11-ath10k-caldata
index 85a2a63022..9a5f82b7cd 100644
--- a/target/linux/ar71xx/base-files/etc/hotplug.d/firmware/11-ath10k-caldata
+++ b/target/linux/ar71xx/base-files/etc/hotplug.d/firmware/11-ath10k-caldata
@@ -111,6 +111,9 @@ case "$FIRMWARE" in
 	unifiac-pro)
 		ath10kcal_extract "EEPROM" 20480 2116
 		;;
+	wi2a-ac200i)
+		ath10kcal_extract "ART" 20480 2116
+		;;
 	esac
 	;;
 "ath10k/cal-pci-0000:01:00.0.bin")
diff --git a/target/linux/ar71xx/base-files/lib/ar71xx.sh b/target/linux/ar71xx/base-files/lib/ar71xx.sh
index 835ced67f3..929845110a 100755
--- a/target/linux/ar71xx/base-files/lib/ar71xx.sh
+++ b/target/linux/ar71xx/base-files/lib/ar71xx.sh
@@ -1231,6 +1231,9 @@ ar71xx_board_detect() {
 	"WeIO"*)
 		name="weio"
 		;;
+	*"WI2A-AC200i")
+		name="wi2a-ac200i"
+		;;
 	*"WHR-G301N")
 		name="whr-g301n"
 		;;
diff --git a/target/linux/ar71xx/base-files/lib/upgrade/platform.sh b/target/linux/ar71xx/base-files/lib/upgrade/platform.sh
index 778faeda5a..97d560189f 100755
--- a/target/linux/ar71xx/base-files/lib/upgrade/platform.sh
+++ b/target/linux/ar71xx/base-files/lib/upgrade/platform.sh
@@ -534,6 +534,7 @@ platform_check_image() {
 	nbg6716|\
 	r6100|\
 	rambutan|\
+	wi2a-ac200i|\
 	wndr3700v4|\
 	wndr4300)
 		nand_do_platform_check $board $1
@@ -708,6 +709,7 @@ platform_pre_upgrade() {
 	rb-2011uias-2hnd|\
 	rb-sxt2n|\
 	rb-sxt5n|\
+	wi2a-ac200i|\
 	wndr3700v4|\
 	wndr4300)
 		nand_do_upgrade "$1"
@@ -743,6 +745,23 @@ platform_nand_pre_upgrade() {
 		mtd erase kernel
 		tar xf "$1" sysupgrade-routerboard/kernel -O | nandwrite -o "$fw_mtd" -
 		;;
+	wi2a-ac200i)
+		case "$(fw_printenv -n dualPartition)" in
+			imgA)
+				fw_setenv dualPartition imgB
+				fw_setenv ActImg NokiaImageB
+			;;
+			imgB)
+				fw_setenv dualPartition imgA
+				fw_setenv ActImg NokiaImageA
+			;;
+		esac
+		ubiblock -r /dev/ubiblock0_0 2>/dev/null >/dev/null
+		rm -f /dev/ubiblock0_0
+		ubidetach -d 0 2>/dev/null >/dev/null
+		CI_UBIPART=ubi_alt
+		CI_KERNPART=kernel_alt
+		;;
 	esac
 }
 
diff --git a/target/linux/ar71xx/config-4.4 b/target/linux/ar71xx/config-4.4
index 0e77560414..4793bf4deb 100644
--- a/target/linux/ar71xx/config-4.4
+++ b/target/linux/ar71xx/config-4.4
@@ -223,6 +223,7 @@ CONFIG_ATH79_MACH_UBNT=y
 CONFIG_ATH79_MACH_UBNT_UNIFIAC=y
 CONFIG_ATH79_MACH_UBNT_XM=y
 CONFIG_ATH79_MACH_WEIO=y
+# CONFIG_ATH79_MACH_WI2A_AC200I is not set
 CONFIG_ATH79_MACH_WHR_HP_G300N=y
 CONFIG_ATH79_MACH_WLAE_AG300N=y
 CONFIG_ATH79_MACH_WLR8100=y
diff --git a/target/linux/ar71xx/config-4.9 b/target/linux/ar71xx/config-4.9
index c5b89088f1..84b2a0b72b 100644
--- a/target/linux/ar71xx/config-4.9
+++ b/target/linux/ar71xx/config-4.9
@@ -221,6 +221,7 @@ CONFIG_ATH79_MACH_UBNT=y
 CONFIG_ATH79_MACH_UBNT_UNIFIAC=y
 CONFIG_ATH79_MACH_UBNT_XM=y
 CONFIG_ATH79_MACH_WEIO=y
+# CONFIG_ATH79_MACH_WI2A_AC200I is not set
 CONFIG_ATH79_MACH_WHR_HP_G300N=y
 CONFIG_ATH79_MACH_WLAE_AG300N=y
 CONFIG_ATH79_MACH_WLR8100=y
diff --git a/target/linux/ar71xx/files/arch/mips/ath79/Kconfig.openwrt b/target/linux/ar71xx/files/arch/mips/ath79/Kconfig.openwrt
index 454aee6268..0b5bae6994 100644
--- a/target/linux/ar71xx/files/arch/mips/ath79/Kconfig.openwrt
+++ b/target/linux/ar71xx/files/arch/mips/ath79/Kconfig.openwrt
@@ -8,6 +8,17 @@ config ATH79_MACH_A60
 	select ATH79_DEV_WMAC
 	select ATH79_DEV_USB
 
+config ATH79_MACH_WI2A_AC200I
+	bool "Nokia WI2A-AC200i support"
+	select SOC_QCA955X
+	select ATH79_DEV_ETH
+	select ATH79_DEV_GPIO_BUTTONS
+	select ATH79_DEV_LEDS_GPIO
+	select ATH79_DEV_M25P80
+	select ATH79_DEV_NFC
+	select ATH79_DEV_WMAC
+	select ATH79_DEV_USB
+
 config ATH79_MACH_ALFA_AP120C
 	bool "ALFA Network AP120C board support"
 	select ATH79_DEV_AP9X_PCI if PCI
diff --git a/target/linux/ar71xx/files/arch/mips/ath79/Makefile b/target/linux/ar71xx/files/arch/mips/ath79/Makefile
index 0975aac8b3..a8d9ddad9c 100644
--- a/target/linux/ar71xx/files/arch/mips/ath79/Makefile
+++ b/target/linux/ar71xx/files/arch/mips/ath79/Makefile
@@ -39,6 +39,7 @@ obj-$(CONFIG_ATH79_ROUTERBOOT)		+= routerboot.o
 # Machines
 #
 obj-$(CONFIG_ATH79_MACH_A60)			+= mach-a60.o
+obj-$(CONFIG_ATH79_MACH_WI2A_AC200I)		+= mach-wi2a-ac200i.o
 obj-$(CONFIG_ATH79_MACH_ALFA_AP120C)		+= mach-alfa-ap120c.o
 obj-$(CONFIG_ATH79_MACH_ALFA_AP96)		+= mach-alfa-ap96.o
 obj-$(CONFIG_ATH79_MACH_ALFA_NX)		+= mach-alfa-nx.o
diff --git a/target/linux/ar71xx/files/arch/mips/ath79/mach-wi2a-ac200i.c b/target/linux/ar71xx/files/arch/mips/ath79/mach-wi2a-ac200i.c
new file mode 100644
index 0000000000..85e0c8fea3
--- /dev/null
+++ b/target/linux/ar71xx/files/arch/mips/ath79/mach-wi2a-ac200i.c
@@ -0,0 +1,217 @@
+/*
+ * Nokia WI2A-AC200i support
+ *
+ * Copyright (c) 2012 Qualcomm Atheros
+ * Copyright (c) 2012-2013 Gabor Juhos <juhosg@openwrt.org>
+ * Copyright (c) 2017 Felix Fietkau <nbd@nbd.name>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+
+#include <linux/platform_device.h>
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/partitions.h>
+#include <linux/platform/ar934x_nfc.h>
+
+#include <asm/mach-ath79/ar71xx_regs.h>
+#include <asm/fw/fw.h>
+
+#include "common.h"
+#include "pci.h"
+#include "dev-ap9x-pci.h"
+#include "dev-gpio-buttons.h"
+#include "dev-eth.h"
+#include "dev-leds-gpio.h"
+#include "dev-m25p80.h"
+#include "dev-nfc.h"
+#include "dev-usb.h"
+#include "dev-wmac.h"
+#include "machtypes.h"
+
+#define AC200I_GPIO_BTN_RESET		17
+
+#define AC200I_KEYS_POLL_INTERVAL	20	/* msecs */
+#define AC200I_KEYS_DEBOUNCE_INTERVAL	(3 * AC200I_KEYS_POLL_INTERVAL)
+
+#define AC200I_MAC_ADDR			0x1f040249
+#define AC200I_MAC1_OFFSET		6
+#define AC200I_WMAC_CALDATA_ADDR	0x1f061000
+
+static struct gpio_led ac200i_leds_gpio[] __initdata = {
+	{
+		.name		= "nokia:red:wlan-2g",
+		.gpio		= 0,
+		.active_low	= 1,
+	},
+	{
+		.name		= "nokia:green:power",
+		.gpio		= 1,
+		.active_low	= 1,
+	},
+	{
+		.name		= "nokia:green:wlan-2g",
+		.gpio		= 2,
+		.active_low	= 1,
+	},
+	{
+		.name		= "nokia:green:ctrl",
+		.gpio		= 3,
+		.active_low	= 1,
+	},
+	{
+		.name		= "nokia:green:eth",
+		.gpio		= 4,
+		.active_low	= 1,
+	},
+	{
+		.name		= "nokia:red:power",
+		.gpio		= 13,
+		.active_low	= 1,
+	},
+	{
+		.name		= "nokia:red:eth",
+		.gpio		= 14,
+		.active_low	= 1,
+	},
+	{
+		.name		= "nokia:red:wlan-5g",
+		.gpio		= 18,
+		.active_low	= 1,
+	},
+	{
+		.name		= "nokia:green:wlan-5g",
+		.gpio		= 19,
+		.active_low	= 1,
+	},
+	{
+		.name		= "nokia:red:ctrl",
+		.gpio		= 20,
+		.active_low	= 1,
+	},
+};
+
+static struct gpio_keys_button ac200i_gpio_keys[] __initdata = {
+	{
+		.desc		= "Reset button",
+		.type		= EV_KEY,
+		.code		= KEY_RESTART,
+		.debounce_interval = AC200I_KEYS_DEBOUNCE_INTERVAL,
+		.gpio		= AC200I_GPIO_BTN_RESET,
+		.active_low	= 1,
+	},
+};
+
+static struct mtd_partition ac200i_nand_partitions[] = {
+	{
+		.name	= "cfg",
+		.offset	= 0x0100000,
+		.size	= 0x1800000,
+		.mask_flags = MTD_WRITEABLE,
+	},
+	{
+		.name	= "kernel",
+		.offset	= 0x2000000,
+		.size	= 0x0400000,
+	},
+	{
+		.name	= "ubi",
+		.offset	= 0x2400000,
+		.size	= 0x2000000,
+	},
+	{
+		.name	= "kernel",
+		.offset	= 0x5000000,
+		.size	= 0x0400000,
+	},
+	{
+		.name	= "ubi",
+		.offset	= 0x5400000,
+		.size	= 0x2000000,
+	},
+};
+
+static const char *boot_getenv(const char *key)
+{
+	const char *start = (const char *) KSEG1ADDR(0x1f070000);
+	const char *end = start + 0x20000;
+	const char *addr;
+
+	for (addr = start + 4;
+	     *addr && *addr != 0xff && addr < end &&
+	     strnlen(addr, end - addr) < end - addr;
+	     addr += strnlen(addr, end - addr) + 1) {
+		const char *val;
+
+		val = strchr(addr, '=');
+		if (!val)
+			continue;
+
+		if (strncmp(addr, key, val - addr))
+			continue;
+
+		return val + 1;
+	}
+	return NULL;
+}
+
+static void __init ac200i_setup(void)
+{
+	const char *img;
+	u8 *wmac = (u8 *) KSEG1ADDR(AC200I_WMAC_CALDATA_ADDR);
+	u8 *mac_addr = (u8 *) KSEG1ADDR(AC200I_MAC_ADDR);
+
+	ath79_register_m25p80(NULL);
+
+	ath79_register_leds_gpio(-1, ARRAY_SIZE(ac200i_leds_gpio),
+				 ac200i_leds_gpio);
+	ath79_register_gpio_keys_polled(-1, AC200I_KEYS_POLL_INTERVAL,
+					ARRAY_SIZE(ac200i_gpio_keys),
+					ac200i_gpio_keys);
+
+	ath79_register_usb();
+	ath79_nfc_set_parts(ac200i_nand_partitions,
+			    ARRAY_SIZE(ac200i_nand_partitions));
+	ath79_nfc_set_ecc_mode(AR934X_NFC_ECC_HW);
+	ath79_register_nfc();
+
+	ath79_register_wmac(wmac, NULL);
+
+	ath79_register_mdio(0, 0x0);
+	ath79_init_mac(ath79_eth0_data.mac_addr, mac_addr, 0);
+
+	/* GMAC0 is connected to the SGMII interface */
+	ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_SGMII;
+	ath79_eth0_data.phy_mask = BIT(4);
+	ath79_eth0_data.mii_bus_dev = &ath79_mdio0_device.dev;
+	ath79_eth0_pll_data.pll_1000 = 0x03000101;
+	ath79_eth0_pll_data.pll_100 = 0x80000101;
+	ath79_eth0_pll_data.pll_10 = 0x80001313;
+
+	img = boot_getenv("dualPartition");
+	if (img && !strcmp(img, "imgA")) {
+		ac200i_nand_partitions[3].name = "kernel_alt";
+		ac200i_nand_partitions[4].name = "ubi_alt";
+	} else {
+		ac200i_nand_partitions[1].name = "kernel_alt";
+		ac200i_nand_partitions[2].name = "ubi_alt";
+	}
+
+	ath79_register_eth(0);
+
+	ath79_register_pci();
+}
+
+MIPS_MACHINE(ATH79_MACH_WI2A_AC200I, "WI2A-AC200i",
+	     "Nokia WI2A-AC200i",
+	     ac200i_setup);
diff --git a/target/linux/ar71xx/files/arch/mips/ath79/machtypes.h b/target/linux/ar71xx/files/arch/mips/ath79/machtypes.h
index 756d406e11..c75ff4f88a 100644
--- a/target/linux/ar71xx/files/arch/mips/ath79/machtypes.h
+++ b/target/linux/ar71xx/files/arch/mips/ath79/machtypes.h
@@ -19,6 +19,7 @@ enum ath79_mach_type {
 	ATH79_MACH_GENERIC = 0,
 	ATH79_MACH_A40,				/* OpenMesh A40 */
 	ATH79_MACH_A60,				/* OpenMesh A60 */
+	ATH79_MACH_WI2A_AC200I,			/* Nokia WI2A-AC200i */
 	ATH79_MACH_ALFA_AP120C,			/* ALFA Network AP120C board */
 	ATH79_MACH_ALFA_AP96,			/* ALFA Network AP96 board */
 	ATH79_MACH_ALFA_NX,			/* ALFA Network N2/N5 board */
diff --git a/target/linux/ar71xx/image/nand.mk b/target/linux/ar71xx/image/nand.mk
index fafccb9b77..ba9e1ba622 100644
--- a/target/linux/ar71xx/image/nand.mk
+++ b/target/linux/ar71xx/image/nand.mk
@@ -84,6 +84,21 @@ define Device/rambutan
 endef
 TARGET_DEVICES += rambutan
 
+define Device/wi2a-ac200i
+  SUPPORTED_DEVICES = $(1)
+  DEVICE_TITLE := Nokia WI2A-AC200i
+  DEVICE_PACKAGES := kmod-usb-core kmod-usb2 kmod-ath9k kmod-ath10k ath10k-firmware-qca988x
+  BOARDNAME := WI2A-AC200i
+  BLOCKSIZE := 64k
+  KERNEL_SIZE := 3648k
+  IMAGE_SIZE := 32m
+  IMAGES := sysupgrade.tar
+  MTDPARTS := spi0.0:256k(u-boot)ro,64k(envFacA)ro,64k(envFacB)ro,64k(ART)ro,128k(u-boot-env)
+  KERNEL := kernel-bin | patch-cmdline | lzma | uImage lzma
+  IMAGE/sysupgrade.tar := sysupgrade-tar | append-metadata
+endef
+TARGET_DEVICES += wi2a-ac200i
+
 define Device/z1
   DEVICE_TITLE := Meraki Z1
   DEVICE_PACKAGES := kmod-usb-core kmod-usb2 kmod-usb-ledtrig-usbport kmod-spi-gpio kmod-ath9k kmod-owl-loader
diff --git a/target/linux/ar71xx/nand/config-default b/target/linux/ar71xx/nand/config-default
index fc243c5008..340d64cab9 100644
--- a/target/linux/ar71xx/nand/config-default
+++ b/target/linux/ar71xx/nand/config-default
@@ -180,6 +180,7 @@ CONFIG_ATH79_MACH_RAMBUTAN=y
 # CONFIG_ATH79_MACH_UBNT_XM is not set
 # CONFIG_ATH79_MACH_WEIO is not set
 # CONFIG_ATH79_MACH_WHR_HP_G300N is not set
+CONFIG_ATH79_MACH_WI2A_AC200I=y
 # CONFIG_ATH79_MACH_WLAE_AG300N is not set
 # CONFIG_ATH79_MACH_WLR8100 is not set
 # CONFIG_ATH79_MACH_WNDAP360 is not set
-- 
2.30.2