2 * Clock tree for CSR SiRFprimaII
4 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
6 * Licensed under GPLv2 or later.
9 #include <linux/module.h>
10 #include <linux/bitops.h>
11 #include <linux/err.h>
12 #include <linux/errno.h>
14 #include <linux/clkdev.h>
15 #include <linux/clk.h>
16 #include <linux/spinlock.h>
18 #include <linux/of_address.h>
19 #include <asm/mach/map.h>
22 #define SIRFSOC_CLKC_CLK_EN0 0x0000
23 #define SIRFSOC_CLKC_CLK_EN1 0x0004
24 #define SIRFSOC_CLKC_REF_CFG 0x0014
25 #define SIRFSOC_CLKC_CPU_CFG 0x0018
26 #define SIRFSOC_CLKC_MEM_CFG 0x001c
27 #define SIRFSOC_CLKC_SYS_CFG 0x0020
28 #define SIRFSOC_CLKC_IO_CFG 0x0024
29 #define SIRFSOC_CLKC_DSP_CFG 0x0028
30 #define SIRFSOC_CLKC_GFX_CFG 0x002c
31 #define SIRFSOC_CLKC_MM_CFG 0x0030
32 #define SIRFSOC_LKC_LCD_CFG 0x0034
33 #define SIRFSOC_CLKC_MMC_CFG 0x0038
34 #define SIRFSOC_CLKC_PLL1_CFG0 0x0040
35 #define SIRFSOC_CLKC_PLL2_CFG0 0x0044
36 #define SIRFSOC_CLKC_PLL3_CFG0 0x0048
37 #define SIRFSOC_CLKC_PLL1_CFG1 0x004c
38 #define SIRFSOC_CLKC_PLL2_CFG1 0x0050
39 #define SIRFSOC_CLKC_PLL3_CFG1 0x0054
40 #define SIRFSOC_CLKC_PLL1_CFG2 0x0058
41 #define SIRFSOC_CLKC_PLL2_CFG2 0x005c
42 #define SIRFSOC_CLKC_PLL3_CFG2 0x0060
44 #define SIRFSOC_CLOCK_VA_BASE SIRFSOC_VA(0x005000)
47 #define MHZ (KHZ * KHZ)
50 unsigned long (*get_rate
)(struct clk
*clk
);
51 long (*round_rate
)(struct clk
*clk
, unsigned long rate
);
52 int (*set_rate
)(struct clk
*clk
, unsigned long rate
);
53 int (*enable
)(struct clk
*clk
);
54 int (*disable
)(struct clk
*clk
);
55 struct clk
*(*get_parent
)(struct clk
*clk
);
56 int (*set_parent
)(struct clk
*clk
, struct clk
*parent
);
60 struct clk
*parent
; /* parent clk */
61 unsigned long rate
; /* clock rate in Hz */
62 signed char usage
; /* clock enable count */
63 signed char enable_bit
; /* enable bit: 0 ~ 63 */
64 unsigned short regofs
; /* register offset */
65 struct clk_ops
*ops
; /* clock operation */
68 static DEFINE_SPINLOCK(clocks_lock
);
70 static inline unsigned long clkc_readl(unsigned reg
)
72 return readl(SIRFSOC_CLOCK_VA_BASE
+ reg
);
75 static inline void clkc_writel(u32 val
, unsigned reg
)
77 writel(val
, SIRFSOC_CLOCK_VA_BASE
+ reg
);
81 * osc_rtc - real time oscillator - 32.768KHz
82 * osc_sys - high speed oscillator - 26MHz
85 static struct clk clk_rtc
= {
89 static struct clk clk_osc
= {
96 static unsigned long std_pll_get_rate(struct clk
*clk
)
98 unsigned long fin
= clk_get_rate(clk
->parent
);
99 u32 regcfg2
= clk
->regofs
+ SIRFSOC_CLKC_PLL1_CFG2
-
100 SIRFSOC_CLKC_PLL1_CFG0
;
102 if (clkc_readl(regcfg2
) & BIT(2)) {
103 /* pll bypass mode */
106 /* fout = fin * nf / nr / od */
107 u32 cfg0
= clkc_readl(clk
->regofs
);
108 u32 nf
= (cfg0
& (BIT(13) - 1)) + 1;
109 u32 nr
= ((cfg0
>> 13) & (BIT(6) - 1)) + 1;
110 u32 od
= ((cfg0
>> 19) & (BIT(4) - 1)) + 1;
112 clk
->rate
= fin
/ MHZ
* nf
/ nr
/ od
* MHZ
;
118 static int std_pll_set_rate(struct clk
*clk
, unsigned long rate
)
120 unsigned long fin
, nf
, nr
, od
, reg
;
123 * fout = fin * nf / (nr * od);
124 * set od = 1, nr = fin/MHz, so fout = nf * MHz
128 if (unlikely((rate
% MHZ
) || nf
> BIT(13) || nf
< 1))
131 fin
= clk_get_rate(clk
->parent
);
135 BUG_ON((fin
% MHZ
) || nr
> BIT(6));
139 reg
= (nf
- 1) | ((nr
- 1) << 13) | ((od
- 1) << 19);
140 clkc_writel(reg
, clk
->regofs
);
142 reg
= clk
->regofs
+ SIRFSOC_CLKC_PLL1_CFG1
- SIRFSOC_CLKC_PLL1_CFG0
;
143 clkc_writel((nf
>> 1) - 1, reg
);
145 reg
= clk
->regofs
+ SIRFSOC_CLKC_PLL1_CFG2
- SIRFSOC_CLKC_PLL1_CFG0
;
146 while (!(clkc_readl(reg
) & BIT(6)))
149 clk
->rate
= 0; /* set to zero will force recalculation */
153 static struct clk_ops std_pll_ops
= {
154 .get_rate
= std_pll_get_rate
,
155 .set_rate
= std_pll_set_rate
,
158 static struct clk clk_pll1
= {
160 .regofs
= SIRFSOC_CLKC_PLL1_CFG0
,
164 static struct clk clk_pll2
= {
166 .regofs
= SIRFSOC_CLKC_PLL2_CFG0
,
170 static struct clk clk_pll3
= {
172 .regofs
= SIRFSOC_CLKC_PLL3_CFG0
,
177 * clock domains - cpu, mem, sys/io
180 static struct clk clk_mem
;
182 static struct clk
*dmn_get_parent(struct clk
*clk
)
184 struct clk
*clks
[] = {
185 &clk_osc
, &clk_rtc
, &clk_pll1
, &clk_pll2
, &clk_pll3
187 u32 cfg
= clkc_readl(clk
->regofs
);
188 WARN_ON((cfg
& (BIT(3) - 1)) > 4);
189 return clks
[cfg
& (BIT(3) - 1)];
192 static int dmn_set_parent(struct clk
*clk
, struct clk
*parent
)
194 const struct clk
*clks
[] = {
195 &clk_osc
, &clk_rtc
, &clk_pll1
, &clk_pll2
, &clk_pll3
197 u32 cfg
= clkc_readl(clk
->regofs
);
199 for (i
= 0; i
< ARRAY_SIZE(clks
); i
++) {
200 if (clks
[i
] == parent
) {
201 cfg
&= ~(BIT(3) - 1);
202 clkc_writel(cfg
| i
, clk
->regofs
);
203 /* BIT(3) - switching status: 1 - busy, 0 - done */
204 while (clkc_readl(clk
->regofs
) & BIT(3))
212 static unsigned long dmn_get_rate(struct clk
*clk
)
214 unsigned long fin
= clk_get_rate(clk
->parent
);
215 u32 cfg
= clkc_readl(clk
->regofs
);
217 /* fcd bypass mode */
221 * wait count: bit[19:16], hold count: bit[23:20]
223 u32 wait
= (cfg
>> 16) & (BIT(4) - 1);
224 u32 hold
= (cfg
>> 20) & (BIT(4) - 1);
226 clk
->rate
= fin
/ (wait
+ hold
+ 2);
232 static int dmn_set_rate(struct clk
*clk
, unsigned long rate
)
235 unsigned ratio
, wait
, hold
, reg
;
236 unsigned bits
= (clk
== &clk_mem
) ? 3 : 4;
238 fin
= clk_get_rate(clk
->parent
);
241 if (unlikely(ratio
< 2 || ratio
> BIT(bits
+ 1)))
246 wait
= (ratio
>> 1) - 1;
247 hold
= ratio
- wait
- 2;
249 reg
= clkc_readl(clk
->regofs
);
250 reg
&= ~(((BIT(bits
) - 1) << 16) | ((BIT(bits
) - 1) << 20));
251 reg
|= (wait
<< 16) | (hold
<< 20) | BIT(25);
252 clkc_writel(reg
, clk
->regofs
);
254 /* waiting FCD been effective */
255 while (clkc_readl(clk
->regofs
) & BIT(25))
258 clk
->rate
= 0; /* set to zero will force recalculation */
264 * cpu clock has no FCD register in Prima2, can only change pll
266 static int cpu_set_rate(struct clk
*clk
, unsigned long rate
)
269 struct clk
*cur_parent
, *tmp_parent
;
271 cur_parent
= dmn_get_parent(clk
);
272 BUG_ON(cur_parent
== NULL
|| cur_parent
->usage
> 1);
274 /* switch to tmp pll before setting parent clock's rate */
275 tmp_parent
= cur_parent
== &clk_pll1
? &clk_pll2
: &clk_pll1
;
276 ret1
= dmn_set_parent(clk
, tmp_parent
);
279 ret2
= clk_set_rate(cur_parent
, rate
);
281 ret1
= dmn_set_parent(clk
, cur_parent
);
283 clk
->rate
= 0; /* set to zero will force recalculation */
285 return ret2
? ret2
: ret1
;
288 static struct clk_ops cpu_ops
= {
289 .get_parent
= dmn_get_parent
,
290 .set_parent
= dmn_set_parent
,
291 .set_rate
= cpu_set_rate
,
294 static struct clk clk_cpu
= {
296 .regofs
= SIRFSOC_CLKC_CPU_CFG
,
301 static struct clk_ops msi_ops
= {
302 .set_rate
= dmn_set_rate
,
303 .get_rate
= dmn_get_rate
,
304 .set_parent
= dmn_set_parent
,
305 .get_parent
= dmn_get_parent
,
308 static struct clk clk_mem
= {
310 .regofs
= SIRFSOC_CLKC_MEM_CFG
,
314 static struct clk clk_sys
= {
316 .regofs
= SIRFSOC_CLKC_SYS_CFG
,
320 static struct clk clk_io
= {
322 .regofs
= SIRFSOC_CLKC_IO_CFG
,
329 static struct clk_lookup onchip_clks
[] = {
360 int clk_enable(struct clk
*clk
)
364 if (unlikely(IS_ERR_OR_NULL(clk
)))
368 clk_enable(clk
->parent
);
370 spin_lock_irqsave(&clocks_lock
, flags
);
371 if (!clk
->usage
++ && clk
->ops
&& clk
->ops
->enable
)
372 clk
->ops
->enable(clk
);
373 spin_unlock_irqrestore(&clocks_lock
, flags
);
376 EXPORT_SYMBOL(clk_enable
);
378 void clk_disable(struct clk
*clk
)
382 if (unlikely(IS_ERR_OR_NULL(clk
)))
385 WARN_ON(!clk
->usage
);
387 spin_lock_irqsave(&clocks_lock
, flags
);
388 if (--clk
->usage
== 0 && clk
->ops
&& clk
->ops
->disable
)
389 clk
->ops
->disable(clk
);
390 spin_unlock_irqrestore(&clocks_lock
, flags
);
393 clk_disable(clk
->parent
);
395 EXPORT_SYMBOL(clk_disable
);
397 unsigned long clk_get_rate(struct clk
*clk
)
399 if (unlikely(IS_ERR_OR_NULL(clk
)))
405 if (clk
->ops
&& clk
->ops
->get_rate
)
406 return clk
->ops
->get_rate(clk
);
408 return clk_get_rate(clk
->parent
);
410 EXPORT_SYMBOL(clk_get_rate
);
412 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
414 if (unlikely(IS_ERR_OR_NULL(clk
)))
417 if (clk
->ops
&& clk
->ops
->round_rate
)
418 return clk
->ops
->round_rate(clk
, rate
);
422 EXPORT_SYMBOL(clk_round_rate
);
424 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
426 if (unlikely(IS_ERR_OR_NULL(clk
)))
429 if (!clk
->ops
|| !clk
->ops
->set_rate
)
432 return clk
->ops
->set_rate(clk
, rate
);
434 EXPORT_SYMBOL(clk_set_rate
);
436 int clk_set_parent(struct clk
*clk
, struct clk
*parent
)
441 if (unlikely(IS_ERR_OR_NULL(clk
)))
444 if (!clk
->ops
|| !clk
->ops
->set_parent
)
447 spin_lock_irqsave(&clocks_lock
, flags
);
448 ret
= clk
->ops
->set_parent(clk
, parent
);
450 parent
->usage
+= clk
->usage
;
451 clk
->parent
->usage
-= clk
->usage
;
452 BUG_ON(clk
->parent
->usage
< 0);
453 clk
->parent
= parent
;
455 spin_unlock_irqrestore(&clocks_lock
, flags
);
458 EXPORT_SYMBOL(clk_set_parent
);
460 struct clk
*clk_get_parent(struct clk
*clk
)
464 if (unlikely(IS_ERR_OR_NULL(clk
)))
467 if (!clk
->ops
|| !clk
->ops
->get_parent
)
470 spin_lock_irqsave(&clocks_lock
, flags
);
471 clk
->parent
= clk
->ops
->get_parent(clk
);
472 spin_unlock_irqrestore(&clocks_lock
, flags
);
475 EXPORT_SYMBOL(clk_get_parent
);
477 static void __init
sirfsoc_clk_init(void)
479 clkdev_add_table(onchip_clks
, ARRAY_SIZE(onchip_clks
));
482 static struct of_device_id clkc_ids
[] = {
483 { .compatible
= "sirf,prima2-clkc" },
487 void __init
sirfsoc_of_clk_init(void)
489 struct device_node
*np
;
491 struct map_desc sirfsoc_clkc_iodesc
= {
492 .virtual = SIRFSOC_CLOCK_VA_BASE
,
496 np
= of_find_matching_node(NULL
, clkc_ids
);
498 panic("unable to find compatible clkc node in dtb\n");
500 if (of_address_to_resource(np
, 0, &res
))
501 panic("unable to find clkc range in dtb");
504 sirfsoc_clkc_iodesc
.pfn
= __phys_to_pfn(res
.start
);
505 sirfsoc_clkc_iodesc
.length
= 1 + res
.end
- res
.start
;
507 iotable_init(&sirfsoc_clkc_iodesc
, 1);