dwmac-sunxi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 (gmac->interface == PHY_INTERFACE_MODE_RGMII) {
  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. clk_prepare(gmac->tx_clk);
  56. }
  57. return 0;
  58. }
  59. static void sun7i_gmac_exit(struct platform_device *pdev, void *priv)
  60. {
  61. struct sunxi_priv_data *gmac = priv;
  62. if (gmac->clk_enabled) {
  63. clk_disable(gmac->tx_clk);
  64. gmac->clk_enabled = 0;
  65. }
  66. clk_unprepare(gmac->tx_clk);
  67. if (gmac->regulator)
  68. regulator_disable(gmac->regulator);
  69. }
  70. static void sun7i_fix_speed(void *priv, unsigned int speed)
  71. {
  72. struct sunxi_priv_data *gmac = priv;
  73. /* only GMII mode requires us to reconfigure the clock lines */
  74. if (gmac->interface != PHY_INTERFACE_MODE_GMII)
  75. return;
  76. if (gmac->clk_enabled) {
  77. clk_disable(gmac->tx_clk);
  78. gmac->clk_enabled = 0;
  79. }
  80. clk_unprepare(gmac->tx_clk);
  81. if (speed == 1000) {
  82. clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE);
  83. clk_prepare_enable(gmac->tx_clk);
  84. gmac->clk_enabled = 1;
  85. } else {
  86. clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
  87. clk_prepare(gmac->tx_clk);
  88. }
  89. }
  90. static int sun7i_gmac_probe(struct platform_device *pdev)
  91. {
  92. struct plat_stmmacenet_data *plat_dat;
  93. struct stmmac_resources stmmac_res;
  94. struct sunxi_priv_data *gmac;
  95. struct device *dev = &pdev->dev;
  96. int ret;
  97. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  98. if (ret)
  99. return ret;
  100. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  101. if (IS_ERR(plat_dat))
  102. return PTR_ERR(plat_dat);
  103. gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL);
  104. if (!gmac) {
  105. ret = -ENOMEM;
  106. goto err_remove_config_dt;
  107. }
  108. gmac->interface = of_get_phy_mode(dev->of_node);
  109. gmac->tx_clk = devm_clk_get(dev, "allwinner_gmac_tx");
  110. if (IS_ERR(gmac->tx_clk)) {
  111. dev_err(dev, "could not get tx clock\n");
  112. ret = PTR_ERR(gmac->tx_clk);
  113. goto err_remove_config_dt;
  114. }
  115. /* Optional regulator for PHY */
  116. gmac->regulator = devm_regulator_get_optional(dev, "phy");
  117. if (IS_ERR(gmac->regulator)) {
  118. if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER) {
  119. ret = -EPROBE_DEFER;
  120. goto err_remove_config_dt;
  121. }
  122. dev_info(dev, "no regulator found\n");
  123. gmac->regulator = NULL;
  124. }
  125. /* platform data specifying hardware features and callbacks.
  126. * hardware features were copied from Allwinner drivers. */
  127. plat_dat->tx_coe = 1;
  128. plat_dat->has_gmac = true;
  129. plat_dat->bsp_priv = gmac;
  130. plat_dat->init = sun7i_gmac_init;
  131. plat_dat->exit = sun7i_gmac_exit;
  132. plat_dat->fix_mac_speed = sun7i_fix_speed;
  133. ret = sun7i_gmac_init(pdev, plat_dat->bsp_priv);
  134. if (ret)
  135. goto err_remove_config_dt;
  136. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  137. if (ret)
  138. goto err_gmac_exit;
  139. return 0;
  140. err_gmac_exit:
  141. sun7i_gmac_exit(pdev, plat_dat->bsp_priv);
  142. err_remove_config_dt:
  143. stmmac_remove_config_dt(pdev, plat_dat);
  144. return ret;
  145. }
  146. static const struct of_device_id sun7i_dwmac_match[] = {
  147. { .compatible = "allwinner,sun7i-a20-gmac" },
  148. { }
  149. };
  150. MODULE_DEVICE_TABLE(of, sun7i_dwmac_match);
  151. static struct platform_driver sun7i_dwmac_driver = {
  152. .probe = sun7i_gmac_probe,
  153. .remove = stmmac_pltfr_remove,
  154. .driver = {
  155. .name = "sun7i-dwmac",
  156. .pm = &stmmac_pltfr_pm_ops,
  157. .of_match_table = sun7i_dwmac_match,
  158. },
  159. };
  160. module_platform_driver(sun7i_dwmac_driver);
  161. MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
  162. MODULE_DESCRIPTION("Allwinner sunxi DWMAC specific glue layer");
  163. MODULE_LICENSE("GPL");