blob: 15804c25e0388cb200a8392fa9ac14de25889350 [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/**
28 * sunxi_osc_clk_setup() - Setup function for gatable oscillator
29 */
30
31#define SUNXI_OSC24M_GATE 0
32
33static void __init sunxi_osc_clk_setup(struct device_node *node)
34{
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}
72
73
74
75/**
76 * sunxi_get_pll1_factors() - calculates n, k, m, p factors for PLL1
77 * PLL1 rate is calculated as follows
78 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
79 * parent_rate is always 24Mhz
80 */
81
82static void sunxi_get_pll1_factors(u32 *freq, u32 parent_rate,
83 u8 *n, u8 *k, u8 *m, u8 *p)
84{
85 u8 div;
86
87 /* Normalize value to a 6M multiple */
88 div = *freq / 6000000;
89 *freq = 6000000 * div;
90
91 /* we were called to round the frequency, we can now return */
92 if (n == NULL)
93 return;
94
95 /* m is always zero for pll1 */
96 *m = 0;
97
98 /* k is 1 only on these cases */
99 if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
100 *k = 1;
101 else
102 *k = 0;
103
104 /* p will be 3 for divs under 10 */
105 if (div < 10)
106 *p = 3;
107
108 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
109 else if (div < 20 || (div < 32 && (div & 1)))
110 *p = 2;
111
112 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
113 * of divs between 40-62 */
114 else if (div < 40 || (div < 64 && (div & 2)))
115 *p = 1;
116
117 /* any other entries have p = 0 */
118 else
119 *p = 0;
120
121 /* calculate a suitable n based on k and p */
122 div <<= *p;
123 div /= (*k + 1);
124 *n = div / 4;
125}
126
127
128
129/**
130 * sunxi_get_apb1_factors() - calculates m, p factors for APB1
131 * APB1 rate is calculated as follows
132 * rate = (parent_rate >> p) / (m + 1);
133 */
134
135static void sunxi_get_apb1_factors(u32 *freq, u32 parent_rate,
136 u8 *n, u8 *k, u8 *m, u8 *p)
137{
138 u8 calcm, calcp;
139
140 if (parent_rate < *freq)
141 *freq = parent_rate;
142
143 parent_rate = (parent_rate + (*freq - 1)) / *freq;
144
145 /* Invalid rate! */
146 if (parent_rate > 32)
147 return;
148
149 if (parent_rate <= 4)
150 calcp = 0;
151 else if (parent_rate <= 8)
152 calcp = 1;
153 else if (parent_rate <= 16)
154 calcp = 2;
155 else
156 calcp = 3;
157
158 calcm = (parent_rate >> calcp) - 1;
159
160 *freq = (parent_rate >> calcp) / (calcm + 1);
161
162 /* we were called to round the frequency, we can now return */
163 if (n == NULL)
164 return;
165
166 *m = calcm;
167 *p = calcp;
168}
169
170
171
172/**
173 * sunxi_factors_clk_setup() - Setup function for factor clocks
174 */
175
176struct factors_data {
177 struct clk_factors_config *table;
178 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
179};
180
181static struct clk_factors_config pll1_config = {
182 .nshift = 8,
183 .nwidth = 5,
184 .kshift = 4,
185 .kwidth = 2,
186 .mshift = 0,
187 .mwidth = 2,
188 .pshift = 16,
189 .pwidth = 2,
190};
191
192static struct clk_factors_config apb1_config = {
193 .mshift = 0,
194 .mwidth = 5,
195 .pshift = 16,
196 .pwidth = 2,
197};
198
199static const __initconst struct factors_data pll1_data = {
200 .table = &pll1_config,
201 .getter = sunxi_get_pll1_factors,
202};
203
204static const __initconst struct factors_data apb1_data = {
205 .table = &apb1_config,
206 .getter = sunxi_get_apb1_factors,
207};
208
209static void __init sunxi_factors_clk_setup(struct device_node *node,
210 struct factors_data *data)
211{
212 struct clk *clk;
213 const char *clk_name = node->name;
214 const char *parent;
215 void *reg;
216
217 reg = of_iomap(node, 0);
218
219 parent = of_clk_get_parent_name(node, 0);
220
Emilio López5a4fe9b2013-03-27 18:20:42 -0300221 clk = clk_register_factors(NULL, clk_name, parent, 0, reg,
222 data->table, data->getter, &clk_lock);
Emilio Lópeze874a662013-02-25 11:44:26 -0300223
Axel Linee85e9b2013-07-12 16:15:15 +0800224 if (!IS_ERR(clk)) {
Emilio Lópeze874a662013-02-25 11:44:26 -0300225 of_clk_add_provider(node, of_clk_src_simple_get, clk);
226 clk_register_clkdev(clk, clk_name, NULL);
227 }
228}
229
230
231
232/**
233 * sunxi_mux_clk_setup() - Setup function for muxes
234 */
235
236#define SUNXI_MUX_GATE_WIDTH 2
237
238struct mux_data {
239 u8 shift;
240};
241
Giacomo A. Catenazzi61fd58d2013-05-17 10:43:20 -0300242static const __initconst struct mux_data cpu_mux_data = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300243 .shift = 16,
244};
245
246static const __initconst struct mux_data apb1_mux_data = {
247 .shift = 24,
248};
249
250static void __init sunxi_mux_clk_setup(struct device_node *node,
251 struct mux_data *data)
252{
253 struct clk *clk;
254 const char *clk_name = node->name;
Emilio López918d7f62013-03-27 18:20:43 -0300255 const char *parents[5];
Emilio Lópeze874a662013-02-25 11:44:26 -0300256 void *reg;
257 int i = 0;
258
259 reg = of_iomap(node, 0);
260
261 while (i < 5 && (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
262 i++;
263
James Hogan819c1de2013-07-29 12:25:01 +0100264 clk = clk_register_mux(NULL, clk_name, parents, i,
265 CLK_SET_RATE_NO_REPARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300266 data->shift, SUNXI_MUX_GATE_WIDTH,
267 0, &clk_lock);
268
269 if (clk) {
270 of_clk_add_provider(node, of_clk_src_simple_get, clk);
271 clk_register_clkdev(clk, clk_name, NULL);
272 }
273}
274
275
276
277/**
278 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
279 */
280
281#define SUNXI_DIVISOR_WIDTH 2
282
283struct div_data {
284 u8 shift;
285 u8 pow;
286};
287
288static const __initconst struct div_data axi_data = {
289 .shift = 0,
290 .pow = 0,
291};
292
293static const __initconst struct div_data ahb_data = {
294 .shift = 4,
295 .pow = 1,
296};
297
298static const __initconst struct div_data apb0_data = {
299 .shift = 8,
300 .pow = 1,
301};
302
303static void __init sunxi_divider_clk_setup(struct device_node *node,
304 struct div_data *data)
305{
306 struct clk *clk;
307 const char *clk_name = node->name;
308 const char *clk_parent;
309 void *reg;
310
311 reg = of_iomap(node, 0);
312
313 clk_parent = of_clk_get_parent_name(node, 0);
314
315 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
316 reg, data->shift, SUNXI_DIVISOR_WIDTH,
317 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
318 &clk_lock);
319 if (clk) {
320 of_clk_add_provider(node, of_clk_src_simple_get, clk);
321 clk_register_clkdev(clk, clk_name, NULL);
322 }
323}
324
325
Emilio López13569a72013-03-27 18:20:37 -0300326
327/**
328 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
329 */
330
331#define SUNXI_GATES_MAX_SIZE 64
332
333struct gates_data {
334 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
335};
336
Maxime Ripard4f985b42013-04-30 11:56:22 +0200337static const __initconst struct gates_data sun4i_axi_gates_data = {
Emilio López13569a72013-03-27 18:20:37 -0300338 .mask = {1},
339};
340
Maxime Ripard4f985b42013-04-30 11:56:22 +0200341static const __initconst struct gates_data sun4i_ahb_gates_data = {
Emilio López13569a72013-03-27 18:20:37 -0300342 .mask = {0x7F77FFF, 0x14FB3F},
343};
344
Maxime Ripard2371dd82013-07-16 11:21:59 +0200345static const __initconst struct gates_data sun5i_a10s_ahb_gates_data = {
346 .mask = {0x147667e7, 0x185915},
347};
348
Maxime Ripard4f985b42013-04-30 11:56:22 +0200349static const __initconst struct gates_data sun5i_a13_ahb_gates_data = {
350 .mask = {0x107067e7, 0x185111},
351};
352
353static const __initconst struct gates_data sun4i_apb0_gates_data = {
Emilio López13569a72013-03-27 18:20:37 -0300354 .mask = {0x4EF},
355};
356
Maxime Ripard2371dd82013-07-16 11:21:59 +0200357static const __initconst struct gates_data sun5i_a10s_apb0_gates_data = {
358 .mask = {0x469},
359};
360
Maxime Ripard4f985b42013-04-30 11:56:22 +0200361static const __initconst struct gates_data sun5i_a13_apb0_gates_data = {
362 .mask = {0x61},
363};
364
365static const __initconst struct gates_data sun4i_apb1_gates_data = {
Emilio López13569a72013-03-27 18:20:37 -0300366 .mask = {0xFF00F7},
367};
368
Maxime Ripard2371dd82013-07-16 11:21:59 +0200369static const __initconst struct gates_data sun5i_a10s_apb1_gates_data = {
370 .mask = {0xf0007},
371};
372
Maxime Ripard4f985b42013-04-30 11:56:22 +0200373static const __initconst struct gates_data sun5i_a13_apb1_gates_data = {
374 .mask = {0xa0007},
375};
376
Emilio López13569a72013-03-27 18:20:37 -0300377static void __init sunxi_gates_clk_setup(struct device_node *node,
378 struct gates_data *data)
379{
380 struct clk_onecell_data *clk_data;
381 const char *clk_parent;
382 const char *clk_name;
383 void *reg;
384 int qty;
385 int i = 0;
386 int j = 0;
387 int ignore;
388
389 reg = of_iomap(node, 0);
390
391 clk_parent = of_clk_get_parent_name(node, 0);
392
393 /* Worst-case size approximation and memory allocation */
394 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
395 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
396 if (!clk_data)
397 return;
398 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
399 if (!clk_data->clks) {
400 kfree(clk_data);
401 return;
402 }
403
404 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
405 of_property_read_string_index(node, "clock-output-names",
406 j, &clk_name);
407
408 /* No driver claims this clock, but it should remain gated */
409 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
410
411 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
412 clk_parent, ignore,
413 reg + 4 * (i/32), i % 32,
414 0, &clk_lock);
415 WARN_ON(IS_ERR(clk_data->clks[i]));
416
417 j++;
418 }
419
420 /* Adjust to the real max */
421 clk_data->clk_num = i;
422
423 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
424}
425
Emilio Lópeze874a662013-02-25 11:44:26 -0300426/* Matches for of_clk_init */
427static const __initconst struct of_device_id clk_match[] = {
Emilio Lópeze3276992013-03-26 23:39:17 -0300428 {.compatible = "allwinner,sun4i-osc-clk", .data = sunxi_osc_clk_setup,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300429 {}
430};
431
432/* Matches for factors clocks */
433static const __initconst struct of_device_id clk_factors_match[] = {
Emilio Lópeze3276992013-03-26 23:39:17 -0300434 {.compatible = "allwinner,sun4i-pll1-clk", .data = &pll1_data,},
435 {.compatible = "allwinner,sun4i-apb1-clk", .data = &apb1_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300436 {}
437};
438
439/* Matches for divider clocks */
440static const __initconst struct of_device_id clk_div_match[] = {
Emilio Lópeze3276992013-03-26 23:39:17 -0300441 {.compatible = "allwinner,sun4i-axi-clk", .data = &axi_data,},
442 {.compatible = "allwinner,sun4i-ahb-clk", .data = &ahb_data,},
443 {.compatible = "allwinner,sun4i-apb0-clk", .data = &apb0_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300444 {}
445};
446
447/* Matches for mux clocks */
448static const __initconst struct of_device_id clk_mux_match[] = {
Giacomo A. Catenazzi61fd58d2013-05-17 10:43:20 -0300449 {.compatible = "allwinner,sun4i-cpu-clk", .data = &cpu_mux_data,},
Emilio Lópeze3276992013-03-26 23:39:17 -0300450 {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &apb1_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300451 {}
452};
453
Emilio López13569a72013-03-27 18:20:37 -0300454/* Matches for gate clocks */
455static const __initconst struct of_device_id clk_gates_match[] = {
Maxime Ripard4f985b42013-04-30 11:56:22 +0200456 {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
457 {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200458 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200459 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
460 {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200461 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200462 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
463 {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
Maxime Ripard2371dd82013-07-16 11:21:59 +0200464 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
Maxime Ripard4f985b42013-04-30 11:56:22 +0200465 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
Emilio López13569a72013-03-27 18:20:37 -0300466 {}
467};
468
Emilio Lópeze874a662013-02-25 11:44:26 -0300469static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
470 void *function)
471{
472 struct device_node *np;
473 const struct div_data *data;
474 const struct of_device_id *match;
475 void (*setup_function)(struct device_node *, const void *) = function;
476
477 for_each_matching_node(np, clk_match) {
478 match = of_match_node(clk_match, np);
479 data = match->data;
480 setup_function(np, data);
481 }
482}
483
484void __init sunxi_init_clocks(void)
485{
486 /* Register all the simple sunxi clocks on DT */
487 of_clk_init(clk_match);
488
489 /* Register factor clocks */
490 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
491
492 /* Register divider clocks */
493 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
494
495 /* Register mux clocks */
496 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -0300497
498 /* Register gate clocks */
499 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -0300500}