dwmac-sunxi.c 4.9 KB

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