blob: f5b1c00673650cf6d617c514af3405fd6813de81 [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
Stephen Boyd9dfefe82015-06-19 15:00:46 -070017#include <linux/clk.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030018#include <linux/clk-provider.h>
19#include <linux/clkdev.h>
Stephen Boyd62e59c42019-04-18 15:20:22 -070020#include <linux/io.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030021#include <linux/of.h>
22#include <linux/of_address.h>
Hans de Goedecfb00862014-02-07 16:21:49 +010023#include <linux/reset-controller.h>
Stephen Boyd9dfefe82015-06-19 15:00:46 -070024#include <linux/slab.h>
Maxime Ripard601da9d2014-07-04 22:24:52 +020025#include <linux/spinlock.h>
Chen-Yu Tsai7954dfa2014-11-26 15:16:52 +080026#include <linux/log2.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030027
28#include "clk-factors.h"
29
30static DEFINE_SPINLOCK(clk_lock);
31
Emilio López40a5dcb2013-12-23 00:32:32 -030032/* Maximum number of parents our clocks have */
33#define SUNXI_MAX_PARENTS 5
34
Emilio Lópeze874a662013-02-25 11:44:26 -030035/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020036 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
Emilio Lópeze874a662013-02-25 11:44:26 -030037 * PLL1 rate is calculated as follows
38 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
39 * parent_rate is always 24Mhz
40 */
41
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080042static void sun4i_get_pll1_factors(struct factors_request *req)
Emilio Lópeze874a662013-02-25 11:44:26 -030043{
44 u8 div;
45
46 /* Normalize value to a 6M multiple */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080047 div = req->rate / 6000000;
48 req->rate = 6000000 * div;
Emilio Lópeze874a662013-02-25 11:44:26 -030049
50 /* m is always zero for pll1 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080051 req->m = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -030052
53 /* k is 1 only on these cases */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080054 if (req->rate >= 768000000 || req->rate == 42000000 ||
55 req->rate == 54000000)
56 req->k = 1;
Emilio Lópeze874a662013-02-25 11:44:26 -030057 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080058 req->k = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -030059
60 /* p will be 3 for divs under 10 */
61 if (div < 10)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080062 req->p = 3;
Emilio Lópeze874a662013-02-25 11:44:26 -030063
64 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
65 else if (div < 20 || (div < 32 && (div & 1)))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080066 req->p = 2;
Emilio Lópeze874a662013-02-25 11:44:26 -030067
68 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
69 * of divs between 40-62 */
70 else if (div < 40 || (div < 64 && (div & 2)))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080071 req->p = 1;
Emilio Lópeze874a662013-02-25 11:44:26 -030072
73 /* any other entries have p = 0 */
74 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080075 req->p = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -030076
77 /* calculate a suitable n based on k and p */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080078 div <<= req->p;
79 div /= (req->k + 1);
80 req->n = div / 4;
Emilio Lópeze874a662013-02-25 11:44:26 -030081}
82
Maxime Ripard6a721db2013-07-23 23:34:10 +020083/**
84 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
85 * PLL1 rate is calculated as follows
86 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
87 * parent_rate should always be 24MHz
88 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080089static void sun6i_a31_get_pll1_factors(struct factors_request *req)
Maxime Ripard6a721db2013-07-23 23:34:10 +020090{
91 /*
92 * We can operate only on MHz, this will make our life easier
93 * later.
94 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080095 u32 freq_mhz = req->rate / 1000000;
96 u32 parent_freq_mhz = req->parent_rate / 1000000;
Emilio Lópeze874a662013-02-25 11:44:26 -030097
Maxime Ripard6a721db2013-07-23 23:34:10 +020098 /*
99 * Round down the frequency to the closest multiple of either
100 * 6 or 16
101 */
102 u32 round_freq_6 = round_down(freq_mhz, 6);
103 u32 round_freq_16 = round_down(freq_mhz, 16);
104
105 if (round_freq_6 > round_freq_16)
106 freq_mhz = round_freq_6;
107 else
108 freq_mhz = round_freq_16;
109
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800110 req->rate = freq_mhz * 1000000;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200111
112 /* If the frequency is a multiple of 32 MHz, k is always 3 */
113 if (!(freq_mhz % 32))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800114 req->k = 3;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200115 /* If the frequency is a multiple of 9 MHz, k is always 2 */
116 else if (!(freq_mhz % 9))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800117 req->k = 2;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200118 /* If the frequency is a multiple of 8 MHz, k is always 1 */
119 else if (!(freq_mhz % 8))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800120 req->k = 1;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200121 /* Otherwise, we don't use the k factor */
122 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800123 req->k = 0;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200124
125 /*
126 * If the frequency is a multiple of 2 but not a multiple of
127 * 3, m is 3. This is the first time we use 6 here, yet we
128 * will use it on several other places.
129 * We use this number because it's the lowest frequency we can
130 * generate (with n = 0, k = 0, m = 3), so every other frequency
131 * somehow relates to this frequency.
132 */
133 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800134 req->m = 2;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200135 /*
136 * If the frequency is a multiple of 6MHz, but the factor is
137 * odd, m will be 3
138 */
139 else if ((freq_mhz / 6) & 1)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800140 req->m = 3;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200141 /* Otherwise, we end up with m = 1 */
142 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800143 req->m = 1;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200144
145 /* Calculate n thanks to the above factors we already got */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800146 req->n = freq_mhz * (req->m + 1) / ((req->k + 1) * parent_freq_mhz)
147 - 1;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200148
149 /*
150 * If n end up being outbound, and that we can still decrease
151 * m, do it.
152 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800153 if ((req->n + 1) > 31 && (req->m + 1) > 1) {
154 req->n = (req->n + 1) / 2 - 1;
155 req->m = (req->m + 1) / 2 - 1;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200156 }
157}
Emilio Lópeze874a662013-02-25 11:44:26 -0300158
159/**
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800160 * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
161 * PLL1 rate is calculated as follows
162 * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
163 * parent_rate is always 24Mhz
164 */
165
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800166static void sun8i_a23_get_pll1_factors(struct factors_request *req)
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800167{
168 u8 div;
169
170 /* Normalize value to a 6M multiple */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800171 div = req->rate / 6000000;
172 req->rate = 6000000 * div;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800173
174 /* m is always zero for pll1 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800175 req->m = 0;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800176
177 /* k is 1 only on these cases */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800178 if (req->rate >= 768000000 || req->rate == 42000000 ||
179 req->rate == 54000000)
180 req->k = 1;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800181 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800182 req->k = 0;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800183
184 /* p will be 2 for divs under 20 and odd divs under 32 */
185 if (div < 20 || (div < 32 && (div & 1)))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800186 req->p = 2;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800187
188 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
189 * of divs between 40-62 */
190 else if (div < 40 || (div < 64 && (div & 2)))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800191 req->p = 1;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800192
193 /* any other entries have p = 0 */
194 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800195 req->p = 0;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800196
197 /* calculate a suitable n based on k and p */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800198 div <<= req->p;
199 div /= (req->k + 1);
200 req->n = div / 4 - 1;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800201}
202
203/**
Emilio Lópezd584c132013-12-23 00:32:37 -0300204 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
205 * PLL5 rate is calculated as follows
206 * rate = parent_rate * n * (k + 1)
207 * parent_rate is always 24Mhz
208 */
209
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800210static void sun4i_get_pll5_factors(struct factors_request *req)
Emilio Lópezd584c132013-12-23 00:32:37 -0300211{
212 u8 div;
213
214 /* Normalize value to a parent_rate multiple (24M) */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800215 div = req->rate / req->parent_rate;
216 req->rate = req->parent_rate * div;
Emilio Lópezd584c132013-12-23 00:32:37 -0300217
218 if (div < 31)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800219 req->k = 0;
Emilio Lópezd584c132013-12-23 00:32:37 -0300220 else if (div / 2 < 31)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800221 req->k = 1;
Emilio Lópezd584c132013-12-23 00:32:37 -0300222 else if (div / 3 < 31)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800223 req->k = 2;
Emilio Lópezd584c132013-12-23 00:32:37 -0300224 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800225 req->k = 3;
Emilio Lópezd584c132013-12-23 00:32:37 -0300226
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800227 req->n = DIV_ROUND_UP(div, (req->k + 1));
Emilio Lópezd584c132013-12-23 00:32:37 -0300228}
229
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100230/**
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800231 * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6x2
232 * PLL6x2 rate is calculated as follows
233 * rate = parent_rate * (n + 1) * (k + 1)
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100234 * parent_rate is always 24Mhz
235 */
Emilio Lópezd584c132013-12-23 00:32:37 -0300236
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800237static void sun6i_a31_get_pll6_factors(struct factors_request *req)
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100238{
239 u8 div;
240
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800241 /* Normalize value to a parent_rate multiple (24M) */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800242 div = req->rate / req->parent_rate;
243 req->rate = req->parent_rate * div;
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100244
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800245 req->k = div / 32;
246 if (req->k > 3)
247 req->k = 3;
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100248
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800249 req->n = DIV_ROUND_UP(div, (req->k + 1)) - 1;
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100250}
Emilio Lópezd584c132013-12-23 00:32:37 -0300251
252/**
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800253 * sun5i_a13_get_ahb_factors() - calculates m, p factors for AHB
254 * AHB rate is calculated as follows
255 * rate = parent_rate >> p
256 */
257
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800258static void sun5i_a13_get_ahb_factors(struct factors_request *req)
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800259{
260 u32 div;
261
262 /* divide only */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800263 if (req->parent_rate < req->rate)
264 req->rate = req->parent_rate;
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800265
266 /*
267 * user manual says valid speed is 8k ~ 276M, but tests show it
268 * can work at speeds up to 300M, just after reparenting to pll6
269 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800270 if (req->rate < 8000)
271 req->rate = 8000;
272 if (req->rate > 300000000)
273 req->rate = 300000000;
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800274
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800275 div = order_base_2(DIV_ROUND_UP(req->parent_rate, req->rate));
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800276
277 /* p = 0 ~ 3 */
278 if (div > 3)
279 div = 3;
280
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800281 req->rate = req->parent_rate >> div;
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800282
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800283 req->p = div;
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800284}
285
Chen-Yu Tsaia78bb352016-01-25 21:15:45 +0800286#define SUN6I_AHB1_PARENT_PLL6 3
287
288/**
289 * sun6i_a31_get_ahb_factors() - calculates m, p factors for AHB
290 * AHB rate is calculated as follows
291 * rate = parent_rate >> p
292 *
293 * if parent is pll6, then
294 * parent_rate = pll6 rate / (m + 1)
295 */
296
297static void sun6i_get_ahb1_factors(struct factors_request *req)
298{
299 u8 div, calcp, calcm = 1;
300
301 /*
302 * clock can only divide, so we will never be able to achieve
303 * frequencies higher than the parent frequency
304 */
305 if (req->parent_rate && req->rate > req->parent_rate)
306 req->rate = req->parent_rate;
307
308 div = DIV_ROUND_UP(req->parent_rate, req->rate);
309
310 /* calculate pre-divider if parent is pll6 */
311 if (req->parent_index == SUN6I_AHB1_PARENT_PLL6) {
312 if (div < 4)
313 calcp = 0;
314 else if (div / 2 < 4)
315 calcp = 1;
316 else if (div / 4 < 4)
317 calcp = 2;
318 else
319 calcp = 3;
320
321 calcm = DIV_ROUND_UP(div, 1 << calcp);
322 } else {
323 calcp = __roundup_pow_of_two(div);
324 calcp = calcp > 3 ? 3 : calcp;
325 }
326
327 req->rate = (req->parent_rate / calcm) >> calcp;
328 req->p = calcp;
329 req->m = calcm - 1;
330}
331
332/**
333 * sun6i_ahb1_recalc() - calculates AHB clock rate from m, p factors and
334 * parent index
335 */
336static void sun6i_ahb1_recalc(struct factors_request *req)
337{
338 req->rate = req->parent_rate;
339
340 /* apply pre-divider first if parent is pll6 */
341 if (req->parent_index == SUN6I_AHB1_PARENT_PLL6)
342 req->rate /= req->m + 1;
343
344 /* clk divider */
345 req->rate >>= req->p;
346}
347
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800348/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200349 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
Emilio Lópeze874a662013-02-25 11:44:26 -0300350 * APB1 rate is calculated as follows
351 * rate = (parent_rate >> p) / (m + 1);
352 */
353
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800354static void sun4i_get_apb1_factors(struct factors_request *req)
Emilio Lópeze874a662013-02-25 11:44:26 -0300355{
356 u8 calcm, calcp;
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800357 int div;
Emilio Lópeze874a662013-02-25 11:44:26 -0300358
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800359 if (req->parent_rate < req->rate)
360 req->rate = req->parent_rate;
Emilio Lópeze874a662013-02-25 11:44:26 -0300361
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800362 div = DIV_ROUND_UP(req->parent_rate, req->rate);
Emilio Lópeze874a662013-02-25 11:44:26 -0300363
364 /* Invalid rate! */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800365 if (div > 32)
Emilio Lópeze874a662013-02-25 11:44:26 -0300366 return;
367
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800368 if (div <= 4)
Emilio Lópeze874a662013-02-25 11:44:26 -0300369 calcp = 0;
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800370 else if (div <= 8)
Emilio Lópeze874a662013-02-25 11:44:26 -0300371 calcp = 1;
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800372 else if (div <= 16)
Emilio Lópeze874a662013-02-25 11:44:26 -0300373 calcp = 2;
374 else
375 calcp = 3;
376
Stéphan Rafinac953302016-11-04 00:53:56 +0100377 calcm = (div >> calcp) - 1;
Emilio Lópeze874a662013-02-25 11:44:26 -0300378
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800379 req->rate = (req->parent_rate >> calcp) / (calcm + 1);
380 req->m = calcm;
381 req->p = calcp;
Emilio Lópeze874a662013-02-25 11:44:26 -0300382}
383
384
385
Emilio López75517692013-12-23 00:32:39 -0300386
387/**
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800388 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
389 * CLK_OUT rate is calculated as follows
390 * rate = (parent_rate >> p) / (m + 1);
391 */
392
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800393static void sun7i_a20_get_out_factors(struct factors_request *req)
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800394{
395 u8 div, calcm, calcp;
396
397 /* These clocks can only divide, so we will never be able to achieve
398 * frequencies higher than the parent frequency */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800399 if (req->rate > req->parent_rate)
400 req->rate = req->parent_rate;
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800401
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800402 div = DIV_ROUND_UP(req->parent_rate, req->rate);
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800403
404 if (div < 32)
405 calcp = 0;
406 else if (div / 2 < 32)
407 calcp = 1;
408 else if (div / 4 < 32)
409 calcp = 2;
410 else
411 calcp = 3;
412
413 calcm = DIV_ROUND_UP(div, 1 << calcp);
414
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800415 req->rate = (req->parent_rate >> calcp) / calcm;
416 req->m = calcm - 1;
417 req->p = calcp;
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800418}
419
Chen-Yu Tsaie4c6d6c2014-02-10 18:35:47 +0800420/**
Emilio Lópeze874a662013-02-25 11:44:26 -0300421 * sunxi_factors_clk_setup() - Setup function for factor clocks
422 */
423
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800424static const struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300425 .nshift = 8,
426 .nwidth = 5,
427 .kshift = 4,
428 .kwidth = 2,
429 .mshift = 0,
430 .mwidth = 2,
431 .pshift = 16,
432 .pwidth = 2,
433};
434
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800435static const struct clk_factors_config sun6i_a31_pll1_config = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200436 .nshift = 8,
437 .nwidth = 5,
438 .kshift = 4,
439 .kwidth = 2,
440 .mshift = 0,
441 .mwidth = 2,
Hans de Goede76820fc2015-01-24 12:56:32 +0100442 .n_start = 1,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200443};
444
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800445static const struct clk_factors_config sun8i_a23_pll1_config = {
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800446 .nshift = 8,
447 .nwidth = 5,
448 .kshift = 4,
449 .kwidth = 2,
450 .mshift = 0,
451 .mwidth = 2,
452 .pshift = 16,
453 .pwidth = 2,
454 .n_start = 1,
455};
456
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800457static const struct clk_factors_config sun4i_pll5_config = {
Emilio Lópezd584c132013-12-23 00:32:37 -0300458 .nshift = 8,
459 .nwidth = 5,
460 .kshift = 4,
461 .kwidth = 2,
462};
463
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800464static const struct clk_factors_config sun6i_a31_pll6_config = {
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100465 .nshift = 8,
466 .nwidth = 5,
467 .kshift = 4,
468 .kwidth = 2,
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800469 .n_start = 1,
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100470};
471
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800472static const struct clk_factors_config sun5i_a13_ahb_config = {
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800473 .pshift = 4,
474 .pwidth = 2,
475};
476
Chen-Yu Tsaia78bb352016-01-25 21:15:45 +0800477static const struct clk_factors_config sun6i_ahb1_config = {
478 .mshift = 6,
479 .mwidth = 2,
480 .pshift = 4,
481 .pwidth = 2,
482};
483
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800484static const struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300485 .mshift = 0,
486 .mwidth = 5,
487 .pshift = 16,
488 .pwidth = 2,
489};
490
Emilio López75517692013-12-23 00:32:39 -0300491/* user manual says "n" but it's really "p" */
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800492static const struct clk_factors_config sun7i_a20_out_config = {
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800493 .mshift = 8,
494 .mwidth = 5,
495 .pshift = 20,
496 .pwidth = 2,
497};
498
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530499static const struct factors_data sun4i_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300500 .enable = 31,
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200501 .table = &sun4i_pll1_config,
502 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300503};
504
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530505static const struct factors_data sun6i_a31_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300506 .enable = 31,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200507 .table = &sun6i_a31_pll1_config,
508 .getter = sun6i_a31_get_pll1_factors,
509};
510
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800511static const struct factors_data sun8i_a23_pll1_data __initconst = {
512 .enable = 31,
513 .table = &sun8i_a23_pll1_config,
514 .getter = sun8i_a23_get_pll1_factors,
515};
516
Emilio López5a8ddf22014-03-19 15:19:30 -0300517static const struct factors_data sun7i_a20_pll4_data __initconst = {
518 .enable = 31,
519 .table = &sun4i_pll5_config,
520 .getter = sun4i_get_pll5_factors,
521};
522
Emilio Lópezd584c132013-12-23 00:32:37 -0300523static const struct factors_data sun4i_pll5_data __initconst = {
524 .enable = 31,
525 .table = &sun4i_pll5_config,
526 .getter = sun4i_get_pll5_factors,
527};
528
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100529static const struct factors_data sun6i_a31_pll6_data __initconst = {
530 .enable = 31,
531 .table = &sun6i_a31_pll6_config,
532 .getter = sun6i_a31_get_pll6_factors,
533};
534
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800535static const struct factors_data sun5i_a13_ahb_data __initconst = {
536 .mux = 6,
537 .muxmask = BIT(1) | BIT(0),
538 .table = &sun5i_a13_ahb_config,
539 .getter = sun5i_a13_get_ahb_factors,
540};
541
Chen-Yu Tsaia78bb352016-01-25 21:15:45 +0800542static const struct factors_data sun6i_ahb1_data __initconst = {
543 .mux = 12,
544 .muxmask = BIT(1) | BIT(0),
545 .table = &sun6i_ahb1_config,
546 .getter = sun6i_get_ahb1_factors,
547 .recalc = sun6i_ahb1_recalc,
548};
549
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530550static const struct factors_data sun4i_apb1_data __initconst = {
Emilio López93746e72014-11-06 11:40:29 +0800551 .mux = 24,
552 .muxmask = BIT(1) | BIT(0),
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200553 .table = &sun4i_apb1_config,
554 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300555};
556
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800557static const struct factors_data sun7i_a20_out_data __initconst = {
558 .enable = 31,
559 .mux = 24,
Chen-Yu Tsaie94f8cb32014-10-20 22:10:26 +0800560 .muxmask = BIT(1) | BIT(0),
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800561 .table = &sun7i_a20_out_config,
562 .getter = sun7i_a20_get_out_factors,
563};
564
Emilio López5f4e0be2013-12-23 00:32:36 -0300565static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
Maxime Ripard601da9d2014-07-04 22:24:52 +0200566 const struct factors_data *data)
Emilio Lópeze874a662013-02-25 11:44:26 -0300567{
Hans de Goede7c74c222014-11-23 14:38:07 +0100568 void __iomem *reg;
569
570 reg = of_iomap(node, 0);
571 if (!reg) {
Rob Herringe665f022018-08-28 10:44:29 -0500572 pr_err("Could not get registers for factors-clk: %pOFn\n",
573 node);
Hans de Goede7c74c222014-11-23 14:38:07 +0100574 return NULL;
575 }
576
577 return sunxi_factors_register(node, data, &clk_lock, reg);
Emilio Lópeze874a662013-02-25 11:44:26 -0300578}
579
Maxime Ripardc0872302016-02-02 09:07:22 +0100580static void __init sun4i_pll1_clk_setup(struct device_node *node)
581{
582 sunxi_factors_clk_setup(node, &sun4i_pll1_data);
583}
584CLK_OF_DECLARE(sun4i_pll1, "allwinner,sun4i-a10-pll1-clk",
585 sun4i_pll1_clk_setup);
586
587static void __init sun6i_pll1_clk_setup(struct device_node *node)
588{
589 sunxi_factors_clk_setup(node, &sun6i_a31_pll1_data);
590}
591CLK_OF_DECLARE(sun6i_pll1, "allwinner,sun6i-a31-pll1-clk",
592 sun6i_pll1_clk_setup);
593
594static void __init sun8i_pll1_clk_setup(struct device_node *node)
595{
596 sunxi_factors_clk_setup(node, &sun8i_a23_pll1_data);
597}
598CLK_OF_DECLARE(sun8i_pll1, "allwinner,sun8i-a23-pll1-clk",
599 sun8i_pll1_clk_setup);
600
601static void __init sun7i_pll4_clk_setup(struct device_node *node)
602{
603 sunxi_factors_clk_setup(node, &sun7i_a20_pll4_data);
604}
605CLK_OF_DECLARE(sun7i_pll4, "allwinner,sun7i-a20-pll4-clk",
606 sun7i_pll4_clk_setup);
607
608static void __init sun5i_ahb_clk_setup(struct device_node *node)
609{
610 sunxi_factors_clk_setup(node, &sun5i_a13_ahb_data);
611}
612CLK_OF_DECLARE(sun5i_ahb, "allwinner,sun5i-a13-ahb-clk",
613 sun5i_ahb_clk_setup);
614
Chen-Yu Tsaia78bb352016-01-25 21:15:45 +0800615static void __init sun6i_ahb1_clk_setup(struct device_node *node)
616{
617 sunxi_factors_clk_setup(node, &sun6i_ahb1_data);
618}
619CLK_OF_DECLARE(sun6i_a31_ahb1, "allwinner,sun6i-a31-ahb1-clk",
620 sun6i_ahb1_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -0300621
Maxime Ripardc0872302016-02-02 09:07:22 +0100622static void __init sun4i_apb1_clk_setup(struct device_node *node)
623{
624 sunxi_factors_clk_setup(node, &sun4i_apb1_data);
625}
626CLK_OF_DECLARE(sun4i_apb1, "allwinner,sun4i-a10-apb1-clk",
627 sun4i_apb1_clk_setup);
628
629static void __init sun7i_out_clk_setup(struct device_node *node)
630{
631 sunxi_factors_clk_setup(node, &sun7i_a20_out_data);
632}
633CLK_OF_DECLARE(sun7i_out, "allwinner,sun7i-a20-out-clk",
634 sun7i_out_clk_setup);
635
Emilio Lópeze874a662013-02-25 11:44:26 -0300636
637/**
638 * sunxi_mux_clk_setup() - Setup function for muxes
639 */
640
641#define SUNXI_MUX_GATE_WIDTH 2
642
643struct mux_data {
644 u8 shift;
645};
646
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530647static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300648 .shift = 16,
649};
650
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530651static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200652 .shift = 12,
653};
654
Jens Kuskeab6e23a2015-12-04 22:24:40 +0100655static const struct mux_data sun8i_h3_ahb2_mux_data __initconst = {
656 .shift = 0,
657};
658
Maxime Ripard96f185a2016-02-02 09:47:10 +0100659static struct clk * __init sunxi_mux_clk_setup(struct device_node *node,
Stephen Boyd9919d442018-01-02 16:50:27 -0800660 const struct mux_data *data,
661 unsigned long flags)
Emilio Lópeze874a662013-02-25 11:44:26 -0300662{
663 struct clk *clk;
664 const char *clk_name = node->name;
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300665 const char *parents[SUNXI_MAX_PARENTS];
Emilio López89a94562014-07-28 00:49:42 -0300666 void __iomem *reg;
Dinh Nguyen8a53fb22015-07-06 22:59:05 -0500667 int i;
Emilio Lópeze874a662013-02-25 11:44:26 -0300668
669 reg = of_iomap(node, 0);
Andre Przywara72360b92016-02-16 10:46:06 +0000670 if (!reg) {
Rob Herring16673932017-07-18 16:42:52 -0500671 pr_err("Could not map registers for mux-clk: %pOF\n", node);
Andre Przywara72360b92016-02-16 10:46:06 +0000672 return NULL;
673 }
Emilio Lópeze874a662013-02-25 11:44:26 -0300674
Dinh Nguyen8a53fb22015-07-06 22:59:05 -0500675 i = of_clk_parent_fill(node, parents, SUNXI_MAX_PARENTS);
Andre Przywarad221b7a2016-02-01 17:39:27 +0000676 if (of_property_read_string(node, "clock-output-names", &clk_name)) {
Rob Herring16673932017-07-18 16:42:52 -0500677 pr_err("%s: could not read clock-output-names from \"%pOF\"\n",
678 __func__, node);
Andre Przywarad221b7a2016-02-01 17:39:27 +0000679 goto out_unmap;
680 }
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800681
James Hogan819c1de2013-07-29 12:25:01 +0100682 clk = clk_register_mux(NULL, clk_name, parents, i,
Stephen Boyd9919d442018-01-02 16:50:27 -0800683 CLK_SET_RATE_PARENT | flags, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300684 data->shift, SUNXI_MUX_GATE_WIDTH,
685 0, &clk_lock);
686
Andre Przywarad221b7a2016-02-01 17:39:27 +0000687 if (IS_ERR(clk)) {
Andre Przywara72360b92016-02-16 10:46:06 +0000688 pr_err("%s: failed to register mux clock %s: %ld\n", __func__,
689 clk_name, PTR_ERR(clk));
Andre Przywarad221b7a2016-02-01 17:39:27 +0000690 goto out_unmap;
Emilio Lópeze874a662013-02-25 11:44:26 -0300691 }
Andre Przywarad221b7a2016-02-01 17:39:27 +0000692
Andre Przywara72360b92016-02-16 10:46:06 +0000693 if (of_clk_add_provider(node, of_clk_src_simple_get, clk)) {
694 pr_err("%s: failed to add clock provider for %s\n",
695 __func__, clk_name);
696 clk_unregister_divider(clk);
697 goto out_unmap;
698 }
Maxime Ripard96f185a2016-02-02 09:47:10 +0100699
700 return clk;
Andre Przywarad221b7a2016-02-01 17:39:27 +0000701out_unmap:
702 iounmap(reg);
Maxime Ripard96f185a2016-02-02 09:47:10 +0100703 return NULL;
Emilio Lópeze874a662013-02-25 11:44:26 -0300704}
705
Maxime Ripardc0872302016-02-02 09:07:22 +0100706static void __init sun4i_cpu_clk_setup(struct device_node *node)
707{
Maxime Ripardc0872302016-02-02 09:07:22 +0100708 /* Protect CPU clock */
Stephen Boyd9919d442018-01-02 16:50:27 -0800709 sunxi_mux_clk_setup(node, &sun4i_cpu_mux_data, CLK_IS_CRITICAL);
Maxime Ripardc0872302016-02-02 09:07:22 +0100710}
711CLK_OF_DECLARE(sun4i_cpu, "allwinner,sun4i-a10-cpu-clk",
712 sun4i_cpu_clk_setup);
713
714static void __init sun6i_ahb1_mux_clk_setup(struct device_node *node)
715{
Stephen Boyd9919d442018-01-02 16:50:27 -0800716 sunxi_mux_clk_setup(node, &sun6i_a31_ahb1_mux_data, 0);
Maxime Ripardc0872302016-02-02 09:07:22 +0100717}
718CLK_OF_DECLARE(sun6i_ahb1_mux, "allwinner,sun6i-a31-ahb1-mux-clk",
719 sun6i_ahb1_mux_clk_setup);
720
721static void __init sun8i_ahb2_clk_setup(struct device_node *node)
722{
Stephen Boyd9919d442018-01-02 16:50:27 -0800723 sunxi_mux_clk_setup(node, &sun8i_h3_ahb2_mux_data, 0);
Maxime Ripardc0872302016-02-02 09:07:22 +0100724}
725CLK_OF_DECLARE(sun8i_ahb2, "allwinner,sun8i-h3-ahb2-clk",
726 sun8i_ahb2_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -0300727
728
729/**
730 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
731 */
732
Emilio Lópeze874a662013-02-25 11:44:26 -0300733struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200734 u8 shift;
735 u8 pow;
736 u8 width;
Chen-Yu Tsaiea5671b2014-06-26 23:55:42 +0800737 const struct clk_div_table *table;
Emilio Lópeze874a662013-02-25 11:44:26 -0300738};
739
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530740static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200741 .shift = 0,
742 .pow = 0,
743 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300744};
745
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800746static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
747 { .val = 0, .div = 1 },
748 { .val = 1, .div = 2 },
749 { .val = 2, .div = 3 },
750 { .val = 3, .div = 4 },
751 { .val = 4, .div = 4 },
752 { .val = 5, .div = 4 },
753 { .val = 6, .div = 4 },
754 { .val = 7, .div = 4 },
755 { } /* sentinel */
756};
757
758static const struct div_data sun8i_a23_axi_data __initconst = {
759 .width = 3,
760 .table = sun8i_a23_axi_table,
761};
762
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530763static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200764 .shift = 4,
765 .pow = 1,
766 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300767};
768
Chen-Yu Tsaicfe4c932014-09-06 14:45:10 +0800769static const struct clk_div_table sun4i_apb0_table[] __initconst = {
770 { .val = 0, .div = 2 },
771 { .val = 1, .div = 2 },
772 { .val = 2, .div = 4 },
773 { .val = 3, .div = 8 },
774 { } /* sentinel */
775};
776
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530777static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200778 .shift = 8,
779 .pow = 1,
780 .width = 2,
Chen-Yu Tsaicfe4c932014-09-06 14:45:10 +0800781 .table = sun4i_apb0_table,
Emilio Lópeze874a662013-02-25 11:44:26 -0300782};
783
784static void __init sunxi_divider_clk_setup(struct device_node *node,
Maxime Ripard5b5226d2016-02-02 09:47:11 +0100785 const struct div_data *data)
Emilio Lópeze874a662013-02-25 11:44:26 -0300786{
787 struct clk *clk;
788 const char *clk_name = node->name;
789 const char *clk_parent;
Emilio López89a94562014-07-28 00:49:42 -0300790 void __iomem *reg;
Emilio Lópeze874a662013-02-25 11:44:26 -0300791
792 reg = of_iomap(node, 0);
Andre Przywarab26803e2016-02-16 10:46:07 +0000793 if (!reg) {
Rob Herring16673932017-07-18 16:42:52 -0500794 pr_err("Could not map registers for mux-clk: %pOF\n", node);
Andre Przywarab26803e2016-02-16 10:46:07 +0000795 return;
796 }
Emilio Lópeze874a662013-02-25 11:44:26 -0300797
798 clk_parent = of_clk_get_parent_name(node, 0);
799
Andre Przywarab26803e2016-02-16 10:46:07 +0000800 if (of_property_read_string(node, "clock-output-names", &clk_name)) {
Rob Herring16673932017-07-18 16:42:52 -0500801 pr_err("%s: could not read clock-output-names from \"%pOF\"\n",
802 __func__, node);
Andre Przywarab26803e2016-02-16 10:46:07 +0000803 goto out_unmap;
804 }
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800805
Chen-Yu Tsaiea5671b2014-06-26 23:55:42 +0800806 clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
807 reg, data->shift, data->width,
808 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
809 data->table, &clk_lock);
Andre Przywarab26803e2016-02-16 10:46:07 +0000810 if (IS_ERR(clk)) {
811 pr_err("%s: failed to register divider clock %s: %ld\n",
812 __func__, clk_name, PTR_ERR(clk));
813 goto out_unmap;
814 }
815
816 if (of_clk_add_provider(node, of_clk_src_simple_get, clk)) {
817 pr_err("%s: failed to add clock provider for %s\n",
818 __func__, clk_name);
819 goto out_unregister;
820 }
821
822 if (clk_register_clkdev(clk, clk_name, NULL)) {
823 of_clk_del_provider(node);
824 goto out_unregister;
825 }
826
827 return;
828out_unregister:
829 clk_unregister_divider(clk);
830
831out_unmap:
832 iounmap(reg);
Emilio Lópeze874a662013-02-25 11:44:26 -0300833}
834
Maxime Ripardc0872302016-02-02 09:07:22 +0100835static void __init sun4i_ahb_clk_setup(struct device_node *node)
836{
837 sunxi_divider_clk_setup(node, &sun4i_ahb_data);
838}
839CLK_OF_DECLARE(sun4i_ahb, "allwinner,sun4i-a10-ahb-clk",
840 sun4i_ahb_clk_setup);
841
842static void __init sun4i_apb0_clk_setup(struct device_node *node)
843{
844 sunxi_divider_clk_setup(node, &sun4i_apb0_data);
845}
846CLK_OF_DECLARE(sun4i_apb0, "allwinner,sun4i-a10-apb0-clk",
847 sun4i_apb0_clk_setup);
848
849static void __init sun4i_axi_clk_setup(struct device_node *node)
850{
851 sunxi_divider_clk_setup(node, &sun4i_axi_data);
852}
853CLK_OF_DECLARE(sun4i_axi, "allwinner,sun4i-a10-axi-clk",
854 sun4i_axi_clk_setup);
855
856static void __init sun8i_axi_clk_setup(struct device_node *node)
857{
858 sunxi_divider_clk_setup(node, &sun8i_a23_axi_data);
859}
860CLK_OF_DECLARE(sun8i_axi, "allwinner,sun8i-a23-axi-clk",
861 sun8i_axi_clk_setup);
862
Emilio Lópeze874a662013-02-25 11:44:26 -0300863
Emilio López13569a72013-03-27 18:20:37 -0300864
865/**
866 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
867 */
868
869#define SUNXI_GATES_MAX_SIZE 64
870
871struct gates_data {
872 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
873};
874
Emilio Lópezd584c132013-12-23 00:32:37 -0300875/**
876 * sunxi_divs_clk_setup() helper data
877 */
878
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800879#define SUNXI_DIVS_MAX_QTY 4
Emilio Lópezd584c132013-12-23 00:32:37 -0300880#define SUNXI_DIVISOR_WIDTH 2
881
882struct divs_data {
883 const struct factors_data *factors; /* data for the factor clock */
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800884 int ndivs; /* number of outputs */
885 /*
886 * List of outputs. Refer to the diagram for sunxi_divs_clk_setup():
887 * self or base factor clock refers to the output from the pll
888 * itself. The remaining refer to fixed or configurable divider
889 * outputs.
890 */
Emilio Lópezd584c132013-12-23 00:32:37 -0300891 struct {
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800892 u8 self; /* is it the base factor clock? (only one) */
Emilio Lópezd584c132013-12-23 00:32:37 -0300893 u8 fixed; /* is it a fixed divisor? if not... */
894 struct clk_div_table *table; /* is it a table based divisor? */
895 u8 shift; /* otherwise it's a normal divisor with this shift */
896 u8 pow; /* is it power-of-two based? */
897 u8 gate; /* is it independently gateable? */
Stephen Boyd9919d442018-01-02 16:50:27 -0800898 bool critical;
Emilio Lópezd584c132013-12-23 00:32:37 -0300899 } div[SUNXI_DIVS_MAX_QTY];
900};
901
902static struct clk_div_table pll6_sata_tbl[] = {
903 { .val = 0, .div = 6, },
904 { .val = 1, .div = 12, },
905 { .val = 2, .div = 18, },
906 { .val = 3, .div = 24, },
907 { } /* sentinel */
908};
909
910static const struct divs_data pll5_divs_data __initconst = {
911 .factors = &sun4i_pll5_data,
Chen-Yu Tsai13d52f62014-11-13 02:08:30 +0800912 .ndivs = 2,
Emilio Lópezd584c132013-12-23 00:32:37 -0300913 .div = {
Stephen Boyd9919d442018-01-02 16:50:27 -0800914 /* Protect PLL5_DDR */
915 { .shift = 0, .pow = 0, .critical = true }, /* M, DDR */
Emilio Lópezd584c132013-12-23 00:32:37 -0300916 { .shift = 16, .pow = 1, }, /* P, other */
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800917 /* No output for the base factor clock */
Emilio Lópezd584c132013-12-23 00:32:37 -0300918 }
919};
920
921static const struct divs_data pll6_divs_data __initconst = {
Jens Kuskeff2bb892016-03-18 09:44:15 +0000922 .factors = &sun4i_pll5_data,
Chen-Yu Tsaif1017962015-03-25 01:22:08 +0800923 .ndivs = 4,
Emilio Lópezd584c132013-12-23 00:32:37 -0300924 .div = {
925 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
926 { .fixed = 2 }, /* P, other */
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800927 { .self = 1 }, /* base factor clock, 2x */
Chen-Yu Tsaif1017962015-03-25 01:22:08 +0800928 { .fixed = 4 }, /* pll6 / 4, used as ahb input */
Emilio Lópezd584c132013-12-23 00:32:37 -0300929 }
930};
931
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800932static const struct divs_data sun6i_a31_pll6_divs_data __initconst = {
933 .factors = &sun6i_a31_pll6_data,
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800934 .ndivs = 2,
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800935 .div = {
936 { .fixed = 2 }, /* normal output */
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800937 { .self = 1 }, /* base factor clock, 2x */
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800938 }
939};
940
Emilio Lópezd584c132013-12-23 00:32:37 -0300941/**
942 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
943 *
944 * These clocks look something like this
945 * ________________________
946 * | ___divisor 1---|----> to consumer
947 * parent >--| pll___/___divisor 2---|----> to consumer
948 * | \_______________|____> to consumer
949 * |________________________|
950 */
951
Maxime Ripard96f185a2016-02-02 09:47:10 +0100952static struct clk ** __init sunxi_divs_clk_setup(struct device_node *node,
Maxime Ripard5b5226d2016-02-02 09:47:11 +0100953 const struct divs_data *data)
Emilio Lópezd584c132013-12-23 00:32:37 -0300954{
955 struct clk_onecell_data *clk_data;
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +0800956 const char *parent;
Emilio Lópezd584c132013-12-23 00:32:37 -0300957 const char *clk_name;
958 struct clk **clks, *pclk;
959 struct clk_hw *gate_hw, *rate_hw;
960 const struct clk_ops *rate_ops;
961 struct clk_gate *gate = NULL;
962 struct clk_fixed_factor *fix_factor;
963 struct clk_divider *divider;
Jens Kuskeff2bb892016-03-18 09:44:15 +0000964 struct factors_data factors = *data->factors;
965 char *derived_name = NULL;
Emilio López89a94562014-07-28 00:49:42 -0300966 void __iomem *reg;
Chen-Yu Tsai13d52f62014-11-13 02:08:30 +0800967 int ndivs = SUNXI_DIVS_MAX_QTY, i = 0;
Emilio Lópezd584c132013-12-23 00:32:37 -0300968 int flags, clkflags;
969
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800970 /* if number of children known, use it */
971 if (data->ndivs)
972 ndivs = data->ndivs;
973
Jens Kuskeff2bb892016-03-18 09:44:15 +0000974 /* Try to find a name for base factor clock */
975 for (i = 0; i < ndivs; i++) {
976 if (data->div[i].self) {
977 of_property_read_string_index(node, "clock-output-names",
978 i, &factors.name);
979 break;
980 }
981 }
982 /* If we don't have a .self clk use the first output-name up to '_' */
983 if (factors.name == NULL) {
984 char *endp;
985
986 of_property_read_string_index(node, "clock-output-names",
987 0, &clk_name);
988 endp = strchr(clk_name, '_');
989 if (endp) {
990 derived_name = kstrndup(clk_name, endp - clk_name,
991 GFP_KERNEL);
992 factors.name = derived_name;
993 } else {
994 factors.name = clk_name;
995 }
996 }
997
Emilio Lópezd584c132013-12-23 00:32:37 -0300998 /* Set up factor clock that we will be dividing */
Jens Kuskeff2bb892016-03-18 09:44:15 +0000999 pclk = sunxi_factors_clk_setup(node, &factors);
Andre Przywarad3313282016-02-16 10:46:08 +00001000 if (!pclk)
1001 return NULL;
Jens Kuskeff2bb892016-03-18 09:44:15 +00001002
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +08001003 parent = __clk_get_name(pclk);
Jens Kuskeff2bb892016-03-18 09:44:15 +00001004 kfree(derived_name);
Emilio Lópezd584c132013-12-23 00:32:37 -03001005
1006 reg = of_iomap(node, 0);
Andre Przywarad3313282016-02-16 10:46:08 +00001007 if (!reg) {
Rob Herring16673932017-07-18 16:42:52 -05001008 pr_err("Could not map registers for divs-clk: %pOF\n", node);
Andre Przywarad3313282016-02-16 10:46:08 +00001009 return NULL;
1010 }
Emilio Lópezd584c132013-12-23 00:32:37 -03001011
1012 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1013 if (!clk_data)
Andre Przywarad3313282016-02-16 10:46:08 +00001014 goto out_unmap;
Emilio Lópezd584c132013-12-23 00:32:37 -03001015
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +08001016 clks = kcalloc(ndivs, sizeof(*clks), GFP_KERNEL);
Emilio Lópezd584c132013-12-23 00:32:37 -03001017 if (!clks)
1018 goto free_clkdata;
1019
1020 clk_data->clks = clks;
1021
1022 /* It's not a good idea to have automatic reparenting changing
1023 * our RAM clock! */
1024 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
1025
Chen-Yu Tsai13d52f62014-11-13 02:08:30 +08001026 for (i = 0; i < ndivs; i++) {
Emilio Lópezd584c132013-12-23 00:32:37 -03001027 if (of_property_read_string_index(node, "clock-output-names",
1028 i, &clk_name) != 0)
1029 break;
1030
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +08001031 /* If this is the base factor clock, only update clks */
1032 if (data->div[i].self) {
1033 clk_data->clks[i] = pclk;
1034 continue;
1035 }
1036
Emilio Lópezd584c132013-12-23 00:32:37 -03001037 gate_hw = NULL;
1038 rate_hw = NULL;
1039 rate_ops = NULL;
1040
1041 /* If this leaf clock can be gated, create a gate */
1042 if (data->div[i].gate) {
1043 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1044 if (!gate)
1045 goto free_clks;
1046
1047 gate->reg = reg;
1048 gate->bit_idx = data->div[i].gate;
1049 gate->lock = &clk_lock;
1050
1051 gate_hw = &gate->hw;
1052 }
1053
1054 /* Leaves can be fixed or configurable divisors */
1055 if (data->div[i].fixed) {
1056 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1057 if (!fix_factor)
1058 goto free_gate;
1059
1060 fix_factor->mult = 1;
1061 fix_factor->div = data->div[i].fixed;
1062
1063 rate_hw = &fix_factor->hw;
1064 rate_ops = &clk_fixed_factor_ops;
1065 } else {
1066 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1067 if (!divider)
1068 goto free_gate;
1069
1070 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1071
1072 divider->reg = reg;
1073 divider->shift = data->div[i].shift;
1074 divider->width = SUNXI_DIVISOR_WIDTH;
1075 divider->flags = flags;
1076 divider->lock = &clk_lock;
1077 divider->table = data->div[i].table;
1078
1079 rate_hw = &divider->hw;
1080 rate_ops = &clk_divider_ops;
1081 }
1082
1083 /* Wrap the (potential) gate and the divisor on a composite
1084 * clock to unify them */
1085 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1086 NULL, NULL,
1087 rate_hw, rate_ops,
1088 gate_hw, &clk_gate_ops,
Stephen Boyd9919d442018-01-02 16:50:27 -08001089 clkflags |
1090 data->div[i].critical ?
1091 CLK_IS_CRITICAL : 0);
Emilio Lópezd584c132013-12-23 00:32:37 -03001092
1093 WARN_ON(IS_ERR(clk_data->clks[i]));
Emilio Lópezd584c132013-12-23 00:32:37 -03001094 }
1095
Emilio Lópezd584c132013-12-23 00:32:37 -03001096 /* Adjust to the real max */
1097 clk_data->clk_num = i;
1098
Andre Przywarad3313282016-02-16 10:46:08 +00001099 if (of_clk_add_provider(node, of_clk_src_onecell_get, clk_data)) {
1100 pr_err("%s: failed to add clock provider for %s\n",
1101 __func__, clk_name);
1102 goto free_gate;
1103 }
Emilio Lópezd584c132013-12-23 00:32:37 -03001104
Maxime Ripard96f185a2016-02-02 09:47:10 +01001105 return clks;
Emilio Lópezd584c132013-12-23 00:32:37 -03001106free_gate:
1107 kfree(gate);
1108free_clks:
1109 kfree(clks);
1110free_clkdata:
1111 kfree(clk_data);
Andre Przywarad3313282016-02-16 10:46:08 +00001112out_unmap:
1113 iounmap(reg);
Maxime Ripard96f185a2016-02-02 09:47:10 +01001114 return NULL;
Emilio Lópezd584c132013-12-23 00:32:37 -03001115}
1116
Maxime Ripardc0872302016-02-02 09:07:22 +01001117static void __init sun4i_pll5_clk_setup(struct device_node *node)
1118{
Stephen Boyd9919d442018-01-02 16:50:27 -08001119 sunxi_divs_clk_setup(node, &pll5_divs_data);
Maxime Ripardc0872302016-02-02 09:07:22 +01001120}
1121CLK_OF_DECLARE(sun4i_pll5, "allwinner,sun4i-a10-pll5-clk",
1122 sun4i_pll5_clk_setup);
1123
1124static void __init sun4i_pll6_clk_setup(struct device_node *node)
1125{
1126 sunxi_divs_clk_setup(node, &pll6_divs_data);
1127}
1128CLK_OF_DECLARE(sun4i_pll6, "allwinner,sun4i-a10-pll6-clk",
1129 sun4i_pll6_clk_setup);
1130
1131static void __init sun6i_pll6_clk_setup(struct device_node *node)
1132{
1133 sunxi_divs_clk_setup(node, &sun6i_a31_pll6_divs_data);
1134}
1135CLK_OF_DECLARE(sun6i_pll6, "allwinner,sun6i-a31-pll6-clk",
1136 sun6i_pll6_clk_setup);
Jean-Francois Moine5ed400d2016-03-30 18:43:29 +02001137
1138/*
1139 * sun6i display
1140 *
1141 * rate = parent_rate / (m + 1);
1142 */
1143static void sun6i_display_factors(struct factors_request *req)
1144{
1145 u8 m;
1146
1147 if (req->rate > req->parent_rate)
1148 req->rate = req->parent_rate;
1149
1150 m = DIV_ROUND_UP(req->parent_rate, req->rate);
1151
1152 req->rate = req->parent_rate / m;
1153 req->m = m - 1;
1154}
1155
1156static const struct clk_factors_config sun6i_display_config = {
1157 .mshift = 0,
1158 .mwidth = 4,
1159};
1160
1161static const struct factors_data sun6i_display_data __initconst = {
1162 .enable = 31,
1163 .mux = 24,
1164 .muxmask = BIT(2) | BIT(1) | BIT(0),
1165 .table = &sun6i_display_config,
1166 .getter = sun6i_display_factors,
1167};
1168
1169static void __init sun6i_display_setup(struct device_node *node)
1170{
1171 sunxi_factors_clk_setup(node, &sun6i_display_data);
1172}
1173CLK_OF_DECLARE(sun6i_display, "allwinner,sun6i-a31-display-clk",
1174 sun6i_display_setup);