These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / clk / at91 / clk-slow.c
1 /*
2  * drivers/clk/at91/clk-slow.c
3  *
4  *  Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
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  */
12
13 #include <linux/clk.h>
14 #include <linux/clk-provider.h>
15 #include <linux/clkdev.h>
16 #include <linux/clk/at91_pmc.h>
17 #include <linux/delay.h>
18 #include <linux/of.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/regmap.h>
21
22 #include "pmc.h"
23 #include "sckc.h"
24
25 #define SLOW_CLOCK_FREQ         32768
26 #define SLOWCK_SW_CYCLES        5
27 #define SLOWCK_SW_TIME_USEC     ((SLOWCK_SW_CYCLES * USEC_PER_SEC) / \
28                                  SLOW_CLOCK_FREQ)
29
30 #define AT91_SCKC_CR                    0x00
31 #define         AT91_SCKC_RCEN          (1 << 0)
32 #define         AT91_SCKC_OSC32EN       (1 << 1)
33 #define         AT91_SCKC_OSC32BYP      (1 << 2)
34 #define         AT91_SCKC_OSCSEL        (1 << 3)
35
36 struct clk_slow_osc {
37         struct clk_hw hw;
38         void __iomem *sckcr;
39         unsigned long startup_usec;
40 };
41
42 #define to_clk_slow_osc(hw) container_of(hw, struct clk_slow_osc, hw)
43
44 struct clk_slow_rc_osc {
45         struct clk_hw hw;
46         void __iomem *sckcr;
47         unsigned long frequency;
48         unsigned long accuracy;
49         unsigned long startup_usec;
50 };
51
52 #define to_clk_slow_rc_osc(hw) container_of(hw, struct clk_slow_rc_osc, hw)
53
54 struct clk_sam9260_slow {
55         struct clk_hw hw;
56         struct regmap *regmap;
57 };
58
59 #define to_clk_sam9260_slow(hw) container_of(hw, struct clk_sam9260_slow, hw)
60
61 struct clk_sam9x5_slow {
62         struct clk_hw hw;
63         void __iomem *sckcr;
64         u8 parent;
65 };
66
67 #define to_clk_sam9x5_slow(hw) container_of(hw, struct clk_sam9x5_slow, hw)
68
69 static struct clk *slow_clk;
70
71 static int clk_slow_osc_prepare(struct clk_hw *hw)
72 {
73         struct clk_slow_osc *osc = to_clk_slow_osc(hw);
74         void __iomem *sckcr = osc->sckcr;
75         u32 tmp = readl(sckcr);
76
77         if (tmp & AT91_SCKC_OSC32BYP)
78                 return 0;
79
80         writel(tmp | AT91_SCKC_OSC32EN, sckcr);
81
82         usleep_range(osc->startup_usec, osc->startup_usec + 1);
83
84         return 0;
85 }
86
87 static void clk_slow_osc_unprepare(struct clk_hw *hw)
88 {
89         struct clk_slow_osc *osc = to_clk_slow_osc(hw);
90         void __iomem *sckcr = osc->sckcr;
91         u32 tmp = readl(sckcr);
92
93         if (tmp & AT91_SCKC_OSC32BYP)
94                 return;
95
96         writel(tmp & ~AT91_SCKC_OSC32EN, sckcr);
97 }
98
99 static int clk_slow_osc_is_prepared(struct clk_hw *hw)
100 {
101         struct clk_slow_osc *osc = to_clk_slow_osc(hw);
102         void __iomem *sckcr = osc->sckcr;
103         u32 tmp = readl(sckcr);
104
105         if (tmp & AT91_SCKC_OSC32BYP)
106                 return 1;
107
108         return !!(tmp & AT91_SCKC_OSC32EN);
109 }
110
111 static const struct clk_ops slow_osc_ops = {
112         .prepare = clk_slow_osc_prepare,
113         .unprepare = clk_slow_osc_unprepare,
114         .is_prepared = clk_slow_osc_is_prepared,
115 };
116
117 static struct clk * __init
118 at91_clk_register_slow_osc(void __iomem *sckcr,
119                            const char *name,
120                            const char *parent_name,
121                            unsigned long startup,
122                            bool bypass)
123 {
124         struct clk_slow_osc *osc;
125         struct clk *clk = NULL;
126         struct clk_init_data init;
127
128         if (!sckcr || !name || !parent_name)
129                 return ERR_PTR(-EINVAL);
130
131         osc = kzalloc(sizeof(*osc), GFP_KERNEL);
132         if (!osc)
133                 return ERR_PTR(-ENOMEM);
134
135         init.name = name;
136         init.ops = &slow_osc_ops;
137         init.parent_names = &parent_name;
138         init.num_parents = 1;
139         init.flags = CLK_IGNORE_UNUSED;
140
141         osc->hw.init = &init;
142         osc->sckcr = sckcr;
143         osc->startup_usec = startup;
144
145         if (bypass)
146                 writel((readl(sckcr) & ~AT91_SCKC_OSC32EN) | AT91_SCKC_OSC32BYP,
147                        sckcr);
148
149         clk = clk_register(NULL, &osc->hw);
150         if (IS_ERR(clk))
151                 kfree(osc);
152
153         return clk;
154 }
155
156 void __init of_at91sam9x5_clk_slow_osc_setup(struct device_node *np,
157                                              void __iomem *sckcr)
158 {
159         struct clk *clk;
160         const char *parent_name;
161         const char *name = np->name;
162         u32 startup;
163         bool bypass;
164
165         parent_name = of_clk_get_parent_name(np, 0);
166         of_property_read_string(np, "clock-output-names", &name);
167         of_property_read_u32(np, "atmel,startup-time-usec", &startup);
168         bypass = of_property_read_bool(np, "atmel,osc-bypass");
169
170         clk = at91_clk_register_slow_osc(sckcr, name, parent_name, startup,
171                                          bypass);
172         if (IS_ERR(clk))
173                 return;
174
175         of_clk_add_provider(np, of_clk_src_simple_get, clk);
176 }
177
178 static unsigned long clk_slow_rc_osc_recalc_rate(struct clk_hw *hw,
179                                                  unsigned long parent_rate)
180 {
181         struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
182
183         return osc->frequency;
184 }
185
186 static unsigned long clk_slow_rc_osc_recalc_accuracy(struct clk_hw *hw,
187                                                      unsigned long parent_acc)
188 {
189         struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
190
191         return osc->accuracy;
192 }
193
194 static int clk_slow_rc_osc_prepare(struct clk_hw *hw)
195 {
196         struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
197         void __iomem *sckcr = osc->sckcr;
198
199         writel(readl(sckcr) | AT91_SCKC_RCEN, sckcr);
200
201         usleep_range(osc->startup_usec, osc->startup_usec + 1);
202
203         return 0;
204 }
205
206 static void clk_slow_rc_osc_unprepare(struct clk_hw *hw)
207 {
208         struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
209         void __iomem *sckcr = osc->sckcr;
210
211         writel(readl(sckcr) & ~AT91_SCKC_RCEN, sckcr);
212 }
213
214 static int clk_slow_rc_osc_is_prepared(struct clk_hw *hw)
215 {
216         struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
217
218         return !!(readl(osc->sckcr) & AT91_SCKC_RCEN);
219 }
220
221 static const struct clk_ops slow_rc_osc_ops = {
222         .prepare = clk_slow_rc_osc_prepare,
223         .unprepare = clk_slow_rc_osc_unprepare,
224         .is_prepared = clk_slow_rc_osc_is_prepared,
225         .recalc_rate = clk_slow_rc_osc_recalc_rate,
226         .recalc_accuracy = clk_slow_rc_osc_recalc_accuracy,
227 };
228
229 static struct clk * __init
230 at91_clk_register_slow_rc_osc(void __iomem *sckcr,
231                               const char *name,
232                               unsigned long frequency,
233                               unsigned long accuracy,
234                               unsigned long startup)
235 {
236         struct clk_slow_rc_osc *osc;
237         struct clk *clk = NULL;
238         struct clk_init_data init;
239
240         if (!sckcr || !name)
241                 return ERR_PTR(-EINVAL);
242
243         osc = kzalloc(sizeof(*osc), GFP_KERNEL);
244         if (!osc)
245                 return ERR_PTR(-ENOMEM);
246
247         init.name = name;
248         init.ops = &slow_rc_osc_ops;
249         init.parent_names = NULL;
250         init.num_parents = 0;
251         init.flags = CLK_IS_ROOT | CLK_IGNORE_UNUSED;
252
253         osc->hw.init = &init;
254         osc->sckcr = sckcr;
255         osc->frequency = frequency;
256         osc->accuracy = accuracy;
257         osc->startup_usec = startup;
258
259         clk = clk_register(NULL, &osc->hw);
260         if (IS_ERR(clk))
261                 kfree(osc);
262
263         return clk;
264 }
265
266 void __init of_at91sam9x5_clk_slow_rc_osc_setup(struct device_node *np,
267                                                 void __iomem *sckcr)
268 {
269         struct clk *clk;
270         u32 frequency = 0;
271         u32 accuracy = 0;
272         u32 startup = 0;
273         const char *name = np->name;
274
275         of_property_read_string(np, "clock-output-names", &name);
276         of_property_read_u32(np, "clock-frequency", &frequency);
277         of_property_read_u32(np, "clock-accuracy", &accuracy);
278         of_property_read_u32(np, "atmel,startup-time-usec", &startup);
279
280         clk = at91_clk_register_slow_rc_osc(sckcr, name, frequency, accuracy,
281                                             startup);
282         if (IS_ERR(clk))
283                 return;
284
285         of_clk_add_provider(np, of_clk_src_simple_get, clk);
286 }
287
288 static int clk_sam9x5_slow_set_parent(struct clk_hw *hw, u8 index)
289 {
290         struct clk_sam9x5_slow *slowck = to_clk_sam9x5_slow(hw);
291         void __iomem *sckcr = slowck->sckcr;
292         u32 tmp;
293
294         if (index > 1)
295                 return -EINVAL;
296
297         tmp = readl(sckcr);
298
299         if ((!index && !(tmp & AT91_SCKC_OSCSEL)) ||
300             (index && (tmp & AT91_SCKC_OSCSEL)))
301                 return 0;
302
303         if (index)
304                 tmp |= AT91_SCKC_OSCSEL;
305         else
306                 tmp &= ~AT91_SCKC_OSCSEL;
307
308         writel(tmp, sckcr);
309
310         usleep_range(SLOWCK_SW_TIME_USEC, SLOWCK_SW_TIME_USEC + 1);
311
312         return 0;
313 }
314
315 static u8 clk_sam9x5_slow_get_parent(struct clk_hw *hw)
316 {
317         struct clk_sam9x5_slow *slowck = to_clk_sam9x5_slow(hw);
318
319         return !!(readl(slowck->sckcr) & AT91_SCKC_OSCSEL);
320 }
321
322 static const struct clk_ops sam9x5_slow_ops = {
323         .set_parent = clk_sam9x5_slow_set_parent,
324         .get_parent = clk_sam9x5_slow_get_parent,
325 };
326
327 static struct clk * __init
328 at91_clk_register_sam9x5_slow(void __iomem *sckcr,
329                               const char *name,
330                               const char **parent_names,
331                               int num_parents)
332 {
333         struct clk_sam9x5_slow *slowck;
334         struct clk *clk = NULL;
335         struct clk_init_data init;
336
337         if (!sckcr || !name || !parent_names || !num_parents)
338                 return ERR_PTR(-EINVAL);
339
340         slowck = kzalloc(sizeof(*slowck), GFP_KERNEL);
341         if (!slowck)
342                 return ERR_PTR(-ENOMEM);
343
344         init.name = name;
345         init.ops = &sam9x5_slow_ops;
346         init.parent_names = parent_names;
347         init.num_parents = num_parents;
348         init.flags = 0;
349
350         slowck->hw.init = &init;
351         slowck->sckcr = sckcr;
352         slowck->parent = !!(readl(sckcr) & AT91_SCKC_OSCSEL);
353
354         clk = clk_register(NULL, &slowck->hw);
355         if (IS_ERR(clk))
356                 kfree(slowck);
357         else
358                 slow_clk = clk;
359
360         return clk;
361 }
362
363 void __init of_at91sam9x5_clk_slow_setup(struct device_node *np,
364                                          void __iomem *sckcr)
365 {
366         struct clk *clk;
367         const char *parent_names[2];
368         int num_parents;
369         const char *name = np->name;
370
371         num_parents = of_clk_get_parent_count(np);
372         if (num_parents <= 0 || num_parents > 2)
373                 return;
374
375         of_clk_parent_fill(np, parent_names, num_parents);
376
377         of_property_read_string(np, "clock-output-names", &name);
378
379         clk = at91_clk_register_sam9x5_slow(sckcr, name, parent_names,
380                                             num_parents);
381         if (IS_ERR(clk))
382                 return;
383
384         of_clk_add_provider(np, of_clk_src_simple_get, clk);
385 }
386
387 static u8 clk_sam9260_slow_get_parent(struct clk_hw *hw)
388 {
389         struct clk_sam9260_slow *slowck = to_clk_sam9260_slow(hw);
390         unsigned int status;
391
392         regmap_read(slowck->regmap, AT91_PMC_SR, &status);
393
394         return status & AT91_PMC_OSCSEL ? 1 : 0;
395 }
396
397 static const struct clk_ops sam9260_slow_ops = {
398         .get_parent = clk_sam9260_slow_get_parent,
399 };
400
401 static struct clk * __init
402 at91_clk_register_sam9260_slow(struct regmap *regmap,
403                                const char *name,
404                                const char **parent_names,
405                                int num_parents)
406 {
407         struct clk_sam9260_slow *slowck;
408         struct clk *clk = NULL;
409         struct clk_init_data init;
410
411         if (!name)
412                 return ERR_PTR(-EINVAL);
413
414         if (!parent_names || !num_parents)
415                 return ERR_PTR(-EINVAL);
416
417         slowck = kzalloc(sizeof(*slowck), GFP_KERNEL);
418         if (!slowck)
419                 return ERR_PTR(-ENOMEM);
420
421         init.name = name;
422         init.ops = &sam9260_slow_ops;
423         init.parent_names = parent_names;
424         init.num_parents = num_parents;
425         init.flags = 0;
426
427         slowck->hw.init = &init;
428         slowck->regmap = regmap;
429
430         clk = clk_register(NULL, &slowck->hw);
431         if (IS_ERR(clk))
432                 kfree(slowck);
433         else
434                 slow_clk = clk;
435
436         return clk;
437 }
438
439 static void __init of_at91sam9260_clk_slow_setup(struct device_node *np)
440 {
441         struct clk *clk;
442         const char *parent_names[2];
443         int num_parents;
444         const char *name = np->name;
445         struct regmap *regmap;
446
447         num_parents = of_clk_get_parent_count(np);
448         if (num_parents != 2)
449                 return;
450
451         of_clk_parent_fill(np, parent_names, num_parents);
452         regmap = syscon_node_to_regmap(of_get_parent(np));
453         if (IS_ERR(regmap))
454                 return;
455
456         of_property_read_string(np, "clock-output-names", &name);
457
458         clk = at91_clk_register_sam9260_slow(regmap, name, parent_names,
459                                              num_parents);
460         if (IS_ERR(clk))
461                 return;
462
463         of_clk_add_provider(np, of_clk_src_simple_get, clk);
464 }
465 CLK_OF_DECLARE(at91sam9260_clk_slow, "atmel,at91sam9260-clk-slow",
466                of_at91sam9260_clk_slow_setup);
467
468 /*
469  * FIXME: All slow clk users are not properly claiming it (get + prepare +
470  * enable) before using it.
471  * If all users properly claiming this clock decide that they don't need it
472  * anymore (or are removed), it is disabled while faulty users are still
473  * requiring it, and the system hangs.
474  * Prevent this clock from being disabled until all users are properly
475  * requesting it.
476  * Once this is done we should remove this function and the slow_clk variable.
477  */
478 static int __init of_at91_clk_slow_retain(void)
479 {
480         if (!slow_clk)
481                 return 0;
482
483         __clk_get(slow_clk);
484         clk_prepare_enable(slow_clk);
485
486         return 0;
487 }
488 arch_initcall(of_at91_clk_slow_retain);