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;
31 max_binding = data->maxbinding;
33 clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, max_binding), GFP_KERNEL);
37 clk_data->num = max_binding;
41 for (n = 0; n < max_binding; n++)
42 hws[n] = ERR_PTR(-ENOENT);
44 for (n = 0; n < data->num_clocks; n++) {
45 const struct clock_config *cfg_clock = &data->tab_clocks[n];
46 struct clk_hw *hw = ERR_PTR(-ENOENT);
48 if (data->check_security &&
49 data->check_security(dev->of_node, base, cfg_clock))
53 hw = (*cfg_clock->func)(dev, data, base, &rlock,
57 dev_err(dev, "Can't register clk %d: %ld\n", n,
62 if (cfg_clock->id != NO_ID)
63 hws[cfg_clock->id] = hw;
66 return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
69 int stm32_rcc_init(struct device *dev, const struct of_device_id *match_data,
72 const struct stm32_rcc_match_data *rcc_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_match_data = match->data;
84 /* RCC Reset Configuration */
85 err = stm32_rcc_reset_init(dev, rcc_match_data->reset_data, base);
87 pr_err("stm32 reset failed to initialize\n");
91 /* RCC Clock Configuration */
92 err = stm32_rcc_clock_init(dev, match, base);
94 pr_err("stm32 clock failed to initialize\n");
101 static u8 stm32_mux_get_parent(void __iomem *base,
102 struct clk_stm32_clock_data *data,
105 const struct stm32_mux_cfg *mux = &data->muxes[mux_id];
106 u32 mask = BIT(mux->width) - 1;
109 val = readl(base + mux->offset) >> mux->shift;
115 static int stm32_mux_set_parent(void __iomem *base,
116 struct clk_stm32_clock_data *data,
117 u16 mux_id, u8 index)
119 const struct stm32_mux_cfg *mux = &data->muxes[mux_id];
121 u32 mask = BIT(mux->width) - 1;
122 u32 reg = readl(base + mux->offset);
123 u32 val = index << mux->shift;
125 reg &= ~(mask << mux->shift);
128 writel(reg, base + mux->offset);
133 static void stm32_gate_endisable(void __iomem *base,
134 struct clk_stm32_clock_data *data,
135 u16 gate_id, int enable)
137 const struct stm32_gate_cfg *gate = &data->gates[gate_id];
138 void __iomem *addr = base + gate->offset;
141 if (data->gate_cpt[gate_id]++ > 0)
144 if (gate->set_clr != 0)
145 writel(BIT(gate->bit_idx), addr);
147 writel(readl(addr) | BIT(gate->bit_idx), addr);
149 if (--data->gate_cpt[gate_id] > 0)
152 if (gate->set_clr != 0)
153 writel(BIT(gate->bit_idx), addr + gate->set_clr);
155 writel(readl(addr) & ~BIT(gate->bit_idx), addr);
159 static void stm32_gate_disable_unused(void __iomem *base,
160 struct clk_stm32_clock_data *data,
163 const struct stm32_gate_cfg *gate = &data->gates[gate_id];
164 void __iomem *addr = base + gate->offset;
166 if (data->gate_cpt[gate_id] > 0)
169 if (gate->set_clr != 0)
170 writel(BIT(gate->bit_idx), addr + gate->set_clr);
172 writel(readl(addr) & ~BIT(gate->bit_idx), addr);
175 static int stm32_gate_is_enabled(void __iomem *base,
176 struct clk_stm32_clock_data *data,
179 const struct stm32_gate_cfg *gate = &data->gates[gate_id];
181 return (readl(base + gate->offset) & BIT(gate->bit_idx)) != 0;
184 static unsigned int _get_table_div(const struct clk_div_table *table,
187 const struct clk_div_table *clkt;
189 for (clkt = table; clkt->div; clkt++)
190 if (clkt->val == val)
195 static unsigned int _get_div(const struct clk_div_table *table,
196 unsigned int val, unsigned long flags, u8 width)
198 if (flags & CLK_DIVIDER_ONE_BASED)
200 if (flags & CLK_DIVIDER_POWER_OF_TWO)
203 return _get_table_div(table, val);
207 static unsigned long stm32_divider_get_rate(void __iomem *base,
208 struct clk_stm32_clock_data *data,
210 unsigned long parent_rate)
212 const struct stm32_div_cfg *divider = &data->dividers[div_id];
216 val = readl(base + divider->offset) >> divider->shift;
217 val &= clk_div_mask(divider->width);
218 div = _get_div(divider->table, val, divider->flags, divider->width);
221 WARN(!(divider->flags & CLK_DIVIDER_ALLOW_ZERO),
222 "%d: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
227 return DIV_ROUND_UP_ULL((u64)parent_rate, div);
230 static int stm32_divider_set_rate(void __iomem *base,
231 struct clk_stm32_clock_data *data,
232 u16 div_id, unsigned long rate,
233 unsigned long parent_rate)
235 const struct stm32_div_cfg *divider = &data->dividers[div_id];
239 value = divider_get_val(rate, parent_rate, divider->table,
240 divider->width, divider->flags);
244 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
245 val = clk_div_mask(divider->width) << (divider->shift + 16);
247 val = readl(base + divider->offset);
248 val &= ~(clk_div_mask(divider->width) << divider->shift);
251 val |= (u32)value << divider->shift;
253 writel(val, base + divider->offset);
258 static u8 clk_stm32_mux_get_parent(struct clk_hw *hw)
260 struct clk_stm32_mux *mux = to_clk_stm32_mux(hw);
262 return stm32_mux_get_parent(mux->base, mux->clock_data, mux->mux_id);
265 static int clk_stm32_mux_set_parent(struct clk_hw *hw, u8 index)
267 struct clk_stm32_mux *mux = to_clk_stm32_mux(hw);
268 unsigned long flags = 0;
270 spin_lock_irqsave(mux->lock, flags);
272 stm32_mux_set_parent(mux->base, mux->clock_data, mux->mux_id, index);
274 spin_unlock_irqrestore(mux->lock, flags);
279 const struct clk_ops clk_stm32_mux_ops = {
280 .determine_rate = __clk_mux_determine_rate,
281 .get_parent = clk_stm32_mux_get_parent,
282 .set_parent = clk_stm32_mux_set_parent,
285 static void clk_stm32_gate_endisable(struct clk_hw *hw, int enable)
287 struct clk_stm32_gate *gate = to_clk_stm32_gate(hw);
288 unsigned long flags = 0;
290 spin_lock_irqsave(gate->lock, flags);
292 stm32_gate_endisable(gate->base, gate->clock_data, gate->gate_id, enable);
294 spin_unlock_irqrestore(gate->lock, flags);
297 static int clk_stm32_gate_enable(struct clk_hw *hw)
299 clk_stm32_gate_endisable(hw, 1);
304 static void clk_stm32_gate_disable(struct clk_hw *hw)
306 clk_stm32_gate_endisable(hw, 0);
309 static int clk_stm32_gate_is_enabled(struct clk_hw *hw)
311 struct clk_stm32_gate *gate = to_clk_stm32_gate(hw);
313 return stm32_gate_is_enabled(gate->base, gate->clock_data, gate->gate_id);
316 static void clk_stm32_gate_disable_unused(struct clk_hw *hw)
318 struct clk_stm32_gate *gate = to_clk_stm32_gate(hw);
319 unsigned long flags = 0;
321 spin_lock_irqsave(gate->lock, flags);
323 stm32_gate_disable_unused(gate->base, gate->clock_data, gate->gate_id);
325 spin_unlock_irqrestore(gate->lock, flags);
328 const struct clk_ops clk_stm32_gate_ops = {
329 .enable = clk_stm32_gate_enable,
330 .disable = clk_stm32_gate_disable,
331 .is_enabled = clk_stm32_gate_is_enabled,
332 .disable_unused = clk_stm32_gate_disable_unused,
335 static int clk_stm32_divider_set_rate(struct clk_hw *hw, unsigned long rate,
336 unsigned long parent_rate)
338 struct clk_stm32_div *div = to_clk_stm32_divider(hw);
339 unsigned long flags = 0;
342 if (div->div_id == NO_STM32_DIV)
345 spin_lock_irqsave(div->lock, flags);
347 ret = stm32_divider_set_rate(div->base, div->clock_data, div->div_id, rate, parent_rate);
349 spin_unlock_irqrestore(div->lock, flags);
354 static long clk_stm32_divider_round_rate(struct clk_hw *hw, unsigned long rate,
355 unsigned long *prate)
357 struct clk_stm32_div *div = to_clk_stm32_divider(hw);
358 const struct stm32_div_cfg *divider;
360 if (div->div_id == NO_STM32_DIV)
363 divider = &div->clock_data->dividers[div->div_id];
365 /* if read only, just return current value */
366 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
369 val = readl(div->base + divider->offset) >> divider->shift;
370 val &= clk_div_mask(divider->width);
372 return divider_ro_round_rate(hw, rate, prate, divider->table,
373 divider->width, divider->flags,
377 return divider_round_rate_parent(hw, clk_hw_get_parent(hw),
378 rate, prate, divider->table,
379 divider->width, divider->flags);
382 static unsigned long clk_stm32_divider_recalc_rate(struct clk_hw *hw,
383 unsigned long parent_rate)
385 struct clk_stm32_div *div = to_clk_stm32_divider(hw);
387 if (div->div_id == NO_STM32_DIV)
390 return stm32_divider_get_rate(div->base, div->clock_data, div->div_id, parent_rate);
393 const struct clk_ops clk_stm32_divider_ops = {
394 .recalc_rate = clk_stm32_divider_recalc_rate,
395 .round_rate = clk_stm32_divider_round_rate,
396 .set_rate = clk_stm32_divider_set_rate,
399 static int clk_stm32_composite_set_rate(struct clk_hw *hw, unsigned long rate,
400 unsigned long parent_rate)
402 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
403 unsigned long flags = 0;
406 if (composite->div_id == NO_STM32_DIV)
409 spin_lock_irqsave(composite->lock, flags);
411 ret = stm32_divider_set_rate(composite->base, composite->clock_data,
412 composite->div_id, rate, parent_rate);
414 spin_unlock_irqrestore(composite->lock, flags);
419 static unsigned long clk_stm32_composite_recalc_rate(struct clk_hw *hw,
420 unsigned long parent_rate)
422 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
424 if (composite->div_id == NO_STM32_DIV)
427 return stm32_divider_get_rate(composite->base, composite->clock_data,
428 composite->div_id, parent_rate);
431 static int clk_stm32_composite_determine_rate(struct clk_hw *hw,
432 struct clk_rate_request *req)
434 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
435 const struct stm32_div_cfg *divider;
438 if (composite->div_id == NO_STM32_DIV)
441 divider = &composite->clock_data->dividers[composite->div_id];
443 /* if read only, just return current value */
444 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
447 val = readl(composite->base + divider->offset) >> divider->shift;
448 val &= clk_div_mask(divider->width);
450 rate = divider_ro_round_rate(hw, req->rate, &req->best_parent_rate,
451 divider->table, divider->width, divider->flags,
460 rate = divider_round_rate_parent(hw, clk_hw_get_parent(hw),
461 req->rate, &req->best_parent_rate,
462 divider->table, divider->width, divider->flags);
470 static u8 clk_stm32_composite_get_parent(struct clk_hw *hw)
472 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
474 return stm32_mux_get_parent(composite->base, composite->clock_data, composite->mux_id);
477 static int clk_stm32_composite_set_parent(struct clk_hw *hw, u8 index)
479 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
480 unsigned long flags = 0;
482 spin_lock_irqsave(composite->lock, flags);
484 stm32_mux_set_parent(composite->base, composite->clock_data, composite->mux_id, index);
486 spin_unlock_irqrestore(composite->lock, flags);
488 if (composite->clock_data->is_multi_mux) {
489 struct clk_hw *other_mux_hw = composite->clock_data->is_multi_mux(hw);
492 struct clk_hw *hwp = clk_hw_get_parent_by_index(hw, index);
494 clk_hw_reparent(other_mux_hw, hwp);
501 static int clk_stm32_composite_is_enabled(struct clk_hw *hw)
503 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
505 if (composite->gate_id == NO_STM32_GATE)
506 return (__clk_get_enable_count(hw->clk) > 0);
508 return stm32_gate_is_enabled(composite->base, composite->clock_data, composite->gate_id);
511 #define MUX_SAFE_POSITION 0
513 static int clk_stm32_has_safe_mux(struct clk_hw *hw)
515 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
516 const struct stm32_mux_cfg *mux = &composite->clock_data->muxes[composite->mux_id];
518 return !!(mux->flags & MUX_SAFE);
521 static void clk_stm32_set_safe_position_mux(struct clk_hw *hw)
523 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
525 if (!clk_stm32_composite_is_enabled(hw)) {
526 unsigned long flags = 0;
528 if (composite->clock_data->is_multi_mux) {
529 struct clk_hw *other_mux_hw = NULL;
531 other_mux_hw = composite->clock_data->is_multi_mux(hw);
533 if (!other_mux_hw || clk_stm32_composite_is_enabled(other_mux_hw))
537 spin_lock_irqsave(composite->lock, flags);
539 stm32_mux_set_parent(composite->base, composite->clock_data,
540 composite->mux_id, MUX_SAFE_POSITION);
542 spin_unlock_irqrestore(composite->lock, flags);
546 static void clk_stm32_safe_restore_position_mux(struct clk_hw *hw)
548 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
549 int sel = clk_hw_get_parent_index(hw);
550 unsigned long flags = 0;
552 spin_lock_irqsave(composite->lock, flags);
554 stm32_mux_set_parent(composite->base, composite->clock_data, composite->mux_id, sel);
556 spin_unlock_irqrestore(composite->lock, flags);
559 static void clk_stm32_composite_gate_endisable(struct clk_hw *hw, int enable)
561 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
562 unsigned long flags = 0;
564 spin_lock_irqsave(composite->lock, flags);
566 stm32_gate_endisable(composite->base, composite->clock_data, composite->gate_id, enable);
568 spin_unlock_irqrestore(composite->lock, flags);
571 static int clk_stm32_composite_gate_enable(struct clk_hw *hw)
573 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
575 if (composite->gate_id == NO_STM32_GATE)
578 clk_stm32_composite_gate_endisable(hw, 1);
580 if (composite->mux_id != NO_STM32_MUX && clk_stm32_has_safe_mux(hw))
581 clk_stm32_safe_restore_position_mux(hw);
586 static void clk_stm32_composite_gate_disable(struct clk_hw *hw)
588 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
590 if (composite->gate_id == NO_STM32_GATE)
593 clk_stm32_composite_gate_endisable(hw, 0);
595 if (composite->mux_id != NO_STM32_MUX && clk_stm32_has_safe_mux(hw))
596 clk_stm32_set_safe_position_mux(hw);
599 static void clk_stm32_composite_disable_unused(struct clk_hw *hw)
601 struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
602 unsigned long flags = 0;
604 if (composite->gate_id == NO_STM32_GATE)
607 spin_lock_irqsave(composite->lock, flags);
609 stm32_gate_disable_unused(composite->base, composite->clock_data, composite->gate_id);
611 spin_unlock_irqrestore(composite->lock, flags);
614 const struct clk_ops clk_stm32_composite_ops = {
615 .set_rate = clk_stm32_composite_set_rate,
616 .recalc_rate = clk_stm32_composite_recalc_rate,
617 .determine_rate = clk_stm32_composite_determine_rate,
618 .get_parent = clk_stm32_composite_get_parent,
619 .set_parent = clk_stm32_composite_set_parent,
620 .enable = clk_stm32_composite_gate_enable,
621 .disable = clk_stm32_composite_gate_disable,
622 .is_enabled = clk_stm32_composite_is_enabled,
623 .disable_unused = clk_stm32_composite_disable_unused,
626 struct clk_hw *clk_stm32_mux_register(struct device *dev,
627 const struct stm32_rcc_match_data *data,
630 const struct clock_config *cfg)
632 struct clk_stm32_mux *mux = cfg->clock_cfg;
633 struct clk_hw *hw = &mux->hw;
638 mux->clock_data = data->clock_data;
640 err = devm_clk_hw_register(dev, hw);
647 struct clk_hw *clk_stm32_gate_register(struct device *dev,
648 const struct stm32_rcc_match_data *data,
651 const struct clock_config *cfg)
653 struct clk_stm32_gate *gate = cfg->clock_cfg;
654 struct clk_hw *hw = &gate->hw;
659 gate->clock_data = data->clock_data;
661 err = devm_clk_hw_register(dev, hw);
668 struct clk_hw *clk_stm32_div_register(struct device *dev,
669 const struct stm32_rcc_match_data *data,
672 const struct clock_config *cfg)
674 struct clk_stm32_div *div = cfg->clock_cfg;
675 struct clk_hw *hw = &div->hw;
680 div->clock_data = data->clock_data;
682 err = devm_clk_hw_register(dev, hw);
689 struct clk_hw *clk_stm32_composite_register(struct device *dev,
690 const struct stm32_rcc_match_data *data,
693 const struct clock_config *cfg)
695 struct clk_stm32_composite *composite = cfg->clock_cfg;
696 struct clk_hw *hw = &composite->hw;
699 composite->base = base;
700 composite->lock = lock;
701 composite->clock_data = data->clock_data;
703 err = devm_clk_hw_register(dev, hw);