memoryCall.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * SH7372 clock framework support
  3. *
  4. * Copyright (C) 2010 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/io.h>
  22. #include <linux/sh_clk.h>
  23. #include <linux/clkdev.h>
  24. #include <mach/common.h>
  25. /* SH7372 registers */
  26. #define FRQCRA IOMEM(0xe6150000)
  27. #define FRQCRB IOMEM(0xe6150004)
  28. #define FRQCRC IOMEM(0xe61500e0)
  29. #define FRQCRD IOMEM(0xe61500e4)
  30. #define VCLKCR1 IOMEM(0xe6150008)
  31. #define VCLKCR2 IOMEM(0xe615000c)
  32. #define VCLKCR3 IOMEM(0xe615001c)
  33. #define FMSICKCR IOMEM(0xe6150010)
  34. #define FMSOCKCR IOMEM(0xe6150014)
  35. #define FSIACKCR IOMEM(0xe6150018)
  36. #define FSIBCKCR IOMEM(0xe6150090)
  37. #define SUBCKCR IOMEM(0xe6150080)
  38. #define SPUCKCR IOMEM(0xe6150084)
  39. #define VOUCKCR IOMEM(0xe6150088)
  40. #define HDMICKCR IOMEM(0xe6150094)
  41. #define DSITCKCR IOMEM(0xe6150060)
  42. #define DSI0PCKCR IOMEM(0xe6150064)
  43. #define DSI1PCKCR IOMEM(0xe6150098)
  44. #define PLLC01CR IOMEM(0xe6150028)
  45. #define PLLC2CR IOMEM(0xe615002c)
  46. #define RMSTPCR0 IOMEM(0xe6150110)
  47. #define RMSTPCR1 IOMEM(0xe6150114)
  48. #define RMSTPCR2 IOMEM(0xe6150118)
  49. #define RMSTPCR3 IOMEM(0xe615011c)
  50. #define RMSTPCR4 IOMEM(0xe6150120)
  51. #define SMSTPCR0 IOMEM(0xe6150130)
  52. #define SMSTPCR1 IOMEM(0xe6150134)
  53. #define SMSTPCR2 IOMEM(0xe6150138)
  54. #define SMSTPCR3 IOMEM(0xe615013c)
  55. #define SMSTPCR4 IOMEM(0xe6150140)
  56. #define FSIDIVA 0xFE1F8000
  57. #define FSIDIVB 0xFE1F8008
  58. /* Platforms must set frequency on their DV_CLKI pin */
  59. struct clk sh7372_dv_clki_clk = {
  60. };
  61. /* Fixed 32 KHz root clock from EXTALR pin */
  62. static struct clk r_clk = {
  63. .rate = 32768,
  64. };
  65. /*
  66. * 26MHz default rate for the EXTAL1 root input clock.
  67. * If needed, reset this with clk_set_rate() from the platform code.
  68. */
  69. struct clk sh7372_extal1_clk = {
  70. .rate = 26000000,
  71. };
  72. /*
  73. * 48MHz default rate for the EXTAL2 root input clock.
  74. * If needed, reset this with clk_set_rate() from the platform code.
  75. */
  76. struct clk sh7372_extal2_clk = {
  77. .rate = 48000000,
  78. };
  79. /* A fixed divide-by-2 block */
  80. static unsigned long div2_recalc(struct clk *clk)
  81. {
  82. return clk->parent->rate / 2;
  83. }
  84. static struct sh_clk_ops div2_clk_ops = {
  85. .recalc = div2_recalc,
  86. };
  87. /* Divide dv_clki by two */
  88. struct clk sh7372_dv_clki_div2_clk = {
  89. .ops = &div2_clk_ops,
  90. .parent = &sh7372_dv_clki_clk,
  91. };
  92. /* Divide extal1 by two */
  93. static struct clk extal1_div2_clk = {
  94. .ops = &div2_clk_ops,
  95. .parent = &sh7372_extal1_clk,
  96. };
  97. /* Divide extal2 by two */
  98. static struct clk extal2_div2_clk = {
  99. .ops = &div2_clk_ops,
  100. .parent = &sh7372_extal2_clk,
  101. };
  102. /* Divide extal2 by four */
  103. static struct clk extal2_div4_clk = {
  104. .ops = &div2_clk_ops,
  105. .parent = &extal2_div2_clk,
  106. };
  107. /* PLLC0 and PLLC1 */
  108. static unsigned long pllc01_recalc(struct clk *clk)
  109. {
  110. unsigned long mult = 1;
  111. if (__raw_readl(PLLC01CR) & (1 << 14))
  112. mult = (((__raw_readl(clk->enable_reg) >> 24) & 0x3f) + 1) * 2;
  113. return clk->parent->rate * mult;
  114. }
  115. static struct sh_clk_ops pllc01_clk_ops = {
  116. .recalc = pllc01_recalc,
  117. };
  118. static struct clk pllc0_clk = {
  119. .ops = &pllc01_clk_ops,
  120. .flags = CLK_ENABLE_ON_INIT,
  121. .parent = &extal1_div2_clk,
  122. .enable_reg = (void __iomem *)FRQCRC,
  123. };
  124. static struct clk pllc1_clk = {
  125. .ops = &pllc01_clk_ops,
  126. .flags = CLK_ENABLE_ON_INIT,
  127. .parent = &extal1_div2_clk,
  128. .enable_reg = (void __iomem *)FRQCRA,
  129. };
  130. /* Divide PLLC1 by two */
  131. static struct clk pllc1_div2_clk = {
  132. .ops = &div2_clk_ops,
  133. .parent = &pllc1_clk,
  134. };
  135. /* PLLC2 */
  136. /* Indices are important - they are the actual src selecting values */
  137. static struct clk *pllc2_parent[] = {
  138. [0] = &extal1_div2_clk,
  139. [1] = &extal2_div2_clk,
  140. [2] = &sh7372_dv_clki_div2_clk,
  141. };
  142. /* Only multipliers 20 * 2 to 46 * 2 are valid, last entry for CPUFREQ_TABLE_END */
  143. static struct cpufreq_frequency_table pllc2_freq_table[29];
  144. static void pllc2_table_rebuild(struct clk *clk)
  145. {
  146. int i;
  147. /* Initialise PLLC2 frequency table */
  148. for (i = 0; i < ARRAY_SIZE(pllc2_freq_table) - 2; i++) {
  149. pllc2_freq_table[i].frequency = clk->parent->rate * (i + 20) * 2;
  150. pllc2_freq_table[i].index = i;
  151. }
  152. /* This is a special entry - switching PLL off makes it a repeater */
  153. pllc2_freq_table[i].frequency = clk->parent->rate;
  154. pllc2_freq_table[i].index = i;
  155. pllc2_freq_table[++i].frequency = CPUFREQ_TABLE_END;
  156. pllc2_freq_table[i].index = i;
  157. }
  158. static unsigned long pllc2_recalc(struct clk *clk)
  159. {
  160. unsigned long mult = 1;
  161. pllc2_table_rebuild(clk);
  162. /*
  163. * If the PLL is off, mult == 1, clk->rate will be updated in
  164. * pllc2_enable().
  165. */
  166. if (__raw_readl(PLLC2CR) & (1 << 31))
  167. mult = (((__raw_readl(PLLC2CR) >> 24) & 0x3f) + 1) * 2;
  168. return clk->parent->rate * mult;
  169. }
  170. static long pllc2_round_rate(struct clk *clk, unsigned long rate)
  171. {
  172. return clk_rate_table_round(clk, clk->freq_table, rate);
  173. }
  174. static int pllc2_enable(struct clk *clk)
  175. {
  176. int i;
  177. __raw_writel(__raw_readl(PLLC2CR) | 0x80000000, PLLC2CR);
  178. for (i = 0; i < 100; i++)
  179. if (__raw_readl(PLLC2CR) & 0x80000000) {
  180. clk->rate = pllc2_recalc(clk);
  181. return 0;
  182. }
  183. pr_err("%s(): timeout!\n", __func__);
  184. return -ETIMEDOUT;
  185. }
  186. static void pllc2_disable(struct clk *clk)
  187. {
  188. __raw_writel(__raw_readl(PLLC2CR) & ~0x80000000, PLLC2CR);
  189. }
  190. static int pllc2_set_rate(struct clk *clk, unsigned long rate)
  191. {
  192. unsigned long value;
  193. int idx;
  194. idx = clk_rate_table_find(clk, clk->freq_table, rate);
  195. if (idx < 0)
  196. return idx;
  197. if (rate == clk->parent->rate)
  198. return -EINVAL;
  199. value = __raw_readl(PLLC2CR) & ~(0x3f << 24);
  200. __raw_writel(value | ((idx + 19) << 24), PLLC2CR);
  201. clk->rate = clk->freq_table[idx].frequency;
  202. return 0;
  203. }
  204. static int pllc2_set_parent(struct clk *clk, struct clk *parent)
  205. {
  206. u32 value;
  207. int ret, i;
  208. if (!clk->parent_table || !clk->parent_num)
  209. return -EINVAL;
  210. /* Search the parent */
  211. for (i = 0; i < clk->parent_num; i++)
  212. if (clk->parent_table[i] == parent)
  213. break;
  214. if (i == clk->parent_num)
  215. return -ENODEV;
  216. ret = clk_reparent(clk, parent);
  217. if (ret < 0)
  218. return ret;
  219. value = __raw_readl(PLLC2CR) & ~(3 << 6);
  220. __raw_writel(value | (i << 6), PLLC2CR);
  221. /* Rebiuld the frequency table */
  222. pllc2_table_rebuild(clk);
  223. return 0;
  224. }
  225. static struct sh_clk_ops pllc2_clk_ops = {
  226. .recalc = pllc2_recalc,
  227. .round_rate = pllc2_round_rate,
  228. .set_rate = pllc2_set_rate,
  229. .enable = pllc2_enable,
  230. .disable = pllc2_disable,
  231. .set_parent = pllc2_set_parent,
  232. };
  233. struct clk sh7372_pllc2_clk = {
  234. .ops = &pllc2_clk_ops,
  235. .parent = &extal1_div2_clk,
  236. .freq_table = pllc2_freq_table,
  237. .nr_freqs = ARRAY_SIZE(pllc2_freq_table) - 1,
  238. .parent_table = pllc2_parent,
  239. .parent_num = ARRAY_SIZE(pllc2_parent),
  240. };
  241. /* External input clock (pin name: FSIACK/FSIBCK ) */
  242. static struct clk fsiack_clk = {
  243. };
  244. static struct clk fsibck_clk = {
  245. };
  246. static struct clk *main_clks[] = {
  247. &sh7372_dv_clki_clk,
  248. &r_clk,
  249. &sh7372_extal1_clk,
  250. &sh7372_extal2_clk,
  251. &sh7372_dv_clki_div2_clk,
  252. &extal1_div2_clk,
  253. &extal2_div2_clk,
  254. &extal2_div4_clk,
  255. &pllc0_clk,
  256. &pllc1_clk,
  257. &pllc1_div2_clk,
  258. &sh7372_pllc2_clk,
  259. &fsiack_clk,
  260. &fsibck_clk,
  261. };
  262. static void div4_kick(struct clk *clk)
  263. {
  264. unsigned long value;
  265. /* set KICK bit in FRQCRB to update hardware setting */
  266. value = __raw_readl(FRQCRB);
  267. value |= (1 << 31);
  268. __raw_writel(value, FRQCRB);
  269. }