cpg.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Helper routines for SuperH Clock Pulse Generator blocks (CPG).
  3. *
  4. * Copyright (C) 2010 Magnus Damm
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/compiler.h>
  12. #include <linux/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/sh_clk.h>
  15. static int sh_clk_mstp32_enable(struct clk *clk)
  16. {
  17. __raw_writel(__raw_readl(clk->enable_reg) & ~(1 << clk->enable_bit),
  18. clk->enable_reg);
  19. return 0;
  20. }
  21. static void sh_clk_mstp32_disable(struct clk *clk)
  22. {
  23. __raw_writel(__raw_readl(clk->enable_reg) | (1 << clk->enable_bit),
  24. clk->enable_reg);
  25. }
  26. static struct clk_ops sh_clk_mstp32_clk_ops = {
  27. .enable = sh_clk_mstp32_enable,
  28. .disable = sh_clk_mstp32_disable,
  29. .recalc = followparent_recalc,
  30. };
  31. int __init sh_clk_mstp32_register(struct clk *clks, int nr)
  32. {
  33. struct clk *clkp;
  34. int ret = 0;
  35. int k;
  36. for (k = 0; !ret && (k < nr); k++) {
  37. clkp = clks + k;
  38. clkp->ops = &sh_clk_mstp32_clk_ops;
  39. ret |= clk_register(clkp);
  40. }
  41. return ret;
  42. }
  43. static long sh_clk_div_round_rate(struct clk *clk, unsigned long rate)
  44. {
  45. return clk_rate_table_round(clk, clk->freq_table, rate);
  46. }
  47. static int sh_clk_div6_divisors[64] = {
  48. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  49. 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
  50. 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  51. 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
  52. };
  53. static struct clk_div_mult_table sh_clk_div6_table = {
  54. .divisors = sh_clk_div6_divisors,
  55. .nr_divisors = ARRAY_SIZE(sh_clk_div6_divisors),
  56. };
  57. static unsigned long sh_clk_div6_recalc(struct clk *clk)
  58. {
  59. struct clk_div_mult_table *table = &sh_clk_div6_table;
  60. unsigned int idx;
  61. clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
  62. table, NULL);
  63. idx = __raw_readl(clk->enable_reg) & 0x003f;
  64. return clk->freq_table[idx].frequency;
  65. }
  66. static int sh_clk_div6_set_parent(struct clk *clk, struct clk *parent)
  67. {
  68. struct clk_div_mult_table *table = &sh_clk_div6_table;
  69. u32 value;
  70. int ret, i;
  71. if (!clk->parent_table || !clk->parent_num)
  72. return -EINVAL;
  73. /* Search the parent */
  74. for (i = 0; i < clk->parent_num; i++)
  75. if (clk->parent_table[i] == parent)
  76. break;
  77. if (i == clk->parent_num)
  78. return -ENODEV;
  79. ret = clk_reparent(clk, parent);
  80. if (ret < 0)
  81. return ret;
  82. value = __raw_readl(clk->enable_reg) &
  83. ~(((1 << clk->src_width) - 1) << clk->src_shift);
  84. __raw_writel(value | (i << clk->src_shift), clk->enable_reg);
  85. /* Rebuild the frequency table */
  86. clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
  87. table, &clk->arch_flags);
  88. return 0;
  89. }
  90. static int sh_clk_div6_set_rate(struct clk *clk,
  91. unsigned long rate, int algo_id)
  92. {
  93. unsigned long value;
  94. int idx;
  95. idx = clk_rate_table_find(clk, clk->freq_table, rate);
  96. if (idx < 0)
  97. return idx;
  98. value = __raw_readl(clk->enable_reg);
  99. value &= ~0x3f;
  100. value |= idx;
  101. __raw_writel(value, clk->enable_reg);
  102. return 0;
  103. }
  104. static int sh_clk_div6_enable(struct clk *clk)
  105. {
  106. unsigned long value;
  107. int ret;
  108. ret = sh_clk_div6_set_rate(clk, clk->rate, 0);
  109. if (ret == 0) {
  110. value = __raw_readl(clk->enable_reg);
  111. value &= ~0x100; /* clear stop bit to enable clock */
  112. __raw_writel(value, clk->enable_reg);
  113. }
  114. return ret;
  115. }
  116. static void sh_clk_div6_disable(struct clk *clk)
  117. {
  118. unsigned long value;
  119. value = __raw_readl(clk->enable_reg);
  120. value |= 0x100; /* stop clock */
  121. value |= 0x3f; /* VDIV bits must be non-zero, overwrite divider */
  122. __raw_writel(value, clk->enable_reg);
  123. }
  124. static struct clk_ops sh_clk_div6_clk_ops = {
  125. .recalc = sh_clk_div6_recalc,
  126. .round_rate = sh_clk_div_round_rate,
  127. .set_rate = sh_clk_div6_set_rate,
  128. .enable = sh_clk_div6_enable,
  129. .disable = sh_clk_div6_disable,
  130. };
  131. static struct clk_ops sh_clk_div6_reparent_clk_ops = {
  132. .recalc = sh_clk_div6_recalc,
  133. .round_rate = sh_clk_div_round_rate,
  134. .set_rate = sh_clk_div6_set_rate,
  135. .enable = sh_clk_div6_enable,
  136. .disable = sh_clk_div6_disable,
  137. .set_parent = sh_clk_div6_set_parent,
  138. };
  139. static int __init sh_clk_div6_register_ops(struct clk *clks, int nr,
  140. struct clk_ops *ops)
  141. {
  142. struct clk *clkp;
  143. void *freq_table;
  144. int nr_divs = sh_clk_div6_table.nr_divisors;
  145. int freq_table_size = sizeof(struct cpufreq_frequency_table);
  146. int ret = 0;
  147. int k;
  148. freq_table_size *= (nr_divs + 1);
  149. freq_table = kzalloc(freq_table_size * nr, GFP_KERNEL);
  150. if (!freq_table) {
  151. pr_err("sh_clk_div6_register: unable to alloc memory\n");
  152. return -ENOMEM;
  153. }
  154. for (k = 0; !ret && (k < nr); k++) {
  155. clkp = clks + k;
  156. clkp->ops = ops;
  157. clkp->freq_table = freq_table + (k * freq_table_size);
  158. clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
  159. ret = clk_register(clkp);
  160. }
  161. return ret;
  162. }
  163. int __init sh_clk_div6_register(struct clk *clks, int nr)
  164. {
  165. return sh_clk_div6_register_ops(clks, nr, &sh_clk_div6_clk_ops);
  166. }
  167. int __init sh_clk_div6_reparent_register(struct clk *clks, int nr)
  168. {
  169. return sh_clk_div6_register_ops(clks, nr,
  170. &sh_clk_div6_reparent_clk_ops);
  171. }
  172. static unsigned long sh_clk_div4_recalc(struct clk *clk)
  173. {
  174. struct clk_div4_table *d4t = clk->priv;
  175. struct clk_div_mult_table *table = d4t->div_mult_table;
  176. unsigned int idx;
  177. clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
  178. table, &clk->arch_flags);
  179. idx = (__raw_readl(clk->enable_reg) >> clk->enable_bit) & 0x000f;
  180. return clk->freq_table[idx].frequency;
  181. }
  182. static int sh_clk_div4_set_parent(struct clk *clk, struct clk *parent)
  183. {
  184. struct clk_div4_table *d4t = clk->priv;
  185. struct clk_div_mult_table *table = d4t->div_mult_table;
  186. u32 value;
  187. int ret;
  188. /* we really need a better way to determine parent index, but for
  189. * now assume internal parent comes with CLK_ENABLE_ON_INIT set,
  190. * no CLK_ENABLE_ON_INIT means external clock...
  191. */
  192. if (parent->flags & CLK_ENABLE_ON_INIT)
  193. value = __raw_readl(clk->enable_reg) & ~(1 << 7);
  194. else
  195. value = __raw_readl(clk->enable_reg) | (1 << 7);
  196. ret = clk_reparent(clk, parent);
  197. if (ret < 0)
  198. return ret;
  199. __raw_writel(value, clk->enable_reg);
  200. /* Rebiuld the frequency table */
  201. clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
  202. table, &clk->arch_flags);
  203. return 0;
  204. }
  205. static int sh_clk_div4_set_rate(struct clk *clk, unsigned long rate, int algo_id)
  206. {
  207. struct clk_div4_table *d4t = clk->priv;
  208. unsigned long value;
  209. int idx = clk_rate_table_find(clk, clk->freq_table, rate);
  210. if (idx < 0)
  211. return idx;
  212. value = __raw_readl(clk->enable_reg);
  213. value &= ~(0xf << clk->enable_bit);
  214. value |= (idx << clk->enable_bit);
  215. __raw_writel(value, clk->enable_reg);
  216. if (d4t->kick)
  217. d4t->kick(clk);
  218. return 0;
  219. }
  220. static int sh_clk_div4_enable(struct clk *clk)
  221. {
  222. __raw_writel(__raw_readl(clk->enable_reg) & ~(1 << 8), clk->enable_reg);
  223. return 0;
  224. }
  225. static void sh_clk_div4_disable(struct clk *clk)
  226. {
  227. __raw_writel(__raw_readl(clk->enable_reg) | (1 << 8), clk->enable_reg);
  228. }
  229. static struct clk_ops sh_clk_div4_clk_ops = {
  230. .recalc = sh_clk_div4_recalc,
  231. .set_rate = sh_clk_div4_set_rate,
  232. .round_rate = sh_clk_div_round_rate,
  233. };
  234. static struct clk_ops sh_clk_div4_enable_clk_ops = {
  235. .recalc = sh_clk_div4_recalc,
  236. .set_rate = sh_clk_div4_set_rate,
  237. .round_rate = sh_clk_div_round_rate,
  238. .enable = sh_clk_div4_enable,
  239. .disable = sh_clk_div4_disable,
  240. };
  241. static struct clk_ops sh_clk_div4_reparent_clk_ops = {
  242. .recalc = sh_clk_div4_recalc,
  243. .set_rate = sh_clk_div4_set_rate,
  244. .round_rate = sh_clk_div_round_rate,
  245. .enable = sh_clk_div4_enable,
  246. .disable = sh_clk_div4_disable,
  247. .set_parent = sh_clk_div4_set_parent,
  248. };
  249. static int __init sh_clk_div4_register_ops(struct clk *clks, int nr,
  250. struct clk_div4_table *table, struct clk_ops *ops)
  251. {
  252. struct clk *clkp;
  253. void *freq_table;
  254. int nr_divs = table->div_mult_table->nr_divisors;
  255. int freq_table_size = sizeof(struct cpufreq_frequency_table);
  256. int ret = 0;
  257. int k;
  258. freq_table_size *= (nr_divs + 1);
  259. freq_table = kzalloc(freq_table_size * nr, GFP_KERNEL);
  260. if (!freq_table) {
  261. pr_err("sh_clk_div4_register: unable to alloc memory\n");
  262. return -ENOMEM;
  263. }
  264. for (k = 0; !ret && (k < nr); k++) {
  265. clkp = clks + k;
  266. clkp->ops = ops;
  267. clkp->priv = table;
  268. clkp->freq_table = freq_table + (k * freq_table_size);
  269. clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
  270. ret = clk_register(clkp);
  271. }
  272. return ret;
  273. }
  274. int __init sh_clk_div4_register(struct clk *clks, int nr,
  275. struct clk_div4_table *table)
  276. {
  277. return sh_clk_div4_register_ops(clks, nr, table, &sh_clk_div4_clk_ops);
  278. }
  279. int __init sh_clk_div4_enable_register(struct clk *clks, int nr,
  280. struct clk_div4_table *table)
  281. {
  282. return sh_clk_div4_register_ops(clks, nr, table,
  283. &sh_clk_div4_enable_clk_ops);
  284. }
  285. int __init sh_clk_div4_reparent_register(struct clk *clks, int nr,
  286. struct clk_div4_table *table)
  287. {
  288. return sh_clk_div4_register_ops(clks, nr, table,
  289. &sh_clk_div4_reparent_clk_ops);
  290. }