dwmac-socfpga.c 7.3 KB

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