dwmac-sunxi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <wens@csie.org>
  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, unsigned int mode)
  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 = devm_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. return -ENOMEM;
  99. ret = of_get_phy_mode(dev->of_node, &gmac->interface);
  100. if (ret && ret != -ENODEV) {
  101. dev_err(dev, "Can't get phy-mode\n");
  102. return ret;
  103. }
  104. gmac->tx_clk = devm_clk_get(dev, "allwinner_gmac_tx");
  105. if (IS_ERR(gmac->tx_clk)) {
  106. dev_err(dev, "could not get tx clock\n");
  107. return PTR_ERR(gmac->tx_clk);
  108. }
  109. /* Optional regulator for PHY */
  110. gmac->regulator = devm_regulator_get_optional(dev, "phy");
  111. if (IS_ERR(gmac->regulator)) {
  112. if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER)
  113. return -EPROBE_DEFER;
  114. dev_info(dev, "no regulator found\n");
  115. gmac->regulator = NULL;
  116. }
  117. /* platform data specifying hardware features and callbacks.
  118. * hardware features were copied from Allwinner drivers. */
  119. plat_dat->tx_coe = 1;
  120. plat_dat->has_gmac = true;
  121. plat_dat->bsp_priv = gmac;
  122. plat_dat->init = sun7i_gmac_init;
  123. plat_dat->exit = sun7i_gmac_exit;
  124. plat_dat->fix_mac_speed = sun7i_fix_speed;
  125. plat_dat->tx_fifo_size = 4096;
  126. plat_dat->rx_fifo_size = 16384;
  127. ret = sun7i_gmac_init(pdev, plat_dat->bsp_priv);
  128. if (ret)
  129. return ret;
  130. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  131. if (ret)
  132. goto err_gmac_exit;
  133. return 0;
  134. err_gmac_exit:
  135. sun7i_gmac_exit(pdev, plat_dat->bsp_priv);
  136. return ret;
  137. }
  138. static const struct of_device_id sun7i_dwmac_match[] = {
  139. { .compatible = "allwinner,sun7i-a20-gmac" },
  140. { }
  141. };
  142. MODULE_DEVICE_TABLE(of, sun7i_dwmac_match);
  143. static struct platform_driver sun7i_dwmac_driver = {
  144. .probe = sun7i_gmac_probe,
  145. .remove_new = stmmac_pltfr_remove,
  146. .driver = {
  147. .name = "sun7i-dwmac",
  148. .pm = &stmmac_pltfr_pm_ops,
  149. .of_match_table = sun7i_dwmac_match,
  150. },
  151. };
  152. module_platform_driver(sun7i_dwmac_driver);
  153. MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
  154. MODULE_DESCRIPTION("Allwinner sunxi DWMAC specific glue layer");
  155. MODULE_LICENSE("GPL");