Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2016 Maxime Ripard |
| 4 | * |
| 5 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Chen-Yu Tsai | 02ae2bc | 2017-04-13 10:13:52 +0800 | [diff] [blame] | 8 | #include <linux/clk.h> |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 9 | #include <linux/clk-provider.h> |
Samuel Holland | 9bec2b9 | 2021-09-01 00:05:19 -0500 | [diff] [blame] | 10 | #include <linux/device.h> |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 11 | #include <linux/iopoll.h> |
Samuel Holland | 91389c3 | 2021-11-18 21:33:37 -0600 | [diff] [blame] | 12 | #include <linux/module.h> |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 13 | #include <linux/slab.h> |
| 14 | |
| 15 | #include "ccu_common.h" |
Chen-Yu Tsai | 02ae2bc | 2017-04-13 10:13:52 +0800 | [diff] [blame] | 16 | #include "ccu_gate.h" |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 17 | #include "ccu_reset.h" |
| 18 | |
Samuel Holland | 9bec2b9 | 2021-09-01 00:05:19 -0500 | [diff] [blame] | 19 | struct sunxi_ccu { |
| 20 | const struct sunxi_ccu_desc *desc; |
Samuel Holland | 8f81632 | 2021-09-01 00:05:21 -0500 | [diff] [blame] | 21 | spinlock_t lock; |
Samuel Holland | 9bec2b9 | 2021-09-01 00:05:19 -0500 | [diff] [blame] | 22 | struct ccu_reset reset; |
| 23 | }; |
| 24 | |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 25 | void ccu_helper_wait_for_lock(struct ccu_common *common, u32 lock) |
| 26 | { |
Chen-Yu Tsai | 3de64bf | 2017-01-28 20:22:33 +0800 | [diff] [blame] | 27 | void __iomem *addr; |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 28 | u32 reg; |
| 29 | |
| 30 | if (!lock) |
| 31 | return; |
| 32 | |
Chen-Yu Tsai | 3de64bf | 2017-01-28 20:22:33 +0800 | [diff] [blame] | 33 | if (common->features & CCU_FEATURE_LOCK_REG) |
| 34 | addr = common->base + common->lock_reg; |
| 35 | else |
| 36 | addr = common->base + common->reg; |
| 37 | |
| 38 | WARN_ON(readl_relaxed_poll_timeout(addr, reg, reg & lock, 100, 70000)); |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 39 | } |
Samuel Holland | 551b62b | 2021-11-18 21:33:34 -0600 | [diff] [blame] | 40 | EXPORT_SYMBOL_NS_GPL(ccu_helper_wait_for_lock, SUNXI_CCU); |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 41 | |
Chen-Yu Tsai | 02ae2bc | 2017-04-13 10:13:52 +0800 | [diff] [blame] | 42 | /* |
| 43 | * This clock notifier is called when the frequency of a PLL clock is |
| 44 | * changed. In common PLL designs, changes to the dividers take effect |
| 45 | * almost immediately, while changes to the multipliers (implemented |
| 46 | * as dividers in the feedback loop) take a few cycles to work into |
| 47 | * the feedback loop for the PLL to stablize. |
| 48 | * |
| 49 | * Sometimes when the PLL clock rate is changed, the decrease in the |
| 50 | * divider is too much for the decrease in the multiplier to catch up. |
| 51 | * The PLL clock rate will spike, and in some cases, might lock up |
| 52 | * completely. |
| 53 | * |
| 54 | * This notifier callback will gate and then ungate the clock, |
| 55 | * effectively resetting it, so it proceeds to work. Care must be |
| 56 | * taken to reparent consumers to other temporary clocks during the |
| 57 | * rate change, and that this notifier callback must be the first |
| 58 | * to be registered. |
| 59 | */ |
| 60 | static int ccu_pll_notifier_cb(struct notifier_block *nb, |
| 61 | unsigned long event, void *data) |
| 62 | { |
| 63 | struct ccu_pll_nb *pll = to_ccu_pll_nb(nb); |
| 64 | int ret = 0; |
| 65 | |
| 66 | if (event != POST_RATE_CHANGE) |
| 67 | goto out; |
| 68 | |
| 69 | ccu_gate_helper_disable(pll->common, pll->enable); |
| 70 | |
| 71 | ret = ccu_gate_helper_enable(pll->common, pll->enable); |
| 72 | if (ret) |
| 73 | goto out; |
| 74 | |
| 75 | ccu_helper_wait_for_lock(pll->common, pll->lock); |
| 76 | |
| 77 | out: |
| 78 | return notifier_from_errno(ret); |
| 79 | } |
| 80 | |
| 81 | int ccu_pll_notifier_register(struct ccu_pll_nb *pll_nb) |
| 82 | { |
| 83 | pll_nb->clk_nb.notifier_call = ccu_pll_notifier_cb; |
| 84 | |
| 85 | return clk_notifier_register(pll_nb->common->hw.clk, |
| 86 | &pll_nb->clk_nb); |
| 87 | } |
Samuel Holland | 551b62b | 2021-11-18 21:33:34 -0600 | [diff] [blame] | 88 | EXPORT_SYMBOL_NS_GPL(ccu_pll_notifier_register, SUNXI_CCU); |
Chen-Yu Tsai | 02ae2bc | 2017-04-13 10:13:52 +0800 | [diff] [blame] | 89 | |
Samuel Holland | 9bec2b9 | 2021-09-01 00:05:19 -0500 | [diff] [blame] | 90 | static int sunxi_ccu_probe(struct sunxi_ccu *ccu, struct device *dev, |
| 91 | struct device_node *node, void __iomem *reg, |
| 92 | const struct sunxi_ccu_desc *desc) |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 93 | { |
| 94 | struct ccu_reset *reset; |
| 95 | int i, ret; |
| 96 | |
Samuel Holland | 9bec2b9 | 2021-09-01 00:05:19 -0500 | [diff] [blame] | 97 | ccu->desc = desc; |
| 98 | |
Samuel Holland | 8f81632 | 2021-09-01 00:05:21 -0500 | [diff] [blame] | 99 | spin_lock_init(&ccu->lock); |
| 100 | |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 101 | for (i = 0; i < desc->num_ccu_clks; i++) { |
| 102 | struct ccu_common *cclk = desc->ccu_clks[i]; |
| 103 | |
| 104 | if (!cclk) |
| 105 | continue; |
| 106 | |
| 107 | cclk->base = reg; |
Samuel Holland | 8f81632 | 2021-09-01 00:05:21 -0500 | [diff] [blame] | 108 | cclk->lock = &ccu->lock; |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | for (i = 0; i < desc->hw_clks->num ; i++) { |
| 112 | struct clk_hw *hw = desc->hw_clks->hws[i]; |
Stephen Boyd | a7b85ad | 2019-08-14 21:10:37 -0700 | [diff] [blame] | 113 | const char *name; |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 114 | |
| 115 | if (!hw) |
| 116 | continue; |
| 117 | |
Stephen Boyd | a7b85ad | 2019-08-14 21:10:37 -0700 | [diff] [blame] | 118 | name = hw->init->name; |
Samuel Holland | 9bec2b9 | 2021-09-01 00:05:19 -0500 | [diff] [blame] | 119 | if (dev) |
| 120 | ret = clk_hw_register(dev, hw); |
| 121 | else |
| 122 | ret = of_clk_hw_register(node, hw); |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 123 | if (ret) { |
Stephen Boyd | a7b85ad | 2019-08-14 21:10:37 -0700 | [diff] [blame] | 124 | pr_err("Couldn't register clock %d - %s\n", i, name); |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 125 | goto err_clk_unreg; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, |
| 130 | desc->hw_clks); |
| 131 | if (ret) |
| 132 | goto err_clk_unreg; |
| 133 | |
Samuel Holland | 9bec2b9 | 2021-09-01 00:05:19 -0500 | [diff] [blame] | 134 | reset = &ccu->reset; |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 135 | reset->rcdev.of_node = node; |
| 136 | reset->rcdev.ops = &ccu_reset_ops; |
Samuel Holland | 9bec2b9 | 2021-09-01 00:05:19 -0500 | [diff] [blame] | 137 | reset->rcdev.owner = dev ? dev->driver->owner : THIS_MODULE; |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 138 | reset->rcdev.nr_resets = desc->num_resets; |
| 139 | reset->base = reg; |
Samuel Holland | 8f81632 | 2021-09-01 00:05:21 -0500 | [diff] [blame] | 140 | reset->lock = &ccu->lock; |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 141 | reset->reset_map = desc->resets; |
| 142 | |
| 143 | ret = reset_controller_register(&reset->rcdev); |
| 144 | if (ret) |
Samuel Holland | 9bec2b9 | 2021-09-01 00:05:19 -0500 | [diff] [blame] | 145 | goto err_del_provider; |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 146 | |
| 147 | return 0; |
| 148 | |
Samuel Holland | 9bec2b9 | 2021-09-01 00:05:19 -0500 | [diff] [blame] | 149 | err_del_provider: |
Stephen Boyd | 5d806f9 | 2017-02-03 12:43:11 -0800 | [diff] [blame] | 150 | of_clk_del_provider(node); |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 151 | err_clk_unreg: |
Stephen Boyd | 5d806f9 | 2017-02-03 12:43:11 -0800 | [diff] [blame] | 152 | while (--i >= 0) { |
| 153 | struct clk_hw *hw = desc->hw_clks->hws[i]; |
| 154 | |
| 155 | if (!hw) |
| 156 | continue; |
| 157 | clk_hw_unregister(hw); |
| 158 | } |
Maxime Ripard | 1d80c14 | 2016-06-29 21:05:23 +0200 | [diff] [blame] | 159 | return ret; |
| 160 | } |
Samuel Holland | 9bec2b9 | 2021-09-01 00:05:19 -0500 | [diff] [blame] | 161 | |
| 162 | static void devm_sunxi_ccu_release(struct device *dev, void *res) |
| 163 | { |
| 164 | struct sunxi_ccu *ccu = res; |
| 165 | const struct sunxi_ccu_desc *desc = ccu->desc; |
| 166 | int i; |
| 167 | |
| 168 | reset_controller_unregister(&ccu->reset.rcdev); |
| 169 | of_clk_del_provider(dev->of_node); |
| 170 | |
| 171 | for (i = 0; i < desc->hw_clks->num; i++) { |
| 172 | struct clk_hw *hw = desc->hw_clks->hws[i]; |
| 173 | |
| 174 | if (!hw) |
| 175 | continue; |
| 176 | clk_hw_unregister(hw); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | int devm_sunxi_ccu_probe(struct device *dev, void __iomem *reg, |
| 181 | const struct sunxi_ccu_desc *desc) |
| 182 | { |
| 183 | struct sunxi_ccu *ccu; |
| 184 | int ret; |
| 185 | |
| 186 | ccu = devres_alloc(devm_sunxi_ccu_release, sizeof(*ccu), GFP_KERNEL); |
| 187 | if (!ccu) |
| 188 | return -ENOMEM; |
| 189 | |
| 190 | ret = sunxi_ccu_probe(ccu, dev, dev->of_node, reg, desc); |
| 191 | if (ret) { |
| 192 | devres_free(ccu); |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | devres_add(dev, ccu); |
| 197 | |
| 198 | return 0; |
| 199 | } |
Samuel Holland | 551b62b | 2021-11-18 21:33:34 -0600 | [diff] [blame] | 200 | EXPORT_SYMBOL_NS_GPL(devm_sunxi_ccu_probe, SUNXI_CCU); |
Samuel Holland | 9bec2b9 | 2021-09-01 00:05:19 -0500 | [diff] [blame] | 201 | |
| 202 | void of_sunxi_ccu_probe(struct device_node *node, void __iomem *reg, |
| 203 | const struct sunxi_ccu_desc *desc) |
| 204 | { |
| 205 | struct sunxi_ccu *ccu; |
| 206 | int ret; |
| 207 | |
| 208 | ccu = kzalloc(sizeof(*ccu), GFP_KERNEL); |
| 209 | if (!ccu) |
| 210 | return; |
| 211 | |
| 212 | ret = sunxi_ccu_probe(ccu, NULL, node, reg, desc); |
| 213 | if (ret) { |
| 214 | pr_err("%pOF: probing clocks failed: %d\n", node, ret); |
| 215 | kfree(ccu); |
| 216 | } |
| 217 | } |
Samuel Holland | 91389c3 | 2021-11-18 21:33:37 -0600 | [diff] [blame] | 218 | |
| 219 | MODULE_LICENSE("GPL"); |