blob: 98fec4e4baa76ed5a0077ae57834da8e47c80626 [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>
21
22#include "clk-factors.h"
23
24static DEFINE_SPINLOCK(clk_lock);
25
26/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020027 * sun4i_osc_clk_setup() - Setup function for gatable oscillator
Emilio Lópeze874a662013-02-25 11:44:26 -030028 */
29
30#define SUNXI_OSC24M_GATE 0
31
Maxime Ripard81ba6c52013-07-22 18:21:32 +020032static void __init sun4i_osc_clk_setup(struct device_node *node)
Emilio Lópeze874a662013-02-25 11:44:26 -030033{
34 struct clk *clk;
Emilio López38e4aa02013-04-10 15:02:57 -070035 struct clk_fixed_rate *fixed;
36 struct clk_gate *gate;
Emilio Lópeze874a662013-02-25 11:44:26 -030037 const char *clk_name = node->name;
Emilio López38e4aa02013-04-10 15:02:57 -070038 u32 rate;
Emilio Lópeze874a662013-02-25 11:44:26 -030039
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030040 if (of_property_read_u32(node, "clock-frequency", &rate))
41 return;
42
Emilio López38e4aa02013-04-10 15:02:57 -070043 /* allocate fixed-rate and gate clock structs */
44 fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
45 if (!fixed)
46 return;
47 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030048 if (!gate)
49 goto err_free_fixed;
Emilio Lópeze874a662013-02-25 11:44:26 -030050
Emilio López38e4aa02013-04-10 15:02:57 -070051 /* set up gate and fixed rate properties */
52 gate->reg = of_iomap(node, 0);
53 gate->bit_idx = SUNXI_OSC24M_GATE;
54 gate->lock = &clk_lock;
55 fixed->fixed_rate = rate;
56
57 clk = clk_register_composite(NULL, clk_name,
58 NULL, 0,
59 NULL, NULL,
60 &fixed->hw, &clk_fixed_rate_ops,
61 &gate->hw, &clk_gate_ops,
62 CLK_IS_ROOT);
Emilio Lópeze874a662013-02-25 11:44:26 -030063
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030064 if (IS_ERR(clk))
65 goto err_free_gate;
66
67 of_clk_add_provider(node, of_clk_src_simple_get, clk);
68 clk_register_clkdev(clk, clk_name, NULL);
69
70 return;
71
72err_free_gate:
73 kfree(gate);
74err_free_fixed:
75 kfree(fixed);
Emilio Lópeze874a662013-02-25 11:44:26 -030076}
Maxime Ripard81ba6c52013-07-22 18:21:32 +020077CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -030078
79
80
81/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020082 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
Emilio Lópeze874a662013-02-25 11:44:26 -030083 * PLL1 rate is calculated as follows
84 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
85 * parent_rate is always 24Mhz
86 */
87
Maxime Ripard81ba6c52013-07-22 18:21:32 +020088static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -030089 u8 *n, u8 *k, u8 *m, u8 *p)
90{
91 u8 div;
92
93 /* Normalize value to a 6M multiple */
94 div = *freq / 6000000;
95 *freq = 6000000 * div;
96
97 /* we were called to round the frequency, we can now return */
98 if (n == NULL)
99 return;
100
101 /* m is always zero for pll1 */
102 *m = 0;
103
104 /* k is 1 only on these cases */
105 if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
106 *k = 1;
107 else
108 *k = 0;
109
110 /* p will be 3 for divs under 10 */
111 if (div < 10)
112 *p = 3;
113
114 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
115 else if (div < 20 || (div < 32 && (div & 1)))
116 *p = 2;
117
118 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
119 * of divs between 40-62 */
120 else if (div < 40 || (div < 64 && (div & 2)))
121 *p = 1;
122
123 /* any other entries have p = 0 */
124 else
125 *p = 0;
126
127 /* calculate a suitable n based on k and p */
128 div <<= *p;
129 div /= (*k + 1);
130 *n = div / 4;
131}
132
Maxime Ripard6a721db2013-07-23 23:34:10 +0200133/**
134 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
135 * PLL1 rate is calculated as follows
136 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
137 * parent_rate should always be 24MHz
138 */
139static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
140 u8 *n, u8 *k, u8 *m, u8 *p)
141{
142 /*
143 * We can operate only on MHz, this will make our life easier
144 * later.
145 */
146 u32 freq_mhz = *freq / 1000000;
147 u32 parent_freq_mhz = parent_rate / 1000000;
Emilio Lópeze874a662013-02-25 11:44:26 -0300148
Maxime Ripard6a721db2013-07-23 23:34:10 +0200149 /*
150 * Round down the frequency to the closest multiple of either
151 * 6 or 16
152 */
153 u32 round_freq_6 = round_down(freq_mhz, 6);
154 u32 round_freq_16 = round_down(freq_mhz, 16);
155
156 if (round_freq_6 > round_freq_16)
157 freq_mhz = round_freq_6;
158 else
159 freq_mhz = round_freq_16;
160
161 *freq = freq_mhz * 1000000;
162
163 /*
164 * If the factors pointer are null, we were just called to
165 * round down the frequency.
166 * Exit.
167 */
168 if (n == NULL)
169 return;
170
171 /* If the frequency is a multiple of 32 MHz, k is always 3 */
172 if (!(freq_mhz % 32))
173 *k = 3;
174 /* If the frequency is a multiple of 9 MHz, k is always 2 */
175 else if (!(freq_mhz % 9))
176 *k = 2;
177 /* If the frequency is a multiple of 8 MHz, k is always 1 */
178 else if (!(freq_mhz % 8))
179 *k = 1;
180 /* Otherwise, we don't use the k factor */
181 else
182 *k = 0;
183
184 /*
185 * If the frequency is a multiple of 2 but not a multiple of
186 * 3, m is 3. This is the first time we use 6 here, yet we
187 * will use it on several other places.
188 * We use this number because it's the lowest frequency we can
189 * generate (with n = 0, k = 0, m = 3), so every other frequency
190 * somehow relates to this frequency.
191 */
192 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
193 *m = 2;
194 /*
195 * If the frequency is a multiple of 6MHz, but the factor is
196 * odd, m will be 3
197 */
198 else if ((freq_mhz / 6) & 1)
199 *m = 3;
200 /* Otherwise, we end up with m = 1 */
201 else
202 *m = 1;
203
204 /* Calculate n thanks to the above factors we already got */
205 *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
206
207 /*
208 * If n end up being outbound, and that we can still decrease
209 * m, do it.
210 */
211 if ((*n + 1) > 31 && (*m + 1) > 1) {
212 *n = (*n + 1) / 2 - 1;
213 *m = (*m + 1) / 2 - 1;
214 }
215}
Emilio Lópeze874a662013-02-25 11:44:26 -0300216
217/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200218 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
Emilio Lópeze874a662013-02-25 11:44:26 -0300219 * APB1 rate is calculated as follows
220 * rate = (parent_rate >> p) / (m + 1);
221 */
222
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200223static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -0300224 u8 *n, u8 *k, u8 *m, u8 *p)
225{
226 u8 calcm, calcp;
227
228 if (parent_rate < *freq)
229 *freq = parent_rate;
230
231 parent_rate = (parent_rate + (*freq - 1)) / *freq;
232
233 /* Invalid rate! */
234 if (parent_rate > 32)
235 return;
236
237 if (parent_rate <= 4)
238 calcp = 0;
239 else if (parent_rate <= 8)
240 calcp = 1;
241 else if (parent_rate <= 16)
242 calcp = 2;
243 else
244 calcp = 3;
245
246 calcm = (parent_rate >> calcp) - 1;
247
248 *freq = (parent_rate >> calcp) / (calcm + 1);
249
250 /* we were called to round the frequency, we can now return */
251 if (n == NULL)
252 return;
253
254 *m = calcm;
255 *p = calcp;
256}
257
258
259
260/**
261 * sunxi_factors_clk_setup() - Setup function for factor clocks
262 */
263
264struct factors_data {
265 struct clk_factors_config *table;
266 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
267};
268
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200269static struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300270 .nshift = 8,
271 .nwidth = 5,
272 .kshift = 4,
273 .kwidth = 2,
274 .mshift = 0,
275 .mwidth = 2,
276 .pshift = 16,
277 .pwidth = 2,
278};
279
Maxime Ripard6a721db2013-07-23 23:34:10 +0200280static struct clk_factors_config sun6i_a31_pll1_config = {
281 .nshift = 8,
282 .nwidth = 5,
283 .kshift = 4,
284 .kwidth = 2,
285 .mshift = 0,
286 .mwidth = 2,
287};
288
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200289static struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300290 .mshift = 0,
291 .mwidth = 5,
292 .pshift = 16,
293 .pwidth = 2,
294};
295
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530296static const struct factors_data sun4i_pll1_data __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200297 .table = &sun4i_pll1_config,
298 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300299};
300
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530301static const struct factors_data sun6i_a31_pll1_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200302 .table = &sun6i_a31_pll1_config,
303 .getter = sun6i_a31_get_pll1_factors,
304};
305
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530306static const struct factors_data sun4i_apb1_data __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200307 .table = &sun4i_apb1_config,
308 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300309};
310
311static void __init sunxi_factors_clk_setup(struct device_node *node,
312 struct factors_data *data)
313{
314 struct clk *clk;
315 const char *clk_name = node->name;
316 const char *parent;
317 void *reg;
318
319 reg = of_iomap(node, 0);
320
321 parent = of_clk_get_parent_name(node, 0);
322
Emilio López5a4fe9b2013-03-27 18:20:42 -0300323 clk = clk_register_factors(NULL, clk_name, parent, 0, reg,
324 data->table, data->getter, &clk_lock);
Emilio Lópeze874a662013-02-25 11:44:26 -0300325
Axel Linee85e9b2013-07-12 16:15:15 +0800326 if (!IS_ERR(clk)) {
Emilio Lópeze874a662013-02-25 11:44:26 -0300327 of_clk_add_provider(node, of_clk_src_simple_get, clk);
328 clk_register_clkdev(clk, clk_name, NULL);
329 }
330}
331
332
333
334/**
335 * sunxi_mux_clk_setup() - Setup function for muxes
336 */
337
338#define SUNXI_MUX_GATE_WIDTH 2
339
340struct mux_data {
341 u8 shift;
342};
343
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530344static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300345 .shift = 16,
346};
347
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530348static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200349 .shift = 12,
350};
351
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530352static const struct mux_data sun4i_apb1_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300353 .shift = 24,
354};
355
356static void __init sunxi_mux_clk_setup(struct device_node *node,
357 struct mux_data *data)
358{
359 struct clk *clk;
360 const char *clk_name = node->name;
Emilio López918d7f62013-03-27 18:20:43 -0300361 const char *parents[5];
Emilio Lópeze874a662013-02-25 11:44:26 -0300362 void *reg;
363 int i = 0;
364
365 reg = of_iomap(node, 0);
366
367 while (i < 5 && (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
368 i++;
369
James Hogan819c1de2013-07-29 12:25:01 +0100370 clk = clk_register_mux(NULL, clk_name, parents, i,
371 CLK_SET_RATE_NO_REPARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300372 data->shift, SUNXI_MUX_GATE_WIDTH,
373 0, &clk_lock);
374
375 if (clk) {
376 of_clk_add_provider(node, of_clk_src_simple_get, clk);
377 clk_register_clkdev(clk, clk_name, NULL);
378 }
379}
380
381
382
383/**
384 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
385 */
386
Emilio Lópeze874a662013-02-25 11:44:26 -0300387struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200388 u8 shift;
389 u8 pow;
390 u8 width;
Emilio Lópeze874a662013-02-25 11:44:26 -0300391};
392
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530393static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200394 .shift = 0,
395 .pow = 0,
396 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300397};
398
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530399static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200400 .shift = 4,
401 .pow = 1,
402 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300403};
404
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530405static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200406 .shift = 8,
407 .pow = 1,
408 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300409};
410
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530411static const struct div_data sun6i_a31_apb2_div_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200412 .shift = 0,
413 .pow = 0,
414 .width = 4,
415};
416
Emilio Lópeze874a662013-02-25 11:44:26 -0300417static void __init sunxi_divider_clk_setup(struct device_node *node,
418 struct div_data *data)
419{
420 struct clk *clk;
421 const char *clk_name = node->name;
422 const char *clk_parent;
423 void *reg;
424
425 reg = of_iomap(node, 0);
426
427 clk_parent = of_clk_get_parent_name(node, 0);
428
429 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
Maxime Ripard70855bb2013-07-23 09:25:56 +0200430 reg, data->shift, data->width,
Emilio Lópeze874a662013-02-25 11:44:26 -0300431 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
432 &clk_lock);
433 if (clk) {
434 of_clk_add_provider(node, of_clk_src_simple_get, clk);
435 clk_register_clkdev(clk, clk_name, NULL);
436 }
437}
438
439
Emilio López13569a72013-03-27 18:20:37 -0300440
441/**
442 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
443 */
444
445#define SUNXI_GATES_MAX_SIZE 64
446
447struct gates_data {
448 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
449};
450
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530451static const struct gates_data sun4i_axi_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300452 .mask = {1},
453};
454
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530455static const struct gates_data sun4i_ahb_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300456 .mask = {0x7F77FFF, 0x14FB3F},
457};
458
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530459static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200460 .mask = {0x147667e7, 0x185915},
461};
462
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530463static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200464 .mask = {0x107067e7, 0x185111},
465};
466
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530467static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200468 .mask = {0xEDFE7F62, 0x794F931},
469};
470
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530471static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200472 .mask = { 0x12f77fff, 0x16ff3f },
473};
474
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530475static const struct gates_data sun4i_apb0_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300476 .mask = {0x4EF},
477};
478
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530479static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200480 .mask = {0x469},
481};
482
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530483static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200484 .mask = {0x61},
485};
486
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530487static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200488 .mask = { 0x4ff },
489};
490
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530491static const struct gates_data sun4i_apb1_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300492 .mask = {0xFF00F7},
493};
494
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530495static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200496 .mask = {0xf0007},
497};
498
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530499static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200500 .mask = {0xa0007},
501};
502
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530503static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200504 .mask = {0x3031},
505};
506
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530507static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200508 .mask = {0x3F000F},
509};
510
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530511static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200512 .mask = { 0xff80ff },
513};
514
Emilio López13569a72013-03-27 18:20:37 -0300515static void __init sunxi_gates_clk_setup(struct device_node *node,
516 struct gates_data *data)
517{
518 struct clk_onecell_data *clk_data;
519 const char *clk_parent;
520 const char *clk_name;
521 void *reg;
522 int qty;
523 int i = 0;
524 int j = 0;
525 int ignore;
526
527 reg = of_iomap(node, 0);
528
529 clk_parent = of_clk_get_parent_name(node, 0);
530
531 /* Worst-case size approximation and memory allocation */
532 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
533 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
534 if (!clk_data)
535 return;
536 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
537 if (!clk_data->clks) {
538 kfree(clk_data);
539 return;
540 }
541
542 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
543 of_property_read_string_index(node, "clock-output-names",
544 j, &clk_name);
545
546 /* No driver claims this clock, but it should remain gated */
547 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
548
549 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
550 clk_parent, ignore,
551 reg + 4 * (i/32), i % 32,
552 0, &clk_lock);
553 WARN_ON(IS_ERR(clk_data->clks[i]));
554
555 j++;
556 }
557
558 /* Adjust to the real max */
559 clk_data->clk_num = i;
560
561 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
562}
563
Emilio Lópeze874a662013-02-25 11:44:26 -0300564/* Matches for factors clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530565static const struct of_device_id clk_factors_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200566 {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200567 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200568 {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300569 {}
570};
571
572/* Matches for divider clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530573static const struct of_device_id clk_div_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200574 {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
575 {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
576 {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200577 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300578 {}
579};
580
581/* Matches for mux clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530582static const struct of_device_id clk_mux_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200583 {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
584 {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200585 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300586 {}
587};
588
Emilio López13569a72013-03-27 18:20:37 -0300589/* Matches for gate clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530590static const struct of_device_id clk_gates_match[] __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200591 {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
592 {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200593 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200594 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200595 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200596 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200597 {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200598 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200599 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200600 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200601 {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200602 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200603 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200604 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200605 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200606 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -0300607 {}
608};
609
Emilio Lópeze874a662013-02-25 11:44:26 -0300610static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
611 void *function)
612{
613 struct device_node *np;
614 const struct div_data *data;
615 const struct of_device_id *match;
616 void (*setup_function)(struct device_node *, const void *) = function;
617
618 for_each_matching_node(np, clk_match) {
619 match = of_match_node(clk_match, np);
620 data = match->data;
621 setup_function(np, data);
622 }
623}
624
Emilio López8e6a4c42013-09-20 22:03:12 -0300625/**
626 * System clock protection
627 *
628 * By enabling these critical clocks, we prevent their accidental gating
629 * by the framework
630 */
631static void __init sunxi_clock_protect(void)
632{
633 struct clk *clk;
634
635 /* memory bus clock - sun5i+ */
636 clk = clk_get(NULL, "mbus");
637 if (!IS_ERR(clk)) {
638 clk_prepare_enable(clk);
639 clk_put(clk);
640 }
641
642 /* DDR clock - sun4i+ */
643 clk = clk_get(NULL, "pll5_ddr");
644 if (!IS_ERR(clk)) {
645 clk_prepare_enable(clk);
646 clk_put(clk);
647 }
648}
649
Mike Turquette1d9438f2013-12-01 12:42:45 -0800650static void __init sunxi_init_clocks(void)
Emilio Lópeze874a662013-02-25 11:44:26 -0300651{
Emilio Lópeze874a662013-02-25 11:44:26 -0300652 /* Register factor clocks */
653 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
654
655 /* Register divider clocks */
656 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
657
658 /* Register mux clocks */
659 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -0300660
661 /* Register gate clocks */
662 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio López8e6a4c42013-09-20 22:03:12 -0300663
664 /* Enable core system clocks */
665 sunxi_clock_protect();
Emilio Lópeze874a662013-02-25 11:44:26 -0300666}
Sebastian Hesselbarthbe080452013-09-06 14:59:57 +0200667CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sunxi_init_clocks);
668CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sunxi_init_clocks);
669CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sunxi_init_clocks);
670CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sunxi_init_clocks);
671CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sunxi_init_clocks);