clock2xxx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * linux/arch/arm/mach-omap2/clock.c
  3. *
  4. * Copyright (C) 2005-2008 Texas Instruments, Inc.
  5. * Copyright (C) 2004-2008 Nokia Corporation
  6. *
  7. * Contacts:
  8. * Richard Woodruff <r-woodruff2@ti.com>
  9. * Paul Walmsley
  10. *
  11. * Based on earlier work by Tuukka Tikkanen, Tony Lindgren,
  12. * Gordon McNutt and RidgeRun, Inc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #undef DEBUG
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/device.h>
  22. #include <linux/list.h>
  23. #include <linux/errno.h>
  24. #include <linux/delay.h>
  25. #include <linux/clk.h>
  26. #include <linux/io.h>
  27. #include <linux/cpufreq.h>
  28. #include <linux/bitops.h>
  29. #include <plat/clock.h>
  30. #include <plat/sram.h>
  31. #include <plat/prcm.h>
  32. #include <plat/clkdev_omap.h>
  33. #include <asm/div64.h>
  34. #include <asm/clkdev.h>
  35. #include <plat/sdrc.h>
  36. #include "clock.h"
  37. #include "clock2xxx.h"
  38. #include "opp2xxx.h"
  39. #include "prm.h"
  40. #include "prm-regbits-24xx.h"
  41. #include "cm.h"
  42. #include "cm-regbits-24xx.h"
  43. /* CM_CLKEN_PLL.EN_{54,96}M_PLL options (24XX) */
  44. #define EN_APLL_STOPPED 0
  45. #define EN_APLL_LOCKED 3
  46. /* CM_CLKSEL1_PLL.APLLS_CLKIN options (24XX) */
  47. #define APLLS_CLKIN_19_2MHZ 0
  48. #define APLLS_CLKIN_13MHZ 2
  49. #define APLLS_CLKIN_12MHZ 3
  50. const struct prcm_config *curr_prcm_set;
  51. const struct prcm_config *rate_table;
  52. struct clk *vclk, *sclk, *dclk;
  53. void __iomem *prcm_clksrc_ctrl;
  54. /*-------------------------------------------------------------------------
  55. * Omap24xx specific clock functions
  56. *-------------------------------------------------------------------------*/
  57. /**
  58. * omap2430_clk_i2chs_find_idlest - return CM_IDLEST info for 2430 I2CHS
  59. * @clk: struct clk * being enabled
  60. * @idlest_reg: void __iomem ** to store CM_IDLEST reg address into
  61. * @idlest_bit: pointer to a u8 to store the CM_IDLEST bit shift into
  62. *
  63. * OMAP2430 I2CHS CM_IDLEST bits are in CM_IDLEST1_CORE, but the
  64. * CM_*CLKEN bits are in CM_{I,F}CLKEN2_CORE. This custom function
  65. * passes back the correct CM_IDLEST register address for I2CHS
  66. * modules. No return value.
  67. */
  68. static void omap2430_clk_i2chs_find_idlest(struct clk *clk,
  69. void __iomem **idlest_reg,
  70. u8 *idlest_bit)
  71. {
  72. *idlest_reg = OMAP_CM_REGADDR(CORE_MOD, CM_IDLEST);
  73. *idlest_bit = clk->enable_bit;
  74. }
  75. /* 2430 I2CHS has non-standard IDLEST register */
  76. const struct clkops clkops_omap2430_i2chs_wait = {
  77. .enable = omap2_dflt_clk_enable,
  78. .disable = omap2_dflt_clk_disable,
  79. .find_idlest = omap2430_clk_i2chs_find_idlest,
  80. .find_companion = omap2_clk_dflt_find_companion,
  81. };
  82. static int omap2_enable_osc_ck(struct clk *clk)
  83. {
  84. u32 pcc;
  85. pcc = __raw_readl(prcm_clksrc_ctrl);
  86. __raw_writel(pcc & ~OMAP_AUTOEXTCLKMODE_MASK, prcm_clksrc_ctrl);
  87. return 0;
  88. }
  89. static void omap2_disable_osc_ck(struct clk *clk)
  90. {
  91. u32 pcc;
  92. pcc = __raw_readl(prcm_clksrc_ctrl);
  93. __raw_writel(pcc | OMAP_AUTOEXTCLKMODE_MASK, prcm_clksrc_ctrl);
  94. }
  95. const struct clkops clkops_oscck = {
  96. .enable = omap2_enable_osc_ck,
  97. .disable = omap2_disable_osc_ck,
  98. };
  99. #ifdef OLD_CK
  100. /* Recalculate SYST_CLK */
  101. static void omap2_sys_clk_recalc(struct clk *clk)
  102. {
  103. u32 div = PRCM_CLKSRC_CTRL;
  104. div &= (1 << 7) | (1 << 6); /* Test if ext clk divided by 1 or 2 */
  105. div >>= clk->rate_offset;
  106. clk->rate = (clk->parent->rate / div);
  107. propagate_rate(clk);
  108. }
  109. #endif /* OLD_CK */
  110. /* Enable an APLL if off */
  111. static int omap2_clk_apll_enable(struct clk *clk, u32 status_mask)
  112. {
  113. u32 cval, apll_mask;
  114. apll_mask = EN_APLL_LOCKED << clk->enable_bit;
  115. cval = cm_read_mod_reg(PLL_MOD, CM_CLKEN);
  116. if ((cval & apll_mask) == apll_mask)
  117. return 0; /* apll already enabled */
  118. cval &= ~apll_mask;
  119. cval |= apll_mask;
  120. cm_write_mod_reg(cval, PLL_MOD, CM_CLKEN);
  121. omap2_cm_wait_idlest(OMAP_CM_REGADDR(PLL_MOD, CM_IDLEST), status_mask,
  122. clk->name);
  123. /*
  124. * REVISIT: Should we return an error code if omap2_wait_clock_ready()
  125. * fails?
  126. */
  127. return 0;
  128. }
  129. static int omap2_clk_apll96_enable(struct clk *clk)
  130. {
  131. return omap2_clk_apll_enable(clk, OMAP24XX_ST_96M_APLL);
  132. }
  133. static int omap2_clk_apll54_enable(struct clk *clk)
  134. {
  135. return omap2_clk_apll_enable(clk, OMAP24XX_ST_54M_APLL);
  136. }
  137. /* Stop APLL */
  138. static void omap2_clk_apll_disable(struct clk *clk)
  139. {
  140. u32 cval;
  141. cval = cm_read_mod_reg(PLL_MOD, CM_CLKEN);
  142. cval &= ~(EN_APLL_LOCKED << clk->enable_bit);
  143. cm_write_mod_reg(cval, PLL_MOD, CM_CLKEN);
  144. }
  145. const struct clkops clkops_apll96 = {
  146. .enable = omap2_clk_apll96_enable,
  147. .disable = omap2_clk_apll_disable,
  148. };
  149. const struct clkops clkops_apll54 = {
  150. .enable = omap2_clk_apll54_enable,
  151. .disable = omap2_clk_apll_disable,
  152. };
  153. /**
  154. * omap2_table_mpu_recalc - just return the MPU speed
  155. * @clk: virt_prcm_set struct clk
  156. *
  157. * Set virt_prcm_set's rate to the mpu_speed field of the current PRCM set.
  158. */
  159. unsigned long omap2_table_mpu_recalc(struct clk *clk)
  160. {
  161. return curr_prcm_set->mpu_speed;
  162. }
  163. /*
  164. * Look for a rate equal or less than the target rate given a configuration set.
  165. *
  166. * What's not entirely clear is "which" field represents the key field.
  167. * Some might argue L3-DDR, others ARM, others IVA. This code is simple and
  168. * just uses the ARM rates.
  169. */
  170. long omap2_round_to_table_rate(struct clk *clk, unsigned long rate)
  171. {
  172. const struct prcm_config *ptr;
  173. long highest_rate;
  174. long sys_ck_rate;
  175. sys_ck_rate = clk_get_rate(sclk);
  176. highest_rate = -EINVAL;
  177. for (ptr = rate_table; ptr->mpu_speed; ptr++) {
  178. if (!(ptr->flags & cpu_mask))
  179. continue;
  180. if (ptr->xtal_speed != sys_ck_rate)
  181. continue;
  182. highest_rate = ptr->mpu_speed;
  183. /* Can check only after xtal frequency check */
  184. if (ptr->mpu_speed <= rate)
  185. break;
  186. }
  187. return highest_rate;
  188. }
  189. /* Sets basic clocks based on the specified rate */
  190. int omap2_select_table_rate(struct clk *clk, unsigned long rate)
  191. {
  192. u32 cur_rate, done_rate, bypass = 0, tmp;
  193. const struct prcm_config *prcm;
  194. unsigned long found_speed = 0;
  195. unsigned long flags;
  196. long sys_ck_rate;
  197. sys_ck_rate = clk_get_rate(sclk);
  198. for (prcm = rate_table; prcm->mpu_speed; prcm++) {
  199. if (!(prcm->flags & cpu_mask))
  200. continue;
  201. if (prcm->xtal_speed != sys_ck_rate)
  202. continue;
  203. if (prcm->mpu_speed <= rate) {
  204. found_speed = prcm->mpu_speed;
  205. break;
  206. }
  207. }
  208. if (!found_speed) {
  209. printk(KERN_INFO "Could not set MPU rate to %luMHz\n",
  210. rate / 1000000);
  211. return -EINVAL;
  212. }
  213. curr_prcm_set = prcm;
  214. cur_rate = omap2xxx_clk_get_core_rate(dclk);
  215. if (prcm->dpll_speed == cur_rate / 2) {
  216. omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);
  217. } else if (prcm->dpll_speed == cur_rate * 2) {
  218. omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
  219. } else if (prcm->dpll_speed != cur_rate) {
  220. local_irq_save(flags);
  221. if (prcm->dpll_speed == prcm->xtal_speed)
  222. bypass = 1;
  223. if ((prcm->cm_clksel2_pll & OMAP24XX_CORE_CLK_SRC_MASK) ==
  224. CORE_CLK_SRC_DPLL_X2)
  225. done_rate = CORE_CLK_SRC_DPLL_X2;
  226. else
  227. done_rate = CORE_CLK_SRC_DPLL;
  228. /* MPU divider */
  229. cm_write_mod_reg(prcm->cm_clksel_mpu, MPU_MOD, CM_CLKSEL);
  230. /* dsp + iva1 div(2420), iva2.1(2430) */
  231. cm_write_mod_reg(prcm->cm_clksel_dsp,
  232. OMAP24XX_DSP_MOD, CM_CLKSEL);
  233. cm_write_mod_reg(prcm->cm_clksel_gfx, GFX_MOD, CM_CLKSEL);
  234. /* Major subsystem dividers */
  235. tmp = cm_read_mod_reg(CORE_MOD, CM_CLKSEL1) & OMAP24XX_CLKSEL_DSS2_MASK;
  236. cm_write_mod_reg(prcm->cm_clksel1_core | tmp, CORE_MOD,
  237. CM_CLKSEL1);
  238. if (cpu_is_omap2430())
  239. cm_write_mod_reg(prcm->cm_clksel_mdm,
  240. OMAP2430_MDM_MOD, CM_CLKSEL);
  241. /* x2 to enter omap2xxx_sdrc_init_params() */
  242. omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
  243. omap2_set_prcm(prcm->cm_clksel1_pll, prcm->base_sdrc_rfr,
  244. bypass);
  245. omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());
  246. omap2xxx_sdrc_reprogram(done_rate, 0);
  247. local_irq_restore(flags);
  248. }
  249. return 0;
  250. }
  251. #ifdef CONFIG_CPU_FREQ
  252. /*
  253. * Walk PRCM rate table and fillout cpufreq freq_table
  254. * XXX This should be replaced by an OPP layer in the near future
  255. */
  256. static struct cpufreq_frequency_table *freq_table;
  257. void omap2xxx_clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
  258. {
  259. const struct prcm_config *prcm;
  260. long sys_ck_rate;
  261. int i = 0;
  262. int tbl_sz = 0;
  263. if (!cpu_is_omap2xxx())
  264. return;
  265. sys_ck_rate = clk_get_rate(sclk);
  266. for (prcm = rate_table; prcm->mpu_speed; prcm++) {
  267. if (!(prcm->flags & cpu_mask))
  268. continue;
  269. if (prcm->xtal_speed != sys_ck_rate)
  270. continue;
  271. /* don't put bypass rates in table */
  272. if (prcm->dpll_speed == prcm->xtal_speed)
  273. continue;
  274. tbl_sz++;
  275. }
  276. /*
  277. * XXX Ensure that we're doing what CPUFreq expects for this error
  278. * case and the following one
  279. */
  280. if (tbl_sz == 0) {
  281. pr_warning("%s: no matching entries in rate_table\n",
  282. __func__);
  283. return;
  284. }
  285. /* Include the CPUFREQ_TABLE_END terminator entry */
  286. tbl_sz++;
  287. freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) * tbl_sz,
  288. GFP_ATOMIC);
  289. if (!freq_table) {
  290. pr_err("%s: could not kzalloc frequency table\n", __func__);
  291. return;
  292. }
  293. for (prcm = rate_table; prcm->mpu_speed; prcm++) {
  294. if (!(prcm->flags & cpu_mask))
  295. continue;
  296. if (prcm->xtal_speed != sys_ck_rate)
  297. continue;
  298. /* don't put bypass rates in table */
  299. if (prcm->dpll_speed == prcm->xtal_speed)
  300. continue;
  301. freq_table[i].index = i;
  302. freq_table[i].frequency = prcm->mpu_speed / 1000;
  303. i++;
  304. }
  305. freq_table[i].index = i;
  306. freq_table[i].frequency = CPUFREQ_TABLE_END;
  307. *table = &freq_table[0];
  308. }
  309. void omap2xxx_clk_exit_cpufreq_table(struct cpufreq_frequency_table **table)
  310. {
  311. if (!cpu_is_omap2xxx())
  312. return;
  313. kfree(freq_table);
  314. }
  315. #endif
  316. static u32 omap2_get_apll_clkin(void)
  317. {
  318. u32 aplls, srate = 0;
  319. aplls = cm_read_mod_reg(PLL_MOD, CM_CLKSEL1);
  320. aplls &= OMAP24XX_APLLS_CLKIN_MASK;
  321. aplls >>= OMAP24XX_APLLS_CLKIN_SHIFT;
  322. if (aplls == APLLS_CLKIN_19_2MHZ)
  323. srate = 19200000;
  324. else if (aplls == APLLS_CLKIN_13MHZ)
  325. srate = 13000000;
  326. else if (aplls == APLLS_CLKIN_12MHZ)
  327. srate = 12000000;
  328. return srate;
  329. }
  330. static u32 omap2_get_sysclkdiv(void)
  331. {
  332. u32 div;
  333. div = __raw_readl(prcm_clksrc_ctrl);
  334. div &= OMAP_SYSCLKDIV_MASK;
  335. div >>= OMAP_SYSCLKDIV_SHIFT;
  336. return div;
  337. }
  338. unsigned long omap2_osc_clk_recalc(struct clk *clk)
  339. {
  340. return omap2_get_apll_clkin() * omap2_get_sysclkdiv();
  341. }
  342. unsigned long omap2_sys_clk_recalc(struct clk *clk)
  343. {
  344. return clk->parent->rate / omap2_get_sysclkdiv();
  345. }
  346. /*
  347. * Set clocks for bypass mode for reboot to work.
  348. */
  349. void omap2_clk_prepare_for_reboot(void)
  350. {
  351. u32 rate;
  352. if (vclk == NULL || sclk == NULL)
  353. return;
  354. rate = clk_get_rate(sclk);
  355. clk_set_rate(vclk, rate);
  356. }
  357. /*
  358. * Switch the MPU rate if specified on cmdline.
  359. * We cannot do this early until cmdline is parsed.
  360. */
  361. static int __init omap2_clk_arch_init(void)
  362. {
  363. struct clk *virt_prcm_set, *sys_ck, *dpll_ck, *mpu_ck;
  364. unsigned long sys_ck_rate;
  365. if (!mpurate)
  366. return -EINVAL;
  367. virt_prcm_set = clk_get(NULL, "virt_prcm_set");
  368. sys_ck = clk_get(NULL, "sys_ck");
  369. dpll_ck = clk_get(NULL, "dpll_ck");
  370. mpu_ck = clk_get(NULL, "mpu_ck");
  371. if (clk_set_rate(virt_prcm_set, mpurate))
  372. printk(KERN_ERR "Could not find matching MPU rate\n");
  373. recalculate_root_clocks();
  374. sys_ck_rate = clk_get_rate(sys_ck);
  375. pr_info("Switched to new clocking rate (Crystal/DPLL/MPU): "
  376. "%ld.%01ld/%ld/%ld MHz\n",
  377. (sys_ck_rate / 1000000), (sys_ck_rate / 100000) % 10,
  378. (clk_get_rate(dpll_ck) / 1000000),
  379. (clk_get_rate(mpu_ck) / 1000000));
  380. return 0;
  381. }
  382. arch_initcall(omap2_clk_arch_init);