dwmac-oxnas.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright OpenWrt.org (C) 2015.
  2. * Copyright Altera Corporation (C) 2014. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Adopted from dwmac-socfpga.c
  17. * Based on code found in mach-oxnas.c
  18. */
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_net.h>
  23. #include <linux/phy.h>
  24. #include <linux/regmap.h>
  25. #include <linux/reset.h>
  26. #include <linux/stmmac.h>
  27. #include <linux/version.h>
  28. #include <mach/hardware.h>
  29. #include "stmmac.h"
  30. #include "stmmac_platform.h"
  31. struct oxnas_gmac {
  32. struct clk *clk;
  33. };
  34. static int oxnas_gmac_init(struct platform_device *pdev, void *priv)
  35. {
  36. struct oxnas_gmac *bsp_priv = priv;
  37. int ret = 0;
  38. unsigned value;
  39. ret = device_reset(&pdev->dev);
  40. if (ret)
  41. return ret;
  42. if (IS_ERR(bsp_priv->clk))
  43. return PTR_ERR(bsp_priv->clk);
  44. clk_prepare_enable(bsp_priv->clk);
  45. value = readl(SYS_CTRL_GMAC_CTRL);
  46. /* Enable GMII_GTXCLK to follow GMII_REFCLK, required for gigabit PHY */
  47. value |= BIT(SYS_CTRL_GMAC_CKEN_GTX);
  48. /* Use simple mux for 25/125 Mhz clock switching */
  49. value |= BIT(SYS_CTRL_GMAC_SIMPLE_MUX);
  50. /* set auto switch tx clock source */
  51. value |= BIT(SYS_CTRL_GMAC_AUTO_TX_SOURCE);
  52. /* enable tx & rx vardelay */
  53. value |= BIT(SYS_CTRL_GMAC_CKEN_TX_OUT);
  54. value |= BIT(SYS_CTRL_GMAC_CKEN_TXN_OUT);
  55. value |= BIT(SYS_CTRL_GMAC_CKEN_TX_IN);
  56. value |= BIT(SYS_CTRL_GMAC_CKEN_RX_OUT);
  57. value |= BIT(SYS_CTRL_GMAC_CKEN_RXN_OUT);
  58. value |= BIT(SYS_CTRL_GMAC_CKEN_RX_IN);
  59. writel(value, SYS_CTRL_GMAC_CTRL);
  60. /* set tx & rx vardelay */
  61. value = 0;
  62. value |= SYS_CTRL_GMAC_TX_VARDELAY(4);
  63. value |= SYS_CTRL_GMAC_TXN_VARDELAY(2);
  64. value |= SYS_CTRL_GMAC_RX_VARDELAY(10);
  65. value |= SYS_CTRL_GMAC_RXN_VARDELAY(8);
  66. writel(value, SYS_CTRL_GMAC_DELAY_CTRL);
  67. return 0;
  68. }
  69. static void oxnas_gmac_exit(struct platform_device *pdev, void *priv)
  70. {
  71. struct reset_control *rstc;
  72. clk_disable_unprepare(priv);
  73. devm_clk_put(&pdev->dev, priv);
  74. rstc = reset_control_get(&pdev->dev, NULL);
  75. if (!IS_ERR(rstc)) {
  76. reset_control_assert(rstc);
  77. reset_control_put(rstc);
  78. }
  79. }
  80. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
  81. static void *oxnas_gmac_probe(struct platform_device *pdev)
  82. {
  83. #else
  84. static int oxnas_gmac_probe(struct platform_device *pdev)
  85. {
  86. struct plat_stmmacenet_data *plat_dat;
  87. struct stmmac_resources stmmac_res;
  88. int ret;
  89. #endif
  90. struct device *dev = &pdev->dev;
  91. struct oxnas_gmac *bsp_priv;
  92. bsp_priv = devm_kzalloc(dev, sizeof(*bsp_priv), GFP_KERNEL);
  93. if (!bsp_priv)
  94. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
  95. return ERR_PTR(-ENOMEM);
  96. #else
  97. return -ENOMEM;
  98. #endif
  99. bsp_priv->clk = devm_clk_get(dev, "gmac");
  100. if (IS_ERR(bsp_priv->clk))
  101. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
  102. return bsp_priv->clk;
  103. #else
  104. return PTR_ERR(bsp_priv->clk);
  105. #endif
  106. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
  107. return bsp_priv;
  108. #else
  109. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  110. if (ret)
  111. return ret;
  112. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  113. if (IS_ERR(plat_dat))
  114. return PTR_ERR(plat_dat);
  115. plat_dat->bsp_priv = bsp_priv;
  116. plat_dat->init = oxnas_gmac_init;
  117. plat_dat->exit = oxnas_gmac_exit;
  118. ret = oxnas_gmac_init(pdev, bsp_priv);
  119. if (ret)
  120. return ret;
  121. return stmmac_dvr_probe(dev, plat_dat, &stmmac_res);
  122. #endif
  123. }
  124. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
  125. const struct stmmac_of_data oxnas_gmac_data = {
  126. .has_gmac = 1,
  127. .setup = oxnas_gmac_probe,
  128. .init = oxnas_gmac_init,
  129. .exit = oxnas_gmac_exit,
  130. };
  131. #else
  132. static const struct of_device_id oxnas_gmac_match[] = {
  133. { .compatible = "plxtech,nas782x-gmac" },
  134. { }
  135. };
  136. MODULE_DEVICE_TABLE(of, oxnas_gmac_match);
  137. static struct platform_driver oxnas_gmac_driver = {
  138. .probe = oxnas_gmac_probe,
  139. .remove = stmmac_pltfr_remove,
  140. .driver = {
  141. .name = "oxnas-gmac",
  142. .pm = &stmmac_pltfr_pm_ops,
  143. .of_match_table = oxnas_gmac_match,
  144. },
  145. };
  146. module_platform_driver(oxnas_gmac_driver);
  147. #endif
  148. MODULE_LICENSE("GPL v2");