Re: [PATCH v5 11/11] board: starfive: Dynamic configuration of DT for 1.2A and 1.3B

2023-06-20 Thread Rick Chen
> From: Yanhong Wang 
> Sent: Thursday, June 15, 2023 5:37 PM
> To: u-boot@lists.denx.de; Rick Jian-Zhi Chen(陳建志) ; Leo 
> Yu-Chi Liang(梁育齊) ; Joe Hershberger 
> ; Ramon Fried 
> Cc: Yanhong Wang ; Torsten Duwe ; 
> Leyfoon Tan ; samin . guo 
> ; Walker Chen ; Hal 
> Feng 
> Subject: [PATCH v5 11/11] board: starfive: Dynamic configuration of DT for 
> 1.2A and 1.3B
>
> The main difference between StarFive VisionFive 2 1.2A and 1.3B is gmac.
> You can read the PCB version of the current board by 
> get_pcb_revision_from_eeprom(), and then dynamically configure the difference 
> of gmac in spl_perform_fixups() according to different PCB versions, so that 
> one DT and one defconfig can support both 1.2A and 1.3B versions, which is 
> more user-friendly.
>
> Signed-off-by: Yanhong Wang 
> ---
>  board/starfive/visionfive2/spl.c | 157 +++
>  1 file changed, 157 insertions(+)

Reviewed-by: Rick Chen 


[PATCH v5 11/11] board: starfive: Dynamic configuration of DT for 1.2A and 1.3B

2023-06-15 Thread Yanhong Wang
The main difference between StarFive VisionFive 2 1.2A and 1.3B is gmac.
You can read the PCB version of the current board by
get_pcb_revision_from_eeprom(), and then dynamically configure the
difference of gmac in spl_perform_fixups() according to different PCB
versions, so that one DT and one defconfig can support both 1.2A and
1.3B versions, which is more user-friendly.

Signed-off-by: Yanhong Wang 
---
 board/starfive/visionfive2/spl.c | 157 +++
 1 file changed, 157 insertions(+)

diff --git a/board/starfive/visionfive2/spl.c b/board/starfive/visionfive2/spl.c
index db0b4cb433..7acd3995aa 100644
--- a/board/starfive/visionfive2/spl.c
+++ b/board/starfive/visionfive2/spl.c
@@ -5,16 +5,173 @@
  */
 
 #include 
+#include 
 #include 
 #include 
 #include 
+#include 
+#include 
+#include 
 #include 
 #include 
 
+DECLARE_GLOBAL_DATA_PTR;
 #define JH7110_CLK_CPU_ROOT_OFFSET 0x0U
 #define JH7110_CLK_CPU_ROOT_SHIFT  24
 #define JH7110_CLK_CPU_ROOT_MASK   GENMASK(29, 24)
 
+struct starfive_vf2_pro {
+   const char *path;
+   const char *name;
+   const char *value;
+};
+
+static const struct starfive_vf2_pro starfive_vera[] = {
+   {"/soc/ethernet@1603/mdio/ethernet-phy@0", "rx-internal-delay-ps",
+   "1900"},
+   {"/soc/ethernet@1603/mdio/ethernet-phy@0", "tx-internal-delay-ps",
+   "1350"}
+};
+
+static const struct starfive_vf2_pro starfive_verb[] = {
+   {"/soc/ethernet@1603", "starfive,tx-use-rgmii-clk", NULL},
+   {"/soc/ethernet@1604", "starfive,tx-use-rgmii-clk", NULL},
+
+   {"/soc/ethernet@1603/mdio/ethernet-phy@0",
+   "motorcomm,tx-clk-adj-enabled", NULL},
+   {"/soc/ethernet@1603/mdio/ethernet-phy@0",
+   "motorcomm,tx-clk-100-inverted", NULL},
+   {"/soc/ethernet@1603/mdio/ethernet-phy@0",
+   "motorcomm,tx-clk-1000-inverted", NULL},
+   {"/soc/ethernet@1603/mdio/ethernet-phy@0",
+   "rx-internal-delay-ps", "1900"},
+   {"/soc/ethernet@1603/mdio/ethernet-phy@0",
+   "tx-internal-delay-ps", "1500"},
+
+   {"/soc/ethernet@1604/mdio/ethernet-phy@1",
+   "motorcomm,tx-clk-adj-enabled", NULL},
+   { "/soc/ethernet@1604/mdio/ethernet-phy@1",
+   "motorcomm,tx-clk-100-inverted", NULL},
+   {"/soc/ethernet@1604/mdio/ethernet-phy@1",
+   "rx-internal-delay-ps", "0"},
+   {"/soc/ethernet@1604/mdio/ethernet-phy@1",
+   "tx-internal-delay-ps", "0"},
+};
+
+void spl_fdt_fixup_version_a(void *fdt)
+{
+   u32 phandle;
+   u8 i;
+   int offset;
+   int ret;
+
+   fdt_setprop_string(fdt, fdt_path_offset(fdt, "/"), "model",
+  "StarFive VisionFive 2 v1.2A");
+
+   offset = fdt_path_offset(fdt, "/soc/clock-controller@1302");
+   phandle = fdt_get_phandle(fdt, offset);
+   offset = fdt_path_offset(fdt, "/soc/ethernet@1604");
+
+   fdt_setprop_u32(fdt, offset, "assigned-clocks", phandle);
+   fdt_appendprop_u32(fdt, offset, "assigned-clocks", 
JH7110_SYSCLK_GMAC1_TX);
+   fdt_appendprop_u32(fdt, offset, "assigned-clocks", phandle);
+   fdt_appendprop_u32(fdt, offset, "assigned-clocks", 
JH7110_SYSCLK_GMAC1_RX);
+
+   fdt_setprop_u32(fdt, offset,  "assigned-clock-parents", phandle);
+   fdt_appendprop_u32(fdt, offset,  "assigned-clock-parents",
+  JH7110_SYSCLK_GMAC1_RMII_RTX);
+   fdt_appendprop_u32(fdt, offset,  "assigned-clock-parents", phandle);
+   fdt_appendprop_u32(fdt, offset,  "assigned-clock-parents",
+  JH7110_SYSCLK_GMAC1_RMII_RTX);
+
+   fdt_setprop_string(fdt, fdt_path_offset(fdt, "/soc/ethernet@1604"),
+  "phy-mode", "rmii");
+
+   for (i = 0; i < ARRAY_SIZE(starfive_vera); i++) {
+   offset = fdt_path_offset(fdt, starfive_vera[i].path);
+
+   if (starfive_vera[i].value)
+   ret = fdt_setprop_u32(fdt, offset,  
starfive_vera[i].name,
+ dectoul(starfive_vera[i].value, 
NULL));
+   else
+   ret = fdt_setprop_empty(fdt, offset, 
starfive_vera[i].name);
+
+   if (ret) {
+   pr_err("%s set prop %s fail.\n", __func__, 
starfive_vera[i].name);
+   break;
+   }
+   }
+}
+
+void spl_fdt_fixup_version_b(void *fdt)
+{
+   u32 phandle;
+   u8 i;
+   int offset;
+   int ret;
+
+   fdt_setprop_string(fdt, fdt_path_offset(fdt, "/"), "model",
+  "StarFive VisionFive 2 v1.3B");
+
+   /* gmac0 */
+   offset = fdt_path_offset(fdt, "/soc/clock-controller@1700");
+   phandle = fdt_get_phandle(fdt, offset);
+   offset = fdt_path_offset(fdt, "/soc/ethernet@1603");
+
+   fdt_setprop