blob: 623fda5e911fb272cb3e4de4f50706ed320e6bfc [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Emilio Lópeze874a662013-02-25 11:44:26 -03002/*
3 * Copyright 2013 Emilio López
4 *
5 * Emilio López <emilio@elopez.com.ar>
Emilio Lópeze874a662013-02-25 11:44:26 -03006 */
7
Stephen Boyd9dfefe82015-06-19 15:00:46 -07008#include <linux/clk.h>
Emilio Lópeze874a662013-02-25 11:44:26 -03009#include <linux/clk-provider.h>
10#include <linux/clkdev.h>
Stephen Boyd62e59c42019-04-18 15:20:22 -070011#include <linux/io.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030012#include <linux/of.h>
13#include <linux/of_address.h>
Hans de Goedecfb00862014-02-07 16:21:49 +010014#include <linux/reset-controller.h>
Stephen Boyd9dfefe82015-06-19 15:00:46 -070015#include <linux/slab.h>
Maxime Ripard601da9d2014-07-04 22:24:52 +020016#include <linux/spinlock.h>
Chen-Yu Tsai7954dfa2014-11-26 15:16:52 +080017#include <linux/log2.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030018
19#include "clk-factors.h"
20
21static DEFINE_SPINLOCK(clk_lock);
22
Emilio López40a5dcb2013-12-23 00:32:32 -030023/* Maximum number of parents our clocks have */
24#define SUNXI_MAX_PARENTS 5
25
Emilio Lópeze874a662013-02-25 11:44:26 -030026/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020027 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
Emilio Lópeze874a662013-02-25 11:44:26 -030028 * PLL1 rate is calculated as follows
29 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
30 * parent_rate is always 24Mhz
31 */
32
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080033static void sun4i_get_pll1_factors(struct factors_request *req)
Emilio Lópeze874a662013-02-25 11:44:26 -030034{
35 u8 div;
36
37 /* Normalize value to a 6M multiple */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080038 div = req->rate / 6000000;
39 req->rate = 6000000 * div;
Emilio Lópeze874a662013-02-25 11:44:26 -030040
41 /* m is always zero for pll1 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080042 req->m = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -030043
44 /* k is 1 only on these cases */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080045 if (req->rate >= 768000000 || req->rate == 42000000 ||
46 req->rate == 54000000)
47 req->k = 1;
Emilio Lópeze874a662013-02-25 11:44:26 -030048 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080049 req->k = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -030050
51 /* p will be 3 for divs under 10 */
52 if (div < 10)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080053 req->p = 3;
Emilio Lópeze874a662013-02-25 11:44:26 -030054
55 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
56 else if (div < 20 || (div < 32 && (div & 1)))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080057 req->p = 2;
Emilio Lópeze874a662013-02-25 11:44:26 -030058
59 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
60 * of divs between 40-62 */
61 else if (div < 40 || (div < 64 && (div & 2)))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080062 req->p = 1;
Emilio Lópeze874a662013-02-25 11:44:26 -030063
64 /* any other entries have p = 0 */
65 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080066 req->p = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -030067
68 /* calculate a suitable n based on k and p */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080069 div <<= req->p;
70 div /= (req->k + 1);
71 req->n = div / 4;
Emilio Lópeze874a662013-02-25 11:44:26 -030072}
73
Maxime Ripard6a721db2013-07-23 23:34:10 +020074/**
75 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
76 * PLL1 rate is calculated as follows
77 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
78 * parent_rate should always be 24MHz
79 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080080static void sun6i_a31_get_pll1_factors(struct factors_request *req)
Maxime Ripard6a721db2013-07-23 23:34:10 +020081{
82 /*
83 * We can operate only on MHz, this will make our life easier
84 * later.
85 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080086 u32 freq_mhz = req->rate / 1000000;
87 u32 parent_freq_mhz = req->parent_rate / 1000000;
Emilio Lópeze874a662013-02-25 11:44:26 -030088
Maxime Ripard6a721db2013-07-23 23:34:10 +020089 /*
90 * Round down the frequency to the closest multiple of either
91 * 6 or 16
92 */
93 u32 round_freq_6 = round_down(freq_mhz, 6);
94 u32 round_freq_16 = round_down(freq_mhz, 16);
95
96 if (round_freq_6 > round_freq_16)
97 freq_mhz = round_freq_6;
98 else
99 freq_mhz = round_freq_16;
100
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800101 req->rate = freq_mhz * 1000000;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200102
103 /* If the frequency is a multiple of 32 MHz, k is always 3 */
104 if (!(freq_mhz % 32))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800105 req->k = 3;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200106 /* If the frequency is a multiple of 9 MHz, k is always 2 */
107 else if (!(freq_mhz % 9))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800108 req->k = 2;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200109 /* If the frequency is a multiple of 8 MHz, k is always 1 */
110 else if (!(freq_mhz % 8))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800111 req->k = 1;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200112 /* Otherwise, we don't use the k factor */
113 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800114 req->k = 0;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200115
116 /*
117 * If the frequency is a multiple of 2 but not a multiple of
118 * 3, m is 3. This is the first time we use 6 here, yet we
119 * will use it on several other places.
120 * We use this number because it's the lowest frequency we can
121 * generate (with n = 0, k = 0, m = 3), so every other frequency
122 * somehow relates to this frequency.
123 */
124 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800125 req->m = 2;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200126 /*
127 * If the frequency is a multiple of 6MHz, but the factor is
128 * odd, m will be 3
129 */
130 else if ((freq_mhz / 6) & 1)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800131 req->m = 3;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200132 /* Otherwise, we end up with m = 1 */
133 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800134 req->m = 1;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200135
136 /* Calculate n thanks to the above factors we already got */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800137 req->n = freq_mhz * (req->m + 1) / ((req->k + 1) * parent_freq_mhz)
138 - 1;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200139
140 /*
141 * If n end up being outbound, and that we can still decrease
142 * m, do it.
143 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800144 if ((req->n + 1) > 31 && (req->m + 1) > 1) {
145 req->n = (req->n + 1) / 2 - 1;
146 req->m = (req->m + 1) / 2 - 1;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200147 }
148}
Emilio Lópeze874a662013-02-25 11:44:26 -0300149
150/**
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800151 * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
152 * PLL1 rate is calculated as follows
153 * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
154 * parent_rate is always 24Mhz
155 */
156
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800157static void sun8i_a23_get_pll1_factors(struct factors_request *req)
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800158{
159 u8 div;
160
161 /* Normalize value to a 6M multiple */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800162 div = req->rate / 6000000;
163 req->rate = 6000000 * div;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800164
165 /* m is always zero for pll1 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800166 req->m = 0;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800167
168 /* k is 1 only on these cases */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800169 if (req->rate >= 768000000 || req->rate == 42000000 ||
170 req->rate == 54000000)
171 req->k = 1;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800172 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800173 req->k = 0;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800174
175 /* p will be 2 for divs under 20 and odd divs under 32 */
176 if (div < 20 || (div < 32 && (div & 1)))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800177 req->p = 2;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800178
179 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
180 * of divs between 40-62 */
181 else if (div < 40 || (div < 64 && (div & 2)))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800182 req->p = 1;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800183
184 /* any other entries have p = 0 */
185 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800186 req->p = 0;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800187
188 /* calculate a suitable n based on k and p */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800189 div <<= req->p;
190 div /= (req->k + 1);
191 req->n = div / 4 - 1;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800192}
193
194/**
Emilio Lópezd584c132013-12-23 00:32:37 -0300195 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
196 * PLL5 rate is calculated as follows
197 * rate = parent_rate * n * (k + 1)
198 * parent_rate is always 24Mhz
199 */
200
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800201static void sun4i_get_pll5_factors(struct factors_request *req)
Emilio Lópezd584c132013-12-23 00:32:37 -0300202{
203 u8 div;
204
205 /* Normalize value to a parent_rate multiple (24M) */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800206 div = req->rate / req->parent_rate;
207 req->rate = req->parent_rate * div;
Emilio Lópezd584c132013-12-23 00:32:37 -0300208
209 if (div < 31)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800210 req->k = 0;
Emilio Lópezd584c132013-12-23 00:32:37 -0300211 else if (div / 2 < 31)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800212 req->k = 1;
Emilio Lópezd584c132013-12-23 00:32:37 -0300213 else if (div / 3 < 31)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800214 req->k = 2;
Emilio Lópezd584c132013-12-23 00:32:37 -0300215 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800216 req->k = 3;
Emilio Lópezd584c132013-12-23 00:32:37 -0300217
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800218 req->n = DIV_ROUND_UP(div, (req->k + 1));
Emilio Lópezd584c132013-12-23 00:32:37 -0300219}
220
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100221/**
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800222 * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6x2
223 * PLL6x2 rate is calculated as follows
224 * rate = parent_rate * (n + 1) * (k + 1)
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100225 * parent_rate is always 24Mhz
226 */
Emilio Lópezd584c132013-12-23 00:32:37 -0300227
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800228static void sun6i_a31_get_pll6_factors(struct factors_request *req)
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100229{
230 u8 div;
231
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800232 /* Normalize value to a parent_rate multiple (24M) */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800233 div = req->rate / req->parent_rate;
234 req->rate = req->parent_rate * div;
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100235
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800236 req->k = div / 32;
237 if (req->k > 3)
238 req->k = 3;
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100239
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800240 req->n = DIV_ROUND_UP(div, (req->k + 1)) - 1;
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100241}
Emilio Lópezd584c132013-12-23 00:32:37 -0300242
243/**
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800244 * sun5i_a13_get_ahb_factors() - calculates m, p factors for AHB
245 * AHB rate is calculated as follows
246 * rate = parent_rate >> p
247 */
248
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800249static void sun5i_a13_get_ahb_factors(struct factors_request *req)
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800250{
251 u32 div;
252
253 /* divide only */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800254 if (req->parent_rate < req->rate)
255 req->rate = req->parent_rate;
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800256
257 /*
258 * user manual says valid speed is 8k ~ 276M, but tests show it
259 * can work at speeds up to 300M, just after reparenting to pll6
260 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800261 if (req->rate < 8000)
262 req->rate = 8000;
263 if (req->rate > 300000000)
264 req->rate = 300000000;
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800265
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800266 div = order_base_2(DIV_ROUND_UP(req->parent_rate, req->rate));
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800267
268 /* p = 0 ~ 3 */
269 if (div > 3)
270 div = 3;
271
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800272 req->rate = req->parent_rate >> div;
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800273
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800274 req->p = div;
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800275}
276
Chen-Yu Tsaia78bb352016-01-25 21:15:45 +0800277#define SUN6I_AHB1_PARENT_PLL6 3
278
279/**
280 * sun6i_a31_get_ahb_factors() - calculates m, p factors for AHB
281 * AHB rate is calculated as follows
282 * rate = parent_rate >> p
283 *
284 * if parent is pll6, then
285 * parent_rate = pll6 rate / (m + 1)
286 */
287
288static void sun6i_get_ahb1_factors(struct factors_request *req)
289{
290 u8 div, calcp, calcm = 1;
291
292 /*
293 * clock can only divide, so we will never be able to achieve
294 * frequencies higher than the parent frequency
295 */
296 if (req->parent_rate && req->rate > req->parent_rate)
297 req->rate = req->parent_rate;
298
299 div = DIV_ROUND_UP(req->parent_rate, req->rate);
300
301 /* calculate pre-divider if parent is pll6 */
302 if (req->parent_index == SUN6I_AHB1_PARENT_PLL6) {
303 if (div < 4)
304 calcp = 0;
305 else if (div / 2 < 4)
306 calcp = 1;
307 else if (div / 4 < 4)
308 calcp = 2;
309 else
310 calcp = 3;
311
312 calcm = DIV_ROUND_UP(div, 1 << calcp);
313 } else {
314 calcp = __roundup_pow_of_two(div);
315 calcp = calcp > 3 ? 3 : calcp;
316 }
317
318 req->rate = (req->parent_rate / calcm) >> calcp;
319 req->p = calcp;
320 req->m = calcm - 1;
321}
322
323/**
324 * sun6i_ahb1_recalc() - calculates AHB clock rate from m, p factors and
325 * parent index
326 */
327static void sun6i_ahb1_recalc(struct factors_request *req)
328{
329 req->rate = req->parent_rate;
330
331 /* apply pre-divider first if parent is pll6 */
332 if (req->parent_index == SUN6I_AHB1_PARENT_PLL6)
333 req->rate /= req->m + 1;
334
335 /* clk divider */
336 req->rate >>= req->p;
337}
338
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800339/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200340 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
Emilio Lópeze874a662013-02-25 11:44:26 -0300341 * APB1 rate is calculated as follows
342 * rate = (parent_rate >> p) / (m + 1);
343 */
344
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800345static void sun4i_get_apb1_factors(struct factors_request *req)
Emilio Lópeze874a662013-02-25 11:44:26 -0300346{
347 u8 calcm, calcp;
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800348 int div;
Emilio Lópeze874a662013-02-25 11:44:26 -0300349
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800350 if (req->parent_rate < req->rate)
351 req->rate = req->parent_rate;
Emilio Lópeze874a662013-02-25 11:44:26 -0300352
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800353 div = DIV_ROUND_UP(req->parent_rate, req->rate);
Emilio Lópeze874a662013-02-25 11:44:26 -0300354
355 /* Invalid rate! */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800356 if (div > 32)
Emilio Lópeze874a662013-02-25 11:44:26 -0300357 return;
358
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800359 if (div <= 4)
Emilio Lópeze874a662013-02-25 11:44:26 -0300360 calcp = 0;
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800361 else if (div <= 8)
Emilio Lópeze874a662013-02-25 11:44:26 -0300362 calcp = 1;
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800363 else if (div <= 16)
Emilio Lópeze874a662013-02-25 11:44:26 -0300364 calcp = 2;
365 else
366 calcp = 3;
367
Stéphan Rafinac953302016-11-04 00:53:56 +0100368 calcm = (div >> calcp) - 1;
Emilio Lópeze874a662013-02-25 11:44:26 -0300369
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800370 req->rate = (req->parent_rate >> calcp) / (calcm + 1);
371 req->m = calcm;
372 req->p = calcp;
Emilio Lópeze874a662013-02-25 11:44:26 -0300373}
374
375
376
Emilio López75517692013-12-23 00:32:39 -0300377
378/**
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800379 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
380 * CLK_OUT rate is calculated as follows
381 * rate = (parent_rate >> p) / (m + 1);
382 */
383
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800384static void sun7i_a20_get_out_factors(struct factors_request *req)
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800385{
386 u8 div, calcm, calcp;
387
388 /* These clocks can only divide, so we will never be able to achieve
389 * frequencies higher than the parent frequency */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800390 if (req->rate > req->parent_rate)
391 req->rate = req->parent_rate;
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800392
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800393 div = DIV_ROUND_UP(req->parent_rate, req->rate);
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800394
395 if (div < 32)
396 calcp = 0;
397 else if (div / 2 < 32)
398 calcp = 1;
399 else if (div / 4 < 32)
400 calcp = 2;
401 else
402 calcp = 3;
403
404 calcm = DIV_ROUND_UP(div, 1 << calcp);
405
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800406 req->rate = (req->parent_rate >> calcp) / calcm;
407 req->m = calcm - 1;
408 req->p = calcp;
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800409}
410
Chen-Yu Tsaie4c6d6c2014-02-10 18:35:47 +0800411/**
Emilio Lópeze874a662013-02-25 11:44:26 -0300412 * sunxi_factors_clk_setup() - Setup function for factor clocks
413 */
414
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800415static const struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300416 .nshift = 8,
417 .nwidth = 5,
418 .kshift = 4,
419 .kwidth = 2,
420 .mshift = 0,
421 .mwidth = 2,
422 .pshift = 16,
423 .pwidth = 2,
424};
425
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800426static const struct clk_factors_config sun6i_a31_pll1_config = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200427 .nshift = 8,
428 .nwidth = 5,
429 .kshift = 4,
430 .kwidth = 2,
431 .mshift = 0,
432 .mwidth = 2,
Hans de Goede76820fc2015-01-24 12:56:32 +0100433 .n_start = 1,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200434};
435
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800436static const struct clk_factors_config sun8i_a23_pll1_config = {
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800437 .nshift = 8,
438 .nwidth = 5,
439 .kshift = 4,
440 .kwidth = 2,
441 .mshift = 0,
442 .mwidth = 2,
443 .pshift = 16,
444 .pwidth = 2,
445 .n_start = 1,
446};
447
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800448static const struct clk_factors_config sun4i_pll5_config = {
Emilio Lópezd584c132013-12-23 00:32:37 -0300449 .nshift = 8,
450 .nwidth = 5,
451 .kshift = 4,
452 .kwidth = 2,
453};
454
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800455static const struct clk_factors_config sun6i_a31_pll6_config = {
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100456 .nshift = 8,
457 .nwidth = 5,
458 .kshift = 4,
459 .kwidth = 2,
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800460 .n_start = 1,
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100461};
462
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800463static const struct clk_factors_config sun5i_a13_ahb_config = {
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800464 .pshift = 4,
465 .pwidth = 2,
466};
467
Chen-Yu Tsaia78bb352016-01-25 21:15:45 +0800468static const struct clk_factors_config sun6i_ahb1_config = {
469 .mshift = 6,
470 .mwidth = 2,
471 .pshift = 4,
472 .pwidth = 2,
473};
474
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800475static const struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300476 .mshift = 0,
477 .mwidth = 5,
478 .pshift = 16,
479 .pwidth = 2,
480};
481
Emilio López75517692013-12-23 00:32:39 -0300482/* user manual says "n" but it's really "p" */
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800483static const struct clk_factors_config sun7i_a20_out_config = {
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800484 .mshift = 8,
485 .mwidth = 5,
486 .pshift = 20,
487 .pwidth = 2,
488};
489
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530490static const struct factors_data sun4i_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300491 .enable = 31,
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200492 .table = &sun4i_pll1_config,
493 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300494};
495
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530496static const struct factors_data sun6i_a31_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300497 .enable = 31,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200498 .table = &sun6i_a31_pll1_config,
499 .getter = sun6i_a31_get_pll1_factors,
500};
501
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800502static const struct factors_data sun8i_a23_pll1_data __initconst = {
503 .enable = 31,
504 .table = &sun8i_a23_pll1_config,
505 .getter = sun8i_a23_get_pll1_factors,
506};
507
Emilio López5a8ddf22014-03-19 15:19:30 -0300508static const struct factors_data sun7i_a20_pll4_data __initconst = {
509 .enable = 31,
510 .table = &sun4i_pll5_config,
511 .getter = sun4i_get_pll5_factors,
512};
513
Emilio Lópezd584c132013-12-23 00:32:37 -0300514static const struct factors_data sun4i_pll5_data __initconst = {
515 .enable = 31,
516 .table = &sun4i_pll5_config,
517 .getter = sun4i_get_pll5_factors,
518};
519
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100520static const struct factors_data sun6i_a31_pll6_data __initconst = {
521 .enable = 31,
522 .table = &sun6i_a31_pll6_config,
523 .getter = sun6i_a31_get_pll6_factors,
524};
525
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800526static const struct factors_data sun5i_a13_ahb_data __initconst = {
527 .mux = 6,
528 .muxmask = BIT(1) | BIT(0),
529 .table = &sun5i_a13_ahb_config,
530 .getter = sun5i_a13_get_ahb_factors,
531};
532
Chen-Yu Tsaia78bb352016-01-25 21:15:45 +0800533static const struct factors_data sun6i_ahb1_data __initconst = {
534 .mux = 12,
535 .muxmask = BIT(1) | BIT(0),
536 .table = &sun6i_ahb1_config,
537 .getter = sun6i_get_ahb1_factors,
538 .recalc = sun6i_ahb1_recalc,
539};
540
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530541static const struct factors_data sun4i_apb1_data __initconst = {
Emilio López93746e72014-11-06 11:40:29 +0800542 .mux = 24,
543 .muxmask = BIT(1) | BIT(0),
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200544 .table = &sun4i_apb1_config,
545 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300546};
547
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800548static const struct factors_data sun7i_a20_out_data __initconst = {
549 .enable = 31,
550 .mux = 24,
Chen-Yu Tsaie94f8cb32014-10-20 22:10:26 +0800551 .muxmask = BIT(1) | BIT(0),
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800552 .table = &sun7i_a20_out_config,
553 .getter = sun7i_a20_get_out_factors,
554};
555
Emilio López5f4e0be2013-12-23 00:32:36 -0300556static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
Maxime Ripard601da9d2014-07-04 22:24:52 +0200557 const struct factors_data *data)
Emilio Lópeze874a662013-02-25 11:44:26 -0300558{
Hans de Goede7c74c222014-11-23 14:38:07 +0100559 void __iomem *reg;
560
561 reg = of_iomap(node, 0);
562 if (!reg) {
Rob Herringe665f022018-08-28 10:44:29 -0500563 pr_err("Could not get registers for factors-clk: %pOFn\n",
564 node);
Hans de Goede7c74c222014-11-23 14:38:07 +0100565 return NULL;
566 }
567
568 return sunxi_factors_register(node, data, &clk_lock, reg);
Emilio Lópeze874a662013-02-25 11:44:26 -0300569}
570
Maxime Ripardc0872302016-02-02 09:07:22 +0100571static void __init sun4i_pll1_clk_setup(struct device_node *node)
572{
573 sunxi_factors_clk_setup(node, &sun4i_pll1_data);
574}
575CLK_OF_DECLARE(sun4i_pll1, "allwinner,sun4i-a10-pll1-clk",
576 sun4i_pll1_clk_setup);
577
578static void __init sun6i_pll1_clk_setup(struct device_node *node)
579{
580 sunxi_factors_clk_setup(node, &sun6i_a31_pll1_data);
581}
582CLK_OF_DECLARE(sun6i_pll1, "allwinner,sun6i-a31-pll1-clk",
583 sun6i_pll1_clk_setup);
584
585static void __init sun8i_pll1_clk_setup(struct device_node *node)
586{
587 sunxi_factors_clk_setup(node, &sun8i_a23_pll1_data);
588}
589CLK_OF_DECLARE(sun8i_pll1, "allwinner,sun8i-a23-pll1-clk",
590 sun8i_pll1_clk_setup);
591
592static void __init sun7i_pll4_clk_setup(struct device_node *node)
593{
594 sunxi_factors_clk_setup(node, &sun7i_a20_pll4_data);
595}
596CLK_OF_DECLARE(sun7i_pll4, "allwinner,sun7i-a20-pll4-clk",
597 sun7i_pll4_clk_setup);
598
599static void __init sun5i_ahb_clk_setup(struct device_node *node)
600{
601 sunxi_factors_clk_setup(node, &sun5i_a13_ahb_data);
602}
603CLK_OF_DECLARE(sun5i_ahb, "allwinner,sun5i-a13-ahb-clk",
604 sun5i_ahb_clk_setup);
605
Chen-Yu Tsaia78bb352016-01-25 21:15:45 +0800606static void __init sun6i_ahb1_clk_setup(struct device_node *node)
607{
608 sunxi_factors_clk_setup(node, &sun6i_ahb1_data);
609}
610CLK_OF_DECLARE(sun6i_a31_ahb1, "allwinner,sun6i-a31-ahb1-clk",
611 sun6i_ahb1_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -0300612
Maxime Ripardc0872302016-02-02 09:07:22 +0100613static void __init sun4i_apb1_clk_setup(struct device_node *node)
614{
615 sunxi_factors_clk_setup(node, &sun4i_apb1_data);
616}
617CLK_OF_DECLARE(sun4i_apb1, "allwinner,sun4i-a10-apb1-clk",
618 sun4i_apb1_clk_setup);
619
620static void __init sun7i_out_clk_setup(struct device_node *node)
621{
622 sunxi_factors_clk_setup(node, &sun7i_a20_out_data);
623}
624CLK_OF_DECLARE(sun7i_out, "allwinner,sun7i-a20-out-clk",
625 sun7i_out_clk_setup);
626
Emilio Lópeze874a662013-02-25 11:44:26 -0300627
628/**
629 * sunxi_mux_clk_setup() - Setup function for muxes
630 */
631
632#define SUNXI_MUX_GATE_WIDTH 2
633
634struct mux_data {
635 u8 shift;
636};
637
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530638static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300639 .shift = 16,
640};
641
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530642static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200643 .shift = 12,
644};
645
Jens Kuskeab6e23a2015-12-04 22:24:40 +0100646static const struct mux_data sun8i_h3_ahb2_mux_data __initconst = {
647 .shift = 0,
648};
649
Maxime Ripard96f185a2016-02-02 09:47:10 +0100650static struct clk * __init sunxi_mux_clk_setup(struct device_node *node,
Stephen Boyd9919d442018-01-02 16:50:27 -0800651 const struct mux_data *data,
652 unsigned long flags)
Emilio Lópeze874a662013-02-25 11:44:26 -0300653{
654 struct clk *clk;
655 const char *clk_name = node->name;
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300656 const char *parents[SUNXI_MAX_PARENTS];
Emilio López89a94562014-07-28 00:49:42 -0300657 void __iomem *reg;
Dinh Nguyen8a53fb22015-07-06 22:59:05 -0500658 int i;
Emilio Lópeze874a662013-02-25 11:44:26 -0300659
660 reg = of_iomap(node, 0);
Andre Przywara72360b92016-02-16 10:46:06 +0000661 if (!reg) {
Rob Herring16673932017-07-18 16:42:52 -0500662 pr_err("Could not map registers for mux-clk: %pOF\n", node);
Andre Przywara72360b92016-02-16 10:46:06 +0000663 return NULL;
664 }
Emilio Lópeze874a662013-02-25 11:44:26 -0300665
Dinh Nguyen8a53fb22015-07-06 22:59:05 -0500666 i = of_clk_parent_fill(node, parents, SUNXI_MAX_PARENTS);
Andre Przywarad221b7a2016-02-01 17:39:27 +0000667 if (of_property_read_string(node, "clock-output-names", &clk_name)) {
Rob Herring16673932017-07-18 16:42:52 -0500668 pr_err("%s: could not read clock-output-names from \"%pOF\"\n",
669 __func__, node);
Andre Przywarad221b7a2016-02-01 17:39:27 +0000670 goto out_unmap;
671 }
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800672
James Hogan819c1de2013-07-29 12:25:01 +0100673 clk = clk_register_mux(NULL, clk_name, parents, i,
Stephen Boyd9919d442018-01-02 16:50:27 -0800674 CLK_SET_RATE_PARENT | flags, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300675 data->shift, SUNXI_MUX_GATE_WIDTH,
676 0, &clk_lock);
677
Andre Przywarad221b7a2016-02-01 17:39:27 +0000678 if (IS_ERR(clk)) {
Andre Przywara72360b92016-02-16 10:46:06 +0000679 pr_err("%s: failed to register mux clock %s: %ld\n", __func__,
680 clk_name, PTR_ERR(clk));
Andre Przywarad221b7a2016-02-01 17:39:27 +0000681 goto out_unmap;
Emilio Lópeze874a662013-02-25 11:44:26 -0300682 }
Andre Przywarad221b7a2016-02-01 17:39:27 +0000683
Andre Przywara72360b92016-02-16 10:46:06 +0000684 if (of_clk_add_provider(node, of_clk_src_simple_get, clk)) {
685 pr_err("%s: failed to add clock provider for %s\n",
686 __func__, clk_name);
687 clk_unregister_divider(clk);
688 goto out_unmap;
689 }
Maxime Ripard96f185a2016-02-02 09:47:10 +0100690
691 return clk;
Andre Przywarad221b7a2016-02-01 17:39:27 +0000692out_unmap:
693 iounmap(reg);
Maxime Ripard96f185a2016-02-02 09:47:10 +0100694 return NULL;
Emilio Lópeze874a662013-02-25 11:44:26 -0300695}
696
Maxime Ripardc0872302016-02-02 09:07:22 +0100697static void __init sun4i_cpu_clk_setup(struct device_node *node)
698{
Maxime Ripardc0872302016-02-02 09:07:22 +0100699 /* Protect CPU clock */
Stephen Boyd9919d442018-01-02 16:50:27 -0800700 sunxi_mux_clk_setup(node, &sun4i_cpu_mux_data, CLK_IS_CRITICAL);
Maxime Ripardc0872302016-02-02 09:07:22 +0100701}
702CLK_OF_DECLARE(sun4i_cpu, "allwinner,sun4i-a10-cpu-clk",
703 sun4i_cpu_clk_setup);
704
705static void __init sun6i_ahb1_mux_clk_setup(struct device_node *node)
706{
Stephen Boyd9919d442018-01-02 16:50:27 -0800707 sunxi_mux_clk_setup(node, &sun6i_a31_ahb1_mux_data, 0);
Maxime Ripardc0872302016-02-02 09:07:22 +0100708}
709CLK_OF_DECLARE(sun6i_ahb1_mux, "allwinner,sun6i-a31-ahb1-mux-clk",
710 sun6i_ahb1_mux_clk_setup);
711
712static void __init sun8i_ahb2_clk_setup(struct device_node *node)
713{
Stephen Boyd9919d442018-01-02 16:50:27 -0800714 sunxi_mux_clk_setup(node, &sun8i_h3_ahb2_mux_data, 0);
Maxime Ripardc0872302016-02-02 09:07:22 +0100715}
716CLK_OF_DECLARE(sun8i_ahb2, "allwinner,sun8i-h3-ahb2-clk",
717 sun8i_ahb2_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -0300718
719
720/**
721 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
722 */
723
Emilio Lópeze874a662013-02-25 11:44:26 -0300724struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200725 u8 shift;
726 u8 pow;
727 u8 width;
Chen-Yu Tsaiea5671b2014-06-26 23:55:42 +0800728 const struct clk_div_table *table;
Emilio Lópeze874a662013-02-25 11:44:26 -0300729};
730
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530731static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200732 .shift = 0,
733 .pow = 0,
734 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300735};
736
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800737static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
738 { .val = 0, .div = 1 },
739 { .val = 1, .div = 2 },
740 { .val = 2, .div = 3 },
741 { .val = 3, .div = 4 },
742 { .val = 4, .div = 4 },
743 { .val = 5, .div = 4 },
744 { .val = 6, .div = 4 },
745 { .val = 7, .div = 4 },
746 { } /* sentinel */
747};
748
749static const struct div_data sun8i_a23_axi_data __initconst = {
750 .width = 3,
751 .table = sun8i_a23_axi_table,
752};
753
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530754static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200755 .shift = 4,
756 .pow = 1,
757 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300758};
759
Chen-Yu Tsaicfe4c932014-09-06 14:45:10 +0800760static const struct clk_div_table sun4i_apb0_table[] __initconst = {
761 { .val = 0, .div = 2 },
762 { .val = 1, .div = 2 },
763 { .val = 2, .div = 4 },
764 { .val = 3, .div = 8 },
765 { } /* sentinel */
766};
767
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530768static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200769 .shift = 8,
770 .pow = 1,
771 .width = 2,
Chen-Yu Tsaicfe4c932014-09-06 14:45:10 +0800772 .table = sun4i_apb0_table,
Emilio Lópeze874a662013-02-25 11:44:26 -0300773};
774
775static void __init sunxi_divider_clk_setup(struct device_node *node,
Maxime Ripard5b5226d2016-02-02 09:47:11 +0100776 const struct div_data *data)
Emilio Lópeze874a662013-02-25 11:44:26 -0300777{
778 struct clk *clk;
779 const char *clk_name = node->name;
780 const char *clk_parent;
Emilio López89a94562014-07-28 00:49:42 -0300781 void __iomem *reg;
Emilio Lópeze874a662013-02-25 11:44:26 -0300782
783 reg = of_iomap(node, 0);
Andre Przywarab26803e2016-02-16 10:46:07 +0000784 if (!reg) {
Rob Herring16673932017-07-18 16:42:52 -0500785 pr_err("Could not map registers for mux-clk: %pOF\n", node);
Andre Przywarab26803e2016-02-16 10:46:07 +0000786 return;
787 }
Emilio Lópeze874a662013-02-25 11:44:26 -0300788
789 clk_parent = of_clk_get_parent_name(node, 0);
790
Andre Przywarab26803e2016-02-16 10:46:07 +0000791 if (of_property_read_string(node, "clock-output-names", &clk_name)) {
Rob Herring16673932017-07-18 16:42:52 -0500792 pr_err("%s: could not read clock-output-names from \"%pOF\"\n",
793 __func__, node);
Andre Przywarab26803e2016-02-16 10:46:07 +0000794 goto out_unmap;
795 }
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800796
Chen-Yu Tsaiea5671b2014-06-26 23:55:42 +0800797 clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
798 reg, data->shift, data->width,
799 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
800 data->table, &clk_lock);
Andre Przywarab26803e2016-02-16 10:46:07 +0000801 if (IS_ERR(clk)) {
802 pr_err("%s: failed to register divider clock %s: %ld\n",
803 __func__, clk_name, PTR_ERR(clk));
804 goto out_unmap;
805 }
806
807 if (of_clk_add_provider(node, of_clk_src_simple_get, clk)) {
808 pr_err("%s: failed to add clock provider for %s\n",
809 __func__, clk_name);
810 goto out_unregister;
811 }
812
813 if (clk_register_clkdev(clk, clk_name, NULL)) {
814 of_clk_del_provider(node);
815 goto out_unregister;
816 }
817
818 return;
819out_unregister:
820 clk_unregister_divider(clk);
821
822out_unmap:
823 iounmap(reg);
Emilio Lópeze874a662013-02-25 11:44:26 -0300824}
825
Maxime Ripardc0872302016-02-02 09:07:22 +0100826static void __init sun4i_ahb_clk_setup(struct device_node *node)
827{
828 sunxi_divider_clk_setup(node, &sun4i_ahb_data);
829}
830CLK_OF_DECLARE(sun4i_ahb, "allwinner,sun4i-a10-ahb-clk",
831 sun4i_ahb_clk_setup);
832
833static void __init sun4i_apb0_clk_setup(struct device_node *node)
834{
835 sunxi_divider_clk_setup(node, &sun4i_apb0_data);
836}
837CLK_OF_DECLARE(sun4i_apb0, "allwinner,sun4i-a10-apb0-clk",
838 sun4i_apb0_clk_setup);
839
840static void __init sun4i_axi_clk_setup(struct device_node *node)
841{
842 sunxi_divider_clk_setup(node, &sun4i_axi_data);
843}
844CLK_OF_DECLARE(sun4i_axi, "allwinner,sun4i-a10-axi-clk",
845 sun4i_axi_clk_setup);
846
847static void __init sun8i_axi_clk_setup(struct device_node *node)
848{
849 sunxi_divider_clk_setup(node, &sun8i_a23_axi_data);
850}
851CLK_OF_DECLARE(sun8i_axi, "allwinner,sun8i-a23-axi-clk",
852 sun8i_axi_clk_setup);
853
Emilio Lópeze874a662013-02-25 11:44:26 -0300854
Emilio López13569a72013-03-27 18:20:37 -0300855
856/**
857 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
858 */
859
860#define SUNXI_GATES_MAX_SIZE 64
861
862struct gates_data {
863 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
864};
865
Emilio Lópezd584c132013-12-23 00:32:37 -0300866/**
867 * sunxi_divs_clk_setup() helper data
868 */
869
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800870#define SUNXI_DIVS_MAX_QTY 4
Emilio Lópezd584c132013-12-23 00:32:37 -0300871#define SUNXI_DIVISOR_WIDTH 2
872
873struct divs_data {
874 const struct factors_data *factors; /* data for the factor clock */
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800875 int ndivs; /* number of outputs */
876 /*
877 * List of outputs. Refer to the diagram for sunxi_divs_clk_setup():
878 * self or base factor clock refers to the output from the pll
879 * itself. The remaining refer to fixed or configurable divider
880 * outputs.
881 */
Emilio Lópezd584c132013-12-23 00:32:37 -0300882 struct {
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800883 u8 self; /* is it the base factor clock? (only one) */
Emilio Lópezd584c132013-12-23 00:32:37 -0300884 u8 fixed; /* is it a fixed divisor? if not... */
885 struct clk_div_table *table; /* is it a table based divisor? */
886 u8 shift; /* otherwise it's a normal divisor with this shift */
887 u8 pow; /* is it power-of-two based? */
888 u8 gate; /* is it independently gateable? */
Stephen Boyd9919d442018-01-02 16:50:27 -0800889 bool critical;
Emilio Lópezd584c132013-12-23 00:32:37 -0300890 } div[SUNXI_DIVS_MAX_QTY];
891};
892
893static struct clk_div_table pll6_sata_tbl[] = {
894 { .val = 0, .div = 6, },
895 { .val = 1, .div = 12, },
896 { .val = 2, .div = 18, },
897 { .val = 3, .div = 24, },
898 { } /* sentinel */
899};
900
901static const struct divs_data pll5_divs_data __initconst = {
902 .factors = &sun4i_pll5_data,
Chen-Yu Tsai13d52f62014-11-13 02:08:30 +0800903 .ndivs = 2,
Emilio Lópezd584c132013-12-23 00:32:37 -0300904 .div = {
Stephen Boyd9919d442018-01-02 16:50:27 -0800905 /* Protect PLL5_DDR */
906 { .shift = 0, .pow = 0, .critical = true }, /* M, DDR */
Emilio Lópezd584c132013-12-23 00:32:37 -0300907 { .shift = 16, .pow = 1, }, /* P, other */
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800908 /* No output for the base factor clock */
Emilio Lópezd584c132013-12-23 00:32:37 -0300909 }
910};
911
912static const struct divs_data pll6_divs_data __initconst = {
Jens Kuskeff2bb892016-03-18 09:44:15 +0000913 .factors = &sun4i_pll5_data,
Chen-Yu Tsaif1017962015-03-25 01:22:08 +0800914 .ndivs = 4,
Emilio Lópezd584c132013-12-23 00:32:37 -0300915 .div = {
916 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
917 { .fixed = 2 }, /* P, other */
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800918 { .self = 1 }, /* base factor clock, 2x */
Chen-Yu Tsaif1017962015-03-25 01:22:08 +0800919 { .fixed = 4 }, /* pll6 / 4, used as ahb input */
Emilio Lópezd584c132013-12-23 00:32:37 -0300920 }
921};
922
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800923static const struct divs_data sun6i_a31_pll6_divs_data __initconst = {
924 .factors = &sun6i_a31_pll6_data,
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800925 .ndivs = 2,
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800926 .div = {
927 { .fixed = 2 }, /* normal output */
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800928 { .self = 1 }, /* base factor clock, 2x */
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800929 }
930};
931
Emilio Lópezd584c132013-12-23 00:32:37 -0300932/**
933 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
934 *
935 * These clocks look something like this
936 * ________________________
937 * | ___divisor 1---|----> to consumer
938 * parent >--| pll___/___divisor 2---|----> to consumer
939 * | \_______________|____> to consumer
940 * |________________________|
941 */
942
Maxime Ripard96f185a2016-02-02 09:47:10 +0100943static struct clk ** __init sunxi_divs_clk_setup(struct device_node *node,
Maxime Ripard5b5226d2016-02-02 09:47:11 +0100944 const struct divs_data *data)
Emilio Lópezd584c132013-12-23 00:32:37 -0300945{
946 struct clk_onecell_data *clk_data;
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +0800947 const char *parent;
Emilio Lópezd584c132013-12-23 00:32:37 -0300948 const char *clk_name;
949 struct clk **clks, *pclk;
950 struct clk_hw *gate_hw, *rate_hw;
951 const struct clk_ops *rate_ops;
952 struct clk_gate *gate = NULL;
953 struct clk_fixed_factor *fix_factor;
954 struct clk_divider *divider;
Jens Kuskeff2bb892016-03-18 09:44:15 +0000955 struct factors_data factors = *data->factors;
956 char *derived_name = NULL;
Emilio López89a94562014-07-28 00:49:42 -0300957 void __iomem *reg;
Chen-Yu Tsai13d52f62014-11-13 02:08:30 +0800958 int ndivs = SUNXI_DIVS_MAX_QTY, i = 0;
Emilio Lópezd584c132013-12-23 00:32:37 -0300959 int flags, clkflags;
960
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800961 /* if number of children known, use it */
962 if (data->ndivs)
963 ndivs = data->ndivs;
964
Jens Kuskeff2bb892016-03-18 09:44:15 +0000965 /* Try to find a name for base factor clock */
966 for (i = 0; i < ndivs; i++) {
967 if (data->div[i].self) {
968 of_property_read_string_index(node, "clock-output-names",
969 i, &factors.name);
970 break;
971 }
972 }
973 /* If we don't have a .self clk use the first output-name up to '_' */
974 if (factors.name == NULL) {
975 char *endp;
976
977 of_property_read_string_index(node, "clock-output-names",
978 0, &clk_name);
979 endp = strchr(clk_name, '_');
980 if (endp) {
981 derived_name = kstrndup(clk_name, endp - clk_name,
982 GFP_KERNEL);
983 factors.name = derived_name;
984 } else {
985 factors.name = clk_name;
986 }
987 }
988
Emilio Lópezd584c132013-12-23 00:32:37 -0300989 /* Set up factor clock that we will be dividing */
Jens Kuskeff2bb892016-03-18 09:44:15 +0000990 pclk = sunxi_factors_clk_setup(node, &factors);
Andre Przywarad3313282016-02-16 10:46:08 +0000991 if (!pclk)
992 return NULL;
Jens Kuskeff2bb892016-03-18 09:44:15 +0000993
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +0800994 parent = __clk_get_name(pclk);
Jens Kuskeff2bb892016-03-18 09:44:15 +0000995 kfree(derived_name);
Emilio Lópezd584c132013-12-23 00:32:37 -0300996
997 reg = of_iomap(node, 0);
Andre Przywarad3313282016-02-16 10:46:08 +0000998 if (!reg) {
Rob Herring16673932017-07-18 16:42:52 -0500999 pr_err("Could not map registers for divs-clk: %pOF\n", node);
Andre Przywarad3313282016-02-16 10:46:08 +00001000 return NULL;
1001 }
Emilio Lópezd584c132013-12-23 00:32:37 -03001002
1003 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1004 if (!clk_data)
Andre Przywarad3313282016-02-16 10:46:08 +00001005 goto out_unmap;
Emilio Lópezd584c132013-12-23 00:32:37 -03001006
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +08001007 clks = kcalloc(ndivs, sizeof(*clks), GFP_KERNEL);
Emilio Lópezd584c132013-12-23 00:32:37 -03001008 if (!clks)
1009 goto free_clkdata;
1010
1011 clk_data->clks = clks;
1012
1013 /* It's not a good idea to have automatic reparenting changing
1014 * our RAM clock! */
1015 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
1016
Chen-Yu Tsai13d52f62014-11-13 02:08:30 +08001017 for (i = 0; i < ndivs; i++) {
Emilio Lópezd584c132013-12-23 00:32:37 -03001018 if (of_property_read_string_index(node, "clock-output-names",
1019 i, &clk_name) != 0)
1020 break;
1021
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +08001022 /* If this is the base factor clock, only update clks */
1023 if (data->div[i].self) {
1024 clk_data->clks[i] = pclk;
1025 continue;
1026 }
1027
Emilio Lópezd584c132013-12-23 00:32:37 -03001028 gate_hw = NULL;
1029 rate_hw = NULL;
1030 rate_ops = NULL;
1031
1032 /* If this leaf clock can be gated, create a gate */
1033 if (data->div[i].gate) {
1034 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1035 if (!gate)
1036 goto free_clks;
1037
1038 gate->reg = reg;
1039 gate->bit_idx = data->div[i].gate;
1040 gate->lock = &clk_lock;
1041
1042 gate_hw = &gate->hw;
1043 }
1044
1045 /* Leaves can be fixed or configurable divisors */
1046 if (data->div[i].fixed) {
1047 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1048 if (!fix_factor)
1049 goto free_gate;
1050
1051 fix_factor->mult = 1;
1052 fix_factor->div = data->div[i].fixed;
1053
1054 rate_hw = &fix_factor->hw;
1055 rate_ops = &clk_fixed_factor_ops;
1056 } else {
1057 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1058 if (!divider)
1059 goto free_gate;
1060
1061 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1062
1063 divider->reg = reg;
1064 divider->shift = data->div[i].shift;
1065 divider->width = SUNXI_DIVISOR_WIDTH;
1066 divider->flags = flags;
1067 divider->lock = &clk_lock;
1068 divider->table = data->div[i].table;
1069
1070 rate_hw = &divider->hw;
1071 rate_ops = &clk_divider_ops;
1072 }
1073
1074 /* Wrap the (potential) gate and the divisor on a composite
1075 * clock to unify them */
1076 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1077 NULL, NULL,
1078 rate_hw, rate_ops,
1079 gate_hw, &clk_gate_ops,
Stephen Boyd9919d442018-01-02 16:50:27 -08001080 clkflags |
1081 data->div[i].critical ?
1082 CLK_IS_CRITICAL : 0);
Emilio Lópezd584c132013-12-23 00:32:37 -03001083
1084 WARN_ON(IS_ERR(clk_data->clks[i]));
Emilio Lópezd584c132013-12-23 00:32:37 -03001085 }
1086
Emilio Lópezd584c132013-12-23 00:32:37 -03001087 /* Adjust to the real max */
1088 clk_data->clk_num = i;
1089
Andre Przywarad3313282016-02-16 10:46:08 +00001090 if (of_clk_add_provider(node, of_clk_src_onecell_get, clk_data)) {
1091 pr_err("%s: failed to add clock provider for %s\n",
1092 __func__, clk_name);
1093 goto free_gate;
1094 }
Emilio Lópezd584c132013-12-23 00:32:37 -03001095
Maxime Ripard96f185a2016-02-02 09:47:10 +01001096 return clks;
Emilio Lópezd584c132013-12-23 00:32:37 -03001097free_gate:
1098 kfree(gate);
1099free_clks:
1100 kfree(clks);
1101free_clkdata:
1102 kfree(clk_data);
Andre Przywarad3313282016-02-16 10:46:08 +00001103out_unmap:
1104 iounmap(reg);
Maxime Ripard96f185a2016-02-02 09:47:10 +01001105 return NULL;
Emilio Lópezd584c132013-12-23 00:32:37 -03001106}
1107
Maxime Ripardc0872302016-02-02 09:07:22 +01001108static void __init sun4i_pll5_clk_setup(struct device_node *node)
1109{
Stephen Boyd9919d442018-01-02 16:50:27 -08001110 sunxi_divs_clk_setup(node, &pll5_divs_data);
Maxime Ripardc0872302016-02-02 09:07:22 +01001111}
1112CLK_OF_DECLARE(sun4i_pll5, "allwinner,sun4i-a10-pll5-clk",
1113 sun4i_pll5_clk_setup);
1114
1115static void __init sun4i_pll6_clk_setup(struct device_node *node)
1116{
1117 sunxi_divs_clk_setup(node, &pll6_divs_data);
1118}
1119CLK_OF_DECLARE(sun4i_pll6, "allwinner,sun4i-a10-pll6-clk",
1120 sun4i_pll6_clk_setup);
1121
1122static void __init sun6i_pll6_clk_setup(struct device_node *node)
1123{
1124 sunxi_divs_clk_setup(node, &sun6i_a31_pll6_divs_data);
1125}
1126CLK_OF_DECLARE(sun6i_pll6, "allwinner,sun6i-a31-pll6-clk",
1127 sun6i_pll6_clk_setup);
Jean-Francois Moine5ed400d2016-03-30 18:43:29 +02001128
1129/*
1130 * sun6i display
1131 *
1132 * rate = parent_rate / (m + 1);
1133 */
1134static void sun6i_display_factors(struct factors_request *req)
1135{
1136 u8 m;
1137
1138 if (req->rate > req->parent_rate)
1139 req->rate = req->parent_rate;
1140
1141 m = DIV_ROUND_UP(req->parent_rate, req->rate);
1142
1143 req->rate = req->parent_rate / m;
1144 req->m = m - 1;
1145}
1146
1147static const struct clk_factors_config sun6i_display_config = {
1148 .mshift = 0,
1149 .mwidth = 4,
1150};
1151
1152static const struct factors_data sun6i_display_data __initconst = {
1153 .enable = 31,
1154 .mux = 24,
1155 .muxmask = BIT(2) | BIT(1) | BIT(0),
1156 .table = &sun6i_display_config,
1157 .getter = sun6i_display_factors,
1158};
1159
1160static void __init sun6i_display_setup(struct device_node *node)
1161{
1162 sunxi_factors_clk_setup(node, &sun6i_display_data);
1163}
1164CLK_OF_DECLARE(sun6i_display, "allwinner,sun6i-a31-display-clk",
1165 sun6i_display_setup);