s5pv210-cpufreq.c 14 KB

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