2 * linux/arch/arm/mach-omap1/clock.c
4 * Copyright (C) 2004 - 2005, 2009-2010 Nokia Corporation
5 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
7 * Modified to use omap shared clock framework by
8 * Tony Lindgren <tony@atomide.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
19 #include <linux/clk.h>
20 #include <linux/clkdev.h>
22 #include <asm/mach-types.h>
26 #include <plat/clock.h>
27 #include <plat/sram.h>
28 #include <plat/clkdev_omap.h>
30 #include <mach/hardware.h>
36 __u32 arm_idlect1_mask;
37 struct clk *api_ck_p, *ck_dpll1_p, *ck_ref_p;
40 * Omap1 specific clock functions
43 unsigned long omap1_uart_recalc(struct clk *clk)
45 unsigned int val = __raw_readl(clk->enable_reg);
46 return val & clk->enable_bit ? 48000000 : 12000000;
49 unsigned long omap1_sossi_recalc(struct clk *clk)
51 u32 div = omap_readl(MOD_CONF_CTRL_1);
53 div = (div >> 17) & 0x7;
56 return clk->parent->rate / div;
59 static void omap1_clk_allow_idle(struct clk *clk)
61 struct arm_idlect1_clk * iclk = (struct arm_idlect1_clk *)clk;
63 if (!(clk->flags & CLOCK_IDLE_CONTROL))
66 if (iclk->no_idle_count > 0 && !(--iclk->no_idle_count))
67 arm_idlect1_mask |= 1 << iclk->idlect_shift;
70 static void omap1_clk_deny_idle(struct clk *clk)
72 struct arm_idlect1_clk * iclk = (struct arm_idlect1_clk *)clk;
74 if (!(clk->flags & CLOCK_IDLE_CONTROL))
77 if (iclk->no_idle_count++ == 0)
78 arm_idlect1_mask &= ~(1 << iclk->idlect_shift);
81 static __u16 verify_ckctl_value(__u16 newval)
83 /* This function checks for following limitations set
84 * by the hardware (all conditions must be true):
85 * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
90 * In addition following rules are enforced:
94 * However, maximum frequencies are not checked for!
103 per_exp = (newval >> CKCTL_PERDIV_OFFSET) & 3;
104 lcd_exp = (newval >> CKCTL_LCDDIV_OFFSET) & 3;
105 arm_exp = (newval >> CKCTL_ARMDIV_OFFSET) & 3;
106 dsp_exp = (newval >> CKCTL_DSPDIV_OFFSET) & 3;
107 tc_exp = (newval >> CKCTL_TCDIV_OFFSET) & 3;
108 dspmmu_exp = (newval >> CKCTL_DSPMMUDIV_OFFSET) & 3;
110 if (dspmmu_exp < dsp_exp)
111 dspmmu_exp = dsp_exp;
112 if (dspmmu_exp > dsp_exp+1)
113 dspmmu_exp = dsp_exp+1;
114 if (tc_exp < arm_exp)
116 if (tc_exp < dspmmu_exp)
118 if (tc_exp > lcd_exp)
120 if (tc_exp > per_exp)
124 newval |= per_exp << CKCTL_PERDIV_OFFSET;
125 newval |= lcd_exp << CKCTL_LCDDIV_OFFSET;
126 newval |= arm_exp << CKCTL_ARMDIV_OFFSET;
127 newval |= dsp_exp << CKCTL_DSPDIV_OFFSET;
128 newval |= tc_exp << CKCTL_TCDIV_OFFSET;
129 newval |= dspmmu_exp << CKCTL_DSPMMUDIV_OFFSET;
134 static int calc_dsor_exp(struct clk *clk, unsigned long rate)
136 /* Note: If target frequency is too low, this function will return 4,
137 * which is invalid value. Caller must check for this value and act
140 * Note: This function does not check for following limitations set
141 * by the hardware (all conditions must be true):
142 * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
147 unsigned long realrate;
151 parent = clk->parent;
152 if (unlikely(parent == NULL))
155 realrate = parent->rate;
156 for (dsor_exp=0; dsor_exp<4; dsor_exp++) {
157 if (realrate <= rate)
166 unsigned long omap1_ckctl_recalc(struct clk *clk)
168 /* Calculate divisor encoded as 2-bit exponent */
169 int dsor = 1 << (3 & (omap_readw(ARM_CKCTL) >> clk->rate_offset));
171 return clk->parent->rate / dsor;
174 unsigned long omap1_ckctl_recalc_dsp_domain(struct clk *clk)
178 /* Calculate divisor encoded as 2-bit exponent
180 * The clock control bits are in DSP domain,
181 * so api_ck is needed for access.
182 * Note that DSP_CKCTL virt addr = phys addr, so
183 * we must use __raw_readw() instead of omap_readw().
185 omap1_clk_enable(api_ck_p);
186 dsor = 1 << (3 & (__raw_readw(DSP_CKCTL) >> clk->rate_offset));
187 omap1_clk_disable(api_ck_p);
189 return clk->parent->rate / dsor;
192 /* MPU virtual clock functions */
193 int omap1_select_table_rate(struct clk *clk, unsigned long rate)
195 /* Find the highest supported frequency <= rate and switch to it */
196 struct mpu_rate * ptr;
197 unsigned long ref_rate;
199 ref_rate = ck_ref_p->rate;
201 for (ptr = omap1_rate_table; ptr->rate; ptr++) {
202 if (!(ptr->flags & cpu_mask))
205 if (ptr->xtal != ref_rate)
208 /* Can check only after xtal frequency check */
209 if (ptr->rate <= rate)
217 * In most cases we should not need to reprogram DPLL.
218 * Reprogramming the DPLL is tricky, it must be done from SRAM.
220 omap_sram_reprogram_clock(ptr->dpllctl_val, ptr->ckctl_val);
222 /* XXX Do we need to recalculate the tree below DPLL1 at this point? */
223 ck_dpll1_p->rate = ptr->pll_rate;
228 int omap1_clk_set_rate_dsp_domain(struct clk *clk, unsigned long rate)
233 dsor_exp = calc_dsor_exp(clk, rate);
239 regval = __raw_readw(DSP_CKCTL);
240 regval &= ~(3 << clk->rate_offset);
241 regval |= dsor_exp << clk->rate_offset;
242 __raw_writew(regval, DSP_CKCTL);
243 clk->rate = clk->parent->rate / (1 << dsor_exp);
248 long omap1_clk_round_rate_ckctl_arm(struct clk *clk, unsigned long rate)
250 int dsor_exp = calc_dsor_exp(clk, rate);
255 return clk->parent->rate / (1 << dsor_exp);
258 int omap1_clk_set_rate_ckctl_arm(struct clk *clk, unsigned long rate)
263 dsor_exp = calc_dsor_exp(clk, rate);
269 regval = omap_readw(ARM_CKCTL);
270 regval &= ~(3 << clk->rate_offset);
271 regval |= dsor_exp << clk->rate_offset;
272 regval = verify_ckctl_value(regval);
273 omap_writew(regval, ARM_CKCTL);
274 clk->rate = clk->parent->rate / (1 << dsor_exp);
278 long omap1_round_to_table_rate(struct clk *clk, unsigned long rate)
280 /* Find the highest supported frequency <= rate */
281 struct mpu_rate * ptr;
283 unsigned long ref_rate;
285 ref_rate = ck_ref_p->rate;
287 highest_rate = -EINVAL;
289 for (ptr = omap1_rate_table; ptr->rate; ptr++) {
290 if (!(ptr->flags & cpu_mask))
293 if (ptr->xtal != ref_rate)
296 highest_rate = ptr->rate;
298 /* Can check only after xtal frequency check */
299 if (ptr->rate <= rate)
306 static unsigned calc_ext_dsor(unsigned long rate)
310 /* MCLK and BCLK divisor selection is not linear:
311 * freq = 96MHz / dsor
313 * RATIO_SEL range: dsor <-> RATIO_SEL
314 * 0..6: (RATIO_SEL+2) <-> (dsor-2)
315 * 6..48: (8+(RATIO_SEL-6)*2) <-> ((dsor-8)/2+6)
316 * Minimum dsor is 2 and maximum is 96. Odd divisors starting from 9
319 for (dsor = 2; dsor < 96; ++dsor) {
320 if ((dsor & 1) && dsor > 8)
322 if (rate >= 96000000 / dsor)
328 /* XXX Only needed on 1510 */
329 int omap1_set_uart_rate(struct clk *clk, unsigned long rate)
333 val = __raw_readl(clk->enable_reg);
334 if (rate == 12000000)
335 val &= ~(1 << clk->enable_bit);
336 else if (rate == 48000000)
337 val |= (1 << clk->enable_bit);
340 __raw_writel(val, clk->enable_reg);
346 /* External clock (MCLK & BCLK) functions */
347 int omap1_set_ext_clk_rate(struct clk *clk, unsigned long rate)
352 dsor = calc_ext_dsor(rate);
353 clk->rate = 96000000 / dsor;
355 ratio_bits = ((dsor - 8) / 2 + 6) << 2;
357 ratio_bits = (dsor - 2) << 2;
359 ratio_bits |= __raw_readw(clk->enable_reg) & ~0xfd;
360 __raw_writew(ratio_bits, clk->enable_reg);
365 int omap1_set_sossi_rate(struct clk *clk, unsigned long rate)
369 unsigned long p_rate;
371 p_rate = clk->parent->rate;
372 /* Round towards slower frequency */
373 div = (p_rate + rate - 1) / rate;
375 if (div < 0 || div > 7)
378 l = omap_readl(MOD_CONF_CTRL_1);
381 omap_writel(l, MOD_CONF_CTRL_1);
383 clk->rate = p_rate / (div + 1);
388 long omap1_round_ext_clk_rate(struct clk *clk, unsigned long rate)
390 return 96000000 / calc_ext_dsor(rate);
393 void omap1_init_ext_clk(struct clk *clk)
398 /* Determine current rate and ensure clock is based on 96MHz APLL */
399 ratio_bits = __raw_readw(clk->enable_reg) & ~1;
400 __raw_writew(ratio_bits, clk->enable_reg);
402 ratio_bits = (ratio_bits & 0xfc) >> 2;
404 dsor = (ratio_bits - 6) * 2 + 8;
406 dsor = ratio_bits + 2;
408 clk-> rate = 96000000 / dsor;
411 int omap1_clk_enable(struct clk *clk)
415 if (clk->usecount++ == 0) {
417 ret = omap1_clk_enable(clk->parent);
421 if (clk->flags & CLOCK_NO_IDLE_PARENT)
422 omap1_clk_deny_idle(clk->parent);
425 ret = clk->ops->enable(clk);
428 omap1_clk_disable(clk->parent);
439 void omap1_clk_disable(struct clk *clk)
441 if (clk->usecount > 0 && !(--clk->usecount)) {
442 clk->ops->disable(clk);
443 if (likely(clk->parent)) {
444 omap1_clk_disable(clk->parent);
445 if (clk->flags & CLOCK_NO_IDLE_PARENT)
446 omap1_clk_allow_idle(clk->parent);
451 static int omap1_clk_enable_generic(struct clk *clk)
456 if (unlikely(clk->enable_reg == NULL)) {
457 printk(KERN_ERR "clock.c: Enable for %s without enable code\n",
462 if (clk->flags & ENABLE_REG_32BIT) {
463 regval32 = __raw_readl(clk->enable_reg);
464 regval32 |= (1 << clk->enable_bit);
465 __raw_writel(regval32, clk->enable_reg);
467 regval16 = __raw_readw(clk->enable_reg);
468 regval16 |= (1 << clk->enable_bit);
469 __raw_writew(regval16, clk->enable_reg);
475 static void omap1_clk_disable_generic(struct clk *clk)
480 if (clk->enable_reg == NULL)
483 if (clk->flags & ENABLE_REG_32BIT) {
484 regval32 = __raw_readl(clk->enable_reg);
485 regval32 &= ~(1 << clk->enable_bit);
486 __raw_writel(regval32, clk->enable_reg);
488 regval16 = __raw_readw(clk->enable_reg);
489 regval16 &= ~(1 << clk->enable_bit);
490 __raw_writew(regval16, clk->enable_reg);
494 const struct clkops clkops_generic = {
495 .enable = omap1_clk_enable_generic,
496 .disable = omap1_clk_disable_generic,
499 static int omap1_clk_enable_dsp_domain(struct clk *clk)
503 retval = omap1_clk_enable(api_ck_p);
505 retval = omap1_clk_enable_generic(clk);
506 omap1_clk_disable(api_ck_p);
512 static void omap1_clk_disable_dsp_domain(struct clk *clk)
514 if (omap1_clk_enable(api_ck_p) == 0) {
515 omap1_clk_disable_generic(clk);
516 omap1_clk_disable(api_ck_p);
520 const struct clkops clkops_dspck = {
521 .enable = omap1_clk_enable_dsp_domain,
522 .disable = omap1_clk_disable_dsp_domain,
525 /* XXX SYSC register handling does not belong in the clock framework */
526 static int omap1_clk_enable_uart_functional_16xx(struct clk *clk)
529 struct uart_clk *uclk;
531 ret = omap1_clk_enable_generic(clk);
533 /* Set smart idle acknowledgement mode */
534 uclk = (struct uart_clk *)clk;
535 omap_writeb((omap_readb(uclk->sysc_addr) & ~0x10) | 8,
542 /* XXX SYSC register handling does not belong in the clock framework */
543 static void omap1_clk_disable_uart_functional_16xx(struct clk *clk)
545 struct uart_clk *uclk;
547 /* Set force idle acknowledgement mode */
548 uclk = (struct uart_clk *)clk;
549 omap_writeb((omap_readb(uclk->sysc_addr) & ~0x18), uclk->sysc_addr);
551 omap1_clk_disable_generic(clk);
554 /* XXX SYSC register handling does not belong in the clock framework */
555 const struct clkops clkops_uart_16xx = {
556 .enable = omap1_clk_enable_uart_functional_16xx,
557 .disable = omap1_clk_disable_uart_functional_16xx,
560 long omap1_clk_round_rate(struct clk *clk, unsigned long rate)
562 if (clk->round_rate != NULL)
563 return clk->round_rate(clk, rate);
568 int omap1_clk_set_rate(struct clk *clk, unsigned long rate)
573 ret = clk->set_rate(clk, rate);
578 * Omap1 clock reset and init functions
581 #ifdef CONFIG_OMAP_RESET_CLOCKS
583 void omap1_clk_disable_unused(struct clk *clk)
587 /* Clocks in the DSP domain need api_ck. Just assume bootloader
588 * has not enabled any DSP clocks */
589 if (clk->enable_reg == DSP_IDLECT2) {
590 printk(KERN_INFO "Skipping reset check for DSP domain "
591 "clock \"%s\"\n", clk->name);
595 /* Is the clock already disabled? */
596 if (clk->flags & ENABLE_REG_32BIT)
597 regval32 = __raw_readl(clk->enable_reg);
599 regval32 = __raw_readw(clk->enable_reg);
601 if ((regval32 & (1 << clk->enable_bit)) == 0)
604 printk(KERN_INFO "Disabling unused clock \"%s\"... ", clk->name);
605 clk->ops->disable(clk);