clock-cpg.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include <linux/clk.h>
  2. #include <linux/compiler.h>
  3. #include <linux/bootmem.h>
  4. #include <linux/io.h>
  5. #include <asm/clock.h>
  6. static int sh_clk_mstp32_enable(struct clk *clk)
  7. {
  8. __raw_writel(__raw_readl(clk->enable_reg) & ~(1 << clk->enable_bit),
  9. clk->enable_reg);
  10. return 0;
  11. }
  12. static void sh_clk_mstp32_disable(struct clk *clk)
  13. {
  14. __raw_writel(__raw_readl(clk->enable_reg) | (1 << clk->enable_bit),
  15. clk->enable_reg);
  16. }
  17. static struct clk_ops sh_clk_mstp32_clk_ops = {
  18. .enable = sh_clk_mstp32_enable,
  19. .disable = sh_clk_mstp32_disable,
  20. .recalc = followparent_recalc,
  21. };
  22. int __init sh_clk_mstp32_register(struct clk *clks, int nr)
  23. {
  24. struct clk *clkp;
  25. int ret = 0;
  26. int k;
  27. for (k = 0; !ret && (k < nr); k++) {
  28. clkp = clks + k;
  29. clkp->ops = &sh_clk_mstp32_clk_ops;
  30. ret |= clk_register(clkp);
  31. }
  32. return ret;
  33. }
  34. static long sh_clk_div_round_rate(struct clk *clk, unsigned long rate)
  35. {
  36. return clk_rate_table_round(clk, clk->freq_table, rate);
  37. }
  38. static int sh_clk_div6_divisors[64] = {
  39. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  40. 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
  41. 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  42. 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
  43. };
  44. static struct clk_div_mult_table sh_clk_div6_table = {
  45. .divisors = sh_clk_div6_divisors,
  46. .nr_divisors = ARRAY_SIZE(sh_clk_div6_divisors),
  47. };
  48. static unsigned long sh_clk_div6_recalc(struct clk *clk)
  49. {
  50. struct clk_div_mult_table *table = &sh_clk_div6_table;
  51. unsigned int idx;
  52. clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
  53. table, NULL);
  54. idx = __raw_readl(clk->enable_reg) & 0x003f;
  55. return clk->freq_table[idx].frequency;
  56. }
  57. static int sh_clk_div6_set_rate(struct clk *clk,
  58. unsigned long rate, int algo_id)
  59. {
  60. unsigned long value;
  61. int idx;
  62. idx = clk_rate_table_find(clk, clk->freq_table, rate);
  63. if (idx < 0)
  64. return idx;
  65. value = __raw_readl(clk->enable_reg);
  66. value &= ~0x3f;
  67. value |= idx;
  68. __raw_writel(value, clk->enable_reg);
  69. return 0;
  70. }
  71. static int sh_clk_div6_enable(struct clk *clk)
  72. {
  73. unsigned long value;
  74. int ret;
  75. ret = sh_clk_div6_set_rate(clk, clk->rate, 0);
  76. if (ret == 0) {
  77. value = __raw_readl(clk->enable_reg);
  78. value &= ~0x100; /* clear stop bit to enable clock */
  79. __raw_writel(value, clk->enable_reg);
  80. }
  81. return ret;
  82. }
  83. static void sh_clk_div6_disable(struct clk *clk)
  84. {
  85. unsigned long value;
  86. value = __raw_readl(clk->enable_reg);
  87. value |= 0x100; /* stop clock */
  88. value |= 0x3f; /* VDIV bits must be non-zero, overwrite divider */
  89. __raw_writel(value, clk->enable_reg);
  90. }
  91. static struct clk_ops sh_clk_div6_clk_ops = {
  92. .recalc = sh_clk_div6_recalc,
  93. .round_rate = sh_clk_div_round_rate,
  94. .set_rate = sh_clk_div6_set_rate,
  95. .enable = sh_clk_div6_enable,
  96. .disable = sh_clk_div6_disable,
  97. };
  98. int __init sh_clk_div6_register(struct clk *clks, int nr)
  99. {
  100. struct clk *clkp;
  101. void *freq_table;
  102. int nr_divs = sh_clk_div6_table.nr_divisors;
  103. int freq_table_size = sizeof(struct cpufreq_frequency_table);
  104. int ret = 0;
  105. int k;
  106. freq_table_size *= (nr_divs + 1);
  107. freq_table = alloc_bootmem(freq_table_size * nr);
  108. if (!freq_table)
  109. return -ENOMEM;
  110. for (k = 0; !ret && (k < nr); k++) {
  111. clkp = clks + k;
  112. clkp->ops = &sh_clk_div6_clk_ops;
  113. clkp->id = -1;
  114. clkp->freq_table = freq_table + (k * freq_table_size);
  115. clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
  116. ret = clk_register(clkp);
  117. }
  118. return ret;
  119. }
  120. static unsigned long sh_clk_div4_recalc(struct clk *clk)
  121. {
  122. struct clk_div_mult_table *table = clk->priv;
  123. unsigned int idx;
  124. clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
  125. table, &clk->arch_flags);
  126. idx = (__raw_readl(clk->enable_reg) >> clk->enable_bit) & 0x000f;
  127. return clk->freq_table[idx].frequency;
  128. }
  129. static struct clk_ops sh_clk_div4_clk_ops = {
  130. .recalc = sh_clk_div4_recalc,
  131. .round_rate = sh_clk_div_round_rate,
  132. };
  133. int __init sh_clk_div4_register(struct clk *clks, int nr,
  134. struct clk_div_mult_table *table)
  135. {
  136. struct clk *clkp;
  137. void *freq_table;
  138. int nr_divs = table->nr_divisors;
  139. int freq_table_size = sizeof(struct cpufreq_frequency_table);
  140. int ret = 0;
  141. int k;
  142. freq_table_size *= (nr_divs + 1);
  143. freq_table = alloc_bootmem(freq_table_size * nr);
  144. if (!freq_table)
  145. return -ENOMEM;
  146. for (k = 0; !ret && (k < nr); k++) {
  147. clkp = clks + k;
  148. clkp->ops = &sh_clk_div4_clk_ops;
  149. clkp->id = -1;
  150. clkp->priv = table;
  151. clkp->freq_table = freq_table + (k * freq_table_size);
  152. clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
  153. ret = clk_register(clkp);
  154. }
  155. return ret;
  156. }
  157. #ifdef CONFIG_SH_CLK_CPG_LEGACY
  158. static struct clk master_clk = {
  159. .name = "master_clk",
  160. .flags = CLK_ENABLE_ON_INIT,
  161. .rate = CONFIG_SH_PCLK_FREQ,
  162. };
  163. static struct clk peripheral_clk = {
  164. .name = "peripheral_clk",
  165. .parent = &master_clk,
  166. .flags = CLK_ENABLE_ON_INIT,
  167. };
  168. static struct clk bus_clk = {
  169. .name = "bus_clk",
  170. .parent = &master_clk,
  171. .flags = CLK_ENABLE_ON_INIT,
  172. };
  173. static struct clk cpu_clk = {
  174. .name = "cpu_clk",
  175. .parent = &master_clk,
  176. .flags = CLK_ENABLE_ON_INIT,
  177. };
  178. /*
  179. * The ordering of these clocks matters, do not change it.
  180. */
  181. static struct clk *onchip_clocks[] = {
  182. &master_clk,
  183. &peripheral_clk,
  184. &bus_clk,
  185. &cpu_clk,
  186. };
  187. int __init __deprecated cpg_clk_init(void)
  188. {
  189. int i, ret = 0;
  190. for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
  191. struct clk *clk = onchip_clocks[i];
  192. arch_init_clk_ops(&clk->ops, i);
  193. if (clk->ops)
  194. ret |= clk_register(clk);
  195. }
  196. return ret;
  197. }
  198. /*
  199. * Placeholder for compatability, until the lazy CPUs do this
  200. * on their own.
  201. */
  202. int __init __weak arch_clk_init(void)
  203. {
  204. return cpg_clk_init();
  205. }
  206. #endif /* CONFIG_SH_CPG_CLK_LEGACY */