mdio-mux-meson-g12a.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019 Baylibre, SAS.
  3. * Author: Jerome Brunet <jbrunet@baylibre.com>
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/delay.h>
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/device.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/mdio-mux.h>
  13. #include <linux/module.h>
  14. #include <linux/phy.h>
  15. #include <linux/platform_device.h>
  16. #define ETH_PLL_STS 0x40
  17. #define ETH_PLL_CTL0 0x44
  18. #define PLL_CTL0_LOCK_DIG BIT(30)
  19. #define PLL_CTL0_RST BIT(29)
  20. #define PLL_CTL0_EN BIT(28)
  21. #define PLL_CTL0_SEL BIT(23)
  22. #define PLL_CTL0_N GENMASK(14, 10)
  23. #define PLL_CTL0_M GENMASK(8, 0)
  24. #define PLL_LOCK_TIMEOUT 1000000
  25. #define PLL_MUX_NUM_PARENT 2
  26. #define ETH_PLL_CTL1 0x48
  27. #define ETH_PLL_CTL2 0x4c
  28. #define ETH_PLL_CTL3 0x50
  29. #define ETH_PLL_CTL4 0x54
  30. #define ETH_PLL_CTL5 0x58
  31. #define ETH_PLL_CTL6 0x5c
  32. #define ETH_PLL_CTL7 0x60
  33. #define ETH_PHY_CNTL0 0x80
  34. #define EPHY_G12A_ID 0x33010180
  35. #define ETH_PHY_CNTL1 0x84
  36. #define PHY_CNTL1_ST_MODE GENMASK(2, 0)
  37. #define PHY_CNTL1_ST_PHYADD GENMASK(7, 3)
  38. #define EPHY_DFLT_ADD 8
  39. #define PHY_CNTL1_MII_MODE GENMASK(15, 14)
  40. #define EPHY_MODE_RMII 0x1
  41. #define PHY_CNTL1_CLK_EN BIT(16)
  42. #define PHY_CNTL1_CLKFREQ BIT(17)
  43. #define PHY_CNTL1_PHY_ENB BIT(18)
  44. #define ETH_PHY_CNTL2 0x88
  45. #define PHY_CNTL2_USE_INTERNAL BIT(5)
  46. #define PHY_CNTL2_SMI_SRC_MAC BIT(6)
  47. #define PHY_CNTL2_RX_CLK_EPHY BIT(9)
  48. #define MESON_G12A_MDIO_EXTERNAL_ID 0
  49. #define MESON_G12A_MDIO_INTERNAL_ID 1
  50. struct g12a_mdio_mux {
  51. void __iomem *regs;
  52. void *mux_handle;
  53. struct clk *pll;
  54. };
  55. struct g12a_ephy_pll {
  56. void __iomem *base;
  57. struct clk_hw hw;
  58. };
  59. #define g12a_ephy_pll_to_dev(_hw) \
  60. container_of(_hw, struct g12a_ephy_pll, hw)
  61. static unsigned long g12a_ephy_pll_recalc_rate(struct clk_hw *hw,
  62. unsigned long parent_rate)
  63. {
  64. struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
  65. u32 val, m, n;
  66. val = readl(pll->base + ETH_PLL_CTL0);
  67. m = FIELD_GET(PLL_CTL0_M, val);
  68. n = FIELD_GET(PLL_CTL0_N, val);
  69. return parent_rate * m / n;
  70. }
  71. static int g12a_ephy_pll_enable(struct clk_hw *hw)
  72. {
  73. struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
  74. u32 val = readl(pll->base + ETH_PLL_CTL0);
  75. /* Apply both enable an reset */
  76. val |= PLL_CTL0_RST | PLL_CTL0_EN;
  77. writel(val, pll->base + ETH_PLL_CTL0);
  78. /* Clear the reset to let PLL lock */
  79. val &= ~PLL_CTL0_RST;
  80. writel(val, pll->base + ETH_PLL_CTL0);
  81. /* Poll on the digital lock instead of the usual analog lock
  82. * This is done because bit 31 is unreliable on some SoC. Bit
  83. * 31 may indicate that the PLL is not lock even though the clock
  84. * is actually running
  85. */
  86. return readl_poll_timeout(pll->base + ETH_PLL_CTL0, val,
  87. val & PLL_CTL0_LOCK_DIG, 0, PLL_LOCK_TIMEOUT);
  88. }
  89. static void g12a_ephy_pll_disable(struct clk_hw *hw)
  90. {
  91. struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
  92. u32 val;
  93. val = readl(pll->base + ETH_PLL_CTL0);
  94. val &= ~PLL_CTL0_EN;
  95. val |= PLL_CTL0_RST;
  96. writel(val, pll->base + ETH_PLL_CTL0);
  97. }
  98. static int g12a_ephy_pll_is_enabled(struct clk_hw *hw)
  99. {
  100. struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
  101. unsigned int val;
  102. val = readl(pll->base + ETH_PLL_CTL0);
  103. return (val & PLL_CTL0_LOCK_DIG) ? 1 : 0;
  104. }
  105. static int g12a_ephy_pll_init(struct clk_hw *hw)
  106. {
  107. struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
  108. /* Apply PLL HW settings */
  109. writel(0x29c0040a, pll->base + ETH_PLL_CTL0);
  110. writel(0x927e0000, pll->base + ETH_PLL_CTL1);
  111. writel(0xac5f49e5, pll->base + ETH_PLL_CTL2);
  112. writel(0x00000000, pll->base + ETH_PLL_CTL3);
  113. writel(0x00000000, pll->base + ETH_PLL_CTL4);
  114. writel(0x20200000, pll->base + ETH_PLL_CTL5);
  115. writel(0x0000c002, pll->base + ETH_PLL_CTL6);
  116. writel(0x00000023, pll->base + ETH_PLL_CTL7);
  117. return 0;
  118. }
  119. static const struct clk_ops g12a_ephy_pll_ops = {
  120. .recalc_rate = g12a_ephy_pll_recalc_rate,
  121. .is_enabled = g12a_ephy_pll_is_enabled,
  122. .enable = g12a_ephy_pll_enable,
  123. .disable = g12a_ephy_pll_disable,
  124. .init = g12a_ephy_pll_init,
  125. };
  126. static int g12a_enable_internal_mdio(struct g12a_mdio_mux *priv)
  127. {
  128. u32 value;
  129. int ret;
  130. /* Enable the phy clock */
  131. if (!__clk_is_enabled(priv->pll)) {
  132. ret = clk_prepare_enable(priv->pll);
  133. if (ret)
  134. return ret;
  135. }
  136. /* Initialize ephy control */
  137. writel(EPHY_G12A_ID, priv->regs + ETH_PHY_CNTL0);
  138. /* Make sure we get a 0 -> 1 transition on the enable bit */
  139. value = FIELD_PREP(PHY_CNTL1_ST_MODE, 3) |
  140. FIELD_PREP(PHY_CNTL1_ST_PHYADD, EPHY_DFLT_ADD) |
  141. FIELD_PREP(PHY_CNTL1_MII_MODE, EPHY_MODE_RMII) |
  142. PHY_CNTL1_CLK_EN |
  143. PHY_CNTL1_CLKFREQ;
  144. writel(value, priv->regs + ETH_PHY_CNTL1);
  145. writel(PHY_CNTL2_USE_INTERNAL |
  146. PHY_CNTL2_SMI_SRC_MAC |
  147. PHY_CNTL2_RX_CLK_EPHY,
  148. priv->regs + ETH_PHY_CNTL2);
  149. value |= PHY_CNTL1_PHY_ENB;
  150. writel(value, priv->regs + ETH_PHY_CNTL1);
  151. /* The phy needs a bit of time to power up */
  152. mdelay(10);
  153. return 0;
  154. }
  155. static int g12a_enable_external_mdio(struct g12a_mdio_mux *priv)
  156. {
  157. /* Reset the mdio bus mux */
  158. writel_relaxed(0x0, priv->regs + ETH_PHY_CNTL2);
  159. /* Disable the phy clock if enabled */
  160. if (__clk_is_enabled(priv->pll))
  161. clk_disable_unprepare(priv->pll);
  162. return 0;
  163. }
  164. static int g12a_mdio_switch_fn(int current_child, int desired_child,
  165. void *data)
  166. {
  167. struct g12a_mdio_mux *priv = dev_get_drvdata(data);
  168. if (current_child == desired_child)
  169. return 0;
  170. switch (desired_child) {
  171. case MESON_G12A_MDIO_EXTERNAL_ID:
  172. return g12a_enable_external_mdio(priv);
  173. case MESON_G12A_MDIO_INTERNAL_ID:
  174. return g12a_enable_internal_mdio(priv);
  175. default:
  176. return -EINVAL;
  177. }
  178. }
  179. static const struct of_device_id g12a_mdio_mux_match[] = {
  180. { .compatible = "amlogic,g12a-mdio-mux", },
  181. {},
  182. };
  183. MODULE_DEVICE_TABLE(of, g12a_mdio_mux_match);
  184. static int g12a_ephy_glue_clk_register(struct device *dev)
  185. {
  186. struct g12a_mdio_mux *priv = dev_get_drvdata(dev);
  187. const char *parent_names[PLL_MUX_NUM_PARENT];
  188. struct clk_init_data init;
  189. struct g12a_ephy_pll *pll;
  190. struct clk_mux *mux;
  191. struct clk *clk;
  192. char *name;
  193. int i;
  194. /* get the mux parents */
  195. for (i = 0; i < PLL_MUX_NUM_PARENT; i++) {
  196. char in_name[8];
  197. snprintf(in_name, sizeof(in_name), "clkin%d", i);
  198. clk = devm_clk_get(dev, in_name);
  199. if (IS_ERR(clk))
  200. return dev_err_probe(dev, PTR_ERR(clk),
  201. "Missing clock %s\n", in_name);
  202. parent_names[i] = __clk_get_name(clk);
  203. }
  204. /* create the input mux */
  205. mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
  206. if (!mux)
  207. return -ENOMEM;
  208. name = kasprintf(GFP_KERNEL, "%s#mux", dev_name(dev));
  209. if (!name)
  210. return -ENOMEM;
  211. init.name = name;
  212. init.ops = &clk_mux_ro_ops;
  213. init.flags = 0;
  214. init.parent_names = parent_names;
  215. init.num_parents = PLL_MUX_NUM_PARENT;
  216. mux->reg = priv->regs + ETH_PLL_CTL0;
  217. mux->shift = __ffs(PLL_CTL0_SEL);
  218. mux->mask = PLL_CTL0_SEL >> mux->shift;
  219. mux->hw.init = &init;
  220. clk = devm_clk_register(dev, &mux->hw);
  221. kfree(name);
  222. if (IS_ERR(clk)) {
  223. dev_err(dev, "failed to register input mux\n");
  224. return PTR_ERR(clk);
  225. }
  226. /* create the pll */
  227. pll = devm_kzalloc(dev, sizeof(*pll), GFP_KERNEL);
  228. if (!pll)
  229. return -ENOMEM;
  230. name = kasprintf(GFP_KERNEL, "%s#pll", dev_name(dev));
  231. if (!name)
  232. return -ENOMEM;
  233. init.name = name;
  234. init.ops = &g12a_ephy_pll_ops;
  235. init.flags = 0;
  236. parent_names[0] = __clk_get_name(clk);
  237. init.parent_names = parent_names;
  238. init.num_parents = 1;
  239. pll->base = priv->regs;
  240. pll->hw.init = &init;
  241. clk = devm_clk_register(dev, &pll->hw);
  242. kfree(name);
  243. if (IS_ERR(clk)) {
  244. dev_err(dev, "failed to register input mux\n");
  245. return PTR_ERR(clk);
  246. }
  247. priv->pll = clk;
  248. return 0;
  249. }
  250. static int g12a_mdio_mux_probe(struct platform_device *pdev)
  251. {
  252. struct device *dev = &pdev->dev;
  253. struct g12a_mdio_mux *priv;
  254. struct clk *pclk;
  255. int ret;
  256. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  257. if (!priv)
  258. return -ENOMEM;
  259. platform_set_drvdata(pdev, priv);
  260. priv->regs = devm_platform_ioremap_resource(pdev, 0);
  261. if (IS_ERR(priv->regs))
  262. return PTR_ERR(priv->regs);
  263. pclk = devm_clk_get_enabled(dev, "pclk");
  264. if (IS_ERR(pclk))
  265. return dev_err_probe(dev, PTR_ERR(pclk),
  266. "failed to get peripheral clock\n");
  267. /* Register PLL in CCF */
  268. ret = g12a_ephy_glue_clk_register(dev);
  269. if (ret)
  270. return ret;
  271. ret = mdio_mux_init(dev, dev->of_node, g12a_mdio_switch_fn,
  272. &priv->mux_handle, dev, NULL);
  273. if (ret)
  274. dev_err_probe(dev, ret, "mdio multiplexer init failed\n");
  275. return ret;
  276. }
  277. static void g12a_mdio_mux_remove(struct platform_device *pdev)
  278. {
  279. struct g12a_mdio_mux *priv = platform_get_drvdata(pdev);
  280. mdio_mux_uninit(priv->mux_handle);
  281. if (__clk_is_enabled(priv->pll))
  282. clk_disable_unprepare(priv->pll);
  283. }
  284. static struct platform_driver g12a_mdio_mux_driver = {
  285. .probe = g12a_mdio_mux_probe,
  286. .remove_new = g12a_mdio_mux_remove,
  287. .driver = {
  288. .name = "g12a-mdio_mux",
  289. .of_match_table = g12a_mdio_mux_match,
  290. },
  291. };
  292. module_platform_driver(g12a_mdio_mux_driver);
  293. MODULE_DESCRIPTION("Amlogic G12a MDIO multiplexer driver");
  294. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  295. MODULE_LICENSE("GPL v2");