From d03c89838eca297dd6c364d3bccfb6bab440d7fb Mon Sep 17 00:00:00 2001
From: Matt Merhar <mattmerhar@protonmail.com>
Date: Mon, 27 Aug 2018 09:59:09 -0400
Subject: [PATCH] ath79: initial support for Ubiquiti RouterStation and
 RouterStation Pro
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

This adds a shared ar7161_ubnt_routerstation.dtsi as well as two other
.dts files that utilize it, ar7161_ubnt_routerstation.dts and
ar7161_ubnt_routerstation-pro.dts.

The modifications to generic-ubnt.mk, config-default, and base-files
necessary for image generation, parsing RedBoot FIS partitions, network
configuration, and sysupgrade are also included.

This reintroduces vital bits from platform_do_upgrade_combined() and its
supporting functions to /lib/upgrade/platform.sh, which were previously
removed from ath79 in 3e9d9f62258f80298710441e0db557e59e152dcf "ath79:
sysupgrade: drop unused platform checks". The new function is called
"routerstation_do_upgrade" and will *only* work for the RouterStation
series of boards. It does however retain the ability to downgrade (e.g.
from master -> 17.01.x using sysupgrade -F).

All hardware is functional including the AR8216 switch (for the Pro),
wireless via ath5k/ath9k using the miniPCI slots, flash, USB, button,
and LED.

Switch and LAN/WAN configuration is the same as it is with the
equivalent ar71xx targets. MAC addresses are assigned based upon the
content stored in the RedBoot config partition.

Flashing via both sysupgrade and TFTP has been confirmed to work. Also,
the initramfs images are now raw .bin files instead of being wrapped in
a uImage (as they currently are in ar71xx), which makes them bootable
with RedBoot.

One notable difference to ar71xx is the inclusion of the RedBoot
"fconfig" utility (analogous to U-Boot’s fw_printenv/fw_setenv) in
DEVICE_PACKAGES. The FIS partitions are probed using the RedBoot MTD
parser’s DT binding, whose proper usage is mutually exclusive to
defining a separate fixed-partitions node for "RedBoot config". This
config partition contains the board's base MAC address. The lack of a
hard-coded flash location means that the mtd-mac-address property cannot
be used in the .dts, so instead fconfig is used to read the MAC
addresses from flash in userspace during first boot.

Signed-off-by: Tomasz Maciej Nowak <tomek_n@o2.pl>
Signed-off-by: Matt Merhar <mattmerhar@protonmail.com>
---
 .../ath79/base-files/etc/board.d/02_network   |  13 +++
 .../ath79/base-files/lib/upgrade/platform.sh  |  13 +++
 .../dts/ar7161_ubnt_routerstation-pro.dts     |  33 ++++++
 .../ath79/dts/ar7161_ubnt_routerstation.dts   |  27 +++++
 .../ath79/dts/ar7161_ubnt_routerstation.dtsi  | 106 ++++++++++++++++++
 target/linux/ath79/generic/config-default     |   1 +
 target/linux/ath79/image/generic-ubnt.mk      |  30 +++++
 7 files changed, 223 insertions(+)
 create mode 100644 target/linux/ath79/dts/ar7161_ubnt_routerstation-pro.dts
 create mode 100644 target/linux/ath79/dts/ar7161_ubnt_routerstation.dts
 create mode 100644 target/linux/ath79/dts/ar7161_ubnt_routerstation.dtsi

diff --git a/target/linux/ath79/base-files/etc/board.d/02_network b/target/linux/ath79/base-files/etc/board.d/02_network
index 1a83d56cc7..14f6c59822 100755
--- a/target/linux/ath79/base-files/etc/board.d/02_network
+++ b/target/linux/ath79/base-files/etc/board.d/02_network
@@ -141,6 +141,14 @@ ath79_setup_interfaces()
 	tplink,tl-wr941-v2)
 		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4" "wan"
 		;;
+	ubnt,routerstation)
+		ucidef_set_interfaces_lan_wan "eth1" "eth0"
+		;;
+	ubnt,routerstation-pro)
+		ucidef_set_interface_wan "eth0"
+		ucidef_add_switch "switch0" \
+			"0@eth1" "2:lan:3" "3:lan:2" "4:lan:1"
+		;;
 	ubnt,unifiac-mesh-pro|\
 	ubnt,unifiac-pro)
 		ucidef_add_switch "switch0" \
@@ -194,6 +202,11 @@ ath79_setup_macs()
 		base_mac=$(mtd_get_mac_binary u-boot 130048)
 		wan_mac=$(macaddr_add "$base_mac" 1)
 		;;
+	ubnt,routerstation|\
+	ubnt,routerstation-pro)
+		wan_mac=$(fconfig -s -r -d $(find_mtd_part "RedBoot config") -n ar7100_esa)
+		lan_mac=$(macaddr_add "$wan_mac" 1)
+		;;
 	esac
 
 	[ -n "$lan_mac" ] && ucidef_set_interface_macaddr "lan" $lan_mac
diff --git a/target/linux/ath79/base-files/lib/upgrade/platform.sh b/target/linux/ath79/base-files/lib/upgrade/platform.sh
index 08039e0052..806b3f0787 100644
--- a/target/linux/ath79/base-files/lib/upgrade/platform.sh
+++ b/target/linux/ath79/base-files/lib/upgrade/platform.sh
@@ -5,6 +5,15 @@
 PART_NAME=firmware
 REQUIRE_IMAGE_METADATA=1
 
+routerstation_do_upgrade() {
+	local append
+	local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
+
+	[ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
+	dd if="$1" bs=64k skip=1 2>/dev/null | \
+		mtd -r $append -Fkernel:$kern_length:0x80060000,rootfs write - kernel:rootfs
+}
+
 platform_check_image() {
 	return 0
 }
@@ -17,6 +26,10 @@ platform_do_upgrade() {
 		PART_NAME="rootfs:kernel"
 		default_do_upgrade "$ARGV"
 		;;
+	ubnt,routerstation|\
+	ubnt,routerstation-pro)
+		routerstation_do_upgrade "$ARGV"
+		;;
 	*)
 		default_do_upgrade "$ARGV"
 		;;
diff --git a/target/linux/ath79/dts/ar7161_ubnt_routerstation-pro.dts b/target/linux/ath79/dts/ar7161_ubnt_routerstation-pro.dts
new file mode 100644
index 0000000000..123d1efbf1
--- /dev/null
+++ b/target/linux/ath79/dts/ar7161_ubnt_routerstation-pro.dts
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0-or-later OR MIT
+/dts-v1/;
+
+#include "ar7161_ubnt_routerstation.dtsi"
+
+/ {
+	compatible = "ubnt,routerstation-pro", "qca,ar7161";
+	model = "Ubiquiti RouterStation Pro";
+};
+
+&mdio0 {
+	status = "okay";
+
+	phy0: ethernet-phy@0 {
+		reg = <0>;
+	};
+
+	phy4: ethernet-phy@4 {
+		reg = <4>;
+	};
+};
+
+&eth0 {
+	compatible = "qca,ar7100-eth", "syscon";
+	phy-mode = "rgmii";
+	phy-handle = <&phy4>;
+};
+
+&eth1 {
+	compatible = "qca,ar7100-eth", "syscon";
+	phy-mode = "rgmii";
+	phy-handle = <&phy0>;
+};
diff --git a/target/linux/ath79/dts/ar7161_ubnt_routerstation.dts b/target/linux/ath79/dts/ar7161_ubnt_routerstation.dts
new file mode 100644
index 0000000000..63b46b9c79
--- /dev/null
+++ b/target/linux/ath79/dts/ar7161_ubnt_routerstation.dts
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0-or-later OR MIT
+/dts-v1/;
+
+#include "ar7161_ubnt_routerstation.dtsi"
+
+/ {
+	compatible = "ubnt,routerstation", "qca,ar7161";
+	model = "Ubiquiti Networks RouterStation";
+};
+
+&eth0 {
+	phy-mode = "mii";
+
+	fixed-link {
+		speed = <100>;
+		full-duplex;
+	};
+};
+
+&eth1 {
+	phy-mode = "rmii";
+
+	fixed-link {
+		speed = <100>;
+		full-duplex;
+	};
+};
diff --git a/target/linux/ath79/dts/ar7161_ubnt_routerstation.dtsi b/target/linux/ath79/dts/ar7161_ubnt_routerstation.dtsi
new file mode 100644
index 0000000000..143a4b7e80
--- /dev/null
+++ b/target/linux/ath79/dts/ar7161_ubnt_routerstation.dtsi
@@ -0,0 +1,106 @@
+// 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 "ar7100.dtsi"
+
+/ {
+	chosen {
+		bootargs = "console=ttyS0,115200";
+	};
+
+	aliases {
+		led-boot = &rf;
+		led-failsafe = &rf;
+		led-running = &rf;
+		led-upgrade = &rf;
+	};
+
+	extosc: ref {
+		compatible = "fixed-clock";
+		#clock-cells = <0>;
+		clock-output-names = "ref";
+		clock-frequency = <40000000>;
+	};
+
+	gpio-leds {
+		compatible = "gpio-leds";
+
+		rf: rf_green {
+			label = "ubnt:green:rf";
+			gpios = <&gpio 2 GPIO_ACTIVE_HIGH>;
+			default-state = "off";
+		};
+	};
+
+	gpio-keys-polled {
+		compatible = "gpio-keys-polled";
+		poll-interval = <20>;
+		debounce-interval = <60>;
+
+		wps {
+			label = "sw4";
+			linux,code = <KEY_RESTART>;
+			gpios = <&gpio 8 GPIO_ACTIVE_LOW>;
+		};
+	};
+};
+
+&eth0 {
+	status = "okay";
+};
+
+&eth1 {
+	status = "okay";
+};
+
+&pcie0 {
+	status = "okay";
+};
+
+&spi {
+	status = "okay";
+	num-cs = <1>;
+
+	flash@0 {
+		compatible = "jedec,spi-nor";
+		reg = <0>;
+		spi-max-frequency = <25000000>;
+
+		partitions {
+			compatible = "ecoscentric,redboot-fis-partitions";
+		};
+	};
+};
+
+&uart {
+	status = "okay";
+};
+
+&usb_phy {
+	status = "okay";
+};
+
+&usb1 {
+	status = "okay";
+	#address-cells = <1>;
+	#size-cells = <0>;
+
+	usb_ochi_port: port@1 {
+		reg = <1>;
+		#trigger-source-cells = <0>;
+	};
+};
+
+&usb2 {
+	status = "okay";
+	#address-cells = <1>;
+	#size-cells = <0>;
+
+	usb_echi_port: port@1 {
+		reg = <1>;
+		#trigger-source-cells = <0>;
+	};
+};
diff --git a/target/linux/ath79/generic/config-default b/target/linux/ath79/generic/config-default
index a7d0d89aa9..b2194a4132 100644
--- a/target/linux/ath79/generic/config-default
+++ b/target/linux/ath79/generic/config-default
@@ -5,6 +5,7 @@ CONFIG_IP17XX_PHY=y
 CONFIG_LEDS_RESET=y
 CONFIG_MARVELL_PHY=y
 CONFIG_MICREL_PHY=y
+CONFIG_MTD_REDBOOT_PARTS=y
 CONFIG_MTD_SPLIT_EVA_FW=y
 CONFIG_OF_ADDRESS_PCI=y
 CONFIG_OF_PCI=y
diff --git a/target/linux/ath79/image/generic-ubnt.mk b/target/linux/ath79/image/generic-ubnt.mk
index 4681996a7b..49f5fcc4f2 100644
--- a/target/linux/ath79/image/generic-ubnt.mk
+++ b/target/linux/ath79/image/generic-ubnt.mk
@@ -117,3 +117,33 @@ define Device/ubnt_unifiac-pro
   SUPPORTED_DEVICES += ubnt-unifiac-pro
 endef
 TARGET_DEVICES += ubnt_unifiac-pro
+
+define Device/ubnt_routerstation_common
+  DEVICE_PACKAGES := -kmod-ath9k -wpad-mini -uboot-envtools kmod-usb-ohci kmod-usb2 fconfig
+  ATH_SOC := ar7161
+  IMAGE_SIZE := 16128k
+  IMAGES := sysupgrade.bin factory.bin
+  IMAGE/factory.bin := append-rootfs | pad-rootfs | mkubntimage | check-size $$$$(IMAGE_SIZE)
+  IMAGE/sysupgrade.bin := append-rootfs | pad-rootfs | combined-image | check-size $$$$(IMAGE_SIZE) | append-metadata
+  KERNEL := kernel-bin | append-dtb | lzma | pad-to $$(BLOCKSIZE)
+  KERNEL_INITRAMFS := kernel-bin | append-dtb
+endef
+
+define Device/ubnt_routerstation
+  $(Device/ubnt_routerstation_common)
+  DEVICE_TITLE := Ubiquiti RouterStation
+  UBNT_BOARD := RS
+  UBNT_TYPE := RSx
+  UBNT_CHIP := ar7100
+  DEVICE_PACKAGES += -swconfig
+endef
+TARGET_DEVICES += ubnt_routerstation
+
+define Device/ubnt_routerstation-pro
+  $(Device/ubnt_routerstation_common)
+  DEVICE_TITLE := Ubiquiti RouterStation Pro
+  UBNT_BOARD := RSPRO
+  UBNT_TYPE := RSPRO
+  UBNT_CHIP := ar7100pro
+endef
+TARGET_DEVICES += ubnt_routerstation-pro
-- 
2.30.2