dwmac-sunxi.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * dwmac-sunxi.c - Allwinner sunxi DWMAC specific glue layer
  4. *
  5. * Copyright (C) 2013 Chen-Yu Tsai
  6. *
  7. * Chen-Yu Tsai <[email protected]>
  8. */
  9. #include <linux/stmmac.h>
  10. #include <linux/clk.h>
  11. #include <linux/module.h>
  12. #include <linux/phy.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/of_net.h>
  15. #include <linux/regulator/consumer.h>
  16. #include "stmmac_platform.h"
  17. struct sunxi_priv_data {
  18. phy_interface_t interface;
  19. int clk_enabled;
  20. struct clk *tx_clk;
  21. struct regulator *regulator;
  22. };
  23. #define SUN7I_GMAC_GMII_RGMII_RATE 125000000
  24. #define SUN7I_GMAC_MII_RATE 25000000
  25. static int sun7i_gmac_init(struct platform_device *pdev, void *priv)
  26. {
  27. struct sunxi_priv_data *gmac = priv;
  28. int ret = 0;
  29. if (gmac->regulator) {
  30. ret = regulator_enable(gmac->regulator);
  31. if (ret)
  32. return ret;
  33. }
  34. /* Set GMAC interface port mode
  35. *
  36. * The GMAC TX clock lines are configured by setting the clock
  37. * rate, which then uses the auto-reparenting feature of the
  38. * clock driver, and enabling/disabling the clock.
  39. */
  40. if (phy_interface_mode_is_rgmii(gmac->interface)) {
  41. clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE);
  42. clk_prepare_enable(gmac->tx_clk);
  43. gmac->clk_enabled = 1;
  44. } else {
  45. clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
  46. ret = clk_prepare(gmac->tx_clk);
  47. if (ret && gmac->regulator)
  48. regulator_disable(gmac->regulator);
  49. }
  50. return ret;
  51. }
  52. static void sun7i_gmac_exit(struct platform_device *pdev, void *priv)
  53. {
  54. struct sunxi_priv_data *gmac = priv;
  55. if (gmac->clk_enabled) {
  56. clk_disable(gmac->tx_clk);
  57. gmac->clk_enabled = 0;
  58. }
  59. clk_unprepare(gmac->tx_clk);
  60. if (gmac->regulator)
  61. regulator_disable(gmac->regulator);
  62. }
  63. static void sun7i_fix_speed(void *priv, unsigned int speed)
  64. {
  65. struct sunxi_priv_data *gmac = priv;
  66. /* only GMII mode requires us to reconfigure the clock lines */
  67. if (gmac->interface != PHY_INTERFACE_MODE_GMII)
  68. return;
  69. if (gmac->clk_enabled) {
  70. clk_disable(gmac->tx_clk);
  71. gmac->clk_enabled = 0;
  72. }
  73. clk_unprepare(gmac->tx_clk);
  74. if (speed == 1000) {
  75. clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE);
  76. clk_prepare_enable(gmac->tx_clk);
  77. gmac->clk_enabled = 1;
  78. } else {
  79. clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
  80. clk_prepare(gmac->tx_clk);
  81. }
  82. }
  83. static int sun7i_gmac_probe(struct platform_device *pdev)
  84. {
  85. struct plat_stmmacenet_data *plat_dat;
  86. struct stmmac_resources stmmac_res;
  87. struct sunxi_priv_data *gmac;
  88. struct device *dev = &pdev->dev;
  89. int ret;
  90. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  91. if (ret)
  92. return ret;
  93. plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
  94. if (IS_ERR(plat_dat))
  95. return PTR_ERR(plat_dat);
  96. gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL);
  97. if (!gmac) {
  98. ret = -ENOMEM;
  99. goto err_remove_config_dt;
  100. }
  101. ret = of_get_phy_mode(dev->of_node, &gmac->interface);
  102. if (ret && ret != -ENODEV) {
  103. dev_err(dev, "Can't get phy-mode\n");
  104. goto err_remove_config_dt;
  105. }
  106. gmac->tx_clk = devm_clk_get(dev, "allwinner_gmac_tx");
  107. if (IS_ERR(gmac->tx_clk)) {
  108. dev_err(dev, "could not get tx clock\n");
  109. ret = PTR_ERR(gmac->tx_clk);
  110. goto err_remove_config_dt;
  111. }
  112. /* Optional regulator for PHY */
  113. gmac->regulator = devm_regulator_get_optional(dev, "phy");
  114. if (IS_ERR(gmac->regulator)) {
  115. if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER) {
  116. ret = -EPROBE_DEFER;
  117. goto err_remove_config_dt;
  118. }
  119. dev_info(dev, "no regulator found\n");
  120. gmac->regulator = NULL;
  121. }
  122. /* platform data specifying hardware features and callbacks.
  123. * hardware features were copied from Allwinner drivers. */
  124. plat_dat->tx_coe = 1;
  125. plat_dat->has_gmac = true;
  126. plat_dat->bsp_priv = gmac;
  127. plat_dat->init = sun7i_gmac_init;
  128. plat_dat->exit = sun7i_gmac_exit;
  129. plat_dat->fix_mac_speed = sun7i_fix_speed;
  130. plat_dat->tx_fifo_size = 4096;
  131. plat_dat->rx_fifo_size = 16384;
  132. ret = sun7i_gmac_init(pdev, plat_dat->bsp_priv);
  133. if (ret)
  134. goto err_remove_config_dt;
  135. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  136. if (ret)
  137. goto err_gmac_exit;
  138. return 0;
  139. err_gmac_exit:
  140. sun7i_gmac_exit(pdev, plat_dat->bsp_priv);
  141. err_remove_config_dt:
  142. stmmac_remove_config_dt(pdev, plat_dat);
  143. return ret;
  144. }
  145. static const struct of_device_id sun7i_dwmac_match[] = {
  146. { .compatible = "allwinner,sun7i-a20-gmac" },
  147. { }
  148. };
  149. MODULE_DEVICE_TABLE(of, sun7i_dwmac_match);
  150. static struct platform_driver sun7i_dwmac_driver = {
  151. .probe = sun7i_gmac_probe,
  152. .remove = stmmac_pltfr_remove,
  153. .driver = {
  154. .name = "sun7i-dwmac",
  155. .pm = &stmmac_pltfr_pm_ops,
  156. .of_match_table = sun7i_dwmac_match,
  157. },
  158. };
  159. module_platform_driver(sun7i_dwmac_driver);
  160. MODULE_AUTHOR("Chen-Yu Tsai <[email protected]>");
  161. MODULE_DESCRIPTION("Allwinner sunxi DWMAC specific glue layer");
  162. MODULE_LICENSE("GPL");