ipq-cmn-pll.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2024-2025 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. /*
  6. * CMN PLL block expects the reference clock from on-board Wi-Fi block,
  7. * and supplies fixed rate clocks as output to the networking hardware
  8. * blocks and to GCC. The networking related blocks include PPE (packet
  9. * process engine), the externally connected PHY or switch devices, and
  10. * the PCS.
  11. *
  12. * On the IPQ9574 SoC, there are three clocks with 50 MHZ and one clock
  13. * with 25 MHZ which are output from the CMN PLL to Ethernet PHY (or switch),
  14. * and one clock with 353 MHZ to PPE. The other fixed rate output clocks
  15. * are supplied to GCC (24 MHZ as XO and 32 KHZ as sleep clock), and to PCS
  16. * with 31.25 MHZ.
  17. *
  18. * On the IPQ5424 SoC, there is an output clock from CMN PLL to PPE at 375 MHZ,
  19. * and an output clock to NSS (network subsystem) at 300 MHZ. The other output
  20. * clocks from CMN PLL on IPQ5424 are the same as IPQ9574.
  21. *
  22. * +---------+
  23. * | GCC |
  24. * +--+---+--+
  25. * AHB CLK| |SYS CLK
  26. * V V
  27. * +-------+---+------+
  28. * | +-------------> eth0-50mhz
  29. * REF CLK | IPQ9574 |
  30. * -------->+ +-------------> eth1-50mhz
  31. * | CMN PLL block |
  32. * | +-------------> eth2-50mhz
  33. * | |
  34. * +----+----+----+---+-------------> eth-25mhz
  35. * | | |
  36. * V V V
  37. * GCC PCS NSS/PPE
  38. */
  39. #include <linux/bitfield.h>
  40. #include <linux/clk-provider.h>
  41. #include <linux/delay.h>
  42. #include <linux/err.h>
  43. #include <linux/mod_devicetable.h>
  44. #include <linux/module.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/pm_clock.h>
  47. #include <linux/pm_runtime.h>
  48. #include <linux/regmap.h>
  49. #include <dt-bindings/clock/qcom,ipq-cmn-pll.h>
  50. #include <dt-bindings/clock/qcom,ipq5018-cmn-pll.h>
  51. #include <dt-bindings/clock/qcom,ipq5424-cmn-pll.h>
  52. #define CMN_PLL_REFCLK_SRC_SELECTION 0x28
  53. #define CMN_PLL_REFCLK_SRC_DIV GENMASK(9, 8)
  54. #define CMN_PLL_LOCKED 0x64
  55. #define CMN_PLL_CLKS_LOCKED BIT(8)
  56. #define CMN_PLL_POWER_ON_AND_RESET 0x780
  57. #define CMN_ANA_EN_SW_RSTN BIT(6)
  58. #define CMN_PLL_REFCLK_CONFIG 0x784
  59. #define CMN_PLL_REFCLK_EXTERNAL BIT(9)
  60. #define CMN_PLL_REFCLK_DIV GENMASK(8, 4)
  61. #define CMN_PLL_REFCLK_INDEX GENMASK(3, 0)
  62. #define CMN_PLL_CTRL 0x78c
  63. #define CMN_PLL_CTRL_LOCK_DETECT_EN BIT(15)
  64. #define CMN_PLL_DIVIDER_CTRL 0x794
  65. #define CMN_PLL_DIVIDER_CTRL_FACTOR GENMASK(9, 0)
  66. /**
  67. * struct cmn_pll_fixed_output_clk - CMN PLL output clocks information
  68. * @id: Clock specifier to be supplied
  69. * @name: Clock name to be registered
  70. * @rate: Clock rate
  71. */
  72. struct cmn_pll_fixed_output_clk {
  73. unsigned int id;
  74. const char *name;
  75. unsigned long rate;
  76. };
  77. /**
  78. * struct clk_cmn_pll - CMN PLL hardware specific data
  79. * @regmap: hardware regmap.
  80. * @hw: handle between common and hardware-specific interfaces
  81. */
  82. struct clk_cmn_pll {
  83. struct regmap *regmap;
  84. struct clk_hw hw;
  85. };
  86. #define CLK_PLL_OUTPUT(_id, _name, _rate) { \
  87. .id = _id, \
  88. .name = _name, \
  89. .rate = _rate, \
  90. }
  91. #define to_clk_cmn_pll(_hw) container_of(_hw, struct clk_cmn_pll, hw)
  92. static const struct regmap_config ipq_cmn_pll_regmap_config = {
  93. .reg_bits = 32,
  94. .reg_stride = 4,
  95. .val_bits = 32,
  96. .max_register = 0x7fc,
  97. };
  98. static const struct cmn_pll_fixed_output_clk ipq5018_output_clks[] = {
  99. CLK_PLL_OUTPUT(IPQ5018_XO_24MHZ_CLK, "xo-24mhz", 24000000UL),
  100. CLK_PLL_OUTPUT(IPQ5018_SLEEP_32KHZ_CLK, "sleep-32khz", 32000UL),
  101. CLK_PLL_OUTPUT(IPQ5018_ETH_50MHZ_CLK, "eth-50mhz", 50000000UL),
  102. { /* Sentinel */ }
  103. };
  104. static const struct cmn_pll_fixed_output_clk ipq5424_output_clks[] = {
  105. CLK_PLL_OUTPUT(IPQ5424_XO_24MHZ_CLK, "xo-24mhz", 24000000UL),
  106. CLK_PLL_OUTPUT(IPQ5424_SLEEP_32KHZ_CLK, "sleep-32khz", 32000UL),
  107. CLK_PLL_OUTPUT(IPQ5424_PCS_31P25MHZ_CLK, "pcs-31p25mhz", 31250000UL),
  108. CLK_PLL_OUTPUT(IPQ5424_NSS_300MHZ_CLK, "nss-300mhz", 300000000UL),
  109. CLK_PLL_OUTPUT(IPQ5424_PPE_375MHZ_CLK, "ppe-375mhz", 375000000UL),
  110. CLK_PLL_OUTPUT(IPQ5424_ETH0_50MHZ_CLK, "eth0-50mhz", 50000000UL),
  111. CLK_PLL_OUTPUT(IPQ5424_ETH1_50MHZ_CLK, "eth1-50mhz", 50000000UL),
  112. CLK_PLL_OUTPUT(IPQ5424_ETH2_50MHZ_CLK, "eth2-50mhz", 50000000UL),
  113. CLK_PLL_OUTPUT(IPQ5424_ETH_25MHZ_CLK, "eth-25mhz", 25000000UL),
  114. { /* Sentinel */ }
  115. };
  116. static const struct cmn_pll_fixed_output_clk ipq9574_output_clks[] = {
  117. CLK_PLL_OUTPUT(XO_24MHZ_CLK, "xo-24mhz", 24000000UL),
  118. CLK_PLL_OUTPUT(SLEEP_32KHZ_CLK, "sleep-32khz", 32000UL),
  119. CLK_PLL_OUTPUT(PCS_31P25MHZ_CLK, "pcs-31p25mhz", 31250000UL),
  120. CLK_PLL_OUTPUT(NSS_1200MHZ_CLK, "nss-1200mhz", 1200000000UL),
  121. CLK_PLL_OUTPUT(PPE_353MHZ_CLK, "ppe-353mhz", 353000000UL),
  122. CLK_PLL_OUTPUT(ETH0_50MHZ_CLK, "eth0-50mhz", 50000000UL),
  123. CLK_PLL_OUTPUT(ETH1_50MHZ_CLK, "eth1-50mhz", 50000000UL),
  124. CLK_PLL_OUTPUT(ETH2_50MHZ_CLK, "eth2-50mhz", 50000000UL),
  125. CLK_PLL_OUTPUT(ETH_25MHZ_CLK, "eth-25mhz", 25000000UL),
  126. { /* Sentinel */ }
  127. };
  128. /*
  129. * CMN PLL has the single parent clock, which supports the several
  130. * possible parent clock rates, each parent clock rate is reflected
  131. * by the specific reference index value in the hardware.
  132. */
  133. static int ipq_cmn_pll_find_freq_index(unsigned long parent_rate)
  134. {
  135. int index = -EINVAL;
  136. switch (parent_rate) {
  137. case 25000000:
  138. index = 3;
  139. break;
  140. case 31250000:
  141. index = 4;
  142. break;
  143. case 40000000:
  144. index = 6;
  145. break;
  146. case 48000000:
  147. case 96000000:
  148. /*
  149. * Parent clock rate 48 MHZ and 96 MHZ take the same value
  150. * of reference clock index. 96 MHZ needs the source clock
  151. * divider to be programmed as 2.
  152. */
  153. index = 7;
  154. break;
  155. case 50000000:
  156. index = 8;
  157. break;
  158. default:
  159. break;
  160. }
  161. return index;
  162. }
  163. static unsigned long clk_cmn_pll_recalc_rate(struct clk_hw *hw,
  164. unsigned long parent_rate)
  165. {
  166. struct clk_cmn_pll *cmn_pll = to_clk_cmn_pll(hw);
  167. u32 val, factor;
  168. /*
  169. * The value of CMN_PLL_DIVIDER_CTRL_FACTOR is automatically adjusted
  170. * by HW according to the parent clock rate.
  171. */
  172. regmap_read(cmn_pll->regmap, CMN_PLL_DIVIDER_CTRL, &val);
  173. factor = FIELD_GET(CMN_PLL_DIVIDER_CTRL_FACTOR, val);
  174. return parent_rate * 2 * factor;
  175. }
  176. static int clk_cmn_pll_determine_rate(struct clk_hw *hw,
  177. struct clk_rate_request *req)
  178. {
  179. int ret;
  180. /* Validate the rate of the single parent clock. */
  181. ret = ipq_cmn_pll_find_freq_index(req->best_parent_rate);
  182. return ret < 0 ? ret : 0;
  183. }
  184. /*
  185. * This function is used to initialize the CMN PLL to enable the fixed
  186. * rate output clocks. It is expected to be configured once.
  187. */
  188. static int clk_cmn_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  189. unsigned long parent_rate)
  190. {
  191. struct clk_cmn_pll *cmn_pll = to_clk_cmn_pll(hw);
  192. int ret, index;
  193. u32 val;
  194. /*
  195. * Configure the reference input clock selection as per the given
  196. * parent clock. The output clock rates are always of fixed value.
  197. */
  198. index = ipq_cmn_pll_find_freq_index(parent_rate);
  199. if (index < 0)
  200. return index;
  201. ret = regmap_update_bits(cmn_pll->regmap, CMN_PLL_REFCLK_CONFIG,
  202. CMN_PLL_REFCLK_INDEX,
  203. FIELD_PREP(CMN_PLL_REFCLK_INDEX, index));
  204. if (ret)
  205. return ret;
  206. /*
  207. * Update the source clock rate selection and source clock
  208. * divider as 2 when the parent clock rate is 96 MHZ.
  209. */
  210. if (parent_rate == 96000000) {
  211. ret = regmap_update_bits(cmn_pll->regmap, CMN_PLL_REFCLK_CONFIG,
  212. CMN_PLL_REFCLK_DIV,
  213. FIELD_PREP(CMN_PLL_REFCLK_DIV, 2));
  214. if (ret)
  215. return ret;
  216. ret = regmap_update_bits(cmn_pll->regmap, CMN_PLL_REFCLK_SRC_SELECTION,
  217. CMN_PLL_REFCLK_SRC_DIV,
  218. FIELD_PREP(CMN_PLL_REFCLK_SRC_DIV, 0));
  219. if (ret)
  220. return ret;
  221. }
  222. /* Enable PLL locked detect. */
  223. ret = regmap_set_bits(cmn_pll->regmap, CMN_PLL_CTRL,
  224. CMN_PLL_CTRL_LOCK_DETECT_EN);
  225. if (ret)
  226. return ret;
  227. /*
  228. * Reset the CMN PLL block to ensure the updated configurations
  229. * take effect.
  230. */
  231. ret = regmap_clear_bits(cmn_pll->regmap, CMN_PLL_POWER_ON_AND_RESET,
  232. CMN_ANA_EN_SW_RSTN);
  233. if (ret)
  234. return ret;
  235. usleep_range(1000, 1200);
  236. ret = regmap_set_bits(cmn_pll->regmap, CMN_PLL_POWER_ON_AND_RESET,
  237. CMN_ANA_EN_SW_RSTN);
  238. if (ret)
  239. return ret;
  240. /* Stability check of CMN PLL output clocks. */
  241. return regmap_read_poll_timeout(cmn_pll->regmap, CMN_PLL_LOCKED, val,
  242. (val & CMN_PLL_CLKS_LOCKED),
  243. 100, 100 * USEC_PER_MSEC);
  244. }
  245. static const struct clk_ops clk_cmn_pll_ops = {
  246. .recalc_rate = clk_cmn_pll_recalc_rate,
  247. .determine_rate = clk_cmn_pll_determine_rate,
  248. .set_rate = clk_cmn_pll_set_rate,
  249. };
  250. static struct clk_hw *ipq_cmn_pll_clk_hw_register(struct platform_device *pdev)
  251. {
  252. struct clk_parent_data pdata = { .index = 0 };
  253. struct device *dev = &pdev->dev;
  254. struct clk_init_data init = {};
  255. struct clk_cmn_pll *cmn_pll;
  256. struct regmap *regmap;
  257. void __iomem *base;
  258. int ret;
  259. base = devm_platform_ioremap_resource(pdev, 0);
  260. if (IS_ERR(base))
  261. return ERR_CAST(base);
  262. regmap = devm_regmap_init_mmio(dev, base, &ipq_cmn_pll_regmap_config);
  263. if (IS_ERR(regmap))
  264. return ERR_CAST(regmap);
  265. cmn_pll = devm_kzalloc(dev, sizeof(*cmn_pll), GFP_KERNEL);
  266. if (!cmn_pll)
  267. return ERR_PTR(-ENOMEM);
  268. init.name = "cmn_pll";
  269. init.parent_data = &pdata;
  270. init.num_parents = 1;
  271. init.ops = &clk_cmn_pll_ops;
  272. cmn_pll->hw.init = &init;
  273. cmn_pll->regmap = regmap;
  274. ret = devm_clk_hw_register(dev, &cmn_pll->hw);
  275. if (ret)
  276. return ERR_PTR(ret);
  277. return &cmn_pll->hw;
  278. }
  279. static int ipq_cmn_pll_register_clks(struct platform_device *pdev)
  280. {
  281. const struct cmn_pll_fixed_output_clk *p, *fixed_clk;
  282. struct clk_hw_onecell_data *hw_data;
  283. struct device *dev = &pdev->dev;
  284. struct clk_hw *cmn_pll_hw;
  285. unsigned int num_clks;
  286. struct clk_hw *hw;
  287. int ret, i;
  288. fixed_clk = device_get_match_data(dev);
  289. if (!fixed_clk)
  290. return -EINVAL;
  291. num_clks = 0;
  292. for (p = fixed_clk; p->name; p++)
  293. num_clks++;
  294. hw_data = devm_kzalloc(dev, struct_size(hw_data, hws, num_clks + 1),
  295. GFP_KERNEL);
  296. if (!hw_data)
  297. return -ENOMEM;
  298. /*
  299. * Register the CMN PLL clock, which is the parent clock of
  300. * the fixed rate output clocks.
  301. */
  302. cmn_pll_hw = ipq_cmn_pll_clk_hw_register(pdev);
  303. if (IS_ERR(cmn_pll_hw))
  304. return PTR_ERR(cmn_pll_hw);
  305. /* Register the fixed rate output clocks. */
  306. for (i = 0; i < num_clks; i++) {
  307. hw = clk_hw_register_fixed_rate_parent_hw(dev, fixed_clk[i].name,
  308. cmn_pll_hw, 0,
  309. fixed_clk[i].rate);
  310. if (IS_ERR(hw)) {
  311. ret = PTR_ERR(hw);
  312. goto unregister_fixed_clk;
  313. }
  314. hw_data->hws[fixed_clk[i].id] = hw;
  315. }
  316. /*
  317. * Provide the CMN PLL clock. The clock rate of CMN PLL
  318. * is configured to 12 GHZ by DT property assigned-clock-rates-u64.
  319. */
  320. hw_data->hws[CMN_PLL_CLK] = cmn_pll_hw;
  321. hw_data->num = num_clks + 1;
  322. ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, hw_data);
  323. if (ret)
  324. goto unregister_fixed_clk;
  325. platform_set_drvdata(pdev, hw_data);
  326. return 0;
  327. unregister_fixed_clk:
  328. while (i > 0)
  329. clk_hw_unregister(hw_data->hws[fixed_clk[--i].id]);
  330. return ret;
  331. }
  332. static int ipq_cmn_pll_clk_probe(struct platform_device *pdev)
  333. {
  334. struct device *dev = &pdev->dev;
  335. int ret;
  336. ret = devm_pm_runtime_enable(dev);
  337. if (ret)
  338. return ret;
  339. ret = devm_pm_clk_create(dev);
  340. if (ret)
  341. return ret;
  342. /*
  343. * To access the CMN PLL registers, the GCC AHB & SYS clocks
  344. * of CMN PLL block need to be enabled.
  345. */
  346. ret = pm_clk_add(dev, "ahb");
  347. if (ret)
  348. return dev_err_probe(dev, ret, "Failed to add AHB clock\n");
  349. ret = pm_clk_add(dev, "sys");
  350. if (ret)
  351. return dev_err_probe(dev, ret, "Failed to add SYS clock\n");
  352. ret = pm_runtime_resume_and_get(dev);
  353. if (ret)
  354. return ret;
  355. /* Register CMN PLL clock and fixed rate output clocks. */
  356. ret = ipq_cmn_pll_register_clks(pdev);
  357. pm_runtime_put(dev);
  358. if (ret)
  359. return dev_err_probe(dev, ret,
  360. "Failed to register CMN PLL clocks\n");
  361. return 0;
  362. }
  363. static void ipq_cmn_pll_clk_remove(struct platform_device *pdev)
  364. {
  365. struct clk_hw_onecell_data *hw_data = platform_get_drvdata(pdev);
  366. int i;
  367. /*
  368. * The clock with index CMN_PLL_CLK is unregistered by
  369. * device management.
  370. */
  371. for (i = 0; i < hw_data->num; i++) {
  372. if (i != CMN_PLL_CLK)
  373. clk_hw_unregister(hw_data->hws[i]);
  374. }
  375. }
  376. static const struct dev_pm_ops ipq_cmn_pll_pm_ops = {
  377. SET_RUNTIME_PM_OPS(pm_clk_suspend, pm_clk_resume, NULL)
  378. };
  379. static const struct of_device_id ipq_cmn_pll_clk_ids[] = {
  380. { .compatible = "qcom,ipq5018-cmn-pll", .data = &ipq5018_output_clks },
  381. { .compatible = "qcom,ipq5424-cmn-pll", .data = &ipq5424_output_clks },
  382. { .compatible = "qcom,ipq9574-cmn-pll", .data = &ipq9574_output_clks },
  383. { }
  384. };
  385. MODULE_DEVICE_TABLE(of, ipq_cmn_pll_clk_ids);
  386. static struct platform_driver ipq_cmn_pll_clk_driver = {
  387. .probe = ipq_cmn_pll_clk_probe,
  388. .remove = ipq_cmn_pll_clk_remove,
  389. .driver = {
  390. .name = "ipq_cmn_pll",
  391. .of_match_table = ipq_cmn_pll_clk_ids,
  392. .pm = &ipq_cmn_pll_pm_ops,
  393. },
  394. };
  395. module_platform_driver(ipq_cmn_pll_clk_driver);
  396. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. IPQ CMN PLL Driver");
  397. MODULE_LICENSE("GPL");