exynos4210-cpufreq.c 12 KB

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