2 * Copyright (C) 2011-2015 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.
20 * @file busfreq-imx6.c
22 * @brief A common API for the Freescale Semiconductor iMX6 Busfreq API
24 * The APIs are for setting bus frequency to different values based on the
25 * highest freqeuncy requested.
30 #include <asm/cacheflush.h>
31 #include <asm/fncpy.h>
33 #include <asm/mach/map.h>
34 #include <asm/mach-types.h>
36 #include <linux/busfreq-imx6.h>
37 #include <linux/clk.h>
38 #include <linux/clk-provider.h>
39 #include <linux/delay.h>
40 #include <linux/module.h>
41 #include <linux/mutex.h>
43 #include <linux/of_fdt.h>
44 #include <linux/platform_device.h>
45 #include <linux/proc_fs.h>
46 #include <linux/reboot.h>
47 #include <linux/regulator/consumer.h>
48 #include <linux/sched.h>
49 #include <linux/suspend.h>
54 #define LPAPM_CLK 24000000
55 #define DDR3_AUDIO_CLK 50000000
56 #define LPDDR2_AUDIO_CLK 100000000
58 #define MMDC_MDMISC_DDR_TYPE_DDR3 0
59 #define MMDC_MDMISC_DDR_TYPE_LPDDR2 1
62 int high_bus_freq_mode;
63 int med_bus_freq_mode;
64 int audio_bus_freq_mode;
65 int low_bus_freq_mode;
66 int ultra_low_bus_freq_mode;
67 unsigned int ddr_med_rate;
68 unsigned int ddr_normal_rate;
69 unsigned long ddr_freq_change_total_size;
70 unsigned long ddr_freq_change_iram_base;
71 unsigned long ddr_freq_change_iram_phys;
73 static int bus_freq_scaling_initialized;
74 static struct device *busfreq_dev;
75 static int busfreq_suspended;
76 static u32 org_arm_rate;
77 static int bus_freq_scaling_is_active;
78 static int high_bus_count, med_bus_count, audio_bus_count, low_bus_count;
79 static unsigned int ddr_low_rate;
81 extern unsigned long iram_tlb_phys_addr;
82 extern int unsigned long iram_tlb_base_addr;
84 extern int init_mmdc_lpddr2_settings(struct platform_device *dev);
85 extern int init_mmdc_ddr3_settings_imx6q(struct platform_device *dev);
86 extern int init_mmdc_ddr3_settings_imx6sx(struct platform_device *dev);
87 extern int update_ddr_freq_imx6q(int ddr_rate);
88 extern int update_ddr_freq_imx6sx(int ddr_rate);
89 extern int update_lpddr2_freq(int ddr_rate);
91 DEFINE_MUTEX(bus_freq_mutex);
93 static struct clk *mmdc_clk;
94 static struct clk *pll2_400;
95 static struct clk *periph_clk;
96 static struct clk *periph_pre_clk;
97 static struct clk *periph_clk2_sel;
98 static struct clk *periph_clk2;
99 static struct clk *osc_clk;
100 static struct clk *cpu_clk;
101 static struct clk *pll3;
102 static struct clk *pll2;
103 static struct clk *pll2_bus;
104 static struct clk *pll2_bypass_src;
105 static struct clk *pll2_bypass;
106 static struct clk *pll2_200;
107 static struct clk *pll1_sys;
108 static struct clk *periph2_clk;
109 static struct clk *ocram_clk;
110 static struct clk *ahb_clk;
111 static struct clk *pll1_sw_clk;
112 static struct clk *periph2_pre_clk;
113 static struct clk *periph2_clk2_sel;
114 static struct clk *periph2_clk2;
115 static struct clk *step_clk;
116 static struct clk *axi_alt_sel_clk;
117 static struct clk *axi_sel_clk;
118 static struct clk *pll3_pfd1_540m;
119 static struct clk *m4_clk;
120 static struct clk *pll1;
121 static struct clk *pll1_bypass;
122 static struct clk *pll1_bypass_src;
124 static u32 pll2_org_rate;
125 static struct delayed_work low_bus_freq_handler;
126 static struct delayed_work bus_freq_daemon;
128 static RAW_NOTIFIER_HEAD(busfreq_notifier_chain);
130 static int busfreq_notify(enum busfreq_event event)
134 ret = raw_notifier_call_chain(&busfreq_notifier_chain, event, NULL);
136 return notifier_to_errno(ret);
139 int register_busfreq_notifier(struct notifier_block *nb)
141 return raw_notifier_chain_register(&busfreq_notifier_chain, nb);
143 EXPORT_SYMBOL(register_busfreq_notifier);
145 int unregister_busfreq_notifier(struct notifier_block *nb)
147 return raw_notifier_chain_unregister(&busfreq_notifier_chain, nb);
149 EXPORT_SYMBOL(unregister_busfreq_notifier);
151 static bool check_m4_sleep(void)
153 unsigned long timeout = jiffies + msecs_to_jiffies(500);
155 while (imx_gpc_is_m4_sleeping() == 0)
156 if (time_after(jiffies, timeout))
161 static void enter_lpm_imx6sx(void)
163 if (imx_src_is_m4_enabled())
164 if (!check_m4_sleep())
165 pr_err("M4 is NOT in sleep!!!\n");
167 /* set periph_clk2 to source from OSC for periph */
168 imx_clk_set_parent(periph_clk2_sel, osc_clk);
169 imx_clk_set_parent(periph_clk, periph_clk2);
170 /* set ahb/ocram to 24MHz */
171 imx_clk_set_rate(ahb_clk, LPAPM_CLK);
172 imx_clk_set_rate(ocram_clk, LPAPM_CLK);
174 if (audio_bus_count) {
175 /* Need to ensure that PLL2_PFD_400M is kept ON. */
176 clk_prepare_enable(pll2_400);
177 if (ddr_type == MMDC_MDMISC_DDR_TYPE_DDR3)
178 update_ddr_freq_imx6sx(DDR3_AUDIO_CLK);
179 else if (ddr_type == MMDC_MDMISC_DDR_TYPE_LPDDR2)
180 update_lpddr2_freq(LPDDR2_AUDIO_CLK);
181 imx_clk_set_parent(periph2_clk2_sel, pll3);
182 imx_clk_set_parent(periph2_pre_clk, pll2_400);
183 imx_clk_set_parent(periph2_clk, periph2_pre_clk);
185 * As periph2_clk's parent is not changed from
186 * high mode to audio mode, so clk framework
187 * will not update its children's freq, but we
188 * change the mmdc's podf in asm code, so here
189 * need to update mmdc rate to make sure clk
190 * tree is right, although it will not do any
191 * change to hardware.
193 if (high_bus_freq_mode) {
194 if (ddr_type == MMDC_MDMISC_DDR_TYPE_DDR3)
195 imx_clk_set_rate(mmdc_clk, DDR3_AUDIO_CLK);
196 else if (ddr_type == MMDC_MDMISC_DDR_TYPE_LPDDR2)
197 imx_clk_set_rate(mmdc_clk, LPDDR2_AUDIO_CLK);
199 audio_bus_freq_mode = 1;
200 low_bus_freq_mode = 0;
202 if (ddr_type == MMDC_MDMISC_DDR_TYPE_DDR3)
203 update_ddr_freq_imx6sx(LPAPM_CLK);
204 else if (ddr_type == MMDC_MDMISC_DDR_TYPE_LPDDR2)
205 update_lpddr2_freq(LPAPM_CLK);
206 imx_clk_set_parent(periph2_clk2_sel, osc_clk);
207 imx_clk_set_parent(periph2_clk, periph2_clk2);
209 if (audio_bus_freq_mode)
210 clk_disable_unprepare(pll2_400);
211 low_bus_freq_mode = 1;
212 audio_bus_freq_mode = 0;
216 static void exit_lpm_imx6sx(void)
218 clk_prepare_enable(pll2_400);
221 * lower ahb/ocram's freq first to avoid too high
222 * freq during parent switch from OSC to pll3.
224 imx_clk_set_rate(ahb_clk, LPAPM_CLK / 3);
225 imx_clk_set_rate(ocram_clk, LPAPM_CLK / 2);
226 /* set periph_clk2 to pll3 */
227 imx_clk_set_parent(periph_clk2_sel, pll3);
228 /* set periph clk to from pll2_400 */
229 imx_clk_set_parent(periph_pre_clk, pll2_400);
230 imx_clk_set_parent(periph_clk, periph_pre_clk);
232 if (ddr_type == MMDC_MDMISC_DDR_TYPE_DDR3)
233 update_ddr_freq_imx6sx(ddr_normal_rate);
234 else if (ddr_type == MMDC_MDMISC_DDR_TYPE_LPDDR2)
235 update_lpddr2_freq(ddr_normal_rate);
236 /* correct parent info after ddr freq change in asm code */
237 imx_clk_set_parent(periph2_pre_clk, pll2_400);
238 imx_clk_set_parent(periph2_clk, periph2_pre_clk);
239 imx_clk_set_parent(periph2_clk2_sel, pll3);
242 * As periph2_clk's parent is not changed from
243 * audio mode to high mode, so clk framework
244 * will not update its children's freq, but we
245 * change the mmdc's podf in asm code, so here
246 * need to update mmdc rate to make sure clk
247 * tree is right, although it will not do any
248 * change to hardware.
250 if (audio_bus_freq_mode)
251 imx_clk_set_rate(mmdc_clk, ddr_normal_rate);
253 clk_disable_unprepare(pll2_400);
255 if (audio_bus_freq_mode)
256 clk_disable_unprepare(pll2_400);
259 static void enter_lpm_imx6sl(void)
261 if (high_bus_freq_mode) {
262 pll2_org_rate = clk_get_rate(pll2_bus);
263 /* Set periph_clk to be sourced from OSC_CLK */
264 imx_clk_set_parent(periph_clk2_sel, osc_clk);
265 imx_clk_set_parent(periph_clk, periph_clk2);
266 /* Ensure AHB/AXI clks are at 24MHz. */
267 imx_clk_set_rate(ahb_clk, LPAPM_CLK);
268 imx_clk_set_rate(ocram_clk, LPAPM_CLK);
270 if (audio_bus_count) {
271 /* Set AHB to 8MHz to lower pwer.*/
272 imx_clk_set_rate(ahb_clk, LPAPM_CLK / 3);
274 /* Set up DDR to 100MHz. */
275 update_lpddr2_freq(LPDDR2_AUDIO_CLK);
277 /* Fix the clock tree in kernel */
278 imx_clk_set_parent(periph2_pre_clk, pll2_200);
279 imx_clk_set_parent(periph2_clk, periph2_pre_clk);
281 if (low_bus_freq_mode || ultra_low_bus_freq_mode) {
283 * Fix the clock tree in kernel, make sure
284 * pll2_bypass is updated as it is
287 imx_clk_set_parent(pll2_bypass, pll2);
289 * Swtich ARM to run off PLL2_PFD2_400MHz
290 * since DDR is anyway at 100MHz.
292 imx_clk_set_parent(step_clk, pll2_400);
293 imx_clk_set_parent(pll1_sw_clk, step_clk);
295 * Need to ensure that PLL1 is bypassed and enabled
296 * before ARM-PODF is set.
298 clk_set_parent(pll1_bypass, pll1_bypass_src);
301 * Ensure that the clock will be
304 imx_clk_set_rate(cpu_clk, org_arm_rate);
306 low_bus_freq_mode = 0;
307 ultra_low_bus_freq_mode = 0;
308 audio_bus_freq_mode = 1;
310 u32 arm_div, pll1_rate;
311 org_arm_rate = clk_get_rate(cpu_clk);
312 if (low_bus_freq_mode && low_bus_count == 0) {
314 * We are already in DDR @ 24MHz state, but
315 * no one but ARM needs the DDR. In this case,
316 * we can lower the DDR freq to 1MHz when ARM
317 * enters WFI in this state. Keep track of this state.
319 ultra_low_bus_freq_mode = 1;
320 low_bus_freq_mode = 0;
321 audio_bus_freq_mode = 0;
323 if (!ultra_low_bus_freq_mode && !low_bus_freq_mode) {
325 * Anyway, make sure the AHB is running at 24MHz
326 * in low_bus_freq_mode.
328 if (audio_bus_freq_mode)
329 imx_clk_set_rate(ahb_clk, LPAPM_CLK);
332 * Since we are going to bypass PLL2,
333 * we need to move ARM clk off PLL2_PFD2
334 * to PLL1. Make sure the PLL1 is running
335 * at the lowest possible freq.
336 * To work well with CPUFREQ we want to ensure that
337 * the CPU freq does not change, so attempt to
338 * get a freq as close to 396MHz as possible.
340 imx_clk_set_rate(pll1,
341 clk_round_rate(pll1, (org_arm_rate * 2)));
342 pll1_rate = clk_get_rate(pll1);
343 arm_div = pll1_rate / org_arm_rate;
344 if (pll1_rate / arm_div > org_arm_rate)
347 * Need to ensure that PLL1 is bypassed and enabled
348 * before ARM-PODF is set.
350 clk_set_parent(pll1_bypass, pll1);
352 * Ensure ARM CLK is lower before
353 * changing the parent.
355 imx_clk_set_rate(cpu_clk, org_arm_rate / arm_div);
356 /* Now set the ARM clk parent to PLL1_SYS. */
357 imx_clk_set_parent(pll1_sw_clk, pll1_sys);
360 * Set STEP_CLK back to OSC to save power and
361 * also to maintain the parent.The WFI iram code
362 * will switch step_clk to osc, but the clock API
363 * is not aware of the change and when a new request
364 * to change the step_clk parent to pll2_pfd2_400M
365 * is requested sometime later, the change is ignored.
367 imx_clk_set_parent(step_clk, osc_clk);
368 /* Now set DDR to 24MHz. */
369 update_lpddr2_freq(LPAPM_CLK);
372 * Fix the clock tree in kernel.
373 * Make sure PLL2 rate is updated as it gets
374 * bypassed in the DDR freq change code.
376 imx_clk_set_parent(pll2_bypass, pll2_bypass_src);
377 imx_clk_set_parent(periph2_clk2_sel, pll2_bus);
378 imx_clk_set_parent(periph2_clk, periph2_clk2);
381 if (low_bus_count == 0) {
382 ultra_low_bus_freq_mode = 1;
383 low_bus_freq_mode = 0;
385 ultra_low_bus_freq_mode = 0;
386 low_bus_freq_mode = 1;
388 audio_bus_freq_mode = 0;
393 static void exit_lpm_imx6sl(void)
395 /* Change DDR freq in IRAM. */
396 update_lpddr2_freq(ddr_normal_rate);
399 * Fix the clock tree in kernel.
400 * Make sure PLL2 rate is updated as it gets
401 * un-bypassed in the DDR freq change code.
403 imx_clk_set_parent(pll2_bypass, pll2);
404 imx_clk_set_parent(periph2_pre_clk, pll2_400);
405 imx_clk_set_parent(periph2_clk, periph2_pre_clk);
407 /* Ensure that periph_clk is sourced from PLL2_400. */
408 imx_clk_set_parent(periph_pre_clk, pll2_400);
410 * Before switching the perhiph_clk, ensure that the
411 * AHB/AXI will not be too fast.
413 imx_clk_set_rate(ahb_clk, LPAPM_CLK / 3);
414 imx_clk_set_rate(ocram_clk, LPAPM_CLK / 2);
415 imx_clk_set_parent(periph_clk, periph_pre_clk);
417 if (low_bus_freq_mode || ultra_low_bus_freq_mode) {
418 /* Move ARM from PLL1_SW_CLK to PLL2_400. */
419 imx_clk_set_parent(step_clk, pll2_400);
420 imx_clk_set_parent(pll1_sw_clk, step_clk);
422 * Need to ensure that PLL1 is bypassed and enabled
423 * before ARM-PODF is set.
425 clk_set_parent(pll1_bypass, pll1_bypass_src);
426 imx_clk_set_rate(cpu_clk, org_arm_rate);
427 ultra_low_bus_freq_mode = 0;
431 static void reduce_bus_freq(void)
433 clk_prepare_enable(pll3);
434 if (audio_bus_count && (low_bus_freq_mode || ultra_low_bus_freq_mode))
435 busfreq_notify(LOW_BUSFREQ_EXIT);
436 else if (!audio_bus_count)
437 busfreq_notify(LOW_BUSFREQ_ENTER);
440 else if (cpu_is_imx6sx())
444 /* Set axi to periph_clk */
445 imx_clk_set_parent(axi_sel_clk, periph_clk);
447 if (audio_bus_count) {
448 /* Need to ensure that PLL2_PFD_400M is kept ON. */
449 clk_prepare_enable(pll2_400);
450 update_ddr_freq_imx6q(DDR3_AUDIO_CLK);
451 /* Make sure periph clk's parent also got updated */
452 imx_clk_set_parent(periph_clk2_sel, pll3);
453 imx_clk_set_parent(periph_pre_clk, pll2_200);
454 imx_clk_set_parent(periph_clk, periph_pre_clk);
455 audio_bus_freq_mode = 1;
456 low_bus_freq_mode = 0;
458 update_ddr_freq_imx6q(LPAPM_CLK);
459 /* Make sure periph clk's parent also got updated */
460 imx_clk_set_parent(periph_clk2_sel, osc_clk);
461 /* Set periph_clk parent to OSC via periph_clk2_sel */
462 imx_clk_set_parent(periph_clk, periph_clk2);
463 if (audio_bus_freq_mode)
464 clk_disable_unprepare(pll2_400);
465 low_bus_freq_mode = 1;
466 audio_bus_freq_mode = 0;
469 clk_disable_unprepare(pll3);
471 med_bus_freq_mode = 0;
472 high_bus_freq_mode = 0;
474 if (audio_bus_freq_mode)
475 dev_dbg(busfreq_dev, "Bus freq set to audio mode. Count:\
476 high %d, med %d, audio %d\n",
477 high_bus_count, med_bus_count, audio_bus_count);
478 if (low_bus_freq_mode)
479 dev_dbg(busfreq_dev, "Bus freq set to low mode. Count:\
480 high %d, med %d, audio %d\n",
481 high_bus_count, med_bus_count, audio_bus_count);
484 static void reduce_bus_freq_handler(struct work_struct *work)
486 mutex_lock(&bus_freq_mutex);
490 mutex_unlock(&bus_freq_mutex);
494 * Set the DDR, AHB to 24MHz.
495 * This mode will be activated only when none of the modules that
496 * need a higher DDR or AHB frequency are active.
498 int set_low_bus_freq(void)
500 if (busfreq_suspended)
503 if (!bus_freq_scaling_initialized || !bus_freq_scaling_is_active)
507 * Check to see if we need to got from
508 * low bus freq mode to audio bus freq mode.
509 * If so, the change needs to be done immediately.
511 if (audio_bus_count && (low_bus_freq_mode || ultra_low_bus_freq_mode))
515 * Don't lower the frequency immediately. Instead
516 * scheduled a delayed work and drop the freq if
517 * the conditions still remain the same.
519 schedule_delayed_work(&low_bus_freq_handler,
520 usecs_to_jiffies(3000000));
525 * Set the DDR to either 528MHz or 400MHz for iMX6qd
526 * or 400MHz for iMX6dl.
528 static int set_high_bus_freq(int high_bus_freq)
530 struct clk *periph_clk_parent;
532 if (bus_freq_scaling_initialized && bus_freq_scaling_is_active)
533 cancel_delayed_work_sync(&low_bus_freq_handler);
535 if (busfreq_suspended)
539 periph_clk_parent = pll2_bus;
541 periph_clk_parent = pll2_400;
543 if (!bus_freq_scaling_initialized || !bus_freq_scaling_is_active)
546 if (high_bus_freq_mode)
549 /* medium bus freq is only supported for MX6DQ */
550 if (med_bus_freq_mode && !high_bus_freq)
553 if (low_bus_freq_mode || ultra_low_bus_freq_mode)
554 busfreq_notify(LOW_BUSFREQ_EXIT);
556 clk_prepare_enable(pll3);
559 else if (cpu_is_imx6sx())
563 clk_prepare_enable(pll2_400);
564 update_ddr_freq_imx6q(ddr_normal_rate);
565 /* Make sure periph clk's parent also got updated */
566 imx_clk_set_parent(periph_clk2_sel, pll3);
567 imx_clk_set_parent(periph_pre_clk, periph_clk_parent);
568 imx_clk_set_parent(periph_clk, periph_pre_clk);
569 if (cpu_is_imx6dl()) {
570 /* Set axi to pll3_pfd1_540m */
571 imx_clk_set_parent(axi_alt_sel_clk, pll3_pfd1_540m);
572 imx_clk_set_parent(axi_sel_clk, axi_alt_sel_clk);
574 clk_disable_unprepare(pll2_400);
576 update_ddr_freq_imx6q(ddr_med_rate);
577 /* Make sure periph clk's parent also got updated */
578 imx_clk_set_parent(periph_clk2_sel, pll3);
579 imx_clk_set_parent(periph_pre_clk, pll2_400);
580 imx_clk_set_parent(periph_clk, periph_pre_clk);
582 if (audio_bus_freq_mode)
583 clk_disable_unprepare(pll2_400);
586 high_bus_freq_mode = 1;
587 med_bus_freq_mode = 0;
588 low_bus_freq_mode = 0;
589 audio_bus_freq_mode = 0;
591 clk_disable_unprepare(pll3);
592 if (high_bus_freq_mode)
593 dev_dbg(busfreq_dev, "Bus freq set to high mode. Count:\
594 high %d, med %d, audio %d\n",
595 high_bus_count, med_bus_count, audio_bus_count);
596 if (med_bus_freq_mode)
597 dev_dbg(busfreq_dev, "Bus freq set to med mode. Count:\
598 high %d, med %d, audio %d\n",
599 high_bus_count, med_bus_count, audio_bus_count);
604 void request_bus_freq(enum bus_freq_mode mode)
606 mutex_lock(&bus_freq_mutex);
608 if (mode == BUS_FREQ_HIGH)
610 else if (mode == BUS_FREQ_MED)
612 else if (mode == BUS_FREQ_AUDIO)
614 else if (mode == BUS_FREQ_LOW)
617 if (busfreq_suspended || !bus_freq_scaling_initialized ||
618 !bus_freq_scaling_is_active) {
619 mutex_unlock(&bus_freq_mutex);
622 cancel_delayed_work_sync(&low_bus_freq_handler);
624 if (cpu_is_imx6dl() || cpu_is_imx6sx()) {
625 /* No support for medium setpoint on i.MX6DL and i.MX6SX. */
626 if (mode == BUS_FREQ_MED) {
628 mode = BUS_FREQ_HIGH;
632 if ((mode == BUS_FREQ_HIGH) && (!high_bus_freq_mode)) {
633 set_high_bus_freq(1);
634 mutex_unlock(&bus_freq_mutex);
638 if ((mode == BUS_FREQ_MED) && (!high_bus_freq_mode) &&
639 (!med_bus_freq_mode)) {
640 set_high_bus_freq(0);
641 mutex_unlock(&bus_freq_mutex);
644 if ((mode == BUS_FREQ_AUDIO) && (!high_bus_freq_mode) &&
645 (!med_bus_freq_mode) && (!audio_bus_freq_mode)) {
647 mutex_unlock(&bus_freq_mutex);
650 mutex_unlock(&bus_freq_mutex);
653 EXPORT_SYMBOL(request_bus_freq);
655 void release_bus_freq(enum bus_freq_mode mode)
657 mutex_lock(&bus_freq_mutex);
659 if (mode == BUS_FREQ_HIGH) {
660 if (high_bus_count == 0) {
661 dev_err(busfreq_dev, "high bus count mismatch!\n");
663 mutex_unlock(&bus_freq_mutex);
667 } else if (mode == BUS_FREQ_MED) {
668 if (med_bus_count == 0) {
669 dev_err(busfreq_dev, "med bus count mismatch!\n");
671 mutex_unlock(&bus_freq_mutex);
675 } else if (mode == BUS_FREQ_AUDIO) {
676 if (audio_bus_count == 0) {
677 dev_err(busfreq_dev, "audio bus count mismatch!\n");
679 mutex_unlock(&bus_freq_mutex);
683 } else if (mode == BUS_FREQ_LOW) {
684 if (low_bus_count == 0) {
685 dev_err(busfreq_dev, "low bus count mismatch!\n");
687 mutex_unlock(&bus_freq_mutex);
693 if (busfreq_suspended || !bus_freq_scaling_initialized ||
694 !bus_freq_scaling_is_active) {
695 mutex_unlock(&bus_freq_mutex);
699 if (cpu_is_imx6dl() || cpu_is_imx6sx()) {
700 /* No support for medium setpoint on i.MX6DL and i.MX6SX. */
701 if (mode == BUS_FREQ_MED) {
703 mode = BUS_FREQ_HIGH;
707 if ((!audio_bus_freq_mode) && (high_bus_count == 0) &&
708 (med_bus_count == 0) && (audio_bus_count != 0)) {
710 mutex_unlock(&bus_freq_mutex);
713 if ((!low_bus_freq_mode) && (high_bus_count == 0) &&
714 (med_bus_count == 0) && (audio_bus_count == 0) &&
715 (low_bus_count != 0)) {
717 mutex_unlock(&bus_freq_mutex);
720 if ((!ultra_low_bus_freq_mode) && (high_bus_count == 0) &&
721 (med_bus_count == 0) && (audio_bus_count == 0) &&
722 (low_bus_count == 0)) {
724 mutex_unlock(&bus_freq_mutex);
728 mutex_unlock(&bus_freq_mutex);
731 EXPORT_SYMBOL(release_bus_freq);
733 static struct map_desc ddr_iram_io_desc __initdata = {
734 /* .virtual and .pfn are run-time assigned */
736 .type = MT_MEMORY_RWX_NONCACHED,
739 const static char *ddr_freq_iram_match[] __initconst = {
744 static int __init imx6_dt_find_ddr_sram(unsigned long node,
745 const char *uname, int depth, void *data)
747 unsigned long ddr_iram_addr;
750 if (of_flat_dt_match(node, ddr_freq_iram_match)) {
752 prop = of_get_flat_dt_prop(node, "reg", &len);
753 if (prop == NULL || len != (sizeof(unsigned long) * 2))
755 ddr_iram_addr = be32_to_cpu(prop[0]);
756 ddr_freq_change_total_size = be32_to_cpu(prop[1]);
757 ddr_freq_change_iram_phys = ddr_iram_addr;
759 /* Make sure ddr_freq_change_iram_phys is 8 byte aligned. */
760 if ((uintptr_t)(ddr_freq_change_iram_phys) & (FNCPY_ALIGN - 1))
761 ddr_freq_change_iram_phys += FNCPY_ALIGN - ((uintptr_t)ddr_freq_change_iram_phys % (FNCPY_ALIGN));
766 void __init imx6_busfreq_map_io(void)
769 * Get the address of IRAM to be used by the ddr frequency
770 * change code from the device tree.
772 WARN_ON(of_scan_flat_dt(imx6_dt_find_ddr_sram, NULL));
774 if (ddr_freq_change_iram_phys) {
775 ddr_freq_change_iram_base = IMX_IO_P2V(ddr_freq_change_iram_phys);
776 if ((iram_tlb_phys_addr & 0xFFF00000) != (ddr_freq_change_iram_phys & 0xFFF00000)) {
777 /* We need to create a 1M page table entry. */
778 ddr_iram_io_desc.virtual = IMX_IO_P2V(ddr_freq_change_iram_phys & 0xFFF00000);
779 ddr_iram_io_desc.pfn = __phys_to_pfn(ddr_freq_change_iram_phys & 0xFFF00000);
780 iotable_init(&ddr_iram_io_desc, 1);
782 memset((void *)ddr_freq_change_iram_base, 0, ddr_freq_change_total_size);
786 static void bus_freq_daemon_handler(struct work_struct *work)
788 mutex_lock(&bus_freq_mutex);
789 if ((!low_bus_freq_mode) && (high_bus_count == 0) &&
790 (med_bus_count == 0) && (audio_bus_count == 0))
792 mutex_unlock(&bus_freq_mutex);
795 static ssize_t bus_freq_scaling_enable_show(struct device *dev,
796 struct device_attribute *attr, char *buf)
798 if (bus_freq_scaling_is_active)
799 return sprintf(buf, "Bus frequency scaling is enabled\n");
801 return sprintf(buf, "Bus frequency scaling is disabled\n");
804 static ssize_t bus_freq_scaling_enable_store(struct device *dev,
805 struct device_attribute *attr,
806 const char *buf, size_t size)
808 if (strncmp(buf, "1", 1) == 0) {
809 bus_freq_scaling_is_active = 1;
810 set_high_bus_freq(1);
812 * We set bus freq to highest at the beginning,
813 * so we use this daemon thread to make sure system
814 * can enter low bus mode if
815 * there is no high bus request pending
817 schedule_delayed_work(&bus_freq_daemon,
818 usecs_to_jiffies(5000000));
819 } else if (strncmp(buf, "0", 1) == 0) {
820 if (bus_freq_scaling_is_active)
821 set_high_bus_freq(1);
822 bus_freq_scaling_is_active = 0;
827 static int bus_freq_pm_notify(struct notifier_block *nb, unsigned long event,
830 mutex_lock(&bus_freq_mutex);
832 if (event == PM_SUSPEND_PREPARE) {
834 set_high_bus_freq(1);
835 busfreq_suspended = 1;
836 } else if (event == PM_POST_SUSPEND) {
837 busfreq_suspended = 0;
839 schedule_delayed_work(&bus_freq_daemon,
840 usecs_to_jiffies(5000000));
843 mutex_unlock(&bus_freq_mutex);
848 static int busfreq_reboot_notifier_event(struct notifier_block *this,
849 unsigned long event, void *ptr)
851 /* System is rebooting. Set the system into high_bus_freq_mode. */
852 request_bus_freq(BUS_FREQ_HIGH);
857 static struct notifier_block imx_bus_freq_pm_notifier = {
858 .notifier_call = bus_freq_pm_notify,
861 static struct notifier_block imx_busfreq_reboot_notifier = {
862 .notifier_call = busfreq_reboot_notifier_event,
866 static DEVICE_ATTR(enable, 0644, bus_freq_scaling_enable_show,
867 bus_freq_scaling_enable_store);
870 * This is the probe routine for the bus frequency driver.
872 * @param pdev The platform device structure
874 * @return The function returns 0 on success
878 static int busfreq_probe(struct platform_device *pdev)
882 busfreq_dev = &pdev->dev;
884 /* Return if no IRAM space is allocated for ddr freq change code. */
885 if (!ddr_freq_change_iram_base)
888 pll2_400 = devm_clk_get(&pdev->dev, "pll2_pfd2_396m");
889 if (IS_ERR(pll2_400)) {
890 dev_err(busfreq_dev, "%s: failed to get pll2_pfd2_396m\n",
892 return PTR_ERR(pll2_400);
895 pll2_200 = devm_clk_get(&pdev->dev, "pll2_198m");
896 if (IS_ERR(pll2_200)) {
897 dev_err(busfreq_dev, "%s: failed to get pll2_198m\n",
899 return PTR_ERR(pll2_200);
902 pll2_bus = devm_clk_get(&pdev->dev, "pll2_bus");
903 if (IS_ERR(pll2_bus)) {
904 dev_err(busfreq_dev, "%s: failed to get pll2_bus\n",
906 return PTR_ERR(pll2_bus);
909 cpu_clk = devm_clk_get(&pdev->dev, "arm");
910 if (IS_ERR(cpu_clk)) {
911 dev_err(busfreq_dev, "%s: failed to get cpu_clk\n",
913 return PTR_ERR(cpu_clk);
916 pll3 = devm_clk_get(&pdev->dev, "pll3_usb_otg");
918 dev_err(busfreq_dev, "%s: failed to get pll3_usb_otg\n",
920 return PTR_ERR(pll3);
923 periph_clk = devm_clk_get(&pdev->dev, "periph");
924 if (IS_ERR(periph_clk)) {
925 dev_err(busfreq_dev, "%s: failed to get periph\n",
927 return PTR_ERR(periph_clk);
930 periph_pre_clk = devm_clk_get(&pdev->dev, "periph_pre");
931 if (IS_ERR(periph_pre_clk)) {
932 dev_err(busfreq_dev, "%s: failed to get periph_pre\n",
934 return PTR_ERR(periph_pre_clk);
937 periph_clk2 = devm_clk_get(&pdev->dev, "periph_clk2");
938 if (IS_ERR(periph_clk2)) {
939 dev_err(busfreq_dev, "%s: failed to get periph_clk2\n",
941 return PTR_ERR(periph_clk2);
944 periph_clk2_sel = devm_clk_get(&pdev->dev, "periph_clk2_sel");
945 if (IS_ERR(periph_clk2_sel)) {
946 dev_err(busfreq_dev, "%s: failed to get periph_clk2_sel\n",
948 return PTR_ERR(periph_clk2_sel);
951 osc_clk = devm_clk_get(&pdev->dev, "osc");
952 if (IS_ERR(osc_clk)) {
953 dev_err(busfreq_dev, "%s: failed to get osc_clk\n",
955 return PTR_ERR(osc_clk);
958 if (cpu_is_imx6dl()) {
959 axi_alt_sel_clk = devm_clk_get(&pdev->dev, "axi_alt_sel");
960 if (IS_ERR(axi_alt_sel_clk)) {
961 dev_err(busfreq_dev, "%s: failed to get axi_alt_sel_clk\n",
963 return PTR_ERR(axi_alt_sel_clk);
966 axi_sel_clk = devm_clk_get(&pdev->dev, "axi_sel");
967 if (IS_ERR(axi_sel_clk)) {
968 dev_err(busfreq_dev, "%s: failed to get axi_sel_clk\n",
970 return PTR_ERR(axi_sel_clk);
973 pll3_pfd1_540m = devm_clk_get(&pdev->dev, "pll3_pfd1_540m");
974 if (IS_ERR(pll3_pfd1_540m)) {
976 "%s: failed to get pll3_pfd1_540m\n", __func__);
977 return PTR_ERR(pll3_pfd1_540m);
981 if (cpu_is_imx6sl() || cpu_is_imx6sx()) {
982 ahb_clk = devm_clk_get(&pdev->dev, "ahb");
983 if (IS_ERR(ahb_clk)) {
984 dev_err(busfreq_dev, "%s: failed to get ahb_clk\n",
986 return PTR_ERR(ahb_clk);
989 ocram_clk = devm_clk_get(&pdev->dev, "ocram");
990 if (IS_ERR(ocram_clk)) {
991 dev_err(busfreq_dev, "%s: failed to get ocram_clk\n",
993 return PTR_ERR(ocram_clk);
996 periph2_clk = devm_clk_get(&pdev->dev, "periph2");
997 if (IS_ERR(periph2_clk)) {
998 dev_err(busfreq_dev, "%s: failed to get periph2\n",
1000 return PTR_ERR(periph2_clk);
1003 periph2_pre_clk = devm_clk_get(&pdev->dev, "periph2_pre");
1004 if (IS_ERR(periph2_pre_clk)) {
1005 dev_err(busfreq_dev,
1006 "%s: failed to get periph2_pre_clk\n",
1008 return PTR_ERR(periph2_pre_clk);
1011 periph2_clk2 = devm_clk_get(&pdev->dev, "periph2_clk2");
1012 if (IS_ERR(periph2_clk2)) {
1013 dev_err(busfreq_dev,
1014 "%s: failed to get periph2_clk2\n",
1016 return PTR_ERR(periph2_clk2);
1019 periph2_clk2_sel = devm_clk_get(&pdev->dev, "periph2_clk2_sel");
1020 if (IS_ERR(periph2_clk2_sel)) {
1021 dev_err(busfreq_dev,
1022 "%s: failed to get periph2_clk2_sel\n",
1024 return PTR_ERR(periph2_clk2_sel);
1027 step_clk = devm_clk_get(&pdev->dev, "step");
1028 if (IS_ERR(step_clk)) {
1029 dev_err(busfreq_dev,
1030 "%s: failed to get step_clk\n",
1032 return PTR_ERR(step_clk);
1035 if (cpu_is_imx6sl()) {
1036 pll1 = devm_clk_get(&pdev->dev, "pll1");
1038 dev_err(busfreq_dev, "%s: failed to get pll1\n",
1040 return PTR_ERR(pll1);
1043 pll1_bypass = devm_clk_get(&pdev->dev, "pll1_bypass");
1044 if (IS_ERR(pll1_bypass)) {
1045 dev_err(busfreq_dev, "%s: failed to get pll1_bypass\n",
1047 return PTR_ERR(pll1_bypass);
1050 pll1_bypass_src = devm_clk_get(&pdev->dev, "pll1_bypass_src");
1051 if (IS_ERR(pll1_bypass_src)) {
1052 dev_err(busfreq_dev, "%s: failed to get pll1_bypass_src\n",
1054 return PTR_ERR(pll1_bypass_src);
1057 pll1_sys = devm_clk_get(&pdev->dev, "pll1_sys");
1058 if (IS_ERR(pll1_sys)) {
1059 dev_err(busfreq_dev, "%s: failed to get pll1_sys\n",
1061 return PTR_ERR(pll1_sys);
1064 pll1_sw_clk = devm_clk_get(&pdev->dev, "pll1_sw");
1065 if (IS_ERR(pll1_sw_clk)) {
1066 dev_err(busfreq_dev, "%s: failed to get pll1_sw_clk\n",
1068 return PTR_ERR(pll1_sw_clk);
1071 pll2_bypass_src = devm_clk_get(&pdev->dev, "pll2_bypass_src");
1072 if (IS_ERR(pll2_bypass_src)) {
1073 dev_err(busfreq_dev, "%s: failed to get pll2_bypass_src\n",
1075 return PTR_ERR(pll2_bypass_src);
1078 pll2 = devm_clk_get(&pdev->dev, "pll2");
1080 dev_err(busfreq_dev, "%s: failed to get pll2\n",
1082 return PTR_ERR(pll2);
1085 pll2_bypass = devm_clk_get(&pdev->dev, "pll2_bypass");
1086 if (IS_ERR(pll2_bypass)) {
1087 dev_err(busfreq_dev, "%s: failed to get pll2_bypass\n",
1089 return PTR_ERR(pll2_bypass);
1092 if (cpu_is_imx6sx()) {
1093 mmdc_clk = devm_clk_get(&pdev->dev, "mmdc");
1094 if (IS_ERR(mmdc_clk)) {
1095 dev_err(busfreq_dev,
1096 "%s: failed to get mmdc_clk\n",
1098 return PTR_ERR(mmdc_clk);
1100 m4_clk = devm_clk_get(&pdev->dev, "m4");
1101 if (IS_ERR(m4_clk)) {
1102 dev_err(busfreq_dev,
1103 "%s: failed to get m4_clk\n",
1105 return PTR_ERR(m4_clk);
1109 err = sysfs_create_file(&busfreq_dev->kobj, &dev_attr_enable.attr);
1111 dev_err(busfreq_dev,
1112 "Unable to register sysdev entry for BUSFREQ");
1116 if (of_property_read_u32(pdev->dev.of_node, "fsl,max_ddr_freq",
1117 &ddr_normal_rate)) {
1118 dev_err(busfreq_dev, "max_ddr_freq entry missing\n");
1122 high_bus_freq_mode = 1;
1123 med_bus_freq_mode = 0;
1124 low_bus_freq_mode = 0;
1125 audio_bus_freq_mode = 0;
1126 ultra_low_bus_freq_mode = 0;
1128 bus_freq_scaling_is_active = 1;
1129 bus_freq_scaling_initialized = 1;
1131 ddr_low_rate = LPAPM_CLK;
1132 if (cpu_is_imx6q()) {
1133 if (of_property_read_u32(pdev->dev.of_node, "fsl,med_ddr_freq",
1135 dev_info(busfreq_dev,
1136 "DDR medium rate not supported.\n");
1137 ddr_med_rate = ddr_normal_rate;
1141 INIT_DELAYED_WORK(&low_bus_freq_handler, reduce_bus_freq_handler);
1142 INIT_DELAYED_WORK(&bus_freq_daemon, bus_freq_daemon_handler);
1143 register_pm_notifier(&imx_bus_freq_pm_notifier);
1144 register_reboot_notifier(&imx_busfreq_reboot_notifier);
1147 * Need to make sure to an entry for the ddr freq change code address in the IRAM page table.
1148 * This is only required if the DDR freq code and suspend/idle code are in different OCRAM spaces.
1150 if ((iram_tlb_phys_addr & 0xFFF00000) != (ddr_freq_change_iram_phys & 0xFFF00000)) {
1154 * Make sure the ddr_iram virtual address has a mapping
1155 * in the IRAM page table.
1157 i = ((IMX_IO_P2V(ddr_freq_change_iram_phys) >> 20) << 2) / 4;
1158 *((unsigned long *)iram_tlb_base_addr + i) =
1159 (ddr_freq_change_iram_phys & 0xFFF00000) | TT_ATTRIB_NON_CACHEABLE_1M;
1162 if (cpu_is_imx6sl()) {
1163 err = init_mmdc_lpddr2_settings(pdev);
1164 } else if (cpu_is_imx6sx()) {
1165 ddr_type = imx_mmdc_get_ddr_type();
1166 /* check whether it is a DDR3 or LPDDR2 board */
1167 if (ddr_type == MMDC_MDMISC_DDR_TYPE_DDR3)
1168 err = init_mmdc_ddr3_settings_imx6sx(pdev);
1169 else if (ddr_type == MMDC_MDMISC_DDR_TYPE_LPDDR2)
1170 err = init_mmdc_lpddr2_settings(pdev);
1171 /* if M4 is enabled and rate > 24MHz, add high bus count */
1172 if (imx_src_is_m4_enabled() &&
1173 (clk_get_rate(m4_clk) > LPAPM_CLK))
1176 err = init_mmdc_ddr3_settings_imx6q(pdev);
1180 dev_err(busfreq_dev, "Busfreq init of MMDC failed\n");
1186 static const struct of_device_id imx6_busfreq_ids[] = {
1187 { .compatible = "fsl,imx6_busfreq", },
1191 static struct platform_driver busfreq_driver = {
1193 .name = "imx6_busfreq",
1194 .owner = THIS_MODULE,
1195 .of_match_table = imx6_busfreq_ids,
1197 .probe = busfreq_probe,
1201 * Initialise the busfreq_driver.
1203 * @return The function always returns 0.
1206 static int __init busfreq_init(void)
1208 #ifndef CONFIG_MX6_VPU_352M
1209 if (platform_driver_register(&busfreq_driver) != 0)
1212 printk(KERN_INFO "Bus freq driver module loaded\n");
1217 static void __exit busfreq_cleanup(void)
1219 sysfs_remove_file(&busfreq_dev->kobj, &dev_attr_enable.attr);
1221 /* Unregister the device structure */
1222 platform_driver_unregister(&busfreq_driver);
1223 bus_freq_scaling_initialized = 0;
1226 module_init(busfreq_init);
1227 module_exit(busfreq_cleanup);
1229 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1230 MODULE_DESCRIPTION("BusFreq driver");
1231 MODULE_LICENSE("GPL");