blob: 59f90401b9000ac9e4b6e9070efd7e0a45a75c94 [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 Ripardfd1b22f2014-02-06 09:55:57 +010083CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-a10-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
Emilio López22260132014-03-19 15:19:32 -0300302 parent_rate = DIV_ROUND_UP(parent_rate, *freq);
Emilio Lópeze874a662013-02-25 11:44:26 -0300303
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
Emilio López9ce71ca2014-03-19 15:19:33 -0300333 * MOD0 rate is calculated as follows
Emilio López75517692013-12-23 00:32:39 -0300334 * 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
Emilio López22260132014-03-19 15:19:32 -0300347 div = DIV_ROUND_UP(parent_rate, *freq);
Emilio López75517692013-12-23 00:32:39 -0300348
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
Emilio López22260132014-03-19 15:19:32 -0300388 div = DIV_ROUND_UP(parent_rate, *freq);
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800389
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/**
Chen-Yu Tsaie4c6d6c2014-02-10 18:35:47 +0800414 * sun7i_a20_gmac_clk_setup - Setup function for A20/A31 GMAC clock module
415 *
416 * This clock looks something like this
417 * ________________________
418 * MII TX clock from PHY >-----|___________ _________|----> to GMAC core
419 * GMAC Int. RGMII TX clk >----|___________\__/__gate---|----> to PHY
420 * Ext. 125MHz RGMII TX clk >--|__divider__/ |
421 * |________________________|
422 *
423 * The external 125 MHz reference is optional, i.e. GMAC can use its
424 * internal TX clock just fine. The A31 GMAC clock module does not have
425 * the divider controls for the external reference.
426 *
427 * To keep it simple, let the GMAC use either the MII TX clock for MII mode,
428 * and its internal TX clock for GMII and RGMII modes. The GMAC driver should
429 * select the appropriate source and gate/ungate the output to the PHY.
430 *
431 * Only the GMAC should use this clock. Altering the clock so that it doesn't
432 * match the GMAC's operation parameters will result in the GMAC not being
433 * able to send traffic out. The GMAC driver should set the clock rate and
434 * enable/disable this clock to configure the required state. The clock
435 * driver then responds by auto-reparenting the clock.
436 */
437
438#define SUN7I_A20_GMAC_GPIT 2
439#define SUN7I_A20_GMAC_MASK 0x3
440#define SUN7I_A20_GMAC_PARENTS 2
441
442static void __init sun7i_a20_gmac_clk_setup(struct device_node *node)
443{
444 struct clk *clk;
445 struct clk_mux *mux;
446 struct clk_gate *gate;
447 const char *clk_name = node->name;
448 const char *parents[SUN7I_A20_GMAC_PARENTS];
449 void *reg;
450
451 if (of_property_read_string(node, "clock-output-names", &clk_name))
452 return;
453
454 /* allocate mux and gate clock structs */
455 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
456 if (!mux)
457 return;
458
459 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
460 if (!gate)
461 goto free_mux;
462
463 /* gmac clock requires exactly 2 parents */
464 parents[0] = of_clk_get_parent_name(node, 0);
465 parents[1] = of_clk_get_parent_name(node, 1);
466 if (!parents[0] || !parents[1])
467 goto free_gate;
468
469 reg = of_iomap(node, 0);
470 if (!reg)
471 goto free_gate;
472
473 /* set up gate and fixed rate properties */
474 gate->reg = reg;
475 gate->bit_idx = SUN7I_A20_GMAC_GPIT;
476 gate->lock = &clk_lock;
477 mux->reg = reg;
478 mux->mask = SUN7I_A20_GMAC_MASK;
479 mux->flags = CLK_MUX_INDEX_BIT;
480 mux->lock = &clk_lock;
481
482 clk = clk_register_composite(NULL, clk_name,
483 parents, SUN7I_A20_GMAC_PARENTS,
484 &mux->hw, &clk_mux_ops,
485 NULL, NULL,
486 &gate->hw, &clk_gate_ops,
487 0);
488
489 if (IS_ERR(clk))
490 goto iounmap_reg;
491
492 of_clk_add_provider(node, of_clk_src_simple_get, clk);
493 clk_register_clkdev(clk, clk_name, NULL);
494
495 return;
496
497iounmap_reg:
498 iounmap(reg);
499free_gate:
500 kfree(gate);
501free_mux:
502 kfree(mux);
503}
504CLK_OF_DECLARE(sun7i_a20_gmac, "allwinner,sun7i-a20-gmac-clk",
505 sun7i_a20_gmac_clk_setup);
506
507
508
509/**
Emilio López95713972014-05-02 17:57:16 +0200510 * clk_sunxi_mmc_phase_control() - configures MMC clock phase control
511 */
512
513void clk_sunxi_mmc_phase_control(struct clk_hw *hw, u8 sample, u8 output)
514{
515 #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
516 #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
517
518 struct clk_composite *composite = to_clk_composite(hw);
519 struct clk_hw *rate_hw = composite->rate_hw;
520 struct clk_factors *factors = to_clk_factors(rate_hw);
521 unsigned long flags = 0;
522 u32 reg;
523
524 if (factors->lock)
525 spin_lock_irqsave(factors->lock, flags);
526
527 reg = readl(factors->reg);
528
529 /* set sample clock phase control */
530 reg &= ~(0x7 << 20);
531 reg |= ((sample & 0x7) << 20);
532
533 /* set output clock phase control */
534 reg &= ~(0x7 << 8);
535 reg |= ((output & 0x7) << 8);
536
537 writel(reg, factors->reg);
538
539 if (factors->lock)
540 spin_unlock_irqrestore(factors->lock, flags);
541}
542EXPORT_SYMBOL(clk_sunxi_mmc_phase_control);
543
544
545/**
Emilio Lópeze874a662013-02-25 11:44:26 -0300546 * sunxi_factors_clk_setup() - Setup function for factor clocks
547 */
548
Emilio López40a5dcb2013-12-23 00:32:32 -0300549#define SUNXI_FACTORS_MUX_MASK 0x3
550
Emilio Lópeze874a662013-02-25 11:44:26 -0300551struct factors_data {
Emilio López40a5dcb2013-12-23 00:32:32 -0300552 int enable;
553 int mux;
Emilio Lópeze874a662013-02-25 11:44:26 -0300554 struct clk_factors_config *table;
555 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800556 const char *name;
Emilio Lópeze874a662013-02-25 11:44:26 -0300557};
558
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200559static struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300560 .nshift = 8,
561 .nwidth = 5,
562 .kshift = 4,
563 .kwidth = 2,
564 .mshift = 0,
565 .mwidth = 2,
566 .pshift = 16,
567 .pwidth = 2,
568};
569
Maxime Ripard6a721db2013-07-23 23:34:10 +0200570static struct clk_factors_config sun6i_a31_pll1_config = {
571 .nshift = 8,
572 .nwidth = 5,
573 .kshift = 4,
574 .kwidth = 2,
575 .mshift = 0,
576 .mwidth = 2,
577};
578
Emilio Lópezd584c132013-12-23 00:32:37 -0300579static struct clk_factors_config sun4i_pll5_config = {
580 .nshift = 8,
581 .nwidth = 5,
582 .kshift = 4,
583 .kwidth = 2,
584};
585
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100586static struct clk_factors_config sun6i_a31_pll6_config = {
587 .nshift = 8,
588 .nwidth = 5,
589 .kshift = 4,
590 .kwidth = 2,
591};
592
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200593static struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300594 .mshift = 0,
595 .mwidth = 5,
596 .pshift = 16,
597 .pwidth = 2,
598};
599
Emilio López75517692013-12-23 00:32:39 -0300600/* user manual says "n" but it's really "p" */
601static struct clk_factors_config sun4i_mod0_config = {
602 .mshift = 0,
603 .mwidth = 4,
604 .pshift = 16,
605 .pwidth = 2,
606};
607
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800608/* user manual says "n" but it's really "p" */
609static struct clk_factors_config sun7i_a20_out_config = {
610 .mshift = 8,
611 .mwidth = 5,
612 .pshift = 20,
613 .pwidth = 2,
614};
615
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530616static const struct factors_data sun4i_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300617 .enable = 31,
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200618 .table = &sun4i_pll1_config,
619 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300620};
621
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530622static const struct factors_data sun6i_a31_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300623 .enable = 31,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200624 .table = &sun6i_a31_pll1_config,
625 .getter = sun6i_a31_get_pll1_factors,
626};
627
Emilio López5a8ddf22014-03-19 15:19:30 -0300628static const struct factors_data sun7i_a20_pll4_data __initconst = {
629 .enable = 31,
630 .table = &sun4i_pll5_config,
631 .getter = sun4i_get_pll5_factors,
632};
633
Emilio Lópezd584c132013-12-23 00:32:37 -0300634static const struct factors_data sun4i_pll5_data __initconst = {
635 .enable = 31,
636 .table = &sun4i_pll5_config,
637 .getter = sun4i_get_pll5_factors,
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800638 .name = "pll5",
639};
640
641static const struct factors_data sun4i_pll6_data __initconst = {
642 .enable = 31,
643 .table = &sun4i_pll5_config,
644 .getter = sun4i_get_pll5_factors,
645 .name = "pll6",
Emilio Lópezd584c132013-12-23 00:32:37 -0300646};
647
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100648static const struct factors_data sun6i_a31_pll6_data __initconst = {
649 .enable = 31,
650 .table = &sun6i_a31_pll6_config,
651 .getter = sun6i_a31_get_pll6_factors,
652};
653
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530654static const struct factors_data sun4i_apb1_data __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200655 .table = &sun4i_apb1_config,
656 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300657};
658
Emilio López75517692013-12-23 00:32:39 -0300659static const struct factors_data sun4i_mod0_data __initconst = {
660 .enable = 31,
661 .mux = 24,
662 .table = &sun4i_mod0_config,
663 .getter = sun4i_get_mod0_factors,
664};
665
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800666static const struct factors_data sun7i_a20_out_data __initconst = {
667 .enable = 31,
668 .mux = 24,
669 .table = &sun7i_a20_out_config,
670 .getter = sun7i_a20_get_out_factors,
671};
672
Emilio López5f4e0be2013-12-23 00:32:36 -0300673static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
674 const struct factors_data *data)
Emilio Lópeze874a662013-02-25 11:44:26 -0300675{
676 struct clk *clk;
Emilio López40a5dcb2013-12-23 00:32:32 -0300677 struct clk_factors *factors;
678 struct clk_gate *gate = NULL;
679 struct clk_mux *mux = NULL;
680 struct clk_hw *gate_hw = NULL;
681 struct clk_hw *mux_hw = NULL;
Emilio Lópeze874a662013-02-25 11:44:26 -0300682 const char *clk_name = node->name;
Emilio López40a5dcb2013-12-23 00:32:32 -0300683 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300684 void *reg;
Emilio López40a5dcb2013-12-23 00:32:32 -0300685 int i = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -0300686
687 reg = of_iomap(node, 0);
688
Emilio López40a5dcb2013-12-23 00:32:32 -0300689 /* if we have a mux, we will have >1 parents */
690 while (i < SUNXI_MAX_PARENTS &&
691 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
692 i++;
Emilio Lópeze874a662013-02-25 11:44:26 -0300693
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800694 /*
695 * some factor clocks, such as pll5 and pll6, may have multiple
696 * outputs, and have their name designated in factors_data
697 */
698 if (data->name)
699 clk_name = data->name;
700 else
701 of_property_read_string(node, "clock-output-names", &clk_name);
Emilio López76192dc2013-12-23 00:32:40 -0300702
Emilio López40a5dcb2013-12-23 00:32:32 -0300703 factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
704 if (!factors)
Emilio López5f4e0be2013-12-23 00:32:36 -0300705 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300706
707 /* Add a gate if this factor clock can be gated */
708 if (data->enable) {
709 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
710 if (!gate) {
711 kfree(factors);
Emilio López5f4e0be2013-12-23 00:32:36 -0300712 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300713 }
714
715 /* set up gate properties */
716 gate->reg = reg;
717 gate->bit_idx = data->enable;
718 gate->lock = &clk_lock;
719 gate_hw = &gate->hw;
720 }
721
722 /* Add a mux if this factor clock can be muxed */
723 if (data->mux) {
724 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
725 if (!mux) {
726 kfree(factors);
727 kfree(gate);
Emilio López5f4e0be2013-12-23 00:32:36 -0300728 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300729 }
730
731 /* set up gate properties */
732 mux->reg = reg;
733 mux->shift = data->mux;
734 mux->mask = SUNXI_FACTORS_MUX_MASK;
735 mux->lock = &clk_lock;
736 mux_hw = &mux->hw;
737 }
738
739 /* set up factors properties */
740 factors->reg = reg;
741 factors->config = data->table;
742 factors->get_factors = data->getter;
743 factors->lock = &clk_lock;
744
745 clk = clk_register_composite(NULL, clk_name,
746 parents, i,
747 mux_hw, &clk_mux_ops,
748 &factors->hw, &clk_factors_ops,
Emilio López5f4e0be2013-12-23 00:32:36 -0300749 gate_hw, &clk_gate_ops, 0);
Emilio Lópeze874a662013-02-25 11:44:26 -0300750
Axel Linee85e9b2013-07-12 16:15:15 +0800751 if (!IS_ERR(clk)) {
Emilio Lópeze874a662013-02-25 11:44:26 -0300752 of_clk_add_provider(node, of_clk_src_simple_get, clk);
753 clk_register_clkdev(clk, clk_name, NULL);
754 }
Emilio López5f4e0be2013-12-23 00:32:36 -0300755
756 return clk;
Emilio Lópeze874a662013-02-25 11:44:26 -0300757}
758
759
760
761/**
762 * sunxi_mux_clk_setup() - Setup function for muxes
763 */
764
765#define SUNXI_MUX_GATE_WIDTH 2
766
767struct mux_data {
768 u8 shift;
769};
770
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530771static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300772 .shift = 16,
773};
774
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530775static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200776 .shift = 12,
777};
778
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530779static const struct mux_data sun4i_apb1_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300780 .shift = 24,
781};
782
783static void __init sunxi_mux_clk_setup(struct device_node *node,
784 struct mux_data *data)
785{
786 struct clk *clk;
787 const char *clk_name = node->name;
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300788 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300789 void *reg;
790 int i = 0;
791
792 reg = of_iomap(node, 0);
793
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300794 while (i < SUNXI_MAX_PARENTS &&
795 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
Emilio Lópeze874a662013-02-25 11:44:26 -0300796 i++;
797
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800798 of_property_read_string(node, "clock-output-names", &clk_name);
799
James Hogan819c1de2013-07-29 12:25:01 +0100800 clk = clk_register_mux(NULL, clk_name, parents, i,
801 CLK_SET_RATE_NO_REPARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300802 data->shift, SUNXI_MUX_GATE_WIDTH,
803 0, &clk_lock);
804
805 if (clk) {
806 of_clk_add_provider(node, of_clk_src_simple_get, clk);
807 clk_register_clkdev(clk, clk_name, NULL);
808 }
809}
810
811
812
813/**
814 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
815 */
816
Emilio Lópeze874a662013-02-25 11:44:26 -0300817struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200818 u8 shift;
819 u8 pow;
820 u8 width;
Emilio Lópeze874a662013-02-25 11:44:26 -0300821};
822
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530823static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200824 .shift = 0,
825 .pow = 0,
826 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300827};
828
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530829static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200830 .shift = 4,
831 .pow = 1,
832 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300833};
834
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530835static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200836 .shift = 8,
837 .pow = 1,
838 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300839};
840
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530841static const struct div_data sun6i_a31_apb2_div_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200842 .shift = 0,
843 .pow = 0,
844 .width = 4,
845};
846
Emilio Lópeze874a662013-02-25 11:44:26 -0300847static void __init sunxi_divider_clk_setup(struct device_node *node,
848 struct div_data *data)
849{
850 struct clk *clk;
851 const char *clk_name = node->name;
852 const char *clk_parent;
853 void *reg;
854
855 reg = of_iomap(node, 0);
856
857 clk_parent = of_clk_get_parent_name(node, 0);
858
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800859 of_property_read_string(node, "clock-output-names", &clk_name);
860
Emilio Lópeze874a662013-02-25 11:44:26 -0300861 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
Maxime Ripard70855bb2013-07-23 09:25:56 +0200862 reg, data->shift, data->width,
Emilio Lópeze874a662013-02-25 11:44:26 -0300863 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
864 &clk_lock);
865 if (clk) {
866 of_clk_add_provider(node, of_clk_src_simple_get, clk);
867 clk_register_clkdev(clk, clk_name, NULL);
868 }
869}
870
871
Emilio López13569a72013-03-27 18:20:37 -0300872
873/**
Hans de Goedecfb00862014-02-07 16:21:49 +0100874 * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
875 */
876
877struct gates_reset_data {
878 void __iomem *reg;
879 spinlock_t *lock;
880 struct reset_controller_dev rcdev;
881};
882
883static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev,
884 unsigned long id)
885{
886 struct gates_reset_data *data = container_of(rcdev,
887 struct gates_reset_data,
888 rcdev);
889 unsigned long flags;
890 u32 reg;
891
892 spin_lock_irqsave(data->lock, flags);
893
894 reg = readl(data->reg);
895 writel(reg & ~BIT(id), data->reg);
896
897 spin_unlock_irqrestore(data->lock, flags);
898
899 return 0;
900}
901
902static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev,
903 unsigned long id)
904{
905 struct gates_reset_data *data = container_of(rcdev,
906 struct gates_reset_data,
907 rcdev);
908 unsigned long flags;
909 u32 reg;
910
911 spin_lock_irqsave(data->lock, flags);
912
913 reg = readl(data->reg);
914 writel(reg | BIT(id), data->reg);
915
916 spin_unlock_irqrestore(data->lock, flags);
917
918 return 0;
919}
920
921static struct reset_control_ops sunxi_gates_reset_ops = {
922 .assert = sunxi_gates_reset_assert,
923 .deassert = sunxi_gates_reset_deassert,
924};
925
926/**
Emilio López13569a72013-03-27 18:20:37 -0300927 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
928 */
929
930#define SUNXI_GATES_MAX_SIZE 64
931
932struct gates_data {
933 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
Hans de Goedecfb00862014-02-07 16:21:49 +0100934 u32 reset_mask;
Emilio López13569a72013-03-27 18:20:37 -0300935};
936
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530937static const struct gates_data sun4i_axi_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300938 .mask = {1},
939};
940
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530941static const struct gates_data sun4i_ahb_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300942 .mask = {0x7F77FFF, 0x14FB3F},
943};
944
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530945static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200946 .mask = {0x147667e7, 0x185915},
947};
948
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530949static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200950 .mask = {0x107067e7, 0x185111},
951};
952
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530953static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200954 .mask = {0xEDFE7F62, 0x794F931},
955};
956
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530957static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200958 .mask = { 0x12f77fff, 0x16ff3f },
959};
960
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530961static const struct gates_data sun4i_apb0_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300962 .mask = {0x4EF},
963};
964
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530965static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200966 .mask = {0x469},
967};
968
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530969static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200970 .mask = {0x61},
971};
972
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530973static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200974 .mask = { 0x4ff },
975};
976
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530977static const struct gates_data sun4i_apb1_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300978 .mask = {0xFF00F7},
979};
980
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530981static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200982 .mask = {0xf0007},
983};
984
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530985static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200986 .mask = {0xa0007},
987};
988
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530989static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200990 .mask = {0x3031},
991};
992
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530993static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200994 .mask = {0x3F000F},
995};
996
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530997static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200998 .mask = { 0xff80ff },
999};
1000
Roman Byshko5abdbf22014-02-07 16:21:50 +01001001static const struct gates_data sun4i_a10_usb_gates_data __initconst = {
1002 .mask = {0x1C0},
1003 .reset_mask = 0x07,
1004};
1005
1006static const struct gates_data sun5i_a13_usb_gates_data __initconst = {
1007 .mask = {0x140},
1008 .reset_mask = 0x03,
1009};
1010
Emilio López13569a72013-03-27 18:20:37 -03001011static void __init sunxi_gates_clk_setup(struct device_node *node,
1012 struct gates_data *data)
1013{
1014 struct clk_onecell_data *clk_data;
Hans de Goedecfb00862014-02-07 16:21:49 +01001015 struct gates_reset_data *reset_data;
Emilio López13569a72013-03-27 18:20:37 -03001016 const char *clk_parent;
1017 const char *clk_name;
1018 void *reg;
1019 int qty;
1020 int i = 0;
1021 int j = 0;
1022 int ignore;
1023
1024 reg = of_iomap(node, 0);
1025
1026 clk_parent = of_clk_get_parent_name(node, 0);
1027
1028 /* Worst-case size approximation and memory allocation */
1029 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
1030 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1031 if (!clk_data)
1032 return;
1033 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
1034 if (!clk_data->clks) {
1035 kfree(clk_data);
1036 return;
1037 }
1038
1039 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
1040 of_property_read_string_index(node, "clock-output-names",
1041 j, &clk_name);
1042
1043 /* No driver claims this clock, but it should remain gated */
1044 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
1045
1046 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
1047 clk_parent, ignore,
1048 reg + 4 * (i/32), i % 32,
1049 0, &clk_lock);
1050 WARN_ON(IS_ERR(clk_data->clks[i]));
1051
1052 j++;
1053 }
1054
1055 /* Adjust to the real max */
1056 clk_data->clk_num = i;
1057
1058 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
Hans de Goedecfb00862014-02-07 16:21:49 +01001059
1060 /* Register a reset controler for gates with reset bits */
1061 if (data->reset_mask == 0)
1062 return;
1063
1064 reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
1065 if (!reset_data)
1066 return;
1067
1068 reset_data->reg = reg;
1069 reset_data->lock = &clk_lock;
1070 reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
1071 reset_data->rcdev.ops = &sunxi_gates_reset_ops;
1072 reset_data->rcdev.of_node = node;
1073 reset_controller_register(&reset_data->rcdev);
Emilio López13569a72013-03-27 18:20:37 -03001074}
1075
Emilio Lópezd584c132013-12-23 00:32:37 -03001076
1077
1078/**
1079 * sunxi_divs_clk_setup() helper data
1080 */
1081
1082#define SUNXI_DIVS_MAX_QTY 2
1083#define SUNXI_DIVISOR_WIDTH 2
1084
1085struct divs_data {
1086 const struct factors_data *factors; /* data for the factor clock */
1087 struct {
1088 u8 fixed; /* is it a fixed divisor? if not... */
1089 struct clk_div_table *table; /* is it a table based divisor? */
1090 u8 shift; /* otherwise it's a normal divisor with this shift */
1091 u8 pow; /* is it power-of-two based? */
1092 u8 gate; /* is it independently gateable? */
1093 } div[SUNXI_DIVS_MAX_QTY];
1094};
1095
1096static struct clk_div_table pll6_sata_tbl[] = {
1097 { .val = 0, .div = 6, },
1098 { .val = 1, .div = 12, },
1099 { .val = 2, .div = 18, },
1100 { .val = 3, .div = 24, },
1101 { } /* sentinel */
1102};
1103
1104static const struct divs_data pll5_divs_data __initconst = {
1105 .factors = &sun4i_pll5_data,
1106 .div = {
1107 { .shift = 0, .pow = 0, }, /* M, DDR */
1108 { .shift = 16, .pow = 1, }, /* P, other */
1109 }
1110};
1111
1112static const struct divs_data pll6_divs_data __initconst = {
Chen-Yu Tsai667f5422014-02-03 09:51:39 +08001113 .factors = &sun4i_pll6_data,
Emilio Lópezd584c132013-12-23 00:32:37 -03001114 .div = {
1115 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
1116 { .fixed = 2 }, /* P, other */
1117 }
1118};
1119
1120/**
1121 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
1122 *
1123 * These clocks look something like this
1124 * ________________________
1125 * | ___divisor 1---|----> to consumer
1126 * parent >--| pll___/___divisor 2---|----> to consumer
1127 * | \_______________|____> to consumer
1128 * |________________________|
1129 */
1130
1131static void __init sunxi_divs_clk_setup(struct device_node *node,
1132 struct divs_data *data)
1133{
1134 struct clk_onecell_data *clk_data;
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +08001135 const char *parent;
Emilio Lópezd584c132013-12-23 00:32:37 -03001136 const char *clk_name;
1137 struct clk **clks, *pclk;
1138 struct clk_hw *gate_hw, *rate_hw;
1139 const struct clk_ops *rate_ops;
1140 struct clk_gate *gate = NULL;
1141 struct clk_fixed_factor *fix_factor;
1142 struct clk_divider *divider;
1143 void *reg;
1144 int i = 0;
1145 int flags, clkflags;
1146
1147 /* Set up factor clock that we will be dividing */
1148 pclk = sunxi_factors_clk_setup(node, data->factors);
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +08001149 parent = __clk_get_name(pclk);
Emilio Lópezd584c132013-12-23 00:32:37 -03001150
1151 reg = of_iomap(node, 0);
1152
1153 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1154 if (!clk_data)
1155 return;
1156
Emilio Lópezd1933682014-01-24 22:32:41 -03001157 clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
Emilio Lópezd584c132013-12-23 00:32:37 -03001158 if (!clks)
1159 goto free_clkdata;
1160
1161 clk_data->clks = clks;
1162
1163 /* It's not a good idea to have automatic reparenting changing
1164 * our RAM clock! */
1165 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
1166
1167 for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
1168 if (of_property_read_string_index(node, "clock-output-names",
1169 i, &clk_name) != 0)
1170 break;
1171
1172 gate_hw = NULL;
1173 rate_hw = NULL;
1174 rate_ops = NULL;
1175
1176 /* If this leaf clock can be gated, create a gate */
1177 if (data->div[i].gate) {
1178 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1179 if (!gate)
1180 goto free_clks;
1181
1182 gate->reg = reg;
1183 gate->bit_idx = data->div[i].gate;
1184 gate->lock = &clk_lock;
1185
1186 gate_hw = &gate->hw;
1187 }
1188
1189 /* Leaves can be fixed or configurable divisors */
1190 if (data->div[i].fixed) {
1191 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1192 if (!fix_factor)
1193 goto free_gate;
1194
1195 fix_factor->mult = 1;
1196 fix_factor->div = data->div[i].fixed;
1197
1198 rate_hw = &fix_factor->hw;
1199 rate_ops = &clk_fixed_factor_ops;
1200 } else {
1201 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1202 if (!divider)
1203 goto free_gate;
1204
1205 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1206
1207 divider->reg = reg;
1208 divider->shift = data->div[i].shift;
1209 divider->width = SUNXI_DIVISOR_WIDTH;
1210 divider->flags = flags;
1211 divider->lock = &clk_lock;
1212 divider->table = data->div[i].table;
1213
1214 rate_hw = &divider->hw;
1215 rate_ops = &clk_divider_ops;
1216 }
1217
1218 /* Wrap the (potential) gate and the divisor on a composite
1219 * clock to unify them */
1220 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1221 NULL, NULL,
1222 rate_hw, rate_ops,
1223 gate_hw, &clk_gate_ops,
1224 clkflags);
1225
1226 WARN_ON(IS_ERR(clk_data->clks[i]));
1227 clk_register_clkdev(clks[i], clk_name, NULL);
1228 }
1229
1230 /* The last clock available on the getter is the parent */
1231 clks[i++] = pclk;
1232
1233 /* Adjust to the real max */
1234 clk_data->clk_num = i;
1235
1236 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
1237
1238 return;
1239
1240free_gate:
1241 kfree(gate);
1242free_clks:
1243 kfree(clks);
1244free_clkdata:
1245 kfree(clk_data);
1246}
1247
1248
1249
Emilio Lópeze874a662013-02-25 11:44:26 -03001250/* Matches for factors clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301251static const struct of_device_id clk_factors_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001252 {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001253 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
Emilio López5a8ddf22014-03-19 15:19:30 -03001254 {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,},
Maxime Ripard92ef67c2014-02-05 14:05:03 +01001255 {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_data,},
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001256 {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,},
1257 {.compatible = "allwinner,sun4i-a10-mod0-clk", .data = &sun4i_mod0_data,},
Chen-Yu Tsai6f863412013-12-24 21:26:17 +08001258 {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001259 {}
1260};
1261
1262/* Matches for divider clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301263static const struct of_device_id clk_div_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001264 {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,},
1265 {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,},
1266 {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001267 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001268 {}
1269};
1270
Emilio Lópezd584c132013-12-23 00:32:37 -03001271/* Matches for divided outputs */
1272static const struct of_device_id clk_divs_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001273 {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,},
1274 {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,},
Emilio Lópezd584c132013-12-23 00:32:37 -03001275 {}
1276};
1277
Emilio Lópeze874a662013-02-25 11:44:26 -03001278/* Matches for mux clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301279static const struct of_device_id clk_mux_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001280 {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
1281 {.compatible = "allwinner,sun4i-a10-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001282 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001283 {}
1284};
1285
Emilio López13569a72013-03-27 18:20:37 -03001286/* Matches for gate clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301287static const struct of_device_id clk_gates_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001288 {.compatible = "allwinner,sun4i-a10-axi-gates-clk", .data = &sun4i_axi_gates_data,},
1289 {.compatible = "allwinner,sun4i-a10-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001290 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001291 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001292 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001293 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001294 {.compatible = "allwinner,sun4i-a10-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001295 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001296 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001297 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
Maxime Ripardfd1b22f2014-02-06 09:55:57 +01001298 {.compatible = "allwinner,sun4i-a10-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001299 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001300 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001301 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001302 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001303 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
Roman Byshko5abdbf22014-02-07 16:21:50 +01001304 {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
1305 {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -03001306 {}
1307};
1308
Emilio Lópeze874a662013-02-25 11:44:26 -03001309static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
1310 void *function)
1311{
1312 struct device_node *np;
1313 const struct div_data *data;
1314 const struct of_device_id *match;
1315 void (*setup_function)(struct device_node *, const void *) = function;
1316
1317 for_each_matching_node(np, clk_match) {
1318 match = of_match_node(clk_match, np);
1319 data = match->data;
1320 setup_function(np, data);
1321 }
1322}
1323
Emilio López8e6a4c42013-09-20 22:03:12 -03001324/**
1325 * System clock protection
1326 *
1327 * By enabling these critical clocks, we prevent their accidental gating
1328 * by the framework
1329 */
1330static void __init sunxi_clock_protect(void)
1331{
1332 struct clk *clk;
1333
1334 /* memory bus clock - sun5i+ */
1335 clk = clk_get(NULL, "mbus");
1336 if (!IS_ERR(clk)) {
1337 clk_prepare_enable(clk);
1338 clk_put(clk);
1339 }
1340
1341 /* DDR clock - sun4i+ */
1342 clk = clk_get(NULL, "pll5_ddr");
1343 if (!IS_ERR(clk)) {
1344 clk_prepare_enable(clk);
1345 clk_put(clk);
1346 }
1347}
1348
Mike Turquette1d9438f2013-12-01 12:42:45 -08001349static void __init sunxi_init_clocks(void)
Emilio Lópeze874a662013-02-25 11:44:26 -03001350{
Emilio Lópeze874a662013-02-25 11:44:26 -03001351 /* Register factor clocks */
1352 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1353
1354 /* Register divider clocks */
1355 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1356
Emilio Lópezd584c132013-12-23 00:32:37 -03001357 /* Register divided output clocks */
1358 of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1359
Emilio Lópeze874a662013-02-25 11:44:26 -03001360 /* Register mux clocks */
1361 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -03001362
1363 /* Register gate clocks */
1364 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio López8e6a4c42013-09-20 22:03:12 -03001365
1366 /* Enable core system clocks */
1367 sunxi_clock_protect();
Emilio Lópeze874a662013-02-25 11:44:26 -03001368}
Sebastian Hesselbarthbe080452013-09-06 14:59:57 +02001369CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sunxi_init_clocks);
1370CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sunxi_init_clocks);
1371CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sunxi_init_clocks);
1372CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sunxi_init_clocks);
1373CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sunxi_init_clocks);