blob: 5fac1aa87bdb27d9df6f4a76941478a9095e104d [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
282#define SUNXI_DIVISOR_WIDTH 2
283
284struct div_data {
285 u8 shift;
286 u8 pow;
287};
288
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200289static const __initconst struct div_data sun4i_axi_data = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300290 .shift = 0,
291 .pow = 0,
292};
293
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200294static const __initconst struct div_data sun4i_ahb_data = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300295 .shift = 4,
296 .pow = 1,
297};
298
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200299static const __initconst struct div_data sun4i_apb0_data = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300300 .shift = 8,
301 .pow = 1,
302};
303
304static void __init sunxi_divider_clk_setup(struct device_node *node,
305 struct div_data *data)
306{
307 struct clk *clk;
308 const char *clk_name = node->name;
309 const char *clk_parent;
310 void *reg;
311
312 reg = of_iomap(node, 0);
313
314 clk_parent = of_clk_get_parent_name(node, 0);
315
316 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
317 reg, data->shift, SUNXI_DIVISOR_WIDTH,
318 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
319 &clk_lock);
320 if (clk) {
321 of_clk_add_provider(node, of_clk_src_simple_get, clk);
322 clk_register_clkdev(clk, clk_name, NULL);
323 }
324}
325
326
Emilio López13569a72013-03-27 18:20:37 -0300327
328/**
329 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
330 */
331
332#define SUNXI_GATES_MAX_SIZE 64
333
334struct gates_data {
335 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
336};
337
Maxime Ripard4f985b42013-04-30 11:56:22 +0200338static const __initconst struct gates_data sun4i_axi_gates_data = {
Emilio López13569a72013-03-27 18:20:37 -0300339 .mask = {1},
340};
341
Maxime Ripard4f985b42013-04-30 11:56:22 +0200342static const __initconst struct gates_data sun4i_ahb_gates_data = {
Emilio López13569a72013-03-27 18:20:37 -0300343 .mask = {0x7F77FFF, 0x14FB3F},
344};
345
Maxime Ripard2371dd82013-07-16 11:21:59 +0200346static const __initconst struct gates_data sun5i_a10s_ahb_gates_data = {
347 .mask = {0x147667e7, 0x185915},
348};
349
Maxime Ripard4f985b42013-04-30 11:56:22 +0200350static const __initconst struct gates_data sun5i_a13_ahb_gates_data = {
351 .mask = {0x107067e7, 0x185111},
352};
353
354static const __initconst struct gates_data sun4i_apb0_gates_data = {
Emilio López13569a72013-03-27 18:20:37 -0300355 .mask = {0x4EF},
356};
357
Maxime Ripard2371dd82013-07-16 11:21:59 +0200358static const __initconst struct gates_data sun5i_a10s_apb0_gates_data = {
359 .mask = {0x469},
360};
361
Maxime Ripard4f985b42013-04-30 11:56:22 +0200362static const __initconst struct gates_data sun5i_a13_apb0_gates_data = {
363 .mask = {0x61},
364};
365
366static const __initconst struct gates_data sun4i_apb1_gates_data = {
Emilio López13569a72013-03-27 18:20:37 -0300367 .mask = {0xFF00F7},
368};
369
Maxime Ripard2371dd82013-07-16 11:21:59 +0200370static const __initconst struct gates_data sun5i_a10s_apb1_gates_data = {
371 .mask = {0xf0007},
372};
373
Maxime Ripard4f985b42013-04-30 11:56:22 +0200374static const __initconst struct gates_data sun5i_a13_apb1_gates_data = {
375 .mask = {0xa0007},
376};
377
Emilio López13569a72013-03-27 18:20:37 -0300378static void __init sunxi_gates_clk_setup(struct device_node *node,
379 struct gates_data *data)
380{
381 struct clk_onecell_data *clk_data;
382 const char *clk_parent;
383 const char *clk_name;
384 void *reg;
385 int qty;
386 int i = 0;
387 int j = 0;
388 int ignore;
389
390 reg = of_iomap(node, 0);
391
392 clk_parent = of_clk_get_parent_name(node, 0);
393
394 /* Worst-case size approximation and memory allocation */
395 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
396 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
397 if (!clk_data)
398 return;
399 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
400 if (!clk_data->clks) {
401 kfree(clk_data);
402 return;
403 }
404
405 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
406 of_property_read_string_index(node, "clock-output-names",
407 j, &clk_name);
408
409 /* No driver claims this clock, but it should remain gated */
410 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
411
412 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
413 clk_parent, ignore,
414 reg + 4 * (i/32), i % 32,
415 0, &clk_lock);
416 WARN_ON(IS_ERR(clk_data->clks[i]));
417
418 j++;
419 }
420
421 /* Adjust to the real max */
422 clk_data->clk_num = i;
423
424 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
425}
426
Emilio Lópeze874a662013-02-25 11:44:26 -0300427/* Matches for factors clocks */
428static const __initconst struct of_device_id clk_factors_match[] = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200429 {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
430 {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300431 {}
432};
433
434/* Matches for divider clocks */
435static const __initconst struct of_device_id clk_div_match[] = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200436 {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
437 {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
438 {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300439 {}
440};
441
442/* Matches for mux clocks */
443static const __initconst struct of_device_id clk_mux_match[] = {
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200444 {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
445 {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300446 {}
447};
448
Emilio López13569a72013-03-27 18:20:37 -0300449/* Matches for gate clocks */
450static const __initconst struct of_device_id clk_gates_match[] = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200451 {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
452 {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200453 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200454 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
455 {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200456 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200457 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
458 {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200459 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200460 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -0300461 {}
462};
463
Emilio Lópeze874a662013-02-25 11:44:26 -0300464static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
465 void *function)
466{
467 struct device_node *np;
468 const struct div_data *data;
469 const struct of_device_id *match;
470 void (*setup_function)(struct device_node *, const void *) = function;
471
472 for_each_matching_node(np, clk_match) {
473 match = of_match_node(clk_match, np);
474 data = match->data;
475 setup_function(np, data);
476 }
477}
478
479void __init sunxi_init_clocks(void)
480{
Emilio López431807f2013-07-22 22:01:05 -0300481 /* Register all the simple and basic clocks on DT */
482 of_clk_init(NULL);
Emilio Lópeze874a662013-02-25 11:44:26 -0300483
484 /* Register factor clocks */
485 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
486
487 /* Register divider clocks */
488 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
489
490 /* Register mux clocks */
491 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -0300492
493 /* Register gate clocks */
494 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -0300495}