1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics 2022 - All Rights Reserved
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
13 #include <linux/of_address.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
17 #include "clk-stm32-core.h"
18 #include "reset-stm32.h"
20 static DEFINE_SPINLOCK(rlock);
22 static int stm32_rcc_clock_init(struct device *dev,
23 const struct of_device_id *match,
26 const struct stm32_rcc_match_data *data = match->data;
27 struct clk_hw_onecell_data *clk_data = data->hw_clks;
28 struct device_node *np = dev_of_node(dev);
32 max_binding = data->maxbinding;
34 clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, max_binding), GFP_KERNEL);
38 clk_data->num = max_binding;
42 for (n = 0; n < max_binding; n++)
43 hws[n] = ERR_PTR(-ENOENT);
45 for (n = 0; n < data->num_clocks; n++) {
46 const struct clock_config *cfg_clock = &data->tab_clocks[n];
47 struct clk_hw *hw = ERR_PTR(-ENOENT);
49 if (data->check_security &&
50 data->check_security(base, cfg_clock))
54 hw = (*cfg_clock->func)(dev, data, base, &rlock,
58 dev_err(dev, "Can't register clk %d: %ld\n", n,
63 if (cfg_clock->id != NO_ID)
64 hws[cfg_clock->id] = hw;
67 return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data);
70 int stm32_rcc_init(struct device *dev, const struct of_device_id *match_data,
73 const struct of_device_id *match;
76 match = of_match_node(match_data, dev_of_node(dev));
78 dev_err(dev, "match data not found\n");
82 /* RCC Reset Configuration */
83 err = stm32_rcc_reset_init(dev, match, base);
85 pr_err("stm32 reset failed to initialize\n");
89 /* RCC Clock Configuration */
90 err = stm32_rcc_clock_init(dev, match, base);
92 pr_err("stm32 clock failed to initialize\n");
99 static u8 stm32_mux_get_parent(void __iomem *base,
100 struct clk_stm32_clock_data *data,
103 const struct stm32_mux_cfg *mux = &data->muxes[mux_id];
104 u32 mask = BIT(mux->width) - 1;
107 val = readl(base + mux->offset) >> mux->shift;
113 static int stm32_mux_set_parent(void __iomem *base,
114 struct clk_stm32_clock_data *data,
115 u16 mux_id, u8 index)
117 const struct stm32_mux_cfg *mux = &data->muxes[mux_id];
119 u32 mask = BIT(mux->width) - 1;
120 u32 reg = readl(base + mux->offset);
121 u32 val = index << mux->shift;
123 reg &= ~(mask << mux->shift);
126 writel(reg, base + mux->offset);
131 static void stm32_gate_endisable(void __iomem *base,
132 struct clk_stm32_clock_data *data,
133 u16 gate_id, int enable)
135 const struct stm32_gate_cfg *gate = &data->gates[gate_id];
136 void __iomem *addr = base + gate->offset;
139 if (data->gate_cpt[gate_id]++ > 0)
142 if (gate->set_clr != 0)
143 writel(BIT(gate->bit_idx), addr);
145 writel(readl(addr) | BIT(gate->bit_idx), addr);
147 if (--data->gate_cpt[gate_id] > 0)
150 if (gate->set_clr != 0)
151 writel(BIT(gate->bit_idx), addr + gate->set_clr);
153 writel(readl(addr) & ~BIT(gate->bit_idx), addr);
157 static void stm32_gate_disable_unused(void __iomem *base,
158 struct clk_stm32_clock_data *data,
161 const struct stm32_gate_cfg *gate = &data->gates[gate_id];
162 void __iomem *addr = base + gate->offset;
164 if (data->gate_cpt[gate_id] > 0)
167 if (gate->set_clr != 0)
168 writel(BIT(gate->bit_idx), addr + gate->set_clr);
170 writel(readl(addr) & ~BIT(gate->bit_idx), addr);
173 static int stm32_gate_is_enabled(void __iomem *base,
174 struct clk_stm32_clock_data *data,
177 const struct stm32_gate_cfg *gate = &data->gates[gate_id];
179 return (readl(base + gate->offset) & BIT(gate->bit_idx)) != 0;
182 static unsigned int _get_table_div(const struct clk_div_table *table,
185 const struct clk_div_table *clkt;
187 for (clkt = table; clkt->div; clkt++)
188 if (clkt->val == val)
193 static unsigned int _get_div(const struct clk_div_table *table,
194 unsigned int val, unsigned long flags, u8 width)
196 if (flags & CLK_DIVIDER_ONE_BASED)
198 if (flags & CLK_DIVIDER_POWER_OF_TWO)
201 return _get_table_div(table, val);
205 static unsigned long stm32_divider_get_rate(void __iomem *base,
206 struct clk_stm32_clock_data *data,
208 unsigned long parent_rate)
210 const struct stm32_div_cfg *divider = &data->dividers[div_id];
214 val = readl(base + divider->offset) >> divider->shift;
215 val &= clk_div_mask(divider->width);
216 div = _get_div(divider->table, val, divider->flags, divider->width);
219 WARN(!(divider->flags & CLK_DIVIDER_ALLOW_ZERO),
220 "%d: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
225 return DIV_ROUND_UP_ULL((u64)parent_rate, div);
228 static int stm32_divider_set_rate(void __iomem *base,
229 struct clk_stm32_clock_data *data,
230 u16 div_id, unsigned long rate,
231 unsigned long parent_rate)
233 const struct stm32_div_cfg *divider = &data->dividers[div_id];
237 value = divider_get_val(rate, parent_rate, divider->table,
238 divider->width, divider->flags);
242 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
243 val = clk_div_mask(divider->width) << (divider->shift + 16);
245 val = readl(base + divider->offset);
246 val &= ~(clk_div_mask(divider->width) << divider->shift);
249 val |= (u32)value << divider->shift;
251 writel(val, base + divider->offset);
256 static u8 clk_stm32_mux_get_parent(struct clk_hw *hw)
258 struct clk_stm32_mux *mux = to_clk_stm32_mux(hw);
260 return stm32_mux_get_parent(mux->base, mux->clock_data, mux->mux_id);
263 static int clk_stm32_mux_set_parent(struct clk_hw *hw, u8 index)
265 struct clk_stm32_mux *mux = to_clk_stm32_mux(hw);
266 unsigned long flags = 0;
268 spin_lock_irqsave(mux->lock, flags);
270 stm32_mux_set_parent(mux->base, mux->clock_data, mux->mux_id, index);
272 spin_unlock_irqrestore(mux->lock, flags);
277 const struct clk_ops clk_stm32_mux_ops = {
278 .determine_rate = __clk_mux_determine_rate,
279 .get_parent = clk_stm32_mux_get_parent,
280 .set_parent = clk_stm32_mux_set_parent,
283 static void clk_stm32_gate_endisable(struct clk_hw *hw, int enable)
285 struct clk_stm32_gate *gate = to_clk_stm32_gate(hw);
286 unsigned long flags = 0;
288 spin_lock_irqsave(gate->lock, flags);
290 stm32_gate_endisable(gate->base, gate->clock_data, gate->gate_id, enable);
292 spin_unlock_irqrestore(gate->lock, flags);
295 static int clk_stm32_gate_enable(struct clk_hw *hw)
297 clk_stm32_gate_endisable(hw, 1);
302 static void clk_stm32_gate_disable(struct clk_hw *hw)
304 clk_stm32_gate_endisable(hw, 0);
307 static int clk_stm32_gate_is_enabled(struct clk_hw *hw)
309 struct clk_stm32_gate *gate = to_clk_stm32_gate(hw);
311 return stm32_gate_is_enabled(gate->base, gate->clock_data, gate->gate_id);
314 static void clk_stm32_gate_disable_unused(struct clk_hw *hw)
316 struct clk_stm32_gate *gate = to_clk_stm32_gate(hw);
317 unsigned long flags = 0;
319 spin_lock_irqsave(gate->lock, flags);
321 stm32_gate_disable_unused(gate->base, gate->clock_data, gate->gate_id);
323 spin_unlock_irqrestore(gate->lock, flags);
326 const struct clk_ops clk_stm32_gate_ops = {
327 .enable = clk_stm32_gate_enable,
328 .disable = clk_stm32_gate_disable,
329 .is_enabled = clk_stm32_gate_is_enabled,
330 .disable_unused = clk_stm32_gate_disable_unused,
333 static int clk_stm32_divider_set_rate(struct clk_hw *hw, unsigned long rate,
334 unsigned long parent_rate)
336 struct clk_stm32_div *div = to_clk_stm32_divider(hw);
337 unsigned long flags = 0;
340 if (div->div_id == NO_STM32_DIV)
343 spin_lock_irqsave(div->lock, flags);
345 ret = stm32_divider_set_rate(div->base, div->clock_data, div->div_id, rate, parent_rate);
347 spin_unlock_irqrestore(div->lock, flags);
352 static long clk_stm32_divider_round_rate(struct clk_hw *hw, unsigned long rate,
353 unsigned long *prate)
355 struct clk_stm32_div *div = to_clk_stm32_divider(hw);
356 const struct stm32_div_cfg *divider;
358 if (div->div_id == NO_STM32_DIV)
361 divider = &div->clock_data->dividers[div->div_id];
363 /* if read only, just return current value */
364 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
367 val = readl(div->base + divider->offset) >> divider->shift;
368 val &= clk_div_mask(divider->width);
370 return divider_ro_round_rate(hw, rate, prate, divider->table,
371 divider->width, divider->flags,
375 return divider_round_rate_parent(hw, clk_hw_get_parent(hw),
376 rate, prate, divider->table,
377 divider->width, divider->flags);
380 static unsigned long clk_stm32_divider_recalc_rate(struct clk_hw *hw,
381 unsigned long parent_rate)
383 struct clk_stm32_div *div = to_clk_stm32_divider(hw);
385 if (div->div_id == NO_STM32_DIV)
388 return stm32_divider_get_rate(div->base, div->clock_data, div->div_id, parent_rate);
391 const struct clk_ops clk_stm32_divider_ops = {
392 .recalc_rate = clk_stm32_divider_recalc_rate,
393 .round_rate = clk_stm32_divider_round_rate,
394 .set_rate = clk_stm32_divider_set_rate,
397 static int clk_stm32_composite_set_rate(struct clk_hw *hw, unsigned long rate,
398 unsigned long parent_rate)
400 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
401 unsigned long flags = 0;
404 if (composite->div_id == NO_STM32_DIV)
407 spin_lock_irqsave(composite->lock, flags);
409 ret = stm32_divider_set_rate(composite->base, composite->clock_data,
410 composite->div_id, rate, parent_rate);
412 spin_unlock_irqrestore(composite->lock, flags);
417 static unsigned long clk_stm32_composite_recalc_rate(struct clk_hw *hw,
418 unsigned long parent_rate)
420 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
422 if (composite->div_id == NO_STM32_DIV)
425 return stm32_divider_get_rate(composite->base, composite->clock_data,
426 composite->div_id, parent_rate);
429 static int clk_stm32_composite_determine_rate(struct clk_hw *hw,
430 struct clk_rate_request *req)
432 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
433 const struct stm32_div_cfg *divider;
436 if (composite->div_id == NO_STM32_DIV)
439 divider = &composite->clock_data->dividers[composite->div_id];
441 /* if read only, just return current value */
442 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
445 val = readl(composite->base + divider->offset) >> divider->shift;
446 val &= clk_div_mask(divider->width);
448 rate = divider_ro_round_rate(hw, req->rate, &req->best_parent_rate,
449 divider->table, divider->width, divider->flags,
458 rate = divider_round_rate_parent(hw, clk_hw_get_parent(hw),
459 req->rate, &req->best_parent_rate,
460 divider->table, divider->width, divider->flags);
468 static u8 clk_stm32_composite_get_parent(struct clk_hw *hw)
470 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
472 return stm32_mux_get_parent(composite->base, composite->clock_data, composite->mux_id);
475 static int clk_stm32_composite_set_parent(struct clk_hw *hw, u8 index)
477 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
478 unsigned long flags = 0;
480 spin_lock_irqsave(composite->lock, flags);
482 stm32_mux_set_parent(composite->base, composite->clock_data, composite->mux_id, index);
484 spin_unlock_irqrestore(composite->lock, flags);
486 if (composite->clock_data->is_multi_mux) {
487 struct clk_hw *other_mux_hw = composite->clock_data->is_multi_mux(hw);
490 struct clk_hw *hwp = clk_hw_get_parent_by_index(hw, index);
492 clk_hw_reparent(other_mux_hw, hwp);
499 static int clk_stm32_composite_is_enabled(struct clk_hw *hw)
501 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
503 if (composite->gate_id == NO_STM32_GATE)
504 return (__clk_get_enable_count(hw->clk) > 0);
506 return stm32_gate_is_enabled(composite->base, composite->clock_data, composite->gate_id);
509 #define MUX_SAFE_POSITION 0
511 static int clk_stm32_has_safe_mux(struct clk_hw *hw)
513 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
514 const struct stm32_mux_cfg *mux = &composite->clock_data->muxes[composite->mux_id];
516 return !!(mux->flags & MUX_SAFE);
519 static void clk_stm32_set_safe_position_mux(struct clk_hw *hw)
521 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
523 if (!clk_stm32_composite_is_enabled(hw)) {
524 unsigned long flags = 0;
526 if (composite->clock_data->is_multi_mux) {
527 struct clk_hw *other_mux_hw = NULL;
529 other_mux_hw = composite->clock_data->is_multi_mux(hw);
531 if (!other_mux_hw || clk_stm32_composite_is_enabled(other_mux_hw))
535 spin_lock_irqsave(composite->lock, flags);
537 stm32_mux_set_parent(composite->base, composite->clock_data,
538 composite->mux_id, MUX_SAFE_POSITION);
540 spin_unlock_irqrestore(composite->lock, flags);
544 static void clk_stm32_safe_restore_position_mux(struct clk_hw *hw)
546 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
547 int sel = clk_hw_get_parent_index(hw);
548 unsigned long flags = 0;
550 spin_lock_irqsave(composite->lock, flags);
552 stm32_mux_set_parent(composite->base, composite->clock_data, composite->mux_id, sel);
554 spin_unlock_irqrestore(composite->lock, flags);
557 static void clk_stm32_composite_gate_endisable(struct clk_hw *hw, int enable)
559 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
560 unsigned long flags = 0;
562 spin_lock_irqsave(composite->lock, flags);
564 stm32_gate_endisable(composite->base, composite->clock_data, composite->gate_id, enable);
566 spin_unlock_irqrestore(composite->lock, flags);
569 static int clk_stm32_composite_gate_enable(struct clk_hw *hw)
571 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
573 if (composite->gate_id == NO_STM32_GATE)
576 clk_stm32_composite_gate_endisable(hw, 1);
578 if (composite->mux_id != NO_STM32_MUX && clk_stm32_has_safe_mux(hw))
579 clk_stm32_safe_restore_position_mux(hw);
584 static void clk_stm32_composite_gate_disable(struct clk_hw *hw)
586 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
588 if (composite->gate_id == NO_STM32_GATE)
591 clk_stm32_composite_gate_endisable(hw, 0);
593 if (composite->mux_id != NO_STM32_MUX && clk_stm32_has_safe_mux(hw))
594 clk_stm32_set_safe_position_mux(hw);
597 static void clk_stm32_composite_disable_unused(struct clk_hw *hw)
599 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
600 unsigned long flags = 0;
602 if (composite->gate_id == NO_STM32_GATE)
605 spin_lock_irqsave(composite->lock, flags);
607 stm32_gate_disable_unused(composite->base, composite->clock_data, composite->gate_id);
609 spin_unlock_irqrestore(composite->lock, flags);
612 const struct clk_ops clk_stm32_composite_ops = {
613 .set_rate = clk_stm32_composite_set_rate,
614 .recalc_rate = clk_stm32_composite_recalc_rate,
615 .determine_rate = clk_stm32_composite_determine_rate,
616 .get_parent = clk_stm32_composite_get_parent,
617 .set_parent = clk_stm32_composite_set_parent,
618 .enable = clk_stm32_composite_gate_enable,
619 .disable = clk_stm32_composite_gate_disable,
620 .is_enabled = clk_stm32_composite_is_enabled,
621 .disable_unused = clk_stm32_composite_disable_unused,
624 struct clk_hw *clk_stm32_mux_register(struct device *dev,
625 const struct stm32_rcc_match_data *data,
628 const struct clock_config *cfg)
630 struct clk_stm32_mux *mux = cfg->clock_cfg;
631 struct clk_hw *hw = &mux->hw;
636 mux->clock_data = data->clock_data;
638 err = clk_hw_register(dev, hw);
645 struct clk_hw *clk_stm32_gate_register(struct device *dev,
646 const struct stm32_rcc_match_data *data,
649 const struct clock_config *cfg)
651 struct clk_stm32_gate *gate = cfg->clock_cfg;
652 struct clk_hw *hw = &gate->hw;
657 gate->clock_data = data->clock_data;
659 err = clk_hw_register(dev, hw);
666 struct clk_hw *clk_stm32_div_register(struct device *dev,
667 const struct stm32_rcc_match_data *data,
670 const struct clock_config *cfg)
672 struct clk_stm32_div *div = cfg->clock_cfg;
673 struct clk_hw *hw = &div->hw;
678 div->clock_data = data->clock_data;
680 err = clk_hw_register(dev, hw);
687 struct clk_hw *clk_stm32_composite_register(struct device *dev,
688 const struct stm32_rcc_match_data *data,
691 const struct clock_config *cfg)
693 struct clk_stm32_composite *composite = cfg->clock_cfg;
694 struct clk_hw *hw = &composite->hw;
697 composite->base = base;
698 composite->lock = lock;
699 composite->clock_data = data->clock_data;
701 err = clk_hw_register(dev, hw);