blob: eeb623bec5ff2de6fc13dcaeffc1566e598324e7 [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/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200221 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
Emilio Lópeze874a662013-02-25 11:44:26 -0300222 * APB1 rate is calculated as follows
223 * rate = (parent_rate >> p) / (m + 1);
224 */
225
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200226static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -0300227 u8 *n, u8 *k, u8 *m, u8 *p)
228{
229 u8 calcm, calcp;
230
231 if (parent_rate < *freq)
232 *freq = parent_rate;
233
234 parent_rate = (parent_rate + (*freq - 1)) / *freq;
235
236 /* Invalid rate! */
237 if (parent_rate > 32)
238 return;
239
240 if (parent_rate <= 4)
241 calcp = 0;
242 else if (parent_rate <= 8)
243 calcp = 1;
244 else if (parent_rate <= 16)
245 calcp = 2;
246 else
247 calcp = 3;
248
249 calcm = (parent_rate >> calcp) - 1;
250
251 *freq = (parent_rate >> calcp) / (calcm + 1);
252
253 /* we were called to round the frequency, we can now return */
254 if (n == NULL)
255 return;
256
257 *m = calcm;
258 *p = calcp;
259}
260
261
262
263/**
264 * sunxi_factors_clk_setup() - Setup function for factor clocks
265 */
266
Emilio López40a5dcb2013-12-23 00:32:32 -0300267#define SUNXI_FACTORS_MUX_MASK 0x3
268
Emilio Lópeze874a662013-02-25 11:44:26 -0300269struct factors_data {
Emilio López40a5dcb2013-12-23 00:32:32 -0300270 int enable;
271 int mux;
Emilio Lópeze874a662013-02-25 11:44:26 -0300272 struct clk_factors_config *table;
273 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
274};
275
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200276static struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300277 .nshift = 8,
278 .nwidth = 5,
279 .kshift = 4,
280 .kwidth = 2,
281 .mshift = 0,
282 .mwidth = 2,
283 .pshift = 16,
284 .pwidth = 2,
285};
286
Maxime Ripard6a721db2013-07-23 23:34:10 +0200287static struct clk_factors_config sun6i_a31_pll1_config = {
288 .nshift = 8,
289 .nwidth = 5,
290 .kshift = 4,
291 .kwidth = 2,
292 .mshift = 0,
293 .mwidth = 2,
294};
295
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200296static struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300297 .mshift = 0,
298 .mwidth = 5,
299 .pshift = 16,
300 .pwidth = 2,
301};
302
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530303static const struct factors_data sun4i_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300304 .enable = 31,
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200305 .table = &sun4i_pll1_config,
306 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300307};
308
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530309static const struct factors_data sun6i_a31_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300310 .enable = 31,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200311 .table = &sun6i_a31_pll1_config,
312 .getter = sun6i_a31_get_pll1_factors,
313};
314
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530315static const struct factors_data sun4i_apb1_data __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200316 .table = &sun4i_apb1_config,
317 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300318};
319
320static void __init sunxi_factors_clk_setup(struct device_node *node,
321 struct factors_data *data)
322{
323 struct clk *clk;
Emilio López40a5dcb2013-12-23 00:32:32 -0300324 struct clk_factors *factors;
325 struct clk_gate *gate = NULL;
326 struct clk_mux *mux = NULL;
327 struct clk_hw *gate_hw = NULL;
328 struct clk_hw *mux_hw = NULL;
Emilio Lópeze874a662013-02-25 11:44:26 -0300329 const char *clk_name = node->name;
Emilio López40a5dcb2013-12-23 00:32:32 -0300330 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300331 void *reg;
Emilio López40a5dcb2013-12-23 00:32:32 -0300332 int i = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -0300333
334 reg = of_iomap(node, 0);
335
Emilio López40a5dcb2013-12-23 00:32:32 -0300336 /* if we have a mux, we will have >1 parents */
337 while (i < SUNXI_MAX_PARENTS &&
338 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
339 i++;
Emilio Lópeze874a662013-02-25 11:44:26 -0300340
Emilio López40a5dcb2013-12-23 00:32:32 -0300341 factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
342 if (!factors)
343 return;
344
345 /* Add a gate if this factor clock can be gated */
346 if (data->enable) {
347 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
348 if (!gate) {
349 kfree(factors);
350 return;
351 }
352
353 /* set up gate properties */
354 gate->reg = reg;
355 gate->bit_idx = data->enable;
356 gate->lock = &clk_lock;
357 gate_hw = &gate->hw;
358 }
359
360 /* Add a mux if this factor clock can be muxed */
361 if (data->mux) {
362 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
363 if (!mux) {
364 kfree(factors);
365 kfree(gate);
366 return;
367 }
368
369 /* set up gate properties */
370 mux->reg = reg;
371 mux->shift = data->mux;
372 mux->mask = SUNXI_FACTORS_MUX_MASK;
373 mux->lock = &clk_lock;
374 mux_hw = &mux->hw;
375 }
376
377 /* set up factors properties */
378 factors->reg = reg;
379 factors->config = data->table;
380 factors->get_factors = data->getter;
381 factors->lock = &clk_lock;
382
383 clk = clk_register_composite(NULL, clk_name,
384 parents, i,
385 mux_hw, &clk_mux_ops,
386 &factors->hw, &clk_factors_ops,
387 gate_hw, &clk_gate_ops,
388 i ? 0 : CLK_IS_ROOT);
Emilio Lópeze874a662013-02-25 11:44:26 -0300389
Axel Linee85e9b2013-07-12 16:15:15 +0800390 if (!IS_ERR(clk)) {
Emilio Lópeze874a662013-02-25 11:44:26 -0300391 of_clk_add_provider(node, of_clk_src_simple_get, clk);
392 clk_register_clkdev(clk, clk_name, NULL);
393 }
394}
395
396
397
398/**
399 * sunxi_mux_clk_setup() - Setup function for muxes
400 */
401
402#define SUNXI_MUX_GATE_WIDTH 2
403
404struct mux_data {
405 u8 shift;
406};
407
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530408static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300409 .shift = 16,
410};
411
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530412static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200413 .shift = 12,
414};
415
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530416static const struct mux_data sun4i_apb1_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300417 .shift = 24,
418};
419
420static void __init sunxi_mux_clk_setup(struct device_node *node,
421 struct mux_data *data)
422{
423 struct clk *clk;
424 const char *clk_name = node->name;
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300425 const char *parents[SUNXI_MAX_PARENTS];
Emilio Lópeze874a662013-02-25 11:44:26 -0300426 void *reg;
427 int i = 0;
428
429 reg = of_iomap(node, 0);
430
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300431 while (i < SUNXI_MAX_PARENTS &&
432 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
Emilio Lópeze874a662013-02-25 11:44:26 -0300433 i++;
434
James Hogan819c1de2013-07-29 12:25:01 +0100435 clk = clk_register_mux(NULL, clk_name, parents, i,
436 CLK_SET_RATE_NO_REPARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300437 data->shift, SUNXI_MUX_GATE_WIDTH,
438 0, &clk_lock);
439
440 if (clk) {
441 of_clk_add_provider(node, of_clk_src_simple_get, clk);
442 clk_register_clkdev(clk, clk_name, NULL);
443 }
444}
445
446
447
448/**
449 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
450 */
451
Emilio Lópeze874a662013-02-25 11:44:26 -0300452struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200453 u8 shift;
454 u8 pow;
455 u8 width;
Emilio Lópeze874a662013-02-25 11:44:26 -0300456};
457
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530458static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200459 .shift = 0,
460 .pow = 0,
461 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300462};
463
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530464static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200465 .shift = 4,
466 .pow = 1,
467 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300468};
469
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530470static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200471 .shift = 8,
472 .pow = 1,
473 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300474};
475
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530476static const struct div_data sun6i_a31_apb2_div_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200477 .shift = 0,
478 .pow = 0,
479 .width = 4,
480};
481
Emilio Lópeze874a662013-02-25 11:44:26 -0300482static void __init sunxi_divider_clk_setup(struct device_node *node,
483 struct div_data *data)
484{
485 struct clk *clk;
486 const char *clk_name = node->name;
487 const char *clk_parent;
488 void *reg;
489
490 reg = of_iomap(node, 0);
491
492 clk_parent = of_clk_get_parent_name(node, 0);
493
494 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
Maxime Ripard70855bb2013-07-23 09:25:56 +0200495 reg, data->shift, data->width,
Emilio Lópeze874a662013-02-25 11:44:26 -0300496 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
497 &clk_lock);
498 if (clk) {
499 of_clk_add_provider(node, of_clk_src_simple_get, clk);
500 clk_register_clkdev(clk, clk_name, NULL);
501 }
502}
503
504
Emilio López13569a72013-03-27 18:20:37 -0300505
506/**
507 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
508 */
509
510#define SUNXI_GATES_MAX_SIZE 64
511
512struct gates_data {
513 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
514};
515
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530516static const struct gates_data sun4i_axi_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300517 .mask = {1},
518};
519
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530520static const struct gates_data sun4i_ahb_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300521 .mask = {0x7F77FFF, 0x14FB3F},
522};
523
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530524static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200525 .mask = {0x147667e7, 0x185915},
526};
527
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530528static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200529 .mask = {0x107067e7, 0x185111},
530};
531
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530532static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200533 .mask = {0xEDFE7F62, 0x794F931},
534};
535
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530536static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200537 .mask = { 0x12f77fff, 0x16ff3f },
538};
539
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530540static const struct gates_data sun4i_apb0_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300541 .mask = {0x4EF},
542};
543
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530544static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200545 .mask = {0x469},
546};
547
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530548static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200549 .mask = {0x61},
550};
551
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530552static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200553 .mask = { 0x4ff },
554};
555
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530556static const struct gates_data sun4i_apb1_gates_data __initconst = {
Emilio López13569a72013-03-27 18:20:37 -0300557 .mask = {0xFF00F7},
558};
559
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530560static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
Maxime Ripard2371dd82013-07-16 11:21:59 +0200561 .mask = {0xf0007},
562};
563
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530564static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200565 .mask = {0xa0007},
566};
567
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530568static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200569 .mask = {0x3031},
570};
571
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530572static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200573 .mask = {0x3F000F},
574};
575
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530576static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200577 .mask = { 0xff80ff },
578};
579
Emilio López13569a72013-03-27 18:20:37 -0300580static void __init sunxi_gates_clk_setup(struct device_node *node,
581 struct gates_data *data)
582{
583 struct clk_onecell_data *clk_data;
584 const char *clk_parent;
585 const char *clk_name;
586 void *reg;
587 int qty;
588 int i = 0;
589 int j = 0;
590 int ignore;
591
592 reg = of_iomap(node, 0);
593
594 clk_parent = of_clk_get_parent_name(node, 0);
595
596 /* Worst-case size approximation and memory allocation */
597 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
598 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
599 if (!clk_data)
600 return;
601 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
602 if (!clk_data->clks) {
603 kfree(clk_data);
604 return;
605 }
606
607 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
608 of_property_read_string_index(node, "clock-output-names",
609 j, &clk_name);
610
611 /* No driver claims this clock, but it should remain gated */
612 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
613
614 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
615 clk_parent, ignore,
616 reg + 4 * (i/32), i % 32,
617 0, &clk_lock);
618 WARN_ON(IS_ERR(clk_data->clks[i]));
619
620 j++;
621 }
622
623 /* Adjust to the real max */
624 clk_data->clk_num = i;
625
626 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
627}
628
Emilio Lópeze874a662013-02-25 11:44:26 -0300629/* Matches for factors clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530630static const struct of_device_id clk_factors_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200631 {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200632 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200633 {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300634 {}
635};
636
637/* Matches for divider clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530638static const struct of_device_id clk_div_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200639 {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
640 {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
641 {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200642 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300643 {}
644};
645
646/* Matches for mux clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530647static const struct of_device_id clk_mux_match[] __initconst = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200648 {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
649 {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200650 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300651 {}
652};
653
Emilio López13569a72013-03-27 18:20:37 -0300654/* Matches for gate clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530655static const struct of_device_id clk_gates_match[] __initconst = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200656 {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
657 {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200658 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200659 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200660 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200661 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200662 {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200663 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200664 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200665 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200666 {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200667 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200668 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200669 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
Maxime Ripard1fb2e4a2013-07-25 21:06:56 +0200670 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200671 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -0300672 {}
673};
674
Emilio Lópeze874a662013-02-25 11:44:26 -0300675static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
676 void *function)
677{
678 struct device_node *np;
679 const struct div_data *data;
680 const struct of_device_id *match;
681 void (*setup_function)(struct device_node *, const void *) = function;
682
683 for_each_matching_node(np, clk_match) {
684 match = of_match_node(clk_match, np);
685 data = match->data;
686 setup_function(np, data);
687 }
688}
689
Emilio López8e6a4c42013-09-20 22:03:12 -0300690/**
691 * System clock protection
692 *
693 * By enabling these critical clocks, we prevent their accidental gating
694 * by the framework
695 */
696static void __init sunxi_clock_protect(void)
697{
698 struct clk *clk;
699
700 /* memory bus clock - sun5i+ */
701 clk = clk_get(NULL, "mbus");
702 if (!IS_ERR(clk)) {
703 clk_prepare_enable(clk);
704 clk_put(clk);
705 }
706
707 /* DDR clock - sun4i+ */
708 clk = clk_get(NULL, "pll5_ddr");
709 if (!IS_ERR(clk)) {
710 clk_prepare_enable(clk);
711 clk_put(clk);
712 }
713}
714
Mike Turquette1d9438f2013-12-01 12:42:45 -0800715static void __init sunxi_init_clocks(void)
Emilio Lópeze874a662013-02-25 11:44:26 -0300716{
Emilio Lópeze874a662013-02-25 11:44:26 -0300717 /* Register factor clocks */
718 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
719
720 /* Register divider clocks */
721 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
722
723 /* Register mux clocks */
724 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -0300725
726 /* Register gate clocks */
727 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio López8e6a4c42013-09-20 22:03:12 -0300728
729 /* Enable core system clocks */
730 sunxi_clock_protect();
Emilio Lópeze874a662013-02-25 11:44:26 -0300731}
Sebastian Hesselbarthbe080452013-09-06 14:59:57 +0200732CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sunxi_init_clocks);
733CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sunxi_init_clocks);
734CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sunxi_init_clocks);
735CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sunxi_init_clocks);
736CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sunxi_init_clocks);