From b3a0c97b8c936716360065cbf2309cb4b15b782c Mon Sep 17 00:00:00 2001
From: Tomasz Maciej Nowak <tomek_n@o2.pl>
Date: Wed, 6 Mar 2019 20:15:19 +0100
Subject: [PATCH] ath79: add support for jjPlus JA76PF2

jjPlus JA76PF2 (marketed as IntellusPro2) is a network embedded board.

Specification
SoC:		Atheros AR7161
RAM:		64 MB DDR
Flash:		16 MB SPI NOR
Ethernet:	2x 10/100/1000 Mbps AR8316
		LAN (CN11), WAN/PoE (CN6 - close to power barrel
		connector, 48 V)
MiniPCI:	2x
LEDS:		4x, which 3 are GPIO controlled
Buttons:	2x GPIO controlled
		Reset (SW1, closer to ethernet ports), WPS (SW2)
Serial:		1x (only RX and TX are wired)
		baud: 115200, parity: none, flow control: none

Currently there is one caveat compared to ar71xx target images as the
MAC addresses are random on every reboot. To remedy this one needs to
store the WAN MAC address in RedBoot configuration. OpenWrt on first
boot, after flashing, will read out the address and assign proper ones
to both WAN and LAN ports. It is iportant to NOT keep the old
configuration when doing sysupgrade from ar71xx.

Upgrading from OpenWrt ar71xx image
1. Connect to serial port,
2. Download OpenWrt sysupgrade image to /tmp directory and flash it
   with:
    sysupgrade -n <openwrt_sysupgrade_image_name>
3. After writing new image OpenWrt will reboot, now interrupt boot
   process and enter RedBoot (bootloader) command line by pressing
   Ctrl+C,
4. Enter following commands (replace variable accordingly),
    set_mac (to view MAC addresses)
    alias ethaddr <wan_port_mac_adress>
    (confirm storing the value by inputting y and pressing Enter)
    reset
5. Now board should restart and boot OpenWrt with proper MAC addresses.

Installation
1. Prepare TFTP server with OpenWrt initramfs image,
2. Connect to WAN ethernet port,
3. Connect to serial port,
4. Power on the board and enter RedBoot (bootloader) command line by
   pressing Ctrl+C,
5. Enter following commands (replace variables accordingly):
    set_mac (to view MAC addresses)
    alias ethaddr <wan_port_mac_address>
    (confirm storing the value by inputting y and pressing Enter)
    ip_adress -l <board_ip_adress>/24 -h <tftp_server_ip_adress>
    load -r -b 0x80060000 <openwrt_initramfs_image_name>
    exec -c ""
6. Now board should boot OpenWrt initramfs image,
7. Download OpenWrt sysupgrade image to /tmp directory and flash it
   with:
    sysupgrade <openwrt_sysupgrade_image_name>
8. Wait few minutes, after the D2 LED will stop blinking, the board
   is ready for configuration.

Signed-off-by: Tomasz Maciej Nowak <tomek_n@o2.pl>
---
 .../ath79/base-files/etc/board.d/02_network   |   4 +
 .../ath79/base-files/lib/upgrade/platform.sh  |  12 +-
 .../linux/ath79/dts/ar7161_jjplus_ja76pf2.dts | 126 ++++++++++++++++++
 target/linux/ath79/generic/config-default     |   1 +
 target/linux/ath79/image/generic.mk           |  12 ++
 .../408-mtd-redboot_partition_scan.patch      |  44 ++++++
 6 files changed, 195 insertions(+), 4 deletions(-)
 create mode 100644 target/linux/ath79/dts/ar7161_jjplus_ja76pf2.dts
 create mode 100644 target/linux/ath79/patches-4.14/408-mtd-redboot_partition_scan.patch

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 e2c303e56572..62318ee2320c 100755
--- a/target/linux/ath79/base-files/etc/board.d/02_network
+++ b/target/linux/ath79/base-files/etc/board.d/02_network
@@ -288,6 +288,10 @@ ath79_setup_macs()
 		lan_mac=$(mtd_get_mac_ascii u-boot-env ethaddr)
 		wan_mac=$(macaddr_add "$lan_mac" -1)
 		;;
+	jjplus,ja76pf2)
+		wan_mac=$(fconfig -s -r -d $(find_mtd_part "RedBoot config") -n alias/ethaddr)
+		lan_mac=$(macaddr_add "$wan_mac" 1)
+		;;
 	nec,wg800hp)
 		lan_mac=$(mtd_get_mac_text board_data 640)
 		wan_mac=$(mtd_get_mac_text board_data 1152)
diff --git a/target/linux/ath79/base-files/lib/upgrade/platform.sh b/target/linux/ath79/base-files/lib/upgrade/platform.sh
index bc19149d5c4d..c2fe08154d9e 100644
--- a/target/linux/ath79/base-files/lib/upgrade/platform.sh
+++ b/target/linux/ath79/base-files/lib/upgrade/platform.sh
@@ -5,9 +5,10 @@
 PART_NAME=firmware
 REQUIRE_IMAGE_METADATA=1
 
-routerstation_do_upgrade() {
+redboot_fis_do_upgrade() {
 	local append
 	local sysup_file="$1"
+	local kern_part="$2"
 	local magic=$(get_magic_word "$sysup_file")
 
 	if [ "$magic" = "4349" ]; then
@@ -15,7 +16,7 @@ routerstation_do_upgrade() {
 
 		[ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
 		dd if="$sysup_file" bs=64k skip=1 2>/dev/null | \
-			mtd -r $append -Fkernel:$kern_length:0x80060000,rootfs write - kernel:rootfs
+			mtd -r $append -F$kern_part:$kern_length:0x80060000,rootfs write - $kern_part:rootfs
 
 	elif [ "$magic" = "7379" ]; then
 		local board_dir=$(tar tf $sysup_file | grep -m 1 '^sysupgrade-.*/$')
@@ -23,7 +24,7 @@ routerstation_do_upgrade() {
 
 		[ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
 		tar xf $sysup_file ${board_dir}kernel ${board_dir}root -O | \
-			mtd -r $append -Fkernel:$kern_length:0x80060000,rootfs write - kernel:rootfs
+			mtd -r $append -F$kern_part:$kern_length:0x80060000,rootfs write - $kern_part:rootfs
 
 	else
 		echo "Unknown image, aborting!"
@@ -39,9 +40,12 @@ platform_do_upgrade() {
 	local board=$(board_name)
 
 	case "$board" in
+	jjplus,ja76pf2)
+		redboot_fis_do_upgrade "$ARGV" linux
+		;;
 	ubnt,routerstation|\
 	ubnt,routerstation-pro)
-		routerstation_do_upgrade "$ARGV"
+		redboot_fis_do_upgrade "$ARGV" kernel
 		;;
 	*)
 		default_do_upgrade "$ARGV"
diff --git a/target/linux/ath79/dts/ar7161_jjplus_ja76pf2.dts b/target/linux/ath79/dts/ar7161_jjplus_ja76pf2.dts
new file mode 100644
index 000000000000..76f140fa5576
--- /dev/null
+++ b/target/linux/ath79/dts/ar7161_jjplus_ja76pf2.dts
@@ -0,0 +1,126 @@
+// 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"
+
+/ {
+	model = "jjPlus JA76PF2";
+	compatible = "jjplus,ja76pf2", "qca,ar7161";
+
+	memory@0 {
+		device_type = "memory";
+		reg = <0x0 0x4000000>;
+	};
+
+	chosen {
+		bootargs = "console=ttyS0,115200n8";
+	};
+
+	aliases {
+		led-boot = &d2;
+		led-failsafe = &d2;
+		led-running = &d2;
+		led-upgrade = &d2;
+	};
+
+	extosc: ref {
+		compatible = "fixed-clock";
+		#clock-cells = <0>;
+		clock-output-names = "ref";
+		clock-frequency = <40000000>;
+	};
+
+	leds {
+		compatible = "gpio-leds";
+
+		d2: d2 {
+			label = "ja76pf2:green:d2";
+			gpios = <&gpio 5 GPIO_ACTIVE_LOW>;
+		};
+
+		d3 {
+			label = "ja76pf2:green:d3";
+			gpios = <&gpio 4 GPIO_ACTIVE_HIGH>;
+		};
+
+		d4 {
+			label = "ja76pf2:green:d4";
+			gpios = <&gpio 3 GPIO_ACTIVE_HIGH>;
+		};
+	};
+
+	keys {
+		compatible = "gpio-keys-polled";
+		poll-interval = <20>;
+
+		sw1 {
+			label = "sw1";
+			linux,code = <KEY_RESTART>;
+			gpios = <&gpio 7 GPIO_ACTIVE_LOW>;
+			debounce-interval = <60>;
+		};
+
+		sw2 {
+			label = "sw2";
+			linux,code = <KEY_WPS_BUTTON>;
+			gpios = <&gpio 8 GPIO_ACTIVE_LOW>;
+			debounce-interval = <60>;
+		};
+	};
+};
+
+&mdio0 {
+	status = "okay";
+
+	phy-mask = <0x1>;
+
+	phy0: ethernet-phy@0 {
+		reg = <0>;
+		phy-mode = "rgmii";
+	};
+
+	phy4: ethernet-phy@4 {
+		reg = <4>;
+		phy-mode = "rgmii";
+	};
+};
+
+&eth0 {
+	status = "okay";
+
+	phy-handle = <&phy0>;
+};
+
+&eth1 {
+	status = "okay";
+
+	phy-handle = <&phy4>;
+};
+
+&pcie0 {
+	status = "okay";
+};
+
+&spi {
+	status = "okay";
+	num-cs = <1>;
+
+	flash@0 {
+		compatible = "jedec,spi-nor";
+		reg = <0>;
+		spi-max-frequency = <104000000>;
+
+		partitions {
+			#address-cells = <1>;
+			#size-cells = <1>;
+			compatible = "ecoscentric,redboot-fis-partitions";
+		};
+	};
+};
+
+&uart {
+	status = "okay";
+};
diff --git a/target/linux/ath79/generic/config-default b/target/linux/ath79/generic/config-default
index fddcb1d704b6..313ab8604bd0 100644
--- a/target/linux/ath79/generic/config-default
+++ b/target/linux/ath79/generic/config-default
@@ -8,6 +8,7 @@ CONFIG_LEDS_RESET=y
 CONFIG_MARVELL_PHY=y
 CONFIG_MICREL_PHY=y
 CONFIG_MTD_REDBOOT_PARTS=y
+CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK=-2
 CONFIG_MTD_SPLIT_EVA_FW=y
 CONFIG_OF_ADDRESS_PCI=y
 CONFIG_OF_PCI=y
diff --git a/target/linux/ath79/image/generic.mk b/target/linux/ath79/image/generic.mk
index 6b20b66669c4..ddf0e62898bf 100644
--- a/target/linux/ath79/image/generic.mk
+++ b/target/linux/ath79/image/generic.mk
@@ -417,6 +417,18 @@ define Device/iodata_wn-ag300dgr
 endef
 TARGET_DEVICES += iodata_wn-ag300dgr
 
+define Device/jjplus_ja76pf2
+  ATH_SOC := ar7161
+  DEVICE_TITLE := jjPlus JA76PF2
+  DEVICE_PACKAGES += -kmod-ath9k -swconfig -wpad-mini -uboot-envtools fconfig
+  IMAGE/sysupgrade.bin := append-rootfs | pad-rootfs | combined-image | check-size $$$$(IMAGE_SIZE)
+#  IMAGE/sysupgrade.bin := append-rootfs | pad-rootfs | check-size $$$$(IMAGE_SIZE) | sysupgrade-tar rootfs=$$$$@ | append-metadata
+  KERNEL := kernel-bin | append-dtb | lzma | pad-to $$(BLOCKSIZE)
+  KERNEL_INITRAMFS := kernel-bin | append-dtb
+  IMAGE_SIZE := 16000k
+endef
+TARGET_DEVICES += jjplus_ja76pf2
+
 define Device/librerouter_librerouter-v1
   ATH_SOC := qca9558
   DEVICE_TITLE := LibreRouter v1
diff --git a/target/linux/ath79/patches-4.14/408-mtd-redboot_partition_scan.patch b/target/linux/ath79/patches-4.14/408-mtd-redboot_partition_scan.patch
new file mode 100644
index 000000000000..cd41e7ceb272
--- /dev/null
+++ b/target/linux/ath79/patches-4.14/408-mtd-redboot_partition_scan.patch
@@ -0,0 +1,44 @@
+--- a/drivers/mtd/redboot.c
++++ b/drivers/mtd/redboot.c
+@@ -76,12 +76,18 @@ static int parse_redboot_partitions(stru
+ 	static char nullstring[] = "unallocated";
+ #endif
+ 
++	buf = vmalloc(master->erasesize);
++	if (!buf)
++		return -ENOMEM;
++
++ restart:
+ 	if ( directory < 0 ) {
+ 		offset = master->size + directory * master->erasesize;
+ 		while (mtd_block_isbad(master, offset)) {
+ 			if (!offset) {
+ 			nogood:
+ 				printk(KERN_NOTICE "Failed to find a non-bad block to check for RedBoot partition table\n");
++				vfree(buf);
+ 				return -EIO;
+ 			}
+ 			offset -= master->erasesize;
+@@ -94,10 +100,6 @@ static int parse_redboot_partitions(stru
+ 				goto nogood;
+ 		}
+ 	}
+-	buf = vmalloc(master->erasesize);
+-
+-	if (!buf)
+-		return -ENOMEM;
+ 
+ 	printk(KERN_NOTICE "Searching for RedBoot partition table in %s at offset 0x%lx\n",
+ 	       master->name, offset);
+@@ -170,6 +172,11 @@ static int parse_redboot_partitions(stru
+ 	}
+ 	if (i == numslots) {
+ 		/* Didn't find it */
++		if (offset + master->erasesize < master->size) {
++			/* not at the end of the flash yet, maybe next block :) */
++			directory++;
++			goto restart;
++		}
+ 		printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
+ 		       master->name);
+ 		ret = 0;
-- 
2.30.2