blob: 124113e33f06e3b5e3817c7fcdfde24af08dc11e [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/**
Emilio López75517692013-12-23 00:32:39 -0300298 * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
299 * MMC rate is calculated as follows
300 * rate = (parent_rate >> p) / (m + 1);
301 */
302
303static void sun4i_get_mod0_factors(u32 *freq, u32 parent_rate,
304 u8 *n, u8 *k, u8 *m, u8 *p)
305{
306 u8 div, calcm, calcp;
307
308 /* These clocks can only divide, so we will never be able to achieve
309 * frequencies higher than the parent frequency */
310 if (*freq > parent_rate)
311 *freq = parent_rate;
312
313 div = parent_rate / *freq;
314
315 if (div < 16)
316 calcp = 0;
317 else if (div / 2 < 16)
318 calcp = 1;
319 else if (div / 4 < 16)
320 calcp = 2;
321 else
322 calcp = 3;
323
324 calcm = DIV_ROUND_UP(div, 1 << calcp);
325
326 *freq = (parent_rate >> calcp) / calcm;
327
328 /* we were called to round the frequency, we can now return */
329 if (n == NULL)
330 return;
331
332 *m = calcm - 1;
333 *p = calcp;
334}
335
336
337
338/**
Emilio Lópeze874a662013-02-25 11:44:26 -0300339 * sunxi_factors_clk_setup() - Setup function for factor clocks
340 */
341
Emilio López40a5dcb2013-12-23 00:32:32 -0300342#define SUNXI_FACTORS_MUX_MASK 0x3
343
Emilio Lópeze874a662013-02-25 11:44:26 -0300344struct factors_data {
Emilio López40a5dcb2013-12-23 00:32:32 -0300345 int enable;
346 int mux;
Emilio Lópeze874a662013-02-25 11:44:26 -0300347 struct clk_factors_config *table;
348 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
349};
350
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200351static struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300352 .nshift = 8,
353 .nwidth = 5,
354 .kshift = 4,
355 .kwidth = 2,
356 .mshift = 0,
357 .mwidth = 2,
358 .pshift = 16,
359 .pwidth = 2,
360};
361
Maxime Ripard6a721db2013-07-23 23:34:10 +0200362static struct clk_factors_config sun6i_a31_pll1_config = {
363 .nshift = 8,
364 .nwidth = 5,
365 .kshift = 4,
366 .kwidth = 2,
367 .mshift = 0,
368 .mwidth = 2,
369};
370
Emilio Lópezd584c132013-12-23 00:32:37 -0300371static struct clk_factors_config sun4i_pll5_config = {
372 .nshift = 8,
373 .nwidth = 5,
374 .kshift = 4,
375 .kwidth = 2,
376};
377
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200378static struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300379 .mshift = 0,
380 .mwidth = 5,
381 .pshift = 16,
382 .pwidth = 2,
383};
384
Emilio López75517692013-12-23 00:32:39 -0300385/* user manual says "n" but it's really "p" */
386static struct clk_factors_config sun4i_mod0_config = {
387 .mshift = 0,
388 .mwidth = 4,
389 .pshift = 16,
390 .pwidth = 2,
391};
392
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530393static const struct factors_data sun4i_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300394 .enable = 31,
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200395 .table = &sun4i_pll1_config,
396 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300397};
398
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530399static const struct factors_data sun6i_a31_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300400 .enable = 31,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200401 .table = &sun6i_a31_pll1_config,
402 .getter = sun6i_a31_get_pll1_factors,
403};
404
Emilio Lópezd584c132013-12-23 00:32:37 -0300405static const struct factors_data sun4i_pll5_data __initconst = {
406 .enable = 31,
407 .table = &sun4i_pll5_config,
408 .getter = sun4i_get_pll5_factors,
409};
410
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530411static const struct factors_data sun4i_apb1_data __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200412 .table = &sun4i_apb1_config,
413 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300414};
415
Emilio López75517692013-12-23 00:32:39 -0300416static const struct factors_data sun4i_mod0_data __initconst = {
417 .enable = 31,
418 .mux = 24,
419 .table = &sun4i_mod0_config,
420 .getter = sun4i_get_mod0_factors,
421};
422
Emilio López5f4e0be2013-12-23 00:32:36 -0300423static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
424 const struct factors_data *data)
Emilio Lópeze874a662013-02-25 11:44:26 -0300425{
426 struct clk *clk;
Emilio López40a5dcb2013-12-23 00:32:32 -0300427 struct clk_factors *factors;
428 struct clk_gate *gate = NULL;
429 struct clk_mux *mux = NULL;
430 struct clk_hw *gate_hw = NULL;
431 struct clk_hw *mux_hw = NULL;
Emilio Lópeze874a662013-02-25 11:44:26 -0300432 const char *clk_name = node->name;
Emilio López40a5dcb2013-12-23 00:32:32 -0300433 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300434 void *reg;
Emilio López40a5dcb2013-12-23 00:32:32 -0300435 int i = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -0300436
437 reg = of_iomap(node, 0);
438
Emilio López40a5dcb2013-12-23 00:32:32 -0300439 /* if we have a mux, we will have >1 parents */
440 while (i < SUNXI_MAX_PARENTS &&
441 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
442 i++;
Emilio Lópeze874a662013-02-25 11:44:26 -0300443
Emilio López76192dc2013-12-23 00:32:40 -0300444 /* Nodes should be providing the name via clock-output-names
445 * but originally our dts didn't, and so we used node->name.
446 * The new, better nodes look like clk@deadbeef, so we pull the
447 * name just in this case */
448 if (!strcmp("clk", clk_name)) {
449 of_property_read_string_index(node, "clock-output-names",
450 0, &clk_name);
451 }
452
Emilio López40a5dcb2013-12-23 00:32:32 -0300453 factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
454 if (!factors)
Emilio López5f4e0be2013-12-23 00:32:36 -0300455 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300456
457 /* Add a gate if this factor clock can be gated */
458 if (data->enable) {
459 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
460 if (!gate) {
461 kfree(factors);
Emilio López5f4e0be2013-12-23 00:32:36 -0300462 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300463 }
464
465 /* set up gate properties */
466 gate->reg = reg;
467 gate->bit_idx = data->enable;
468 gate->lock = &clk_lock;
469 gate_hw = &gate->hw;
470 }
471
472 /* Add a mux if this factor clock can be muxed */
473 if (data->mux) {
474 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
475 if (!mux) {
476 kfree(factors);
477 kfree(gate);
Emilio López5f4e0be2013-12-23 00:32:36 -0300478 return NULL;
Emilio López40a5dcb2013-12-23 00:32:32 -0300479 }
480
481 /* set up gate properties */
482 mux->reg = reg;
483 mux->shift = data->mux;
484 mux->mask = SUNXI_FACTORS_MUX_MASK;
485 mux->lock = &clk_lock;
486 mux_hw = &mux->hw;
487 }
488
489 /* set up factors properties */
490 factors->reg = reg;
491 factors->config = data->table;
492 factors->get_factors = data->getter;
493 factors->lock = &clk_lock;
494
495 clk = clk_register_composite(NULL, clk_name,
496 parents, i,
497 mux_hw, &clk_mux_ops,
498 &factors->hw, &clk_factors_ops,
Emilio López5f4e0be2013-12-23 00:32:36 -0300499 gate_hw, &clk_gate_ops, 0);
Emilio Lópeze874a662013-02-25 11:44:26 -0300500
Axel Linee85e9b2013-07-12 16:15:15 +0800501 if (!IS_ERR(clk)) {
Emilio Lópeze874a662013-02-25 11:44:26 -0300502 of_clk_add_provider(node, of_clk_src_simple_get, clk);
503 clk_register_clkdev(clk, clk_name, NULL);
504 }
Emilio López5f4e0be2013-12-23 00:32:36 -0300505
506 return clk;
Emilio Lópeze874a662013-02-25 11:44:26 -0300507}
508
509
510
511/**
512 * sunxi_mux_clk_setup() - Setup function for muxes
513 */
514
515#define SUNXI_MUX_GATE_WIDTH 2
516
517struct mux_data {
518 u8 shift;
519};
520
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530521static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300522 .shift = 16,
523};
524
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530525static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200526 .shift = 12,
527};
528
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530529static const struct mux_data sun4i_apb1_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300530 .shift = 24,
531};
532
533static void __init sunxi_mux_clk_setup(struct device_node *node,
534 struct mux_data *data)
535{
536 struct clk *clk;
537 const char *clk_name = node->name;
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300538 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300539 void *reg;
540 int i = 0;
541
542 reg = of_iomap(node, 0);
543
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300544 while (i < SUNXI_MAX_PARENTS &&
545 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
Emilio Lópeze874a662013-02-25 11:44:26 -0300546 i++;
547
James Hogan819c1de2013-07-29 12:25:01 +0100548 clk = clk_register_mux(NULL, clk_name, parents, i,
549 CLK_SET_RATE_NO_REPARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300550 data->shift, SUNXI_MUX_GATE_WIDTH,
551 0, &clk_lock);
552
553 if (clk) {
554 of_clk_add_provider(node, of_clk_src_simple_get, clk);
555 clk_register_clkdev(clk, clk_name, NULL);
556 }
557}
558
559
560
561/**
562 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
563 */
564
Emilio Lópeze874a662013-02-25 11:44:26 -0300565struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200566 u8 shift;
567 u8 pow;
568 u8 width;
Emilio Lópeze874a662013-02-25 11:44:26 -0300569};
570
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530571static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200572 .shift = 0,
573 .pow = 0,
574 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300575};
576
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530577static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200578 .shift = 4,
579 .pow = 1,
580 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300581};
582
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530583static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200584 .shift = 8,
585 .pow = 1,
586 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300587};
588
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530589static const struct div_data sun6i_a31_apb2_div_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200590 .shift = 0,
591 .pow = 0,
592 .width = 4,
593};
594
Emilio Lópeze874a662013-02-25 11:44:26 -0300595static void __init sunxi_divider_clk_setup(struct device_node *node,
596 struct div_data *data)
597{
598 struct clk *clk;
599 const char *clk_name = node->name;
600 const char *clk_parent;
601 void *reg;
602
603 reg = of_iomap(node, 0);
604
605 clk_parent = of_clk_get_parent_name(node, 0);
606
607 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
Maxime Ripard70855bb2013-07-23 09:25:56 +0200608 reg, data->shift, data->width,
Emilio Lópeze874a662013-02-25 11:44:26 -0300609 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
610 &clk_lock);
611 if (clk) {
612 of_clk_add_provider(node, of_clk_src_simple_get, clk);
613 clk_register_clkdev(clk, clk_name, NULL);
614 }
615}
616
617
Emilio López13569a72013-03-27 18:20:37 -0300618
619/**
620 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
621 */
622
623#define SUNXI_GATES_MAX_SIZE 64
624
625struct gates_data {
626 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
627};
628
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530629static const struct gates_data sun4i_axi_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300630 .mask = {1},
631};
632
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530633static const struct gates_data sun4i_ahb_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300634 .mask = {0x7F77FFF, 0x14FB3F},
635};
636
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530637static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200638 .mask = {0x147667e7, 0x185915},
639};
640
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530641static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200642 .mask = {0x107067e7, 0x185111},
643};
644
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530645static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200646 .mask = {0xEDFE7F62, 0x794F931},
647};
648
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530649static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200650 .mask = { 0x12f77fff, 0x16ff3f },
651};
652
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530653static const struct gates_data sun4i_apb0_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300654 .mask = {0x4EF},
655};
656
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530657static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200658 .mask = {0x469},
659};
660
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530661static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200662 .mask = {0x61},
663};
664
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530665static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200666 .mask = { 0x4ff },
667};
668
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530669static const struct gates_data sun4i_apb1_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300670 .mask = {0xFF00F7},
671};
672
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530673static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200674 .mask = {0xf0007},
675};
676
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530677static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200678 .mask = {0xa0007},
679};
680
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530681static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200682 .mask = {0x3031},
683};
684
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530685static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200686 .mask = {0x3F000F},
687};
688
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530689static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200690 .mask = { 0xff80ff },
691};
692
Emilio López13569a72013-03-27 18:20:37 -0300693static void __init sunxi_gates_clk_setup(struct device_node *node,
694 struct gates_data *data)
695{
696 struct clk_onecell_data *clk_data;
697 const char *clk_parent;
698 const char *clk_name;
699 void *reg;
700 int qty;
701 int i = 0;
702 int j = 0;
703 int ignore;
704
705 reg = of_iomap(node, 0);
706
707 clk_parent = of_clk_get_parent_name(node, 0);
708
709 /* Worst-case size approximation and memory allocation */
710 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
711 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
712 if (!clk_data)
713 return;
714 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
715 if (!clk_data->clks) {
716 kfree(clk_data);
717 return;
718 }
719
720 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
721 of_property_read_string_index(node, "clock-output-names",
722 j, &clk_name);
723
724 /* No driver claims this clock, but it should remain gated */
725 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
726
727 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
728 clk_parent, ignore,
729 reg + 4 * (i/32), i % 32,
730 0, &clk_lock);
731 WARN_ON(IS_ERR(clk_data->clks[i]));
732
733 j++;
734 }
735
736 /* Adjust to the real max */
737 clk_data->clk_num = i;
738
739 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
740}
741
Emilio Lópezd584c132013-12-23 00:32:37 -0300742
743
744/**
745 * sunxi_divs_clk_setup() helper data
746 */
747
748#define SUNXI_DIVS_MAX_QTY 2
749#define SUNXI_DIVISOR_WIDTH 2
750
751struct divs_data {
752 const struct factors_data *factors; /* data for the factor clock */
753 struct {
754 u8 fixed; /* is it a fixed divisor? if not... */
755 struct clk_div_table *table; /* is it a table based divisor? */
756 u8 shift; /* otherwise it's a normal divisor with this shift */
757 u8 pow; /* is it power-of-two based? */
758 u8 gate; /* is it independently gateable? */
759 } div[SUNXI_DIVS_MAX_QTY];
760};
761
762static struct clk_div_table pll6_sata_tbl[] = {
763 { .val = 0, .div = 6, },
764 { .val = 1, .div = 12, },
765 { .val = 2, .div = 18, },
766 { .val = 3, .div = 24, },
767 { } /* sentinel */
768};
769
770static const struct divs_data pll5_divs_data __initconst = {
771 .factors = &sun4i_pll5_data,
772 .div = {
773 { .shift = 0, .pow = 0, }, /* M, DDR */
774 { .shift = 16, .pow = 1, }, /* P, other */
775 }
776};
777
778static const struct divs_data pll6_divs_data __initconst = {
779 .factors = &sun4i_pll5_data,
780 .div = {
781 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
782 { .fixed = 2 }, /* P, other */
783 }
784};
785
786/**
787 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
788 *
789 * These clocks look something like this
790 * ________________________
791 * | ___divisor 1---|----> to consumer
792 * parent >--| pll___/___divisor 2---|----> to consumer
793 * | \_______________|____> to consumer
794 * |________________________|
795 */
796
797static void __init sunxi_divs_clk_setup(struct device_node *node,
798 struct divs_data *data)
799{
800 struct clk_onecell_data *clk_data;
801 const char *parent = node->name;
802 const char *clk_name;
803 struct clk **clks, *pclk;
804 struct clk_hw *gate_hw, *rate_hw;
805 const struct clk_ops *rate_ops;
806 struct clk_gate *gate = NULL;
807 struct clk_fixed_factor *fix_factor;
808 struct clk_divider *divider;
809 void *reg;
810 int i = 0;
811 int flags, clkflags;
812
813 /* Set up factor clock that we will be dividing */
814 pclk = sunxi_factors_clk_setup(node, data->factors);
815
816 reg = of_iomap(node, 0);
817
818 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
819 if (!clk_data)
820 return;
821
822 clks = kzalloc(SUNXI_DIVS_MAX_QTY * sizeof(struct clk *), GFP_KERNEL);
823 if (!clks)
824 goto free_clkdata;
825
826 clk_data->clks = clks;
827
828 /* It's not a good idea to have automatic reparenting changing
829 * our RAM clock! */
830 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
831
832 for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
833 if (of_property_read_string_index(node, "clock-output-names",
834 i, &clk_name) != 0)
835 break;
836
837 gate_hw = NULL;
838 rate_hw = NULL;
839 rate_ops = NULL;
840
841 /* If this leaf clock can be gated, create a gate */
842 if (data->div[i].gate) {
843 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
844 if (!gate)
845 goto free_clks;
846
847 gate->reg = reg;
848 gate->bit_idx = data->div[i].gate;
849 gate->lock = &clk_lock;
850
851 gate_hw = &gate->hw;
852 }
853
854 /* Leaves can be fixed or configurable divisors */
855 if (data->div[i].fixed) {
856 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
857 if (!fix_factor)
858 goto free_gate;
859
860 fix_factor->mult = 1;
861 fix_factor->div = data->div[i].fixed;
862
863 rate_hw = &fix_factor->hw;
864 rate_ops = &clk_fixed_factor_ops;
865 } else {
866 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
867 if (!divider)
868 goto free_gate;
869
870 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
871
872 divider->reg = reg;
873 divider->shift = data->div[i].shift;
874 divider->width = SUNXI_DIVISOR_WIDTH;
875 divider->flags = flags;
876 divider->lock = &clk_lock;
877 divider->table = data->div[i].table;
878
879 rate_hw = &divider->hw;
880 rate_ops = &clk_divider_ops;
881 }
882
883 /* Wrap the (potential) gate and the divisor on a composite
884 * clock to unify them */
885 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
886 NULL, NULL,
887 rate_hw, rate_ops,
888 gate_hw, &clk_gate_ops,
889 clkflags);
890
891 WARN_ON(IS_ERR(clk_data->clks[i]));
892 clk_register_clkdev(clks[i], clk_name, NULL);
893 }
894
895 /* The last clock available on the getter is the parent */
896 clks[i++] = pclk;
897
898 /* Adjust to the real max */
899 clk_data->clk_num = i;
900
901 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
902
903 return;
904
905free_gate:
906 kfree(gate);
907free_clks:
908 kfree(clks);
909free_clkdata:
910 kfree(clk_data);
911}
912
913
914
Emilio Lópeze874a662013-02-25 11:44:26 -0300915/* Matches for factors clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530916static const struct of_device_id clk_factors_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200917 {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200918 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200919 {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
Emilio López75517692013-12-23 00:32:39 -0300920 {.compatible = "allwinner,sun4i-mod0-clk", .data = &sun4i_mod0_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300921 {}
922};
923
924/* Matches for divider clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530925static const struct of_device_id clk_div_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200926 {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
927 {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
928 {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200929 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300930 {}
931};
932
Emilio Lópezd584c132013-12-23 00:32:37 -0300933/* Matches for divided outputs */
934static const struct of_device_id clk_divs_match[] __initconst = {
935 {.compatible = "allwinner,sun4i-pll5-clk", .data = &pll5_divs_data,},
936 {.compatible = "allwinner,sun4i-pll6-clk", .data = &pll6_divs_data,},
937 {}
938};
939
Emilio Lópeze874a662013-02-25 11:44:26 -0300940/* Matches for mux clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530941static const struct of_device_id clk_mux_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200942 {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
943 {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200944 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300945 {}
946};
947
Emilio López13569a72013-03-27 18:20:37 -0300948/* Matches for gate clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530949static const struct of_device_id clk_gates_match[] __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200950 {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
951 {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200952 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200953 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200954 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200955 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200956 {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200957 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200958 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200959 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200960 {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200961 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200962 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200963 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200964 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200965 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -0300966 {}
967};
968
Emilio Lópeze874a662013-02-25 11:44:26 -0300969static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
970 void *function)
971{
972 struct device_node *np;
973 const struct div_data *data;
974 const struct of_device_id *match;
975 void (*setup_function)(struct device_node *, const void *) = function;
976
977 for_each_matching_node(np, clk_match) {
978 match = of_match_node(clk_match, np);
979 data = match->data;
980 setup_function(np, data);
981 }
982}
983
Emilio López8e6a4c42013-09-20 22:03:12 -0300984/**
985 * System clock protection
986 *
987 * By enabling these critical clocks, we prevent their accidental gating
988 * by the framework
989 */
990static void __init sunxi_clock_protect(void)
991{
992 struct clk *clk;
993
994 /* memory bus clock - sun5i+ */
995 clk = clk_get(NULL, "mbus");
996 if (!IS_ERR(clk)) {
997 clk_prepare_enable(clk);
998 clk_put(clk);
999 }
1000
1001 /* DDR clock - sun4i+ */
1002 clk = clk_get(NULL, "pll5_ddr");
1003 if (!IS_ERR(clk)) {
1004 clk_prepare_enable(clk);
1005 clk_put(clk);
1006 }
1007}
1008
Mike Turquette1d9438f2013-12-01 12:42:45 -08001009static void __init sunxi_init_clocks(void)
Emilio Lópeze874a662013-02-25 11:44:26 -03001010{
Emilio Lópeze874a662013-02-25 11:44:26 -03001011 /* Register factor clocks */
1012 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1013
1014 /* Register divider clocks */
1015 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1016
Emilio Lópezd584c132013-12-23 00:32:37 -03001017 /* Register divided output clocks */
1018 of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1019
Emilio Lópeze874a662013-02-25 11:44:26 -03001020 /* Register mux clocks */
1021 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -03001022
1023 /* Register gate clocks */
1024 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio López8e6a4c42013-09-20 22:03:12 -03001025
1026 /* Enable core system clocks */
1027 sunxi_clock_protect();
Emilio Lópeze874a662013-02-25 11:44:26 -03001028}
Sebastian Hesselbarthbe080452013-09-06 14:59:57 +02001029CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sunxi_init_clocks);
1030CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sunxi_init_clocks);
1031CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sunxi_init_clocks);
1032CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sunxi_init_clocks);
1033CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sunxi_init_clocks);