cpg.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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, NULL);
  88. return 0;
  89. }
  90. static int sh_clk_div6_set_rate(struct clk *clk, unsigned long rate)
  91. {
  92. unsigned long value;
  93. int idx;
  94. idx = clk_rate_table_find(clk, clk->freq_table, rate);
  95. if (idx < 0)
  96. return idx;
  97. value = __raw_readl(clk->enable_reg);
  98. value &= ~0x3f;
  99. value |= idx;
  100. __raw_writel(value, clk->enable_reg);
  101. return 0;
  102. }
  103. static int sh_clk_div6_enable(struct clk *clk)
  104. {
  105. unsigned long value;
  106. int ret;
  107. ret = sh_clk_div6_set_rate(clk, clk->rate);
  108. if (ret == 0) {
  109. value = __raw_readl(clk->enable_reg);
  110. value &= ~0x100; /* clear stop bit to enable clock */
  111. __raw_writel(value, clk->enable_reg);
  112. }
  113. return ret;
  114. }
  115. static void sh_clk_div6_disable(struct clk *clk)
  116. {
  117. unsigned long value;
  118. value = __raw_readl(clk->enable_reg);
  119. value |= 0x100; /* stop clock */
  120. value |= 0x3f; /* VDIV bits must be non-zero, overwrite divider */
  121. __raw_writel(value, clk->enable_reg);
  122. }
  123. static struct clk_ops sh_clk_div6_clk_ops = {
  124. .recalc = sh_clk_div6_recalc,
  125. .round_rate = sh_clk_div_round_rate,
  126. .set_rate = sh_clk_div6_set_rate,
  127. .enable = sh_clk_div6_enable,
  128. .disable = sh_clk_div6_disable,
  129. };
  130. static struct clk_ops sh_clk_div6_reparent_clk_ops = {
  131. .recalc = sh_clk_div6_recalc,
  132. .round_rate = sh_clk_div_round_rate,
  133. .set_rate = sh_clk_div6_set_rate,
  134. .enable = sh_clk_div6_enable,
  135. .disable = sh_clk_div6_disable,
  136. .set_parent = sh_clk_div6_set_parent,
  137. };
  138. static int __init sh_clk_div6_register_ops(struct clk *clks, int nr,
  139. struct clk_ops *ops)
  140. {
  141. struct clk *clkp;
  142. void *freq_table;
  143. int nr_divs = sh_clk_div6_table.nr_divisors;
  144. int freq_table_size = sizeof(struct cpufreq_frequency_table);
  145. int ret = 0;
  146. int k;
  147. freq_table_size *= (nr_divs + 1);
  148. freq_table = kzalloc(freq_table_size * nr, GFP_KERNEL);
  149. if (!freq_table) {
  150. pr_err("sh_clk_div6_register: unable to alloc memory\n");
  151. return -ENOMEM;
  152. }
  153. for (k = 0; !ret && (k < nr); k++) {
  154. clkp = clks + k;
  155. clkp->ops = ops;
  156. clkp->freq_table = freq_table + (k * freq_table_size);
  157. clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
  158. ret = clk_register(clkp);
  159. }
  160. return ret;
  161. }
  162. int __init sh_clk_div6_register(struct clk *clks, int nr)
  163. {
  164. return sh_clk_div6_register_ops(clks, nr, &sh_clk_div6_clk_ops);
  165. }
  166. int __init sh_clk_div6_reparent_register(struct clk *clks, int nr)
  167. {
  168. return sh_clk_div6_register_ops(clks, nr,
  169. &sh_clk_div6_reparent_clk_ops);
  170. }
  171. static unsigned long sh_clk_div4_recalc(struct clk *clk)
  172. {
  173. struct clk_div4_table *d4t = clk->priv;
  174. struct clk_div_mult_table *table = d4t->div_mult_table;
  175. unsigned int idx;
  176. clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
  177. table, &clk->arch_flags);
  178. idx = (__raw_readl(clk->enable_reg) >> clk->enable_bit) & 0x000f;
  179. return clk->freq_table[idx].frequency;
  180. }
  181. static int sh_clk_div4_set_parent(struct clk *clk, struct clk *parent)
  182. {
  183. struct clk_div4_table *d4t = clk->priv;
  184. struct clk_div_mult_table *table = d4t->div_mult_table;
  185. u32 value;
  186. int ret;
  187. /* we really need a better way to determine parent index, but for
  188. * now assume internal parent comes with CLK_ENABLE_ON_INIT set,
  189. * no CLK_ENABLE_ON_INIT means external clock...
  190. */
  191. if (parent->flags & CLK_ENABLE_ON_INIT)
  192. value = __raw_readl(clk->enable_reg) & ~(1 << 7);
  193. else
  194. value = __raw_readl(clk->enable_reg) | (1 << 7);
  195. ret = clk_reparent(clk, parent);
  196. if (ret < 0)
  197. return ret;
  198. __raw_writel(value, clk->enable_reg);
  199. /* Rebiuld the frequency table */
  200. clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
  201. table, &clk->arch_flags);
  202. return 0;
  203. }
  204. static int sh_clk_div4_set_rate(struct clk *clk, unsigned long rate)
  205. {
  206. struct clk_div4_table *d4t = clk->priv;
  207. unsigned long value;
  208. int idx = clk_rate_table_find(clk, clk->freq_table, rate);
  209. if (idx < 0)
  210. return idx;
  211. value = __raw_readl(clk->enable_reg);
  212. value &= ~(0xf << clk->enable_bit);
  213. value |= (idx << clk->enable_bit);
  214. __raw_writel(value, clk->enable_reg);
  215. if (d4t->kick)
  216. d4t->kick(clk);
  217. return 0;
  218. }
  219. static int sh_clk_div4_enable(struct clk *clk)
  220. {
  221. __raw_writel(__raw_readl(clk->enable_reg) & ~(1 << 8), clk->enable_reg);
  222. return 0;
  223. }
  224. static void sh_clk_div4_disable(struct clk *clk)
  225. {
  226. __raw_writel(__raw_readl(clk->enable_reg) | (1 << 8), clk->enable_reg);
  227. }
  228. static struct clk_ops sh_clk_div4_clk_ops = {
  229. .recalc = sh_clk_div4_recalc,
  230. .set_rate = sh_clk_div4_set_rate,
  231. .round_rate = sh_clk_div_round_rate,
  232. };
  233. static struct clk_ops sh_clk_div4_enable_clk_ops = {
  234. .recalc = sh_clk_div4_recalc,
  235. .set_rate = sh_clk_div4_set_rate,
  236. .round_rate = sh_clk_div_round_rate,
  237. .enable = sh_clk_div4_enable,
  238. .disable = sh_clk_div4_disable,
  239. };
  240. static struct clk_ops sh_clk_div4_reparent_clk_ops = {
  241. .recalc = sh_clk_div4_recalc,
  242. .set_rate = sh_clk_div4_set_rate,
  243. .round_rate = sh_clk_div_round_rate,
  244. .enable = sh_clk_div4_enable,
  245. .disable = sh_clk_div4_disable,
  246. .set_parent = sh_clk_div4_set_parent,
  247. };
  248. static int __init sh_clk_div4_register_ops(struct clk *clks, int nr,
  249. struct clk_div4_table *table, struct clk_ops *ops)
  250. {
  251. struct clk *clkp;
  252. void *freq_table;
  253. int nr_divs = table->div_mult_table->nr_divisors;
  254. int freq_table_size = sizeof(struct cpufreq_frequency_table);
  255. int ret = 0;
  256. int k;
  257. freq_table_size *= (nr_divs + 1);
  258. freq_table = kzalloc(freq_table_size * nr, GFP_KERNEL);
  259. if (!freq_table) {
  260. pr_err("sh_clk_div4_register: unable to alloc memory\n");
  261. return -ENOMEM;
  262. }
  263. for (k = 0; !ret && (k < nr); k++) {
  264. clkp = clks + k;
  265. clkp->ops = ops;
  266. clkp->priv = table;
  267. clkp->freq_table = freq_table + (k * freq_table_size);
  268. clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
  269. ret = clk_register(clkp);
  270. }
  271. return ret;
  272. }
  273. int __init sh_clk_div4_register(struct clk *clks, int nr,
  274. struct clk_div4_table *table)
  275. {
  276. return sh_clk_div4_register_ops(clks, nr, table, &sh_clk_div4_clk_ops);
  277. }
  278. int __init sh_clk_div4_enable_register(struct clk *clks, int nr,
  279. struct clk_div4_table *table)
  280. {
  281. return sh_clk_div4_register_ops(clks, nr, table,
  282. &sh_clk_div4_enable_clk_ops);
  283. }
  284. int __init sh_clk_div4_reparent_register(struct clk *clks, int nr,
  285. struct clk_div4_table *table)
  286. {
  287. return sh_clk_div4_register_ops(clks, nr, table,
  288. &sh_clk_div4_reparent_clk_ops);
  289. }