2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
18 #include <linux/kernel.h>
19 #include <linux/wait.h>
20 #include <linux/sched.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
26 #define PLL_STATUS_MASK(id) (1 << (1 + (id)))
27 #define PLL_REG(id) (AT91_CKGR_PLLAR + ((id) * 4))
28 #define PLL_DIV_MASK 0xff
29 #define PLL_DIV_MAX PLL_DIV_MASK
30 #define PLL_DIV(reg) ((reg) & PLL_DIV_MASK)
31 #define PLL_MUL(reg, layout) (((reg) >> (layout)->mul_shift) & \
34 #define PLL_MUL_MASK(layout) ((layout)->mul_mask)
35 #define PLL_MUL_MAX(layout) (PLL_MUL_MASK(layout) + 1)
36 #define PLL_ICPR_SHIFT(id) ((id) * 16)
37 #define PLL_ICPR_MASK(id) (0xffff << PLL_ICPR_SHIFT(id))
38 #define PLL_MAX_COUNT 0x3f
39 #define PLL_COUNT_SHIFT 8
40 #define PLL_OUT_SHIFT 14
43 struct clk_pll_characteristics
{
44 struct clk_range input
;
46 struct clk_range
*output
;
51 struct clk_pll_layout
{
57 #define to_clk_pll(hw) container_of(hw, struct clk_pll, hw)
63 wait_queue_head_t wait
;
68 const struct clk_pll_layout
*layout
;
69 const struct clk_pll_characteristics
*characteristics
;
72 static irqreturn_t
clk_pll_irq_handler(int irq
, void *dev_id
)
74 struct clk_pll
*pll
= (struct clk_pll
*)dev_id
;
77 disable_irq_nosync(pll
->irq
);
82 static int clk_pll_prepare(struct clk_hw
*hw
)
84 struct clk_pll
*pll
= to_clk_pll(hw
);
85 struct at91_pmc
*pmc
= pll
->pmc
;
86 const struct clk_pll_layout
*layout
= pll
->layout
;
87 const struct clk_pll_characteristics
*characteristics
=
90 u32 mask
= PLL_STATUS_MASK(id
);
91 int offset
= PLL_REG(id
);
97 pllr
= pmc_read(pmc
, offset
);
99 mul
= PLL_MUL(pllr
, layout
);
101 if ((pmc_read(pmc
, AT91_PMC_SR
) & mask
) &&
102 (div
== pll
->div
&& mul
== pll
->mul
))
105 if (characteristics
->out
)
106 out
= characteristics
->out
[pll
->range
];
107 if (characteristics
->icpll
) {
108 icpr
= pmc_read(pmc
, AT91_PMC_PLLICPR
) & ~PLL_ICPR_MASK(id
);
109 icpr
|= (characteristics
->icpll
[pll
->range
] <<
111 pmc_write(pmc
, AT91_PMC_PLLICPR
, icpr
);
114 pllr
&= ~layout
->pllr_mask
;
115 pllr
|= layout
->pllr_mask
&
116 (pll
->div
| (PLL_MAX_COUNT
<< PLL_COUNT_SHIFT
) |
117 (out
<< PLL_OUT_SHIFT
) |
118 ((pll
->mul
& layout
->mul_mask
) << layout
->mul_shift
));
119 pmc_write(pmc
, offset
, pllr
);
121 while (!(pmc_read(pmc
, AT91_PMC_SR
) & mask
)) {
122 enable_irq(pll
->irq
);
123 wait_event(pll
->wait
,
124 pmc_read(pmc
, AT91_PMC_SR
) & mask
);
130 static int clk_pll_is_prepared(struct clk_hw
*hw
)
132 struct clk_pll
*pll
= to_clk_pll(hw
);
133 struct at91_pmc
*pmc
= pll
->pmc
;
135 return !!(pmc_read(pmc
, AT91_PMC_SR
) &
136 PLL_STATUS_MASK(pll
->id
));
139 static void clk_pll_unprepare(struct clk_hw
*hw
)
141 struct clk_pll
*pll
= to_clk_pll(hw
);
142 struct at91_pmc
*pmc
= pll
->pmc
;
143 const struct clk_pll_layout
*layout
= pll
->layout
;
144 int offset
= PLL_REG(pll
->id
);
145 u32 tmp
= pmc_read(pmc
, offset
) & ~(layout
->pllr_mask
);
147 pmc_write(pmc
, offset
, tmp
);
150 static unsigned long clk_pll_recalc_rate(struct clk_hw
*hw
,
151 unsigned long parent_rate
)
153 struct clk_pll
*pll
= to_clk_pll(hw
);
155 if (!pll
->div
|| !pll
->mul
)
158 return (parent_rate
/ pll
->div
) * (pll
->mul
+ 1);
161 static long clk_pll_get_best_div_mul(struct clk_pll
*pll
, unsigned long rate
,
162 unsigned long parent_rate
,
165 const struct clk_pll_layout
*layout
= pll
->layout
;
166 const struct clk_pll_characteristics
*characteristics
=
167 pll
->characteristics
;
168 unsigned long bestremainder
= ULONG_MAX
;
169 unsigned long maxdiv
, mindiv
, tmpdiv
;
170 long bestrate
= -ERANGE
;
171 unsigned long bestdiv
;
172 unsigned long bestmul
;
175 /* Check if parent_rate is a valid input rate */
176 if (parent_rate
< characteristics
->input
.min
)
180 * Calculate minimum divider based on the minimum multiplier, the
181 * parent_rate and the requested rate.
182 * Should always be 2 according to the input and output characteristics
185 mindiv
= (parent_rate
* PLL_MUL_MIN
) / rate
;
189 if (parent_rate
> characteristics
->input
.max
) {
190 tmpdiv
= DIV_ROUND_UP(parent_rate
, characteristics
->input
.max
);
191 if (tmpdiv
> PLL_DIV_MAX
)
199 * Calculate the maximum divider which is limited by PLL register
200 * layout (limited by the MUL or DIV field size).
202 maxdiv
= DIV_ROUND_UP(parent_rate
* PLL_MUL_MAX(layout
), rate
);
203 if (maxdiv
> PLL_DIV_MAX
)
204 maxdiv
= PLL_DIV_MAX
;
207 * Iterate over the acceptable divider values to find the best
208 * divider/multiplier pair (the one that generates the closest
209 * rate to the requested one).
211 for (tmpdiv
= mindiv
; tmpdiv
<= maxdiv
; tmpdiv
++) {
212 unsigned long remainder
;
213 unsigned long tmprate
;
214 unsigned long tmpmul
;
217 * Calculate the multiplier associated with the current
218 * divider that provide the closest rate to the requested one.
220 tmpmul
= DIV_ROUND_CLOSEST(rate
, parent_rate
/ tmpdiv
);
221 tmprate
= (parent_rate
/ tmpdiv
) * tmpmul
;
223 remainder
= tmprate
- rate
;
225 remainder
= rate
- tmprate
;
228 * Compare the remainder with the best remainder found until
229 * now and elect a new best multiplier/divider pair if the
230 * current remainder is smaller than the best one.
232 if (remainder
< bestremainder
) {
233 bestremainder
= remainder
;
240 * We've found a perfect match!
241 * Stop searching now and use this multiplier/divider pair.
247 /* We haven't found any multiplier/divider pair => return -ERANGE */
251 /* Check if bestrate is a valid output rate */
252 for (i
= 0; i
< characteristics
->num_output
; i
++) {
253 if (bestrate
>= characteristics
->output
[i
].min
&&
254 bestrate
<= characteristics
->output
[i
].max
)
258 if (i
>= characteristics
->num_output
)
271 static long clk_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
272 unsigned long *parent_rate
)
274 struct clk_pll
*pll
= to_clk_pll(hw
);
276 return clk_pll_get_best_div_mul(pll
, rate
, *parent_rate
,
280 static int clk_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
281 unsigned long parent_rate
)
283 struct clk_pll
*pll
= to_clk_pll(hw
);
289 ret
= clk_pll_get_best_div_mul(pll
, rate
, parent_rate
,
301 static const struct clk_ops pll_ops
= {
302 .prepare
= clk_pll_prepare
,
303 .unprepare
= clk_pll_unprepare
,
304 .is_prepared
= clk_pll_is_prepared
,
305 .recalc_rate
= clk_pll_recalc_rate
,
306 .round_rate
= clk_pll_round_rate
,
307 .set_rate
= clk_pll_set_rate
,
310 static struct clk
* __init
311 at91_clk_register_pll(struct at91_pmc
*pmc
, unsigned int irq
, const char *name
,
312 const char *parent_name
, u8 id
,
313 const struct clk_pll_layout
*layout
,
314 const struct clk_pll_characteristics
*characteristics
)
317 struct clk
*clk
= NULL
;
318 struct clk_init_data init
;
320 int offset
= PLL_REG(id
);
324 return ERR_PTR(-EINVAL
);
326 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
328 return ERR_PTR(-ENOMEM
);
332 init
.parent_names
= &parent_name
;
333 init
.num_parents
= 1;
334 init
.flags
= CLK_SET_RATE_GATE
;
337 pll
->hw
.init
= &init
;
338 pll
->layout
= layout
;
339 pll
->characteristics
= characteristics
;
342 tmp
= pmc_read(pmc
, offset
) & layout
->pllr_mask
;
343 pll
->div
= PLL_DIV(tmp
);
344 pll
->mul
= PLL_MUL(tmp
, layout
);
345 init_waitqueue_head(&pll
->wait
);
346 irq_set_status_flags(pll
->irq
, IRQ_NOAUTOEN
);
347 ret
= request_irq(pll
->irq
, clk_pll_irq_handler
, IRQF_TRIGGER_HIGH
,
348 id
? "clk-pllb" : "clk-plla", pll
);
354 clk
= clk_register(NULL
, &pll
->hw
);
356 free_irq(pll
->irq
, pll
);
364 static const struct clk_pll_layout at91rm9200_pll_layout
= {
365 .pllr_mask
= 0x7FFFFFF,
370 static const struct clk_pll_layout at91sam9g45_pll_layout
= {
371 .pllr_mask
= 0xFFFFFF,
376 static const struct clk_pll_layout at91sam9g20_pllb_layout
= {
377 .pllr_mask
= 0x3FFFFF,
382 static const struct clk_pll_layout sama5d3_pll_layout
= {
383 .pllr_mask
= 0x1FFFFFF,
389 static struct clk_pll_characteristics
* __init
390 of_at91_clk_pll_get_characteristics(struct device_node
*np
)
397 struct clk_range input
;
398 struct clk_range
*output
;
401 struct clk_pll_characteristics
*characteristics
;
403 if (of_at91_get_clk_range(np
, "atmel,clk-input-range", &input
))
406 if (of_property_read_u32(np
, "#atmel,pll-clk-output-range-cells",
410 if (num_cells
< 2 || num_cells
> 4)
413 if (!of_get_property(np
, "atmel,pll-clk-output-ranges", &tmp
))
415 num_output
= tmp
/ (sizeof(u32
) * num_cells
);
417 characteristics
= kzalloc(sizeof(*characteristics
), GFP_KERNEL
);
418 if (!characteristics
)
421 output
= kzalloc(sizeof(*output
) * num_output
, GFP_KERNEL
);
423 goto out_free_characteristics
;
426 out
= kzalloc(sizeof(*out
) * num_output
, GFP_KERNEL
);
428 goto out_free_output
;
432 icpll
= kzalloc(sizeof(*icpll
) * num_output
, GFP_KERNEL
);
434 goto out_free_output
;
437 for (i
= 0; i
< num_output
; i
++) {
438 offset
= i
* num_cells
;
439 if (of_property_read_u32_index(np
,
440 "atmel,pll-clk-output-ranges",
442 goto out_free_output
;
444 if (of_property_read_u32_index(np
,
445 "atmel,pll-clk-output-ranges",
447 goto out_free_output
;
453 if (of_property_read_u32_index(np
,
454 "atmel,pll-clk-output-ranges",
456 goto out_free_output
;
462 if (of_property_read_u32_index(np
,
463 "atmel,pll-clk-output-ranges",
465 goto out_free_output
;
469 characteristics
->input
= input
;
470 characteristics
->num_output
= num_output
;
471 characteristics
->output
= output
;
472 characteristics
->out
= out
;
473 characteristics
->icpll
= icpll
;
474 return characteristics
;
480 out_free_characteristics
:
481 kfree(characteristics
);
486 of_at91_clk_pll_setup(struct device_node
*np
, struct at91_pmc
*pmc
,
487 const struct clk_pll_layout
*layout
)
492 const char *parent_name
;
493 const char *name
= np
->name
;
494 struct clk_pll_characteristics
*characteristics
;
496 if (of_property_read_u32(np
, "reg", &id
))
499 parent_name
= of_clk_get_parent_name(np
, 0);
501 of_property_read_string(np
, "clock-output-names", &name
);
503 characteristics
= of_at91_clk_pll_get_characteristics(np
);
504 if (!characteristics
)
507 irq
= irq_of_parse_and_map(np
, 0);
511 clk
= at91_clk_register_pll(pmc
, irq
, name
, parent_name
, id
, layout
,
514 goto out_free_characteristics
;
516 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
519 out_free_characteristics
:
520 kfree(characteristics
);
523 void __init
of_at91rm9200_clk_pll_setup(struct device_node
*np
,
524 struct at91_pmc
*pmc
)
526 of_at91_clk_pll_setup(np
, pmc
, &at91rm9200_pll_layout
);
529 void __init
of_at91sam9g45_clk_pll_setup(struct device_node
*np
,
530 struct at91_pmc
*pmc
)
532 of_at91_clk_pll_setup(np
, pmc
, &at91sam9g45_pll_layout
);
535 void __init
of_at91sam9g20_clk_pllb_setup(struct device_node
*np
,
536 struct at91_pmc
*pmc
)
538 of_at91_clk_pll_setup(np
, pmc
, &at91sam9g20_pllb_layout
);
541 void __init
of_sama5d3_clk_pll_setup(struct device_node
*np
,
542 struct at91_pmc
*pmc
)
544 of_at91_clk_pll_setup(np
, pmc
, &sama5d3_pll_layout
);