dwmac-sunxi.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/phy.h>
  21. #include <linux/of_net.h>
  22. #include <linux/regulator/consumer.h>
  23. #include "stmmac_platform.h"
  24. struct sunxi_priv_data {
  25. int interface;
  26. int clk_enabled;
  27. struct clk *tx_clk;
  28. struct regulator *regulator;
  29. };
  30. static void *sun7i_gmac_setup(struct platform_device *pdev)
  31. {
  32. struct sunxi_priv_data *gmac;
  33. struct device *dev = &pdev->dev;
  34. gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL);
  35. if (!gmac)
  36. return ERR_PTR(-ENOMEM);
  37. gmac->interface = of_get_phy_mode(dev->of_node);
  38. gmac->tx_clk = devm_clk_get(dev, "allwinner_gmac_tx");
  39. if (IS_ERR(gmac->tx_clk)) {
  40. dev_err(dev, "could not get tx clock\n");
  41. return gmac->tx_clk;
  42. }
  43. /* Optional regulator for PHY */
  44. gmac->regulator = devm_regulator_get_optional(dev, "phy");
  45. if (IS_ERR(gmac->regulator)) {
  46. if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER)
  47. return ERR_PTR(-EPROBE_DEFER);
  48. dev_info(dev, "no regulator found\n");
  49. gmac->regulator = NULL;
  50. }
  51. return gmac;
  52. }
  53. #define SUN7I_GMAC_GMII_RGMII_RATE 125000000
  54. #define SUN7I_GMAC_MII_RATE 25000000
  55. static int sun7i_gmac_init(struct platform_device *pdev, void *priv)
  56. {
  57. struct sunxi_priv_data *gmac = priv;
  58. int ret;
  59. if (gmac->regulator) {
  60. ret = regulator_enable(gmac->regulator);
  61. if (ret)
  62. return ret;
  63. }
  64. /* Set GMAC interface port mode
  65. *
  66. * The GMAC TX clock lines are configured by setting the clock
  67. * rate, which then uses the auto-reparenting feature of the
  68. * clock driver, and enabling/disabling the clock.
  69. */
  70. if (gmac->interface == PHY_INTERFACE_MODE_RGMII) {
  71. clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE);
  72. clk_prepare_enable(gmac->tx_clk);
  73. gmac->clk_enabled = 1;
  74. } else {
  75. clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
  76. clk_prepare(gmac->tx_clk);
  77. }
  78. return 0;
  79. }
  80. static void sun7i_gmac_exit(struct platform_device *pdev, void *priv)
  81. {
  82. struct sunxi_priv_data *gmac = priv;
  83. if (gmac->clk_enabled) {
  84. clk_disable(gmac->tx_clk);
  85. gmac->clk_enabled = 0;
  86. }
  87. clk_unprepare(gmac->tx_clk);
  88. if (gmac->regulator)
  89. regulator_disable(gmac->regulator);
  90. }
  91. static void sun7i_fix_speed(void *priv, unsigned int speed)
  92. {
  93. struct sunxi_priv_data *gmac = priv;
  94. /* only GMII mode requires us to reconfigure the clock lines */
  95. if (gmac->interface != PHY_INTERFACE_MODE_GMII)
  96. return;
  97. if (gmac->clk_enabled) {
  98. clk_disable(gmac->tx_clk);
  99. gmac->clk_enabled = 0;
  100. }
  101. clk_unprepare(gmac->tx_clk);
  102. if (speed == 1000) {
  103. clk_set_rate(gmac->tx_clk, SUN7I_GMAC_GMII_RGMII_RATE);
  104. clk_prepare_enable(gmac->tx_clk);
  105. gmac->clk_enabled = 1;
  106. } else {
  107. clk_set_rate(gmac->tx_clk, SUN7I_GMAC_MII_RATE);
  108. clk_prepare(gmac->tx_clk);
  109. }
  110. }
  111. /* of_data specifying hardware features and callbacks.
  112. * hardware features were copied from Allwinner drivers. */
  113. const struct stmmac_of_data sun7i_gmac_data = {
  114. .has_gmac = 1,
  115. .tx_coe = 1,
  116. .fix_mac_speed = sun7i_fix_speed,
  117. .setup = sun7i_gmac_setup,
  118. .init = sun7i_gmac_init,
  119. .exit = sun7i_gmac_exit,
  120. };