dwmac-meson8b.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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_device.h>
  21. #include <linux/of_net.h>
  22. #include <linux/mfd/syscon.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/stmmac.h>
  25. #include "stmmac_platform.h"
  26. #define PRG_ETH0 0x0
  27. #define PRG_ETH0_RGMII_MODE BIT(0)
  28. #define PRG_ETH0_EXT_PHY_MODE_MASK GENMASK(2, 0)
  29. #define PRG_ETH0_EXT_RGMII_MODE 1
  30. #define PRG_ETH0_EXT_RMII_MODE 4
  31. /* mux to choose between fclk_div2 (bit unset) and mpll2 (bit set) */
  32. #define PRG_ETH0_CLK_M250_SEL_SHIFT 4
  33. #define PRG_ETH0_CLK_M250_SEL_MASK GENMASK(4, 4)
  34. #define PRG_ETH0_TXDLY_SHIFT 5
  35. #define PRG_ETH0_TXDLY_MASK GENMASK(6, 5)
  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. #define PRG_ETH0_RGMII_TX_CLK_EN 10
  40. #define PRG_ETH0_INVERTED_RMII_CLK BIT(11)
  41. #define PRG_ETH0_TX_AND_PHY_REF_CLK BIT(12)
  42. #define MUX_CLK_NUM_PARENTS 2
  43. struct meson8b_dwmac;
  44. struct meson8b_dwmac_data {
  45. int (*set_phy_mode)(struct meson8b_dwmac *dwmac);
  46. };
  47. struct meson8b_dwmac {
  48. struct device *dev;
  49. void __iomem *regs;
  50. const struct meson8b_dwmac_data *data;
  51. phy_interface_t phy_mode;
  52. struct clk *rgmii_tx_clk;
  53. u32 tx_delay_ns;
  54. };
  55. struct meson8b_dwmac_clk_configs {
  56. struct clk_mux m250_mux;
  57. struct clk_divider m250_div;
  58. struct clk_fixed_factor fixed_div2;
  59. struct clk_gate rgmii_tx_en;
  60. };
  61. static void meson8b_dwmac_mask_bits(struct meson8b_dwmac *dwmac, u32 reg,
  62. u32 mask, u32 value)
  63. {
  64. u32 data;
  65. data = readl(dwmac->regs + reg);
  66. data &= ~mask;
  67. data |= (value & mask);
  68. writel(data, dwmac->regs + reg);
  69. }
  70. static struct clk *meson8b_dwmac_register_clk(struct meson8b_dwmac *dwmac,
  71. const char *name_suffix,
  72. const char **parent_names,
  73. int num_parents,
  74. const struct clk_ops *ops,
  75. struct clk_hw *hw)
  76. {
  77. struct clk_init_data init;
  78. char clk_name[32];
  79. snprintf(clk_name, sizeof(clk_name), "%s#%s", dev_name(dwmac->dev),
  80. name_suffix);
  81. init.name = clk_name;
  82. init.ops = ops;
  83. init.flags = CLK_SET_RATE_PARENT;
  84. init.parent_names = parent_names;
  85. init.num_parents = num_parents;
  86. hw->init = &init;
  87. return devm_clk_register(dwmac->dev, hw);
  88. }
  89. static int meson8b_init_rgmii_tx_clk(struct meson8b_dwmac *dwmac)
  90. {
  91. int i, ret;
  92. struct clk *clk;
  93. struct device *dev = dwmac->dev;
  94. const char *parent_name, *mux_parent_names[MUX_CLK_NUM_PARENTS];
  95. struct meson8b_dwmac_clk_configs *clk_configs;
  96. static const struct clk_div_table div_table[] = {
  97. { .div = 2, .val = 2, },
  98. { .div = 3, .val = 3, },
  99. { .div = 4, .val = 4, },
  100. { .div = 5, .val = 5, },
  101. { .div = 6, .val = 6, },
  102. { .div = 7, .val = 7, },
  103. };
  104. clk_configs = devm_kzalloc(dev, sizeof(*clk_configs), GFP_KERNEL);
  105. if (!clk_configs)
  106. return -ENOMEM;
  107. /* get the mux parents from DT */
  108. for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
  109. char name[16];
  110. snprintf(name, sizeof(name), "clkin%d", i);
  111. clk = devm_clk_get(dev, name);
  112. if (IS_ERR(clk)) {
  113. ret = PTR_ERR(clk);
  114. if (ret != -EPROBE_DEFER)
  115. dev_err(dev, "Missing clock %s\n", name);
  116. return ret;
  117. }
  118. mux_parent_names[i] = __clk_get_name(clk);
  119. }
  120. clk_configs->m250_mux.reg = dwmac->regs + PRG_ETH0;
  121. clk_configs->m250_mux.shift = PRG_ETH0_CLK_M250_SEL_SHIFT;
  122. clk_configs->m250_mux.mask = PRG_ETH0_CLK_M250_SEL_MASK;
  123. clk = meson8b_dwmac_register_clk(dwmac, "m250_sel", mux_parent_names,
  124. MUX_CLK_NUM_PARENTS, &clk_mux_ops,
  125. &clk_configs->m250_mux.hw);
  126. if (WARN_ON(IS_ERR(clk)))
  127. return PTR_ERR(clk);
  128. parent_name = __clk_get_name(clk);
  129. clk_configs->m250_div.reg = dwmac->regs + PRG_ETH0;
  130. clk_configs->m250_div.shift = PRG_ETH0_CLK_M250_DIV_SHIFT;
  131. clk_configs->m250_div.width = PRG_ETH0_CLK_M250_DIV_WIDTH;
  132. clk_configs->m250_div.table = div_table;
  133. clk_configs->m250_div.flags = CLK_DIVIDER_ALLOW_ZERO |
  134. CLK_DIVIDER_ROUND_CLOSEST;
  135. clk = meson8b_dwmac_register_clk(dwmac, "m250_div", &parent_name, 1,
  136. &clk_divider_ops,
  137. &clk_configs->m250_div.hw);
  138. if (WARN_ON(IS_ERR(clk)))
  139. return PTR_ERR(clk);
  140. parent_name = __clk_get_name(clk);
  141. clk_configs->fixed_div2.mult = 1;
  142. clk_configs->fixed_div2.div = 2;
  143. clk = meson8b_dwmac_register_clk(dwmac, "fixed_div2", &parent_name, 1,
  144. &clk_fixed_factor_ops,
  145. &clk_configs->fixed_div2.hw);
  146. if (WARN_ON(IS_ERR(clk)))
  147. return PTR_ERR(clk);
  148. parent_name = __clk_get_name(clk);
  149. clk_configs->rgmii_tx_en.reg = dwmac->regs + PRG_ETH0;
  150. clk_configs->rgmii_tx_en.bit_idx = PRG_ETH0_RGMII_TX_CLK_EN;
  151. clk = meson8b_dwmac_register_clk(dwmac, "rgmii_tx_en", &parent_name, 1,
  152. &clk_gate_ops,
  153. &clk_configs->rgmii_tx_en.hw);
  154. if (WARN_ON(IS_ERR(clk)))
  155. return PTR_ERR(clk);
  156. dwmac->rgmii_tx_clk = clk;
  157. return 0;
  158. }
  159. static int meson8b_set_phy_mode(struct meson8b_dwmac *dwmac)
  160. {
  161. switch (dwmac->phy_mode) {
  162. case PHY_INTERFACE_MODE_RGMII:
  163. case PHY_INTERFACE_MODE_RGMII_RXID:
  164. case PHY_INTERFACE_MODE_RGMII_ID:
  165. case PHY_INTERFACE_MODE_RGMII_TXID:
  166. /* enable RGMII mode */
  167. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
  168. PRG_ETH0_RGMII_MODE,
  169. PRG_ETH0_RGMII_MODE);
  170. break;
  171. case PHY_INTERFACE_MODE_RMII:
  172. /* disable RGMII mode -> enables RMII mode */
  173. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
  174. PRG_ETH0_RGMII_MODE, 0);
  175. break;
  176. default:
  177. dev_err(dwmac->dev, "fail to set phy-mode %s\n",
  178. phy_modes(dwmac->phy_mode));
  179. return -EINVAL;
  180. }
  181. return 0;
  182. }
  183. static int meson_axg_set_phy_mode(struct meson8b_dwmac *dwmac)
  184. {
  185. switch (dwmac->phy_mode) {
  186. case PHY_INTERFACE_MODE_RGMII:
  187. case PHY_INTERFACE_MODE_RGMII_RXID:
  188. case PHY_INTERFACE_MODE_RGMII_ID:
  189. case PHY_INTERFACE_MODE_RGMII_TXID:
  190. /* enable RGMII mode */
  191. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
  192. PRG_ETH0_EXT_PHY_MODE_MASK,
  193. PRG_ETH0_EXT_RGMII_MODE);
  194. break;
  195. case PHY_INTERFACE_MODE_RMII:
  196. /* disable RGMII mode -> enables RMII mode */
  197. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
  198. PRG_ETH0_EXT_PHY_MODE_MASK,
  199. PRG_ETH0_EXT_RMII_MODE);
  200. break;
  201. default:
  202. dev_err(dwmac->dev, "fail to set phy-mode %s\n",
  203. phy_modes(dwmac->phy_mode));
  204. return -EINVAL;
  205. }
  206. return 0;
  207. }
  208. static int meson8b_init_prg_eth(struct meson8b_dwmac *dwmac)
  209. {
  210. int ret;
  211. u8 tx_dly_val = 0;
  212. switch (dwmac->phy_mode) {
  213. case PHY_INTERFACE_MODE_RGMII:
  214. case PHY_INTERFACE_MODE_RGMII_RXID:
  215. /* TX clock delay in ns = "8ns / 4 * tx_dly_val" (where
  216. * 8ns are exactly one cycle of the 125MHz RGMII TX clock):
  217. * 0ns = 0x0, 2ns = 0x1, 4ns = 0x2, 6ns = 0x3
  218. */
  219. tx_dly_val = dwmac->tx_delay_ns >> 1;
  220. /* fall through */
  221. case PHY_INTERFACE_MODE_RGMII_ID:
  222. case PHY_INTERFACE_MODE_RGMII_TXID:
  223. /* only relevant for RMII mode -> disable in RGMII mode */
  224. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
  225. PRG_ETH0_INVERTED_RMII_CLK, 0);
  226. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK,
  227. tx_dly_val << PRG_ETH0_TXDLY_SHIFT);
  228. /* Configure the 125MHz RGMII TX clock, the IP block changes
  229. * the output automatically (= without us having to configure
  230. * a register) based on the line-speed (125MHz for Gbit speeds,
  231. * 25MHz for 100Mbit/s and 2.5MHz for 10Mbit/s).
  232. */
  233. ret = clk_set_rate(dwmac->rgmii_tx_clk, 125 * 1000 * 1000);
  234. if (ret) {
  235. dev_err(dwmac->dev,
  236. "failed to set RGMII TX clock\n");
  237. return ret;
  238. }
  239. ret = clk_prepare_enable(dwmac->rgmii_tx_clk);
  240. if (ret) {
  241. dev_err(dwmac->dev,
  242. "failed to enable the RGMII TX clock\n");
  243. return ret;
  244. }
  245. devm_add_action_or_reset(dwmac->dev,
  246. (void(*)(void *))clk_disable_unprepare,
  247. dwmac->rgmii_tx_clk);
  248. break;
  249. case PHY_INTERFACE_MODE_RMII:
  250. /* invert internal clk_rmii_i to generate 25/2.5 tx_rx_clk */
  251. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0,
  252. PRG_ETH0_INVERTED_RMII_CLK,
  253. PRG_ETH0_INVERTED_RMII_CLK);
  254. /* TX clock delay cannot be configured in RMII mode */
  255. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TXDLY_MASK,
  256. 0);
  257. break;
  258. default:
  259. dev_err(dwmac->dev, "unsupported phy-mode %s\n",
  260. phy_modes(dwmac->phy_mode));
  261. return -EINVAL;
  262. }
  263. /* enable TX_CLK and PHY_REF_CLK generator */
  264. meson8b_dwmac_mask_bits(dwmac, PRG_ETH0, PRG_ETH0_TX_AND_PHY_REF_CLK,
  265. PRG_ETH0_TX_AND_PHY_REF_CLK);
  266. return 0;
  267. }
  268. static int meson8b_dwmac_probe(struct platform_device *pdev)
  269. {
  270. struct plat_stmmacenet_data *plat_dat;
  271. struct stmmac_resources stmmac_res;
  272. struct resource *res;
  273. struct meson8b_dwmac *dwmac;
  274. int ret;
  275. ret = stmmac_get_platform_resources(pdev, &stmmac_res);
  276. if (ret)
  277. return ret;
  278. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  279. if (IS_ERR(plat_dat))
  280. return PTR_ERR(plat_dat);
  281. dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
  282. if (!dwmac) {
  283. ret = -ENOMEM;
  284. goto err_remove_config_dt;
  285. }
  286. dwmac->data = (const struct meson8b_dwmac_data *)
  287. of_device_get_match_data(&pdev->dev);
  288. if (!dwmac->data) {
  289. ret = -EINVAL;
  290. goto err_remove_config_dt;
  291. }
  292. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  293. dwmac->regs = devm_ioremap_resource(&pdev->dev, res);
  294. if (IS_ERR(dwmac->regs)) {
  295. ret = PTR_ERR(dwmac->regs);
  296. goto err_remove_config_dt;
  297. }
  298. dwmac->dev = &pdev->dev;
  299. dwmac->phy_mode = of_get_phy_mode(pdev->dev.of_node);
  300. if ((int)dwmac->phy_mode < 0) {
  301. dev_err(&pdev->dev, "missing phy-mode property\n");
  302. ret = -EINVAL;
  303. goto err_remove_config_dt;
  304. }
  305. /* use 2ns as fallback since this value was previously hardcoded */
  306. if (of_property_read_u32(pdev->dev.of_node, "amlogic,tx-delay-ns",
  307. &dwmac->tx_delay_ns))
  308. dwmac->tx_delay_ns = 2;
  309. ret = meson8b_init_rgmii_tx_clk(dwmac);
  310. if (ret)
  311. goto err_remove_config_dt;
  312. ret = dwmac->data->set_phy_mode(dwmac);
  313. if (ret)
  314. goto err_remove_config_dt;
  315. ret = meson8b_init_prg_eth(dwmac);
  316. if (ret)
  317. goto err_remove_config_dt;
  318. plat_dat->bsp_priv = dwmac;
  319. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  320. if (ret)
  321. goto err_remove_config_dt;
  322. return 0;
  323. err_remove_config_dt:
  324. stmmac_remove_config_dt(pdev, plat_dat);
  325. return ret;
  326. }
  327. static const struct meson8b_dwmac_data meson8b_dwmac_data = {
  328. .set_phy_mode = meson8b_set_phy_mode,
  329. };
  330. static const struct meson8b_dwmac_data meson_axg_dwmac_data = {
  331. .set_phy_mode = meson_axg_set_phy_mode,
  332. };
  333. static const struct of_device_id meson8b_dwmac_match[] = {
  334. {
  335. .compatible = "amlogic,meson8b-dwmac",
  336. .data = &meson8b_dwmac_data,
  337. },
  338. {
  339. .compatible = "amlogic,meson8m2-dwmac",
  340. .data = &meson8b_dwmac_data,
  341. },
  342. {
  343. .compatible = "amlogic,meson-gxbb-dwmac",
  344. .data = &meson8b_dwmac_data,
  345. },
  346. {
  347. .compatible = "amlogic,meson-axg-dwmac",
  348. .data = &meson_axg_dwmac_data,
  349. },
  350. { }
  351. };
  352. MODULE_DEVICE_TABLE(of, meson8b_dwmac_match);
  353. static struct platform_driver meson8b_dwmac_driver = {
  354. .probe = meson8b_dwmac_probe,
  355. .remove = stmmac_pltfr_remove,
  356. .driver = {
  357. .name = "meson8b-dwmac",
  358. .pm = &stmmac_pltfr_pm_ops,
  359. .of_match_table = meson8b_dwmac_match,
  360. },
  361. };
  362. module_platform_driver(meson8b_dwmac_driver);
  363. MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
  364. MODULE_DESCRIPTION("Amlogic Meson8b, Meson8m2 and GXBB DWMAC glue layer");
  365. MODULE_LICENSE("GPL v2");