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;
121 static u32 pll2_org_rate;
122 static struct delayed_work low_bus_freq_handler;
123 static struct delayed_work bus_freq_daemon;
125 static RAW_NOTIFIER_HEAD(busfreq_notifier_chain);
127 static int busfreq_notify(enum busfreq_event event)
131 ret = raw_notifier_call_chain(&busfreq_notifier_chain, event, NULL);
133 return notifier_to_errno(ret);
136 int register_busfreq_notifier(struct notifier_block *nb)
138 return raw_notifier_chain_register(&busfreq_notifier_chain, nb);
140 EXPORT_SYMBOL(register_busfreq_notifier);
142 int unregister_busfreq_notifier(struct notifier_block *nb)
144 return raw_notifier_chain_unregister(&busfreq_notifier_chain, nb);
146 EXPORT_SYMBOL(unregister_busfreq_notifier);
148 static bool check_m4_sleep(void)
150 unsigned long timeout = jiffies + msecs_to_jiffies(500);
152 while (imx_gpc_is_m4_sleeping() == 0)
153 if (time_after(jiffies, timeout))
158 static void enter_lpm_imx6sx(void)
160 if (imx_src_is_m4_enabled())
161 if (!check_m4_sleep())
162 pr_err("M4 is NOT in sleep!!!\n");
164 /* set periph_clk2 to source from OSC for periph */
165 imx_clk_set_parent(periph_clk2_sel, osc_clk);
166 imx_clk_set_parent(periph_clk, periph_clk2);
167 /* set ahb/ocram to 24MHz */
168 imx_clk_set_rate(ahb_clk, LPAPM_CLK);
169 imx_clk_set_rate(ocram_clk, LPAPM_CLK);
171 if (audio_bus_count) {
172 /* Need to ensure that PLL2_PFD_400M is kept ON. */
173 clk_prepare_enable(pll2_400);
174 if (ddr_type == MMDC_MDMISC_DDR_TYPE_DDR3)
175 update_ddr_freq_imx6sx(DDR3_AUDIO_CLK);
176 else if (ddr_type == MMDC_MDMISC_DDR_TYPE_LPDDR2)
177 update_lpddr2_freq(LPDDR2_AUDIO_CLK);
178 imx_clk_set_parent(periph2_clk2_sel, pll3);
179 imx_clk_set_parent(periph2_pre_clk, pll2_400);
180 imx_clk_set_parent(periph2_clk, periph2_pre_clk);
182 * As periph2_clk's parent is not changed from
183 * high mode to audio mode, so clk framework
184 * will not update its children's freq, but we
185 * change the mmdc's podf in asm code, so here
186 * need to update mmdc rate to make sure clk
187 * tree is right, although it will not do any
188 * change to hardware.
190 if (high_bus_freq_mode) {
191 if (ddr_type == MMDC_MDMISC_DDR_TYPE_DDR3)
192 imx_clk_set_rate(mmdc_clk, DDR3_AUDIO_CLK);
193 else if (ddr_type == MMDC_MDMISC_DDR_TYPE_LPDDR2)
194 imx_clk_set_rate(mmdc_clk, LPDDR2_AUDIO_CLK);
196 audio_bus_freq_mode = 1;
197 low_bus_freq_mode = 0;
199 if (ddr_type == MMDC_MDMISC_DDR_TYPE_DDR3)
200 update_ddr_freq_imx6sx(LPAPM_CLK);
201 else if (ddr_type == MMDC_MDMISC_DDR_TYPE_LPDDR2)
202 update_lpddr2_freq(LPAPM_CLK);
203 imx_clk_set_parent(periph2_clk2_sel, osc_clk);
204 imx_clk_set_parent(periph2_clk, periph2_clk2);
206 if (audio_bus_freq_mode)
207 clk_disable_unprepare(pll2_400);
208 low_bus_freq_mode = 1;
209 audio_bus_freq_mode = 0;
213 static void exit_lpm_imx6sx(void)
215 clk_prepare_enable(pll2_400);
218 * lower ahb/ocram's freq first to avoid too high
219 * freq during parent switch from OSC to pll3.
221 imx_clk_set_rate(ahb_clk, LPAPM_CLK / 3);
222 imx_clk_set_rate(ocram_clk, LPAPM_CLK / 2);
223 /* set periph_clk2 to pll3 */
224 imx_clk_set_parent(periph_clk2_sel, pll3);
225 /* set periph clk to from pll2_400 */
226 imx_clk_set_parent(periph_pre_clk, pll2_400);
227 imx_clk_set_parent(periph_clk, periph_pre_clk);
229 if (ddr_type == MMDC_MDMISC_DDR_TYPE_DDR3)
230 update_ddr_freq_imx6sx(ddr_normal_rate);
231 else if (ddr_type == MMDC_MDMISC_DDR_TYPE_LPDDR2)
232 update_lpddr2_freq(ddr_normal_rate);
233 /* correct parent info after ddr freq change in asm code */
234 imx_clk_set_parent(periph2_clk2_sel, pll3);
235 imx_clk_set_parent(periph2_pre_clk, pll2_400);
236 imx_clk_set_parent(periph2_clk, periph2_pre_clk);
238 * As periph2_clk's parent is not changed from
239 * audio mode to high mode, so clk framework
240 * will not update its children's freq, but we
241 * change the mmdc's podf in asm code, so here
242 * need to update mmdc rate to make sure clk
243 * tree is right, although it will not do any
244 * change to hardware.
246 if (audio_bus_freq_mode)
247 imx_clk_set_rate(mmdc_clk, ddr_normal_rate);
249 clk_disable_unprepare(pll2_400);
250 if (audio_bus_freq_mode)
251 clk_disable_unprepare(pll2_400);
254 static void enter_lpm_imx6sl(void)
256 if (high_bus_freq_mode) {
257 pll2_org_rate = clk_get_rate(pll2_bus);
258 /* Set periph_clk to be sourced from OSC_CLK */
259 imx_clk_set_parent(periph_clk2_sel, osc_clk);
260 imx_clk_set_parent(periph_clk, periph_clk2);
261 /* Ensure AHB/AXI clks are at 24MHz. */
262 imx_clk_set_rate(ahb_clk, LPAPM_CLK);
263 imx_clk_set_rate(ocram_clk, LPAPM_CLK);
265 if (audio_bus_count) {
266 /* Set AHB to 8MHz to lower pwer.*/
267 imx_clk_set_rate(ahb_clk, LPAPM_CLK / 3);
269 /* Set up DDR to 100MHz. */
270 update_lpddr2_freq(LPDDR2_AUDIO_CLK);
272 /* Fix the clock tree in kernel */
273 imx_clk_set_parent(periph2_pre_clk, pll2_200);
274 imx_clk_set_parent(periph2_clk, periph2_pre_clk);
276 if (low_bus_freq_mode || ultra_low_bus_freq_mode) {
278 * Fix the clock tree in kernel, make sure
279 * pll2_bypass is updated as it is
282 imx_clk_set_parent(pll2_bypass, pll2);
284 * Swtich ARM to run off PLL2_PFD2_400MHz
285 * since DDR is anyway at 100MHz.
287 imx_clk_set_parent(step_clk, pll2_400);
288 imx_clk_set_parent(pll1_sw_clk, step_clk);
290 * Ensure that the clock will be
293 imx_clk_set_rate(cpu_clk, org_arm_rate);
295 low_bus_freq_mode = 0;
296 ultra_low_bus_freq_mode = 0;
297 audio_bus_freq_mode = 1;
299 u32 arm_div, pll1_rate;
300 org_arm_rate = clk_get_rate(cpu_clk);
301 if (low_bus_freq_mode && low_bus_count == 0) {
303 * We are already in DDR @ 24MHz state, but
304 * no one but ARM needs the DDR. In this case,
305 * we can lower the DDR freq to 1MHz when ARM
306 * enters WFI in this state. Keep track of this state.
308 ultra_low_bus_freq_mode = 1;
309 low_bus_freq_mode = 0;
310 audio_bus_freq_mode = 0;
312 if (!ultra_low_bus_freq_mode && !low_bus_freq_mode) {
314 * Anyway, make sure the AHB is running at 24MHz
315 * in low_bus_freq_mode.
317 if (audio_bus_freq_mode)
318 imx_clk_set_rate(ahb_clk, LPAPM_CLK);
321 * Since we are going to bypass PLL2,
322 * we need to move ARM clk off PLL2_PFD2
323 * to PLL1. Make sure the PLL1 is running
324 * at the lowest possible freq.
325 * To work well with CPUFREQ we want to ensure that
326 * the CPU freq does not change, so attempt to
327 * get a freq as close to 396MHz as possible.
329 imx_clk_set_rate(pll1_sys,
330 clk_round_rate(pll1_sys, (org_arm_rate * 2)));
331 pll1_rate = clk_get_rate(pll1_sys);
332 arm_div = pll1_rate / org_arm_rate;
333 if (pll1_rate / arm_div > org_arm_rate)
336 * Ensure ARM CLK is lower before
337 * changing the parent.
339 imx_clk_set_rate(cpu_clk, org_arm_rate / arm_div);
340 /* Now set the ARM clk parent to PLL1_SYS. */
341 imx_clk_set_parent(pll1_sw_clk, pll1_sys);
344 * Set STEP_CLK back to OSC to save power and
345 * also to maintain the parent.The WFI iram code
346 * will switch step_clk to osc, but the clock API
347 * is not aware of the change and when a new request
348 * to change the step_clk parent to pll2_pfd2_400M
349 * is requested sometime later, the change is ignored.
351 imx_clk_set_parent(step_clk, osc_clk);
352 /* Now set DDR to 24MHz. */
353 update_lpddr2_freq(LPAPM_CLK);
356 * Fix the clock tree in kernel.
357 * Make sure PLL2 rate is updated as it gets
358 * bypassed in the DDR freq change code.
360 imx_clk_set_parent(pll2_bypass, pll2_bypass_src);
361 imx_clk_set_parent(periph2_clk2_sel, pll2_bus);
362 imx_clk_set_parent(periph2_clk, periph2_clk2);
365 if (low_bus_count == 0) {
366 ultra_low_bus_freq_mode = 1;
367 low_bus_freq_mode = 0;
369 ultra_low_bus_freq_mode = 0;
370 low_bus_freq_mode = 1;
372 audio_bus_freq_mode = 0;
377 static void exit_lpm_imx6sl(void)
379 /* Change DDR freq in IRAM. */
380 update_lpddr2_freq(ddr_normal_rate);
383 * Fix the clock tree in kernel.
384 * Make sure PLL2 rate is updated as it gets
385 * un-bypassed in the DDR freq change code.
387 imx_clk_set_parent(pll2_bypass, pll2);
388 imx_clk_set_parent(periph2_pre_clk, pll2_400);
389 imx_clk_set_parent(periph2_clk, periph2_pre_clk);
391 /* Ensure that periph_clk is sourced from PLL2_400. */
392 imx_clk_set_parent(periph_pre_clk, pll2_400);
394 * Before switching the perhiph_clk, ensure that the
395 * AHB/AXI will not be too fast.
397 imx_clk_set_rate(ahb_clk, LPAPM_CLK / 3);
398 imx_clk_set_rate(ocram_clk, LPAPM_CLK / 2);
399 imx_clk_set_parent(periph_clk, periph_pre_clk);
401 if (low_bus_freq_mode || ultra_low_bus_freq_mode) {
402 /* Move ARM from PLL1_SW_CLK to PLL2_400. */
403 imx_clk_set_parent(step_clk, pll2_400);
404 imx_clk_set_parent(pll1_sw_clk, step_clk);
405 imx_clk_set_rate(cpu_clk, org_arm_rate);
406 ultra_low_bus_freq_mode = 0;
410 static void reduce_bus_freq(void)
412 clk_prepare_enable(pll3);
413 if (audio_bus_count && (low_bus_freq_mode || ultra_low_bus_freq_mode))
414 busfreq_notify(LOW_BUSFREQ_EXIT);
415 else if (!audio_bus_count)
416 busfreq_notify(LOW_BUSFREQ_ENTER);
419 else if (cpu_is_imx6sx())
423 /* Set axi to periph_clk */
424 imx_clk_set_parent(axi_sel_clk, periph_clk);
426 if (audio_bus_count) {
427 /* Need to ensure that PLL2_PFD_400M is kept ON. */
428 clk_prepare_enable(pll2_400);
429 update_ddr_freq_imx6q(DDR3_AUDIO_CLK);
430 /* Make sure periph clk's parent also got updated */
431 imx_clk_set_parent(periph_clk2_sel, pll3);
432 imx_clk_set_parent(periph_pre_clk, pll2_200);
433 imx_clk_set_parent(periph_clk, periph_pre_clk);
434 audio_bus_freq_mode = 1;
435 low_bus_freq_mode = 0;
437 update_ddr_freq_imx6q(LPAPM_CLK);
438 /* Make sure periph clk's parent also got updated */
439 imx_clk_set_parent(periph_clk2_sel, osc_clk);
440 /* Set periph_clk parent to OSC via periph_clk2_sel */
441 imx_clk_set_parent(periph_clk, periph_clk2);
442 if (audio_bus_freq_mode)
443 clk_disable_unprepare(pll2_400);
444 low_bus_freq_mode = 1;
445 audio_bus_freq_mode = 0;
448 clk_disable_unprepare(pll3);
450 med_bus_freq_mode = 0;
451 high_bus_freq_mode = 0;
453 if (audio_bus_freq_mode)
454 dev_dbg(busfreq_dev, "Bus freq set to audio mode. Count:\
455 high %d, med %d, audio %d\n",
456 high_bus_count, med_bus_count, audio_bus_count);
457 if (low_bus_freq_mode)
458 dev_dbg(busfreq_dev, "Bus freq set to low mode. Count:\
459 high %d, med %d, audio %d\n",
460 high_bus_count, med_bus_count, audio_bus_count);
463 static void reduce_bus_freq_handler(struct work_struct *work)
465 mutex_lock(&bus_freq_mutex);
469 mutex_unlock(&bus_freq_mutex);
473 * Set the DDR, AHB to 24MHz.
474 * This mode will be activated only when none of the modules that
475 * need a higher DDR or AHB frequency are active.
477 int set_low_bus_freq(void)
479 if (busfreq_suspended)
482 if (!bus_freq_scaling_initialized || !bus_freq_scaling_is_active)
486 * Check to see if we need to got from
487 * low bus freq mode to audio bus freq mode.
488 * If so, the change needs to be done immediately.
490 if (audio_bus_count && (low_bus_freq_mode || ultra_low_bus_freq_mode))
494 * Don't lower the frequency immediately. Instead
495 * scheduled a delayed work and drop the freq if
496 * the conditions still remain the same.
498 schedule_delayed_work(&low_bus_freq_handler,
499 usecs_to_jiffies(3000000));
504 * Set the DDR to either 528MHz or 400MHz for iMX6qd
505 * or 400MHz for iMX6dl.
507 static int set_high_bus_freq(int high_bus_freq)
509 struct clk *periph_clk_parent;
511 if (bus_freq_scaling_initialized && bus_freq_scaling_is_active)
512 cancel_delayed_work_sync(&low_bus_freq_handler);
514 if (busfreq_suspended)
518 periph_clk_parent = pll2_bus;
520 periph_clk_parent = pll2_400;
522 if (!bus_freq_scaling_initialized || !bus_freq_scaling_is_active)
525 if (high_bus_freq_mode)
528 /* medium bus freq is only supported for MX6DQ */
529 if (med_bus_freq_mode && !high_bus_freq)
532 if (low_bus_freq_mode || ultra_low_bus_freq_mode)
533 busfreq_notify(LOW_BUSFREQ_EXIT);
535 clk_prepare_enable(pll3);
538 else if (cpu_is_imx6sx())
542 clk_prepare_enable(pll2_400);
543 update_ddr_freq_imx6q(ddr_normal_rate);
544 /* Make sure periph clk's parent also got updated */
545 imx_clk_set_parent(periph_clk2_sel, pll3);
546 imx_clk_set_parent(periph_pre_clk, periph_clk_parent);
547 imx_clk_set_parent(periph_clk, periph_pre_clk);
548 if (cpu_is_imx6dl()) {
549 /* Set axi to pll3_pfd1_540m */
550 imx_clk_set_parent(axi_alt_sel_clk, pll3_pfd1_540m);
551 imx_clk_set_parent(axi_sel_clk, axi_alt_sel_clk);
553 clk_disable_unprepare(pll2_400);
555 update_ddr_freq_imx6q(ddr_med_rate);
556 /* Make sure periph clk's parent also got updated */
557 imx_clk_set_parent(periph_clk2_sel, pll3);
558 imx_clk_set_parent(periph_pre_clk, pll2_400);
559 imx_clk_set_parent(periph_clk, periph_pre_clk);
561 if (audio_bus_freq_mode)
562 clk_disable_unprepare(pll2_400);
565 high_bus_freq_mode = 1;
566 med_bus_freq_mode = 0;
567 low_bus_freq_mode = 0;
568 audio_bus_freq_mode = 0;
570 clk_disable_unprepare(pll3);
571 if (high_bus_freq_mode)
572 dev_dbg(busfreq_dev, "Bus freq set to high mode. Count:\
573 high %d, med %d, audio %d\n",
574 high_bus_count, med_bus_count, audio_bus_count);
575 if (med_bus_freq_mode)
576 dev_dbg(busfreq_dev, "Bus freq set to med mode. Count:\
577 high %d, med %d, audio %d\n",
578 high_bus_count, med_bus_count, audio_bus_count);
583 void request_bus_freq(enum bus_freq_mode mode)
585 mutex_lock(&bus_freq_mutex);
587 if (mode == BUS_FREQ_HIGH)
589 else if (mode == BUS_FREQ_MED)
591 else if (mode == BUS_FREQ_AUDIO)
593 else if (mode == BUS_FREQ_LOW)
596 if (busfreq_suspended || !bus_freq_scaling_initialized ||
597 !bus_freq_scaling_is_active) {
598 mutex_unlock(&bus_freq_mutex);
601 cancel_delayed_work_sync(&low_bus_freq_handler);
603 if (cpu_is_imx6dl() || cpu_is_imx6sx()) {
604 /* No support for medium setpoint on i.MX6DL and i.MX6SX. */
605 if (mode == BUS_FREQ_MED) {
607 mode = BUS_FREQ_HIGH;
611 if ((mode == BUS_FREQ_HIGH) && (!high_bus_freq_mode)) {
612 set_high_bus_freq(1);
613 mutex_unlock(&bus_freq_mutex);
617 if ((mode == BUS_FREQ_MED) && (!high_bus_freq_mode) &&
618 (!med_bus_freq_mode)) {
619 set_high_bus_freq(0);
620 mutex_unlock(&bus_freq_mutex);
623 if ((mode == BUS_FREQ_AUDIO) && (!high_bus_freq_mode) &&
624 (!med_bus_freq_mode) && (!audio_bus_freq_mode)) {
626 mutex_unlock(&bus_freq_mutex);
629 mutex_unlock(&bus_freq_mutex);
632 EXPORT_SYMBOL(request_bus_freq);
634 void release_bus_freq(enum bus_freq_mode mode)
636 mutex_lock(&bus_freq_mutex);
638 if (mode == BUS_FREQ_HIGH) {
639 if (high_bus_count == 0) {
640 dev_err(busfreq_dev, "high bus count mismatch!\n");
642 mutex_unlock(&bus_freq_mutex);
646 } else if (mode == BUS_FREQ_MED) {
647 if (med_bus_count == 0) {
648 dev_err(busfreq_dev, "med bus count mismatch!\n");
650 mutex_unlock(&bus_freq_mutex);
654 } else if (mode == BUS_FREQ_AUDIO) {
655 if (audio_bus_count == 0) {
656 dev_err(busfreq_dev, "audio bus count mismatch!\n");
658 mutex_unlock(&bus_freq_mutex);
662 } else if (mode == BUS_FREQ_LOW) {
663 if (low_bus_count == 0) {
664 dev_err(busfreq_dev, "low bus count mismatch!\n");
666 mutex_unlock(&bus_freq_mutex);
672 if (busfreq_suspended || !bus_freq_scaling_initialized ||
673 !bus_freq_scaling_is_active) {
674 mutex_unlock(&bus_freq_mutex);
678 if (cpu_is_imx6dl() || cpu_is_imx6sx()) {
679 /* No support for medium setpoint on i.MX6DL and i.MX6SX. */
680 if (mode == BUS_FREQ_MED) {
682 mode = BUS_FREQ_HIGH;
686 if ((!audio_bus_freq_mode) && (high_bus_count == 0) &&
687 (med_bus_count == 0) && (audio_bus_count != 0)) {
689 mutex_unlock(&bus_freq_mutex);
692 if ((!low_bus_freq_mode) && (high_bus_count == 0) &&
693 (med_bus_count == 0) && (audio_bus_count == 0) &&
694 (low_bus_count != 0)) {
696 mutex_unlock(&bus_freq_mutex);
699 if ((!ultra_low_bus_freq_mode) && (high_bus_count == 0) &&
700 (med_bus_count == 0) && (audio_bus_count == 0) &&
701 (low_bus_count == 0)) {
703 mutex_unlock(&bus_freq_mutex);
707 mutex_unlock(&bus_freq_mutex);
710 EXPORT_SYMBOL(release_bus_freq);
712 static struct map_desc ddr_iram_io_desc __initdata = {
713 /* .virtual and .pfn are run-time assigned */
715 .type = MT_MEMORY_RWX_NONCACHED,
718 const static char *ddr_freq_iram_match[] __initconst = {
723 static int __init imx6_dt_find_ddr_sram(unsigned long node,
724 const char *uname, int depth, void *data)
726 unsigned long ddr_iram_addr;
729 if (of_flat_dt_match(node, ddr_freq_iram_match)) {
731 prop = of_get_flat_dt_prop(node, "reg", &len);
732 if (prop == NULL || len != (sizeof(unsigned long) * 2))
734 ddr_iram_addr = be32_to_cpu(prop[0]);
735 ddr_freq_change_total_size = be32_to_cpu(prop[1]);
736 ddr_freq_change_iram_phys = ddr_iram_addr;
738 /* Make sure ddr_freq_change_iram_phys is 8 byte aligned. */
739 if ((uintptr_t)(ddr_freq_change_iram_phys) & (FNCPY_ALIGN - 1))
740 ddr_freq_change_iram_phys += FNCPY_ALIGN - ((uintptr_t)ddr_freq_change_iram_phys % (FNCPY_ALIGN));
745 void __init imx6_busfreq_map_io(void)
748 * Get the address of IRAM to be used by the ddr frequency
749 * change code from the device tree.
751 WARN_ON(of_scan_flat_dt(imx6_dt_find_ddr_sram, NULL));
753 if (ddr_freq_change_iram_phys) {
754 ddr_freq_change_iram_base = IMX_IO_P2V(ddr_freq_change_iram_phys);
755 if ((iram_tlb_phys_addr & 0xFFF00000) != (ddr_freq_change_iram_phys & 0xFFF00000)) {
756 /* We need to create a 1M page table entry. */
757 ddr_iram_io_desc.virtual = IMX_IO_P2V(ddr_freq_change_iram_phys & 0xFFF00000);
758 ddr_iram_io_desc.pfn = __phys_to_pfn(ddr_freq_change_iram_phys & 0xFFF00000);
759 iotable_init(&ddr_iram_io_desc, 1);
761 memset((void *)ddr_freq_change_iram_base, 0, ddr_freq_change_total_size);
765 static void bus_freq_daemon_handler(struct work_struct *work)
767 mutex_lock(&bus_freq_mutex);
768 if ((!low_bus_freq_mode) && (high_bus_count == 0) &&
769 (med_bus_count == 0) && (audio_bus_count == 0))
771 mutex_unlock(&bus_freq_mutex);
774 static ssize_t bus_freq_scaling_enable_show(struct device *dev,
775 struct device_attribute *attr, char *buf)
777 if (bus_freq_scaling_is_active)
778 return sprintf(buf, "Bus frequency scaling is enabled\n");
780 return sprintf(buf, "Bus frequency scaling is disabled\n");
783 static ssize_t bus_freq_scaling_enable_store(struct device *dev,
784 struct device_attribute *attr,
785 const char *buf, size_t size)
787 if (strncmp(buf, "1", 1) == 0) {
788 bus_freq_scaling_is_active = 1;
789 set_high_bus_freq(1);
791 * We set bus freq to highest at the beginning,
792 * so we use this daemon thread to make sure system
793 * can enter low bus mode if
794 * there is no high bus request pending
796 schedule_delayed_work(&bus_freq_daemon,
797 usecs_to_jiffies(5000000));
798 } else if (strncmp(buf, "0", 1) == 0) {
799 if (bus_freq_scaling_is_active)
800 set_high_bus_freq(1);
801 bus_freq_scaling_is_active = 0;
806 static int bus_freq_pm_notify(struct notifier_block *nb, unsigned long event,
809 mutex_lock(&bus_freq_mutex);
811 if (event == PM_SUSPEND_PREPARE) {
813 set_high_bus_freq(1);
814 busfreq_suspended = 1;
815 } else if (event == PM_POST_SUSPEND) {
816 busfreq_suspended = 0;
818 schedule_delayed_work(&bus_freq_daemon,
819 usecs_to_jiffies(5000000));
822 mutex_unlock(&bus_freq_mutex);
827 static int busfreq_reboot_notifier_event(struct notifier_block *this,
828 unsigned long event, void *ptr)
830 /* System is rebooting. Set the system into high_bus_freq_mode. */
831 request_bus_freq(BUS_FREQ_HIGH);
836 static struct notifier_block imx_bus_freq_pm_notifier = {
837 .notifier_call = bus_freq_pm_notify,
840 static struct notifier_block imx_busfreq_reboot_notifier = {
841 .notifier_call = busfreq_reboot_notifier_event,
845 static DEVICE_ATTR(enable, 0644, bus_freq_scaling_enable_show,
846 bus_freq_scaling_enable_store);
849 * This is the probe routine for the bus frequency driver.
851 * @param pdev The platform device structure
853 * @return The function returns 0 on success
857 static int busfreq_probe(struct platform_device *pdev)
861 busfreq_dev = &pdev->dev;
863 /* Return if no IRAM space is allocated for ddr freq change code. */
864 if (!ddr_freq_change_iram_base)
867 pll2_400 = devm_clk_get(&pdev->dev, "pll2_pfd2_396m");
868 if (IS_ERR(pll2_400)) {
869 dev_err(busfreq_dev, "%s: failed to get pll2_pfd2_396m\n",
871 return PTR_ERR(pll2_400);
874 pll2_200 = devm_clk_get(&pdev->dev, "pll2_198m");
875 if (IS_ERR(pll2_200)) {
876 dev_err(busfreq_dev, "%s: failed to get pll2_198m\n",
878 return PTR_ERR(pll2_200);
881 pll2_bus = devm_clk_get(&pdev->dev, "pll2_bus");
882 if (IS_ERR(pll2_bus)) {
883 dev_err(busfreq_dev, "%s: failed to get pll2_bus\n",
885 return PTR_ERR(pll2_bus);
888 cpu_clk = devm_clk_get(&pdev->dev, "arm");
889 if (IS_ERR(cpu_clk)) {
890 dev_err(busfreq_dev, "%s: failed to get cpu_clk\n",
892 return PTR_ERR(cpu_clk);
895 pll3 = devm_clk_get(&pdev->dev, "pll3_usb_otg");
897 dev_err(busfreq_dev, "%s: failed to get pll3_usb_otg\n",
899 return PTR_ERR(pll3);
902 periph_clk = devm_clk_get(&pdev->dev, "periph");
903 if (IS_ERR(periph_clk)) {
904 dev_err(busfreq_dev, "%s: failed to get periph\n",
906 return PTR_ERR(periph_clk);
909 periph_pre_clk = devm_clk_get(&pdev->dev, "periph_pre");
910 if (IS_ERR(periph_pre_clk)) {
911 dev_err(busfreq_dev, "%s: failed to get periph_pre\n",
913 return PTR_ERR(periph_pre_clk);
916 periph_clk2 = devm_clk_get(&pdev->dev, "periph_clk2");
917 if (IS_ERR(periph_clk2)) {
918 dev_err(busfreq_dev, "%s: failed to get periph_clk2\n",
920 return PTR_ERR(periph_clk2);
923 periph_clk2_sel = devm_clk_get(&pdev->dev, "periph_clk2_sel");
924 if (IS_ERR(periph_clk2_sel)) {
925 dev_err(busfreq_dev, "%s: failed to get periph_clk2_sel\n",
927 return PTR_ERR(periph_clk2_sel);
930 osc_clk = devm_clk_get(&pdev->dev, "osc");
931 if (IS_ERR(osc_clk)) {
932 dev_err(busfreq_dev, "%s: failed to get osc_clk\n",
934 return PTR_ERR(osc_clk);
937 if (cpu_is_imx6dl()) {
938 axi_alt_sel_clk = devm_clk_get(&pdev->dev, "axi_alt_sel");
939 if (IS_ERR(axi_alt_sel_clk)) {
940 dev_err(busfreq_dev, "%s: failed to get axi_alt_sel_clk\n",
942 return PTR_ERR(axi_alt_sel_clk);
945 axi_sel_clk = devm_clk_get(&pdev->dev, "axi_sel");
946 if (IS_ERR(axi_sel_clk)) {
947 dev_err(busfreq_dev, "%s: failed to get axi_sel_clk\n",
949 return PTR_ERR(axi_sel_clk);
952 pll3_pfd1_540m = devm_clk_get(&pdev->dev, "pll3_pfd1_540m");
953 if (IS_ERR(pll3_pfd1_540m)) {
955 "%s: failed to get pll3_pfd1_540m\n", __func__);
956 return PTR_ERR(pll3_pfd1_540m);
960 if (cpu_is_imx6sl() || cpu_is_imx6sx()) {
961 pll1_sys = devm_clk_get(&pdev->dev, "pll1_sys");
962 if (IS_ERR(pll1_sys)) {
963 dev_err(busfreq_dev, "%s: failed to get pll1_sys\n",
965 return PTR_ERR(pll1_sys);
968 ahb_clk = devm_clk_get(&pdev->dev, "ahb");
969 if (IS_ERR(ahb_clk)) {
970 dev_err(busfreq_dev, "%s: failed to get ahb_clk\n",
972 return PTR_ERR(ahb_clk);
975 ocram_clk = devm_clk_get(&pdev->dev, "ocram");
976 if (IS_ERR(ocram_clk)) {
977 dev_err(busfreq_dev, "%s: failed to get ocram_clk\n",
979 return PTR_ERR(ocram_clk);
982 pll1_sw_clk = devm_clk_get(&pdev->dev, "pll1_sw");
983 if (IS_ERR(pll1_sw_clk)) {
984 dev_err(busfreq_dev, "%s: failed to get pll1_sw_clk\n",
986 return PTR_ERR(pll1_sw_clk);
989 periph2_clk = devm_clk_get(&pdev->dev, "periph2");
990 if (IS_ERR(periph2_clk)) {
991 dev_err(busfreq_dev, "%s: failed to get periph2\n",
993 return PTR_ERR(periph2_clk);
996 periph2_pre_clk = devm_clk_get(&pdev->dev, "periph2_pre");
997 if (IS_ERR(periph2_pre_clk)) {
999 "%s: failed to get periph2_pre_clk\n",
1001 return PTR_ERR(periph2_pre_clk);
1004 periph2_clk2 = devm_clk_get(&pdev->dev, "periph2_clk2");
1005 if (IS_ERR(periph2_clk2)) {
1006 dev_err(busfreq_dev,
1007 "%s: failed to get periph2_clk2\n",
1009 return PTR_ERR(periph2_clk2);
1012 periph2_clk2_sel = devm_clk_get(&pdev->dev, "periph2_clk2_sel");
1013 if (IS_ERR(periph2_clk2_sel)) {
1014 dev_err(busfreq_dev,
1015 "%s: failed to get periph2_clk2_sel\n",
1017 return PTR_ERR(periph2_clk2_sel);
1020 step_clk = devm_clk_get(&pdev->dev, "step");
1021 if (IS_ERR(step_clk)) {
1022 dev_err(busfreq_dev,
1023 "%s: failed to get step_clk\n",
1025 return PTR_ERR(step_clk);
1028 if (cpu_is_imx6sl()) {
1029 pll2_bypass_src = devm_clk_get(&pdev->dev, "pll2_bypass_src");
1030 if (IS_ERR(pll2_bypass_src)) {
1031 dev_err(busfreq_dev, "%s: failed to get pll2_bypass_src\n",
1033 return PTR_ERR(pll2_bypass_src);
1036 pll2 = devm_clk_get(&pdev->dev, "pll2");
1038 dev_err(busfreq_dev, "%s: failed to get pll2\n",
1040 return PTR_ERR(pll2);
1043 pll2_bypass = devm_clk_get(&pdev->dev, "pll2_bypass");
1044 if (IS_ERR(pll2_bypass)) {
1045 dev_err(busfreq_dev, "%s: failed to get pll2_bypass\n",
1047 return PTR_ERR(pll2_bypass);
1050 if (cpu_is_imx6sx()) {
1051 mmdc_clk = devm_clk_get(&pdev->dev, "mmdc");
1052 if (IS_ERR(mmdc_clk)) {
1053 dev_err(busfreq_dev,
1054 "%s: failed to get mmdc_clk\n",
1056 return PTR_ERR(mmdc_clk);
1058 m4_clk = devm_clk_get(&pdev->dev, "m4");
1059 if (IS_ERR(m4_clk)) {
1060 dev_err(busfreq_dev,
1061 "%s: failed to get m4_clk\n",
1063 return PTR_ERR(m4_clk);
1067 err = sysfs_create_file(&busfreq_dev->kobj, &dev_attr_enable.attr);
1069 dev_err(busfreq_dev,
1070 "Unable to register sysdev entry for BUSFREQ");
1074 if (of_property_read_u32(pdev->dev.of_node, "fsl,max_ddr_freq",
1075 &ddr_normal_rate)) {
1076 dev_err(busfreq_dev, "max_ddr_freq entry missing\n");
1080 high_bus_freq_mode = 1;
1081 med_bus_freq_mode = 0;
1082 low_bus_freq_mode = 0;
1083 audio_bus_freq_mode = 0;
1084 ultra_low_bus_freq_mode = 0;
1086 bus_freq_scaling_is_active = 1;
1087 bus_freq_scaling_initialized = 1;
1089 ddr_low_rate = LPAPM_CLK;
1090 if (cpu_is_imx6q()) {
1091 if (of_property_read_u32(pdev->dev.of_node, "fsl,med_ddr_freq",
1093 dev_info(busfreq_dev,
1094 "DDR medium rate not supported.\n");
1095 ddr_med_rate = ddr_normal_rate;
1099 INIT_DELAYED_WORK(&low_bus_freq_handler, reduce_bus_freq_handler);
1100 INIT_DELAYED_WORK(&bus_freq_daemon, bus_freq_daemon_handler);
1101 register_pm_notifier(&imx_bus_freq_pm_notifier);
1102 register_reboot_notifier(&imx_busfreq_reboot_notifier);
1105 * Need to make sure to an entry for the ddr freq change code address in the IRAM page table.
1106 * This is only required if the DDR freq code and suspend/idle code are in different OCRAM spaces.
1108 if ((iram_tlb_phys_addr & 0xFFF00000) != (ddr_freq_change_iram_phys & 0xFFF00000)) {
1112 * Make sure the ddr_iram virtual address has a mapping
1113 * in the IRAM page table.
1115 i = ((IMX_IO_P2V(ddr_freq_change_iram_phys) >> 20) << 2) / 4;
1116 *((unsigned long *)iram_tlb_base_addr + i) =
1117 (ddr_freq_change_iram_phys & 0xFFF00000) | TT_ATTRIB_NON_CACHEABLE_1M;
1120 if (cpu_is_imx6sl()) {
1121 err = init_mmdc_lpddr2_settings(pdev);
1122 } else if (cpu_is_imx6sx()) {
1123 ddr_type = imx_mmdc_get_ddr_type();
1124 /* check whether it is a DDR3 or LPDDR2 board */
1125 if (ddr_type == MMDC_MDMISC_DDR_TYPE_DDR3)
1126 err = init_mmdc_ddr3_settings_imx6sx(pdev);
1127 else if (ddr_type == MMDC_MDMISC_DDR_TYPE_LPDDR2)
1128 err = init_mmdc_lpddr2_settings(pdev);
1129 /* if M4 is enabled and rate > 24MHz, add high bus count */
1130 if (imx_src_is_m4_enabled() &&
1131 (clk_get_rate(m4_clk) > LPAPM_CLK))
1134 err = init_mmdc_ddr3_settings_imx6q(pdev);
1138 dev_err(busfreq_dev, "Busfreq init of MMDC failed\n");
1144 static const struct of_device_id imx6_busfreq_ids[] = {
1145 { .compatible = "fsl,imx6_busfreq", },
1149 static struct platform_driver busfreq_driver = {
1151 .name = "imx6_busfreq",
1152 .owner = THIS_MODULE,
1153 .of_match_table = imx6_busfreq_ids,
1155 .probe = busfreq_probe,
1159 * Initialise the busfreq_driver.
1161 * @return The function always returns 0.
1164 static int __init busfreq_init(void)
1166 #ifndef CONFIG_MX6_VPU_352M
1167 if (platform_driver_register(&busfreq_driver) != 0)
1170 printk(KERN_INFO "Bus freq driver module loaded\n");
1175 static void __exit busfreq_cleanup(void)
1177 sysfs_remove_file(&busfreq_dev->kobj, &dev_attr_enable.attr);
1179 /* Unregister the device structure */
1180 platform_driver_unregister(&busfreq_driver);
1181 bus_freq_scaling_initialized = 0;
1184 module_init(busfreq_init);
1185 module_exit(busfreq_cleanup);
1187 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1188 MODULE_DESCRIPTION("BusFreq driver");
1189 MODULE_LICENSE("GPL");