dwmac-socfpga.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. if (PTR_ERR(dwmac->stmmac_rst) == -EPROBE_DEFER)
  83. return -EPROBE_DEFER;
  84. dwmac->stmmac_rst = NULL;
  85. }
  86. dwmac->interface = of_get_phy_mode(np);
  87. sys_mgr_base_addr = syscon_regmap_lookup_by_phandle(np, "altr,sysmgr-syscon");
  88. if (IS_ERR(sys_mgr_base_addr)) {
  89. dev_info(dev, "No sysmgr-syscon node found\n");
  90. return PTR_ERR(sys_mgr_base_addr);
  91. }
  92. ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, &reg_offset);
  93. if (ret) {
  94. dev_info(dev, "Could not read reg_offset from sysmgr-syscon!\n");
  95. return -EINVAL;
  96. }
  97. ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 2, &reg_shift);
  98. if (ret) {
  99. dev_info(dev, "Could not read reg_shift from sysmgr-syscon!\n");
  100. return -EINVAL;
  101. }
  102. np_splitter = of_parse_phandle(np, "altr,emac-splitter", 0);
  103. if (np_splitter) {
  104. if (of_address_to_resource(np_splitter, 0, &res_splitter)) {
  105. dev_info(dev, "Missing emac splitter address\n");
  106. return -EINVAL;
  107. }
  108. dwmac->splitter_base = devm_ioremap_resource(dev, &res_splitter);
  109. if (IS_ERR(dwmac->splitter_base)) {
  110. dev_info(dev, "Failed to mapping emac splitter\n");
  111. return PTR_ERR(dwmac->splitter_base);
  112. }
  113. }
  114. dwmac->reg_offset = reg_offset;
  115. dwmac->reg_shift = reg_shift;
  116. dwmac->sys_mgr_base_addr = sys_mgr_base_addr;
  117. dwmac->dev = dev;
  118. return 0;
  119. }
  120. static int socfpga_dwmac_setup(struct socfpga_dwmac *dwmac)
  121. {
  122. struct regmap *sys_mgr_base_addr = dwmac->sys_mgr_base_addr;
  123. int phymode = dwmac->interface;
  124. u32 reg_offset = dwmac->reg_offset;
  125. u32 reg_shift = dwmac->reg_shift;
  126. u32 ctrl, val;
  127. switch (phymode) {
  128. case PHY_INTERFACE_MODE_RGMII:
  129. case PHY_INTERFACE_MODE_RGMII_ID:
  130. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
  131. break;
  132. case PHY_INTERFACE_MODE_MII:
  133. case PHY_INTERFACE_MODE_GMII:
  134. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
  135. break;
  136. default:
  137. dev_err(dwmac->dev, "bad phy mode %d\n", phymode);
  138. return -EINVAL;
  139. }
  140. /* Overwrite val to GMII if splitter core is enabled. The phymode here
  141. * is the actual phy mode on phy hardware, but phy interface from
  142. * EMAC core is GMII.
  143. */
  144. if (dwmac->splitter_base)
  145. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
  146. regmap_read(sys_mgr_base_addr, reg_offset, &ctrl);
  147. ctrl &= ~(SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << reg_shift);
  148. ctrl |= val << reg_shift;
  149. regmap_write(sys_mgr_base_addr, reg_offset, ctrl);
  150. return 0;
  151. }
  152. static void socfpga_dwmac_exit(struct platform_device *pdev, void *priv)
  153. {
  154. struct socfpga_dwmac *dwmac = priv;
  155. /* On socfpga platform exit, assert and hold reset to the
  156. * enet controller - the default state after a hard reset.
  157. */
  158. if (dwmac->stmmac_rst)
  159. reset_control_assert(dwmac->stmmac_rst);
  160. }
  161. static int socfpga_dwmac_init(struct platform_device *pdev, void *priv)
  162. {
  163. struct socfpga_dwmac *dwmac = priv;
  164. struct net_device *ndev = platform_get_drvdata(pdev);
  165. struct stmmac_priv *stpriv = NULL;
  166. int ret = 0;
  167. if (ndev)
  168. stpriv = netdev_priv(ndev);
  169. /* Assert reset to the enet controller before changing the phy mode */
  170. if (dwmac->stmmac_rst)
  171. reset_control_assert(dwmac->stmmac_rst);
  172. /* Setup the phy mode in the system manager registers according to
  173. * devicetree configuration
  174. */
  175. ret = socfpga_dwmac_setup(dwmac);
  176. /* Deassert reset for the phy configuration to be sampled by
  177. * the enet controller, and operation to start in requested mode
  178. */
  179. if (dwmac->stmmac_rst)
  180. reset_control_deassert(dwmac->stmmac_rst);
  181. /* Before the enet controller is suspended, the phy is suspended.
  182. * This causes the phy clock to be gated. The enet controller is
  183. * resumed before the phy, so the clock is still gated "off" when
  184. * the enet controller is resumed. This code makes sure the phy
  185. * is "resumed" before reinitializing the enet controller since
  186. * the enet controller depends on an active phy clock to complete
  187. * a DMA reset. A DMA reset will "time out" if executed
  188. * with no phy clock input on the Synopsys enet controller.
  189. * Verified through Synopsys Case #8000711656.
  190. *
  191. * Note that the phy clock is also gated when the phy is isolated.
  192. * Phy "suspend" and "isolate" controls are located in phy basic
  193. * control register 0, and can be modified by the phy driver
  194. * framework.
  195. */
  196. if (stpriv && stpriv->phydev)
  197. phy_resume(stpriv->phydev);
  198. return ret;
  199. }
  200. static int socfpga_dwmac_probe(struct platform_device *pdev)
  201. {
  202. struct plat_stmmacenet_data *plat_dat;
  203. struct stmmac_resources stmmac_res;
  204. struct device *dev = &pdev->dev;
  205. int ret;
  206. struct socfpga_dwmac *dwmac;
  207. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  208. if (ret)
  209. return ret;
  210. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  211. if (IS_ERR(plat_dat))
  212. return PTR_ERR(plat_dat);
  213. dwmac = devm_kzalloc(dev, sizeof(*dwmac), GFP_KERNEL);
  214. if (!dwmac)
  215. return -ENOMEM;
  216. ret = socfpga_dwmac_parse_data(dwmac, dev);
  217. if (ret) {
  218. dev_err(dev, "Unable to parse OF data\n");
  219. return ret;
  220. }
  221. ret = socfpga_dwmac_setup(dwmac);
  222. if (ret) {
  223. dev_err(dev, "couldn't setup SoC glue (%d)\n", ret);
  224. return ret;
  225. }
  226. plat_dat->bsp_priv = dwmac;
  227. plat_dat->init = socfpga_dwmac_init;
  228. plat_dat->exit = socfpga_dwmac_exit;
  229. plat_dat->fix_mac_speed = socfpga_dwmac_fix_mac_speed;
  230. ret = socfpga_dwmac_init(pdev, plat_dat->bsp_priv);
  231. if (ret)
  232. return ret;
  233. return stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  234. }
  235. static const struct of_device_id socfpga_dwmac_match[] = {
  236. { .compatible = "altr,socfpga-stmmac" },
  237. { }
  238. };
  239. MODULE_DEVICE_TABLE(of, socfpga_dwmac_match);
  240. static struct platform_driver socfpga_dwmac_driver = {
  241. .probe = socfpga_dwmac_probe,
  242. .remove = stmmac_pltfr_remove,
  243. .driver = {
  244. .name = "socfpga-dwmac",
  245. .pm = &stmmac_pltfr_pm_ops,
  246. .of_match_table = socfpga_dwmac_match,
  247. },
  248. };
  249. module_platform_driver(socfpga_dwmac_driver);
  250. MODULE_LICENSE("GPL v2");