2 * arch/arm/mach-pnx4008/clock.c
4 * Clock control driver for PNX4008
6 * Authors: Vitaly Wool, Dmitry Chigirev <source@mvista.com>
7 * Generic clock management functions are partially based on:
8 * linux/arch/arm/mach-omap/clock.c
10 * 2005-2006 (c) MontaVista Software, Inc. This file is licensed under
11 * the terms of the GNU General Public License version 2. This program
12 * is licensed "as is" without any warranty of any kind, whether express
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/errno.h>
20 #include <linux/device.h>
21 #include <linux/err.h>
22 #include <linux/delay.h>
25 #include <mach/hardware.h>
27 #include <mach/clock.h>
30 /*forward declaration*/
31 static struct clk per_ck;
32 static struct clk hclk_ck;
33 static struct clk ck_1MHz;
34 static struct clk ck_13MHz;
35 static struct clk ck_pll1;
36 static int local_set_rate(struct clk *clk, u32 rate);
38 static inline void clock_lock(void)
43 static inline void clock_unlock(void)
48 static void propagate_rate(struct clk *clk)
53 while (tmp_clk->propagate_next) {
54 tmp_clk = tmp_clk->propagate_next;
55 local_set_rate(tmp_clk, tmp_clk->user_rate);
59 static inline void clk_reg_disable(struct clk *clk)
62 __raw_writel(__raw_readl(clk->enable_reg) &
63 ~(1 << clk->enable_shift), clk->enable_reg);
66 static inline void clk_reg_enable(struct clk *clk)
69 __raw_writel(__raw_readl(clk->enable_reg) |
70 (1 << clk->enable_shift), clk->enable_reg);
73 static inline void clk_reg_disable1(struct clk *clk)
76 __raw_writel(__raw_readl(clk->enable_reg1) &
77 ~(1 << clk->enable_shift1), clk->enable_reg1);
80 static inline void clk_reg_enable1(struct clk *clk)
83 __raw_writel(__raw_readl(clk->enable_reg1) |
84 (1 << clk->enable_shift1), clk->enable_reg1);
87 static int clk_wait_for_pll_lock(struct clk *clk)
91 while (i++ < 0xFFF && !(__raw_readl(clk->scale_reg) & 1)) ; /*wait for PLL to lock */
93 if (!(__raw_readl(clk->scale_reg) & 1)) {
95 "%s ERROR: failed to lock, scale reg data: %x\n",
96 clk->name, __raw_readl(clk->scale_reg));
102 static int switch_to_dirty_13mhz(struct clk *clk)
111 clk_reg_enable1(clk);
113 tmp_reg = __raw_readl(clk->parent_switch_reg);
114 /*if 13Mhz clock selected, select 13'MHz (dirty) source from OSC */
115 if (!(tmp_reg & 1)) {
116 tmp_reg |= (1 << 1); /* Trigger switch to 13'MHz (dirty) clock */
117 __raw_writel(tmp_reg, clk->parent_switch_reg);
119 while (i++ < 0xFFF && !(__raw_readl(clk->parent_switch_reg) & 1)) ; /*wait for 13'MHz selection status */
121 if (!(__raw_readl(clk->parent_switch_reg) & 1)) {
123 "%s ERROR: failed to select 13'MHz, parent sw reg data: %x\n",
124 clk->name, __raw_readl(clk->parent_switch_reg));
130 clk_reg_disable1(clk);
135 static int switch_to_clean_13mhz(struct clk *clk)
144 clk_reg_enable1(clk);
146 tmp_reg = __raw_readl(clk->parent_switch_reg);
147 /*if 13'Mhz clock selected, select 13MHz (clean) source from OSC */
149 tmp_reg &= ~(1 << 1); /* Trigger switch to 13MHz (clean) clock */
150 __raw_writel(tmp_reg, clk->parent_switch_reg);
152 while (i++ < 0xFFF && (__raw_readl(clk->parent_switch_reg) & 1)) ; /*wait for 13MHz selection status */
154 if (__raw_readl(clk->parent_switch_reg) & 1) {
156 "%s ERROR: failed to select 13MHz, parent sw reg data: %x\n",
157 clk->name, __raw_readl(clk->parent_switch_reg));
163 clk_reg_disable1(clk);
168 static int set_13MHz_parent(struct clk *clk, struct clk *parent)
172 if (parent == &ck_13MHz)
173 ret = switch_to_clean_13mhz(clk);
174 else if (parent == &ck_pll1)
175 ret = switch_to_dirty_13mhz(clk);
180 #define PLL160_MIN_FCCO 156000
181 #define PLL160_MAX_FCCO 320000
184 * Calculate pll160 settings.
185 * Possible input: up to 320MHz with step of clk->parent->rate.
186 * In PNX4008 parent rate for pll160s may be either 1 or 13MHz.
187 * Ignored paths: "feedback" (bit 13 set), "div-by-N".
188 * Setting ARM PLL4 rate to 0 will put CPU into direct run mode.
189 * Setting PLL5 and PLL3 rate to 0 will disable USB and DSP clock input.
190 * Please refer to PNX4008 IC manual for details.
193 static int pll160_set_rate(struct clk *clk, u32 rate)
195 u32 tmp_reg, tmp_m, tmp_2p, i;
199 parent_rate = clk->parent->rate;
204 /* set direct run for ARM or disable output for others */
205 clk_reg_disable(clk);
207 /* disable source input as well (ignored for ARM) */
208 clk_reg_disable1(clk);
210 tmp_reg = __raw_readl(clk->scale_reg);
211 tmp_reg &= ~0x1ffff; /*clear all settings, power down */
212 __raw_writel(tmp_reg, clk->scale_reg);
214 rate -= rate % parent_rate; /*round down the input */
216 if (rate > PLL160_MAX_FCCO)
217 rate = PLL160_MAX_FCCO;
225 clk_reg_enable1(clk);
226 tmp_reg = __raw_readl(clk->scale_reg);
228 if (rate == parent_rate) {
229 /*enter direct bypass mode */
230 tmp_reg |= ((1 << 14) | (1 << 15));
231 __raw_writel(tmp_reg, clk->scale_reg);
232 clk->rate = parent_rate;
239 for (tmp_2p = 1; tmp_2p < 16; tmp_2p <<= 1) {
240 if (rate * tmp_2p >= PLL160_MIN_FCCO)
246 tmp_reg |= ((i - 1) << 11);
248 tmp_reg |= (1 << 14); /*direct mode, no divide */
250 tmp_m = rate * tmp_2p;
251 tmp_m /= parent_rate;
253 tmp_reg |= (tmp_m - 1) << 1; /*calculate M */
254 tmp_reg |= (1 << 16); /*power up PLL */
255 __raw_writel(tmp_reg, clk->scale_reg);
257 if (clk_wait_for_pll_lock(clk) < 0) {
258 clk_reg_disable(clk);
259 clk_reg_disable1(clk);
261 tmp_reg = __raw_readl(clk->scale_reg);
262 tmp_reg &= ~0x1ffff; /*clear all settings, power down */
263 __raw_writel(tmp_reg, clk->scale_reg);
269 clk->rate = (tmp_m * parent_rate) / tmp_2p;
271 if (clk->flags & RATE_PROPAGATES)
281 /*configure PER_CLK*/
282 static int per_clk_set_rate(struct clk *clk, u32 rate)
286 tmp = __raw_readl(clk->scale_reg);
288 tmp |= ((clk->parent->rate / clk->rate) - 1) << 2;
289 __raw_writel(tmp, clk->scale_reg);
295 static int hclk_set_rate(struct clk *clk, u32 rate)
298 tmp = __raw_readl(clk->scale_reg);
311 __raw_writel(tmp, clk->scale_reg);
316 static u32 hclk_round_rate(struct clk *clk, u32 rate)
326 static u32 per_clk_round_rate(struct clk *clk, u32 rate)
328 return CLK_RATE_13MHZ;
331 static int on_off_set_rate(struct clk *clk, u32 rate)
337 clk_reg_disable(clk);
343 static int on_off_inv_set_rate(struct clk *clk, u32 rate)
346 clk_reg_disable(clk); /*enable bit is inverted */
355 static u32 on_off_round_rate(struct clk *clk, u32 rate)
357 return (rate ? 1 : 0);
360 static u32 pll4_round_rate(struct clk *clk, u32 rate)
362 if (rate > CLK_RATE_208MHZ)
363 rate = CLK_RATE_208MHZ;
364 if (rate == CLK_RATE_208MHZ && hclk_ck.user_rate == 1)
365 rate = CLK_RATE_208MHZ - CLK_RATE_13MHZ;
366 return (rate - (rate % (hclk_ck.user_rate * CLK_RATE_13MHZ)));
369 static u32 pll3_round_rate(struct clk *clk, u32 rate)
371 if (rate > CLK_RATE_208MHZ)
372 rate = CLK_RATE_208MHZ;
373 return (rate - rate % CLK_RATE_13MHZ);
376 static u32 pll5_round_rate(struct clk *clk, u32 rate)
378 return (rate ? CLK_RATE_48MHZ : 0);
381 static u32 ck_13MHz_round_rate(struct clk *clk, u32 rate)
383 return (rate ? CLK_RATE_13MHZ : 0);
386 static int ck_13MHz_set_rate(struct clk *clk, u32 rate)
389 clk_reg_disable(clk); /*enable bit is inverted */
391 clk->rate = CLK_RATE_13MHZ;
392 ck_1MHz.rate = CLK_RATE_1MHZ;
401 static int pll1_set_rate(struct clk *clk, u32 rate)
403 #if 0 /* doesn't work on some boards, probably a HW BUG */
405 clk_reg_disable(clk); /*enable bit is inverted */
406 if (!clk_wait_for_pll_lock(clk)) {
407 clk->rate = CLK_RATE_13MHZ;
423 static struct clk osc_13MHz = {
426 .rate = CLK_RATE_13MHZ,
429 static struct clk ck_13MHz = {
431 .parent = &osc_13MHz,
432 .flags = NEEDS_INITIALIZATION,
433 .round_rate = &ck_13MHz_round_rate,
434 .set_rate = &ck_13MHz_set_rate,
435 .enable_reg = OSC13CTRL_REG,
437 .rate = CLK_RATE_13MHZ,
440 static struct clk osc_32KHz = {
443 .rate = CLK_RATE_32KHZ,
447 static struct clk ck_1MHz = {
449 .flags = FIXED_RATE | PARENT_SET_RATE,
453 /* PLL1 (397) - provides 13' MHz clock */
454 static struct clk ck_pll1 = {
456 .parent = &osc_32KHz,
457 .flags = NEEDS_INITIALIZATION,
458 .round_rate = &ck_13MHz_round_rate,
459 .set_rate = &pll1_set_rate,
460 .enable_reg = PLLCTRL_REG,
462 .scale_reg = PLLCTRL_REG,
463 .rate = CLK_RATE_13MHZ,
467 static struct clk ck_pll4 = {
470 .flags = RATE_PROPAGATES | NEEDS_INITIALIZATION,
471 .propagate_next = &per_ck,
472 .round_rate = &pll4_round_rate,
473 .set_rate = &pll160_set_rate,
474 .rate = CLK_RATE_208MHZ,
475 .scale_reg = HCLKPLLCTRL_REG,
476 .enable_reg = PWRCTRL_REG,
478 .parent_switch_reg = SYSCLKCTRL_REG,
479 .set_parent = &set_13MHz_parent,
483 static struct clk ck_pll5 = {
486 .flags = NEEDS_INITIALIZATION,
487 .round_rate = &pll5_round_rate,
488 .set_rate = &pll160_set_rate,
489 .scale_reg = USBCTRL_REG,
490 .enable_reg = USBCTRL_REG,
492 .enable_reg1 = USBCTRL_REG,
496 /* XPERTTeak DSP PLL */
497 static struct clk ck_pll3 = {
500 .flags = NEEDS_INITIALIZATION,
501 .round_rate = &pll3_round_rate,
502 .set_rate = &pll160_set_rate,
503 .scale_reg = DSPPLLCTRL_REG,
504 .enable_reg = DSPCLKCTRL_REG,
506 .enable_reg1 = DSPCLKCTRL_REG,
508 .parent_switch_reg = DSPCLKCTRL_REG,
509 .set_parent = &set_13MHz_parent,
512 static struct clk hclk_ck = {
515 .flags = PARENT_SET_RATE,
516 .set_rate = &hclk_set_rate,
517 .round_rate = &hclk_round_rate,
518 .scale_reg = HCLKDIVCTRL_REG,
523 static struct clk per_ck = {
527 .propagate_next = &hclk_ck,
528 .set_rate = &per_clk_set_rate,
529 .round_rate = &per_clk_round_rate,
530 .scale_reg = HCLKDIVCTRL_REG,
531 .rate = CLK_RATE_13MHZ,
532 .user_rate = CLK_RATE_13MHZ,
535 static struct clk m2hclk_ck = {
538 .flags = NEEDS_INITIALIZATION,
539 .round_rate = &on_off_round_rate,
540 .set_rate = &on_off_inv_set_rate,
543 .enable_reg = PWRCTRL_REG,
546 static struct clk vfp9_ck = {
549 .flags = NEEDS_INITIALIZATION,
550 .round_rate = &on_off_round_rate,
551 .set_rate = &on_off_set_rate,
554 .enable_reg = VFP9CLKCTRL_REG,
557 static struct clk keyscan_ck = {
558 .name = "keyscan_ck",
559 .parent = &osc_32KHz,
560 .flags = NEEDS_INITIALIZATION,
561 .round_rate = &on_off_round_rate,
562 .set_rate = &on_off_set_rate,
564 .enable_reg = KEYCLKCTRL_REG,
567 static struct clk touch_ck = {
569 .parent = &osc_32KHz,
570 .flags = NEEDS_INITIALIZATION,
571 .round_rate = &on_off_round_rate,
572 .set_rate = &on_off_set_rate,
574 .enable_reg = TSCLKCTRL_REG,
577 static struct clk pwm1_ck = {
579 .parent = &osc_32KHz,
580 .flags = NEEDS_INITIALIZATION,
581 .round_rate = &on_off_round_rate,
582 .set_rate = &on_off_set_rate,
584 .enable_reg = PWMCLKCTRL_REG,
587 static struct clk pwm2_ck = {
589 .parent = &osc_32KHz,
590 .flags = NEEDS_INITIALIZATION,
591 .round_rate = &on_off_round_rate,
592 .set_rate = &on_off_set_rate,
594 .enable_reg = PWMCLKCTRL_REG,
597 static struct clk jpeg_ck = {
600 .flags = NEEDS_INITIALIZATION,
601 .round_rate = &on_off_round_rate,
602 .set_rate = &on_off_set_rate,
604 .enable_reg = JPEGCLKCTRL_REG,
607 static struct clk ms_ck = {
610 .flags = NEEDS_INITIALIZATION,
611 .round_rate = &on_off_round_rate,
612 .set_rate = &on_off_set_rate,
614 .enable_reg = MSCTRL_REG,
617 static struct clk dum_ck = {
620 .flags = NEEDS_INITIALIZATION,
621 .round_rate = &on_off_round_rate,
622 .set_rate = &on_off_set_rate,
624 .enable_reg = DUMCLKCTRL_REG,
627 static struct clk flash_ck = {
630 .round_rate = &on_off_round_rate,
631 .set_rate = &on_off_set_rate,
632 .enable_shift = 1, /* Only MLC clock supported */
633 .enable_reg = FLASHCLKCTRL_REG,
636 static struct clk i2c0_ck = {
639 .flags = NEEDS_INITIALIZATION,
640 .round_rate = &on_off_round_rate,
641 .set_rate = &on_off_set_rate,
643 .enable_reg = I2CCLKCTRL_REG,
646 static struct clk i2c1_ck = {
649 .flags = NEEDS_INITIALIZATION,
650 .round_rate = &on_off_round_rate,
651 .set_rate = &on_off_set_rate,
653 .enable_reg = I2CCLKCTRL_REG,
656 static struct clk i2c2_ck = {
659 .flags = NEEDS_INITIALIZATION,
660 .round_rate = &on_off_round_rate,
661 .set_rate = &on_off_set_rate,
663 .enable_reg = USB_OTG_CLKCTRL_REG,
666 static struct clk spi0_ck = {
669 .flags = NEEDS_INITIALIZATION,
670 .round_rate = &on_off_round_rate,
671 .set_rate = &on_off_set_rate,
673 .enable_reg = SPICTRL_REG,
676 static struct clk spi1_ck = {
679 .flags = NEEDS_INITIALIZATION,
680 .round_rate = &on_off_round_rate,
681 .set_rate = &on_off_set_rate,
683 .enable_reg = SPICTRL_REG,
686 static struct clk dma_ck = {
689 .round_rate = &on_off_round_rate,
690 .set_rate = &on_off_set_rate,
692 .enable_reg = DMACLKCTRL_REG,
695 static struct clk uart3_ck = {
698 .flags = NEEDS_INITIALIZATION,
699 .round_rate = &on_off_round_rate,
700 .set_rate = &on_off_set_rate,
703 .enable_reg = UARTCLKCTRL_REG,
706 static struct clk uart4_ck = {
709 .flags = NEEDS_INITIALIZATION,
710 .round_rate = &on_off_round_rate,
711 .set_rate = &on_off_set_rate,
713 .enable_reg = UARTCLKCTRL_REG,
716 static struct clk uart5_ck = {
719 .flags = NEEDS_INITIALIZATION,
720 .round_rate = &on_off_round_rate,
721 .set_rate = &on_off_set_rate,
724 .enable_reg = UARTCLKCTRL_REG,
727 static struct clk uart6_ck = {
730 .flags = NEEDS_INITIALIZATION,
731 .round_rate = &on_off_round_rate,
732 .set_rate = &on_off_set_rate,
734 .enable_reg = UARTCLKCTRL_REG,
737 static struct clk wdt_ck = {
740 .flags = NEEDS_INITIALIZATION,
741 .round_rate = &on_off_round_rate,
742 .set_rate = &on_off_set_rate,
744 .enable_reg = TIMCLKCTRL_REG,
747 /* These clocks are visible outside this module
748 * and can be initialized
750 static struct clk *onchip_clks[] = {
780 static int local_clk_enable(struct clk *clk)
784 if (!(clk->flags & FIXED_RATE) && !clk->rate && clk->set_rate
786 ret = clk->set_rate(clk, clk->user_rate);
790 static void local_clk_disable(struct clk *clk)
792 if (!(clk->flags & FIXED_RATE) && clk->rate && clk->set_rate)
793 clk->set_rate(clk, 0);
796 static void local_clk_unuse(struct clk *clk)
798 if (clk->usecount > 0 && !(--clk->usecount)) {
799 local_clk_disable(clk);
801 local_clk_unuse(clk->parent);
805 static int local_clk_use(struct clk *clk)
808 if (clk->usecount++ == 0) {
810 ret = local_clk_use(clk->parent);
817 ret = local_clk_enable(clk);
819 if (ret != 0 && clk->parent) {
820 local_clk_unuse(clk->parent);
828 static int local_set_rate(struct clk *clk, u32 rate)
833 if (clk->user_rate == clk->rate && clk->parent->rate) {
834 /* if clock enabled or rate not set */
835 clk->user_rate = clk->round_rate(clk, rate);
836 ret = clk->set_rate(clk, clk->user_rate);
838 clk->user_rate = clk->round_rate(clk, rate);
844 int clk_set_rate(struct clk *clk, unsigned long rate)
848 if (clk->flags & FIXED_RATE)
852 if ((clk->flags & PARENT_SET_RATE) && clk->parent) {
854 clk->user_rate = clk->round_rate(clk, rate);
855 /* parent clock needs to be refreshed
856 for the setting to take effect */
858 ret = local_set_rate(clk, rate);
867 EXPORT_SYMBOL(clk_set_rate);
869 struct clk *clk_get(struct device *dev, const char *id)
871 struct clk *clk = ERR_PTR(-ENOENT);
875 for (clkp = onchip_clks; clkp < onchip_clks + ARRAY_SIZE(onchip_clks);
877 if (strcmp(id, (*clkp)->name) == 0
878 && try_module_get((*clkp)->owner)) {
887 EXPORT_SYMBOL(clk_get);
889 void clk_put(struct clk *clk)
892 if (clk && !IS_ERR(clk))
893 module_put(clk->owner);
896 EXPORT_SYMBOL(clk_put);
898 unsigned long clk_get_rate(struct clk *clk)
906 EXPORT_SYMBOL(clk_get_rate);
908 int clk_enable(struct clk *clk)
913 ret = local_clk_use(clk);
918 EXPORT_SYMBOL(clk_enable);
920 void clk_disable(struct clk *clk)
923 local_clk_unuse(clk);
927 EXPORT_SYMBOL(clk_disable);
929 long clk_round_rate(struct clk *clk, unsigned long rate)
934 ret = clk->round_rate(clk, rate);
941 EXPORT_SYMBOL(clk_round_rate);
943 int clk_set_parent(struct clk *clk, struct clk *parent)
946 if (!clk->set_parent)
950 ret = clk->set_parent(clk, parent);
952 clk->parent = parent;
959 EXPORT_SYMBOL(clk_set_parent);
961 static int __init clk_init(void)
965 /* Disable autoclocking, as it doesn't seem to work */
966 __raw_writel(0xff, AUTOCLK_CTRL);
968 for (clkp = onchip_clks; clkp < onchip_clks + ARRAY_SIZE(onchip_clks);
970 if (((*clkp)->flags & NEEDS_INITIALIZATION)
971 && ((*clkp)->set_rate)) {
972 (*clkp)->user_rate = (*clkp)->rate;
973 local_set_rate((*clkp), (*clkp)->user_rate);
974 if ((*clkp)->set_parent)
975 (*clkp)->set_parent((*clkp), (*clkp)->parent);
977 pr_debug("%s: clock %s, rate %ld\n",
978 __func__, (*clkp)->name, (*clkp)->rate);
981 local_clk_use(&ck_pll4);
983 /* if ck_13MHz is not used, disable it. */
984 if (ck_13MHz.usecount == 0)
985 local_clk_disable(&ck_13MHz);
987 /* Disable autoclocking */
988 __raw_writeb(0xff, AUTOCLK_CTRL);
993 arch_initcall(clk_init);