apll.c 9.5 KB

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