dwmac-oxnas.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <mach/hardware.h>
  28. #include "stmmac.h"
  29. #include "stmmac_platform.h"
  30. struct oxnas_gmac {
  31. struct clk *clk;
  32. };
  33. static int oxnas_gmac_init(struct platform_device *pdev, void *priv)
  34. {
  35. struct oxnas_gmac *bsp_priv = priv;
  36. int ret = 0;
  37. unsigned value;
  38. ret = device_reset(&pdev->dev);
  39. if (ret)
  40. return ret;
  41. if (IS_ERR(bsp_priv->clk))
  42. return PTR_ERR(bsp_priv->clk);
  43. clk_prepare_enable(bsp_priv->clk);
  44. value = readl(SYS_CTRL_GMAC_CTRL);
  45. /* Enable GMII_GTXCLK to follow GMII_REFCLK, required for gigabit PHY */
  46. value |= BIT(SYS_CTRL_GMAC_CKEN_GTX);
  47. /* Use simple mux for 25/125 Mhz clock switching */
  48. value |= BIT(SYS_CTRL_GMAC_SIMPLE_MUX);
  49. /* set auto switch tx clock source */
  50. value |= BIT(SYS_CTRL_GMAC_AUTO_TX_SOURCE);
  51. /* enable tx & rx vardelay */
  52. value |= BIT(SYS_CTRL_GMAC_CKEN_TX_OUT);
  53. value |= BIT(SYS_CTRL_GMAC_CKEN_TXN_OUT);
  54. value |= BIT(SYS_CTRL_GMAC_CKEN_TX_IN);
  55. value |= BIT(SYS_CTRL_GMAC_CKEN_RX_OUT);
  56. value |= BIT(SYS_CTRL_GMAC_CKEN_RXN_OUT);
  57. value |= BIT(SYS_CTRL_GMAC_CKEN_RX_IN);
  58. writel(value, SYS_CTRL_GMAC_CTRL);
  59. /* set tx & rx vardelay */
  60. value = 0;
  61. value |= SYS_CTRL_GMAC_TX_VARDELAY(4);
  62. value |= SYS_CTRL_GMAC_TXN_VARDELAY(2);
  63. value |= SYS_CTRL_GMAC_RX_VARDELAY(10);
  64. value |= SYS_CTRL_GMAC_RXN_VARDELAY(8);
  65. writel(value, SYS_CTRL_GMAC_DELAY_CTRL);
  66. return 0;
  67. }
  68. static void oxnas_gmac_exit(struct platform_device *pdev, void *priv)
  69. {
  70. struct reset_control *rstc;
  71. clk_disable_unprepare(priv);
  72. devm_clk_put(&pdev->dev, priv);
  73. rstc = reset_control_get(&pdev->dev, NULL);
  74. if (!IS_ERR(rstc)) {
  75. reset_control_assert(rstc);
  76. reset_control_put(rstc);
  77. }
  78. }
  79. static int oxnas_gmac_probe(struct platform_device *pdev)
  80. {
  81. struct plat_stmmacenet_data *plat_dat;
  82. struct stmmac_resources stmmac_res;
  83. int ret;
  84. struct device *dev = &pdev->dev;
  85. struct oxnas_gmac *bsp_priv;
  86. bsp_priv = devm_kzalloc(dev, sizeof(*bsp_priv), GFP_KERNEL);
  87. if (!bsp_priv)
  88. return -ENOMEM;
  89. bsp_priv->clk = devm_clk_get(dev, "gmac");
  90. if (IS_ERR(bsp_priv->clk))
  91. return PTR_ERR(bsp_priv->clk);
  92. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  93. if (ret)
  94. return ret;
  95. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  96. if (IS_ERR(plat_dat))
  97. return PTR_ERR(plat_dat);
  98. plat_dat->bsp_priv = bsp_priv;
  99. plat_dat->init = oxnas_gmac_init;
  100. plat_dat->exit = oxnas_gmac_exit;
  101. ret = oxnas_gmac_init(pdev, bsp_priv);
  102. if (ret)
  103. return ret;
  104. return stmmac_dvr_probe(dev, plat_dat, &stmmac_res);
  105. }
  106. static const struct of_device_id oxnas_gmac_match[] = {
  107. { .compatible = "plxtech,nas782x-gmac" },
  108. { }
  109. };
  110. MODULE_DEVICE_TABLE(of, oxnas_gmac_match);
  111. static struct platform_driver oxnas_gmac_driver = {
  112. .probe = oxnas_gmac_probe,
  113. .remove = stmmac_pltfr_remove,
  114. .driver = {
  115. .name = "oxnas-gmac",
  116. .pm = &stmmac_pltfr_pm_ops,
  117. .of_match_table = oxnas_gmac_match,
  118. },
  119. };
  120. module_platform_driver(oxnas_gmac_driver);
  121. MODULE_LICENSE("GPL v2");