blob: d4cf297e8fac0e1af9c869962af82e3302ab5042 [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>
Emilio Lópeze874a662013-02-25 11:44:26 -030022
23#include "clk-factors.h"
24
25static DEFINE_SPINLOCK(clk_lock);
26
Emilio López40a5dcb2013-12-23 00:32:32 -030027/* Maximum number of parents our clocks have */
28#define SUNXI_MAX_PARENTS 5
29
Emilio Lópeze874a662013-02-25 11:44:26 -030030/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020031 * sun4i_osc_clk_setup() - Setup function for gatable oscillator
Emilio Lópeze874a662013-02-25 11:44:26 -030032 */
33
34#define SUNXI_OSC24M_GATE 0
35
Maxime Ripard81ba6c52013-07-22 18:21:32 +020036static void __init sun4i_osc_clk_setup(struct device_node *node)
Emilio Lópeze874a662013-02-25 11:44:26 -030037{
38 struct clk *clk;
Emilio López38e4aa02013-04-10 15:02:57 -070039 struct clk_fixed_rate *fixed;
40 struct clk_gate *gate;
Emilio Lópeze874a662013-02-25 11:44:26 -030041 const char *clk_name = node->name;
Emilio López38e4aa02013-04-10 15:02:57 -070042 u32 rate;
Emilio Lópeze874a662013-02-25 11:44:26 -030043
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030044 if (of_property_read_u32(node, "clock-frequency", &rate))
45 return;
46
Emilio López38e4aa02013-04-10 15:02:57 -070047 /* 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);
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030052 if (!gate)
53 goto err_free_fixed;
Emilio Lópeze874a662013-02-25 11:44:26 -030054
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +080055 of_property_read_string(node, "clock-output-names", &clk_name);
56
Emilio López38e4aa02013-04-10 15:02:57 -070057 /* 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);
Emilio Lópeze874a662013-02-25 11:44:26 -030069
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030070 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
78err_free_gate:
79 kfree(gate);
80err_free_fixed:
81 kfree(fixed);
Emilio Lópeze874a662013-02-25 11:44:26 -030082}
Maxime Ripard81ba6c52013-07-22 18:21:32 +020083CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -030084
85
86
87/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020088 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
Emilio Lópeze874a662013-02-25 11:44:26 -030089 * PLL1 rate is calculated as follows
90 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
91 * parent_rate is always 24Mhz
92 */
93
Maxime Ripard81ba6c52013-07-22 18:21:32 +020094static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -030095 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
Maxime Ripard6a721db2013-07-23 23:34:10 +0200139/**
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 */
145static 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;
Emilio Lópeze874a662013-02-25 11:44:26 -0300154
Maxime Ripard6a721db2013-07-23 23:34:10 +0200155 /*
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}
Emilio Lópeze874a662013-02-25 11:44:26 -0300222
223/**
Emilio Lópezd584c132013-12-23 00:32:37 -0300224 * 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
230static 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
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100255/**
256 * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6
257 * PLL6 rate is calculated as follows
258 * rate = parent_rate * n * (k + 1) / 2
259 * parent_rate is always 24Mhz
260 */
Emilio Lópezd584c132013-12-23 00:32:37 -0300261
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100262static void sun6i_a31_get_pll6_factors(u32 *freq, u32 parent_rate,
263 u8 *n, u8 *k, u8 *m, u8 *p)
264{
265 u8 div;
266
267 /*
268 * We always have 24MHz / 2, so we can just say that our
269 * parent clock is 12MHz.
270 */
271 parent_rate = parent_rate / 2;
272
273 /* Normalize value to a parent_rate multiple (24M / 2) */
274 div = *freq / parent_rate;
275 *freq = parent_rate * div;
276
277 /* we were called to round the frequency, we can now return */
278 if (n == NULL)
279 return;
280
281 *k = div / 32;
282 if (*k > 3)
283 *k = 3;
284
285 *n = DIV_ROUND_UP(div, (*k+1));
286}
Emilio Lópezd584c132013-12-23 00:32:37 -0300287
288/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200289 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
Emilio Lópeze874a662013-02-25 11:44:26 -0300290 * APB1 rate is calculated as follows
291 * rate = (parent_rate >> p) / (m + 1);
292 */
293
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200294static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -0300295 u8 *n, u8 *k, u8 *m, u8 *p)
296{
297 u8 calcm, calcp;
298
299 if (parent_rate < *freq)
300 *freq = parent_rate;
301
302 parent_rate = (parent_rate + (*freq - 1)) / *freq;
303
304 /* Invalid rate! */
305 if (parent_rate > 32)
306 return;
307
308 if (parent_rate <= 4)
309 calcp = 0;
310 else if (parent_rate <= 8)
311 calcp = 1;
312 else if (parent_rate <= 16)
313 calcp = 2;
314 else
315 calcp = 3;
316
317 calcm = (parent_rate >> calcp) - 1;
318
319 *freq = (parent_rate >> calcp) / (calcm + 1);
320
321 /* we were called to round the frequency, we can now return */
322 if (n == NULL)
323 return;
324
325 *m = calcm;
326 *p = calcp;
327}
328
329
330
331/**
Emilio López75517692013-12-23 00:32:39 -0300332 * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
333 * MMC rate is calculated as follows
334 * rate = (parent_rate >> p) / (m + 1);
335 */
336
337static void sun4i_get_mod0_factors(u32 *freq, u32 parent_rate,
338 u8 *n, u8 *k, u8 *m, u8 *p)
339{
340 u8 div, calcm, calcp;
341
342 /* These clocks can only divide, so we will never be able to achieve
343 * frequencies higher than the parent frequency */
344 if (*freq > parent_rate)
345 *freq = parent_rate;
346
347 div = parent_rate / *freq;
348
349 if (div < 16)
350 calcp = 0;
351 else if (div / 2 < 16)
352 calcp = 1;
353 else if (div / 4 < 16)
354 calcp = 2;
355 else
356 calcp = 3;
357
358 calcm = DIV_ROUND_UP(div, 1 << calcp);
359
360 *freq = (parent_rate >> calcp) / calcm;
361
362 /* we were called to round the frequency, we can now return */
363 if (n == NULL)
364 return;
365
366 *m = calcm - 1;
367 *p = calcp;
368}
369
370
371
372/**
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800373 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
374 * CLK_OUT rate is calculated as follows
375 * rate = (parent_rate >> p) / (m + 1);
376 */
377
378static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
379 u8 *n, u8 *k, u8 *m, u8 *p)
380{
381 u8 div, calcm, calcp;
382
383 /* These clocks can only divide, so we will never be able to achieve
384 * frequencies higher than the parent frequency */
385 if (*freq > parent_rate)
386 *freq = parent_rate;
387
388 div = parent_rate / *freq;
389
390 if (div < 32)
391 calcp = 0;
392 else if (div / 2 < 32)
393 calcp = 1;
394 else if (div / 4 < 32)
395 calcp = 2;
396 else
397 calcp = 3;
398
399 calcm = DIV_ROUND_UP(div, 1 << calcp);
400
401 *freq = (parent_rate >> calcp) / calcm;
402
403 /* we were called to round the frequency, we can now return */
404 if (n == NULL)
405 return;
406
407 *m = calcm - 1;
408 *p = calcp;
409}
410
411
412
413/**
Emilio Lópeze874a662013-02-25 11:44:26 -0300414 * sunxi_factors_clk_setup() - Setup function for factor clocks
415 */
416
Emilio López40a5dcb2013-12-23 00:32:32 -0300417#define SUNXI_FACTORS_MUX_MASK 0x3
418
Emilio Lópeze874a662013-02-25 11:44:26 -0300419struct factors_data {
Emilio López40a5dcb2013-12-23 00:32:32 -0300420 int enable;
421 int mux;
Emilio Lópeze874a662013-02-25 11:44:26 -0300422 struct clk_factors_config *table;
423 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800424 const char *name;
Emilio Lópeze874a662013-02-25 11:44:26 -0300425};
426
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200427static struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300428 .nshift = 8,
429 .nwidth = 5,
430 .kshift = 4,
431 .kwidth = 2,
432 .mshift = 0,
433 .mwidth = 2,
434 .pshift = 16,
435 .pwidth = 2,
436};
437
Maxime Ripard6a721db2013-07-23 23:34:10 +0200438static struct clk_factors_config sun6i_a31_pll1_config = {
439 .nshift = 8,
440 .nwidth = 5,
441 .kshift = 4,
442 .kwidth = 2,
443 .mshift = 0,
444 .mwidth = 2,
445};
446
Emilio Lópezd584c132013-12-23 00:32:37 -0300447static struct clk_factors_config sun4i_pll5_config = {
448 .nshift = 8,
449 .nwidth = 5,
450 .kshift = 4,
451 .kwidth = 2,
452};
453
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100454static struct clk_factors_config sun6i_a31_pll6_config = {
455 .nshift = 8,
456 .nwidth = 5,
457 .kshift = 4,
458 .kwidth = 2,
459};
460
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200461static struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300462 .mshift = 0,
463 .mwidth = 5,
464 .pshift = 16,
465 .pwidth = 2,
466};
467
Emilio López75517692013-12-23 00:32:39 -0300468/* user manual says "n" but it's really "p" */
469static struct clk_factors_config sun4i_mod0_config = {
470 .mshift = 0,
471 .mwidth = 4,
472 .pshift = 16,
473 .pwidth = 2,
474};
475
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800476/* user manual says "n" but it's really "p" */
477static struct clk_factors_config sun7i_a20_out_config = {
478 .mshift = 8,
479 .mwidth = 5,
480 .pshift = 20,
481 .pwidth = 2,
482};
483
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530484static const struct factors_data sun4i_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300485 .enable = 31,
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200486 .table = &sun4i_pll1_config,
487 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300488};
489
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530490static const struct factors_data sun6i_a31_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300491 .enable = 31,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200492 .table = &sun6i_a31_pll1_config,
493 .getter = sun6i_a31_get_pll1_factors,
494};
495
Emilio Lópezd584c132013-12-23 00:32:37 -0300496static const struct factors_data sun4i_pll5_data __initconst = {
497 .enable = 31,
498 .table = &sun4i_pll5_config,
499 .getter = sun4i_get_pll5_factors,
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800500 .name = "pll5",
501};
502
503static const struct factors_data sun4i_pll6_data __initconst = {
504 .enable = 31,
505 .table = &sun4i_pll5_config,
506 .getter = sun4i_get_pll5_factors,
507 .name = "pll6",
Emilio Lópezd584c132013-12-23 00:32:37 -0300508};
509
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100510static const struct factors_data sun6i_a31_pll6_data __initconst = {
511 .enable = 31,
512 .table = &sun6i_a31_pll6_config,
513 .getter = sun6i_a31_get_pll6_factors,
514};
515
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530516static const struct factors_data sun4i_apb1_data __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200517 .table = &sun4i_apb1_config,
518 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300519};
520
Emilio López75517692013-12-23 00:32:39 -0300521static const struct factors_data sun4i_mod0_data __initconst = {
522 .enable = 31,
523 .mux = 24,
524 .table = &sun4i_mod0_config,
525 .getter = sun4i_get_mod0_factors,
526};
527
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800528static const struct factors_data sun7i_a20_out_data __initconst = {
529 .enable = 31,
530 .mux = 24,
531 .table = &sun7i_a20_out_config,
532 .getter = sun7i_a20_get_out_factors,
533};
534
Emilio López5f4e0be2013-12-23 00:32:36 -0300535static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
536 const struct factors_data *data)
Emilio Lópeze874a662013-02-25 11:44:26 -0300537{
538 struct clk *clk;
Emilio López40a5dcb2013-12-23 00:32:32 -0300539 struct clk_factors *factors;
540 struct clk_gate *gate = NULL;
541 struct clk_mux *mux = NULL;
542 struct clk_hw *gate_hw = NULL;
543 struct clk_hw *mux_hw = NULL;
Emilio Lópeze874a662013-02-25 11:44:26 -0300544 const char *clk_name = node->name;
Emilio López40a5dcb2013-12-23 00:32:32 -0300545 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300546 void *reg;
Emilio López40a5dcb2013-12-23 00:32:32 -0300547 int i = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -0300548
549 reg = of_iomap(node, 0);
550
Emilio López40a5dcb2013-12-23 00:32:32 -0300551 /* if we have a mux, we will have >1 parents */
552 while (i < SUNXI_MAX_PARENTS &&
553 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
554 i++;
Emilio Lópeze874a662013-02-25 11:44:26 -0300555
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800556 /*
557 * some factor clocks, such as pll5 and pll6, may have multiple
558 * outputs, and have their name designated in factors_data
559 */
560 if (data->name)
561 clk_name = data->name;
562 else
563 of_property_read_string(node, "clock-output-names", &clk_name);
Emilio López76192dc2013-12-23 00:32:40 -0300564
Emilio López40a5dcb2013-12-23 00:32:32 -0300565 factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
566 if (!factors)
Emilio López5f4e0be2013-12-23 00:32:36 -0300567 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300568
569 /* Add a gate if this factor clock can be gated */
570 if (data->enable) {
571 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
572 if (!gate) {
573 kfree(factors);
Emilio López5f4e0be2013-12-23 00:32:36 -0300574 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300575 }
576
577 /* set up gate properties */
578 gate->reg = reg;
579 gate->bit_idx = data->enable;
580 gate->lock = &clk_lock;
581 gate_hw = &gate->hw;
582 }
583
584 /* Add a mux if this factor clock can be muxed */
585 if (data->mux) {
586 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
587 if (!mux) {
588 kfree(factors);
589 kfree(gate);
Emilio López5f4e0be2013-12-23 00:32:36 -0300590 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300591 }
592
593 /* set up gate properties */
594 mux->reg = reg;
595 mux->shift = data->mux;
596 mux->mask = SUNXI_FACTORS_MUX_MASK;
597 mux->lock = &clk_lock;
598 mux_hw = &mux->hw;
599 }
600
601 /* set up factors properties */
602 factors->reg = reg;
603 factors->config = data->table;
604 factors->get_factors = data->getter;
605 factors->lock = &clk_lock;
606
607 clk = clk_register_composite(NULL, clk_name,
608 parents, i,
609 mux_hw, &clk_mux_ops,
610 &factors->hw, &clk_factors_ops,
Emilio López5f4e0be2013-12-23 00:32:36 -0300611 gate_hw, &clk_gate_ops, 0);
Emilio Lópeze874a662013-02-25 11:44:26 -0300612
Axel Linee85e9b2013-07-12 16:15:15 +0800613 if (!IS_ERR(clk)) {
Emilio Lópeze874a662013-02-25 11:44:26 -0300614 of_clk_add_provider(node, of_clk_src_simple_get, clk);
615 clk_register_clkdev(clk, clk_name, NULL);
616 }
Emilio López5f4e0be2013-12-23 00:32:36 -0300617
618 return clk;
Emilio Lópeze874a662013-02-25 11:44:26 -0300619}
620
621
622
623/**
624 * sunxi_mux_clk_setup() - Setup function for muxes
625 */
626
627#define SUNXI_MUX_GATE_WIDTH 2
628
629struct mux_data {
630 u8 shift;
631};
632
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530633static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300634 .shift = 16,
635};
636
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530637static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200638 .shift = 12,
639};
640
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530641static const struct mux_data sun4i_apb1_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300642 .shift = 24,
643};
644
645static void __init sunxi_mux_clk_setup(struct device_node *node,
646 struct mux_data *data)
647{
648 struct clk *clk;
649 const char *clk_name = node->name;
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300650 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300651 void *reg;
652 int i = 0;
653
654 reg = of_iomap(node, 0);
655
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300656 while (i < SUNXI_MAX_PARENTS &&
657 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
Emilio Lópeze874a662013-02-25 11:44:26 -0300658 i++;
659
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800660 of_property_read_string(node, "clock-output-names", &clk_name);
661
James Hogan819c1de2013-07-29 12:25:01 +0100662 clk = clk_register_mux(NULL, clk_name, parents, i,
663 CLK_SET_RATE_NO_REPARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300664 data->shift, SUNXI_MUX_GATE_WIDTH,
665 0, &clk_lock);
666
667 if (clk) {
668 of_clk_add_provider(node, of_clk_src_simple_get, clk);
669 clk_register_clkdev(clk, clk_name, NULL);
670 }
671}
672
673
674
675/**
676 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
677 */
678
Emilio Lópeze874a662013-02-25 11:44:26 -0300679struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200680 u8 shift;
681 u8 pow;
682 u8 width;
Emilio Lópeze874a662013-02-25 11:44:26 -0300683};
684
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530685static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200686 .shift = 0,
687 .pow = 0,
688 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300689};
690
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530691static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200692 .shift = 4,
693 .pow = 1,
694 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300695};
696
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530697static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200698 .shift = 8,
699 .pow = 1,
700 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300701};
702
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530703static const struct div_data sun6i_a31_apb2_div_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200704 .shift = 0,
705 .pow = 0,
706 .width = 4,
707};
708
Emilio Lópeze874a662013-02-25 11:44:26 -0300709static void __init sunxi_divider_clk_setup(struct device_node *node,
710 struct div_data *data)
711{
712 struct clk *clk;
713 const char *clk_name = node->name;
714 const char *clk_parent;
715 void *reg;
716
717 reg = of_iomap(node, 0);
718
719 clk_parent = of_clk_get_parent_name(node, 0);
720
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800721 of_property_read_string(node, "clock-output-names", &clk_name);
722
Emilio Lópeze874a662013-02-25 11:44:26 -0300723 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
Maxime Ripard70855bb2013-07-23 09:25:56 +0200724 reg, data->shift, data->width,
Emilio Lópeze874a662013-02-25 11:44:26 -0300725 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
726 &clk_lock);
727 if (clk) {
728 of_clk_add_provider(node, of_clk_src_simple_get, clk);
729 clk_register_clkdev(clk, clk_name, NULL);
730 }
731}
732
733
Emilio López13569a72013-03-27 18:20:37 -0300734
735/**
Hans de Goedecfb00862014-02-07 16:21:49 +0100736 * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
737 */
738
739struct gates_reset_data {
740 void __iomem *reg;
741 spinlock_t *lock;
742 struct reset_controller_dev rcdev;
743};
744
745static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev,
746 unsigned long id)
747{
748 struct gates_reset_data *data = container_of(rcdev,
749 struct gates_reset_data,
750 rcdev);
751 unsigned long flags;
752 u32 reg;
753
754 spin_lock_irqsave(data->lock, flags);
755
756 reg = readl(data->reg);
757 writel(reg & ~BIT(id), data->reg);
758
759 spin_unlock_irqrestore(data->lock, flags);
760
761 return 0;
762}
763
764static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev,
765 unsigned long id)
766{
767 struct gates_reset_data *data = container_of(rcdev,
768 struct gates_reset_data,
769 rcdev);
770 unsigned long flags;
771 u32 reg;
772
773 spin_lock_irqsave(data->lock, flags);
774
775 reg = readl(data->reg);
776 writel(reg | BIT(id), data->reg);
777
778 spin_unlock_irqrestore(data->lock, flags);
779
780 return 0;
781}
782
783static struct reset_control_ops sunxi_gates_reset_ops = {
784 .assert = sunxi_gates_reset_assert,
785 .deassert = sunxi_gates_reset_deassert,
786};
787
788/**
Emilio López13569a72013-03-27 18:20:37 -0300789 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
790 */
791
792#define SUNXI_GATES_MAX_SIZE 64
793
794struct gates_data {
795 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
Hans de Goedecfb00862014-02-07 16:21:49 +0100796 u32 reset_mask;
Emilio López13569a72013-03-27 18:20:37 -0300797};
798
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530799static const struct gates_data sun4i_axi_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300800 .mask = {1},
801};
802
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530803static const struct gates_data sun4i_ahb_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300804 .mask = {0x7F77FFF, 0x14FB3F},
805};
806
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530807static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200808 .mask = {0x147667e7, 0x185915},
809};
810
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530811static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200812 .mask = {0x107067e7, 0x185111},
813};
814
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530815static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200816 .mask = {0xEDFE7F62, 0x794F931},
817};
818
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530819static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200820 .mask = { 0x12f77fff, 0x16ff3f },
821};
822
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530823static const struct gates_data sun4i_apb0_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300824 .mask = {0x4EF},
825};
826
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530827static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200828 .mask = {0x469},
829};
830
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530831static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200832 .mask = {0x61},
833};
834
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530835static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200836 .mask = { 0x4ff },
837};
838
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530839static const struct gates_data sun4i_apb1_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300840 .mask = {0xFF00F7},
841};
842
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530843static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200844 .mask = {0xf0007},
845};
846
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530847static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200848 .mask = {0xa0007},
849};
850
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530851static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200852 .mask = {0x3031},
853};
854
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530855static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200856 .mask = {0x3F000F},
857};
858
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530859static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200860 .mask = { 0xff80ff },
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
Emilio López13569a72013-03-27 18:20:37 -0300873static void __init sunxi_gates_clk_setup(struct device_node *node,
874 struct gates_data *data)
875{
876 struct clk_onecell_data *clk_data;
Hans de Goedecfb00862014-02-07 16:21:49 +0100877 struct gates_reset_data *reset_data;
Emilio López13569a72013-03-27 18:20:37 -0300878 const char *clk_parent;
879 const char *clk_name;
880 void *reg;
881 int qty;
882 int i = 0;
883 int j = 0;
884 int ignore;
885
886 reg = of_iomap(node, 0);
887
888 clk_parent = of_clk_get_parent_name(node, 0);
889
890 /* Worst-case size approximation and memory allocation */
891 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
892 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
893 if (!clk_data)
894 return;
895 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
896 if (!clk_data->clks) {
897 kfree(clk_data);
898 return;
899 }
900
901 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
902 of_property_read_string_index(node, "clock-output-names",
903 j, &clk_name);
904
905 /* No driver claims this clock, but it should remain gated */
906 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
907
908 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
909 clk_parent, ignore,
910 reg + 4 * (i/32), i % 32,
911 0, &clk_lock);
912 WARN_ON(IS_ERR(clk_data->clks[i]));
913
914 j++;
915 }
916
917 /* Adjust to the real max */
918 clk_data->clk_num = i;
919
920 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
Hans de Goedecfb00862014-02-07 16:21:49 +0100921
922 /* Register a reset controler for gates with reset bits */
923 if (data->reset_mask == 0)
924 return;
925
926 reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
927 if (!reset_data)
928 return;
929
930 reset_data->reg = reg;
931 reset_data->lock = &clk_lock;
932 reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
933 reset_data->rcdev.ops = &sunxi_gates_reset_ops;
934 reset_data->rcdev.of_node = node;
935 reset_controller_register(&reset_data->rcdev);
Emilio López13569a72013-03-27 18:20:37 -0300936}
937
Emilio Lópezd584c132013-12-23 00:32:37 -0300938
939
940/**
941 * sunxi_divs_clk_setup() helper data
942 */
943
944#define SUNXI_DIVS_MAX_QTY 2
945#define SUNXI_DIVISOR_WIDTH 2
946
947struct divs_data {
948 const struct factors_data *factors; /* data for the factor clock */
949 struct {
950 u8 fixed; /* is it a fixed divisor? if not... */
951 struct clk_div_table *table; /* is it a table based divisor? */
952 u8 shift; /* otherwise it's a normal divisor with this shift */
953 u8 pow; /* is it power-of-two based? */
954 u8 gate; /* is it independently gateable? */
955 } div[SUNXI_DIVS_MAX_QTY];
956};
957
958static struct clk_div_table pll6_sata_tbl[] = {
959 { .val = 0, .div = 6, },
960 { .val = 1, .div = 12, },
961 { .val = 2, .div = 18, },
962 { .val = 3, .div = 24, },
963 { } /* sentinel */
964};
965
966static const struct divs_data pll5_divs_data __initconst = {
967 .factors = &sun4i_pll5_data,
968 .div = {
969 { .shift = 0, .pow = 0, }, /* M, DDR */
970 { .shift = 16, .pow = 1, }, /* P, other */
971 }
972};
973
974static const struct divs_data pll6_divs_data __initconst = {
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800975 .factors = &sun4i_pll6_data,
Emilio Lópezd584c132013-12-23 00:32:37 -0300976 .div = {
977 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
978 { .fixed = 2 }, /* P, other */
979 }
980};
981
982/**
983 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
984 *
985 * These clocks look something like this
986 * ________________________
987 * | ___divisor 1---|----> to consumer
988 * parent >--| pll___/___divisor 2---|----> to consumer
989 * | \_______________|____> to consumer
990 * |________________________|
991 */
992
993static void __init sunxi_divs_clk_setup(struct device_node *node,
994 struct divs_data *data)
995{
996 struct clk_onecell_data *clk_data;
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +0800997 const char *parent;
Emilio Lópezd584c132013-12-23 00:32:37 -0300998 const char *clk_name;
999 struct clk **clks, *pclk;
1000 struct clk_hw *gate_hw, *rate_hw;
1001 const struct clk_ops *rate_ops;
1002 struct clk_gate *gate = NULL;
1003 struct clk_fixed_factor *fix_factor;
1004 struct clk_divider *divider;
1005 void *reg;
1006 int i = 0;
1007 int flags, clkflags;
1008
1009 /* Set up factor clock that we will be dividing */
1010 pclk = sunxi_factors_clk_setup(node, data->factors);
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +08001011 parent = __clk_get_name(pclk);
Emilio Lópezd584c132013-12-23 00:32:37 -03001012
1013 reg = of_iomap(node, 0);
1014
1015 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1016 if (!clk_data)
1017 return;
1018
Emilio Lópezd1933682014-01-24 22:32:41 -03001019 clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
Emilio Lópezd584c132013-12-23 00:32:37 -03001020 if (!clks)
1021 goto free_clkdata;
1022
1023 clk_data->clks = clks;
1024
1025 /* It's not a good idea to have automatic reparenting changing
1026 * our RAM clock! */
1027 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
1028
1029 for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
1030 if (of_property_read_string_index(node, "clock-output-names",
1031 i, &clk_name) != 0)
1032 break;
1033
1034 gate_hw = NULL;
1035 rate_hw = NULL;
1036 rate_ops = NULL;
1037
1038 /* If this leaf clock can be gated, create a gate */
1039 if (data->div[i].gate) {
1040 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1041 if (!gate)
1042 goto free_clks;
1043
1044 gate->reg = reg;
1045 gate->bit_idx = data->div[i].gate;
1046 gate->lock = &clk_lock;
1047
1048 gate_hw = &gate->hw;
1049 }
1050
1051 /* Leaves can be fixed or configurable divisors */
1052 if (data->div[i].fixed) {
1053 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1054 if (!fix_factor)
1055 goto free_gate;
1056
1057 fix_factor->mult = 1;
1058 fix_factor->div = data->div[i].fixed;
1059
1060 rate_hw = &fix_factor->hw;
1061 rate_ops = &clk_fixed_factor_ops;
1062 } else {
1063 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1064 if (!divider)
1065 goto free_gate;
1066
1067 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1068
1069 divider->reg = reg;
1070 divider->shift = data->div[i].shift;
1071 divider->width = SUNXI_DIVISOR_WIDTH;
1072 divider->flags = flags;
1073 divider->lock = &clk_lock;
1074 divider->table = data->div[i].table;
1075
1076 rate_hw = &divider->hw;
1077 rate_ops = &clk_divider_ops;
1078 }
1079
1080 /* Wrap the (potential) gate and the divisor on a composite
1081 * clock to unify them */
1082 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1083 NULL, NULL,
1084 rate_hw, rate_ops,
1085 gate_hw, &clk_gate_ops,
1086 clkflags);
1087
1088 WARN_ON(IS_ERR(clk_data->clks[i]));
1089 clk_register_clkdev(clks[i], clk_name, NULL);
1090 }
1091
1092 /* The last clock available on the getter is the parent */
1093 clks[i++] = pclk;
1094
1095 /* Adjust to the real max */
1096 clk_data->clk_num = i;
1097
1098 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
1099
1100 return;
1101
1102free_gate:
1103 kfree(gate);
1104free_clks:
1105 kfree(clks);
1106free_clkdata:
1107 kfree(clk_data);
1108}
1109
1110
1111
Emilio Lópeze874a662013-02-25 11:44:26 -03001112/* Matches for factors clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301113static const struct of_device_id clk_factors_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +02001114 {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001115 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
Maxime Ripard92ef67c2014-02-05 14:05:03 +01001116 {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_data,},
Maxime Ripard81ba6c52013-07-22 18:21:32 +02001117 {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
Emilio López75517692013-12-23 00:32:39 -03001118 {.compatible = "allwinner,sun4i-mod0-clk", .data = &sun4i_mod0_data,},
Chen-Yu Tsai6f863412013-12-24 21:26:17 +08001119 {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001120 {}
1121};
1122
1123/* Matches for divider clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301124static const struct of_device_id clk_div_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +02001125 {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
1126 {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
1127 {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001128 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001129 {}
1130};
1131
Emilio Lópezd584c132013-12-23 00:32:37 -03001132/* Matches for divided outputs */
1133static const struct of_device_id clk_divs_match[] __initconst = {
1134 {.compatible = "allwinner,sun4i-pll5-clk", .data = &pll5_divs_data,},
1135 {.compatible = "allwinner,sun4i-pll6-clk", .data = &pll6_divs_data,},
1136 {}
1137};
1138
Emilio Lópeze874a662013-02-25 11:44:26 -03001139/* Matches for mux clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301140static const struct of_device_id clk_mux_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +02001141 {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
1142 {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001143 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001144 {}
1145};
1146
Emilio López13569a72013-03-27 18:20:37 -03001147/* Matches for gate clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301148static const struct of_device_id clk_gates_match[] __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +02001149 {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
1150 {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001151 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001152 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001153 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001154 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001155 {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001156 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001157 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001158 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001159 {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001160 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001161 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001162 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001163 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001164 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
Roman Byshko5abdbf22014-02-07 16:21:50 +01001165 {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
1166 {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -03001167 {}
1168};
1169
Emilio Lópeze874a662013-02-25 11:44:26 -03001170static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
1171 void *function)
1172{
1173 struct device_node *np;
1174 const struct div_data *data;
1175 const struct of_device_id *match;
1176 void (*setup_function)(struct device_node *, const void *) = function;
1177
1178 for_each_matching_node(np, clk_match) {
1179 match = of_match_node(clk_match, np);
1180 data = match->data;
1181 setup_function(np, data);
1182 }
1183}
1184
Emilio López8e6a4c42013-09-20 22:03:12 -03001185/**
1186 * System clock protection
1187 *
1188 * By enabling these critical clocks, we prevent their accidental gating
1189 * by the framework
1190 */
1191static void __init sunxi_clock_protect(void)
1192{
1193 struct clk *clk;
1194
1195 /* memory bus clock - sun5i+ */
1196 clk = clk_get(NULL, "mbus");
1197 if (!IS_ERR(clk)) {
1198 clk_prepare_enable(clk);
1199 clk_put(clk);
1200 }
1201
1202 /* DDR clock - sun4i+ */
1203 clk = clk_get(NULL, "pll5_ddr");
1204 if (!IS_ERR(clk)) {
1205 clk_prepare_enable(clk);
1206 clk_put(clk);
1207 }
1208}
1209
Mike Turquette1d9438f2013-12-01 12:42:45 -08001210static void __init sunxi_init_clocks(void)
Emilio Lópeze874a662013-02-25 11:44:26 -03001211{
Emilio Lópeze874a662013-02-25 11:44:26 -03001212 /* Register factor clocks */
1213 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1214
1215 /* Register divider clocks */
1216 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1217
Emilio Lópezd584c132013-12-23 00:32:37 -03001218 /* Register divided output clocks */
1219 of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1220
Emilio Lópeze874a662013-02-25 11:44:26 -03001221 /* Register mux clocks */
1222 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -03001223
1224 /* Register gate clocks */
1225 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio López8e6a4c42013-09-20 22:03:12 -03001226
1227 /* Enable core system clocks */
1228 sunxi_clock_protect();
Emilio Lópeze874a662013-02-25 11:44:26 -03001229}
Sebastian Hesselbarthbe080452013-09-06 14:59:57 +02001230CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sunxi_init_clocks);
1231CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sunxi_init_clocks);
1232CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sunxi_init_clocks);
1233CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sunxi_init_clocks);
1234CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sunxi_init_clocks);