clock.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* linux/arch/arm/mach-s5p64x0/clock.c
  2. *
  3. * Copyright (c) 2010 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * S5P64X0 - Clock support
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/list.h>
  16. #include <linux/errno.h>
  17. #include <linux/err.h>
  18. #include <linux/clk.h>
  19. #include <linux/device.h>
  20. #include <linux/io.h>
  21. #include <mach/hardware.h>
  22. #include <mach/map.h>
  23. #include <mach/regs-clock.h>
  24. #include <plat/cpu-freq.h>
  25. #include <plat/clock.h>
  26. #include <plat/cpu.h>
  27. #include <plat/pll.h>
  28. #include <plat/s5p-clock.h>
  29. #include <plat/clock-clksrc.h>
  30. #include "common.h"
  31. struct clksrc_clk clk_mout_apll = {
  32. .clk = {
  33. .name = "mout_apll",
  34. .id = -1,
  35. },
  36. .sources = &clk_src_apll,
  37. .reg_src = { .reg = S5P64X0_CLK_SRC0, .shift = 0, .size = 1 },
  38. };
  39. struct clksrc_clk clk_mout_mpll = {
  40. .clk = {
  41. .name = "mout_mpll",
  42. .id = -1,
  43. },
  44. .sources = &clk_src_mpll,
  45. .reg_src = { .reg = S5P64X0_CLK_SRC0, .shift = 1, .size = 1 },
  46. };
  47. struct clksrc_clk clk_mout_epll = {
  48. .clk = {
  49. .name = "mout_epll",
  50. .id = -1,
  51. },
  52. .sources = &clk_src_epll,
  53. .reg_src = { .reg = S5P64X0_CLK_SRC0, .shift = 2, .size = 1 },
  54. };
  55. enum perf_level {
  56. L0 = 532*1000,
  57. L1 = 266*1000,
  58. L2 = 133*1000,
  59. };
  60. static const u32 clock_table[][3] = {
  61. /*{ARM_CLK, DIVarm, DIVhclk}*/
  62. {L0 * 1000, (0 << ARM_DIV_RATIO_SHIFT), (3 << S5P64X0_CLKDIV0_HCLK_SHIFT)},
  63. {L1 * 1000, (1 << ARM_DIV_RATIO_SHIFT), (1 << S5P64X0_CLKDIV0_HCLK_SHIFT)},
  64. {L2 * 1000, (3 << ARM_DIV_RATIO_SHIFT), (0 << S5P64X0_CLKDIV0_HCLK_SHIFT)},
  65. };
  66. static unsigned long s5p64x0_armclk_get_rate(struct clk *clk)
  67. {
  68. unsigned long rate = clk_get_rate(clk->parent);
  69. u32 clkdiv;
  70. /* divisor mask starts at bit0, so no need to shift */
  71. clkdiv = __raw_readl(ARM_CLK_DIV) & ARM_DIV_MASK;
  72. return rate / (clkdiv + 1);
  73. }
  74. static unsigned long s5p64x0_armclk_round_rate(struct clk *clk,
  75. unsigned long rate)
  76. {
  77. u32 iter;
  78. for (iter = 1 ; iter < ARRAY_SIZE(clock_table) ; iter++) {
  79. if (rate > clock_table[iter][0])
  80. return clock_table[iter-1][0];
  81. }
  82. return clock_table[ARRAY_SIZE(clock_table) - 1][0];
  83. }
  84. static int s5p64x0_armclk_set_rate(struct clk *clk, unsigned long rate)
  85. {
  86. u32 round_tmp;
  87. u32 iter;
  88. u32 clk_div0_tmp;
  89. u32 cur_rate = clk->ops->get_rate(clk);
  90. unsigned long flags;
  91. round_tmp = clk->ops->round_rate(clk, rate);
  92. if (round_tmp == cur_rate)
  93. return 0;
  94. for (iter = 0 ; iter < ARRAY_SIZE(clock_table) ; iter++) {
  95. if (round_tmp == clock_table[iter][0])
  96. break;
  97. }
  98. if (iter >= ARRAY_SIZE(clock_table))
  99. iter = ARRAY_SIZE(clock_table) - 1;
  100. local_irq_save(flags);
  101. if (cur_rate > round_tmp) {
  102. /* Frequency Down */
  103. clk_div0_tmp = __raw_readl(ARM_CLK_DIV) & ~(ARM_DIV_MASK);
  104. clk_div0_tmp |= clock_table[iter][1];
  105. __raw_writel(clk_div0_tmp, ARM_CLK_DIV);
  106. clk_div0_tmp = __raw_readl(ARM_CLK_DIV) &
  107. ~(S5P64X0_CLKDIV0_HCLK_MASK);
  108. clk_div0_tmp |= clock_table[iter][2];
  109. __raw_writel(clk_div0_tmp, ARM_CLK_DIV);
  110. } else {
  111. /* Frequency Up */
  112. clk_div0_tmp = __raw_readl(ARM_CLK_DIV) &
  113. ~(S5P64X0_CLKDIV0_HCLK_MASK);
  114. clk_div0_tmp |= clock_table[iter][2];
  115. __raw_writel(clk_div0_tmp, ARM_CLK_DIV);
  116. clk_div0_tmp = __raw_readl(ARM_CLK_DIV) & ~(ARM_DIV_MASK);
  117. clk_div0_tmp |= clock_table[iter][1];
  118. __raw_writel(clk_div0_tmp, ARM_CLK_DIV);
  119. }
  120. local_irq_restore(flags);
  121. clk->rate = clock_table[iter][0];
  122. return 0;
  123. }
  124. static struct clk_ops s5p64x0_clkarm_ops = {
  125. .get_rate = s5p64x0_armclk_get_rate,
  126. .set_rate = s5p64x0_armclk_set_rate,
  127. .round_rate = s5p64x0_armclk_round_rate,
  128. };
  129. struct clksrc_clk clk_armclk = {
  130. .clk = {
  131. .name = "armclk",
  132. .id = 1,
  133. .parent = &clk_mout_apll.clk,
  134. .ops = &s5p64x0_clkarm_ops,
  135. },
  136. .reg_div = { .reg = S5P64X0_CLK_DIV0, .shift = 0, .size = 4 },
  137. };
  138. struct clksrc_clk clk_dout_mpll = {
  139. .clk = {
  140. .name = "dout_mpll",
  141. .id = -1,
  142. .parent = &clk_mout_mpll.clk,
  143. },
  144. .reg_div = { .reg = S5P64X0_CLK_DIV0, .shift = 4, .size = 1 },
  145. };
  146. static struct clk *clkset_hclk_low_list[] = {
  147. &clk_mout_apll.clk,
  148. &clk_mout_mpll.clk,
  149. };
  150. struct clksrc_sources clkset_hclk_low = {
  151. .sources = clkset_hclk_low_list,
  152. .nr_sources = ARRAY_SIZE(clkset_hclk_low_list),
  153. };
  154. int s5p64x0_pclk_ctrl(struct clk *clk, int enable)
  155. {
  156. return s5p_gatectrl(S5P64X0_CLK_GATE_PCLK, clk, enable);
  157. }
  158. int s5p64x0_hclk0_ctrl(struct clk *clk, int enable)
  159. {
  160. return s5p_gatectrl(S5P64X0_CLK_GATE_HCLK0, clk, enable);
  161. }
  162. int s5p64x0_hclk1_ctrl(struct clk *clk, int enable)
  163. {
  164. return s5p_gatectrl(S5P64X0_CLK_GATE_HCLK1, clk, enable);
  165. }
  166. int s5p64x0_sclk_ctrl(struct clk *clk, int enable)
  167. {
  168. return s5p_gatectrl(S5P64X0_CLK_GATE_SCLK0, clk, enable);
  169. }
  170. int s5p64x0_sclk1_ctrl(struct clk *clk, int enable)
  171. {
  172. return s5p_gatectrl(S5P64X0_CLK_GATE_SCLK1, clk, enable);
  173. }
  174. int s5p64x0_mem_ctrl(struct clk *clk, int enable)
  175. {
  176. return s5p_gatectrl(S5P64X0_CLK_GATE_MEM0, clk, enable);
  177. }
  178. int s5p64x0_clk48m_ctrl(struct clk *clk, int enable)
  179. {
  180. unsigned long flags;
  181. u32 val;
  182. /* can't rely on clock lock, this register has other usages */
  183. local_irq_save(flags);
  184. val = __raw_readl(S5P64X0_OTHERS);
  185. if (enable)
  186. val |= S5P64X0_OTHERS_USB_SIG_MASK;
  187. else
  188. val &= ~S5P64X0_OTHERS_USB_SIG_MASK;
  189. __raw_writel(val, S5P64X0_OTHERS);
  190. local_irq_restore(flags);
  191. return 0;
  192. }