starfive: refresh patches
[openwrt/staging/981213.git] / target / linux / starfive / patches-6.1 / 0044-net-stmmac-Add-glue-layer-for-StarFive-JH7110-SoC.patch
1 From 7c82049adb0460985dd6a1e5c9b6954d901247d2 Mon Sep 17 00:00:00 2001
2 From: Samin Guo <samin.guo@starfivetech.com>
3 Date: Fri, 3 Mar 2023 16:50:58 +0800
4 Subject: [PATCH 044/122] net: stmmac: Add glue layer for StarFive JH7110 SoC
5
6 This adds StarFive dwmac driver support on the StarFive JH7110 SoC.
7
8 Tested-by: Tommaso Merciai <tomm.merciai@gmail.com>
9 Co-developed-by: Emil Renner Berthing <kernel@esmil.dk>
10 Signed-off-by: Emil Renner Berthing <kernel@esmil.dk>
11 Signed-off-by: Paolo Abeni <pabeni@redhat.com>
12 Signed-off-by: Samin Guo <samin.guo@starfivetech.com>
13 ---
14 MAINTAINERS | 1 +
15 drivers/net/ethernet/stmicro/stmmac/Kconfig | 12 ++
16 drivers/net/ethernet/stmicro/stmmac/Makefile | 1 +
17 .../ethernet/stmicro/stmmac/dwmac-starfive.c | 123 ++++++++++++++++++
18 4 files changed, 137 insertions(+)
19 create mode 100644 drivers/net/ethernet/stmicro/stmmac/dwmac-starfive.c
20
21 --- a/MAINTAINERS
22 +++ b/MAINTAINERS
23 @@ -19662,6 +19662,7 @@ M: Emil Renner Berthing <kernel@esmil.dk
24 M: Samin Guo <samin.guo@starfivetech.com>
25 S: Maintained
26 F: Documentation/devicetree/bindings/net/starfive,jh7110-dwmac.yaml
27 +F: drivers/net/ethernet/stmicro/stmmac/dwmac-starfive.c
28
29 STARFIVE JH7110 PLL CLOCK DRIVER
30 M: Xingyu Wu <xingyu.wu@starfivetech.com>
31 --- a/drivers/net/ethernet/stmicro/stmmac/Kconfig
32 +++ b/drivers/net/ethernet/stmicro/stmmac/Kconfig
33 @@ -165,6 +165,18 @@ config DWMAC_SOCFPGA
34 for the stmmac device driver. This driver is used for
35 arria5 and cyclone5 FPGA SoCs.
36
37 +config DWMAC_STARFIVE
38 + tristate "StarFive dwmac support"
39 + depends on OF && (ARCH_STARFIVE || COMPILE_TEST)
40 + select MFD_SYSCON
41 + default m if ARCH_STARFIVE
42 + help
43 + Support for ethernet controllers on StarFive RISC-V SoCs
44 +
45 + This selects the StarFive platform specific glue layer support for
46 + the stmmac device driver. This driver is used for StarFive JH7110
47 + ethernet controller.
48 +
49 config DWMAC_STI
50 tristate "STi GMAC support"
51 default ARCH_STI
52 --- a/drivers/net/ethernet/stmicro/stmmac/Makefile
53 +++ b/drivers/net/ethernet/stmicro/stmmac/Makefile
54 @@ -23,6 +23,7 @@ obj-$(CONFIG_DWMAC_OXNAS) += dwmac-oxnas
55 obj-$(CONFIG_DWMAC_QCOM_ETHQOS) += dwmac-qcom-ethqos.o
56 obj-$(CONFIG_DWMAC_ROCKCHIP) += dwmac-rk.o
57 obj-$(CONFIG_DWMAC_SOCFPGA) += dwmac-altr-socfpga.o
58 +obj-$(CONFIG_DWMAC_STARFIVE) += dwmac-starfive.o
59 obj-$(CONFIG_DWMAC_STI) += dwmac-sti.o
60 obj-$(CONFIG_DWMAC_STM32) += dwmac-stm32.o
61 obj-$(CONFIG_DWMAC_SUNXI) += dwmac-sunxi.o
62 --- /dev/null
63 +++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-starfive.c
64 @@ -0,0 +1,123 @@
65 +// SPDX-License-Identifier: GPL-2.0+
66 +/*
67 + * StarFive DWMAC platform driver
68 + *
69 + * Copyright (C) 2021 Emil Renner Berthing <kernel@esmil.dk>
70 + * Copyright (C) 2022 StarFive Technology Co., Ltd.
71 + *
72 + */
73 +
74 +#include <linux/mfd/syscon.h>
75 +#include <linux/of_device.h>
76 +#include <linux/regmap.h>
77 +
78 +#include "stmmac_platform.h"
79 +
80 +struct starfive_dwmac {
81 + struct device *dev;
82 + struct clk *clk_tx;
83 +};
84 +
85 +static void starfive_dwmac_fix_mac_speed(void *priv, unsigned int speed)
86 +{
87 + struct starfive_dwmac *dwmac = priv;
88 + unsigned long rate;
89 + int err;
90 +
91 + rate = clk_get_rate(dwmac->clk_tx);
92 +
93 + switch (speed) {
94 + case SPEED_1000:
95 + rate = 125000000;
96 + break;
97 + case SPEED_100:
98 + rate = 25000000;
99 + break;
100 + case SPEED_10:
101 + rate = 2500000;
102 + break;
103 + default:
104 + dev_err(dwmac->dev, "invalid speed %u\n", speed);
105 + break;
106 + }
107 +
108 + err = clk_set_rate(dwmac->clk_tx, rate);
109 + if (err)
110 + dev_err(dwmac->dev, "failed to set tx rate %lu\n", rate);
111 +}
112 +
113 +static int starfive_dwmac_probe(struct platform_device *pdev)
114 +{
115 + struct plat_stmmacenet_data *plat_dat;
116 + struct stmmac_resources stmmac_res;
117 + struct starfive_dwmac *dwmac;
118 + struct clk *clk_gtx;
119 + int err;
120 +
121 + err = stmmac_get_platform_resources(pdev, &stmmac_res);
122 + if (err)
123 + return dev_err_probe(&pdev->dev, err,
124 + "failed to get resources\n");
125 +
126 + plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
127 + if (IS_ERR(plat_dat))
128 + return dev_err_probe(&pdev->dev, PTR_ERR(plat_dat),
129 + "dt configuration failed\n");
130 +
131 + dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
132 + if (!dwmac)
133 + return -ENOMEM;
134 +
135 + dwmac->clk_tx = devm_clk_get_enabled(&pdev->dev, "tx");
136 + if (IS_ERR(dwmac->clk_tx))
137 + return dev_err_probe(&pdev->dev, PTR_ERR(dwmac->clk_tx),
138 + "error getting tx clock\n");
139 +
140 + clk_gtx = devm_clk_get_enabled(&pdev->dev, "gtx");
141 + if (IS_ERR(clk_gtx))
142 + return dev_err_probe(&pdev->dev, PTR_ERR(clk_gtx),
143 + "error getting gtx clock\n");
144 +
145 + /* Generally, the rgmii_tx clock is provided by the internal clock,
146 + * which needs to match the corresponding clock frequency according
147 + * to different speeds. If the rgmii_tx clock is provided by the
148 + * external rgmii_rxin, there is no need to configure the clock
149 + * internally, because rgmii_rxin will be adaptively adjusted.
150 + */
151 + if (!device_property_read_bool(&pdev->dev, "starfive,tx-use-rgmii-clk"))
152 + plat_dat->fix_mac_speed = starfive_dwmac_fix_mac_speed;
153 +
154 + dwmac->dev = &pdev->dev;
155 + plat_dat->bsp_priv = dwmac;
156 + plat_dat->dma_cfg->dche = true;
157 +
158 + err = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
159 + if (err) {
160 + stmmac_remove_config_dt(pdev, plat_dat);
161 + return err;
162 + }
163 +
164 + return 0;
165 +}
166 +
167 +static const struct of_device_id starfive_dwmac_match[] = {
168 + { .compatible = "starfive,jh7110-dwmac" },
169 + { /* sentinel */ }
170 +};
171 +MODULE_DEVICE_TABLE(of, starfive_dwmac_match);
172 +
173 +static struct platform_driver starfive_dwmac_driver = {
174 + .probe = starfive_dwmac_probe,
175 + .remove = stmmac_pltfr_remove,
176 + .driver = {
177 + .name = "starfive-dwmac",
178 + .pm = &stmmac_pltfr_pm_ops,
179 + .of_match_table = starfive_dwmac_match,
180 + },
181 +};
182 +module_platform_driver(starfive_dwmac_driver);
183 +
184 +MODULE_LICENSE("GPL");
185 +MODULE_DESCRIPTION("StarFive DWMAC platform driver");
186 +MODULE_AUTHOR("Emil Renner Berthing <kernel@esmil.dk>");
187 +MODULE_AUTHOR("Samin Guo <samin.guo@starfivetech.com>");