clock-cpg.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include <linux/clk.h>
  2. #include <linux/compiler.h>
  3. #include <linux/slab.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 = kzalloc(freq_table_size * nr, GFP_KERNEL);
  108. if (!freq_table) {
  109. pr_err("sh_clk_div6_register: unable to alloc memory\n");
  110. return -ENOMEM;
  111. }
  112. for (k = 0; !ret && (k < nr); k++) {
  113. clkp = clks + k;
  114. clkp->ops = &sh_clk_div6_clk_ops;
  115. clkp->id = -1;
  116. clkp->freq_table = freq_table + (k * freq_table_size);
  117. clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
  118. ret = clk_register(clkp);
  119. }
  120. return ret;
  121. }
  122. static unsigned long sh_clk_div4_recalc(struct clk *clk)
  123. {
  124. struct clk_div_mult_table *table = clk->priv;
  125. unsigned int idx;
  126. clk_rate_table_build(clk, clk->freq_table, table->nr_divisors,
  127. table, &clk->arch_flags);
  128. idx = (__raw_readl(clk->enable_reg) >> clk->enable_bit) & 0x000f;
  129. return clk->freq_table[idx].frequency;
  130. }
  131. static struct clk_ops sh_clk_div4_clk_ops = {
  132. .recalc = sh_clk_div4_recalc,
  133. .round_rate = sh_clk_div_round_rate,
  134. };
  135. int __init sh_clk_div4_register(struct clk *clks, int nr,
  136. struct clk_div_mult_table *table)
  137. {
  138. struct clk *clkp;
  139. void *freq_table;
  140. int nr_divs = table->nr_divisors;
  141. int freq_table_size = sizeof(struct cpufreq_frequency_table);
  142. int ret = 0;
  143. int k;
  144. freq_table_size *= (nr_divs + 1);
  145. freq_table = kzalloc(freq_table_size * nr, GFP_KERNEL);
  146. if (!freq_table) {
  147. pr_err("sh_clk_div4_register: unable to alloc memory\n");
  148. return -ENOMEM;
  149. }
  150. for (k = 0; !ret && (k < nr); k++) {
  151. clkp = clks + k;
  152. clkp->ops = &sh_clk_div4_clk_ops;
  153. clkp->id = -1;
  154. clkp->priv = table;
  155. clkp->freq_table = freq_table + (k * freq_table_size);
  156. clkp->freq_table[nr_divs].frequency = CPUFREQ_TABLE_END;
  157. ret = clk_register(clkp);
  158. }
  159. return ret;
  160. }
  161. #ifdef CONFIG_SH_CLK_CPG_LEGACY
  162. static struct clk master_clk = {
  163. .name = "master_clk",
  164. .flags = CLK_ENABLE_ON_INIT,
  165. .rate = CONFIG_SH_PCLK_FREQ,
  166. };
  167. static struct clk peripheral_clk = {
  168. .name = "peripheral_clk",
  169. .parent = &master_clk,
  170. .flags = CLK_ENABLE_ON_INIT,
  171. };
  172. static struct clk bus_clk = {
  173. .name = "bus_clk",
  174. .parent = &master_clk,
  175. .flags = CLK_ENABLE_ON_INIT,
  176. };
  177. static struct clk cpu_clk = {
  178. .name = "cpu_clk",
  179. .parent = &master_clk,
  180. .flags = CLK_ENABLE_ON_INIT,
  181. };
  182. /*
  183. * The ordering of these clocks matters, do not change it.
  184. */
  185. static struct clk *onchip_clocks[] = {
  186. &master_clk,
  187. &peripheral_clk,
  188. &bus_clk,
  189. &cpu_clk,
  190. };
  191. int __init __deprecated cpg_clk_init(void)
  192. {
  193. int i, ret = 0;
  194. for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
  195. struct clk *clk = onchip_clocks[i];
  196. arch_init_clk_ops(&clk->ops, i);
  197. if (clk->ops)
  198. ret |= clk_register(clk);
  199. }
  200. return ret;
  201. }
  202. /*
  203. * Placeholder for compatability, until the lazy CPUs do this
  204. * on their own.
  205. */
  206. int __init __weak arch_clk_init(void)
  207. {
  208. return cpg_clk_init();
  209. }
  210. #endif /* CONFIG_SH_CPG_CLK_LEGACY */