mdio-mux-meson-g12a.c 9.2 KB

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