s3c6400-clock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /* linux/arch/arm/plat-s3c64xx/s3c6400-clock.c
  2. *
  3. * Copyright 2008 Openmoko, Inc.
  4. * Copyright 2008 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>
  6. * http://armlinux.simtec.co.uk/
  7. *
  8. * S3C6400 based common clock support
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/list.h>
  18. #include <linux/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/clk.h>
  21. #include <linux/sysdev.h>
  22. #include <linux/io.h>
  23. #include <mach/hardware.h>
  24. #include <mach/map.h>
  25. #include <plat/cpu-freq.h>
  26. #include <plat/regs-clock.h>
  27. #include <plat/clock.h>
  28. #include <plat/clock-clksrc.h>
  29. #include <plat/cpu.h>
  30. #include <plat/pll.h>
  31. /* fin_apll, fin_mpll and fin_epll are all the same clock, which we call
  32. * ext_xtal_mux for want of an actual name from the manual.
  33. */
  34. static struct clk clk_ext_xtal_mux = {
  35. .name = "ext_xtal",
  36. .id = -1,
  37. };
  38. #define clk_fin_apll clk_ext_xtal_mux
  39. #define clk_fin_mpll clk_ext_xtal_mux
  40. #define clk_fin_epll clk_ext_xtal_mux
  41. #define clk_fout_mpll clk_mpll
  42. #define clk_fout_epll clk_epll
  43. static struct clk clk_fout_apll = {
  44. .name = "fout_apll",
  45. .id = -1,
  46. };
  47. static struct clk *clk_src_apll_list[] = {
  48. [0] = &clk_fin_apll,
  49. [1] = &clk_fout_apll,
  50. };
  51. static struct clksrc_sources clk_src_apll = {
  52. .sources = clk_src_apll_list,
  53. .nr_sources = ARRAY_SIZE(clk_src_apll_list),
  54. };
  55. static struct clksrc_clk clk_mout_apll = {
  56. .clk = {
  57. .name = "mout_apll",
  58. .id = -1,
  59. },
  60. .reg_src = { S3C_CLK_SRC, 0, 1 },
  61. .sources = &clk_src_apll,
  62. };
  63. static struct clk *clk_src_epll_list[] = {
  64. [0] = &clk_fin_epll,
  65. [1] = &clk_fout_epll,
  66. };
  67. static struct clksrc_sources clk_src_epll = {
  68. .sources = clk_src_epll_list,
  69. .nr_sources = ARRAY_SIZE(clk_src_epll_list),
  70. };
  71. static struct clksrc_clk clk_mout_epll = {
  72. .clk = {
  73. .name = "mout_epll",
  74. .id = -1,
  75. },
  76. .reg_src = { S3C_CLK_SRC, 2, 1 },
  77. .sources = &clk_src_epll,
  78. };
  79. static struct clk *clk_src_mpll_list[] = {
  80. [0] = &clk_fin_mpll,
  81. [1] = &clk_fout_mpll,
  82. };
  83. static struct clksrc_sources clk_src_mpll = {
  84. .sources = clk_src_mpll_list,
  85. .nr_sources = ARRAY_SIZE(clk_src_mpll_list),
  86. };
  87. static struct clksrc_clk clk_mout_mpll = {
  88. .clk = {
  89. .name = "mout_mpll",
  90. .id = -1,
  91. },
  92. .reg_src = { S3C_CLK_SRC, 1, 1 },
  93. .sources = &clk_src_mpll,
  94. };
  95. static unsigned int armclk_mask;
  96. static unsigned long s3c64xx_clk_arm_get_rate(struct clk *clk)
  97. {
  98. unsigned long rate = clk_get_rate(clk->parent);
  99. u32 clkdiv;
  100. /* divisor mask starts at bit0, so no need to shift */
  101. clkdiv = __raw_readl(S3C_CLK_DIV0) & armclk_mask;
  102. return rate / (clkdiv + 1);
  103. }
  104. static unsigned long s3c64xx_clk_arm_round_rate(struct clk *clk,
  105. unsigned long rate)
  106. {
  107. unsigned long parent = clk_get_rate(clk->parent);
  108. u32 div;
  109. if (parent < rate)
  110. return parent;
  111. div = (parent / rate) - 1;
  112. if (div > armclk_mask)
  113. div = armclk_mask;
  114. return parent / (div + 1);
  115. }
  116. static int s3c64xx_clk_arm_set_rate(struct clk *clk, unsigned long rate)
  117. {
  118. unsigned long parent = clk_get_rate(clk->parent);
  119. u32 div;
  120. u32 val;
  121. if (rate < parent / (armclk_mask + 1))
  122. return -EINVAL;
  123. rate = clk_round_rate(clk, rate);
  124. div = clk_get_rate(clk->parent) / rate;
  125. val = __raw_readl(S3C_CLK_DIV0);
  126. val &= ~armclk_mask;
  127. val |= (div - 1);
  128. __raw_writel(val, S3C_CLK_DIV0);
  129. return 0;
  130. }
  131. static struct clk clk_arm = {
  132. .name = "armclk",
  133. .id = -1,
  134. .parent = &clk_mout_apll.clk,
  135. .ops = &(struct clk_ops) {
  136. .get_rate = s3c64xx_clk_arm_get_rate,
  137. .set_rate = s3c64xx_clk_arm_set_rate,
  138. .round_rate = s3c64xx_clk_arm_round_rate,
  139. },
  140. };
  141. static unsigned long s3c64xx_clk_doutmpll_get_rate(struct clk *clk)
  142. {
  143. unsigned long rate = clk_get_rate(clk->parent);
  144. printk(KERN_DEBUG "%s: parent is %ld\n", __func__, rate);
  145. if (__raw_readl(S3C_CLK_DIV0) & S3C6400_CLKDIV0_MPLL_MASK)
  146. rate /= 2;
  147. return rate;
  148. }
  149. static struct clk_ops clk_dout_ops = {
  150. .get_rate = s3c64xx_clk_doutmpll_get_rate,
  151. };
  152. static struct clk clk_dout_mpll = {
  153. .name = "dout_mpll",
  154. .id = -1,
  155. .parent = &clk_mout_mpll.clk,
  156. .ops = &clk_dout_ops,
  157. };
  158. static struct clk *clkset_spi_mmc_list[] = {
  159. &clk_mout_epll.clk,
  160. &clk_dout_mpll,
  161. &clk_fin_epll,
  162. &clk_27m,
  163. };
  164. static struct clksrc_sources clkset_spi_mmc = {
  165. .sources = clkset_spi_mmc_list,
  166. .nr_sources = ARRAY_SIZE(clkset_spi_mmc_list),
  167. };
  168. static struct clk *clkset_irda_list[] = {
  169. &clk_mout_epll.clk,
  170. &clk_dout_mpll,
  171. NULL,
  172. &clk_27m,
  173. };
  174. static struct clksrc_sources clkset_irda = {
  175. .sources = clkset_irda_list,
  176. .nr_sources = ARRAY_SIZE(clkset_irda_list),
  177. };
  178. static struct clk *clkset_uart_list[] = {
  179. &clk_mout_epll.clk,
  180. &clk_dout_mpll,
  181. NULL,
  182. NULL
  183. };
  184. static struct clksrc_sources clkset_uart = {
  185. .sources = clkset_uart_list,
  186. .nr_sources = ARRAY_SIZE(clkset_uart_list),
  187. };
  188. static struct clk *clkset_uhost_list[] = {
  189. &clk_48m,
  190. &clk_mout_epll.clk,
  191. &clk_dout_mpll,
  192. &clk_fin_epll,
  193. };
  194. static struct clksrc_sources clkset_uhost = {
  195. .sources = clkset_uhost_list,
  196. .nr_sources = ARRAY_SIZE(clkset_uhost_list),
  197. };
  198. /* The peripheral clocks are all controlled via clocksource followed
  199. * by an optional divider and gate stage. We currently roll this into
  200. * one clock which hides the intermediate clock from the mux.
  201. *
  202. * Note, the JPEG clock can only be an even divider...
  203. *
  204. * The scaler and LCD clocks depend on the S3C64XX version, and also
  205. * have a common parent divisor so are not included here.
  206. */
  207. /* clocks that feed other parts of the clock source tree */
  208. static struct clk clk_iis_cd0 = {
  209. .name = "iis_cdclk0",
  210. .id = -1,
  211. };
  212. static struct clk clk_iis_cd1 = {
  213. .name = "iis_cdclk1",
  214. .id = -1,
  215. };
  216. static struct clk clk_pcm_cd = {
  217. .name = "pcm_cdclk",
  218. .id = -1,
  219. };
  220. static struct clk *clkset_audio0_list[] = {
  221. [0] = &clk_mout_epll.clk,
  222. [1] = &clk_dout_mpll,
  223. [2] = &clk_fin_epll,
  224. [3] = &clk_iis_cd0,
  225. [4] = &clk_pcm_cd,
  226. };
  227. static struct clksrc_sources clkset_audio0 = {
  228. .sources = clkset_audio0_list,
  229. .nr_sources = ARRAY_SIZE(clkset_audio0_list),
  230. };
  231. static struct clk *clkset_audio1_list[] = {
  232. [0] = &clk_mout_epll.clk,
  233. [1] = &clk_dout_mpll,
  234. [2] = &clk_fin_epll,
  235. [3] = &clk_iis_cd1,
  236. [4] = &clk_pcm_cd,
  237. };
  238. static struct clksrc_sources clkset_audio1 = {
  239. .sources = clkset_audio1_list,
  240. .nr_sources = ARRAY_SIZE(clkset_audio1_list),
  241. };
  242. static struct clk *clkset_camif_list[] = {
  243. &clk_h2,
  244. };
  245. static struct clksrc_sources clkset_camif = {
  246. .sources = clkset_camif_list,
  247. .nr_sources = ARRAY_SIZE(clkset_camif_list),
  248. };
  249. static struct clksrc_clk clksrcs[] = {
  250. {
  251. .clk = {
  252. .name = "mmc_bus",
  253. .id = 0,
  254. .ctrlbit = S3C_CLKCON_SCLK_MMC0,
  255. .enable = s3c64xx_sclk_ctrl,
  256. },
  257. .reg_src = { S3C_CLK_SRC, 18, 2 },
  258. .reg_div = { S3C_CLK_DIV1, 0, 4 },
  259. .sources = &clkset_spi_mmc,
  260. }, {
  261. .clk = {
  262. .name = "mmc_bus",
  263. .id = 1,
  264. .ctrlbit = S3C_CLKCON_SCLK_MMC1,
  265. .enable = s3c64xx_sclk_ctrl,
  266. },
  267. .reg_src = { S3C_CLK_SRC, 20, 2 },
  268. .reg_div = { S3C_CLK_DIV1, 4, 4 },
  269. .sources = &clkset_spi_mmc,
  270. }, {
  271. .clk = {
  272. .name = "mmc_bus",
  273. .id = 2,
  274. .ctrlbit = S3C_CLKCON_SCLK_MMC2,
  275. .enable = s3c64xx_sclk_ctrl,
  276. },
  277. .reg_src = { S3C_CLK_SRC, 22, 2 },
  278. .reg_div = { S3C_CLK_DIV1, 8, 4 },
  279. .sources = &clkset_spi_mmc,
  280. }, {
  281. .clk = {
  282. .name = "usb-bus-host",
  283. .id = -1,
  284. .ctrlbit = S3C_CLKCON_SCLK_UHOST,
  285. .enable = s3c64xx_sclk_ctrl,
  286. },
  287. .reg_src = { S3C_CLK_SRC, 5, 2 },
  288. .reg_div = { S3C_CLK_DIV1, 20, 4 },
  289. .sources = &clkset_uhost,
  290. }, {
  291. .clk = {
  292. .name = "uclk1",
  293. .id = -1,
  294. .ctrlbit = S3C_CLKCON_SCLK_UART,
  295. .enable = s3c64xx_sclk_ctrl,
  296. },
  297. .reg_src = { S3C_CLK_SRC, 13, 1 },
  298. .reg_div = { S3C_CLK_DIV2, 16, 4 },
  299. .sources = &clkset_uart,
  300. }, {
  301. /* Where does UCLK0 come from? */
  302. .clk = {
  303. .name = "spi-bus",
  304. .id = 0,
  305. .ctrlbit = S3C_CLKCON_SCLK_SPI0,
  306. .enable = s3c64xx_sclk_ctrl,
  307. },
  308. .reg_src = { S3C_CLK_SRC, 14, 2 },
  309. .reg_div = { S3C_CLK_DIV2, 0, 4 },
  310. .sources = &clkset_spi_mmc,
  311. }, {
  312. .clk = {
  313. .name = "spi-bus",
  314. .id = 1,
  315. .ctrlbit = S3C_CLKCON_SCLK_SPI1,
  316. .enable = s3c64xx_sclk_ctrl,
  317. },
  318. .reg_src = { S3C_CLK_SRC, 16, 2 },
  319. .reg_div = { S3C_CLK_DIV2, 4, 4 },
  320. .sources = &clkset_spi_mmc,
  321. }, {
  322. .clk = {
  323. .name = "audio-bus",
  324. .id = 0,
  325. .ctrlbit = S3C_CLKCON_SCLK_AUDIO0,
  326. .enable = s3c64xx_sclk_ctrl,
  327. },
  328. .reg_src = { S3C_CLK_SRC, 7, 3 },
  329. .reg_div = { S3C_CLK_DIV2, 8, 4 },
  330. .sources = &clkset_audio0,
  331. }, {
  332. .clk = {
  333. .name = "audio-bus",
  334. .id = 1,
  335. .ctrlbit = S3C_CLKCON_SCLK_AUDIO1,
  336. .enable = s3c64xx_sclk_ctrl,
  337. },
  338. .reg_src = { S3C_CLK_SRC, 10, 3 },
  339. .reg_div = { S3C_CLK_DIV2, 12, 4 },
  340. .sources = &clkset_audio1,
  341. }, {
  342. .clk = {
  343. .name = "irda-bus",
  344. .id = 0,
  345. .ctrlbit = S3C_CLKCON_SCLK_IRDA,
  346. .enable = s3c64xx_sclk_ctrl,
  347. },
  348. .reg_src = { S3C_CLK_SRC, 24, 2 },
  349. .reg_div = { S3C_CLK_DIV2, 20, 4 },
  350. .sources = &clkset_irda,
  351. }, {
  352. .clk = {
  353. .name = "camera",
  354. .id = -1,
  355. .ctrlbit = S3C_CLKCON_SCLK_CAM,
  356. .enable = s3c64xx_sclk_ctrl,
  357. },
  358. .reg_div = { S3C_CLK_DIV0, 20, 4 },
  359. .reg_src = { NULL, 0, 0 },
  360. .sources = &clkset_camif,
  361. },
  362. };
  363. /* Clock initialisation code */
  364. static struct clksrc_clk *init_parents[] = {
  365. &clk_mout_apll,
  366. &clk_mout_epll,
  367. &clk_mout_mpll,
  368. };
  369. #define GET_DIV(clk, field) ((((clk) & field##_MASK) >> field##_SHIFT) + 1)
  370. void __init_or_cpufreq s3c6400_setup_clocks(void)
  371. {
  372. struct clk *xtal_clk;
  373. unsigned long xtal;
  374. unsigned long fclk;
  375. unsigned long hclk;
  376. unsigned long hclk2;
  377. unsigned long pclk;
  378. unsigned long epll;
  379. unsigned long apll;
  380. unsigned long mpll;
  381. unsigned int ptr;
  382. u32 clkdiv0;
  383. printk(KERN_DEBUG "%s: registering clocks\n", __func__);
  384. clkdiv0 = __raw_readl(S3C_CLK_DIV0);
  385. printk(KERN_DEBUG "%s: clkdiv0 = %08x\n", __func__, clkdiv0);
  386. xtal_clk = clk_get(NULL, "xtal");
  387. BUG_ON(IS_ERR(xtal_clk));
  388. xtal = clk_get_rate(xtal_clk);
  389. clk_put(xtal_clk);
  390. printk(KERN_DEBUG "%s: xtal is %ld\n", __func__, xtal);
  391. /* For now assume the mux always selects the crystal */
  392. clk_ext_xtal_mux.parent = xtal_clk;
  393. epll = s3c6400_get_epll(xtal);
  394. mpll = s3c6400_get_pll(xtal, __raw_readl(S3C_MPLL_CON));
  395. apll = s3c6400_get_pll(xtal, __raw_readl(S3C_APLL_CON));
  396. fclk = mpll;
  397. printk(KERN_INFO "S3C64XX: PLL settings, A=%ld, M=%ld, E=%ld\n",
  398. apll, mpll, epll);
  399. hclk2 = mpll / GET_DIV(clkdiv0, S3C6400_CLKDIV0_HCLK2);
  400. hclk = hclk2 / GET_DIV(clkdiv0, S3C6400_CLKDIV0_HCLK);
  401. pclk = hclk2 / GET_DIV(clkdiv0, S3C6400_CLKDIV0_PCLK);
  402. printk(KERN_INFO "S3C64XX: HCLK2=%ld, HCLK=%ld, PCLK=%ld\n",
  403. hclk2, hclk, pclk);
  404. clk_fout_mpll.rate = mpll;
  405. clk_fout_epll.rate = epll;
  406. clk_fout_apll.rate = apll;
  407. clk_h2.rate = hclk2;
  408. clk_h.rate = hclk;
  409. clk_p.rate = pclk;
  410. clk_f.rate = fclk;
  411. for (ptr = 0; ptr < ARRAY_SIZE(init_parents); ptr++)
  412. s3c_set_clksrc(init_parents[ptr]);
  413. for (ptr = 0; ptr < ARRAY_SIZE(clksrcs); ptr++)
  414. s3c_set_clksrc(&clksrcs[ptr]);
  415. }
  416. static struct clk *clks[] __initdata = {
  417. &clk_ext_xtal_mux,
  418. &clk_iis_cd0,
  419. &clk_iis_cd1,
  420. &clk_pcm_cd,
  421. &clk_mout_epll.clk,
  422. &clk_mout_mpll.clk,
  423. &clk_dout_mpll,
  424. &clk_arm,
  425. };
  426. /**
  427. * s3c6400_register_clocks - register clocks for s3c6400 and above
  428. * @armclk_divlimit: Divisor mask for ARMCLK
  429. *
  430. * Register the clocks for the S3C6400 and above SoC range, such
  431. * as ARMCLK and the clocks which have divider chains attached.
  432. *
  433. * This call does not setup the clocks, which is left to the
  434. * s3c6400_setup_clocks() call which may be needed by the cpufreq
  435. * or resume code to re-set the clocks if the bootloader has changed
  436. * them.
  437. */
  438. void __init s3c6400_register_clocks(unsigned armclk_divlimit)
  439. {
  440. struct clk *clkp;
  441. int ret;
  442. int ptr;
  443. armclk_mask = armclk_divlimit;
  444. for (ptr = 0; ptr < ARRAY_SIZE(clks); ptr++) {
  445. clkp = clks[ptr];
  446. ret = s3c24xx_register_clock(clkp);
  447. if (ret < 0) {
  448. printk(KERN_ERR "Failed to register clock %s (%d)\n",
  449. clkp->name, ret);
  450. }
  451. }
  452. s3c_register_clksrc(clksrcs, ARRAY_SIZE(clksrcs));
  453. }