clock.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. };
  53. /* armdiv
  54. *
  55. * this clock is sourced from msysclk and can have a number of
  56. * divider values applied to it to then be fed into armclk.
  57. */
  58. /* armdiv divisor table */
  59. static unsigned int armdiv[16] = {
  60. [S3C2443_CLKDIV0_ARMDIV_1 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 1,
  61. [S3C2443_CLKDIV0_ARMDIV_2 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 2,
  62. [S3C2443_CLKDIV0_ARMDIV_3 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 3,
  63. [S3C2443_CLKDIV0_ARMDIV_4 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 4,
  64. [S3C2443_CLKDIV0_ARMDIV_6 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 6,
  65. [S3C2443_CLKDIV0_ARMDIV_8 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 8,
  66. [S3C2443_CLKDIV0_ARMDIV_12 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 12,
  67. [S3C2443_CLKDIV0_ARMDIV_16 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 16,
  68. };
  69. static inline unsigned int s3c2443_fclk_div(unsigned long clkcon0)
  70. {
  71. clkcon0 &= S3C2443_CLKDIV0_ARMDIV_MASK;
  72. return armdiv[clkcon0 >> S3C2443_CLKDIV0_ARMDIV_SHIFT];
  73. }
  74. static unsigned long s3c2443_armclk_roundrate(struct clk *clk,
  75. unsigned long rate)
  76. {
  77. unsigned long parent = clk_get_rate(clk->parent);
  78. unsigned long calc;
  79. unsigned best = 256; /* bigger than any value */
  80. unsigned div;
  81. int ptr;
  82. for (ptr = 0; ptr < ARRAY_SIZE(armdiv); ptr++) {
  83. div = armdiv[ptr];
  84. calc = parent / div;
  85. if (calc <= rate && div < best)
  86. best = div;
  87. }
  88. return parent / best;
  89. }
  90. static int s3c2443_armclk_setrate(struct clk *clk, unsigned long rate)
  91. {
  92. unsigned long parent = clk_get_rate(clk->parent);
  93. unsigned long calc;
  94. unsigned div;
  95. unsigned best = 256; /* bigger than any value */
  96. int ptr;
  97. int val = -1;
  98. for (ptr = 0; ptr < ARRAY_SIZE(armdiv); ptr++) {
  99. div = armdiv[ptr];
  100. calc = parent / div;
  101. if (calc <= rate && div < best) {
  102. best = div;
  103. val = ptr;
  104. }
  105. }
  106. if (val >= 0) {
  107. unsigned long clkcon0;
  108. clkcon0 = __raw_readl(S3C2443_CLKDIV0);
  109. clkcon0 &= S3C2443_CLKDIV0_ARMDIV_MASK;
  110. clkcon0 |= val << S3C2443_CLKDIV0_ARMDIV_SHIFT;
  111. __raw_writel(clkcon0, S3C2443_CLKDIV0);
  112. }
  113. return (val == -1) ? -EINVAL : 0;
  114. }
  115. static struct clk clk_armdiv = {
  116. .name = "armdiv",
  117. .parent = &clk_msysclk.clk,
  118. .ops = &(struct clk_ops) {
  119. .round_rate = s3c2443_armclk_roundrate,
  120. .set_rate = s3c2443_armclk_setrate,
  121. },
  122. };
  123. /* armclk
  124. *
  125. * this is the clock fed into the ARM core itself, from armdiv or from hclk.
  126. */
  127. static struct clk *clk_arm_sources[] = {
  128. [0] = &clk_armdiv,
  129. [1] = &clk_h,
  130. };
  131. static struct clksrc_clk clk_arm = {
  132. .clk = {
  133. .name = "armclk",
  134. },
  135. .sources = &(struct clksrc_sources) {
  136. .sources = clk_arm_sources,
  137. .nr_sources = ARRAY_SIZE(clk_arm_sources),
  138. },
  139. .reg_src = { .reg = S3C2443_CLKDIV0, .size = 1, .shift = 13 },
  140. };
  141. /* hsspi
  142. *
  143. * high-speed spi clock, sourced from esysclk
  144. */
  145. static struct clksrc_clk clk_hsspi = {
  146. .clk = {
  147. .name = "hsspi",
  148. .parent = &clk_esysclk.clk,
  149. .ctrlbit = S3C2443_SCLKCON_HSSPICLK,
  150. .enable = s3c2443_clkcon_enable_s,
  151. },
  152. .reg_div = { .reg = S3C2443_CLKDIV1, .size = 2, .shift = 4 },
  153. };
  154. /* clk_hsmcc_div
  155. *
  156. * this clock is sourced from epll, and is fed through a divider,
  157. * to a mux controlled by sclkcon where either it or a extclk can
  158. * be fed to the hsmmc block
  159. */
  160. static struct clksrc_clk clk_hsmmc_div = {
  161. .clk = {
  162. .name = "hsmmc-div",
  163. .devname = "s3c-sdhci.1",
  164. .parent = &clk_esysclk.clk,
  165. },
  166. .reg_div = { .reg = S3C2443_CLKDIV1, .size = 2, .shift = 6 },
  167. };
  168. static int s3c2443_setparent_hsmmc(struct clk *clk, struct clk *parent)
  169. {
  170. unsigned long clksrc = __raw_readl(S3C2443_SCLKCON);
  171. clksrc &= ~(S3C2443_SCLKCON_HSMMCCLK_EXT |
  172. S3C2443_SCLKCON_HSMMCCLK_EPLL);
  173. if (parent == &clk_epll)
  174. clksrc |= S3C2443_SCLKCON_HSMMCCLK_EPLL;
  175. else if (parent == &clk_ext)
  176. clksrc |= S3C2443_SCLKCON_HSMMCCLK_EXT;
  177. else
  178. return -EINVAL;
  179. if (clk->usage > 0) {
  180. __raw_writel(clksrc, S3C2443_SCLKCON);
  181. }
  182. clk->parent = parent;
  183. return 0;
  184. }
  185. static int s3c2443_enable_hsmmc(struct clk *clk, int enable)
  186. {
  187. return s3c2443_setparent_hsmmc(clk, clk->parent);
  188. }
  189. static struct clk clk_hsmmc = {
  190. .name = "hsmmc-if",
  191. .devname = "s3c-sdhci.1",
  192. .parent = &clk_hsmmc_div.clk,
  193. .enable = s3c2443_enable_hsmmc,
  194. .ops = &(struct clk_ops) {
  195. .set_parent = s3c2443_setparent_hsmmc,
  196. },
  197. };
  198. /* i2s_eplldiv
  199. *
  200. * This clock is the output from the I2S divisor of ESYSCLK, and is separate
  201. * from the mux that comes after it (cannot merge into one single clock)
  202. */
  203. static struct clksrc_clk clk_i2s_eplldiv = {
  204. .clk = {
  205. .name = "i2s-eplldiv",
  206. .parent = &clk_esysclk.clk,
  207. },
  208. .reg_div = { .reg = S3C2443_CLKDIV1, .size = 4, .shift = 12, },
  209. };
  210. /* i2s-ref
  211. *
  212. * i2s bus reference clock, selectable from external, esysclk or epllref
  213. *
  214. * Note, this used to be two clocks, but was compressed into one.
  215. */
  216. struct clk *clk_i2s_srclist[] = {
  217. [0] = &clk_i2s_eplldiv.clk,
  218. [1] = &clk_i2s_ext,
  219. [2] = &clk_epllref.clk,
  220. [3] = &clk_epllref.clk,
  221. };
  222. static struct clksrc_clk clk_i2s = {
  223. .clk = {
  224. .name = "i2s-if",
  225. .ctrlbit = S3C2443_SCLKCON_I2SCLK,
  226. .enable = s3c2443_clkcon_enable_s,
  227. },
  228. .sources = &(struct clksrc_sources) {
  229. .sources = clk_i2s_srclist,
  230. .nr_sources = ARRAY_SIZE(clk_i2s_srclist),
  231. },
  232. .reg_src = { .reg = S3C2443_CLKSRC, .size = 2, .shift = 14 },
  233. };
  234. /* standard clock definitions */
  235. static struct clk init_clocks_off[] = {
  236. {
  237. .name = "sdi",
  238. .parent = &clk_p,
  239. .enable = s3c2443_clkcon_enable_p,
  240. .ctrlbit = S3C2443_PCLKCON_SDI,
  241. }, {
  242. .name = "iis",
  243. .parent = &clk_p,
  244. .enable = s3c2443_clkcon_enable_p,
  245. .ctrlbit = S3C2443_PCLKCON_IIS,
  246. }, {
  247. .name = "spi",
  248. .devname = "s3c2410-spi.0",
  249. .parent = &clk_p,
  250. .enable = s3c2443_clkcon_enable_p,
  251. .ctrlbit = S3C2443_PCLKCON_SPI0,
  252. }, {
  253. .name = "spi",
  254. .devname = "s3c2410-spi.1",
  255. .parent = &clk_p,
  256. .enable = s3c2443_clkcon_enable_p,
  257. .ctrlbit = S3C2443_PCLKCON_SPI1,
  258. }
  259. };
  260. static struct clk init_clocks[] = {
  261. };
  262. /* clocks to add straight away */
  263. static struct clksrc_clk *clksrcs[] __initdata = {
  264. &clk_arm,
  265. &clk_i2s_eplldiv,
  266. &clk_i2s,
  267. &clk_hsspi,
  268. &clk_hsmmc_div,
  269. };
  270. static struct clk *clks[] __initdata = {
  271. &clk_hsmmc,
  272. &clk_armdiv,
  273. };
  274. void __init_or_cpufreq s3c2443_setup_clocks(void)
  275. {
  276. s3c2443_common_setup_clocks(s3c2443_get_mpll, s3c2443_fclk_div);
  277. }
  278. void __init s3c2443_init_clocks(int xtal)
  279. {
  280. unsigned long epllcon = __raw_readl(S3C2443_EPLLCON);
  281. int ptr;
  282. clk_epll.rate = s3c2443_get_epll(epllcon, xtal);
  283. clk_epll.parent = &clk_epllref.clk;
  284. s3c2443_common_init_clocks(xtal, s3c2443_get_mpll, s3c2443_fclk_div);
  285. s3c2443_setup_clocks();
  286. s3c24xx_register_clocks(clks, ARRAY_SIZE(clks));
  287. for (ptr = 0; ptr < ARRAY_SIZE(clksrcs); ptr++)
  288. s3c_register_clksrc(clksrcs[ptr], 1);
  289. /* register clocks from clock array */
  290. s3c_register_clocks(init_clocks, ARRAY_SIZE(init_clocks));
  291. /* We must be careful disabling the clocks we are not intending to
  292. * be using at boot time, as subsystems such as the LCD which do
  293. * their own DMA requests to the bus can cause the system to lockup
  294. * if they where in the middle of requesting bus access.
  295. *
  296. * Disabling the LCD clock if the LCD is active is very dangerous,
  297. * and therefore the bootloader should be careful to not enable
  298. * the LCD clock if it is not needed.
  299. */
  300. /* install (and disable) the clocks we do not need immediately */
  301. s3c_register_clocks(init_clocks_off, ARRAY_SIZE(init_clocks_off));
  302. s3c_disable_clocks(init_clocks_off, ARRAY_SIZE(init_clocks_off));
  303. s3c_pwmclk_init();
  304. }