s5pv210-cpufreq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * Copyright (c) 2010 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * CPU frequency scaling for S5PC110/S5PV210
  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/init.h>
  14. #include <linux/err.h>
  15. #include <linux/clk.h>
  16. #include <linux/io.h>
  17. #include <linux/cpufreq.h>
  18. #include <linux/reboot.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/suspend.h>
  21. #include <mach/map.h>
  22. #include <mach/regs-clock.h>
  23. static struct clk *dmc0_clk;
  24. static struct clk *dmc1_clk;
  25. static DEFINE_MUTEX(set_freq_lock);
  26. /* APLL M,P,S values for 1G/800Mhz */
  27. #define APLL_VAL_1000 ((1 << 31) | (125 << 16) | (3 << 8) | 1)
  28. #define APLL_VAL_800 ((1 << 31) | (100 << 16) | (3 << 8) | 1)
  29. /* Use 800MHz when entering sleep mode */
  30. #define SLEEP_FREQ (800 * 1000)
  31. /* Tracks if cpu freqency can be updated anymore */
  32. static bool no_cpufreq_access;
  33. /*
  34. * DRAM configurations to calculate refresh counter for changing
  35. * frequency of memory.
  36. */
  37. struct dram_conf {
  38. unsigned long freq; /* HZ */
  39. unsigned long refresh; /* DRAM refresh counter * 1000 */
  40. };
  41. /* DRAM configuration (DMC0 and DMC1) */
  42. static struct dram_conf s5pv210_dram_conf[2];
  43. enum perf_level {
  44. L0, L1, L2, L3, L4,
  45. };
  46. enum s5pv210_mem_type {
  47. LPDDR = 0x1,
  48. LPDDR2 = 0x2,
  49. DDR2 = 0x4,
  50. };
  51. enum s5pv210_dmc_port {
  52. DMC0 = 0,
  53. DMC1,
  54. };
  55. static struct cpufreq_frequency_table s5pv210_freq_table[] = {
  56. {L0, 1000*1000},
  57. {L1, 800*1000},
  58. {L2, 400*1000},
  59. {L3, 200*1000},
  60. {L4, 100*1000},
  61. {0, CPUFREQ_TABLE_END},
  62. };
  63. static struct regulator *arm_regulator;
  64. static struct regulator *int_regulator;
  65. struct s5pv210_dvs_conf {
  66. int arm_volt; /* uV */
  67. int int_volt; /* uV */
  68. };
  69. static const int arm_volt_max = 1350000;
  70. static const int int_volt_max = 1250000;
  71. static struct s5pv210_dvs_conf dvs_conf[] = {
  72. [L0] = {
  73. .arm_volt = 1250000,
  74. .int_volt = 1100000,
  75. },
  76. [L1] = {
  77. .arm_volt = 1200000,
  78. .int_volt = 1100000,
  79. },
  80. [L2] = {
  81. .arm_volt = 1050000,
  82. .int_volt = 1100000,
  83. },
  84. [L3] = {
  85. .arm_volt = 950000,
  86. .int_volt = 1100000,
  87. },
  88. [L4] = {
  89. .arm_volt = 950000,
  90. .int_volt = 1000000,
  91. },
  92. };
  93. static u32 clkdiv_val[5][11] = {
  94. /*
  95. * Clock divider value for following
  96. * { APLL, A2M, HCLK_MSYS, PCLK_MSYS,
  97. * HCLK_DSYS, PCLK_DSYS, HCLK_PSYS, PCLK_PSYS,
  98. * ONEDRAM, MFC, G3D }
  99. */
  100. /* L0 : [1000/200/100][166/83][133/66][200/200] */
  101. {0, 4, 4, 1, 3, 1, 4, 1, 3, 0, 0},
  102. /* L1 : [800/200/100][166/83][133/66][200/200] */
  103. {0, 3, 3, 1, 3, 1, 4, 1, 3, 0, 0},
  104. /* L2 : [400/200/100][166/83][133/66][200/200] */
  105. {1, 3, 1, 1, 3, 1, 4, 1, 3, 0, 0},
  106. /* L3 : [200/200/100][166/83][133/66][200/200] */
  107. {3, 3, 1, 1, 3, 1, 4, 1, 3, 0, 0},
  108. /* L4 : [100/100/100][83/83][66/66][100/100] */
  109. {7, 7, 0, 0, 7, 0, 9, 0, 7, 0, 0},
  110. };
  111. /*
  112. * This function set DRAM refresh counter
  113. * accoriding to operating frequency of DRAM
  114. * ch: DMC port number 0 or 1
  115. * freq: Operating frequency of DRAM(KHz)
  116. */
  117. static void s5pv210_set_refresh(enum s5pv210_dmc_port ch, unsigned long freq)
  118. {
  119. unsigned long tmp, tmp1;
  120. void __iomem *reg = NULL;
  121. if (ch == DMC0) {
  122. reg = (S5P_VA_DMC0 + 0x30);
  123. } else if (ch == DMC1) {
  124. reg = (S5P_VA_DMC1 + 0x30);
  125. } else {
  126. printk(KERN_ERR "Cannot find DMC port\n");
  127. return;
  128. }
  129. /* Find current DRAM frequency */
  130. tmp = s5pv210_dram_conf[ch].freq;
  131. do_div(tmp, freq);
  132. tmp1 = s5pv210_dram_conf[ch].refresh;
  133. do_div(tmp1, tmp);
  134. __raw_writel(tmp1, reg);
  135. }
  136. static int s5pv210_target(struct cpufreq_policy *policy, unsigned int index)
  137. {
  138. unsigned long reg;
  139. unsigned int priv_index;
  140. unsigned int pll_changing = 0;
  141. unsigned int bus_speed_changing = 0;
  142. unsigned int old_freq, new_freq;
  143. int arm_volt, int_volt;
  144. int ret = 0;
  145. mutex_lock(&set_freq_lock);
  146. if (no_cpufreq_access) {
  147. #ifdef CONFIG_PM_VERBOSE
  148. pr_err("%s:%d denied access to %s as it is disabled"
  149. "temporarily\n", __FILE__, __LINE__, __func__);
  150. #endif
  151. ret = -EINVAL;
  152. goto exit;
  153. }
  154. old_freq = policy->cur;
  155. new_freq = s5pv210_freq_table[index].frequency;
  156. /* Finding current running level index */
  157. if (cpufreq_frequency_table_target(policy, s5pv210_freq_table,
  158. old_freq, CPUFREQ_RELATION_H,
  159. &priv_index)) {
  160. ret = -EINVAL;
  161. goto exit;
  162. }
  163. arm_volt = dvs_conf[index].arm_volt;
  164. int_volt = dvs_conf[index].int_volt;
  165. if (new_freq > old_freq) {
  166. ret = regulator_set_voltage(arm_regulator,
  167. arm_volt, arm_volt_max);
  168. if (ret)
  169. goto exit;
  170. ret = regulator_set_voltage(int_regulator,
  171. int_volt, int_volt_max);
  172. if (ret)
  173. goto exit;
  174. }
  175. /* Check if there need to change PLL */
  176. if ((index == L0) || (priv_index == L0))
  177. pll_changing = 1;
  178. /* Check if there need to change System bus clock */
  179. if ((index == L4) || (priv_index == L4))
  180. bus_speed_changing = 1;
  181. if (bus_speed_changing) {
  182. /*
  183. * Reconfigure DRAM refresh counter value for minimum
  184. * temporary clock while changing divider.
  185. * expected clock is 83Mhz : 7.8usec/(1/83Mhz) = 0x287
  186. */
  187. if (pll_changing)
  188. s5pv210_set_refresh(DMC1, 83000);
  189. else
  190. s5pv210_set_refresh(DMC1, 100000);
  191. s5pv210_set_refresh(DMC0, 83000);
  192. }
  193. /*
  194. * APLL should be changed in this level
  195. * APLL -> MPLL(for stable transition) -> APLL
  196. * Some clock source's clock API are not prepared.
  197. * Do not use clock API in below code.
  198. */
  199. if (pll_changing) {
  200. /*
  201. * 1. Temporary Change divider for MFC and G3D
  202. * SCLKA2M(200/1=200)->(200/4=50)Mhz
  203. */
  204. reg = __raw_readl(S5P_CLK_DIV2);
  205. reg &= ~(S5P_CLKDIV2_G3D_MASK | S5P_CLKDIV2_MFC_MASK);
  206. reg |= (3 << S5P_CLKDIV2_G3D_SHIFT) |
  207. (3 << S5P_CLKDIV2_MFC_SHIFT);
  208. __raw_writel(reg, S5P_CLK_DIV2);
  209. /* For MFC, G3D dividing */
  210. do {
  211. reg = __raw_readl(S5P_CLKDIV_STAT0);
  212. } while (reg & ((1 << 16) | (1 << 17)));
  213. /*
  214. * 2. Change SCLKA2M(200Mhz)to SCLKMPLL in MFC_MUX, G3D MUX
  215. * (200/4=50)->(667/4=166)Mhz
  216. */
  217. reg = __raw_readl(S5P_CLK_SRC2);
  218. reg &= ~(S5P_CLKSRC2_G3D_MASK | S5P_CLKSRC2_MFC_MASK);
  219. reg |= (1 << S5P_CLKSRC2_G3D_SHIFT) |
  220. (1 << S5P_CLKSRC2_MFC_SHIFT);
  221. __raw_writel(reg, S5P_CLK_SRC2);
  222. do {
  223. reg = __raw_readl(S5P_CLKMUX_STAT1);
  224. } while (reg & ((1 << 7) | (1 << 3)));
  225. /*
  226. * 3. DMC1 refresh count for 133Mhz if (index == L4) is
  227. * true refresh counter is already programed in upper
  228. * code. 0x287@83Mhz
  229. */
  230. if (!bus_speed_changing)
  231. s5pv210_set_refresh(DMC1, 133000);
  232. /* 4. SCLKAPLL -> SCLKMPLL */
  233. reg = __raw_readl(S5P_CLK_SRC0);
  234. reg &= ~(S5P_CLKSRC0_MUX200_MASK);
  235. reg |= (0x1 << S5P_CLKSRC0_MUX200_SHIFT);
  236. __raw_writel(reg, S5P_CLK_SRC0);
  237. do {
  238. reg = __raw_readl(S5P_CLKMUX_STAT0);
  239. } while (reg & (0x1 << 18));
  240. }
  241. /* Change divider */
  242. reg = __raw_readl(S5P_CLK_DIV0);
  243. reg &= ~(S5P_CLKDIV0_APLL_MASK | S5P_CLKDIV0_A2M_MASK |
  244. S5P_CLKDIV0_HCLK200_MASK | S5P_CLKDIV0_PCLK100_MASK |
  245. S5P_CLKDIV0_HCLK166_MASK | S5P_CLKDIV0_PCLK83_MASK |
  246. S5P_CLKDIV0_HCLK133_MASK | S5P_CLKDIV0_PCLK66_MASK);
  247. reg |= ((clkdiv_val[index][0] << S5P_CLKDIV0_APLL_SHIFT) |
  248. (clkdiv_val[index][1] << S5P_CLKDIV0_A2M_SHIFT) |
  249. (clkdiv_val[index][2] << S5P_CLKDIV0_HCLK200_SHIFT) |
  250. (clkdiv_val[index][3] << S5P_CLKDIV0_PCLK100_SHIFT) |
  251. (clkdiv_val[index][4] << S5P_CLKDIV0_HCLK166_SHIFT) |
  252. (clkdiv_val[index][5] << S5P_CLKDIV0_PCLK83_SHIFT) |
  253. (clkdiv_val[index][6] << S5P_CLKDIV0_HCLK133_SHIFT) |
  254. (clkdiv_val[index][7] << S5P_CLKDIV0_PCLK66_SHIFT));
  255. __raw_writel(reg, S5P_CLK_DIV0);
  256. do {
  257. reg = __raw_readl(S5P_CLKDIV_STAT0);
  258. } while (reg & 0xff);
  259. /* ARM MCS value changed */
  260. reg = __raw_readl(S5P_ARM_MCS_CON);
  261. reg &= ~0x3;
  262. if (index >= L3)
  263. reg |= 0x3;
  264. else
  265. reg |= 0x1;
  266. __raw_writel(reg, S5P_ARM_MCS_CON);
  267. if (pll_changing) {
  268. /* 5. Set Lock time = 30us*24Mhz = 0x2cf */
  269. __raw_writel(0x2cf, S5P_APLL_LOCK);
  270. /*
  271. * 6. Turn on APLL
  272. * 6-1. Set PMS values
  273. * 6-2. Wait untile the PLL is locked
  274. */
  275. if (index == L0)
  276. __raw_writel(APLL_VAL_1000, S5P_APLL_CON);
  277. else
  278. __raw_writel(APLL_VAL_800, S5P_APLL_CON);
  279. do {
  280. reg = __raw_readl(S5P_APLL_CON);
  281. } while (!(reg & (0x1 << 29)));
  282. /*
  283. * 7. Change souce clock from SCLKMPLL(667Mhz)
  284. * to SCLKA2M(200Mhz) in MFC_MUX and G3D MUX
  285. * (667/4=166)->(200/4=50)Mhz
  286. */
  287. reg = __raw_readl(S5P_CLK_SRC2);
  288. reg &= ~(S5P_CLKSRC2_G3D_MASK | S5P_CLKSRC2_MFC_MASK);
  289. reg |= (0 << S5P_CLKSRC2_G3D_SHIFT) |
  290. (0 << S5P_CLKSRC2_MFC_SHIFT);
  291. __raw_writel(reg, S5P_CLK_SRC2);
  292. do {
  293. reg = __raw_readl(S5P_CLKMUX_STAT1);
  294. } while (reg & ((1 << 7) | (1 << 3)));
  295. /*
  296. * 8. Change divider for MFC and G3D
  297. * (200/4=50)->(200/1=200)Mhz
  298. */
  299. reg = __raw_readl(S5P_CLK_DIV2);
  300. reg &= ~(S5P_CLKDIV2_G3D_MASK | S5P_CLKDIV2_MFC_MASK);
  301. reg |= (clkdiv_val[index][10] << S5P_CLKDIV2_G3D_SHIFT) |
  302. (clkdiv_val[index][9] << S5P_CLKDIV2_MFC_SHIFT);
  303. __raw_writel(reg, S5P_CLK_DIV2);
  304. /* For MFC, G3D dividing */
  305. do {
  306. reg = __raw_readl(S5P_CLKDIV_STAT0);
  307. } while (reg & ((1 << 16) | (1 << 17)));
  308. /* 9. Change MPLL to APLL in MSYS_MUX */
  309. reg = __raw_readl(S5P_CLK_SRC0);
  310. reg &= ~(S5P_CLKSRC0_MUX200_MASK);
  311. reg |= (0x0 << S5P_CLKSRC0_MUX200_SHIFT);
  312. __raw_writel(reg, S5P_CLK_SRC0);
  313. do {
  314. reg = __raw_readl(S5P_CLKMUX_STAT0);
  315. } while (reg & (0x1 << 18));
  316. /*
  317. * 10. DMC1 refresh counter
  318. * L4 : DMC1 = 100Mhz 7.8us/(1/100) = 0x30c
  319. * Others : DMC1 = 200Mhz 7.8us/(1/200) = 0x618
  320. */
  321. if (!bus_speed_changing)
  322. s5pv210_set_refresh(DMC1, 200000);
  323. }
  324. /*
  325. * L4 level need to change memory bus speed, hence onedram clock divier
  326. * and memory refresh parameter should be changed
  327. */
  328. if (bus_speed_changing) {
  329. reg = __raw_readl(S5P_CLK_DIV6);
  330. reg &= ~S5P_CLKDIV6_ONEDRAM_MASK;
  331. reg |= (clkdiv_val[index][8] << S5P_CLKDIV6_ONEDRAM_SHIFT);
  332. __raw_writel(reg, S5P_CLK_DIV6);
  333. do {
  334. reg = __raw_readl(S5P_CLKDIV_STAT1);
  335. } while (reg & (1 << 15));
  336. /* Reconfigure DRAM refresh counter value */
  337. if (index != L4) {
  338. /*
  339. * DMC0 : 166Mhz
  340. * DMC1 : 200Mhz
  341. */
  342. s5pv210_set_refresh(DMC0, 166000);
  343. s5pv210_set_refresh(DMC1, 200000);
  344. } else {
  345. /*
  346. * DMC0 : 83Mhz
  347. * DMC1 : 100Mhz
  348. */
  349. s5pv210_set_refresh(DMC0, 83000);
  350. s5pv210_set_refresh(DMC1, 100000);
  351. }
  352. }
  353. if (new_freq < old_freq) {
  354. regulator_set_voltage(int_regulator,
  355. int_volt, int_volt_max);
  356. regulator_set_voltage(arm_regulator,
  357. arm_volt, arm_volt_max);
  358. }
  359. printk(KERN_DEBUG "Perf changed[L%d]\n", index);
  360. exit:
  361. mutex_unlock(&set_freq_lock);
  362. return ret;
  363. }
  364. #ifdef CONFIG_PM
  365. static int s5pv210_cpufreq_suspend(struct cpufreq_policy *policy)
  366. {
  367. return 0;
  368. }
  369. static int s5pv210_cpufreq_resume(struct cpufreq_policy *policy)
  370. {
  371. return 0;
  372. }
  373. #endif
  374. static int check_mem_type(void __iomem *dmc_reg)
  375. {
  376. unsigned long val;
  377. val = __raw_readl(dmc_reg + 0x4);
  378. val = (val & (0xf << 8));
  379. return val >> 8;
  380. }
  381. static int __init s5pv210_cpu_init(struct cpufreq_policy *policy)
  382. {
  383. unsigned long mem_type;
  384. int ret;
  385. policy->clk = clk_get(NULL, "armclk");
  386. if (IS_ERR(policy->clk))
  387. return PTR_ERR(policy->clk);
  388. dmc0_clk = clk_get(NULL, "sclk_dmc0");
  389. if (IS_ERR(dmc0_clk)) {
  390. ret = PTR_ERR(dmc0_clk);
  391. goto out_dmc0;
  392. }
  393. dmc1_clk = clk_get(NULL, "hclk_msys");
  394. if (IS_ERR(dmc1_clk)) {
  395. ret = PTR_ERR(dmc1_clk);
  396. goto out_dmc1;
  397. }
  398. if (policy->cpu != 0) {
  399. ret = -EINVAL;
  400. goto out_dmc1;
  401. }
  402. /*
  403. * check_mem_type : This driver only support LPDDR & LPDDR2.
  404. * other memory type is not supported.
  405. */
  406. mem_type = check_mem_type(S5P_VA_DMC0);
  407. if ((mem_type != LPDDR) && (mem_type != LPDDR2)) {
  408. printk(KERN_ERR "CPUFreq doesn't support this memory type\n");
  409. ret = -EINVAL;
  410. goto out_dmc1;
  411. }
  412. /* Find current refresh counter and frequency each DMC */
  413. s5pv210_dram_conf[0].refresh = (__raw_readl(S5P_VA_DMC0 + 0x30) * 1000);
  414. s5pv210_dram_conf[0].freq = clk_get_rate(dmc0_clk);
  415. s5pv210_dram_conf[1].refresh = (__raw_readl(S5P_VA_DMC1 + 0x30) * 1000);
  416. s5pv210_dram_conf[1].freq = clk_get_rate(dmc1_clk);
  417. return cpufreq_generic_init(policy, s5pv210_freq_table, 40000);
  418. out_dmc1:
  419. clk_put(dmc0_clk);
  420. out_dmc0:
  421. clk_put(policy->clk);
  422. return ret;
  423. }
  424. static int s5pv210_cpufreq_notifier_event(struct notifier_block *this,
  425. unsigned long event, void *ptr)
  426. {
  427. int ret;
  428. switch (event) {
  429. case PM_SUSPEND_PREPARE:
  430. ret = cpufreq_driver_target(cpufreq_cpu_get(0), SLEEP_FREQ, 0);
  431. if (ret < 0)
  432. return NOTIFY_BAD;
  433. /* Disable updation of cpu frequency */
  434. no_cpufreq_access = true;
  435. return NOTIFY_OK;
  436. case PM_POST_RESTORE:
  437. case PM_POST_SUSPEND:
  438. /* Enable updation of cpu frequency */
  439. no_cpufreq_access = false;
  440. cpufreq_driver_target(cpufreq_cpu_get(0), SLEEP_FREQ, 0);
  441. return NOTIFY_OK;
  442. }
  443. return NOTIFY_DONE;
  444. }
  445. static int s5pv210_cpufreq_reboot_notifier_event(struct notifier_block *this,
  446. unsigned long event, void *ptr)
  447. {
  448. int ret;
  449. ret = cpufreq_driver_target(cpufreq_cpu_get(0), SLEEP_FREQ, 0);
  450. if (ret < 0)
  451. return NOTIFY_BAD;
  452. no_cpufreq_access = true;
  453. return NOTIFY_DONE;
  454. }
  455. static struct cpufreq_driver s5pv210_driver = {
  456. .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  457. .verify = cpufreq_generic_frequency_table_verify,
  458. .target_index = s5pv210_target,
  459. .get = cpufreq_generic_get,
  460. .init = s5pv210_cpu_init,
  461. .name = "s5pv210",
  462. #ifdef CONFIG_PM
  463. .suspend = s5pv210_cpufreq_suspend,
  464. .resume = s5pv210_cpufreq_resume,
  465. #endif
  466. };
  467. static struct notifier_block s5pv210_cpufreq_notifier = {
  468. .notifier_call = s5pv210_cpufreq_notifier_event,
  469. };
  470. static struct notifier_block s5pv210_cpufreq_reboot_notifier = {
  471. .notifier_call = s5pv210_cpufreq_reboot_notifier_event,
  472. };
  473. static int __init s5pv210_cpufreq_init(void)
  474. {
  475. arm_regulator = regulator_get(NULL, "vddarm");
  476. if (IS_ERR(arm_regulator)) {
  477. pr_err("failed to get regulator vddarm");
  478. return PTR_ERR(arm_regulator);
  479. }
  480. int_regulator = regulator_get(NULL, "vddint");
  481. if (IS_ERR(int_regulator)) {
  482. pr_err("failed to get regulator vddint");
  483. regulator_put(arm_regulator);
  484. return PTR_ERR(int_regulator);
  485. }
  486. register_pm_notifier(&s5pv210_cpufreq_notifier);
  487. register_reboot_notifier(&s5pv210_cpufreq_reboot_notifier);
  488. return cpufreq_register_driver(&s5pv210_driver);
  489. }
  490. late_initcall(s5pv210_cpufreq_init);