dwmac-socfpga.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Copyright Altera Corporation (C) 2014. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License, version 2,
  5. * as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * Adopted from dwmac-sti.c
  16. */
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/of.h>
  19. #include <linux/of_net.h>
  20. #include <linux/phy.h>
  21. #include <linux/regmap.h>
  22. #include <linux/reset.h>
  23. #include <linux/stmmac.h>
  24. #include "stmmac.h"
  25. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII 0x0
  26. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII 0x1
  27. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII 0x2
  28. #define SYSMGR_EMACGRP_CTRL_PHYSEL_WIDTH 2
  29. #define SYSMGR_EMACGRP_CTRL_PHYSEL_MASK 0x00000003
  30. struct socfpga_dwmac {
  31. int interface;
  32. u32 reg_offset;
  33. u32 reg_shift;
  34. struct device *dev;
  35. struct regmap *sys_mgr_base_addr;
  36. struct reset_control *stmmac_rst;
  37. };
  38. static int socfpga_dwmac_parse_data(struct socfpga_dwmac *dwmac, struct device *dev)
  39. {
  40. struct device_node *np = dev->of_node;
  41. struct regmap *sys_mgr_base_addr;
  42. u32 reg_offset, reg_shift;
  43. int ret;
  44. dwmac->stmmac_rst = devm_reset_control_get(dev,
  45. STMMAC_RESOURCE_NAME);
  46. if (IS_ERR(dwmac->stmmac_rst)) {
  47. dev_info(dev, "Could not get reset control!\n");
  48. return -EINVAL;
  49. }
  50. dwmac->interface = of_get_phy_mode(np);
  51. sys_mgr_base_addr = syscon_regmap_lookup_by_phandle(np, "altr,sysmgr-syscon");
  52. if (IS_ERR(sys_mgr_base_addr)) {
  53. dev_info(dev, "No sysmgr-syscon node found\n");
  54. return PTR_ERR(sys_mgr_base_addr);
  55. }
  56. ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, &reg_offset);
  57. if (ret) {
  58. dev_info(dev, "Could not read reg_offset from sysmgr-syscon!\n");
  59. return -EINVAL;
  60. }
  61. ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 2, &reg_shift);
  62. if (ret) {
  63. dev_info(dev, "Could not read reg_shift from sysmgr-syscon!\n");
  64. return -EINVAL;
  65. }
  66. dwmac->reg_offset = reg_offset;
  67. dwmac->reg_shift = reg_shift;
  68. dwmac->sys_mgr_base_addr = sys_mgr_base_addr;
  69. dwmac->dev = dev;
  70. return 0;
  71. }
  72. static int socfpga_dwmac_setup(struct socfpga_dwmac *dwmac)
  73. {
  74. struct regmap *sys_mgr_base_addr = dwmac->sys_mgr_base_addr;
  75. int phymode = dwmac->interface;
  76. u32 reg_offset = dwmac->reg_offset;
  77. u32 reg_shift = dwmac->reg_shift;
  78. u32 ctrl, val;
  79. switch (phymode) {
  80. case PHY_INTERFACE_MODE_RGMII:
  81. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
  82. break;
  83. case PHY_INTERFACE_MODE_MII:
  84. case PHY_INTERFACE_MODE_GMII:
  85. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
  86. break;
  87. default:
  88. dev_err(dwmac->dev, "bad phy mode %d\n", phymode);
  89. return -EINVAL;
  90. }
  91. regmap_read(sys_mgr_base_addr, reg_offset, &ctrl);
  92. ctrl &= ~(SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << reg_shift);
  93. ctrl |= val << reg_shift;
  94. regmap_write(sys_mgr_base_addr, reg_offset, ctrl);
  95. return 0;
  96. }
  97. static void *socfpga_dwmac_probe(struct platform_device *pdev)
  98. {
  99. struct device *dev = &pdev->dev;
  100. int ret;
  101. struct socfpga_dwmac *dwmac;
  102. dwmac = devm_kzalloc(dev, sizeof(*dwmac), GFP_KERNEL);
  103. if (!dwmac)
  104. return ERR_PTR(-ENOMEM);
  105. ret = socfpga_dwmac_parse_data(dwmac, dev);
  106. if (ret) {
  107. dev_err(dev, "Unable to parse OF data\n");
  108. return ERR_PTR(ret);
  109. }
  110. ret = socfpga_dwmac_setup(dwmac);
  111. if (ret) {
  112. dev_err(dev, "couldn't setup SoC glue (%d)\n", ret);
  113. return ERR_PTR(ret);
  114. }
  115. return dwmac;
  116. }
  117. static void socfpga_dwmac_exit(struct platform_device *pdev, void *priv)
  118. {
  119. struct socfpga_dwmac *dwmac = priv;
  120. /* On socfpga platform exit, assert and hold reset to the
  121. * enet controller - the default state after a hard reset.
  122. */
  123. if (dwmac->stmmac_rst)
  124. reset_control_assert(dwmac->stmmac_rst);
  125. }
  126. static int socfpga_dwmac_init(struct platform_device *pdev, void *priv)
  127. {
  128. struct socfpga_dwmac *dwmac = priv;
  129. struct net_device *ndev = platform_get_drvdata(pdev);
  130. struct stmmac_priv *stpriv = NULL;
  131. int ret = 0;
  132. if (ndev)
  133. stpriv = netdev_priv(ndev);
  134. /* Assert reset to the enet controller before changing the phy mode */
  135. if (dwmac->stmmac_rst)
  136. reset_control_assert(dwmac->stmmac_rst);
  137. /* Setup the phy mode in the system manager registers according to
  138. * devicetree configuration
  139. */
  140. ret = socfpga_dwmac_setup(dwmac);
  141. /* Deassert reset for the phy configuration to be sampled by
  142. * the enet controller, and operation to start in requested mode
  143. */
  144. if (dwmac->stmmac_rst)
  145. reset_control_deassert(dwmac->stmmac_rst);
  146. /* Before the enet controller is suspended, the phy is suspended.
  147. * This causes the phy clock to be gated. The enet controller is
  148. * resumed before the phy, so the clock is still gated "off" when
  149. * the enet controller is resumed. This code makes sure the phy
  150. * is "resumed" before reinitializing the enet controller since
  151. * the enet controller depends on an active phy clock to complete
  152. * a DMA reset. A DMA reset will "time out" if executed
  153. * with no phy clock input on the Synopsys enet controller.
  154. * Verified through Synopsys Case #8000711656.
  155. *
  156. * Note that the phy clock is also gated when the phy is isolated.
  157. * Phy "suspend" and "isolate" controls are located in phy basic
  158. * control register 0, and can be modified by the phy driver
  159. * framework.
  160. */
  161. if (stpriv && stpriv->phydev)
  162. phy_resume(stpriv->phydev);
  163. return ret;
  164. }
  165. const struct stmmac_of_data socfpga_gmac_data = {
  166. .setup = socfpga_dwmac_probe,
  167. .init = socfpga_dwmac_init,
  168. .exit = socfpga_dwmac_exit,
  169. };