exynos5440-cpufreq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright (c) 2013 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * Amit Daniel Kachhap <amit.daniel@samsung.com>
  6. *
  7. * EXYNOS5440 - CPU frequency scaling support
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/cpu.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/err.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/pm_opp.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. /* Register definitions */
  26. #define XMU_DVFS_CTRL 0x0060
  27. #define XMU_PMU_P0_7 0x0064
  28. #define XMU_C0_3_PSTATE 0x0090
  29. #define XMU_P_LIMIT 0x00a0
  30. #define XMU_P_STATUS 0x00a4
  31. #define XMU_PMUEVTEN 0x00d0
  32. #define XMU_PMUIRQEN 0x00d4
  33. #define XMU_PMUIRQ 0x00d8
  34. /* PMU mask and shift definations */
  35. #define P_VALUE_MASK 0x7
  36. #define XMU_DVFS_CTRL_EN_SHIFT 0
  37. #define P0_7_CPUCLKDEV_SHIFT 21
  38. #define P0_7_CPUCLKDEV_MASK 0x7
  39. #define P0_7_ATBCLKDEV_SHIFT 18
  40. #define P0_7_ATBCLKDEV_MASK 0x7
  41. #define P0_7_CSCLKDEV_SHIFT 15
  42. #define P0_7_CSCLKDEV_MASK 0x7
  43. #define P0_7_CPUEMA_SHIFT 28
  44. #define P0_7_CPUEMA_MASK 0xf
  45. #define P0_7_L2EMA_SHIFT 24
  46. #define P0_7_L2EMA_MASK 0xf
  47. #define P0_7_VDD_SHIFT 8
  48. #define P0_7_VDD_MASK 0x7f
  49. #define P0_7_FREQ_SHIFT 0
  50. #define P0_7_FREQ_MASK 0xff
  51. #define C0_3_PSTATE_VALID_SHIFT 8
  52. #define C0_3_PSTATE_CURR_SHIFT 4
  53. #define C0_3_PSTATE_NEW_SHIFT 0
  54. #define PSTATE_CHANGED_EVTEN_SHIFT 0
  55. #define PSTATE_CHANGED_IRQEN_SHIFT 0
  56. #define PSTATE_CHANGED_SHIFT 0
  57. /* some constant values for clock divider calculation */
  58. #define CPU_DIV_FREQ_MAX 500
  59. #define CPU_DBG_FREQ_MAX 375
  60. #define CPU_ATB_FREQ_MAX 500
  61. #define PMIC_LOW_VOLT 0x30
  62. #define PMIC_HIGH_VOLT 0x28
  63. #define CPUEMA_HIGH 0x2
  64. #define CPUEMA_MID 0x4
  65. #define CPUEMA_LOW 0x7
  66. #define L2EMA_HIGH 0x1
  67. #define L2EMA_MID 0x3
  68. #define L2EMA_LOW 0x4
  69. #define DIV_TAB_MAX 2
  70. /* frequency unit is 20MHZ */
  71. #define FREQ_UNIT 20
  72. #define MAX_VOLTAGE 1550000 /* In microvolt */
  73. #define VOLTAGE_STEP 12500 /* In microvolt */
  74. #define CPUFREQ_NAME "exynos5440_dvfs"
  75. #define DEF_TRANS_LATENCY 100000
  76. enum cpufreq_level_index {
  77. L0, L1, L2, L3, L4,
  78. L5, L6, L7, L8, L9,
  79. };
  80. #define CPUFREQ_LEVEL_END (L7 + 1)
  81. struct exynos_dvfs_data {
  82. void __iomem *base;
  83. struct resource *mem;
  84. int irq;
  85. struct clk *cpu_clk;
  86. unsigned int latency;
  87. struct cpufreq_frequency_table *freq_table;
  88. unsigned int freq_count;
  89. struct device *dev;
  90. bool dvfs_enabled;
  91. struct work_struct irq_work;
  92. };
  93. static struct exynos_dvfs_data *dvfs_info;
  94. static DEFINE_MUTEX(cpufreq_lock);
  95. static struct cpufreq_freqs freqs;
  96. static int init_div_table(void)
  97. {
  98. struct cpufreq_frequency_table *pos, *freq_tbl = dvfs_info->freq_table;
  99. unsigned int tmp, clk_div, ema_div, freq, volt_id;
  100. struct dev_pm_opp *opp;
  101. rcu_read_lock();
  102. cpufreq_for_each_entry(pos, freq_tbl) {
  103. opp = dev_pm_opp_find_freq_exact(dvfs_info->dev,
  104. pos->frequency * 1000, true);
  105. if (IS_ERR(opp)) {
  106. rcu_read_unlock();
  107. dev_err(dvfs_info->dev,
  108. "failed to find valid OPP for %u KHZ\n",
  109. pos->frequency);
  110. return PTR_ERR(opp);
  111. }
  112. freq = pos->frequency / 1000; /* In MHZ */
  113. clk_div = ((freq / CPU_DIV_FREQ_MAX) & P0_7_CPUCLKDEV_MASK)
  114. << P0_7_CPUCLKDEV_SHIFT;
  115. clk_div |= ((freq / CPU_ATB_FREQ_MAX) & P0_7_ATBCLKDEV_MASK)
  116. << P0_7_ATBCLKDEV_SHIFT;
  117. clk_div |= ((freq / CPU_DBG_FREQ_MAX) & P0_7_CSCLKDEV_MASK)
  118. << P0_7_CSCLKDEV_SHIFT;
  119. /* Calculate EMA */
  120. volt_id = dev_pm_opp_get_voltage(opp);
  121. volt_id = (MAX_VOLTAGE - volt_id) / VOLTAGE_STEP;
  122. if (volt_id < PMIC_HIGH_VOLT) {
  123. ema_div = (CPUEMA_HIGH << P0_7_CPUEMA_SHIFT) |
  124. (L2EMA_HIGH << P0_7_L2EMA_SHIFT);
  125. } else if (volt_id > PMIC_LOW_VOLT) {
  126. ema_div = (CPUEMA_LOW << P0_7_CPUEMA_SHIFT) |
  127. (L2EMA_LOW << P0_7_L2EMA_SHIFT);
  128. } else {
  129. ema_div = (CPUEMA_MID << P0_7_CPUEMA_SHIFT) |
  130. (L2EMA_MID << P0_7_L2EMA_SHIFT);
  131. }
  132. tmp = (clk_div | ema_div | (volt_id << P0_7_VDD_SHIFT)
  133. | ((freq / FREQ_UNIT) << P0_7_FREQ_SHIFT));
  134. __raw_writel(tmp, dvfs_info->base + XMU_PMU_P0_7 + 4 *
  135. (pos - freq_tbl));
  136. }
  137. rcu_read_unlock();
  138. return 0;
  139. }
  140. static void exynos_enable_dvfs(unsigned int cur_frequency)
  141. {
  142. unsigned int tmp, cpu;
  143. struct cpufreq_frequency_table *freq_table = dvfs_info->freq_table;
  144. struct cpufreq_frequency_table *pos;
  145. /* Disable DVFS */
  146. __raw_writel(0, dvfs_info->base + XMU_DVFS_CTRL);
  147. /* Enable PSTATE Change Event */
  148. tmp = __raw_readl(dvfs_info->base + XMU_PMUEVTEN);
  149. tmp |= (1 << PSTATE_CHANGED_EVTEN_SHIFT);
  150. __raw_writel(tmp, dvfs_info->base + XMU_PMUEVTEN);
  151. /* Enable PSTATE Change IRQ */
  152. tmp = __raw_readl(dvfs_info->base + XMU_PMUIRQEN);
  153. tmp |= (1 << PSTATE_CHANGED_IRQEN_SHIFT);
  154. __raw_writel(tmp, dvfs_info->base + XMU_PMUIRQEN);
  155. /* Set initial performance index */
  156. cpufreq_for_each_entry(pos, freq_table)
  157. if (pos->frequency == cur_frequency)
  158. break;
  159. if (pos->frequency == CPUFREQ_TABLE_END) {
  160. dev_crit(dvfs_info->dev, "Boot up frequency not supported\n");
  161. /* Assign the highest frequency */
  162. pos = freq_table;
  163. cur_frequency = pos->frequency;
  164. }
  165. dev_info(dvfs_info->dev, "Setting dvfs initial frequency = %uKHZ",
  166. cur_frequency);
  167. for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++) {
  168. tmp = __raw_readl(dvfs_info->base + XMU_C0_3_PSTATE + cpu * 4);
  169. tmp &= ~(P_VALUE_MASK << C0_3_PSTATE_NEW_SHIFT);
  170. tmp |= ((pos - freq_table) << C0_3_PSTATE_NEW_SHIFT);
  171. __raw_writel(tmp, dvfs_info->base + XMU_C0_3_PSTATE + cpu * 4);
  172. }
  173. /* Enable DVFS */
  174. __raw_writel(1 << XMU_DVFS_CTRL_EN_SHIFT,
  175. dvfs_info->base + XMU_DVFS_CTRL);
  176. }
  177. static int exynos_target(struct cpufreq_policy *policy, unsigned int index)
  178. {
  179. unsigned int tmp;
  180. int i;
  181. struct cpufreq_frequency_table *freq_table = dvfs_info->freq_table;
  182. mutex_lock(&cpufreq_lock);
  183. freqs.old = policy->cur;
  184. freqs.new = freq_table[index].frequency;
  185. cpufreq_freq_transition_begin(policy, &freqs);
  186. /* Set the target frequency in all C0_3_PSTATE register */
  187. for_each_cpu(i, policy->cpus) {
  188. tmp = __raw_readl(dvfs_info->base + XMU_C0_3_PSTATE + i * 4);
  189. tmp &= ~(P_VALUE_MASK << C0_3_PSTATE_NEW_SHIFT);
  190. tmp |= (index << C0_3_PSTATE_NEW_SHIFT);
  191. __raw_writel(tmp, dvfs_info->base + XMU_C0_3_PSTATE + i * 4);
  192. }
  193. mutex_unlock(&cpufreq_lock);
  194. return 0;
  195. }
  196. static void exynos_cpufreq_work(struct work_struct *work)
  197. {
  198. unsigned int cur_pstate, index;
  199. struct cpufreq_policy *policy = cpufreq_cpu_get(0); /* boot CPU */
  200. struct cpufreq_frequency_table *freq_table = dvfs_info->freq_table;
  201. /* Ensure we can access cpufreq structures */
  202. if (unlikely(dvfs_info->dvfs_enabled == false))
  203. goto skip_work;
  204. mutex_lock(&cpufreq_lock);
  205. freqs.old = policy->cur;
  206. cur_pstate = __raw_readl(dvfs_info->base + XMU_P_STATUS);
  207. if (cur_pstate >> C0_3_PSTATE_VALID_SHIFT & 0x1)
  208. index = (cur_pstate >> C0_3_PSTATE_CURR_SHIFT) & P_VALUE_MASK;
  209. else
  210. index = (cur_pstate >> C0_3_PSTATE_NEW_SHIFT) & P_VALUE_MASK;
  211. if (likely(index < dvfs_info->freq_count)) {
  212. freqs.new = freq_table[index].frequency;
  213. } else {
  214. dev_crit(dvfs_info->dev, "New frequency out of range\n");
  215. freqs.new = freqs.old;
  216. }
  217. cpufreq_freq_transition_end(policy, &freqs, 0);
  218. cpufreq_cpu_put(policy);
  219. mutex_unlock(&cpufreq_lock);
  220. skip_work:
  221. enable_irq(dvfs_info->irq);
  222. }
  223. static irqreturn_t exynos_cpufreq_irq(int irq, void *id)
  224. {
  225. unsigned int tmp;
  226. tmp = __raw_readl(dvfs_info->base + XMU_PMUIRQ);
  227. if (tmp >> PSTATE_CHANGED_SHIFT & 0x1) {
  228. __raw_writel(tmp, dvfs_info->base + XMU_PMUIRQ);
  229. disable_irq_nosync(irq);
  230. schedule_work(&dvfs_info->irq_work);
  231. }
  232. return IRQ_HANDLED;
  233. }
  234. static void exynos_sort_descend_freq_table(void)
  235. {
  236. struct cpufreq_frequency_table *freq_tbl = dvfs_info->freq_table;
  237. int i = 0, index;
  238. unsigned int tmp_freq;
  239. /*
  240. * Exynos5440 clock controller state logic expects the cpufreq table to
  241. * be in descending order. But the OPP library constructs the table in
  242. * ascending order. So to make the table descending we just need to
  243. * swap the i element with the N - i element.
  244. */
  245. for (i = 0; i < dvfs_info->freq_count / 2; i++) {
  246. index = dvfs_info->freq_count - i - 1;
  247. tmp_freq = freq_tbl[i].frequency;
  248. freq_tbl[i].frequency = freq_tbl[index].frequency;
  249. freq_tbl[index].frequency = tmp_freq;
  250. }
  251. }
  252. static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy)
  253. {
  254. policy->clk = dvfs_info->cpu_clk;
  255. return cpufreq_generic_init(policy, dvfs_info->freq_table,
  256. dvfs_info->latency);
  257. }
  258. static struct cpufreq_driver exynos_driver = {
  259. .flags = CPUFREQ_STICKY | CPUFREQ_ASYNC_NOTIFICATION |
  260. CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  261. .verify = cpufreq_generic_frequency_table_verify,
  262. .target_index = exynos_target,
  263. .get = cpufreq_generic_get,
  264. .init = exynos_cpufreq_cpu_init,
  265. .name = CPUFREQ_NAME,
  266. .attr = cpufreq_generic_attr,
  267. };
  268. static const struct of_device_id exynos_cpufreq_match[] = {
  269. {
  270. .compatible = "samsung,exynos5440-cpufreq",
  271. },
  272. {},
  273. };
  274. MODULE_DEVICE_TABLE(of, exynos_cpufreq_match);
  275. static int exynos_cpufreq_probe(struct platform_device *pdev)
  276. {
  277. int ret = -EINVAL;
  278. struct device_node *np;
  279. struct resource res;
  280. unsigned int cur_frequency;
  281. np = pdev->dev.of_node;
  282. if (!np)
  283. return -ENODEV;
  284. dvfs_info = devm_kzalloc(&pdev->dev, sizeof(*dvfs_info), GFP_KERNEL);
  285. if (!dvfs_info) {
  286. ret = -ENOMEM;
  287. goto err_put_node;
  288. }
  289. dvfs_info->dev = &pdev->dev;
  290. ret = of_address_to_resource(np, 0, &res);
  291. if (ret)
  292. goto err_put_node;
  293. dvfs_info->base = devm_ioremap_resource(dvfs_info->dev, &res);
  294. if (IS_ERR(dvfs_info->base)) {
  295. ret = PTR_ERR(dvfs_info->base);
  296. goto err_put_node;
  297. }
  298. dvfs_info->irq = irq_of_parse_and_map(np, 0);
  299. if (!dvfs_info->irq) {
  300. dev_err(dvfs_info->dev, "No cpufreq irq found\n");
  301. ret = -ENODEV;
  302. goto err_put_node;
  303. }
  304. ret = dev_pm_opp_of_add_table(dvfs_info->dev);
  305. if (ret) {
  306. dev_err(dvfs_info->dev, "failed to init OPP table: %d\n", ret);
  307. goto err_put_node;
  308. }
  309. ret = dev_pm_opp_init_cpufreq_table(dvfs_info->dev,
  310. &dvfs_info->freq_table);
  311. if (ret) {
  312. dev_err(dvfs_info->dev,
  313. "failed to init cpufreq table: %d\n", ret);
  314. goto err_free_opp;
  315. }
  316. dvfs_info->freq_count = dev_pm_opp_get_opp_count(dvfs_info->dev);
  317. exynos_sort_descend_freq_table();
  318. if (of_property_read_u32(np, "clock-latency", &dvfs_info->latency))
  319. dvfs_info->latency = DEF_TRANS_LATENCY;
  320. dvfs_info->cpu_clk = devm_clk_get(dvfs_info->dev, "armclk");
  321. if (IS_ERR(dvfs_info->cpu_clk)) {
  322. dev_err(dvfs_info->dev, "Failed to get cpu clock\n");
  323. ret = PTR_ERR(dvfs_info->cpu_clk);
  324. goto err_free_table;
  325. }
  326. cur_frequency = clk_get_rate(dvfs_info->cpu_clk);
  327. if (!cur_frequency) {
  328. dev_err(dvfs_info->dev, "Failed to get clock rate\n");
  329. ret = -EINVAL;
  330. goto err_free_table;
  331. }
  332. cur_frequency /= 1000;
  333. INIT_WORK(&dvfs_info->irq_work, exynos_cpufreq_work);
  334. ret = devm_request_irq(dvfs_info->dev, dvfs_info->irq,
  335. exynos_cpufreq_irq, IRQF_TRIGGER_NONE,
  336. CPUFREQ_NAME, dvfs_info);
  337. if (ret) {
  338. dev_err(dvfs_info->dev, "Failed to register IRQ\n");
  339. goto err_free_table;
  340. }
  341. ret = init_div_table();
  342. if (ret) {
  343. dev_err(dvfs_info->dev, "Failed to initialise div table\n");
  344. goto err_free_table;
  345. }
  346. exynos_enable_dvfs(cur_frequency);
  347. ret = cpufreq_register_driver(&exynos_driver);
  348. if (ret) {
  349. dev_err(dvfs_info->dev,
  350. "%s: failed to register cpufreq driver\n", __func__);
  351. goto err_free_table;
  352. }
  353. of_node_put(np);
  354. dvfs_info->dvfs_enabled = true;
  355. return 0;
  356. err_free_table:
  357. dev_pm_opp_free_cpufreq_table(dvfs_info->dev, &dvfs_info->freq_table);
  358. err_free_opp:
  359. dev_pm_opp_of_remove_table(dvfs_info->dev);
  360. err_put_node:
  361. of_node_put(np);
  362. dev_err(&pdev->dev, "%s: failed initialization\n", __func__);
  363. return ret;
  364. }
  365. static int exynos_cpufreq_remove(struct platform_device *pdev)
  366. {
  367. cpufreq_unregister_driver(&exynos_driver);
  368. dev_pm_opp_free_cpufreq_table(dvfs_info->dev, &dvfs_info->freq_table);
  369. dev_pm_opp_of_remove_table(dvfs_info->dev);
  370. return 0;
  371. }
  372. static struct platform_driver exynos_cpufreq_platdrv = {
  373. .driver = {
  374. .name = "exynos5440-cpufreq",
  375. .of_match_table = exynos_cpufreq_match,
  376. },
  377. .probe = exynos_cpufreq_probe,
  378. .remove = exynos_cpufreq_remove,
  379. };
  380. module_platform_driver(exynos_cpufreq_platdrv);
  381. MODULE_AUTHOR("Amit Daniel Kachhap <amit.daniel@samsung.com>");
  382. MODULE_DESCRIPTION("Exynos5440 cpufreq driver");
  383. MODULE_LICENSE("GPL");