mdio-mux-meson-g12a.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/clk.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/device.h>
  9. #include <linux/io.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/mdio-mux.h>
  12. #include <linux/module.h>
  13. #include <linux/phy.h>
  14. #include <linux/platform_device.h>
  15. #define ETH_PLL_STS 0x40
  16. #define ETH_PLL_CTL0 0x44
  17. #define PLL_CTL0_LOCK_DIG BIT(30)
  18. #define PLL_CTL0_RST BIT(29)
  19. #define PLL_CTL0_EN BIT(28)
  20. #define PLL_CTL0_SEL BIT(23)
  21. #define PLL_CTL0_N GENMASK(14, 10)
  22. #define PLL_CTL0_M GENMASK(8, 0)
  23. #define PLL_LOCK_TIMEOUT 1000000
  24. #define PLL_MUX_NUM_PARENT 2
  25. #define ETH_PLL_CTL1 0x48
  26. #define ETH_PLL_CTL2 0x4c
  27. #define ETH_PLL_CTL3 0x50
  28. #define ETH_PLL_CTL4 0x54
  29. #define ETH_PLL_CTL5 0x58
  30. #define ETH_PLL_CTL6 0x5c
  31. #define ETH_PLL_CTL7 0x60
  32. #define ETH_PHY_CNTL0 0x80
  33. #define EPHY_G12A_ID 0x33010180
  34. #define ETH_PHY_CNTL1 0x84
  35. #define PHY_CNTL1_ST_MODE GENMASK(2, 0)
  36. #define PHY_CNTL1_ST_PHYADD GENMASK(7, 3)
  37. #define EPHY_DFLT_ADD 8
  38. #define PHY_CNTL1_MII_MODE GENMASK(15, 14)
  39. #define EPHY_MODE_RMII 0x1
  40. #define PHY_CNTL1_CLK_EN BIT(16)
  41. #define PHY_CNTL1_CLKFREQ BIT(17)
  42. #define PHY_CNTL1_PHY_ENB BIT(18)
  43. #define ETH_PHY_CNTL2 0x88
  44. #define PHY_CNTL2_USE_INTERNAL BIT(5)
  45. #define PHY_CNTL2_SMI_SRC_MAC BIT(6)
  46. #define PHY_CNTL2_RX_CLK_EPHY BIT(9)
  47. #define MESON_G12A_MDIO_EXTERNAL_ID 0
  48. #define MESON_G12A_MDIO_INTERNAL_ID 1
  49. struct g12a_mdio_mux {
  50. bool pll_is_enabled;
  51. void __iomem *regs;
  52. void *mux_handle;
  53. struct clk *pclk;
  54. struct clk *pll;
  55. };
  56. struct g12a_ephy_pll {
  57. void __iomem *base;
  58. struct clk_hw hw;
  59. };
  60. #define g12a_ephy_pll_to_dev(_hw) \
  61. container_of(_hw, struct g12a_ephy_pll, hw)
  62. static unsigned long g12a_ephy_pll_recalc_rate(struct clk_hw *hw,
  63. unsigned long parent_rate)
  64. {
  65. struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
  66. u32 val, m, n;
  67. val = readl(pll->base + ETH_PLL_CTL0);
  68. m = FIELD_GET(PLL_CTL0_M, val);
  69. n = FIELD_GET(PLL_CTL0_N, val);
  70. return parent_rate * m / n;
  71. }
  72. static int g12a_ephy_pll_enable(struct clk_hw *hw)
  73. {
  74. struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
  75. u32 val = readl(pll->base + ETH_PLL_CTL0);
  76. /* Apply both enable an reset */
  77. val |= PLL_CTL0_RST | PLL_CTL0_EN;
  78. writel(val, pll->base + ETH_PLL_CTL0);
  79. /* Clear the reset to let PLL lock */
  80. val &= ~PLL_CTL0_RST;
  81. writel(val, pll->base + ETH_PLL_CTL0);
  82. /* Poll on the digital lock instead of the usual analog lock
  83. * This is done because bit 31 is unreliable on some SoC. Bit
  84. * 31 may indicate that the PLL is not lock eventhough the clock
  85. * is actually running
  86. */
  87. return readl_poll_timeout(pll->base + ETH_PLL_CTL0, val,
  88. val & PLL_CTL0_LOCK_DIG, 0, PLL_LOCK_TIMEOUT);
  89. }
  90. static void g12a_ephy_pll_disable(struct clk_hw *hw)
  91. {
  92. struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
  93. u32 val;
  94. val = readl(pll->base + ETH_PLL_CTL0);
  95. val &= ~PLL_CTL0_EN;
  96. val |= PLL_CTL0_RST;
  97. writel(val, pll->base + ETH_PLL_CTL0);
  98. }
  99. static int g12a_ephy_pll_is_enabled(struct clk_hw *hw)
  100. {
  101. struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
  102. unsigned int val;
  103. val = readl(pll->base + ETH_PLL_CTL0);
  104. return (val & PLL_CTL0_LOCK_DIG) ? 1 : 0;
  105. }
  106. static void g12a_ephy_pll_init(struct clk_hw *hw)
  107. {
  108. struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
  109. /* Apply PLL HW settings */
  110. writel(0x29c0040a, pll->base + ETH_PLL_CTL0);
  111. writel(0x927e0000, pll->base + ETH_PLL_CTL1);
  112. writel(0xac5f49e5, pll->base + ETH_PLL_CTL2);
  113. writel(0x00000000, pll->base + ETH_PLL_CTL3);
  114. writel(0x00000000, pll->base + ETH_PLL_CTL4);
  115. writel(0x20200000, pll->base + ETH_PLL_CTL5);
  116. writel(0x0000c002, pll->base + ETH_PLL_CTL6);
  117. writel(0x00000023, pll->base + ETH_PLL_CTL7);
  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. int ret;
  129. /* Enable the phy clock */
  130. if (!priv->pll_is_enabled) {
  131. ret = clk_prepare_enable(priv->pll);
  132. if (ret)
  133. return ret;
  134. }
  135. priv->pll_is_enabled = true;
  136. /* Initialize ephy control */
  137. writel(EPHY_G12A_ID, priv->regs + ETH_PHY_CNTL0);
  138. writel(FIELD_PREP(PHY_CNTL1_ST_MODE, 3) |
  139. FIELD_PREP(PHY_CNTL1_ST_PHYADD, EPHY_DFLT_ADD) |
  140. FIELD_PREP(PHY_CNTL1_MII_MODE, EPHY_MODE_RMII) |
  141. PHY_CNTL1_CLK_EN |
  142. PHY_CNTL1_CLKFREQ |
  143. PHY_CNTL1_PHY_ENB,
  144. 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. return 0;
  150. }
  151. static int g12a_enable_external_mdio(struct g12a_mdio_mux *priv)
  152. {
  153. /* Reset the mdio bus mux */
  154. writel_relaxed(0x0, priv->regs + ETH_PHY_CNTL2);
  155. /* Disable the phy clock if enabled */
  156. if (priv->pll_is_enabled) {
  157. clk_disable_unprepare(priv->pll);
  158. priv->pll_is_enabled = false;
  159. }
  160. return 0;
  161. }
  162. static int g12a_mdio_switch_fn(int current_child, int desired_child,
  163. void *data)
  164. {
  165. struct g12a_mdio_mux *priv = dev_get_drvdata(data);
  166. if (current_child == desired_child)
  167. return 0;
  168. switch (desired_child) {
  169. case MESON_G12A_MDIO_EXTERNAL_ID:
  170. return g12a_enable_external_mdio(priv);
  171. case MESON_G12A_MDIO_INTERNAL_ID:
  172. return g12a_enable_internal_mdio(priv);
  173. default:
  174. return -EINVAL;
  175. }
  176. }
  177. static const struct of_device_id g12a_mdio_mux_match[] = {
  178. { .compatible = "amlogic,g12a-mdio-mux", },
  179. {},
  180. };
  181. MODULE_DEVICE_TABLE(of, g12a_mdio_mux_match);
  182. static int g12a_ephy_glue_clk_register(struct device *dev)
  183. {
  184. struct g12a_mdio_mux *priv = dev_get_drvdata(dev);
  185. const char *parent_names[PLL_MUX_NUM_PARENT];
  186. struct clk_init_data init;
  187. struct g12a_ephy_pll *pll;
  188. struct clk_mux *mux;
  189. struct clk *clk;
  190. char *name;
  191. int i;
  192. /* get the mux parents */
  193. for (i = 0; i < PLL_MUX_NUM_PARENT; i++) {
  194. char in_name[8];
  195. snprintf(in_name, sizeof(in_name), "clkin%d", i);
  196. clk = devm_clk_get(dev, in_name);
  197. if (IS_ERR(clk)) {
  198. if (PTR_ERR(clk) != -EPROBE_DEFER)
  199. dev_err(dev, "Missing clock %s\n", in_name);
  200. return PTR_ERR(clk);
  201. }
  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. int ret;
  255. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  256. if (!priv)
  257. return -ENOMEM;
  258. platform_set_drvdata(pdev, priv);
  259. priv->regs = devm_platform_ioremap_resource(pdev, 0);
  260. if (IS_ERR(priv->regs))
  261. return PTR_ERR(priv->regs);
  262. priv->pclk = devm_clk_get(dev, "pclk");
  263. if (IS_ERR(priv->pclk)) {
  264. ret = PTR_ERR(priv->pclk);
  265. if (ret != -EPROBE_DEFER)
  266. dev_err(dev, "failed to get peripheral clock\n");
  267. return ret;
  268. }
  269. /* Make sure the device registers are clocked */
  270. ret = clk_prepare_enable(priv->pclk);
  271. if (ret) {
  272. dev_err(dev, "failed to enable peripheral clock");
  273. return ret;
  274. }
  275. /* Register PLL in CCF */
  276. ret = g12a_ephy_glue_clk_register(dev);
  277. if (ret)
  278. goto err;
  279. ret = mdio_mux_init(dev, dev->of_node, g12a_mdio_switch_fn,
  280. &priv->mux_handle, dev, NULL);
  281. if (ret) {
  282. if (ret != -EPROBE_DEFER)
  283. dev_err(dev, "mdio multiplexer init failed: %d", ret);
  284. goto err;
  285. }
  286. return 0;
  287. err:
  288. clk_disable_unprepare(priv->pclk);
  289. return ret;
  290. }
  291. static int g12a_mdio_mux_remove(struct platform_device *pdev)
  292. {
  293. struct g12a_mdio_mux *priv = platform_get_drvdata(pdev);
  294. mdio_mux_uninit(priv->mux_handle);
  295. if (priv->pll_is_enabled)
  296. clk_disable_unprepare(priv->pll);
  297. clk_disable_unprepare(priv->pclk);
  298. return 0;
  299. }
  300. static struct platform_driver g12a_mdio_mux_driver = {
  301. .probe = g12a_mdio_mux_probe,
  302. .remove = g12a_mdio_mux_remove,
  303. .driver = {
  304. .name = "g12a-mdio_mux",
  305. .of_match_table = g12a_mdio_mux_match,
  306. },
  307. };
  308. module_platform_driver(g12a_mdio_mux_driver);
  309. MODULE_DESCRIPTION("Amlogic G12a MDIO multiplexer driver");
  310. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  311. MODULE_LICENSE("GPL v2");