blob: 6fe9492f84ad19afd4a8d84fd0d734fe66e97da9 [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_get_pll1_factors() - calculates n, k, m, p factors for PLL1
Emilio Lópeze874a662013-02-25 11:44:26 -030032 * PLL1 rate is calculated as follows
33 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
34 * parent_rate is always 24Mhz
35 */
36
Maxime Ripard81ba6c52013-07-22 18:21:32 +020037static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -030038 u8 *n, u8 *k, u8 *m, u8 *p)
39{
40 u8 div;
41
42 /* Normalize value to a 6M multiple */
43 div = *freq / 6000000;
44 *freq = 6000000 * div;
45
46 /* we were called to round the frequency, we can now return */
47 if (n == NULL)
48 return;
49
50 /* m is always zero for pll1 */
51 *m = 0;
52
53 /* k is 1 only on these cases */
54 if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
55 *k = 1;
56 else
57 *k = 0;
58
59 /* p will be 3 for divs under 10 */
60 if (div < 10)
61 *p = 3;
62
63 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
64 else if (div < 20 || (div < 32 && (div & 1)))
65 *p = 2;
66
67 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
68 * of divs between 40-62 */
69 else if (div < 40 || (div < 64 && (div & 2)))
70 *p = 1;
71
72 /* any other entries have p = 0 */
73 else
74 *p = 0;
75
76 /* calculate a suitable n based on k and p */
77 div <<= *p;
78 div /= (*k + 1);
79 *n = div / 4;
80}
81
Maxime Ripard6a721db2013-07-23 23:34:10 +020082/**
83 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
84 * PLL1 rate is calculated as follows
85 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
86 * parent_rate should always be 24MHz
87 */
88static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
89 u8 *n, u8 *k, u8 *m, u8 *p)
90{
91 /*
92 * We can operate only on MHz, this will make our life easier
93 * later.
94 */
95 u32 freq_mhz = *freq / 1000000;
96 u32 parent_freq_mhz = parent_rate / 1000000;
Emilio Lópeze874a662013-02-25 11:44:26 -030097
Maxime Ripard6a721db2013-07-23 23:34:10 +020098 /*
99 * Round down the frequency to the closest multiple of either
100 * 6 or 16
101 */
102 u32 round_freq_6 = round_down(freq_mhz, 6);
103 u32 round_freq_16 = round_down(freq_mhz, 16);
104
105 if (round_freq_6 > round_freq_16)
106 freq_mhz = round_freq_6;
107 else
108 freq_mhz = round_freq_16;
109
110 *freq = freq_mhz * 1000000;
111
112 /*
113 * If the factors pointer are null, we were just called to
114 * round down the frequency.
115 * Exit.
116 */
117 if (n == NULL)
118 return;
119
120 /* If the frequency is a multiple of 32 MHz, k is always 3 */
121 if (!(freq_mhz % 32))
122 *k = 3;
123 /* If the frequency is a multiple of 9 MHz, k is always 2 */
124 else if (!(freq_mhz % 9))
125 *k = 2;
126 /* If the frequency is a multiple of 8 MHz, k is always 1 */
127 else if (!(freq_mhz % 8))
128 *k = 1;
129 /* Otherwise, we don't use the k factor */
130 else
131 *k = 0;
132
133 /*
134 * If the frequency is a multiple of 2 but not a multiple of
135 * 3, m is 3. This is the first time we use 6 here, yet we
136 * will use it on several other places.
137 * We use this number because it's the lowest frequency we can
138 * generate (with n = 0, k = 0, m = 3), so every other frequency
139 * somehow relates to this frequency.
140 */
141 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
142 *m = 2;
143 /*
144 * If the frequency is a multiple of 6MHz, but the factor is
145 * odd, m will be 3
146 */
147 else if ((freq_mhz / 6) & 1)
148 *m = 3;
149 /* Otherwise, we end up with m = 1 */
150 else
151 *m = 1;
152
153 /* Calculate n thanks to the above factors we already got */
154 *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
155
156 /*
157 * If n end up being outbound, and that we can still decrease
158 * m, do it.
159 */
160 if ((*n + 1) > 31 && (*m + 1) > 1) {
161 *n = (*n + 1) / 2 - 1;
162 *m = (*m + 1) / 2 - 1;
163 }
164}
Emilio Lópeze874a662013-02-25 11:44:26 -0300165
166/**
Emilio Lópezd584c132013-12-23 00:32:37 -0300167 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
168 * PLL5 rate is calculated as follows
169 * rate = parent_rate * n * (k + 1)
170 * parent_rate is always 24Mhz
171 */
172
173static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate,
174 u8 *n, u8 *k, u8 *m, u8 *p)
175{
176 u8 div;
177
178 /* Normalize value to a parent_rate multiple (24M) */
179 div = *freq / parent_rate;
180 *freq = parent_rate * div;
181
182 /* we were called to round the frequency, we can now return */
183 if (n == NULL)
184 return;
185
186 if (div < 31)
187 *k = 0;
188 else if (div / 2 < 31)
189 *k = 1;
190 else if (div / 3 < 31)
191 *k = 2;
192 else
193 *k = 3;
194
195 *n = DIV_ROUND_UP(div, (*k+1));
196}
197
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100198/**
199 * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6
200 * PLL6 rate is calculated as follows
201 * rate = parent_rate * n * (k + 1) / 2
202 * parent_rate is always 24Mhz
203 */
Emilio Lópezd584c132013-12-23 00:32:37 -0300204
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100205static void sun6i_a31_get_pll6_factors(u32 *freq, u32 parent_rate,
206 u8 *n, u8 *k, u8 *m, u8 *p)
207{
208 u8 div;
209
210 /*
211 * We always have 24MHz / 2, so we can just say that our
212 * parent clock is 12MHz.
213 */
214 parent_rate = parent_rate / 2;
215
216 /* Normalize value to a parent_rate multiple (24M / 2) */
217 div = *freq / parent_rate;
218 *freq = parent_rate * div;
219
220 /* we were called to round the frequency, we can now return */
221 if (n == NULL)
222 return;
223
224 *k = div / 32;
225 if (*k > 3)
226 *k = 3;
227
228 *n = DIV_ROUND_UP(div, (*k+1));
229}
Emilio Lópezd584c132013-12-23 00:32:37 -0300230
231/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200232 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
Emilio Lópeze874a662013-02-25 11:44:26 -0300233 * APB1 rate is calculated as follows
234 * rate = (parent_rate >> p) / (m + 1);
235 */
236
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200237static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -0300238 u8 *n, u8 *k, u8 *m, u8 *p)
239{
240 u8 calcm, calcp;
241
242 if (parent_rate < *freq)
243 *freq = parent_rate;
244
Emilio López22260132014-03-19 15:19:32 -0300245 parent_rate = DIV_ROUND_UP(parent_rate, *freq);
Emilio Lópeze874a662013-02-25 11:44:26 -0300246
247 /* Invalid rate! */
248 if (parent_rate > 32)
249 return;
250
251 if (parent_rate <= 4)
252 calcp = 0;
253 else if (parent_rate <= 8)
254 calcp = 1;
255 else if (parent_rate <= 16)
256 calcp = 2;
257 else
258 calcp = 3;
259
260 calcm = (parent_rate >> calcp) - 1;
261
262 *freq = (parent_rate >> calcp) / (calcm + 1);
263
264 /* we were called to round the frequency, we can now return */
265 if (n == NULL)
266 return;
267
268 *m = calcm;
269 *p = calcp;
270}
271
272
273
274/**
Emilio López75517692013-12-23 00:32:39 -0300275 * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
Emilio López9ce71ca2014-03-19 15:19:33 -0300276 * MOD0 rate is calculated as follows
Emilio López75517692013-12-23 00:32:39 -0300277 * rate = (parent_rate >> p) / (m + 1);
278 */
279
280static void sun4i_get_mod0_factors(u32 *freq, u32 parent_rate,
281 u8 *n, u8 *k, u8 *m, u8 *p)
282{
283 u8 div, calcm, calcp;
284
285 /* These clocks can only divide, so we will never be able to achieve
286 * frequencies higher than the parent frequency */
287 if (*freq > parent_rate)
288 *freq = parent_rate;
289
Emilio López22260132014-03-19 15:19:32 -0300290 div = DIV_ROUND_UP(parent_rate, *freq);
Emilio López75517692013-12-23 00:32:39 -0300291
292 if (div < 16)
293 calcp = 0;
294 else if (div / 2 < 16)
295 calcp = 1;
296 else if (div / 4 < 16)
297 calcp = 2;
298 else
299 calcp = 3;
300
301 calcm = DIV_ROUND_UP(div, 1 << calcp);
302
303 *freq = (parent_rate >> calcp) / calcm;
304
305 /* we were called to round the frequency, we can now return */
306 if (n == NULL)
307 return;
308
309 *m = calcm - 1;
310 *p = calcp;
311}
312
313
314
315/**
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800316 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
317 * CLK_OUT rate is calculated as follows
318 * rate = (parent_rate >> p) / (m + 1);
319 */
320
321static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
322 u8 *n, u8 *k, u8 *m, u8 *p)
323{
324 u8 div, calcm, calcp;
325
326 /* These clocks can only divide, so we will never be able to achieve
327 * frequencies higher than the parent frequency */
328 if (*freq > parent_rate)
329 *freq = parent_rate;
330
Emilio López22260132014-03-19 15:19:32 -0300331 div = DIV_ROUND_UP(parent_rate, *freq);
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800332
333 if (div < 32)
334 calcp = 0;
335 else if (div / 2 < 32)
336 calcp = 1;
337 else if (div / 4 < 32)
338 calcp = 2;
339 else
340 calcp = 3;
341
342 calcm = DIV_ROUND_UP(div, 1 << calcp);
343
344 *freq = (parent_rate >> calcp) / calcm;
345
346 /* we were called to round the frequency, we can now return */
347 if (n == NULL)
348 return;
349
350 *m = calcm - 1;
351 *p = calcp;
352}
353
Chen-Yu Tsaie4c6d6c2014-02-10 18:35:47 +0800354/**
Emilio López95713972014-05-02 17:57:16 +0200355 * clk_sunxi_mmc_phase_control() - configures MMC clock phase control
356 */
357
Hans de Goedea97181a2014-05-12 14:04:47 +0200358void clk_sunxi_mmc_phase_control(struct clk *clk, u8 sample, u8 output)
Emilio López95713972014-05-02 17:57:16 +0200359{
360 #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
361 #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
362
Hans de Goedea97181a2014-05-12 14:04:47 +0200363 struct clk_hw *hw = __clk_get_hw(clk);
Emilio López95713972014-05-02 17:57:16 +0200364 struct clk_composite *composite = to_clk_composite(hw);
365 struct clk_hw *rate_hw = composite->rate_hw;
366 struct clk_factors *factors = to_clk_factors(rate_hw);
367 unsigned long flags = 0;
368 u32 reg;
369
370 if (factors->lock)
371 spin_lock_irqsave(factors->lock, flags);
372
373 reg = readl(factors->reg);
374
375 /* set sample clock phase control */
376 reg &= ~(0x7 << 20);
377 reg |= ((sample & 0x7) << 20);
378
379 /* set output clock phase control */
380 reg &= ~(0x7 << 8);
381 reg |= ((output & 0x7) << 8);
382
383 writel(reg, factors->reg);
384
385 if (factors->lock)
386 spin_unlock_irqrestore(factors->lock, flags);
387}
388EXPORT_SYMBOL(clk_sunxi_mmc_phase_control);
389
390
391/**
Emilio Lópeze874a662013-02-25 11:44:26 -0300392 * sunxi_factors_clk_setup() - Setup function for factor clocks
393 */
394
Emilio López40a5dcb2013-12-23 00:32:32 -0300395#define SUNXI_FACTORS_MUX_MASK 0x3
396
Emilio Lópeze874a662013-02-25 11:44:26 -0300397struct factors_data {
Emilio López40a5dcb2013-12-23 00:32:32 -0300398 int enable;
399 int mux;
Emilio Lópeze874a662013-02-25 11:44:26 -0300400 struct clk_factors_config *table;
401 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800402 const char *name;
Emilio Lópeze874a662013-02-25 11:44:26 -0300403};
404
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200405static struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300406 .nshift = 8,
407 .nwidth = 5,
408 .kshift = 4,
409 .kwidth = 2,
410 .mshift = 0,
411 .mwidth = 2,
412 .pshift = 16,
413 .pwidth = 2,
414};
415
Maxime Ripard6a721db2013-07-23 23:34:10 +0200416static struct clk_factors_config sun6i_a31_pll1_config = {
417 .nshift = 8,
418 .nwidth = 5,
419 .kshift = 4,
420 .kwidth = 2,
421 .mshift = 0,
422 .mwidth = 2,
423};
424
Emilio Lópezd584c132013-12-23 00:32:37 -0300425static struct clk_factors_config sun4i_pll5_config = {
426 .nshift = 8,
427 .nwidth = 5,
428 .kshift = 4,
429 .kwidth = 2,
430};
431
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100432static struct clk_factors_config sun6i_a31_pll6_config = {
433 .nshift = 8,
434 .nwidth = 5,
435 .kshift = 4,
436 .kwidth = 2,
437};
438
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200439static struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300440 .mshift = 0,
441 .mwidth = 5,
442 .pshift = 16,
443 .pwidth = 2,
444};
445
Emilio López75517692013-12-23 00:32:39 -0300446/* user manual says "n" but it's really "p" */
447static struct clk_factors_config sun4i_mod0_config = {
448 .mshift = 0,
449 .mwidth = 4,
450 .pshift = 16,
451 .pwidth = 2,
452};
453
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800454/* user manual says "n" but it's really "p" */
455static struct clk_factors_config sun7i_a20_out_config = {
456 .mshift = 8,
457 .mwidth = 5,
458 .pshift = 20,
459 .pwidth = 2,
460};
461
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530462static const struct factors_data sun4i_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300463 .enable = 31,
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200464 .table = &sun4i_pll1_config,
465 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300466};
467
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530468static const struct factors_data sun6i_a31_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300469 .enable = 31,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200470 .table = &sun6i_a31_pll1_config,
471 .getter = sun6i_a31_get_pll1_factors,
472};
473
Emilio López5a8ddf22014-03-19 15:19:30 -0300474static const struct factors_data sun7i_a20_pll4_data __initconst = {
475 .enable = 31,
476 .table = &sun4i_pll5_config,
477 .getter = sun4i_get_pll5_factors,
478};
479
Emilio Lópezd584c132013-12-23 00:32:37 -0300480static const struct factors_data sun4i_pll5_data __initconst = {
481 .enable = 31,
482 .table = &sun4i_pll5_config,
483 .getter = sun4i_get_pll5_factors,
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800484 .name = "pll5",
485};
486
487static const struct factors_data sun4i_pll6_data __initconst = {
488 .enable = 31,
489 .table = &sun4i_pll5_config,
490 .getter = sun4i_get_pll5_factors,
491 .name = "pll6",
Emilio Lópezd584c132013-12-23 00:32:37 -0300492};
493
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100494static const struct factors_data sun6i_a31_pll6_data __initconst = {
495 .enable = 31,
496 .table = &sun6i_a31_pll6_config,
497 .getter = sun6i_a31_get_pll6_factors,
498};
499
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530500static const struct factors_data sun4i_apb1_data __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200501 .table = &sun4i_apb1_config,
502 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300503};
504
Emilio López75517692013-12-23 00:32:39 -0300505static const struct factors_data sun4i_mod0_data __initconst = {
506 .enable = 31,
507 .mux = 24,
508 .table = &sun4i_mod0_config,
509 .getter = sun4i_get_mod0_factors,
510};
511
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800512static const struct factors_data sun7i_a20_out_data __initconst = {
513 .enable = 31,
514 .mux = 24,
515 .table = &sun7i_a20_out_config,
516 .getter = sun7i_a20_get_out_factors,
517};
518
Emilio López5f4e0be2013-12-23 00:32:36 -0300519static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
520 const struct factors_data *data)
Emilio Lópeze874a662013-02-25 11:44:26 -0300521{
522 struct clk *clk;
Emilio López40a5dcb2013-12-23 00:32:32 -0300523 struct clk_factors *factors;
524 struct clk_gate *gate = NULL;
525 struct clk_mux *mux = NULL;
526 struct clk_hw *gate_hw = NULL;
527 struct clk_hw *mux_hw = NULL;
Emilio Lópeze874a662013-02-25 11:44:26 -0300528 const char *clk_name = node->name;
Emilio López40a5dcb2013-12-23 00:32:32 -0300529 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300530 void *reg;
Emilio López40a5dcb2013-12-23 00:32:32 -0300531 int i = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -0300532
533 reg = of_iomap(node, 0);
534
Emilio López40a5dcb2013-12-23 00:32:32 -0300535 /* if we have a mux, we will have >1 parents */
536 while (i < SUNXI_MAX_PARENTS &&
537 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
538 i++;
Emilio Lópeze874a662013-02-25 11:44:26 -0300539
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800540 /*
541 * some factor clocks, such as pll5 and pll6, may have multiple
542 * outputs, and have their name designated in factors_data
543 */
544 if (data->name)
545 clk_name = data->name;
546 else
547 of_property_read_string(node, "clock-output-names", &clk_name);
Emilio López76192dc2013-12-23 00:32:40 -0300548
Emilio López40a5dcb2013-12-23 00:32:32 -0300549 factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
550 if (!factors)
Emilio López5f4e0be2013-12-23 00:32:36 -0300551 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300552
553 /* Add a gate if this factor clock can be gated */
554 if (data->enable) {
555 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
556 if (!gate) {
557 kfree(factors);
Emilio López5f4e0be2013-12-23 00:32:36 -0300558 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300559 }
560
561 /* set up gate properties */
562 gate->reg = reg;
563 gate->bit_idx = data->enable;
564 gate->lock = &clk_lock;
565 gate_hw = &gate->hw;
566 }
567
568 /* Add a mux if this factor clock can be muxed */
569 if (data->mux) {
570 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
571 if (!mux) {
572 kfree(factors);
573 kfree(gate);
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 mux->reg = reg;
579 mux->shift = data->mux;
580 mux->mask = SUNXI_FACTORS_MUX_MASK;
581 mux->lock = &clk_lock;
582 mux_hw = &mux->hw;
583 }
584
585 /* set up factors properties */
586 factors->reg = reg;
587 factors->config = data->table;
588 factors->get_factors = data->getter;
589 factors->lock = &clk_lock;
590
591 clk = clk_register_composite(NULL, clk_name,
592 parents, i,
593 mux_hw, &clk_mux_ops,
594 &factors->hw, &clk_factors_ops,
Emilio López5f4e0be2013-12-23 00:32:36 -0300595 gate_hw, &clk_gate_ops, 0);
Emilio Lópeze874a662013-02-25 11:44:26 -0300596
Axel Linee85e9b2013-07-12 16:15:15 +0800597 if (!IS_ERR(clk)) {
Emilio Lópeze874a662013-02-25 11:44:26 -0300598 of_clk_add_provider(node, of_clk_src_simple_get, clk);
599 clk_register_clkdev(clk, clk_name, NULL);
600 }
Emilio López5f4e0be2013-12-23 00:32:36 -0300601
602 return clk;
Emilio Lópeze874a662013-02-25 11:44:26 -0300603}
604
605
606
607/**
608 * sunxi_mux_clk_setup() - Setup function for muxes
609 */
610
611#define SUNXI_MUX_GATE_WIDTH 2
612
613struct mux_data {
614 u8 shift;
615};
616
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530617static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300618 .shift = 16,
619};
620
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530621static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200622 .shift = 12,
623};
624
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530625static const struct mux_data sun4i_apb1_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300626 .shift = 24,
627};
628
629static void __init sunxi_mux_clk_setup(struct device_node *node,
630 struct mux_data *data)
631{
632 struct clk *clk;
633 const char *clk_name = node->name;
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300634 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300635 void *reg;
636 int i = 0;
637
638 reg = of_iomap(node, 0);
639
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300640 while (i < SUNXI_MAX_PARENTS &&
641 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
Emilio Lópeze874a662013-02-25 11:44:26 -0300642 i++;
643
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800644 of_property_read_string(node, "clock-output-names", &clk_name);
645
James Hogan819c1de2013-07-29 12:25:01 +0100646 clk = clk_register_mux(NULL, clk_name, parents, i,
647 CLK_SET_RATE_NO_REPARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300648 data->shift, SUNXI_MUX_GATE_WIDTH,
649 0, &clk_lock);
650
651 if (clk) {
652 of_clk_add_provider(node, of_clk_src_simple_get, clk);
653 clk_register_clkdev(clk, clk_name, NULL);
654 }
655}
656
657
658
659/**
660 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
661 */
662
Emilio Lópeze874a662013-02-25 11:44:26 -0300663struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200664 u8 shift;
665 u8 pow;
666 u8 width;
Chen-Yu Tsaiea5671b2014-06-26 23:55:42 +0800667 const struct clk_div_table *table;
Emilio Lópeze874a662013-02-25 11:44:26 -0300668};
669
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530670static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200671 .shift = 0,
672 .pow = 0,
673 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300674};
675
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530676static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200677 .shift = 4,
678 .pow = 1,
679 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300680};
681
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530682static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200683 .shift = 8,
684 .pow = 1,
685 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300686};
687
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530688static const struct div_data sun6i_a31_apb2_div_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200689 .shift = 0,
690 .pow = 0,
691 .width = 4,
692};
693
Emilio Lópeze874a662013-02-25 11:44:26 -0300694static void __init sunxi_divider_clk_setup(struct device_node *node,
695 struct div_data *data)
696{
697 struct clk *clk;
698 const char *clk_name = node->name;
699 const char *clk_parent;
700 void *reg;
701
702 reg = of_iomap(node, 0);
703
704 clk_parent = of_clk_get_parent_name(node, 0);
705
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800706 of_property_read_string(node, "clock-output-names", &clk_name);
707
Chen-Yu Tsaiea5671b2014-06-26 23:55:42 +0800708 clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
709 reg, data->shift, data->width,
710 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
711 data->table, &clk_lock);
Emilio Lópeze874a662013-02-25 11:44:26 -0300712 if (clk) {
713 of_clk_add_provider(node, of_clk_src_simple_get, clk);
714 clk_register_clkdev(clk, clk_name, NULL);
715 }
716}
717
718
Emilio López13569a72013-03-27 18:20:37 -0300719
720/**
Hans de Goedecfb00862014-02-07 16:21:49 +0100721 * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
722 */
723
724struct gates_reset_data {
725 void __iomem *reg;
726 spinlock_t *lock;
727 struct reset_controller_dev rcdev;
728};
729
730static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev,
731 unsigned long id)
732{
733 struct gates_reset_data *data = container_of(rcdev,
734 struct gates_reset_data,
735 rcdev);
736 unsigned long flags;
737 u32 reg;
738
739 spin_lock_irqsave(data->lock, flags);
740
741 reg = readl(data->reg);
742 writel(reg & ~BIT(id), data->reg);
743
744 spin_unlock_irqrestore(data->lock, flags);
745
746 return 0;
747}
748
749static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev,
750 unsigned long id)
751{
752 struct gates_reset_data *data = container_of(rcdev,
753 struct gates_reset_data,
754 rcdev);
755 unsigned long flags;
756 u32 reg;
757
758 spin_lock_irqsave(data->lock, flags);
759
760 reg = readl(data->reg);
761 writel(reg | BIT(id), data->reg);
762
763 spin_unlock_irqrestore(data->lock, flags);
764
765 return 0;
766}
767
768static struct reset_control_ops sunxi_gates_reset_ops = {
769 .assert = sunxi_gates_reset_assert,
770 .deassert = sunxi_gates_reset_deassert,
771};
772
773/**
Emilio López13569a72013-03-27 18:20:37 -0300774 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
775 */
776
777#define SUNXI_GATES_MAX_SIZE 64
778
779struct gates_data {
780 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
Hans de Goedecfb00862014-02-07 16:21:49 +0100781 u32 reset_mask;
Emilio López13569a72013-03-27 18:20:37 -0300782};
783
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530784static const struct gates_data sun4i_axi_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300785 .mask = {1},
786};
787
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530788static const struct gates_data sun4i_ahb_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300789 .mask = {0x7F77FFF, 0x14FB3F},
790};
791
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530792static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200793 .mask = {0x147667e7, 0x185915},
794};
795
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530796static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200797 .mask = {0x107067e7, 0x185111},
798};
799
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530800static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200801 .mask = {0xEDFE7F62, 0x794F931},
802};
803
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530804static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200805 .mask = { 0x12f77fff, 0x16ff3f },
806};
807
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530808static const struct gates_data sun4i_apb0_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300809 .mask = {0x4EF},
810};
811
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530812static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200813 .mask = {0x469},
814};
815
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530816static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200817 .mask = {0x61},
818};
819
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530820static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200821 .mask = { 0x4ff },
822};
823
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530824static const struct gates_data sun4i_apb1_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300825 .mask = {0xFF00F7},
826};
827
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530828static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200829 .mask = {0xf0007},
830};
831
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530832static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200833 .mask = {0xa0007},
834};
835
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530836static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200837 .mask = {0x3031},
838};
839
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530840static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200841 .mask = {0x3F000F},
842};
843
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530844static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200845 .mask = { 0xff80ff },
846};
847
Roman Byshko5abdbf22014-02-07 16:21:50 +0100848static const struct gates_data sun4i_a10_usb_gates_data __initconst = {
849 .mask = {0x1C0},
850 .reset_mask = 0x07,
851};
852
853static const struct gates_data sun5i_a13_usb_gates_data __initconst = {
854 .mask = {0x140},
855 .reset_mask = 0x03,
856};
857
Maxime Riparde0e79432014-05-13 17:44:15 +0200858static const struct gates_data sun6i_a31_usb_gates_data __initconst = {
859 .mask = { BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8) },
860 .reset_mask = BIT(2) | BIT(1) | BIT(0),
861};
862
Emilio López13569a72013-03-27 18:20:37 -0300863static void __init sunxi_gates_clk_setup(struct device_node *node,
864 struct gates_data *data)
865{
866 struct clk_onecell_data *clk_data;
Hans de Goedecfb00862014-02-07 16:21:49 +0100867 struct gates_reset_data *reset_data;
Emilio López13569a72013-03-27 18:20:37 -0300868 const char *clk_parent;
869 const char *clk_name;
870 void *reg;
871 int qty;
872 int i = 0;
873 int j = 0;
Emilio López13569a72013-03-27 18:20:37 -0300874
875 reg = of_iomap(node, 0);
876
877 clk_parent = of_clk_get_parent_name(node, 0);
878
879 /* Worst-case size approximation and memory allocation */
880 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
881 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
882 if (!clk_data)
883 return;
884 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
885 if (!clk_data->clks) {
886 kfree(clk_data);
887 return;
888 }
889
890 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
891 of_property_read_string_index(node, "clock-output-names",
892 j, &clk_name);
893
Emilio López13569a72013-03-27 18:20:37 -0300894 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
Chen-Yu Tsai70eab192014-06-26 23:55:40 +0800895 clk_parent, 0,
Emilio López13569a72013-03-27 18:20:37 -0300896 reg + 4 * (i/32), i % 32,
897 0, &clk_lock);
898 WARN_ON(IS_ERR(clk_data->clks[i]));
Chen-Yu Tsaid14e4702014-06-26 23:55:39 +0800899 clk_register_clkdev(clk_data->clks[i], clk_name, NULL);
Emilio López13569a72013-03-27 18:20:37 -0300900
901 j++;
902 }
903
904 /* Adjust to the real max */
905 clk_data->clk_num = i;
906
907 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
Hans de Goedecfb00862014-02-07 16:21:49 +0100908
909 /* Register a reset controler for gates with reset bits */
910 if (data->reset_mask == 0)
911 return;
912
913 reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
914 if (!reset_data)
915 return;
916
917 reset_data->reg = reg;
918 reset_data->lock = &clk_lock;
919 reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
920 reset_data->rcdev.ops = &sunxi_gates_reset_ops;
921 reset_data->rcdev.of_node = node;
922 reset_controller_register(&reset_data->rcdev);
Emilio López13569a72013-03-27 18:20:37 -0300923}
924
Emilio Lópezd584c132013-12-23 00:32:37 -0300925
926
927/**
928 * sunxi_divs_clk_setup() helper data
929 */
930
931#define SUNXI_DIVS_MAX_QTY 2
932#define SUNXI_DIVISOR_WIDTH 2
933
934struct divs_data {
935 const struct factors_data *factors; /* data for the factor clock */
936 struct {
937 u8 fixed; /* is it a fixed divisor? if not... */
938 struct clk_div_table *table; /* is it a table based divisor? */
939 u8 shift; /* otherwise it's a normal divisor with this shift */
940 u8 pow; /* is it power-of-two based? */
941 u8 gate; /* is it independently gateable? */
942 } div[SUNXI_DIVS_MAX_QTY];
943};
944
945static struct clk_div_table pll6_sata_tbl[] = {
946 { .val = 0, .div = 6, },
947 { .val = 1, .div = 12, },
948 { .val = 2, .div = 18, },
949 { .val = 3, .div = 24, },
950 { } /* sentinel */
951};
952
953static const struct divs_data pll5_divs_data __initconst = {
954 .factors = &sun4i_pll5_data,
955 .div = {
956 { .shift = 0, .pow = 0, }, /* M, DDR */
957 { .shift = 16, .pow = 1, }, /* P, other */
958 }
959};
960
961static const struct divs_data pll6_divs_data __initconst = {
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800962 .factors = &sun4i_pll6_data,
Emilio Lópezd584c132013-12-23 00:32:37 -0300963 .div = {
964 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
965 { .fixed = 2 }, /* P, other */
966 }
967};
968
969/**
970 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
971 *
972 * These clocks look something like this
973 * ________________________
974 * | ___divisor 1---|----> to consumer
975 * parent >--| pll___/___divisor 2---|----> to consumer
976 * | \_______________|____> to consumer
977 * |________________________|
978 */
979
980static void __init sunxi_divs_clk_setup(struct device_node *node,
981 struct divs_data *data)
982{
983 struct clk_onecell_data *clk_data;
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +0800984 const char *parent;
Emilio Lópezd584c132013-12-23 00:32:37 -0300985 const char *clk_name;
986 struct clk **clks, *pclk;
987 struct clk_hw *gate_hw, *rate_hw;
988 const struct clk_ops *rate_ops;
989 struct clk_gate *gate = NULL;
990 struct clk_fixed_factor *fix_factor;
991 struct clk_divider *divider;
992 void *reg;
993 int i = 0;
994 int flags, clkflags;
995
996 /* Set up factor clock that we will be dividing */
997 pclk = sunxi_factors_clk_setup(node, data->factors);
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +0800998 parent = __clk_get_name(pclk);
Emilio Lópezd584c132013-12-23 00:32:37 -0300999
1000 reg = of_iomap(node, 0);
1001
1002 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1003 if (!clk_data)
1004 return;
1005
Emilio Lópezd1933682014-01-24 22:32:41 -03001006 clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
Emilio Lópezd584c132013-12-23 00:32:37 -03001007 if (!clks)
1008 goto free_clkdata;
1009
1010 clk_data->clks = clks;
1011
1012 /* It's not a good idea to have automatic reparenting changing
1013 * our RAM clock! */
1014 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
1015
1016 for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
1017 if (of_property_read_string_index(node, "clock-output-names",
1018 i, &clk_name) != 0)
1019 break;
1020
1021 gate_hw = NULL;
1022 rate_hw = NULL;
1023 rate_ops = NULL;
1024
1025 /* If this leaf clock can be gated, create a gate */
1026 if (data->div[i].gate) {
1027 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1028 if (!gate)
1029 goto free_clks;
1030
1031 gate->reg = reg;
1032 gate->bit_idx = data->div[i].gate;
1033 gate->lock = &clk_lock;
1034
1035 gate_hw = &gate->hw;
1036 }
1037
1038 /* Leaves can be fixed or configurable divisors */
1039 if (data->div[i].fixed) {
1040 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1041 if (!fix_factor)
1042 goto free_gate;
1043
1044 fix_factor->mult = 1;
1045 fix_factor->div = data->div[i].fixed;
1046
1047 rate_hw = &fix_factor->hw;
1048 rate_ops = &clk_fixed_factor_ops;
1049 } else {
1050 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1051 if (!divider)
1052 goto free_gate;
1053
1054 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1055
1056 divider->reg = reg;
1057 divider->shift = data->div[i].shift;
1058 divider->width = SUNXI_DIVISOR_WIDTH;
1059 divider->flags = flags;
1060 divider->lock = &clk_lock;
1061 divider->table = data->div[i].table;
1062
1063 rate_hw = &divider->hw;
1064 rate_ops = &clk_divider_ops;
1065 }
1066
1067 /* Wrap the (potential) gate and the divisor on a composite
1068 * clock to unify them */
1069 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1070 NULL, NULL,
1071 rate_hw, rate_ops,
1072 gate_hw, &clk_gate_ops,
1073 clkflags);
1074
1075 WARN_ON(IS_ERR(clk_data->clks[i]));
1076 clk_register_clkdev(clks[i], clk_name, NULL);
1077 }
1078
1079 /* The last clock available on the getter is the parent */
1080 clks[i++] = pclk;
1081
1082 /* Adjust to the real max */
1083 clk_data->clk_num = i;
1084
1085 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
1086
1087 return;
1088
1089free_gate:
1090 kfree(gate);
1091free_clks:
1092 kfree(clks);
1093free_clkdata:
1094 kfree(clk_data);
1095}
1096
1097
1098
Emilio Lópeze874a662013-02-25 11:44:26 -03001099/* Matches for factors clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301100static const struct of_device_id clk_factors_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001101 {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001102 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
Emilio López5a8ddf22014-03-19 15:19:30 -03001103 {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,},
Maxime Ripard92ef67c2014-02-05 14:05:03 +01001104 {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_data,},
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001105 {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,},
1106 {.compatible = "allwinner,sun4i-a10-mod0-clk", .data = &sun4i_mod0_data,},
Chen-Yu Tsai6f863412013-12-24 21:26:17 +08001107 {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001108 {}
1109};
1110
1111/* Matches for divider clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301112static const struct of_device_id clk_div_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001113 {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,},
1114 {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,},
1115 {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001116 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001117 {}
1118};
1119
Emilio Lópezd584c132013-12-23 00:32:37 -03001120/* Matches for divided outputs */
1121static const struct of_device_id clk_divs_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001122 {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,},
1123 {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,},
Emilio Lópezd584c132013-12-23 00:32:37 -03001124 {}
1125};
1126
Emilio Lópeze874a662013-02-25 11:44:26 -03001127/* Matches for mux clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301128static const struct of_device_id clk_mux_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001129 {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
1130 {.compatible = "allwinner,sun4i-a10-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001131 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001132 {}
1133};
1134
Emilio López13569a72013-03-27 18:20:37 -03001135/* Matches for gate clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301136static const struct of_device_id clk_gates_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001137 {.compatible = "allwinner,sun4i-a10-axi-gates-clk", .data = &sun4i_axi_gates_data,},
1138 {.compatible = "allwinner,sun4i-a10-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001139 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001140 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001141 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001142 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001143 {.compatible = "allwinner,sun4i-a10-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001144 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001145 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001146 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001147 {.compatible = "allwinner,sun4i-a10-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001148 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001149 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001150 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001151 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001152 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
Roman Byshko5abdbf22014-02-07 16:21:50 +01001153 {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
1154 {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
Maxime Riparde0e79432014-05-13 17:44:15 +02001155 {.compatible = "allwinner,sun6i-a31-usb-clk", .data = &sun6i_a31_usb_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -03001156 {}
1157};
1158
Emilio Lópeze874a662013-02-25 11:44:26 -03001159static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
1160 void *function)
1161{
1162 struct device_node *np;
1163 const struct div_data *data;
1164 const struct of_device_id *match;
1165 void (*setup_function)(struct device_node *, const void *) = function;
1166
Rob Herringcb7d5f42014-05-12 11:24:31 -05001167 for_each_matching_node_and_match(np, clk_match, &match) {
Emilio Lópeze874a662013-02-25 11:44:26 -03001168 data = match->data;
1169 setup_function(np, data);
1170 }
1171}
1172
Maxime Ripard134a6692014-05-09 22:33:39 -05001173static void __init sunxi_init_clocks(const char *clocks[], int nclocks)
Emilio López8e6a4c42013-09-20 22:03:12 -03001174{
Maxime Ripard134a6692014-05-09 22:33:39 -05001175 unsigned int i;
Emilio López8e6a4c42013-09-20 22:03:12 -03001176
Emilio Lópeze874a662013-02-25 11:44:26 -03001177 /* Register factor clocks */
1178 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1179
1180 /* Register divider clocks */
1181 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1182
Emilio Lópezd584c132013-12-23 00:32:37 -03001183 /* Register divided output clocks */
1184 of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1185
Emilio Lópeze874a662013-02-25 11:44:26 -03001186 /* Register mux clocks */
1187 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -03001188
1189 /* Register gate clocks */
1190 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio López8e6a4c42013-09-20 22:03:12 -03001191
Maxime Ripard134a6692014-05-09 22:33:39 -05001192 /* Protect the clocks that needs to stay on */
1193 for (i = 0; i < nclocks; i++) {
1194 struct clk *clk = clk_get(NULL, clocks[i]);
1195
1196 if (!IS_ERR(clk))
1197 clk_prepare_enable(clk);
1198 }
Emilio Lópeze874a662013-02-25 11:44:26 -03001199}
Maxime Ripard134a6692014-05-09 22:33:39 -05001200
1201static const char *sun4i_a10_critical_clocks[] __initdata = {
1202 "pll5_ddr",
Chen-Yu Tsai70eab192014-06-26 23:55:40 +08001203 "ahb_sdram",
Maxime Ripard134a6692014-05-09 22:33:39 -05001204};
1205
1206static void __init sun4i_a10_init_clocks(struct device_node *node)
1207{
1208 sunxi_init_clocks(sun4i_a10_critical_clocks,
1209 ARRAY_SIZE(sun4i_a10_critical_clocks));
1210}
1211CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sun4i_a10_init_clocks);
1212
1213static const char *sun5i_critical_clocks[] __initdata = {
1214 "mbus",
1215 "pll5_ddr",
Chen-Yu Tsai70eab192014-06-26 23:55:40 +08001216 "ahb_sdram",
Maxime Ripard134a6692014-05-09 22:33:39 -05001217};
1218
1219static void __init sun5i_init_clocks(struct device_node *node)
1220{
1221 sunxi_init_clocks(sun5i_critical_clocks,
1222 ARRAY_SIZE(sun5i_critical_clocks));
1223}
1224CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sun5i_init_clocks);
1225CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sun5i_init_clocks);
1226CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sun5i_init_clocks);
1227
1228static const char *sun6i_critical_clocks[] __initdata = {
Maxime Ripard2df73f42014-05-09 22:33:40 -05001229 "cpu",
Maxime Ripardefb31842014-05-09 22:33:41 -05001230 "ahb1_sdram",
Maxime Ripard134a6692014-05-09 22:33:39 -05001231};
1232
1233static void __init sun6i_init_clocks(struct device_node *node)
1234{
1235 sunxi_init_clocks(sun6i_critical_clocks,
1236 ARRAY_SIZE(sun6i_critical_clocks));
1237}
1238CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sun6i_init_clocks);