blob: a779c31b0de98bfa174df907ccec85967507b233 [file] [log] [blame]
Emilio Lópeze874a662013-02-25 11:44:26 -03001/*
2 * Copyright 2013 Emilio López
3 *
4 * Emilio López <emilio@elopez.com.ar>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/clk-provider.h>
18#include <linux/clkdev.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030019#include <linux/of.h>
20#include <linux/of_address.h>
Hans de Goedecfb00862014-02-07 16:21:49 +010021#include <linux/reset-controller.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030022
23#include "clk-factors.h"
24
25static DEFINE_SPINLOCK(clk_lock);
26
Emilio López40a5dcb2013-12-23 00:32:32 -030027/* Maximum number of parents our clocks have */
28#define SUNXI_MAX_PARENTS 5
29
Emilio Lópeze874a662013-02-25 11:44:26 -030030/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020031 * sun4i_osc_clk_setup() - Setup function for gatable oscillator
Emilio Lópeze874a662013-02-25 11:44:26 -030032 */
33
34#define SUNXI_OSC24M_GATE 0
35
Maxime Ripard81ba6c52013-07-22 18:21:32 +020036static void __init sun4i_osc_clk_setup(struct device_node *node)
Emilio Lópeze874a662013-02-25 11:44:26 -030037{
38 struct clk *clk;
Emilio López38e4aa02013-04-10 15:02:57 -070039 struct clk_fixed_rate *fixed;
40 struct clk_gate *gate;
Emilio Lópeze874a662013-02-25 11:44:26 -030041 const char *clk_name = node->name;
Emilio López38e4aa02013-04-10 15:02:57 -070042 u32 rate;
Emilio Lópeze874a662013-02-25 11:44:26 -030043
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030044 if (of_property_read_u32(node, "clock-frequency", &rate))
45 return;
46
Emilio López38e4aa02013-04-10 15:02:57 -070047 /* allocate fixed-rate and gate clock structs */
48 fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
49 if (!fixed)
50 return;
51 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030052 if (!gate)
53 goto err_free_fixed;
Emilio Lópeze874a662013-02-25 11:44:26 -030054
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +080055 of_property_read_string(node, "clock-output-names", &clk_name);
56
Emilio López38e4aa02013-04-10 15:02:57 -070057 /* set up gate and fixed rate properties */
58 gate->reg = of_iomap(node, 0);
59 gate->bit_idx = SUNXI_OSC24M_GATE;
60 gate->lock = &clk_lock;
61 fixed->fixed_rate = rate;
62
63 clk = clk_register_composite(NULL, clk_name,
64 NULL, 0,
65 NULL, NULL,
66 &fixed->hw, &clk_fixed_rate_ops,
67 &gate->hw, &clk_gate_ops,
68 CLK_IS_ROOT);
Emilio Lópeze874a662013-02-25 11:44:26 -030069
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030070 if (IS_ERR(clk))
71 goto err_free_gate;
72
73 of_clk_add_provider(node, of_clk_src_simple_get, clk);
74 clk_register_clkdev(clk, clk_name, NULL);
75
76 return;
77
78err_free_gate:
79 kfree(gate);
80err_free_fixed:
81 kfree(fixed);
Emilio Lópeze874a662013-02-25 11:44:26 -030082}
Maxime Ripard81ba6c52013-07-22 18:21:32 +020083CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -030084
85
86
87/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020088 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
Emilio Lópeze874a662013-02-25 11:44:26 -030089 * PLL1 rate is calculated as follows
90 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
91 * parent_rate is always 24Mhz
92 */
93
Maxime Ripard81ba6c52013-07-22 18:21:32 +020094static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -030095 u8 *n, u8 *k, u8 *m, u8 *p)
96{
97 u8 div;
98
99 /* Normalize value to a 6M multiple */
100 div = *freq / 6000000;
101 *freq = 6000000 * div;
102
103 /* we were called to round the frequency, we can now return */
104 if (n == NULL)
105 return;
106
107 /* m is always zero for pll1 */
108 *m = 0;
109
110 /* k is 1 only on these cases */
111 if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
112 *k = 1;
113 else
114 *k = 0;
115
116 /* p will be 3 for divs under 10 */
117 if (div < 10)
118 *p = 3;
119
120 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
121 else if (div < 20 || (div < 32 && (div & 1)))
122 *p = 2;
123
124 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
125 * of divs between 40-62 */
126 else if (div < 40 || (div < 64 && (div & 2)))
127 *p = 1;
128
129 /* any other entries have p = 0 */
130 else
131 *p = 0;
132
133 /* calculate a suitable n based on k and p */
134 div <<= *p;
135 div /= (*k + 1);
136 *n = div / 4;
137}
138
Maxime Ripard6a721db2013-07-23 23:34:10 +0200139/**
140 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
141 * PLL1 rate is calculated as follows
142 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
143 * parent_rate should always be 24MHz
144 */
145static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
146 u8 *n, u8 *k, u8 *m, u8 *p)
147{
148 /*
149 * We can operate only on MHz, this will make our life easier
150 * later.
151 */
152 u32 freq_mhz = *freq / 1000000;
153 u32 parent_freq_mhz = parent_rate / 1000000;
Emilio Lópeze874a662013-02-25 11:44:26 -0300154
Maxime Ripard6a721db2013-07-23 23:34:10 +0200155 /*
156 * Round down the frequency to the closest multiple of either
157 * 6 or 16
158 */
159 u32 round_freq_6 = round_down(freq_mhz, 6);
160 u32 round_freq_16 = round_down(freq_mhz, 16);
161
162 if (round_freq_6 > round_freq_16)
163 freq_mhz = round_freq_6;
164 else
165 freq_mhz = round_freq_16;
166
167 *freq = freq_mhz * 1000000;
168
169 /*
170 * If the factors pointer are null, we were just called to
171 * round down the frequency.
172 * Exit.
173 */
174 if (n == NULL)
175 return;
176
177 /* If the frequency is a multiple of 32 MHz, k is always 3 */
178 if (!(freq_mhz % 32))
179 *k = 3;
180 /* If the frequency is a multiple of 9 MHz, k is always 2 */
181 else if (!(freq_mhz % 9))
182 *k = 2;
183 /* If the frequency is a multiple of 8 MHz, k is always 1 */
184 else if (!(freq_mhz % 8))
185 *k = 1;
186 /* Otherwise, we don't use the k factor */
187 else
188 *k = 0;
189
190 /*
191 * If the frequency is a multiple of 2 but not a multiple of
192 * 3, m is 3. This is the first time we use 6 here, yet we
193 * will use it on several other places.
194 * We use this number because it's the lowest frequency we can
195 * generate (with n = 0, k = 0, m = 3), so every other frequency
196 * somehow relates to this frequency.
197 */
198 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
199 *m = 2;
200 /*
201 * If the frequency is a multiple of 6MHz, but the factor is
202 * odd, m will be 3
203 */
204 else if ((freq_mhz / 6) & 1)
205 *m = 3;
206 /* Otherwise, we end up with m = 1 */
207 else
208 *m = 1;
209
210 /* Calculate n thanks to the above factors we already got */
211 *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
212
213 /*
214 * If n end up being outbound, and that we can still decrease
215 * m, do it.
216 */
217 if ((*n + 1) > 31 && (*m + 1) > 1) {
218 *n = (*n + 1) / 2 - 1;
219 *m = (*m + 1) / 2 - 1;
220 }
221}
Emilio Lópeze874a662013-02-25 11:44:26 -0300222
223/**
Emilio Lópezd584c132013-12-23 00:32:37 -0300224 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
225 * PLL5 rate is calculated as follows
226 * rate = parent_rate * n * (k + 1)
227 * parent_rate is always 24Mhz
228 */
229
230static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate,
231 u8 *n, u8 *k, u8 *m, u8 *p)
232{
233 u8 div;
234
235 /* Normalize value to a parent_rate multiple (24M) */
236 div = *freq / parent_rate;
237 *freq = parent_rate * div;
238
239 /* we were called to round the frequency, we can now return */
240 if (n == NULL)
241 return;
242
243 if (div < 31)
244 *k = 0;
245 else if (div / 2 < 31)
246 *k = 1;
247 else if (div / 3 < 31)
248 *k = 2;
249 else
250 *k = 3;
251
252 *n = DIV_ROUND_UP(div, (*k+1));
253}
254
255
256
257/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200258 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
Emilio Lópeze874a662013-02-25 11:44:26 -0300259 * APB1 rate is calculated as follows
260 * rate = (parent_rate >> p) / (m + 1);
261 */
262
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200263static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -0300264 u8 *n, u8 *k, u8 *m, u8 *p)
265{
266 u8 calcm, calcp;
267
268 if (parent_rate < *freq)
269 *freq = parent_rate;
270
271 parent_rate = (parent_rate + (*freq - 1)) / *freq;
272
273 /* Invalid rate! */
274 if (parent_rate > 32)
275 return;
276
277 if (parent_rate <= 4)
278 calcp = 0;
279 else if (parent_rate <= 8)
280 calcp = 1;
281 else if (parent_rate <= 16)
282 calcp = 2;
283 else
284 calcp = 3;
285
286 calcm = (parent_rate >> calcp) - 1;
287
288 *freq = (parent_rate >> calcp) / (calcm + 1);
289
290 /* we were called to round the frequency, we can now return */
291 if (n == NULL)
292 return;
293
294 *m = calcm;
295 *p = calcp;
296}
297
298
299
300/**
Emilio López75517692013-12-23 00:32:39 -0300301 * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
302 * MMC rate is calculated as follows
303 * rate = (parent_rate >> p) / (m + 1);
304 */
305
306static void sun4i_get_mod0_factors(u32 *freq, u32 parent_rate,
307 u8 *n, u8 *k, u8 *m, u8 *p)
308{
309 u8 div, calcm, calcp;
310
311 /* These clocks can only divide, so we will never be able to achieve
312 * frequencies higher than the parent frequency */
313 if (*freq > parent_rate)
314 *freq = parent_rate;
315
316 div = parent_rate / *freq;
317
318 if (div < 16)
319 calcp = 0;
320 else if (div / 2 < 16)
321 calcp = 1;
322 else if (div / 4 < 16)
323 calcp = 2;
324 else
325 calcp = 3;
326
327 calcm = DIV_ROUND_UP(div, 1 << calcp);
328
329 *freq = (parent_rate >> calcp) / calcm;
330
331 /* we were called to round the frequency, we can now return */
332 if (n == NULL)
333 return;
334
335 *m = calcm - 1;
336 *p = calcp;
337}
338
339
340
341/**
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800342 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
343 * CLK_OUT rate is calculated as follows
344 * rate = (parent_rate >> p) / (m + 1);
345 */
346
347static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
348 u8 *n, u8 *k, u8 *m, u8 *p)
349{
350 u8 div, calcm, calcp;
351
352 /* These clocks can only divide, so we will never be able to achieve
353 * frequencies higher than the parent frequency */
354 if (*freq > parent_rate)
355 *freq = parent_rate;
356
357 div = parent_rate / *freq;
358
359 if (div < 32)
360 calcp = 0;
361 else if (div / 2 < 32)
362 calcp = 1;
363 else if (div / 4 < 32)
364 calcp = 2;
365 else
366 calcp = 3;
367
368 calcm = DIV_ROUND_UP(div, 1 << calcp);
369
370 *freq = (parent_rate >> calcp) / calcm;
371
372 /* we were called to round the frequency, we can now return */
373 if (n == NULL)
374 return;
375
376 *m = calcm - 1;
377 *p = calcp;
378}
379
380
381
382/**
Emilio Lópeze874a662013-02-25 11:44:26 -0300383 * sunxi_factors_clk_setup() - Setup function for factor clocks
384 */
385
Emilio López40a5dcb2013-12-23 00:32:32 -0300386#define SUNXI_FACTORS_MUX_MASK 0x3
387
Emilio Lópeze874a662013-02-25 11:44:26 -0300388struct factors_data {
Emilio López40a5dcb2013-12-23 00:32:32 -0300389 int enable;
390 int mux;
Emilio Lópeze874a662013-02-25 11:44:26 -0300391 struct clk_factors_config *table;
392 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800393 const char *name;
Emilio Lópeze874a662013-02-25 11:44:26 -0300394};
395
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200396static struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300397 .nshift = 8,
398 .nwidth = 5,
399 .kshift = 4,
400 .kwidth = 2,
401 .mshift = 0,
402 .mwidth = 2,
403 .pshift = 16,
404 .pwidth = 2,
405};
406
Maxime Ripard6a721db2013-07-23 23:34:10 +0200407static struct clk_factors_config sun6i_a31_pll1_config = {
408 .nshift = 8,
409 .nwidth = 5,
410 .kshift = 4,
411 .kwidth = 2,
412 .mshift = 0,
413 .mwidth = 2,
414};
415
Emilio Lópezd584c132013-12-23 00:32:37 -0300416static struct clk_factors_config sun4i_pll5_config = {
417 .nshift = 8,
418 .nwidth = 5,
419 .kshift = 4,
420 .kwidth = 2,
421};
422
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200423static struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300424 .mshift = 0,
425 .mwidth = 5,
426 .pshift = 16,
427 .pwidth = 2,
428};
429
Emilio López75517692013-12-23 00:32:39 -0300430/* user manual says "n" but it's really "p" */
431static struct clk_factors_config sun4i_mod0_config = {
432 .mshift = 0,
433 .mwidth = 4,
434 .pshift = 16,
435 .pwidth = 2,
436};
437
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800438/* user manual says "n" but it's really "p" */
439static struct clk_factors_config sun7i_a20_out_config = {
440 .mshift = 8,
441 .mwidth = 5,
442 .pshift = 20,
443 .pwidth = 2,
444};
445
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530446static const struct factors_data sun4i_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300447 .enable = 31,
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200448 .table = &sun4i_pll1_config,
449 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300450};
451
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530452static const struct factors_data sun6i_a31_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300453 .enable = 31,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200454 .table = &sun6i_a31_pll1_config,
455 .getter = sun6i_a31_get_pll1_factors,
456};
457
Emilio Lópezd584c132013-12-23 00:32:37 -0300458static const struct factors_data sun4i_pll5_data __initconst = {
459 .enable = 31,
460 .table = &sun4i_pll5_config,
461 .getter = sun4i_get_pll5_factors,
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800462 .name = "pll5",
463};
464
465static const struct factors_data sun4i_pll6_data __initconst = {
466 .enable = 31,
467 .table = &sun4i_pll5_config,
468 .getter = sun4i_get_pll5_factors,
469 .name = "pll6",
Emilio Lópezd584c132013-12-23 00:32:37 -0300470};
471
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530472static const struct factors_data sun4i_apb1_data __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200473 .table = &sun4i_apb1_config,
474 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300475};
476
Emilio López75517692013-12-23 00:32:39 -0300477static const struct factors_data sun4i_mod0_data __initconst = {
478 .enable = 31,
479 .mux = 24,
480 .table = &sun4i_mod0_config,
481 .getter = sun4i_get_mod0_factors,
482};
483
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800484static const struct factors_data sun7i_a20_out_data __initconst = {
485 .enable = 31,
486 .mux = 24,
487 .table = &sun7i_a20_out_config,
488 .getter = sun7i_a20_get_out_factors,
489};
490
Emilio López5f4e0be2013-12-23 00:32:36 -0300491static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
492 const struct factors_data *data)
Emilio Lópeze874a662013-02-25 11:44:26 -0300493{
494 struct clk *clk;
Emilio López40a5dcb2013-12-23 00:32:32 -0300495 struct clk_factors *factors;
496 struct clk_gate *gate = NULL;
497 struct clk_mux *mux = NULL;
498 struct clk_hw *gate_hw = NULL;
499 struct clk_hw *mux_hw = NULL;
Emilio Lópeze874a662013-02-25 11:44:26 -0300500 const char *clk_name = node->name;
Emilio López40a5dcb2013-12-23 00:32:32 -0300501 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300502 void *reg;
Emilio López40a5dcb2013-12-23 00:32:32 -0300503 int i = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -0300504
505 reg = of_iomap(node, 0);
506
Emilio López40a5dcb2013-12-23 00:32:32 -0300507 /* if we have a mux, we will have >1 parents */
508 while (i < SUNXI_MAX_PARENTS &&
509 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
510 i++;
Emilio Lópeze874a662013-02-25 11:44:26 -0300511
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800512 /*
513 * some factor clocks, such as pll5 and pll6, may have multiple
514 * outputs, and have their name designated in factors_data
515 */
516 if (data->name)
517 clk_name = data->name;
518 else
519 of_property_read_string(node, "clock-output-names", &clk_name);
Emilio López76192dc2013-12-23 00:32:40 -0300520
Emilio López40a5dcb2013-12-23 00:32:32 -0300521 factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
522 if (!factors)
Emilio López5f4e0be2013-12-23 00:32:36 -0300523 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300524
525 /* Add a gate if this factor clock can be gated */
526 if (data->enable) {
527 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
528 if (!gate) {
529 kfree(factors);
Emilio López5f4e0be2013-12-23 00:32:36 -0300530 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300531 }
532
533 /* set up gate properties */
534 gate->reg = reg;
535 gate->bit_idx = data->enable;
536 gate->lock = &clk_lock;
537 gate_hw = &gate->hw;
538 }
539
540 /* Add a mux if this factor clock can be muxed */
541 if (data->mux) {
542 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
543 if (!mux) {
544 kfree(factors);
545 kfree(gate);
Emilio López5f4e0be2013-12-23 00:32:36 -0300546 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300547 }
548
549 /* set up gate properties */
550 mux->reg = reg;
551 mux->shift = data->mux;
552 mux->mask = SUNXI_FACTORS_MUX_MASK;
553 mux->lock = &clk_lock;
554 mux_hw = &mux->hw;
555 }
556
557 /* set up factors properties */
558 factors->reg = reg;
559 factors->config = data->table;
560 factors->get_factors = data->getter;
561 factors->lock = &clk_lock;
562
563 clk = clk_register_composite(NULL, clk_name,
564 parents, i,
565 mux_hw, &clk_mux_ops,
566 &factors->hw, &clk_factors_ops,
Emilio López5f4e0be2013-12-23 00:32:36 -0300567 gate_hw, &clk_gate_ops, 0);
Emilio Lópeze874a662013-02-25 11:44:26 -0300568
Axel Linee85e9b2013-07-12 16:15:15 +0800569 if (!IS_ERR(clk)) {
Emilio Lópeze874a662013-02-25 11:44:26 -0300570 of_clk_add_provider(node, of_clk_src_simple_get, clk);
571 clk_register_clkdev(clk, clk_name, NULL);
572 }
Emilio López5f4e0be2013-12-23 00:32:36 -0300573
574 return clk;
Emilio Lópeze874a662013-02-25 11:44:26 -0300575}
576
577
578
579/**
580 * sunxi_mux_clk_setup() - Setup function for muxes
581 */
582
583#define SUNXI_MUX_GATE_WIDTH 2
584
585struct mux_data {
586 u8 shift;
587};
588
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530589static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300590 .shift = 16,
591};
592
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530593static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200594 .shift = 12,
595};
596
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530597static const struct mux_data sun4i_apb1_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300598 .shift = 24,
599};
600
601static void __init sunxi_mux_clk_setup(struct device_node *node,
602 struct mux_data *data)
603{
604 struct clk *clk;
605 const char *clk_name = node->name;
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300606 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300607 void *reg;
608 int i = 0;
609
610 reg = of_iomap(node, 0);
611
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300612 while (i < SUNXI_MAX_PARENTS &&
613 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
Emilio Lópeze874a662013-02-25 11:44:26 -0300614 i++;
615
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800616 of_property_read_string(node, "clock-output-names", &clk_name);
617
James Hogan819c1de2013-07-29 12:25:01 +0100618 clk = clk_register_mux(NULL, clk_name, parents, i,
619 CLK_SET_RATE_NO_REPARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300620 data->shift, SUNXI_MUX_GATE_WIDTH,
621 0, &clk_lock);
622
623 if (clk) {
624 of_clk_add_provider(node, of_clk_src_simple_get, clk);
625 clk_register_clkdev(clk, clk_name, NULL);
626 }
627}
628
629
630
631/**
632 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
633 */
634
Emilio Lópeze874a662013-02-25 11:44:26 -0300635struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200636 u8 shift;
637 u8 pow;
638 u8 width;
Emilio Lópeze874a662013-02-25 11:44:26 -0300639};
640
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530641static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200642 .shift = 0,
643 .pow = 0,
644 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300645};
646
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530647static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200648 .shift = 4,
649 .pow = 1,
650 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300651};
652
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530653static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200654 .shift = 8,
655 .pow = 1,
656 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300657};
658
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530659static const struct div_data sun6i_a31_apb2_div_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200660 .shift = 0,
661 .pow = 0,
662 .width = 4,
663};
664
Emilio Lópeze874a662013-02-25 11:44:26 -0300665static void __init sunxi_divider_clk_setup(struct device_node *node,
666 struct div_data *data)
667{
668 struct clk *clk;
669 const char *clk_name = node->name;
670 const char *clk_parent;
671 void *reg;
672
673 reg = of_iomap(node, 0);
674
675 clk_parent = of_clk_get_parent_name(node, 0);
676
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800677 of_property_read_string(node, "clock-output-names", &clk_name);
678
Emilio Lópeze874a662013-02-25 11:44:26 -0300679 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
Maxime Ripard70855bb2013-07-23 09:25:56 +0200680 reg, data->shift, data->width,
Emilio Lópeze874a662013-02-25 11:44:26 -0300681 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
682 &clk_lock);
683 if (clk) {
684 of_clk_add_provider(node, of_clk_src_simple_get, clk);
685 clk_register_clkdev(clk, clk_name, NULL);
686 }
687}
688
689
Emilio López13569a72013-03-27 18:20:37 -0300690
691/**
Hans de Goedecfb00862014-02-07 16:21:49 +0100692 * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
693 */
694
695struct gates_reset_data {
696 void __iomem *reg;
697 spinlock_t *lock;
698 struct reset_controller_dev rcdev;
699};
700
701static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev,
702 unsigned long id)
703{
704 struct gates_reset_data *data = container_of(rcdev,
705 struct gates_reset_data,
706 rcdev);
707 unsigned long flags;
708 u32 reg;
709
710 spin_lock_irqsave(data->lock, flags);
711
712 reg = readl(data->reg);
713 writel(reg & ~BIT(id), data->reg);
714
715 spin_unlock_irqrestore(data->lock, flags);
716
717 return 0;
718}
719
720static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev,
721 unsigned long id)
722{
723 struct gates_reset_data *data = container_of(rcdev,
724 struct gates_reset_data,
725 rcdev);
726 unsigned long flags;
727 u32 reg;
728
729 spin_lock_irqsave(data->lock, flags);
730
731 reg = readl(data->reg);
732 writel(reg | BIT(id), data->reg);
733
734 spin_unlock_irqrestore(data->lock, flags);
735
736 return 0;
737}
738
739static struct reset_control_ops sunxi_gates_reset_ops = {
740 .assert = sunxi_gates_reset_assert,
741 .deassert = sunxi_gates_reset_deassert,
742};
743
744/**
Emilio López13569a72013-03-27 18:20:37 -0300745 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
746 */
747
748#define SUNXI_GATES_MAX_SIZE 64
749
750struct gates_data {
751 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
Hans de Goedecfb00862014-02-07 16:21:49 +0100752 u32 reset_mask;
Emilio López13569a72013-03-27 18:20:37 -0300753};
754
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530755static const struct gates_data sun4i_axi_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300756 .mask = {1},
757};
758
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530759static const struct gates_data sun4i_ahb_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300760 .mask = {0x7F77FFF, 0x14FB3F},
761};
762
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530763static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200764 .mask = {0x147667e7, 0x185915},
765};
766
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530767static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200768 .mask = {0x107067e7, 0x185111},
769};
770
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530771static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200772 .mask = {0xEDFE7F62, 0x794F931},
773};
774
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530775static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200776 .mask = { 0x12f77fff, 0x16ff3f },
777};
778
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530779static const struct gates_data sun4i_apb0_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300780 .mask = {0x4EF},
781};
782
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530783static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200784 .mask = {0x469},
785};
786
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530787static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200788 .mask = {0x61},
789};
790
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530791static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200792 .mask = { 0x4ff },
793};
794
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530795static const struct gates_data sun4i_apb1_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300796 .mask = {0xFF00F7},
797};
798
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530799static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200800 .mask = {0xf0007},
801};
802
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530803static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200804 .mask = {0xa0007},
805};
806
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530807static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200808 .mask = {0x3031},
809};
810
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530811static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200812 .mask = {0x3F000F},
813};
814
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530815static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200816 .mask = { 0xff80ff },
817};
818
Roman Byshko5abdbf22014-02-07 16:21:50 +0100819static const struct gates_data sun4i_a10_usb_gates_data __initconst = {
820 .mask = {0x1C0},
821 .reset_mask = 0x07,
822};
823
824static const struct gates_data sun5i_a13_usb_gates_data __initconst = {
825 .mask = {0x140},
826 .reset_mask = 0x03,
827};
828
Emilio López13569a72013-03-27 18:20:37 -0300829static void __init sunxi_gates_clk_setup(struct device_node *node,
830 struct gates_data *data)
831{
832 struct clk_onecell_data *clk_data;
Hans de Goedecfb00862014-02-07 16:21:49 +0100833 struct gates_reset_data *reset_data;
Emilio López13569a72013-03-27 18:20:37 -0300834 const char *clk_parent;
835 const char *clk_name;
836 void *reg;
837 int qty;
838 int i = 0;
839 int j = 0;
840 int ignore;
841
842 reg = of_iomap(node, 0);
843
844 clk_parent = of_clk_get_parent_name(node, 0);
845
846 /* Worst-case size approximation and memory allocation */
847 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
848 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
849 if (!clk_data)
850 return;
851 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
852 if (!clk_data->clks) {
853 kfree(clk_data);
854 return;
855 }
856
857 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
858 of_property_read_string_index(node, "clock-output-names",
859 j, &clk_name);
860
861 /* No driver claims this clock, but it should remain gated */
862 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
863
864 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
865 clk_parent, ignore,
866 reg + 4 * (i/32), i % 32,
867 0, &clk_lock);
868 WARN_ON(IS_ERR(clk_data->clks[i]));
869
870 j++;
871 }
872
873 /* Adjust to the real max */
874 clk_data->clk_num = i;
875
876 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
Hans de Goedecfb00862014-02-07 16:21:49 +0100877
878 /* Register a reset controler for gates with reset bits */
879 if (data->reset_mask == 0)
880 return;
881
882 reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
883 if (!reset_data)
884 return;
885
886 reset_data->reg = reg;
887 reset_data->lock = &clk_lock;
888 reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
889 reset_data->rcdev.ops = &sunxi_gates_reset_ops;
890 reset_data->rcdev.of_node = node;
891 reset_controller_register(&reset_data->rcdev);
Emilio López13569a72013-03-27 18:20:37 -0300892}
893
Emilio Lópezd584c132013-12-23 00:32:37 -0300894
895
896/**
897 * sunxi_divs_clk_setup() helper data
898 */
899
900#define SUNXI_DIVS_MAX_QTY 2
901#define SUNXI_DIVISOR_WIDTH 2
902
903struct divs_data {
904 const struct factors_data *factors; /* data for the factor clock */
905 struct {
906 u8 fixed; /* is it a fixed divisor? if not... */
907 struct clk_div_table *table; /* is it a table based divisor? */
908 u8 shift; /* otherwise it's a normal divisor with this shift */
909 u8 pow; /* is it power-of-two based? */
910 u8 gate; /* is it independently gateable? */
911 } div[SUNXI_DIVS_MAX_QTY];
912};
913
914static struct clk_div_table pll6_sata_tbl[] = {
915 { .val = 0, .div = 6, },
916 { .val = 1, .div = 12, },
917 { .val = 2, .div = 18, },
918 { .val = 3, .div = 24, },
919 { } /* sentinel */
920};
921
922static const struct divs_data pll5_divs_data __initconst = {
923 .factors = &sun4i_pll5_data,
924 .div = {
925 { .shift = 0, .pow = 0, }, /* M, DDR */
926 { .shift = 16, .pow = 1, }, /* P, other */
927 }
928};
929
930static const struct divs_data pll6_divs_data __initconst = {
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800931 .factors = &sun4i_pll6_data,
Emilio Lópezd584c132013-12-23 00:32:37 -0300932 .div = {
933 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
934 { .fixed = 2 }, /* P, other */
935 }
936};
937
938/**
939 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
940 *
941 * These clocks look something like this
942 * ________________________
943 * | ___divisor 1---|----> to consumer
944 * parent >--| pll___/___divisor 2---|----> to consumer
945 * | \_______________|____> to consumer
946 * |________________________|
947 */
948
949static void __init sunxi_divs_clk_setup(struct device_node *node,
950 struct divs_data *data)
951{
952 struct clk_onecell_data *clk_data;
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +0800953 const char *parent;
Emilio Lópezd584c132013-12-23 00:32:37 -0300954 const char *clk_name;
955 struct clk **clks, *pclk;
956 struct clk_hw *gate_hw, *rate_hw;
957 const struct clk_ops *rate_ops;
958 struct clk_gate *gate = NULL;
959 struct clk_fixed_factor *fix_factor;
960 struct clk_divider *divider;
961 void *reg;
962 int i = 0;
963 int flags, clkflags;
964
965 /* Set up factor clock that we will be dividing */
966 pclk = sunxi_factors_clk_setup(node, data->factors);
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +0800967 parent = __clk_get_name(pclk);
Emilio Lópezd584c132013-12-23 00:32:37 -0300968
969 reg = of_iomap(node, 0);
970
971 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
972 if (!clk_data)
973 return;
974
Emilio Lópezd1933682014-01-24 22:32:41 -0300975 clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
Emilio Lópezd584c132013-12-23 00:32:37 -0300976 if (!clks)
977 goto free_clkdata;
978
979 clk_data->clks = clks;
980
981 /* It's not a good idea to have automatic reparenting changing
982 * our RAM clock! */
983 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
984
985 for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
986 if (of_property_read_string_index(node, "clock-output-names",
987 i, &clk_name) != 0)
988 break;
989
990 gate_hw = NULL;
991 rate_hw = NULL;
992 rate_ops = NULL;
993
994 /* If this leaf clock can be gated, create a gate */
995 if (data->div[i].gate) {
996 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
997 if (!gate)
998 goto free_clks;
999
1000 gate->reg = reg;
1001 gate->bit_idx = data->div[i].gate;
1002 gate->lock = &clk_lock;
1003
1004 gate_hw = &gate->hw;
1005 }
1006
1007 /* Leaves can be fixed or configurable divisors */
1008 if (data->div[i].fixed) {
1009 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1010 if (!fix_factor)
1011 goto free_gate;
1012
1013 fix_factor->mult = 1;
1014 fix_factor->div = data->div[i].fixed;
1015
1016 rate_hw = &fix_factor->hw;
1017 rate_ops = &clk_fixed_factor_ops;
1018 } else {
1019 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1020 if (!divider)
1021 goto free_gate;
1022
1023 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1024
1025 divider->reg = reg;
1026 divider->shift = data->div[i].shift;
1027 divider->width = SUNXI_DIVISOR_WIDTH;
1028 divider->flags = flags;
1029 divider->lock = &clk_lock;
1030 divider->table = data->div[i].table;
1031
1032 rate_hw = &divider->hw;
1033 rate_ops = &clk_divider_ops;
1034 }
1035
1036 /* Wrap the (potential) gate and the divisor on a composite
1037 * clock to unify them */
1038 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1039 NULL, NULL,
1040 rate_hw, rate_ops,
1041 gate_hw, &clk_gate_ops,
1042 clkflags);
1043
1044 WARN_ON(IS_ERR(clk_data->clks[i]));
1045 clk_register_clkdev(clks[i], clk_name, NULL);
1046 }
1047
1048 /* The last clock available on the getter is the parent */
1049 clks[i++] = pclk;
1050
1051 /* Adjust to the real max */
1052 clk_data->clk_num = i;
1053
1054 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
1055
1056 return;
1057
1058free_gate:
1059 kfree(gate);
1060free_clks:
1061 kfree(clks);
1062free_clkdata:
1063 kfree(clk_data);
1064}
1065
1066
1067
Emilio Lópeze874a662013-02-25 11:44:26 -03001068/* Matches for factors clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301069static const struct of_device_id clk_factors_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +02001070 {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001071 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
Maxime Ripard81ba6c52013-07-22 18:21:32 +02001072 {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
Emilio López75517692013-12-23 00:32:39 -03001073 {.compatible = "allwinner,sun4i-mod0-clk", .data = &sun4i_mod0_data,},
Chen-Yu Tsai6f863412013-12-24 21:26:17 +08001074 {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001075 {}
1076};
1077
1078/* Matches for divider clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301079static const struct of_device_id clk_div_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +02001080 {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
1081 {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
1082 {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001083 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001084 {}
1085};
1086
Emilio Lópezd584c132013-12-23 00:32:37 -03001087/* Matches for divided outputs */
1088static const struct of_device_id clk_divs_match[] __initconst = {
1089 {.compatible = "allwinner,sun4i-pll5-clk", .data = &pll5_divs_data,},
1090 {.compatible = "allwinner,sun4i-pll6-clk", .data = &pll6_divs_data,},
1091 {}
1092};
1093
Emilio Lópeze874a662013-02-25 11:44:26 -03001094/* Matches for mux clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301095static const struct of_device_id clk_mux_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +02001096 {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
1097 {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001098 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -03001099 {}
1100};
1101
Emilio López13569a72013-03-27 18:20:37 -03001102/* Matches for gate clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +05301103static const struct of_device_id clk_gates_match[] __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +02001104 {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
1105 {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001106 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001107 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001108 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001109 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001110 {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001111 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001112 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001113 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001114 {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +02001115 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +02001116 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001117 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +02001118 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +02001119 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
Roman Byshko5abdbf22014-02-07 16:21:50 +01001120 {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
1121 {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -03001122 {}
1123};
1124
Emilio Lópeze874a662013-02-25 11:44:26 -03001125static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
1126 void *function)
1127{
1128 struct device_node *np;
1129 const struct div_data *data;
1130 const struct of_device_id *match;
1131 void (*setup_function)(struct device_node *, const void *) = function;
1132
1133 for_each_matching_node(np, clk_match) {
1134 match = of_match_node(clk_match, np);
1135 data = match->data;
1136 setup_function(np, data);
1137 }
1138}
1139
Emilio López8e6a4c42013-09-20 22:03:12 -03001140/**
1141 * System clock protection
1142 *
1143 * By enabling these critical clocks, we prevent their accidental gating
1144 * by the framework
1145 */
1146static void __init sunxi_clock_protect(void)
1147{
1148 struct clk *clk;
1149
1150 /* memory bus clock - sun5i+ */
1151 clk = clk_get(NULL, "mbus");
1152 if (!IS_ERR(clk)) {
1153 clk_prepare_enable(clk);
1154 clk_put(clk);
1155 }
1156
1157 /* DDR clock - sun4i+ */
1158 clk = clk_get(NULL, "pll5_ddr");
1159 if (!IS_ERR(clk)) {
1160 clk_prepare_enable(clk);
1161 clk_put(clk);
1162 }
1163}
1164
Mike Turquette1d9438f2013-12-01 12:42:45 -08001165static void __init sunxi_init_clocks(void)
Emilio Lópeze874a662013-02-25 11:44:26 -03001166{
Emilio Lópeze874a662013-02-25 11:44:26 -03001167 /* Register factor clocks */
1168 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1169
1170 /* Register divider clocks */
1171 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1172
Emilio Lópezd584c132013-12-23 00:32:37 -03001173 /* Register divided output clocks */
1174 of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1175
Emilio Lópeze874a662013-02-25 11:44:26 -03001176 /* Register mux clocks */
1177 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -03001178
1179 /* Register gate clocks */
1180 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio López8e6a4c42013-09-20 22:03:12 -03001181
1182 /* Enable core system clocks */
1183 sunxi_clock_protect();
Emilio Lópeze874a662013-02-25 11:44:26 -03001184}
Sebastian Hesselbarthbe080452013-09-06 14:59:57 +02001185CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sunxi_init_clocks);
1186CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sunxi_init_clocks);
1187CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sunxi_init_clocks);
1188CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sunxi_init_clocks);
1189CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sunxi_init_clocks);