From c7059c56a85567ce6bc943960168b19e2572508b Mon Sep 17 00:00:00 2001
From: Shiji Yang <yangshiji66@qq.com>
Date: Fri, 20 Jan 2023 03:16:33 +0000
Subject: [PATCH] ath79: improve support for Letv LBA-047-CH

1. Convert wireless calibration data to NVMEM.
2. Enable control green status LED and change default LED behaviors.
   The three LEDs of LBA-047-CH are in the same position, and the green
   LED will be completely covered by the other two LEDs. So don's use
   green LED as WAN indicator to ensure that only one LED is on at a time.
   LED     Factory          OpenWrt
   blue    internet fail    failsafe && upgrade
   green   internet okay    run
   red     boot             boot
3. Reduce the SPI clock to 30 MHz because the ath79 target does not
   support 50 MHz SPI operation well. Keep the fast-read support to
   ensure the spi-mem feature (b3f9842330) is enabled.
4. Remove unused package "uboot-envtools".
5. Split the factory image into two parts: rootfs and kernel.
   This change can reduce the factory image size and allow users to
   upgrade the OpenWrt kernel loader uImage (OKLI) independently.

   The new installation method: First, rename "squashfs-kernel.bin" to
   "openwrt-ar71xx-generic-ap147-16M-kernel.bin" and rename "rootfs.bin"
   to "openwrt-ar71xx-generic-ap147-16M-rootfs-squashfs.bin". Then we
   can press reset button for about 5 seconds to enter tftp download mode.
   Finally, set IP address to 192.168.67.100 and upload the above two
   parts via tftp server.

Tested on Letv LBA-047-CH

Signed-off-by: Shiji Yang <yangshiji66@qq.com>
---
 .../ath79/dts/qca9531_letv_lba-047-ch.dts     | 71 ++++++++++---------
 target/linux/ath79/image/generic.mk           | 10 +--
 2 files changed, 44 insertions(+), 37 deletions(-)

diff --git a/target/linux/ath79/dts/qca9531_letv_lba-047-ch.dts b/target/linux/ath79/dts/qca9531_letv_lba-047-ch.dts
index d65417c0d52..586facc9e5b 100644
--- a/target/linux/ath79/dts/qca9531_letv_lba-047-ch.dts
+++ b/target/linux/ath79/dts/qca9531_letv_lba-047-ch.dts
@@ -4,6 +4,7 @@
 
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/leds/common.h>
 #include <dt-bindings/mtd/partitions/uimage.h>
 
 / {
@@ -11,11 +12,11 @@
 	compatible = "letv,lba-047-ch", "qca,qca9531";
 
 	aliases {
-		led-boot = &led_status_red;
-		led-failsafe = &led_status_red;
-		led-running = &led_status_blue;
-		led-upgrade = &led_status_red;
 		label-mac-device = &eth0;
+		led-boot = &led_status_red;
+		led-failsafe = &led_status_blue;
+		led-running = &led_status_green;
+		led-upgrade = &led_status_blue;
 	};
 
 	keys {
@@ -31,16 +32,24 @@
 	leds {
 		compatible = "gpio-leds";
 
-		pinctrl-names = "default";
-		pinctrl-0 = <&led_wan_pin>;
-
-		led_status_blue: status_blue {
+		led_status_blue: led-0 {
 			label = "blue:status";
+			color = <LED_COLOR_ID_BLUE>;
+			function = LED_FUNCTION_STATUS;
 			gpios = <&gpio 13 GPIO_ACTIVE_LOW>;
 		};
 
-		led_status_red: status_red {
+		led_status_green: led-1 {
+			label = "green:status";
+			color = <LED_COLOR_ID_GREEN>;
+			function = LED_FUNCTION_STATUS;
+			gpios = <&gpio 4 GPIO_ACTIVE_LOW>;
+		};
+
+		led_status_red: led-2 {
 			label = "red:status";
+			color = <LED_COLOR_ID_RED>;
+			function = LED_FUNCTION_STATUS;
 			gpios = <&gpio 11 GPIO_ACTIVE_LOW>;
 		};
 	};
@@ -64,20 +73,13 @@
 	};
 };
 
-&pinmux {
-	/* GPIO 4: LED_LINK_5 (WAN) */
-	led_wan_pin: pinmux_led_wan_pin {
-		pinctrl-single,bits = <0x04 0x0000002d 0x000000ff>;
-	};
-};
-
 &spi {
 	status = "okay";
 
 	flash@0 {
 		compatible = "jedec,spi-nor";
 		reg = <0>;
-		spi-max-frequency = <50000000>;
+		spi-max-frequency = <30000000>;
 		m25p,fast-read;
 
 		partitions {
@@ -112,10 +114,26 @@
 				reg = <0xe90000 0x160000>;
 			};
 
-			art: partition@ff0000 {
+			partition@ff0000 {
 				label = "art";
 				reg = <0xff0000 0x10000>;
 				read-only;
+
+				compatible = "nvmem-cells";
+				#address-cells = <1>;
+				#size-cells = <1>;
+
+				macaddr_art_0: macaddr@0 {
+					reg = <0x0 0x6>;
+				};
+
+				macaddr_art_6: macaddr@6 {
+					reg = <0x6 0x6>;
+				};
+
+				cal_art_1000: calibration@1000 {
+					reg = <0x1000 0x440>;
+				};
 			};
 		};
 	};
@@ -140,19 +158,6 @@
 &wmac {
 	status = "okay";
 
-	mtd-cal-data = <&art 0x1000>;
-};
-
-&art {
-	compatible = "nvmem-cells";
-	#address-cells = <1>;
-	#size-cells = <1>;
-
-	macaddr_art_0: macaddr@0 {
-		reg = <0x0 0x6>;
-	};
-
-	macaddr_art_6: macaddr@6 {
-		reg = <0x6 0x6>;
-	};
+	nvmem-cells = <&cal_art_1000>;
+	nvmem-cell-names = "calibration";
 };
diff --git a/target/linux/ath79/image/generic.mk b/target/linux/ath79/image/generic.mk
index b3b510a2ec4..0837d37a307 100644
--- a/target/linux/ath79/image/generic.mk
+++ b/target/linux/ath79/image/generic.mk
@@ -1645,13 +1645,15 @@ define Device/letv_lba-047-ch
   SOC := qca9531
   DEVICE_VENDOR := Letv
   DEVICE_MODEL := LBA-047-CH
+  DEVICE_PACKAGES := -uboot-envtools
+  FACTORY_SIZE := 14528k
   IMAGE_SIZE := 15936k
   LOADER_FLASH_OFFS := 0x50000
   KERNEL := kernel-bin | append-dtb | lzma | uImage lzma -M 0x4f4b4c49
-  IMAGES += factory.bin
-  IMAGE/factory.bin := append-kernel | pad-to $$$$(BLOCKSIZE) | \
-	append-rootfs | pad-rootfs | check-size | pad-to 14528k | \
-	append-loader-okli-uimage $(1) | pad-to 64k
+  IMAGES += kernel.bin rootfs.bin
+  IMAGE/kernel.bin := append-loader-okli-uimage $(1) | pad-to 64k
+  IMAGE/rootfs.bin := append-kernel | pad-to $$$$(BLOCKSIZE) | \
+	append-rootfs | pad-rootfs | check-size $$$$(FACTORY_SIZE)
 endef
 TARGET_DEVICES += letv_lba-047-ch