clk-iproc-asiu.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (C) 2014 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/err.h>
  15. #include <linux/clk-provider.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/clkdev.h>
  19. #include <linux/of_address.h>
  20. #include <linux/delay.h>
  21. #include "clk-iproc.h"
  22. struct iproc_asiu;
  23. struct iproc_asiu_clk {
  24. struct clk_hw hw;
  25. const char *name;
  26. struct iproc_asiu *asiu;
  27. unsigned long rate;
  28. struct iproc_asiu_div div;
  29. struct iproc_asiu_gate gate;
  30. };
  31. struct iproc_asiu {
  32. void __iomem *div_base;
  33. void __iomem *gate_base;
  34. struct clk_hw_onecell_data *clk_data;
  35. struct iproc_asiu_clk *clks;
  36. };
  37. #define to_asiu_clk(hw) container_of(hw, struct iproc_asiu_clk, hw)
  38. static int iproc_asiu_clk_enable(struct clk_hw *hw)
  39. {
  40. struct iproc_asiu_clk *clk = to_asiu_clk(hw);
  41. struct iproc_asiu *asiu = clk->asiu;
  42. u32 val;
  43. /* some clocks at the ASIU level are always enabled */
  44. if (clk->gate.offset == IPROC_CLK_INVALID_OFFSET)
  45. return 0;
  46. val = readl(asiu->gate_base + clk->gate.offset);
  47. val |= (1 << clk->gate.en_shift);
  48. writel(val, asiu->gate_base + clk->gate.offset);
  49. return 0;
  50. }
  51. static void iproc_asiu_clk_disable(struct clk_hw *hw)
  52. {
  53. struct iproc_asiu_clk *clk = to_asiu_clk(hw);
  54. struct iproc_asiu *asiu = clk->asiu;
  55. u32 val;
  56. /* some clocks at the ASIU level are always enabled */
  57. if (clk->gate.offset == IPROC_CLK_INVALID_OFFSET)
  58. return;
  59. val = readl(asiu->gate_base + clk->gate.offset);
  60. val &= ~(1 << clk->gate.en_shift);
  61. writel(val, asiu->gate_base + clk->gate.offset);
  62. }
  63. static unsigned long iproc_asiu_clk_recalc_rate(struct clk_hw *hw,
  64. unsigned long parent_rate)
  65. {
  66. struct iproc_asiu_clk *clk = to_asiu_clk(hw);
  67. struct iproc_asiu *asiu = clk->asiu;
  68. u32 val;
  69. unsigned int div_h, div_l;
  70. if (parent_rate == 0) {
  71. clk->rate = 0;
  72. return 0;
  73. }
  74. /* if clock divisor is not enabled, simply return parent rate */
  75. val = readl(asiu->div_base + clk->div.offset);
  76. if ((val & (1 << clk->div.en_shift)) == 0) {
  77. clk->rate = parent_rate;
  78. return parent_rate;
  79. }
  80. /* clock rate = parent rate / (high_div + 1) + (low_div + 1) */
  81. div_h = (val >> clk->div.high_shift) & bit_mask(clk->div.high_width);
  82. div_h++;
  83. div_l = (val >> clk->div.low_shift) & bit_mask(clk->div.low_width);
  84. div_l++;
  85. clk->rate = parent_rate / (div_h + div_l);
  86. pr_debug("%s: rate: %lu. parent rate: %lu div_h: %u div_l: %u\n",
  87. __func__, clk->rate, parent_rate, div_h, div_l);
  88. return clk->rate;
  89. }
  90. static long iproc_asiu_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  91. unsigned long *parent_rate)
  92. {
  93. unsigned int div;
  94. if (rate == 0 || *parent_rate == 0)
  95. return -EINVAL;
  96. if (rate == *parent_rate)
  97. return *parent_rate;
  98. div = DIV_ROUND_UP(*parent_rate, rate);
  99. if (div < 2)
  100. return *parent_rate;
  101. return *parent_rate / div;
  102. }
  103. static int iproc_asiu_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  104. unsigned long parent_rate)
  105. {
  106. struct iproc_asiu_clk *clk = to_asiu_clk(hw);
  107. struct iproc_asiu *asiu = clk->asiu;
  108. unsigned int div, div_h, div_l;
  109. u32 val;
  110. if (rate == 0 || parent_rate == 0)
  111. return -EINVAL;
  112. /* simply disable the divisor if one wants the same rate as parent */
  113. if (rate == parent_rate) {
  114. val = readl(asiu->div_base + clk->div.offset);
  115. val &= ~(1 << clk->div.en_shift);
  116. writel(val, asiu->div_base + clk->div.offset);
  117. return 0;
  118. }
  119. div = DIV_ROUND_UP(parent_rate, rate);
  120. if (div < 2)
  121. return -EINVAL;
  122. div_h = div_l = div >> 1;
  123. div_h--;
  124. div_l--;
  125. val = readl(asiu->div_base + clk->div.offset);
  126. val |= 1 << clk->div.en_shift;
  127. if (div_h) {
  128. val &= ~(bit_mask(clk->div.high_width)
  129. << clk->div.high_shift);
  130. val |= div_h << clk->div.high_shift;
  131. } else {
  132. val &= ~(bit_mask(clk->div.high_width)
  133. << clk->div.high_shift);
  134. }
  135. if (div_l) {
  136. val &= ~(bit_mask(clk->div.low_width) << clk->div.low_shift);
  137. val |= div_l << clk->div.low_shift;
  138. } else {
  139. val &= ~(bit_mask(clk->div.low_width) << clk->div.low_shift);
  140. }
  141. writel(val, asiu->div_base + clk->div.offset);
  142. return 0;
  143. }
  144. static const struct clk_ops iproc_asiu_ops = {
  145. .enable = iproc_asiu_clk_enable,
  146. .disable = iproc_asiu_clk_disable,
  147. .recalc_rate = iproc_asiu_clk_recalc_rate,
  148. .round_rate = iproc_asiu_clk_round_rate,
  149. .set_rate = iproc_asiu_clk_set_rate,
  150. };
  151. void __init iproc_asiu_setup(struct device_node *node,
  152. const struct iproc_asiu_div *div,
  153. const struct iproc_asiu_gate *gate,
  154. unsigned int num_clks)
  155. {
  156. int i, ret;
  157. struct iproc_asiu *asiu;
  158. if (WARN_ON(!gate || !div))
  159. return;
  160. asiu = kzalloc(sizeof(*asiu), GFP_KERNEL);
  161. if (WARN_ON(!asiu))
  162. return;
  163. asiu->clk_data = kzalloc(sizeof(*asiu->clk_data->hws) * num_clks +
  164. sizeof(*asiu->clk_data), GFP_KERNEL);
  165. if (WARN_ON(!asiu->clk_data))
  166. goto err_clks;
  167. asiu->clk_data->num = num_clks;
  168. asiu->clks = kcalloc(num_clks, sizeof(*asiu->clks), GFP_KERNEL);
  169. if (WARN_ON(!asiu->clks))
  170. goto err_asiu_clks;
  171. asiu->div_base = of_iomap(node, 0);
  172. if (WARN_ON(!asiu->div_base))
  173. goto err_iomap_div;
  174. asiu->gate_base = of_iomap(node, 1);
  175. if (WARN_ON(!asiu->gate_base))
  176. goto err_iomap_gate;
  177. for (i = 0; i < num_clks; i++) {
  178. struct clk_init_data init;
  179. const char *parent_name;
  180. struct iproc_asiu_clk *asiu_clk;
  181. const char *clk_name;
  182. ret = of_property_read_string_index(node, "clock-output-names",
  183. i, &clk_name);
  184. if (WARN_ON(ret))
  185. goto err_clk_register;
  186. asiu_clk = &asiu->clks[i];
  187. asiu_clk->name = clk_name;
  188. asiu_clk->asiu = asiu;
  189. asiu_clk->div = div[i];
  190. asiu_clk->gate = gate[i];
  191. init.name = clk_name;
  192. init.ops = &iproc_asiu_ops;
  193. init.flags = 0;
  194. parent_name = of_clk_get_parent_name(node, 0);
  195. init.parent_names = (parent_name ? &parent_name : NULL);
  196. init.num_parents = (parent_name ? 1 : 0);
  197. asiu_clk->hw.init = &init;
  198. ret = clk_hw_register(NULL, &asiu_clk->hw);
  199. if (WARN_ON(ret))
  200. goto err_clk_register;
  201. asiu->clk_data->hws[i] = &asiu_clk->hw;
  202. }
  203. ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get,
  204. asiu->clk_data);
  205. if (WARN_ON(ret))
  206. goto err_clk_register;
  207. return;
  208. err_clk_register:
  209. while (--i >= 0)
  210. clk_hw_unregister(asiu->clk_data->hws[i]);
  211. iounmap(asiu->gate_base);
  212. err_iomap_gate:
  213. iounmap(asiu->div_base);
  214. err_iomap_div:
  215. kfree(asiu->clks);
  216. err_asiu_clks:
  217. kfree(asiu->clk_data);
  218. err_clks:
  219. kfree(asiu);
  220. }