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 .get_parent = clk_stm32_mux_get_parent,
279 .set_parent = clk_stm32_mux_set_parent,
282 static void clk_stm32_gate_endisable(struct clk_hw *hw, int enable)
284 struct clk_stm32_gate *gate = to_clk_stm32_gate(hw);
285 unsigned long flags = 0;
287 spin_lock_irqsave(gate->lock, flags);
289 stm32_gate_endisable(gate->base, gate->clock_data, gate->gate_id, enable);
291 spin_unlock_irqrestore(gate->lock, flags);
294 static int clk_stm32_gate_enable(struct clk_hw *hw)
296 clk_stm32_gate_endisable(hw, 1);
301 static void clk_stm32_gate_disable(struct clk_hw *hw)
303 clk_stm32_gate_endisable(hw, 0);
306 static int clk_stm32_gate_is_enabled(struct clk_hw *hw)
308 struct clk_stm32_gate *gate = to_clk_stm32_gate(hw);
310 return stm32_gate_is_enabled(gate->base, gate->clock_data, gate->gate_id);
313 static void clk_stm32_gate_disable_unused(struct clk_hw *hw)
315 struct clk_stm32_gate *gate = to_clk_stm32_gate(hw);
316 unsigned long flags = 0;
318 spin_lock_irqsave(gate->lock, flags);
320 stm32_gate_disable_unused(gate->base, gate->clock_data, gate->gate_id);
322 spin_unlock_irqrestore(gate->lock, flags);
325 const struct clk_ops clk_stm32_gate_ops = {
326 .enable = clk_stm32_gate_enable,
327 .disable = clk_stm32_gate_disable,
328 .is_enabled = clk_stm32_gate_is_enabled,
329 .disable_unused = clk_stm32_gate_disable_unused,
332 static int clk_stm32_divider_set_rate(struct clk_hw *hw, unsigned long rate,
333 unsigned long parent_rate)
335 struct clk_stm32_div *div = to_clk_stm32_divider(hw);
336 unsigned long flags = 0;
339 if (div->div_id == NO_STM32_DIV)
342 spin_lock_irqsave(div->lock, flags);
344 ret = stm32_divider_set_rate(div->base, div->clock_data, div->div_id, rate, parent_rate);
346 spin_unlock_irqrestore(div->lock, flags);
351 static long clk_stm32_divider_round_rate(struct clk_hw *hw, unsigned long rate,
352 unsigned long *prate)
354 struct clk_stm32_div *div = to_clk_stm32_divider(hw);
355 const struct stm32_div_cfg *divider;
357 if (div->div_id == NO_STM32_DIV)
360 divider = &div->clock_data->dividers[div->div_id];
362 /* if read only, just return current value */
363 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
366 val = readl(div->base + divider->offset) >> divider->shift;
367 val &= clk_div_mask(divider->width);
369 return divider_ro_round_rate(hw, rate, prate, divider->table,
370 divider->width, divider->flags,
374 return divider_round_rate_parent(hw, clk_hw_get_parent(hw),
375 rate, prate, divider->table,
376 divider->width, divider->flags);
379 static unsigned long clk_stm32_divider_recalc_rate(struct clk_hw *hw,
380 unsigned long parent_rate)
382 struct clk_stm32_div *div = to_clk_stm32_divider(hw);
384 if (div->div_id == NO_STM32_DIV)
387 return stm32_divider_get_rate(div->base, div->clock_data, div->div_id, parent_rate);
390 const struct clk_ops clk_stm32_divider_ops = {
391 .recalc_rate = clk_stm32_divider_recalc_rate,
392 .round_rate = clk_stm32_divider_round_rate,
393 .set_rate = clk_stm32_divider_set_rate,
396 static int clk_stm32_composite_set_rate(struct clk_hw *hw, unsigned long rate,
397 unsigned long parent_rate)
399 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
400 unsigned long flags = 0;
403 if (composite->div_id == NO_STM32_DIV)
406 spin_lock_irqsave(composite->lock, flags);
408 ret = stm32_divider_set_rate(composite->base, composite->clock_data,
409 composite->div_id, rate, parent_rate);
411 spin_unlock_irqrestore(composite->lock, flags);
416 static unsigned long clk_stm32_composite_recalc_rate(struct clk_hw *hw,
417 unsigned long parent_rate)
419 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
421 if (composite->div_id == NO_STM32_DIV)
424 return stm32_divider_get_rate(composite->base, composite->clock_data,
425 composite->div_id, parent_rate);
428 static long clk_stm32_composite_round_rate(struct clk_hw *hw, unsigned long rate,
429 unsigned long *prate)
431 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
433 const struct stm32_div_cfg *divider;
435 if (composite->div_id == NO_STM32_DIV)
438 divider = &composite->clock_data->dividers[composite->div_id];
440 /* if read only, just return current value */
441 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
444 val = readl(composite->base + divider->offset) >> divider->shift;
445 val &= clk_div_mask(divider->width);
447 return divider_ro_round_rate(hw, rate, prate, divider->table,
448 divider->width, divider->flags,
452 return divider_round_rate_parent(hw, clk_hw_get_parent(hw),
453 rate, prate, divider->table,
454 divider->width, divider->flags);
457 static u8 clk_stm32_composite_get_parent(struct clk_hw *hw)
459 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
461 return stm32_mux_get_parent(composite->base, composite->clock_data, composite->mux_id);
464 static int clk_stm32_composite_set_parent(struct clk_hw *hw, u8 index)
466 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
467 unsigned long flags = 0;
469 spin_lock_irqsave(composite->lock, flags);
471 stm32_mux_set_parent(composite->base, composite->clock_data, composite->mux_id, index);
473 spin_unlock_irqrestore(composite->lock, flags);
475 if (composite->clock_data->is_multi_mux) {
476 struct clk_hw *other_mux_hw = composite->clock_data->is_multi_mux(hw);
479 struct clk_hw *hwp = clk_hw_get_parent_by_index(hw, index);
481 clk_hw_reparent(other_mux_hw, hwp);
488 static int clk_stm32_composite_is_enabled(struct clk_hw *hw)
490 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
492 if (composite->gate_id == NO_STM32_GATE)
493 return (__clk_get_enable_count(hw->clk) > 0);
495 return stm32_gate_is_enabled(composite->base, composite->clock_data, composite->gate_id);
498 #define MUX_SAFE_POSITION 0
500 static int clk_stm32_has_safe_mux(struct clk_hw *hw)
502 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
503 const struct stm32_mux_cfg *mux = &composite->clock_data->muxes[composite->mux_id];
505 return !!(mux->flags & MUX_SAFE);
508 static void clk_stm32_set_safe_position_mux(struct clk_hw *hw)
510 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
512 if (!clk_stm32_composite_is_enabled(hw)) {
513 unsigned long flags = 0;
515 if (composite->clock_data->is_multi_mux) {
516 struct clk_hw *other_mux_hw = NULL;
518 other_mux_hw = composite->clock_data->is_multi_mux(hw);
520 if (!other_mux_hw || clk_stm32_composite_is_enabled(other_mux_hw))
524 spin_lock_irqsave(composite->lock, flags);
526 stm32_mux_set_parent(composite->base, composite->clock_data,
527 composite->mux_id, MUX_SAFE_POSITION);
529 spin_unlock_irqrestore(composite->lock, flags);
533 static void clk_stm32_safe_restore_position_mux(struct clk_hw *hw)
535 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
536 int sel = clk_hw_get_parent_index(hw);
537 unsigned long flags = 0;
539 spin_lock_irqsave(composite->lock, flags);
541 stm32_mux_set_parent(composite->base, composite->clock_data, composite->mux_id, sel);
543 spin_unlock_irqrestore(composite->lock, flags);
546 static void clk_stm32_composite_gate_endisable(struct clk_hw *hw, int enable)
548 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
549 unsigned long flags = 0;
551 spin_lock_irqsave(composite->lock, flags);
553 stm32_gate_endisable(composite->base, composite->clock_data, composite->gate_id, enable);
555 spin_unlock_irqrestore(composite->lock, flags);
558 static int clk_stm32_composite_gate_enable(struct clk_hw *hw)
560 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
562 if (composite->gate_id == NO_STM32_GATE)
565 clk_stm32_composite_gate_endisable(hw, 1);
567 if (composite->mux_id != NO_STM32_MUX && clk_stm32_has_safe_mux(hw))
568 clk_stm32_safe_restore_position_mux(hw);
573 static void clk_stm32_composite_gate_disable(struct clk_hw *hw)
575 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
577 if (composite->gate_id == NO_STM32_GATE)
580 clk_stm32_composite_gate_endisable(hw, 0);
582 if (composite->mux_id != NO_STM32_MUX && clk_stm32_has_safe_mux(hw))
583 clk_stm32_set_safe_position_mux(hw);
586 static void clk_stm32_composite_disable_unused(struct clk_hw *hw)
588 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
589 unsigned long flags = 0;
591 if (composite->gate_id == NO_STM32_GATE)
594 spin_lock_irqsave(composite->lock, flags);
596 stm32_gate_disable_unused(composite->base, composite->clock_data, composite->gate_id);
598 spin_unlock_irqrestore(composite->lock, flags);
601 const struct clk_ops clk_stm32_composite_ops = {
602 .set_rate = clk_stm32_composite_set_rate,
603 .recalc_rate = clk_stm32_composite_recalc_rate,
604 .round_rate = clk_stm32_composite_round_rate,
605 .get_parent = clk_stm32_composite_get_parent,
606 .set_parent = clk_stm32_composite_set_parent,
607 .enable = clk_stm32_composite_gate_enable,
608 .disable = clk_stm32_composite_gate_disable,
609 .is_enabled = clk_stm32_composite_is_enabled,
610 .disable_unused = clk_stm32_composite_disable_unused,
613 struct clk_hw *clk_stm32_mux_register(struct device *dev,
614 const struct stm32_rcc_match_data *data,
617 const struct clock_config *cfg)
619 struct clk_stm32_mux *mux = cfg->clock_cfg;
620 struct clk_hw *hw = &mux->hw;
625 mux->clock_data = data->clock_data;
627 err = clk_hw_register(dev, hw);
634 struct clk_hw *clk_stm32_gate_register(struct device *dev,
635 const struct stm32_rcc_match_data *data,
638 const struct clock_config *cfg)
640 struct clk_stm32_gate *gate = cfg->clock_cfg;
641 struct clk_hw *hw = &gate->hw;
646 gate->clock_data = data->clock_data;
648 err = clk_hw_register(dev, hw);
655 struct clk_hw *clk_stm32_div_register(struct device *dev,
656 const struct stm32_rcc_match_data *data,
659 const struct clock_config *cfg)
661 struct clk_stm32_div *div = cfg->clock_cfg;
662 struct clk_hw *hw = &div->hw;
667 div->clock_data = data->clock_data;
669 err = clk_hw_register(dev, hw);
676 struct clk_hw *clk_stm32_composite_register(struct device *dev,
677 const struct stm32_rcc_match_data *data,
680 const struct clock_config *cfg)
682 struct clk_stm32_composite *composite = cfg->clock_cfg;
683 struct clk_hw *hw = &composite->hw;
686 composite->base = base;
687 composite->lock = lock;
688 composite->clock_data = data->clock_data;
690 err = clk_hw_register(dev, hw);