dwmac-meson8b.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Amlogic Meson8b and GXBB DWMAC glue layer
  3. *
  4. * Copyright (C) 2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * You should have received a copy of the GNU General Public License
  11. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/device.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/io.h>
  18. #include <linux/ioport.h>
  19. #include <linux/module.h>
  20. #include <linux/of_net.h>
  21. #include <linux/mfd/syscon.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/stmmac.h>
  24. #include "stmmac_platform.h"
  25. #define PRG_ETH0 0x0
  26. #define PRG_ETH0_RGMII_MODE BIT(0)
  27. /* mux to choose between fclk_div2 (bit unset) and mpll2 (bit set) */
  28. #define PRG_ETH0_CLK_M250_SEL_SHIFT 4
  29. #define PRG_ETH0_CLK_M250_SEL_MASK GENMASK(4, 4)
  30. #define PRG_ETH0_TXDLY_SHIFT 5
  31. #define PRG_ETH0_TXDLY_MASK GENMASK(6, 5)
  32. #define PRG_ETH0_TXDLY_OFF (0x0 << PRG_ETH0_TXDLY_SHIFT)
  33. #define PRG_ETH0_TXDLY_QUARTER (0x1 << PRG_ETH0_TXDLY_SHIFT)
  34. #define PRG_ETH0_TXDLY_HALF (0x2 << PRG_ETH0_TXDLY_SHIFT)
  35. #define PRG_ETH0_TXDLY_THREE_QUARTERS (0x3 << PRG_ETH0_TXDLY_SHIFT)
  36. /* divider for the result of m250_sel */
  37. #define PRG_ETH0_CLK_M250_DIV_SHIFT 7
  38. #define PRG_ETH0_CLK_M250_DIV_WIDTH 3
  39. /* divides the result of m25_sel by either 5 (bit unset) or 10 (bit set) */
  40. #define PRG_ETH0_CLK_M25_DIV_SHIFT 10
  41. #define PRG_ETH0_CLK_M25_DIV_WIDTH 1
  42. #define PRG_ETH0_INVERTED_RMII_CLK BIT(11)
  43. #define PRG_ETH0_TX_AND_PHY_REF_CLK BIT(12)
  44. #define MUX_CLK_NUM_PARENTS 2
  45. struct meson8b_dwmac {
  46. struct platform_device *pdev;
  47. void __iomem *regs;
  48. phy_interface_t phy_mode;
  49. struct clk_mux m250_mux;
  50. struct clk *m250_mux_clk;
  51. struct clk *m250_mux_parent[MUX_CLK_NUM_PARENTS];
  52. struct clk_divider m250_div;
  53. struct clk *m250_div_clk;
  54. struct clk_divider m25_div;
  55. struct clk *m25_div_clk;
  56. };
  57. static void meson8b_dwmac_mask_bits(struct meson8b_dwmac *dwmac, u32 reg,
  58. u32 mask, u32 value)
  59. {
  60. u32 data;
  61. data = readl(dwmac->regs + reg);
  62. data &= ~mask;
  63. data |= (value & mask);
  64. writel(data, dwmac->regs + reg);
  65. }
  66. static int meson8b_init_clk(struct meson8b_dwmac *dwmac)
  67. {
  68. struct clk_init_data init;
  69. int i, ret;
  70. struct device *dev = &dwmac->pdev->dev;
  71. char clk_name[32];
  72. const char *clk_div_parents[1];
  73. const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
  74. static struct clk_div_table clk_25m_div_table[] = {
  75. { .val = 0, .div = 5 },
  76. { .val = 1, .div = 10 },
  77. { /* sentinel */ },
  78. };
  79. /* get the mux parents from DT */
  80. for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
  81. char name[16];
  82. snprintf(name, sizeof(name), "clkin%d", i);
  83. dwmac->m250_mux_parent[i] = devm_clk_get(dev, name);
  84. if (IS_ERR(dwmac->m250_mux_parent[i])) {
  85. ret = PTR_ERR(dwmac->m250_mux_parent[i]);
  86. if (ret != -EPROBE_DEFER)
  87. dev_err(dev, "Missing clock %s\n", name);
  88. return ret;
  89. }
  90. mux_parent_names[i] =
  91. __clk_get_name(dwmac->m250_mux_parent[i]);
  92. }
  93. /* create the m250_mux */
  94. snprintf(clk_name, sizeof(clk_name), "%s#m250_sel", dev_name(dev));
  95. init.name = clk_name;
  96. init.ops = &clk_mux_ops;
  97. init.flags = CLK_SET_RATE_PARENT;
  98. init.parent_names = mux_parent_names;
  99. init.num_parents = MUX_CLK_NUM_PARENTS;
  100. dwmac->m250_mux.reg = dwmac->regs + PRG_ETH0;
  101. dwmac->m250_mux.shift = PRG_ETH0_CLK_M250_SEL_SHIFT;
  102. dwmac->m250_mux.mask = PRG_ETH0_CLK_M250_SEL_MASK;
  103. dwmac->m250_mux.flags = 0;
  104. dwmac->m250_mux.table = NULL;
  105. dwmac->m250_mux.hw.init = &init;
  106. dwmac->m250_mux_clk = devm_clk_register(dev, &dwmac->m250_mux.hw);
  107. if (WARN_ON(IS_ERR(dwmac->m250_mux_clk)))
  108. return PTR_ERR(dwmac->m250_mux_clk);
  109. /* create the m250_div */
  110. snprintf(clk_name, sizeof(clk_name), "%s#m250_div", dev_name(dev));
  111. init.name = devm_kstrdup(dev, clk_name, GFP_KERNEL);
  112. init.ops = &clk_divider_ops;
  113. init.flags = CLK_SET_RATE_PARENT;
  114. clk_div_parents[0] = __clk_get_name(dwmac->m250_mux_clk);
  115. init.parent_names = clk_div_parents;
  116. init.num_parents = ARRAY_SIZE(clk_div_parents);
  117. dwmac->m250_div.reg = dwmac->regs + PRG_ETH0;
  118. dwmac->m250_div.shift = PRG_ETH0_CLK_M250_DIV_SHIFT;
  119. dwmac->m250_div.width = PRG_ETH0_CLK_M250_DIV_WIDTH;
  120. dwmac->m250_div.hw.init = &init;
  121. dwmac->m250_div.flags = CLK_DIVIDER_ONE_BASED |
  122. CLK_DIVIDER_ALLOW_ZERO |
  123. CLK_DIVIDER_ROUND_CLOSEST;
  124. dwmac->m250_div_clk = devm_clk_register(dev, &dwmac->m250_div.hw);
  125. if (WARN_ON(IS_ERR(dwmac->m250_div_clk)))
  126. return PTR_ERR(dwmac->m250_div_clk);
  127. /* create the m25_div */
  128. snprintf(clk_name, sizeof(clk_name), "%s#m25_div", dev_name(dev));
  129. init.name = devm_kstrdup(dev, clk_name, GFP_KERNEL);
  130. init.ops = &clk_divider_ops;
  131. init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
  132. clk_div_parents[0] = __clk_get_name(dwmac->m250_div_clk);
  133. init.parent_names = clk_div_parents;
  134. init.num_parents = ARRAY_SIZE(clk_div_parents);
  135. dwmac->m25_div.reg = dwmac->regs + PRG_ETH0;
  136. dwmac->m25_div.shift = PRG_ETH0_CLK_M25_DIV_SHIFT;
  137. dwmac->m25_div.width = PRG_ETH0_CLK_M25_DIV_WIDTH;
  138. dwmac->m25_div.table = clk_25m_div_table;
  139. dwmac->m25_div.hw.init = &init;
  140. dwmac->m25_div.flags = CLK_DIVIDER_ALLOW_ZERO;
  141. dwmac->m25_div_clk = devm_clk_register(dev, &dwmac->m25_div.hw);
  142. if (WARN_ON(IS_ERR(dwmac->m25_div_clk)))
  143. return PTR_ERR(dwmac->m25_div_clk);
  144. return 0;
  145. }
  146. static int meson8b_init_prg_eth(struct meson8b_dwmac *dwmac)
  147. {
  148. int ret;
  149. unsigned long clk_rate;
  150. switch (dwmac->phy_mode) {
  151. case PHY_INTERFACE_MODE_RGMII:
  152. case PHY_INTERFACE_MODE_RGMII_ID:
  153. case PHY_INTERFACE_MODE_RGMII_RXID:
  154. case PHY_INTERFACE_MODE_RGMII_TXID:
  155. /* Generate a 25MHz clock for the PHY */
  156. clk_rate = 25 * 1000 * 1000;
  157. /* enable RGMII mode */
  158. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_RGMII_MODE,
  159. PRG_ETH0_RGMII_MODE);
  160. /* only relevant for RMII mode -> disable in RGMII mode */
  161. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
  162. PRG_ETH0_INVERTED_RMII_CLK, 0);
  163. /* TX clock delay - all known boards use a 1/4 cycle delay */
  164. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK,
  165. PRG_ETH0_TXDLY_QUARTER);
  166. break;
  167. case PHY_INTERFACE_MODE_RMII:
  168. /* Use the rate of the mux clock for the internal RMII PHY */
  169. clk_rate = clk_get_rate(dwmac->m250_mux_clk);
  170. /* disable RGMII mode -> enables RMII mode */
  171. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_RGMII_MODE,
  172. 0);
  173. /* invert internal clk_rmii_i to generate 25/2.5 tx_rx_clk */
  174. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
  175. PRG_ETH0_INVERTED_RMII_CLK,
  176. PRG_ETH0_INVERTED_RMII_CLK);
  177. /* TX clock delay cannot be configured in RMII mode */
  178. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK,
  179. 0);
  180. break;
  181. default:
  182. dev_err(&dwmac->pdev->dev, "unsupported phy-mode %s\n",
  183. phy_modes(dwmac->phy_mode));
  184. return -EINVAL;
  185. }
  186. ret = clk_prepare_enable(dwmac->m25_div_clk);
  187. if (ret) {
  188. dev_err(&dwmac->pdev->dev, "failed to enable the PHY clock\n");
  189. return ret;
  190. }
  191. ret = clk_set_rate(dwmac->m25_div_clk, clk_rate);
  192. if (ret) {
  193. clk_disable_unprepare(dwmac->m25_div_clk);
  194. dev_err(&dwmac->pdev->dev, "failed to set PHY clock\n");
  195. return ret;
  196. }
  197. /* enable TX_CLK and PHY_REF_CLK generator */
  198. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TX_AND_PHY_REF_CLK,
  199. PRG_ETH0_TX_AND_PHY_REF_CLK);
  200. return 0;
  201. }
  202. static int meson8b_dwmac_probe(struct platform_device *pdev)
  203. {
  204. struct plat_stmmacenet_data *plat_dat;
  205. struct stmmac_resources stmmac_res;
  206. struct resource *res;
  207. struct meson8b_dwmac *dwmac;
  208. int ret;
  209. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  210. if (ret)
  211. return ret;
  212. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  213. if (IS_ERR(plat_dat))
  214. return PTR_ERR(plat_dat);
  215. dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
  216. if (!dwmac) {
  217. ret = -ENOMEM;
  218. goto err_remove_config_dt;
  219. }
  220. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  221. dwmac->regs = devm_ioremap_resource(&pdev->dev, res);
  222. if (IS_ERR(dwmac->regs)) {
  223. ret = PTR_ERR(dwmac->regs);
  224. goto err_remove_config_dt;
  225. }
  226. dwmac->pdev = pdev;
  227. dwmac->phy_mode = of_get_phy_mode(pdev->dev.of_node);
  228. if (dwmac->phy_mode < 0) {
  229. dev_err(&pdev->dev, "missing phy-mode property\n");
  230. ret = -EINVAL;
  231. goto err_remove_config_dt;
  232. }
  233. ret = meson8b_init_clk(dwmac);
  234. if (ret)
  235. goto err_remove_config_dt;
  236. ret = meson8b_init_prg_eth(dwmac);
  237. if (ret)
  238. goto err_remove_config_dt;
  239. plat_dat->bsp_priv = dwmac;
  240. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  241. if (ret)
  242. goto err_clk_disable;
  243. return 0;
  244. err_clk_disable:
  245. clk_disable_unprepare(dwmac->m25_div_clk);
  246. err_remove_config_dt:
  247. stmmac_remove_config_dt(pdev, plat_dat);
  248. return ret;
  249. }
  250. static int meson8b_dwmac_remove(struct platform_device *pdev)
  251. {
  252. struct meson8b_dwmac *dwmac = get_stmmac_bsp_priv(&pdev->dev);
  253. clk_disable_unprepare(dwmac->m25_div_clk);
  254. return stmmac_pltfr_remove(pdev);
  255. }
  256. static const struct of_device_id meson8b_dwmac_match[] = {
  257. { .compatible = "amlogic,meson8b-dwmac" },
  258. { .compatible = "amlogic,meson-gxbb-dwmac" },
  259. { }
  260. };
  261. MODULE_DEVICE_TABLE(of, meson8b_dwmac_match);
  262. static struct platform_driver meson8b_dwmac_driver = {
  263. .probe = meson8b_dwmac_probe,
  264. .remove = meson8b_dwmac_remove,
  265. .driver = {
  266. .name = "meson8b-dwmac",
  267. .pm = &stmmac_pltfr_pm_ops,
  268. .of_match_table = meson8b_dwmac_match,
  269. },
  270. };
  271. module_platform_driver(meson8b_dwmac_driver);
  272. MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
  273. MODULE_DESCRIPTION("Amlogic Meson8b and GXBB DWMAC glue layer");
  274. MODULE_LICENSE("GPL v2");