dwmac-starfive.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * StarFive DWMAC platform driver
  4. *
  5. * Copyright (C) 2021 Emil Renner Berthing <kernel@esmil.dk>
  6. * Copyright (C) 2022 StarFive Technology Co., Ltd.
  7. *
  8. */
  9. #include <linux/mod_devicetable.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/property.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/regmap.h>
  14. #include "stmmac_platform.h"
  15. #define STARFIVE_DWMAC_PHY_INFT_FIELD 0x7U
  16. #define JH7100_SYSMAIN_REGISTER49_DLYCHAIN 0xc8
  17. struct starfive_dwmac_data {
  18. unsigned int gtxclk_dlychain;
  19. };
  20. struct starfive_dwmac {
  21. struct device *dev;
  22. const struct starfive_dwmac_data *data;
  23. };
  24. static int starfive_dwmac_set_mode(struct plat_stmmacenet_data *plat_dat)
  25. {
  26. struct starfive_dwmac *dwmac = plat_dat->bsp_priv;
  27. struct regmap *regmap;
  28. unsigned int args[2];
  29. int phy_intf_sel;
  30. int err;
  31. phy_intf_sel = stmmac_get_phy_intf_sel(plat_dat->phy_interface);
  32. if (phy_intf_sel != PHY_INTF_SEL_RGMII &&
  33. phy_intf_sel != PHY_INTF_SEL_RMII) {
  34. dev_err(dwmac->dev, "unsupported interface %s\n",
  35. phy_modes(plat_dat->phy_interface));
  36. return phy_intf_sel < 0 ? phy_intf_sel : -EINVAL;
  37. }
  38. regmap = syscon_regmap_lookup_by_phandle_args(dwmac->dev->of_node,
  39. "starfive,syscon",
  40. 2, args);
  41. if (IS_ERR(regmap))
  42. return dev_err_probe(dwmac->dev, PTR_ERR(regmap), "getting the regmap failed\n");
  43. /* args[0]:offset args[1]: shift */
  44. err = regmap_update_bits(regmap, args[0],
  45. STARFIVE_DWMAC_PHY_INFT_FIELD << args[1],
  46. phy_intf_sel << args[1]);
  47. if (err)
  48. return dev_err_probe(dwmac->dev, err, "error setting phy mode\n");
  49. if (dwmac->data) {
  50. err = regmap_write(regmap, JH7100_SYSMAIN_REGISTER49_DLYCHAIN,
  51. dwmac->data->gtxclk_dlychain);
  52. if (err)
  53. return dev_err_probe(dwmac->dev, err,
  54. "error selecting gtxclk delay chain\n");
  55. }
  56. return 0;
  57. }
  58. static int starfive_dwmac_probe(struct platform_device *pdev)
  59. {
  60. struct plat_stmmacenet_data *plat_dat;
  61. struct stmmac_resources stmmac_res;
  62. struct starfive_dwmac *dwmac;
  63. struct clk *clk_gtx;
  64. int err;
  65. err = stmmac_get_platform_resources(pdev, &stmmac_res);
  66. if (err)
  67. return dev_err_probe(&pdev->dev, err,
  68. "failed to get resources\n");
  69. plat_dat = devm_stmmac_probe_config_dt(pdev, stmmac_res.mac);
  70. if (IS_ERR(plat_dat))
  71. return dev_err_probe(&pdev->dev, PTR_ERR(plat_dat),
  72. "dt configuration failed\n");
  73. dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
  74. if (!dwmac)
  75. return -ENOMEM;
  76. dwmac->data = device_get_match_data(&pdev->dev);
  77. plat_dat->clk_tx_i = devm_clk_get_enabled(&pdev->dev, "tx");
  78. if (IS_ERR(plat_dat->clk_tx_i))
  79. return dev_err_probe(&pdev->dev, PTR_ERR(plat_dat->clk_tx_i),
  80. "error getting tx clock\n");
  81. clk_gtx = devm_clk_get_enabled(&pdev->dev, "gtx");
  82. if (IS_ERR(clk_gtx))
  83. return dev_err_probe(&pdev->dev, PTR_ERR(clk_gtx),
  84. "error getting gtx clock\n");
  85. /* Generally, the rgmii_tx clock is provided by the internal clock,
  86. * which needs to match the corresponding clock frequency according
  87. * to different speeds. If the rgmii_tx clock is provided by the
  88. * external rgmii_rxin, there is no need to configure the clock
  89. * internally, because rgmii_rxin will be adaptively adjusted.
  90. */
  91. if (!device_property_read_bool(&pdev->dev, "starfive,tx-use-rgmii-clk"))
  92. plat_dat->set_clk_tx_rate = stmmac_set_clk_tx_rate;
  93. dwmac->dev = &pdev->dev;
  94. plat_dat->flags |= STMMAC_FLAG_EN_TX_LPI_CLK_PHY_CAP;
  95. plat_dat->bsp_priv = dwmac;
  96. plat_dat->dma_cfg->dche = true;
  97. err = starfive_dwmac_set_mode(plat_dat);
  98. if (err)
  99. return err;
  100. return stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  101. }
  102. static const struct starfive_dwmac_data jh7100_data = {
  103. .gtxclk_dlychain = 4,
  104. };
  105. static const struct of_device_id starfive_dwmac_match[] = {
  106. { .compatible = "starfive,jh7100-dwmac", .data = &jh7100_data },
  107. { .compatible = "starfive,jh7110-dwmac" },
  108. { /* sentinel */ }
  109. };
  110. MODULE_DEVICE_TABLE(of, starfive_dwmac_match);
  111. static struct platform_driver starfive_dwmac_driver = {
  112. .probe = starfive_dwmac_probe,
  113. .remove = stmmac_pltfr_remove,
  114. .driver = {
  115. .name = "starfive-dwmac",
  116. .pm = &stmmac_pltfr_pm_ops,
  117. .of_match_table = starfive_dwmac_match,
  118. },
  119. };
  120. module_platform_driver(starfive_dwmac_driver);
  121. MODULE_LICENSE("GPL");
  122. MODULE_DESCRIPTION("StarFive DWMAC platform driver");
  123. MODULE_AUTHOR("Emil Renner Berthing <kernel@esmil.dk>");
  124. MODULE_AUTHOR("Samin Guo <samin.guo@starfivetech.com>");