2 * Copyright 2013-2015 Emilio López
4 * Emilio López <emilio@elopez.com.ar>
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.
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.
17 #include <linux/clk-provider.h>
18 #include <linux/clkdev.h>
20 #include <linux/of_address.h>
21 #include <linux/reset-controller.h>
22 #include <linux/spinlock.h>
26 * sunxi_usb_reset... - reset bits in usb clk registers handling
29 struct usb_reset_data {
33 struct reset_controller_dev rcdev;
36 static int sunxi_usb_reset_assert(struct reset_controller_dev *rcdev,
39 struct usb_reset_data *data = container_of(rcdev,
40 struct usb_reset_data,
45 clk_prepare_enable(data->clk);
46 spin_lock_irqsave(data->lock, flags);
48 reg = readl(data->reg);
49 writel(reg & ~BIT(id), data->reg);
51 spin_unlock_irqrestore(data->lock, flags);
52 clk_disable_unprepare(data->clk);
57 static int sunxi_usb_reset_deassert(struct reset_controller_dev *rcdev,
60 struct usb_reset_data *data = container_of(rcdev,
61 struct usb_reset_data,
66 clk_prepare_enable(data->clk);
67 spin_lock_irqsave(data->lock, flags);
69 reg = readl(data->reg);
70 writel(reg | BIT(id), data->reg);
72 spin_unlock_irqrestore(data->lock, flags);
73 clk_disable_unprepare(data->clk);
78 static struct reset_control_ops sunxi_usb_reset_ops = {
79 .assert = sunxi_usb_reset_assert,
80 .deassert = sunxi_usb_reset_deassert,
84 * sunxi_usb_clk_setup() - Setup function for usb gate clocks
87 #define SUNXI_USB_MAX_SIZE 32
95 static void __init sunxi_usb_clk_setup(struct device_node *node,
96 const struct usb_clk_data *data,
99 struct clk_onecell_data *clk_data;
100 struct usb_reset_data *reset_data;
101 const char *clk_parent;
102 const char *clk_name;
108 reg = of_io_request_and_map(node, 0, of_node_full_name(node));
112 clk_parent = of_clk_get_parent_name(node, 0);
116 /* Worst-case size approximation and memory allocation */
117 qty = find_last_bit((unsigned long *)&data->clk_mask,
120 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
124 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
125 if (!clk_data->clks) {
130 for_each_set_bit(i, (unsigned long *)&data->clk_mask,
131 SUNXI_USB_MAX_SIZE) {
132 of_property_read_string_index(node, "clock-output-names",
134 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
137 WARN_ON(IS_ERR(clk_data->clks[i]));
142 /* Adjust to the real max */
143 clk_data->clk_num = i;
145 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
147 /* Register a reset controller for usb with reset bits */
148 if (data->reset_mask == 0)
151 reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
155 if (data->reset_needs_clk) {
156 reset_data->clk = of_clk_get(node, 0);
157 if (IS_ERR(reset_data->clk)) {
158 pr_err("Could not get clock for reset controls\n");
164 reset_data->reg = reg;
165 reset_data->lock = lock;
166 reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
167 reset_data->rcdev.ops = &sunxi_usb_reset_ops;
168 reset_data->rcdev.of_node = node;
169 reset_controller_register(&reset_data->rcdev);
172 static const struct usb_clk_data sun4i_a10_usb_clk_data __initconst = {
173 .clk_mask = BIT(8) | BIT(7) | BIT(6),
174 .reset_mask = BIT(2) | BIT(1) | BIT(0),
177 static DEFINE_SPINLOCK(sun4i_a10_usb_lock);
179 static void __init sun4i_a10_usb_setup(struct device_node *node)
181 sunxi_usb_clk_setup(node, &sun4i_a10_usb_clk_data, &sun4i_a10_usb_lock);
183 CLK_OF_DECLARE(sun4i_a10_usb, "allwinner,sun4i-a10-usb-clk", sun4i_a10_usb_setup);
185 static const struct usb_clk_data sun5i_a13_usb_clk_data __initconst = {
186 .clk_mask = BIT(8) | BIT(6),
187 .reset_mask = BIT(1) | BIT(0),
190 static void __init sun5i_a13_usb_setup(struct device_node *node)
192 sunxi_usb_clk_setup(node, &sun5i_a13_usb_clk_data, &sun4i_a10_usb_lock);
194 CLK_OF_DECLARE(sun5i_a13_usb, "allwinner,sun5i-a13-usb-clk", sun5i_a13_usb_setup);
196 static const struct usb_clk_data sun6i_a31_usb_clk_data __initconst = {
197 .clk_mask = BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8),
198 .reset_mask = BIT(2) | BIT(1) | BIT(0),
201 static void __init sun6i_a31_usb_setup(struct device_node *node)
203 sunxi_usb_clk_setup(node, &sun6i_a31_usb_clk_data, &sun4i_a10_usb_lock);
205 CLK_OF_DECLARE(sun6i_a31_usb, "allwinner,sun6i-a31-usb-clk", sun6i_a31_usb_setup);
207 static const struct usb_clk_data sun9i_a80_usb_mod_data __initconst = {
208 .clk_mask = BIT(6) | BIT(5) | BIT(4) | BIT(3) | BIT(2) | BIT(1),
209 .reset_mask = BIT(19) | BIT(18) | BIT(17),
210 .reset_needs_clk = 1,
213 static DEFINE_SPINLOCK(a80_usb_mod_lock);
215 static void __init sun9i_a80_usb_mod_setup(struct device_node *node)
217 sunxi_usb_clk_setup(node, &sun9i_a80_usb_mod_data, &a80_usb_mod_lock);
219 CLK_OF_DECLARE(sun9i_a80_usb_mod, "allwinner,sun9i-a80-usb-mod-clk", sun9i_a80_usb_mod_setup);
221 static const struct usb_clk_data sun9i_a80_usb_phy_data __initconst = {
222 .clk_mask = BIT(10) | BIT(5) | BIT(4) | BIT(3) | BIT(2) | BIT(1),
223 .reset_mask = BIT(21) | BIT(20) | BIT(19) | BIT(18) | BIT(17),
224 .reset_needs_clk = 1,
227 static DEFINE_SPINLOCK(a80_usb_phy_lock);
229 static void __init sun9i_a80_usb_phy_setup(struct device_node *node)
231 sunxi_usb_clk_setup(node, &sun9i_a80_usb_phy_data, &a80_usb_phy_lock);
233 CLK_OF_DECLARE(sun9i_a80_usb_phy, "allwinner,sun9i-a80-usb-phy-clk", sun9i_a80_usb_phy_setup);