apll.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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(struct clk_hw *hw,
  110. struct device_node *node)
  111. {
  112. struct clk_hw_omap *clk_hw = to_clk_hw_omap(hw);
  113. struct dpll_data *ad = clk_hw->dpll_data;
  114. struct clk *clk;
  115. ad->clk_ref = of_clk_get(node, 0);
  116. ad->clk_bypass = of_clk_get(node, 1);
  117. if (IS_ERR(ad->clk_ref) || IS_ERR(ad->clk_bypass)) {
  118. pr_debug("clk-ref or clk-bypass for %s not ready, retry\n",
  119. node->name);
  120. if (!ti_clk_retry_init(node, hw, omap_clk_register_apll))
  121. return;
  122. goto cleanup;
  123. }
  124. clk = clk_register(NULL, &clk_hw->hw);
  125. if (!IS_ERR(clk)) {
  126. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  127. kfree(clk_hw->hw.init->parent_names);
  128. kfree(clk_hw->hw.init);
  129. return;
  130. }
  131. cleanup:
  132. kfree(clk_hw->dpll_data);
  133. kfree(clk_hw->hw.init->parent_names);
  134. kfree(clk_hw->hw.init);
  135. kfree(clk_hw);
  136. }
  137. static void __init of_dra7_apll_setup(struct device_node *node)
  138. {
  139. struct dpll_data *ad = NULL;
  140. struct clk_hw_omap *clk_hw = NULL;
  141. struct clk_init_data *init = NULL;
  142. const char **parent_names = NULL;
  143. ad = kzalloc(sizeof(*ad), GFP_KERNEL);
  144. clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
  145. init = kzalloc(sizeof(*init), GFP_KERNEL);
  146. if (!ad || !clk_hw || !init)
  147. goto cleanup;
  148. clk_hw->dpll_data = ad;
  149. clk_hw->hw.init = init;
  150. clk_hw->flags = MEMMAP_ADDRESSING;
  151. init->name = node->name;
  152. init->ops = &apll_ck_ops;
  153. init->num_parents = of_clk_get_parent_count(node);
  154. if (init->num_parents < 1) {
  155. pr_err("dra7 apll %s must have parent(s)\n", node->name);
  156. goto cleanup;
  157. }
  158. parent_names = kzalloc(sizeof(char *) * init->num_parents, GFP_KERNEL);
  159. if (!parent_names)
  160. goto cleanup;
  161. of_clk_parent_fill(node, parent_names, init->num_parents);
  162. init->parent_names = parent_names;
  163. ad->control_reg = ti_clk_get_reg_addr(node, 0);
  164. ad->idlest_reg = ti_clk_get_reg_addr(node, 1);
  165. if (IS_ERR(ad->control_reg) || IS_ERR(ad->idlest_reg))
  166. goto cleanup;
  167. ad->idlest_mask = 0x1;
  168. ad->enable_mask = 0x3;
  169. omap_clk_register_apll(&clk_hw->hw, node);
  170. return;
  171. cleanup:
  172. kfree(parent_names);
  173. kfree(ad);
  174. kfree(clk_hw);
  175. kfree(init);
  176. }
  177. CLK_OF_DECLARE(dra7_apll_clock, "ti,dra7-apll-clock", of_dra7_apll_setup);
  178. #define OMAP2_EN_APLL_LOCKED 0x3
  179. #define OMAP2_EN_APLL_STOPPED 0x0
  180. static int omap2_apll_is_enabled(struct clk_hw *hw)
  181. {
  182. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  183. struct dpll_data *ad = clk->dpll_data;
  184. u32 v;
  185. v = ti_clk_ll_ops->clk_readl(ad->control_reg);
  186. v &= ad->enable_mask;
  187. v >>= __ffs(ad->enable_mask);
  188. return v == OMAP2_EN_APLL_LOCKED ? 1 : 0;
  189. }
  190. static unsigned long omap2_apll_recalc(struct clk_hw *hw,
  191. unsigned long parent_rate)
  192. {
  193. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  194. if (omap2_apll_is_enabled(hw))
  195. return clk->fixed_rate;
  196. return 0;
  197. }
  198. static int omap2_apll_enable(struct clk_hw *hw)
  199. {
  200. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  201. struct dpll_data *ad = clk->dpll_data;
  202. u32 v;
  203. int i = 0;
  204. v = ti_clk_ll_ops->clk_readl(ad->control_reg);
  205. v &= ~ad->enable_mask;
  206. v |= OMAP2_EN_APLL_LOCKED << __ffs(ad->enable_mask);
  207. ti_clk_ll_ops->clk_writel(v, ad->control_reg);
  208. while (1) {
  209. v = ti_clk_ll_ops->clk_readl(ad->idlest_reg);
  210. if (v & ad->idlest_mask)
  211. break;
  212. if (i > MAX_APLL_WAIT_TRIES)
  213. break;
  214. i++;
  215. udelay(1);
  216. }
  217. if (i == MAX_APLL_WAIT_TRIES) {
  218. pr_warn("%s failed to transition to locked\n",
  219. clk_hw_get_name(&clk->hw));
  220. return -EBUSY;
  221. }
  222. return 0;
  223. }
  224. static void omap2_apll_disable(struct clk_hw *hw)
  225. {
  226. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  227. struct dpll_data *ad = clk->dpll_data;
  228. u32 v;
  229. v = ti_clk_ll_ops->clk_readl(ad->control_reg);
  230. v &= ~ad->enable_mask;
  231. v |= OMAP2_EN_APLL_STOPPED << __ffs(ad->enable_mask);
  232. ti_clk_ll_ops->clk_writel(v, ad->control_reg);
  233. }
  234. static struct clk_ops omap2_apll_ops = {
  235. .enable = &omap2_apll_enable,
  236. .disable = &omap2_apll_disable,
  237. .is_enabled = &omap2_apll_is_enabled,
  238. .recalc_rate = &omap2_apll_recalc,
  239. };
  240. static void omap2_apll_set_autoidle(struct clk_hw_omap *clk, u32 val)
  241. {
  242. struct dpll_data *ad = clk->dpll_data;
  243. u32 v;
  244. v = ti_clk_ll_ops->clk_readl(ad->autoidle_reg);
  245. v &= ~ad->autoidle_mask;
  246. v |= val << __ffs(ad->autoidle_mask);
  247. ti_clk_ll_ops->clk_writel(v, ad->control_reg);
  248. }
  249. #define OMAP2_APLL_AUTOIDLE_LOW_POWER_STOP 0x3
  250. #define OMAP2_APLL_AUTOIDLE_DISABLE 0x0
  251. static void omap2_apll_allow_idle(struct clk_hw_omap *clk)
  252. {
  253. omap2_apll_set_autoidle(clk, OMAP2_APLL_AUTOIDLE_LOW_POWER_STOP);
  254. }
  255. static void omap2_apll_deny_idle(struct clk_hw_omap *clk)
  256. {
  257. omap2_apll_set_autoidle(clk, OMAP2_APLL_AUTOIDLE_DISABLE);
  258. }
  259. static struct clk_hw_omap_ops omap2_apll_hwops = {
  260. .allow_idle = &omap2_apll_allow_idle,
  261. .deny_idle = &omap2_apll_deny_idle,
  262. };
  263. static void __init of_omap2_apll_setup(struct device_node *node)
  264. {
  265. struct dpll_data *ad = NULL;
  266. struct clk_hw_omap *clk_hw = NULL;
  267. struct clk_init_data *init = NULL;
  268. struct clk *clk;
  269. const char *parent_name;
  270. u32 val;
  271. ad = kzalloc(sizeof(*ad), GFP_KERNEL);
  272. clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
  273. init = kzalloc(sizeof(*init), GFP_KERNEL);
  274. if (!ad || !clk_hw || !init)
  275. goto cleanup;
  276. clk_hw->dpll_data = ad;
  277. clk_hw->hw.init = init;
  278. init->ops = &omap2_apll_ops;
  279. init->name = node->name;
  280. clk_hw->ops = &omap2_apll_hwops;
  281. init->num_parents = of_clk_get_parent_count(node);
  282. if (init->num_parents != 1) {
  283. pr_err("%s must have one parent\n", node->name);
  284. goto cleanup;
  285. }
  286. parent_name = of_clk_get_parent_name(node, 0);
  287. init->parent_names = &parent_name;
  288. if (of_property_read_u32(node, "ti,clock-frequency", &val)) {
  289. pr_err("%s missing clock-frequency\n", node->name);
  290. goto cleanup;
  291. }
  292. clk_hw->fixed_rate = val;
  293. if (of_property_read_u32(node, "ti,bit-shift", &val)) {
  294. pr_err("%s missing bit-shift\n", node->name);
  295. goto cleanup;
  296. }
  297. clk_hw->enable_bit = val;
  298. ad->enable_mask = 0x3 << val;
  299. ad->autoidle_mask = 0x3 << val;
  300. if (of_property_read_u32(node, "ti,idlest-shift", &val)) {
  301. pr_err("%s missing idlest-shift\n", node->name);
  302. goto cleanup;
  303. }
  304. ad->idlest_mask = 1 << val;
  305. ad->control_reg = ti_clk_get_reg_addr(node, 0);
  306. ad->autoidle_reg = ti_clk_get_reg_addr(node, 1);
  307. ad->idlest_reg = ti_clk_get_reg_addr(node, 2);
  308. if (IS_ERR(ad->control_reg) || IS_ERR(ad->autoidle_reg) ||
  309. IS_ERR(ad->idlest_reg))
  310. goto cleanup;
  311. clk = clk_register(NULL, &clk_hw->hw);
  312. if (!IS_ERR(clk)) {
  313. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  314. kfree(init);
  315. return;
  316. }
  317. cleanup:
  318. kfree(ad);
  319. kfree(clk_hw);
  320. kfree(init);
  321. }
  322. CLK_OF_DECLARE(omap2_apll_clock, "ti,omap2-apll-clock",
  323. of_omap2_apll_setup);