2 * linux/arch/arm/plat-omap/clock.c
4 * Copyright (C) 2004 Nokia corporation
5 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/string.h>
19 #include <asm/semaphore.h>
20 #include <asm/hardware/clock.h>
21 #include <asm/arch/board.h>
22 #include <asm/arch/usb.h>
27 static LIST_HEAD(clocks);
28 static DECLARE_MUTEX(clocks_sem);
29 static DEFINE_SPINLOCK(clockfw_lock);
30 static void propagate_rate(struct clk * clk);
31 /* UART clock function */
32 static int set_uart_rate(struct clk * clk, unsigned long rate);
33 /* External clock (MCLK & BCLK) functions */
34 static int set_ext_clk_rate(struct clk * clk, unsigned long rate);
35 static long round_ext_clk_rate(struct clk * clk, unsigned long rate);
36 static void init_ext_clk(struct clk * clk);
37 /* MPU virtual clock functions */
38 static int select_table_rate(struct clk * clk, unsigned long rate);
39 static long round_to_table_rate(struct clk * clk, unsigned long rate);
40 void clk_setdpll(__u16, __u16);
42 static struct mpu_rate rate_table[] = {
43 /* MPU MHz, xtal MHz, dpll1 MHz, CKCTL, DPLL_CTL
44 * armdiv, dspdiv, dspmmu, tcdiv, perdiv, lcddiv
46 #if defined(CONFIG_OMAP_ARM_216MHZ)
47 { 216000000, 12000000, 216000000, 0x050d, 0x2910 }, /* 1/1/2/2/2/8 */
49 #if defined(CONFIG_OMAP_ARM_195MHZ)
50 { 195000000, 13000000, 195000000, 0x050e, 0x2790 }, /* 1/1/2/2/4/8 */
52 #if defined(CONFIG_OMAP_ARM_192MHZ)
53 { 192000000, 19200000, 192000000, 0x050f, 0x2510 }, /* 1/1/2/2/8/8 */
54 { 192000000, 12000000, 192000000, 0x050f, 0x2810 }, /* 1/1/2/2/8/8 */
55 { 96000000, 12000000, 192000000, 0x055f, 0x2810 }, /* 2/2/2/2/8/8 */
56 { 48000000, 12000000, 192000000, 0x0baf, 0x2810 }, /* 4/8/4/4/8/8 */
57 { 24000000, 12000000, 192000000, 0x0fff, 0x2810 }, /* 8/8/8/8/8/8 */
59 #if defined(CONFIG_OMAP_ARM_182MHZ)
60 { 182000000, 13000000, 182000000, 0x050e, 0x2710 }, /* 1/1/2/2/4/8 */
62 #if defined(CONFIG_OMAP_ARM_168MHZ)
63 { 168000000, 12000000, 168000000, 0x010f, 0x2710 }, /* 1/1/1/2/8/8 */
65 #if defined(CONFIG_OMAP_ARM_150MHZ)
66 { 150000000, 12000000, 150000000, 0x010a, 0x2cb0 }, /* 1/1/1/2/4/4 */
68 #if defined(CONFIG_OMAP_ARM_120MHZ)
69 { 120000000, 12000000, 120000000, 0x010a, 0x2510 }, /* 1/1/1/2/4/4 */
71 #if defined(CONFIG_OMAP_ARM_96MHZ)
72 { 96000000, 12000000, 96000000, 0x0005, 0x2410 }, /* 1/1/1/1/2/2 */
74 #if defined(CONFIG_OMAP_ARM_60MHZ)
75 { 60000000, 12000000, 60000000, 0x0005, 0x2290 }, /* 1/1/1/1/2/2 */
77 #if defined(CONFIG_OMAP_ARM_30MHZ)
78 { 30000000, 12000000, 60000000, 0x0555, 0x2290 }, /* 2/2/2/2/2/2 */
84 static void ckctl_recalc(struct clk * clk);
85 int __clk_enable(struct clk *clk);
86 void __clk_disable(struct clk *clk);
87 void __clk_unuse(struct clk *clk);
88 int __clk_use(struct clk *clk);
91 static void followparent_recalc(struct clk * clk)
93 clk->rate = clk->parent->rate;
97 static void watchdog_recalc(struct clk * clk)
99 clk->rate = clk->parent->rate / 14;
102 static void uart_recalc(struct clk * clk)
104 unsigned int val = omap_readl(clk->enable_reg);
105 if (val & clk->enable_bit)
106 clk->rate = 48000000;
108 clk->rate = 12000000;
111 static struct clk ck_ref = {
114 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
118 static struct clk ck_dpll1 = {
121 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
122 RATE_PROPAGATES | ALWAYS_ENABLED,
125 static struct clk ck_dpll1out = {
126 .name = "ck_dpll1out",
128 .flags = CLOCK_IN_OMAP16XX,
129 .enable_reg = ARM_IDLECT2,
130 .enable_bit = EN_CKOUT_ARM,
131 .recalc = &followparent_recalc,
134 static struct clk arm_ck = {
137 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
138 RATE_CKCTL | RATE_PROPAGATES | ALWAYS_ENABLED,
139 .rate_offset = CKCTL_ARMDIV_OFFSET,
140 .recalc = &ckctl_recalc,
143 static struct clk armper_ck = {
146 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
148 .enable_reg = ARM_IDLECT2,
149 .enable_bit = EN_PERCK,
150 .rate_offset = CKCTL_PERDIV_OFFSET,
151 .recalc = &ckctl_recalc,
154 static struct clk arm_gpio_ck = {
155 .name = "arm_gpio_ck",
157 .flags = CLOCK_IN_OMAP1510,
158 .enable_reg = ARM_IDLECT2,
159 .enable_bit = EN_GPIOCK,
160 .recalc = &followparent_recalc,
163 static struct clk armxor_ck = {
166 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX,
167 .enable_reg = ARM_IDLECT2,
168 .enable_bit = EN_XORPCK,
169 .recalc = &followparent_recalc,
172 static struct clk armtim_ck = {
175 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX,
176 .enable_reg = ARM_IDLECT2,
177 .enable_bit = EN_TIMCK,
178 .recalc = &followparent_recalc,
181 static struct clk armwdt_ck = {
184 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX,
185 .enable_reg = ARM_IDLECT2,
186 .enable_bit = EN_WDTCK,
187 .recalc = &watchdog_recalc,
190 static struct clk arminth_ck16xx = {
191 .name = "arminth_ck",
193 .flags = CLOCK_IN_OMAP16XX | ALWAYS_ENABLED,
194 .recalc = &followparent_recalc,
195 /* Note: On 16xx the frequency can be divided by 2 by programming
196 * ARM_CKCTL:ARM_INTHCK_SEL(14) to 1
198 * 1510 version is in TC clocks.
202 static struct clk dsp_ck = {
205 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
207 .enable_reg = ARM_CKCTL,
208 .enable_bit = EN_DSPCK,
209 .rate_offset = CKCTL_DSPDIV_OFFSET,
210 .recalc = &ckctl_recalc,
213 static struct clk dspmmu_ck = {
216 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
217 RATE_CKCTL | ALWAYS_ENABLED,
218 .rate_offset = CKCTL_DSPMMUDIV_OFFSET,
219 .recalc = &ckctl_recalc,
222 static struct clk dspper_ck = {
225 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
226 RATE_CKCTL | DSP_DOMAIN_CLOCK | VIRTUAL_IO_ADDRESS,
227 .enable_reg = DSP_IDLECT2,
228 .enable_bit = EN_PERCK,
229 .rate_offset = CKCTL_PERDIV_OFFSET,
230 .recalc = &followparent_recalc,
231 //.recalc = &ckctl_recalc,
234 static struct clk dspxor_ck = {
237 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
238 DSP_DOMAIN_CLOCK | VIRTUAL_IO_ADDRESS,
239 .enable_reg = DSP_IDLECT2,
240 .enable_bit = EN_XORPCK,
241 .recalc = &followparent_recalc,
244 static struct clk dsptim_ck = {
247 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
248 DSP_DOMAIN_CLOCK | VIRTUAL_IO_ADDRESS,
249 .enable_reg = DSP_IDLECT2,
250 .enable_bit = EN_DSPTIMCK,
251 .recalc = &followparent_recalc,
254 static struct clk tc_ck = {
257 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX | CLOCK_IN_OMAP730 |
258 RATE_CKCTL | RATE_PROPAGATES | ALWAYS_ENABLED,
259 .rate_offset = CKCTL_TCDIV_OFFSET,
260 .recalc = &ckctl_recalc,
263 static struct clk arminth_ck1510 = {
264 .name = "arminth_ck",
266 .flags = CLOCK_IN_OMAP1510 | ALWAYS_ENABLED,
267 .recalc = &followparent_recalc,
268 /* Note: On 1510 the frequency follows TC_CK
270 * 16xx version is in MPU clocks.
274 static struct clk tipb_ck = {
277 .flags = CLOCK_IN_OMAP1510 | ALWAYS_ENABLED,
278 .recalc = &followparent_recalc,
281 static struct clk l3_ocpi_ck = {
282 .name = "l3_ocpi_ck",
284 .flags = CLOCK_IN_OMAP16XX,
285 .enable_reg = ARM_IDLECT3,
286 .enable_bit = EN_OCPI_CK,
287 .recalc = &followparent_recalc,
290 static struct clk tc1_ck = {
293 .flags = CLOCK_IN_OMAP16XX,
294 .enable_reg = ARM_IDLECT3,
295 .enable_bit = EN_TC1_CK,
296 .recalc = &followparent_recalc,
299 static struct clk tc2_ck = {
302 .flags = CLOCK_IN_OMAP16XX,
303 .enable_reg = ARM_IDLECT3,
304 .enable_bit = EN_TC2_CK,
305 .recalc = &followparent_recalc,
308 static struct clk dma_ck = {
311 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
313 .recalc = &followparent_recalc,
316 static struct clk dma_lcdfree_ck = {
317 .name = "dma_lcdfree_ck",
319 .flags = CLOCK_IN_OMAP16XX | ALWAYS_ENABLED,
320 .recalc = &followparent_recalc,
323 static struct clk api_ck = {
326 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX,
327 .enable_reg = ARM_IDLECT2,
328 .enable_bit = EN_APICK,
329 .recalc = &followparent_recalc,
332 static struct clk lb_ck = {
335 .flags = CLOCK_IN_OMAP1510,
336 .enable_reg = ARM_IDLECT2,
337 .enable_bit = EN_LBCK,
338 .recalc = &followparent_recalc,
341 static struct clk rhea1_ck = {
344 .flags = CLOCK_IN_OMAP16XX | ALWAYS_ENABLED,
345 .recalc = &followparent_recalc,
348 static struct clk rhea2_ck = {
351 .flags = CLOCK_IN_OMAP16XX | ALWAYS_ENABLED,
352 .recalc = &followparent_recalc,
355 static struct clk lcd_ck = {
358 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX | CLOCK_IN_OMAP730 |
360 .enable_reg = ARM_IDLECT2,
361 .enable_bit = EN_LCDCK,
362 .rate_offset = CKCTL_LCDDIV_OFFSET,
363 .recalc = &ckctl_recalc,
366 static struct clk uart1_1510 = {
368 /* Direct from ULPD, no parent */
370 .flags = CLOCK_IN_OMAP1510 | ENABLE_REG_32BIT | ALWAYS_ENABLED,
371 .enable_reg = MOD_CONF_CTRL_0,
372 .enable_bit = 29, /* Chooses between 12MHz and 48MHz */
373 .set_rate = &set_uart_rate,
374 .recalc = &uart_recalc,
377 static struct clk uart1_16xx = {
379 /* Direct from ULPD, no parent */
381 .flags = CLOCK_IN_OMAP16XX | RATE_FIXED | ENABLE_REG_32BIT,
382 .enable_reg = MOD_CONF_CTRL_0,
386 static struct clk uart2_ck = {
388 /* Direct from ULPD, no parent */
390 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX | ENABLE_REG_32BIT |
392 .enable_reg = MOD_CONF_CTRL_0,
393 .enable_bit = 30, /* Chooses between 12MHz and 48MHz */
394 .set_rate = &set_uart_rate,
395 .recalc = &uart_recalc,
398 static struct clk uart3_1510 = {
400 /* Direct from ULPD, no parent */
402 .flags = CLOCK_IN_OMAP1510 | ENABLE_REG_32BIT | ALWAYS_ENABLED,
403 .enable_reg = MOD_CONF_CTRL_0,
404 .enable_bit = 31, /* Chooses between 12MHz and 48MHz */
405 .set_rate = &set_uart_rate,
406 .recalc = &uart_recalc,
409 static struct clk uart3_16xx = {
411 /* Direct from ULPD, no parent */
413 .flags = CLOCK_IN_OMAP16XX | RATE_FIXED | ENABLE_REG_32BIT,
414 .enable_reg = MOD_CONF_CTRL_0,
418 static struct clk usb_clko = { /* 6 MHz output on W4_USB_CLKO */
420 /* Direct from ULPD, no parent */
422 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
423 RATE_FIXED | ENABLE_REG_32BIT,
424 .enable_reg = ULPD_CLOCK_CTRL,
425 .enable_bit = USB_MCLK_EN_BIT,
428 static struct clk usb_hhc_ck1510 = {
429 .name = "usb_hhc_ck",
430 /* Direct from ULPD, no parent */
431 .rate = 48000000, /* Actually 2 clocks, 12MHz and 48MHz */
432 .flags = CLOCK_IN_OMAP1510 |
433 RATE_FIXED | ENABLE_REG_32BIT,
434 .enable_reg = MOD_CONF_CTRL_0,
435 .enable_bit = USB_HOST_HHC_UHOST_EN,
438 static struct clk usb_hhc_ck16xx = {
439 .name = "usb_hhc_ck",
440 /* Direct from ULPD, no parent */
442 /* OTG_SYSCON_2.OTG_PADEN == 0 (not 1510-compatible) */
443 .flags = CLOCK_IN_OMAP16XX |
444 RATE_FIXED | ENABLE_REG_32BIT,
445 .enable_reg = OTG_BASE + 0x08 /* OTG_SYSCON_2 */,
446 .enable_bit = 8 /* UHOST_EN */,
449 static struct clk usb_dc_ck = {
451 /* Direct from ULPD, no parent */
453 .flags = CLOCK_IN_OMAP16XX | RATE_FIXED,
454 .enable_reg = SOFT_REQ_REG,
458 static struct clk mclk_1510 = {
460 /* Direct from ULPD, no parent. May be enabled by ext hardware. */
462 .flags = CLOCK_IN_OMAP1510 | RATE_FIXED,
465 static struct clk mclk_16xx = {
467 /* Direct from ULPD, no parent. May be enabled by ext hardware. */
468 .flags = CLOCK_IN_OMAP16XX,
469 .enable_reg = COM_CLK_DIV_CTRL_SEL,
470 .enable_bit = COM_ULPD_PLL_CLK_REQ,
471 .set_rate = &set_ext_clk_rate,
472 .round_rate = &round_ext_clk_rate,
473 .init = &init_ext_clk,
476 static struct clk bclk_1510 = {
478 /* Direct from ULPD, no parent. May be enabled by ext hardware. */
480 .flags = CLOCK_IN_OMAP1510 | RATE_FIXED,
483 static struct clk bclk_16xx = {
485 /* Direct from ULPD, no parent. May be enabled by ext hardware. */
486 .flags = CLOCK_IN_OMAP16XX,
487 .enable_reg = SWD_CLK_DIV_CTRL_SEL,
488 .enable_bit = SWD_ULPD_PLL_CLK_REQ,
489 .set_rate = &set_ext_clk_rate,
490 .round_rate = &round_ext_clk_rate,
491 .init = &init_ext_clk,
494 static struct clk mmc1_ck = {
496 /* Functional clock is direct from ULPD, interface clock is ARMPER */
497 .parent = &armper_ck,
499 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
500 RATE_FIXED | ENABLE_REG_32BIT,
501 .enable_reg = MOD_CONF_CTRL_0,
505 static struct clk mmc2_ck = {
507 /* Functional clock is direct from ULPD, interface clock is ARMPER */
508 .parent = &armper_ck,
510 .flags = CLOCK_IN_OMAP16XX |
511 RATE_FIXED | ENABLE_REG_32BIT,
512 .enable_reg = MOD_CONF_CTRL_0,
516 static struct clk virtual_ck_mpu = {
518 .flags = CLOCK_IN_OMAP1510 | CLOCK_IN_OMAP16XX |
519 VIRTUAL_CLOCK | ALWAYS_ENABLED,
520 .parent = &arm_ck, /* Is smarter alias for */
521 .recalc = &followparent_recalc,
522 .set_rate = &select_table_rate,
523 .round_rate = &round_to_table_rate,
527 static struct clk * onchip_clks[] = {
528 /* non-ULPD clocks */
539 &arminth_ck1510, &arminth_ck16xx,
566 &usb_hhc_ck1510, &usb_hhc_ck16xx,
568 &mclk_1510, &mclk_16xx,
569 &bclk_1510, &bclk_16xx,
576 struct clk *clk_get(struct device *dev, const char *id)
578 struct clk *p, *clk = ERR_PTR(-ENOENT);
581 list_for_each_entry(p, &clocks, node) {
582 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
591 EXPORT_SYMBOL(clk_get);
594 void clk_put(struct clk *clk)
596 if (clk && !IS_ERR(clk))
597 module_put(clk->owner);
599 EXPORT_SYMBOL(clk_put);
602 int __clk_enable(struct clk *clk)
607 if (clk->flags & ALWAYS_ENABLED)
610 if (unlikely(clk->enable_reg == 0)) {
611 printk(KERN_ERR "clock.c: Enable for %s without enable code\n",
616 if (clk->flags & DSP_DOMAIN_CLOCK) {
620 if (clk->flags & ENABLE_REG_32BIT) {
621 if (clk->flags & VIRTUAL_IO_ADDRESS) {
622 regval32 = __raw_readl(clk->enable_reg);
623 regval32 |= (1 << clk->enable_bit);
624 __raw_writel(regval32, clk->enable_reg);
626 regval32 = omap_readl(clk->enable_reg);
627 regval32 |= (1 << clk->enable_bit);
628 omap_writel(regval32, clk->enable_reg);
631 if (clk->flags & VIRTUAL_IO_ADDRESS) {
632 regval16 = __raw_readw(clk->enable_reg);
633 regval16 |= (1 << clk->enable_bit);
634 __raw_writew(regval16, clk->enable_reg);
636 regval16 = omap_readw(clk->enable_reg);
637 regval16 |= (1 << clk->enable_bit);
638 omap_writew(regval16, clk->enable_reg);
642 if (clk->flags & DSP_DOMAIN_CLOCK) {
643 __clk_unuse(&api_ck);
650 void __clk_disable(struct clk *clk)
655 if (clk->enable_reg == 0)
658 if (clk->flags & DSP_DOMAIN_CLOCK) {
662 if (clk->flags & ENABLE_REG_32BIT) {
663 if (clk->flags & VIRTUAL_IO_ADDRESS) {
664 regval32 = __raw_readl(clk->enable_reg);
665 regval32 &= ~(1 << clk->enable_bit);
666 __raw_writel(regval32, clk->enable_reg);
668 regval32 = omap_readl(clk->enable_reg);
669 regval32 &= ~(1 << clk->enable_bit);
670 omap_writel(regval32, clk->enable_reg);
673 if (clk->flags & VIRTUAL_IO_ADDRESS) {
674 regval16 = __raw_readw(clk->enable_reg);
675 regval16 &= ~(1 << clk->enable_bit);
676 __raw_writew(regval16, clk->enable_reg);
678 regval16 = omap_readw(clk->enable_reg);
679 regval16 &= ~(1 << clk->enable_bit);
680 omap_writew(regval16, clk->enable_reg);
684 if (clk->flags & DSP_DOMAIN_CLOCK) {
685 __clk_unuse(&api_ck);
690 void __clk_unuse(struct clk *clk)
692 if (clk->usecount > 0 && !(--clk->usecount)) {
694 if (likely(clk->parent))
695 __clk_unuse(clk->parent);
700 int __clk_use(struct clk *clk)
703 if (clk->usecount++ == 0) {
704 if (likely(clk->parent))
705 ret = __clk_use(clk->parent);
707 if (unlikely(ret != 0)) {
712 ret = __clk_enable(clk);
714 if (unlikely(ret != 0) && clk->parent) {
715 __clk_unuse(clk->parent);
724 int clk_enable(struct clk *clk)
729 spin_lock_irqsave(&clockfw_lock, flags);
730 ret = __clk_enable(clk);
731 spin_unlock_irqrestore(&clockfw_lock, flags);
734 EXPORT_SYMBOL(clk_enable);
737 void clk_disable(struct clk *clk)
741 spin_lock_irqsave(&clockfw_lock, flags);
743 spin_unlock_irqrestore(&clockfw_lock, flags);
745 EXPORT_SYMBOL(clk_disable);
748 int clk_use(struct clk *clk)
753 spin_lock_irqsave(&clockfw_lock, flags);
754 ret = __clk_use(clk);
755 spin_unlock_irqrestore(&clockfw_lock, flags);
758 EXPORT_SYMBOL(clk_use);
761 void clk_unuse(struct clk *clk)
765 spin_lock_irqsave(&clockfw_lock, flags);
767 spin_unlock_irqrestore(&clockfw_lock, flags);
769 EXPORT_SYMBOL(clk_unuse);
772 int clk_get_usecount(struct clk *clk)
774 return clk->usecount;
776 EXPORT_SYMBOL(clk_get_usecount);
779 unsigned long clk_get_rate(struct clk *clk)
783 EXPORT_SYMBOL(clk_get_rate);
786 static __u16 verify_ckctl_value(__u16 newval)
788 /* This function checks for following limitations set
789 * by the hardware (all conditions must be true):
790 * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
795 * In addition following rules are enforced:
799 * However, maximum frequencies are not checked for!
808 per_exp = (newval >> CKCTL_PERDIV_OFFSET) & 3;
809 lcd_exp = (newval >> CKCTL_LCDDIV_OFFSET) & 3;
810 arm_exp = (newval >> CKCTL_ARMDIV_OFFSET) & 3;
811 dsp_exp = (newval >> CKCTL_DSPDIV_OFFSET) & 3;
812 tc_exp = (newval >> CKCTL_TCDIV_OFFSET) & 3;
813 dspmmu_exp = (newval >> CKCTL_DSPMMUDIV_OFFSET) & 3;
815 if (dspmmu_exp < dsp_exp)
816 dspmmu_exp = dsp_exp;
817 if (dspmmu_exp > dsp_exp+1)
818 dspmmu_exp = dsp_exp+1;
819 if (tc_exp < arm_exp)
821 if (tc_exp < dspmmu_exp)
823 if (tc_exp > lcd_exp)
825 if (tc_exp > per_exp)
829 newval |= per_exp << CKCTL_PERDIV_OFFSET;
830 newval |= lcd_exp << CKCTL_LCDDIV_OFFSET;
831 newval |= arm_exp << CKCTL_ARMDIV_OFFSET;
832 newval |= dsp_exp << CKCTL_DSPDIV_OFFSET;
833 newval |= tc_exp << CKCTL_TCDIV_OFFSET;
834 newval |= dspmmu_exp << CKCTL_DSPMMUDIV_OFFSET;
840 static int calc_dsor_exp(struct clk *clk, unsigned long rate)
842 /* Note: If target frequency is too low, this function will return 4,
843 * which is invalid value. Caller must check for this value and act
846 * Note: This function does not check for following limitations set
847 * by the hardware (all conditions must be true):
848 * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
853 unsigned long realrate;
857 if (unlikely(!(clk->flags & RATE_CKCTL)))
860 parent = clk->parent;
861 if (unlikely(parent == 0))
864 realrate = parent->rate;
865 for (dsor_exp=0; dsor_exp<4; dsor_exp++) {
866 if (realrate <= rate)
876 static void ckctl_recalc(struct clk * clk)
880 /* Calculate divisor encoded as 2-bit exponent */
881 if (clk->flags & DSP_DOMAIN_CLOCK) {
882 /* The clock control bits are in DSP domain,
883 * so api_ck is needed for access.
884 * Note that DSP_CKCTL virt addr = phys addr, so
885 * we must use __raw_readw() instead of omap_readw().
888 dsor = 1 << (3 & (__raw_readw(DSP_CKCTL) >> clk->rate_offset));
889 __clk_unuse(&api_ck);
891 dsor = 1 << (3 & (omap_readw(ARM_CKCTL) >> clk->rate_offset));
893 if (unlikely(clk->rate == clk->parent->rate / dsor))
894 return; /* No change, quick exit */
895 clk->rate = clk->parent->rate / dsor;
897 if (unlikely(clk->flags & RATE_PROPAGATES))
902 long clk_round_rate(struct clk *clk, unsigned long rate)
906 if (clk->flags & RATE_FIXED)
909 if (clk->flags & RATE_CKCTL) {
910 dsor_exp = calc_dsor_exp(clk, rate);
915 return clk->parent->rate / (1 << dsor_exp);
918 if(clk->round_rate != 0)
919 return clk->round_rate(clk, rate);
923 EXPORT_SYMBOL(clk_round_rate);
926 static void propagate_rate(struct clk * clk)
930 for (clkp = onchip_clks; clkp < onchip_clks+ARRAY_SIZE(onchip_clks); clkp++) {
931 if (likely((*clkp)->parent != clk)) continue;
932 if (likely((*clkp)->recalc))
933 (*clkp)->recalc(*clkp);
938 static int select_table_rate(struct clk * clk, unsigned long rate)
940 /* Find the highest supported frequency <= rate and switch to it */
941 struct mpu_rate * ptr;
943 if (clk != &virtual_ck_mpu)
946 for (ptr = rate_table; ptr->rate; ptr++) {
947 if (ptr->xtal != ck_ref.rate)
950 /* DPLL1 cannot be reprogrammed without risking system crash */
951 if (likely(ck_dpll1.rate!=0) && ptr->pll_rate != ck_dpll1.rate)
954 /* Can check only after xtal frequency check */
955 if (ptr->rate <= rate)
963 * In most cases we should not need to reprogram DPLL.
964 * Reprogramming the DPLL is tricky, it must be done from SRAM.
966 omap_sram_reprogram_clock(ptr->dpllctl_val, ptr->ckctl_val);
968 ck_dpll1.rate = ptr->pll_rate;
969 propagate_rate(&ck_dpll1);
974 static long round_to_table_rate(struct clk * clk, unsigned long rate)
976 /* Find the highest supported frequency <= rate */
977 struct mpu_rate * ptr;
980 if (clk != &virtual_ck_mpu)
983 highest_rate = -EINVAL;
985 for (ptr = rate_table; ptr->rate; ptr++) {
986 if (ptr->xtal != ck_ref.rate)
989 highest_rate = ptr->rate;
991 /* Can check only after xtal frequency check */
992 if (ptr->rate <= rate)
1000 int clk_set_rate(struct clk *clk, unsigned long rate)
1005 unsigned long flags;
1007 if (clk->flags & RATE_CKCTL) {
1008 dsor_exp = calc_dsor_exp(clk, rate);
1014 spin_lock_irqsave(&clockfw_lock, flags);
1015 regval = omap_readw(ARM_CKCTL);
1016 regval &= ~(3 << clk->rate_offset);
1017 regval |= dsor_exp << clk->rate_offset;
1018 regval = verify_ckctl_value(regval);
1019 omap_writew(regval, ARM_CKCTL);
1020 clk->rate = clk->parent->rate / (1 << dsor_exp);
1021 spin_unlock_irqrestore(&clockfw_lock, flags);
1023 } else if(clk->set_rate != 0) {
1024 spin_lock_irqsave(&clockfw_lock, flags);
1025 ret = clk->set_rate(clk, rate);
1026 spin_unlock_irqrestore(&clockfw_lock, flags);
1029 if (unlikely(ret == 0 && (clk->flags & RATE_PROPAGATES)))
1030 propagate_rate(clk);
1034 EXPORT_SYMBOL(clk_set_rate);
1037 static unsigned calc_ext_dsor(unsigned long rate)
1041 /* MCLK and BCLK divisor selection is not linear:
1042 * freq = 96MHz / dsor
1044 * RATIO_SEL range: dsor <-> RATIO_SEL
1045 * 0..6: (RATIO_SEL+2) <-> (dsor-2)
1046 * 6..48: (8+(RATIO_SEL-6)*2) <-> ((dsor-8)/2+6)
1047 * Minimum dsor is 2 and maximum is 96. Odd divisors starting from 9
1050 for (dsor = 2; dsor < 96; ++dsor) {
1051 if ((dsor & 1) && dsor > 8)
1053 if (rate >= 96000000 / dsor)
1059 /* Only needed on 1510 */
1060 static int set_uart_rate(struct clk * clk, unsigned long rate)
1064 val = omap_readl(clk->enable_reg);
1065 if (rate == 12000000)
1066 val &= ~(1 << clk->enable_bit);
1067 else if (rate == 48000000)
1068 val |= (1 << clk->enable_bit);
1071 omap_writel(val, clk->enable_reg);
1077 static int set_ext_clk_rate(struct clk * clk, unsigned long rate)
1082 dsor = calc_ext_dsor(rate);
1083 clk->rate = 96000000 / dsor;
1085 ratio_bits = ((dsor - 8) / 2 + 6) << 2;
1087 ratio_bits = (dsor - 2) << 2;
1089 ratio_bits |= omap_readw(clk->enable_reg) & ~0xfd;
1090 omap_writew(ratio_bits, clk->enable_reg);
1096 static long round_ext_clk_rate(struct clk * clk, unsigned long rate)
1098 return 96000000 / calc_ext_dsor(rate);
1102 static void init_ext_clk(struct clk * clk)
1107 /* Determine current rate and ensure clock is based on 96MHz APLL */
1108 ratio_bits = omap_readw(clk->enable_reg) & ~1;
1109 omap_writew(ratio_bits, clk->enable_reg);
1111 ratio_bits = (ratio_bits & 0xfc) >> 2;
1113 dsor = (ratio_bits - 6) * 2 + 8;
1115 dsor = ratio_bits + 2;
1117 clk-> rate = 96000000 / dsor;
1121 int clk_register(struct clk *clk)
1124 list_add(&clk->node, &clocks);
1130 EXPORT_SYMBOL(clk_register);
1132 void clk_unregister(struct clk *clk)
1135 list_del(&clk->node);
1138 EXPORT_SYMBOL(clk_unregister);
1140 #ifdef CONFIG_OMAP_RESET_CLOCKS
1142 * Resets some clocks that may be left on from bootloader,
1143 * but leaves serial clocks on. See also omap_late_clk_reset().
1145 static inline void omap_early_clk_reset(void)
1147 //omap_writel(0x3 << 29, MOD_CONF_CTRL_0);
1150 #define omap_early_clk_reset() {}
1153 int __init clk_init(void)
1156 const struct omap_clock_config *info;
1157 int crystal_type = 0; /* Default 12 MHz */
1159 omap_early_clk_reset();
1161 for (clkp = onchip_clks; clkp < onchip_clks+ARRAY_SIZE(onchip_clks); clkp++) {
1162 if (((*clkp)->flags &CLOCK_IN_OMAP1510) && cpu_is_omap1510()) {
1163 clk_register(*clkp);
1167 if (((*clkp)->flags &CLOCK_IN_OMAP16XX) && cpu_is_omap16xx()) {
1168 clk_register(*clkp);
1172 if (((*clkp)->flags &CLOCK_IN_OMAP730) && cpu_is_omap730()) {
1173 clk_register(*clkp);
1178 info = omap_get_config(OMAP_TAG_CLOCK, struct omap_clock_config);
1180 if (!cpu_is_omap1510())
1181 crystal_type = info->system_clock_type;
1184 #if defined(CONFIG_ARCH_OMAP730)
1185 ck_ref.rate = 13000000;
1186 #elif defined(CONFIG_ARCH_OMAP16XX)
1187 if (crystal_type == 2)
1188 ck_ref.rate = 19200000;
1191 printk("Clocks: ARM_SYSST: 0x%04x DPLL_CTL: 0x%04x ARM_CKCTL: 0x%04x\n",
1192 omap_readw(ARM_SYSST), omap_readw(DPLL_CTL),
1193 omap_readw(ARM_CKCTL));
1195 /* We want to be in syncronous scalable mode */
1196 omap_writew(0x1000, ARM_SYSST);
1198 #ifdef CONFIG_OMAP_CLOCKS_SET_BY_BOOTLOADER
1199 /* Use values set by bootloader. Determine PLL rate and recalculate
1200 * dependent clocks as if kernel had changed PLL or divisors.
1203 unsigned pll_ctl_val = omap_readw(DPLL_CTL);
1205 ck_dpll1.rate = ck_ref.rate; /* Base xtal rate */
1206 if (pll_ctl_val & 0x10) {
1207 /* PLL enabled, apply multiplier and divisor */
1208 if (pll_ctl_val & 0xf80)
1209 ck_dpll1.rate *= (pll_ctl_val & 0xf80) >> 7;
1210 ck_dpll1.rate /= ((pll_ctl_val & 0x60) >> 5) + 1;
1212 /* PLL disabled, apply bypass divisor */
1213 switch (pll_ctl_val & 0xc) {
1225 propagate_rate(&ck_dpll1);
1227 /* Find the highest supported frequency and enable it */
1228 if (select_table_rate(&virtual_ck_mpu, ~0)) {
1229 printk(KERN_ERR "System frequencies not set. Check your config.\n");
1230 /* Guess sane values (60MHz) */
1231 omap_writew(0x2290, DPLL_CTL);
1232 omap_writew(0x1005, ARM_CKCTL);
1233 ck_dpll1.rate = 60000000;
1234 propagate_rate(&ck_dpll1);
1237 /* Cache rates for clocks connected to ck_ref (not dpll1) */
1238 propagate_rate(&ck_ref);
1239 printk(KERN_INFO "Clocking rate (xtal/DPLL1/MPU): "
1240 "%ld.%01ld/%ld.%01ld/%ld.%01ld MHz\n",
1241 ck_ref.rate / 1000000, (ck_ref.rate / 100000) % 10,
1242 ck_dpll1.rate / 1000000, (ck_dpll1.rate / 100000) % 10,
1243 arm_ck.rate / 1000000, (arm_ck.rate / 100000) % 10);
1245 #ifdef CONFIG_MACH_OMAP_PERSEUS2
1246 /* Select slicer output as OMAP input clock */
1247 omap_writew(omap_readw(OMAP730_PCC_UPLD_CTRL) & ~0x1, OMAP730_PCC_UPLD_CTRL);
1250 /* Turn off DSP and ARM_TIMXO. Make sure ARM_INTHCK is not divided */
1251 omap_writew(omap_readw(ARM_CKCTL) & 0x0fff, ARM_CKCTL);
1253 /* Put DSP/MPUI into reset until needed */
1254 omap_writew(0, ARM_RSTCT1);
1255 omap_writew(1, ARM_RSTCT2);
1256 omap_writew(0x400, ARM_IDLECT1);
1259 * According to OMAP5910 Erratum SYS_DMA_1, bit DMACK_REQ (bit 8)
1260 * of the ARM_IDLECT2 register must be set to zero. The power-on
1261 * default value of this bit is one.
1263 omap_writew(0x0000, ARM_IDLECT2); /* Turn LCD clock off also */
1266 * Only enable those clocks we will need, let the drivers
1267 * enable other clocks as necessary
1269 clk_use(&armper_ck);
1270 clk_use(&armxor_ck);
1271 clk_use(&armtim_ck);
1273 if (cpu_is_omap1510())
1274 clk_enable(&arm_gpio_ck);
1280 #ifdef CONFIG_OMAP_RESET_CLOCKS
1282 static int __init omap_late_clk_reset(void)
1284 /* Turn off all unused clocks */
1288 /* USB_REQ_EN will be disabled later if necessary (usb_dc_ck) */
1289 regval32 = omap_readw(SOFT_REQ_REG) & (1 << 4);
1290 omap_writew(regval32, SOFT_REQ_REG);
1291 omap_writew(0, SOFT_REQ_REG2);
1293 list_for_each_entry(p, &clocks, node) {
1294 if (p->usecount > 0 || (p->flags & ALWAYS_ENABLED) ||
1298 /* Assume no DSP clocks have been activated by bootloader */
1299 if (p->flags & DSP_DOMAIN_CLOCK)
1302 /* Is the clock already disabled? */
1303 if (p->flags & ENABLE_REG_32BIT) {
1304 if (p->flags & VIRTUAL_IO_ADDRESS)
1305 regval32 = __raw_readl(p->enable_reg);
1307 regval32 = omap_readl(p->enable_reg);
1309 if (p->flags & VIRTUAL_IO_ADDRESS)
1310 regval32 = __raw_readw(p->enable_reg);
1312 regval32 = omap_readw(p->enable_reg);
1315 if ((regval32 & (1 << p->enable_bit)) == 0)
1318 /* FIXME: This clock seems to be necessary but no-one
1319 * has asked for its activation. */
1320 if (p == &tc2_ck // FIX: pm.c (SRAM), CCP, Camera
1321 || p == &ck_dpll1out // FIX: SoSSI, SSR
1322 || p == &arm_gpio_ck // FIX: GPIO code for 1510
1324 printk(KERN_INFO "FIXME: Clock \"%s\" seems unused\n",
1329 printk(KERN_INFO "Disabling unused clock \"%s\"... ", p->name);
1337 late_initcall(omap_late_clk_reset);