2 * cpu.c: clock scaling for the iMX
4 * Copyright (C) 2000 2001, The Delft University of Technology
5 * Copyright (c) 2004 Sascha Hauer <sascha@saschahauer.de>
6 * Copyright (C) 2006 Inky Lung <ilung@cwlinux.com>
7 * Copyright (C) 2006 Pavel Pisa, PiKRON <ppisa@pikron.com>
9 * Based on SA1100 version written by:
10 * - Johan Pouwelse (J.A.Pouwelse@its.tudelft.nl): initial version
11 * - Erik Mouw (J.A.K.Mouw@its.tudelft.nl):
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/init.h>
34 #include <linux/cpufreq.h>
35 #include <asm/system.h>
37 #include <asm/hardware.h>
42 #define __val2mfld(mask,val) (((mask)&~((mask)<<1))*(val)&(mask))
45 #define __mfld2val(mask,val) (((val)&(mask))/((mask)&~((mask)<<1)))
48 #define CR_920T_CLOCK_MODE 0xC0000000
49 #define CR_920T_FASTBUS_MODE 0x00000000
50 #define CR_920T_ASYNC_MODE 0xC0000000
52 static u32 mpctl0_at_boot;
54 static void imx_set_async_mode(void)
56 adjust_cr(CR_920T_CLOCK_MODE, CR_920T_ASYNC_MODE);
59 static void imx_set_fastbus_mode(void)
61 adjust_cr(CR_920T_CLOCK_MODE, CR_920T_FASTBUS_MODE);
64 static void imx_set_mpctl0(u32 mpctl0)
69 local_irq_save(flags);
71 local_irq_restore(flags);
75 local_irq_save(flags);
78 local_irq_restore(flags);
82 * imx_compute_mpctl - compute new PLL parameters
83 * @new_mpctl: pointer to location assigned by new PLL control register value
84 * @cur_mpctl: current PLL control register parameters
85 * @freq: required frequency in Hz
86 * @relation: is one of %CPUFREQ_RELATION_L (supremum)
87 * and %CPUFREQ_RELATION_H (infimum)
89 long imx_compute_mpctl(u32 *new_mpctl, u32 cur_mpctl, unsigned long freq, int relation)
91 u32 f_ref = (CSCR & CSCR_SYSTEM_SEL) ? 16000000 : (CLK32 * 512);
96 unsigned long long ll;
100 /* Fdppl=2*Fref*(MFI+MFN/(MFD+1))/(PD+1) */
101 /* PD=<0,15>, MFD=<1,1023>, MFI=<5,15> MFN=<0,1022> */
104 mfd = ((cur_mpctl >> 16) & 0x3ff) + 1;
105 pd = ((cur_mpctl >> 26) & 0xf) + 1;
110 /* pd=2; mfd=313; mfi=8; mfn=183; */
111 /* (MFI+MFN/(MFD)) = Fdppl / (2*Fref) * (PD); */
113 quot = (f_ref + (1 << 9)) >> 10;
114 l = (freq * pd + quot) / (2 * quot);
116 mfn = ((l & ((1 << 10) - 1)) * mfd + (1 << 9)) >> 10;
121 *new_mpctl = ((mfi & 0xf) << 10) | (mfn & 0x3ff) | ((mfd & 0x3ff) << 16)
122 | ((pd & 0xf) << 26);
124 ll = 2 * (unsigned long long)f_ref * ( (mfi<<16) + (mfn<<16) / (mfd+1) );
125 quot = (pd+1) * (1<<16);
130 pr_debug(KERN_DEBUG "imx: new PLL parameters pd=%d mfd=%d mfi=%d mfn=%d, freq=%ld\n",
131 pd, mfd, mfi, mfn, freq);
137 static int imx_verify_speed(struct cpufreq_policy *policy)
139 if (policy->cpu != 0)
142 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, policy->cpuinfo.max_freq);
147 static unsigned int imx_get_speed(unsigned int cpu)
152 unsigned int bclk_div;
158 bclk_div = __mfld2val(CSCR_BCLK_DIV, cscr) + 1;
161 if((cr & CR_920T_CLOCK_MODE) == CR_920T_FASTBUS_MODE) {
162 freq = imx_get_system_clk();
163 freq = (freq + bclk_div/2) / bclk_div;
165 freq = imx_get_mcu_clk();
166 if (cscr & CSCR_MPU_PRESC)
170 freq = (freq + 500) / 1000;
175 static int imx_set_target(struct cpufreq_policy *policy,
176 unsigned int target_freq,
177 unsigned int relation)
179 struct cpufreq_freqs freqs;
185 unsigned int bclk_div = 1;
188 * Some governors do not respects CPU and policy lower limits
189 * which leads to bad things (division by zero etc), ensure
190 * that such things do not happen.
192 if(target_freq < policy->cpuinfo.min_freq)
193 target_freq = policy->cpuinfo.min_freq;
195 if(target_freq < policy->min)
196 target_freq = policy->min;
198 freq = target_freq * 1000;
200 pr_debug(KERN_DEBUG "imx: requested frequency %ld Hz, mpctl0 at boot 0x%08x\n",
201 freq, mpctl0_at_boot);
203 sysclk = imx_get_system_clk();
205 if (freq > sysclk + 1000000) {
206 freq = imx_compute_mpctl(&mpctl0, mpctl0_at_boot, freq, relation);
208 printk(KERN_WARNING "imx: target frequency %ld Hz cannot be set\n", freq);
212 if(freq + 1000 < sysclk) {
213 if (relation == CPUFREQ_RELATION_L)
214 bclk_div = (sysclk - 1000) / freq;
216 bclk_div = (sysclk + freq + 1000) / freq;
221 freq = (sysclk + bclk_div / 2) / bclk_div;
224 freqs.old = imx_get_speed(0);
225 freqs.new = (freq + 500) / 1000;
229 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
231 local_irq_save(flags);
233 imx_set_fastbus_mode();
235 imx_set_mpctl0(mpctl0);
238 cscr &= ~CSCR_BCLK_DIV;
239 cscr |= __val2mfld(CSCR_BCLK_DIV, bclk_div - 1);
243 CSCR |= CSCR_MPLL_RESTART;
245 /* Wait until MPLL is stablized */
246 while( CSCR & CSCR_MPLL_RESTART );
248 imx_set_async_mode();
251 local_irq_restore(flags);
253 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
255 pr_debug(KERN_INFO "imx: set frequency %ld Hz, running from %s\n",
256 freq, mpctl0? "MPLL": "SPLL");
261 static int __init imx_cpufreq_driver_init(struct cpufreq_policy *policy)
263 printk(KERN_INFO "i.MX cpu freq change driver v1.0\n");
265 if (policy->cpu != 0)
268 policy->cur = policy->min = policy->max = imx_get_speed(0);
269 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
270 policy->cpuinfo.min_freq = 8000;
271 policy->cpuinfo.max_freq = 200000;
272 /* Manual states, that PLL stabilizes in two CLK32 periods */
273 policy->cpuinfo.transition_latency = 4 * 1000000000LL / CLK32;
277 static struct cpufreq_driver imx_driver = {
278 .flags = CPUFREQ_STICKY,
279 .verify = imx_verify_speed,
280 .target = imx_set_target,
281 .get = imx_get_speed,
282 .init = imx_cpufreq_driver_init,
286 static int __init imx_cpufreq_init(void)
291 if((CSCR & CSCR_MPEN) &&
292 ((get_cr() & CR_920T_CLOCK_MODE) != CR_920T_FASTBUS_MODE))
293 mpctl0_at_boot = MPCTL0;
295 return cpufreq_register_driver(&imx_driver);
298 arch_initcall(imx_cpufreq_init);