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>
23 #include <mach/hardware.h>
25 #include <asm/clkdev.h>
26 #include <asm/div64.h>
34 void __iomem
*enable_reg
;
37 unsigned long (*get_rate
)(struct clk
*clk
);
38 int (*set_rate
)(struct clk
*clk
, unsigned long rate
);
42 static unsigned long get_uart_rate(struct clk
*clk
);
44 static int set_keytchclk_rate(struct clk
*clk
, unsigned long rate
);
45 static int set_div_rate(struct clk
*clk
, unsigned long rate
);
48 static struct clk clk_xtali
= {
49 .rate
= EP93XX_EXT_CLK_RATE
,
51 static struct clk clk_uart1
= {
54 .enable_reg
= EP93XX_SYSCON_DEVCFG
,
55 .enable_mask
= EP93XX_SYSCON_DEVCFG_U1EN
,
56 .get_rate
= get_uart_rate
,
58 static struct clk clk_uart2
= {
61 .enable_reg
= EP93XX_SYSCON_DEVCFG
,
62 .enable_mask
= EP93XX_SYSCON_DEVCFG_U2EN
,
63 .get_rate
= get_uart_rate
,
65 static struct clk clk_uart3
= {
68 .enable_reg
= EP93XX_SYSCON_DEVCFG
,
69 .enable_mask
= EP93XX_SYSCON_DEVCFG_U3EN
,
70 .get_rate
= get_uart_rate
,
72 static struct clk clk_pll1
= {
75 static struct clk clk_f
= {
78 static struct clk clk_h
= {
81 static struct clk clk_p
= {
84 static struct clk clk_pll2
= {
87 static struct clk clk_usb_host
= {
89 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
90 .enable_mask
= EP93XX_SYSCON_PWRCNT_USH_EN
,
92 static struct clk clk_keypad
= {
95 .enable_reg
= EP93XX_SYSCON_KEYTCHCLKDIV
,
96 .enable_mask
= EP93XX_SYSCON_KEYTCHCLKDIV_KEN
,
97 .set_rate
= set_keytchclk_rate
,
99 static struct clk clk_pwm
= {
100 .parent
= &clk_xtali
,
101 .rate
= EP93XX_EXT_CLK_RATE
,
104 static struct clk clk_video
= {
106 .enable_reg
= EP93XX_SYSCON_VIDCLKDIV
,
107 .enable_mask
= EP93XX_SYSCON_CLKDIV_ENABLE
,
108 .set_rate
= set_div_rate
,
112 static struct clk clk_m2p0
= {
114 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
115 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P0
,
117 static struct clk clk_m2p1
= {
119 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
120 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P1
,
122 static struct clk clk_m2p2
= {
124 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
125 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P2
,
127 static struct clk clk_m2p3
= {
129 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
130 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P3
,
132 static struct clk clk_m2p4
= {
134 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
135 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P4
,
137 static struct clk clk_m2p5
= {
139 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
140 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P5
,
142 static struct clk clk_m2p6
= {
144 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
145 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P6
,
147 static struct clk clk_m2p7
= {
149 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
150 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P7
,
152 static struct clk clk_m2p8
= {
154 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
155 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P8
,
157 static struct clk clk_m2p9
= {
159 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
160 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2P9
,
162 static struct clk clk_m2m0
= {
164 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
165 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2M0
,
167 static struct clk clk_m2m1
= {
169 .enable_reg
= EP93XX_SYSCON_PWRCNT
,
170 .enable_mask
= EP93XX_SYSCON_PWRCNT_DMA_M2M1
,
173 #define INIT_CK(dev,con,ck) \
174 { .dev_id = dev, .con_id = con, .clk = ck }
176 static struct clk_lookup clocks
[] = {
177 INIT_CK(NULL
, "xtali", &clk_xtali
),
178 INIT_CK("apb:uart1", NULL
, &clk_uart1
),
179 INIT_CK("apb:uart2", NULL
, &clk_uart2
),
180 INIT_CK("apb:uart3", NULL
, &clk_uart3
),
181 INIT_CK(NULL
, "pll1", &clk_pll1
),
182 INIT_CK(NULL
, "fclk", &clk_f
),
183 INIT_CK(NULL
, "hclk", &clk_h
),
184 INIT_CK(NULL
, "pclk", &clk_p
),
185 INIT_CK(NULL
, "pll2", &clk_pll2
),
186 INIT_CK("ep93xx-ohci", NULL
, &clk_usb_host
),
187 INIT_CK("ep93xx-keypad", NULL
, &clk_keypad
),
188 INIT_CK("ep93xx-fb", NULL
, &clk_video
),
189 INIT_CK(NULL
, "pwm_clk", &clk_pwm
),
190 INIT_CK(NULL
, "m2p0", &clk_m2p0
),
191 INIT_CK(NULL
, "m2p1", &clk_m2p1
),
192 INIT_CK(NULL
, "m2p2", &clk_m2p2
),
193 INIT_CK(NULL
, "m2p3", &clk_m2p3
),
194 INIT_CK(NULL
, "m2p4", &clk_m2p4
),
195 INIT_CK(NULL
, "m2p5", &clk_m2p5
),
196 INIT_CK(NULL
, "m2p6", &clk_m2p6
),
197 INIT_CK(NULL
, "m2p7", &clk_m2p7
),
198 INIT_CK(NULL
, "m2p8", &clk_m2p8
),
199 INIT_CK(NULL
, "m2p9", &clk_m2p9
),
200 INIT_CK(NULL
, "m2m0", &clk_m2m0
),
201 INIT_CK(NULL
, "m2m1", &clk_m2m1
),
204 static DEFINE_SPINLOCK(clk_lock
);
206 static void __clk_enable(struct clk
*clk
)
210 __clk_enable(clk
->parent
);
212 if (clk
->enable_reg
) {
215 v
= __raw_readl(clk
->enable_reg
);
216 v
|= clk
->enable_mask
;
218 ep93xx_syscon_swlocked_write(v
, clk
->enable_reg
);
220 __raw_writel(v
, clk
->enable_reg
);
225 int clk_enable(struct clk
*clk
)
232 spin_lock_irqsave(&clk_lock
, flags
);
234 spin_unlock_irqrestore(&clk_lock
, flags
);
238 EXPORT_SYMBOL(clk_enable
);
240 static void __clk_disable(struct clk
*clk
)
243 if (clk
->enable_reg
) {
246 v
= __raw_readl(clk
->enable_reg
);
247 v
&= ~clk
->enable_mask
;
249 ep93xx_syscon_swlocked_write(v
, clk
->enable_reg
);
251 __raw_writel(v
, clk
->enable_reg
);
255 __clk_disable(clk
->parent
);
259 void clk_disable(struct clk
*clk
)
266 spin_lock_irqsave(&clk_lock
, flags
);
268 spin_unlock_irqrestore(&clk_lock
, flags
);
270 EXPORT_SYMBOL(clk_disable
);
272 static unsigned long get_uart_rate(struct clk
*clk
)
274 unsigned long rate
= clk_get_rate(clk
->parent
);
277 value
= __raw_readl(EP93XX_SYSCON_PWRCNT
);
278 if (value
& EP93XX_SYSCON_PWRCNT_UARTBAUD
)
284 unsigned long clk_get_rate(struct clk
*clk
)
287 return clk
->get_rate(clk
);
291 EXPORT_SYMBOL(clk_get_rate
);
293 static int set_keytchclk_rate(struct clk
*clk
, unsigned long rate
)
298 val
= __raw_readl(clk
->enable_reg
);
301 * The Key Matrix and ADC clocks are configured using the same
302 * System Controller register. The clock used will be either
303 * 1/4 or 1/16 the external clock rate depending on the
304 * EP93XX_SYSCON_KEYTCHCLKDIV_KDIV/EP93XX_SYSCON_KEYTCHCLKDIV_ADIV
305 * bit being set or cleared.
307 div_bit
= clk
->enable_mask
>> 15;
309 if (rate
== EP93XX_KEYTCHCLK_DIV4
)
311 else if (rate
== EP93XX_KEYTCHCLK_DIV16
)
316 ep93xx_syscon_swlocked_write(val
, clk
->enable_reg
);
321 static int calc_clk_div(struct clk
*clk
, unsigned long rate
,
322 int *psel
, int *esel
, int *pdiv
, int *div
)
325 unsigned long max_rate
, actual_rate
, mclk_rate
, rate_err
= -1;
326 int i
, found
= 0, __div
= 0, __pdiv
= 0;
328 /* Don't exceed the maximum rate */
329 max_rate
= max(max(clk_pll1
.rate
/ 4, clk_pll2
.rate
/ 4),
331 rate
= min(rate
, max_rate
);
334 * Try the two pll's and the external clock
335 * Because the valid predividers are 2, 2.5 and 3, we multiply
336 * all the clocks by 2 to avoid floating point math.
338 * This is based on the algorithm in the ep93xx raster guide:
339 * http://be-a-maverick.com/en/pubs/appNote/AN269REV1.pdf
342 for (i
= 0; i
< 3; i
++) {
349 mclk_rate
= mclk
->rate
* 2;
351 /* Try each predivider value */
352 for (__pdiv
= 4; __pdiv
<= 6; __pdiv
++) {
353 __div
= mclk_rate
/ (rate
* __pdiv
);
354 if (__div
< 2 || __div
> 127)
357 actual_rate
= mclk_rate
/ (__pdiv
* __div
);
359 if (!found
|| abs(actual_rate
- rate
) < rate_err
) {
365 clk
->rate
= actual_rate
;
366 rate_err
= abs(actual_rate
- rate
);
378 static int set_div_rate(struct clk
*clk
, unsigned long rate
)
380 int err
, psel
= 0, esel
= 0, pdiv
= 0, div
= 0;
383 err
= calc_clk_div(clk
, rate
, &psel
, &esel
, &pdiv
, &div
);
387 /* Clear the esel, psel, pdiv and div bits */
388 val
= __raw_readl(clk
->enable_reg
);
391 /* Set the new esel, psel, pdiv and div bits for the new clock rate */
392 val
|= (esel
? EP93XX_SYSCON_CLKDIV_ESEL
: 0) |
393 (psel
? EP93XX_SYSCON_CLKDIV_PSEL
: 0) |
394 (pdiv
<< EP93XX_SYSCON_CLKDIV_PDIV_SHIFT
) | div
;
395 ep93xx_syscon_swlocked_write(val
, clk
->enable_reg
);
399 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
402 return clk
->set_rate(clk
, rate
);
406 EXPORT_SYMBOL(clk_set_rate
);
409 static char fclk_divisors
[] = { 1, 2, 4, 8, 16, 1, 1, 1 };
410 static char hclk_divisors
[] = { 1, 2, 4, 5, 6, 8, 16, 32 };
411 static char pclk_divisors
[] = { 1, 2, 4, 8 };
414 * PLL rate = 14.7456 MHz * (X1FBD + 1) * (X2FBD + 1) / (X2IPD + 1) / 2^PS
416 static unsigned long calc_pll_rate(u32 config_word
)
418 unsigned long long rate
;
421 rate
= clk_xtali
.rate
;
422 rate
*= ((config_word
>> 11) & 0x1f) + 1; /* X1FBD */
423 rate
*= ((config_word
>> 5) & 0x3f) + 1; /* X2FBD */
424 do_div(rate
, (config_word
& 0x1f) + 1); /* X2IPD */
425 for (i
= 0; i
< ((config_word
>> 16) & 3); i
++) /* PS */
428 return (unsigned long)rate
;
431 static void __init
ep93xx_dma_clock_init(void)
433 clk_m2p0
.rate
= clk_h
.rate
;
434 clk_m2p1
.rate
= clk_h
.rate
;
435 clk_m2p2
.rate
= clk_h
.rate
;
436 clk_m2p3
.rate
= clk_h
.rate
;
437 clk_m2p4
.rate
= clk_h
.rate
;
438 clk_m2p5
.rate
= clk_h
.rate
;
439 clk_m2p6
.rate
= clk_h
.rate
;
440 clk_m2p7
.rate
= clk_h
.rate
;
441 clk_m2p8
.rate
= clk_h
.rate
;
442 clk_m2p9
.rate
= clk_h
.rate
;
443 clk_m2m0
.rate
= clk_h
.rate
;
444 clk_m2m1
.rate
= clk_h
.rate
;
447 static int __init
ep93xx_clock_init(void)
451 /* Determine the bootloader configured pll1 rate */
452 value
= __raw_readl(EP93XX_SYSCON_CLKSET1
);
453 if (!(value
& EP93XX_SYSCON_CLKSET1_NBYP1
))
454 clk_pll1
.rate
= clk_xtali
.rate
;
456 clk_pll1
.rate
= calc_pll_rate(value
);
458 /* Initialize the pll1 derived clocks */
459 clk_f
.rate
= clk_pll1
.rate
/ fclk_divisors
[(value
>> 25) & 0x7];
460 clk_h
.rate
= clk_pll1
.rate
/ hclk_divisors
[(value
>> 20) & 0x7];
461 clk_p
.rate
= clk_h
.rate
/ pclk_divisors
[(value
>> 18) & 0x3];
462 ep93xx_dma_clock_init();
464 /* Determine the bootloader configured pll2 rate */
465 value
= __raw_readl(EP93XX_SYSCON_CLKSET2
);
466 if (!(value
& EP93XX_SYSCON_CLKSET2_NBYP2
))
467 clk_pll2
.rate
= clk_xtali
.rate
;
468 else if (value
& EP93XX_SYSCON_CLKSET2_PLL2_EN
)
469 clk_pll2
.rate
= calc_pll_rate(value
);
473 /* Initialize the pll2 derived clocks */
474 clk_usb_host
.rate
= clk_pll2
.rate
/ (((value
>> 28) & 0xf) + 1);
476 pr_info("PLL1 running at %ld MHz, PLL2 at %ld MHz\n",
477 clk_pll1
.rate
/ 1000000, clk_pll2
.rate
/ 1000000);
478 pr_info("FCLK %ld MHz, HCLK %ld MHz, PCLK %ld MHz\n",
479 clk_f
.rate
/ 1000000, clk_h
.rate
/ 1000000,
480 clk_p
.rate
/ 1000000);
482 clkdev_add_table(clocks
, ARRAY_SIZE(clocks
));
485 arch_initcall(ep93xx_clock_init
);