Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / clk / at91 / clk-main.c
blobcfae2f59df665c04a58365a5969d5a4b4fec9f1c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4 */
6 #include <linux/clk-provider.h>
7 #include <linux/clkdev.h>
8 #include <linux/clk/at91_pmc.h>
9 #include <linux/delay.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/regmap.h>
13 #include "pmc.h"
15 #define SLOW_CLOCK_FREQ 32768
16 #define MAINF_DIV 16
17 #define MAINFRDY_TIMEOUT (((MAINF_DIV + 1) * USEC_PER_SEC) / \
18 SLOW_CLOCK_FREQ)
19 #define MAINF_LOOP_MIN_WAIT (USEC_PER_SEC / SLOW_CLOCK_FREQ)
20 #define MAINF_LOOP_MAX_WAIT MAINFRDY_TIMEOUT
22 #define MOR_KEY_MASK (0xff << 16)
24 #define clk_main_parent_select(s) (((s) & \
25 (AT91_PMC_MOSCEN | \
26 AT91_PMC_OSCBYPASS)) ? 1 : 0)
28 struct clk_main_osc {
29 struct clk_hw hw;
30 struct regmap *regmap;
33 #define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw)
35 struct clk_main_rc_osc {
36 struct clk_hw hw;
37 struct regmap *regmap;
38 unsigned long frequency;
39 unsigned long accuracy;
42 #define to_clk_main_rc_osc(hw) container_of(hw, struct clk_main_rc_osc, hw)
44 struct clk_rm9200_main {
45 struct clk_hw hw;
46 struct regmap *regmap;
49 #define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw)
51 struct clk_sam9x5_main {
52 struct clk_hw hw;
53 struct regmap *regmap;
54 u8 parent;
57 #define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw)
59 static inline bool clk_main_osc_ready(struct regmap *regmap)
61 unsigned int status;
63 regmap_read(regmap, AT91_PMC_SR, &status);
65 return status & AT91_PMC_MOSCS;
68 static int clk_main_osc_prepare(struct clk_hw *hw)
70 struct clk_main_osc *osc = to_clk_main_osc(hw);
71 struct regmap *regmap = osc->regmap;
72 u32 tmp;
74 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
75 tmp &= ~MOR_KEY_MASK;
77 if (tmp & AT91_PMC_OSCBYPASS)
78 return 0;
80 if (!(tmp & AT91_PMC_MOSCEN)) {
81 tmp |= AT91_PMC_MOSCEN | AT91_PMC_KEY;
82 regmap_write(regmap, AT91_CKGR_MOR, tmp);
85 while (!clk_main_osc_ready(regmap))
86 cpu_relax();
88 return 0;
91 static void clk_main_osc_unprepare(struct clk_hw *hw)
93 struct clk_main_osc *osc = to_clk_main_osc(hw);
94 struct regmap *regmap = osc->regmap;
95 u32 tmp;
97 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
98 if (tmp & AT91_PMC_OSCBYPASS)
99 return;
101 if (!(tmp & AT91_PMC_MOSCEN))
102 return;
104 tmp &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN);
105 regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
108 static int clk_main_osc_is_prepared(struct clk_hw *hw)
110 struct clk_main_osc *osc = to_clk_main_osc(hw);
111 struct regmap *regmap = osc->regmap;
112 u32 tmp, status;
114 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
115 if (tmp & AT91_PMC_OSCBYPASS)
116 return 1;
118 regmap_read(regmap, AT91_PMC_SR, &status);
120 return (status & AT91_PMC_MOSCS) && clk_main_parent_select(tmp);
123 static const struct clk_ops main_osc_ops = {
124 .prepare = clk_main_osc_prepare,
125 .unprepare = clk_main_osc_unprepare,
126 .is_prepared = clk_main_osc_is_prepared,
129 struct clk_hw * __init
130 at91_clk_register_main_osc(struct regmap *regmap,
131 const char *name,
132 const char *parent_name,
133 bool bypass)
135 struct clk_main_osc *osc;
136 struct clk_init_data init;
137 struct clk_hw *hw;
138 int ret;
140 if (!name || !parent_name)
141 return ERR_PTR(-EINVAL);
143 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
144 if (!osc)
145 return ERR_PTR(-ENOMEM);
147 init.name = name;
148 init.ops = &main_osc_ops;
149 init.parent_names = &parent_name;
150 init.num_parents = 1;
151 init.flags = CLK_IGNORE_UNUSED;
153 osc->hw.init = &init;
154 osc->regmap = regmap;
156 if (bypass)
157 regmap_update_bits(regmap,
158 AT91_CKGR_MOR, MOR_KEY_MASK |
159 AT91_PMC_OSCBYPASS,
160 AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
162 hw = &osc->hw;
163 ret = clk_hw_register(NULL, &osc->hw);
164 if (ret) {
165 kfree(osc);
166 hw = ERR_PTR(ret);
169 return hw;
172 static bool clk_main_rc_osc_ready(struct regmap *regmap)
174 unsigned int status;
176 regmap_read(regmap, AT91_PMC_SR, &status);
178 return !!(status & AT91_PMC_MOSCRCS);
181 static int clk_main_rc_osc_prepare(struct clk_hw *hw)
183 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
184 struct regmap *regmap = osc->regmap;
185 unsigned int mor;
187 regmap_read(regmap, AT91_CKGR_MOR, &mor);
189 if (!(mor & AT91_PMC_MOSCRCEN))
190 regmap_update_bits(regmap, AT91_CKGR_MOR,
191 MOR_KEY_MASK | AT91_PMC_MOSCRCEN,
192 AT91_PMC_MOSCRCEN | AT91_PMC_KEY);
194 while (!clk_main_rc_osc_ready(regmap))
195 cpu_relax();
197 return 0;
200 static void clk_main_rc_osc_unprepare(struct clk_hw *hw)
202 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
203 struct regmap *regmap = osc->regmap;
204 unsigned int mor;
206 regmap_read(regmap, AT91_CKGR_MOR, &mor);
208 if (!(mor & AT91_PMC_MOSCRCEN))
209 return;
211 regmap_update_bits(regmap, AT91_CKGR_MOR,
212 MOR_KEY_MASK | AT91_PMC_MOSCRCEN, AT91_PMC_KEY);
215 static int clk_main_rc_osc_is_prepared(struct clk_hw *hw)
217 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
218 struct regmap *regmap = osc->regmap;
219 unsigned int mor, status;
221 regmap_read(regmap, AT91_CKGR_MOR, &mor);
222 regmap_read(regmap, AT91_PMC_SR, &status);
224 return (mor & AT91_PMC_MOSCRCEN) && (status & AT91_PMC_MOSCRCS);
227 static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw,
228 unsigned long parent_rate)
230 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
232 return osc->frequency;
235 static unsigned long clk_main_rc_osc_recalc_accuracy(struct clk_hw *hw,
236 unsigned long parent_acc)
238 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
240 return osc->accuracy;
243 static const struct clk_ops main_rc_osc_ops = {
244 .prepare = clk_main_rc_osc_prepare,
245 .unprepare = clk_main_rc_osc_unprepare,
246 .is_prepared = clk_main_rc_osc_is_prepared,
247 .recalc_rate = clk_main_rc_osc_recalc_rate,
248 .recalc_accuracy = clk_main_rc_osc_recalc_accuracy,
251 struct clk_hw * __init
252 at91_clk_register_main_rc_osc(struct regmap *regmap,
253 const char *name,
254 u32 frequency, u32 accuracy)
256 struct clk_main_rc_osc *osc;
257 struct clk_init_data init;
258 struct clk_hw *hw;
259 int ret;
261 if (!name || !frequency)
262 return ERR_PTR(-EINVAL);
264 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
265 if (!osc)
266 return ERR_PTR(-ENOMEM);
268 init.name = name;
269 init.ops = &main_rc_osc_ops;
270 init.parent_names = NULL;
271 init.num_parents = 0;
272 init.flags = CLK_IGNORE_UNUSED;
274 osc->hw.init = &init;
275 osc->regmap = regmap;
276 osc->frequency = frequency;
277 osc->accuracy = accuracy;
279 hw = &osc->hw;
280 ret = clk_hw_register(NULL, hw);
281 if (ret) {
282 kfree(osc);
283 hw = ERR_PTR(ret);
286 return hw;
289 static int clk_main_probe_frequency(struct regmap *regmap)
291 unsigned long prep_time, timeout;
292 unsigned int mcfr;
294 timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT);
295 do {
296 prep_time = jiffies;
297 regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
298 if (mcfr & AT91_PMC_MAINRDY)
299 return 0;
300 if (system_state < SYSTEM_RUNNING)
301 udelay(MAINF_LOOP_MIN_WAIT);
302 else
303 usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT);
304 } while (time_before(prep_time, timeout));
306 return -ETIMEDOUT;
309 static unsigned long clk_main_recalc_rate(struct regmap *regmap,
310 unsigned long parent_rate)
312 unsigned int mcfr;
314 if (parent_rate)
315 return parent_rate;
317 pr_warn("Main crystal frequency not set, using approximate value\n");
318 regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
319 if (!(mcfr & AT91_PMC_MAINRDY))
320 return 0;
322 return ((mcfr & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
325 static int clk_rm9200_main_prepare(struct clk_hw *hw)
327 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
329 return clk_main_probe_frequency(clkmain->regmap);
332 static int clk_rm9200_main_is_prepared(struct clk_hw *hw)
334 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
335 unsigned int status;
337 regmap_read(clkmain->regmap, AT91_CKGR_MCFR, &status);
339 return !!(status & AT91_PMC_MAINRDY);
342 static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
343 unsigned long parent_rate)
345 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
347 return clk_main_recalc_rate(clkmain->regmap, parent_rate);
350 static const struct clk_ops rm9200_main_ops = {
351 .prepare = clk_rm9200_main_prepare,
352 .is_prepared = clk_rm9200_main_is_prepared,
353 .recalc_rate = clk_rm9200_main_recalc_rate,
356 struct clk_hw * __init
357 at91_clk_register_rm9200_main(struct regmap *regmap,
358 const char *name,
359 const char *parent_name)
361 struct clk_rm9200_main *clkmain;
362 struct clk_init_data init;
363 struct clk_hw *hw;
364 int ret;
366 if (!name)
367 return ERR_PTR(-EINVAL);
369 if (!parent_name)
370 return ERR_PTR(-EINVAL);
372 clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
373 if (!clkmain)
374 return ERR_PTR(-ENOMEM);
376 init.name = name;
377 init.ops = &rm9200_main_ops;
378 init.parent_names = &parent_name;
379 init.num_parents = 1;
380 init.flags = 0;
382 clkmain->hw.init = &init;
383 clkmain->regmap = regmap;
385 hw = &clkmain->hw;
386 ret = clk_hw_register(NULL, &clkmain->hw);
387 if (ret) {
388 kfree(clkmain);
389 hw = ERR_PTR(ret);
392 return hw;
395 static inline bool clk_sam9x5_main_ready(struct regmap *regmap)
397 unsigned int status;
399 regmap_read(regmap, AT91_PMC_SR, &status);
401 return !!(status & AT91_PMC_MOSCSELS);
404 static int clk_sam9x5_main_prepare(struct clk_hw *hw)
406 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
407 struct regmap *regmap = clkmain->regmap;
409 while (!clk_sam9x5_main_ready(regmap))
410 cpu_relax();
412 return clk_main_probe_frequency(regmap);
415 static int clk_sam9x5_main_is_prepared(struct clk_hw *hw)
417 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
419 return clk_sam9x5_main_ready(clkmain->regmap);
422 static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
423 unsigned long parent_rate)
425 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
427 return clk_main_recalc_rate(clkmain->regmap, parent_rate);
430 static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
432 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
433 struct regmap *regmap = clkmain->regmap;
434 unsigned int tmp;
436 if (index > 1)
437 return -EINVAL;
439 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
441 if (index && !(tmp & AT91_PMC_MOSCSEL))
442 tmp = AT91_PMC_MOSCSEL;
443 else if (!index && (tmp & AT91_PMC_MOSCSEL))
444 tmp = 0;
445 else
446 return 0;
448 regmap_update_bits(regmap, AT91_CKGR_MOR,
449 AT91_PMC_MOSCSEL | MOR_KEY_MASK,
450 tmp | AT91_PMC_KEY);
452 while (!clk_sam9x5_main_ready(regmap))
453 cpu_relax();
455 return 0;
458 static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw)
460 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
461 unsigned int status;
463 regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
465 return clk_main_parent_select(status);
468 static const struct clk_ops sam9x5_main_ops = {
469 .prepare = clk_sam9x5_main_prepare,
470 .is_prepared = clk_sam9x5_main_is_prepared,
471 .recalc_rate = clk_sam9x5_main_recalc_rate,
472 .set_parent = clk_sam9x5_main_set_parent,
473 .get_parent = clk_sam9x5_main_get_parent,
476 struct clk_hw * __init
477 at91_clk_register_sam9x5_main(struct regmap *regmap,
478 const char *name,
479 const char **parent_names,
480 int num_parents)
482 struct clk_sam9x5_main *clkmain;
483 struct clk_init_data init;
484 unsigned int status;
485 struct clk_hw *hw;
486 int ret;
488 if (!name)
489 return ERR_PTR(-EINVAL);
491 if (!parent_names || !num_parents)
492 return ERR_PTR(-EINVAL);
494 clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
495 if (!clkmain)
496 return ERR_PTR(-ENOMEM);
498 init.name = name;
499 init.ops = &sam9x5_main_ops;
500 init.parent_names = parent_names;
501 init.num_parents = num_parents;
502 init.flags = CLK_SET_PARENT_GATE;
504 clkmain->hw.init = &init;
505 clkmain->regmap = regmap;
506 regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
507 clkmain->parent = clk_main_parent_select(status);
509 hw = &clkmain->hw;
510 ret = clk_hw_register(NULL, &clkmain->hw);
511 if (ret) {
512 kfree(clkmain);
513 hw = ERR_PTR(ret);
516 return hw;