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