blob: 355c9dab2da3c78b039781a1d0f0104a079db368 [file] [log] [blame]
Gabriel Fernandez1308d752020-03-11 11:30:34 +01001/*
2 * Copyright (C) 2022, STMicroelectronics - All Rights Reserved
3 *
4 * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
5 */
6
7#include <assert.h>
8#include <errno.h>
9
10#include "clk-stm32-core.h"
11#include <common/debug.h>
12#include <common/fdt_wrappers.h>
13#include <drivers/clk.h>
14#include <drivers/delay_timer.h>
15#include <drivers/st/stm32mp_clkfunc.h>
16#include <lib/mmio.h>
17#include <lib/spinlock.h>
18
19static struct spinlock reg_lock;
20static struct spinlock refcount_lock;
21
22static struct stm32_clk_priv *stm32_clock_data;
23
24const struct stm32_clk_ops clk_mux_ops;
25
26struct stm32_clk_priv *clk_stm32_get_priv(void)
27{
28 return stm32_clock_data;
29}
30
31static void stm32mp1_clk_lock(struct spinlock *lock)
32{
33 if (stm32mp_lock_available()) {
34 /* Assume interrupts are masked */
35 spin_lock(lock);
36 }
37}
38
39static void stm32mp1_clk_unlock(struct spinlock *lock)
40{
41 if (stm32mp_lock_available()) {
42 spin_unlock(lock);
43 }
44}
45
46void stm32mp1_clk_rcc_regs_lock(void)
47{
48 stm32mp1_clk_lock(&reg_lock);
49}
50
51void stm32mp1_clk_rcc_regs_unlock(void)
52{
53 stm32mp1_clk_unlock(&reg_lock);
54}
55
56#define TIMEOUT_US_1S U(1000000)
57#define OSCRDY_TIMEOUT TIMEOUT_US_1S
58
59struct clk_oscillator_data *clk_oscillator_get_data(struct stm32_clk_priv *priv, int id)
60{
61 const struct clk_stm32 *clk = _clk_get(priv, id);
62 struct stm32_osc_cfg *osc_cfg = clk->clock_cfg;
63 int osc_id = osc_cfg->osc_id;
64
65 return &priv->osci_data[osc_id];
66}
67
68void clk_oscillator_set_bypass(struct stm32_clk_priv *priv, int id, bool digbyp, bool bypass)
69{
70 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id);
71
72 struct stm32_clk_bypass *bypass_data = osc_data->bypass;
73 uintptr_t address;
74
75 if (bypass_data == NULL) {
76 return;
77 }
78
79 address = priv->base + bypass_data->offset;
80
81 if (digbyp) {
82 mmio_setbits_32(address, BIT(bypass_data->bit_digbyp));
83 }
84
85 if (bypass || digbyp) {
86 mmio_setbits_32(address, BIT(bypass_data->bit_byp));
87 }
88}
89
90void clk_oscillator_set_css(struct stm32_clk_priv *priv, int id, bool css)
91{
92 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id);
93
94 struct stm32_clk_css *css_data = osc_data->css;
95 uintptr_t address;
96
97 if (css_data == NULL) {
98 return;
99 }
100
101 address = priv->base + css_data->offset;
102
103 if (css) {
104 mmio_setbits_32(address, BIT(css_data->bit_css));
105 }
106}
107
108void clk_oscillator_set_drive(struct stm32_clk_priv *priv, int id, uint8_t lsedrv)
109{
110 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id);
111
112 struct stm32_clk_drive *drive_data = osc_data->drive;
113 uintptr_t address;
114 uint32_t mask;
115 uint32_t value;
116
117 if (drive_data == NULL) {
118 return;
119 }
120
121 address = priv->base + drive_data->offset;
122
123 mask = (BIT(drive_data->drv_width) - 1U) << drive_data->drv_shift;
124
125 /*
126 * Warning: not recommended to switch directly from "high drive"
127 * to "medium low drive", and vice-versa.
128 */
129 value = (mmio_read_32(address) & mask) >> drive_data->drv_shift;
130
131 while (value != lsedrv) {
132 if (value > lsedrv) {
133 value--;
134 } else {
135 value++;
136 }
137
138 mmio_clrsetbits_32(address, mask, value << drive_data->drv_shift);
139 }
140}
141
142int clk_oscillator_wait_ready(struct stm32_clk_priv *priv, int id, bool ready_on)
143{
144 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id);
145
146 return _clk_stm32_gate_wait_ready(priv, osc_data->gate_id, ready_on);
147}
148
149int clk_oscillator_wait_ready_on(struct stm32_clk_priv *priv, int id)
150{
151 return clk_oscillator_wait_ready(priv, id, true);
152}
153
154int clk_oscillator_wait_ready_off(struct stm32_clk_priv *priv, int id)
155{
156 return clk_oscillator_wait_ready(priv, id, false);
157}
158
159static int clk_gate_enable(struct stm32_clk_priv *priv, int id)
160{
161 const struct clk_stm32 *clk = _clk_get(priv, id);
162 struct clk_gate_cfg *cfg = clk->clock_cfg;
163
164 mmio_setbits_32(priv->base + cfg->offset, BIT(cfg->bit_idx));
165
166 return 0;
167}
168
169static void clk_gate_disable(struct stm32_clk_priv *priv, int id)
170{
171 const struct clk_stm32 *clk = _clk_get(priv, id);
172 struct clk_gate_cfg *cfg = clk->clock_cfg;
173
174 mmio_clrbits_32(priv->base + cfg->offset, BIT(cfg->bit_idx));
175}
176
177static bool clk_gate_is_enabled(struct stm32_clk_priv *priv, int id)
178{
179 const struct clk_stm32 *clk = _clk_get(priv, id);
180 struct clk_gate_cfg *cfg = clk->clock_cfg;
181
182 return ((mmio_read_32(priv->base + cfg->offset) & BIT(cfg->bit_idx)) != 0U);
183}
184
185const struct stm32_clk_ops clk_gate_ops = {
186 .enable = clk_gate_enable,
187 .disable = clk_gate_disable,
188 .is_enabled = clk_gate_is_enabled,
189};
190
191void _clk_stm32_gate_disable(struct stm32_clk_priv *priv, uint16_t gate_id)
192{
193 const struct gate_cfg *gate = &priv->gates[gate_id];
194 uintptr_t addr = priv->base + gate->offset;
195
196 if (gate->set_clr != 0U) {
197 mmio_write_32(addr + RCC_MP_ENCLRR_OFFSET, BIT(gate->bit_idx));
198 } else {
199 mmio_clrbits_32(addr, BIT(gate->bit_idx));
200 }
201}
202
203int _clk_stm32_gate_enable(struct stm32_clk_priv *priv, uint16_t gate_id)
204{
205 const struct gate_cfg *gate = &priv->gates[gate_id];
206 uintptr_t addr = priv->base + gate->offset;
207
208 if (gate->set_clr != 0U) {
209 mmio_write_32(addr, BIT(gate->bit_idx));
210
211 } else {
212 mmio_setbits_32(addr, BIT(gate->bit_idx));
213 }
214
215 return 0;
216}
217
218const char *_clk_stm32_get_name(struct stm32_clk_priv *priv, int id)
219{
220 return priv->clks[id].name;
221}
222
223const char *clk_stm32_get_name(struct stm32_clk_priv *priv,
224 unsigned long binding_id)
225{
226 int id;
227
228 id = clk_get_index(priv, binding_id);
229 if (id == -EINVAL) {
230 return NULL;
231 }
232
233 return _clk_stm32_get_name(priv, id);
234}
235
236const struct clk_stm32 *_clk_get(struct stm32_clk_priv *priv, int id)
237{
238 if ((unsigned int)id < priv->num) {
239 return &priv->clks[id];
240 }
241
242 return NULL;
243}
244
245#define clk_div_mask(_width) GENMASK(((_width) - 1U), 0U)
246
247static unsigned int _get_table_div(const struct clk_div_table *table,
248 unsigned int val)
249{
250 const struct clk_div_table *clkt;
251
252 for (clkt = table; clkt->div; clkt++) {
253 if (clkt->val == val) {
254 return clkt->div;
255 }
256 }
257
258 return 0;
259}
260
261static unsigned int _get_div(const struct clk_div_table *table,
262 unsigned int val, unsigned long flags,
263 uint8_t width)
264{
265 if ((flags & CLK_DIVIDER_ONE_BASED) != 0UL) {
266 return val;
267 }
268
269 if ((flags & CLK_DIVIDER_POWER_OF_TWO) != 0UL) {
270 return BIT(val);
271 }
272
273 if ((flags & CLK_DIVIDER_MAX_AT_ZERO) != 0UL) {
274 return (val != 0U) ? val : BIT(width);
275 }
276
277 if (table != NULL) {
278 return _get_table_div(table, val);
279 }
280
281 return val + 1U;
282}
283
284#define TIMEOUT_US_200MS U(200000)
285#define CLKSRC_TIMEOUT TIMEOUT_US_200MS
286
287int clk_mux_set_parent(struct stm32_clk_priv *priv, uint16_t pid, uint8_t sel)
288{
289 const struct parent_cfg *parents = &priv->parents[pid & MUX_PARENT_MASK];
290 const struct mux_cfg *mux = parents->mux;
291 uintptr_t address = priv->base + mux->offset;
292 uint32_t mask;
293 uint64_t timeout;
294
295 mask = MASK_WIDTH_SHIFT(mux->width, mux->shift);
296
297 mmio_clrsetbits_32(address, mask, (sel << mux->shift) & mask);
298
299 if (mux->bitrdy == MUX_NO_BIT_RDY) {
300 return 0;
301 }
302
303 timeout = timeout_init_us(CLKSRC_TIMEOUT);
304
305 mask = BIT(mux->bitrdy);
306
307 while ((mmio_read_32(address) & mask) == 0U) {
308 if (timeout_elapsed(timeout)) {
309 return -ETIMEDOUT;
310 }
311 }
312
313 return 0;
314}
315
316int _clk_stm32_set_parent(struct stm32_clk_priv *priv, int clk, int clkp)
317{
318 const struct parent_cfg *parents;
319 uint16_t pid;
320 uint8_t sel;
321 int old_parent;
322
323 pid = priv->clks[clk].parent;
324
325 if ((pid == CLK_IS_ROOT) || (pid < MUX_MAX_PARENTS)) {
326 return -EINVAL;
327 }
328
329 old_parent = _clk_stm32_get_parent(priv, clk);
330 if (old_parent == clkp) {
331 return 0;
332 }
333
334 parents = &priv->parents[pid & MUX_PARENT_MASK];
335
336 for (sel = 0; sel < parents->num_parents; sel++) {
337 if (parents->id_parents[sel] == (uint16_t)clkp) {
338 bool clk_was_enabled = _clk_stm32_is_enabled(priv, clk);
339 int err = 0;
340
341 /* Enable the parents (for glitch free mux) */
342 _clk_stm32_enable(priv, clkp);
343 _clk_stm32_enable(priv, old_parent);
344
345 err = clk_mux_set_parent(priv, pid, sel);
346
347 _clk_stm32_disable(priv, old_parent);
348
349 if (clk_was_enabled) {
350 _clk_stm32_disable(priv, old_parent);
351 } else {
352 _clk_stm32_disable(priv, clkp);
353 }
354
355 return err;
356 }
357 }
358
359 return -EINVAL;
360}
361
362int clk_mux_get_parent(struct stm32_clk_priv *priv, uint32_t mux_id)
363{
364 const struct parent_cfg *parent;
365 const struct mux_cfg *mux;
366 uint32_t mask;
367
368 if (mux_id >= priv->nb_parents) {
369 panic();
370 }
371
372 parent = &priv->parents[mux_id];
373 mux = parent->mux;
374
375 mask = MASK_WIDTH_SHIFT(mux->width, mux->shift);
376
377 return (mmio_read_32(priv->base + mux->offset) & mask) >> mux->shift;
378}
379
380int _clk_stm32_set_parent_by_index(struct stm32_clk_priv *priv, int clk, int sel)
381{
382 uint16_t pid;
383
384 pid = priv->clks[clk].parent;
385
386 if ((pid == CLK_IS_ROOT) || (pid < MUX_MAX_PARENTS)) {
387 return -EINVAL;
388 }
389
390 return clk_mux_set_parent(priv, pid, sel);
391}
392
393int _clk_stm32_get_parent(struct stm32_clk_priv *priv, int clk_id)
394{
395 const struct clk_stm32 *clk = _clk_get(priv, clk_id);
396 const struct parent_cfg *parent;
397 uint16_t mux_id;
398 int sel;
399
400 mux_id = priv->clks[clk_id].parent;
401 if (mux_id == CLK_IS_ROOT) {
402 return CLK_IS_ROOT;
403 }
404
405 if (mux_id < MUX_MAX_PARENTS) {
406 return mux_id & MUX_PARENT_MASK;
407 }
408
409 mux_id &= MUX_PARENT_MASK;
410 parent = &priv->parents[mux_id];
411
412 if (clk->ops->get_parent != NULL) {
413 sel = clk->ops->get_parent(priv, clk_id);
414 } else {
415 sel = clk_mux_get_parent(priv, mux_id);
416 }
417
418 if (sel < parent->num_parents) {
419 return parent->id_parents[sel];
420 }
421
422 return -EINVAL;
423}
424
425int _clk_stm32_get_parent_index(struct stm32_clk_priv *priv, int clk_id)
426{
427 uint16_t mux_id;
428
429 mux_id = priv->clks[clk_id].parent;
430 if (mux_id == CLK_IS_ROOT) {
431 return CLK_IS_ROOT;
432 }
433
434 if (mux_id < MUX_MAX_PARENTS) {
435 return mux_id & MUX_PARENT_MASK;
436 }
437
438 mux_id &= MUX_PARENT_MASK;
439
440 return clk_mux_get_parent(priv, mux_id);
441}
442
443int _clk_stm32_get_parent_by_index(struct stm32_clk_priv *priv, int clk_id, int idx)
444{
445 const struct parent_cfg *parent;
446 uint16_t mux_id;
447
448 mux_id = priv->clks[clk_id].parent;
449 if (mux_id == CLK_IS_ROOT) {
450 return CLK_IS_ROOT;
451 }
452
453 if (mux_id < MUX_MAX_PARENTS) {
454 return mux_id & MUX_PARENT_MASK;
455 }
456
457 mux_id &= MUX_PARENT_MASK;
458 parent = &priv->parents[mux_id];
459
460 if (idx < parent->num_parents) {
461 return parent->id_parents[idx];
462 }
463
464 return -EINVAL;
465}
466
467int clk_get_index(struct stm32_clk_priv *priv, unsigned long binding_id)
468{
469 unsigned int i;
470
471 for (i = 0U; i < priv->num; i++) {
472 if (binding_id == priv->clks[i].binding) {
473 return (int)i;
474 }
475 }
476
477 return -EINVAL;
478}
479
480unsigned long _clk_stm32_get_rate(struct stm32_clk_priv *priv, int id)
481{
482 const struct clk_stm32 *clk = _clk_get(priv, id);
483 int parent;
484 unsigned long rate = 0UL;
485
486 if ((unsigned int)id >= priv->num) {
487 return rate;
488 }
489
490 parent = _clk_stm32_get_parent(priv, id);
491
492 if (clk->ops->recalc_rate != NULL) {
493 unsigned long prate = 0UL;
494
495 if (parent != CLK_IS_ROOT) {
496 prate = _clk_stm32_get_rate(priv, parent);
497 }
498
499 rate = clk->ops->recalc_rate(priv, id, prate);
500
501 return rate;
502 }
503
504 switch (parent) {
505 case CLK_IS_ROOT:
506 panic();
507
508 default:
509 rate = _clk_stm32_get_rate(priv, parent);
510 break;
511 }
512 return rate;
513
514}
515
516unsigned long _clk_stm32_get_parent_rate(struct stm32_clk_priv *priv, int id)
517{
518 int parent_id = _clk_stm32_get_parent(priv, id);
519
520 return _clk_stm32_get_rate(priv, parent_id);
521}
522
523static uint8_t _stm32_clk_get_flags(struct stm32_clk_priv *priv, int id)
524{
525 return priv->clks[id].flags;
526}
527
528bool _stm32_clk_is_flags(struct stm32_clk_priv *priv, int id, uint8_t flag)
529{
530 if (_stm32_clk_get_flags(priv, id) & flag) {
531 return true;
532 }
533
534 return false;
535}
536
537int clk_stm32_enable_call_ops(struct stm32_clk_priv *priv, uint16_t id)
538{
539 const struct clk_stm32 *clk = _clk_get(priv, id);
540
541 if (clk->ops->enable != NULL) {
542 clk->ops->enable(priv, id);
543 }
544
545 return 0;
546}
547
548static int _clk_stm32_enable_core(struct stm32_clk_priv *priv, int id)
549{
550 int parent;
551 int ret = 0;
552
553 if (priv->gate_refcounts[id] == 0U) {
554 parent = _clk_stm32_get_parent(priv, id);
555 if (parent != CLK_IS_ROOT) {
556 ret = _clk_stm32_enable_core(priv, parent);
557 if (ret) {
558 return ret;
559 }
560 }
561 clk_stm32_enable_call_ops(priv, id);
562 }
563
564 priv->gate_refcounts[id]++;
565
566 if (priv->gate_refcounts[id] == UINT_MAX) {
567 ERROR("%s: %d max enable count !", __func__, id);
568 panic();
569 }
570
571 return 0;
572}
573
574int _clk_stm32_enable(struct stm32_clk_priv *priv, int id)
575{
576 int ret;
577
578 stm32mp1_clk_lock(&refcount_lock);
579 ret = _clk_stm32_enable_core(priv, id);
580 stm32mp1_clk_unlock(&refcount_lock);
581
582 return ret;
583}
584
585void clk_stm32_disable_call_ops(struct stm32_clk_priv *priv, uint16_t id)
586{
587 const struct clk_stm32 *clk = _clk_get(priv, id);
588
589 if (clk->ops->disable != NULL) {
590 clk->ops->disable(priv, id);
591 }
592}
593
594static void _clk_stm32_disable_core(struct stm32_clk_priv *priv, int id)
595{
596 int parent;
597
598 if ((priv->gate_refcounts[id] == 1U) && _stm32_clk_is_flags(priv, id, CLK_IS_CRITICAL)) {
599 return;
600 }
601
602 if (priv->gate_refcounts[id] == 0U) {
603 /* case of clock ignore unused */
604 if (_clk_stm32_is_enabled(priv, id)) {
605 clk_stm32_disable_call_ops(priv, id);
606 return;
607 }
608 VERBOSE("%s: %d already disabled !\n\n", __func__, id);
609 return;
610 }
611
612 if (--priv->gate_refcounts[id] > 0U) {
613 return;
614 }
615
616 clk_stm32_disable_call_ops(priv, id);
617
618 parent = _clk_stm32_get_parent(priv, id);
619 if (parent != CLK_IS_ROOT) {
620 _clk_stm32_disable_core(priv, parent);
621 }
622}
623
624void _clk_stm32_disable(struct stm32_clk_priv *priv, int id)
625{
626 stm32mp1_clk_lock(&refcount_lock);
627
628 _clk_stm32_disable_core(priv, id);
629
630 stm32mp1_clk_unlock(&refcount_lock);
631}
632
633bool _clk_stm32_is_enabled(struct stm32_clk_priv *priv, int id)
634{
635 const struct clk_stm32 *clk = _clk_get(priv, id);
636
637 if (clk->ops->is_enabled != NULL) {
638 return clk->ops->is_enabled(priv, id);
639 }
640
641 return priv->gate_refcounts[id];
642}
643
644static int clk_stm32_enable(unsigned long binding_id)
645{
646 struct stm32_clk_priv *priv = clk_stm32_get_priv();
647 int id;
648
649 id = clk_get_index(priv, binding_id);
650 if (id == -EINVAL) {
651 return id;
652 }
653
654 return _clk_stm32_enable(priv, id);
655}
656
657static void clk_stm32_disable(unsigned long binding_id)
658{
659 struct stm32_clk_priv *priv = clk_stm32_get_priv();
660 int id;
661
662 id = clk_get_index(priv, binding_id);
663 if (id != -EINVAL) {
664 _clk_stm32_disable(priv, id);
665 }
666}
667
668static bool clk_stm32_is_enabled(unsigned long binding_id)
669{
670 struct stm32_clk_priv *priv = clk_stm32_get_priv();
671 int id;
672
673 id = clk_get_index(priv, binding_id);
674 if (id == -EINVAL) {
675 return false;
676 }
677
678 return _clk_stm32_is_enabled(priv, id);
679}
680
681static unsigned long clk_stm32_get_rate(unsigned long binding_id)
682{
683 struct stm32_clk_priv *priv = clk_stm32_get_priv();
684 int id;
685
686 id = clk_get_index(priv, binding_id);
687 if (id == -EINVAL) {
688 return 0UL;
689 }
690
691 return _clk_stm32_get_rate(priv, id);
692}
693
694static int clk_stm32_get_parent(unsigned long binding_id)
695{
696 struct stm32_clk_priv *priv = clk_stm32_get_priv();
697 int id;
698
699 id = clk_get_index(priv, binding_id);
700 if (id == -EINVAL) {
701 return id;
702 }
703
704 return _clk_stm32_get_parent(priv, id);
705}
706
707static const struct clk_ops stm32mp_clk_ops = {
708 .enable = clk_stm32_enable,
709 .disable = clk_stm32_disable,
710 .is_enabled = clk_stm32_is_enabled,
711 .get_rate = clk_stm32_get_rate,
712 .get_parent = clk_stm32_get_parent,
713};
714
715void clk_stm32_enable_critical_clocks(void)
716{
717 struct stm32_clk_priv *priv = clk_stm32_get_priv();
718 unsigned int i;
719
720 for (i = 0U; i < priv->num; i++) {
721 if (_stm32_clk_is_flags(priv, i, CLK_IS_CRITICAL)) {
722 _clk_stm32_enable(priv, i);
723 }
724 }
725}
726
727static void stm32_clk_register(void)
728{
729 clk_register(&stm32mp_clk_ops);
730}
731
732uint32_t clk_stm32_div_get_value(struct stm32_clk_priv *priv, int div_id)
733{
734 const struct div_cfg *divider = &priv->div[div_id];
735 uint32_t val = 0;
736
737 val = mmio_read_32(priv->base + divider->offset) >> divider->shift;
738 val &= clk_div_mask(divider->width);
739
740 return val;
741}
742
743unsigned long _clk_stm32_divider_recalc(struct stm32_clk_priv *priv,
744 int div_id,
745 unsigned long prate)
746{
747 const struct div_cfg *divider = &priv->div[div_id];
748 uint32_t val = clk_stm32_div_get_value(priv, div_id);
749 unsigned int div = 0U;
750
751 div = _get_div(divider->table, val, divider->flags, divider->width);
752 if (div == 0U) {
753 return prate;
754 }
755
756 return div_round_up((uint64_t)prate, div);
757}
758
759unsigned long clk_stm32_divider_recalc(struct stm32_clk_priv *priv, int id,
760 unsigned long prate)
761{
762 const struct clk_stm32 *clk = _clk_get(priv, id);
763 struct clk_stm32_div_cfg *div_cfg = clk->clock_cfg;
764
765 return _clk_stm32_divider_recalc(priv, div_cfg->id, prate);
766}
767
768const struct stm32_clk_ops clk_stm32_divider_ops = {
769 .recalc_rate = clk_stm32_divider_recalc,
770};
771
772int clk_stm32_set_div(struct stm32_clk_priv *priv, uint32_t div_id, uint32_t value)
773{
774 const struct div_cfg *divider;
775 uintptr_t address;
776 uint64_t timeout;
777 uint32_t mask;
778
779 if (div_id >= priv->nb_div) {
780 panic();
781 }
782
783 divider = &priv->div[div_id];
784 address = priv->base + divider->offset;
785
786 mask = MASK_WIDTH_SHIFT(divider->width, divider->shift);
787 mmio_clrsetbits_32(address, mask, (value << divider->shift) & mask);
788
789 if (divider->bitrdy == DIV_NO_BIT_RDY) {
790 return 0;
791 }
792
793 timeout = timeout_init_us(CLKSRC_TIMEOUT);
794 mask = BIT(divider->bitrdy);
795
796 while ((mmio_read_32(address) & mask) == 0U) {
797 if (timeout_elapsed(timeout)) {
798 return -ETIMEDOUT;
799 }
800 }
801
802 return 0;
803}
804
805int _clk_stm32_gate_wait_ready(struct stm32_clk_priv *priv, uint16_t gate_id,
806 bool ready_on)
807{
808 const struct gate_cfg *gate = &priv->gates[gate_id];
809 uintptr_t address = priv->base + gate->offset;
810 uint32_t mask_rdy = BIT(gate->bit_idx);
811 uint64_t timeout;
812 uint32_t mask_test;
813
814 if (ready_on) {
815 mask_test = BIT(gate->bit_idx);
816 } else {
817 mask_test = 0U;
818 }
819
820 timeout = timeout_init_us(OSCRDY_TIMEOUT);
821
822 while ((mmio_read_32(address) & mask_rdy) != mask_test) {
823 if (timeout_elapsed(timeout)) {
824 break;
825 }
826 }
827
828 if ((mmio_read_32(address) & mask_rdy) != mask_test)
829 return -ETIMEDOUT;
830
831 return 0;
832}
833
834int clk_stm32_gate_enable(struct stm32_clk_priv *priv, int id)
835{
836 const struct clk_stm32 *clk = _clk_get(priv, id);
837 struct clk_stm32_gate_cfg *cfg = clk->clock_cfg;
838 const struct gate_cfg *gate = &priv->gates[cfg->id];
839 uintptr_t addr = priv->base + gate->offset;
840
841 if (gate->set_clr != 0U) {
842 mmio_write_32(addr, BIT(gate->bit_idx));
843
844 } else {
845 mmio_setbits_32(addr, BIT(gate->bit_idx));
846 }
847
848 return 0;
849}
850
851void clk_stm32_gate_disable(struct stm32_clk_priv *priv, int id)
852{
853 const struct clk_stm32 *clk = _clk_get(priv, id);
854 struct clk_stm32_gate_cfg *cfg = clk->clock_cfg;
855 const struct gate_cfg *gate = &priv->gates[cfg->id];
856 uintptr_t addr = priv->base + gate->offset;
857
858 if (gate->set_clr != 0U) {
859 mmio_write_32(addr + RCC_MP_ENCLRR_OFFSET, BIT(gate->bit_idx));
860 } else {
861 mmio_clrbits_32(addr, BIT(gate->bit_idx));
862 }
863}
864
865bool _clk_stm32_gate_is_enabled(struct stm32_clk_priv *priv, int gate_id)
866{
867 const struct gate_cfg *gate;
868 uint32_t addr;
869
870 gate = &priv->gates[gate_id];
871 addr = priv->base + gate->offset;
872
873 return ((mmio_read_32(addr) & BIT(gate->bit_idx)) != 0U);
874}
875
876bool clk_stm32_gate_is_enabled(struct stm32_clk_priv *priv, int id)
877{
878 const struct clk_stm32 *clk = _clk_get(priv, id);
879 struct clk_stm32_gate_cfg *cfg = clk->clock_cfg;
880
881 return _clk_stm32_gate_is_enabled(priv, cfg->id);
882}
883
884const struct stm32_clk_ops clk_stm32_gate_ops = {
885 .enable = clk_stm32_gate_enable,
886 .disable = clk_stm32_gate_disable,
887 .is_enabled = clk_stm32_gate_is_enabled,
888};
889
890const struct stm32_clk_ops clk_fixed_factor_ops = {
891 .recalc_rate = fixed_factor_recalc_rate,
892};
893
894unsigned long fixed_factor_recalc_rate(struct stm32_clk_priv *priv,
895 int id, unsigned long prate)
896{
897 const struct clk_stm32 *clk = _clk_get(priv, id);
898 const struct fixed_factor_cfg *cfg = clk->clock_cfg;
899 unsigned long long rate;
900
901 rate = (unsigned long long)prate * cfg->mult;
902
903 if (cfg->div == 0U) {
904 ERROR("division by zero\n");
905 panic();
906 }
907
908 return (unsigned long)(rate / cfg->div);
909};
910
911#define APB_DIV_MASK GENMASK(2, 0)
912#define TIM_PRE_MASK BIT(0)
913
914static unsigned long timer_recalc_rate(struct stm32_clk_priv *priv,
915 int id, unsigned long prate)
916{
917 const struct clk_stm32 *clk = _clk_get(priv, id);
918 const struct clk_timer_cfg *cfg = clk->clock_cfg;
919 uint32_t prescaler, timpre;
920 uintptr_t rcc_base = priv->base;
921
922 prescaler = mmio_read_32(rcc_base + cfg->apbdiv) &
923 APB_DIV_MASK;
924
925 timpre = mmio_read_32(rcc_base + cfg->timpre) &
926 TIM_PRE_MASK;
927
928 if (prescaler == 0U) {
929 return prate;
930 }
931
932 return prate * (timpre + 1U) * 2U;
933};
934
935const struct stm32_clk_ops clk_timer_ops = {
936 .recalc_rate = timer_recalc_rate,
937};
938
939static unsigned long clk_fixed_rate_recalc(struct stm32_clk_priv *priv, int id,
940 unsigned long prate)
941{
942 const struct clk_stm32 *clk = _clk_get(priv, id);
943 struct clk_stm32_fixed_rate_cfg *cfg = clk->clock_cfg;
944
945 return cfg->rate;
946}
947
948const struct stm32_clk_ops clk_stm32_fixed_rate_ops = {
949 .recalc_rate = clk_fixed_rate_recalc,
950};
951
952static unsigned long clk_stm32_osc_recalc_rate(struct stm32_clk_priv *priv,
953 int id, unsigned long prate)
954{
955 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id);
956
957 return osc_data->frequency;
958};
959
960bool clk_stm32_osc_gate_is_enabled(struct stm32_clk_priv *priv, int id)
961{
962 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id);
963
964 return _clk_stm32_gate_is_enabled(priv, osc_data->gate_id);
965
966}
967
968int clk_stm32_osc_gate_enable(struct stm32_clk_priv *priv, int id)
969{
970 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id);
971
972 _clk_stm32_gate_enable(priv, osc_data->gate_id);
973
974 if (_clk_stm32_gate_wait_ready(priv, osc_data->gate_rdy_id, true) != 0U) {
975 ERROR("%s: %s (%d)\n", __func__, osc_data->name, __LINE__);
976 panic();
977 }
978
979 return 0;
980}
981
982void clk_stm32_osc_gate_disable(struct stm32_clk_priv *priv, int id)
983{
984 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id);
985
986 _clk_stm32_gate_disable(priv, osc_data->gate_id);
987
988 if (_clk_stm32_gate_wait_ready(priv, osc_data->gate_rdy_id, false) != 0U) {
989 ERROR("%s: %s (%d)\n", __func__, osc_data->name, __LINE__);
990 panic();
991 }
992}
993
994static unsigned long clk_stm32_get_dt_oscillator_frequency(const char *name)
995{
996 void *fdt = NULL;
997 int node = 0;
998 int subnode = 0;
999
1000 if (fdt_get_address(&fdt) == 0) {
1001 panic();
1002 }
1003
1004 node = fdt_path_offset(fdt, "/clocks");
1005 if (node < 0) {
1006 return 0UL;
1007 }
1008
1009 fdt_for_each_subnode(subnode, fdt, node) {
1010 const char *cchar = NULL;
1011 const fdt32_t *cuint = NULL;
1012 int ret = 0;
1013
1014 cchar = fdt_get_name(fdt, subnode, &ret);
1015 if (cchar == NULL) {
1016 continue;
1017 }
1018
1019 if (strncmp(cchar, name, (size_t)ret) ||
1020 fdt_get_status(subnode) == DT_DISABLED) {
1021 continue;
1022 }
1023
1024 cuint = fdt_getprop(fdt, subnode, "clock-frequency", &ret);
1025 if (cuint == NULL) {
1026 return 0UL;
1027 }
1028
1029 return fdt32_to_cpu(*cuint);
1030 }
1031
1032 return 0UL;
1033}
1034
1035void clk_stm32_osc_init(struct stm32_clk_priv *priv, int id)
1036{
1037 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id);
1038 const char *name = osc_data->name;
1039
1040 osc_data->frequency = clk_stm32_get_dt_oscillator_frequency(name);
1041}
1042
1043const struct stm32_clk_ops clk_stm32_osc_ops = {
1044 .recalc_rate = clk_stm32_osc_recalc_rate,
1045 .is_enabled = clk_stm32_osc_gate_is_enabled,
1046 .enable = clk_stm32_osc_gate_enable,
1047 .disable = clk_stm32_osc_gate_disable,
1048 .init = clk_stm32_osc_init,
1049};
1050
1051const struct stm32_clk_ops clk_stm32_osc_nogate_ops = {
1052 .recalc_rate = clk_stm32_osc_recalc_rate,
1053 .init = clk_stm32_osc_init,
1054};
1055
1056int stm32_clk_parse_fdt_by_name(void *fdt, int node, const char *name, uint32_t *tab, uint32_t *nb)
1057{
1058 const fdt32_t *cell;
1059 int len = 0;
1060 uint32_t i;
1061
1062 cell = fdt_getprop(fdt, node, name, &len);
1063 if (cell != NULL) {
1064 for (i = 0; i < ((uint32_t)len / sizeof(uint32_t)); i++) {
1065 uint32_t val = fdt32_to_cpu(cell[i]);
1066
1067 tab[i] = val;
1068 }
1069 }
1070
1071 *nb = (uint32_t)len / sizeof(uint32_t);
1072
1073 return 0;
1074}
1075
1076int clk_stm32_init(struct stm32_clk_priv *priv, uintptr_t base)
1077{
1078 unsigned int i;
1079
1080 stm32_clock_data = priv;
1081
1082 priv->base = base;
1083
1084 for (i = 0U; i < priv->num; i++) {
1085 const struct clk_stm32 *clk = _clk_get(priv, i);
1086
1087 assert(clk->ops != NULL);
1088
1089 if (clk->ops->init != NULL) {
1090 clk->ops->init(priv, i);
1091 }
1092 }
1093
1094 stm32_clk_register();
1095
1096 return 0;
1097}