dwmac-socfpga.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_address.h>
  20. #include <linux/of_net.h>
  21. #include <linux/phy.h>
  22. #include <linux/regmap.h>
  23. #include <linux/reset.h>
  24. #include <linux/stmmac.h>
  25. #include "stmmac.h"
  26. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII 0x0
  27. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII 0x1
  28. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII 0x2
  29. #define SYSMGR_EMACGRP_CTRL_PHYSEL_WIDTH 2
  30. #define SYSMGR_EMACGRP_CTRL_PHYSEL_MASK 0x00000003
  31. #define EMAC_SPLITTER_CTRL_REG 0x0
  32. #define EMAC_SPLITTER_CTRL_SPEED_MASK 0x3
  33. #define EMAC_SPLITTER_CTRL_SPEED_10 0x2
  34. #define EMAC_SPLITTER_CTRL_SPEED_100 0x3
  35. #define EMAC_SPLITTER_CTRL_SPEED_1000 0x0
  36. struct socfpga_dwmac {
  37. int interface;
  38. u32 reg_offset;
  39. u32 reg_shift;
  40. struct device *dev;
  41. struct regmap *sys_mgr_base_addr;
  42. struct reset_control *stmmac_rst;
  43. void __iomem *splitter_base;
  44. };
  45. static void socfpga_dwmac_fix_mac_speed(void *priv, unsigned int speed)
  46. {
  47. struct socfpga_dwmac *dwmac = (struct socfpga_dwmac *)priv;
  48. void __iomem *splitter_base = dwmac->splitter_base;
  49. u32 val;
  50. if (!splitter_base)
  51. return;
  52. val = readl(splitter_base + EMAC_SPLITTER_CTRL_REG);
  53. val &= ~EMAC_SPLITTER_CTRL_SPEED_MASK;
  54. switch (speed) {
  55. case 1000:
  56. val |= EMAC_SPLITTER_CTRL_SPEED_1000;
  57. break;
  58. case 100:
  59. val |= EMAC_SPLITTER_CTRL_SPEED_100;
  60. break;
  61. case 10:
  62. val |= EMAC_SPLITTER_CTRL_SPEED_10;
  63. break;
  64. default:
  65. return;
  66. }
  67. writel(val, splitter_base + EMAC_SPLITTER_CTRL_REG);
  68. }
  69. static int socfpga_dwmac_parse_data(struct socfpga_dwmac *dwmac, struct device *dev)
  70. {
  71. struct device_node *np = dev->of_node;
  72. struct regmap *sys_mgr_base_addr;
  73. u32 reg_offset, reg_shift;
  74. int ret;
  75. struct device_node *np_splitter;
  76. struct resource res_splitter;
  77. dwmac->stmmac_rst = devm_reset_control_get(dev,
  78. STMMAC_RESOURCE_NAME);
  79. if (IS_ERR(dwmac->stmmac_rst)) {
  80. dev_info(dev, "Could not get reset control!\n");
  81. return -EINVAL;
  82. }
  83. dwmac->interface = of_get_phy_mode(np);
  84. sys_mgr_base_addr = syscon_regmap_lookup_by_phandle(np, "altr,sysmgr-syscon");
  85. if (IS_ERR(sys_mgr_base_addr)) {
  86. dev_info(dev, "No sysmgr-syscon node found\n");
  87. return PTR_ERR(sys_mgr_base_addr);
  88. }
  89. ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, &reg_offset);
  90. if (ret) {
  91. dev_info(dev, "Could not read reg_offset from sysmgr-syscon!\n");
  92. return -EINVAL;
  93. }
  94. ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 2, &reg_shift);
  95. if (ret) {
  96. dev_info(dev, "Could not read reg_shift from sysmgr-syscon!\n");
  97. return -EINVAL;
  98. }
  99. np_splitter = of_parse_phandle(np, "altr,emac-splitter", 0);
  100. if (np_splitter) {
  101. if (of_address_to_resource(np_splitter, 0, &res_splitter)) {
  102. dev_info(dev, "Missing emac splitter address\n");
  103. return -EINVAL;
  104. }
  105. dwmac->splitter_base = devm_ioremap_resource(dev, &res_splitter);
  106. if (IS_ERR(dwmac->splitter_base)) {
  107. dev_info(dev, "Failed to mapping emac splitter\n");
  108. return PTR_ERR(dwmac->splitter_base);
  109. }
  110. }
  111. dwmac->reg_offset = reg_offset;
  112. dwmac->reg_shift = reg_shift;
  113. dwmac->sys_mgr_base_addr = sys_mgr_base_addr;
  114. dwmac->dev = dev;
  115. return 0;
  116. }
  117. static int socfpga_dwmac_setup(struct socfpga_dwmac *dwmac)
  118. {
  119. struct regmap *sys_mgr_base_addr = dwmac->sys_mgr_base_addr;
  120. int phymode = dwmac->interface;
  121. u32 reg_offset = dwmac->reg_offset;
  122. u32 reg_shift = dwmac->reg_shift;
  123. u32 ctrl, val;
  124. switch (phymode) {
  125. case PHY_INTERFACE_MODE_RGMII:
  126. case PHY_INTERFACE_MODE_RGMII_ID:
  127. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
  128. break;
  129. case PHY_INTERFACE_MODE_MII:
  130. case PHY_INTERFACE_MODE_GMII:
  131. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
  132. break;
  133. default:
  134. dev_err(dwmac->dev, "bad phy mode %d\n", phymode);
  135. return -EINVAL;
  136. }
  137. /* Overwrite val to GMII if splitter core is enabled. The phymode here
  138. * is the actual phy mode on phy hardware, but phy interface from
  139. * EMAC core is GMII.
  140. */
  141. if (dwmac->splitter_base)
  142. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
  143. regmap_read(sys_mgr_base_addr, reg_offset, &ctrl);
  144. ctrl &= ~(SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << reg_shift);
  145. ctrl |= val << reg_shift;
  146. regmap_write(sys_mgr_base_addr, reg_offset, ctrl);
  147. return 0;
  148. }
  149. static void *socfpga_dwmac_probe(struct platform_device *pdev)
  150. {
  151. struct device *dev = &pdev->dev;
  152. int ret;
  153. struct socfpga_dwmac *dwmac;
  154. dwmac = devm_kzalloc(dev, sizeof(*dwmac), GFP_KERNEL);
  155. if (!dwmac)
  156. return ERR_PTR(-ENOMEM);
  157. ret = socfpga_dwmac_parse_data(dwmac, dev);
  158. if (ret) {
  159. dev_err(dev, "Unable to parse OF data\n");
  160. return ERR_PTR(ret);
  161. }
  162. ret = socfpga_dwmac_setup(dwmac);
  163. if (ret) {
  164. dev_err(dev, "couldn't setup SoC glue (%d)\n", ret);
  165. return ERR_PTR(ret);
  166. }
  167. return dwmac;
  168. }
  169. static void socfpga_dwmac_exit(struct platform_device *pdev, void *priv)
  170. {
  171. struct socfpga_dwmac *dwmac = priv;
  172. /* On socfpga platform exit, assert and hold reset to the
  173. * enet controller - the default state after a hard reset.
  174. */
  175. if (dwmac->stmmac_rst)
  176. reset_control_assert(dwmac->stmmac_rst);
  177. }
  178. static int socfpga_dwmac_init(struct platform_device *pdev, void *priv)
  179. {
  180. struct socfpga_dwmac *dwmac = priv;
  181. struct net_device *ndev = platform_get_drvdata(pdev);
  182. struct stmmac_priv *stpriv = NULL;
  183. int ret = 0;
  184. if (ndev)
  185. stpriv = netdev_priv(ndev);
  186. /* Assert reset to the enet controller before changing the phy mode */
  187. if (dwmac->stmmac_rst)
  188. reset_control_assert(dwmac->stmmac_rst);
  189. /* Setup the phy mode in the system manager registers according to
  190. * devicetree configuration
  191. */
  192. ret = socfpga_dwmac_setup(dwmac);
  193. /* Deassert reset for the phy configuration to be sampled by
  194. * the enet controller, and operation to start in requested mode
  195. */
  196. if (dwmac->stmmac_rst)
  197. reset_control_deassert(dwmac->stmmac_rst);
  198. /* Before the enet controller is suspended, the phy is suspended.
  199. * This causes the phy clock to be gated. The enet controller is
  200. * resumed before the phy, so the clock is still gated "off" when
  201. * the enet controller is resumed. This code makes sure the phy
  202. * is "resumed" before reinitializing the enet controller since
  203. * the enet controller depends on an active phy clock to complete
  204. * a DMA reset. A DMA reset will "time out" if executed
  205. * with no phy clock input on the Synopsys enet controller.
  206. * Verified through Synopsys Case #8000711656.
  207. *
  208. * Note that the phy clock is also gated when the phy is isolated.
  209. * Phy "suspend" and "isolate" controls are located in phy basic
  210. * control register 0, and can be modified by the phy driver
  211. * framework.
  212. */
  213. if (stpriv && stpriv->phydev)
  214. phy_resume(stpriv->phydev);
  215. return ret;
  216. }
  217. const struct stmmac_of_data socfpga_gmac_data = {
  218. .setup = socfpga_dwmac_probe,
  219. .init = socfpga_dwmac_init,
  220. .exit = socfpga_dwmac_exit,
  221. .fix_mac_speed = socfpga_dwmac_fix_mac_speed,
  222. };