blob: 67b8e38f4ee96d1dfec6a1230f0258ab65285980 [file] [log] [blame]
Chen-Yu Tsai6089ef12015-01-28 03:54:06 +08001/*
2 * Copyright 2013-2015 Emilio López
3 *
4 * Emilio López <emilio@elopez.com.ar>
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
Stephen Boyd9dfefe82015-06-19 15:00:46 -070017#include <linux/clk.h>
Chen-Yu Tsai6089ef12015-01-28 03:54:06 +080018#include <linux/clk-provider.h>
Chen-Yu Tsai6089ef12015-01-28 03:54:06 +080019#include <linux/of.h>
20#include <linux/of_address.h>
21#include <linux/reset-controller.h>
Stephen Boyd9dfefe82015-06-19 15:00:46 -070022#include <linux/slab.h>
Chen-Yu Tsai6089ef12015-01-28 03:54:06 +080023#include <linux/spinlock.h>
24
25
26/**
27 * sunxi_usb_reset... - reset bits in usb clk registers handling
28 */
29
30struct usb_reset_data {
31 void __iomem *reg;
32 spinlock_t *lock;
Chen-Yu Tsai71f32f52015-01-28 03:54:07 +080033 struct clk *clk;
Chen-Yu Tsai6089ef12015-01-28 03:54:06 +080034 struct reset_controller_dev rcdev;
35};
36
37static int sunxi_usb_reset_assert(struct reset_controller_dev *rcdev,
38 unsigned long id)
39{
40 struct usb_reset_data *data = container_of(rcdev,
41 struct usb_reset_data,
42 rcdev);
43 unsigned long flags;
44 u32 reg;
45
Chen-Yu Tsai71f32f52015-01-28 03:54:07 +080046 clk_prepare_enable(data->clk);
Chen-Yu Tsai6089ef12015-01-28 03:54:06 +080047 spin_lock_irqsave(data->lock, flags);
48
49 reg = readl(data->reg);
50 writel(reg & ~BIT(id), data->reg);
51
52 spin_unlock_irqrestore(data->lock, flags);
Chen-Yu Tsai71f32f52015-01-28 03:54:07 +080053 clk_disable_unprepare(data->clk);
Chen-Yu Tsai6089ef12015-01-28 03:54:06 +080054
55 return 0;
56}
57
58static int sunxi_usb_reset_deassert(struct reset_controller_dev *rcdev,
59 unsigned long id)
60{
61 struct usb_reset_data *data = container_of(rcdev,
62 struct usb_reset_data,
63 rcdev);
64 unsigned long flags;
65 u32 reg;
66
Chen-Yu Tsai71f32f52015-01-28 03:54:07 +080067 clk_prepare_enable(data->clk);
Chen-Yu Tsai6089ef12015-01-28 03:54:06 +080068 spin_lock_irqsave(data->lock, flags);
69
70 reg = readl(data->reg);
71 writel(reg | BIT(id), data->reg);
72
73 spin_unlock_irqrestore(data->lock, flags);
Chen-Yu Tsai71f32f52015-01-28 03:54:07 +080074 clk_disable_unprepare(data->clk);
Chen-Yu Tsai6089ef12015-01-28 03:54:06 +080075
76 return 0;
77}
78
79static struct reset_control_ops sunxi_usb_reset_ops = {
80 .assert = sunxi_usb_reset_assert,
81 .deassert = sunxi_usb_reset_deassert,
82};
83
84/**
85 * sunxi_usb_clk_setup() - Setup function for usb gate clocks
86 */
87
88#define SUNXI_USB_MAX_SIZE 32
89
90struct usb_clk_data {
91 u32 clk_mask;
92 u32 reset_mask;
Chen-Yu Tsai71f32f52015-01-28 03:54:07 +080093 bool reset_needs_clk;
Chen-Yu Tsai6089ef12015-01-28 03:54:06 +080094};
95
96static void __init sunxi_usb_clk_setup(struct device_node *node,
97 const struct usb_clk_data *data,
98 spinlock_t *lock)
99{
100 struct clk_onecell_data *clk_data;
101 struct usb_reset_data *reset_data;
102 const char *clk_parent;
103 const char *clk_name;
104 void __iomem *reg;
105 int qty;
106 int i = 0;
107 int j = 0;
108
109 reg = of_io_request_and_map(node, 0, of_node_full_name(node));
110 if (IS_ERR(reg))
111 return;
112
113 clk_parent = of_clk_get_parent_name(node, 0);
114 if (!clk_parent)
115 return;
116
117 /* Worst-case size approximation and memory allocation */
118 qty = find_last_bit((unsigned long *)&data->clk_mask,
119 SUNXI_USB_MAX_SIZE);
120
121 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
122 if (!clk_data)
123 return;
124
125 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
126 if (!clk_data->clks) {
127 kfree(clk_data);
128 return;
129 }
130
131 for_each_set_bit(i, (unsigned long *)&data->clk_mask,
132 SUNXI_USB_MAX_SIZE) {
133 of_property_read_string_index(node, "clock-output-names",
134 j, &clk_name);
135 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
136 clk_parent, 0,
137 reg, i, 0, lock);
138 WARN_ON(IS_ERR(clk_data->clks[i]));
139
140 j++;
141 }
142
143 /* Adjust to the real max */
144 clk_data->clk_num = i;
145
146 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
147
148 /* Register a reset controller for usb with reset bits */
149 if (data->reset_mask == 0)
150 return;
151
152 reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
153 if (!reset_data)
154 return;
155
Chen-Yu Tsai71f32f52015-01-28 03:54:07 +0800156 if (data->reset_needs_clk) {
157 reset_data->clk = of_clk_get(node, 0);
158 if (IS_ERR(reset_data->clk)) {
159 pr_err("Could not get clock for reset controls\n");
160 kfree(reset_data);
161 return;
162 }
163 }
164
Chen-Yu Tsai6089ef12015-01-28 03:54:06 +0800165 reset_data->reg = reg;
166 reset_data->lock = lock;
167 reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
168 reset_data->rcdev.ops = &sunxi_usb_reset_ops;
169 reset_data->rcdev.of_node = node;
170 reset_controller_register(&reset_data->rcdev);
171}
172
173static const struct usb_clk_data sun4i_a10_usb_clk_data __initconst = {
174 .clk_mask = BIT(8) | BIT(7) | BIT(6),
175 .reset_mask = BIT(2) | BIT(1) | BIT(0),
176};
177
178static DEFINE_SPINLOCK(sun4i_a10_usb_lock);
179
180static void __init sun4i_a10_usb_setup(struct device_node *node)
181{
182 sunxi_usb_clk_setup(node, &sun4i_a10_usb_clk_data, &sun4i_a10_usb_lock);
183}
184CLK_OF_DECLARE(sun4i_a10_usb, "allwinner,sun4i-a10-usb-clk", sun4i_a10_usb_setup);
185
186static const struct usb_clk_data sun5i_a13_usb_clk_data __initconst = {
187 .clk_mask = BIT(8) | BIT(6),
188 .reset_mask = BIT(1) | BIT(0),
189};
190
191static void __init sun5i_a13_usb_setup(struct device_node *node)
192{
193 sunxi_usb_clk_setup(node, &sun5i_a13_usb_clk_data, &sun4i_a10_usb_lock);
194}
195CLK_OF_DECLARE(sun5i_a13_usb, "allwinner,sun5i-a13-usb-clk", sun5i_a13_usb_setup);
196
197static const struct usb_clk_data sun6i_a31_usb_clk_data __initconst = {
198 .clk_mask = BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8),
199 .reset_mask = BIT(2) | BIT(1) | BIT(0),
200};
201
202static void __init sun6i_a31_usb_setup(struct device_node *node)
203{
204 sunxi_usb_clk_setup(node, &sun6i_a31_usb_clk_data, &sun4i_a10_usb_lock);
205}
206CLK_OF_DECLARE(sun6i_a31_usb, "allwinner,sun6i-a31-usb-clk", sun6i_a31_usb_setup);
Chen-Yu Tsai71f32f52015-01-28 03:54:07 +0800207
Hans de Goedeec807492015-06-01 20:23:27 +0200208static const struct usb_clk_data sun8i_a23_usb_clk_data __initconst = {
209 .clk_mask = BIT(16) | BIT(11) | BIT(10) | BIT(9) | BIT(8),
210 .reset_mask = BIT(2) | BIT(1) | BIT(0),
211};
212
213static void __init sun8i_a23_usb_setup(struct device_node *node)
214{
215 sunxi_usb_clk_setup(node, &sun8i_a23_usb_clk_data, &sun4i_a10_usb_lock);
216}
217CLK_OF_DECLARE(sun8i_a23_usb, "allwinner,sun8i-a23-usb-clk", sun8i_a23_usb_setup);
218
Chen-Yu Tsai71f32f52015-01-28 03:54:07 +0800219static const struct usb_clk_data sun9i_a80_usb_mod_data __initconst = {
220 .clk_mask = BIT(6) | BIT(5) | BIT(4) | BIT(3) | BIT(2) | BIT(1),
221 .reset_mask = BIT(19) | BIT(18) | BIT(17),
222 .reset_needs_clk = 1,
223};
224
225static DEFINE_SPINLOCK(a80_usb_mod_lock);
226
227static void __init sun9i_a80_usb_mod_setup(struct device_node *node)
228{
229 sunxi_usb_clk_setup(node, &sun9i_a80_usb_mod_data, &a80_usb_mod_lock);
230}
231CLK_OF_DECLARE(sun9i_a80_usb_mod, "allwinner,sun9i-a80-usb-mod-clk", sun9i_a80_usb_mod_setup);
232
233static const struct usb_clk_data sun9i_a80_usb_phy_data __initconst = {
234 .clk_mask = BIT(10) | BIT(5) | BIT(4) | BIT(3) | BIT(2) | BIT(1),
235 .reset_mask = BIT(21) | BIT(20) | BIT(19) | BIT(18) | BIT(17),
236 .reset_needs_clk = 1,
237};
238
239static DEFINE_SPINLOCK(a80_usb_phy_lock);
240
241static void __init sun9i_a80_usb_phy_setup(struct device_node *node)
242{
243 sunxi_usb_clk_setup(node, &sun9i_a80_usb_phy_data, &a80_usb_phy_lock);
244}
245CLK_OF_DECLARE(sun9i_a80_usb_phy, "allwinner,sun9i-a80-usb-phy-clk", sun9i_a80_usb_phy_setup);
Reinder de Haan7bec02002015-11-15 20:46:13 +0100246
247static const struct usb_clk_data sun8i_h3_usb_clk_data __initconst = {
248 .clk_mask = BIT(19) | BIT(18) | BIT(17) | BIT(16) |
249 BIT(11) | BIT(10) | BIT(9) | BIT(8),
250 .reset_mask = BIT(3) | BIT(2) | BIT(1) | BIT(0),
251};
252
253static void __init sun8i_h3_usb_setup(struct device_node *node)
254{
255 sunxi_usb_clk_setup(node, &sun8i_h3_usb_clk_data, &sun4i_a10_usb_lock);
256}
257CLK_OF_DECLARE(sun8i_h3_usb, "allwinner,sun8i-h3-usb-clk", sun8i_h3_usb_setup);