From 445949adbf7537a37fd88c73b60aabb777fd84d1 Mon Sep 17 00:00:00 2001 From: LGA1150 <9155358+LGA1150@users.noreply.github.com> Date: Sat, 22 Feb 2020 12:26:41 +0800 Subject: [PATCH] ramips: add JD-Cloud router support (#3239) * generic: support mtd-mac-address-ascii It supports formats of both 001122334455 and 00:11:22:33:44:55 Signed-off-by: Yousong Zhou * ramips: add JDCloud Co-authored-by: Yousong Zhou --- .../681-NET-add-of_get_mac_address_mtd.patch | 107 ++++++++++--- .../681-NET-add-of_get_mac_address_mtd.patch | 107 ++++++++++--- .../ramips/dts/mt7621_jdcloud_re-sp-01b.dts | 144 ++++++++++++++++++ target/linux/ramips/image/mt7621.mk | 8 + .../mt7621/base-files/etc/board.d/02_network | 1 + 5 files changed, 319 insertions(+), 48 deletions(-) create mode 100644 target/linux/ramips/dts/mt7621_jdcloud_re-sp-01b.dts diff --git a/target/linux/generic/pending-4.14/681-NET-add-of_get_mac_address_mtd.patch b/target/linux/generic/pending-4.14/681-NET-add-of_get_mac_address_mtd.patch index 2b89a0523..9de3097cc 100644 --- a/target/linux/generic/pending-4.14/681-NET-add-of_get_mac_address_mtd.patch +++ b/target/linux/generic/pending-4.14/681-NET-add-of_get_mac_address_mtd.patch @@ -32,17 +32,67 @@ Signed-off-by: Felix Fietkau { struct property *pp = of_find_property(np, name, NULL); -@@ -47,6 +48,79 @@ static const void *of_get_mac_addr(struc +@@ -47,6 +48,138 @@ static const void *of_get_mac_addr(struc return NULL; } ++typedef int(*mtd_mac_address_read)(struct mtd_info *mtd, loff_t from, u_char *buf); ++ ++static int read_mtd_mac_address(struct mtd_info *mtd, loff_t from, u_char *mac) ++{ ++ size_t retlen; ++ ++ return mtd_read(mtd, from, 6, &retlen, mac); ++} ++ ++static int read_mtd_mac_address_ascii(struct mtd_info *mtd, loff_t from, u_char *mac) ++{ ++ size_t retlen; ++ char buf[17]; ++ ++ if (mtd_read(mtd, from, 12, &retlen, buf)) { ++ return -1; ++ } ++ if (sscanf(buf, "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", ++ &mac[0], &mac[1], &mac[2], &mac[3], ++ &mac[4], &mac[5]) == 6) { ++ if (mac[0] == '\0' && mac[1] == '\0') { /* First 2 bytes are zero, probably a bug. Trying to re-read */ ++ buf[4] = '\0'; /* Make it null-terminated */ ++ if (sscanf(buf, "%4hx", mac) == 1) ++ *(uint16_t*)mac = htons(*(uint16_t*)mac); ++ } ++ return 0; ++ } ++ if (mtd_read(mtd, from+12, 5, &retlen, buf+12)) { ++ return -1; ++ } ++ if (sscanf(buf, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", ++ &mac[0], &mac[1], &mac[2], &mac[3], ++ &mac[4], &mac[5]) == 6) { ++ return 0; ++ } ++ return -1; ++} ++ ++static struct mtd_mac_address_property { ++ char *name; ++ mtd_mac_address_read read; ++} mtd_mac_address_properties[] = { ++ { ++ .name = "mtd-mac-address", ++ .read = read_mtd_mac_address, ++ }, { ++ .name = "mtd-mac-address-ascii", ++ .read = read_mtd_mac_address_ascii, ++ }, ++}; ++ +static const void *of_get_mac_address_mtd(struct device_node *np) +{ +#ifdef CONFIG_MTD + struct device_node *mtd_np = NULL; + struct property *prop; -+ size_t retlen; -+ int size, ret; ++ int size, ret = -1; + struct mtd_info *mtd; + const char *part; + const __be32 *list; @@ -51,28 +101,37 @@ Signed-off-by: Felix Fietkau + u8 mac[ETH_ALEN]; + void *addr; + u32 inc_idx; ++ int i; + -+ list = of_get_property(np, "mtd-mac-address", &size); -+ if (!list || (size != (2 * sizeof(*list)))) ++ for (i = 0; i < ARRAY_SIZE(mtd_mac_address_properties); i++) { ++ list = of_get_property(np, mtd_mac_address_properties[i].name, &size); ++ if (!list || (size != (2 * sizeof(*list)))) ++ continue; ++ ++ phandle = be32_to_cpup(list++); ++ if (phandle) ++ mtd_np = of_find_node_by_phandle(phandle); ++ ++ if (!mtd_np) ++ continue; ++ ++ part = of_get_property(mtd_np, "label", NULL); ++ if (!part) ++ part = mtd_np->name; ++ ++ mtd = get_mtd_device_nm(part); ++ if (IS_ERR(mtd)) ++ continue; ++ ++ ret = mtd_mac_address_properties[i].read(mtd, be32_to_cpup(list), mac); ++ put_mtd_device(mtd); ++ if (!ret) { ++ break; ++ } ++ } ++ if (ret) { + return NULL; -+ -+ phandle = be32_to_cpup(list++); -+ if (phandle) -+ mtd_np = of_find_node_by_phandle(phandle); -+ -+ if (!mtd_np) -+ return NULL; -+ -+ part = of_get_property(mtd_np, "label", NULL); -+ if (!part) -+ part = mtd_np->name; -+ -+ mtd = get_mtd_device_nm(part); -+ if (IS_ERR(mtd)) -+ return NULL; -+ -+ ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac); -+ put_mtd_device(mtd); ++ } + + if (of_property_read_u32(np, "mtd-mac-address-increment-byte", &inc_idx)) + inc_idx = 5; @@ -112,7 +171,7 @@ Signed-off-by: Felix Fietkau /** * Search the device tree for the best MAC address to use. 'mac-address' is * checked first, because that is supposed to contain to "most recent" MAC -@@ -64,11 +138,18 @@ static const void *of_get_mac_addr(struc +@@ -64,11 +192,18 @@ static const void *of_get_mac_addr(struc * addresses. Some older U-Boots only initialized 'local-mac-address'. In * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists * but is all zeros. diff --git a/target/linux/generic/pending-4.19/681-NET-add-of_get_mac_address_mtd.patch b/target/linux/generic/pending-4.19/681-NET-add-of_get_mac_address_mtd.patch index 13f564059..ab43007e2 100644 --- a/target/linux/generic/pending-4.19/681-NET-add-of_get_mac_address_mtd.patch +++ b/target/linux/generic/pending-4.19/681-NET-add-of_get_mac_address_mtd.patch @@ -32,17 +32,67 @@ Signed-off-by: Felix Fietkau { struct property *pp = of_find_property(np, name, NULL); -@@ -48,6 +49,79 @@ static const void *of_get_mac_addr(struc +@@ -48,6 +49,138 @@ static const void *of_get_mac_addr(struc return NULL; } ++typedef int(*mtd_mac_address_read)(struct mtd_info *mtd, loff_t from, u_char *buf); ++ ++static int read_mtd_mac_address(struct mtd_info *mtd, loff_t from, u_char *mac) ++{ ++ size_t retlen; ++ ++ return mtd_read(mtd, from, 6, &retlen, mac); ++} ++ ++static int read_mtd_mac_address_ascii(struct mtd_info *mtd, loff_t from, u_char *mac) ++{ ++ size_t retlen; ++ char buf[17]; ++ ++ if (mtd_read(mtd, from, 12, &retlen, buf)) { ++ return -1; ++ } ++ if (sscanf(buf, "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", ++ &mac[0], &mac[1], &mac[2], &mac[3], ++ &mac[4], &mac[5]) == 6) { ++ if (mac[0] == '\0' && mac[1] == '\0') { /* First 2 bytes are zero, probably a bug. Trying to re-read */ ++ buf[4] = '\0'; /* Make it null-terminated */ ++ if (sscanf(buf, "%4hx", mac) == 1) ++ *(uint16_t*)mac = htons(*(uint16_t*)mac); ++ } ++ return 0; ++ } ++ if (mtd_read(mtd, from+12, 5, &retlen, buf+12)) { ++ return -1; ++ } ++ if (sscanf(buf, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", ++ &mac[0], &mac[1], &mac[2], &mac[3], ++ &mac[4], &mac[5]) == 6) { ++ return 0; ++ } ++ return -1; ++} ++ ++static struct mtd_mac_address_property { ++ char *name; ++ mtd_mac_address_read read; ++} mtd_mac_address_properties[] = { ++ { ++ .name = "mtd-mac-address", ++ .read = read_mtd_mac_address, ++ }, { ++ .name = "mtd-mac-address-ascii", ++ .read = read_mtd_mac_address_ascii, ++ }, ++}; ++ +static const void *of_get_mac_address_mtd(struct device_node *np) +{ +#ifdef CONFIG_MTD + struct device_node *mtd_np = NULL; + struct property *prop; -+ size_t retlen; -+ int size, ret; ++ int size, ret = -1; + struct mtd_info *mtd; + const char *part; + const __be32 *list; @@ -51,28 +101,37 @@ Signed-off-by: Felix Fietkau + u8 mac[ETH_ALEN]; + void *addr; + u32 inc_idx; ++ int i; + -+ list = of_get_property(np, "mtd-mac-address", &size); -+ if (!list || (size != (2 * sizeof(*list)))) ++ for (i = 0; i < ARRAY_SIZE(mtd_mac_address_properties); i++) { ++ list = of_get_property(np, mtd_mac_address_properties[i].name, &size); ++ if (!list || (size != (2 * sizeof(*list)))) ++ continue; ++ ++ phandle = be32_to_cpup(list++); ++ if (phandle) ++ mtd_np = of_find_node_by_phandle(phandle); ++ ++ if (!mtd_np) ++ continue; ++ ++ part = of_get_property(mtd_np, "label", NULL); ++ if (!part) ++ part = mtd_np->name; ++ ++ mtd = get_mtd_device_nm(part); ++ if (IS_ERR(mtd)) ++ continue; ++ ++ ret = mtd_mac_address_properties[i].read(mtd, be32_to_cpup(list), mac); ++ put_mtd_device(mtd); ++ if (!ret) { ++ break; ++ } ++ } ++ if (ret) { + return NULL; -+ -+ phandle = be32_to_cpup(list++); -+ if (phandle) -+ mtd_np = of_find_node_by_phandle(phandle); -+ -+ if (!mtd_np) -+ return NULL; -+ -+ part = of_get_property(mtd_np, "label", NULL); -+ if (!part) -+ part = mtd_np->name; -+ -+ mtd = get_mtd_device_nm(part); -+ if (IS_ERR(mtd)) -+ return NULL; -+ -+ ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac); -+ put_mtd_device(mtd); ++ } + + if (of_property_read_u32(np, "mtd-mac-address-increment-byte", &inc_idx)) + inc_idx = 5; @@ -112,7 +171,7 @@ Signed-off-by: Felix Fietkau /** * Search the device tree for the best MAC address to use. 'mac-address' is * checked first, because that is supposed to contain to "most recent" MAC -@@ -65,11 +139,18 @@ static const void *of_get_mac_addr(struc +@@ -65,11 +193,18 @@ static const void *of_get_mac_addr(struc * addresses. Some older U-Boots only initialized 'local-mac-address'. In * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists * but is all zeros. diff --git a/target/linux/ramips/dts/mt7621_jdcloud_re-sp-01b.dts b/target/linux/ramips/dts/mt7621_jdcloud_re-sp-01b.dts new file mode 100644 index 000000000..4392a8d1f --- /dev/null +++ b/target/linux/ramips/dts/mt7621_jdcloud_re-sp-01b.dts @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0-or-later OR MIT +/dts-v1/; + +#include "mt7621.dtsi" + +#include +#include + +/ { + compatible = "jdcloud,re-sp-01b", "mediatek,mt7621-soc"; + model = "JDCloud RE-SP-01B"; + + aliases { + led-boot = &led_red; + led-failsafe = &led_red; + led-running = &led_green; + led-upgrade = &led_blue; + label-mac-device = ðernet; + }; + + chosen { + bootargs = "console=ttyS0,115200"; + }; + + keys { + compatible = "gpio-keys"; + + reset { + label = "reset"; + gpios = <&gpio0 18 GPIO_ACTIVE_LOW>; + linux,code = ; + }; + }; + + leds { + compatible = "gpio-leds"; + + led_red: red { + label = "jdcloud:red:sys"; + gpios = <&gpio0 6 GPIO_ACTIVE_LOW>; + panic-indicator; + }; + + led_green: green { + label = "jdcloud:green:sys"; + gpios = <&gpio0 8 GPIO_ACTIVE_LOW>; + }; + + led_blue: blue { + label = "jdcloud:blue:sys"; + gpios = <&gpio0 12 GPIO_ACTIVE_LOW>; + }; + }; +}; + +&sdhci { + status = "okay"; +}; + +&spi0 { + status = "okay"; + + flash@0 { + compatible = "jedec,spi-nor"; + reg = <0>; + spi-max-frequency = <50000000>; + + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + partition@0 { + label = "u-boot"; + reg = <0x0 0x30000>; + read-only; + }; + + config: partition@30000 { + label = "config"; + reg = <0x30000 0x10000>; + read-only; + }; + + factory: partition@40000 { + label = "factory"; + reg = <0x40000 0x10000>; + read-only; + }; + + partition@50000 { + compatible = "denx,uimage"; + label = "firmware"; + reg = <0x50000 0x1ab0000>; + }; + + partition@1b00000 { + label = "mini"; + reg = <0x1b00000 0x400000>; + read-only; + }; + + partition@1f00000 { + label = "oem"; + reg = <0x1f00000 0x100000>; + read-only; + }; + }; + }; +}; + +ðernet { + mtd-mac-address-ascii = <&config 0x4429>; +}; + +&pcie { + status = "okay"; +}; + +&pcie0 { + wifi@0,0 { + reg = <0x0000 0 0 0 0>; + mtd-mac-address-ascii = <&config 0x4429>; + mediatek,mtd-eeprom = <&factory 0x0>; + }; +}; + +&pcie1 { + wifi@0,0 { + reg = <0x0000 0 0 0 0>; + mtd-mac-address-ascii = <&config 0x4429>; + mtd-mac-address-increment = <0x80>; + mtd-mac-address-increment-byte = <3>; + mediatek,mtd-eeprom = <&factory 0x8000>; + ieee80211-freq-limit = <5000000 6000000>; + }; +}; + +&state_default { + gpio { + ralink,group = "uart2", "uart3", "wdt"; + ralink,function = "gpio"; + }; +}; diff --git a/target/linux/ramips/image/mt7621.mk b/target/linux/ramips/image/mt7621.mk index 336b0fdea..b21ebd4c8 100644 --- a/target/linux/ramips/image/mt7621.mk +++ b/target/linux/ramips/image/mt7621.mk @@ -385,6 +385,14 @@ define Device/jcg_jhr-ac876m endef TARGET_DEVICES += jcg_jhr-ac876m +define Device/jdcloud_re-sp-01b + IMAGE_SIZE := 27328k + DEVICE_VENDOR := JDCloud + DEVICE_MODEL := RE-SP-01B + DEVICE_PACKAGES := kmod-fs-ext4 kmod-mt7603 kmod-mt7615e kmod-sdhci-mt7620 kmod-usb3 wpad-openssl +endef +TARGET_DEVICES += jdcloud_re-sp-01b + define Device/lenovo_newifi-d1 IMAGE_SIZE := 32448k DEVICE_VENDOR := Newifi diff --git a/target/linux/ramips/mt7621/base-files/etc/board.d/02_network b/target/linux/ramips/mt7621/base-files/etc/board.d/02_network index f53c8f11e..978e8df64 100755 --- a/target/linux/ramips/mt7621/base-files/etc/board.d/02_network +++ b/target/linux/ramips/mt7621/base-files/etc/board.d/02_network @@ -124,6 +124,7 @@ ramips_setup_interfaces() ucidef_add_switch_attr "switch0" "enable" "false" ucidef_set_interface_lan "eth0" ;; + jdcloud,re-sp-01b|\ mikrotik,rbm33g) ucidef_add_switch "switch0" \ "1:lan" "2:lan" "0:wan" "6@eth0"