1/*
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
17#include <linux/clk-provider.h>
18#include <linux/clkdev.h>
19#include <linux/of.h>
20#include <linux/of_address.h>
21#include <linux/reset-controller.h>
22#include <linux/spinlock.h>
23
24
25/**
26 * sunxi_usb_reset... - reset bits in usb clk registers handling
27 */
28
29struct usb_reset_data {
30	void __iomem			*reg;
31	spinlock_t			*lock;
32	struct clk			*clk;
33	struct reset_controller_dev	rcdev;
34};
35
36static int sunxi_usb_reset_assert(struct reset_controller_dev *rcdev,
37			      unsigned long id)
38{
39	struct usb_reset_data *data = container_of(rcdev,
40						   struct usb_reset_data,
41						   rcdev);
42	unsigned long flags;
43	u32 reg;
44
45	clk_prepare_enable(data->clk);
46	spin_lock_irqsave(data->lock, flags);
47
48	reg = readl(data->reg);
49	writel(reg & ~BIT(id), data->reg);
50
51	spin_unlock_irqrestore(data->lock, flags);
52	clk_disable_unprepare(data->clk);
53
54	return 0;
55}
56
57static int sunxi_usb_reset_deassert(struct reset_controller_dev *rcdev,
58				unsigned long id)
59{
60	struct usb_reset_data *data = container_of(rcdev,
61						     struct usb_reset_data,
62						     rcdev);
63	unsigned long flags;
64	u32 reg;
65
66	clk_prepare_enable(data->clk);
67	spin_lock_irqsave(data->lock, flags);
68
69	reg = readl(data->reg);
70	writel(reg | BIT(id), data->reg);
71
72	spin_unlock_irqrestore(data->lock, flags);
73	clk_disable_unprepare(data->clk);
74
75	return 0;
76}
77
78static struct reset_control_ops sunxi_usb_reset_ops = {
79	.assert		= sunxi_usb_reset_assert,
80	.deassert	= sunxi_usb_reset_deassert,
81};
82
83/**
84 * sunxi_usb_clk_setup() - Setup function for usb gate clocks
85 */
86
87#define SUNXI_USB_MAX_SIZE 32
88
89struct usb_clk_data {
90	u32 clk_mask;
91	u32 reset_mask;
92	bool reset_needs_clk;
93};
94
95static void __init sunxi_usb_clk_setup(struct device_node *node,
96				       const struct usb_clk_data *data,
97				       spinlock_t *lock)
98{
99	struct clk_onecell_data *clk_data;
100	struct usb_reset_data *reset_data;
101	const char *clk_parent;
102	const char *clk_name;
103	void __iomem *reg;
104	int qty;
105	int i = 0;
106	int j = 0;
107
108	reg = of_io_request_and_map(node, 0, of_node_full_name(node));
109	if (IS_ERR(reg))
110		return;
111
112	clk_parent = of_clk_get_parent_name(node, 0);
113	if (!clk_parent)
114		return;
115
116	/* Worst-case size approximation and memory allocation */
117	qty = find_last_bit((unsigned long *)&data->clk_mask,
118			    SUNXI_USB_MAX_SIZE);
119
120	clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
121	if (!clk_data)
122		return;
123
124	clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
125	if (!clk_data->clks) {
126		kfree(clk_data);
127		return;
128	}
129
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",
133					      j, &clk_name);
134		clk_data->clks[i] = clk_register_gate(NULL, clk_name,
135						      clk_parent, 0,
136						      reg, i, 0, lock);
137		WARN_ON(IS_ERR(clk_data->clks[i]));
138
139		j++;
140	}
141
142	/* Adjust to the real max */
143	clk_data->clk_num = i;
144
145	of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
146
147	/* Register a reset controller for usb with reset bits */
148	if (data->reset_mask == 0)
149		return;
150
151	reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
152	if (!reset_data)
153		return;
154
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");
159			kfree(reset_data);
160			return;
161		}
162	}
163
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);
170}
171
172static 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),
175};
176
177static DEFINE_SPINLOCK(sun4i_a10_usb_lock);
178
179static void __init sun4i_a10_usb_setup(struct device_node *node)
180{
181	sunxi_usb_clk_setup(node, &sun4i_a10_usb_clk_data, &sun4i_a10_usb_lock);
182}
183CLK_OF_DECLARE(sun4i_a10_usb, "allwinner,sun4i-a10-usb-clk", sun4i_a10_usb_setup);
184
185static 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),
188};
189
190static void __init sun5i_a13_usb_setup(struct device_node *node)
191{
192	sunxi_usb_clk_setup(node, &sun5i_a13_usb_clk_data, &sun4i_a10_usb_lock);
193}
194CLK_OF_DECLARE(sun5i_a13_usb, "allwinner,sun5i-a13-usb-clk", sun5i_a13_usb_setup);
195
196static 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),
199};
200
201static void __init sun6i_a31_usb_setup(struct device_node *node)
202{
203	sunxi_usb_clk_setup(node, &sun6i_a31_usb_clk_data, &sun4i_a10_usb_lock);
204}
205CLK_OF_DECLARE(sun6i_a31_usb, "allwinner,sun6i-a31-usb-clk", sun6i_a31_usb_setup);
206
207static 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,
211};
212
213static DEFINE_SPINLOCK(a80_usb_mod_lock);
214
215static void __init sun9i_a80_usb_mod_setup(struct device_node *node)
216{
217	sunxi_usb_clk_setup(node, &sun9i_a80_usb_mod_data, &a80_usb_mod_lock);
218}
219CLK_OF_DECLARE(sun9i_a80_usb_mod, "allwinner,sun9i-a80-usb-mod-clk", sun9i_a80_usb_mod_setup);
220
221static 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,
225};
226
227static DEFINE_SPINLOCK(a80_usb_phy_lock);
228
229static void __init sun9i_a80_usb_phy_setup(struct device_node *node)
230{
231	sunxi_usb_clk_setup(node, &sun9i_a80_usb_phy_data, &a80_usb_phy_lock);
232}
233CLK_OF_DECLARE(sun9i_a80_usb_phy, "allwinner,sun9i-a80-usb-phy-clk", sun9i_a80_usb_phy_setup);
234