dwmac-stm32.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * dwmac-stm32.c - DWMAC Specific Glue layer for STM32 MCU
  4. *
  5. * Copyright (C) STMicroelectronics SA 2017
  6. * Author: Alexandre Torgue <[email protected]> for STMicroelectronics.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/of_net.h>
  15. #include <linux/phy.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_wakeirq.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/stmmac.h>
  21. #include "stmmac_platform.h"
  22. #define SYSCFG_MCU_ETH_MASK BIT(23)
  23. #define SYSCFG_MP1_ETH_MASK GENMASK(23, 16)
  24. #define SYSCFG_PMCCLRR_OFFSET 0x40
  25. #define SYSCFG_PMCR_ETH_CLK_SEL BIT(16)
  26. #define SYSCFG_PMCR_ETH_REF_CLK_SEL BIT(17)
  27. /* CLOCK feed to PHY*/
  28. #define ETH_CK_F_25M 25000000
  29. #define ETH_CK_F_50M 50000000
  30. #define ETH_CK_F_125M 125000000
  31. /* Ethernet PHY interface selection in register SYSCFG Configuration
  32. *------------------------------------------
  33. * src |BIT(23)| BIT(22)| BIT(21)|BIT(20)|
  34. *------------------------------------------
  35. * MII | 0 | 0 | 0 | 1 |
  36. *------------------------------------------
  37. * GMII | 0 | 0 | 0 | 0 |
  38. *------------------------------------------
  39. * RGMII | 0 | 0 | 1 | n/a |
  40. *------------------------------------------
  41. * RMII | 1 | 0 | 0 | n/a |
  42. *------------------------------------------
  43. */
  44. #define SYSCFG_PMCR_ETH_SEL_MII BIT(20)
  45. #define SYSCFG_PMCR_ETH_SEL_RGMII BIT(21)
  46. #define SYSCFG_PMCR_ETH_SEL_RMII BIT(23)
  47. #define SYSCFG_PMCR_ETH_SEL_GMII 0
  48. #define SYSCFG_MCU_ETH_SEL_MII 0
  49. #define SYSCFG_MCU_ETH_SEL_RMII 1
  50. /* STM32MP1 register definitions
  51. *
  52. * Below table summarizes the clock requirement and clock sources for
  53. * supported phy interface modes.
  54. * __________________________________________________________________________
  55. *|PHY_MODE | Normal | PHY wo crystal| PHY wo crystal |No 125Mhz from PHY|
  56. *| | | 25MHz | 50MHz | |
  57. * ---------------------------------------------------------------------------
  58. *| MII | - | eth-ck | n/a | n/a |
  59. *| | | st,ext-phyclk | | |
  60. * ---------------------------------------------------------------------------
  61. *| GMII | - | eth-ck | n/a | n/a |
  62. *| | | st,ext-phyclk | | |
  63. * ---------------------------------------------------------------------------
  64. *| RGMII | - | eth-ck | n/a | eth-ck |
  65. *| | | st,ext-phyclk | | st,eth-clk-sel or|
  66. *| | | | | st,ext-phyclk |
  67. * ---------------------------------------------------------------------------
  68. *| RMII | - | eth-ck | eth-ck | n/a |
  69. *| | | st,ext-phyclk | st,eth-ref-clk-sel | |
  70. *| | | | or st,ext-phyclk | |
  71. * ---------------------------------------------------------------------------
  72. *
  73. */
  74. struct stm32_dwmac {
  75. struct clk *clk_tx;
  76. struct clk *clk_rx;
  77. struct clk *clk_eth_ck;
  78. struct clk *clk_ethstp;
  79. struct clk *syscfg_clk;
  80. int ext_phyclk;
  81. int enable_eth_ck;
  82. int eth_clk_sel_reg;
  83. int eth_ref_clk_sel_reg;
  84. int irq_pwr_wakeup;
  85. u32 mode_reg; /* MAC glue-logic mode register */
  86. struct regmap *regmap;
  87. u32 speed;
  88. const struct stm32_ops *ops;
  89. struct device *dev;
  90. };
  91. struct stm32_ops {
  92. int (*set_mode)(struct plat_stmmacenet_data *plat_dat);
  93. int (*clk_prepare)(struct stm32_dwmac *dwmac, bool prepare);
  94. int (*suspend)(struct stm32_dwmac *dwmac);
  95. void (*resume)(struct stm32_dwmac *dwmac);
  96. int (*parse_data)(struct stm32_dwmac *dwmac,
  97. struct device *dev);
  98. u32 syscfg_eth_mask;
  99. bool clk_rx_enable_in_suspend;
  100. };
  101. static int stm32_dwmac_init(struct plat_stmmacenet_data *plat_dat)
  102. {
  103. struct stm32_dwmac *dwmac = plat_dat->bsp_priv;
  104. int ret;
  105. if (dwmac->ops->set_mode) {
  106. ret = dwmac->ops->set_mode(plat_dat);
  107. if (ret)
  108. return ret;
  109. }
  110. ret = clk_prepare_enable(dwmac->clk_tx);
  111. if (ret)
  112. return ret;
  113. if (!dwmac->ops->clk_rx_enable_in_suspend ||
  114. !dwmac->dev->power.is_suspended) {
  115. ret = clk_prepare_enable(dwmac->clk_rx);
  116. if (ret) {
  117. clk_disable_unprepare(dwmac->clk_tx);
  118. return ret;
  119. }
  120. }
  121. if (dwmac->ops->clk_prepare) {
  122. ret = dwmac->ops->clk_prepare(dwmac, true);
  123. if (ret) {
  124. clk_disable_unprepare(dwmac->clk_rx);
  125. clk_disable_unprepare(dwmac->clk_tx);
  126. }
  127. }
  128. return ret;
  129. }
  130. static int stm32mp1_clk_prepare(struct stm32_dwmac *dwmac, bool prepare)
  131. {
  132. int ret = 0;
  133. if (prepare) {
  134. ret = clk_prepare_enable(dwmac->syscfg_clk);
  135. if (ret)
  136. return ret;
  137. if (dwmac->enable_eth_ck) {
  138. ret = clk_prepare_enable(dwmac->clk_eth_ck);
  139. if (ret) {
  140. clk_disable_unprepare(dwmac->syscfg_clk);
  141. return ret;
  142. }
  143. }
  144. } else {
  145. clk_disable_unprepare(dwmac->syscfg_clk);
  146. if (dwmac->enable_eth_ck)
  147. clk_disable_unprepare(dwmac->clk_eth_ck);
  148. }
  149. return ret;
  150. }
  151. static int stm32mp1_set_mode(struct plat_stmmacenet_data *plat_dat)
  152. {
  153. struct stm32_dwmac *dwmac = plat_dat->bsp_priv;
  154. u32 reg = dwmac->mode_reg, clk_rate;
  155. int val;
  156. clk_rate = clk_get_rate(dwmac->clk_eth_ck);
  157. dwmac->enable_eth_ck = false;
  158. switch (plat_dat->interface) {
  159. case PHY_INTERFACE_MODE_MII:
  160. if (clk_rate == ETH_CK_F_25M && dwmac->ext_phyclk)
  161. dwmac->enable_eth_ck = true;
  162. val = SYSCFG_PMCR_ETH_SEL_MII;
  163. pr_debug("SYSCFG init : PHY_INTERFACE_MODE_MII\n");
  164. break;
  165. case PHY_INTERFACE_MODE_GMII:
  166. val = SYSCFG_PMCR_ETH_SEL_GMII;
  167. if (clk_rate == ETH_CK_F_25M &&
  168. (dwmac->eth_clk_sel_reg || dwmac->ext_phyclk)) {
  169. dwmac->enable_eth_ck = true;
  170. val |= SYSCFG_PMCR_ETH_CLK_SEL;
  171. }
  172. pr_debug("SYSCFG init : PHY_INTERFACE_MODE_GMII\n");
  173. break;
  174. case PHY_INTERFACE_MODE_RMII:
  175. val = SYSCFG_PMCR_ETH_SEL_RMII;
  176. if ((clk_rate == ETH_CK_F_25M || clk_rate == ETH_CK_F_50M) &&
  177. (dwmac->eth_ref_clk_sel_reg || dwmac->ext_phyclk)) {
  178. dwmac->enable_eth_ck = true;
  179. val |= SYSCFG_PMCR_ETH_REF_CLK_SEL;
  180. }
  181. pr_debug("SYSCFG init : PHY_INTERFACE_MODE_RMII\n");
  182. break;
  183. case PHY_INTERFACE_MODE_RGMII:
  184. case PHY_INTERFACE_MODE_RGMII_ID:
  185. case PHY_INTERFACE_MODE_RGMII_RXID:
  186. case PHY_INTERFACE_MODE_RGMII_TXID:
  187. val = SYSCFG_PMCR_ETH_SEL_RGMII;
  188. if ((clk_rate == ETH_CK_F_25M || clk_rate == ETH_CK_F_125M) &&
  189. (dwmac->eth_clk_sel_reg || dwmac->ext_phyclk)) {
  190. dwmac->enable_eth_ck = true;
  191. val |= SYSCFG_PMCR_ETH_CLK_SEL;
  192. }
  193. pr_debug("SYSCFG init : PHY_INTERFACE_MODE_RGMII\n");
  194. break;
  195. default:
  196. pr_debug("SYSCFG init : Do not manage %d interface\n",
  197. plat_dat->interface);
  198. /* Do not manage others interfaces */
  199. return -EINVAL;
  200. }
  201. /* Need to update PMCCLRR (clear register) */
  202. regmap_write(dwmac->regmap, reg + SYSCFG_PMCCLRR_OFFSET,
  203. dwmac->ops->syscfg_eth_mask);
  204. /* Update PMCSETR (set register) */
  205. return regmap_update_bits(dwmac->regmap, reg,
  206. dwmac->ops->syscfg_eth_mask, val);
  207. }
  208. static int stm32mcu_set_mode(struct plat_stmmacenet_data *plat_dat)
  209. {
  210. struct stm32_dwmac *dwmac = plat_dat->bsp_priv;
  211. u32 reg = dwmac->mode_reg;
  212. int val;
  213. switch (plat_dat->interface) {
  214. case PHY_INTERFACE_MODE_MII:
  215. val = SYSCFG_MCU_ETH_SEL_MII;
  216. pr_debug("SYSCFG init : PHY_INTERFACE_MODE_MII\n");
  217. break;
  218. case PHY_INTERFACE_MODE_RMII:
  219. val = SYSCFG_MCU_ETH_SEL_RMII;
  220. pr_debug("SYSCFG init : PHY_INTERFACE_MODE_RMII\n");
  221. break;
  222. default:
  223. pr_debug("SYSCFG init : Do not manage %d interface\n",
  224. plat_dat->interface);
  225. /* Do not manage others interfaces */
  226. return -EINVAL;
  227. }
  228. return regmap_update_bits(dwmac->regmap, reg,
  229. dwmac->ops->syscfg_eth_mask, val << 23);
  230. }
  231. static void stm32_dwmac_clk_disable(struct stm32_dwmac *dwmac)
  232. {
  233. clk_disable_unprepare(dwmac->clk_tx);
  234. clk_disable_unprepare(dwmac->clk_rx);
  235. if (dwmac->ops->clk_prepare)
  236. dwmac->ops->clk_prepare(dwmac, false);
  237. }
  238. static int stm32_dwmac_parse_data(struct stm32_dwmac *dwmac,
  239. struct device *dev)
  240. {
  241. struct device_node *np = dev->of_node;
  242. int err;
  243. /* Get TX/RX clocks */
  244. dwmac->clk_tx = devm_clk_get(dev, "mac-clk-tx");
  245. if (IS_ERR(dwmac->clk_tx)) {
  246. dev_err(dev, "No ETH Tx clock provided...\n");
  247. return PTR_ERR(dwmac->clk_tx);
  248. }
  249. dwmac->clk_rx = devm_clk_get(dev, "mac-clk-rx");
  250. if (IS_ERR(dwmac->clk_rx)) {
  251. dev_err(dev, "No ETH Rx clock provided...\n");
  252. return PTR_ERR(dwmac->clk_rx);
  253. }
  254. if (dwmac->ops->parse_data) {
  255. err = dwmac->ops->parse_data(dwmac, dev);
  256. if (err)
  257. return err;
  258. }
  259. /* Get mode register */
  260. dwmac->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscon");
  261. if (IS_ERR(dwmac->regmap))
  262. return PTR_ERR(dwmac->regmap);
  263. err = of_property_read_u32_index(np, "st,syscon", 1, &dwmac->mode_reg);
  264. if (err)
  265. dev_err(dev, "Can't get sysconfig mode offset (%d)\n", err);
  266. return err;
  267. }
  268. static int stm32mp1_parse_data(struct stm32_dwmac *dwmac,
  269. struct device *dev)
  270. {
  271. struct platform_device *pdev = to_platform_device(dev);
  272. struct device_node *np = dev->of_node;
  273. int err = 0;
  274. /* Ethernet PHY have no crystal */
  275. dwmac->ext_phyclk = of_property_read_bool(np, "st,ext-phyclk");
  276. /* Gigabit Ethernet 125MHz clock selection. */
  277. dwmac->eth_clk_sel_reg = of_property_read_bool(np, "st,eth-clk-sel");
  278. /* Ethernet 50Mhz RMII clock selection */
  279. dwmac->eth_ref_clk_sel_reg =
  280. of_property_read_bool(np, "st,eth-ref-clk-sel");
  281. /* Get ETH_CLK clocks */
  282. dwmac->clk_eth_ck = devm_clk_get(dev, "eth-ck");
  283. if (IS_ERR(dwmac->clk_eth_ck)) {
  284. dev_info(dev, "No phy clock provided...\n");
  285. dwmac->clk_eth_ck = NULL;
  286. }
  287. /* Clock used for low power mode */
  288. dwmac->clk_ethstp = devm_clk_get(dev, "ethstp");
  289. if (IS_ERR(dwmac->clk_ethstp)) {
  290. dev_err(dev,
  291. "No ETH peripheral clock provided for CStop mode ...\n");
  292. return PTR_ERR(dwmac->clk_ethstp);
  293. }
  294. /* Optional Clock for sysconfig */
  295. dwmac->syscfg_clk = devm_clk_get(dev, "syscfg-clk");
  296. if (IS_ERR(dwmac->syscfg_clk))
  297. dwmac->syscfg_clk = NULL;
  298. /* Get IRQ information early to have an ability to ask for deferred
  299. * probe if needed before we went too far with resource allocation.
  300. */
  301. dwmac->irq_pwr_wakeup = platform_get_irq_byname_optional(pdev,
  302. "stm32_pwr_wakeup");
  303. if (dwmac->irq_pwr_wakeup == -EPROBE_DEFER)
  304. return -EPROBE_DEFER;
  305. if (!dwmac->clk_eth_ck && dwmac->irq_pwr_wakeup >= 0) {
  306. err = device_init_wakeup(&pdev->dev, true);
  307. if (err) {
  308. dev_err(&pdev->dev, "Failed to init wake up irq\n");
  309. return err;
  310. }
  311. err = dev_pm_set_dedicated_wake_irq(&pdev->dev,
  312. dwmac->irq_pwr_wakeup);
  313. if (err) {
  314. dev_err(&pdev->dev, "Failed to set wake up irq\n");
  315. device_init_wakeup(&pdev->dev, false);
  316. }
  317. device_set_wakeup_enable(&pdev->dev, false);
  318. }
  319. return err;
  320. }
  321. static int stm32_dwmac_probe(struct platform_device *pdev)
  322. {
  323. struct plat_stmmacenet_data *plat_dat;
  324. struct stmmac_resources stmmac_res;
  325. struct stm32_dwmac *dwmac;
  326. const struct stm32_ops *data;
  327. int ret;
  328. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  329. if (ret)
  330. return ret;
  331. plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
  332. if (IS_ERR(plat_dat))
  333. return PTR_ERR(plat_dat);
  334. dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
  335. if (!dwmac) {
  336. ret = -ENOMEM;
  337. goto err_remove_config_dt;
  338. }
  339. data = of_device_get_match_data(&pdev->dev);
  340. if (!data) {
  341. dev_err(&pdev->dev, "no of match data provided\n");
  342. ret = -EINVAL;
  343. goto err_remove_config_dt;
  344. }
  345. dwmac->ops = data;
  346. dwmac->dev = &pdev->dev;
  347. ret = stm32_dwmac_parse_data(dwmac, &pdev->dev);
  348. if (ret) {
  349. dev_err(&pdev->dev, "Unable to parse OF data\n");
  350. goto err_remove_config_dt;
  351. }
  352. plat_dat->bsp_priv = dwmac;
  353. ret = stm32_dwmac_init(plat_dat);
  354. if (ret)
  355. goto err_remove_config_dt;
  356. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  357. if (ret)
  358. goto err_clk_disable;
  359. return 0;
  360. err_clk_disable:
  361. stm32_dwmac_clk_disable(dwmac);
  362. err_remove_config_dt:
  363. stmmac_remove_config_dt(pdev, plat_dat);
  364. return ret;
  365. }
  366. static int stm32_dwmac_remove(struct platform_device *pdev)
  367. {
  368. struct net_device *ndev = platform_get_drvdata(pdev);
  369. struct stmmac_priv *priv = netdev_priv(ndev);
  370. int ret = stmmac_dvr_remove(&pdev->dev);
  371. struct stm32_dwmac *dwmac = priv->plat->bsp_priv;
  372. stm32_dwmac_clk_disable(priv->plat->bsp_priv);
  373. if (dwmac->irq_pwr_wakeup >= 0) {
  374. dev_pm_clear_wake_irq(&pdev->dev);
  375. device_init_wakeup(&pdev->dev, false);
  376. }
  377. return ret;
  378. }
  379. static int stm32mp1_suspend(struct stm32_dwmac *dwmac)
  380. {
  381. int ret = 0;
  382. ret = clk_prepare_enable(dwmac->clk_ethstp);
  383. if (ret)
  384. return ret;
  385. clk_disable_unprepare(dwmac->clk_tx);
  386. clk_disable_unprepare(dwmac->syscfg_clk);
  387. if (dwmac->enable_eth_ck)
  388. clk_disable_unprepare(dwmac->clk_eth_ck);
  389. return ret;
  390. }
  391. static void stm32mp1_resume(struct stm32_dwmac *dwmac)
  392. {
  393. clk_disable_unprepare(dwmac->clk_ethstp);
  394. }
  395. static int stm32mcu_suspend(struct stm32_dwmac *dwmac)
  396. {
  397. clk_disable_unprepare(dwmac->clk_tx);
  398. clk_disable_unprepare(dwmac->clk_rx);
  399. return 0;
  400. }
  401. #ifdef CONFIG_PM_SLEEP
  402. static int stm32_dwmac_suspend(struct device *dev)
  403. {
  404. struct net_device *ndev = dev_get_drvdata(dev);
  405. struct stmmac_priv *priv = netdev_priv(ndev);
  406. struct stm32_dwmac *dwmac = priv->plat->bsp_priv;
  407. int ret;
  408. ret = stmmac_suspend(dev);
  409. if (dwmac->ops->suspend)
  410. ret = dwmac->ops->suspend(dwmac);
  411. return ret;
  412. }
  413. static int stm32_dwmac_resume(struct device *dev)
  414. {
  415. struct net_device *ndev = dev_get_drvdata(dev);
  416. struct stmmac_priv *priv = netdev_priv(ndev);
  417. struct stm32_dwmac *dwmac = priv->plat->bsp_priv;
  418. int ret;
  419. if (dwmac->ops->resume)
  420. dwmac->ops->resume(dwmac);
  421. ret = stm32_dwmac_init(priv->plat);
  422. if (ret)
  423. return ret;
  424. ret = stmmac_resume(dev);
  425. return ret;
  426. }
  427. #endif /* CONFIG_PM_SLEEP */
  428. static SIMPLE_DEV_PM_OPS(stm32_dwmac_pm_ops,
  429. stm32_dwmac_suspend, stm32_dwmac_resume);
  430. static struct stm32_ops stm32mcu_dwmac_data = {
  431. .set_mode = stm32mcu_set_mode,
  432. .suspend = stm32mcu_suspend,
  433. .syscfg_eth_mask = SYSCFG_MCU_ETH_MASK
  434. };
  435. static struct stm32_ops stm32mp1_dwmac_data = {
  436. .set_mode = stm32mp1_set_mode,
  437. .clk_prepare = stm32mp1_clk_prepare,
  438. .suspend = stm32mp1_suspend,
  439. .resume = stm32mp1_resume,
  440. .parse_data = stm32mp1_parse_data,
  441. .syscfg_eth_mask = SYSCFG_MP1_ETH_MASK,
  442. .clk_rx_enable_in_suspend = true
  443. };
  444. static const struct of_device_id stm32_dwmac_match[] = {
  445. { .compatible = "st,stm32-dwmac", .data = &stm32mcu_dwmac_data},
  446. { .compatible = "st,stm32mp1-dwmac", .data = &stm32mp1_dwmac_data},
  447. { }
  448. };
  449. MODULE_DEVICE_TABLE(of, stm32_dwmac_match);
  450. static struct platform_driver stm32_dwmac_driver = {
  451. .probe = stm32_dwmac_probe,
  452. .remove = stm32_dwmac_remove,
  453. .driver = {
  454. .name = "stm32-dwmac",
  455. .pm = &stm32_dwmac_pm_ops,
  456. .of_match_table = stm32_dwmac_match,
  457. },
  458. };
  459. module_platform_driver(stm32_dwmac_driver);
  460. MODULE_AUTHOR("Alexandre Torgue <[email protected]>");
  461. MODULE_AUTHOR("Christophe Roullier <[email protected]>");
  462. MODULE_DESCRIPTION("STMicroelectronics STM32 DWMAC Specific Glue layer");
  463. MODULE_LICENSE("GPL v2");