dwmac-intel-plat.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Intel DWMAC platform driver
  3. *
  4. * Copyright(C) 2020 Intel Corporation
  5. */
  6. #include <linux/ethtool.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/of_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/stmmac.h>
  12. #include "dwmac4.h"
  13. #include "stmmac.h"
  14. #include "stmmac_platform.h"
  15. struct intel_dwmac {
  16. struct device *dev;
  17. struct clk *tx_clk;
  18. const struct intel_dwmac_data *data;
  19. };
  20. struct intel_dwmac_data {
  21. void (*fix_mac_speed)(void *priv, unsigned int speed);
  22. unsigned long ptp_ref_clk_rate;
  23. unsigned long tx_clk_rate;
  24. bool tx_clk_en;
  25. };
  26. static void kmb_eth_fix_mac_speed(void *priv, unsigned int speed)
  27. {
  28. struct intel_dwmac *dwmac = priv;
  29. unsigned long rate;
  30. int ret;
  31. rate = clk_get_rate(dwmac->tx_clk);
  32. switch (speed) {
  33. case SPEED_1000:
  34. rate = 125000000;
  35. break;
  36. case SPEED_100:
  37. rate = 25000000;
  38. break;
  39. case SPEED_10:
  40. rate = 2500000;
  41. break;
  42. default:
  43. dev_err(dwmac->dev, "Invalid speed\n");
  44. break;
  45. }
  46. ret = clk_set_rate(dwmac->tx_clk, rate);
  47. if (ret)
  48. dev_err(dwmac->dev, "Failed to configure tx clock rate\n");
  49. }
  50. static const struct intel_dwmac_data kmb_data = {
  51. .fix_mac_speed = kmb_eth_fix_mac_speed,
  52. .ptp_ref_clk_rate = 200000000,
  53. .tx_clk_rate = 125000000,
  54. .tx_clk_en = true,
  55. };
  56. static const struct of_device_id intel_eth_plat_match[] = {
  57. { .compatible = "intel,keembay-dwmac", .data = &kmb_data },
  58. { }
  59. };
  60. MODULE_DEVICE_TABLE(of, intel_eth_plat_match);
  61. static int intel_eth_plat_probe(struct platform_device *pdev)
  62. {
  63. struct plat_stmmacenet_data *plat_dat;
  64. struct stmmac_resources stmmac_res;
  65. const struct of_device_id *match;
  66. struct intel_dwmac *dwmac;
  67. unsigned long rate;
  68. int ret;
  69. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  70. if (ret)
  71. return ret;
  72. plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
  73. if (IS_ERR(plat_dat)) {
  74. dev_err(&pdev->dev, "dt configuration failed\n");
  75. return PTR_ERR(plat_dat);
  76. }
  77. dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
  78. if (!dwmac) {
  79. ret = -ENOMEM;
  80. goto err_remove_config_dt;
  81. }
  82. dwmac->dev = &pdev->dev;
  83. dwmac->tx_clk = NULL;
  84. match = of_match_device(intel_eth_plat_match, &pdev->dev);
  85. if (match && match->data) {
  86. dwmac->data = (const struct intel_dwmac_data *)match->data;
  87. if (dwmac->data->fix_mac_speed)
  88. plat_dat->fix_mac_speed = dwmac->data->fix_mac_speed;
  89. /* Enable TX clock */
  90. if (dwmac->data->tx_clk_en) {
  91. dwmac->tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
  92. if (IS_ERR(dwmac->tx_clk)) {
  93. ret = PTR_ERR(dwmac->tx_clk);
  94. goto err_remove_config_dt;
  95. }
  96. clk_prepare_enable(dwmac->tx_clk);
  97. /* Check and configure TX clock rate */
  98. rate = clk_get_rate(dwmac->tx_clk);
  99. if (dwmac->data->tx_clk_rate &&
  100. rate != dwmac->data->tx_clk_rate) {
  101. rate = dwmac->data->tx_clk_rate;
  102. ret = clk_set_rate(dwmac->tx_clk, rate);
  103. if (ret) {
  104. dev_err(&pdev->dev,
  105. "Failed to set tx_clk\n");
  106. goto err_remove_config_dt;
  107. }
  108. }
  109. }
  110. /* Check and configure PTP ref clock rate */
  111. rate = clk_get_rate(plat_dat->clk_ptp_ref);
  112. if (dwmac->data->ptp_ref_clk_rate &&
  113. rate != dwmac->data->ptp_ref_clk_rate) {
  114. rate = dwmac->data->ptp_ref_clk_rate;
  115. ret = clk_set_rate(plat_dat->clk_ptp_ref, rate);
  116. if (ret) {
  117. dev_err(&pdev->dev,
  118. "Failed to set clk_ptp_ref\n");
  119. goto err_remove_config_dt;
  120. }
  121. }
  122. }
  123. plat_dat->bsp_priv = dwmac;
  124. plat_dat->eee_usecs_rate = plat_dat->clk_ptp_rate;
  125. if (plat_dat->eee_usecs_rate > 0) {
  126. u32 tx_lpi_usec;
  127. tx_lpi_usec = (plat_dat->eee_usecs_rate / 1000000) - 1;
  128. writel(tx_lpi_usec, stmmac_res.addr + GMAC_1US_TIC_COUNTER);
  129. }
  130. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  131. if (ret) {
  132. clk_disable_unprepare(dwmac->tx_clk);
  133. goto err_remove_config_dt;
  134. }
  135. return 0;
  136. err_remove_config_dt:
  137. stmmac_remove_config_dt(pdev, plat_dat);
  138. return ret;
  139. }
  140. static int intel_eth_plat_remove(struct platform_device *pdev)
  141. {
  142. struct intel_dwmac *dwmac = get_stmmac_bsp_priv(&pdev->dev);
  143. int ret;
  144. ret = stmmac_pltfr_remove(pdev);
  145. clk_disable_unprepare(dwmac->tx_clk);
  146. return ret;
  147. }
  148. static struct platform_driver intel_eth_plat_driver = {
  149. .probe = intel_eth_plat_probe,
  150. .remove = intel_eth_plat_remove,
  151. .driver = {
  152. .name = "intel-eth-plat",
  153. .pm = &stmmac_pltfr_pm_ops,
  154. .of_match_table = intel_eth_plat_match,
  155. },
  156. };
  157. module_platform_driver(intel_eth_plat_driver);
  158. MODULE_LICENSE("GPL v2");
  159. MODULE_DESCRIPTION("Intel DWMAC platform driver");