blob: ff3380ea38619cece4498276f7eb7ebdfc784f6c [file] [log] [blame]
Tero Kristof60b1ea2013-06-18 18:55:59 +03001/*
2 * OMAP gate clock support
3 *
4 * Copyright (C) 2013 Texas Instruments, Inc.
5 *
6 * Tero Kristo <t-kristo@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <linux/clk-provider.h>
19#include <linux/slab.h>
20#include <linux/io.h>
21#include <linux/of.h>
22#include <linux/of_address.h>
23#include <linux/clk/ti.h>
24
Tero Kristof1876162014-12-16 18:20:48 +020025#include "clock.h"
26
Tero Kristof60b1ea2013-06-18 18:55:59 +030027#define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
28
29#undef pr_fmt
30#define pr_fmt(fmt) "%s: " fmt, __func__
31
32static int omap36xx_gate_clk_enable_with_hsdiv_restore(struct clk_hw *clk);
33
34static const struct clk_ops omap_gate_clkdm_clk_ops = {
35 .init = &omap2_init_clk_clkdm,
36 .enable = &omap2_clkops_enable_clkdm,
37 .disable = &omap2_clkops_disable_clkdm,
38};
39
40static const struct clk_ops omap_gate_clk_ops = {
41 .init = &omap2_init_clk_clkdm,
42 .enable = &omap2_dflt_clk_enable,
43 .disable = &omap2_dflt_clk_disable,
44 .is_enabled = &omap2_dflt_clk_is_enabled,
45};
46
47static const struct clk_ops omap_gate_clk_hsdiv_restore_ops = {
48 .init = &omap2_init_clk_clkdm,
49 .enable = &omap36xx_gate_clk_enable_with_hsdiv_restore,
50 .disable = &omap2_dflt_clk_disable,
51 .is_enabled = &omap2_dflt_clk_is_enabled,
52};
53
54/**
55 * omap36xx_gate_clk_enable_with_hsdiv_restore - enable clocks suffering
56 * from HSDivider PWRDN problem Implements Errata ID: i556.
57 * @clk: DPLL output struct clk
58 *
59 * 3630 only: dpll3_m3_ck, dpll4_m2_ck, dpll4_m3_ck, dpll4_m4_ck,
60 * dpll4_m5_ck & dpll4_m6_ck dividers gets loaded with reset
61 * valueafter their respective PWRDN bits are set. Any dummy write
62 * (Any other value different from the Read value) to the
63 * corresponding CM_CLKSEL register will refresh the dividers.
64 */
65static int omap36xx_gate_clk_enable_with_hsdiv_restore(struct clk_hw *clk)
66{
67 struct clk_divider *parent;
68 struct clk_hw *parent_hw;
69 u32 dummy_v, orig_v;
70 int ret;
71
72 /* Clear PWRDN bit of HSDIVIDER */
73 ret = omap2_dflt_clk_enable(clk);
74
75 /* Parent is the x2 node, get parent of parent for the m2 div */
76 parent_hw = __clk_get_hw(__clk_get_parent(__clk_get_parent(clk->clk)));
77 parent = to_clk_divider(parent_hw);
78
79 /* Restore the dividers */
80 if (!ret) {
81 orig_v = ti_clk_ll_ops->clk_readl(parent->reg);
82 dummy_v = orig_v;
83
84 /* Write any other value different from the Read value */
85 dummy_v ^= (1 << parent->shift);
86 ti_clk_ll_ops->clk_writel(dummy_v, parent->reg);
87
88 /* Write the original divider */
89 ti_clk_ll_ops->clk_writel(orig_v, parent->reg);
90 }
91
92 return ret;
93}
94
Tero Kristof1876162014-12-16 18:20:48 +020095static struct clk *_register_gate(struct device *dev, const char *name,
96 const char *parent_name, unsigned long flags,
97 void __iomem *reg, u8 bit_idx,
98 u8 clk_gate_flags, const struct clk_ops *ops,
99 const struct clk_hw_omap_ops *hw_ops)
100{
101 struct clk_init_data init = { NULL };
102 struct clk_hw_omap *clk_hw;
103 struct clk *clk;
104
105 clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
106 if (!clk_hw)
107 return ERR_PTR(-ENOMEM);
108
109 clk_hw->hw.init = &init;
110
111 init.name = name;
112 init.ops = ops;
113
114 clk_hw->enable_reg = reg;
115 clk_hw->enable_bit = bit_idx;
116 clk_hw->ops = hw_ops;
117
118 clk_hw->flags = MEMMAP_ADDRESSING | clk_gate_flags;
119
120 init.parent_names = &parent_name;
121 init.num_parents = 1;
122
123 init.flags = flags;
124
125 clk = clk_register(NULL, &clk_hw->hw);
126
127 if (IS_ERR(clk))
128 kfree(clk_hw);
129
130 return clk;
131}
132
133struct clk *ti_clk_register_gate(struct ti_clk *setup)
134{
135 const struct clk_ops *ops = &omap_gate_clk_ops;
136 const struct clk_hw_omap_ops *hw_ops = NULL;
137 u32 reg;
138 struct clk_omap_reg *reg_setup;
139 u32 flags = 0;
140 u8 clk_gate_flags = 0;
141 struct ti_clk_gate *gate;
142
143 gate = setup->data;
144
145 reg_setup = (struct clk_omap_reg *)&reg;
146
147 if (gate->flags & CLKF_SET_RATE_PARENT)
148 flags |= CLK_SET_RATE_PARENT;
149
150 if (gate->flags & CLKF_SET_BIT_TO_DISABLE)
151 clk_gate_flags |= INVERT_ENABLE;
152
153 if (gate->flags & CLKF_HSDIV) {
154 ops = &omap_gate_clk_hsdiv_restore_ops;
155 hw_ops = &clkhwops_wait;
156 }
157
158 if (gate->flags & CLKF_DSS)
159 hw_ops = &clkhwops_omap3430es2_dss_usbhost_wait;
160
161 if (gate->flags & CLKF_WAIT)
162 hw_ops = &clkhwops_wait;
163
164 if (gate->flags & CLKF_CLKDM)
165 ops = &omap_gate_clkdm_clk_ops;
166
167 if (gate->flags & CLKF_AM35XX)
168 hw_ops = &clkhwops_am35xx_ipss_module_wait;
169
170 reg_setup->index = gate->module;
171 reg_setup->offset = gate->reg;
172
173 return _register_gate(NULL, setup->name, gate->parent, flags,
174 (void __iomem *)reg, gate->bit_shift,
175 clk_gate_flags, ops, hw_ops);
176}
177
178struct clk_hw *ti_clk_build_component_gate(struct ti_clk_gate *setup)
179{
180 struct clk_hw_omap *gate;
181 struct clk_omap_reg *reg;
182 const struct clk_hw_omap_ops *ops = &clkhwops_wait;
183
184 if (!setup)
185 return NULL;
186
187 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
188 if (!gate)
189 return ERR_PTR(-ENOMEM);
190
191 reg = (struct clk_omap_reg *)&gate->enable_reg;
192 reg->index = setup->module;
193 reg->offset = setup->reg;
194
195 gate->enable_bit = setup->bit_shift;
196
197 if (setup->flags & CLKF_NO_WAIT)
198 ops = NULL;
199
200 if (setup->flags & CLKF_INTERFACE)
201 ops = &clkhwops_iclk_wait;
202
203 gate->ops = ops;
204 gate->flags = MEMMAP_ADDRESSING;
205
206 return &gate->hw;
207}
208
Tero Kristof60b1ea2013-06-18 18:55:59 +0300209static void __init _of_ti_gate_clk_setup(struct device_node *node,
210 const struct clk_ops *ops,
211 const struct clk_hw_omap_ops *hw_ops)
212{
213 struct clk *clk;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300214 const char *parent_name;
Tero Kristof1876162014-12-16 18:20:48 +0200215 void __iomem *reg = NULL;
216 u8 enable_bit = 0;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300217 u32 val;
Tero Kristof1876162014-12-16 18:20:48 +0200218 u32 flags = 0;
219 u8 clk_gate_flags = 0;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300220
221 if (ops != &omap_gate_clkdm_clk_ops) {
Tero Kristof1876162014-12-16 18:20:48 +0200222 reg = ti_clk_get_reg_addr(node, 0);
223 if (!reg)
224 return;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300225
226 if (!of_property_read_u32(node, "ti,bit-shift", &val))
Tero Kristof1876162014-12-16 18:20:48 +0200227 enable_bit = val;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300228 }
229
Tero Kristof60b1ea2013-06-18 18:55:59 +0300230 if (of_clk_get_parent_count(node) != 1) {
Tero Kristof1876162014-12-16 18:20:48 +0200231 pr_err("%s must have 1 parent\n", node->name);
232 return;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300233 }
234
235 parent_name = of_clk_get_parent_name(node, 0);
Tero Kristof60b1ea2013-06-18 18:55:59 +0300236
237 if (of_property_read_bool(node, "ti,set-rate-parent"))
Tero Kristof1876162014-12-16 18:20:48 +0200238 flags |= CLK_SET_RATE_PARENT;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300239
240 if (of_property_read_bool(node, "ti,set-bit-to-disable"))
Tero Kristof1876162014-12-16 18:20:48 +0200241 clk_gate_flags |= INVERT_ENABLE;
Tero Kristof60b1ea2013-06-18 18:55:59 +0300242
Tero Kristof1876162014-12-16 18:20:48 +0200243 clk = _register_gate(NULL, node->name, parent_name, flags, reg,
244 enable_bit, clk_gate_flags, ops, hw_ops);
Tero Kristof60b1ea2013-06-18 18:55:59 +0300245
Tero Kristof1876162014-12-16 18:20:48 +0200246 if (!IS_ERR(clk))
Tero Kristof60b1ea2013-06-18 18:55:59 +0300247 of_clk_add_provider(node, of_clk_src_simple_get, clk);
Tero Kristof60b1ea2013-06-18 18:55:59 +0300248}
249
250static void __init
251_of_ti_composite_gate_clk_setup(struct device_node *node,
252 const struct clk_hw_omap_ops *hw_ops)
253{
254 struct clk_hw_omap *gate;
255 u32 val = 0;
256
257 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
258 if (!gate)
259 return;
260
261 gate->enable_reg = ti_clk_get_reg_addr(node, 0);
262 if (!gate->enable_reg)
263 goto cleanup;
264
265 of_property_read_u32(node, "ti,bit-shift", &val);
266
267 gate->enable_bit = val;
268 gate->ops = hw_ops;
269 gate->flags = MEMMAP_ADDRESSING;
270
271 if (!ti_clk_add_component(node, &gate->hw, CLK_COMPONENT_TYPE_GATE))
272 return;
273
274cleanup:
275 kfree(gate);
276}
277
278static void __init
279of_ti_composite_no_wait_gate_clk_setup(struct device_node *node)
280{
281 _of_ti_composite_gate_clk_setup(node, NULL);
282}
283CLK_OF_DECLARE(ti_composite_no_wait_gate_clk, "ti,composite-no-wait-gate-clock",
284 of_ti_composite_no_wait_gate_clk_setup);
285
Tero Kristob3654d72014-03-05 10:03:38 +0200286#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tero Kristof60b1ea2013-06-18 18:55:59 +0300287static void __init of_ti_composite_interface_clk_setup(struct device_node *node)
288{
289 _of_ti_composite_gate_clk_setup(node, &clkhwops_iclk_wait);
290}
291CLK_OF_DECLARE(ti_composite_interface_clk, "ti,composite-interface-clock",
292 of_ti_composite_interface_clk_setup);
293#endif
294
295static void __init of_ti_composite_gate_clk_setup(struct device_node *node)
296{
297 _of_ti_composite_gate_clk_setup(node, &clkhwops_wait);
298}
299CLK_OF_DECLARE(ti_composite_gate_clk, "ti,composite-gate-clock",
300 of_ti_composite_gate_clk_setup);
301
302
303static void __init of_ti_clkdm_gate_clk_setup(struct device_node *node)
304{
305 _of_ti_gate_clk_setup(node, &omap_gate_clkdm_clk_ops, NULL);
306}
307CLK_OF_DECLARE(ti_clkdm_gate_clk, "ti,clkdm-gate-clock",
308 of_ti_clkdm_gate_clk_setup);
309
310static void __init of_ti_hsdiv_gate_clk_setup(struct device_node *node)
311{
312 _of_ti_gate_clk_setup(node, &omap_gate_clk_hsdiv_restore_ops,
313 &clkhwops_wait);
314}
315CLK_OF_DECLARE(ti_hsdiv_gate_clk, "ti,hsdiv-gate-clock",
316 of_ti_hsdiv_gate_clk_setup);
317
318static void __init of_ti_gate_clk_setup(struct device_node *node)
319{
320 _of_ti_gate_clk_setup(node, &omap_gate_clk_ops, NULL);
321}
Rob Herring826d8952014-05-12 11:41:19 -0500322CLK_OF_DECLARE(ti_gate_clk, "ti,gate-clock", of_ti_gate_clk_setup);
Tero Kristof60b1ea2013-06-18 18:55:59 +0300323
324static void __init of_ti_wait_gate_clk_setup(struct device_node *node)
325{
326 _of_ti_gate_clk_setup(node, &omap_gate_clk_ops, &clkhwops_wait);
327}
328CLK_OF_DECLARE(ti_wait_gate_clk, "ti,wait-gate-clock",
329 of_ti_wait_gate_clk_setup);
330
331#ifdef CONFIG_ARCH_OMAP3
332static void __init of_ti_am35xx_gate_clk_setup(struct device_node *node)
333{
334 _of_ti_gate_clk_setup(node, &omap_gate_clk_ops,
335 &clkhwops_am35xx_ipss_module_wait);
336}
337CLK_OF_DECLARE(ti_am35xx_gate_clk, "ti,am35xx-gate-clock",
338 of_ti_am35xx_gate_clk_setup);
339
340static void __init of_ti_dss_gate_clk_setup(struct device_node *node)
341{
342 _of_ti_gate_clk_setup(node, &omap_gate_clk_ops,
343 &clkhwops_omap3430es2_dss_usbhost_wait);
344}
345CLK_OF_DECLARE(ti_dss_gate_clk, "ti,dss-gate-clock",
346 of_ti_dss_gate_clk_setup);
347#endif