dwmac-socfpga.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #include "altr_tse_pcs.h"
  28. #define SGMII_ADAPTER_CTRL_REG 0x00
  29. #define SGMII_ADAPTER_DISABLE 0x0001
  30. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII 0x0
  31. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII 0x1
  32. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII 0x2
  33. #define SYSMGR_EMACGRP_CTRL_PHYSEL_WIDTH 2
  34. #define SYSMGR_EMACGRP_CTRL_PHYSEL_MASK 0x00000003
  35. #define SYSMGR_EMACGRP_CTRL_PTP_REF_CLK_MASK 0x00000010
  36. #define SYSMGR_FPGAGRP_MODULE_REG 0x00000028
  37. #define SYSMGR_FPGAGRP_MODULE_EMAC 0x00000004
  38. #define EMAC_SPLITTER_CTRL_REG 0x0
  39. #define EMAC_SPLITTER_CTRL_SPEED_MASK 0x3
  40. #define EMAC_SPLITTER_CTRL_SPEED_10 0x2
  41. #define EMAC_SPLITTER_CTRL_SPEED_100 0x3
  42. #define EMAC_SPLITTER_CTRL_SPEED_1000 0x0
  43. struct socfpga_dwmac {
  44. int interface;
  45. u32 reg_offset;
  46. u32 reg_shift;
  47. struct device *dev;
  48. struct regmap *sys_mgr_base_addr;
  49. struct reset_control *stmmac_rst;
  50. struct reset_control *stmmac_ocp_rst;
  51. void __iomem *splitter_base;
  52. bool f2h_ptp_ref_clk;
  53. struct tse_pcs pcs;
  54. };
  55. static void socfpga_dwmac_fix_mac_speed(void *priv, unsigned int speed)
  56. {
  57. struct socfpga_dwmac *dwmac = (struct socfpga_dwmac *)priv;
  58. void __iomem *splitter_base = dwmac->splitter_base;
  59. void __iomem *tse_pcs_base = dwmac->pcs.tse_pcs_base;
  60. void __iomem *sgmii_adapter_base = dwmac->pcs.sgmii_adapter_base;
  61. struct device *dev = dwmac->dev;
  62. struct net_device *ndev = dev_get_drvdata(dev);
  63. struct phy_device *phy_dev = ndev->phydev;
  64. u32 val;
  65. if ((tse_pcs_base) && (sgmii_adapter_base))
  66. writew(SGMII_ADAPTER_DISABLE,
  67. sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
  68. if (splitter_base) {
  69. val = readl(splitter_base + EMAC_SPLITTER_CTRL_REG);
  70. val &= ~EMAC_SPLITTER_CTRL_SPEED_MASK;
  71. switch (speed) {
  72. case 1000:
  73. val |= EMAC_SPLITTER_CTRL_SPEED_1000;
  74. break;
  75. case 100:
  76. val |= EMAC_SPLITTER_CTRL_SPEED_100;
  77. break;
  78. case 10:
  79. val |= EMAC_SPLITTER_CTRL_SPEED_10;
  80. break;
  81. default:
  82. return;
  83. }
  84. writel(val, splitter_base + EMAC_SPLITTER_CTRL_REG);
  85. }
  86. if (tse_pcs_base && sgmii_adapter_base)
  87. tse_pcs_fix_mac_speed(&dwmac->pcs, phy_dev, speed);
  88. }
  89. static int socfpga_dwmac_parse_data(struct socfpga_dwmac *dwmac, struct device *dev)
  90. {
  91. struct device_node *np = dev->of_node;
  92. struct regmap *sys_mgr_base_addr;
  93. u32 reg_offset, reg_shift;
  94. int ret, index;
  95. struct device_node *np_splitter = NULL;
  96. struct device_node *np_sgmii_adapter = NULL;
  97. struct resource res_splitter;
  98. struct resource res_tse_pcs;
  99. struct resource res_sgmii_adapter;
  100. dwmac->interface = of_get_phy_mode(np);
  101. sys_mgr_base_addr = syscon_regmap_lookup_by_phandle(np, "altr,sysmgr-syscon");
  102. if (IS_ERR(sys_mgr_base_addr)) {
  103. dev_info(dev, "No sysmgr-syscon node found\n");
  104. return PTR_ERR(sys_mgr_base_addr);
  105. }
  106. ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, &reg_offset);
  107. if (ret) {
  108. dev_info(dev, "Could not read reg_offset from sysmgr-syscon!\n");
  109. return -EINVAL;
  110. }
  111. ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 2, &reg_shift);
  112. if (ret) {
  113. dev_info(dev, "Could not read reg_shift from sysmgr-syscon!\n");
  114. return -EINVAL;
  115. }
  116. dwmac->f2h_ptp_ref_clk = of_property_read_bool(np, "altr,f2h_ptp_ref_clk");
  117. np_splitter = of_parse_phandle(np, "altr,emac-splitter", 0);
  118. if (np_splitter) {
  119. ret = of_address_to_resource(np_splitter, 0, &res_splitter);
  120. of_node_put(np_splitter);
  121. if (ret) {
  122. dev_info(dev, "Missing emac splitter address\n");
  123. return -EINVAL;
  124. }
  125. dwmac->splitter_base = devm_ioremap_resource(dev, &res_splitter);
  126. if (IS_ERR(dwmac->splitter_base)) {
  127. dev_info(dev, "Failed to mapping emac splitter\n");
  128. return PTR_ERR(dwmac->splitter_base);
  129. }
  130. }
  131. np_sgmii_adapter = of_parse_phandle(np,
  132. "altr,gmii-to-sgmii-converter", 0);
  133. if (np_sgmii_adapter) {
  134. index = of_property_match_string(np_sgmii_adapter, "reg-names",
  135. "hps_emac_interface_splitter_avalon_slave");
  136. if (index >= 0) {
  137. if (of_address_to_resource(np_sgmii_adapter, index,
  138. &res_splitter)) {
  139. dev_err(dev,
  140. "%s: ERROR: missing emac splitter address\n",
  141. __func__);
  142. ret = -EINVAL;
  143. goto err_node_put;
  144. }
  145. dwmac->splitter_base =
  146. devm_ioremap_resource(dev, &res_splitter);
  147. if (IS_ERR(dwmac->splitter_base)) {
  148. ret = PTR_ERR(dwmac->splitter_base);
  149. goto err_node_put;
  150. }
  151. }
  152. index = of_property_match_string(np_sgmii_adapter, "reg-names",
  153. "gmii_to_sgmii_adapter_avalon_slave");
  154. if (index >= 0) {
  155. if (of_address_to_resource(np_sgmii_adapter, index,
  156. &res_sgmii_adapter)) {
  157. dev_err(dev,
  158. "%s: ERROR: failed mapping adapter\n",
  159. __func__);
  160. ret = -EINVAL;
  161. goto err_node_put;
  162. }
  163. dwmac->pcs.sgmii_adapter_base =
  164. devm_ioremap_resource(dev, &res_sgmii_adapter);
  165. if (IS_ERR(dwmac->pcs.sgmii_adapter_base)) {
  166. ret = PTR_ERR(dwmac->pcs.sgmii_adapter_base);
  167. goto err_node_put;
  168. }
  169. }
  170. index = of_property_match_string(np_sgmii_adapter, "reg-names",
  171. "eth_tse_control_port");
  172. if (index >= 0) {
  173. if (of_address_to_resource(np_sgmii_adapter, index,
  174. &res_tse_pcs)) {
  175. dev_err(dev,
  176. "%s: ERROR: failed mapping tse control port\n",
  177. __func__);
  178. ret = -EINVAL;
  179. goto err_node_put;
  180. }
  181. dwmac->pcs.tse_pcs_base =
  182. devm_ioremap_resource(dev, &res_tse_pcs);
  183. if (IS_ERR(dwmac->pcs.tse_pcs_base)) {
  184. ret = PTR_ERR(dwmac->pcs.tse_pcs_base);
  185. goto err_node_put;
  186. }
  187. }
  188. }
  189. dwmac->reg_offset = reg_offset;
  190. dwmac->reg_shift = reg_shift;
  191. dwmac->sys_mgr_base_addr = sys_mgr_base_addr;
  192. dwmac->dev = dev;
  193. of_node_put(np_sgmii_adapter);
  194. return 0;
  195. err_node_put:
  196. of_node_put(np_sgmii_adapter);
  197. return ret;
  198. }
  199. static int socfpga_dwmac_set_phy_mode(struct socfpga_dwmac *dwmac)
  200. {
  201. struct regmap *sys_mgr_base_addr = dwmac->sys_mgr_base_addr;
  202. int phymode = dwmac->interface;
  203. u32 reg_offset = dwmac->reg_offset;
  204. u32 reg_shift = dwmac->reg_shift;
  205. u32 ctrl, val, module;
  206. switch (phymode) {
  207. case PHY_INTERFACE_MODE_RGMII:
  208. case PHY_INTERFACE_MODE_RGMII_ID:
  209. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
  210. break;
  211. case PHY_INTERFACE_MODE_MII:
  212. case PHY_INTERFACE_MODE_GMII:
  213. case PHY_INTERFACE_MODE_SGMII:
  214. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
  215. break;
  216. default:
  217. dev_err(dwmac->dev, "bad phy mode %d\n", phymode);
  218. return -EINVAL;
  219. }
  220. /* Overwrite val to GMII if splitter core is enabled. The phymode here
  221. * is the actual phy mode on phy hardware, but phy interface from
  222. * EMAC core is GMII.
  223. */
  224. if (dwmac->splitter_base)
  225. val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
  226. /* Assert reset to the enet controller before changing the phy mode */
  227. reset_control_assert(dwmac->stmmac_ocp_rst);
  228. reset_control_assert(dwmac->stmmac_rst);
  229. regmap_read(sys_mgr_base_addr, reg_offset, &ctrl);
  230. ctrl &= ~(SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << reg_shift);
  231. ctrl |= val << reg_shift;
  232. if (dwmac->f2h_ptp_ref_clk ||
  233. phymode == PHY_INTERFACE_MODE_MII ||
  234. phymode == PHY_INTERFACE_MODE_GMII ||
  235. phymode == PHY_INTERFACE_MODE_SGMII) {
  236. ctrl |= SYSMGR_EMACGRP_CTRL_PTP_REF_CLK_MASK << (reg_shift / 2);
  237. regmap_read(sys_mgr_base_addr, SYSMGR_FPGAGRP_MODULE_REG,
  238. &module);
  239. module |= (SYSMGR_FPGAGRP_MODULE_EMAC << (reg_shift / 2));
  240. regmap_write(sys_mgr_base_addr, SYSMGR_FPGAGRP_MODULE_REG,
  241. module);
  242. } else {
  243. ctrl &= ~(SYSMGR_EMACGRP_CTRL_PTP_REF_CLK_MASK << (reg_shift / 2));
  244. }
  245. regmap_write(sys_mgr_base_addr, reg_offset, ctrl);
  246. /* Deassert reset for the phy configuration to be sampled by
  247. * the enet controller, and operation to start in requested mode
  248. */
  249. reset_control_deassert(dwmac->stmmac_ocp_rst);
  250. reset_control_deassert(dwmac->stmmac_rst);
  251. if (phymode == PHY_INTERFACE_MODE_SGMII) {
  252. if (tse_pcs_init(dwmac->pcs.tse_pcs_base, &dwmac->pcs) != 0) {
  253. dev_err(dwmac->dev, "Unable to initialize TSE PCS");
  254. return -EINVAL;
  255. }
  256. }
  257. return 0;
  258. }
  259. static int socfpga_dwmac_probe(struct platform_device *pdev)
  260. {
  261. struct plat_stmmacenet_data *plat_dat;
  262. struct stmmac_resources stmmac_res;
  263. struct device *dev = &pdev->dev;
  264. int ret;
  265. struct socfpga_dwmac *dwmac;
  266. struct net_device *ndev;
  267. struct stmmac_priv *stpriv;
  268. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  269. if (ret)
  270. return ret;
  271. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  272. if (IS_ERR(plat_dat))
  273. return PTR_ERR(plat_dat);
  274. dwmac = devm_kzalloc(dev, sizeof(*dwmac), GFP_KERNEL);
  275. if (!dwmac) {
  276. ret = -ENOMEM;
  277. goto err_remove_config_dt;
  278. }
  279. dwmac->stmmac_ocp_rst = devm_reset_control_get_optional(dev, "stmmaceth-ocp");
  280. if (IS_ERR(dwmac->stmmac_ocp_rst)) {
  281. ret = PTR_ERR(dwmac->stmmac_ocp_rst);
  282. dev_err(dev, "error getting reset control of ocp %d\n", ret);
  283. goto err_remove_config_dt;
  284. }
  285. reset_control_deassert(dwmac->stmmac_ocp_rst);
  286. ret = socfpga_dwmac_parse_data(dwmac, dev);
  287. if (ret) {
  288. dev_err(dev, "Unable to parse OF data\n");
  289. goto err_remove_config_dt;
  290. }
  291. plat_dat->bsp_priv = dwmac;
  292. plat_dat->fix_mac_speed = socfpga_dwmac_fix_mac_speed;
  293. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  294. if (ret)
  295. goto err_remove_config_dt;
  296. ndev = platform_get_drvdata(pdev);
  297. stpriv = netdev_priv(ndev);
  298. /* The socfpga driver needs to control the stmmac reset to set the phy
  299. * mode. Create a copy of the core reset handle so it can be used by
  300. * the driver later.
  301. */
  302. dwmac->stmmac_rst = stpriv->plat->stmmac_rst;
  303. ret = socfpga_dwmac_set_phy_mode(dwmac);
  304. if (ret)
  305. goto err_dvr_remove;
  306. return 0;
  307. err_dvr_remove:
  308. stmmac_dvr_remove(&pdev->dev);
  309. err_remove_config_dt:
  310. stmmac_remove_config_dt(pdev, plat_dat);
  311. return ret;
  312. }
  313. #ifdef CONFIG_PM_SLEEP
  314. static int socfpga_dwmac_resume(struct device *dev)
  315. {
  316. struct net_device *ndev = dev_get_drvdata(dev);
  317. struct stmmac_priv *priv = netdev_priv(ndev);
  318. socfpga_dwmac_set_phy_mode(priv->plat->bsp_priv);
  319. /* Before the enet controller is suspended, the phy is suspended.
  320. * This causes the phy clock to be gated. The enet controller is
  321. * resumed before the phy, so the clock is still gated "off" when
  322. * the enet controller is resumed. This code makes sure the phy
  323. * is "resumed" before reinitializing the enet controller since
  324. * the enet controller depends on an active phy clock to complete
  325. * a DMA reset. A DMA reset will "time out" if executed
  326. * with no phy clock input on the Synopsys enet controller.
  327. * Verified through Synopsys Case #8000711656.
  328. *
  329. * Note that the phy clock is also gated when the phy is isolated.
  330. * Phy "suspend" and "isolate" controls are located in phy basic
  331. * control register 0, and can be modified by the phy driver
  332. * framework.
  333. */
  334. if (ndev->phydev)
  335. phy_resume(ndev->phydev);
  336. return stmmac_resume(dev);
  337. }
  338. #endif /* CONFIG_PM_SLEEP */
  339. static SIMPLE_DEV_PM_OPS(socfpga_dwmac_pm_ops, stmmac_suspend,
  340. socfpga_dwmac_resume);
  341. static const struct of_device_id socfpga_dwmac_match[] = {
  342. { .compatible = "altr,socfpga-stmmac" },
  343. { }
  344. };
  345. MODULE_DEVICE_TABLE(of, socfpga_dwmac_match);
  346. static struct platform_driver socfpga_dwmac_driver = {
  347. .probe = socfpga_dwmac_probe,
  348. .remove = stmmac_pltfr_remove,
  349. .driver = {
  350. .name = "socfpga-dwmac",
  351. .pm = &socfpga_dwmac_pm_ops,
  352. .of_match_table = socfpga_dwmac_match,
  353. },
  354. };
  355. module_platform_driver(socfpga_dwmac_driver);
  356. MODULE_LICENSE("GPL v2");