dwmac-sunxi.c 3.5 KB

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