ENGR00221277 MX6DL/S - Set AXI clock to 270MHz
[wandboard.git] / arch / arm / mach-mx6 / bus_freq.c
blob9304c48361fa4cb664d714ea56e0ff30067153a5
1 /*
2 * Copyright (C) 2011-2012 Freescale Semiconductor, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /*!
20 * @file bus_freq.c
22 * @brief A common API for the Freescale Semiconductor i.MXC CPUfreq module
23 * and DVFS CORE module.
25 * The APIs are for setting bus frequency to low or high.
27 * @ingroup PM
29 #include <asm/io.h>
30 #include <linux/sched.h>
31 #include <linux/proc_fs.h>
32 #include <linux/clk.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/mutex.h>
37 #include <mach/iram.h>
38 #include <mach/hardware.h>
39 #include <mach/clock.h>
40 #include <mach/mxc_dvfs.h>
41 #include <mach/sdram_autogating.h>
42 #include <asm/mach/map.h>
43 #include <asm/mach-types.h>
44 #include <asm/cacheflush.h>
45 #include <asm/tlb.h>
46 #include "crm_regs.h"
47 #include <linux/suspend.h>
49 #define LPAPM_CLK 24000000
50 #define DDR_MED_CLK 400000000
51 #define DDR3_NORMAL_CLK 528000000
52 #define GPC_PGC_GPU_PGCR_OFFSET 0x260
53 #define GPC_CNTR_OFFSET 0x0
55 DEFINE_SPINLOCK(ddr_freq_lock);
57 int low_bus_freq_mode;
58 int audio_bus_freq_mode;
59 int high_bus_freq_mode;
60 int med_bus_freq_mode;
62 int bus_freq_scaling_initialized;
63 static struct device *busfreq_dev;
64 static int busfreq_suspended;
66 /* True if bus_frequency is scaled not using DVFS-PER */
67 int bus_freq_scaling_is_active;
69 int lp_high_freq;
70 int lp_med_freq;
71 int lp_audio_freq;
72 unsigned int ddr_low_rate;
73 unsigned int ddr_med_rate;
74 unsigned int ddr_normal_rate;
76 int low_freq_bus_used(void);
77 void set_ddr_freq(int ddr_freq);
79 extern int init_mmdc_settings(void);
80 extern struct cpu_op *(*get_cpu_op)(int *op);
81 extern int update_ddr_freq(int ddr_rate);
82 extern int chip_rev;
83 extern bool arm_mem_clked_in_wait;
85 DEFINE_MUTEX(bus_freq_mutex);
87 struct timeval start_time;
88 struct timeval end_time;
90 static int cpu_op_nr;
91 static struct cpu_op *cpu_op_tbl;
92 static struct clk *pll2_400;
93 static struct clk *axi_clk;
94 static struct clk *ahb_clk;
95 static struct clk *periph_clk;
96 static struct clk *osc_clk;
97 static struct clk *cpu_clk;
98 static struct clk *pll3;
99 static struct clk *pll2;
100 static struct clk *pll3_sw_clk;
101 static struct clk *pll2_200;
102 static struct clk *mmdc_ch0_axi;
103 struct regulator *vddsoc_cap_regulator;
104 static struct clk *pll3_540;
106 static struct delayed_work low_bus_freq_handler;
108 static void reduce_bus_freq_handler(struct work_struct *work)
110 int ret;
112 mutex_lock(&bus_freq_mutex);
113 if (low_bus_freq_mode || !low_freq_bus_used()) {
114 mutex_unlock(&bus_freq_mutex);
115 return;
118 if (audio_bus_freq_mode && lp_audio_freq) {
119 mutex_unlock(&bus_freq_mutex);
120 return;
123 if (!cpu_is_mx6sl()) {
124 if (cpu_is_mx6dl() &&
125 (clk_get_parent(axi_clk) != periph_clk))
126 /* Set the axi_clk to be sourced from the periph_clk.
127 * So that its frequency can be lowered down to 50MHz
128 * or 24MHz as the case may be.
130 clk_set_parent(axi_clk, periph_clk);
132 clk_enable(pll3);
133 if (lp_audio_freq) {
134 /* Need to ensure that PLL2_PFD_400M is kept ON. */
135 clk_enable(pll2_400);
136 update_ddr_freq(50000000);
137 /* Make sure periph clk's parent also got updated */
138 clk_set_parent(periph_clk, pll2_200);
139 audio_bus_freq_mode = 1;
140 low_bus_freq_mode = 0;
141 } else {
142 update_ddr_freq(24000000);
143 /* Make sure periph clk's parent also got updated */
144 clk_set_parent(periph_clk, osc_clk);
145 if (audio_bus_freq_mode)
146 clk_disable(pll2_400);
147 low_bus_freq_mode = 1;
148 audio_bus_freq_mode = 0;
151 if (med_bus_freq_mode)
152 clk_disable(pll2_400);
154 clk_disable(pll3);
155 med_bus_freq_mode = 0;
156 } else {
157 /* Set VDDSOC_CAP to 1.1V */
158 ret = regulator_set_voltage(vddsoc_cap_regulator, 1100000,
159 1100000);
160 if (ret < 0) {
161 printk(KERN_DEBUG
162 "COULD NOT DECREASE VDDSOC_CAP VOLTAGE!!!!\n");
163 return;
166 udelay(150);
168 arm_mem_clked_in_wait = true;
170 /* Set periph_clk to be sourced from OSC_CLK */
171 /* Set MMDC clk to 25MHz. */
172 /* First need to set the divider before changing the parent */
173 /* if parent clock is larger than previous one */
174 clk_set_rate(mmdc_ch0_axi, clk_get_rate(mmdc_ch0_axi) / 2);
175 clk_set_parent(mmdc_ch0_axi, pll3_sw_clk);
176 clk_set_parent(mmdc_ch0_axi, pll2_200);
177 clk_set_rate(mmdc_ch0_axi,
178 clk_round_rate(mmdc_ch0_axi, LPAPM_CLK));
180 /* Set AXI to 24MHz. */
181 clk_set_parent(periph_clk, osc_clk);
182 clk_set_rate(axi_clk, clk_round_rate(axi_clk, LPAPM_CLK));
183 /* Set AHB to 24MHz. */
184 clk_set_rate(ahb_clk, clk_round_rate(ahb_clk, LPAPM_CLK));
186 low_bus_freq_mode = 1;
187 audio_bus_freq_mode = 0;
190 high_bus_freq_mode = 0;
191 med_bus_freq_mode = 0;
192 mutex_unlock(&bus_freq_mutex);
194 /* Set the DDR, AHB to 24MHz.
195 * This mode will be activated only when none of the modules that
196 * need a higher DDR or AHB frequency are active.
198 int set_low_bus_freq(void)
200 if (busfreq_suspended)
201 return 0;
203 if (!bus_freq_scaling_initialized || !bus_freq_scaling_is_active)
204 return 0;
206 /* Don't lower the frequency immediately. Instead scheduled a delayed
207 * work and drop the freq if the conditions still remain the same.
209 schedule_delayed_work(&low_bus_freq_handler, usecs_to_jiffies(3000000));
210 return 0;
213 /* Set the DDR to either 528MHz or 400MHz for MX6q
214 * or 400MHz for MX6DL.
216 int set_high_bus_freq(int high_bus_freq)
218 int ret;
220 if (bus_freq_scaling_initialized && bus_freq_scaling_is_active)
221 cancel_delayed_work_sync(&low_bus_freq_handler);
222 mutex_lock(&bus_freq_mutex);
223 if (busfreq_suspended) {
224 mutex_unlock(&bus_freq_mutex);
225 return 0;
228 if (!bus_freq_scaling_initialized || !bus_freq_scaling_is_active) {
229 mutex_unlock(&bus_freq_mutex);
230 return 0;
233 if (high_bus_freq_mode && high_bus_freq) {
234 mutex_unlock(&bus_freq_mutex);
235 return 0;
238 if (med_bus_freq_mode && !high_bus_freq) {
239 mutex_unlock(&bus_freq_mutex);
240 return 0;
243 if (cpu_is_mx6dl() && high_bus_freq)
244 high_bus_freq = 0;
246 if (cpu_is_mx6dl() && med_bus_freq_mode) {
247 mutex_unlock(&bus_freq_mutex);
248 return 0;
250 if ((high_bus_freq_mode && (high_bus_freq || lp_high_freq)) ||
251 (med_bus_freq_mode && !high_bus_freq && lp_med_freq &&
252 !lp_high_freq)) {
253 mutex_unlock(&bus_freq_mutex);
254 return 0;
256 if (cpu_is_mx6sl()) {
257 /* Set the voltage of VDDSOC to 1.2V as in normal mode. */
258 ret = regulator_set_voltage(vddsoc_cap_regulator, 1200000,
259 1200000);
260 if (ret < 0) {
261 printk(KERN_DEBUG
262 "COULD NOT INCREASE VDDSOC_CAP VOLTAGE!!!!\n");
263 return ret;
266 /* Need to wait for the regulator to come back up */
268 * Delay time is based on the number of 24MHz clock cycles
269 * programmed in the ANA_MISC2_BASE_ADDR for each
270 * 25mV step.
272 udelay(150);
274 /* Set periph_clk to be sourced from pll2_pfd2_400M */
275 /* First need to set the divider before changing the */
276 /* parent if parent clock is larger than previous one */
277 clk_set_rate(ahb_clk, clk_round_rate(ahb_clk,
278 LPAPM_CLK / 3));
279 clk_set_rate(axi_clk,
280 clk_round_rate(axi_clk, LPAPM_CLK / 2));
281 clk_set_parent(periph_clk, pll2_400);
283 /* Set mmdc_clk_root to be sourced */
284 /* from pll2_pfd2_400M */
285 clk_set_rate(mmdc_ch0_axi,
286 clk_round_rate(mmdc_ch0_axi, LPAPM_CLK / 2));
287 clk_set_parent(mmdc_ch0_axi, pll3_sw_clk);
288 clk_set_parent(mmdc_ch0_axi, pll2_400);
289 clk_set_rate(mmdc_ch0_axi,
290 clk_round_rate(mmdc_ch0_axi, DDR_MED_CLK));
292 high_bus_freq_mode = 1;
293 med_bus_freq_mode = 0;
294 } else {
295 clk_enable(pll3);
296 if (high_bus_freq) {
297 update_ddr_freq(ddr_normal_rate);
298 /* Make sure periph clk's parent also got updated */
299 clk_set_parent(periph_clk, pll2);
300 if (med_bus_freq_mode)
301 clk_disable(pll2_400);
302 high_bus_freq_mode = 1;
303 med_bus_freq_mode = 0;
304 } else {
305 clk_enable(pll2_400);
306 update_ddr_freq(ddr_med_rate);
307 /* Make sure periph clk's parent also got updated */
308 clk_set_parent(periph_clk, pll2_400);
309 high_bus_freq_mode = 0;
310 med_bus_freq_mode = 1;
312 if (audio_bus_freq_mode)
313 clk_disable(pll2_400);
315 /* AXI_CLK is sourced from PLL3_PFD_540 on MX6DL */
316 if (cpu_is_mx6dl() &&
317 clk_get_parent(axi_clk) != pll3_540)
318 clk_set_parent(axi_clk, pll3_540);
320 clk_disable(pll3);
323 low_bus_freq_mode = 0;
324 audio_bus_freq_mode = 0;
326 /* Ensure that WAIT mode can be entered in high bus freq mode. */
328 if (cpu_is_mx6sl())
329 arm_mem_clked_in_wait = false;
331 mutex_unlock(&bus_freq_mutex);
332 return 0;
335 int low_freq_bus_used(void)
337 if (!bus_freq_scaling_initialized)
338 return 0;
340 /* We only go the lowest setpoint if ARM is also
341 * at the lowest setpoint.
343 if ((clk_get_rate(cpu_clk) >
344 cpu_op_tbl[cpu_op_nr - 1].cpu_rate)
345 || (cpu_op_nr == 1)) {
346 return 0;
349 if ((lp_high_freq == 0)
350 && (lp_med_freq == 0))
351 return 1;
352 else
353 return 0;
356 void bus_freq_update(struct clk *clk, bool flag)
358 mutex_lock(&bus_freq_mutex);
359 if (flag) {
360 /* Update count */
361 if (clk->flags & AHB_HIGH_SET_POINT)
362 lp_high_freq++;
363 else if (clk->flags & AHB_MED_SET_POINT)
364 lp_med_freq++;
365 else if (clk->flags & AHB_AUDIO_SET_POINT)
366 lp_audio_freq++;
367 /* Update bus freq */
368 if ((clk->flags & CPU_FREQ_TRIG_UPDATE)
369 && (clk_get_usecount(clk) == 0)) {
370 if (!(clk->flags &
371 (AHB_HIGH_SET_POINT | AHB_MED_SET_POINT))) {
372 if (low_freq_bus_used()) {
373 if ((clk->flags & AHB_AUDIO_SET_POINT) & !audio_bus_freq_mode)
374 set_low_bus_freq();
375 else if (!low_bus_freq_mode)
376 set_low_bus_freq();
378 } else {
379 if ((clk->flags & AHB_MED_SET_POINT)
380 && !med_bus_freq_mode) {
381 /* Set to Medium setpoint */
382 mutex_unlock(&bus_freq_mutex);
383 set_high_bus_freq(0);
384 return;
386 else if ((clk->flags & AHB_HIGH_SET_POINT)
387 && !high_bus_freq_mode) {
388 /* Currently at low or medium set point,
389 * need to set to high setpoint
391 mutex_unlock(&bus_freq_mutex);
392 set_high_bus_freq(1);
393 return;
397 } else {
398 /* Update count */
399 if (clk->flags & AHB_HIGH_SET_POINT)
400 lp_high_freq--;
401 else if (clk->flags & AHB_MED_SET_POINT)
402 lp_med_freq--;
403 else if (clk->flags & AHB_AUDIO_SET_POINT)
404 lp_audio_freq--;
405 /* Update bus freq */
406 if ((clk->flags & CPU_FREQ_TRIG_UPDATE)
407 && (clk_get_usecount(clk) == 0)) {
408 if (low_freq_bus_used() && !low_bus_freq_mode)
409 set_low_bus_freq();
410 else {
411 /* Set to either high or medium setpoint. */
412 mutex_unlock(&bus_freq_mutex);
413 set_high_bus_freq(0);
414 return;
418 mutex_unlock(&bus_freq_mutex);
419 return;
421 void setup_pll(void)
425 static ssize_t bus_freq_scaling_enable_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
428 if (bus_freq_scaling_is_active)
429 return sprintf(buf, "Bus frequency scaling is enabled\n");
430 else
431 return sprintf(buf, "Bus frequency scaling is disabled\n");
434 static ssize_t bus_freq_scaling_enable_store(struct device *dev,
435 struct device_attribute *attr,
436 const char *buf, size_t size)
438 if (strncmp(buf, "1", 1) == 0) {
439 bus_freq_scaling_is_active = 1;
440 set_high_bus_freq(0);
441 /* Make sure system can enter low bus mode if it should be in
442 low bus mode */
443 if (low_freq_bus_used() && !low_bus_freq_mode)
444 set_low_bus_freq();
445 } else if (strncmp(buf, "0", 1) == 0) {
446 if (bus_freq_scaling_is_active)
447 set_high_bus_freq(1);
448 bus_freq_scaling_is_active = 0;
450 return size;
453 static int busfreq_suspend(struct platform_device *pdev, pm_message_t message)
455 return 0;
458 static int bus_freq_pm_notify(struct notifier_block *nb, unsigned long event,
459 void *dummy)
461 if (event == PM_SUSPEND_PREPARE) {
462 set_high_bus_freq(1);
463 busfreq_suspended = 1;
464 } else if (event == PM_POST_SUSPEND) {
465 busfreq_suspended = 0;
468 return NOTIFY_OK;
470 static int busfreq_resume(struct platform_device *pdev)
472 return 0;
474 static struct notifier_block imx_bus_freq_pm_notifier = {
475 .notifier_call = bus_freq_pm_notify,
478 static DEVICE_ATTR(enable, 0644, bus_freq_scaling_enable_show,
479 bus_freq_scaling_enable_store);
482 * This is the probe routine for the bus frequency driver.
484 * @param pdev The platform device structure
486 * @return The function returns 0 on success
489 static int __devinit busfreq_probe(struct platform_device *pdev)
491 u32 err;
493 busfreq_dev = &pdev->dev;
495 pll2_400 = clk_get(NULL, "pll2_pfd_400M");
496 if (IS_ERR(pll2_400)) {
497 printk(KERN_DEBUG "%s: failed to get pll2_pfd_400M\n",
498 __func__);
499 return PTR_ERR(pll2_400);
502 pll2_200 = clk_get(NULL, "pll2_200M");
503 if (IS_ERR(pll2_200)) {
504 printk(KERN_DEBUG "%s: failed to get pll2_200M\n",
505 __func__);
506 return PTR_ERR(pll2_200);
509 pll2 = clk_get(NULL, "pll2");
510 if (IS_ERR(pll2)) {
511 printk(KERN_DEBUG "%s: failed to get pll2\n",
512 __func__);
513 return PTR_ERR(pll2);
516 cpu_clk = clk_get(NULL, "cpu_clk");
517 if (IS_ERR(cpu_clk)) {
518 printk(KERN_DEBUG "%s: failed to get cpu_clk\n",
519 __func__);
520 return PTR_ERR(cpu_clk);
523 pll3 = clk_get(NULL, "pll3_main_clk");
524 if (IS_ERR(pll3)) {
525 printk(KERN_DEBUG "%s: failed to get pll3\n",
526 __func__);
527 return PTR_ERR(pll3);
530 pll3_540 = clk_get(NULL, "pll3_pfd_540M");
531 if (IS_ERR(pll3_540)) {
532 printk(KERN_DEBUG "%s: failed to get periph_clk\n",
533 __func__);
534 return PTR_ERR(pll3_540);
537 pll3_sw_clk = clk_get(NULL, "pll3_sw_clk");
538 if (IS_ERR(pll3_sw_clk)) {
539 printk(KERN_DEBUG "%s: failed to get pll3_sw_clk\n",
540 __func__);
541 return PTR_ERR(pll3_sw_clk);
544 axi_clk = clk_get(NULL, "axi_clk");
545 if (IS_ERR(axi_clk)) {
546 printk(KERN_DEBUG "%s: failed to get axi_clk\n",
547 __func__);
548 return PTR_ERR(axi_clk);
551 ahb_clk = clk_get(NULL, "ahb");
552 if (IS_ERR(ahb_clk)) {
553 printk(KERN_DEBUG "%s: failed to get ahb_clk\n",
554 __func__);
555 return PTR_ERR(ahb_clk);
558 periph_clk = clk_get(NULL, "periph_clk");
559 if (IS_ERR(periph_clk)) {
560 printk(KERN_DEBUG "%s: failed to get periph_clk\n",
561 __func__);
562 return PTR_ERR(periph_clk);
565 osc_clk = clk_get(NULL, "osc");
566 if (IS_ERR(osc_clk)) {
567 printk(KERN_DEBUG "%s: failed to get osc_clk\n",
568 __func__);
569 return PTR_ERR(osc_clk);
572 mmdc_ch0_axi = clk_get(NULL, "mmdc_ch0_axi");
573 if (IS_ERR(mmdc_ch0_axi)) {
574 printk(KERN_DEBUG "%s: failed to get mmdc_ch0_axi\n",
575 __func__);
576 return PTR_ERR(mmdc_ch0_axi);
579 vddsoc_cap_regulator = regulator_get(NULL, "cpu_vddsoc");
580 if (IS_ERR(vddsoc_cap_regulator)) {
581 printk(KERN_ERR "%s: failed to get vddsoc_cap regulator\n",
582 __func__);
583 return PTR_ERR(vddsoc_cap_regulator);
586 err = sysfs_create_file(&busfreq_dev->kobj, &dev_attr_enable.attr);
587 if (err) {
588 printk(KERN_ERR
589 "Unable to register sysdev entry for BUSFREQ");
590 return err;
593 cpu_op_tbl = get_cpu_op(&cpu_op_nr);
594 low_bus_freq_mode = 0;
595 if (cpu_is_mx6dl()) {
596 high_bus_freq_mode = 0;
597 med_bus_freq_mode = 1;
598 /* To make pll2_400 use count right, as when
599 system enter 24M, it will disable pll2_400 */
600 clk_enable(pll2_400);
601 } else {
602 high_bus_freq_mode = 1;
603 med_bus_freq_mode = 0;
605 bus_freq_scaling_is_active = 0;
606 bus_freq_scaling_initialized = 1;
608 if (cpu_is_mx6q()) {
609 ddr_low_rate = LPAPM_CLK;
610 ddr_med_rate = DDR_MED_CLK;
611 ddr_normal_rate = DDR3_NORMAL_CLK;
613 if (cpu_is_mx6dl() || cpu_is_mx6sl()) {
614 ddr_low_rate = LPAPM_CLK;
615 ddr_normal_rate = ddr_med_rate = DDR_MED_CLK;
618 INIT_DELAYED_WORK(&low_bus_freq_handler, reduce_bus_freq_handler);
619 register_pm_notifier(&imx_bus_freq_pm_notifier);
621 if (!cpu_is_mx6sl())
622 init_mmdc_settings();
624 return 0;
627 static struct platform_driver busfreq_driver = {
628 .driver = {
629 .name = "imx_busfreq",
631 .probe = busfreq_probe,
632 .suspend = busfreq_suspend,
633 .resume = busfreq_resume,
637 * Initialise the busfreq_driver.
639 * @return The function always returns 0.
642 static int __init busfreq_init(void)
644 if (platform_driver_register(&busfreq_driver) != 0) {
645 printk(KERN_ERR "busfreq_driver register failed\n");
646 return -ENODEV;
649 printk(KERN_INFO "Bus freq driver module loaded\n");
651 /* Enable busfreq by default. */
652 bus_freq_scaling_is_active = 1;
654 if (cpu_is_mx6q())
655 set_high_bus_freq(1);
656 else
657 set_high_bus_freq(0);
659 /* Make sure system can enter low bus mode if it should be in
660 low bus mode */
661 if (low_freq_bus_used() && !low_bus_freq_mode)
662 set_low_bus_freq();
664 printk(KERN_INFO "Bus freq driver Enabled\n");
665 return 0;
668 static void __exit busfreq_cleanup(void)
670 sysfs_remove_file(&busfreq_dev->kobj, &dev_attr_enable.attr);
672 /* Unregister the device structure */
673 platform_driver_unregister(&busfreq_driver);
674 bus_freq_scaling_initialized = 0;
677 late_initcall(busfreq_init);
678 module_exit(busfreq_cleanup);
680 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
681 MODULE_DESCRIPTION("BusFreq driver");
682 MODULE_LICENSE("GPL");