2 * OMAP clkctrl clock support
4 * Copyright (C) 2017 Texas Instruments, Inc.
6 * Tero Kristo <t-kristo@ti.com>
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.
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.
18 #include <linux/clk-provider.h>
19 #include <linux/slab.h>
21 #include <linux/of_address.h>
22 #include <linux/clk/ti.h>
23 #include <linux/delay.h>
24 #include <linux/timekeeping.h>
29 #define OMAP4_MODULEMODE_MASK 0x3
31 #define MODULEMODE_HWCTRL 0x1
32 #define MODULEMODE_SWCTRL 0x2
34 #define OMAP4_IDLEST_MASK (0x3 << 16)
35 #define OMAP4_IDLEST_SHIFT 16
37 #define OMAP4_STBYST_MASK BIT(18)
38 #define OMAP4_STBYST_SHIFT 18
40 #define CLKCTRL_IDLEST_FUNCTIONAL 0x0
41 #define CLKCTRL_IDLEST_INTERFACE_IDLE 0x2
42 #define CLKCTRL_IDLEST_DISABLED 0x3
44 /* These timeouts are in us */
45 #define OMAP4_MAX_MODULE_READY_TIME 2000
46 #define OMAP4_MAX_MODULE_DISABLE_TIME 5000
48 static bool _early_timeout = true;
50 struct omap_clkctrl_provider {
52 struct list_head clocks;
56 struct omap_clkctrl_clk {
60 struct list_head node;
68 static const struct omap_clkctrl_data default_clkctrl_data[] __initconst = {
72 static u32 _omap4_idlest(u32 val)
74 val &= OMAP4_IDLEST_MASK;
75 val >>= OMAP4_IDLEST_SHIFT;
80 static bool _omap4_is_idle(u32 val)
82 val = _omap4_idlest(val);
84 return val == CLKCTRL_IDLEST_DISABLED;
87 static bool _omap4_is_ready(u32 val)
89 val = _omap4_idlest(val);
91 return val == CLKCTRL_IDLEST_FUNCTIONAL ||
92 val == CLKCTRL_IDLEST_INTERFACE_IDLE;
95 static bool _omap4_is_timeout(union omap4_timeout *time, u32 timeout)
98 * There are two special cases where ktime_to_ns() can't be
99 * used to track the timeouts. First one is during early boot
100 * when the timers haven't been initialized yet. The second
101 * one is during suspend-resume cycle while timekeeping is
102 * being suspended / resumed. Clocksource for the system
103 * can be from a timer that requires pm_runtime access, which
104 * will eventually bring us here with timekeeping_suspended,
105 * during both suspend entry and resume paths. This happens
106 * at least on am43xx platform. Account for flakeyness
107 * with udelay() by multiplying the timeout value by 2.
109 if (unlikely(_early_timeout || timekeeping_suspended)) {
110 if (time->cycles++ < timeout) {
115 if (!ktime_to_ns(time->start)) {
116 time->start = ktime_get();
120 if (ktime_us_delta(ktime_get(), time->start) < timeout) {
129 static int __init _omap4_disable_early_timeout(void)
131 _early_timeout = false;
135 arch_initcall(_omap4_disable_early_timeout);
137 static int _omap4_clkctrl_clk_enable(struct clk_hw *hw)
139 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
142 union omap4_timeout timeout = { 0 };
145 ret = ti_clk_ll_ops->clkdm_clk_enable(clk->clkdm, hw->clk);
148 "%s: could not enable %s's clockdomain %s: %d\n",
149 __func__, clk_hw_get_name(hw),
150 clk->clkdm_name, ret);
155 if (!clk->enable_bit)
158 val = ti_clk_ll_ops->clk_readl(&clk->enable_reg);
160 val &= ~OMAP4_MODULEMODE_MASK;
161 val |= clk->enable_bit;
163 ti_clk_ll_ops->clk_writel(val, &clk->enable_reg);
165 if (test_bit(NO_IDLEST, &clk->flags))
168 /* Wait until module is enabled */
169 while (!_omap4_is_ready(ti_clk_ll_ops->clk_readl(&clk->enable_reg))) {
170 if (_omap4_is_timeout(&timeout, OMAP4_MAX_MODULE_READY_TIME)) {
171 pr_err("%s: failed to enable\n", clk_hw_get_name(hw));
179 static void _omap4_clkctrl_clk_disable(struct clk_hw *hw)
181 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
183 union omap4_timeout timeout = { 0 };
185 if (!clk->enable_bit)
188 val = ti_clk_ll_ops->clk_readl(&clk->enable_reg);
190 val &= ~OMAP4_MODULEMODE_MASK;
192 ti_clk_ll_ops->clk_writel(val, &clk->enable_reg);
194 if (test_bit(NO_IDLEST, &clk->flags))
197 /* Wait until module is disabled */
198 while (!_omap4_is_idle(ti_clk_ll_ops->clk_readl(&clk->enable_reg))) {
199 if (_omap4_is_timeout(&timeout,
200 OMAP4_MAX_MODULE_DISABLE_TIME)) {
201 pr_err("%s: failed to disable\n", clk_hw_get_name(hw));
208 ti_clk_ll_ops->clkdm_clk_disable(clk->clkdm, hw->clk);
211 static int _omap4_clkctrl_clk_is_enabled(struct clk_hw *hw)
213 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
216 val = ti_clk_ll_ops->clk_readl(&clk->enable_reg);
218 if (val & clk->enable_bit)
224 static const struct clk_ops omap4_clkctrl_clk_ops = {
225 .enable = _omap4_clkctrl_clk_enable,
226 .disable = _omap4_clkctrl_clk_disable,
227 .is_enabled = _omap4_clkctrl_clk_is_enabled,
228 .init = omap2_init_clk_clkdm,
231 static struct clk_hw *_ti_omap4_clkctrl_xlate(struct of_phandle_args *clkspec,
234 struct omap_clkctrl_provider *provider = data;
235 struct omap_clkctrl_clk *entry;
238 if (clkspec->args_count != 2)
239 return ERR_PTR(-EINVAL);
241 pr_debug("%s: looking for %x:%x\n", __func__,
242 clkspec->args[0], clkspec->args[1]);
244 list_for_each_entry(entry, &provider->clocks, node) {
245 if (entry->reg_offset == clkspec->args[0] &&
246 entry->bit_offset == clkspec->args[1]) {
253 return ERR_PTR(-EINVAL);
259 _ti_clkctrl_clk_register(struct omap_clkctrl_provider *provider,
260 struct device_node *node, struct clk_hw *clk_hw,
261 u16 offset, u8 bit, const char * const *parents,
262 int num_parents, const struct clk_ops *ops)
264 struct clk_init_data init = { NULL };
266 struct omap_clkctrl_clk *clkctrl_clk;
269 if (ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT)
270 init.name = kasprintf(GFP_KERNEL, "%pOFn:%pOFn:%04x:%d",
271 node->parent, node, offset,
274 init.name = kasprintf(GFP_KERNEL, "%pOFn:%04x:%d", node,
276 clkctrl_clk = kzalloc(sizeof(*clkctrl_clk), GFP_KERNEL);
277 if (!init.name || !clkctrl_clk) {
282 clk_hw->init = &init;
283 init.parent_names = parents;
284 init.num_parents = num_parents;
288 clk = ti_clk_register(NULL, clk_hw, init.name);
289 if (IS_ERR_OR_NULL(clk)) {
294 clkctrl_clk->reg_offset = offset;
295 clkctrl_clk->bit_offset = bit;
296 clkctrl_clk->clk = clk_hw;
298 list_add(&clkctrl_clk->node, &provider->clocks);
309 _ti_clkctrl_setup_gate(struct omap_clkctrl_provider *provider,
310 struct device_node *node, u16 offset,
311 const struct omap_clkctrl_bit_data *data,
314 struct clk_hw_omap *clk_hw;
316 clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
320 clk_hw->enable_bit = data->bit;
321 clk_hw->enable_reg.ptr = reg;
323 if (_ti_clkctrl_clk_register(provider, node, &clk_hw->hw, offset,
324 data->bit, data->parents, 1,
330 _ti_clkctrl_setup_mux(struct omap_clkctrl_provider *provider,
331 struct device_node *node, u16 offset,
332 const struct omap_clkctrl_bit_data *data,
335 struct clk_omap_mux *mux;
337 const char * const *pname;
339 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
343 pname = data->parents;
349 mux->mask = num_parents;
350 if (!(mux->flags & CLK_MUX_INDEX_ONE))
353 mux->mask = (1 << fls(mux->mask)) - 1;
355 mux->shift = data->bit;
358 if (_ti_clkctrl_clk_register(provider, node, &mux->hw, offset,
359 data->bit, data->parents, num_parents,
365 _ti_clkctrl_setup_div(struct omap_clkctrl_provider *provider,
366 struct device_node *node, u16 offset,
367 const struct omap_clkctrl_bit_data *data,
370 struct clk_omap_divider *div;
371 const struct omap_clkctrl_div_data *div_data = data->data;
374 div = kzalloc(sizeof(*div), GFP_KERNEL);
379 div->shift = data->bit;
380 div->flags = div_data->flags;
382 if (div->flags & CLK_DIVIDER_POWER_OF_TWO)
383 div_flags |= CLKF_INDEX_POWER_OF_TWO;
385 if (ti_clk_parse_divider_data((int *)div_data->dividers, 0,
386 div_data->max_div, div_flags,
388 pr_err("%s: Data parsing for %pOF:%04x:%d failed\n", __func__,
389 node, offset, data->bit);
394 if (_ti_clkctrl_clk_register(provider, node, &div->hw, offset,
395 data->bit, data->parents, 1,
396 &ti_clk_divider_ops))
401 _ti_clkctrl_setup_subclks(struct omap_clkctrl_provider *provider,
402 struct device_node *node,
403 const struct omap_clkctrl_reg_data *data,
406 const struct omap_clkctrl_bit_data *bits = data->bit_data;
412 switch (bits->type) {
414 _ti_clkctrl_setup_gate(provider, node, data->offset,
419 _ti_clkctrl_setup_div(provider, node, data->offset,
424 _ti_clkctrl_setup_mux(provider, node, data->offset,
429 pr_err("%s: bad subclk type: %d\n", __func__,
437 static void __init _clkctrl_add_provider(void *data,
438 struct device_node *np)
440 of_clk_add_hw_provider(np, _ti_omap4_clkctrl_xlate, data);
443 static void __init _ti_omap4_clkctrl_setup(struct device_node *node)
445 struct omap_clkctrl_provider *provider;
446 const struct omap_clkctrl_data *data = default_clkctrl_data;
447 const struct omap_clkctrl_reg_data *reg_data;
448 struct clk_init_data init = { NULL };
449 struct clk_hw_omap *hw;
451 struct omap_clkctrl_clk *clkctrl_clk;
458 if (!(ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT) &&
459 of_node_name_eq(node, "clk"))
460 ti_clk_features.flags |= TI_CLK_CLKCTRL_COMPAT;
462 addrp = of_get_address(node, 0, NULL, NULL);
463 addr = (u32)of_translate_address(node, addrp);
465 #ifdef CONFIG_ARCH_OMAP4
466 if (of_machine_is_compatible("ti,omap4"))
467 data = omap4_clkctrl_data;
469 #ifdef CONFIG_SOC_OMAP5
470 if (of_machine_is_compatible("ti,omap5"))
471 data = omap5_clkctrl_data;
473 #ifdef CONFIG_SOC_DRA7XX
474 if (of_machine_is_compatible("ti,dra7")) {
475 if (ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT)
476 data = dra7_clkctrl_compat_data;
478 data = dra7_clkctrl_data;
481 if (of_machine_is_compatible("ti,dra72"))
482 soc_mask = CLKF_SOC_DRA72;
483 if (of_machine_is_compatible("ti,dra74"))
484 soc_mask = CLKF_SOC_DRA74;
485 if (of_machine_is_compatible("ti,dra76"))
486 soc_mask = CLKF_SOC_DRA76;
488 #ifdef CONFIG_SOC_AM33XX
489 if (of_machine_is_compatible("ti,am33xx")) {
490 if (ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT)
491 data = am3_clkctrl_compat_data;
493 data = am3_clkctrl_data;
496 #ifdef CONFIG_SOC_AM43XX
497 if (of_machine_is_compatible("ti,am4372")) {
498 if (ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT)
499 data = am4_clkctrl_compat_data;
501 data = am4_clkctrl_data;
504 if (of_machine_is_compatible("ti,am438x")) {
505 if (ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT)
506 data = am438x_clkctrl_compat_data;
508 data = am438x_clkctrl_data;
511 #ifdef CONFIG_SOC_TI81XX
512 if (of_machine_is_compatible("ti,dm814"))
513 data = dm814_clkctrl_data;
515 if (of_machine_is_compatible("ti,dm816"))
516 data = dm816_clkctrl_data;
519 if (ti_clk_get_features()->flags & TI_CLK_DEVICE_TYPE_GP)
520 soc_mask |= CLKF_SOC_NONSEC;
523 if (addr == data->addr)
530 pr_err("%pOF not found from clkctrl data.\n", node);
534 provider = kzalloc(sizeof(*provider), GFP_KERNEL);
538 provider->base = of_iomap(node, 0);
540 if (ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT) {
541 provider->clkdm_name = kasprintf(GFP_KERNEL, "%pOFnxxx", node->parent);
542 if (!provider->clkdm_name) {
548 * Create default clkdm name, replace _cm from end of parent
549 * node name with _clkdm
551 provider->clkdm_name[strlen(provider->clkdm_name) - 2] = 0;
553 provider->clkdm_name = kasprintf(GFP_KERNEL, "%pOFn", node);
554 if (!provider->clkdm_name) {
560 * Create default clkdm name, replace _clkctrl from end of
561 * node name with _clkdm
563 provider->clkdm_name[strlen(provider->clkdm_name) - 7] = 0;
566 strcat(provider->clkdm_name, "clkdm");
568 /* Replace any dash from the clkdm name with underscore */
569 c = provider->clkdm_name;
577 INIT_LIST_HEAD(&provider->clocks);
579 /* Generate clocks */
580 reg_data = data->regs;
582 while (reg_data->parent) {
583 if ((reg_data->flags & CLKF_SOC_MASK) &&
584 (reg_data->flags & soc_mask) == 0) {
589 hw = kzalloc(sizeof(*hw), GFP_KERNEL);
593 hw->enable_reg.ptr = provider->base + reg_data->offset;
595 _ti_clkctrl_setup_subclks(provider, node, reg_data,
598 if (reg_data->flags & CLKF_SW_SUP)
599 hw->enable_bit = MODULEMODE_SWCTRL;
600 if (reg_data->flags & CLKF_HW_SUP)
601 hw->enable_bit = MODULEMODE_HWCTRL;
602 if (reg_data->flags & CLKF_NO_IDLEST)
603 set_bit(NO_IDLEST, &hw->flags);
605 if (reg_data->clkdm_name)
606 hw->clkdm_name = reg_data->clkdm_name;
608 hw->clkdm_name = provider->clkdm_name;
610 init.parent_names = ®_data->parent;
611 init.num_parents = 1;
613 if (reg_data->flags & CLKF_SET_RATE_PARENT)
614 init.flags |= CLK_SET_RATE_PARENT;
615 if (ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT)
616 init.name = kasprintf(GFP_KERNEL, "%pOFn:%pOFn:%04x:%d",
618 reg_data->offset, 0);
620 init.name = kasprintf(GFP_KERNEL, "%pOFn:%04x:%d",
621 node, reg_data->offset, 0);
622 clkctrl_clk = kzalloc(sizeof(*clkctrl_clk), GFP_KERNEL);
623 if (!init.name || !clkctrl_clk)
626 init.ops = &omap4_clkctrl_clk_ops;
629 clk = ti_clk_register_omap_hw(NULL, &hw->hw, init.name);
630 if (IS_ERR_OR_NULL(clk))
633 clkctrl_clk->reg_offset = reg_data->offset;
634 clkctrl_clk->clk = &hw->hw;
636 list_add(&clkctrl_clk->node, &provider->clocks);
641 ret = of_clk_add_hw_provider(node, _ti_omap4_clkctrl_xlate, provider);
642 if (ret == -EPROBE_DEFER)
643 ti_clk_retry_init(node, provider, _clkctrl_add_provider);
652 CLK_OF_DECLARE(ti_omap4_clkctrl_clock, "ti,clkctrl",
653 _ti_omap4_clkctrl_setup);
656 * ti_clk_is_in_standby - Check if clkctrl clock is in standby or not
657 * @clk: clock to check standby status for
659 * Finds whether the provided clock is in standby mode or not. Returns
660 * true if the provided clock is a clkctrl type clock and it is in standby,
663 bool ti_clk_is_in_standby(struct clk *clk)
666 struct clk_hw_omap *hwclk;
669 hw = __clk_get_hw(clk);
671 if (!omap2_clk_is_hw_omap(hw))
674 hwclk = to_clk_hw_omap(hw);
676 val = ti_clk_ll_ops->clk_readl(&hwclk->enable_reg);
678 if (val & OMAP4_STBYST_MASK)
683 EXPORT_SYMBOL_GPL(ti_clk_is_in_standby);