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/wait.h>
19 #include <linux/sched.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
25 #define PLL_STATUS_MASK(id) (1 << (1 + (id)))
26 #define PLL_REG(id) (AT91_CKGR_PLLAR + ((id) * 4))
27 #define PLL_DIV_MASK 0xff
28 #define PLL_DIV_MAX PLL_DIV_MASK
29 #define PLL_DIV(reg) ((reg) & PLL_DIV_MASK)
30 #define PLL_MUL(reg, layout) (((reg) >> (layout)->mul_shift) & \
32 #define PLL_ICPR_SHIFT(id) ((id) * 16)
33 #define PLL_ICPR_MASK(id) (0xffff << PLL_ICPR_SHIFT(id))
34 #define PLL_MAX_COUNT 0x3ff
35 #define PLL_COUNT_SHIFT 8
36 #define PLL_OUT_SHIFT 14
39 struct clk_pll_characteristics
{
40 struct clk_range input
;
42 struct clk_range
*output
;
47 struct clk_pll_layout
{
53 #define to_clk_pll(hw) container_of(hw, struct clk_pll, hw)
59 wait_queue_head_t wait
;
64 const struct clk_pll_layout
*layout
;
65 const struct clk_pll_characteristics
*characteristics
;
68 static irqreturn_t
clk_pll_irq_handler(int irq
, void *dev_id
)
70 struct clk_pll
*pll
= (struct clk_pll
*)dev_id
;
73 disable_irq_nosync(pll
->irq
);
78 static int clk_pll_prepare(struct clk_hw
*hw
)
80 struct clk_pll
*pll
= to_clk_pll(hw
);
81 struct at91_pmc
*pmc
= pll
->pmc
;
82 const struct clk_pll_layout
*layout
= pll
->layout
;
83 const struct clk_pll_characteristics
*characteristics
=
86 u32 mask
= PLL_STATUS_MASK(id
);
87 int offset
= PLL_REG(id
);
93 pllr
= pmc_read(pmc
, offset
);
95 mul
= PLL_MUL(pllr
, layout
);
97 if ((pmc_read(pmc
, AT91_PMC_SR
) & mask
) &&
98 (div
== pll
->div
&& mul
== pll
->mul
))
101 if (characteristics
->out
)
102 out
= characteristics
->out
[pll
->range
];
103 if (characteristics
->icpll
) {
104 icpr
= pmc_read(pmc
, AT91_PMC_PLLICPR
) & ~PLL_ICPR_MASK(id
);
105 icpr
|= (characteristics
->icpll
[pll
->range
] <<
107 pmc_write(pmc
, AT91_PMC_PLLICPR
, icpr
);
110 pllr
&= ~layout
->pllr_mask
;
111 pllr
|= layout
->pllr_mask
&
112 (pll
->div
| (PLL_MAX_COUNT
<< PLL_COUNT_SHIFT
) |
113 (out
<< PLL_OUT_SHIFT
) |
114 ((pll
->mul
& layout
->mul_mask
) << layout
->mul_shift
));
115 pmc_write(pmc
, offset
, pllr
);
117 while (!(pmc_read(pmc
, AT91_PMC_SR
) & mask
)) {
118 enable_irq(pll
->irq
);
119 wait_event(pll
->wait
,
120 pmc_read(pmc
, AT91_PMC_SR
) & mask
);
126 static int clk_pll_is_prepared(struct clk_hw
*hw
)
128 struct clk_pll
*pll
= to_clk_pll(hw
);
129 struct at91_pmc
*pmc
= pll
->pmc
;
131 return !!(pmc_read(pmc
, AT91_PMC_SR
) &
132 PLL_STATUS_MASK(pll
->id
));
135 static void clk_pll_unprepare(struct clk_hw
*hw
)
137 struct clk_pll
*pll
= to_clk_pll(hw
);
138 struct at91_pmc
*pmc
= pll
->pmc
;
139 const struct clk_pll_layout
*layout
= pll
->layout
;
140 int offset
= PLL_REG(pll
->id
);
141 u32 tmp
= pmc_read(pmc
, offset
) & ~(layout
->pllr_mask
);
143 pmc_write(pmc
, offset
, tmp
);
146 static unsigned long clk_pll_recalc_rate(struct clk_hw
*hw
,
147 unsigned long parent_rate
)
149 struct clk_pll
*pll
= to_clk_pll(hw
);
150 const struct clk_pll_layout
*layout
= pll
->layout
;
151 struct at91_pmc
*pmc
= pll
->pmc
;
152 int offset
= PLL_REG(pll
->id
);
153 u32 tmp
= pmc_read(pmc
, offset
) & layout
->pllr_mask
;
154 u8 div
= PLL_DIV(tmp
);
155 u16 mul
= PLL_MUL(tmp
, layout
);
159 return (parent_rate
* (mul
+ 1)) / div
;
162 static long clk_pll_get_best_div_mul(struct clk_pll
*pll
, unsigned long rate
,
163 unsigned long parent_rate
,
166 unsigned long maxrate
;
167 unsigned long minrate
;
168 unsigned long divrate
;
169 unsigned long bestdiv
= 1;
170 unsigned long bestmul
;
171 unsigned long tmpdiv
;
172 unsigned long roundup
;
173 unsigned long rounddown
;
174 unsigned long remainder
;
175 unsigned long bestremainder
;
176 unsigned long maxmul
;
177 unsigned long maxdiv
;
178 unsigned long mindiv
;
180 const struct clk_pll_layout
*layout
= pll
->layout
;
181 const struct clk_pll_characteristics
*characteristics
=
182 pll
->characteristics
;
184 /* Minimum divider = 1 */
185 /* Maximum multiplier = max_mul */
186 maxmul
= layout
->mul_mask
+ 1;
187 maxrate
= (parent_rate
* maxmul
) / 1;
189 /* Maximum divider = max_div */
190 /* Minimum multiplier = 2 */
191 maxdiv
= PLL_DIV_MAX
;
192 minrate
= (parent_rate
* 2) / maxdiv
;
194 if (parent_rate
< characteristics
->input
.min
||
195 parent_rate
< characteristics
->input
.max
)
198 if (parent_rate
< minrate
|| parent_rate
> maxrate
)
201 for (i
= 0; i
< characteristics
->num_output
; i
++) {
202 if (parent_rate
>= characteristics
->output
[i
].min
&&
203 parent_rate
<= characteristics
->output
[i
].max
)
207 if (i
>= characteristics
->num_output
)
210 bestmul
= rate
/ parent_rate
;
211 rounddown
= parent_rate
% rate
;
212 roundup
= rate
- rounddown
;
213 bestremainder
= roundup
< rounddown
? roundup
: rounddown
;
215 if (!bestremainder
) {
225 maxdiv
= 255 / (bestmul
+ 1);
226 if (parent_rate
/ maxdiv
< characteristics
->input
.min
)
227 maxdiv
= parent_rate
/ characteristics
->input
.min
;
228 mindiv
= parent_rate
/ characteristics
->input
.max
;
229 if (parent_rate
% characteristics
->input
.max
)
232 for (tmpdiv
= mindiv
; tmpdiv
< maxdiv
; tmpdiv
++) {
233 divrate
= parent_rate
/ tmpdiv
;
235 rounddown
= rate
% divrate
;
236 roundup
= divrate
- rounddown
;
237 remainder
= roundup
< rounddown
? roundup
: rounddown
;
239 if (remainder
< bestremainder
) {
240 bestremainder
= remainder
;
241 bestmul
= rate
/ divrate
;
249 rate
= (parent_rate
/ bestdiv
) * bestmul
;
261 static long clk_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
262 unsigned long *parent_rate
)
264 struct clk_pll
*pll
= to_clk_pll(hw
);
266 return clk_pll_get_best_div_mul(pll
, rate
, *parent_rate
,
270 static int clk_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
271 unsigned long parent_rate
)
273 struct clk_pll
*pll
= to_clk_pll(hw
);
279 ret
= clk_pll_get_best_div_mul(pll
, rate
, parent_rate
,
291 static const struct clk_ops pll_ops
= {
292 .prepare
= clk_pll_prepare
,
293 .unprepare
= clk_pll_unprepare
,
294 .is_prepared
= clk_pll_is_prepared
,
295 .recalc_rate
= clk_pll_recalc_rate
,
296 .round_rate
= clk_pll_round_rate
,
297 .set_rate
= clk_pll_set_rate
,
300 static struct clk
* __init
301 at91_clk_register_pll(struct at91_pmc
*pmc
, unsigned int irq
, const char *name
,
302 const char *parent_name
, u8 id
,
303 const struct clk_pll_layout
*layout
,
304 const struct clk_pll_characteristics
*characteristics
)
307 struct clk
*clk
= NULL
;
308 struct clk_init_data init
;
310 int offset
= PLL_REG(id
);
314 return ERR_PTR(-EINVAL
);
316 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
318 return ERR_PTR(-ENOMEM
);
322 init
.parent_names
= &parent_name
;
323 init
.num_parents
= 1;
324 init
.flags
= CLK_SET_RATE_GATE
;
327 pll
->hw
.init
= &init
;
328 pll
->layout
= layout
;
329 pll
->characteristics
= characteristics
;
332 tmp
= pmc_read(pmc
, offset
) & layout
->pllr_mask
;
333 pll
->div
= PLL_DIV(tmp
);
334 pll
->mul
= PLL_MUL(tmp
, layout
);
335 init_waitqueue_head(&pll
->wait
);
336 irq_set_status_flags(pll
->irq
, IRQ_NOAUTOEN
);
337 ret
= request_irq(pll
->irq
, clk_pll_irq_handler
, IRQF_TRIGGER_HIGH
,
338 id
? "clk-pllb" : "clk-plla", pll
);
342 clk
= clk_register(NULL
, &pll
->hw
);
350 static const struct clk_pll_layout at91rm9200_pll_layout
= {
351 .pllr_mask
= 0x7FFFFFF,
356 static const struct clk_pll_layout at91sam9g45_pll_layout
= {
357 .pllr_mask
= 0xFFFFFF,
362 static const struct clk_pll_layout at91sam9g20_pllb_layout
= {
363 .pllr_mask
= 0x3FFFFF,
368 static const struct clk_pll_layout sama5d3_pll_layout
= {
369 .pllr_mask
= 0x1FFFFFF,
375 static struct clk_pll_characteristics
* __init
376 of_at91_clk_pll_get_characteristics(struct device_node
*np
)
383 struct clk_range input
;
384 struct clk_range
*output
;
387 struct clk_pll_characteristics
*characteristics
;
389 if (of_at91_get_clk_range(np
, "atmel,clk-input-range", &input
))
392 if (of_property_read_u32(np
, "#atmel,pll-clk-output-range-cells",
396 if (num_cells
< 2 || num_cells
> 4)
399 if (!of_get_property(np
, "atmel,pll-clk-output-ranges", &tmp
))
401 num_output
= tmp
/ (sizeof(u32
) * num_cells
);
403 characteristics
= kzalloc(sizeof(*characteristics
), GFP_KERNEL
);
404 if (!characteristics
)
407 output
= kzalloc(sizeof(*output
) * num_output
, GFP_KERNEL
);
409 goto out_free_characteristics
;
412 out
= kzalloc(sizeof(*out
) * num_output
, GFP_KERNEL
);
414 goto out_free_output
;
418 icpll
= kzalloc(sizeof(*icpll
) * num_output
, GFP_KERNEL
);
420 goto out_free_output
;
423 for (i
= 0; i
< num_output
; i
++) {
424 offset
= i
* num_cells
;
425 if (of_property_read_u32_index(np
,
426 "atmel,pll-clk-output-ranges",
428 goto out_free_output
;
430 if (of_property_read_u32_index(np
,
431 "atmel,pll-clk-output-ranges",
433 goto out_free_output
;
439 if (of_property_read_u32_index(np
,
440 "atmel,pll-clk-output-ranges",
442 goto out_free_output
;
448 if (of_property_read_u32_index(np
,
449 "atmel,pll-clk-output-ranges",
451 goto out_free_output
;
455 characteristics
->input
= input
;
456 characteristics
->num_output
= num_output
;
457 characteristics
->output
= output
;
458 characteristics
->out
= out
;
459 characteristics
->icpll
= icpll
;
460 return characteristics
;
466 out_free_characteristics
:
467 kfree(characteristics
);
472 of_at91_clk_pll_setup(struct device_node
*np
, struct at91_pmc
*pmc
,
473 const struct clk_pll_layout
*layout
)
478 const char *parent_name
;
479 const char *name
= np
->name
;
480 struct clk_pll_characteristics
*characteristics
;
482 if (of_property_read_u32(np
, "reg", &id
))
485 parent_name
= of_clk_get_parent_name(np
, 0);
487 of_property_read_string(np
, "clock-output-names", &name
);
489 characteristics
= of_at91_clk_pll_get_characteristics(np
);
490 if (!characteristics
)
493 irq
= irq_of_parse_and_map(np
, 0);
497 clk
= at91_clk_register_pll(pmc
, irq
, name
, parent_name
, id
, layout
,
500 goto out_free_characteristics
;
502 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
505 out_free_characteristics
:
506 kfree(characteristics
);
509 void __init
of_at91rm9200_clk_pll_setup(struct device_node
*np
,
510 struct at91_pmc
*pmc
)
512 of_at91_clk_pll_setup(np
, pmc
, &at91rm9200_pll_layout
);
515 void __init
of_at91sam9g45_clk_pll_setup(struct device_node
*np
,
516 struct at91_pmc
*pmc
)
518 of_at91_clk_pll_setup(np
, pmc
, &at91sam9g45_pll_layout
);
521 void __init
of_at91sam9g20_clk_pllb_setup(struct device_node
*np
,
522 struct at91_pmc
*pmc
)
524 of_at91_clk_pll_setup(np
, pmc
, &at91sam9g20_pllb_layout
);
527 void __init
of_sama5d3_clk_pll_setup(struct device_node
*np
,
528 struct at91_pmc
*pmc
)
530 of_at91_clk_pll_setup(np
, pmc
, &sama5d3_pll_layout
);