clk-hsdk-pll.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Synopsys HSDK SDP Generic PLL clock driver
  4. *
  5. * Copyright (C) 2017 Synopsys
  6. */
  7. #include <linux/clk-provider.h>
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #define CGU_PLL_CTRL 0x000 /* ARC PLL control register */
  18. #define CGU_PLL_STATUS 0x004 /* ARC PLL status register */
  19. #define CGU_PLL_FMEAS 0x008 /* ARC PLL frequency measurement register */
  20. #define CGU_PLL_MON 0x00C /* ARC PLL monitor register */
  21. #define CGU_PLL_CTRL_ODIV_SHIFT 2
  22. #define CGU_PLL_CTRL_IDIV_SHIFT 4
  23. #define CGU_PLL_CTRL_FBDIV_SHIFT 9
  24. #define CGU_PLL_CTRL_BAND_SHIFT 20
  25. #define CGU_PLL_CTRL_ODIV_MASK GENMASK(3, CGU_PLL_CTRL_ODIV_SHIFT)
  26. #define CGU_PLL_CTRL_IDIV_MASK GENMASK(8, CGU_PLL_CTRL_IDIV_SHIFT)
  27. #define CGU_PLL_CTRL_FBDIV_MASK GENMASK(15, CGU_PLL_CTRL_FBDIV_SHIFT)
  28. #define CGU_PLL_CTRL_PD BIT(0)
  29. #define CGU_PLL_CTRL_BYPASS BIT(1)
  30. #define CGU_PLL_STATUS_LOCK BIT(0)
  31. #define CGU_PLL_STATUS_ERR BIT(1)
  32. #define HSDK_PLL_MAX_LOCK_TIME 100 /* 100 us */
  33. #define CGU_PLL_SOURCE_MAX 1
  34. #define CORE_IF_CLK_THRESHOLD_HZ 500000000
  35. #define CREG_CORE_IF_CLK_DIV_1 0x0
  36. #define CREG_CORE_IF_CLK_DIV_2 0x1
  37. struct hsdk_pll_cfg {
  38. u32 rate;
  39. u32 idiv;
  40. u32 fbdiv;
  41. u32 odiv;
  42. u32 band;
  43. u32 bypass;
  44. };
  45. static const struct hsdk_pll_cfg asdt_pll_cfg[] = {
  46. { 100000000, 0, 11, 3, 0, 0 },
  47. { 133000000, 0, 15, 3, 0, 0 },
  48. { 200000000, 1, 47, 3, 0, 0 },
  49. { 233000000, 1, 27, 2, 0, 0 },
  50. { 300000000, 1, 35, 2, 0, 0 },
  51. { 333000000, 1, 39, 2, 0, 0 },
  52. { 400000000, 1, 47, 2, 0, 0 },
  53. { 500000000, 0, 14, 1, 0, 0 },
  54. { 600000000, 0, 17, 1, 0, 0 },
  55. { 700000000, 0, 20, 1, 0, 0 },
  56. { 800000000, 0, 23, 1, 0, 0 },
  57. { 900000000, 1, 26, 0, 0, 0 },
  58. { 1000000000, 1, 29, 0, 0, 0 },
  59. { 1100000000, 1, 32, 0, 0, 0 },
  60. { 1200000000, 1, 35, 0, 0, 0 },
  61. { 1300000000, 1, 38, 0, 0, 0 },
  62. { 1400000000, 1, 41, 0, 0, 0 },
  63. { 1500000000, 1, 44, 0, 0, 0 },
  64. { 1600000000, 1, 47, 0, 0, 0 },
  65. {}
  66. };
  67. static const struct hsdk_pll_cfg hdmi_pll_cfg[] = {
  68. { 27000000, 0, 0, 0, 0, 1 },
  69. { 148500000, 0, 21, 3, 0, 0 },
  70. { 297000000, 0, 21, 2, 0, 0 },
  71. { 540000000, 0, 19, 1, 0, 0 },
  72. { 594000000, 0, 21, 1, 0, 0 },
  73. {}
  74. };
  75. struct hsdk_pll_clk {
  76. struct clk_hw hw;
  77. void __iomem *regs;
  78. void __iomem *spec_regs;
  79. const struct hsdk_pll_devdata *pll_devdata;
  80. struct device *dev;
  81. };
  82. struct hsdk_pll_devdata {
  83. const struct hsdk_pll_cfg *pll_cfg;
  84. int (*update_rate)(struct hsdk_pll_clk *clk, unsigned long rate,
  85. const struct hsdk_pll_cfg *cfg);
  86. };
  87. static int hsdk_pll_core_update_rate(struct hsdk_pll_clk *, unsigned long,
  88. const struct hsdk_pll_cfg *);
  89. static int hsdk_pll_comm_update_rate(struct hsdk_pll_clk *, unsigned long,
  90. const struct hsdk_pll_cfg *);
  91. static const struct hsdk_pll_devdata core_pll_devdata = {
  92. .pll_cfg = asdt_pll_cfg,
  93. .update_rate = hsdk_pll_core_update_rate,
  94. };
  95. static const struct hsdk_pll_devdata sdt_pll_devdata = {
  96. .pll_cfg = asdt_pll_cfg,
  97. .update_rate = hsdk_pll_comm_update_rate,
  98. };
  99. static const struct hsdk_pll_devdata hdmi_pll_devdata = {
  100. .pll_cfg = hdmi_pll_cfg,
  101. .update_rate = hsdk_pll_comm_update_rate,
  102. };
  103. static inline void hsdk_pll_write(struct hsdk_pll_clk *clk, u32 reg, u32 val)
  104. {
  105. iowrite32(val, clk->regs + reg);
  106. }
  107. static inline u32 hsdk_pll_read(struct hsdk_pll_clk *clk, u32 reg)
  108. {
  109. return ioread32(clk->regs + reg);
  110. }
  111. static inline void hsdk_pll_set_cfg(struct hsdk_pll_clk *clk,
  112. const struct hsdk_pll_cfg *cfg)
  113. {
  114. u32 val = 0;
  115. if (cfg->bypass) {
  116. val = hsdk_pll_read(clk, CGU_PLL_CTRL);
  117. val |= CGU_PLL_CTRL_BYPASS;
  118. } else {
  119. /* Powerdown and Bypass bits should be cleared */
  120. val |= cfg->idiv << CGU_PLL_CTRL_IDIV_SHIFT;
  121. val |= cfg->fbdiv << CGU_PLL_CTRL_FBDIV_SHIFT;
  122. val |= cfg->odiv << CGU_PLL_CTRL_ODIV_SHIFT;
  123. val |= cfg->band << CGU_PLL_CTRL_BAND_SHIFT;
  124. }
  125. dev_dbg(clk->dev, "write configuration: %#x\n", val);
  126. hsdk_pll_write(clk, CGU_PLL_CTRL, val);
  127. }
  128. static inline bool hsdk_pll_is_locked(struct hsdk_pll_clk *clk)
  129. {
  130. return !!(hsdk_pll_read(clk, CGU_PLL_STATUS) & CGU_PLL_STATUS_LOCK);
  131. }
  132. static inline bool hsdk_pll_is_err(struct hsdk_pll_clk *clk)
  133. {
  134. return !!(hsdk_pll_read(clk, CGU_PLL_STATUS) & CGU_PLL_STATUS_ERR);
  135. }
  136. static inline struct hsdk_pll_clk *to_hsdk_pll_clk(struct clk_hw *hw)
  137. {
  138. return container_of(hw, struct hsdk_pll_clk, hw);
  139. }
  140. static unsigned long hsdk_pll_recalc_rate(struct clk_hw *hw,
  141. unsigned long parent_rate)
  142. {
  143. u32 val;
  144. u64 rate;
  145. u32 idiv, fbdiv, odiv;
  146. struct hsdk_pll_clk *clk = to_hsdk_pll_clk(hw);
  147. val = hsdk_pll_read(clk, CGU_PLL_CTRL);
  148. dev_dbg(clk->dev, "current configuration: %#x\n", val);
  149. /* Check if PLL is bypassed */
  150. if (val & CGU_PLL_CTRL_BYPASS)
  151. return parent_rate;
  152. /* Check if PLL is disabled */
  153. if (val & CGU_PLL_CTRL_PD)
  154. return 0;
  155. /* input divider = reg.idiv + 1 */
  156. idiv = 1 + ((val & CGU_PLL_CTRL_IDIV_MASK) >> CGU_PLL_CTRL_IDIV_SHIFT);
  157. /* fb divider = 2*(reg.fbdiv + 1) */
  158. fbdiv = 2 * (1 + ((val & CGU_PLL_CTRL_FBDIV_MASK) >> CGU_PLL_CTRL_FBDIV_SHIFT));
  159. /* output divider = 2^(reg.odiv) */
  160. odiv = 1 << ((val & CGU_PLL_CTRL_ODIV_MASK) >> CGU_PLL_CTRL_ODIV_SHIFT);
  161. rate = (u64)parent_rate * fbdiv;
  162. do_div(rate, idiv * odiv);
  163. return rate;
  164. }
  165. static long hsdk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  166. unsigned long *prate)
  167. {
  168. int i;
  169. unsigned long best_rate;
  170. struct hsdk_pll_clk *clk = to_hsdk_pll_clk(hw);
  171. const struct hsdk_pll_cfg *pll_cfg = clk->pll_devdata->pll_cfg;
  172. if (pll_cfg[0].rate == 0)
  173. return -EINVAL;
  174. best_rate = pll_cfg[0].rate;
  175. for (i = 1; pll_cfg[i].rate != 0; i++) {
  176. if (abs(rate - pll_cfg[i].rate) < abs(rate - best_rate))
  177. best_rate = pll_cfg[i].rate;
  178. }
  179. dev_dbg(clk->dev, "chosen best rate: %lu\n", best_rate);
  180. return best_rate;
  181. }
  182. static int hsdk_pll_comm_update_rate(struct hsdk_pll_clk *clk,
  183. unsigned long rate,
  184. const struct hsdk_pll_cfg *cfg)
  185. {
  186. hsdk_pll_set_cfg(clk, cfg);
  187. /*
  188. * Wait until CGU relocks and check error status.
  189. * If after timeout CGU is unlocked yet return error.
  190. */
  191. udelay(HSDK_PLL_MAX_LOCK_TIME);
  192. if (!hsdk_pll_is_locked(clk))
  193. return -ETIMEDOUT;
  194. if (hsdk_pll_is_err(clk))
  195. return -EINVAL;
  196. return 0;
  197. }
  198. static int hsdk_pll_core_update_rate(struct hsdk_pll_clk *clk,
  199. unsigned long rate,
  200. const struct hsdk_pll_cfg *cfg)
  201. {
  202. /*
  203. * When core clock exceeds 500MHz, the divider for the interface
  204. * clock must be programmed to div-by-2.
  205. */
  206. if (rate > CORE_IF_CLK_THRESHOLD_HZ)
  207. iowrite32(CREG_CORE_IF_CLK_DIV_2, clk->spec_regs);
  208. hsdk_pll_set_cfg(clk, cfg);
  209. /*
  210. * Wait until CGU relocks and check error status.
  211. * If after timeout CGU is unlocked yet return error.
  212. */
  213. udelay(HSDK_PLL_MAX_LOCK_TIME);
  214. if (!hsdk_pll_is_locked(clk))
  215. return -ETIMEDOUT;
  216. if (hsdk_pll_is_err(clk))
  217. return -EINVAL;
  218. /*
  219. * Program divider to div-by-1 if we succesfuly set core clock below
  220. * 500MHz threshold.
  221. */
  222. if (rate <= CORE_IF_CLK_THRESHOLD_HZ)
  223. iowrite32(CREG_CORE_IF_CLK_DIV_1, clk->spec_regs);
  224. return 0;
  225. }
  226. static int hsdk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  227. unsigned long parent_rate)
  228. {
  229. int i;
  230. struct hsdk_pll_clk *clk = to_hsdk_pll_clk(hw);
  231. const struct hsdk_pll_cfg *pll_cfg = clk->pll_devdata->pll_cfg;
  232. for (i = 0; pll_cfg[i].rate != 0; i++) {
  233. if (pll_cfg[i].rate == rate) {
  234. return clk->pll_devdata->update_rate(clk, rate,
  235. &pll_cfg[i]);
  236. }
  237. }
  238. dev_err(clk->dev, "invalid rate=%ld, parent_rate=%ld\n", rate,
  239. parent_rate);
  240. return -EINVAL;
  241. }
  242. static const struct clk_ops hsdk_pll_ops = {
  243. .recalc_rate = hsdk_pll_recalc_rate,
  244. .round_rate = hsdk_pll_round_rate,
  245. .set_rate = hsdk_pll_set_rate,
  246. };
  247. static int hsdk_pll_clk_probe(struct platform_device *pdev)
  248. {
  249. int ret;
  250. struct resource *mem;
  251. const char *parent_name;
  252. unsigned int num_parents;
  253. struct hsdk_pll_clk *pll_clk;
  254. struct clk_init_data init = { };
  255. struct device *dev = &pdev->dev;
  256. pll_clk = devm_kzalloc(dev, sizeof(*pll_clk), GFP_KERNEL);
  257. if (!pll_clk)
  258. return -ENOMEM;
  259. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  260. pll_clk->regs = devm_ioremap_resource(dev, mem);
  261. if (IS_ERR(pll_clk->regs))
  262. return PTR_ERR(pll_clk->regs);
  263. init.name = dev->of_node->name;
  264. init.ops = &hsdk_pll_ops;
  265. parent_name = of_clk_get_parent_name(dev->of_node, 0);
  266. init.parent_names = &parent_name;
  267. num_parents = of_clk_get_parent_count(dev->of_node);
  268. if (num_parents == 0 || num_parents > CGU_PLL_SOURCE_MAX) {
  269. dev_err(dev, "wrong clock parents number: %u\n", num_parents);
  270. return -EINVAL;
  271. }
  272. init.num_parents = num_parents;
  273. pll_clk->hw.init = &init;
  274. pll_clk->dev = dev;
  275. pll_clk->pll_devdata = of_device_get_match_data(dev);
  276. if (!pll_clk->pll_devdata) {
  277. dev_err(dev, "No OF match data provided\n");
  278. return -EINVAL;
  279. }
  280. ret = devm_clk_hw_register(dev, &pll_clk->hw);
  281. if (ret) {
  282. dev_err(dev, "failed to register %s clock\n", init.name);
  283. return ret;
  284. }
  285. return of_clk_add_hw_provider(dev->of_node, of_clk_hw_simple_get,
  286. &pll_clk->hw);
  287. }
  288. static int hsdk_pll_clk_remove(struct platform_device *pdev)
  289. {
  290. of_clk_del_provider(pdev->dev.of_node);
  291. return 0;
  292. }
  293. static void __init of_hsdk_pll_clk_setup(struct device_node *node)
  294. {
  295. int ret;
  296. const char *parent_name;
  297. unsigned int num_parents;
  298. struct hsdk_pll_clk *pll_clk;
  299. struct clk_init_data init = { };
  300. pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL);
  301. if (!pll_clk)
  302. return;
  303. pll_clk->regs = of_iomap(node, 0);
  304. if (!pll_clk->regs) {
  305. pr_err("failed to map pll registers\n");
  306. goto err_free_pll_clk;
  307. }
  308. pll_clk->spec_regs = of_iomap(node, 1);
  309. if (!pll_clk->spec_regs) {
  310. pr_err("failed to map pll registers\n");
  311. goto err_unmap_comm_regs;
  312. }
  313. init.name = node->name;
  314. init.ops = &hsdk_pll_ops;
  315. parent_name = of_clk_get_parent_name(node, 0);
  316. init.parent_names = &parent_name;
  317. num_parents = of_clk_get_parent_count(node);
  318. if (num_parents > CGU_PLL_SOURCE_MAX) {
  319. pr_err("too much clock parents: %u\n", num_parents);
  320. goto err_unmap_spec_regs;
  321. }
  322. init.num_parents = num_parents;
  323. pll_clk->hw.init = &init;
  324. pll_clk->pll_devdata = &core_pll_devdata;
  325. ret = clk_hw_register(NULL, &pll_clk->hw);
  326. if (ret) {
  327. pr_err("failed to register %pOFn clock\n", node);
  328. goto err_unmap_spec_regs;
  329. }
  330. ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, &pll_clk->hw);
  331. if (ret) {
  332. pr_err("failed to add hw provider for %pOFn clock\n", node);
  333. goto err_unmap_spec_regs;
  334. }
  335. return;
  336. err_unmap_spec_regs:
  337. iounmap(pll_clk->spec_regs);
  338. err_unmap_comm_regs:
  339. iounmap(pll_clk->regs);
  340. err_free_pll_clk:
  341. kfree(pll_clk);
  342. }
  343. /* Core PLL needed early for ARC cpus timers */
  344. CLK_OF_DECLARE(hsdk_pll_clock, "snps,hsdk-core-pll-clock",
  345. of_hsdk_pll_clk_setup);
  346. static const struct of_device_id hsdk_pll_clk_id[] = {
  347. { .compatible = "snps,hsdk-gp-pll-clock", .data = &sdt_pll_devdata},
  348. { .compatible = "snps,hsdk-hdmi-pll-clock", .data = &hdmi_pll_devdata},
  349. { }
  350. };
  351. static struct platform_driver hsdk_pll_clk_driver = {
  352. .driver = {
  353. .name = "hsdk-gp-pll-clock",
  354. .of_match_table = hsdk_pll_clk_id,
  355. },
  356. .probe = hsdk_pll_clk_probe,
  357. .remove = hsdk_pll_clk_remove,
  358. };
  359. builtin_platform_driver(hsdk_pll_clk_driver);