exynos4210-cpufreq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * EXYNOS4 - CPU frequency scaling support
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/err.h>
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/cpufreq.h>
  19. #include <linux/notifier.h>
  20. #include <linux/suspend.h>
  21. #include <mach/map.h>
  22. #include <mach/regs-clock.h>
  23. #include <mach/regs-mem.h>
  24. #include <plat/clock.h>
  25. #include <plat/pm.h>
  26. static struct clk *cpu_clk;
  27. static struct clk *moutcore;
  28. static struct clk *mout_mpll;
  29. static struct clk *mout_apll;
  30. static struct regulator *arm_regulator;
  31. static struct cpufreq_freqs freqs;
  32. static unsigned int locking_frequency;
  33. static bool frequency_locked;
  34. static DEFINE_MUTEX(cpufreq_lock);
  35. enum cpufreq_level_index {
  36. L0, L1, L2, L3, CPUFREQ_LEVEL_END,
  37. };
  38. static struct cpufreq_frequency_table exynos4_freq_table[] = {
  39. {L0, 1000*1000},
  40. {L1, 800*1000},
  41. {L2, 400*1000},
  42. {L3, 100*1000},
  43. {0, CPUFREQ_TABLE_END},
  44. };
  45. static unsigned int clkdiv_cpu0[CPUFREQ_LEVEL_END][7] = {
  46. /*
  47. * Clock divider value for following
  48. * { DIVCORE, DIVCOREM0, DIVCOREM1, DIVPERIPH,
  49. * DIVATB, DIVPCLK_DBG, DIVAPLL }
  50. */
  51. /* ARM L0: 1000MHz */
  52. { 0, 3, 7, 3, 3, 0, 1 },
  53. /* ARM L1: 800MHz */
  54. { 0, 3, 7, 3, 3, 0, 1 },
  55. /* ARM L2: 400MHz */
  56. { 0, 1, 3, 1, 3, 0, 1 },
  57. /* ARM L3: 100MHz */
  58. { 0, 0, 1, 0, 3, 1, 1 },
  59. };
  60. static unsigned int clkdiv_cpu1[CPUFREQ_LEVEL_END][2] = {
  61. /*
  62. * Clock divider value for following
  63. * { DIVCOPY, DIVHPM }
  64. */
  65. /* ARM L0: 1000MHz */
  66. { 3, 0 },
  67. /* ARM L1: 800MHz */
  68. { 3, 0 },
  69. /* ARM L2: 400MHz */
  70. { 3, 0 },
  71. /* ARM L3: 100MHz */
  72. { 3, 0 },
  73. };
  74. struct cpufreq_voltage_table {
  75. unsigned int index; /* any */
  76. unsigned int arm_volt; /* uV */
  77. };
  78. static struct cpufreq_voltage_table exynos4_volt_table[CPUFREQ_LEVEL_END] = {
  79. {
  80. .index = L0,
  81. .arm_volt = 1200000,
  82. }, {
  83. .index = L1,
  84. .arm_volt = 1100000,
  85. }, {
  86. .index = L2,
  87. .arm_volt = 1000000,
  88. }, {
  89. .index = L3,
  90. .arm_volt = 900000,
  91. },
  92. };
  93. static unsigned int exynos4_apll_pms_table[CPUFREQ_LEVEL_END] = {
  94. /* APLL FOUT L0: 1000MHz */
  95. ((250 << 16) | (6 << 8) | 1),
  96. /* APLL FOUT L1: 800MHz */
  97. ((200 << 16) | (6 << 8) | 1),
  98. /* APLL FOUT L2 : 400MHz */
  99. ((200 << 16) | (6 << 8) | 2),
  100. /* APLL FOUT L3: 100MHz */
  101. ((200 << 16) | (6 << 8) | 4),
  102. };
  103. static int exynos4_verify_speed(struct cpufreq_policy *policy)
  104. {
  105. return cpufreq_frequency_table_verify(policy, exynos4_freq_table);
  106. }
  107. static unsigned int exynos4_getspeed(unsigned int cpu)
  108. {
  109. return clk_get_rate(cpu_clk) / 1000;
  110. }
  111. static void exynos4_set_clkdiv(unsigned int div_index)
  112. {
  113. unsigned int tmp;
  114. /* Change Divider - CPU0 */
  115. tmp = __raw_readl(S5P_CLKDIV_CPU);
  116. tmp &= ~(S5P_CLKDIV_CPU0_CORE_MASK | S5P_CLKDIV_CPU0_COREM0_MASK |
  117. S5P_CLKDIV_CPU0_COREM1_MASK | S5P_CLKDIV_CPU0_PERIPH_MASK |
  118. S5P_CLKDIV_CPU0_ATB_MASK | S5P_CLKDIV_CPU0_PCLKDBG_MASK |
  119. S5P_CLKDIV_CPU0_APLL_MASK);
  120. tmp |= ((clkdiv_cpu0[div_index][0] << S5P_CLKDIV_CPU0_CORE_SHIFT) |
  121. (clkdiv_cpu0[div_index][1] << S5P_CLKDIV_CPU0_COREM0_SHIFT) |
  122. (clkdiv_cpu0[div_index][2] << S5P_CLKDIV_CPU0_COREM1_SHIFT) |
  123. (clkdiv_cpu0[div_index][3] << S5P_CLKDIV_CPU0_PERIPH_SHIFT) |
  124. (clkdiv_cpu0[div_index][4] << S5P_CLKDIV_CPU0_ATB_SHIFT) |
  125. (clkdiv_cpu0[div_index][5] << S5P_CLKDIV_CPU0_PCLKDBG_SHIFT) |
  126. (clkdiv_cpu0[div_index][6] << S5P_CLKDIV_CPU0_APLL_SHIFT));
  127. __raw_writel(tmp, S5P_CLKDIV_CPU);
  128. do {
  129. tmp = __raw_readl(S5P_CLKDIV_STATCPU);
  130. } while (tmp & 0x1111111);
  131. /* Change Divider - CPU1 */
  132. tmp = __raw_readl(S5P_CLKDIV_CPU1);
  133. tmp &= ~((0x7 << 4) | 0x7);
  134. tmp |= ((clkdiv_cpu1[div_index][0] << 4) |
  135. (clkdiv_cpu1[div_index][1] << 0));
  136. __raw_writel(tmp, S5P_CLKDIV_CPU1);
  137. do {
  138. tmp = __raw_readl(S5P_CLKDIV_STATCPU1);
  139. } while (tmp & 0x11);
  140. }
  141. static void exynos4_set_apll(unsigned int index)
  142. {
  143. unsigned int tmp;
  144. /* 1. MUX_CORE_SEL = MPLL, ARMCLK uses MPLL for lock time */
  145. clk_set_parent(moutcore, mout_mpll);
  146. do {
  147. tmp = (__raw_readl(S5P_CLKMUX_STATCPU)
  148. >> S5P_CLKSRC_CPU_MUXCORE_SHIFT);
  149. tmp &= 0x7;
  150. } while (tmp != 0x2);
  151. /* 2. Set APLL Lock time */
  152. __raw_writel(S5P_APLL_LOCKTIME, S5P_APLL_LOCK);
  153. /* 3. Change PLL PMS values */
  154. tmp = __raw_readl(S5P_APLL_CON0);
  155. tmp &= ~((0x3ff << 16) | (0x3f << 8) | (0x7 << 0));
  156. tmp |= exynos4_apll_pms_table[index];
  157. __raw_writel(tmp, S5P_APLL_CON0);
  158. /* 4. wait_lock_time */
  159. do {
  160. tmp = __raw_readl(S5P_APLL_CON0);
  161. } while (!(tmp & (0x1 << S5P_APLLCON0_LOCKED_SHIFT)));
  162. /* 5. MUX_CORE_SEL = APLL */
  163. clk_set_parent(moutcore, mout_apll);
  164. do {
  165. tmp = __raw_readl(S5P_CLKMUX_STATCPU);
  166. tmp &= S5P_CLKMUX_STATCPU_MUXCORE_MASK;
  167. } while (tmp != (0x1 << S5P_CLKSRC_CPU_MUXCORE_SHIFT));
  168. }
  169. static void exynos4_set_frequency(unsigned int old_index, unsigned int new_index)
  170. {
  171. unsigned int tmp;
  172. if (old_index > new_index) {
  173. /* The frequency changing to L0 needs to change apll */
  174. if (freqs.new == exynos4_freq_table[L0].frequency) {
  175. /* 1. Change the system clock divider values */
  176. exynos4_set_clkdiv(new_index);
  177. /* 2. Change the apll m,p,s value */
  178. exynos4_set_apll(new_index);
  179. } else {
  180. /* 1. Change the system clock divider values */
  181. exynos4_set_clkdiv(new_index);
  182. /* 2. Change just s value in apll m,p,s value */
  183. tmp = __raw_readl(S5P_APLL_CON0);
  184. tmp &= ~(0x7 << 0);
  185. tmp |= (exynos4_apll_pms_table[new_index] & 0x7);
  186. __raw_writel(tmp, S5P_APLL_CON0);
  187. }
  188. }
  189. else if (old_index < new_index) {
  190. /* The frequency changing from L0 needs to change apll */
  191. if (freqs.old == exynos4_freq_table[L0].frequency) {
  192. /* 1. Change the apll m,p,s value */
  193. exynos4_set_apll(new_index);
  194. /* 2. Change the system clock divider values */
  195. exynos4_set_clkdiv(new_index);
  196. } else {
  197. /* 1. Change just s value in apll m,p,s value */
  198. tmp = __raw_readl(S5P_APLL_CON0);
  199. tmp &= ~(0x7 << 0);
  200. tmp |= (exynos4_apll_pms_table[new_index] & 0x7);
  201. __raw_writel(tmp, S5P_APLL_CON0);
  202. /* 2. Change the system clock divider values */
  203. exynos4_set_clkdiv(new_index);
  204. }
  205. }
  206. }
  207. static int exynos4_target(struct cpufreq_policy *policy,
  208. unsigned int target_freq,
  209. unsigned int relation)
  210. {
  211. unsigned int index, old_index;
  212. unsigned int arm_volt;
  213. int err = -EINVAL;
  214. freqs.old = exynos4_getspeed(policy->cpu);
  215. mutex_lock(&cpufreq_lock);
  216. if (frequency_locked && target_freq != locking_frequency) {
  217. err = -EAGAIN;
  218. goto out;
  219. }
  220. if (cpufreq_frequency_table_target(policy, exynos4_freq_table,
  221. freqs.old, relation, &old_index))
  222. goto out;
  223. if (cpufreq_frequency_table_target(policy, exynos4_freq_table,
  224. target_freq, relation, &index))
  225. goto out;
  226. err = 0;
  227. freqs.new = exynos4_freq_table[index].frequency;
  228. freqs.cpu = policy->cpu;
  229. if (freqs.new == freqs.old)
  230. goto out;
  231. /* get the voltage value */
  232. arm_volt = exynos4_volt_table[index].arm_volt;
  233. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  234. /* control regulator */
  235. if (freqs.new > freqs.old) {
  236. /* Voltage up */
  237. regulator_set_voltage(arm_regulator, arm_volt, arm_volt);
  238. }
  239. /* Clock Configuration Procedure */
  240. exynos4_set_frequency(old_index, index);
  241. /* control regulator */
  242. if (freqs.new < freqs.old) {
  243. /* Voltage down */
  244. regulator_set_voltage(arm_regulator, arm_volt, arm_volt);
  245. }
  246. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  247. out:
  248. mutex_unlock(&cpufreq_lock);
  249. return err;
  250. }
  251. #ifdef CONFIG_PM
  252. /*
  253. * These suspend/resume are used as syscore_ops, it is already too
  254. * late to set regulator voltages at this stage.
  255. */
  256. static int exynos4_cpufreq_suspend(struct cpufreq_policy *policy)
  257. {
  258. return 0;
  259. }
  260. static int exynos4_cpufreq_resume(struct cpufreq_policy *policy)
  261. {
  262. return 0;
  263. }
  264. #endif
  265. /**
  266. * exynos4_cpufreq_pm_notifier - block CPUFREQ's activities in suspend-resume
  267. * context
  268. * @notifier
  269. * @pm_event
  270. * @v
  271. *
  272. * While frequency_locked == true, target() ignores every frequency but
  273. * locking_frequency. The locking_frequency value is the initial frequency,
  274. * which is set by the bootloader. In order to eliminate possible
  275. * inconsistency in clock values, we save and restore frequencies during
  276. * suspend and resume and block CPUFREQ activities. Note that the standard
  277. * suspend/resume cannot be used as they are too deep (syscore_ops) for
  278. * regulator actions.
  279. */
  280. static int exynos4_cpufreq_pm_notifier(struct notifier_block *notifier,
  281. unsigned long pm_event, void *v)
  282. {
  283. struct cpufreq_policy *policy = cpufreq_cpu_get(0); /* boot CPU */
  284. static unsigned int saved_frequency;
  285. unsigned int temp;
  286. mutex_lock(&cpufreq_lock);
  287. switch (pm_event) {
  288. case PM_SUSPEND_PREPARE:
  289. if (frequency_locked)
  290. goto out;
  291. frequency_locked = true;
  292. if (locking_frequency) {
  293. saved_frequency = exynos4_getspeed(0);
  294. mutex_unlock(&cpufreq_lock);
  295. exynos4_target(policy, locking_frequency,
  296. CPUFREQ_RELATION_H);
  297. mutex_lock(&cpufreq_lock);
  298. }
  299. break;
  300. case PM_POST_SUSPEND:
  301. if (saved_frequency) {
  302. /*
  303. * While frequency_locked, only locking_frequency
  304. * is valid for target(). In order to use
  305. * saved_frequency while keeping frequency_locked,
  306. * we temporarly overwrite locking_frequency.
  307. */
  308. temp = locking_frequency;
  309. locking_frequency = saved_frequency;
  310. mutex_unlock(&cpufreq_lock);
  311. exynos4_target(policy, locking_frequency,
  312. CPUFREQ_RELATION_H);
  313. mutex_lock(&cpufreq_lock);
  314. locking_frequency = temp;
  315. }
  316. frequency_locked = false;
  317. break;
  318. }
  319. out:
  320. mutex_unlock(&cpufreq_lock);
  321. return NOTIFY_OK;
  322. }
  323. static struct notifier_block exynos4_cpufreq_nb = {
  324. .notifier_call = exynos4_cpufreq_pm_notifier,
  325. };
  326. static int exynos4_cpufreq_cpu_init(struct cpufreq_policy *policy)
  327. {
  328. int ret;
  329. policy->cur = policy->min = policy->max = exynos4_getspeed(policy->cpu);
  330. cpufreq_frequency_table_get_attr(exynos4_freq_table, policy->cpu);
  331. /* set the transition latency value */
  332. policy->cpuinfo.transition_latency = 100000;
  333. /*
  334. * EXYNOS4 multi-core processors has 2 cores
  335. * that the frequency cannot be set independently.
  336. * Each cpu is bound to the same speed.
  337. * So the affected cpu is all of the cpus.
  338. */
  339. cpumask_setall(policy->cpus);
  340. ret = cpufreq_frequency_table_cpuinfo(policy, exynos4_freq_table);
  341. if (ret)
  342. return ret;
  343. cpufreq_frequency_table_get_attr(exynos4_freq_table, policy->cpu);
  344. return 0;
  345. }
  346. static int exynos4_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  347. {
  348. cpufreq_frequency_table_put_attr(policy->cpu);
  349. return 0;
  350. }
  351. static struct freq_attr *exynos4_cpufreq_attr[] = {
  352. &cpufreq_freq_attr_scaling_available_freqs,
  353. NULL,
  354. };
  355. static struct cpufreq_driver exynos4_driver = {
  356. .flags = CPUFREQ_STICKY,
  357. .verify = exynos4_verify_speed,
  358. .target = exynos4_target,
  359. .get = exynos4_getspeed,
  360. .init = exynos4_cpufreq_cpu_init,
  361. .exit = exynos4_cpufreq_cpu_exit,
  362. .name = "exynos4_cpufreq",
  363. .attr = exynos4_cpufreq_attr,
  364. #ifdef CONFIG_PM
  365. .suspend = exynos4_cpufreq_suspend,
  366. .resume = exynos4_cpufreq_resume,
  367. #endif
  368. };
  369. static int __init exynos4_cpufreq_init(void)
  370. {
  371. cpu_clk = clk_get(NULL, "armclk");
  372. if (IS_ERR(cpu_clk))
  373. return PTR_ERR(cpu_clk);
  374. locking_frequency = exynos4_getspeed(0);
  375. moutcore = clk_get(NULL, "moutcore");
  376. if (IS_ERR(moutcore))
  377. goto out;
  378. mout_mpll = clk_get(NULL, "mout_mpll");
  379. if (IS_ERR(mout_mpll))
  380. goto out;
  381. mout_apll = clk_get(NULL, "mout_apll");
  382. if (IS_ERR(mout_apll))
  383. goto out;
  384. arm_regulator = regulator_get(NULL, "vdd_arm");
  385. if (IS_ERR(arm_regulator)) {
  386. printk(KERN_ERR "failed to get resource %s\n", "vdd_arm");
  387. goto out;
  388. }
  389. register_pm_notifier(&exynos4_cpufreq_nb);
  390. return cpufreq_register_driver(&exynos4_driver);
  391. out:
  392. if (!IS_ERR(cpu_clk))
  393. clk_put(cpu_clk);
  394. if (!IS_ERR(moutcore))
  395. clk_put(moutcore);
  396. if (!IS_ERR(mout_mpll))
  397. clk_put(mout_mpll);
  398. if (!IS_ERR(mout_apll))
  399. clk_put(mout_apll);
  400. if (!IS_ERR(arm_regulator))
  401. regulator_put(arm_regulator);
  402. printk(KERN_ERR "%s: failed initialization\n", __func__);
  403. return -EINVAL;
  404. }
  405. late_initcall(exynos4_cpufreq_init);