apll.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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-provider.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/io.h>
  21. #include <linux/err.h>
  22. #include <linux/string.h>
  23. #include <linux/log2.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/clk/ti.h>
  27. #include <linux/delay.h>
  28. #define APLL_FORCE_LOCK 0x1
  29. #define APLL_AUTO_IDLE 0x2
  30. #define MAX_APLL_WAIT_TRIES 1000000
  31. #undef pr_fmt
  32. #define pr_fmt(fmt) "%s: " fmt, __func__
  33. static int dra7_apll_enable(struct clk_hw *hw)
  34. {
  35. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  36. int r = 0, i = 0;
  37. struct dpll_data *ad;
  38. const char *clk_name;
  39. u8 state = 1;
  40. u32 v;
  41. ad = clk->dpll_data;
  42. if (!ad)
  43. return -EINVAL;
  44. clk_name = __clk_get_name(clk->hw.clk);
  45. state <<= __ffs(ad->idlest_mask);
  46. /* Check is already locked */
  47. v = ti_clk_ll_ops->clk_readl(ad->idlest_reg);
  48. if ((v & ad->idlest_mask) == state)
  49. return r;
  50. v = ti_clk_ll_ops->clk_readl(ad->control_reg);
  51. v &= ~ad->enable_mask;
  52. v |= APLL_FORCE_LOCK << __ffs(ad->enable_mask);
  53. ti_clk_ll_ops->clk_writel(v, ad->control_reg);
  54. state <<= __ffs(ad->idlest_mask);
  55. while (1) {
  56. v = ti_clk_ll_ops->clk_readl(ad->idlest_reg);
  57. if ((v & ad->idlest_mask) == state)
  58. break;
  59. if (i > MAX_APLL_WAIT_TRIES)
  60. break;
  61. i++;
  62. udelay(1);
  63. }
  64. if (i == MAX_APLL_WAIT_TRIES) {
  65. pr_warn("clock: %s failed transition to '%s'\n",
  66. clk_name, (state) ? "locked" : "bypassed");
  67. r = -EBUSY;
  68. } else
  69. pr_debug("clock: %s transition to '%s' in %d loops\n",
  70. clk_name, (state) ? "locked" : "bypassed", i);
  71. return r;
  72. }
  73. static void dra7_apll_disable(struct clk_hw *hw)
  74. {
  75. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  76. struct dpll_data *ad;
  77. u8 state = 1;
  78. u32 v;
  79. ad = clk->dpll_data;
  80. state <<= __ffs(ad->idlest_mask);
  81. v = ti_clk_ll_ops->clk_readl(ad->control_reg);
  82. v &= ~ad->enable_mask;
  83. v |= APLL_AUTO_IDLE << __ffs(ad->enable_mask);
  84. ti_clk_ll_ops->clk_writel(v, ad->control_reg);
  85. }
  86. static int dra7_apll_is_enabled(struct clk_hw *hw)
  87. {
  88. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  89. struct dpll_data *ad;
  90. u32 v;
  91. ad = clk->dpll_data;
  92. v = ti_clk_ll_ops->clk_readl(ad->control_reg);
  93. v &= ad->enable_mask;
  94. v >>= __ffs(ad->enable_mask);
  95. return v == APLL_AUTO_IDLE ? 0 : 1;
  96. }
  97. static u8 dra7_init_apll_parent(struct clk_hw *hw)
  98. {
  99. return 0;
  100. }
  101. static const struct clk_ops apll_ck_ops = {
  102. .enable = &dra7_apll_enable,
  103. .disable = &dra7_apll_disable,
  104. .is_enabled = &dra7_apll_is_enabled,
  105. .get_parent = &dra7_init_apll_parent,
  106. };
  107. static void __init omap_clk_register_apll(struct clk_hw *hw,
  108. struct device_node *node)
  109. {
  110. struct clk_hw_omap *clk_hw = to_clk_hw_omap(hw);
  111. struct dpll_data *ad = clk_hw->dpll_data;
  112. struct clk *clk;
  113. ad->clk_ref = of_clk_get(node, 0);
  114. ad->clk_bypass = of_clk_get(node, 1);
  115. if (IS_ERR(ad->clk_ref) || IS_ERR(ad->clk_bypass)) {
  116. pr_debug("clk-ref or clk-bypass for %s not ready, retry\n",
  117. node->name);
  118. if (!ti_clk_retry_init(node, hw, omap_clk_register_apll))
  119. return;
  120. goto cleanup;
  121. }
  122. clk = clk_register(NULL, &clk_hw->hw);
  123. if (!IS_ERR(clk)) {
  124. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  125. kfree(clk_hw->hw.init->parent_names);
  126. kfree(clk_hw->hw.init);
  127. return;
  128. }
  129. cleanup:
  130. kfree(clk_hw->dpll_data);
  131. kfree(clk_hw->hw.init->parent_names);
  132. kfree(clk_hw->hw.init);
  133. kfree(clk_hw);
  134. }
  135. static void __init of_dra7_apll_setup(struct device_node *node)
  136. {
  137. struct dpll_data *ad = NULL;
  138. struct clk_hw_omap *clk_hw = NULL;
  139. struct clk_init_data *init = NULL;
  140. const char **parent_names = NULL;
  141. int i;
  142. ad = kzalloc(sizeof(*ad), GFP_KERNEL);
  143. clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
  144. init = kzalloc(sizeof(*init), GFP_KERNEL);
  145. if (!ad || !clk_hw || !init)
  146. goto cleanup;
  147. clk_hw->dpll_data = ad;
  148. clk_hw->hw.init = init;
  149. clk_hw->flags = MEMMAP_ADDRESSING;
  150. init->name = node->name;
  151. init->ops = &apll_ck_ops;
  152. init->num_parents = of_clk_get_parent_count(node);
  153. if (init->num_parents < 1) {
  154. pr_err("dra7 apll %s must have parent(s)\n", node->name);
  155. goto cleanup;
  156. }
  157. parent_names = kzalloc(sizeof(char *) * init->num_parents, GFP_KERNEL);
  158. if (!parent_names)
  159. goto cleanup;
  160. for (i = 0; i < init->num_parents; i++)
  161. parent_names[i] = of_clk_get_parent_name(node, i);
  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 (!ad->control_reg || !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_get_name(clk->hw.clk));
  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 (!ad->control_reg || !ad->autoidle_reg || !ad->idlest_reg)
  309. goto cleanup;
  310. clk = clk_register(NULL, &clk_hw->hw);
  311. if (!IS_ERR(clk)) {
  312. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  313. kfree(init);
  314. return;
  315. }
  316. cleanup:
  317. kfree(ad);
  318. kfree(clk_hw);
  319. kfree(init);
  320. }
  321. CLK_OF_DECLARE(omap2_apll_clock, "ti,omap2-apll-clock",
  322. of_omap2_apll_setup);