blob: 9665cb8d023878231c0fa5beeffe198c3ba4f5cc [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>
19#include <linux/clk/sunxi.h>
20#include <linux/of.h>
21#include <linux/of_address.h>
22
23#include "clk-factors.h"
24
25static DEFINE_SPINLOCK(clk_lock);
26
27/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020028 * sun4i_osc_clk_setup() - Setup function for gatable oscillator
Emilio Lópeze874a662013-02-25 11:44:26 -030029 */
30
31#define SUNXI_OSC24M_GATE 0
32
Maxime Ripard81ba6c52013-07-22 18:21:32 +020033static void __init sun4i_osc_clk_setup(struct device_node *node)
Emilio Lópeze874a662013-02-25 11:44:26 -030034{
35 struct clk *clk;
Emilio López38e4aa02013-04-10 15:02:57 -070036 struct clk_fixed_rate *fixed;
37 struct clk_gate *gate;
Emilio Lópeze874a662013-02-25 11:44:26 -030038 const char *clk_name = node->name;
Emilio López38e4aa02013-04-10 15:02:57 -070039 u32 rate;
Emilio Lópeze874a662013-02-25 11:44:26 -030040
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030041 if (of_property_read_u32(node, "clock-frequency", &rate))
42 return;
43
Emilio López38e4aa02013-04-10 15:02:57 -070044 /* allocate fixed-rate and gate clock structs */
45 fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
46 if (!fixed)
47 return;
48 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030049 if (!gate)
50 goto err_free_fixed;
Emilio Lópeze874a662013-02-25 11:44:26 -030051
Emilio López38e4aa02013-04-10 15:02:57 -070052 /* set up gate and fixed rate properties */
53 gate->reg = of_iomap(node, 0);
54 gate->bit_idx = SUNXI_OSC24M_GATE;
55 gate->lock = &clk_lock;
56 fixed->fixed_rate = rate;
57
58 clk = clk_register_composite(NULL, clk_name,
59 NULL, 0,
60 NULL, NULL,
61 &fixed->hw, &clk_fixed_rate_ops,
62 &gate->hw, &clk_gate_ops,
63 CLK_IS_ROOT);
Emilio Lópeze874a662013-02-25 11:44:26 -030064
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030065 if (IS_ERR(clk))
66 goto err_free_gate;
67
68 of_clk_add_provider(node, of_clk_src_simple_get, clk);
69 clk_register_clkdev(clk, clk_name, NULL);
70
71 return;
72
73err_free_gate:
74 kfree(gate);
75err_free_fixed:
76 kfree(fixed);
Emilio Lópeze874a662013-02-25 11:44:26 -030077}
Maxime Ripard81ba6c52013-07-22 18:21:32 +020078CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -030079
80
81
82/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020083 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
Emilio Lópeze874a662013-02-25 11:44:26 -030084 * PLL1 rate is calculated as follows
85 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
86 * parent_rate is always 24Mhz
87 */
88
Maxime Ripard81ba6c52013-07-22 18:21:32 +020089static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -030090 u8 *n, u8 *k, u8 *m, u8 *p)
91{
92 u8 div;
93
94 /* Normalize value to a 6M multiple */
95 div = *freq / 6000000;
96 *freq = 6000000 * div;
97
98 /* we were called to round the frequency, we can now return */
99 if (n == NULL)
100 return;
101
102 /* m is always zero for pll1 */
103 *m = 0;
104
105 /* k is 1 only on these cases */
106 if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
107 *k = 1;
108 else
109 *k = 0;
110
111 /* p will be 3 for divs under 10 */
112 if (div < 10)
113 *p = 3;
114
115 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
116 else if (div < 20 || (div < 32 && (div & 1)))
117 *p = 2;
118
119 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
120 * of divs between 40-62 */
121 else if (div < 40 || (div < 64 && (div & 2)))
122 *p = 1;
123
124 /* any other entries have p = 0 */
125 else
126 *p = 0;
127
128 /* calculate a suitable n based on k and p */
129 div <<= *p;
130 div /= (*k + 1);
131 *n = div / 4;
132}
133
Maxime Ripard6a721db2013-07-23 23:34:10 +0200134/**
135 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
136 * PLL1 rate is calculated as follows
137 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
138 * parent_rate should always be 24MHz
139 */
140static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
141 u8 *n, u8 *k, u8 *m, u8 *p)
142{
143 /*
144 * We can operate only on MHz, this will make our life easier
145 * later.
146 */
147 u32 freq_mhz = *freq / 1000000;
148 u32 parent_freq_mhz = parent_rate / 1000000;
Emilio Lópeze874a662013-02-25 11:44:26 -0300149
Maxime Ripard6a721db2013-07-23 23:34:10 +0200150 /*
151 * Round down the frequency to the closest multiple of either
152 * 6 or 16
153 */
154 u32 round_freq_6 = round_down(freq_mhz, 6);
155 u32 round_freq_16 = round_down(freq_mhz, 16);
156
157 if (round_freq_6 > round_freq_16)
158 freq_mhz = round_freq_6;
159 else
160 freq_mhz = round_freq_16;
161
162 *freq = freq_mhz * 1000000;
163
164 /*
165 * If the factors pointer are null, we were just called to
166 * round down the frequency.
167 * Exit.
168 */
169 if (n == NULL)
170 return;
171
172 /* If the frequency is a multiple of 32 MHz, k is always 3 */
173 if (!(freq_mhz % 32))
174 *k = 3;
175 /* If the frequency is a multiple of 9 MHz, k is always 2 */
176 else if (!(freq_mhz % 9))
177 *k = 2;
178 /* If the frequency is a multiple of 8 MHz, k is always 1 */
179 else if (!(freq_mhz % 8))
180 *k = 1;
181 /* Otherwise, we don't use the k factor */
182 else
183 *k = 0;
184
185 /*
186 * If the frequency is a multiple of 2 but not a multiple of
187 * 3, m is 3. This is the first time we use 6 here, yet we
188 * will use it on several other places.
189 * We use this number because it's the lowest frequency we can
190 * generate (with n = 0, k = 0, m = 3), so every other frequency
191 * somehow relates to this frequency.
192 */
193 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
194 *m = 2;
195 /*
196 * If the frequency is a multiple of 6MHz, but the factor is
197 * odd, m will be 3
198 */
199 else if ((freq_mhz / 6) & 1)
200 *m = 3;
201 /* Otherwise, we end up with m = 1 */
202 else
203 *m = 1;
204
205 /* Calculate n thanks to the above factors we already got */
206 *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
207
208 /*
209 * If n end up being outbound, and that we can still decrease
210 * m, do it.
211 */
212 if ((*n + 1) > 31 && (*m + 1) > 1) {
213 *n = (*n + 1) / 2 - 1;
214 *m = (*m + 1) / 2 - 1;
215 }
216}
Emilio Lópeze874a662013-02-25 11:44:26 -0300217
218/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200219 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
Emilio Lópeze874a662013-02-25 11:44:26 -0300220 * APB1 rate is calculated as follows
221 * rate = (parent_rate >> p) / (m + 1);
222 */
223
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200224static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -0300225 u8 *n, u8 *k, u8 *m, u8 *p)
226{
227 u8 calcm, calcp;
228
229 if (parent_rate < *freq)
230 *freq = parent_rate;
231
232 parent_rate = (parent_rate + (*freq - 1)) / *freq;
233
234 /* Invalid rate! */
235 if (parent_rate > 32)
236 return;
237
238 if (parent_rate <= 4)
239 calcp = 0;
240 else if (parent_rate <= 8)
241 calcp = 1;
242 else if (parent_rate <= 16)
243 calcp = 2;
244 else
245 calcp = 3;
246
247 calcm = (parent_rate >> calcp) - 1;
248
249 *freq = (parent_rate >> calcp) / (calcm + 1);
250
251 /* we were called to round the frequency, we can now return */
252 if (n == NULL)
253 return;
254
255 *m = calcm;
256 *p = calcp;
257}
258
259
260
261/**
262 * sunxi_factors_clk_setup() - Setup function for factor clocks
263 */
264
265struct factors_data {
266 struct clk_factors_config *table;
267 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
268};
269
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200270static struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300271 .nshift = 8,
272 .nwidth = 5,
273 .kshift = 4,
274 .kwidth = 2,
275 .mshift = 0,
276 .mwidth = 2,
277 .pshift = 16,
278 .pwidth = 2,
279};
280
Maxime Ripard6a721db2013-07-23 23:34:10 +0200281static struct clk_factors_config sun6i_a31_pll1_config = {
282 .nshift = 8,
283 .nwidth = 5,
284 .kshift = 4,
285 .kwidth = 2,
286 .mshift = 0,
287 .mwidth = 2,
288};
289
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200290static struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300291 .mshift = 0,
292 .mwidth = 5,
293 .pshift = 16,
294 .pwidth = 2,
295};
296
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530297static const struct factors_data sun4i_pll1_data __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200298 .table = &sun4i_pll1_config,
299 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300300};
301
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530302static const struct factors_data sun6i_a31_pll1_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200303 .table = &sun6i_a31_pll1_config,
304 .getter = sun6i_a31_get_pll1_factors,
305};
306
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530307static const struct factors_data sun4i_apb1_data __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200308 .table = &sun4i_apb1_config,
309 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300310};
311
312static void __init sunxi_factors_clk_setup(struct device_node *node,
313 struct factors_data *data)
314{
315 struct clk *clk;
316 const char *clk_name = node->name;
317 const char *parent;
318 void *reg;
319
320 reg = of_iomap(node, 0);
321
322 parent = of_clk_get_parent_name(node, 0);
323
Emilio López5a4fe9b2013-03-27 18:20:42 -0300324 clk = clk_register_factors(NULL, clk_name, parent, 0, reg,
325 data->table, data->getter, &clk_lock);
Emilio Lópeze874a662013-02-25 11:44:26 -0300326
Axel Linee85e9b2013-07-12 16:15:15 +0800327 if (!IS_ERR(clk)) {
Emilio Lópeze874a662013-02-25 11:44:26 -0300328 of_clk_add_provider(node, of_clk_src_simple_get, clk);
329 clk_register_clkdev(clk, clk_name, NULL);
330 }
331}
332
333
334
335/**
336 * sunxi_mux_clk_setup() - Setup function for muxes
337 */
338
339#define SUNXI_MUX_GATE_WIDTH 2
340
341struct mux_data {
342 u8 shift;
343};
344
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530345static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300346 .shift = 16,
347};
348
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530349static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200350 .shift = 12,
351};
352
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530353static const struct mux_data sun4i_apb1_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300354 .shift = 24,
355};
356
357static void __init sunxi_mux_clk_setup(struct device_node *node,
358 struct mux_data *data)
359{
360 struct clk *clk;
361 const char *clk_name = node->name;
Emilio López918d7f62013-03-27 18:20:43 -0300362 const char *parents[5];
Emilio Lópeze874a662013-02-25 11:44:26 -0300363 void *reg;
364 int i = 0;
365
366 reg = of_iomap(node, 0);
367
368 while (i < 5 && (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
369 i++;
370
James Hogan819c1de2013-07-29 12:25:01 +0100371 clk = clk_register_mux(NULL, clk_name, parents, i,
372 CLK_SET_RATE_NO_REPARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300373 data->shift, SUNXI_MUX_GATE_WIDTH,
374 0, &clk_lock);
375
376 if (clk) {
377 of_clk_add_provider(node, of_clk_src_simple_get, clk);
378 clk_register_clkdev(clk, clk_name, NULL);
379 }
380}
381
382
383
384/**
385 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
386 */
387
Emilio Lópeze874a662013-02-25 11:44:26 -0300388struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200389 u8 shift;
390 u8 pow;
391 u8 width;
Emilio Lópeze874a662013-02-25 11:44:26 -0300392};
393
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530394static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200395 .shift = 0,
396 .pow = 0,
397 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300398};
399
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530400static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200401 .shift = 4,
402 .pow = 1,
403 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300404};
405
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530406static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200407 .shift = 8,
408 .pow = 1,
409 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300410};
411
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530412static const struct div_data sun6i_a31_apb2_div_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200413 .shift = 0,
414 .pow = 0,
415 .width = 4,
416};
417
Emilio Lópeze874a662013-02-25 11:44:26 -0300418static void __init sunxi_divider_clk_setup(struct device_node *node,
419 struct div_data *data)
420{
421 struct clk *clk;
422 const char *clk_name = node->name;
423 const char *clk_parent;
424 void *reg;
425
426 reg = of_iomap(node, 0);
427
428 clk_parent = of_clk_get_parent_name(node, 0);
429
430 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
Maxime Ripard70855bb2013-07-23 09:25:56 +0200431 reg, data->shift, data->width,
Emilio Lópeze874a662013-02-25 11:44:26 -0300432 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
433 &clk_lock);
434 if (clk) {
435 of_clk_add_provider(node, of_clk_src_simple_get, clk);
436 clk_register_clkdev(clk, clk_name, NULL);
437 }
438}
439
440
Emilio López13569a72013-03-27 18:20:37 -0300441
442/**
443 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
444 */
445
446#define SUNXI_GATES_MAX_SIZE 64
447
448struct gates_data {
449 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
450};
451
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530452static const struct gates_data sun4i_axi_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300453 .mask = {1},
454};
455
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530456static const struct gates_data sun4i_ahb_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300457 .mask = {0x7F77FFF, 0x14FB3F},
458};
459
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530460static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200461 .mask = {0x147667e7, 0x185915},
462};
463
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530464static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200465 .mask = {0x107067e7, 0x185111},
466};
467
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530468static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200469 .mask = {0xEDFE7F62, 0x794F931},
470};
471
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530472static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200473 .mask = { 0x12f77fff, 0x16ff3f },
474};
475
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530476static const struct gates_data sun4i_apb0_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300477 .mask = {0x4EF},
478};
479
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530480static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200481 .mask = {0x469},
482};
483
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530484static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200485 .mask = {0x61},
486};
487
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530488static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200489 .mask = { 0x4ff },
490};
491
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530492static const struct gates_data sun4i_apb1_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300493 .mask = {0xFF00F7},
494};
495
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530496static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200497 .mask = {0xf0007},
498};
499
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530500static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200501 .mask = {0xa0007},
502};
503
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530504static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200505 .mask = {0x3031},
506};
507
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530508static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200509 .mask = {0x3F000F},
510};
511
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530512static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200513 .mask = { 0xff80ff },
514};
515
Emilio López13569a72013-03-27 18:20:37 -0300516static void __init sunxi_gates_clk_setup(struct device_node *node,
517 struct gates_data *data)
518{
519 struct clk_onecell_data *clk_data;
520 const char *clk_parent;
521 const char *clk_name;
522 void *reg;
523 int qty;
524 int i = 0;
525 int j = 0;
526 int ignore;
527
528 reg = of_iomap(node, 0);
529
530 clk_parent = of_clk_get_parent_name(node, 0);
531
532 /* Worst-case size approximation and memory allocation */
533 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
534 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
535 if (!clk_data)
536 return;
537 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
538 if (!clk_data->clks) {
539 kfree(clk_data);
540 return;
541 }
542
543 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
544 of_property_read_string_index(node, "clock-output-names",
545 j, &clk_name);
546
547 /* No driver claims this clock, but it should remain gated */
548 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
549
550 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
551 clk_parent, ignore,
552 reg + 4 * (i/32), i % 32,
553 0, &clk_lock);
554 WARN_ON(IS_ERR(clk_data->clks[i]));
555
556 j++;
557 }
558
559 /* Adjust to the real max */
560 clk_data->clk_num = i;
561
562 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
563}
564
Emilio Lópeze874a662013-02-25 11:44:26 -0300565/* Matches for factors clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530566static const struct of_device_id clk_factors_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200567 {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200568 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200569 {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300570 {}
571};
572
573/* Matches for divider clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530574static const struct of_device_id clk_div_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200575 {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
576 {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
577 {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200578 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300579 {}
580};
581
582/* Matches for mux clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530583static const struct of_device_id clk_mux_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200584 {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
585 {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200586 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300587 {}
588};
589
Emilio López13569a72013-03-27 18:20:37 -0300590/* Matches for gate clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530591static const struct of_device_id clk_gates_match[] __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200592 {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
593 {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200594 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200595 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200596 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200597 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200598 {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200599 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200600 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200601 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200602 {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200603 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200604 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200605 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200606 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200607 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -0300608 {}
609};
610
Emilio Lópeze874a662013-02-25 11:44:26 -0300611static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
612 void *function)
613{
614 struct device_node *np;
615 const struct div_data *data;
616 const struct of_device_id *match;
617 void (*setup_function)(struct device_node *, const void *) = function;
618
619 for_each_matching_node(np, clk_match) {
620 match = of_match_node(clk_match, np);
621 data = match->data;
622 setup_function(np, data);
623 }
624}
625
Emilio López8e6a4c42013-09-20 22:03:12 -0300626/**
627 * System clock protection
628 *
629 * By enabling these critical clocks, we prevent their accidental gating
630 * by the framework
631 */
632static void __init sunxi_clock_protect(void)
633{
634 struct clk *clk;
635
636 /* memory bus clock - sun5i+ */
637 clk = clk_get(NULL, "mbus");
638 if (!IS_ERR(clk)) {
639 clk_prepare_enable(clk);
640 clk_put(clk);
641 }
642
643 /* DDR clock - sun4i+ */
644 clk = clk_get(NULL, "pll5_ddr");
645 if (!IS_ERR(clk)) {
646 clk_prepare_enable(clk);
647 clk_put(clk);
648 }
649}
650
Emilio Lópeze874a662013-02-25 11:44:26 -0300651void __init sunxi_init_clocks(void)
652{
Emilio López431807f2013-07-22 22:01:05 -0300653 /* Register all the simple and basic clocks on DT */
654 of_clk_init(NULL);
Emilio Lópeze874a662013-02-25 11:44:26 -0300655
656 /* Register factor clocks */
657 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
658
659 /* Register divider clocks */
660 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
661
662 /* Register mux clocks */
663 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -0300664
665 /* Register gate clocks */
666 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio López8e6a4c42013-09-20 22:03:12 -0300667
668 /* Enable core system clocks */
669 sunxi_clock_protect();
Emilio Lópeze874a662013-02-25 11:44:26 -0300670}