dwmac-meson8b.c 9.5 KB

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