blob: f1dcc8fb5a7ddf99be3c8f11783bc4a58c56330e [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
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 reset_controller_dev rcdev;
33};
34
35static int sunxi_usb_reset_assert(struct reset_controller_dev *rcdev,
36 unsigned long id)
37{
38 struct usb_reset_data *data = container_of(rcdev,
39 struct usb_reset_data,
40 rcdev);
41 unsigned long flags;
42 u32 reg;
43
44 spin_lock_irqsave(data->lock, flags);
45
46 reg = readl(data->reg);
47 writel(reg & ~BIT(id), data->reg);
48
49 spin_unlock_irqrestore(data->lock, flags);
50
51 return 0;
52}
53
54static int sunxi_usb_reset_deassert(struct reset_controller_dev *rcdev,
55 unsigned long id)
56{
57 struct usb_reset_data *data = container_of(rcdev,
58 struct usb_reset_data,
59 rcdev);
60 unsigned long flags;
61 u32 reg;
62
63 spin_lock_irqsave(data->lock, flags);
64
65 reg = readl(data->reg);
66 writel(reg | BIT(id), data->reg);
67
68 spin_unlock_irqrestore(data->lock, flags);
69
70 return 0;
71}
72
73static struct reset_control_ops sunxi_usb_reset_ops = {
74 .assert = sunxi_usb_reset_assert,
75 .deassert = sunxi_usb_reset_deassert,
76};
77
78/**
79 * sunxi_usb_clk_setup() - Setup function for usb gate clocks
80 */
81
82#define SUNXI_USB_MAX_SIZE 32
83
84struct usb_clk_data {
85 u32 clk_mask;
86 u32 reset_mask;
87};
88
89static void __init sunxi_usb_clk_setup(struct device_node *node,
90 const struct usb_clk_data *data,
91 spinlock_t *lock)
92{
93 struct clk_onecell_data *clk_data;
94 struct usb_reset_data *reset_data;
95 const char *clk_parent;
96 const char *clk_name;
97 void __iomem *reg;
98 int qty;
99 int i = 0;
100 int j = 0;
101
102 reg = of_io_request_and_map(node, 0, of_node_full_name(node));
103 if (IS_ERR(reg))
104 return;
105
106 clk_parent = of_clk_get_parent_name(node, 0);
107 if (!clk_parent)
108 return;
109
110 /* Worst-case size approximation and memory allocation */
111 qty = find_last_bit((unsigned long *)&data->clk_mask,
112 SUNXI_USB_MAX_SIZE);
113
114 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
115 if (!clk_data)
116 return;
117
118 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
119 if (!clk_data->clks) {
120 kfree(clk_data);
121 return;
122 }
123
124 for_each_set_bit(i, (unsigned long *)&data->clk_mask,
125 SUNXI_USB_MAX_SIZE) {
126 of_property_read_string_index(node, "clock-output-names",
127 j, &clk_name);
128 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
129 clk_parent, 0,
130 reg, i, 0, lock);
131 WARN_ON(IS_ERR(clk_data->clks[i]));
132
133 j++;
134 }
135
136 /* Adjust to the real max */
137 clk_data->clk_num = i;
138
139 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
140
141 /* Register a reset controller for usb with reset bits */
142 if (data->reset_mask == 0)
143 return;
144
145 reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
146 if (!reset_data)
147 return;
148
149 reset_data->reg = reg;
150 reset_data->lock = lock;
151 reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
152 reset_data->rcdev.ops = &sunxi_usb_reset_ops;
153 reset_data->rcdev.of_node = node;
154 reset_controller_register(&reset_data->rcdev);
155}
156
157static const struct usb_clk_data sun4i_a10_usb_clk_data __initconst = {
158 .clk_mask = BIT(8) | BIT(7) | BIT(6),
159 .reset_mask = BIT(2) | BIT(1) | BIT(0),
160};
161
162static DEFINE_SPINLOCK(sun4i_a10_usb_lock);
163
164static void __init sun4i_a10_usb_setup(struct device_node *node)
165{
166 sunxi_usb_clk_setup(node, &sun4i_a10_usb_clk_data, &sun4i_a10_usb_lock);
167}
168CLK_OF_DECLARE(sun4i_a10_usb, "allwinner,sun4i-a10-usb-clk", sun4i_a10_usb_setup);
169
170static const struct usb_clk_data sun5i_a13_usb_clk_data __initconst = {
171 .clk_mask = BIT(8) | BIT(6),
172 .reset_mask = BIT(1) | BIT(0),
173};
174
175static void __init sun5i_a13_usb_setup(struct device_node *node)
176{
177 sunxi_usb_clk_setup(node, &sun5i_a13_usb_clk_data, &sun4i_a10_usb_lock);
178}
179CLK_OF_DECLARE(sun5i_a13_usb, "allwinner,sun5i-a13-usb-clk", sun5i_a13_usb_setup);
180
181static const struct usb_clk_data sun6i_a31_usb_clk_data __initconst = {
182 .clk_mask = BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8),
183 .reset_mask = BIT(2) | BIT(1) | BIT(0),
184};
185
186static void __init sun6i_a31_usb_setup(struct device_node *node)
187{
188 sunxi_usb_clk_setup(node, &sun6i_a31_usb_clk_data, &sun4i_a10_usb_lock);
189}
190CLK_OF_DECLARE(sun6i_a31_usb, "allwinner,sun6i-a31-usb-clk", sun6i_a31_usb_setup);