0044-net-stmmac-Add-glue-layer-for-StarFive-JH7110-SoC.patch 5.8 KB

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