clk: sunxi: Introduce mbus compatible
[cascardo/linux.git] / drivers / clk / sunxi / clk-sunxi.c
1 /*
2  * Copyright 2013 Emilio López
3  *
4  * Emilio López <emilio@elopez.com.ar>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/clk-provider.h>
18 #include <linux/clkdev.h>
19 #include <linux/of.h>
20 #include <linux/of_address.h>
21 #include <linux/reset-controller.h>
22 #include <linux/spinlock.h>
23
24 #include "clk-factors.h"
25
26 static DEFINE_SPINLOCK(clk_lock);
27
28 /* Maximum number of parents our clocks have */
29 #define SUNXI_MAX_PARENTS       5
30
31 /**
32  * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
33  * PLL1 rate is calculated as follows
34  * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
35  * parent_rate is always 24Mhz
36  */
37
38 static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
39                                    u8 *n, u8 *k, u8 *m, u8 *p)
40 {
41         u8 div;
42
43         /* Normalize value to a 6M multiple */
44         div = *freq / 6000000;
45         *freq = 6000000 * div;
46
47         /* we were called to round the frequency, we can now return */
48         if (n == NULL)
49                 return;
50
51         /* m is always zero for pll1 */
52         *m = 0;
53
54         /* k is 1 only on these cases */
55         if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
56                 *k = 1;
57         else
58                 *k = 0;
59
60         /* p will be 3 for divs under 10 */
61         if (div < 10)
62                 *p = 3;
63
64         /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
65         else if (div < 20 || (div < 32 && (div & 1)))
66                 *p = 2;
67
68         /* p will be 1 for even divs under 32, divs under 40 and odd pairs
69          * of divs between 40-62 */
70         else if (div < 40 || (div < 64 && (div & 2)))
71                 *p = 1;
72
73         /* any other entries have p = 0 */
74         else
75                 *p = 0;
76
77         /* calculate a suitable n based on k and p */
78         div <<= *p;
79         div /= (*k + 1);
80         *n = div / 4;
81 }
82
83 /**
84  * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
85  * PLL1 rate is calculated as follows
86  * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
87  * parent_rate should always be 24MHz
88  */
89 static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
90                                        u8 *n, u8 *k, u8 *m, u8 *p)
91 {
92         /*
93          * We can operate only on MHz, this will make our life easier
94          * later.
95          */
96         u32 freq_mhz = *freq / 1000000;
97         u32 parent_freq_mhz = parent_rate / 1000000;
98
99         /*
100          * Round down the frequency to the closest multiple of either
101          * 6 or 16
102          */
103         u32 round_freq_6 = round_down(freq_mhz, 6);
104         u32 round_freq_16 = round_down(freq_mhz, 16);
105
106         if (round_freq_6 > round_freq_16)
107                 freq_mhz = round_freq_6;
108         else
109                 freq_mhz = round_freq_16;
110
111         *freq = freq_mhz * 1000000;
112
113         /*
114          * If the factors pointer are null, we were just called to
115          * round down the frequency.
116          * Exit.
117          */
118         if (n == NULL)
119                 return;
120
121         /* If the frequency is a multiple of 32 MHz, k is always 3 */
122         if (!(freq_mhz % 32))
123                 *k = 3;
124         /* If the frequency is a multiple of 9 MHz, k is always 2 */
125         else if (!(freq_mhz % 9))
126                 *k = 2;
127         /* If the frequency is a multiple of 8 MHz, k is always 1 */
128         else if (!(freq_mhz % 8))
129                 *k = 1;
130         /* Otherwise, we don't use the k factor */
131         else
132                 *k = 0;
133
134         /*
135          * If the frequency is a multiple of 2 but not a multiple of
136          * 3, m is 3. This is the first time we use 6 here, yet we
137          * will use it on several other places.
138          * We use this number because it's the lowest frequency we can
139          * generate (with n = 0, k = 0, m = 3), so every other frequency
140          * somehow relates to this frequency.
141          */
142         if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
143                 *m = 2;
144         /*
145          * If the frequency is a multiple of 6MHz, but the factor is
146          * odd, m will be 3
147          */
148         else if ((freq_mhz / 6) & 1)
149                 *m = 3;
150         /* Otherwise, we end up with m = 1 */
151         else
152                 *m = 1;
153
154         /* Calculate n thanks to the above factors we already got */
155         *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
156
157         /*
158          * If n end up being outbound, and that we can still decrease
159          * m, do it.
160          */
161         if ((*n + 1) > 31 && (*m + 1) > 1) {
162                 *n = (*n + 1) / 2 - 1;
163                 *m = (*m + 1) / 2 - 1;
164         }
165 }
166
167 /**
168  * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
169  * PLL1 rate is calculated as follows
170  * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
171  * parent_rate is always 24Mhz
172  */
173
174 static void sun8i_a23_get_pll1_factors(u32 *freq, u32 parent_rate,
175                                    u8 *n, u8 *k, u8 *m, u8 *p)
176 {
177         u8 div;
178
179         /* Normalize value to a 6M multiple */
180         div = *freq / 6000000;
181         *freq = 6000000 * div;
182
183         /* we were called to round the frequency, we can now return */
184         if (n == NULL)
185                 return;
186
187         /* m is always zero for pll1 */
188         *m = 0;
189
190         /* k is 1 only on these cases */
191         if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
192                 *k = 1;
193         else
194                 *k = 0;
195
196         /* p will be 2 for divs under 20 and odd divs under 32 */
197         if (div < 20 || (div < 32 && (div & 1)))
198                 *p = 2;
199
200         /* p will be 1 for even divs under 32, divs under 40 and odd pairs
201          * of divs between 40-62 */
202         else if (div < 40 || (div < 64 && (div & 2)))
203                 *p = 1;
204
205         /* any other entries have p = 0 */
206         else
207                 *p = 0;
208
209         /* calculate a suitable n based on k and p */
210         div <<= *p;
211         div /= (*k + 1);
212         *n = div / 4 - 1;
213 }
214
215 /**
216  * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
217  * PLL5 rate is calculated as follows
218  * rate = parent_rate * n * (k + 1)
219  * parent_rate is always 24Mhz
220  */
221
222 static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate,
223                                    u8 *n, u8 *k, u8 *m, u8 *p)
224 {
225         u8 div;
226
227         /* Normalize value to a parent_rate multiple (24M) */
228         div = *freq / parent_rate;
229         *freq = parent_rate * div;
230
231         /* we were called to round the frequency, we can now return */
232         if (n == NULL)
233                 return;
234
235         if (div < 31)
236                 *k = 0;
237         else if (div / 2 < 31)
238                 *k = 1;
239         else if (div / 3 < 31)
240                 *k = 2;
241         else
242                 *k = 3;
243
244         *n = DIV_ROUND_UP(div, (*k+1));
245 }
246
247 /**
248  * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6
249  * PLL6 rate is calculated as follows
250  * rate = parent_rate * n * (k + 1) / 2
251  * parent_rate is always 24Mhz
252  */
253
254 static void sun6i_a31_get_pll6_factors(u32 *freq, u32 parent_rate,
255                                        u8 *n, u8 *k, u8 *m, u8 *p)
256 {
257         u8 div;
258
259         /*
260          * We always have 24MHz / 2, so we can just say that our
261          * parent clock is 12MHz.
262          */
263         parent_rate = parent_rate / 2;
264
265         /* Normalize value to a parent_rate multiple (24M / 2) */
266         div = *freq / parent_rate;
267         *freq = parent_rate * div;
268
269         /* we were called to round the frequency, we can now return */
270         if (n == NULL)
271                 return;
272
273         *k = div / 32;
274         if (*k > 3)
275                 *k = 3;
276
277         *n = DIV_ROUND_UP(div, (*k+1));
278 }
279
280 /**
281  * sun4i_get_apb1_factors() - calculates m, p factors for APB1
282  * APB1 rate is calculated as follows
283  * rate = (parent_rate >> p) / (m + 1);
284  */
285
286 static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
287                                    u8 *n, u8 *k, u8 *m, u8 *p)
288 {
289         u8 calcm, calcp;
290
291         if (parent_rate < *freq)
292                 *freq = parent_rate;
293
294         parent_rate = DIV_ROUND_UP(parent_rate, *freq);
295
296         /* Invalid rate! */
297         if (parent_rate > 32)
298                 return;
299
300         if (parent_rate <= 4)
301                 calcp = 0;
302         else if (parent_rate <= 8)
303                 calcp = 1;
304         else if (parent_rate <= 16)
305                 calcp = 2;
306         else
307                 calcp = 3;
308
309         calcm = (parent_rate >> calcp) - 1;
310
311         *freq = (parent_rate >> calcp) / (calcm + 1);
312
313         /* we were called to round the frequency, we can now return */
314         if (n == NULL)
315                 return;
316
317         *m = calcm;
318         *p = calcp;
319 }
320
321
322
323 /**
324  * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
325  * MOD0 rate is calculated as follows
326  * rate = (parent_rate >> p) / (m + 1);
327  */
328
329 static void sun4i_get_mod0_factors(u32 *freq, u32 parent_rate,
330                                    u8 *n, u8 *k, u8 *m, u8 *p)
331 {
332         u8 div, calcm, calcp;
333
334         /* These clocks can only divide, so we will never be able to achieve
335          * frequencies higher than the parent frequency */
336         if (*freq > parent_rate)
337                 *freq = parent_rate;
338
339         div = DIV_ROUND_UP(parent_rate, *freq);
340
341         if (div < 16)
342                 calcp = 0;
343         else if (div / 2 < 16)
344                 calcp = 1;
345         else if (div / 4 < 16)
346                 calcp = 2;
347         else
348                 calcp = 3;
349
350         calcm = DIV_ROUND_UP(div, 1 << calcp);
351
352         *freq = (parent_rate >> calcp) / calcm;
353
354         /* we were called to round the frequency, we can now return */
355         if (n == NULL)
356                 return;
357
358         *m = calcm - 1;
359         *p = calcp;
360 }
361
362
363
364 /**
365  * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
366  * CLK_OUT rate is calculated as follows
367  * rate = (parent_rate >> p) / (m + 1);
368  */
369
370 static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
371                                       u8 *n, u8 *k, u8 *m, u8 *p)
372 {
373         u8 div, calcm, calcp;
374
375         /* These clocks can only divide, so we will never be able to achieve
376          * frequencies higher than the parent frequency */
377         if (*freq > parent_rate)
378                 *freq = parent_rate;
379
380         div = DIV_ROUND_UP(parent_rate, *freq);
381
382         if (div < 32)
383                 calcp = 0;
384         else if (div / 2 < 32)
385                 calcp = 1;
386         else if (div / 4 < 32)
387                 calcp = 2;
388         else
389                 calcp = 3;
390
391         calcm = DIV_ROUND_UP(div, 1 << calcp);
392
393         *freq = (parent_rate >> calcp) / calcm;
394
395         /* we were called to round the frequency, we can now return */
396         if (n == NULL)
397                 return;
398
399         *m = calcm - 1;
400         *p = calcp;
401 }
402
403 /**
404  * clk_sunxi_mmc_phase_control() - configures MMC clock phase control
405  */
406
407 void clk_sunxi_mmc_phase_control(struct clk *clk, u8 sample, u8 output)
408 {
409         #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
410         #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
411
412         struct clk_hw *hw = __clk_get_hw(clk);
413         struct clk_composite *composite = to_clk_composite(hw);
414         struct clk_hw *rate_hw = composite->rate_hw;
415         struct clk_factors *factors = to_clk_factors(rate_hw);
416         unsigned long flags = 0;
417         u32 reg;
418
419         if (factors->lock)
420                 spin_lock_irqsave(factors->lock, flags);
421
422         reg = readl(factors->reg);
423
424         /* set sample clock phase control */
425         reg &= ~(0x7 << 20);
426         reg |= ((sample & 0x7) << 20);
427
428         /* set output clock phase control */
429         reg &= ~(0x7 << 8);
430         reg |= ((output & 0x7) << 8);
431
432         writel(reg, factors->reg);
433
434         if (factors->lock)
435                 spin_unlock_irqrestore(factors->lock, flags);
436 }
437 EXPORT_SYMBOL(clk_sunxi_mmc_phase_control);
438
439
440 /**
441  * sunxi_factors_clk_setup() - Setup function for factor clocks
442  */
443
444 static struct clk_factors_config sun4i_pll1_config = {
445         .nshift = 8,
446         .nwidth = 5,
447         .kshift = 4,
448         .kwidth = 2,
449         .mshift = 0,
450         .mwidth = 2,
451         .pshift = 16,
452         .pwidth = 2,
453 };
454
455 static struct clk_factors_config sun6i_a31_pll1_config = {
456         .nshift = 8,
457         .nwidth = 5,
458         .kshift = 4,
459         .kwidth = 2,
460         .mshift = 0,
461         .mwidth = 2,
462 };
463
464 static struct clk_factors_config sun8i_a23_pll1_config = {
465         .nshift = 8,
466         .nwidth = 5,
467         .kshift = 4,
468         .kwidth = 2,
469         .mshift = 0,
470         .mwidth = 2,
471         .pshift = 16,
472         .pwidth = 2,
473         .n_start = 1,
474 };
475
476 static struct clk_factors_config sun4i_pll5_config = {
477         .nshift = 8,
478         .nwidth = 5,
479         .kshift = 4,
480         .kwidth = 2,
481 };
482
483 static struct clk_factors_config sun6i_a31_pll6_config = {
484         .nshift = 8,
485         .nwidth = 5,
486         .kshift = 4,
487         .kwidth = 2,
488 };
489
490 static struct clk_factors_config sun4i_apb1_config = {
491         .mshift = 0,
492         .mwidth = 5,
493         .pshift = 16,
494         .pwidth = 2,
495 };
496
497 /* user manual says "n" but it's really "p" */
498 static struct clk_factors_config sun4i_mod0_config = {
499         .mshift = 0,
500         .mwidth = 4,
501         .pshift = 16,
502         .pwidth = 2,
503 };
504
505 /* user manual says "n" but it's really "p" */
506 static struct clk_factors_config sun7i_a20_out_config = {
507         .mshift = 8,
508         .mwidth = 5,
509         .pshift = 20,
510         .pwidth = 2,
511 };
512
513 static const struct factors_data sun4i_pll1_data __initconst = {
514         .enable = 31,
515         .table = &sun4i_pll1_config,
516         .getter = sun4i_get_pll1_factors,
517 };
518
519 static const struct factors_data sun6i_a31_pll1_data __initconst = {
520         .enable = 31,
521         .table = &sun6i_a31_pll1_config,
522         .getter = sun6i_a31_get_pll1_factors,
523 };
524
525 static const struct factors_data sun8i_a23_pll1_data __initconst = {
526         .enable = 31,
527         .table = &sun8i_a23_pll1_config,
528         .getter = sun8i_a23_get_pll1_factors,
529 };
530
531 static const struct factors_data sun7i_a20_pll4_data __initconst = {
532         .enable = 31,
533         .table = &sun4i_pll5_config,
534         .getter = sun4i_get_pll5_factors,
535 };
536
537 static const struct factors_data sun4i_pll5_data __initconst = {
538         .enable = 31,
539         .table = &sun4i_pll5_config,
540         .getter = sun4i_get_pll5_factors,
541         .name = "pll5",
542 };
543
544 static const struct factors_data sun4i_pll6_data __initconst = {
545         .enable = 31,
546         .table = &sun4i_pll5_config,
547         .getter = sun4i_get_pll5_factors,
548         .name = "pll6",
549 };
550
551 static const struct factors_data sun6i_a31_pll6_data __initconst = {
552         .enable = 31,
553         .table = &sun6i_a31_pll6_config,
554         .getter = sun6i_a31_get_pll6_factors,
555 };
556
557 static const struct factors_data sun4i_apb1_data __initconst = {
558         .table = &sun4i_apb1_config,
559         .getter = sun4i_get_apb1_factors,
560 };
561
562 static const struct factors_data sun4i_mod0_data __initconst = {
563         .enable = 31,
564         .mux = 24,
565         .table = &sun4i_mod0_config,
566         .getter = sun4i_get_mod0_factors,
567 };
568
569 static const struct factors_data sun7i_a20_out_data __initconst = {
570         .enable = 31,
571         .mux = 24,
572         .table = &sun7i_a20_out_config,
573         .getter = sun7i_a20_get_out_factors,
574 };
575
576 static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
577                                                    const struct factors_data *data)
578 {
579         return sunxi_factors_register(node, data, &clk_lock);
580 }
581
582
583
584 /**
585  * sunxi_mux_clk_setup() - Setup function for muxes
586  */
587
588 #define SUNXI_MUX_GATE_WIDTH    2
589
590 struct mux_data {
591         u8 shift;
592 };
593
594 static const struct mux_data sun4i_cpu_mux_data __initconst = {
595         .shift = 16,
596 };
597
598 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
599         .shift = 12,
600 };
601
602 static const struct mux_data sun4i_apb1_mux_data __initconst = {
603         .shift = 24,
604 };
605
606 static void __init sunxi_mux_clk_setup(struct device_node *node,
607                                        struct mux_data *data)
608 {
609         struct clk *clk;
610         const char *clk_name = node->name;
611         const char *parents[SUNXI_MAX_PARENTS];
612         void __iomem *reg;
613         int i = 0;
614
615         reg = of_iomap(node, 0);
616
617         while (i < SUNXI_MAX_PARENTS &&
618                (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
619                 i++;
620
621         of_property_read_string(node, "clock-output-names", &clk_name);
622
623         clk = clk_register_mux(NULL, clk_name, parents, i,
624                                CLK_SET_RATE_NO_REPARENT, reg,
625                                data->shift, SUNXI_MUX_GATE_WIDTH,
626                                0, &clk_lock);
627
628         if (clk) {
629                 of_clk_add_provider(node, of_clk_src_simple_get, clk);
630                 clk_register_clkdev(clk, clk_name, NULL);
631         }
632 }
633
634
635
636 /**
637  * sunxi_divider_clk_setup() - Setup function for simple divider clocks
638  */
639
640 struct div_data {
641         u8      shift;
642         u8      pow;
643         u8      width;
644         const struct clk_div_table *table;
645 };
646
647 static const struct div_data sun4i_axi_data __initconst = {
648         .shift  = 0,
649         .pow    = 0,
650         .width  = 2,
651 };
652
653 static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
654         { .val = 0, .div = 1 },
655         { .val = 1, .div = 2 },
656         { .val = 2, .div = 3 },
657         { .val = 3, .div = 4 },
658         { .val = 4, .div = 4 },
659         { .val = 5, .div = 4 },
660         { .val = 6, .div = 4 },
661         { .val = 7, .div = 4 },
662         { } /* sentinel */
663 };
664
665 static const struct div_data sun8i_a23_axi_data __initconst = {
666         .width  = 3,
667         .table  = sun8i_a23_axi_table,
668 };
669
670 static const struct div_data sun4i_ahb_data __initconst = {
671         .shift  = 4,
672         .pow    = 1,
673         .width  = 2,
674 };
675
676 static const struct clk_div_table sun4i_apb0_table[] __initconst = {
677         { .val = 0, .div = 2 },
678         { .val = 1, .div = 2 },
679         { .val = 2, .div = 4 },
680         { .val = 3, .div = 8 },
681         { } /* sentinel */
682 };
683
684 static const struct div_data sun4i_apb0_data __initconst = {
685         .shift  = 8,
686         .pow    = 1,
687         .width  = 2,
688         .table  = sun4i_apb0_table,
689 };
690
691 static const struct div_data sun6i_a31_apb2_div_data __initconst = {
692         .shift  = 0,
693         .pow    = 0,
694         .width  = 4,
695 };
696
697 static void __init sunxi_divider_clk_setup(struct device_node *node,
698                                            struct div_data *data)
699 {
700         struct clk *clk;
701         const char *clk_name = node->name;
702         const char *clk_parent;
703         void __iomem *reg;
704
705         reg = of_iomap(node, 0);
706
707         clk_parent = of_clk_get_parent_name(node, 0);
708
709         of_property_read_string(node, "clock-output-names", &clk_name);
710
711         clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
712                                          reg, data->shift, data->width,
713                                          data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
714                                          data->table, &clk_lock);
715         if (clk) {
716                 of_clk_add_provider(node, of_clk_src_simple_get, clk);
717                 clk_register_clkdev(clk, clk_name, NULL);
718         }
719 }
720
721
722
723 /**
724  * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
725  */
726
727 struct gates_reset_data {
728         void __iomem                    *reg;
729         spinlock_t                      *lock;
730         struct reset_controller_dev     rcdev;
731 };
732
733 static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev,
734                               unsigned long id)
735 {
736         struct gates_reset_data *data = container_of(rcdev,
737                                                      struct gates_reset_data,
738                                                      rcdev);
739         unsigned long flags;
740         u32 reg;
741
742         spin_lock_irqsave(data->lock, flags);
743
744         reg = readl(data->reg);
745         writel(reg & ~BIT(id), data->reg);
746
747         spin_unlock_irqrestore(data->lock, flags);
748
749         return 0;
750 }
751
752 static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev,
753                                 unsigned long id)
754 {
755         struct gates_reset_data *data = container_of(rcdev,
756                                                      struct gates_reset_data,
757                                                      rcdev);
758         unsigned long flags;
759         u32 reg;
760
761         spin_lock_irqsave(data->lock, flags);
762
763         reg = readl(data->reg);
764         writel(reg | BIT(id), data->reg);
765
766         spin_unlock_irqrestore(data->lock, flags);
767
768         return 0;
769 }
770
771 static struct reset_control_ops sunxi_gates_reset_ops = {
772         .assert         = sunxi_gates_reset_assert,
773         .deassert       = sunxi_gates_reset_deassert,
774 };
775
776 /**
777  * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
778  */
779
780 #define SUNXI_GATES_MAX_SIZE    64
781
782 struct gates_data {
783         DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
784         u32 reset_mask;
785 };
786
787 static const struct gates_data sun4i_axi_gates_data __initconst = {
788         .mask = {1},
789 };
790
791 static const struct gates_data sun4i_ahb_gates_data __initconst = {
792         .mask = {0x7F77FFF, 0x14FB3F},
793 };
794
795 static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
796         .mask = {0x147667e7, 0x185915},
797 };
798
799 static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
800         .mask = {0x107067e7, 0x185111},
801 };
802
803 static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
804         .mask = {0xEDFE7F62, 0x794F931},
805 };
806
807 static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
808         .mask = { 0x12f77fff, 0x16ff3f },
809 };
810
811 static const struct gates_data sun8i_a23_ahb1_gates_data __initconst = {
812         .mask = {0x25386742, 0x2505111},
813 };
814
815 static const struct gates_data sun4i_apb0_gates_data __initconst = {
816         .mask = {0x4EF},
817 };
818
819 static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
820         .mask = {0x469},
821 };
822
823 static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
824         .mask = {0x61},
825 };
826
827 static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
828         .mask = { 0x4ff },
829 };
830
831 static const struct gates_data sun4i_apb1_gates_data __initconst = {
832         .mask = {0xFF00F7},
833 };
834
835 static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
836         .mask = {0xf0007},
837 };
838
839 static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
840         .mask = {0xa0007},
841 };
842
843 static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
844         .mask = {0x3031},
845 };
846
847 static const struct gates_data sun8i_a23_apb1_gates_data __initconst = {
848         .mask = {0x3021},
849 };
850
851 static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
852         .mask = {0x3F000F},
853 };
854
855 static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
856         .mask = { 0xff80ff },
857 };
858
859 static const struct gates_data sun8i_a23_apb2_gates_data __initconst = {
860         .mask = {0x1F0007},
861 };
862
863 static const struct gates_data sun4i_a10_usb_gates_data __initconst = {
864         .mask = {0x1C0},
865         .reset_mask = 0x07,
866 };
867
868 static const struct gates_data sun5i_a13_usb_gates_data __initconst = {
869         .mask = {0x140},
870         .reset_mask = 0x03,
871 };
872
873 static const struct gates_data sun6i_a31_usb_gates_data __initconst = {
874         .mask = { BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8) },
875         .reset_mask = BIT(2) | BIT(1) | BIT(0),
876 };
877
878 static void __init sunxi_gates_clk_setup(struct device_node *node,
879                                          struct gates_data *data)
880 {
881         struct clk_onecell_data *clk_data;
882         struct gates_reset_data *reset_data;
883         const char *clk_parent;
884         const char *clk_name;
885         void __iomem *reg;
886         int qty;
887         int i = 0;
888         int j = 0;
889
890         reg = of_iomap(node, 0);
891
892         clk_parent = of_clk_get_parent_name(node, 0);
893
894         /* Worst-case size approximation and memory allocation */
895         qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
896         clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
897         if (!clk_data)
898                 return;
899         clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
900         if (!clk_data->clks) {
901                 kfree(clk_data);
902                 return;
903         }
904
905         for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
906                 of_property_read_string_index(node, "clock-output-names",
907                                               j, &clk_name);
908
909                 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
910                                                       clk_parent, 0,
911                                                       reg + 4 * (i/32), i % 32,
912                                                       0, &clk_lock);
913                 WARN_ON(IS_ERR(clk_data->clks[i]));
914                 clk_register_clkdev(clk_data->clks[i], clk_name, NULL);
915
916                 j++;
917         }
918
919         /* Adjust to the real max */
920         clk_data->clk_num = i;
921
922         of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
923
924         /* Register a reset controler for gates with reset bits */
925         if (data->reset_mask == 0)
926                 return;
927
928         reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
929         if (!reset_data)
930                 return;
931
932         reset_data->reg = reg;
933         reset_data->lock = &clk_lock;
934         reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
935         reset_data->rcdev.ops = &sunxi_gates_reset_ops;
936         reset_data->rcdev.of_node = node;
937         reset_controller_register(&reset_data->rcdev);
938 }
939
940
941
942 /**
943  * sunxi_divs_clk_setup() helper data
944  */
945
946 #define SUNXI_DIVS_MAX_QTY      2
947 #define SUNXI_DIVISOR_WIDTH     2
948
949 struct divs_data {
950         const struct factors_data *factors; /* data for the factor clock */
951         struct {
952                 u8 fixed; /* is it a fixed divisor? if not... */
953                 struct clk_div_table *table; /* is it a table based divisor? */
954                 u8 shift; /* otherwise it's a normal divisor with this shift */
955                 u8 pow;   /* is it power-of-two based? */
956                 u8 gate;  /* is it independently gateable? */
957         } div[SUNXI_DIVS_MAX_QTY];
958 };
959
960 static struct clk_div_table pll6_sata_tbl[] = {
961         { .val = 0, .div = 6, },
962         { .val = 1, .div = 12, },
963         { .val = 2, .div = 18, },
964         { .val = 3, .div = 24, },
965         { } /* sentinel */
966 };
967
968 static const struct divs_data pll5_divs_data __initconst = {
969         .factors = &sun4i_pll5_data,
970         .div = {
971                 { .shift = 0, .pow = 0, }, /* M, DDR */
972                 { .shift = 16, .pow = 1, }, /* P, other */
973         }
974 };
975
976 static const struct divs_data pll6_divs_data __initconst = {
977         .factors = &sun4i_pll6_data,
978         .div = {
979                 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
980                 { .fixed = 2 }, /* P, other */
981         }
982 };
983
984 /**
985  * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
986  *
987  * These clocks look something like this
988  *            ________________________
989  *           |         ___divisor 1---|----> to consumer
990  * parent >--|  pll___/___divisor 2---|----> to consumer
991  *           |        \_______________|____> to consumer
992  *           |________________________|
993  */
994
995 static void __init sunxi_divs_clk_setup(struct device_node *node,
996                                         struct divs_data *data)
997 {
998         struct clk_onecell_data *clk_data;
999         const char *parent;
1000         const char *clk_name;
1001         struct clk **clks, *pclk;
1002         struct clk_hw *gate_hw, *rate_hw;
1003         const struct clk_ops *rate_ops;
1004         struct clk_gate *gate = NULL;
1005         struct clk_fixed_factor *fix_factor;
1006         struct clk_divider *divider;
1007         void __iomem *reg;
1008         int i = 0;
1009         int flags, clkflags;
1010
1011         /* Set up factor clock that we will be dividing */
1012         pclk = sunxi_factors_clk_setup(node, data->factors);
1013         parent = __clk_get_name(pclk);
1014
1015         reg = of_iomap(node, 0);
1016
1017         clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1018         if (!clk_data)
1019                 return;
1020
1021         clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
1022         if (!clks)
1023                 goto free_clkdata;
1024
1025         clk_data->clks = clks;
1026
1027         /* It's not a good idea to have automatic reparenting changing
1028          * our RAM clock! */
1029         clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
1030
1031         for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
1032                 if (of_property_read_string_index(node, "clock-output-names",
1033                                                   i, &clk_name) != 0)
1034                         break;
1035
1036                 gate_hw = NULL;
1037                 rate_hw = NULL;
1038                 rate_ops = NULL;
1039
1040                 /* If this leaf clock can be gated, create a gate */
1041                 if (data->div[i].gate) {
1042                         gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1043                         if (!gate)
1044                                 goto free_clks;
1045
1046                         gate->reg = reg;
1047                         gate->bit_idx = data->div[i].gate;
1048                         gate->lock = &clk_lock;
1049
1050                         gate_hw = &gate->hw;
1051                 }
1052
1053                 /* Leaves can be fixed or configurable divisors */
1054                 if (data->div[i].fixed) {
1055                         fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1056                         if (!fix_factor)
1057                                 goto free_gate;
1058
1059                         fix_factor->mult = 1;
1060                         fix_factor->div = data->div[i].fixed;
1061
1062                         rate_hw = &fix_factor->hw;
1063                         rate_ops = &clk_fixed_factor_ops;
1064                 } else {
1065                         divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1066                         if (!divider)
1067                                 goto free_gate;
1068
1069                         flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1070
1071                         divider->reg = reg;
1072                         divider->shift = data->div[i].shift;
1073                         divider->width = SUNXI_DIVISOR_WIDTH;
1074                         divider->flags = flags;
1075                         divider->lock = &clk_lock;
1076                         divider->table = data->div[i].table;
1077
1078                         rate_hw = &divider->hw;
1079                         rate_ops = &clk_divider_ops;
1080                 }
1081
1082                 /* Wrap the (potential) gate and the divisor on a composite
1083                  * clock to unify them */
1084                 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1085                                                  NULL, NULL,
1086                                                  rate_hw, rate_ops,
1087                                                  gate_hw, &clk_gate_ops,
1088                                                  clkflags);
1089
1090                 WARN_ON(IS_ERR(clk_data->clks[i]));
1091                 clk_register_clkdev(clks[i], clk_name, NULL);
1092         }
1093
1094         /* The last clock available on the getter is the parent */
1095         clks[i++] = pclk;
1096
1097         /* Adjust to the real max */
1098         clk_data->clk_num = i;
1099
1100         of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
1101
1102         return;
1103
1104 free_gate:
1105         kfree(gate);
1106 free_clks:
1107         kfree(clks);
1108 free_clkdata:
1109         kfree(clk_data);
1110 }
1111
1112
1113
1114 /* Matches for factors clocks */
1115 static const struct of_device_id clk_factors_match[] __initconst = {
1116         {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,},
1117         {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
1118         {.compatible = "allwinner,sun8i-a23-pll1-clk", .data = &sun8i_a23_pll1_data,},
1119         {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,},
1120         {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_data,},
1121         {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,},
1122         {.compatible = "allwinner,sun5i-a13-mbus-clk", .data = &sun4i_mod0_data,},
1123         {.compatible = "allwinner,sun4i-a10-mod0-clk", .data = &sun4i_mod0_data,},
1124         {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
1125         {}
1126 };
1127
1128 /* Matches for divider clocks */
1129 static const struct of_device_id clk_div_match[] __initconst = {
1130         {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,},
1131         {.compatible = "allwinner,sun8i-a23-axi-clk", .data = &sun8i_a23_axi_data,},
1132         {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,},
1133         {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,},
1134         {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
1135         {}
1136 };
1137
1138 /* Matches for divided outputs */
1139 static const struct of_device_id clk_divs_match[] __initconst = {
1140         {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,},
1141         {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,},
1142         {}
1143 };
1144
1145 /* Matches for mux clocks */
1146 static const struct of_device_id clk_mux_match[] __initconst = {
1147         {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
1148         {.compatible = "allwinner,sun4i-a10-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
1149         {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
1150         {}
1151 };
1152
1153 /* Matches for gate clocks */
1154 static const struct of_device_id clk_gates_match[] __initconst = {
1155         {.compatible = "allwinner,sun4i-a10-axi-gates-clk", .data = &sun4i_axi_gates_data,},
1156         {.compatible = "allwinner,sun4i-a10-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
1157         {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
1158         {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
1159         {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
1160         {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
1161         {.compatible = "allwinner,sun8i-a23-ahb1-gates-clk", .data = &sun8i_a23_ahb1_gates_data,},
1162         {.compatible = "allwinner,sun4i-a10-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
1163         {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
1164         {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
1165         {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
1166         {.compatible = "allwinner,sun4i-a10-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
1167         {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
1168         {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
1169         {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
1170         {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
1171         {.compatible = "allwinner,sun8i-a23-apb1-gates-clk", .data = &sun8i_a23_apb1_gates_data,},
1172         {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
1173         {.compatible = "allwinner,sun8i-a23-apb2-gates-clk", .data = &sun8i_a23_apb2_gates_data,},
1174         {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
1175         {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
1176         {.compatible = "allwinner,sun6i-a31-usb-clk", .data = &sun6i_a31_usb_gates_data,},
1177         {}
1178 };
1179
1180 static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
1181                                               void *function)
1182 {
1183         struct device_node *np;
1184         const struct div_data *data;
1185         const struct of_device_id *match;
1186         void (*setup_function)(struct device_node *, const void *) = function;
1187
1188         for_each_matching_node_and_match(np, clk_match, &match) {
1189                 data = match->data;
1190                 setup_function(np, data);
1191         }
1192 }
1193
1194 static void __init sunxi_init_clocks(const char *clocks[], int nclocks)
1195 {
1196         unsigned int i;
1197
1198         /* Register factor clocks */
1199         of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1200
1201         /* Register divider clocks */
1202         of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1203
1204         /* Register divided output clocks */
1205         of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1206
1207         /* Register mux clocks */
1208         of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
1209
1210         /* Register gate clocks */
1211         of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
1212
1213         /* Protect the clocks that needs to stay on */
1214         for (i = 0; i < nclocks; i++) {
1215                 struct clk *clk = clk_get(NULL, clocks[i]);
1216
1217                 if (!IS_ERR(clk))
1218                         clk_prepare_enable(clk);
1219         }
1220 }
1221
1222 static const char *sun4i_a10_critical_clocks[] __initdata = {
1223         "pll5_ddr",
1224         "ahb_sdram",
1225 };
1226
1227 static void __init sun4i_a10_init_clocks(struct device_node *node)
1228 {
1229         sunxi_init_clocks(sun4i_a10_critical_clocks,
1230                           ARRAY_SIZE(sun4i_a10_critical_clocks));
1231 }
1232 CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sun4i_a10_init_clocks);
1233
1234 static const char *sun5i_critical_clocks[] __initdata = {
1235         "mbus",
1236         "pll5_ddr",
1237         "ahb_sdram",
1238 };
1239
1240 static void __init sun5i_init_clocks(struct device_node *node)
1241 {
1242         sunxi_init_clocks(sun5i_critical_clocks,
1243                           ARRAY_SIZE(sun5i_critical_clocks));
1244 }
1245 CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sun5i_init_clocks);
1246 CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sun5i_init_clocks);
1247 CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sun5i_init_clocks);
1248
1249 static const char *sun6i_critical_clocks[] __initdata = {
1250         "cpu",
1251         "ahb1_sdram",
1252 };
1253
1254 static void __init sun6i_init_clocks(struct device_node *node)
1255 {
1256         sunxi_init_clocks(sun6i_critical_clocks,
1257                           ARRAY_SIZE(sun6i_critical_clocks));
1258 }
1259 CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sun6i_init_clocks);
1260 CLK_OF_DECLARE(sun8i_a23_clk_init, "allwinner,sun8i-a23", sun6i_init_clocks);