blob: 2cafda88b7f66747672b1bddaec279a234460969 [file] [log] [blame]
Emilio Lópeze874a662013-02-25 11:44:26 -03001/*
2 * Copyright 2013 Emilio López
3 *
4 * Emilio López <emilio@elopez.com.ar>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/clk-provider.h>
18#include <linux/clkdev.h>
19#include <linux/clk/sunxi.h>
20#include <linux/of.h>
21#include <linux/of_address.h>
22
23#include "clk-factors.h"
24
25static DEFINE_SPINLOCK(clk_lock);
26
27/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020028 * sun4i_osc_clk_setup() - Setup function for gatable oscillator
Emilio Lópeze874a662013-02-25 11:44:26 -030029 */
30
31#define SUNXI_OSC24M_GATE 0
32
Maxime Ripard81ba6c52013-07-22 18:21:32 +020033static void __init sun4i_osc_clk_setup(struct device_node *node)
Emilio Lópeze874a662013-02-25 11:44:26 -030034{
35 struct clk *clk;
Emilio López38e4aa02013-04-10 15:02:57 -070036 struct clk_fixed_rate *fixed;
37 struct clk_gate *gate;
Emilio Lópeze874a662013-02-25 11:44:26 -030038 const char *clk_name = node->name;
Emilio López38e4aa02013-04-10 15:02:57 -070039 u32 rate;
Emilio Lópeze874a662013-02-25 11:44:26 -030040
Emilio López38e4aa02013-04-10 15:02:57 -070041 /* allocate fixed-rate and gate clock structs */
42 fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
43 if (!fixed)
44 return;
45 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
46 if (!gate) {
47 kfree(fixed);
48 return;
49 }
Emilio Lópeze874a662013-02-25 11:44:26 -030050
Emilio López38e4aa02013-04-10 15:02:57 -070051 if (of_property_read_u32(node, "clock-frequency", &rate))
52 return;
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
Axel Linee85e9b2013-07-12 16:15:15 +080067 if (!IS_ERR(clk)) {
Emilio Lópeze874a662013-02-25 11:44:26 -030068 of_clk_add_provider(node, of_clk_src_simple_get, clk);
69 clk_register_clkdev(clk, clk_name, NULL);
70 }
71}
Maxime Ripard81ba6c52013-07-22 18:21:32 +020072CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -030073
74
75
76/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020077 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
Emilio Lópeze874a662013-02-25 11:44:26 -030078 * PLL1 rate is calculated as follows
79 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
80 * parent_rate is always 24Mhz
81 */
82
Maxime Ripard81ba6c52013-07-22 18:21:32 +020083static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -030084 u8 *n, u8 *k, u8 *m, u8 *p)
85{
86 u8 div;
87
88 /* Normalize value to a 6M multiple */
89 div = *freq / 6000000;
90 *freq = 6000000 * div;
91
92 /* we were called to round the frequency, we can now return */
93 if (n == NULL)
94 return;
95
96 /* m is always zero for pll1 */
97 *m = 0;
98
99 /* k is 1 only on these cases */
100 if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
101 *k = 1;
102 else
103 *k = 0;
104
105 /* p will be 3 for divs under 10 */
106 if (div < 10)
107 *p = 3;
108
109 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
110 else if (div < 20 || (div < 32 && (div & 1)))
111 *p = 2;
112
113 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
114 * of divs between 40-62 */
115 else if (div < 40 || (div < 64 && (div & 2)))
116 *p = 1;
117
118 /* any other entries have p = 0 */
119 else
120 *p = 0;
121
122 /* calculate a suitable n based on k and p */
123 div <<= *p;
124 div /= (*k + 1);
125 *n = div / 4;
126}
127
128
129
130/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200131 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
Emilio Lópeze874a662013-02-25 11:44:26 -0300132 * APB1 rate is calculated as follows
133 * rate = (parent_rate >> p) / (m + 1);
134 */
135
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200136static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -0300137 u8 *n, u8 *k, u8 *m, u8 *p)
138{
139 u8 calcm, calcp;
140
141 if (parent_rate < *freq)
142 *freq = parent_rate;
143
144 parent_rate = (parent_rate + (*freq - 1)) / *freq;
145
146 /* Invalid rate! */
147 if (parent_rate > 32)
148 return;
149
150 if (parent_rate <= 4)
151 calcp = 0;
152 else if (parent_rate <= 8)
153 calcp = 1;
154 else if (parent_rate <= 16)
155 calcp = 2;
156 else
157 calcp = 3;
158
159 calcm = (parent_rate >> calcp) - 1;
160
161 *freq = (parent_rate >> calcp) / (calcm + 1);
162
163 /* we were called to round the frequency, we can now return */
164 if (n == NULL)
165 return;
166
167 *m = calcm;
168 *p = calcp;
169}
170
171
172
173/**
174 * sunxi_factors_clk_setup() - Setup function for factor clocks
175 */
176
177struct factors_data {
178 struct clk_factors_config *table;
179 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
180};
181
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200182static struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300183 .nshift = 8,
184 .nwidth = 5,
185 .kshift = 4,
186 .kwidth = 2,
187 .mshift = 0,
188 .mwidth = 2,
189 .pshift = 16,
190 .pwidth = 2,
191};
192
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200193static struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300194 .mshift = 0,
195 .mwidth = 5,
196 .pshift = 16,
197 .pwidth = 2,
198};
199
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200200static const __initconst struct factors_data sun4i_pll1_data = {
201 .table = &sun4i_pll1_config,
202 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300203};
204
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200205static const __initconst struct factors_data sun4i_apb1_data = {
206 .table = &sun4i_apb1_config,
207 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300208};
209
210static void __init sunxi_factors_clk_setup(struct device_node *node,
211 struct factors_data *data)
212{
213 struct clk *clk;
214 const char *clk_name = node->name;
215 const char *parent;
216 void *reg;
217
218 reg = of_iomap(node, 0);
219
220 parent = of_clk_get_parent_name(node, 0);
221
Emilio López5a4fe9b2013-03-27 18:20:42 -0300222 clk = clk_register_factors(NULL, clk_name, parent, 0, reg,
223 data->table, data->getter, &clk_lock);
Emilio Lópeze874a662013-02-25 11:44:26 -0300224
Axel Linee85e9b2013-07-12 16:15:15 +0800225 if (!IS_ERR(clk)) {
Emilio Lópeze874a662013-02-25 11:44:26 -0300226 of_clk_add_provider(node, of_clk_src_simple_get, clk);
227 clk_register_clkdev(clk, clk_name, NULL);
228 }
229}
230
231
232
233/**
234 * sunxi_mux_clk_setup() - Setup function for muxes
235 */
236
237#define SUNXI_MUX_GATE_WIDTH 2
238
239struct mux_data {
240 u8 shift;
241};
242
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200243static const __initconst struct mux_data sun4i_cpu_mux_data = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300244 .shift = 16,
245};
246
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200247static const __initconst struct mux_data sun4i_apb1_mux_data = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300248 .shift = 24,
249};
250
251static void __init sunxi_mux_clk_setup(struct device_node *node,
252 struct mux_data *data)
253{
254 struct clk *clk;
255 const char *clk_name = node->name;
Emilio López918d7f62013-03-27 18:20:43 -0300256 const char *parents[5];
Emilio Lópeze874a662013-02-25 11:44:26 -0300257 void *reg;
258 int i = 0;
259
260 reg = of_iomap(node, 0);
261
262 while (i < 5 && (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
263 i++;
264
James Hogan819c1de2013-07-29 12:25:01 +0100265 clk = clk_register_mux(NULL, clk_name, parents, i,
266 CLK_SET_RATE_NO_REPARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300267 data->shift, SUNXI_MUX_GATE_WIDTH,
268 0, &clk_lock);
269
270 if (clk) {
271 of_clk_add_provider(node, of_clk_src_simple_get, clk);
272 clk_register_clkdev(clk, clk_name, NULL);
273 }
274}
275
276
277
278/**
279 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
280 */
281
Emilio Lópeze874a662013-02-25 11:44:26 -0300282struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200283 u8 shift;
284 u8 pow;
285 u8 width;
Emilio Lópeze874a662013-02-25 11:44:26 -0300286};
287
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200288static const __initconst struct div_data sun4i_axi_data = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200289 .shift = 0,
290 .pow = 0,
291 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300292};
293
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200294static const __initconst struct div_data sun4i_ahb_data = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200295 .shift = 4,
296 .pow = 1,
297 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300298};
299
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200300static const __initconst struct div_data sun4i_apb0_data = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200301 .shift = 8,
302 .pow = 1,
303 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300304};
305
306static void __init sunxi_divider_clk_setup(struct device_node *node,
307 struct div_data *data)
308{
309 struct clk *clk;
310 const char *clk_name = node->name;
311 const char *clk_parent;
312 void *reg;
313
314 reg = of_iomap(node, 0);
315
316 clk_parent = of_clk_get_parent_name(node, 0);
317
318 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
Maxime Ripard70855bb2013-07-23 09:25:56 +0200319 reg, data->shift, data->width,
Emilio Lópeze874a662013-02-25 11:44:26 -0300320 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
321 &clk_lock);
322 if (clk) {
323 of_clk_add_provider(node, of_clk_src_simple_get, clk);
324 clk_register_clkdev(clk, clk_name, NULL);
325 }
326}
327
328
Emilio López13569a72013-03-27 18:20:37 -0300329
330/**
331 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
332 */
333
334#define SUNXI_GATES_MAX_SIZE 64
335
336struct gates_data {
337 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
338};
339
Maxime Ripard4f985b42013-04-30 11:56:22 +0200340static const __initconst struct gates_data sun4i_axi_gates_data = {
Emilio López13569a72013-03-27 18:20:37 -0300341 .mask = {1},
342};
343
Maxime Ripard4f985b42013-04-30 11:56:22 +0200344static const __initconst struct gates_data sun4i_ahb_gates_data = {
Emilio López13569a72013-03-27 18:20:37 -0300345 .mask = {0x7F77FFF, 0x14FB3F},
346};
347
Maxime Ripard2371dd82013-07-16 11:21:59 +0200348static const __initconst struct gates_data sun5i_a10s_ahb_gates_data = {
349 .mask = {0x147667e7, 0x185915},
350};
351
Maxime Ripard4f985b42013-04-30 11:56:22 +0200352static const __initconst struct gates_data sun5i_a13_ahb_gates_data = {
353 .mask = {0x107067e7, 0x185111},
354};
355
356static const __initconst struct gates_data sun4i_apb0_gates_data = {
Emilio López13569a72013-03-27 18:20:37 -0300357 .mask = {0x4EF},
358};
359
Maxime Ripard2371dd82013-07-16 11:21:59 +0200360static const __initconst struct gates_data sun5i_a10s_apb0_gates_data = {
361 .mask = {0x469},
362};
363
Maxime Ripard4f985b42013-04-30 11:56:22 +0200364static const __initconst struct gates_data sun5i_a13_apb0_gates_data = {
365 .mask = {0x61},
366};
367
368static const __initconst struct gates_data sun4i_apb1_gates_data = {
Emilio López13569a72013-03-27 18:20:37 -0300369 .mask = {0xFF00F7},
370};
371
Maxime Ripard2371dd82013-07-16 11:21:59 +0200372static const __initconst struct gates_data sun5i_a10s_apb1_gates_data = {
373 .mask = {0xf0007},
374};
375
Maxime Ripard4f985b42013-04-30 11:56:22 +0200376static const __initconst struct gates_data sun5i_a13_apb1_gates_data = {
377 .mask = {0xa0007},
378};
379
Emilio López13569a72013-03-27 18:20:37 -0300380static void __init sunxi_gates_clk_setup(struct device_node *node,
381 struct gates_data *data)
382{
383 struct clk_onecell_data *clk_data;
384 const char *clk_parent;
385 const char *clk_name;
386 void *reg;
387 int qty;
388 int i = 0;
389 int j = 0;
390 int ignore;
391
392 reg = of_iomap(node, 0);
393
394 clk_parent = of_clk_get_parent_name(node, 0);
395
396 /* Worst-case size approximation and memory allocation */
397 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
398 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
399 if (!clk_data)
400 return;
401 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
402 if (!clk_data->clks) {
403 kfree(clk_data);
404 return;
405 }
406
407 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
408 of_property_read_string_index(node, "clock-output-names",
409 j, &clk_name);
410
411 /* No driver claims this clock, but it should remain gated */
412 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
413
414 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
415 clk_parent, ignore,
416 reg + 4 * (i/32), i % 32,
417 0, &clk_lock);
418 WARN_ON(IS_ERR(clk_data->clks[i]));
419
420 j++;
421 }
422
423 /* Adjust to the real max */
424 clk_data->clk_num = i;
425
426 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
427}
428
Emilio Lópeze874a662013-02-25 11:44:26 -0300429/* Matches for factors clocks */
430static const __initconst struct of_device_id clk_factors_match[] = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200431 {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
432 {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300433 {}
434};
435
436/* Matches for divider clocks */
437static const __initconst struct of_device_id clk_div_match[] = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200438 {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
439 {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
440 {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300441 {}
442};
443
444/* Matches for mux clocks */
445static const __initconst struct of_device_id clk_mux_match[] = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200446 {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
447 {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300448 {}
449};
450
Emilio López13569a72013-03-27 18:20:37 -0300451/* Matches for gate clocks */
452static const __initconst struct of_device_id clk_gates_match[] = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200453 {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
454 {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200455 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200456 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
457 {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200458 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200459 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
460 {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200461 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200462 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -0300463 {}
464};
465
Emilio Lópeze874a662013-02-25 11:44:26 -0300466static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
467 void *function)
468{
469 struct device_node *np;
470 const struct div_data *data;
471 const struct of_device_id *match;
472 void (*setup_function)(struct device_node *, const void *) = function;
473
474 for_each_matching_node(np, clk_match) {
475 match = of_match_node(clk_match, np);
476 data = match->data;
477 setup_function(np, data);
478 }
479}
480
481void __init sunxi_init_clocks(void)
482{
Emilio López431807f2013-07-22 22:01:05 -0300483 /* Register all the simple and basic clocks on DT */
484 of_clk_init(NULL);
Emilio Lópeze874a662013-02-25 11:44:26 -0300485
486 /* Register factor clocks */
487 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
488
489 /* Register divider clocks */
490 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
491
492 /* Register mux clocks */
493 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -0300494
495 /* Register gate clocks */
496 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -0300497}