blob: e90df083a36dc57d170384276adfec3133e50996 [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
Emilio López40a5dcb2013-12-23 00:32:32 -030026/* Maximum number of parents our clocks have */
27#define SUNXI_MAX_PARENTS 5
28
Emilio Lópeze874a662013-02-25 11:44:26 -030029/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020030 * sun4i_osc_clk_setup() - Setup function for gatable oscillator
Emilio Lópeze874a662013-02-25 11:44:26 -030031 */
32
33#define SUNXI_OSC24M_GATE 0
34
Maxime Ripard81ba6c52013-07-22 18:21:32 +020035static void __init sun4i_osc_clk_setup(struct device_node *node)
Emilio Lópeze874a662013-02-25 11:44:26 -030036{
37 struct clk *clk;
Emilio López38e4aa02013-04-10 15:02:57 -070038 struct clk_fixed_rate *fixed;
39 struct clk_gate *gate;
Emilio Lópeze874a662013-02-25 11:44:26 -030040 const char *clk_name = node->name;
Emilio López38e4aa02013-04-10 15:02:57 -070041 u32 rate;
Emilio Lópeze874a662013-02-25 11:44:26 -030042
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030043 if (of_property_read_u32(node, "clock-frequency", &rate))
44 return;
45
Emilio López38e4aa02013-04-10 15:02:57 -070046 /* allocate fixed-rate and gate clock structs */
47 fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
48 if (!fixed)
49 return;
50 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030051 if (!gate)
52 goto err_free_fixed;
Emilio Lópeze874a662013-02-25 11:44:26 -030053
Emilio López38e4aa02013-04-10 15:02:57 -070054 /* set up gate and fixed rate properties */
55 gate->reg = of_iomap(node, 0);
56 gate->bit_idx = SUNXI_OSC24M_GATE;
57 gate->lock = &clk_lock;
58 fixed->fixed_rate = rate;
59
60 clk = clk_register_composite(NULL, clk_name,
61 NULL, 0,
62 NULL, NULL,
63 &fixed->hw, &clk_fixed_rate_ops,
64 &gate->hw, &clk_gate_ops,
65 CLK_IS_ROOT);
Emilio Lópeze874a662013-02-25 11:44:26 -030066
Victor N. Ramos Melloe71c69f2013-10-18 20:27:51 -030067 if (IS_ERR(clk))
68 goto err_free_gate;
69
70 of_clk_add_provider(node, of_clk_src_simple_get, clk);
71 clk_register_clkdev(clk, clk_name, NULL);
72
73 return;
74
75err_free_gate:
76 kfree(gate);
77err_free_fixed:
78 kfree(fixed);
Emilio Lópeze874a662013-02-25 11:44:26 -030079}
Maxime Ripard81ba6c52013-07-22 18:21:32 +020080CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -030081
82
83
84/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020085 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
Emilio Lópeze874a662013-02-25 11:44:26 -030086 * PLL1 rate is calculated as follows
87 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
88 * parent_rate is always 24Mhz
89 */
90
Maxime Ripard81ba6c52013-07-22 18:21:32 +020091static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -030092 u8 *n, u8 *k, u8 *m, u8 *p)
93{
94 u8 div;
95
96 /* Normalize value to a 6M multiple */
97 div = *freq / 6000000;
98 *freq = 6000000 * div;
99
100 /* we were called to round the frequency, we can now return */
101 if (n == NULL)
102 return;
103
104 /* m is always zero for pll1 */
105 *m = 0;
106
107 /* k is 1 only on these cases */
108 if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
109 *k = 1;
110 else
111 *k = 0;
112
113 /* p will be 3 for divs under 10 */
114 if (div < 10)
115 *p = 3;
116
117 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
118 else if (div < 20 || (div < 32 && (div & 1)))
119 *p = 2;
120
121 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
122 * of divs between 40-62 */
123 else if (div < 40 || (div < 64 && (div & 2)))
124 *p = 1;
125
126 /* any other entries have p = 0 */
127 else
128 *p = 0;
129
130 /* calculate a suitable n based on k and p */
131 div <<= *p;
132 div /= (*k + 1);
133 *n = div / 4;
134}
135
Maxime Ripard6a721db2013-07-23 23:34:10 +0200136/**
137 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
138 * PLL1 rate is calculated as follows
139 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
140 * parent_rate should always be 24MHz
141 */
142static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
143 u8 *n, u8 *k, u8 *m, u8 *p)
144{
145 /*
146 * We can operate only on MHz, this will make our life easier
147 * later.
148 */
149 u32 freq_mhz = *freq / 1000000;
150 u32 parent_freq_mhz = parent_rate / 1000000;
Emilio Lópeze874a662013-02-25 11:44:26 -0300151
Maxime Ripard6a721db2013-07-23 23:34:10 +0200152 /*
153 * Round down the frequency to the closest multiple of either
154 * 6 or 16
155 */
156 u32 round_freq_6 = round_down(freq_mhz, 6);
157 u32 round_freq_16 = round_down(freq_mhz, 16);
158
159 if (round_freq_6 > round_freq_16)
160 freq_mhz = round_freq_6;
161 else
162 freq_mhz = round_freq_16;
163
164 *freq = freq_mhz * 1000000;
165
166 /*
167 * If the factors pointer are null, we were just called to
168 * round down the frequency.
169 * Exit.
170 */
171 if (n == NULL)
172 return;
173
174 /* If the frequency is a multiple of 32 MHz, k is always 3 */
175 if (!(freq_mhz % 32))
176 *k = 3;
177 /* If the frequency is a multiple of 9 MHz, k is always 2 */
178 else if (!(freq_mhz % 9))
179 *k = 2;
180 /* If the frequency is a multiple of 8 MHz, k is always 1 */
181 else if (!(freq_mhz % 8))
182 *k = 1;
183 /* Otherwise, we don't use the k factor */
184 else
185 *k = 0;
186
187 /*
188 * If the frequency is a multiple of 2 but not a multiple of
189 * 3, m is 3. This is the first time we use 6 here, yet we
190 * will use it on several other places.
191 * We use this number because it's the lowest frequency we can
192 * generate (with n = 0, k = 0, m = 3), so every other frequency
193 * somehow relates to this frequency.
194 */
195 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
196 *m = 2;
197 /*
198 * If the frequency is a multiple of 6MHz, but the factor is
199 * odd, m will be 3
200 */
201 else if ((freq_mhz / 6) & 1)
202 *m = 3;
203 /* Otherwise, we end up with m = 1 */
204 else
205 *m = 1;
206
207 /* Calculate n thanks to the above factors we already got */
208 *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
209
210 /*
211 * If n end up being outbound, and that we can still decrease
212 * m, do it.
213 */
214 if ((*n + 1) > 31 && (*m + 1) > 1) {
215 *n = (*n + 1) / 2 - 1;
216 *m = (*m + 1) / 2 - 1;
217 }
218}
Emilio Lópeze874a662013-02-25 11:44:26 -0300219
220/**
Emilio Lópezd584c132013-12-23 00:32:37 -0300221 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
222 * PLL5 rate is calculated as follows
223 * rate = parent_rate * n * (k + 1)
224 * parent_rate is always 24Mhz
225 */
226
227static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate,
228 u8 *n, u8 *k, u8 *m, u8 *p)
229{
230 u8 div;
231
232 /* Normalize value to a parent_rate multiple (24M) */
233 div = *freq / parent_rate;
234 *freq = parent_rate * div;
235
236 /* we were called to round the frequency, we can now return */
237 if (n == NULL)
238 return;
239
240 if (div < 31)
241 *k = 0;
242 else if (div / 2 < 31)
243 *k = 1;
244 else if (div / 3 < 31)
245 *k = 2;
246 else
247 *k = 3;
248
249 *n = DIV_ROUND_UP(div, (*k+1));
250}
251
252
253
254/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200255 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
Emilio Lópeze874a662013-02-25 11:44:26 -0300256 * APB1 rate is calculated as follows
257 * rate = (parent_rate >> p) / (m + 1);
258 */
259
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200260static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -0300261 u8 *n, u8 *k, u8 *m, u8 *p)
262{
263 u8 calcm, calcp;
264
265 if (parent_rate < *freq)
266 *freq = parent_rate;
267
268 parent_rate = (parent_rate + (*freq - 1)) / *freq;
269
270 /* Invalid rate! */
271 if (parent_rate > 32)
272 return;
273
274 if (parent_rate <= 4)
275 calcp = 0;
276 else if (parent_rate <= 8)
277 calcp = 1;
278 else if (parent_rate <= 16)
279 calcp = 2;
280 else
281 calcp = 3;
282
283 calcm = (parent_rate >> calcp) - 1;
284
285 *freq = (parent_rate >> calcp) / (calcm + 1);
286
287 /* we were called to round the frequency, we can now return */
288 if (n == NULL)
289 return;
290
291 *m = calcm;
292 *p = calcp;
293}
294
295
296
297/**
298 * sunxi_factors_clk_setup() - Setup function for factor clocks
299 */
300
Emilio López40a5dcb2013-12-23 00:32:32 -0300301#define SUNXI_FACTORS_MUX_MASK 0x3
302
Emilio Lópeze874a662013-02-25 11:44:26 -0300303struct factors_data {
Emilio López40a5dcb2013-12-23 00:32:32 -0300304 int enable;
305 int mux;
Emilio Lópeze874a662013-02-25 11:44:26 -0300306 struct clk_factors_config *table;
307 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
308};
309
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200310static struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300311 .nshift = 8,
312 .nwidth = 5,
313 .kshift = 4,
314 .kwidth = 2,
315 .mshift = 0,
316 .mwidth = 2,
317 .pshift = 16,
318 .pwidth = 2,
319};
320
Maxime Ripard6a721db2013-07-23 23:34:10 +0200321static struct clk_factors_config sun6i_a31_pll1_config = {
322 .nshift = 8,
323 .nwidth = 5,
324 .kshift = 4,
325 .kwidth = 2,
326 .mshift = 0,
327 .mwidth = 2,
328};
329
Emilio Lópezd584c132013-12-23 00:32:37 -0300330static struct clk_factors_config sun4i_pll5_config = {
331 .nshift = 8,
332 .nwidth = 5,
333 .kshift = 4,
334 .kwidth = 2,
335};
336
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200337static struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300338 .mshift = 0,
339 .mwidth = 5,
340 .pshift = 16,
341 .pwidth = 2,
342};
343
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530344static const struct factors_data sun4i_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300345 .enable = 31,
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200346 .table = &sun4i_pll1_config,
347 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300348};
349
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530350static const struct factors_data sun6i_a31_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300351 .enable = 31,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200352 .table = &sun6i_a31_pll1_config,
353 .getter = sun6i_a31_get_pll1_factors,
354};
355
Emilio Lópezd584c132013-12-23 00:32:37 -0300356static const struct factors_data sun4i_pll5_data __initconst = {
357 .enable = 31,
358 .table = &sun4i_pll5_config,
359 .getter = sun4i_get_pll5_factors,
360};
361
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530362static const struct factors_data sun4i_apb1_data __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200363 .table = &sun4i_apb1_config,
364 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300365};
366
Emilio López5f4e0be2013-12-23 00:32:36 -0300367static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
368 const struct factors_data *data)
Emilio Lópeze874a662013-02-25 11:44:26 -0300369{
370 struct clk *clk;
Emilio López40a5dcb2013-12-23 00:32:32 -0300371 struct clk_factors *factors;
372 struct clk_gate *gate = NULL;
373 struct clk_mux *mux = NULL;
374 struct clk_hw *gate_hw = NULL;
375 struct clk_hw *mux_hw = NULL;
Emilio Lópeze874a662013-02-25 11:44:26 -0300376 const char *clk_name = node->name;
Emilio López40a5dcb2013-12-23 00:32:32 -0300377 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300378 void *reg;
Emilio López40a5dcb2013-12-23 00:32:32 -0300379 int i = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -0300380
381 reg = of_iomap(node, 0);
382
Emilio López40a5dcb2013-12-23 00:32:32 -0300383 /* if we have a mux, we will have >1 parents */
384 while (i < SUNXI_MAX_PARENTS &&
385 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
386 i++;
Emilio Lópeze874a662013-02-25 11:44:26 -0300387
Emilio López40a5dcb2013-12-23 00:32:32 -0300388 factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
389 if (!factors)
Emilio López5f4e0be2013-12-23 00:32:36 -0300390 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300391
392 /* Add a gate if this factor clock can be gated */
393 if (data->enable) {
394 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
395 if (!gate) {
396 kfree(factors);
Emilio López5f4e0be2013-12-23 00:32:36 -0300397 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300398 }
399
400 /* set up gate properties */
401 gate->reg = reg;
402 gate->bit_idx = data->enable;
403 gate->lock = &clk_lock;
404 gate_hw = &gate->hw;
405 }
406
407 /* Add a mux if this factor clock can be muxed */
408 if (data->mux) {
409 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
410 if (!mux) {
411 kfree(factors);
412 kfree(gate);
Emilio López5f4e0be2013-12-23 00:32:36 -0300413 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300414 }
415
416 /* set up gate properties */
417 mux->reg = reg;
418 mux->shift = data->mux;
419 mux->mask = SUNXI_FACTORS_MUX_MASK;
420 mux->lock = &clk_lock;
421 mux_hw = &mux->hw;
422 }
423
424 /* set up factors properties */
425 factors->reg = reg;
426 factors->config = data->table;
427 factors->get_factors = data->getter;
428 factors->lock = &clk_lock;
429
430 clk = clk_register_composite(NULL, clk_name,
431 parents, i,
432 mux_hw, &clk_mux_ops,
433 &factors->hw, &clk_factors_ops,
Emilio López5f4e0be2013-12-23 00:32:36 -0300434 gate_hw, &clk_gate_ops, 0);
Emilio Lópeze874a662013-02-25 11:44:26 -0300435
Axel Linee85e9b2013-07-12 16:15:15 +0800436 if (!IS_ERR(clk)) {
Emilio Lópeze874a662013-02-25 11:44:26 -0300437 of_clk_add_provider(node, of_clk_src_simple_get, clk);
438 clk_register_clkdev(clk, clk_name, NULL);
439 }
Emilio López5f4e0be2013-12-23 00:32:36 -0300440
441 return clk;
Emilio Lópeze874a662013-02-25 11:44:26 -0300442}
443
444
445
446/**
447 * sunxi_mux_clk_setup() - Setup function for muxes
448 */
449
450#define SUNXI_MUX_GATE_WIDTH 2
451
452struct mux_data {
453 u8 shift;
454};
455
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530456static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300457 .shift = 16,
458};
459
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530460static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200461 .shift = 12,
462};
463
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530464static const struct mux_data sun4i_apb1_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300465 .shift = 24,
466};
467
468static void __init sunxi_mux_clk_setup(struct device_node *node,
469 struct mux_data *data)
470{
471 struct clk *clk;
472 const char *clk_name = node->name;
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300473 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300474 void *reg;
475 int i = 0;
476
477 reg = of_iomap(node, 0);
478
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300479 while (i < SUNXI_MAX_PARENTS &&
480 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
Emilio Lópeze874a662013-02-25 11:44:26 -0300481 i++;
482
James Hogan819c1de2013-07-29 12:25:01 +0100483 clk = clk_register_mux(NULL, clk_name, parents, i,
484 CLK_SET_RATE_NO_REPARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300485 data->shift, SUNXI_MUX_GATE_WIDTH,
486 0, &clk_lock);
487
488 if (clk) {
489 of_clk_add_provider(node, of_clk_src_simple_get, clk);
490 clk_register_clkdev(clk, clk_name, NULL);
491 }
492}
493
494
495
496/**
497 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
498 */
499
Emilio Lópeze874a662013-02-25 11:44:26 -0300500struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200501 u8 shift;
502 u8 pow;
503 u8 width;
Emilio Lópeze874a662013-02-25 11:44:26 -0300504};
505
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530506static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200507 .shift = 0,
508 .pow = 0,
509 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300510};
511
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530512static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200513 .shift = 4,
514 .pow = 1,
515 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300516};
517
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530518static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200519 .shift = 8,
520 .pow = 1,
521 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300522};
523
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530524static const struct div_data sun6i_a31_apb2_div_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200525 .shift = 0,
526 .pow = 0,
527 .width = 4,
528};
529
Emilio Lópeze874a662013-02-25 11:44:26 -0300530static void __init sunxi_divider_clk_setup(struct device_node *node,
531 struct div_data *data)
532{
533 struct clk *clk;
534 const char *clk_name = node->name;
535 const char *clk_parent;
536 void *reg;
537
538 reg = of_iomap(node, 0);
539
540 clk_parent = of_clk_get_parent_name(node, 0);
541
542 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
Maxime Ripard70855bb2013-07-23 09:25:56 +0200543 reg, data->shift, data->width,
Emilio Lópeze874a662013-02-25 11:44:26 -0300544 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
545 &clk_lock);
546 if (clk) {
547 of_clk_add_provider(node, of_clk_src_simple_get, clk);
548 clk_register_clkdev(clk, clk_name, NULL);
549 }
550}
551
552
Emilio López13569a72013-03-27 18:20:37 -0300553
554/**
555 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
556 */
557
558#define SUNXI_GATES_MAX_SIZE 64
559
560struct gates_data {
561 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
562};
563
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530564static const struct gates_data sun4i_axi_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300565 .mask = {1},
566};
567
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530568static const struct gates_data sun4i_ahb_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300569 .mask = {0x7F77FFF, 0x14FB3F},
570};
571
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530572static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200573 .mask = {0x147667e7, 0x185915},
574};
575
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530576static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200577 .mask = {0x107067e7, 0x185111},
578};
579
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530580static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200581 .mask = {0xEDFE7F62, 0x794F931},
582};
583
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530584static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200585 .mask = { 0x12f77fff, 0x16ff3f },
586};
587
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530588static const struct gates_data sun4i_apb0_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300589 .mask = {0x4EF},
590};
591
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530592static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200593 .mask = {0x469},
594};
595
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530596static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200597 .mask = {0x61},
598};
599
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530600static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200601 .mask = { 0x4ff },
602};
603
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530604static const struct gates_data sun4i_apb1_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300605 .mask = {0xFF00F7},
606};
607
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530608static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200609 .mask = {0xf0007},
610};
611
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530612static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200613 .mask = {0xa0007},
614};
615
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530616static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200617 .mask = {0x3031},
618};
619
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530620static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200621 .mask = {0x3F000F},
622};
623
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530624static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200625 .mask = { 0xff80ff },
626};
627
Emilio López13569a72013-03-27 18:20:37 -0300628static void __init sunxi_gates_clk_setup(struct device_node *node,
629 struct gates_data *data)
630{
631 struct clk_onecell_data *clk_data;
632 const char *clk_parent;
633 const char *clk_name;
634 void *reg;
635 int qty;
636 int i = 0;
637 int j = 0;
638 int ignore;
639
640 reg = of_iomap(node, 0);
641
642 clk_parent = of_clk_get_parent_name(node, 0);
643
644 /* Worst-case size approximation and memory allocation */
645 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
646 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
647 if (!clk_data)
648 return;
649 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
650 if (!clk_data->clks) {
651 kfree(clk_data);
652 return;
653 }
654
655 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
656 of_property_read_string_index(node, "clock-output-names",
657 j, &clk_name);
658
659 /* No driver claims this clock, but it should remain gated */
660 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
661
662 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
663 clk_parent, ignore,
664 reg + 4 * (i/32), i % 32,
665 0, &clk_lock);
666 WARN_ON(IS_ERR(clk_data->clks[i]));
667
668 j++;
669 }
670
671 /* Adjust to the real max */
672 clk_data->clk_num = i;
673
674 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
675}
676
Emilio Lópezd584c132013-12-23 00:32:37 -0300677
678
679/**
680 * sunxi_divs_clk_setup() helper data
681 */
682
683#define SUNXI_DIVS_MAX_QTY 2
684#define SUNXI_DIVISOR_WIDTH 2
685
686struct divs_data {
687 const struct factors_data *factors; /* data for the factor clock */
688 struct {
689 u8 fixed; /* is it a fixed divisor? if not... */
690 struct clk_div_table *table; /* is it a table based divisor? */
691 u8 shift; /* otherwise it's a normal divisor with this shift */
692 u8 pow; /* is it power-of-two based? */
693 u8 gate; /* is it independently gateable? */
694 } div[SUNXI_DIVS_MAX_QTY];
695};
696
697static struct clk_div_table pll6_sata_tbl[] = {
698 { .val = 0, .div = 6, },
699 { .val = 1, .div = 12, },
700 { .val = 2, .div = 18, },
701 { .val = 3, .div = 24, },
702 { } /* sentinel */
703};
704
705static const struct divs_data pll5_divs_data __initconst = {
706 .factors = &sun4i_pll5_data,
707 .div = {
708 { .shift = 0, .pow = 0, }, /* M, DDR */
709 { .shift = 16, .pow = 1, }, /* P, other */
710 }
711};
712
713static const struct divs_data pll6_divs_data __initconst = {
714 .factors = &sun4i_pll5_data,
715 .div = {
716 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
717 { .fixed = 2 }, /* P, other */
718 }
719};
720
721/**
722 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
723 *
724 * These clocks look something like this
725 * ________________________
726 * | ___divisor 1---|----> to consumer
727 * parent >--| pll___/___divisor 2---|----> to consumer
728 * | \_______________|____> to consumer
729 * |________________________|
730 */
731
732static void __init sunxi_divs_clk_setup(struct device_node *node,
733 struct divs_data *data)
734{
735 struct clk_onecell_data *clk_data;
736 const char *parent = node->name;
737 const char *clk_name;
738 struct clk **clks, *pclk;
739 struct clk_hw *gate_hw, *rate_hw;
740 const struct clk_ops *rate_ops;
741 struct clk_gate *gate = NULL;
742 struct clk_fixed_factor *fix_factor;
743 struct clk_divider *divider;
744 void *reg;
745 int i = 0;
746 int flags, clkflags;
747
748 /* Set up factor clock that we will be dividing */
749 pclk = sunxi_factors_clk_setup(node, data->factors);
750
751 reg = of_iomap(node, 0);
752
753 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
754 if (!clk_data)
755 return;
756
757 clks = kzalloc(SUNXI_DIVS_MAX_QTY * sizeof(struct clk *), GFP_KERNEL);
758 if (!clks)
759 goto free_clkdata;
760
761 clk_data->clks = clks;
762
763 /* It's not a good idea to have automatic reparenting changing
764 * our RAM clock! */
765 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
766
767 for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
768 if (of_property_read_string_index(node, "clock-output-names",
769 i, &clk_name) != 0)
770 break;
771
772 gate_hw = NULL;
773 rate_hw = NULL;
774 rate_ops = NULL;
775
776 /* If this leaf clock can be gated, create a gate */
777 if (data->div[i].gate) {
778 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
779 if (!gate)
780 goto free_clks;
781
782 gate->reg = reg;
783 gate->bit_idx = data->div[i].gate;
784 gate->lock = &clk_lock;
785
786 gate_hw = &gate->hw;
787 }
788
789 /* Leaves can be fixed or configurable divisors */
790 if (data->div[i].fixed) {
791 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
792 if (!fix_factor)
793 goto free_gate;
794
795 fix_factor->mult = 1;
796 fix_factor->div = data->div[i].fixed;
797
798 rate_hw = &fix_factor->hw;
799 rate_ops = &clk_fixed_factor_ops;
800 } else {
801 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
802 if (!divider)
803 goto free_gate;
804
805 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
806
807 divider->reg = reg;
808 divider->shift = data->div[i].shift;
809 divider->width = SUNXI_DIVISOR_WIDTH;
810 divider->flags = flags;
811 divider->lock = &clk_lock;
812 divider->table = data->div[i].table;
813
814 rate_hw = &divider->hw;
815 rate_ops = &clk_divider_ops;
816 }
817
818 /* Wrap the (potential) gate and the divisor on a composite
819 * clock to unify them */
820 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
821 NULL, NULL,
822 rate_hw, rate_ops,
823 gate_hw, &clk_gate_ops,
824 clkflags);
825
826 WARN_ON(IS_ERR(clk_data->clks[i]));
827 clk_register_clkdev(clks[i], clk_name, NULL);
828 }
829
830 /* The last clock available on the getter is the parent */
831 clks[i++] = pclk;
832
833 /* Adjust to the real max */
834 clk_data->clk_num = i;
835
836 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
837
838 return;
839
840free_gate:
841 kfree(gate);
842free_clks:
843 kfree(clks);
844free_clkdata:
845 kfree(clk_data);
846}
847
848
849
Emilio Lópeze874a662013-02-25 11:44:26 -0300850/* Matches for factors clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530851static const struct of_device_id clk_factors_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200852 {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200853 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200854 {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300855 {}
856};
857
858/* Matches for divider clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530859static const struct of_device_id clk_div_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200860 {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
861 {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
862 {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200863 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300864 {}
865};
866
Emilio Lópezd584c132013-12-23 00:32:37 -0300867/* Matches for divided outputs */
868static const struct of_device_id clk_divs_match[] __initconst = {
869 {.compatible = "allwinner,sun4i-pll5-clk", .data = &pll5_divs_data,},
870 {.compatible = "allwinner,sun4i-pll6-clk", .data = &pll6_divs_data,},
871 {}
872};
873
Emilio Lópeze874a662013-02-25 11:44:26 -0300874/* Matches for mux clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530875static const struct of_device_id clk_mux_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200876 {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
877 {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200878 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300879 {}
880};
881
Emilio López13569a72013-03-27 18:20:37 -0300882/* Matches for gate clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530883static const struct of_device_id clk_gates_match[] __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200884 {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
885 {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200886 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200887 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200888 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200889 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200890 {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200891 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200892 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200893 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200894 {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200895 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200896 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200897 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200898 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200899 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -0300900 {}
901};
902
Emilio Lópeze874a662013-02-25 11:44:26 -0300903static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
904 void *function)
905{
906 struct device_node *np;
907 const struct div_data *data;
908 const struct of_device_id *match;
909 void (*setup_function)(struct device_node *, const void *) = function;
910
911 for_each_matching_node(np, clk_match) {
912 match = of_match_node(clk_match, np);
913 data = match->data;
914 setup_function(np, data);
915 }
916}
917
Emilio López8e6a4c42013-09-20 22:03:12 -0300918/**
919 * System clock protection
920 *
921 * By enabling these critical clocks, we prevent their accidental gating
922 * by the framework
923 */
924static void __init sunxi_clock_protect(void)
925{
926 struct clk *clk;
927
928 /* memory bus clock - sun5i+ */
929 clk = clk_get(NULL, "mbus");
930 if (!IS_ERR(clk)) {
931 clk_prepare_enable(clk);
932 clk_put(clk);
933 }
934
935 /* DDR clock - sun4i+ */
936 clk = clk_get(NULL, "pll5_ddr");
937 if (!IS_ERR(clk)) {
938 clk_prepare_enable(clk);
939 clk_put(clk);
940 }
941}
942
Mike Turquette1d9438f2013-12-01 12:42:45 -0800943static void __init sunxi_init_clocks(void)
Emilio Lópeze874a662013-02-25 11:44:26 -0300944{
Emilio Lópeze874a662013-02-25 11:44:26 -0300945 /* Register factor clocks */
946 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
947
948 /* Register divider clocks */
949 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
950
Emilio Lópezd584c132013-12-23 00:32:37 -0300951 /* Register divided output clocks */
952 of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
953
Emilio Lópeze874a662013-02-25 11:44:26 -0300954 /* Register mux clocks */
955 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -0300956
957 /* Register gate clocks */
958 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio López8e6a4c42013-09-20 22:03:12 -0300959
960 /* Enable core system clocks */
961 sunxi_clock_protect();
Emilio Lópeze874a662013-02-25 11:44:26 -0300962}
Sebastian Hesselbarthbe080452013-09-06 14:59:57 +0200963CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sunxi_init_clocks);
964CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sunxi_init_clocks);
965CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sunxi_init_clocks);
966CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sunxi_init_clocks);
967CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sunxi_init_clocks);