blob: 6986e11e91b084057bb03ea7f469d6b1dc6117b5 [file] [log] [blame]
Maxime Ripard1d80c142016-06-29 21:05:23 +02001/*
2 * Copyright 2016 Maxime Ripard
3 *
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/clk-provider.h>
18#include <linux/iopoll.h>
19#include <linux/slab.h>
20
21#include "ccu_common.h"
22#include "ccu_reset.h"
23
24static DEFINE_SPINLOCK(ccu_lock);
25
26void ccu_helper_wait_for_lock(struct ccu_common *common, u32 lock)
27{
Chen-Yu Tsai3de64bf2017-01-28 20:22:33 +080028 void __iomem *addr;
Maxime Ripard1d80c142016-06-29 21:05:23 +020029 u32 reg;
30
31 if (!lock)
32 return;
33
Chen-Yu Tsai3de64bf2017-01-28 20:22:33 +080034 if (common->features & CCU_FEATURE_LOCK_REG)
35 addr = common->base + common->lock_reg;
36 else
37 addr = common->base + common->reg;
38
39 WARN_ON(readl_relaxed_poll_timeout(addr, reg, reg & lock, 100, 70000));
Maxime Ripard1d80c142016-06-29 21:05:23 +020040}
41
42int sunxi_ccu_probe(struct device_node *node, void __iomem *reg,
43 const struct sunxi_ccu_desc *desc)
44{
45 struct ccu_reset *reset;
46 int i, ret;
47
48 for (i = 0; i < desc->num_ccu_clks; i++) {
49 struct ccu_common *cclk = desc->ccu_clks[i];
50
51 if (!cclk)
52 continue;
53
54 cclk->base = reg;
55 cclk->lock = &ccu_lock;
56 }
57
58 for (i = 0; i < desc->hw_clks->num ; i++) {
59 struct clk_hw *hw = desc->hw_clks->hws[i];
60
61 if (!hw)
62 continue;
63
64 ret = clk_hw_register(NULL, hw);
65 if (ret) {
66 pr_err("Couldn't register clock %s\n",
67 clk_hw_get_name(hw));
68 goto err_clk_unreg;
69 }
70 }
71
72 ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get,
73 desc->hw_clks);
74 if (ret)
75 goto err_clk_unreg;
76
77 reset = kzalloc(sizeof(*reset), GFP_KERNEL);
78 reset->rcdev.of_node = node;
79 reset->rcdev.ops = &ccu_reset_ops;
80 reset->rcdev.owner = THIS_MODULE;
81 reset->rcdev.nr_resets = desc->num_resets;
82 reset->base = reg;
83 reset->lock = &ccu_lock;
84 reset->reset_map = desc->resets;
85
86 ret = reset_controller_register(&reset->rcdev);
87 if (ret)
88 goto err_of_clk_unreg;
89
90 return 0;
91
92err_of_clk_unreg:
93err_clk_unreg:
94 return ret;
95}