blob: a9febd6356b826cf3b853f2426acc82d6a2562c7 [file] [log] [blame]
Thomas Gleixner5a729242022-06-07 16:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Tero Kristof60b1ea2013-06-18 18:55:59 +03002/*
3 * OMAP gate clock support
4 *
5 * Copyright (C) 2013 Texas Instruments, Inc.
6 *
7 * Tero Kristo <t-kristo@ti.com>
Tero Kristof60b1ea2013-06-18 18:55:59 +03008 */
9
10#include <linux/clk-provider.h>
11#include <linux/slab.h>
12#include <linux/io.h>
13#include <linux/of.h>
14#include <linux/of_address.h>
15#include <linux/clk/ti.h>
16
Tero Kristof1876162014-12-16 18:20:48 +020017#include "clock.h"
18
Tero Kristof60b1ea2013-06-18 18:55:59 +030019#undef pr_fmt
20#define pr_fmt(fmt) "%s: " fmt, __func__
21
22static int omap36xx_gate_clk_enable_with_hsdiv_restore(struct clk_hw *clk);
23
24static const struct clk_ops omap_gate_clkdm_clk_ops = {
25 .init = &omap2_init_clk_clkdm,
26 .enable = &omap2_clkops_enable_clkdm,
27 .disable = &omap2_clkops_disable_clkdm,
Russ Dilld6e7bbc2018-09-04 12:19:37 +053028 .restore_context = clk_gate_restore_context,
Tero Kristof60b1ea2013-06-18 18:55:59 +030029};
30
Tero Kristo9a00fa62017-02-09 11:10:19 +020031const struct clk_ops omap_gate_clk_ops = {
Tero Kristof60b1ea2013-06-18 18:55:59 +030032 .init = &omap2_init_clk_clkdm,
33 .enable = &omap2_dflt_clk_enable,
34 .disable = &omap2_dflt_clk_disable,
35 .is_enabled = &omap2_dflt_clk_is_enabled,
Russ Dilld6e7bbc2018-09-04 12:19:37 +053036 .restore_context = clk_gate_restore_context,
Tero Kristof60b1ea2013-06-18 18:55:59 +030037};
38
39static const struct clk_ops omap_gate_clk_hsdiv_restore_ops = {
40 .init = &omap2_init_clk_clkdm,
41 .enable = &omap36xx_gate_clk_enable_with_hsdiv_restore,
42 .disable = &omap2_dflt_clk_disable,
43 .is_enabled = &omap2_dflt_clk_is_enabled,
Russ Dilld6e7bbc2018-09-04 12:19:37 +053044 .restore_context = clk_gate_restore_context,
Tero Kristof60b1ea2013-06-18 18:55:59 +030045};
46
47/**
48 * omap36xx_gate_clk_enable_with_hsdiv_restore - enable clocks suffering
49 * from HSDivider PWRDN problem Implements Errata ID: i556.
Lee Jonesd52848c2021-01-20 09:30:36 +000050 * @hw: DPLL output struct clk_hw
Tero Kristof60b1ea2013-06-18 18:55:59 +030051 *
52 * 3630 only: dpll3_m3_ck, dpll4_m2_ck, dpll4_m3_ck, dpll4_m4_ck,
53 * dpll4_m5_ck & dpll4_m6_ck dividers gets loaded with reset
54 * valueafter their respective PWRDN bits are set. Any dummy write
55 * (Any other value different from the Read value) to the
56 * corresponding CM_CLKSEL register will refresh the dividers.
57 */
Stephen Boyda53ad8e2015-07-30 17:20:57 -070058static int omap36xx_gate_clk_enable_with_hsdiv_restore(struct clk_hw *hw)
Tero Kristof60b1ea2013-06-18 18:55:59 +030059{
Tero Kristo6dbde942017-02-09 14:45:45 +020060 struct clk_omap_divider *parent;
Tero Kristof60b1ea2013-06-18 18:55:59 +030061 struct clk_hw *parent_hw;
62 u32 dummy_v, orig_v;
63 int ret;
64
65 /* Clear PWRDN bit of HSDIVIDER */
Stephen Boyda53ad8e2015-07-30 17:20:57 -070066 ret = omap2_dflt_clk_enable(hw);
Tero Kristof60b1ea2013-06-18 18:55:59 +030067
68 /* Parent is the x2 node, get parent of parent for the m2 div */
Stephen Boyda53ad8e2015-07-30 17:20:57 -070069 parent_hw = clk_hw_get_parent(clk_hw_get_parent(hw));
Tero Kristo6dbde942017-02-09 14:45:45 +020070 parent = to_clk_omap_divider(parent_hw);
Tero Kristof60b1ea2013-06-18 18:55:59 +030071
72 /* Restore the dividers */
73 if (!ret) {
Tero Kristo6c0afb52017-02-09 11:24:37 +020074 orig_v = ti_clk_ll_ops->clk_readl(&parent->reg);
Tero Kristof60b1ea2013-06-18 18:55:59 +030075 dummy_v = orig_v;
76
77 /* Write any other value different from the Read value */
78 dummy_v ^= (1 << parent->shift);
Tero Kristo6c0afb52017-02-09 11:24:37 +020079 ti_clk_ll_ops->clk_writel(dummy_v, &parent->reg);
Tero Kristof60b1ea2013-06-18 18:55:59 +030080
81 /* Write the original divider */
Tero Kristo6c0afb52017-02-09 11:24:37 +020082 ti_clk_ll_ops->clk_writel(orig_v, &parent->reg);
Tero Kristof60b1ea2013-06-18 18:55:59 +030083 }
84
85 return ret;
86}
87
Dario Binacchi3400d542022-11-13 19:11:46 +010088static struct clk *_register_gate(struct device_node *node, const char *name,
Tero Kristof1876162014-12-16 18:20:48 +020089 const char *parent_name, unsigned long flags,
Tero Kristo6c0afb52017-02-09 11:24:37 +020090 struct clk_omap_reg *reg, u8 bit_idx,
Tero Kristof1876162014-12-16 18:20:48 +020091 u8 clk_gate_flags, const struct clk_ops *ops,
92 const struct clk_hw_omap_ops *hw_ops)
93{
94 struct clk_init_data init = { NULL };
95 struct clk_hw_omap *clk_hw;
96 struct clk *clk;
97
98 clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
99 if (!clk_hw)
100 return ERR_PTR(-ENOMEM);
101
102 clk_hw->hw.init = &init;
103
104 init.name = name;
105 init.ops = ops;
106
Tero Kristo6c0afb52017-02-09 11:24:37 +0200107 memcpy(&clk_hw->enable_reg, reg, sizeof(*reg));
Tero Kristof1876162014-12-16 18:20:48 +0200108 clk_hw->enable_bit = bit_idx;
109 clk_hw->ops = hw_ops;
110
Tero Kristoc91f0782017-01-30 16:01:36 +0200111 clk_hw->flags = clk_gate_flags;
Tero Kristof1876162014-12-16 18:20:48 +0200112
113 init.parent_names = &parent_name;
114 init.num_parents = 1;
115
116 init.flags = flags;
117
Dario Binacchi3400d542022-11-13 19:11:46 +0100118 clk = of_ti_clk_register_omap_hw(node, &clk_hw->hw, name);
Tero Kristof1876162014-12-16 18:20:48 +0200119
120 if (IS_ERR(clk))
121 kfree(clk_hw);
122
123 return clk;
124}
125
Tero Kristof60b1ea2013-06-18 18:55:59 +0300126static void __init _of_ti_gate_clk_setup(struct device_node *node,
127 const struct clk_ops *ops,
128 const struct clk_hw_omap_ops *hw_ops)
129{
130 struct clk *clk;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300131 const char *parent_name;
Tero Kristo6c0afb52017-02-09 11:24:37 +0200132 struct clk_omap_reg reg;
Tony Lindgrened060992022-02-04 09:14:49 +0200133 const char *name;
Tero Kristof1876162014-12-16 18:20:48 +0200134 u8 enable_bit = 0;
Tero Kristof1876162014-12-16 18:20:48 +0200135 u32 flags = 0;
136 u8 clk_gate_flags = 0;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300137
138 if (ops != &omap_gate_clkdm_clk_ops) {
Tero Kristo6c0afb52017-02-09 11:24:37 +0200139 if (ti_clk_get_reg_addr(node, 0, &reg))
Tero Kristof1876162014-12-16 18:20:48 +0200140 return;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300141
Tony Lindgren4a5917c2024-02-13 12:48:52 +0200142 enable_bit = reg.bit;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300143 }
144
Tero Kristof60b1ea2013-06-18 18:55:59 +0300145 if (of_clk_get_parent_count(node) != 1) {
Rob Herringe665f022018-08-28 10:44:29 -0500146 pr_err("%pOFn must have 1 parent\n", node);
Tero Kristof1876162014-12-16 18:20:48 +0200147 return;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300148 }
149
150 parent_name = of_clk_get_parent_name(node, 0);
Tero Kristof60b1ea2013-06-18 18:55:59 +0300151
152 if (of_property_read_bool(node, "ti,set-rate-parent"))
Tero Kristof1876162014-12-16 18:20:48 +0200153 flags |= CLK_SET_RATE_PARENT;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300154
155 if (of_property_read_bool(node, "ti,set-bit-to-disable"))
Tero Kristof1876162014-12-16 18:20:48 +0200156 clk_gate_flags |= INVERT_ENABLE;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300157
Tony Lindgrened060992022-02-04 09:14:49 +0200158 name = ti_dt_clk_name(node);
Dario Binacchi3400d542022-11-13 19:11:46 +0100159 clk = _register_gate(node, name, parent_name, flags, &reg,
Tero Kristof1876162014-12-16 18:20:48 +0200160 enable_bit, clk_gate_flags, ops, hw_ops);
Tero Kristof60b1ea2013-06-18 18:55:59 +0300161
Tero Kristof1876162014-12-16 18:20:48 +0200162 if (!IS_ERR(clk))
Tero Kristof60b1ea2013-06-18 18:55:59 +0300163 of_clk_add_provider(node, of_clk_src_simple_get, clk);
Tero Kristof60b1ea2013-06-18 18:55:59 +0300164}
165
166static void __init
167_of_ti_composite_gate_clk_setup(struct device_node *node,
168 const struct clk_hw_omap_ops *hw_ops)
169{
170 struct clk_hw_omap *gate;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300171
172 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
173 if (!gate)
174 return;
175
Tero Kristo6c0afb52017-02-09 11:24:37 +0200176 if (ti_clk_get_reg_addr(node, 0, &gate->enable_reg))
Tero Kristof60b1ea2013-06-18 18:55:59 +0300177 goto cleanup;
178
Tony Lindgren4a5917c2024-02-13 12:48:52 +0200179 gate->enable_bit = gate->enable_reg.bit;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300180 gate->ops = hw_ops;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300181
182 if (!ti_clk_add_component(node, &gate->hw, CLK_COMPONENT_TYPE_GATE))
183 return;
184
185cleanup:
186 kfree(gate);
187}
188
189static void __init
190of_ti_composite_no_wait_gate_clk_setup(struct device_node *node)
191{
192 _of_ti_composite_gate_clk_setup(node, NULL);
193}
194CLK_OF_DECLARE(ti_composite_no_wait_gate_clk, "ti,composite-no-wait-gate-clock",
195 of_ti_composite_no_wait_gate_clk_setup);
196
Tero Kristob3654d72014-03-05 10:03:38 +0200197#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tero Kristof60b1ea2013-06-18 18:55:59 +0300198static void __init of_ti_composite_interface_clk_setup(struct device_node *node)
199{
200 _of_ti_composite_gate_clk_setup(node, &clkhwops_iclk_wait);
201}
202CLK_OF_DECLARE(ti_composite_interface_clk, "ti,composite-interface-clock",
203 of_ti_composite_interface_clk_setup);
204#endif
205
206static void __init of_ti_composite_gate_clk_setup(struct device_node *node)
207{
208 _of_ti_composite_gate_clk_setup(node, &clkhwops_wait);
209}
210CLK_OF_DECLARE(ti_composite_gate_clk, "ti,composite-gate-clock",
211 of_ti_composite_gate_clk_setup);
212
213
214static void __init of_ti_clkdm_gate_clk_setup(struct device_node *node)
215{
216 _of_ti_gate_clk_setup(node, &omap_gate_clkdm_clk_ops, NULL);
217}
218CLK_OF_DECLARE(ti_clkdm_gate_clk, "ti,clkdm-gate-clock",
219 of_ti_clkdm_gate_clk_setup);
220
221static void __init of_ti_hsdiv_gate_clk_setup(struct device_node *node)
222{
223 _of_ti_gate_clk_setup(node, &omap_gate_clk_hsdiv_restore_ops,
224 &clkhwops_wait);
225}
226CLK_OF_DECLARE(ti_hsdiv_gate_clk, "ti,hsdiv-gate-clock",
227 of_ti_hsdiv_gate_clk_setup);
228
229static void __init of_ti_gate_clk_setup(struct device_node *node)
230{
231 _of_ti_gate_clk_setup(node, &omap_gate_clk_ops, NULL);
232}
Rob Herring826d8952014-05-12 11:41:19 -0500233CLK_OF_DECLARE(ti_gate_clk, "ti,gate-clock", of_ti_gate_clk_setup);
Tero Kristof60b1ea2013-06-18 18:55:59 +0300234
235static void __init of_ti_wait_gate_clk_setup(struct device_node *node)
236{
237 _of_ti_gate_clk_setup(node, &omap_gate_clk_ops, &clkhwops_wait);
238}
239CLK_OF_DECLARE(ti_wait_gate_clk, "ti,wait-gate-clock",
240 of_ti_wait_gate_clk_setup);
241
242#ifdef CONFIG_ARCH_OMAP3
243static void __init of_ti_am35xx_gate_clk_setup(struct device_node *node)
244{
245 _of_ti_gate_clk_setup(node, &omap_gate_clk_ops,
246 &clkhwops_am35xx_ipss_module_wait);
247}
248CLK_OF_DECLARE(ti_am35xx_gate_clk, "ti,am35xx-gate-clock",
249 of_ti_am35xx_gate_clk_setup);
250
251static void __init of_ti_dss_gate_clk_setup(struct device_node *node)
252{
253 _of_ti_gate_clk_setup(node, &omap_gate_clk_ops,
254 &clkhwops_omap3430es2_dss_usbhost_wait);
255}
256CLK_OF_DECLARE(ti_dss_gate_clk, "ti,dss-gate-clock",
257 of_ti_dss_gate_clk_setup);
258#endif