clock.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* linux/arch/arm/mach-s3c2443/clock.c
  2. *
  3. * Copyright (c) 2007, 2010 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C2443 Clock control 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 as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/list.h>
  26. #include <linux/errno.h>
  27. #include <linux/err.h>
  28. #include <linux/sysdev.h>
  29. #include <linux/clk.h>
  30. #include <linux/mutex.h>
  31. #include <linux/serial_core.h>
  32. #include <linux/io.h>
  33. #include <asm/mach/map.h>
  34. #include <mach/hardware.h>
  35. #include <mach/regs-s3c2443-clock.h>
  36. #include <plat/cpu-freq.h>
  37. #include <plat/s3c2443.h>
  38. #include <plat/clock.h>
  39. #include <plat/clock-clksrc.h>
  40. #include <plat/cpu.h>
  41. /* We currently have to assume that the system is running
  42. * from the XTPll input, and that all ***REFCLKs are being
  43. * fed from it, as we cannot read the state of OM[4] from
  44. * software.
  45. *
  46. * It would be possible for each board initialisation to
  47. * set the correct muxing at initialisation
  48. */
  49. /* clock selections */
  50. static struct clk clk_i2s_ext = {
  51. .name = "i2s-ext",
  52. .id = -1,
  53. };
  54. /* armdiv
  55. *
  56. * this clock is sourced from msysclk and can have a number of
  57. * divider values applied to it to then be fed into armclk.
  58. */
  59. /* armdiv divisor table */
  60. static unsigned int armdiv[16] = {
  61. [S3C2443_CLKDIV0_ARMDIV_1 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 1,
  62. [S3C2443_CLKDIV0_ARMDIV_2 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 2,
  63. [S3C2443_CLKDIV0_ARMDIV_3 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 3,
  64. [S3C2443_CLKDIV0_ARMDIV_4 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 4,
  65. [S3C2443_CLKDIV0_ARMDIV_6 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 6,
  66. [S3C2443_CLKDIV0_ARMDIV_8 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 8,
  67. [S3C2443_CLKDIV0_ARMDIV_12 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 12,
  68. [S3C2443_CLKDIV0_ARMDIV_16 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 16,
  69. };
  70. static inline unsigned int s3c2443_fclk_div(unsigned long clkcon0)
  71. {
  72. clkcon0 &= S3C2443_CLKDIV0_ARMDIV_MASK;
  73. return armdiv[clkcon0 >> S3C2443_CLKDIV0_ARMDIV_SHIFT];
  74. }
  75. static unsigned long s3c2443_armclk_roundrate(struct clk *clk,
  76. unsigned long rate)
  77. {
  78. unsigned long parent = clk_get_rate(clk->parent);
  79. unsigned long calc;
  80. unsigned best = 256; /* bigger than any value */
  81. unsigned div;
  82. int ptr;
  83. for (ptr = 0; ptr < ARRAY_SIZE(armdiv); ptr++) {
  84. div = armdiv[ptr];
  85. calc = parent / div;
  86. if (calc <= rate && div < best)
  87. best = div;
  88. }
  89. return parent / best;
  90. }
  91. static int s3c2443_armclk_setrate(struct clk *clk, unsigned long rate)
  92. {
  93. unsigned long parent = clk_get_rate(clk->parent);
  94. unsigned long calc;
  95. unsigned div;
  96. unsigned best = 256; /* bigger than any value */
  97. int ptr;
  98. int val = -1;
  99. for (ptr = 0; ptr < ARRAY_SIZE(armdiv); ptr++) {
  100. div = armdiv[ptr];
  101. calc = parent / div;
  102. if (calc <= rate && div < best) {
  103. best = div;
  104. val = ptr;
  105. }
  106. }
  107. if (val >= 0) {
  108. unsigned long clkcon0;
  109. clkcon0 = __raw_readl(S3C2443_CLKDIV0);
  110. clkcon0 &= S3C2443_CLKDIV0_ARMDIV_MASK;
  111. clkcon0 |= val << S3C2443_CLKDIV0_ARMDIV_SHIFT;
  112. __raw_writel(clkcon0, S3C2443_CLKDIV0);
  113. }
  114. return (val == -1) ? -EINVAL : 0;
  115. }
  116. static struct clk clk_armdiv = {
  117. .name = "armdiv",
  118. .id = -1,
  119. .parent = &clk_msysclk.clk,
  120. .ops = &(struct clk_ops) {
  121. .round_rate = s3c2443_armclk_roundrate,
  122. .set_rate = s3c2443_armclk_setrate,
  123. },
  124. };
  125. /* armclk
  126. *
  127. * this is the clock fed into the ARM core itself, from armdiv or from hclk.
  128. */
  129. static struct clk *clk_arm_sources[] = {
  130. [0] = &clk_armdiv,
  131. [1] = &clk_h,
  132. };
  133. static struct clksrc_clk clk_arm = {
  134. .clk = {
  135. .name = "armclk",
  136. .id = -1,
  137. },
  138. .sources = &(struct clksrc_sources) {
  139. .sources = clk_arm_sources,
  140. .nr_sources = ARRAY_SIZE(clk_arm_sources),
  141. },
  142. .reg_src = { .reg = S3C2443_CLKDIV0, .size = 1, .shift = 13 },
  143. };
  144. /* hsspi
  145. *
  146. * high-speed spi clock, sourced from esysclk
  147. */
  148. static struct clksrc_clk clk_hsspi = {
  149. .clk = {
  150. .name = "hsspi",
  151. .id = -1,
  152. .parent = &clk_esysclk.clk,
  153. .ctrlbit = S3C2443_SCLKCON_HSSPICLK,
  154. .enable = s3c2443_clkcon_enable_s,
  155. },
  156. .reg_div = { .reg = S3C2443_CLKDIV1, .size = 2, .shift = 4 },
  157. };
  158. /* clk_hsmcc_div
  159. *
  160. * this clock is sourced from epll, and is fed through a divider,
  161. * to a mux controlled by sclkcon where either it or a extclk can
  162. * be fed to the hsmmc block
  163. */
  164. static struct clksrc_clk clk_hsmmc_div = {
  165. .clk = {
  166. .name = "hsmmc-div",
  167. .id = -1,
  168. .parent = &clk_esysclk.clk,
  169. },
  170. .reg_div = { .reg = S3C2443_CLKDIV1, .size = 2, .shift = 6 },
  171. };
  172. static int s3c2443_setparent_hsmmc(struct clk *clk, struct clk *parent)
  173. {
  174. unsigned long clksrc = __raw_readl(S3C2443_SCLKCON);
  175. clksrc &= ~(S3C2443_SCLKCON_HSMMCCLK_EXT |
  176. S3C2443_SCLKCON_HSMMCCLK_EPLL);
  177. if (parent == &clk_epll)
  178. clksrc |= S3C2443_SCLKCON_HSMMCCLK_EPLL;
  179. else if (parent == &clk_ext)
  180. clksrc |= S3C2443_SCLKCON_HSMMCCLK_EXT;
  181. else
  182. return -EINVAL;
  183. if (clk->usage > 0) {
  184. __raw_writel(clksrc, S3C2443_SCLKCON);
  185. }
  186. clk->parent = parent;
  187. return 0;
  188. }
  189. static int s3c2443_enable_hsmmc(struct clk *clk, int enable)
  190. {
  191. return s3c2443_setparent_hsmmc(clk, clk->parent);
  192. }
  193. static struct clk clk_hsmmc = {
  194. .name = "hsmmc-if",
  195. .id = -1,
  196. .parent = &clk_hsmmc_div.clk,
  197. .enable = s3c2443_enable_hsmmc,
  198. .ops = &(struct clk_ops) {
  199. .set_parent = s3c2443_setparent_hsmmc,
  200. },
  201. };
  202. /* i2s_eplldiv
  203. *
  204. * This clock is the output from the I2S divisor of ESYSCLK, and is separate
  205. * from the mux that comes after it (cannot merge into one single clock)
  206. */
  207. static struct clksrc_clk clk_i2s_eplldiv = {
  208. .clk = {
  209. .name = "i2s-eplldiv",
  210. .id = -1,
  211. .parent = &clk_esysclk.clk,
  212. },
  213. .reg_div = { .reg = S3C2443_CLKDIV1, .size = 4, .shift = 12, },
  214. };
  215. /* i2s-ref
  216. *
  217. * i2s bus reference clock, selectable from external, esysclk or epllref
  218. *
  219. * Note, this used to be two clocks, but was compressed into one.
  220. */
  221. struct clk *clk_i2s_srclist[] = {
  222. [0] = &clk_i2s_eplldiv.clk,
  223. [1] = &clk_i2s_ext,
  224. [2] = &clk_epllref.clk,
  225. [3] = &clk_epllref.clk,
  226. };
  227. static struct clksrc_clk clk_i2s = {
  228. .clk = {
  229. .name = "i2s-if",
  230. .id = -1,
  231. .ctrlbit = S3C2443_SCLKCON_I2SCLK,
  232. .enable = s3c2443_clkcon_enable_s,
  233. },
  234. .sources = &(struct clksrc_sources) {
  235. .sources = clk_i2s_srclist,
  236. .nr_sources = ARRAY_SIZE(clk_i2s_srclist),
  237. },
  238. .reg_src = { .reg = S3C2443_CLKSRC, .size = 2, .shift = 14 },
  239. };
  240. /* standard clock definitions */
  241. static struct clk init_clocks_off[] = {
  242. {
  243. .name = "sdi",
  244. .id = -1,
  245. .parent = &clk_p,
  246. .enable = s3c2443_clkcon_enable_p,
  247. .ctrlbit = S3C2443_PCLKCON_SDI,
  248. }, {
  249. .name = "iis",
  250. .id = -1,
  251. .parent = &clk_p,
  252. .enable = s3c2443_clkcon_enable_p,
  253. .ctrlbit = S3C2443_PCLKCON_IIS,
  254. }, {
  255. .name = "spi",
  256. .id = 0,
  257. .parent = &clk_p,
  258. .enable = s3c2443_clkcon_enable_p,
  259. .ctrlbit = S3C2443_PCLKCON_SPI0,
  260. }, {
  261. .name = "spi",
  262. .id = 1,
  263. .parent = &clk_p,
  264. .enable = s3c2443_clkcon_enable_p,
  265. .ctrlbit = S3C2443_PCLKCON_SPI1,
  266. }
  267. };
  268. static struct clk init_clocks[] = {
  269. };
  270. /* clocks to add straight away */
  271. static struct clksrc_clk *clksrcs[] __initdata = {
  272. &clk_arm,
  273. &clk_i2s_eplldiv,
  274. &clk_i2s,
  275. &clk_hsspi,
  276. &clk_hsmmc_div,
  277. };
  278. static struct clk *clks[] __initdata = {
  279. &clk_hsmmc,
  280. &clk_armdiv,
  281. };
  282. void __init_or_cpufreq s3c2443_setup_clocks(void)
  283. {
  284. s3c2443_common_setup_clocks(s3c2443_get_mpll, s3c2443_fclk_div);
  285. }
  286. void __init s3c2443_init_clocks(int xtal)
  287. {
  288. unsigned long epllcon = __raw_readl(S3C2443_EPLLCON);
  289. int ptr;
  290. clk_epll.rate = s3c2443_get_epll(epllcon, xtal);
  291. clk_epll.parent = &clk_epllref.clk;
  292. s3c2443_common_init_clocks(xtal, s3c2443_get_mpll, s3c2443_fclk_div);
  293. s3c2443_setup_clocks();
  294. s3c24xx_register_clocks(clks, ARRAY_SIZE(clks));
  295. for (ptr = 0; ptr < ARRAY_SIZE(clksrcs); ptr++)
  296. s3c_register_clksrc(clksrcs[ptr], 1);
  297. /* register clocks from clock array */
  298. s3c_register_clocks(init_clocks, ARRAY_SIZE(init_clocks));
  299. /* We must be careful disabling the clocks we are not intending to
  300. * be using at boot time, as subsystems such as the LCD which do
  301. * their own DMA requests to the bus can cause the system to lockup
  302. * if they where in the middle of requesting bus access.
  303. *
  304. * Disabling the LCD clock if the LCD is active is very dangerous,
  305. * and therefore the bootloader should be careful to not enable
  306. * the LCD clock if it is not needed.
  307. */
  308. /* install (and disable) the clocks we do not need immediately */
  309. s3c_register_clocks(init_clocks_off, ARRAY_SIZE(init_clocks_off));
  310. s3c_disable_clocks(init_clocks_off, ARRAY_SIZE(init_clocks_off));
  311. s3c_pwmclk_init();
  312. }