dwmac-stm32.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * dwmac-stm32.c - DWMAC Specific Glue layer for STM32 MCU
  3. *
  4. * Copyright (C) Alexandre Torgue 2015
  5. * Author: Alexandre Torgue <alexandre.torgue@gmail.com>
  6. * License terms: GNU General Public License (GPL), version 2
  7. *
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of_net.h>
  16. #include <linux/phy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/stmmac.h>
  21. #include "stmmac_platform.h"
  22. #define MII_PHY_SEL_MASK BIT(23)
  23. struct stm32_dwmac {
  24. struct clk *clk_tx;
  25. struct clk *clk_rx;
  26. u32 mode_reg; /* MAC glue-logic mode register */
  27. struct regmap *regmap;
  28. u32 speed;
  29. };
  30. static int stm32_dwmac_init(struct plat_stmmacenet_data *plat_dat)
  31. {
  32. struct stm32_dwmac *dwmac = plat_dat->bsp_priv;
  33. u32 reg = dwmac->mode_reg;
  34. u32 val;
  35. int ret;
  36. val = (plat_dat->interface == PHY_INTERFACE_MODE_MII) ? 0 : 1;
  37. ret = regmap_update_bits(dwmac->regmap, reg, MII_PHY_SEL_MASK, val);
  38. if (ret)
  39. return ret;
  40. ret = clk_prepare_enable(dwmac->clk_tx);
  41. if (ret)
  42. return ret;
  43. ret = clk_prepare_enable(dwmac->clk_rx);
  44. if (ret)
  45. clk_disable_unprepare(dwmac->clk_tx);
  46. return ret;
  47. }
  48. static void stm32_dwmac_clk_disable(struct stm32_dwmac *dwmac)
  49. {
  50. clk_disable_unprepare(dwmac->clk_tx);
  51. clk_disable_unprepare(dwmac->clk_rx);
  52. }
  53. static int stm32_dwmac_parse_data(struct stm32_dwmac *dwmac,
  54. struct device *dev)
  55. {
  56. struct device_node *np = dev->of_node;
  57. int err;
  58. /* Get TX/RX clocks */
  59. dwmac->clk_tx = devm_clk_get(dev, "mac-clk-tx");
  60. if (IS_ERR(dwmac->clk_tx)) {
  61. dev_err(dev, "No tx clock provided...\n");
  62. return PTR_ERR(dwmac->clk_tx);
  63. }
  64. dwmac->clk_rx = devm_clk_get(dev, "mac-clk-rx");
  65. if (IS_ERR(dwmac->clk_rx)) {
  66. dev_err(dev, "No rx clock provided...\n");
  67. return PTR_ERR(dwmac->clk_rx);
  68. }
  69. /* Get mode register */
  70. dwmac->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscon");
  71. if (IS_ERR(dwmac->regmap))
  72. return PTR_ERR(dwmac->regmap);
  73. err = of_property_read_u32_index(np, "st,syscon", 1, &dwmac->mode_reg);
  74. if (err)
  75. dev_err(dev, "Can't get sysconfig mode offset (%d)\n", err);
  76. return err;
  77. }
  78. static int stm32_dwmac_probe(struct platform_device *pdev)
  79. {
  80. struct plat_stmmacenet_data *plat_dat;
  81. struct stmmac_resources stmmac_res;
  82. struct stm32_dwmac *dwmac;
  83. int ret;
  84. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  85. if (ret)
  86. return ret;
  87. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  88. if (IS_ERR(plat_dat))
  89. return PTR_ERR(plat_dat);
  90. dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
  91. if (!dwmac) {
  92. ret = -ENOMEM;
  93. goto err_remove_config_dt;
  94. }
  95. ret = stm32_dwmac_parse_data(dwmac, &pdev->dev);
  96. if (ret) {
  97. dev_err(&pdev->dev, "Unable to parse OF data\n");
  98. goto err_remove_config_dt;
  99. }
  100. plat_dat->bsp_priv = dwmac;
  101. ret = stm32_dwmac_init(plat_dat);
  102. if (ret)
  103. goto err_remove_config_dt;
  104. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  105. if (ret)
  106. goto err_clk_disable;
  107. return 0;
  108. err_clk_disable:
  109. stm32_dwmac_clk_disable(dwmac);
  110. err_remove_config_dt:
  111. stmmac_remove_config_dt(pdev, plat_dat);
  112. return ret;
  113. }
  114. static int stm32_dwmac_remove(struct platform_device *pdev)
  115. {
  116. struct net_device *ndev = platform_get_drvdata(pdev);
  117. struct stmmac_priv *priv = netdev_priv(ndev);
  118. int ret = stmmac_dvr_remove(&pdev->dev);
  119. stm32_dwmac_clk_disable(priv->plat->bsp_priv);
  120. return ret;
  121. }
  122. #ifdef CONFIG_PM_SLEEP
  123. static int stm32_dwmac_suspend(struct device *dev)
  124. {
  125. struct net_device *ndev = dev_get_drvdata(dev);
  126. struct stmmac_priv *priv = netdev_priv(ndev);
  127. int ret;
  128. ret = stmmac_suspend(dev);
  129. stm32_dwmac_clk_disable(priv->plat->bsp_priv);
  130. return ret;
  131. }
  132. static int stm32_dwmac_resume(struct device *dev)
  133. {
  134. struct net_device *ndev = dev_get_drvdata(dev);
  135. struct stmmac_priv *priv = netdev_priv(ndev);
  136. int ret;
  137. ret = stm32_dwmac_init(priv->plat);
  138. if (ret)
  139. return ret;
  140. ret = stmmac_resume(dev);
  141. return ret;
  142. }
  143. #endif /* CONFIG_PM_SLEEP */
  144. static SIMPLE_DEV_PM_OPS(stm32_dwmac_pm_ops,
  145. stm32_dwmac_suspend, stm32_dwmac_resume);
  146. static const struct of_device_id stm32_dwmac_match[] = {
  147. { .compatible = "st,stm32-dwmac"},
  148. { }
  149. };
  150. MODULE_DEVICE_TABLE(of, stm32_dwmac_match);
  151. static struct platform_driver stm32_dwmac_driver = {
  152. .probe = stm32_dwmac_probe,
  153. .remove = stm32_dwmac_remove,
  154. .driver = {
  155. .name = "stm32-dwmac",
  156. .pm = &stm32_dwmac_pm_ops,
  157. .of_match_table = stm32_dwmac_match,
  158. },
  159. };
  160. module_platform_driver(stm32_dwmac_driver);
  161. MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@gmail.com>");
  162. MODULE_DESCRIPTION("STMicroelectronics MCU DWMAC Specific Glue layer");
  163. MODULE_LICENSE("GPL v2");