apll.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OMAP APLL clock support
  4. *
  5. * Copyright (C) 2013 Texas Instruments, Inc.
  6. *
  7. * J Keerthy <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/err.h>
  15. #include <linux/string.h>
  16. #include <linux/log2.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/clk/ti.h>
  20. #include <linux/delay.h>
  21. #include "clock.h"
  22. #define APLL_FORCE_LOCK 0x1
  23. #define APLL_AUTO_IDLE 0x2
  24. #define MAX_APLL_WAIT_TRIES 1000000
  25. #undef pr_fmt
  26. #define pr_fmt(fmt) "%s: " fmt, __func__
  27. static int dra7_apll_enable(struct clk_hw *hw)
  28. {
  29. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  30. int r = 0, i = 0;
  31. struct dpll_data *ad;
  32. const char *clk_name;
  33. u8 state = 1;
  34. u32 v;
  35. ad = clk->dpll_data;
  36. if (!ad)
  37. return -EINVAL;
  38. clk_name = clk_hw_get_name(&clk->hw);
  39. state <<= __ffs(ad->idlest_mask);
  40. /* Check is already locked */
  41. v = ti_clk_ll_ops->clk_readl(&ad->idlest_reg);
  42. if ((v & ad->idlest_mask) == state)
  43. return r;
  44. v = ti_clk_ll_ops->clk_readl(&ad->control_reg);
  45. v &= ~ad->enable_mask;
  46. v |= APLL_FORCE_LOCK << __ffs(ad->enable_mask);
  47. ti_clk_ll_ops->clk_writel(v, &ad->control_reg);
  48. state <<= __ffs(ad->idlest_mask);
  49. while (1) {
  50. v = ti_clk_ll_ops->clk_readl(&ad->idlest_reg);
  51. if ((v & ad->idlest_mask) == state)
  52. break;
  53. if (i > MAX_APLL_WAIT_TRIES)
  54. break;
  55. i++;
  56. udelay(1);
  57. }
  58. if (i == MAX_APLL_WAIT_TRIES) {
  59. pr_warn("clock: %s failed transition to '%s'\n",
  60. clk_name, (state) ? "locked" : "bypassed");
  61. r = -EBUSY;
  62. } else
  63. pr_debug("clock: %s transition to '%s' in %d loops\n",
  64. clk_name, (state) ? "locked" : "bypassed", i);
  65. return r;
  66. }
  67. static void dra7_apll_disable(struct clk_hw *hw)
  68. {
  69. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  70. struct dpll_data *ad;
  71. u8 state = 1;
  72. u32 v;
  73. ad = clk->dpll_data;
  74. state <<= __ffs(ad->idlest_mask);
  75. v = ti_clk_ll_ops->clk_readl(&ad->control_reg);
  76. v &= ~ad->enable_mask;
  77. v |= APLL_AUTO_IDLE << __ffs(ad->enable_mask);
  78. ti_clk_ll_ops->clk_writel(v, &ad->control_reg);
  79. }
  80. static int dra7_apll_is_enabled(struct clk_hw *hw)
  81. {
  82. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  83. struct dpll_data *ad;
  84. u32 v;
  85. ad = clk->dpll_data;
  86. v = ti_clk_ll_ops->clk_readl(&ad->control_reg);
  87. v &= ad->enable_mask;
  88. v >>= __ffs(ad->enable_mask);
  89. return v == APLL_AUTO_IDLE ? 0 : 1;
  90. }
  91. static u8 dra7_init_apll_parent(struct clk_hw *hw)
  92. {
  93. return 0;
  94. }
  95. static const struct clk_ops apll_ck_ops = {
  96. .enable = &dra7_apll_enable,
  97. .disable = &dra7_apll_disable,
  98. .is_enabled = &dra7_apll_is_enabled,
  99. .get_parent = &dra7_init_apll_parent,
  100. };
  101. static void __init omap_clk_register_apll(void *user,
  102. struct device_node *node)
  103. {
  104. struct clk_hw *hw = user;
  105. struct clk_hw_omap *clk_hw = to_clk_hw_omap(hw);
  106. struct dpll_data *ad = clk_hw->dpll_data;
  107. const char *name;
  108. struct clk *clk;
  109. const struct clk_init_data *init = clk_hw->hw.init;
  110. clk = of_clk_get(node, 0);
  111. if (IS_ERR(clk)) {
  112. pr_debug("clk-ref for %pOFn not ready, retry\n",
  113. node);
  114. if (!ti_clk_retry_init(node, hw, omap_clk_register_apll))
  115. return;
  116. goto cleanup;
  117. }
  118. ad->clk_ref = __clk_get_hw(clk);
  119. clk = of_clk_get(node, 1);
  120. if (IS_ERR(clk)) {
  121. pr_debug("clk-bypass for %pOFn not ready, retry\n",
  122. node);
  123. if (!ti_clk_retry_init(node, hw, omap_clk_register_apll))
  124. return;
  125. goto cleanup;
  126. }
  127. ad->clk_bypass = __clk_get_hw(clk);
  128. name = ti_dt_clk_name(node);
  129. clk = of_ti_clk_register_omap_hw(node, &clk_hw->hw, name);
  130. if (!IS_ERR(clk)) {
  131. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  132. kfree(init->parent_names);
  133. kfree(init);
  134. return;
  135. }
  136. cleanup:
  137. kfree(clk_hw->dpll_data);
  138. kfree(init->parent_names);
  139. kfree(init);
  140. kfree(clk_hw);
  141. }
  142. static void __init of_dra7_apll_setup(struct device_node *node)
  143. {
  144. struct dpll_data *ad = NULL;
  145. struct clk_hw_omap *clk_hw = NULL;
  146. struct clk_init_data *init = NULL;
  147. const char **parent_names = NULL;
  148. int ret;
  149. ad = kzalloc(sizeof(*ad), GFP_KERNEL);
  150. clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
  151. init = kzalloc(sizeof(*init), GFP_KERNEL);
  152. if (!ad || !clk_hw || !init)
  153. goto cleanup;
  154. clk_hw->dpll_data = ad;
  155. clk_hw->hw.init = init;
  156. init->name = ti_dt_clk_name(node);
  157. init->ops = &apll_ck_ops;
  158. init->num_parents = of_clk_get_parent_count(node);
  159. if (init->num_parents < 1) {
  160. pr_err("dra7 apll %pOFn must have parent(s)\n", node);
  161. goto cleanup;
  162. }
  163. parent_names = kcalloc(init->num_parents, sizeof(char *), GFP_KERNEL);
  164. if (!parent_names)
  165. goto cleanup;
  166. of_clk_parent_fill(node, parent_names, init->num_parents);
  167. init->parent_names = parent_names;
  168. ret = ti_clk_get_reg_addr(node, 0, &ad->control_reg);
  169. ret |= ti_clk_get_reg_addr(node, 1, &ad->idlest_reg);
  170. if (ret)
  171. goto cleanup;
  172. ad->idlest_mask = 0x1;
  173. ad->enable_mask = 0x3;
  174. omap_clk_register_apll(&clk_hw->hw, node);
  175. return;
  176. cleanup:
  177. kfree(parent_names);
  178. kfree(ad);
  179. kfree(clk_hw);
  180. kfree(init);
  181. }
  182. CLK_OF_DECLARE(dra7_apll_clock, "ti,dra7-apll-clock", of_dra7_apll_setup);
  183. #define OMAP2_EN_APLL_LOCKED 0x3
  184. #define OMAP2_EN_APLL_STOPPED 0x0
  185. static int omap2_apll_is_enabled(struct clk_hw *hw)
  186. {
  187. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  188. struct dpll_data *ad = clk->dpll_data;
  189. u32 v;
  190. v = ti_clk_ll_ops->clk_readl(&ad->control_reg);
  191. v &= ad->enable_mask;
  192. v >>= __ffs(ad->enable_mask);
  193. return v == OMAP2_EN_APLL_LOCKED ? 1 : 0;
  194. }
  195. static unsigned long omap2_apll_recalc(struct clk_hw *hw,
  196. unsigned long parent_rate)
  197. {
  198. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  199. if (omap2_apll_is_enabled(hw))
  200. return clk->fixed_rate;
  201. return 0;
  202. }
  203. static int omap2_apll_enable(struct clk_hw *hw)
  204. {
  205. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  206. struct dpll_data *ad = clk->dpll_data;
  207. u32 v;
  208. int i = 0;
  209. v = ti_clk_ll_ops->clk_readl(&ad->control_reg);
  210. v &= ~ad->enable_mask;
  211. v |= OMAP2_EN_APLL_LOCKED << __ffs(ad->enable_mask);
  212. ti_clk_ll_ops->clk_writel(v, &ad->control_reg);
  213. while (1) {
  214. v = ti_clk_ll_ops->clk_readl(&ad->idlest_reg);
  215. if (v & ad->idlest_mask)
  216. break;
  217. if (i > MAX_APLL_WAIT_TRIES)
  218. break;
  219. i++;
  220. udelay(1);
  221. }
  222. if (i == MAX_APLL_WAIT_TRIES) {
  223. pr_warn("%s failed to transition to locked\n",
  224. clk_hw_get_name(&clk->hw));
  225. return -EBUSY;
  226. }
  227. return 0;
  228. }
  229. static void omap2_apll_disable(struct clk_hw *hw)
  230. {
  231. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  232. struct dpll_data *ad = clk->dpll_data;
  233. u32 v;
  234. v = ti_clk_ll_ops->clk_readl(&ad->control_reg);
  235. v &= ~ad->enable_mask;
  236. v |= OMAP2_EN_APLL_STOPPED << __ffs(ad->enable_mask);
  237. ti_clk_ll_ops->clk_writel(v, &ad->control_reg);
  238. }
  239. static const struct clk_ops omap2_apll_ops = {
  240. .enable = &omap2_apll_enable,
  241. .disable = &omap2_apll_disable,
  242. .is_enabled = &omap2_apll_is_enabled,
  243. .recalc_rate = &omap2_apll_recalc,
  244. };
  245. static void omap2_apll_set_autoidle(struct clk_hw_omap *clk, u32 val)
  246. {
  247. struct dpll_data *ad = clk->dpll_data;
  248. u32 v;
  249. v = ti_clk_ll_ops->clk_readl(&ad->autoidle_reg);
  250. v &= ~ad->autoidle_mask;
  251. v |= val << __ffs(ad->autoidle_mask);
  252. ti_clk_ll_ops->clk_writel(v, &ad->control_reg);
  253. }
  254. #define OMAP2_APLL_AUTOIDLE_LOW_POWER_STOP 0x3
  255. #define OMAP2_APLL_AUTOIDLE_DISABLE 0x0
  256. static void omap2_apll_allow_idle(struct clk_hw_omap *clk)
  257. {
  258. omap2_apll_set_autoidle(clk, OMAP2_APLL_AUTOIDLE_LOW_POWER_STOP);
  259. }
  260. static void omap2_apll_deny_idle(struct clk_hw_omap *clk)
  261. {
  262. omap2_apll_set_autoidle(clk, OMAP2_APLL_AUTOIDLE_DISABLE);
  263. }
  264. static const struct clk_hw_omap_ops omap2_apll_hwops = {
  265. .allow_idle = &omap2_apll_allow_idle,
  266. .deny_idle = &omap2_apll_deny_idle,
  267. };
  268. static void __init of_omap2_apll_setup(struct device_node *node)
  269. {
  270. struct dpll_data *ad = NULL;
  271. struct clk_hw_omap *clk_hw = NULL;
  272. struct clk_init_data *init = NULL;
  273. const char *name;
  274. struct clk *clk;
  275. const char *parent_name;
  276. u32 val;
  277. int ret;
  278. ad = kzalloc(sizeof(*ad), GFP_KERNEL);
  279. clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
  280. init = kzalloc(sizeof(*init), GFP_KERNEL);
  281. if (!ad || !clk_hw || !init)
  282. goto cleanup;
  283. clk_hw->dpll_data = ad;
  284. clk_hw->hw.init = init;
  285. init->ops = &omap2_apll_ops;
  286. name = ti_dt_clk_name(node);
  287. init->name = name;
  288. clk_hw->ops = &omap2_apll_hwops;
  289. init->num_parents = of_clk_get_parent_count(node);
  290. if (init->num_parents != 1) {
  291. pr_err("%pOFn must have one parent\n", node);
  292. goto cleanup;
  293. }
  294. parent_name = of_clk_get_parent_name(node, 0);
  295. init->parent_names = &parent_name;
  296. if (of_property_read_u32(node, "ti,clock-frequency", &val)) {
  297. pr_err("%pOFn missing clock-frequency\n", node);
  298. goto cleanup;
  299. }
  300. clk_hw->fixed_rate = val;
  301. if (of_property_read_u32(node, "ti,bit-shift", &val)) {
  302. pr_err("%pOFn missing bit-shift\n", node);
  303. goto cleanup;
  304. }
  305. clk_hw->enable_bit = val;
  306. ad->enable_mask = 0x3 << val;
  307. ad->autoidle_mask = 0x3 << val;
  308. if (of_property_read_u32(node, "ti,idlest-shift", &val)) {
  309. pr_err("%pOFn missing idlest-shift\n", node);
  310. goto cleanup;
  311. }
  312. ad->idlest_mask = 1 << val;
  313. ret = ti_clk_get_reg_addr(node, 0, &ad->control_reg);
  314. ret |= ti_clk_get_reg_addr(node, 1, &ad->autoidle_reg);
  315. ret |= ti_clk_get_reg_addr(node, 2, &ad->idlest_reg);
  316. if (ret)
  317. goto cleanup;
  318. name = ti_dt_clk_name(node);
  319. clk = of_ti_clk_register_omap_hw(node, &clk_hw->hw, name);
  320. if (!IS_ERR(clk)) {
  321. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  322. kfree(init);
  323. return;
  324. }
  325. cleanup:
  326. kfree(ad);
  327. kfree(clk_hw);
  328. kfree(init);
  329. }
  330. CLK_OF_DECLARE(omap2_apll_clock, "ti,omap2-apll-clock",
  331. of_omap2_apll_setup);