2 * arch/arm/mach-ep93xx/clock.c
3 * Clock control for Cirrus EP93xx chips.
5 * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at
10 * your option) any later version.
13 #define pr_fmt(fmt) "ep93xx " KBUILD_MODNAME ": " fmt
15 #include <linux/kernel.h>
16 #include <linux/clk.h>
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/string.h>
21 #include <linux/spinlock.h>
22 #include <linux/clkdev.h>
24 #include <mach/hardware.h>
26 #include <asm/div64.h>
35 void __iomem
*enable_reg
;
38 unsigned long (*get_rate
)(struct clk
*clk
);
39 int (*set_rate
)(struct clk
*clk
, unsigned long rate
);
43 static unsigned long get_uart_rate(struct clk
*clk
);
45 static int set_keytchclk_rate(struct clk
*clk
, unsigned long rate
);
46 static int set_div_rate(struct clk
*clk
, unsigned long rate
);
47 static int set_i2s_sclk_rate(struct clk
*clk
, unsigned long rate
);
48 static int set_i2s_lrclk_rate(struct clk
*clk
, unsigned long rate
);
50 static struct clk clk_xtali
= {
51 .rate
= EP93XX_EXT_CLK_RATE
,
53 static struct clk clk_uart1
= {
56 .enable_reg
= EP93XX_SYSCON_DEVCFG
,
57 .enable_mask
= EP93XX_SYSCON_DEVCFG_U1EN
,
58 .get_rate
= get_uart_rate
,
60 static struct clk clk_uart2
= {
63 .enable_reg
= EP93XX_SYSCON_DEVCFG
,
64 .enable_mask
= EP93XX_SYSCON_DEVCFG_U2EN
,
65 .get_rate
= get_uart_rate
,
67 static struct clk clk_uart3
= {
70 .enable_reg
= EP93XX_SYSCON_DEVCFG
,
71 .enable_mask
= EP93XX_SYSCON_DEVCFG_U3EN
,
72 .get_rate
= get_uart_rate
,
74 static struct clk clk_pll1
= {
77 static struct clk clk_f
= {
80 static struct clk clk_h
= {
83 static struct clk clk_p
= {
86 static struct clk clk_pll2
= {
89 static struct clk clk_usb_host
= {
91 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
92 .enable_mask
= EP93XX_SYSCON_PWRCNT_USH_EN
,
94 static struct clk clk_keypad
= {
97 .enable_reg
= EP93XX_SYSCON_KEYTCHCLKDIV
,
98 .enable_mask
= EP93XX_SYSCON_KEYTCHCLKDIV_KEN
,
99 .set_rate
= set_keytchclk_rate
,
101 static struct clk clk_adc
= {
102 .parent
= &clk_xtali
,
104 .enable_reg
= EP93XX_SYSCON_KEYTCHCLKDIV
,
105 .enable_mask
= EP93XX_SYSCON_KEYTCHCLKDIV_TSEN
,
106 .set_rate
= set_keytchclk_rate
,
108 static struct clk clk_spi
= {
109 .parent
= &clk_xtali
,
110 .rate
= EP93XX_EXT_CLK_RATE
,
112 static struct clk clk_pwm
= {
113 .parent
= &clk_xtali
,
114 .rate
= EP93XX_EXT_CLK_RATE
,
117 static struct clk clk_video
= {
119 .enable_reg
= EP93XX_SYSCON_VIDCLKDIV
,
120 .enable_mask
= EP93XX_SYSCON_CLKDIV_ENABLE
,
121 .set_rate
= set_div_rate
,
124 static struct clk clk_i2s_mclk
= {
126 .enable_reg
= EP93XX_SYSCON_I2SCLKDIV
,
127 .enable_mask
= EP93XX_SYSCON_CLKDIV_ENABLE
,
128 .set_rate
= set_div_rate
,
131 static struct clk clk_i2s_sclk
= {
133 .parent
= &clk_i2s_mclk
,
134 .enable_reg
= EP93XX_SYSCON_I2SCLKDIV
,
135 .enable_mask
= EP93XX_SYSCON_I2SCLKDIV_SENA
,
136 .set_rate
= set_i2s_sclk_rate
,
139 static struct clk clk_i2s_lrclk
= {
141 .parent
= &clk_i2s_sclk
,
142 .enable_reg
= EP93XX_SYSCON_I2SCLKDIV
,
143 .enable_mask
= EP93XX_SYSCON_I2SCLKDIV_SENA
,
144 .set_rate
= set_i2s_lrclk_rate
,
148 static struct clk clk_m2p0
= {
150 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
151 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P0
,
153 static struct clk clk_m2p1
= {
155 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
156 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P1
,
158 static struct clk clk_m2p2
= {
160 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
161 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P2
,
163 static struct clk clk_m2p3
= {
165 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
166 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P3
,
168 static struct clk clk_m2p4
= {
170 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
171 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P4
,
173 static struct clk clk_m2p5
= {
175 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
176 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P5
,
178 static struct clk clk_m2p6
= {
180 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
181 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P6
,
183 static struct clk clk_m2p7
= {
185 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
186 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P7
,
188 static struct clk clk_m2p8
= {
190 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
191 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P8
,
193 static struct clk clk_m2p9
= {
195 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
196 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P9
,
198 static struct clk clk_m2m0
= {
200 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
201 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2M0
,
203 static struct clk clk_m2m1
= {
205 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
206 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2M1
,
209 #define INIT_CK(dev,con,ck) \
210 { .dev_id = dev, .con_id = con, .clk = ck }
212 static struct clk_lookup clocks
[] = {
213 INIT_CK(NULL
, "xtali", &clk_xtali
),
214 INIT_CK("apb:uart1", NULL
, &clk_uart1
),
215 INIT_CK("apb:uart2", NULL
, &clk_uart2
),
216 INIT_CK("apb:uart3", NULL
, &clk_uart3
),
217 INIT_CK(NULL
, "pll1", &clk_pll1
),
218 INIT_CK(NULL
, "fclk", &clk_f
),
219 INIT_CK(NULL
, "hclk", &clk_h
),
220 INIT_CK(NULL
, "apb_pclk", &clk_p
),
221 INIT_CK(NULL
, "pll2", &clk_pll2
),
222 INIT_CK("ohci-platform", NULL
, &clk_usb_host
),
223 INIT_CK("ep93xx-keypad", NULL
, &clk_keypad
),
224 INIT_CK("ep93xx-adc", NULL
, &clk_adc
),
225 INIT_CK("ep93xx-fb", NULL
, &clk_video
),
226 INIT_CK("ep93xx-spi.0", NULL
, &clk_spi
),
227 INIT_CK("ep93xx-i2s", "mclk", &clk_i2s_mclk
),
228 INIT_CK("ep93xx-i2s", "sclk", &clk_i2s_sclk
),
229 INIT_CK("ep93xx-i2s", "lrclk", &clk_i2s_lrclk
),
230 INIT_CK(NULL
, "pwm_clk", &clk_pwm
),
231 INIT_CK(NULL
, "m2p0", &clk_m2p0
),
232 INIT_CK(NULL
, "m2p1", &clk_m2p1
),
233 INIT_CK(NULL
, "m2p2", &clk_m2p2
),
234 INIT_CK(NULL
, "m2p3", &clk_m2p3
),
235 INIT_CK(NULL
, "m2p4", &clk_m2p4
),
236 INIT_CK(NULL
, "m2p5", &clk_m2p5
),
237 INIT_CK(NULL
, "m2p6", &clk_m2p6
),
238 INIT_CK(NULL
, "m2p7", &clk_m2p7
),
239 INIT_CK(NULL
, "m2p8", &clk_m2p8
),
240 INIT_CK(NULL
, "m2p9", &clk_m2p9
),
241 INIT_CK(NULL
, "m2m0", &clk_m2m0
),
242 INIT_CK(NULL
, "m2m1", &clk_m2m1
),
245 static DEFINE_SPINLOCK(clk_lock
);
247 static void __clk_enable(struct clk
*clk
)
251 __clk_enable(clk
->parent
);
253 if (clk
->enable_reg
) {
256 v
= __raw_readl(clk
->enable_reg
);
257 v
|= clk
->enable_mask
;
259 ep93xx_syscon_swlocked_write(v
, clk
->enable_reg
);
261 __raw_writel(v
, clk
->enable_reg
);
266 int clk_enable(struct clk
*clk
)
273 spin_lock_irqsave(&clk_lock
, flags
);
275 spin_unlock_irqrestore(&clk_lock
, flags
);
279 EXPORT_SYMBOL(clk_enable
);
281 static void __clk_disable(struct clk
*clk
)
284 if (clk
->enable_reg
) {
287 v
= __raw_readl(clk
->enable_reg
);
288 v
&= ~clk
->enable_mask
;
290 ep93xx_syscon_swlocked_write(v
, clk
->enable_reg
);
292 __raw_writel(v
, clk
->enable_reg
);
296 __clk_disable(clk
->parent
);
300 void clk_disable(struct clk
*clk
)
307 spin_lock_irqsave(&clk_lock
, flags
);
309 spin_unlock_irqrestore(&clk_lock
, flags
);
311 EXPORT_SYMBOL(clk_disable
);
313 static unsigned long get_uart_rate(struct clk
*clk
)
315 unsigned long rate
= clk_get_rate(clk
->parent
);
318 value
= __raw_readl(EP93XX_SYSCON_PWRCNT
);
319 if (value
& EP93XX_SYSCON_PWRCNT_UARTBAUD
)
325 unsigned long clk_get_rate(struct clk
*clk
)
328 return clk
->get_rate(clk
);
332 EXPORT_SYMBOL(clk_get_rate
);
334 static int set_keytchclk_rate(struct clk
*clk
, unsigned long rate
)
339 val
= __raw_readl(clk
->enable_reg
);
342 * The Key Matrix and ADC clocks are configured using the same
343 * System Controller register. The clock used will be either
344 * 1/4 or 1/16 the external clock rate depending on the
345 * EP93XX_SYSCON_KEYTCHCLKDIV_KDIV/EP93XX_SYSCON_KEYTCHCLKDIV_ADIV
346 * bit being set or cleared.
348 div_bit
= clk
->enable_mask
>> 15;
350 if (rate
== EP93XX_KEYTCHCLK_DIV4
)
352 else if (rate
== EP93XX_KEYTCHCLK_DIV16
)
357 ep93xx_syscon_swlocked_write(val
, clk
->enable_reg
);
362 static int calc_clk_div(struct clk
*clk
, unsigned long rate
,
363 int *psel
, int *esel
, int *pdiv
, int *div
)
366 unsigned long max_rate
, actual_rate
, mclk_rate
, rate_err
= -1;
367 int i
, found
= 0, __div
= 0, __pdiv
= 0;
369 /* Don't exceed the maximum rate */
370 max_rate
= max3(clk_pll1
.rate
/ 4, clk_pll2
.rate
/ 4, clk_xtali
.rate
/ 4);
371 rate
= min(rate
, max_rate
);
374 * Try the two pll's and the external clock
375 * Because the valid predividers are 2, 2.5 and 3, we multiply
376 * all the clocks by 2 to avoid floating point math.
378 * This is based on the algorithm in the ep93xx raster guide:
379 * http://be-a-maverick.com/en/pubs/appNote/AN269REV1.pdf
382 for (i
= 0; i
< 3; i
++) {
389 mclk_rate
= mclk
->rate
* 2;
391 /* Try each predivider value */
392 for (__pdiv
= 4; __pdiv
<= 6; __pdiv
++) {
393 __div
= mclk_rate
/ (rate
* __pdiv
);
394 if (__div
< 2 || __div
> 127)
397 actual_rate
= mclk_rate
/ (__pdiv
* __div
);
399 if (!found
|| abs(actual_rate
- rate
) < rate_err
) {
405 clk
->rate
= actual_rate
;
406 rate_err
= abs(actual_rate
- rate
);
418 static int set_div_rate(struct clk
*clk
, unsigned long rate
)
420 int err
, psel
= 0, esel
= 0, pdiv
= 0, div
= 0;
423 err
= calc_clk_div(clk
, rate
, &psel
, &esel
, &pdiv
, &div
);
427 /* Clear the esel, psel, pdiv and div bits */
428 val
= __raw_readl(clk
->enable_reg
);
431 /* Set the new esel, psel, pdiv and div bits for the new clock rate */
432 val
|= (esel
? EP93XX_SYSCON_CLKDIV_ESEL
: 0) |
433 (psel
? EP93XX_SYSCON_CLKDIV_PSEL
: 0) |
434 (pdiv
<< EP93XX_SYSCON_CLKDIV_PDIV_SHIFT
) | div
;
435 ep93xx_syscon_swlocked_write(val
, clk
->enable_reg
);
439 static int set_i2s_sclk_rate(struct clk
*clk
, unsigned long rate
)
441 unsigned val
= __raw_readl(clk
->enable_reg
);
443 if (rate
== clk_i2s_mclk
.rate
/ 2)
444 ep93xx_syscon_swlocked_write(val
& ~EP93XX_I2SCLKDIV_SDIV
,
446 else if (rate
== clk_i2s_mclk
.rate
/ 4)
447 ep93xx_syscon_swlocked_write(val
| EP93XX_I2SCLKDIV_SDIV
,
452 clk_i2s_sclk
.rate
= rate
;
456 static int set_i2s_lrclk_rate(struct clk
*clk
, unsigned long rate
)
458 unsigned val
= __raw_readl(clk
->enable_reg
) &
459 ~EP93XX_I2SCLKDIV_LRDIV_MASK
;
461 if (rate
== clk_i2s_sclk
.rate
/ 32)
462 ep93xx_syscon_swlocked_write(val
| EP93XX_I2SCLKDIV_LRDIV32
,
464 else if (rate
== clk_i2s_sclk
.rate
/ 64)
465 ep93xx_syscon_swlocked_write(val
| EP93XX_I2SCLKDIV_LRDIV64
,
467 else if (rate
== clk_i2s_sclk
.rate
/ 128)
468 ep93xx_syscon_swlocked_write(val
| EP93XX_I2SCLKDIV_LRDIV128
,
473 clk_i2s_lrclk
.rate
= rate
;
477 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
480 return clk
->set_rate(clk
, rate
);
484 EXPORT_SYMBOL(clk_set_rate
);
486 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
491 EXPORT_SYMBOL(clk_round_rate
);
493 int clk_set_parent(struct clk
*clk
, struct clk
*parent
)
498 EXPORT_SYMBOL(clk_set_parent
);
500 struct clk
*clk_get_parent(struct clk
*clk
)
504 EXPORT_SYMBOL(clk_get_parent
);
507 static char fclk_divisors
[] = { 1, 2, 4, 8, 16, 1, 1, 1 };
508 static char hclk_divisors
[] = { 1, 2, 4, 5, 6, 8, 16, 32 };
509 static char pclk_divisors
[] = { 1, 2, 4, 8 };
512 * PLL rate = 14.7456 MHz * (X1FBD + 1) * (X2FBD + 1) / (X2IPD + 1) / 2^PS
514 static unsigned long calc_pll_rate(u32 config_word
)
516 unsigned long long rate
;
519 rate
= clk_xtali
.rate
;
520 rate
*= ((config_word
>> 11) & 0x1f) + 1; /* X1FBD */
521 rate
*= ((config_word
>> 5) & 0x3f) + 1; /* X2FBD */
522 do_div(rate
, (config_word
& 0x1f) + 1); /* X2IPD */
523 for (i
= 0; i
< ((config_word
>> 16) & 3); i
++) /* PS */
526 return (unsigned long)rate
;
529 static void __init
ep93xx_dma_clock_init(void)
531 clk_m2p0
.rate
= clk_h
.rate
;
532 clk_m2p1
.rate
= clk_h
.rate
;
533 clk_m2p2
.rate
= clk_h
.rate
;
534 clk_m2p3
.rate
= clk_h
.rate
;
535 clk_m2p4
.rate
= clk_h
.rate
;
536 clk_m2p5
.rate
= clk_h
.rate
;
537 clk_m2p6
.rate
= clk_h
.rate
;
538 clk_m2p7
.rate
= clk_h
.rate
;
539 clk_m2p8
.rate
= clk_h
.rate
;
540 clk_m2p9
.rate
= clk_h
.rate
;
541 clk_m2m0
.rate
= clk_h
.rate
;
542 clk_m2m1
.rate
= clk_h
.rate
;
545 static int __init
ep93xx_clock_init(void)
549 /* Determine the bootloader configured pll1 rate */
550 value
= __raw_readl(EP93XX_SYSCON_CLKSET1
);
551 if (!(value
& EP93XX_SYSCON_CLKSET1_NBYP1
))
552 clk_pll1
.rate
= clk_xtali
.rate
;
554 clk_pll1
.rate
= calc_pll_rate(value
);
556 /* Initialize the pll1 derived clocks */
557 clk_f
.rate
= clk_pll1
.rate
/ fclk_divisors
[(value
>> 25) & 0x7];
558 clk_h
.rate
= clk_pll1
.rate
/ hclk_divisors
[(value
>> 20) & 0x7];
559 clk_p
.rate
= clk_h
.rate
/ pclk_divisors
[(value
>> 18) & 0x3];
560 ep93xx_dma_clock_init();
562 /* Determine the bootloader configured pll2 rate */
563 value
= __raw_readl(EP93XX_SYSCON_CLKSET2
);
564 if (!(value
& EP93XX_SYSCON_CLKSET2_NBYP2
))
565 clk_pll2
.rate
= clk_xtali
.rate
;
566 else if (value
& EP93XX_SYSCON_CLKSET2_PLL2_EN
)
567 clk_pll2
.rate
= calc_pll_rate(value
);
571 /* Initialize the pll2 derived clocks */
572 clk_usb_host
.rate
= clk_pll2
.rate
/ (((value
>> 28) & 0xf) + 1);
575 * EP93xx SSP clock rate was doubled in version E2. For more information
577 * http://www.cirrus.com/en/pubs/appNote/AN273REV4.pdf
579 if (ep93xx_chip_revision() < EP93XX_CHIP_REV_E2
)
582 pr_info("PLL1 running at %ld MHz, PLL2 at %ld MHz\n",
583 clk_pll1
.rate
/ 1000000, clk_pll2
.rate
/ 1000000);
584 pr_info("FCLK %ld MHz, HCLK %ld MHz, PCLK %ld MHz\n",
585 clk_f
.rate
/ 1000000, clk_h
.rate
/ 1000000,
586 clk_p
.rate
/ 1000000);
588 clkdev_add_table(clocks
, ARRAY_SIZE(clocks
));
591 postcore_initcall(ep93xx_clock_init
);