blob: 9a0744c9947c5fc3dc439f679b5c85e88b9b3c86 [file] [log] [blame]
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +01001/*
2 * AXI clkgen driver
3 *
4 * Copyright 2012-2013 Analog Devices Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
6 *
7 * Licensed under the GPL-2.
8 *
9 */
10
11#include <linux/platform_device.h>
12#include <linux/clk-provider.h>
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +010013#include <linux/slab.h>
14#include <linux/io.h>
15#include <linux/of.h>
16#include <linux/module.h>
17#include <linux/err.h>
18
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +010019#define AXI_CLKGEN_V2_REG_RESET 0x40
Lars-Peter Clausen62d1e782015-11-30 17:54:56 +010020#define AXI_CLKGEN_V2_REG_CLKSEL 0x44
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +010021#define AXI_CLKGEN_V2_REG_DRP_CNTRL 0x70
22#define AXI_CLKGEN_V2_REG_DRP_STATUS 0x74
23
24#define AXI_CLKGEN_V2_RESET_MMCM_ENABLE BIT(1)
25#define AXI_CLKGEN_V2_RESET_ENABLE BIT(0)
26
27#define AXI_CLKGEN_V2_DRP_CNTRL_SEL BIT(29)
28#define AXI_CLKGEN_V2_DRP_CNTRL_READ BIT(28)
29
30#define AXI_CLKGEN_V2_DRP_STATUS_BUSY BIT(16)
31
32#define MMCM_REG_CLKOUT0_1 0x08
33#define MMCM_REG_CLKOUT0_2 0x09
34#define MMCM_REG_CLK_FB1 0x14
35#define MMCM_REG_CLK_FB2 0x15
36#define MMCM_REG_CLK_DIV 0x16
37#define MMCM_REG_LOCK1 0x18
38#define MMCM_REG_LOCK2 0x19
39#define MMCM_REG_LOCK3 0x1a
40#define MMCM_REG_FILTER1 0x4e
41#define MMCM_REG_FILTER2 0x4f
42
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +010043struct axi_clkgen {
44 void __iomem *base;
45 struct clk_hw clk_hw;
46};
47
48static uint32_t axi_clkgen_lookup_filter(unsigned int m)
49{
50 switch (m) {
51 case 0:
52 return 0x01001990;
53 case 1:
54 return 0x01001190;
55 case 2:
56 return 0x01009890;
57 case 3:
58 return 0x01001890;
59 case 4:
60 return 0x01008890;
61 case 5 ... 8:
62 return 0x01009090;
63 case 9 ... 11:
64 return 0x01000890;
65 case 12:
66 return 0x08009090;
67 case 13 ... 22:
68 return 0x01001090;
69 case 23 ... 36:
70 return 0x01008090;
71 case 37 ... 46:
72 return 0x08001090;
73 default:
74 return 0x08008090;
75 }
76}
77
78static const uint32_t axi_clkgen_lock_table[] = {
79 0x060603e8, 0x060603e8, 0x080803e8, 0x0b0b03e8,
80 0x0e0e03e8, 0x111103e8, 0x131303e8, 0x161603e8,
81 0x191903e8, 0x1c1c03e8, 0x1f1f0384, 0x1f1f0339,
82 0x1f1f02ee, 0x1f1f02bc, 0x1f1f028a, 0x1f1f0271,
83 0x1f1f023f, 0x1f1f0226, 0x1f1f020d, 0x1f1f01f4,
84 0x1f1f01db, 0x1f1f01c2, 0x1f1f01a9, 0x1f1f0190,
85 0x1f1f0190, 0x1f1f0177, 0x1f1f015e, 0x1f1f015e,
86 0x1f1f0145, 0x1f1f0145, 0x1f1f012c, 0x1f1f012c,
87 0x1f1f012c, 0x1f1f0113, 0x1f1f0113, 0x1f1f0113,
88};
89
90static uint32_t axi_clkgen_lookup_lock(unsigned int m)
91{
92 if (m < ARRAY_SIZE(axi_clkgen_lock_table))
93 return axi_clkgen_lock_table[m];
94 return 0x1f1f00fa;
95}
96
97static const unsigned int fpfd_min = 10000;
98static const unsigned int fpfd_max = 300000;
99static const unsigned int fvco_min = 600000;
100static const unsigned int fvco_max = 1200000;
101
102static void axi_clkgen_calc_params(unsigned long fin, unsigned long fout,
103 unsigned int *best_d, unsigned int *best_m, unsigned int *best_dout)
104{
105 unsigned long d, d_min, d_max, _d_min, _d_max;
106 unsigned long m, m_min, m_max;
107 unsigned long f, dout, best_f, fvco;
108
109 fin /= 1000;
110 fout /= 1000;
111
112 best_f = ULONG_MAX;
113 *best_d = 0;
114 *best_m = 0;
115 *best_dout = 0;
116
117 d_min = max_t(unsigned long, DIV_ROUND_UP(fin, fpfd_max), 1);
118 d_max = min_t(unsigned long, fin / fpfd_min, 80);
119
120 m_min = max_t(unsigned long, DIV_ROUND_UP(fvco_min, fin) * d_min, 1);
121 m_max = min_t(unsigned long, fvco_max * d_max / fin, 64);
122
123 for (m = m_min; m <= m_max; m++) {
124 _d_min = max(d_min, DIV_ROUND_UP(fin * m, fvco_max));
125 _d_max = min(d_max, fin * m / fvco_min);
126
127 for (d = _d_min; d <= _d_max; d++) {
128 fvco = fin * m / d;
129
130 dout = DIV_ROUND_CLOSEST(fvco, fout);
131 dout = clamp_t(unsigned long, dout, 1, 128);
132 f = fvco / dout;
133 if (abs(f - fout) < abs(best_f - fout)) {
134 best_f = f;
135 *best_d = d;
136 *best_m = m;
137 *best_dout = dout;
138 if (best_f == fout)
139 return;
140 }
141 }
142 }
143}
144
145static void axi_clkgen_calc_clk_params(unsigned int divider, unsigned int *low,
146 unsigned int *high, unsigned int *edge, unsigned int *nocount)
147{
148 if (divider == 1)
149 *nocount = 1;
150 else
151 *nocount = 0;
152
153 *high = divider / 2;
154 *edge = divider % 2;
155 *low = divider - *high;
156}
157
158static void axi_clkgen_write(struct axi_clkgen *axi_clkgen,
159 unsigned int reg, unsigned int val)
160{
161 writel(val, axi_clkgen->base + reg);
162}
163
164static void axi_clkgen_read(struct axi_clkgen *axi_clkgen,
165 unsigned int reg, unsigned int *val)
166{
167 *val = readl(axi_clkgen->base + reg);
168}
169
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100170static int axi_clkgen_wait_non_busy(struct axi_clkgen *axi_clkgen)
171{
172 unsigned int timeout = 10000;
173 unsigned int val;
174
175 do {
176 axi_clkgen_read(axi_clkgen, AXI_CLKGEN_V2_REG_DRP_STATUS, &val);
177 } while ((val & AXI_CLKGEN_V2_DRP_STATUS_BUSY) && --timeout);
178
179 if (val & AXI_CLKGEN_V2_DRP_STATUS_BUSY)
180 return -EIO;
181
182 return val & 0xffff;
183}
184
Lars-Peter Clausend95b5992015-11-30 17:54:55 +0100185static int axi_clkgen_mmcm_read(struct axi_clkgen *axi_clkgen,
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100186 unsigned int reg, unsigned int *val)
187{
188 unsigned int reg_val;
189 int ret;
190
191 ret = axi_clkgen_wait_non_busy(axi_clkgen);
192 if (ret < 0)
193 return ret;
194
195 reg_val = AXI_CLKGEN_V2_DRP_CNTRL_SEL | AXI_CLKGEN_V2_DRP_CNTRL_READ;
196 reg_val |= (reg << 16);
197
198 axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_DRP_CNTRL, reg_val);
199
200 ret = axi_clkgen_wait_non_busy(axi_clkgen);
201 if (ret < 0)
202 return ret;
203
204 *val = ret;
205
206 return 0;
207}
208
Lars-Peter Clausend95b5992015-11-30 17:54:55 +0100209static int axi_clkgen_mmcm_write(struct axi_clkgen *axi_clkgen,
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100210 unsigned int reg, unsigned int val, unsigned int mask)
211{
212 unsigned int reg_val = 0;
213 int ret;
214
215 ret = axi_clkgen_wait_non_busy(axi_clkgen);
216 if (ret < 0)
217 return ret;
218
219 if (mask != 0xffff) {
Lars-Peter Clausend95b5992015-11-30 17:54:55 +0100220 axi_clkgen_mmcm_read(axi_clkgen, reg, &reg_val);
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100221 reg_val &= ~mask;
222 }
223
224 reg_val |= AXI_CLKGEN_V2_DRP_CNTRL_SEL | (reg << 16) | (val & mask);
225
226 axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_DRP_CNTRL, reg_val);
227
228 return 0;
229}
230
Lars-Peter Clausend95b5992015-11-30 17:54:55 +0100231static void axi_clkgen_mmcm_enable(struct axi_clkgen *axi_clkgen,
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100232 bool enable)
233{
234 unsigned int val = AXI_CLKGEN_V2_RESET_ENABLE;
235
236 if (enable)
237 val |= AXI_CLKGEN_V2_RESET_MMCM_ENABLE;
238
239 axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_RESET, val);
240}
241
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100242static struct axi_clkgen *clk_hw_to_axi_clkgen(struct clk_hw *clk_hw)
243{
244 return container_of(clk_hw, struct axi_clkgen, clk_hw);
245}
246
247static int axi_clkgen_set_rate(struct clk_hw *clk_hw,
248 unsigned long rate, unsigned long parent_rate)
249{
250 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
251 unsigned int d, m, dout;
252 unsigned int nocount;
253 unsigned int high;
254 unsigned int edge;
255 unsigned int low;
256 uint32_t filter;
257 uint32_t lock;
258
259 if (parent_rate == 0 || rate == 0)
260 return -EINVAL;
261
262 axi_clkgen_calc_params(parent_rate, rate, &d, &m, &dout);
263
264 if (d == 0 || dout == 0 || m == 0)
265 return -EINVAL;
266
267 filter = axi_clkgen_lookup_filter(m - 1);
268 lock = axi_clkgen_lookup_lock(m - 1);
269
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100270 axi_clkgen_calc_clk_params(dout, &low, &high, &edge, &nocount);
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100271 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_CLKOUT0_1,
272 (high << 6) | low, 0xefff);
273 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_CLKOUT0_2,
274 (edge << 7) | (nocount << 6), 0x03ff);
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100275
276 axi_clkgen_calc_clk_params(d, &low, &high, &edge, &nocount);
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100277 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_CLK_DIV,
278 (edge << 13) | (nocount << 12) | (high << 6) | low, 0x3fff);
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100279
280 axi_clkgen_calc_clk_params(m, &low, &high, &edge, &nocount);
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100281 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_CLK_FB1,
282 (high << 6) | low, 0xefff);
283 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_CLK_FB2,
284 (edge << 7) | (nocount << 6), 0x03ff);
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100285
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100286 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_LOCK1, lock & 0x3ff, 0x3ff);
287 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_LOCK2,
288 (((lock >> 16) & 0x1f) << 10) | 0x1, 0x7fff);
289 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_LOCK3,
290 (((lock >> 24) & 0x1f) << 10) | 0x3e9, 0x7fff);
291 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_FILTER1, filter >> 16, 0x9900);
292 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_FILTER2, filter, 0x9900);
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100293
294 return 0;
295}
296
297static long axi_clkgen_round_rate(struct clk_hw *hw, unsigned long rate,
298 unsigned long *parent_rate)
299{
300 unsigned int d, m, dout;
301
302 axi_clkgen_calc_params(*parent_rate, rate, &d, &m, &dout);
303
304 if (d == 0 || dout == 0 || m == 0)
305 return -EINVAL;
306
307 return *parent_rate / d * m / dout;
308}
309
310static unsigned long axi_clkgen_recalc_rate(struct clk_hw *clk_hw,
311 unsigned long parent_rate)
312{
313 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
314 unsigned int d, m, dout;
315 unsigned int reg;
316 unsigned long long tmp;
317
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100318 axi_clkgen_mmcm_read(axi_clkgen, MMCM_REG_CLKOUT0_1, &reg);
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100319 dout = (reg & 0x3f) + ((reg >> 6) & 0x3f);
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100320 axi_clkgen_mmcm_read(axi_clkgen, MMCM_REG_CLK_DIV, &reg);
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100321 d = (reg & 0x3f) + ((reg >> 6) & 0x3f);
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100322 axi_clkgen_mmcm_read(axi_clkgen, MMCM_REG_CLK_FB1, &reg);
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100323 m = (reg & 0x3f) + ((reg >> 6) & 0x3f);
324
325 if (d == 0 || dout == 0)
326 return 0;
327
328 tmp = (unsigned long long)(parent_rate / d) * m;
329 do_div(tmp, dout);
330
331 if (tmp > ULONG_MAX)
332 return ULONG_MAX;
333
334 return tmp;
335}
336
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100337static int axi_clkgen_enable(struct clk_hw *clk_hw)
338{
339 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
340
341 axi_clkgen_mmcm_enable(axi_clkgen, true);
342
343 return 0;
344}
345
346static void axi_clkgen_disable(struct clk_hw *clk_hw)
347{
348 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
349
350 axi_clkgen_mmcm_enable(axi_clkgen, false);
351}
352
Lars-Peter Clausen62d1e782015-11-30 17:54:56 +0100353static int axi_clkgen_set_parent(struct clk_hw *clk_hw, u8 index)
354{
355 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
356
357 axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_CLKSEL, index);
358
359 return 0;
360}
361
362static u8 axi_clkgen_get_parent(struct clk_hw *clk_hw)
363{
364 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
365 unsigned int parent;
366
367 axi_clkgen_read(axi_clkgen, AXI_CLKGEN_V2_REG_CLKSEL, &parent);
368
369 return parent;
370}
371
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100372static const struct clk_ops axi_clkgen_ops = {
373 .recalc_rate = axi_clkgen_recalc_rate,
374 .round_rate = axi_clkgen_round_rate,
375 .set_rate = axi_clkgen_set_rate,
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100376 .enable = axi_clkgen_enable,
377 .disable = axi_clkgen_disable,
Lars-Peter Clausen62d1e782015-11-30 17:54:56 +0100378 .set_parent = axi_clkgen_set_parent,
379 .get_parent = axi_clkgen_get_parent,
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100380};
381
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100382static const struct of_device_id axi_clkgen_ids[] = {
383 {
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100384 .compatible = "adi,axi-clkgen-2.00.a",
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100385 },
386 { },
387};
388MODULE_DEVICE_TABLE(of, axi_clkgen_ids);
389
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100390static int axi_clkgen_probe(struct platform_device *pdev)
391{
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100392 const struct of_device_id *id;
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100393 struct axi_clkgen *axi_clkgen;
394 struct clk_init_data init;
Lars-Peter Clausen62d1e782015-11-30 17:54:56 +0100395 const char *parent_names[2];
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100396 const char *clk_name;
397 struct resource *mem;
398 struct clk *clk;
Lars-Peter Clausen62d1e782015-11-30 17:54:56 +0100399 unsigned int i;
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100400
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100401 if (!pdev->dev.of_node)
402 return -ENODEV;
403
404 id = of_match_node(axi_clkgen_ids, pdev->dev.of_node);
405 if (!id)
406 return -ENODEV;
407
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100408 axi_clkgen = devm_kzalloc(&pdev->dev, sizeof(*axi_clkgen), GFP_KERNEL);
409 if (!axi_clkgen)
410 return -ENOMEM;
411
412 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
413 axi_clkgen->base = devm_ioremap_resource(&pdev->dev, mem);
414 if (IS_ERR(axi_clkgen->base))
415 return PTR_ERR(axi_clkgen->base);
416
Lars-Peter Clausen62d1e782015-11-30 17:54:56 +0100417 init.num_parents = of_clk_get_parent_count(pdev->dev.of_node);
418 if (init.num_parents < 1 || init.num_parents > 2)
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100419 return -EINVAL;
420
Lars-Peter Clausen62d1e782015-11-30 17:54:56 +0100421 for (i = 0; i < init.num_parents; i++) {
422 parent_names[i] = of_clk_get_parent_name(pdev->dev.of_node, i);
423 if (!parent_names[i])
424 return -EINVAL;
425 }
426
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100427 clk_name = pdev->dev.of_node->name;
428 of_property_read_string(pdev->dev.of_node, "clock-output-names",
429 &clk_name);
430
431 init.name = clk_name;
432 init.ops = &axi_clkgen_ops;
Lars-Peter Clausen62d1e782015-11-30 17:54:56 +0100433 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
434 init.parent_names = parent_names;
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100435
Lars-Peter Clausen1887c3a2014-02-17 10:31:53 +0100436 axi_clkgen_mmcm_enable(axi_clkgen, false);
437
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100438 axi_clkgen->clk_hw.init = &init;
439 clk = devm_clk_register(&pdev->dev, &axi_clkgen->clk_hw);
440 if (IS_ERR(clk))
441 return PTR_ERR(clk);
442
443 return of_clk_add_provider(pdev->dev.of_node, of_clk_src_simple_get,
444 clk);
445}
446
447static int axi_clkgen_remove(struct platform_device *pdev)
448{
449 of_clk_del_provider(pdev->dev.of_node);
450
451 return 0;
452}
453
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100454static struct platform_driver axi_clkgen_driver = {
455 .driver = {
456 .name = "adi-axi-clkgen",
Lars-Peter Clausen0e646c52013-03-11 16:22:29 +0100457 .of_match_table = axi_clkgen_ids,
458 },
459 .probe = axi_clkgen_probe,
460 .remove = axi_clkgen_remove,
461};
462module_platform_driver(axi_clkgen_driver);
463
464MODULE_LICENSE("GPL v2");
465MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
466MODULE_DESCRIPTION("Driver for the Analog Devices' AXI clkgen pcore clock generator");