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