apll.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. } else {
  68. pr_debug("clock: %s transition to '%s' in %d loops\n",
  69. clk_name, (state) ? "locked" : "bypassed", i);
  70. r = 0;
  71. }
  72. return r;
  73. }
  74. static void dra7_apll_disable(struct clk_hw *hw)
  75. {
  76. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  77. struct dpll_data *ad;
  78. u8 state = 1;
  79. u32 v;
  80. ad = clk->dpll_data;
  81. state <<= __ffs(ad->idlest_mask);
  82. v = ti_clk_ll_ops->clk_readl(ad->control_reg);
  83. v &= ~ad->enable_mask;
  84. v |= APLL_AUTO_IDLE << __ffs(ad->enable_mask);
  85. ti_clk_ll_ops->clk_writel(v, ad->control_reg);
  86. }
  87. static int dra7_apll_is_enabled(struct clk_hw *hw)
  88. {
  89. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  90. struct dpll_data *ad;
  91. u32 v;
  92. ad = clk->dpll_data;
  93. v = ti_clk_ll_ops->clk_readl(ad->control_reg);
  94. v &= ad->enable_mask;
  95. v >>= __ffs(ad->enable_mask);
  96. return v == APLL_AUTO_IDLE ? 0 : 1;
  97. }
  98. static u8 dra7_init_apll_parent(struct clk_hw *hw)
  99. {
  100. return 0;
  101. }
  102. static const struct clk_ops apll_ck_ops = {
  103. .enable = &dra7_apll_enable,
  104. .disable = &dra7_apll_disable,
  105. .is_enabled = &dra7_apll_is_enabled,
  106. .get_parent = &dra7_init_apll_parent,
  107. };
  108. static void __init omap_clk_register_apll(struct clk_hw *hw,
  109. struct device_node *node)
  110. {
  111. struct clk_hw_omap *clk_hw = to_clk_hw_omap(hw);
  112. struct dpll_data *ad = clk_hw->dpll_data;
  113. struct clk *clk;
  114. ad->clk_ref = of_clk_get(node, 0);
  115. ad->clk_bypass = of_clk_get(node, 1);
  116. if (IS_ERR(ad->clk_ref) || IS_ERR(ad->clk_bypass)) {
  117. pr_debug("clk-ref or clk-bypass 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. clk = clk_register(NULL, &clk_hw->hw);
  124. if (!IS_ERR(clk)) {
  125. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  126. kfree(clk_hw->hw.init->parent_names);
  127. kfree(clk_hw->hw.init);
  128. return;
  129. }
  130. cleanup:
  131. kfree(clk_hw->dpll_data);
  132. kfree(clk_hw->hw.init->parent_names);
  133. kfree(clk_hw->hw.init);
  134. kfree(clk_hw);
  135. }
  136. static void __init of_dra7_apll_setup(struct device_node *node)
  137. {
  138. struct dpll_data *ad = NULL;
  139. struct clk_hw_omap *clk_hw = NULL;
  140. struct clk_init_data *init = NULL;
  141. const char **parent_names = NULL;
  142. int i;
  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. for (i = 0; i < init->num_parents; i++)
  162. parent_names[i] = of_clk_get_parent_name(node, i);
  163. init->parent_names = parent_names;
  164. ad->control_reg = ti_clk_get_reg_addr(node, 0);
  165. ad->idlest_reg = ti_clk_get_reg_addr(node, 1);
  166. if (!ad->control_reg || !ad->idlest_reg)
  167. goto cleanup;
  168. ad->idlest_mask = 0x1;
  169. ad->enable_mask = 0x3;
  170. omap_clk_register_apll(&clk_hw->hw, node);
  171. return;
  172. cleanup:
  173. kfree(parent_names);
  174. kfree(ad);
  175. kfree(clk_hw);
  176. kfree(init);
  177. }
  178. CLK_OF_DECLARE(dra7_apll_clock, "ti,dra7-apll-clock", of_dra7_apll_setup);