blob: a1adcf186023466bb000443fd57df5e71b313977 [file] [log] [blame]
Boris BREZILLON1a748d22013-10-11 10:48:26 +02001/*
2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
3 *
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.
8 *
9 */
10
11#include <linux/clk-provider.h>
12#include <linux/clkdev.h>
13#include <linux/clk/at91_pmc.h>
14#include <linux/of.h>
15#include <linux/of_address.h>
16#include <linux/of_irq.h>
17#include <linux/io.h>
Boris BREZILLON3ef9dd22014-09-02 09:50:15 +020018#include <linux/kernel.h>
Boris BREZILLON1a748d22013-10-11 10:48:26 +020019#include <linux/wait.h>
20#include <linux/sched.h>
21#include <linux/interrupt.h>
22#include <linux/irq.h>
23
24#include "pmc.h"
25
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) & \
32 (layout)->mul_mask)
Boris BREZILLON3ef9dd22014-09-02 09:50:15 +020033#define PLL_MUL_MIN 2
34#define PLL_MUL_MASK(layout) ((layout)->mul_mask)
35#define PLL_MUL_MAX(layout) (PLL_MUL_MASK(layout) + 1)
Boris BREZILLON1a748d22013-10-11 10:48:26 +020036#define PLL_ICPR_SHIFT(id) ((id) * 16)
37#define PLL_ICPR_MASK(id) (0xffff << PLL_ICPR_SHIFT(id))
Boris BREZILLON078a3eb2014-09-02 09:50:14 +020038#define PLL_MAX_COUNT 0x3f
Boris BREZILLON1a748d22013-10-11 10:48:26 +020039#define PLL_COUNT_SHIFT 8
40#define PLL_OUT_SHIFT 14
41#define PLL_MAX_ID 1
42
43struct clk_pll_characteristics {
44 struct clk_range input;
45 int num_output;
46 struct clk_range *output;
47 u16 *icpll;
48 u8 *out;
49};
50
51struct clk_pll_layout {
52 u32 pllr_mask;
53 u16 mul_mask;
54 u8 mul_shift;
55};
56
57#define to_clk_pll(hw) container_of(hw, struct clk_pll, hw)
58
59struct clk_pll {
60 struct clk_hw hw;
61 struct at91_pmc *pmc;
62 unsigned int irq;
63 wait_queue_head_t wait;
64 u8 id;
65 u8 div;
66 u8 range;
67 u16 mul;
68 const struct clk_pll_layout *layout;
69 const struct clk_pll_characteristics *characteristics;
70};
71
72static irqreturn_t clk_pll_irq_handler(int irq, void *dev_id)
73{
74 struct clk_pll *pll = (struct clk_pll *)dev_id;
75
76 wake_up(&pll->wait);
77 disable_irq_nosync(pll->irq);
78
79 return IRQ_HANDLED;
80}
81
82static int clk_pll_prepare(struct clk_hw *hw)
83{
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;
Boris BREZILLONe442d232013-10-11 10:51:23 +020087 const struct clk_pll_characteristics *characteristics =
88 pll->characteristics;
Boris BREZILLON1a748d22013-10-11 10:48:26 +020089 u8 id = pll->id;
90 u32 mask = PLL_STATUS_MASK(id);
91 int offset = PLL_REG(id);
92 u8 out = 0;
93 u32 pllr, icpr;
94 u8 div;
95 u16 mul;
96
97 pllr = pmc_read(pmc, offset);
98 div = PLL_DIV(pllr);
99 mul = PLL_MUL(pllr, layout);
100
101 if ((pmc_read(pmc, AT91_PMC_SR) & mask) &&
102 (div == pll->div && mul == pll->mul))
103 return 0;
104
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] <<
110 PLL_ICPR_SHIFT(id));
111 pmc_write(pmc, AT91_PMC_PLLICPR, icpr);
112 }
113
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);
120
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);
125 }
126
127 return 0;
128}
129
130static int clk_pll_is_prepared(struct clk_hw *hw)
131{
132 struct clk_pll *pll = to_clk_pll(hw);
133 struct at91_pmc *pmc = pll->pmc;
134
135 return !!(pmc_read(pmc, AT91_PMC_SR) &
136 PLL_STATUS_MASK(pll->id));
137}
138
139static void clk_pll_unprepare(struct clk_hw *hw)
140{
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);
146
147 pmc_write(pmc, offset, tmp);
148}
149
150static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
151 unsigned long parent_rate)
152{
153 struct clk_pll *pll = to_clk_pll(hw);
154 const struct clk_pll_layout *layout = pll->layout;
155 struct at91_pmc *pmc = pll->pmc;
156 int offset = PLL_REG(pll->id);
157 u32 tmp = pmc_read(pmc, offset) & layout->pllr_mask;
158 u8 div = PLL_DIV(tmp);
159 u16 mul = PLL_MUL(tmp, layout);
160 if (!div || !mul)
161 return 0;
162
163 return (parent_rate * (mul + 1)) / div;
164}
165
166static long clk_pll_get_best_div_mul(struct clk_pll *pll, unsigned long rate,
167 unsigned long parent_rate,
168 u32 *div, u32 *mul,
169 u32 *index) {
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200170 const struct clk_pll_layout *layout = pll->layout;
171 const struct clk_pll_characteristics *characteristics =
172 pll->characteristics;
Boris BREZILLON3ef9dd22014-09-02 09:50:15 +0200173 unsigned long bestremainder = ULONG_MAX;
174 unsigned long maxdiv, mindiv, tmpdiv;
175 long bestrate = -ERANGE;
176 unsigned long bestdiv;
177 unsigned long bestmul;
178 int i = 0;
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200179
Boris BREZILLON3ef9dd22014-09-02 09:50:15 +0200180 /* Check if parent_rate is a valid input rate */
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200181 if (parent_rate < characteristics->input.min ||
Boris BREZILLON3ef9dd22014-09-02 09:50:15 +0200182 parent_rate > characteristics->input.max)
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200183 return -ERANGE;
184
Boris BREZILLON3ef9dd22014-09-02 09:50:15 +0200185 /*
186 * Calculate minimum divider based on the minimum multiplier, the
187 * parent_rate and the requested rate.
188 * Should always be 2 according to the input and output characteristics
189 * of the PLL blocks.
190 */
191 mindiv = (parent_rate * PLL_MUL_MIN) / rate;
192 if (!mindiv)
193 mindiv = 1;
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200194
Boris BREZILLON3ef9dd22014-09-02 09:50:15 +0200195 /*
196 * Calculate the maximum divider which is limited by PLL register
197 * layout (limited by the MUL or DIV field size).
198 */
199 maxdiv = DIV_ROUND_UP(parent_rate * PLL_MUL_MAX(layout), rate);
200 if (maxdiv > PLL_DIV_MAX)
201 maxdiv = PLL_DIV_MAX;
202
203 /*
204 * Iterate over the acceptable divider values to find the best
205 * divider/multiplier pair (the one that generates the closest
206 * rate to the requested one).
207 */
208 for (tmpdiv = mindiv; tmpdiv <= maxdiv; tmpdiv++) {
209 unsigned long remainder;
210 unsigned long tmprate;
211 unsigned long tmpmul;
212
213 /*
214 * Calculate the multiplier associated with the current
215 * divider that provide the closest rate to the requested one.
216 */
217 tmpmul = DIV_ROUND_CLOSEST(rate, parent_rate / tmpdiv);
218 tmprate = (parent_rate / tmpdiv) * tmpmul;
219 if (tmprate > rate)
220 remainder = tmprate - rate;
221 else
222 remainder = rate - tmprate;
223
224 /*
225 * Compare the remainder with the best remainder found until
226 * now and elect a new best multiplier/divider pair if the
227 * current remainder is smaller than the best one.
228 */
229 if (remainder < bestremainder) {
230 bestremainder = remainder;
231 bestdiv = tmpdiv;
232 bestmul = tmpmul;
233 bestrate = tmprate;
234 }
235
236 /*
237 * We've found a perfect match!
238 * Stop searching now and use this multiplier/divider pair.
239 */
240 if (!remainder)
241 break;
242 }
243
244 /* We haven't found any multiplier/divider pair => return -ERANGE */
245 if (bestrate < 0)
246 return bestrate;
247
248 /* Check if bestrate is a valid output rate */
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200249 for (i = 0; i < characteristics->num_output; i++) {
Boris BREZILLON3ef9dd22014-09-02 09:50:15 +0200250 if (bestrate >= characteristics->output[i].min &&
251 bestrate <= characteristics->output[i].max)
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200252 break;
253 }
254
255 if (i >= characteristics->num_output)
256 return -ERANGE;
257
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200258 if (div)
259 *div = bestdiv;
260 if (mul)
Boris BREZILLON3ef9dd22014-09-02 09:50:15 +0200261 *mul = bestmul - 1;
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200262 if (index)
263 *index = i;
264
Boris BREZILLON3ef9dd22014-09-02 09:50:15 +0200265 return bestrate;
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200266}
267
268static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
269 unsigned long *parent_rate)
270{
271 struct clk_pll *pll = to_clk_pll(hw);
272
273 return clk_pll_get_best_div_mul(pll, rate, *parent_rate,
274 NULL, NULL, NULL);
275}
276
277static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
278 unsigned long parent_rate)
279{
280 struct clk_pll *pll = to_clk_pll(hw);
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200281 long ret;
282 u32 div;
283 u32 mul;
284 u32 index;
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200285
286 ret = clk_pll_get_best_div_mul(pll, rate, parent_rate,
287 &div, &mul, &index);
288 if (ret < 0)
289 return ret;
290
291 pll->range = index;
292 pll->div = div;
293 pll->mul = mul;
294
295 return 0;
296}
297
298static const struct clk_ops pll_ops = {
299 .prepare = clk_pll_prepare,
300 .unprepare = clk_pll_unprepare,
301 .is_prepared = clk_pll_is_prepared,
302 .recalc_rate = clk_pll_recalc_rate,
303 .round_rate = clk_pll_round_rate,
304 .set_rate = clk_pll_set_rate,
305};
306
307static struct clk * __init
308at91_clk_register_pll(struct at91_pmc *pmc, unsigned int irq, const char *name,
309 const char *parent_name, u8 id,
310 const struct clk_pll_layout *layout,
311 const struct clk_pll_characteristics *characteristics)
312{
313 struct clk_pll *pll;
314 struct clk *clk = NULL;
315 struct clk_init_data init;
316 int ret;
317 int offset = PLL_REG(id);
318 u32 tmp;
319
320 if (id > PLL_MAX_ID)
321 return ERR_PTR(-EINVAL);
322
323 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
324 if (!pll)
325 return ERR_PTR(-ENOMEM);
326
327 init.name = name;
328 init.ops = &pll_ops;
329 init.parent_names = &parent_name;
330 init.num_parents = 1;
331 init.flags = CLK_SET_RATE_GATE;
332
333 pll->id = id;
334 pll->hw.init = &init;
335 pll->layout = layout;
336 pll->characteristics = characteristics;
337 pll->pmc = pmc;
338 pll->irq = irq;
339 tmp = pmc_read(pmc, offset) & layout->pllr_mask;
340 pll->div = PLL_DIV(tmp);
341 pll->mul = PLL_MUL(tmp, layout);
342 init_waitqueue_head(&pll->wait);
343 irq_set_status_flags(pll->irq, IRQ_NOAUTOEN);
344 ret = request_irq(pll->irq, clk_pll_irq_handler, IRQF_TRIGGER_HIGH,
345 id ? "clk-pllb" : "clk-plla", pll);
346 if (ret)
347 return ERR_PTR(ret);
348
349 clk = clk_register(NULL, &pll->hw);
350 if (IS_ERR(clk))
351 kfree(pll);
352
353 return clk;
354}
355
356
357static const struct clk_pll_layout at91rm9200_pll_layout = {
358 .pllr_mask = 0x7FFFFFF,
359 .mul_shift = 16,
360 .mul_mask = 0x7FF,
361};
362
363static const struct clk_pll_layout at91sam9g45_pll_layout = {
364 .pllr_mask = 0xFFFFFF,
365 .mul_shift = 16,
366 .mul_mask = 0xFF,
367};
368
369static const struct clk_pll_layout at91sam9g20_pllb_layout = {
370 .pllr_mask = 0x3FFFFF,
371 .mul_shift = 16,
372 .mul_mask = 0x3F,
373};
374
375static const struct clk_pll_layout sama5d3_pll_layout = {
376 .pllr_mask = 0x1FFFFFF,
377 .mul_shift = 18,
378 .mul_mask = 0x7F,
379};
380
381
382static struct clk_pll_characteristics * __init
383of_at91_clk_pll_get_characteristics(struct device_node *np)
384{
385 int i;
386 int offset;
387 u32 tmp;
388 int num_output;
389 u32 num_cells;
390 struct clk_range input;
391 struct clk_range *output;
392 u8 *out = NULL;
393 u16 *icpll = NULL;
394 struct clk_pll_characteristics *characteristics;
395
396 if (of_at91_get_clk_range(np, "atmel,clk-input-range", &input))
397 return NULL;
398
399 if (of_property_read_u32(np, "#atmel,pll-clk-output-range-cells",
400 &num_cells))
401 return NULL;
402
403 if (num_cells < 2 || num_cells > 4)
404 return NULL;
405
406 if (!of_get_property(np, "atmel,pll-clk-output-ranges", &tmp))
407 return NULL;
408 num_output = tmp / (sizeof(u32) * num_cells);
409
410 characteristics = kzalloc(sizeof(*characteristics), GFP_KERNEL);
411 if (!characteristics)
412 return NULL;
413
414 output = kzalloc(sizeof(*output) * num_output, GFP_KERNEL);
415 if (!output)
416 goto out_free_characteristics;
417
418 if (num_cells > 2) {
419 out = kzalloc(sizeof(*out) * num_output, GFP_KERNEL);
420 if (!out)
421 goto out_free_output;
422 }
423
424 if (num_cells > 3) {
425 icpll = kzalloc(sizeof(*icpll) * num_output, GFP_KERNEL);
426 if (!icpll)
427 goto out_free_output;
428 }
429
430 for (i = 0; i < num_output; i++) {
431 offset = i * num_cells;
432 if (of_property_read_u32_index(np,
433 "atmel,pll-clk-output-ranges",
434 offset, &tmp))
435 goto out_free_output;
436 output[i].min = tmp;
437 if (of_property_read_u32_index(np,
438 "atmel,pll-clk-output-ranges",
439 offset + 1, &tmp))
440 goto out_free_output;
441 output[i].max = tmp;
442
443 if (num_cells == 2)
444 continue;
445
446 if (of_property_read_u32_index(np,
447 "atmel,pll-clk-output-ranges",
448 offset + 2, &tmp))
449 goto out_free_output;
450 out[i] = tmp;
451
452 if (num_cells == 3)
453 continue;
454
455 if (of_property_read_u32_index(np,
456 "atmel,pll-clk-output-ranges",
457 offset + 3, &tmp))
458 goto out_free_output;
459 icpll[i] = tmp;
460 }
461
462 characteristics->input = input;
463 characteristics->num_output = num_output;
464 characteristics->output = output;
465 characteristics->out = out;
466 characteristics->icpll = icpll;
467 return characteristics;
468
469out_free_output:
470 kfree(icpll);
471 kfree(out);
472 kfree(output);
473out_free_characteristics:
474 kfree(characteristics);
475 return NULL;
476}
477
478static void __init
479of_at91_clk_pll_setup(struct device_node *np, struct at91_pmc *pmc,
480 const struct clk_pll_layout *layout)
481{
482 u32 id;
483 unsigned int irq;
484 struct clk *clk;
485 const char *parent_name;
486 const char *name = np->name;
487 struct clk_pll_characteristics *characteristics;
488
489 if (of_property_read_u32(np, "reg", &id))
490 return;
491
492 parent_name = of_clk_get_parent_name(np, 0);
493
494 of_property_read_string(np, "clock-output-names", &name);
495
496 characteristics = of_at91_clk_pll_get_characteristics(np);
497 if (!characteristics)
498 return;
499
500 irq = irq_of_parse_and_map(np, 0);
501 if (!irq)
502 return;
503
504 clk = at91_clk_register_pll(pmc, irq, name, parent_name, id, layout,
505 characteristics);
506 if (IS_ERR(clk))
507 goto out_free_characteristics;
508
509 of_clk_add_provider(np, of_clk_src_simple_get, clk);
510 return;
511
512out_free_characteristics:
513 kfree(characteristics);
514}
515
516void __init of_at91rm9200_clk_pll_setup(struct device_node *np,
517 struct at91_pmc *pmc)
518{
519 of_at91_clk_pll_setup(np, pmc, &at91rm9200_pll_layout);
520}
521
522void __init of_at91sam9g45_clk_pll_setup(struct device_node *np,
523 struct at91_pmc *pmc)
524{
525 of_at91_clk_pll_setup(np, pmc, &at91sam9g45_pll_layout);
526}
527
528void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np,
529 struct at91_pmc *pmc)
530{
531 of_at91_clk_pll_setup(np, pmc, &at91sam9g20_pllb_layout);
532}
533
534void __init of_sama5d3_clk_pll_setup(struct device_node *np,
535 struct at91_pmc *pmc)
536{
537 of_at91_clk_pll_setup(np, pmc, &sama5d3_pll_layout);
538}