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 void omap1_sossi_recalc(struct clk *clk)
54 u32 div = omap_readl(MOD_CONF_CTRL_1);
56 div = (div >> 17) & 0x7;
58 clk->rate = clk->parent->rate / div;
61 static int omap1_clk_enable_dsp_domain(struct clk *clk)
65 retval = omap1_clk_enable(&api_ck.clk);
67 retval = omap1_clk_enable_generic(clk);
68 omap1_clk_disable(&api_ck.clk);
74 static void omap1_clk_disable_dsp_domain(struct clk *clk)
76 if (omap1_clk_enable(&api_ck.clk) == 0) {
77 omap1_clk_disable_generic(clk);
78 omap1_clk_disable(&api_ck.clk);
82 static int omap1_clk_enable_uart_functional(struct clk *clk)
85 struct uart_clk *uclk;
87 ret = omap1_clk_enable_generic(clk);
89 /* Set smart idle acknowledgement mode */
90 uclk = (struct uart_clk *)clk;
91 omap_writeb((omap_readb(uclk->sysc_addr) & ~0x10) | 8,
98 static void omap1_clk_disable_uart_functional(struct clk *clk)
100 struct uart_clk *uclk;
102 /* Set force idle acknowledgement mode */
103 uclk = (struct uart_clk *)clk;
104 omap_writeb((omap_readb(uclk->sysc_addr) & ~0x18), uclk->sysc_addr);
106 omap1_clk_disable_generic(clk);
109 static void omap1_clk_allow_idle(struct clk *clk)
111 struct arm_idlect1_clk * iclk = (struct arm_idlect1_clk *)clk;
113 if (!(clk->flags & CLOCK_IDLE_CONTROL))
116 if (iclk->no_idle_count > 0 && !(--iclk->no_idle_count))
117 arm_idlect1_mask |= 1 << iclk->idlect_shift;
120 static void omap1_clk_deny_idle(struct clk *clk)
122 struct arm_idlect1_clk * iclk = (struct arm_idlect1_clk *)clk;
124 if (!(clk->flags & CLOCK_IDLE_CONTROL))
127 if (iclk->no_idle_count++ == 0)
128 arm_idlect1_mask &= ~(1 << iclk->idlect_shift);
131 static __u16 verify_ckctl_value(__u16 newval)
133 /* This function checks for following limitations set
134 * by the hardware (all conditions must be true):
135 * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
140 * In addition following rules are enforced:
144 * However, maximum frequencies are not checked for!
153 per_exp = (newval >> CKCTL_PERDIV_OFFSET) & 3;
154 lcd_exp = (newval >> CKCTL_LCDDIV_OFFSET) & 3;
155 arm_exp = (newval >> CKCTL_ARMDIV_OFFSET) & 3;
156 dsp_exp = (newval >> CKCTL_DSPDIV_OFFSET) & 3;
157 tc_exp = (newval >> CKCTL_TCDIV_OFFSET) & 3;
158 dspmmu_exp = (newval >> CKCTL_DSPMMUDIV_OFFSET) & 3;
160 if (dspmmu_exp < dsp_exp)
161 dspmmu_exp = dsp_exp;
162 if (dspmmu_exp > dsp_exp+1)
163 dspmmu_exp = dsp_exp+1;
164 if (tc_exp < arm_exp)
166 if (tc_exp < dspmmu_exp)
168 if (tc_exp > lcd_exp)
170 if (tc_exp > per_exp)
174 newval |= per_exp << CKCTL_PERDIV_OFFSET;
175 newval |= lcd_exp << CKCTL_LCDDIV_OFFSET;
176 newval |= arm_exp << CKCTL_ARMDIV_OFFSET;
177 newval |= dsp_exp << CKCTL_DSPDIV_OFFSET;
178 newval |= tc_exp << CKCTL_TCDIV_OFFSET;
179 newval |= dspmmu_exp << CKCTL_DSPMMUDIV_OFFSET;
184 static int calc_dsor_exp(struct clk *clk, unsigned long rate)
186 /* Note: If target frequency is too low, this function will return 4,
187 * which is invalid value. Caller must check for this value and act
190 * Note: This function does not check for following limitations set
191 * by the hardware (all conditions must be true):
192 * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
197 unsigned long realrate;
201 if (unlikely(!(clk->flags & RATE_CKCTL)))
204 parent = clk->parent;
205 if (unlikely(parent == 0))
208 realrate = parent->rate;
209 for (dsor_exp=0; dsor_exp<4; dsor_exp++) {
210 if (realrate <= rate)
219 static void omap1_ckctl_recalc(struct clk * clk)
223 /* Calculate divisor encoded as 2-bit exponent */
224 dsor = 1 << (3 & (omap_readw(ARM_CKCTL) >> clk->rate_offset));
226 if (unlikely(clk->rate == clk->parent->rate / dsor))
227 return; /* No change, quick exit */
228 clk->rate = clk->parent->rate / dsor;
230 if (unlikely(clk->flags & RATE_PROPAGATES))
234 static void omap1_ckctl_recalc_dsp_domain(struct clk * clk)
238 /* Calculate divisor encoded as 2-bit exponent
240 * The clock control bits are in DSP domain,
241 * so api_ck is needed for access.
242 * Note that DSP_CKCTL virt addr = phys addr, so
243 * we must use __raw_readw() instead of omap_readw().
245 omap1_clk_enable(&api_ck.clk);
246 dsor = 1 << (3 & (__raw_readw(DSP_CKCTL) >> clk->rate_offset));
247 omap1_clk_disable(&api_ck.clk);
249 if (unlikely(clk->rate == clk->parent->rate / dsor))
250 return; /* No change, quick exit */
251 clk->rate = clk->parent->rate / dsor;
253 if (unlikely(clk->flags & RATE_PROPAGATES))
257 /* MPU virtual clock functions */
258 static int omap1_select_table_rate(struct clk * clk, unsigned long rate)
260 /* Find the highest supported frequency <= rate and switch to it */
261 struct mpu_rate * ptr;
263 if (clk != &virtual_ck_mpu)
266 for (ptr = rate_table; ptr->rate; ptr++) {
267 if (ptr->xtal != ck_ref.rate)
270 /* DPLL1 cannot be reprogrammed without risking system crash */
271 if (likely(ck_dpll1.rate!=0) && ptr->pll_rate != ck_dpll1.rate)
274 /* Can check only after xtal frequency check */
275 if (ptr->rate <= rate)
283 * In most cases we should not need to reprogram DPLL.
284 * Reprogramming the DPLL is tricky, it must be done from SRAM.
285 * (on 730, bit 13 must always be 1)
287 if (cpu_is_omap730())
288 omap_sram_reprogram_clock(ptr->dpllctl_val, ptr->ckctl_val | 0x2000);
290 omap_sram_reprogram_clock(ptr->dpllctl_val, ptr->ckctl_val);
292 ck_dpll1.rate = ptr->pll_rate;
293 propagate_rate(&ck_dpll1);
297 static int omap1_clk_set_rate_dsp_domain(struct clk *clk, unsigned long rate)
303 if (clk->flags & RATE_CKCTL) {
304 dsor_exp = calc_dsor_exp(clk, rate);
310 regval = __raw_readw(DSP_CKCTL);
311 regval &= ~(3 << clk->rate_offset);
312 regval |= dsor_exp << clk->rate_offset;
313 __raw_writew(regval, DSP_CKCTL);
314 clk->rate = clk->parent->rate / (1 << dsor_exp);
318 if (unlikely(ret == 0 && (clk->flags & RATE_PROPAGATES)))
324 static long omap1_round_to_table_rate(struct clk * clk, unsigned long rate)
326 /* Find the highest supported frequency <= rate */
327 struct mpu_rate * ptr;
330 if (clk != &virtual_ck_mpu)
333 highest_rate = -EINVAL;
335 for (ptr = rate_table; ptr->rate; ptr++) {
336 if (ptr->xtal != ck_ref.rate)
339 highest_rate = ptr->rate;
341 /* Can check only after xtal frequency check */
342 if (ptr->rate <= rate)
349 static unsigned calc_ext_dsor(unsigned long rate)
353 /* MCLK and BCLK divisor selection is not linear:
354 * freq = 96MHz / dsor
356 * RATIO_SEL range: dsor <-> RATIO_SEL
357 * 0..6: (RATIO_SEL+2) <-> (dsor-2)
358 * 6..48: (8+(RATIO_SEL-6)*2) <-> ((dsor-8)/2+6)
359 * Minimum dsor is 2 and maximum is 96. Odd divisors starting from 9
362 for (dsor = 2; dsor < 96; ++dsor) {
363 if ((dsor & 1) && dsor > 8)
365 if (rate >= 96000000 / dsor)
371 /* Only needed on 1510 */
372 static int omap1_set_uart_rate(struct clk * clk, unsigned long rate)
376 val = omap_readl(clk->enable_reg);
377 if (rate == 12000000)
378 val &= ~(1 << clk->enable_bit);
379 else if (rate == 48000000)
380 val |= (1 << clk->enable_bit);
383 omap_writel(val, clk->enable_reg);
389 /* External clock (MCLK & BCLK) functions */
390 static int omap1_set_ext_clk_rate(struct clk * clk, unsigned long rate)
395 dsor = calc_ext_dsor(rate);
396 clk->rate = 96000000 / dsor;
398 ratio_bits = ((dsor - 8) / 2 + 6) << 2;
400 ratio_bits = (dsor - 2) << 2;
402 ratio_bits |= omap_readw(clk->enable_reg) & ~0xfd;
403 omap_writew(ratio_bits, clk->enable_reg);
408 static int omap1_set_sossi_rate(struct clk *clk, unsigned long rate)
412 unsigned long p_rate;
414 p_rate = clk->parent->rate;
415 /* Round towards slower frequency */
416 div = (p_rate + rate - 1) / rate;
418 if (div < 0 || div > 7)
421 l = omap_readl(MOD_CONF_CTRL_1);
424 omap_writel(l, MOD_CONF_CTRL_1);
426 clk->rate = p_rate / (div + 1);
427 if (unlikely(clk->flags & RATE_PROPAGATES))
433 static long omap1_round_ext_clk_rate(struct clk * clk, unsigned long rate)
435 return 96000000 / calc_ext_dsor(rate);
438 static void omap1_init_ext_clk(struct clk * clk)
443 /* Determine current rate and ensure clock is based on 96MHz APLL */
444 ratio_bits = omap_readw(clk->enable_reg) & ~1;
445 omap_writew(ratio_bits, clk->enable_reg);
447 ratio_bits = (ratio_bits & 0xfc) >> 2;
449 dsor = (ratio_bits - 6) * 2 + 8;
451 dsor = ratio_bits + 2;
453 clk-> rate = 96000000 / dsor;
456 static int omap1_clk_enable(struct clk *clk)
459 if (clk->usecount++ == 0) {
460 if (likely(clk->parent)) {
461 ret = omap1_clk_enable(clk->parent);
463 if (unlikely(ret != 0)) {
468 if (clk->flags & CLOCK_NO_IDLE_PARENT)
469 omap1_clk_deny_idle(clk->parent);
472 ret = clk->enable(clk);
474 if (unlikely(ret != 0) && clk->parent) {
475 omap1_clk_disable(clk->parent);
483 static void omap1_clk_disable(struct clk *clk)
485 if (clk->usecount > 0 && !(--clk->usecount)) {
487 if (likely(clk->parent)) {
488 omap1_clk_disable(clk->parent);
489 if (clk->flags & CLOCK_NO_IDLE_PARENT)
490 omap1_clk_allow_idle(clk->parent);
495 static int omap1_clk_enable_generic(struct clk *clk)
500 if (clk->flags & ALWAYS_ENABLED)
503 if (unlikely(clk->enable_reg == 0)) {
504 printk(KERN_ERR "clock.c: Enable for %s without enable code\n",
509 if (clk->flags & ENABLE_REG_32BIT) {
510 if (clk->flags & VIRTUAL_IO_ADDRESS) {
511 regval32 = __raw_readl(clk->enable_reg);
512 regval32 |= (1 << clk->enable_bit);
513 __raw_writel(regval32, clk->enable_reg);
515 regval32 = omap_readl(clk->enable_reg);
516 regval32 |= (1 << clk->enable_bit);
517 omap_writel(regval32, clk->enable_reg);
520 if (clk->flags & VIRTUAL_IO_ADDRESS) {
521 regval16 = __raw_readw(clk->enable_reg);
522 regval16 |= (1 << clk->enable_bit);
523 __raw_writew(regval16, clk->enable_reg);
525 regval16 = omap_readw(clk->enable_reg);
526 regval16 |= (1 << clk->enable_bit);
527 omap_writew(regval16, clk->enable_reg);
534 static void omap1_clk_disable_generic(struct clk *clk)
539 if (clk->enable_reg == 0)
542 if (clk->flags & ENABLE_REG_32BIT) {
543 if (clk->flags & VIRTUAL_IO_ADDRESS) {
544 regval32 = __raw_readl(clk->enable_reg);
545 regval32 &= ~(1 << clk->enable_bit);
546 __raw_writel(regval32, clk->enable_reg);
548 regval32 = omap_readl(clk->enable_reg);
549 regval32 &= ~(1 << clk->enable_bit);
550 omap_writel(regval32, clk->enable_reg);
553 if (clk->flags & VIRTUAL_IO_ADDRESS) {
554 regval16 = __raw_readw(clk->enable_reg);
555 regval16 &= ~(1 << clk->enable_bit);
556 __raw_writew(regval16, clk->enable_reg);
558 regval16 = omap_readw(clk->enable_reg);
559 regval16 &= ~(1 << clk->enable_bit);
560 omap_writew(regval16, clk->enable_reg);
565 static long omap1_clk_round_rate(struct clk *clk, unsigned long rate)
569 if (clk->flags & RATE_FIXED)
572 if (clk->flags & RATE_CKCTL) {
573 dsor_exp = calc_dsor_exp(clk, rate);
578 return clk->parent->rate / (1 << dsor_exp);
581 if(clk->round_rate != 0)
582 return clk->round_rate(clk, rate);
587 static int omap1_clk_set_rate(struct clk *clk, unsigned long rate)
594 ret = clk->set_rate(clk, rate);
595 else if (clk->flags & RATE_CKCTL) {
596 dsor_exp = calc_dsor_exp(clk, rate);
602 regval = omap_readw(ARM_CKCTL);
603 regval &= ~(3 << clk->rate_offset);
604 regval |= dsor_exp << clk->rate_offset;
605 regval = verify_ckctl_value(regval);
606 omap_writew(regval, ARM_CKCTL);
607 clk->rate = clk->parent->rate / (1 << dsor_exp);
611 if (unlikely(ret == 0 && (clk->flags & RATE_PROPAGATES)))
617 /*-------------------------------------------------------------------------
618 * Omap1 clock reset and init functions
619 *-------------------------------------------------------------------------*/
621 #ifdef CONFIG_OMAP_RESET_CLOCKS
623 static void __init omap1_clk_disable_unused(struct clk *clk)
627 /* Clocks in the DSP domain need api_ck. Just assume bootloader
628 * has not enabled any DSP clocks */
629 if ((u32)clk->enable_reg == DSP_IDLECT2) {
630 printk(KERN_INFO "Skipping reset check for DSP domain "
631 "clock \"%s\"\n", clk->name);
635 /* Is the clock already disabled? */
636 if (clk->flags & ENABLE_REG_32BIT) {
637 if (clk->flags & VIRTUAL_IO_ADDRESS)
638 regval32 = __raw_readl(clk->enable_reg);
640 regval32 = omap_readl(clk->enable_reg);
642 if (clk->flags & VIRTUAL_IO_ADDRESS)
643 regval32 = __raw_readw(clk->enable_reg);
645 regval32 = omap_readw(clk->enable_reg);
648 if ((regval32 & (1 << clk->enable_bit)) == 0)
651 /* FIXME: This clock seems to be necessary but no-one
652 * has asked for its activation. */
653 if (clk == &tc2_ck // FIX: pm.c (SRAM), CCP, Camera
654 || clk == &ck_dpll1out.clk // FIX: SoSSI, SSR
655 || clk == &arm_gpio_ck // FIX: GPIO code for 1510
657 printk(KERN_INFO "FIXME: Clock \"%s\" seems unused\n",
662 printk(KERN_INFO "Disabling unused clock \"%s\"... ", clk->name);
668 #define omap1_clk_disable_unused NULL
671 static struct clk_functions omap1_clk_functions = {
672 .clk_enable = omap1_clk_enable,
673 .clk_disable = omap1_clk_disable,
674 .clk_round_rate = omap1_clk_round_rate,
675 .clk_set_rate = omap1_clk_set_rate,
676 .clk_disable_unused = omap1_clk_disable_unused,
679 int __init omap1_clk_init(void)
682 const struct omap_clock_config *info;
683 int crystal_type = 0; /* Default 12 MHz */
686 #ifdef CONFIG_DEBUG_LL
687 /* Resets some clocks that may be left on from bootloader,
688 * but leaves serial clocks on.
690 omap_writel(0x3 << 29, MOD_CONF_CTRL_0);
693 /* USB_REQ_EN will be disabled later if necessary (usb_dc_ck) */
694 reg = omap_readw(SOFT_REQ_REG) & (1 << 4);
695 omap_writew(reg, SOFT_REQ_REG);
696 if (!cpu_is_omap15xx())
697 omap_writew(0, SOFT_REQ_REG2);
699 clk_init(&omap1_clk_functions);
701 /* By default all idlect1 clocks are allowed to idle */
702 arm_idlect1_mask = ~0;
704 for (clkp = onchip_clks; clkp < onchip_clks+ARRAY_SIZE(onchip_clks); clkp++) {
705 if (((*clkp)->flags &CLOCK_IN_OMAP1510) && cpu_is_omap1510()) {
710 if (((*clkp)->flags &CLOCK_IN_OMAP16XX) && cpu_is_omap16xx()) {
715 if (((*clkp)->flags &CLOCK_IN_OMAP730) && cpu_is_omap730()) {
720 if (((*clkp)->flags &CLOCK_IN_OMAP310) && cpu_is_omap310()) {
726 info = omap_get_config(OMAP_TAG_CLOCK, struct omap_clock_config);
728 if (!cpu_is_omap15xx())
729 crystal_type = info->system_clock_type;
732 #if defined(CONFIG_ARCH_OMAP730)
733 ck_ref.rate = 13000000;
734 #elif defined(CONFIG_ARCH_OMAP16XX)
735 if (crystal_type == 2)
736 ck_ref.rate = 19200000;
739 printk("Clocks: ARM_SYSST: 0x%04x DPLL_CTL: 0x%04x ARM_CKCTL: 0x%04x\n",
740 omap_readw(ARM_SYSST), omap_readw(DPLL_CTL),
741 omap_readw(ARM_CKCTL));
743 /* We want to be in syncronous scalable mode */
744 omap_writew(0x1000, ARM_SYSST);
746 #ifdef CONFIG_OMAP_CLOCKS_SET_BY_BOOTLOADER
747 /* Use values set by bootloader. Determine PLL rate and recalculate
748 * dependent clocks as if kernel had changed PLL or divisors.
751 unsigned pll_ctl_val = omap_readw(DPLL_CTL);
753 ck_dpll1.rate = ck_ref.rate; /* Base xtal rate */
754 if (pll_ctl_val & 0x10) {
755 /* PLL enabled, apply multiplier and divisor */
756 if (pll_ctl_val & 0xf80)
757 ck_dpll1.rate *= (pll_ctl_val & 0xf80) >> 7;
758 ck_dpll1.rate /= ((pll_ctl_val & 0x60) >> 5) + 1;
760 /* PLL disabled, apply bypass divisor */
761 switch (pll_ctl_val & 0xc) {
773 propagate_rate(&ck_dpll1);
775 /* Find the highest supported frequency and enable it */
776 if (omap1_select_table_rate(&virtual_ck_mpu, ~0)) {
777 printk(KERN_ERR "System frequencies not set. Check your config.\n");
778 /* Guess sane values (60MHz) */
779 omap_writew(0x2290, DPLL_CTL);
780 omap_writew(cpu_is_omap730() ? 0x3005 : 0x1005, ARM_CKCTL);
781 ck_dpll1.rate = 60000000;
782 propagate_rate(&ck_dpll1);
785 /* Cache rates for clocks connected to ck_ref (not dpll1) */
786 propagate_rate(&ck_ref);
787 printk(KERN_INFO "Clocking rate (xtal/DPLL1/MPU): "
788 "%ld.%01ld/%ld.%01ld/%ld.%01ld MHz\n",
789 ck_ref.rate / 1000000, (ck_ref.rate / 100000) % 10,
790 ck_dpll1.rate / 1000000, (ck_dpll1.rate / 100000) % 10,
791 arm_ck.rate / 1000000, (arm_ck.rate / 100000) % 10);
793 #if defined(CONFIG_MACH_OMAP_PERSEUS2) || defined(CONFIG_MACH_OMAP_FSAMPLE)
794 /* Select slicer output as OMAP input clock */
795 omap_writew(omap_readw(OMAP730_PCC_UPLD_CTRL) & ~0x1, OMAP730_PCC_UPLD_CTRL);
798 /* Amstrad Delta wants BCLK high when inactive */
799 if (machine_is_ams_delta())
800 omap_writel(omap_readl(ULPD_CLOCK_CTRL) |
801 (1 << SDW_MCLK_INV_BIT),
804 /* Turn off DSP and ARM_TIMXO. Make sure ARM_INTHCK is not divided */
805 /* (on 730, bit 13 must not be cleared) */
806 if (cpu_is_omap730())
807 omap_writew(omap_readw(ARM_CKCTL) & 0x2fff, ARM_CKCTL);
809 omap_writew(omap_readw(ARM_CKCTL) & 0x0fff, ARM_CKCTL);
811 /* Put DSP/MPUI into reset until needed */
812 omap_writew(0, ARM_RSTCT1);
813 omap_writew(1, ARM_RSTCT2);
814 omap_writew(0x400, ARM_IDLECT1);
817 * According to OMAP5910 Erratum SYS_DMA_1, bit DMACK_REQ (bit 8)
818 * of the ARM_IDLECT2 register must be set to zero. The power-on
819 * default value of this bit is one.
821 omap_writew(0x0000, ARM_IDLECT2); /* Turn LCD clock off also */
824 * Only enable those clocks we will need, let the drivers
825 * enable other clocks as necessary
827 clk_enable(&armper_ck.clk);
828 clk_enable(&armxor_ck.clk);
829 clk_enable(&armtim_ck.clk); /* This should be done by timer code */
831 if (cpu_is_omap15xx())
832 clk_enable(&arm_gpio_ck);