PageRenderTime 266ms CodeModel.GetById 16ms RepoModel.GetById 4ms app.codeStats 0ms

/drivers/clk/ti/apll.c

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