blob: fc06abe5eaaf8d7e57473cdba45d3cad4385ecf3 [file] [log] [blame]
Tony Lindgren163152c2015-01-13 14:51:27 -08001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation version 2.
5 *
6 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
7 * kind, whether express or implied; without even the implied warranty
8 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 */
11
12#include <linux/clk-provider.h>
13#include <linux/delay.h>
Tony Lindgren163152c2015-01-13 14:51:27 -080014#include <linux/err.h>
Tony Lindgrencafeb002015-03-16 18:04:20 -070015#include <linux/math64.h>
Tony Lindgren163152c2015-01-13 14:51:27 -080016#include <linux/of.h>
17#include <linux/of_address.h>
18#include <linux/clk/ti.h>
Tony Lindgren163152c2015-01-13 14:51:27 -080019
20/* FAPLL Control Register PLL_CTRL */
21#define FAPLL_MAIN_LOCK BIT(7)
22#define FAPLL_MAIN_PLLEN BIT(3)
23#define FAPLL_MAIN_BP BIT(2)
24#define FAPLL_MAIN_LOC_CTL BIT(0)
25
26/* FAPLL powerdown register PWD */
27#define FAPLL_PWD_OFFSET 4
28
29#define MAX_FAPLL_OUTPUTS 7
30#define FAPLL_MAX_RETRIES 1000
31
32#define to_fapll(_hw) container_of(_hw, struct fapll_data, hw)
33#define to_synth(_hw) container_of(_hw, struct fapll_synth, hw)
34
35/* The bypass bit is inverted on the ddr_pll.. */
36#define fapll_is_ddr_pll(va) (((u32)(va) & 0xffff) == 0x0440)
37
38/*
39 * The audio_pll_clk1 input is hard wired to the 27MHz bypass clock,
40 * and the audio_pll_clk1 synthesizer is hardwared to 32KiHz output.
41 */
42#define is_ddr_pll_clk1(va) (((u32)(va) & 0xffff) == 0x044c)
43#define is_audio_pll_clk1(va) (((u32)(va) & 0xffff) == 0x04a8)
44
45/* Synthesizer divider register */
46#define SYNTH_LDMDIV1 BIT(8)
47
48/* Synthesizer frequency register */
49#define SYNTH_LDFREQ BIT(31)
50
Tony Lindgrencafeb002015-03-16 18:04:20 -070051#define SYNTH_PHASE_K 8
52#define SYNTH_MAX_INT_DIV 0xf
Tony Lindgren33ca29c2015-03-22 15:35:24 -070053#define SYNTH_MAX_DIV_M 0xff
54
Tony Lindgren163152c2015-01-13 14:51:27 -080055struct fapll_data {
56 struct clk_hw hw;
57 void __iomem *base;
58 const char *name;
59 struct clk *clk_ref;
60 struct clk *clk_bypass;
61 struct clk_onecell_data outputs;
62 bool bypass_bit_inverted;
63};
64
65struct fapll_synth {
66 struct clk_hw hw;
67 struct fapll_data *fd;
68 int index;
69 void __iomem *freq;
70 void __iomem *div;
71 const char *name;
72 struct clk *clk_pll;
73};
74
75static bool ti_fapll_clock_is_bypass(struct fapll_data *fd)
76{
77 u32 v = readl_relaxed(fd->base);
78
79 if (fd->bypass_bit_inverted)
80 return !(v & FAPLL_MAIN_BP);
81 else
82 return !!(v & FAPLL_MAIN_BP);
83}
84
85static int ti_fapll_enable(struct clk_hw *hw)
86{
87 struct fapll_data *fd = to_fapll(hw);
88 u32 v = readl_relaxed(fd->base);
89
90 v |= (1 << FAPLL_MAIN_PLLEN);
91 writel_relaxed(v, fd->base);
92
93 return 0;
94}
95
96static void ti_fapll_disable(struct clk_hw *hw)
97{
98 struct fapll_data *fd = to_fapll(hw);
99 u32 v = readl_relaxed(fd->base);
100
101 v &= ~(1 << FAPLL_MAIN_PLLEN);
102 writel_relaxed(v, fd->base);
103}
104
105static int ti_fapll_is_enabled(struct clk_hw *hw)
106{
107 struct fapll_data *fd = to_fapll(hw);
108 u32 v = readl_relaxed(fd->base);
109
110 return v & (1 << FAPLL_MAIN_PLLEN);
111}
112
113static unsigned long ti_fapll_recalc_rate(struct clk_hw *hw,
114 unsigned long parent_rate)
115{
116 struct fapll_data *fd = to_fapll(hw);
117 u32 fapll_n, fapll_p, v;
118 long long rate;
119
120 if (ti_fapll_clock_is_bypass(fd))
121 return parent_rate;
122
123 rate = parent_rate;
124
125 /* PLL pre-divider is P and multiplier is N */
126 v = readl_relaxed(fd->base);
127 fapll_p = (v >> 8) & 0xff;
128 if (fapll_p)
129 do_div(rate, fapll_p);
130 fapll_n = v >> 16;
131 if (fapll_n)
132 rate *= fapll_n;
133
134 return rate;
135}
136
137static u8 ti_fapll_get_parent(struct clk_hw *hw)
138{
139 struct fapll_data *fd = to_fapll(hw);
140
141 if (ti_fapll_clock_is_bypass(fd))
142 return 1;
143
144 return 0;
145}
146
147static struct clk_ops ti_fapll_ops = {
148 .enable = ti_fapll_enable,
149 .disable = ti_fapll_disable,
150 .is_enabled = ti_fapll_is_enabled,
151 .recalc_rate = ti_fapll_recalc_rate,
152 .get_parent = ti_fapll_get_parent,
153};
154
155static int ti_fapll_synth_enable(struct clk_hw *hw)
156{
157 struct fapll_synth *synth = to_synth(hw);
158 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
159
160 v &= ~(1 << synth->index);
161 writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET);
162
163 return 0;
164}
165
166static void ti_fapll_synth_disable(struct clk_hw *hw)
167{
168 struct fapll_synth *synth = to_synth(hw);
169 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
170
171 v |= 1 << synth->index;
172 writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET);
173}
174
175static int ti_fapll_synth_is_enabled(struct clk_hw *hw)
176{
177 struct fapll_synth *synth = to_synth(hw);
178 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
179
180 return !(v & (1 << synth->index));
181}
182
183/*
184 * See dm816x TRM chapter 1.10.3 Flying Adder PLL fore more info
185 */
186static unsigned long ti_fapll_synth_recalc_rate(struct clk_hw *hw,
187 unsigned long parent_rate)
188{
189 struct fapll_synth *synth = to_synth(hw);
190 u32 synth_div_m;
191 long long rate;
192
193 /* The audio_pll_clk1 is hardwired to produce 32.768KiHz clock */
194 if (!synth->div)
195 return 32768;
196
197 /*
198 * PLL in bypass sets the synths in bypass mode too. The PLL rate
199 * can be also be set to 27MHz, so we can't use parent_rate to
200 * check for bypass mode.
201 */
202 if (ti_fapll_clock_is_bypass(synth->fd))
203 return parent_rate;
204
205 rate = parent_rate;
206
207 /*
208 * Synth frequency integer and fractional divider.
209 * Note that the phase output K is 8, so the result needs
Tony Lindgrencafeb002015-03-16 18:04:20 -0700210 * to be multiplied by SYNTH_PHASE_K.
Tony Lindgren163152c2015-01-13 14:51:27 -0800211 */
212 if (synth->freq) {
213 u32 v, synth_int_div, synth_frac_div, synth_div_freq;
214
215 v = readl_relaxed(synth->freq);
216 synth_int_div = (v >> 24) & 0xf;
217 synth_frac_div = v & 0xffffff;
218 synth_div_freq = (synth_int_div * 10000000) + synth_frac_div;
219 rate *= 10000000;
220 do_div(rate, synth_div_freq);
Tony Lindgrencafeb002015-03-16 18:04:20 -0700221 rate *= SYNTH_PHASE_K;
Tony Lindgren163152c2015-01-13 14:51:27 -0800222 }
223
Tony Lindgren33ca29c2015-03-22 15:35:24 -0700224 /* Synth post-divider M */
225 synth_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M;
Tony Lindgren163152c2015-01-13 14:51:27 -0800226
Tony Lindgren33ca29c2015-03-22 15:35:24 -0700227 return DIV_ROUND_UP_ULL(rate, synth_div_m);
Tony Lindgren163152c2015-01-13 14:51:27 -0800228}
229
Tony Lindgrencafeb002015-03-16 18:04:20 -0700230static unsigned long ti_fapll_synth_get_frac_rate(struct clk_hw *hw,
231 unsigned long parent_rate)
232{
233 struct fapll_synth *synth = to_synth(hw);
234 unsigned long current_rate, frac_rate;
235 u32 post_div_m;
236
237 current_rate = ti_fapll_synth_recalc_rate(hw, parent_rate);
238 post_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M;
239 frac_rate = current_rate * post_div_m;
240
241 return frac_rate;
242}
243
244static u32 ti_fapll_synth_set_frac_rate(struct fapll_synth *synth,
245 unsigned long rate,
246 unsigned long parent_rate)
247{
248 u32 post_div_m, synth_int_div = 0, synth_frac_div = 0, v;
249
250 post_div_m = DIV_ROUND_UP_ULL((u64)parent_rate * SYNTH_PHASE_K, rate);
251 post_div_m = post_div_m / SYNTH_MAX_INT_DIV;
252 if (post_div_m > SYNTH_MAX_DIV_M)
253 return -EINVAL;
254 if (!post_div_m)
255 post_div_m = 1;
256
257 for (; post_div_m < SYNTH_MAX_DIV_M; post_div_m++) {
258 synth_int_div = DIV_ROUND_UP_ULL((u64)parent_rate *
259 SYNTH_PHASE_K *
260 10000000,
261 rate * post_div_m);
262 synth_frac_div = synth_int_div % 10000000;
263 synth_int_div /= 10000000;
264
265 if (synth_int_div <= SYNTH_MAX_INT_DIV)
266 break;
267 }
268
269 if (synth_int_div > SYNTH_MAX_INT_DIV)
270 return -EINVAL;
271
272 v = readl_relaxed(synth->freq);
273 v &= ~0x1fffffff;
274 v |= (synth_int_div & SYNTH_MAX_INT_DIV) << 24;
275 v |= (synth_frac_div & 0xffffff);
276 v |= SYNTH_LDFREQ;
277 writel_relaxed(v, synth->freq);
278
279 return post_div_m;
280}
281
282static long ti_fapll_synth_round_rate(struct clk_hw *hw, unsigned long rate,
283 unsigned long *parent_rate)
284{
285 struct fapll_synth *synth = to_synth(hw);
286 struct fapll_data *fd = synth->fd;
287 unsigned long r;
288
289 if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate)
290 return -EINVAL;
291
292 /* Only post divider m available with no fractional divider? */
293 if (!synth->freq) {
294 unsigned long frac_rate;
295 u32 synth_post_div_m;
296
297 frac_rate = ti_fapll_synth_get_frac_rate(hw, *parent_rate);
298 synth_post_div_m = DIV_ROUND_UP(frac_rate, rate);
299 r = DIV_ROUND_UP(frac_rate, synth_post_div_m);
300 goto out;
301 }
302
303 r = *parent_rate * SYNTH_PHASE_K;
304 if (rate > r)
305 goto out;
306
307 r = DIV_ROUND_UP_ULL(r, SYNTH_MAX_INT_DIV * SYNTH_MAX_DIV_M);
308 if (rate < r)
309 goto out;
310
311 r = rate;
312out:
313 return r;
314}
315
316static int ti_fapll_synth_set_rate(struct clk_hw *hw, unsigned long rate,
317 unsigned long parent_rate)
318{
319 struct fapll_synth *synth = to_synth(hw);
320 struct fapll_data *fd = synth->fd;
321 unsigned long frac_rate, post_rate = 0;
322 u32 post_div_m = 0, v;
323
324 if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate)
325 return -EINVAL;
326
327 /* Produce the rate with just post divider M? */
328 frac_rate = ti_fapll_synth_get_frac_rate(hw, parent_rate);
329 if (frac_rate < rate) {
330 if (!synth->freq)
331 return -EINVAL;
332 } else {
333 post_div_m = DIV_ROUND_UP(frac_rate, rate);
334 if (post_div_m && (post_div_m <= SYNTH_MAX_DIV_M))
335 post_rate = DIV_ROUND_UP(frac_rate, post_div_m);
336 if (!synth->freq && !post_rate)
337 return -EINVAL;
338 }
339
340 /* Need to recalculate the fractional divider? */
341 if ((post_rate != rate) && synth->freq)
342 post_div_m = ti_fapll_synth_set_frac_rate(synth,
343 rate,
344 parent_rate);
345
346 v = readl_relaxed(synth->div);
347 v &= ~SYNTH_MAX_DIV_M;
348 v |= post_div_m;
349 v |= SYNTH_LDMDIV1;
350 writel_relaxed(v, synth->div);
351
352 return 0;
353}
354
Tony Lindgren163152c2015-01-13 14:51:27 -0800355static struct clk_ops ti_fapll_synt_ops = {
356 .enable = ti_fapll_synth_enable,
357 .disable = ti_fapll_synth_disable,
358 .is_enabled = ti_fapll_synth_is_enabled,
359 .recalc_rate = ti_fapll_synth_recalc_rate,
Tony Lindgrencafeb002015-03-16 18:04:20 -0700360 .round_rate = ti_fapll_synth_round_rate,
361 .set_rate = ti_fapll_synth_set_rate,
Tony Lindgren163152c2015-01-13 14:51:27 -0800362};
363
364static struct clk * __init ti_fapll_synth_setup(struct fapll_data *fd,
365 void __iomem *freq,
366 void __iomem *div,
367 int index,
368 const char *name,
369 const char *parent,
370 struct clk *pll_clk)
371{
372 struct clk_init_data *init;
373 struct fapll_synth *synth;
374
375 init = kzalloc(sizeof(*init), GFP_KERNEL);
376 if (!init)
377 return ERR_PTR(-ENOMEM);
378
379 init->ops = &ti_fapll_synt_ops;
380 init->name = name;
381 init->parent_names = &parent;
382 init->num_parents = 1;
383
384 synth = kzalloc(sizeof(*synth), GFP_KERNEL);
385 if (!synth)
386 goto free;
387
388 synth->fd = fd;
389 synth->index = index;
390 synth->freq = freq;
391 synth->div = div;
392 synth->name = name;
393 synth->hw.init = init;
394 synth->clk_pll = pll_clk;
395
396 return clk_register(NULL, &synth->hw);
397
398free:
399 kfree(synth);
400 kfree(init);
401
402 return ERR_PTR(-ENOMEM);
403}
404
405static void __init ti_fapll_setup(struct device_node *node)
406{
407 struct fapll_data *fd;
408 struct clk_init_data *init = NULL;
409 const char *parent_name[2];
410 struct clk *pll_clk;
411 int i;
412
413 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
414 if (!fd)
415 return;
416
417 fd->outputs.clks = kzalloc(sizeof(struct clk *) *
418 MAX_FAPLL_OUTPUTS + 1,
419 GFP_KERNEL);
420 if (!fd->outputs.clks)
421 goto free;
422
423 init = kzalloc(sizeof(*init), GFP_KERNEL);
424 if (!init)
425 goto free;
426
427 init->ops = &ti_fapll_ops;
428 init->name = node->name;
429
430 init->num_parents = of_clk_get_parent_count(node);
431 if (init->num_parents != 2) {
432 pr_err("%s must have two parents\n", node->name);
433 goto free;
434 }
435
436 parent_name[0] = of_clk_get_parent_name(node, 0);
437 parent_name[1] = of_clk_get_parent_name(node, 1);
438 init->parent_names = parent_name;
439
440 fd->clk_ref = of_clk_get(node, 0);
441 if (IS_ERR(fd->clk_ref)) {
442 pr_err("%s could not get clk_ref\n", node->name);
443 goto free;
444 }
445
446 fd->clk_bypass = of_clk_get(node, 1);
447 if (IS_ERR(fd->clk_bypass)) {
448 pr_err("%s could not get clk_bypass\n", node->name);
449 goto free;
450 }
451
452 fd->base = of_iomap(node, 0);
453 if (!fd->base) {
454 pr_err("%s could not get IO base\n", node->name);
455 goto free;
456 }
457
458 if (fapll_is_ddr_pll(fd->base))
459 fd->bypass_bit_inverted = true;
460
461 fd->name = node->name;
462 fd->hw.init = init;
463
464 /* Register the parent PLL */
465 pll_clk = clk_register(NULL, &fd->hw);
466 if (IS_ERR(pll_clk))
467 goto unmap;
468
469 fd->outputs.clks[0] = pll_clk;
470 fd->outputs.clk_num++;
471
472 /*
473 * Set up the child synthesizers starting at index 1 as the
474 * PLL output is at index 0. We need to check the clock-indices
475 * for numbering in case there are holes in the synth mapping,
476 * and then probe the synth register to see if it has a FREQ
477 * register available.
478 */
479 for (i = 0; i < MAX_FAPLL_OUTPUTS; i++) {
480 const char *output_name;
481 void __iomem *freq, *div;
482 struct clk *synth_clk;
483 int output_instance;
484 u32 v;
485
486 if (of_property_read_string_index(node, "clock-output-names",
487 i, &output_name))
488 continue;
489
490 if (of_property_read_u32_index(node, "clock-indices", i,
491 &output_instance))
492 output_instance = i;
493
494 freq = fd->base + (output_instance * 8);
495 div = freq + 4;
496
497 /* Check for hardwired audio_pll_clk1 */
498 if (is_audio_pll_clk1(freq)) {
499 freq = 0;
500 div = 0;
501 } else {
502 /* Does the synthesizer have a FREQ register? */
503 v = readl_relaxed(freq);
504 if (!v)
505 freq = 0;
506 }
507 synth_clk = ti_fapll_synth_setup(fd, freq, div, output_instance,
508 output_name, node->name,
509 pll_clk);
510 if (IS_ERR(synth_clk))
511 continue;
512
513 fd->outputs.clks[output_instance] = synth_clk;
514 fd->outputs.clk_num++;
515
516 clk_register_clkdev(synth_clk, output_name, NULL);
517 }
518
519 /* Register the child synthesizers as the FAPLL outputs */
520 of_clk_add_provider(node, of_clk_src_onecell_get, &fd->outputs);
521 /* Add clock alias for the outputs */
522
523 kfree(init);
524
525 return;
526
527unmap:
528 iounmap(fd->base);
529free:
530 if (fd->clk_bypass)
531 clk_put(fd->clk_bypass);
532 if (fd->clk_ref)
533 clk_put(fd->clk_ref);
534 kfree(fd->outputs.clks);
535 kfree(fd);
536 kfree(init);
537}
538
539CLK_OF_DECLARE(ti_fapll_clock, "ti,dm816-fapll-clock", ti_fapll_setup);