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_spi
= {
102 .parent
= &clk_xtali
,
103 .rate
= EP93XX_EXT_CLK_RATE
,
105 static struct clk clk_pwm
= {
106 .parent
= &clk_xtali
,
107 .rate
= EP93XX_EXT_CLK_RATE
,
110 static struct clk clk_video
= {
112 .enable_reg
= EP93XX_SYSCON_VIDCLKDIV
,
113 .enable_mask
= EP93XX_SYSCON_CLKDIV_ENABLE
,
114 .set_rate
= set_div_rate
,
117 static struct clk clk_i2s_mclk
= {
119 .enable_reg
= EP93XX_SYSCON_I2SCLKDIV
,
120 .enable_mask
= EP93XX_SYSCON_CLKDIV_ENABLE
,
121 .set_rate
= set_div_rate
,
124 static struct clk clk_i2s_sclk
= {
126 .parent
= &clk_i2s_mclk
,
127 .enable_reg
= EP93XX_SYSCON_I2SCLKDIV
,
128 .enable_mask
= EP93XX_SYSCON_I2SCLKDIV_SENA
,
129 .set_rate
= set_i2s_sclk_rate
,
132 static struct clk clk_i2s_lrclk
= {
134 .parent
= &clk_i2s_sclk
,
135 .enable_reg
= EP93XX_SYSCON_I2SCLKDIV
,
136 .enable_mask
= EP93XX_SYSCON_I2SCLKDIV_SENA
,
137 .set_rate
= set_i2s_lrclk_rate
,
141 static struct clk clk_m2p0
= {
143 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
144 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P0
,
146 static struct clk clk_m2p1
= {
148 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
149 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P1
,
151 static struct clk clk_m2p2
= {
153 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
154 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P2
,
156 static struct clk clk_m2p3
= {
158 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
159 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P3
,
161 static struct clk clk_m2p4
= {
163 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
164 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P4
,
166 static struct clk clk_m2p5
= {
168 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
169 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P5
,
171 static struct clk clk_m2p6
= {
173 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
174 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P6
,
176 static struct clk clk_m2p7
= {
178 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
179 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P7
,
181 static struct clk clk_m2p8
= {
183 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
184 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P8
,
186 static struct clk clk_m2p9
= {
188 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
189 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P9
,
191 static struct clk clk_m2m0
= {
193 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
194 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2M0
,
196 static struct clk clk_m2m1
= {
198 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
199 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2M1
,
202 #define INIT_CK(dev,con,ck) \
203 { .dev_id = dev, .con_id = con, .clk = ck }
205 static struct clk_lookup clocks
[] = {
206 INIT_CK(NULL
, "xtali", &clk_xtali
),
207 INIT_CK("apb:uart1", NULL
, &clk_uart1
),
208 INIT_CK("apb:uart2", NULL
, &clk_uart2
),
209 INIT_CK("apb:uart3", NULL
, &clk_uart3
),
210 INIT_CK(NULL
, "pll1", &clk_pll1
),
211 INIT_CK(NULL
, "fclk", &clk_f
),
212 INIT_CK(NULL
, "hclk", &clk_h
),
213 INIT_CK(NULL
, "apb_pclk", &clk_p
),
214 INIT_CK(NULL
, "pll2", &clk_pll2
),
215 INIT_CK("ohci-platform", NULL
, &clk_usb_host
),
216 INIT_CK("ep93xx-keypad", NULL
, &clk_keypad
),
217 INIT_CK("ep93xx-fb", NULL
, &clk_video
),
218 INIT_CK("ep93xx-spi.0", NULL
, &clk_spi
),
219 INIT_CK("ep93xx-i2s", "mclk", &clk_i2s_mclk
),
220 INIT_CK("ep93xx-i2s", "sclk", &clk_i2s_sclk
),
221 INIT_CK("ep93xx-i2s", "lrclk", &clk_i2s_lrclk
),
222 INIT_CK(NULL
, "pwm_clk", &clk_pwm
),
223 INIT_CK(NULL
, "m2p0", &clk_m2p0
),
224 INIT_CK(NULL
, "m2p1", &clk_m2p1
),
225 INIT_CK(NULL
, "m2p2", &clk_m2p2
),
226 INIT_CK(NULL
, "m2p3", &clk_m2p3
),
227 INIT_CK(NULL
, "m2p4", &clk_m2p4
),
228 INIT_CK(NULL
, "m2p5", &clk_m2p5
),
229 INIT_CK(NULL
, "m2p6", &clk_m2p6
),
230 INIT_CK(NULL
, "m2p7", &clk_m2p7
),
231 INIT_CK(NULL
, "m2p8", &clk_m2p8
),
232 INIT_CK(NULL
, "m2p9", &clk_m2p9
),
233 INIT_CK(NULL
, "m2m0", &clk_m2m0
),
234 INIT_CK(NULL
, "m2m1", &clk_m2m1
),
237 static DEFINE_SPINLOCK(clk_lock
);
239 static void __clk_enable(struct clk
*clk
)
243 __clk_enable(clk
->parent
);
245 if (clk
->enable_reg
) {
248 v
= __raw_readl(clk
->enable_reg
);
249 v
|= clk
->enable_mask
;
251 ep93xx_syscon_swlocked_write(v
, clk
->enable_reg
);
253 __raw_writel(v
, clk
->enable_reg
);
258 int clk_enable(struct clk
*clk
)
265 spin_lock_irqsave(&clk_lock
, flags
);
267 spin_unlock_irqrestore(&clk_lock
, flags
);
271 EXPORT_SYMBOL(clk_enable
);
273 static void __clk_disable(struct clk
*clk
)
276 if (clk
->enable_reg
) {
279 v
= __raw_readl(clk
->enable_reg
);
280 v
&= ~clk
->enable_mask
;
282 ep93xx_syscon_swlocked_write(v
, clk
->enable_reg
);
284 __raw_writel(v
, clk
->enable_reg
);
288 __clk_disable(clk
->parent
);
292 void clk_disable(struct clk
*clk
)
299 spin_lock_irqsave(&clk_lock
, flags
);
301 spin_unlock_irqrestore(&clk_lock
, flags
);
303 EXPORT_SYMBOL(clk_disable
);
305 static unsigned long get_uart_rate(struct clk
*clk
)
307 unsigned long rate
= clk_get_rate(clk
->parent
);
310 value
= __raw_readl(EP93XX_SYSCON_PWRCNT
);
311 if (value
& EP93XX_SYSCON_PWRCNT_UARTBAUD
)
317 unsigned long clk_get_rate(struct clk
*clk
)
320 return clk
->get_rate(clk
);
324 EXPORT_SYMBOL(clk_get_rate
);
326 static int set_keytchclk_rate(struct clk
*clk
, unsigned long rate
)
331 val
= __raw_readl(clk
->enable_reg
);
334 * The Key Matrix and ADC clocks are configured using the same
335 * System Controller register. The clock used will be either
336 * 1/4 or 1/16 the external clock rate depending on the
337 * EP93XX_SYSCON_KEYTCHCLKDIV_KDIV/EP93XX_SYSCON_KEYTCHCLKDIV_ADIV
338 * bit being set or cleared.
340 div_bit
= clk
->enable_mask
>> 15;
342 if (rate
== EP93XX_KEYTCHCLK_DIV4
)
344 else if (rate
== EP93XX_KEYTCHCLK_DIV16
)
349 ep93xx_syscon_swlocked_write(val
, clk
->enable_reg
);
354 static int calc_clk_div(struct clk
*clk
, unsigned long rate
,
355 int *psel
, int *esel
, int *pdiv
, int *div
)
358 unsigned long max_rate
, actual_rate
, mclk_rate
, rate_err
= -1;
359 int i
, found
= 0, __div
= 0, __pdiv
= 0;
361 /* Don't exceed the maximum rate */
362 max_rate
= max3(clk_pll1
.rate
/ 4, clk_pll2
.rate
/ 4, clk_xtali
.rate
/ 4);
363 rate
= min(rate
, max_rate
);
366 * Try the two pll's and the external clock
367 * Because the valid predividers are 2, 2.5 and 3, we multiply
368 * all the clocks by 2 to avoid floating point math.
370 * This is based on the algorithm in the ep93xx raster guide:
371 * http://be-a-maverick.com/en/pubs/appNote/AN269REV1.pdf
374 for (i
= 0; i
< 3; i
++) {
381 mclk_rate
= mclk
->rate
* 2;
383 /* Try each predivider value */
384 for (__pdiv
= 4; __pdiv
<= 6; __pdiv
++) {
385 __div
= mclk_rate
/ (rate
* __pdiv
);
386 if (__div
< 2 || __div
> 127)
389 actual_rate
= mclk_rate
/ (__pdiv
* __div
);
391 if (!found
|| abs(actual_rate
- rate
) < rate_err
) {
397 clk
->rate
= actual_rate
;
398 rate_err
= abs(actual_rate
- rate
);
410 static int set_div_rate(struct clk
*clk
, unsigned long rate
)
412 int err
, psel
= 0, esel
= 0, pdiv
= 0, div
= 0;
415 err
= calc_clk_div(clk
, rate
, &psel
, &esel
, &pdiv
, &div
);
419 /* Clear the esel, psel, pdiv and div bits */
420 val
= __raw_readl(clk
->enable_reg
);
423 /* Set the new esel, psel, pdiv and div bits for the new clock rate */
424 val
|= (esel
? EP93XX_SYSCON_CLKDIV_ESEL
: 0) |
425 (psel
? EP93XX_SYSCON_CLKDIV_PSEL
: 0) |
426 (pdiv
<< EP93XX_SYSCON_CLKDIV_PDIV_SHIFT
) | div
;
427 ep93xx_syscon_swlocked_write(val
, clk
->enable_reg
);
431 static int set_i2s_sclk_rate(struct clk
*clk
, unsigned long rate
)
433 unsigned val
= __raw_readl(clk
->enable_reg
);
435 if (rate
== clk_i2s_mclk
.rate
/ 2)
436 ep93xx_syscon_swlocked_write(val
& ~EP93XX_I2SCLKDIV_SDIV
,
438 else if (rate
== clk_i2s_mclk
.rate
/ 4)
439 ep93xx_syscon_swlocked_write(val
| EP93XX_I2SCLKDIV_SDIV
,
444 clk_i2s_sclk
.rate
= rate
;
448 static int set_i2s_lrclk_rate(struct clk
*clk
, unsigned long rate
)
450 unsigned val
= __raw_readl(clk
->enable_reg
) &
451 ~EP93XX_I2SCLKDIV_LRDIV_MASK
;
453 if (rate
== clk_i2s_sclk
.rate
/ 32)
454 ep93xx_syscon_swlocked_write(val
| EP93XX_I2SCLKDIV_LRDIV32
,
456 else if (rate
== clk_i2s_sclk
.rate
/ 64)
457 ep93xx_syscon_swlocked_write(val
| EP93XX_I2SCLKDIV_LRDIV64
,
459 else if (rate
== clk_i2s_sclk
.rate
/ 128)
460 ep93xx_syscon_swlocked_write(val
| EP93XX_I2SCLKDIV_LRDIV128
,
465 clk_i2s_lrclk
.rate
= rate
;
469 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
472 return clk
->set_rate(clk
, rate
);
476 EXPORT_SYMBOL(clk_set_rate
);
479 static char fclk_divisors
[] = { 1, 2, 4, 8, 16, 1, 1, 1 };
480 static char hclk_divisors
[] = { 1, 2, 4, 5, 6, 8, 16, 32 };
481 static char pclk_divisors
[] = { 1, 2, 4, 8 };
484 * PLL rate = 14.7456 MHz * (X1FBD + 1) * (X2FBD + 1) / (X2IPD + 1) / 2^PS
486 static unsigned long calc_pll_rate(u32 config_word
)
488 unsigned long long rate
;
491 rate
= clk_xtali
.rate
;
492 rate
*= ((config_word
>> 11) & 0x1f) + 1; /* X1FBD */
493 rate
*= ((config_word
>> 5) & 0x3f) + 1; /* X2FBD */
494 do_div(rate
, (config_word
& 0x1f) + 1); /* X2IPD */
495 for (i
= 0; i
< ((config_word
>> 16) & 3); i
++) /* PS */
498 return (unsigned long)rate
;
501 static void __init
ep93xx_dma_clock_init(void)
503 clk_m2p0
.rate
= clk_h
.rate
;
504 clk_m2p1
.rate
= clk_h
.rate
;
505 clk_m2p2
.rate
= clk_h
.rate
;
506 clk_m2p3
.rate
= clk_h
.rate
;
507 clk_m2p4
.rate
= clk_h
.rate
;
508 clk_m2p5
.rate
= clk_h
.rate
;
509 clk_m2p6
.rate
= clk_h
.rate
;
510 clk_m2p7
.rate
= clk_h
.rate
;
511 clk_m2p8
.rate
= clk_h
.rate
;
512 clk_m2p9
.rate
= clk_h
.rate
;
513 clk_m2m0
.rate
= clk_h
.rate
;
514 clk_m2m1
.rate
= clk_h
.rate
;
517 static int __init
ep93xx_clock_init(void)
521 /* Determine the bootloader configured pll1 rate */
522 value
= __raw_readl(EP93XX_SYSCON_CLKSET1
);
523 if (!(value
& EP93XX_SYSCON_CLKSET1_NBYP1
))
524 clk_pll1
.rate
= clk_xtali
.rate
;
526 clk_pll1
.rate
= calc_pll_rate(value
);
528 /* Initialize the pll1 derived clocks */
529 clk_f
.rate
= clk_pll1
.rate
/ fclk_divisors
[(value
>> 25) & 0x7];
530 clk_h
.rate
= clk_pll1
.rate
/ hclk_divisors
[(value
>> 20) & 0x7];
531 clk_p
.rate
= clk_h
.rate
/ pclk_divisors
[(value
>> 18) & 0x3];
532 ep93xx_dma_clock_init();
534 /* Determine the bootloader configured pll2 rate */
535 value
= __raw_readl(EP93XX_SYSCON_CLKSET2
);
536 if (!(value
& EP93XX_SYSCON_CLKSET2_NBYP2
))
537 clk_pll2
.rate
= clk_xtali
.rate
;
538 else if (value
& EP93XX_SYSCON_CLKSET2_PLL2_EN
)
539 clk_pll2
.rate
= calc_pll_rate(value
);
543 /* Initialize the pll2 derived clocks */
544 clk_usb_host
.rate
= clk_pll2
.rate
/ (((value
>> 28) & 0xf) + 1);
547 * EP93xx SSP clock rate was doubled in version E2. For more information
549 * http://www.cirrus.com/en/pubs/appNote/AN273REV4.pdf
551 if (ep93xx_chip_revision() < EP93XX_CHIP_REV_E2
)
554 pr_info("PLL1 running at %ld MHz, PLL2 at %ld MHz\n",
555 clk_pll1
.rate
/ 1000000, clk_pll2
.rate
/ 1000000);
556 pr_info("FCLK %ld MHz, HCLK %ld MHz, PCLK %ld MHz\n",
557 clk_f
.rate
/ 1000000, clk_h
.rate
/ 1000000,
558 clk_p
.rate
/ 1000000);
560 clkdev_add_table(clocks
, ARRAY_SIZE(clocks
));
563 postcore_initcall(ep93xx_clock_init
);