dwmac-meson8b.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Amlogic Meson8b, Meson8m2 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. #define PRG_ETH0_RGMII_TX_CLK_EN 10
  36. #define PRG_ETH0_INVERTED_RMII_CLK BIT(11)
  37. #define PRG_ETH0_TX_AND_PHY_REF_CLK BIT(12)
  38. #define MUX_CLK_NUM_PARENTS 2
  39. struct meson8b_dwmac {
  40. struct device *dev;
  41. void __iomem *regs;
  42. phy_interface_t phy_mode;
  43. struct clk *rgmii_tx_clk;
  44. u32 tx_delay_ns;
  45. };
  46. struct meson8b_dwmac_clk_configs {
  47. struct clk_mux m250_mux;
  48. struct clk_divider m250_div;
  49. struct clk_fixed_factor fixed_div2;
  50. struct clk_gate rgmii_tx_en;
  51. };
  52. static void meson8b_dwmac_mask_bits(struct meson8b_dwmac *dwmac, u32 reg,
  53. u32 mask, u32 value)
  54. {
  55. u32 data;
  56. data = readl(dwmac->regs + reg);
  57. data &= ~mask;
  58. data |= (value & mask);
  59. writel(data, dwmac->regs + reg);
  60. }
  61. static struct clk *meson8b_dwmac_register_clk(struct meson8b_dwmac *dwmac,
  62. const char *name_suffix,
  63. const char **parent_names,
  64. int num_parents,
  65. const struct clk_ops *ops,
  66. struct clk_hw *hw)
  67. {
  68. struct clk_init_data init;
  69. char clk_name[32];
  70. snprintf(clk_name, sizeof(clk_name), "%s#%s", dev_name(dwmac->dev),
  71. name_suffix);
  72. init.name = clk_name;
  73. init.ops = ops;
  74. init.flags = CLK_SET_RATE_PARENT;
  75. init.parent_names = parent_names;
  76. init.num_parents = num_parents;
  77. hw->init = &init;
  78. return devm_clk_register(dwmac->dev, hw);
  79. }
  80. static int meson8b_init_rgmii_tx_clk(struct meson8b_dwmac *dwmac)
  81. {
  82. int i, ret;
  83. struct clk *clk;
  84. struct device *dev = dwmac->dev;
  85. const char *parent_name, *mux_parent_names[MUX_CLK_NUM_PARENTS];
  86. struct meson8b_dwmac_clk_configs *clk_configs;
  87. clk_configs = devm_kzalloc(dev, sizeof(*clk_configs), GFP_KERNEL);
  88. if (!clk_configs)
  89. return -ENOMEM;
  90. /* get the mux parents from DT */
  91. for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
  92. char name[16];
  93. snprintf(name, sizeof(name), "clkin%d", i);
  94. clk = devm_clk_get(dev, name);
  95. if (IS_ERR(clk)) {
  96. ret = PTR_ERR(clk);
  97. if (ret != -EPROBE_DEFER)
  98. dev_err(dev, "Missing clock %s\n", name);
  99. return ret;
  100. }
  101. mux_parent_names[i] = __clk_get_name(clk);
  102. }
  103. clk_configs->m250_mux.reg = dwmac->regs + PRG_ETH0;
  104. clk_configs->m250_mux.shift = PRG_ETH0_CLK_M250_SEL_SHIFT;
  105. clk_configs->m250_mux.mask = PRG_ETH0_CLK_M250_SEL_MASK;
  106. clk = meson8b_dwmac_register_clk(dwmac, "m250_sel", mux_parent_names,
  107. MUX_CLK_NUM_PARENTS, &clk_mux_ops,
  108. &clk_configs->m250_mux.hw);
  109. if (WARN_ON(IS_ERR(clk)))
  110. return PTR_ERR(clk);
  111. parent_name = __clk_get_name(clk);
  112. clk_configs->m250_div.reg = dwmac->regs + PRG_ETH0;
  113. clk_configs->m250_div.shift = PRG_ETH0_CLK_M250_DIV_SHIFT;
  114. clk_configs->m250_div.width = PRG_ETH0_CLK_M250_DIV_WIDTH;
  115. clk_configs->m250_div.flags = CLK_DIVIDER_ONE_BASED |
  116. CLK_DIVIDER_ALLOW_ZERO |
  117. CLK_DIVIDER_ROUND_CLOSEST;
  118. clk = meson8b_dwmac_register_clk(dwmac, "m250_div", &parent_name, 1,
  119. &clk_divider_ops,
  120. &clk_configs->m250_div.hw);
  121. if (WARN_ON(IS_ERR(clk)))
  122. return PTR_ERR(clk);
  123. parent_name = __clk_get_name(clk);
  124. clk_configs->fixed_div2.mult = 1;
  125. clk_configs->fixed_div2.div = 2;
  126. clk = meson8b_dwmac_register_clk(dwmac, "fixed_div2", &parent_name, 1,
  127. &clk_fixed_factor_ops,
  128. &clk_configs->fixed_div2.hw);
  129. if (WARN_ON(IS_ERR(clk)))
  130. return PTR_ERR(clk);
  131. parent_name = __clk_get_name(clk);
  132. clk_configs->rgmii_tx_en.reg = dwmac->regs + PRG_ETH0;
  133. clk_configs->rgmii_tx_en.bit_idx = PRG_ETH0_RGMII_TX_CLK_EN;
  134. clk = meson8b_dwmac_register_clk(dwmac, "rgmii_tx_en", &parent_name, 1,
  135. &clk_gate_ops,
  136. &clk_configs->rgmii_tx_en.hw);
  137. if (WARN_ON(IS_ERR(clk)))
  138. return PTR_ERR(clk);
  139. dwmac->rgmii_tx_clk = clk;
  140. return 0;
  141. }
  142. static int meson8b_init_prg_eth(struct meson8b_dwmac *dwmac)
  143. {
  144. int ret;
  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. /* 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. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK,
  164. tx_dly_val << PRG_ETH0_TXDLY_SHIFT);
  165. /* Configure the 125MHz RGMII TX clock, the IP block changes
  166. * the output automatically (= without us having to configure
  167. * a register) based on the line-speed (125MHz for Gbit speeds,
  168. * 25MHz for 100Mbit/s and 2.5MHz for 10Mbit/s).
  169. */
  170. ret = clk_set_rate(dwmac->rgmii_tx_clk, 125 * 1000 * 1000);
  171. if (ret) {
  172. dev_err(dwmac->dev,
  173. "failed to set RGMII TX clock\n");
  174. return ret;
  175. }
  176. ret = clk_prepare_enable(dwmac->rgmii_tx_clk);
  177. if (ret) {
  178. dev_err(dwmac->dev,
  179. "failed to enable the RGMII TX clock\n");
  180. return ret;
  181. }
  182. devm_add_action_or_reset(dwmac->dev,
  183. (void(*)(void *))clk_disable_unprepare,
  184. dwmac->rgmii_tx_clk);
  185. break;
  186. case PHY_INTERFACE_MODE_RMII:
  187. /* disable RGMII mode -> enables RMII mode */
  188. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_RGMII_MODE,
  189. 0);
  190. /* invert internal clk_rmii_i to generate 25/2.5 tx_rx_clk */
  191. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
  192. PRG_ETH0_INVERTED_RMII_CLK,
  193. PRG_ETH0_INVERTED_RMII_CLK);
  194. /* TX clock delay cannot be configured in RMII mode */
  195. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK,
  196. 0);
  197. break;
  198. default:
  199. dev_err(dwmac->dev, "unsupported phy-mode %s\n",
  200. phy_modes(dwmac->phy_mode));
  201. return -EINVAL;
  202. }
  203. /* enable TX_CLK and PHY_REF_CLK generator */
  204. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TX_AND_PHY_REF_CLK,
  205. PRG_ETH0_TX_AND_PHY_REF_CLK);
  206. return 0;
  207. }
  208. static int meson8b_dwmac_probe(struct platform_device *pdev)
  209. {
  210. struct plat_stmmacenet_data *plat_dat;
  211. struct stmmac_resources stmmac_res;
  212. struct resource *res;
  213. struct meson8b_dwmac *dwmac;
  214. int ret;
  215. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  216. if (ret)
  217. return ret;
  218. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  219. if (IS_ERR(plat_dat))
  220. return PTR_ERR(plat_dat);
  221. dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
  222. if (!dwmac) {
  223. ret = -ENOMEM;
  224. goto err_remove_config_dt;
  225. }
  226. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  227. dwmac->regs = devm_ioremap_resource(&pdev->dev, res);
  228. if (IS_ERR(dwmac->regs)) {
  229. ret = PTR_ERR(dwmac->regs);
  230. goto err_remove_config_dt;
  231. }
  232. dwmac->dev = &pdev->dev;
  233. dwmac->phy_mode = of_get_phy_mode(pdev->dev.of_node);
  234. if (dwmac->phy_mode < 0) {
  235. dev_err(&pdev->dev, "missing phy-mode property\n");
  236. ret = -EINVAL;
  237. goto err_remove_config_dt;
  238. }
  239. /* use 2ns as fallback since this value was previously hardcoded */
  240. if (of_property_read_u32(pdev->dev.of_node, "amlogic,tx-delay-ns",
  241. &dwmac->tx_delay_ns))
  242. dwmac->tx_delay_ns = 2;
  243. ret = meson8b_init_rgmii_tx_clk(dwmac);
  244. if (ret)
  245. goto err_remove_config_dt;
  246. ret = meson8b_init_prg_eth(dwmac);
  247. if (ret)
  248. goto err_remove_config_dt;
  249. plat_dat->bsp_priv = dwmac;
  250. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  251. if (ret)
  252. goto err_remove_config_dt;
  253. return 0;
  254. err_remove_config_dt:
  255. stmmac_remove_config_dt(pdev, plat_dat);
  256. return ret;
  257. }
  258. static const struct of_device_id meson8b_dwmac_match[] = {
  259. { .compatible = "amlogic,meson8b-dwmac" },
  260. { .compatible = "amlogic,meson8m2-dwmac" },
  261. { .compatible = "amlogic,meson-gxbb-dwmac" },
  262. { }
  263. };
  264. MODULE_DEVICE_TABLE(of, meson8b_dwmac_match);
  265. static struct platform_driver meson8b_dwmac_driver = {
  266. .probe = meson8b_dwmac_probe,
  267. .remove = stmmac_pltfr_remove,
  268. .driver = {
  269. .name = "meson8b-dwmac",
  270. .pm = &stmmac_pltfr_pm_ops,
  271. .of_match_table = meson8b_dwmac_match,
  272. },
  273. };
  274. module_platform_driver(meson8b_dwmac_driver);
  275. MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
  276. MODULE_DESCRIPTION("Amlogic Meson8b, Meson8m2 and GXBB DWMAC glue layer");
  277. MODULE_LICENSE("GPL v2");