[LEDE-DEV] [PATCH] ramips: improve Xiaomi Mi Router 3G support

Kevin Darbyshire-Bryant ldir at darbyshire-bryant.me.uk
Mon Sep 25 06:25:02 PDT 2017


This commit improves support for the Xiaomi Mi Router 3G originally
added in commit 6e283cdc0da25928f8148805ebef7f8f2b769ee8

Improvements:

- Remove software watchdog as hardware watchdog now working as per
  commit 3fbf3ab44f5cebb22e30a4c8681b13341feed6a6 for all mt7621
  devices.

- Reset button polarity corrected - length of press determines reboot
  (sort press) vs. reset to defaults (long press) behaviour.

- Enable GPIO amber switch port LEDs on board rear - lit indicates 1Gbit
  link and blink on activity.  Green LEDs driven directly by switch
  indicating any link speed and tx activity.

- Fix failsafe network functionality allowing ssh access in failsafe
  mode.

- USB port power on/off GPIO exposed as 'usbpower'

- Add access to uboot environment settings for checking/setting uboot
  boot order preference from user space.

Changes:

- Front LED indicator is physically made of independent Yellow/Amber,
  Red & Blue LEDs combined via a plastic 'lightpipe' to a front panel
  indicator, hence the colour behaviour is similar to an RGB LED. RGB
  LEDs are not supported at this time because they produce colour results
  that do not then match colour labels, e.g. enabling 'mir3g:red' and
  'mir3g:blue' would result in a purple indicator and we have no such
  label for purple.
  The yellow, red & blue LEDs have been split out as individual yellow,
  red & blue status LEDs, with yellow being the default status LED as
  before and with red's WAN and blue's USB default associations removed.

- Swapped order of vlan interfaces (eth0.1 & eth0.2) to match stock vlan
  layout. eth0.1 is LAN, eth0.2 is WAN

- Add 'lwlll' vlan layout to mt7530 switch driver to prevent packet
  leakage between kernel switch init and uci swconfig

uboot behaviour & system 'recovery'

uboot expects to find bootable kernels at nand addresses 0x200000 &
0x600000 known by uboot as "system 1" and "system 2" respectively.
uboot chooses which system to hand control to based on 3 environment
variables: flag_last_success, flag_try_sys1_failed & flag_try_sys2_failed

last_success represents a preference for a particular system and is set
to 0 for system 1, set to 1 for system 2.  last_success is considered *if*
and only if both try_sys'n'_failed flags are 0 (ie. unset) If *either*
failed flags are set then uboot will attempt to hand control to the
non failed system. If both failed flags are set then uboot will check
the uImage CRC of system 1 and hand control to it if ok.  If the uImage
CRC of system is not ok, uboot will hand control to system 2
irrespective of system 2's uImage CRC.

NOTE: uboot only ever sets failed flags, it *never* clears them. uboot
sets a system's failed flag if that system's was selected for boot but
the uImage CRC is incorrect.

Fortunately with serial console access, uboot provides the ability to
boot an initramfs image transferred via tftp, similarly an image may
be flashed to nand however it will flash to *both* kernels so a backup
of stock kernel image is suggested. Note that the suggested install
procedure below set's system 1's failed flag (stock) thus uboot ignores
the last_success preference and boots LEDE located in system 2.

Considerable thought has gone into whether LEDE should replace both
kernels, only one (and which one) etc. LEDE kernels do not include a
minimal rootfs and thus unlike the stock kernel cannot include a
method of controlling uboot environment variables in the event of
rootfs mount failure. Similarly uboot fails to provide an external
mechanism for indicating boot system failure.

Installation - from stock.

Installation through telnet/ssh:
- copy lede-ramips-mt7621-mir3g-squashfs-kernel1.bin and
  lede-ramips-mt7621-mir3g-squashfs-rootfs0.bin to usb disk or wget it
  from LEDE download site to /tmp
- switch to /extdisks/sda1/ (if copied to USB drive) or to /tmp if
  wgetted from LEDE download site
- run: mtd write lede-ramips-mt7621-mir3g-squashfs-kernel1.bin kernel1
- run: mtd write lede-ramips-mt7621-mir3g-squashfs-rootfs0.bin rootfs0
- run: nvram set flag_try_sys1_failed=1
- run: nvram commit
- run: reboot

Recovery - to stock.

Assuming you used the above installation instructions you will have a
stock kernel image in system 1. If it can be booted then it may be used
to perform a stock firmware recovery, thus erasing LEDE completely. From
a 'working' LEDE state (even failsafe)

Failsafe only:
- run: mount_root
- run: sh /etc/uci-defaults/30_uboot-envtools
Then do the steps for 'All'

All:
- run: fw_setenv flag_try_sys2_failed 1
- run: reboot

The board will reboot into system 1 (stock basic kernel) and wait with
system red light slowly blinking for a FAT formatted usb stick with a
recovery image to be inserted.  Press and hold the reset button for
around 1 second. Status LED will turn yellow during recovery and blue
when recovery complete.

Signed-off-by: Kevin Darbyshire-Bryant <ldir at darbyshire-bryant.me.uk>
---
 package/boot/uboot-envtools/files/ramips           |  3 +
 target/linux/ramips/base-files/etc/board.d/01_leds |  5 +-
 .../linux/ramips/base-files/etc/board.d/02_network |  4 +-
 .../lib/preinit/07_set_preinit_iface_ramips        |  9 ++-
 target/linux/ramips/dts/MIR3G.dts                  | 74 ++++++++++++++++------
 .../files-4.9/drivers/net/ethernet/mtk/mt7530.c    |  8 ++-
 target/linux/ramips/image/mt7621.mk                |  2 +-
 7 files changed, 76 insertions(+), 29 deletions(-)

diff --git a/package/boot/uboot-envtools/files/ramips b/package/boot/uboot-envtools/files/ramips
index 70ba6a2..05c1407 100644
--- a/package/boot/uboot-envtools/files/ramips
+++ b/package/boot/uboot-envtools/files/ramips
@@ -28,6 +28,9 @@ wsr-600|\
 zbt-wg2626)
 	ubootenv_add_uci_config "/dev/mtd1" "0x0" "0x1000" "0x10000"
 	;;
+mir3g)
+	ubootenv_add_uci_config "/dev/mtd1" "0x0" "0x1000" "0x20000"
+	;;
 esac
 
 config_load ubootenv
diff --git a/target/linux/ramips/base-files/etc/board.d/01_leds b/target/linux/ramips/base-files/etc/board.d/01_leds
index a0baa61..c871dc4 100755
--- a/target/linux/ramips/base-files/etc/board.d/01_leds
+++ b/target/linux/ramips/base-files/etc/board.d/01_leds
@@ -253,8 +253,9 @@ miniembplug)
 	set_usb_led "$board:green:mobile"
 	;;
 mir3g)
-	ucidef_set_led_netdev "eth" "Ethernet" "$board:red:wan" "eth0"
-	set_usb_led "$board:blue:usb"
+	ucidef_set_led_switch "wan-amber"  "WAN (amber)"  "$board:amber:wan"  "switch0" "0x02" "0x08"
+	ucidef_set_led_switch "lan1-amber" "LAN1 (amber)" "$board:amber:lan1" "switch0" "0x08" "0x08"
+	ucidef_set_led_switch "lan2-amber" "LAN2 (amber)" "$board:amber:lan2" "switch0" "0x04" "0x08"
 	;;
 miwifi-mini)
 	ucidef_set_led_default "power" "power" "$board:red:status" "1"
diff --git a/target/linux/ramips/base-files/etc/board.d/02_network b/target/linux/ramips/base-files/etc/board.d/02_network
index 862e99b..a1a82d6 100755
--- a/target/linux/ramips/base-files/etc/board.d/02_network
+++ b/target/linux/ramips/base-files/etc/board.d/02_network
@@ -124,7 +124,7 @@ ramips_setup_interfaces()
 		;;
 	mir3g)
 		ucidef_add_switch "switch0" \
-			"1:wan" "2:lan:2" "3:lan:1" "6 at eth0"
+			"2:lan:2" "3:lan:1" "1:wan" "6t at eth0"
 		;;
 	psg1218b)
 		ucidef_add_switch "switch0" \
@@ -429,7 +429,7 @@ ramips_setup_macs()
 		wan_mac=$(macaddr_add "$lan_mac" 1)
 		;;
 	mir3g)
-		lan_mac=$(mtd_get_mac_binary Factory e006)
+		lan_mac=$(mtd_get_mac_binary Factory 0xe006)
 		;;
 	miwifi-mini)
 		wan_mac=$(cat /sys/class/net/eth0/address)
diff --git a/target/linux/ramips/base-files/lib/preinit/07_set_preinit_iface_ramips b/target/linux/ramips/base-files/lib/preinit/07_set_preinit_iface_ramips
index 452b1b2..4b53580 100644
--- a/target/linux/ramips/base-files/lib/preinit/07_set_preinit_iface_ramips
+++ b/target/linux/ramips/base-files/lib/preinit/07_set_preinit_iface_ramips
@@ -6,17 +6,22 @@
 . /lib/ramips.sh
 
 ramips_set_preinit_iface() {
-	RT3X5X=`cat /proc/cpuinfo | egrep "(RT3.5|RT5350|MT7628|MT7688|MT7620)"`
+	RT3X5X=`cat /proc/cpuinfo | egrep "(RT3.5|RT5350|MT7628|MT7688|MT7620|MT7621)"`
 
 	if [ -n "${RT3X5X}" ]; then
 		# The ethernet switch driver enables VLAN by default, but
 		# failsafe uses eth0, making the device unreachable:
 		# https://dev.openwrt.org/ticket/18768
-		ralink_switchdev=rt305x
 		case "${RT3X5X}" in
 		*MT7620*)
 			ralink_switchdev=mt7620
 			;;
+		*MT7621*)
+			ralink_switchdev=mt7530
+			;;
+		default)
+			ralink_switchdev=rt305x
+			;;
 		esac
 		swconfig dev $ralink_switchdev set reset 1
 		swconfig dev $ralink_switchdev set enable_vlan 0
diff --git a/target/linux/ramips/dts/MIR3G.dts b/target/linux/ramips/dts/MIR3G.dts
index 0cdb6d5..efe98b0 100644
--- a/target/linux/ramips/dts/MIR3G.dts
+++ b/target/linux/ramips/dts/MIR3G.dts
@@ -21,20 +21,36 @@
 	gpio-leds {
 		compatible = "gpio-leds";
 
-		wan {
-			label = "mir3g:red:wan";
+		status_red {
+			label = "mir3g:red:status";
 			gpios = <&gpio0 6 GPIO_ACTIVE_LOW>;
 		};
 
-		usb {
-			label = "mir3g:blue:usb";
+		status_blue {
+			label = "mir3g:blue:status";
 			gpios = <&gpio0 8 GPIO_ACTIVE_LOW>;
 		};
 
-		status {
+		status_yellow {
 			label = "mir3g:yellow:status";
 			gpios = <&gpio0 10 GPIO_ACTIVE_LOW>;
 		};
+
+		wan_amber {
+			label = "mir3g:amber:wan";
+			gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
+		};
+
+		lan1_amber {
+			label = "mir3g:amber:lan1";
+			gpios = <&gpio0 14 GPIO_ACTIVE_LOW>;
+		};
+
+		lan2_amber {
+			label = "mir3g:amber:lan2";
+			gpios = <&gpio0 16 GPIO_ACTIVE_LOW>;
+		};
+
 	};
 
 	gpio-keys-polled {
@@ -45,10 +61,21 @@
 
 		reset {
 			label = "reset";
-			gpios = <&gpio0 18 GPIO_ACTIVE_HIGH>;
+			gpios = <&gpio0 18 GPIO_ACTIVE_LOW>;
 			linux,code = <KEY_RESTART>;
 		};
 	};
+
+	gpio_export {
+		compatible = "gpio-export";
+		#size-cells = <0>;
+
+		usbpower {
+			gpio-export,name = "usbpower";
+			gpio-export,output = <1>;
+			gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>;
+		};
+	};
 };
 
 &nand {
@@ -63,7 +90,6 @@
 	partition at 80000 {
 		label = "Config";
 		reg = <0x80000 0x40000>;
-		read-only;
 	};
 
 	partition at c0000 {
@@ -81,13 +107,11 @@
 	partition at 140000 {
 		label = "crash";
 		reg = <0x140000 0x40000>;
-		read-only;
 	};
 
 	partition at 180000 {
 		label = "crash_syslog";
 		reg = <0x180000 0x40000>;
-		read-only;
 	};
 
 	partition at 1c0000 {
@@ -96,15 +120,22 @@
 		read-only;
 	};
 
-	/* 
-	 * kernel0 partition should be erased, so 
-	 * u-boot in failsafe routine switches 
-	 * to next one looking for kernel image.
-	 * To remind about this fact rename kernel0
-	 * into kernel_erase.
+	/* uboot expects to find kernels at 0x200000 & 0x600000
+	 * referred to as system 1 & system 2 respectively.
+	 * a kernel is considered suitable for handing control over
+	 * if its linux magic number exists & uImage CRC are correct.
+	 * If either of those conditions fail, a matching sys'n'_fail flag
+	 * is set in uboot env & a restart performed in the hope that the
+	 * alternate kernel is okay.
+	 * if neither kernel checksums ok and both are marked failed, system 2
+	 * is booted anyway.
+	 *
+	 * Note uboot's tftp flash install writes the transferred
+	 * image to both kernel partitions.
 	 */
+
 	partition at 200000 {
-		label = "kernel_erase";
+		label = "kernel_stock";
 		reg = <0x200000 0x400000>;
 	};
 
@@ -114,12 +145,13 @@
 	};
 
 	/* ubi partition is the result of squashing
-	 * next consequent stock partitions:
-	 * - rootfs0 (rootfs partition for stock kernel0), 
-	 * - rootfs1 (rootfs partition for stock failsafe kernel1), 
+	 * next consecutive stock partitions:
+	 * - rootfs0 (rootfs partition for stock kernel0),
+	 * - rootfs1 (rootfs partition for stock failsafe kernel1),
 	 * - overlay (used as ubi overlay in stock fw)
 	 * resulting 117,5MiB space for packages.
 	 */
+
 	partition at a00000 {
 		label = "ubi";
 		reg = <0xa00000 0x7580000>;
@@ -136,7 +168,7 @@
 			mediatek,mtd-eeprom = <&factory 0x0000>;
 			ieee80211-freq-limit = <2400000 2500000>;
 		};
-	};	
+	};
 
 	pcie1 {
 		wifi at 14c3,7662 {
@@ -156,7 +188,7 @@
 &pinctrl {
 	state_default: pinctrl0 {
 		gpio {
-			ralink,group = "jtag", "uart3", "wdt";
+			ralink,group = "jtag", "uart2", "uart3", "wdt";
 			ralink,function = "gpio";
 		};
 	};
diff --git a/target/linux/ramips/files-4.9/drivers/net/ethernet/mtk/mt7530.c b/target/linux/ramips/files-4.9/drivers/net/ethernet/mtk/mt7530.c
index 74b4a71..a878d32 100644
--- a/target/linux/ramips/files-4.9/drivers/net/ethernet/mtk/mt7530.c
+++ b/target/linux/ramips/files-4.9/drivers/net/ethernet/mtk/mt7530.c
@@ -220,6 +220,12 @@ struct mt7530_mapping {
 		.members = { 0, 0x7e, 0x41 },
 		.etags = { 0, 0x40, 0x40 },
 		.vids = { 0, 1, 2 },
+	}, {
+		.name = "lwlll",
+		.pvids = { 2, 1, 1, 1, 1, 1, 1 },
+		.members = { 0, 0x7d, 0x42 },
+		.etags = { 0, 0x40, 0x40 },
+		.vids = { 0, 1, 2 },
 	},
 };
 
@@ -922,7 +928,7 @@ mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus, int vl
 
 	/* magic vodoo */
 	if (!IS_ENABLED(CONFIG_SOC_MT7621) && bus && mt7530_r32(mt7530, REG_HWTRAP) !=  0x1117edf) {
-	        dev_info(dev, "fixing up MHWTRAP register - bootloader probably played with it\n");
+		dev_info(dev, "fixing up MHWTRAP register - bootloader probably played with it\n");
 		mt7530_w32(mt7530, REG_HWTRAP, 0x1117edf);
 	}
 	dev_info(dev, "loaded %s driver\n", swdev->name);
diff --git a/target/linux/ramips/image/mt7621.mk b/target/linux/ramips/image/mt7621.mk
index 7796162..8bd7e03 100644
--- a/target/linux/ramips/image/mt7621.mk
+++ b/target/linux/ramips/image/mt7621.mk
@@ -121,7 +121,7 @@ define Device/mir3g
   SUPPORTED_DEVICES += R3G
   DEVICE_PACKAGES := \
 	kmod-mt7603 kmod-mt76x2 kmod-usb3 kmod-usb-ledtrig-usbport wpad-mini \
-	kmod-softdog
+	uboot-envtools
 endef
 TARGET_DEVICES += mir3g
 
-- 
2.7.4




More information about the Lede-dev mailing list