blob: 393df321010bb03713f7bfa5d1b94f29da12d003 [file] [log] [blame]
Emilio Lópeze874a662013-02-25 11:44:26 -03001/*
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>
Emilio Lópeze874a662013-02-25 11:44:26 -030019#include <linux/of.h>
20#include <linux/of_address.h>
Hans de Goedecfb00862014-02-07 16:21:49 +010021#include <linux/reset-controller.h>
Maxime Ripard601da9d2014-07-04 22:24:52 +020022#include <linux/spinlock.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030023
24#include "clk-factors.h"
25
26static DEFINE_SPINLOCK(clk_lock);
27
Emilio López40a5dcb2013-12-23 00:32:32 -030028/* Maximum number of parents our clocks have */
29#define SUNXI_MAX_PARENTS 5
30
Emilio Lópeze874a662013-02-25 11:44:26 -030031/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020032 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
Emilio Lópeze874a662013-02-25 11:44:26 -030033 * PLL1 rate is calculated as follows
34 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
35 * parent_rate is always 24Mhz
36 */
37
Maxime Ripard81ba6c52013-07-22 18:21:32 +020038static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -030039 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
Maxime Ripard6a721db2013-07-23 23:34:10 +020083/**
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 */
89static 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;
Emilio Lópeze874a662013-02-25 11:44:26 -030098
Maxime Ripard6a721db2013-07-23 23:34:10 +020099 /*
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}
Emilio Lópeze874a662013-02-25 11:44:26 -0300166
167/**
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800168 * 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
174static 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/**
Emilio Lópezd584c132013-12-23 00:32:37 -0300216 * 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
222static 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
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100247/**
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 */
Emilio Lópezd584c132013-12-23 00:32:37 -0300253
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100254static 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}
Emilio Lópezd584c132013-12-23 00:32:37 -0300279
280/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200281 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
Emilio Lópeze874a662013-02-25 11:44:26 -0300282 * APB1 rate is calculated as follows
283 * rate = (parent_rate >> p) / (m + 1);
284 */
285
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200286static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -0300287 u8 *n, u8 *k, u8 *m, u8 *p)
288{
289 u8 calcm, calcp;
290
291 if (parent_rate < *freq)
292 *freq = parent_rate;
293
Emilio López22260132014-03-19 15:19:32 -0300294 parent_rate = DIV_ROUND_UP(parent_rate, *freq);
Emilio Lópeze874a662013-02-25 11:44:26 -0300295
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/**
Emilio López75517692013-12-23 00:32:39 -0300324 * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
Emilio López9ce71ca2014-03-19 15:19:33 -0300325 * MOD0 rate is calculated as follows
Emilio López75517692013-12-23 00:32:39 -0300326 * rate = (parent_rate >> p) / (m + 1);
327 */
328
329static 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
Emilio López22260132014-03-19 15:19:32 -0300339 div = DIV_ROUND_UP(parent_rate, *freq);
Emilio López75517692013-12-23 00:32:39 -0300340
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/**
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800365 * 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
370static 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
Emilio López22260132014-03-19 15:19:32 -0300380 div = DIV_ROUND_UP(parent_rate, *freq);
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800381
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
Chen-Yu Tsaie4c6d6c2014-02-10 18:35:47 +0800403/**
Emilio López95713972014-05-02 17:57:16 +0200404 * clk_sunxi_mmc_phase_control() - configures MMC clock phase control
405 */
406
Hans de Goedea97181a2014-05-12 14:04:47 +0200407void clk_sunxi_mmc_phase_control(struct clk *clk, u8 sample, u8 output)
Emilio López95713972014-05-02 17:57:16 +0200408{
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
Hans de Goedea97181a2014-05-12 14:04:47 +0200412 struct clk_hw *hw = __clk_get_hw(clk);
Emilio López95713972014-05-02 17:57:16 +0200413 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}
437EXPORT_SYMBOL(clk_sunxi_mmc_phase_control);
438
439
440/**
Emilio Lópeze874a662013-02-25 11:44:26 -0300441 * sunxi_factors_clk_setup() - Setup function for factor clocks
442 */
443
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200444static struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300445 .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
Maxime Ripard6a721db2013-07-23 23:34:10 +0200455static 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
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800464static 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
Emilio Lópezd584c132013-12-23 00:32:37 -0300476static struct clk_factors_config sun4i_pll5_config = {
477 .nshift = 8,
478 .nwidth = 5,
479 .kshift = 4,
480 .kwidth = 2,
481};
482
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100483static struct clk_factors_config sun6i_a31_pll6_config = {
484 .nshift = 8,
485 .nwidth = 5,
486 .kshift = 4,
487 .kwidth = 2,
488};
489
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200490static struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300491 .mshift = 0,
492 .mwidth = 5,
493 .pshift = 16,
494 .pwidth = 2,
495};
496
Emilio López75517692013-12-23 00:32:39 -0300497/* user manual says "n" but it's really "p" */
498static struct clk_factors_config sun4i_mod0_config = {
499 .mshift = 0,
500 .mwidth = 4,
501 .pshift = 16,
502 .pwidth = 2,
503};
504
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800505/* user manual says "n" but it's really "p" */
506static struct clk_factors_config sun7i_a20_out_config = {
507 .mshift = 8,
508 .mwidth = 5,
509 .pshift = 20,
510 .pwidth = 2,
511};
512
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530513static const struct factors_data sun4i_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300514 .enable = 31,
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200515 .table = &sun4i_pll1_config,
516 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300517};
518
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530519static const struct factors_data sun6i_a31_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300520 .enable = 31,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200521 .table = &sun6i_a31_pll1_config,
522 .getter = sun6i_a31_get_pll1_factors,
523};
524
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800525static 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
Emilio López5a8ddf22014-03-19 15:19:30 -0300531static 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
Emilio Lópezd584c132013-12-23 00:32:37 -0300537static const struct factors_data sun4i_pll5_data __initconst = {
538 .enable = 31,
539 .table = &sun4i_pll5_config,
540 .getter = sun4i_get_pll5_factors,
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800541 .name = "pll5",
542};
543
544static 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",
Emilio Lópezd584c132013-12-23 00:32:37 -0300549};
550
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100551static 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
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530557static const struct factors_data sun4i_apb1_data __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200558 .table = &sun4i_apb1_config,
559 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300560};
561
Emilio López75517692013-12-23 00:32:39 -0300562static 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
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800569static 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
Emilio López5f4e0be2013-12-23 00:32:36 -0300576static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
Maxime Ripard601da9d2014-07-04 22:24:52 +0200577 const struct factors_data *data)
Emilio Lópeze874a662013-02-25 11:44:26 -0300578{
Maxime Ripard601da9d2014-07-04 22:24:52 +0200579 return sunxi_factors_register(node, data, &clk_lock);
Emilio Lópeze874a662013-02-25 11:44:26 -0300580}
581
582
583
584/**
585 * sunxi_mux_clk_setup() - Setup function for muxes
586 */
587
588#define SUNXI_MUX_GATE_WIDTH 2
589
590struct mux_data {
591 u8 shift;
592};
593
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530594static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300595 .shift = 16,
596};
597
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530598static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200599 .shift = 12,
600};
601
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530602static const struct mux_data sun4i_apb1_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300603 .shift = 24,
604};
605
606static 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;
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300611 const char *parents[SUNXI_MAX_PARENTS];
Emilio López89a94562014-07-28 00:49:42 -0300612 void __iomem *reg;
Emilio Lópeze874a662013-02-25 11:44:26 -0300613 int i = 0;
614
615 reg = of_iomap(node, 0);
616
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300617 while (i < SUNXI_MAX_PARENTS &&
618 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
Emilio Lópeze874a662013-02-25 11:44:26 -0300619 i++;
620
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800621 of_property_read_string(node, "clock-output-names", &clk_name);
622
James Hogan819c1de2013-07-29 12:25:01 +0100623 clk = clk_register_mux(NULL, clk_name, parents, i,
624 CLK_SET_RATE_NO_REPARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300625 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
Emilio Lópeze874a662013-02-25 11:44:26 -0300640struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200641 u8 shift;
642 u8 pow;
643 u8 width;
Chen-Yu Tsaiea5671b2014-06-26 23:55:42 +0800644 const struct clk_div_table *table;
Emilio Lópeze874a662013-02-25 11:44:26 -0300645};
646
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530647static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200648 .shift = 0,
649 .pow = 0,
650 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300651};
652
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800653static 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
665static const struct div_data sun8i_a23_axi_data __initconst = {
666 .width = 3,
667 .table = sun8i_a23_axi_table,
668};
669
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530670static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200671 .shift = 4,
672 .pow = 1,
673 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300674};
675
Chen-Yu Tsaicfe4c932014-09-06 14:45:10 +0800676static 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
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530684static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200685 .shift = 8,
686 .pow = 1,
687 .width = 2,
Chen-Yu Tsaicfe4c932014-09-06 14:45:10 +0800688 .table = sun4i_apb0_table,
Emilio Lópeze874a662013-02-25 11:44:26 -0300689};
690
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530691static const struct div_data sun6i_a31_apb2_div_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200692 .shift = 0,
693 .pow = 0,
694 .width = 4,
695};
696
Emilio Lópeze874a662013-02-25 11:44:26 -0300697static 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;
Emilio López89a94562014-07-28 00:49:42 -0300703 void __iomem *reg;
Emilio Lópeze874a662013-02-25 11:44:26 -0300704
705 reg = of_iomap(node, 0);
706
707 clk_parent = of_clk_get_parent_name(node, 0);
708
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800709 of_property_read_string(node, "clock-output-names", &clk_name);
710
Chen-Yu Tsaiea5671b2014-06-26 23:55:42 +0800711 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);
Emilio Lópeze874a662013-02-25 11:44:26 -0300715 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
Emilio López13569a72013-03-27 18:20:37 -0300722
723/**
Hans de Goedecfb00862014-02-07 16:21:49 +0100724 * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
725 */
726
727struct gates_reset_data {
728 void __iomem *reg;
729 spinlock_t *lock;
730 struct reset_controller_dev rcdev;
731};
732
733static 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
752static 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
771static struct reset_control_ops sunxi_gates_reset_ops = {
772 .assert = sunxi_gates_reset_assert,
773 .deassert = sunxi_gates_reset_deassert,
774};
775
776/**
Emilio López13569a72013-03-27 18:20:37 -0300777 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
778 */
779
780#define SUNXI_GATES_MAX_SIZE 64
781
782struct gates_data {
783 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
Hans de Goedecfb00862014-02-07 16:21:49 +0100784 u32 reset_mask;
Emilio López13569a72013-03-27 18:20:37 -0300785};
786
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530787static const struct gates_data sun4i_axi_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300788 .mask = {1},
789};
790
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530791static const struct gates_data sun4i_ahb_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300792 .mask = {0x7F77FFF, 0x14FB3F},
793};
794
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530795static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200796 .mask = {0x147667e7, 0x185915},
797};
798
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530799static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200800 .mask = {0x107067e7, 0x185111},
801};
802
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530803static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200804 .mask = {0xEDFE7F62, 0x794F931},
805};
806
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530807static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200808 .mask = { 0x12f77fff, 0x16ff3f },
809};
810
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800811static const struct gates_data sun8i_a23_ahb1_gates_data __initconst = {
812 .mask = {0x25386742, 0x2505111},
813};
814
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530815static const struct gates_data sun4i_apb0_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300816 .mask = {0x4EF},
817};
818
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530819static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200820 .mask = {0x469},
821};
822
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530823static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200824 .mask = {0x61},
825};
826
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530827static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200828 .mask = { 0x4ff },
829};
830
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530831static const struct gates_data sun4i_apb1_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300832 .mask = {0xFF00F7},
833};
834
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530835static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200836 .mask = {0xf0007},
837};
838
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530839static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200840 .mask = {0xa0007},
841};
842
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530843static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200844 .mask = {0x3031},
845};
846
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800847static const struct gates_data sun8i_a23_apb1_gates_data __initconst = {
848 .mask = {0x3021},
849};
850
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530851static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200852 .mask = {0x3F000F},
853};
854
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530855static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200856 .mask = { 0xff80ff },
857};
858
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800859static const struct gates_data sun8i_a23_apb2_gates_data __initconst = {
860 .mask = {0x1F0007},
861};
862
Roman Byshko5abdbf22014-02-07 16:21:50 +0100863static const struct gates_data sun4i_a10_usb_gates_data __initconst = {
864 .mask = {0x1C0},
865 .reset_mask = 0x07,
866};
867
868static const struct gates_data sun5i_a13_usb_gates_data __initconst = {
869 .mask = {0x140},
870 .reset_mask = 0x03,
871};
872
Maxime Riparde0e79432014-05-13 17:44:15 +0200873static 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
Emilio López13569a72013-03-27 18:20:37 -0300878static void __init sunxi_gates_clk_setup(struct device_node *node,
879 struct gates_data *data)
880{
881 struct clk_onecell_data *clk_data;
Hans de Goedecfb00862014-02-07 16:21:49 +0100882 struct gates_reset_data *reset_data;
Emilio López13569a72013-03-27 18:20:37 -0300883 const char *clk_parent;
884 const char *clk_name;
Emilio López89a94562014-07-28 00:49:42 -0300885 void __iomem *reg;
Emilio López13569a72013-03-27 18:20:37 -0300886 int qty;
887 int i = 0;
888 int j = 0;
Emilio López13569a72013-03-27 18:20:37 -0300889
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
Emilio López13569a72013-03-27 18:20:37 -0300909 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
Chen-Yu Tsai70eab192014-06-26 23:55:40 +0800910 clk_parent, 0,
Emilio López13569a72013-03-27 18:20:37 -0300911 reg + 4 * (i/32), i % 32,
912 0, &clk_lock);
913 WARN_ON(IS_ERR(clk_data->clks[i]));
Chen-Yu Tsaid14e4702014-06-26 23:55:39 +0800914 clk_register_clkdev(clk_data->clks[i], clk_name, NULL);
Emilio López13569a72013-03-27 18:20:37 -0300915
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);
Hans de Goedecfb00862014-02-07 16:21:49 +0100923
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);
Emilio López13569a72013-03-27 18:20:37 -0300938}
939
Emilio Lópezd584c132013-12-23 00:32:37 -0300940
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
949struct 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
960static 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
968static 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
976static const struct divs_data pll6_divs_data __initconst = {
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800977 .factors = &sun4i_pll6_data,
Emilio Lópezd584c132013-12-23 00:32:37 -0300978 .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
995static void __init sunxi_divs_clk_setup(struct device_node *node,
996 struct divs_data *data)
997{
998 struct clk_onecell_data *clk_data;
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +0800999 const char *parent;
Emilio Lópezd584c132013-12-23 00:32:37 -03001000 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;
Emilio López89a94562014-07-28 00:49:42 -03001007 void __iomem *reg;
Emilio Lópezd584c132013-12-23 00:32:37 -03001008 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);
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +08001013 parent = __clk_get_name(pclk);
Emilio Lópezd584c132013-12-23 00:32:37 -03001014
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
Emilio Lópezd1933682014-01-24 22:32:41 -03001021 clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
Emilio Lópezd584c132013-12-23 00:32:37 -03001022 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
1104free_gate:
1105 kfree(gate);
1106free_clks:
1107 kfree(clks);
1108free_clkdata:
1109 kfree(clk_data);
1110}
1111
1112
1113
Emilio Lópeze874a662013-02-25 11:44:26 -03001114/* Matches for factors clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301115static const struct of_device_id clk_factors_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001116 {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001117 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +08001118 {.compatible = "allwinner,sun8i-a23-pll1-clk", .data = &sun8i_a23_pll1_data,},
Emilio López5a8ddf22014-03-19 15:19:30 -03001119 {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,},
Maxime Ripard92ef67c2014-02-05 14:05:03 +01001120 {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_data,},
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001121 {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,},
1122 {.compatible = "allwinner,sun4i-a10-mod0-clk", .data = &sun4i_mod0_data,},
Chen-Yu Tsai6f863412013-12-24 21:26:17 +08001123 {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001124 {}
1125};
1126
1127/* Matches for divider clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301128static const struct of_device_id clk_div_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001129 {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,},
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +08001130 {.compatible = "allwinner,sun8i-a23-axi-clk", .data = &sun8i_a23_axi_data,},
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001131 {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,},
1132 {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001133 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001134 {}
1135};
1136
Emilio Lópezd584c132013-12-23 00:32:37 -03001137/* Matches for divided outputs */
1138static const struct of_device_id clk_divs_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001139 {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,},
1140 {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,},
Emilio Lópezd584c132013-12-23 00:32:37 -03001141 {}
1142};
1143
Emilio Lópeze874a662013-02-25 11:44:26 -03001144/* Matches for mux clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301145static const struct of_device_id clk_mux_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001146 {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
1147 {.compatible = "allwinner,sun4i-a10-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001148 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001149 {}
1150};
1151
Emilio López13569a72013-03-27 18:20:37 -03001152/* Matches for gate clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301153static const struct of_device_id clk_gates_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001154 {.compatible = "allwinner,sun4i-a10-axi-gates-clk", .data = &sun4i_axi_gates_data,},
1155 {.compatible = "allwinner,sun4i-a10-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001156 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001157 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001158 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001159 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +08001160 {.compatible = "allwinner,sun8i-a23-ahb1-gates-clk", .data = &sun8i_a23_ahb1_gates_data,},
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001161 {.compatible = "allwinner,sun4i-a10-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001162 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001163 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001164 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001165 {.compatible = "allwinner,sun4i-a10-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001166 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001167 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001168 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001169 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +08001170 {.compatible = "allwinner,sun8i-a23-apb1-gates-clk", .data = &sun8i_a23_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001171 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +08001172 {.compatible = "allwinner,sun8i-a23-apb2-gates-clk", .data = &sun8i_a23_apb2_gates_data,},
Roman Byshko5abdbf22014-02-07 16:21:50 +01001173 {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
1174 {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
Maxime Riparde0e79432014-05-13 17:44:15 +02001175 {.compatible = "allwinner,sun6i-a31-usb-clk", .data = &sun6i_a31_usb_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -03001176 {}
1177};
1178
Emilio Lópeze874a662013-02-25 11:44:26 -03001179static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
1180 void *function)
1181{
1182 struct device_node *np;
1183 const struct div_data *data;
1184 const struct of_device_id *match;
1185 void (*setup_function)(struct device_node *, const void *) = function;
1186
Rob Herringcb7d5f42014-05-12 11:24:31 -05001187 for_each_matching_node_and_match(np, clk_match, &match) {
Emilio Lópeze874a662013-02-25 11:44:26 -03001188 data = match->data;
1189 setup_function(np, data);
1190 }
1191}
1192
Maxime Ripard134a6692014-05-09 22:33:39 -05001193static void __init sunxi_init_clocks(const char *clocks[], int nclocks)
Emilio López8e6a4c42013-09-20 22:03:12 -03001194{
Maxime Ripard134a6692014-05-09 22:33:39 -05001195 unsigned int i;
Emilio López8e6a4c42013-09-20 22:03:12 -03001196
Emilio Lópeze874a662013-02-25 11:44:26 -03001197 /* Register factor clocks */
1198 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1199
1200 /* Register divider clocks */
1201 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1202
Emilio Lópezd584c132013-12-23 00:32:37 -03001203 /* Register divided output clocks */
1204 of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1205
Emilio Lópeze874a662013-02-25 11:44:26 -03001206 /* Register mux clocks */
1207 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -03001208
1209 /* Register gate clocks */
1210 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio López8e6a4c42013-09-20 22:03:12 -03001211
Maxime Ripard134a6692014-05-09 22:33:39 -05001212 /* Protect the clocks that needs to stay on */
1213 for (i = 0; i < nclocks; i++) {
1214 struct clk *clk = clk_get(NULL, clocks[i]);
1215
1216 if (!IS_ERR(clk))
1217 clk_prepare_enable(clk);
1218 }
Emilio Lópeze874a662013-02-25 11:44:26 -03001219}
Maxime Ripard134a6692014-05-09 22:33:39 -05001220
1221static const char *sun4i_a10_critical_clocks[] __initdata = {
1222 "pll5_ddr",
Chen-Yu Tsai70eab192014-06-26 23:55:40 +08001223 "ahb_sdram",
Maxime Ripard134a6692014-05-09 22:33:39 -05001224};
1225
1226static void __init sun4i_a10_init_clocks(struct device_node *node)
1227{
1228 sunxi_init_clocks(sun4i_a10_critical_clocks,
1229 ARRAY_SIZE(sun4i_a10_critical_clocks));
1230}
1231CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sun4i_a10_init_clocks);
1232
1233static const char *sun5i_critical_clocks[] __initdata = {
1234 "mbus",
1235 "pll5_ddr",
Chen-Yu Tsai70eab192014-06-26 23:55:40 +08001236 "ahb_sdram",
Maxime Ripard134a6692014-05-09 22:33:39 -05001237};
1238
1239static void __init sun5i_init_clocks(struct device_node *node)
1240{
1241 sunxi_init_clocks(sun5i_critical_clocks,
1242 ARRAY_SIZE(sun5i_critical_clocks));
1243}
1244CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sun5i_init_clocks);
1245CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sun5i_init_clocks);
1246CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sun5i_init_clocks);
1247
1248static const char *sun6i_critical_clocks[] __initdata = {
Maxime Ripard2df73f42014-05-09 22:33:40 -05001249 "cpu",
Maxime Ripardefb31842014-05-09 22:33:41 -05001250 "ahb1_sdram",
Maxime Ripard134a6692014-05-09 22:33:39 -05001251};
1252
1253static void __init sun6i_init_clocks(struct device_node *node)
1254{
1255 sunxi_init_clocks(sun6i_critical_clocks,
1256 ARRAY_SIZE(sun6i_critical_clocks));
1257}
1258CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sun6i_init_clocks);
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +08001259CLK_OF_DECLARE(sun8i_a23_clk_init, "allwinner,sun8i-a23", sun6i_init_clocks);