MIPS: Yosemite, Emma: Fix off-by-two in arcs_cmdline buffer size check
[linux-2.6/linux-mips.git] / arch / arm / mach-lpc32xx / clock.c
blob1e027514096d0d9e15f87152a0e92761f1e959e8
1 /*
2 * arch/arm/mach-lpc32xx/clock.c
4 * Author: Kevin Wells <kevin.wells@nxp.com>
6 * Copyright (C) 2010 NXP Semiconductors
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 * LPC32xx clock management driver overview
22 * The LPC32XX contains a number of high level system clocks that can be
23 * generated from different sources. These system clocks are used to
24 * generate the CPU and bus rates and the individual peripheral clocks in
25 * the system. When Linux is started by the boot loader, the system
26 * clocks are already running. Stopping a system clock during normal
27 * Linux operation should never be attempted, as peripherals that require
28 * those clocks will quit working (ie, DRAM).
30 * The LPC32xx high level clock tree looks as follows. Clocks marked with
31 * an asterisk are always on and cannot be disabled. Clocks marked with
32 * an ampersand can only be disabled in CPU suspend mode. Clocks marked
33 * with a caret are always on if it is the selected clock for the SYSCLK
34 * source. The clock that isn't used for SYSCLK can be enabled and
35 * disabled normally.
36 * 32KHz oscillator*
37 * / | \
38 * RTC* PLL397^ TOUCH
39 * /
40 * Main oscillator^ /
41 * | \ /
42 * | SYSCLK&
43 * | \
44 * | \
45 * USB_PLL HCLK_PLL&
46 * | | |
47 * USB host/device PCLK& |
48 * | |
49 * Peripherals
51 * The CPU and chip bus rates are derived from the HCLK PLL, which can
52 * generate various clock rates up to 266MHz and beyond. The internal bus
53 * rates (PCLK and HCLK) are generated from dividers based on the HCLK
54 * PLL rate. HCLK can be a ratio of 1:1, 1:2, or 1:4 or HCLK PLL rate,
55 * while PCLK can be 1:1 to 1:32 of HCLK PLL rate. Most peripherals high
56 * level clocks are based on either HCLK or PCLK, but have their own
57 * dividers as part of the IP itself. Because of this, the system clock
58 * rates should not be changed.
60 * The HCLK PLL is clocked from SYSCLK, which can be derived from the
61 * main oscillator or PLL397. PLL397 generates a rate that is 397 times
62 * the 32KHz oscillator rate. The main oscillator runs at the selected
63 * oscillator/crystal rate on the mosc_in pin of the LPC32xx. This rate
64 * is normally 13MHz, but depends on the selection of external crystals
65 * or oscillators. If USB operation is required, the main oscillator must
66 * be used in the system.
68 * Switching SYSCLK between sources during normal Linux operation is not
69 * supported. SYSCLK is preset in the bootloader. Because of the
70 * complexities of clock management during clock frequency changes,
71 * there are some limitations to the clock driver explained below:
72 * - The PLL397 and main oscillator can be enabled and disabled by the
73 * clk_enable() and clk_disable() functions unless SYSCLK is based
74 * on that clock. This allows the other oscillator that isn't driving
75 * the HCLK PLL to be used as another system clock that can be routed
76 * to an external pin.
77 * - The muxed SYSCLK input and HCLK_PLL rate cannot be changed with
78 * this driver.
79 * - HCLK and PCLK rates cannot be changed as part of this driver.
80 * - Most peripherals have their own dividers are part of the peripheral
81 * block. Changing SYSCLK, HCLK PLL, HCLK, or PCLK sources or rates
82 * will also impact the individual peripheral rates.
85 #include <linux/kernel.h>
86 #include <linux/list.h>
87 #include <linux/errno.h>
88 #include <linux/device.h>
89 #include <linux/err.h>
90 #include <linux/clk.h>
91 #include <linux/amba/bus.h>
92 #include <linux/amba/clcd.h>
93 #include <linux/clkdev.h>
95 #include <mach/hardware.h>
96 #include <mach/platform.h>
97 #include "clock.h"
98 #include "common.h"
100 static struct clk clk_armpll;
101 static struct clk clk_usbpll;
102 static DEFINE_MUTEX(clkm_lock);
105 * Post divider values for PLLs based on selected register value
107 static const u32 pll_postdivs[4] = {1, 2, 4, 8};
109 static unsigned long local_return_parent_rate(struct clk *clk)
112 * If a clock has a rate of 0, then it inherits it's parent
113 * clock rate
115 while (clk->rate == 0)
116 clk = clk->parent;
118 return clk->rate;
121 /* 32KHz clock has a fixed rate and is not stoppable */
122 static struct clk osc_32KHz = {
123 .rate = LPC32XX_CLOCK_OSC_FREQ,
124 .get_rate = local_return_parent_rate,
127 static int local_pll397_enable(struct clk *clk, int enable)
129 u32 reg;
130 unsigned long timeout = 1 + msecs_to_jiffies(10);
132 reg = __raw_readl(LPC32XX_CLKPWR_PLL397_CTRL);
134 if (enable == 0) {
135 reg |= LPC32XX_CLKPWR_SYSCTRL_PLL397_DIS;
136 __raw_writel(reg, LPC32XX_CLKPWR_PLL397_CTRL);
137 } else {
138 /* Enable PLL397 */
139 reg &= ~LPC32XX_CLKPWR_SYSCTRL_PLL397_DIS;
140 __raw_writel(reg, LPC32XX_CLKPWR_PLL397_CTRL);
142 /* Wait for PLL397 lock */
143 while (((__raw_readl(LPC32XX_CLKPWR_PLL397_CTRL) &
144 LPC32XX_CLKPWR_SYSCTRL_PLL397_STS) == 0) &&
145 (timeout > jiffies))
146 cpu_relax();
148 if ((__raw_readl(LPC32XX_CLKPWR_PLL397_CTRL) &
149 LPC32XX_CLKPWR_SYSCTRL_PLL397_STS) == 0)
150 return -ENODEV;
153 return 0;
156 static int local_oscmain_enable(struct clk *clk, int enable)
158 u32 reg;
159 unsigned long timeout = 1 + msecs_to_jiffies(10);
161 reg = __raw_readl(LPC32XX_CLKPWR_MAIN_OSC_CTRL);
163 if (enable == 0) {
164 reg |= LPC32XX_CLKPWR_MOSC_DISABLE;
165 __raw_writel(reg, LPC32XX_CLKPWR_MAIN_OSC_CTRL);
166 } else {
167 /* Enable main oscillator */
168 reg &= ~LPC32XX_CLKPWR_MOSC_DISABLE;
169 __raw_writel(reg, LPC32XX_CLKPWR_MAIN_OSC_CTRL);
171 /* Wait for main oscillator to start */
172 while (((__raw_readl(LPC32XX_CLKPWR_MAIN_OSC_CTRL) &
173 LPC32XX_CLKPWR_MOSC_DISABLE) != 0) &&
174 (timeout > jiffies))
175 cpu_relax();
177 if ((__raw_readl(LPC32XX_CLKPWR_MAIN_OSC_CTRL) &
178 LPC32XX_CLKPWR_MOSC_DISABLE) != 0)
179 return -ENODEV;
182 return 0;
185 static struct clk osc_pll397 = {
186 .parent = &osc_32KHz,
187 .enable = local_pll397_enable,
188 .rate = LPC32XX_CLOCK_OSC_FREQ * 397,
189 .get_rate = local_return_parent_rate,
192 static struct clk osc_main = {
193 .enable = local_oscmain_enable,
194 .rate = LPC32XX_MAIN_OSC_FREQ,
195 .get_rate = local_return_parent_rate,
198 static struct clk clk_sys;
201 * Convert a PLL register value to a PLL output frequency
203 u32 clk_get_pllrate_from_reg(u32 inputclk, u32 regval)
205 struct clk_pll_setup pllcfg;
207 pllcfg.cco_bypass_b15 = 0;
208 pllcfg.direct_output_b14 = 0;
209 pllcfg.fdbk_div_ctrl_b13 = 0;
210 if ((regval & LPC32XX_CLKPWR_HCLKPLL_CCO_BYPASS) != 0)
211 pllcfg.cco_bypass_b15 = 1;
212 if ((regval & LPC32XX_CLKPWR_HCLKPLL_POSTDIV_BYPASS) != 0)
213 pllcfg.direct_output_b14 = 1;
214 if ((regval & LPC32XX_CLKPWR_HCLKPLL_FDBK_SEL_FCLK) != 0)
215 pllcfg.fdbk_div_ctrl_b13 = 1;
216 pllcfg.pll_m = 1 + ((regval >> 1) & 0xFF);
217 pllcfg.pll_n = 1 + ((regval >> 9) & 0x3);
218 pllcfg.pll_p = pll_postdivs[((regval >> 11) & 0x3)];
220 return clk_check_pll_setup(inputclk, &pllcfg);
224 * Setup the HCLK PLL with a PLL structure
226 static u32 local_clk_pll_setup(struct clk_pll_setup *PllSetup)
228 u32 tv, tmp = 0;
230 if (PllSetup->analog_on != 0)
231 tmp |= LPC32XX_CLKPWR_HCLKPLL_POWER_UP;
232 if (PllSetup->cco_bypass_b15 != 0)
233 tmp |= LPC32XX_CLKPWR_HCLKPLL_CCO_BYPASS;
234 if (PllSetup->direct_output_b14 != 0)
235 tmp |= LPC32XX_CLKPWR_HCLKPLL_POSTDIV_BYPASS;
236 if (PllSetup->fdbk_div_ctrl_b13 != 0)
237 tmp |= LPC32XX_CLKPWR_HCLKPLL_FDBK_SEL_FCLK;
239 tv = ffs(PllSetup->pll_p) - 1;
240 if ((!is_power_of_2(PllSetup->pll_p)) || (tv > 3))
241 return 0;
243 tmp |= LPC32XX_CLKPWR_HCLKPLL_POSTDIV_2POW(tv);
244 tmp |= LPC32XX_CLKPWR_HCLKPLL_PREDIV_PLUS1(PllSetup->pll_n - 1);
245 tmp |= LPC32XX_CLKPWR_HCLKPLL_PLLM(PllSetup->pll_m - 1);
247 return tmp;
251 * Update the ARM core PLL frequency rate variable from the actual PLL setting
253 static void local_update_armpll_rate(void)
255 u32 clkin, pllreg;
257 clkin = clk_armpll.parent->rate;
258 pllreg = __raw_readl(LPC32XX_CLKPWR_HCLKPLL_CTRL) & 0x1FFFF;
260 clk_armpll.rate = clk_get_pllrate_from_reg(clkin, pllreg);
264 * Find a PLL configuration for the selected input frequency
266 static u32 local_clk_find_pll_cfg(u32 pllin_freq, u32 target_freq,
267 struct clk_pll_setup *pllsetup)
269 u32 ifreq, freqtol, m, n, p, fclkout;
271 /* Determine frequency tolerance limits */
272 freqtol = target_freq / 250;
273 ifreq = pllin_freq;
275 /* Is direct bypass mode possible? */
276 if (abs(pllin_freq - target_freq) <= freqtol) {
277 pllsetup->analog_on = 0;
278 pllsetup->cco_bypass_b15 = 1;
279 pllsetup->direct_output_b14 = 1;
280 pllsetup->fdbk_div_ctrl_b13 = 1;
281 pllsetup->pll_p = pll_postdivs[0];
282 pllsetup->pll_n = 1;
283 pllsetup->pll_m = 1;
284 return clk_check_pll_setup(ifreq, pllsetup);
285 } else if (target_freq <= ifreq) {
286 pllsetup->analog_on = 0;
287 pllsetup->cco_bypass_b15 = 1;
288 pllsetup->direct_output_b14 = 0;
289 pllsetup->fdbk_div_ctrl_b13 = 1;
290 pllsetup->pll_n = 1;
291 pllsetup->pll_m = 1;
292 for (p = 0; p <= 3; p++) {
293 pllsetup->pll_p = pll_postdivs[p];
294 fclkout = clk_check_pll_setup(ifreq, pllsetup);
295 if (abs(target_freq - fclkout) <= freqtol)
296 return fclkout;
300 /* Is direct mode possible? */
301 pllsetup->analog_on = 1;
302 pllsetup->cco_bypass_b15 = 0;
303 pllsetup->direct_output_b14 = 1;
304 pllsetup->fdbk_div_ctrl_b13 = 0;
305 pllsetup->pll_p = pll_postdivs[0];
306 for (m = 1; m <= 256; m++) {
307 for (n = 1; n <= 4; n++) {
308 /* Compute output frequency for this value */
309 pllsetup->pll_n = n;
310 pllsetup->pll_m = m;
311 fclkout = clk_check_pll_setup(ifreq,
312 pllsetup);
313 if (abs(target_freq - fclkout) <=
314 freqtol)
315 return fclkout;
319 /* Is integer mode possible? */
320 pllsetup->analog_on = 1;
321 pllsetup->cco_bypass_b15 = 0;
322 pllsetup->direct_output_b14 = 0;
323 pllsetup->fdbk_div_ctrl_b13 = 1;
324 for (m = 1; m <= 256; m++) {
325 for (n = 1; n <= 4; n++) {
326 for (p = 0; p < 4; p++) {
327 /* Compute output frequency */
328 pllsetup->pll_p = pll_postdivs[p];
329 pllsetup->pll_n = n;
330 pllsetup->pll_m = m;
331 fclkout = clk_check_pll_setup(
332 ifreq, pllsetup);
333 if (abs(target_freq - fclkout) <= freqtol)
334 return fclkout;
339 /* Try non-integer mode */
340 pllsetup->analog_on = 1;
341 pllsetup->cco_bypass_b15 = 0;
342 pllsetup->direct_output_b14 = 0;
343 pllsetup->fdbk_div_ctrl_b13 = 0;
344 for (m = 1; m <= 256; m++) {
345 for (n = 1; n <= 4; n++) {
346 for (p = 0; p < 4; p++) {
347 /* Compute output frequency */
348 pllsetup->pll_p = pll_postdivs[p];
349 pllsetup->pll_n = n;
350 pllsetup->pll_m = m;
351 fclkout = clk_check_pll_setup(
352 ifreq, pllsetup);
353 if (abs(target_freq - fclkout) <= freqtol)
354 return fclkout;
359 return 0;
362 static struct clk clk_armpll = {
363 .parent = &clk_sys,
364 .get_rate = local_return_parent_rate,
368 * Setup the USB PLL with a PLL structure
370 static u32 local_clk_usbpll_setup(struct clk_pll_setup *pHCLKPllSetup)
372 u32 reg, tmp = local_clk_pll_setup(pHCLKPllSetup);
374 reg = __raw_readl(LPC32XX_CLKPWR_USB_CTRL) & ~0x1FFFF;
375 reg |= tmp;
376 __raw_writel(reg, LPC32XX_CLKPWR_USB_CTRL);
378 return clk_check_pll_setup(clk_usbpll.parent->rate,
379 pHCLKPllSetup);
382 static int local_usbpll_enable(struct clk *clk, int enable)
384 u32 reg;
385 int ret = -ENODEV;
386 unsigned long timeout = 1 + msecs_to_jiffies(10);
388 reg = __raw_readl(LPC32XX_CLKPWR_USB_CTRL);
390 if (enable == 0) {
391 reg &= ~(LPC32XX_CLKPWR_USBCTRL_CLK_EN1 |
392 LPC32XX_CLKPWR_USBCTRL_CLK_EN2);
393 __raw_writel(reg, LPC32XX_CLKPWR_USB_CTRL);
394 } else if (reg & LPC32XX_CLKPWR_USBCTRL_PLL_PWRUP) {
395 reg |= LPC32XX_CLKPWR_USBCTRL_CLK_EN1;
396 __raw_writel(reg, LPC32XX_CLKPWR_USB_CTRL);
398 /* Wait for PLL lock */
399 while ((timeout > jiffies) & (ret == -ENODEV)) {
400 reg = __raw_readl(LPC32XX_CLKPWR_USB_CTRL);
401 if (reg & LPC32XX_CLKPWR_USBCTRL_PLL_STS)
402 ret = 0;
405 if (ret == 0) {
406 reg |= LPC32XX_CLKPWR_USBCTRL_CLK_EN2;
407 __raw_writel(reg, LPC32XX_CLKPWR_USB_CTRL);
411 return ret;
414 static unsigned long local_usbpll_round_rate(struct clk *clk,
415 unsigned long rate)
417 u32 clkin, usbdiv;
418 struct clk_pll_setup pllsetup;
421 * Unlike other clocks, this clock has a KHz input rate, so bump
422 * it up to work with the PLL function
424 rate = rate * 1000;
426 clkin = clk->parent->rate;
427 usbdiv = (__raw_readl(LPC32XX_CLKPWR_USBCLK_PDIV) &
428 LPC32XX_CLKPWR_USBPDIV_PLL_MASK) + 1;
429 clkin = clkin / usbdiv;
431 /* Try to find a good rate setup */
432 if (local_clk_find_pll_cfg(clkin, rate, &pllsetup) == 0)
433 return 0;
435 return clk_check_pll_setup(clkin, &pllsetup);
438 static int local_usbpll_set_rate(struct clk *clk, unsigned long rate)
440 u32 clkin, reg, usbdiv;
441 struct clk_pll_setup pllsetup;
444 * Unlike other clocks, this clock has a KHz input rate, so bump
445 * it up to work with the PLL function
447 rate = rate * 1000;
449 clkin = clk->get_rate(clk);
450 usbdiv = (__raw_readl(LPC32XX_CLKPWR_USBCLK_PDIV) &
451 LPC32XX_CLKPWR_USBPDIV_PLL_MASK) + 1;
452 clkin = clkin / usbdiv;
454 /* Try to find a good rate setup */
455 if (local_clk_find_pll_cfg(clkin, rate, &pllsetup) == 0)
456 return -EINVAL;
458 local_usbpll_enable(clk, 0);
460 reg = __raw_readl(LPC32XX_CLKPWR_USB_CTRL);
461 reg |= LPC32XX_CLKPWR_USBCTRL_CLK_EN1;
462 __raw_writel(reg, LPC32XX_CLKPWR_USB_CTRL);
464 pllsetup.analog_on = 1;
465 local_clk_usbpll_setup(&pllsetup);
467 clk->rate = clk_check_pll_setup(clkin, &pllsetup);
469 reg = __raw_readl(LPC32XX_CLKPWR_USB_CTRL);
470 reg |= LPC32XX_CLKPWR_USBCTRL_CLK_EN2;
471 __raw_writel(reg, LPC32XX_CLKPWR_USB_CTRL);
473 return 0;
476 static struct clk clk_usbpll = {
477 .parent = &osc_main,
478 .set_rate = local_usbpll_set_rate,
479 .enable = local_usbpll_enable,
480 .rate = 48000, /* In KHz */
481 .get_rate = local_return_parent_rate,
482 .round_rate = local_usbpll_round_rate,
485 static u32 clk_get_hclk_div(void)
487 static const u32 hclkdivs[4] = {1, 2, 4, 4};
488 return hclkdivs[LPC32XX_CLKPWR_HCLKDIV_DIV_2POW(
489 __raw_readl(LPC32XX_CLKPWR_HCLK_DIV))];
492 static struct clk clk_hclk = {
493 .parent = &clk_armpll,
494 .get_rate = local_return_parent_rate,
497 static struct clk clk_pclk = {
498 .parent = &clk_armpll,
499 .get_rate = local_return_parent_rate,
502 static int local_onoff_enable(struct clk *clk, int enable)
504 u32 tmp;
506 tmp = __raw_readl(clk->enable_reg);
508 if (enable == 0)
509 tmp &= ~clk->enable_mask;
510 else
511 tmp |= clk->enable_mask;
513 __raw_writel(tmp, clk->enable_reg);
515 return 0;
518 /* Peripheral clock sources */
519 static struct clk clk_timer0 = {
520 .parent = &clk_pclk,
521 .enable = local_onoff_enable,
522 .enable_reg = LPC32XX_CLKPWR_TIMERS_PWMS_CLK_CTRL_1,
523 .enable_mask = LPC32XX_CLKPWR_TMRPWMCLK_TIMER0_EN,
524 .get_rate = local_return_parent_rate,
526 static struct clk clk_timer1 = {
527 .parent = &clk_pclk,
528 .enable = local_onoff_enable,
529 .enable_reg = LPC32XX_CLKPWR_TIMERS_PWMS_CLK_CTRL_1,
530 .enable_mask = LPC32XX_CLKPWR_TMRPWMCLK_TIMER1_EN,
531 .get_rate = local_return_parent_rate,
533 static struct clk clk_timer2 = {
534 .parent = &clk_pclk,
535 .enable = local_onoff_enable,
536 .enable_reg = LPC32XX_CLKPWR_TIMERS_PWMS_CLK_CTRL_1,
537 .enable_mask = LPC32XX_CLKPWR_TMRPWMCLK_TIMER2_EN,
538 .get_rate = local_return_parent_rate,
540 static struct clk clk_timer3 = {
541 .parent = &clk_pclk,
542 .enable = local_onoff_enable,
543 .enable_reg = LPC32XX_CLKPWR_TIMERS_PWMS_CLK_CTRL_1,
544 .enable_mask = LPC32XX_CLKPWR_TMRPWMCLK_TIMER3_EN,
545 .get_rate = local_return_parent_rate,
547 static struct clk clk_wdt = {
548 .parent = &clk_pclk,
549 .enable = local_onoff_enable,
550 .enable_reg = LPC32XX_CLKPWR_TIMER_CLK_CTRL,
551 .enable_mask = LPC32XX_CLKPWR_PWMCLK_WDOG_EN,
552 .get_rate = local_return_parent_rate,
554 static struct clk clk_vfp9 = {
555 .parent = &clk_pclk,
556 .enable = local_onoff_enable,
557 .enable_reg = LPC32XX_CLKPWR_DEBUG_CTRL,
558 .enable_mask = LPC32XX_CLKPWR_VFP_CLOCK_ENABLE_BIT,
559 .get_rate = local_return_parent_rate,
561 static struct clk clk_dma = {
562 .parent = &clk_hclk,
563 .enable = local_onoff_enable,
564 .enable_reg = LPC32XX_CLKPWR_DMA_CLK_CTRL,
565 .enable_mask = LPC32XX_CLKPWR_DMACLKCTRL_CLK_EN,
566 .get_rate = local_return_parent_rate,
569 static struct clk clk_uart3 = {
570 .parent = &clk_pclk,
571 .enable = local_onoff_enable,
572 .enable_reg = LPC32XX_CLKPWR_UART_CLK_CTRL,
573 .enable_mask = LPC32XX_CLKPWR_UARTCLKCTRL_UART3_EN,
574 .get_rate = local_return_parent_rate,
577 static struct clk clk_uart4 = {
578 .parent = &clk_pclk,
579 .enable = local_onoff_enable,
580 .enable_reg = LPC32XX_CLKPWR_UART_CLK_CTRL,
581 .enable_mask = LPC32XX_CLKPWR_UARTCLKCTRL_UART4_EN,
582 .get_rate = local_return_parent_rate,
585 static struct clk clk_uart5 = {
586 .parent = &clk_pclk,
587 .enable = local_onoff_enable,
588 .enable_reg = LPC32XX_CLKPWR_UART_CLK_CTRL,
589 .enable_mask = LPC32XX_CLKPWR_UARTCLKCTRL_UART5_EN,
590 .get_rate = local_return_parent_rate,
593 static struct clk clk_uart6 = {
594 .parent = &clk_pclk,
595 .enable = local_onoff_enable,
596 .enable_reg = LPC32XX_CLKPWR_UART_CLK_CTRL,
597 .enable_mask = LPC32XX_CLKPWR_UARTCLKCTRL_UART6_EN,
598 .get_rate = local_return_parent_rate,
601 static struct clk clk_i2c0 = {
602 .parent = &clk_hclk,
603 .enable = local_onoff_enable,
604 .enable_reg = LPC32XX_CLKPWR_I2C_CLK_CTRL,
605 .enable_mask = LPC32XX_CLKPWR_I2CCLK_I2C1CLK_EN,
606 .get_rate = local_return_parent_rate,
609 static struct clk clk_i2c1 = {
610 .parent = &clk_hclk,
611 .enable = local_onoff_enable,
612 .enable_reg = LPC32XX_CLKPWR_I2C_CLK_CTRL,
613 .enable_mask = LPC32XX_CLKPWR_I2CCLK_I2C2CLK_EN,
614 .get_rate = local_return_parent_rate,
617 static struct clk clk_i2c2 = {
618 .parent = &clk_pclk,
619 .enable = local_onoff_enable,
620 .enable_reg = io_p2v(LPC32XX_USB_BASE + 0xFF4),
621 .enable_mask = 0x4,
622 .get_rate = local_return_parent_rate,
625 static struct clk clk_ssp0 = {
626 .parent = &clk_hclk,
627 .enable = local_onoff_enable,
628 .enable_reg = LPC32XX_CLKPWR_SSP_CLK_CTRL,
629 .enable_mask = LPC32XX_CLKPWR_SSPCTRL_SSPCLK0_EN,
630 .get_rate = local_return_parent_rate,
633 static struct clk clk_ssp1 = {
634 .parent = &clk_hclk,
635 .enable = local_onoff_enable,
636 .enable_reg = LPC32XX_CLKPWR_SSP_CLK_CTRL,
637 .enable_mask = LPC32XX_CLKPWR_SSPCTRL_SSPCLK1_EN,
638 .get_rate = local_return_parent_rate,
641 static struct clk clk_kscan = {
642 .parent = &osc_32KHz,
643 .enable = local_onoff_enable,
644 .enable_reg = LPC32XX_CLKPWR_KEY_CLK_CTRL,
645 .enable_mask = LPC32XX_CLKPWR_KEYCLKCTRL_CLK_EN,
646 .get_rate = local_return_parent_rate,
649 static struct clk clk_nand = {
650 .parent = &clk_hclk,
651 .enable = local_onoff_enable,
652 .enable_reg = LPC32XX_CLKPWR_NAND_CLK_CTRL,
653 .enable_mask = LPC32XX_CLKPWR_NANDCLK_SLCCLK_EN,
654 .get_rate = local_return_parent_rate,
657 static struct clk clk_i2s0 = {
658 .parent = &clk_hclk,
659 .enable = local_onoff_enable,
660 .enable_reg = LPC32XX_CLKPWR_I2S_CLK_CTRL,
661 .enable_mask = LPC32XX_CLKPWR_I2SCTRL_I2SCLK0_EN,
662 .get_rate = local_return_parent_rate,
665 static struct clk clk_i2s1 = {
666 .parent = &clk_hclk,
667 .enable = local_onoff_enable,
668 .enable_reg = LPC32XX_CLKPWR_I2S_CLK_CTRL,
669 .enable_mask = LPC32XX_CLKPWR_I2SCTRL_I2SCLK1_EN,
670 .get_rate = local_return_parent_rate,
673 static struct clk clk_net = {
674 .parent = &clk_hclk,
675 .enable = local_onoff_enable,
676 .enable_reg = LPC32XX_CLKPWR_MACCLK_CTRL,
677 .enable_mask = (LPC32XX_CLKPWR_MACCTRL_DMACLK_EN |
678 LPC32XX_CLKPWR_MACCTRL_MMIOCLK_EN |
679 LPC32XX_CLKPWR_MACCTRL_HRCCLK_EN),
680 .get_rate = local_return_parent_rate,
683 static struct clk clk_rtc = {
684 .parent = &osc_32KHz,
685 .rate = 1, /* 1 Hz */
686 .get_rate = local_return_parent_rate,
689 static struct clk clk_usbd = {
690 .parent = &clk_usbpll,
691 .enable = local_onoff_enable,
692 .enable_reg = LPC32XX_CLKPWR_USB_CTRL,
693 .enable_mask = LPC32XX_CLKPWR_USBCTRL_HCLK_EN,
694 .get_rate = local_return_parent_rate,
697 static int tsc_onoff_enable(struct clk *clk, int enable)
699 u32 tmp;
701 /* Make sure 32KHz clock is the selected clock */
702 tmp = __raw_readl(LPC32XX_CLKPWR_ADC_CLK_CTRL_1);
703 tmp &= ~LPC32XX_CLKPWR_ADCCTRL1_PCLK_SEL;
704 __raw_writel(tmp, LPC32XX_CLKPWR_ADC_CLK_CTRL_1);
706 if (enable == 0)
707 __raw_writel(0, clk->enable_reg);
708 else
709 __raw_writel(clk->enable_mask, clk->enable_reg);
711 return 0;
714 static struct clk clk_tsc = {
715 .parent = &osc_32KHz,
716 .enable = tsc_onoff_enable,
717 .enable_reg = LPC32XX_CLKPWR_ADC_CLK_CTRL,
718 .enable_mask = LPC32XX_CLKPWR_ADC32CLKCTRL_CLK_EN,
719 .get_rate = local_return_parent_rate,
722 static int mmc_onoff_enable(struct clk *clk, int enable)
724 u32 tmp;
726 tmp = __raw_readl(LPC32XX_CLKPWR_MS_CTRL) &
727 ~LPC32XX_CLKPWR_MSCARD_SDCARD_EN;
729 /* If rate is 0, disable clock */
730 if (enable != 0)
731 tmp |= LPC32XX_CLKPWR_MSCARD_SDCARD_EN;
733 __raw_writel(tmp, LPC32XX_CLKPWR_MS_CTRL);
735 return 0;
738 static unsigned long mmc_get_rate(struct clk *clk)
740 u32 div, rate, oldclk;
742 /* The MMC clock must be on when accessing an MMC register */
743 oldclk = __raw_readl(LPC32XX_CLKPWR_MS_CTRL);
744 __raw_writel(oldclk | LPC32XX_CLKPWR_MSCARD_SDCARD_EN,
745 LPC32XX_CLKPWR_MS_CTRL);
746 div = __raw_readl(LPC32XX_CLKPWR_MS_CTRL);
747 __raw_writel(oldclk, LPC32XX_CLKPWR_MS_CTRL);
749 /* Get the parent clock rate */
750 rate = clk->parent->get_rate(clk->parent);
752 /* Get the MMC controller clock divider value */
753 div = div & LPC32XX_CLKPWR_MSCARD_SDCARD_DIV(0xf);
755 if (!div)
756 div = 1;
758 return rate / div;
761 static unsigned long mmc_round_rate(struct clk *clk, unsigned long rate)
763 unsigned long div, prate;
765 /* Get the parent clock rate */
766 prate = clk->parent->get_rate(clk->parent);
768 if (rate >= prate)
769 return prate;
771 div = prate / rate;
772 if (div > 0xf)
773 div = 0xf;
775 return prate / div;
778 static int mmc_set_rate(struct clk *clk, unsigned long rate)
780 u32 oldclk, tmp;
781 unsigned long prate, div, crate = mmc_round_rate(clk, rate);
783 prate = clk->parent->get_rate(clk->parent);
785 div = prate / crate;
787 /* The MMC clock must be on when accessing an MMC register */
788 oldclk = __raw_readl(LPC32XX_CLKPWR_MS_CTRL);
789 __raw_writel(oldclk | LPC32XX_CLKPWR_MSCARD_SDCARD_EN,
790 LPC32XX_CLKPWR_MS_CTRL);
791 tmp = __raw_readl(LPC32XX_CLKPWR_MS_CTRL) &
792 ~LPC32XX_CLKPWR_MSCARD_SDCARD_DIV(0xf);
793 tmp |= LPC32XX_CLKPWR_MSCARD_SDCARD_DIV(div);
794 __raw_writel(tmp, LPC32XX_CLKPWR_MS_CTRL);
796 __raw_writel(oldclk, LPC32XX_CLKPWR_MS_CTRL);
798 return 0;
801 static struct clk clk_mmc = {
802 .parent = &clk_armpll,
803 .set_rate = mmc_set_rate,
804 .get_rate = mmc_get_rate,
805 .round_rate = mmc_round_rate,
806 .enable = mmc_onoff_enable,
807 .enable_reg = LPC32XX_CLKPWR_MS_CTRL,
808 .enable_mask = LPC32XX_CLKPWR_MSCARD_SDCARD_EN,
811 static unsigned long clcd_get_rate(struct clk *clk)
813 u32 tmp, div, rate, oldclk;
815 /* The LCD clock must be on when accessing an LCD register */
816 oldclk = __raw_readl(LPC32XX_CLKPWR_LCDCLK_CTRL);
817 __raw_writel(oldclk | LPC32XX_CLKPWR_LCDCTRL_CLK_EN,
818 LPC32XX_CLKPWR_LCDCLK_CTRL);
819 tmp = __raw_readl(io_p2v(LPC32XX_LCD_BASE + CLCD_TIM2));
820 __raw_writel(oldclk, LPC32XX_CLKPWR_LCDCLK_CTRL);
822 rate = clk->parent->get_rate(clk->parent);
824 /* Only supports internal clocking */
825 if (tmp & TIM2_BCD)
826 return rate;
828 div = (tmp & 0x1F) | ((tmp & 0xF8) >> 22);
829 tmp = rate / (2 + div);
831 return tmp;
834 static int clcd_set_rate(struct clk *clk, unsigned long rate)
836 u32 tmp, prate, div, oldclk;
838 /* The LCD clock must be on when accessing an LCD register */
839 oldclk = __raw_readl(LPC32XX_CLKPWR_LCDCLK_CTRL);
840 __raw_writel(oldclk | LPC32XX_CLKPWR_LCDCTRL_CLK_EN,
841 LPC32XX_CLKPWR_LCDCLK_CTRL);
843 tmp = __raw_readl(io_p2v(LPC32XX_LCD_BASE + CLCD_TIM2)) | TIM2_BCD;
844 prate = clk->parent->get_rate(clk->parent);
846 if (rate < prate) {
847 /* Find closest divider */
848 div = prate / rate;
849 if (div >= 2) {
850 div -= 2;
851 tmp &= ~TIM2_BCD;
854 tmp &= ~(0xF800001F);
855 tmp |= (div & 0x1F);
856 tmp |= (((div >> 5) & 0x1F) << 27);
859 __raw_writel(tmp, io_p2v(LPC32XX_LCD_BASE + CLCD_TIM2));
860 __raw_writel(oldclk, LPC32XX_CLKPWR_LCDCLK_CTRL);
862 return 0;
865 static unsigned long clcd_round_rate(struct clk *clk, unsigned long rate)
867 u32 prate, div;
869 prate = clk->parent->get_rate(clk->parent);
871 if (rate >= prate)
872 rate = prate;
873 else {
874 div = prate / rate;
875 if (div > 0x3ff)
876 div = 0x3ff;
878 rate = prate / div;
881 return rate;
884 static struct clk clk_lcd = {
885 .parent = &clk_hclk,
886 .set_rate = clcd_set_rate,
887 .get_rate = clcd_get_rate,
888 .round_rate = clcd_round_rate,
889 .enable = local_onoff_enable,
890 .enable_reg = LPC32XX_CLKPWR_LCDCLK_CTRL,
891 .enable_mask = LPC32XX_CLKPWR_LCDCTRL_CLK_EN,
894 static inline void clk_lock(void)
896 mutex_lock(&clkm_lock);
899 static inline void clk_unlock(void)
901 mutex_unlock(&clkm_lock);
904 static void local_clk_disable(struct clk *clk)
906 WARN_ON(clk->usecount == 0);
908 /* Don't attempt to disable clock if it has no users */
909 if (clk->usecount > 0) {
910 clk->usecount--;
912 /* Only disable clock when it has no more users */
913 if ((clk->usecount == 0) && (clk->enable))
914 clk->enable(clk, 0);
916 /* Check parent clocks, they may need to be disabled too */
917 if (clk->parent)
918 local_clk_disable(clk->parent);
922 static int local_clk_enable(struct clk *clk)
924 int ret = 0;
926 /* Enable parent clocks first and update use counts */
927 if (clk->parent)
928 ret = local_clk_enable(clk->parent);
930 if (!ret) {
931 /* Only enable clock if it's currently disabled */
932 if ((clk->usecount == 0) && (clk->enable))
933 ret = clk->enable(clk, 1);
935 if (!ret)
936 clk->usecount++;
937 else if (clk->parent)
938 local_clk_disable(clk->parent);
941 return ret;
945 * clk_enable - inform the system when the clock source should be running.
947 int clk_enable(struct clk *clk)
949 int ret;
951 clk_lock();
952 ret = local_clk_enable(clk);
953 clk_unlock();
955 return ret;
957 EXPORT_SYMBOL(clk_enable);
960 * clk_disable - inform the system when the clock source is no longer required
962 void clk_disable(struct clk *clk)
964 clk_lock();
965 local_clk_disable(clk);
966 clk_unlock();
968 EXPORT_SYMBOL(clk_disable);
971 * clk_get_rate - obtain the current clock rate (in Hz) for a clock source
973 unsigned long clk_get_rate(struct clk *clk)
975 unsigned long rate;
977 clk_lock();
978 rate = clk->get_rate(clk);
979 clk_unlock();
981 return rate;
983 EXPORT_SYMBOL(clk_get_rate);
986 * clk_set_rate - set the clock rate for a clock source
988 int clk_set_rate(struct clk *clk, unsigned long rate)
990 int ret = -EINVAL;
993 * Most system clocks can only be enabled or disabled, with
994 * the actual rate set as part of the peripheral dividers
995 * instead of high level clock control
997 if (clk->set_rate) {
998 clk_lock();
999 ret = clk->set_rate(clk, rate);
1000 clk_unlock();
1003 return ret;
1005 EXPORT_SYMBOL(clk_set_rate);
1008 * clk_round_rate - adjust a rate to the exact rate a clock can provide
1010 long clk_round_rate(struct clk *clk, unsigned long rate)
1012 clk_lock();
1014 if (clk->round_rate)
1015 rate = clk->round_rate(clk, rate);
1016 else
1017 rate = clk->get_rate(clk);
1019 clk_unlock();
1021 return rate;
1023 EXPORT_SYMBOL(clk_round_rate);
1026 * clk_set_parent - set the parent clock source for this clock
1028 int clk_set_parent(struct clk *clk, struct clk *parent)
1030 /* Clock re-parenting is not supported */
1031 return -EINVAL;
1033 EXPORT_SYMBOL(clk_set_parent);
1036 * clk_get_parent - get the parent clock source for this clock
1038 struct clk *clk_get_parent(struct clk *clk)
1040 return clk->parent;
1042 EXPORT_SYMBOL(clk_get_parent);
1044 #define _REGISTER_CLOCK(d, n, c) \
1046 .dev_id = (d), \
1047 .con_id = (n), \
1048 .clk = &(c), \
1051 static struct clk_lookup lookups[] = {
1052 _REGISTER_CLOCK(NULL, "osc_32KHz", osc_32KHz)
1053 _REGISTER_CLOCK(NULL, "osc_pll397", osc_pll397)
1054 _REGISTER_CLOCK(NULL, "osc_main", osc_main)
1055 _REGISTER_CLOCK(NULL, "sys_ck", clk_sys)
1056 _REGISTER_CLOCK(NULL, "arm_pll_ck", clk_armpll)
1057 _REGISTER_CLOCK(NULL, "ck_pll5", clk_usbpll)
1058 _REGISTER_CLOCK(NULL, "hclk_ck", clk_hclk)
1059 _REGISTER_CLOCK(NULL, "pclk_ck", clk_pclk)
1060 _REGISTER_CLOCK(NULL, "timer0_ck", clk_timer0)
1061 _REGISTER_CLOCK(NULL, "timer1_ck", clk_timer1)
1062 _REGISTER_CLOCK(NULL, "timer2_ck", clk_timer2)
1063 _REGISTER_CLOCK(NULL, "timer3_ck", clk_timer3)
1064 _REGISTER_CLOCK(NULL, "vfp9_ck", clk_vfp9)
1065 _REGISTER_CLOCK(NULL, "clk_dmac", clk_dma)
1066 _REGISTER_CLOCK("pnx4008-watchdog", NULL, clk_wdt)
1067 _REGISTER_CLOCK(NULL, "uart3_ck", clk_uart3)
1068 _REGISTER_CLOCK(NULL, "uart4_ck", clk_uart4)
1069 _REGISTER_CLOCK(NULL, "uart5_ck", clk_uart5)
1070 _REGISTER_CLOCK(NULL, "uart6_ck", clk_uart6)
1071 _REGISTER_CLOCK("pnx-i2c.0", NULL, clk_i2c0)
1072 _REGISTER_CLOCK("pnx-i2c.1", NULL, clk_i2c1)
1073 _REGISTER_CLOCK("pnx-i2c.2", NULL, clk_i2c2)
1074 _REGISTER_CLOCK("dev:ssp0", NULL, clk_ssp0)
1075 _REGISTER_CLOCK("dev:ssp1", NULL, clk_ssp1)
1076 _REGISTER_CLOCK("lpc32xx_keys.0", NULL, clk_kscan)
1077 _REGISTER_CLOCK("lpc32xx-nand.0", "nand_ck", clk_nand)
1078 _REGISTER_CLOCK("tbd", "i2s0_ck", clk_i2s0)
1079 _REGISTER_CLOCK("tbd", "i2s1_ck", clk_i2s1)
1080 _REGISTER_CLOCK("ts-lpc32xx", NULL, clk_tsc)
1081 _REGISTER_CLOCK("dev:mmc0", "MCLK", clk_mmc)
1082 _REGISTER_CLOCK("lpc-net.0", NULL, clk_net)
1083 _REGISTER_CLOCK("dev:clcd", NULL, clk_lcd)
1084 _REGISTER_CLOCK("lpc32xx_udc", "ck_usbd", clk_usbd)
1085 _REGISTER_CLOCK("lpc32xx_rtc", NULL, clk_rtc)
1088 static int __init clk_init(void)
1090 int i;
1092 for (i = 0; i < ARRAY_SIZE(lookups); i++)
1093 clkdev_add(&lookups[i]);
1096 * Setup muxed SYSCLK for HCLK PLL base -this selects the
1097 * parent clock used for the ARM PLL and is used to derive
1098 * the many system clock rates in the device.
1100 if (clk_is_sysclk_mainosc() != 0)
1101 clk_sys.parent = &osc_main;
1102 else
1103 clk_sys.parent = &osc_pll397;
1105 clk_sys.rate = clk_sys.parent->rate;
1107 /* Compute the current ARM PLL and USB PLL frequencies */
1108 local_update_armpll_rate();
1110 /* Compute HCLK and PCLK bus rates */
1111 clk_hclk.rate = clk_hclk.parent->rate / clk_get_hclk_div();
1112 clk_pclk.rate = clk_pclk.parent->rate / clk_get_pclk_div();
1115 * Enable system clocks - this step is somewhat formal, as the
1116 * clocks are already running, but it does get the clock data
1117 * inline with the actual system state. Never disable these
1118 * clocks as they will only stop if the system is going to sleep.
1119 * In that case, the chip/system power management functions will
1120 * handle clock gating.
1122 if (clk_enable(&clk_hclk) || clk_enable(&clk_pclk))
1123 printk(KERN_ERR "Error enabling system HCLK and PCLK\n");
1126 * Timers 0 and 1 were enabled and are being used by the high
1127 * resolution tick function prior to this driver being initialized.
1128 * Tag them now as used.
1130 if (clk_enable(&clk_timer0) || clk_enable(&clk_timer1))
1131 printk(KERN_ERR "Error enabling timer tick clocks\n");
1133 return 0;
1135 core_initcall(clk_init);