Blame view

drivers/clk/mvebu/clk-corediv.c 8.5 KB
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  /*
   * MVEBU Core divider clock
   *
   * Copyright (C) 2013 Marvell
   *
   * Ezequiel Garcia <ezequiel.garcia@free-electrons.com>
   *
   * This file is licensed under the terms of the GNU General Public
   * License version 2.  This program is licensed "as is" without any
   * warranty of any kind, whether express or implied.
   */
  
  #include <linux/kernel.h>
  #include <linux/clk-provider.h>
  #include <linux/of_address.h>
  #include <linux/slab.h>
  #include <linux/delay.h>
  #include "common.h"
  
  #define CORE_CLK_DIV_RATIO_MASK		0xff
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
21

846f33e6c   Thomas Petazzoni   clk: mvebu: add a...
22
23
24
25
26
27
28
  /*
   * This structure describes the hardware details (bit offset and mask)
   * to configure one particular core divider clock. Those hardware
   * details may differ from one SoC to another. This structure is
   * therefore typically instantiated statically to describe the
   * hardware details.
   */
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
29
30
31
32
33
  struct clk_corediv_desc {
  	unsigned int mask;
  	unsigned int offset;
  	unsigned int fieldbit;
  };
846f33e6c   Thomas Petazzoni   clk: mvebu: add a...
34
  /*
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
   * This structure describes the hardware details to configure the core
   * divider clocks on a given SoC. Amongst others, it points to the
   * array of core divider clock descriptors for this SoC, as well as
   * the corresponding operations to manipulate them.
   */
  struct clk_corediv_soc_desc {
  	const struct clk_corediv_desc *descs;
  	unsigned int ndescs;
  	const struct clk_ops ops;
  	u32 ratio_reload;
  	u32 enable_bit_offset;
  	u32 ratio_offset;
  };
  
  /*
846f33e6c   Thomas Petazzoni   clk: mvebu: add a...
50
51
52
53
   * This structure represents one core divider clock for the clock
   * framework, and is dynamically allocated for each core divider clock
   * existing in the current SoC.
   */
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
54
55
56
  struct clk_corediv {
  	struct clk_hw hw;
  	void __iomem *reg;
5d836c58f   Thomas Petazzoni   clk: mvebu: do no...
57
  	const struct clk_corediv_desc *desc;
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
58
  	const struct clk_corediv_soc_desc *soc_desc;
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
59
60
61
62
  	spinlock_t lock;
  };
  
  static struct clk_onecell_data clk_data;
846f33e6c   Thomas Petazzoni   clk: mvebu: add a...
63
64
65
66
67
  /*
   * Description of the core divider clocks available. For now, we
   * support only NAND, and it is available at the same register
   * locations regardless of the SoC.
   */
5d836c58f   Thomas Petazzoni   clk: mvebu: do no...
68
  static const struct clk_corediv_desc mvebu_corediv_desc[] = {
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
69
70
71
72
73
74
75
76
  	{ .mask = 0x3f, .offset = 8, .fieldbit = 1 }, /* NAND clock */
  };
  
  #define to_corediv_clk(p) container_of(p, struct clk_corediv, hw)
  
  static int clk_corediv_is_enabled(struct clk_hw *hwclk)
  {
  	struct clk_corediv *corediv = to_corediv_clk(hwclk);
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
77
  	const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
5d836c58f   Thomas Petazzoni   clk: mvebu: do no...
78
  	const struct clk_corediv_desc *desc = corediv->desc;
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
79
  	u32 enable_mask = BIT(desc->fieldbit) << soc_desc->enable_bit_offset;
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
80
81
82
83
84
85
86
  
  	return !!(readl(corediv->reg) & enable_mask);
  }
  
  static int clk_corediv_enable(struct clk_hw *hwclk)
  {
  	struct clk_corediv *corediv = to_corediv_clk(hwclk);
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
87
  	const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
5d836c58f   Thomas Petazzoni   clk: mvebu: do no...
88
  	const struct clk_corediv_desc *desc = corediv->desc;
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
89
90
91
92
93
94
  	unsigned long flags = 0;
  	u32 reg;
  
  	spin_lock_irqsave(&corediv->lock, flags);
  
  	reg = readl(corediv->reg);
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
95
  	reg |= (BIT(desc->fieldbit) << soc_desc->enable_bit_offset);
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
96
97
98
99
100
101
102
103
104
105
  	writel(reg, corediv->reg);
  
  	spin_unlock_irqrestore(&corediv->lock, flags);
  
  	return 0;
  }
  
  static void clk_corediv_disable(struct clk_hw *hwclk)
  {
  	struct clk_corediv *corediv = to_corediv_clk(hwclk);
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
106
  	const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
5d836c58f   Thomas Petazzoni   clk: mvebu: do no...
107
  	const struct clk_corediv_desc *desc = corediv->desc;
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
108
109
110
111
112
113
  	unsigned long flags = 0;
  	u32 reg;
  
  	spin_lock_irqsave(&corediv->lock, flags);
  
  	reg = readl(corediv->reg);
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
114
  	reg &= ~(BIT(desc->fieldbit) << soc_desc->enable_bit_offset);
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
115
116
117
118
119
120
121
122
123
  	writel(reg, corediv->reg);
  
  	spin_unlock_irqrestore(&corediv->lock, flags);
  }
  
  static unsigned long clk_corediv_recalc_rate(struct clk_hw *hwclk,
  					 unsigned long parent_rate)
  {
  	struct clk_corediv *corediv = to_corediv_clk(hwclk);
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
124
  	const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
5d836c58f   Thomas Petazzoni   clk: mvebu: do no...
125
  	const struct clk_corediv_desc *desc = corediv->desc;
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
126
  	u32 reg, div;
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
127
  	reg = readl(corediv->reg + soc_desc->ratio_offset);
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  	div = (reg >> desc->offset) & desc->mask;
  	return parent_rate / div;
  }
  
  static long clk_corediv_round_rate(struct clk_hw *hwclk, unsigned long rate,
  			       unsigned long *parent_rate)
  {
  	/* Valid ratio are 1:4, 1:5, 1:6 and 1:8 */
  	u32 div;
  
  	div = *parent_rate / rate;
  	if (div < 4)
  		div = 4;
  	else if (div > 6)
  		div = 8;
  
  	return *parent_rate / div;
  }
  
  static int clk_corediv_set_rate(struct clk_hw *hwclk, unsigned long rate,
  			    unsigned long parent_rate)
  {
  	struct clk_corediv *corediv = to_corediv_clk(hwclk);
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
151
  	const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
5d836c58f   Thomas Petazzoni   clk: mvebu: do no...
152
  	const struct clk_corediv_desc *desc = corediv->desc;
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
153
154
155
156
157
158
159
160
  	unsigned long flags = 0;
  	u32 reg, div;
  
  	div = parent_rate / rate;
  
  	spin_lock_irqsave(&corediv->lock, flags);
  
  	/* Write new divider to the divider ratio register */
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
161
  	reg = readl(corediv->reg + soc_desc->ratio_offset);
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
162
163
  	reg &= ~(desc->mask << desc->offset);
  	reg |= (div & desc->mask) << desc->offset;
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
164
  	writel(reg, corediv->reg + soc_desc->ratio_offset);
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
165
166
167
168
169
170
  
  	/* Set reload-force for this clock */
  	reg = readl(corediv->reg) | BIT(desc->fieldbit);
  	writel(reg, corediv->reg);
  
  	/* Now trigger the clock update */
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
171
  	reg = readl(corediv->reg) | soc_desc->ratio_reload;
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
172
173
174
175
176
177
178
  	writel(reg, corediv->reg);
  
  	/*
  	 * Wait for clocks to settle down, and then clear all the
  	 * ratios request and the reload request.
  	 */
  	udelay(1000);
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
179
  	reg &= ~(CORE_CLK_DIV_RATIO_MASK | soc_desc->ratio_reload);
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
180
181
182
183
184
185
186
  	writel(reg, corediv->reg);
  	udelay(1000);
  
  	spin_unlock_irqrestore(&corediv->lock, flags);
  
  	return 0;
  }
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  static const struct clk_corediv_soc_desc armada370_corediv_soc = {
  	.descs = mvebu_corediv_desc,
  	.ndescs = ARRAY_SIZE(mvebu_corediv_desc),
  	.ops = {
  		.enable = clk_corediv_enable,
  		.disable = clk_corediv_disable,
  		.is_enabled = clk_corediv_is_enabled,
  		.recalc_rate = clk_corediv_recalc_rate,
  		.round_rate = clk_corediv_round_rate,
  		.set_rate = clk_corediv_set_rate,
  	},
  	.ratio_reload = BIT(8),
  	.enable_bit_offset = 24,
  	.ratio_offset = 0x8,
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
201
  };
0737c15ff   Ezequiel Garcia   clk: mvebu: Suppo...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  static const struct clk_corediv_soc_desc armada380_corediv_soc = {
  	.descs = mvebu_corediv_desc,
  	.ndescs = ARRAY_SIZE(mvebu_corediv_desc),
  	.ops = {
  		.enable = clk_corediv_enable,
  		.disable = clk_corediv_disable,
  		.is_enabled = clk_corediv_is_enabled,
  		.recalc_rate = clk_corediv_recalc_rate,
  		.round_rate = clk_corediv_round_rate,
  		.set_rate = clk_corediv_set_rate,
  	},
  	.ratio_reload = BIT(8),
  	.enable_bit_offset = 16,
  	.ratio_offset = 0x4,
  };
e4aec65c8   Thomas Petazzoni   clk: mvebu: add A...
217
218
219
220
221
222
223
224
225
  static const struct clk_corediv_soc_desc armada375_corediv_soc = {
  	.descs = mvebu_corediv_desc,
  	.ndescs = ARRAY_SIZE(mvebu_corediv_desc),
  	.ops = {
  		.recalc_rate = clk_corediv_recalc_rate,
  		.round_rate = clk_corediv_round_rate,
  		.set_rate = clk_corediv_set_rate,
  	},
  	.ratio_reload = BIT(8),
8230a5ab4   Ezequiel Garcia   clk: mvebu: Fix r...
226
  	.ratio_offset = 0x4,
e4aec65c8   Thomas Petazzoni   clk: mvebu: add A...
227
  };
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
228
229
230
  static void __init
  mvebu_corediv_clk_init(struct device_node *node,
  		       const struct clk_corediv_soc_desc *soc_desc)
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  {
  	struct clk_init_data init;
  	struct clk_corediv *corediv;
  	struct clk **clks;
  	void __iomem *base;
  	const char *parent_name;
  	const char *clk_name;
  	int i;
  
  	base = of_iomap(node, 0);
  	if (WARN_ON(!base))
  		return;
  
  	parent_name = of_clk_get_parent_name(node, 0);
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
245
  	clk_data.clk_num = soc_desc->ndescs;
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  
  	/* clks holds the clock array */
  	clks = kcalloc(clk_data.clk_num, sizeof(struct clk *),
  				GFP_KERNEL);
  	if (WARN_ON(!clks))
  		goto err_unmap;
  	/* corediv holds the clock specific array */
  	corediv = kcalloc(clk_data.clk_num, sizeof(struct clk_corediv),
  				GFP_KERNEL);
  	if (WARN_ON(!corediv))
  		goto err_free_clks;
  
  	spin_lock_init(&corediv->lock);
  
  	for (i = 0; i < clk_data.clk_num; i++) {
  		of_property_read_string_index(node, "clock-output-names",
  					      i, &clk_name);
  		init.num_parents = 1;
  		init.parent_names = &parent_name;
  		init.name = clk_name;
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
266
  		init.ops = &soc_desc->ops;
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
267
  		init.flags = 0;
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
268
269
  		corediv[i].soc_desc = soc_desc;
  		corediv[i].desc = soc_desc->descs + i;
a2473b6c8   Ezequiel Garcia   clk: mvebu: Add C...
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  		corediv[i].reg = base;
  		corediv[i].hw.init = &init;
  
  		clks[i] = clk_register(NULL, &corediv[i].hw);
  		WARN_ON(IS_ERR(clks[i]));
  	}
  
  	clk_data.clks = clks;
  	of_clk_add_provider(node, of_clk_src_onecell_get, &clk_data);
  	return;
  
  err_free_clks:
  	kfree(clks);
  err_unmap:
  	iounmap(base);
  }
c642e6a95   Thomas Petazzoni   clk: mvebu: refac...
286
287
288
289
290
291
292
  
  static void __init armada370_corediv_clk_init(struct device_node *node)
  {
  	return mvebu_corediv_clk_init(node, &armada370_corediv_soc);
  }
  CLK_OF_DECLARE(armada370_corediv_clk, "marvell,armada-370-corediv-clock",
  	       armada370_corediv_clk_init);
e4aec65c8   Thomas Petazzoni   clk: mvebu: add A...
293
294
295
296
297
298
299
  
  static void __init armada375_corediv_clk_init(struct device_node *node)
  {
  	return mvebu_corediv_clk_init(node, &armada375_corediv_soc);
  }
  CLK_OF_DECLARE(armada375_corediv_clk, "marvell,armada-375-corediv-clock",
  	       armada375_corediv_clk_init);
0737c15ff   Ezequiel Garcia   clk: mvebu: Suppo...
300
301
302
303
304
305
306
  
  static void __init armada380_corediv_clk_init(struct device_node *node)
  {
  	return mvebu_corediv_clk_init(node, &armada380_corediv_soc);
  }
  CLK_OF_DECLARE(armada380_corediv_clk, "marvell,armada-380-corediv-clock",
  	       armada380_corediv_clk_init);