1 //kernel/linux-omap-fsample/arch/arm/mach-omap1/clock.c#2 - edit change 3808 (text)
3 * linux/arch/arm/mach-omap1/clock.c
5 * Copyright (C) 2004 - 2005 Nokia corporation
6 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
8 * Modified to use omap shared clock framework by
9 * Tony Lindgren <tony@atomide.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/clk.h>
23 #include <asm/mach-types.h>
25 #include <asm/arch/cpu.h>
26 #include <asm/arch/usb.h>
27 #include <asm/arch/clock.h>
28 #include <asm/arch/sram.h>
32 __u32 arm_idlect1_mask;
34 /*-------------------------------------------------------------------------
35 * Omap1 specific clock functions
36 *-------------------------------------------------------------------------*/
38 static void omap1_watchdog_recalc(struct clk * clk)
40 clk->rate = clk->parent->rate / 14;
43 static void omap1_uart_recalc(struct clk * clk)
45 unsigned int val = omap_readl(clk->enable_reg);
46 if (val & clk->enable_bit)
52 static int omap1_clk_enable_dsp_domain(struct clk *clk)
56 retval = omap1_clk_enable(&api_ck.clk);
58 retval = omap1_clk_enable_generic(clk);
59 omap1_clk_disable(&api_ck.clk);
65 static void omap1_clk_disable_dsp_domain(struct clk *clk)
67 if (omap1_clk_enable(&api_ck.clk) == 0) {
68 omap1_clk_disable_generic(clk);
69 omap1_clk_disable(&api_ck.clk);
73 static int omap1_clk_enable_uart_functional(struct clk *clk)
76 struct uart_clk *uclk;
78 ret = omap1_clk_enable_generic(clk);
80 /* Set smart idle acknowledgement mode */
81 uclk = (struct uart_clk *)clk;
82 omap_writeb((omap_readb(uclk->sysc_addr) & ~0x10) | 8,
89 static void omap1_clk_disable_uart_functional(struct clk *clk)
91 struct uart_clk *uclk;
93 /* Set force idle acknowledgement mode */
94 uclk = (struct uart_clk *)clk;
95 omap_writeb((omap_readb(uclk->sysc_addr) & ~0x18), uclk->sysc_addr);
97 omap1_clk_disable_generic(clk);
100 static void omap1_clk_allow_idle(struct clk *clk)
102 struct arm_idlect1_clk * iclk = (struct arm_idlect1_clk *)clk;
104 if (!(clk->flags & CLOCK_IDLE_CONTROL))
107 if (iclk->no_idle_count > 0 && !(--iclk->no_idle_count))
108 arm_idlect1_mask |= 1 << iclk->idlect_shift;
111 static void omap1_clk_deny_idle(struct clk *clk)
113 struct arm_idlect1_clk * iclk = (struct arm_idlect1_clk *)clk;
115 if (!(clk->flags & CLOCK_IDLE_CONTROL))
118 if (iclk->no_idle_count++ == 0)
119 arm_idlect1_mask &= ~(1 << iclk->idlect_shift);
122 static __u16 verify_ckctl_value(__u16 newval)
124 /* This function checks for following limitations set
125 * by the hardware (all conditions must be true):
126 * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
131 * In addition following rules are enforced:
135 * However, maximum frequencies are not checked for!
144 per_exp = (newval >> CKCTL_PERDIV_OFFSET) & 3;
145 lcd_exp = (newval >> CKCTL_LCDDIV_OFFSET) & 3;
146 arm_exp = (newval >> CKCTL_ARMDIV_OFFSET) & 3;
147 dsp_exp = (newval >> CKCTL_DSPDIV_OFFSET) & 3;
148 tc_exp = (newval >> CKCTL_TCDIV_OFFSET) & 3;
149 dspmmu_exp = (newval >> CKCTL_DSPMMUDIV_OFFSET) & 3;
151 if (dspmmu_exp < dsp_exp)
152 dspmmu_exp = dsp_exp;
153 if (dspmmu_exp > dsp_exp+1)
154 dspmmu_exp = dsp_exp+1;
155 if (tc_exp < arm_exp)
157 if (tc_exp < dspmmu_exp)
159 if (tc_exp > lcd_exp)
161 if (tc_exp > per_exp)
165 newval |= per_exp << CKCTL_PERDIV_OFFSET;
166 newval |= lcd_exp << CKCTL_LCDDIV_OFFSET;
167 newval |= arm_exp << CKCTL_ARMDIV_OFFSET;
168 newval |= dsp_exp << CKCTL_DSPDIV_OFFSET;
169 newval |= tc_exp << CKCTL_TCDIV_OFFSET;
170 newval |= dspmmu_exp << CKCTL_DSPMMUDIV_OFFSET;
175 static int calc_dsor_exp(struct clk *clk, unsigned long rate)
177 /* Note: If target frequency is too low, this function will return 4,
178 * which is invalid value. Caller must check for this value and act
181 * Note: This function does not check for following limitations set
182 * by the hardware (all conditions must be true):
183 * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
188 unsigned long realrate;
192 if (unlikely(!(clk->flags & RATE_CKCTL)))
195 parent = clk->parent;
196 if (unlikely(parent == 0))
199 realrate = parent->rate;
200 for (dsor_exp=0; dsor_exp<4; dsor_exp++) {
201 if (realrate <= rate)
210 static void omap1_ckctl_recalc(struct clk * clk)
214 /* Calculate divisor encoded as 2-bit exponent */
215 dsor = 1 << (3 & (omap_readw(ARM_CKCTL) >> clk->rate_offset));
217 if (unlikely(clk->rate == clk->parent->rate / dsor))
218 return; /* No change, quick exit */
219 clk->rate = clk->parent->rate / dsor;
221 if (unlikely(clk->flags & RATE_PROPAGATES))
225 static void omap1_ckctl_recalc_dsp_domain(struct clk * clk)
229 /* Calculate divisor encoded as 2-bit exponent
231 * The clock control bits are in DSP domain,
232 * so api_ck is needed for access.
233 * Note that DSP_CKCTL virt addr = phys addr, so
234 * we must use __raw_readw() instead of omap_readw().
236 omap1_clk_enable(&api_ck.clk);
237 dsor = 1 << (3 & (__raw_readw(DSP_CKCTL) >> clk->rate_offset));
238 omap1_clk_disable(&api_ck.clk);
240 if (unlikely(clk->rate == clk->parent->rate / dsor))
241 return; /* No change, quick exit */
242 clk->rate = clk->parent->rate / dsor;
244 if (unlikely(clk->flags & RATE_PROPAGATES))
248 /* MPU virtual clock functions */
249 static int omap1_select_table_rate(struct clk * clk, unsigned long rate)
251 /* Find the highest supported frequency <= rate and switch to it */
252 struct mpu_rate * ptr;
254 if (clk != &virtual_ck_mpu)
257 for (ptr = rate_table; ptr->rate; ptr++) {
258 if (ptr->xtal != ck_ref.rate)
261 /* DPLL1 cannot be reprogrammed without risking system crash */
262 if (likely(ck_dpll1.rate!=0) && ptr->pll_rate != ck_dpll1.rate)
265 /* Can check only after xtal frequency check */
266 if (ptr->rate <= rate)
274 * In most cases we should not need to reprogram DPLL.
275 * Reprogramming the DPLL is tricky, it must be done from SRAM.
276 * (on 730, bit 13 must always be 1)
278 if (cpu_is_omap730())
279 omap_sram_reprogram_clock(ptr->dpllctl_val, ptr->ckctl_val | 0x2000);
281 omap_sram_reprogram_clock(ptr->dpllctl_val, ptr->ckctl_val);
283 ck_dpll1.rate = ptr->pll_rate;
284 propagate_rate(&ck_dpll1);
288 static int omap1_clk_set_rate_dsp_domain(struct clk *clk, unsigned long rate)
294 if (clk->flags & RATE_CKCTL) {
295 dsor_exp = calc_dsor_exp(clk, rate);
301 regval = __raw_readw(DSP_CKCTL);
302 regval &= ~(3 << clk->rate_offset);
303 regval |= dsor_exp << clk->rate_offset;
304 __raw_writew(regval, DSP_CKCTL);
305 clk->rate = clk->parent->rate / (1 << dsor_exp);
309 if (unlikely(ret == 0 && (clk->flags & RATE_PROPAGATES)))
315 static long omap1_round_to_table_rate(struct clk * clk, unsigned long rate)
317 /* Find the highest supported frequency <= rate */
318 struct mpu_rate * ptr;
321 if (clk != &virtual_ck_mpu)
324 highest_rate = -EINVAL;
326 for (ptr = rate_table; ptr->rate; ptr++) {
327 if (ptr->xtal != ck_ref.rate)
330 highest_rate = ptr->rate;
332 /* Can check only after xtal frequency check */
333 if (ptr->rate <= rate)
340 static unsigned calc_ext_dsor(unsigned long rate)
344 /* MCLK and BCLK divisor selection is not linear:
345 * freq = 96MHz / dsor
347 * RATIO_SEL range: dsor <-> RATIO_SEL
348 * 0..6: (RATIO_SEL+2) <-> (dsor-2)
349 * 6..48: (8+(RATIO_SEL-6)*2) <-> ((dsor-8)/2+6)
350 * Minimum dsor is 2 and maximum is 96. Odd divisors starting from 9
353 for (dsor = 2; dsor < 96; ++dsor) {
354 if ((dsor & 1) && dsor > 8)
356 if (rate >= 96000000 / dsor)
362 /* Only needed on 1510 */
363 static int omap1_set_uart_rate(struct clk * clk, unsigned long rate)
367 val = omap_readl(clk->enable_reg);
368 if (rate == 12000000)
369 val &= ~(1 << clk->enable_bit);
370 else if (rate == 48000000)
371 val |= (1 << clk->enable_bit);
374 omap_writel(val, clk->enable_reg);
380 /* External clock (MCLK & BCLK) functions */
381 static int omap1_set_ext_clk_rate(struct clk * clk, unsigned long rate)
386 dsor = calc_ext_dsor(rate);
387 clk->rate = 96000000 / dsor;
389 ratio_bits = ((dsor - 8) / 2 + 6) << 2;
391 ratio_bits = (dsor - 2) << 2;
393 ratio_bits |= omap_readw(clk->enable_reg) & ~0xfd;
394 omap_writew(ratio_bits, clk->enable_reg);
399 static long omap1_round_ext_clk_rate(struct clk * clk, unsigned long rate)
401 return 96000000 / calc_ext_dsor(rate);
404 static void omap1_init_ext_clk(struct clk * clk)
409 /* Determine current rate and ensure clock is based on 96MHz APLL */
410 ratio_bits = omap_readw(clk->enable_reg) & ~1;
411 omap_writew(ratio_bits, clk->enable_reg);
413 ratio_bits = (ratio_bits & 0xfc) >> 2;
415 dsor = (ratio_bits - 6) * 2 + 8;
417 dsor = ratio_bits + 2;
419 clk-> rate = 96000000 / dsor;
422 static int omap1_clk_enable(struct clk *clk)
425 if (clk->usecount++ == 0) {
426 if (likely(clk->parent)) {
427 ret = omap1_clk_enable(clk->parent);
429 if (unlikely(ret != 0)) {
434 if (clk->flags & CLOCK_NO_IDLE_PARENT)
435 omap1_clk_deny_idle(clk->parent);
438 ret = clk->enable(clk);
440 if (unlikely(ret != 0) && clk->parent) {
441 omap1_clk_disable(clk->parent);
449 static void omap1_clk_disable(struct clk *clk)
451 if (clk->usecount > 0 && !(--clk->usecount)) {
453 if (likely(clk->parent)) {
454 omap1_clk_disable(clk->parent);
455 if (clk->flags & CLOCK_NO_IDLE_PARENT)
456 omap1_clk_allow_idle(clk->parent);
461 static int omap1_clk_enable_generic(struct clk *clk)
466 if (clk->flags & ALWAYS_ENABLED)
469 if (unlikely(clk->enable_reg == 0)) {
470 printk(KERN_ERR "clock.c: Enable for %s without enable code\n",
475 if (clk->flags & ENABLE_REG_32BIT) {
476 if (clk->flags & VIRTUAL_IO_ADDRESS) {
477 regval32 = __raw_readl(clk->enable_reg);
478 regval32 |= (1 << clk->enable_bit);
479 __raw_writel(regval32, clk->enable_reg);
481 regval32 = omap_readl(clk->enable_reg);
482 regval32 |= (1 << clk->enable_bit);
483 omap_writel(regval32, clk->enable_reg);
486 if (clk->flags & VIRTUAL_IO_ADDRESS) {
487 regval16 = __raw_readw(clk->enable_reg);
488 regval16 |= (1 << clk->enable_bit);
489 __raw_writew(regval16, clk->enable_reg);
491 regval16 = omap_readw(clk->enable_reg);
492 regval16 |= (1 << clk->enable_bit);
493 omap_writew(regval16, clk->enable_reg);
500 static void omap1_clk_disable_generic(struct clk *clk)
505 if (clk->enable_reg == 0)
508 if (clk->flags & ENABLE_REG_32BIT) {
509 if (clk->flags & VIRTUAL_IO_ADDRESS) {
510 regval32 = __raw_readl(clk->enable_reg);
511 regval32 &= ~(1 << clk->enable_bit);
512 __raw_writel(regval32, clk->enable_reg);
514 regval32 = omap_readl(clk->enable_reg);
515 regval32 &= ~(1 << clk->enable_bit);
516 omap_writel(regval32, clk->enable_reg);
519 if (clk->flags & VIRTUAL_IO_ADDRESS) {
520 regval16 = __raw_readw(clk->enable_reg);
521 regval16 &= ~(1 << clk->enable_bit);
522 __raw_writew(regval16, clk->enable_reg);
524 regval16 = omap_readw(clk->enable_reg);
525 regval16 &= ~(1 << clk->enable_bit);
526 omap_writew(regval16, clk->enable_reg);
531 static long omap1_clk_round_rate(struct clk *clk, unsigned long rate)
535 if (clk->flags & RATE_FIXED)
538 if (clk->flags & RATE_CKCTL) {
539 dsor_exp = calc_dsor_exp(clk, rate);
544 return clk->parent->rate / (1 << dsor_exp);
547 if(clk->round_rate != 0)
548 return clk->round_rate(clk, rate);
553 static int omap1_clk_set_rate(struct clk *clk, unsigned long rate)
560 ret = clk->set_rate(clk, rate);
561 else if (clk->flags & RATE_CKCTL) {
562 dsor_exp = calc_dsor_exp(clk, rate);
568 regval = omap_readw(ARM_CKCTL);
569 regval &= ~(3 << clk->rate_offset);
570 regval |= dsor_exp << clk->rate_offset;
571 regval = verify_ckctl_value(regval);
572 omap_writew(regval, ARM_CKCTL);
573 clk->rate = clk->parent->rate / (1 << dsor_exp);
577 if (unlikely(ret == 0 && (clk->flags & RATE_PROPAGATES)))
583 /*-------------------------------------------------------------------------
584 * Omap1 clock reset and init functions
585 *-------------------------------------------------------------------------*/
587 #ifdef CONFIG_OMAP_RESET_CLOCKS
589 static void __init omap1_clk_disable_unused(struct clk *clk)
593 /* Clocks in the DSP domain need api_ck. Just assume bootloader
594 * has not enabled any DSP clocks */
595 if ((u32)clk->enable_reg == DSP_IDLECT2) {
596 printk(KERN_INFO "Skipping reset check for DSP domain "
597 "clock \"%s\"\n", clk->name);
601 /* Is the clock already disabled? */
602 if (clk->flags & ENABLE_REG_32BIT) {
603 if (clk->flags & VIRTUAL_IO_ADDRESS)
604 regval32 = __raw_readl(clk->enable_reg);
606 regval32 = omap_readl(clk->enable_reg);
608 if (clk->flags & VIRTUAL_IO_ADDRESS)
609 regval32 = __raw_readw(clk->enable_reg);
611 regval32 = omap_readw(clk->enable_reg);
614 if ((regval32 & (1 << clk->enable_bit)) == 0)
617 /* FIXME: This clock seems to be necessary but no-one
618 * has asked for its activation. */
619 if (clk == &tc2_ck // FIX: pm.c (SRAM), CCP, Camera
620 || clk == &ck_dpll1out.clk // FIX: SoSSI, SSR
621 || clk == &arm_gpio_ck // FIX: GPIO code for 1510
623 printk(KERN_INFO "FIXME: Clock \"%s\" seems unused\n",
628 printk(KERN_INFO "Disabling unused clock \"%s\"... ", clk->name);
634 #define omap1_clk_disable_unused NULL
637 static struct clk_functions omap1_clk_functions = {
638 .clk_enable = omap1_clk_enable,
639 .clk_disable = omap1_clk_disable,
640 .clk_round_rate = omap1_clk_round_rate,
641 .clk_set_rate = omap1_clk_set_rate,
642 .clk_disable_unused = omap1_clk_disable_unused,
645 int __init omap1_clk_init(void)
648 const struct omap_clock_config *info;
649 int crystal_type = 0; /* Default 12 MHz */
652 #ifdef CONFIG_DEBUG_LL
653 /* Resets some clocks that may be left on from bootloader,
654 * but leaves serial clocks on.
656 omap_writel(0x3 << 29, MOD_CONF_CTRL_0);
659 /* USB_REQ_EN will be disabled later if necessary (usb_dc_ck) */
660 reg = omap_readw(SOFT_REQ_REG) & (1 << 4);
661 omap_writew(reg, SOFT_REQ_REG);
662 if (!cpu_is_omap15xx())
663 omap_writew(0, SOFT_REQ_REG2);
665 clk_init(&omap1_clk_functions);
667 /* By default all idlect1 clocks are allowed to idle */
668 arm_idlect1_mask = ~0;
670 for (clkp = onchip_clks; clkp < onchip_clks+ARRAY_SIZE(onchip_clks); clkp++) {
671 if (((*clkp)->flags &CLOCK_IN_OMAP1510) && cpu_is_omap1510()) {
676 if (((*clkp)->flags &CLOCK_IN_OMAP16XX) && cpu_is_omap16xx()) {
681 if (((*clkp)->flags &CLOCK_IN_OMAP730) && cpu_is_omap730()) {
686 if (((*clkp)->flags &CLOCK_IN_OMAP310) && cpu_is_omap310()) {
692 info = omap_get_config(OMAP_TAG_CLOCK, struct omap_clock_config);
694 if (!cpu_is_omap15xx())
695 crystal_type = info->system_clock_type;
698 #if defined(CONFIG_ARCH_OMAP730)
699 ck_ref.rate = 13000000;
700 #elif defined(CONFIG_ARCH_OMAP16XX)
701 if (crystal_type == 2)
702 ck_ref.rate = 19200000;
705 printk("Clocks: ARM_SYSST: 0x%04x DPLL_CTL: 0x%04x ARM_CKCTL: 0x%04x\n",
706 omap_readw(ARM_SYSST), omap_readw(DPLL_CTL),
707 omap_readw(ARM_CKCTL));
709 /* We want to be in syncronous scalable mode */
710 omap_writew(0x1000, ARM_SYSST);
712 #ifdef CONFIG_OMAP_CLOCKS_SET_BY_BOOTLOADER
713 /* Use values set by bootloader. Determine PLL rate and recalculate
714 * dependent clocks as if kernel had changed PLL or divisors.
717 unsigned pll_ctl_val = omap_readw(DPLL_CTL);
719 ck_dpll1.rate = ck_ref.rate; /* Base xtal rate */
720 if (pll_ctl_val & 0x10) {
721 /* PLL enabled, apply multiplier and divisor */
722 if (pll_ctl_val & 0xf80)
723 ck_dpll1.rate *= (pll_ctl_val & 0xf80) >> 7;
724 ck_dpll1.rate /= ((pll_ctl_val & 0x60) >> 5) + 1;
726 /* PLL disabled, apply bypass divisor */
727 switch (pll_ctl_val & 0xc) {
739 propagate_rate(&ck_dpll1);
741 /* Find the highest supported frequency and enable it */
742 if (omap1_select_table_rate(&virtual_ck_mpu, ~0)) {
743 printk(KERN_ERR "System frequencies not set. Check your config.\n");
744 /* Guess sane values (60MHz) */
745 omap_writew(0x2290, DPLL_CTL);
746 omap_writew(cpu_is_omap730() ? 0x3005 : 0x1005, ARM_CKCTL);
747 ck_dpll1.rate = 60000000;
748 propagate_rate(&ck_dpll1);
751 /* Cache rates for clocks connected to ck_ref (not dpll1) */
752 propagate_rate(&ck_ref);
753 printk(KERN_INFO "Clocking rate (xtal/DPLL1/MPU): "
754 "%ld.%01ld/%ld.%01ld/%ld.%01ld MHz\n",
755 ck_ref.rate / 1000000, (ck_ref.rate / 100000) % 10,
756 ck_dpll1.rate / 1000000, (ck_dpll1.rate / 100000) % 10,
757 arm_ck.rate / 1000000, (arm_ck.rate / 100000) % 10);
759 #if defined(CONFIG_MACH_OMAP_PERSEUS2) || defined(CONFIG_MACH_OMAP_FSAMPLE)
760 /* Select slicer output as OMAP input clock */
761 omap_writew(omap_readw(OMAP730_PCC_UPLD_CTRL) & ~0x1, OMAP730_PCC_UPLD_CTRL);
764 /* Amstrad Delta wants BCLK high when inactive */
765 if (machine_is_ams_delta())
766 omap_writel(omap_readl(ULPD_CLOCK_CTRL) |
767 (1 << SDW_MCLK_INV_BIT),
770 /* Turn off DSP and ARM_TIMXO. Make sure ARM_INTHCK is not divided */
771 /* (on 730, bit 13 must not be cleared) */
772 if (cpu_is_omap730())
773 omap_writew(omap_readw(ARM_CKCTL) & 0x2fff, ARM_CKCTL);
775 omap_writew(omap_readw(ARM_CKCTL) & 0x0fff, ARM_CKCTL);
777 /* Put DSP/MPUI into reset until needed */
778 omap_writew(0, ARM_RSTCT1);
779 omap_writew(1, ARM_RSTCT2);
780 omap_writew(0x400, ARM_IDLECT1);
783 * According to OMAP5910 Erratum SYS_DMA_1, bit DMACK_REQ (bit 8)
784 * of the ARM_IDLECT2 register must be set to zero. The power-on
785 * default value of this bit is one.
787 omap_writew(0x0000, ARM_IDLECT2); /* Turn LCD clock off also */
790 * Only enable those clocks we will need, let the drivers
791 * enable other clocks as necessary
793 clk_enable(&armper_ck.clk);
794 clk_enable(&armxor_ck.clk);
795 clk_enable(&armtim_ck.clk); /* This should be done by timer code */
797 if (cpu_is_omap15xx())
798 clk_enable(&arm_gpio_ck);