2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
14 #include <linux/delay.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
19 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/sched.h>
22 #include <linux/wait.h>
26 #define SLOW_CLOCK_FREQ 32768
28 #define MAINFRDY_TIMEOUT (((MAINF_DIV + 1) * USEC_PER_SEC) / \
30 #define MAINF_LOOP_MIN_WAIT (USEC_PER_SEC / SLOW_CLOCK_FREQ)
31 #define MAINF_LOOP_MAX_WAIT MAINFRDY_TIMEOUT
33 #define MOR_KEY_MASK (0xff << 16)
39 wait_queue_head_t wait;
42 #define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw)
44 struct clk_main_rc_osc {
48 wait_queue_head_t wait;
49 unsigned long frequency;
50 unsigned long accuracy;
53 #define to_clk_main_rc_osc(hw) container_of(hw, struct clk_main_rc_osc, hw)
55 struct clk_rm9200_main {
60 #define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw)
62 struct clk_sam9x5_main {
66 wait_queue_head_t wait;
70 #define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw)
72 static irqreturn_t clk_main_osc_irq_handler(int irq, void *dev_id)
74 struct clk_main_osc *osc = dev_id;
77 disable_irq_nosync(osc->irq);
82 static int clk_main_osc_prepare(struct clk_hw *hw)
84 struct clk_main_osc *osc = to_clk_main_osc(hw);
85 struct at91_pmc *pmc = osc->pmc;
88 tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
89 if (tmp & AT91_PMC_OSCBYPASS)
92 if (!(tmp & AT91_PMC_MOSCEN)) {
93 tmp |= AT91_PMC_MOSCEN | AT91_PMC_KEY;
94 pmc_write(pmc, AT91_CKGR_MOR, tmp);
97 while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS)) {
100 pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS);
106 static void clk_main_osc_unprepare(struct clk_hw *hw)
108 struct clk_main_osc *osc = to_clk_main_osc(hw);
109 struct at91_pmc *pmc = osc->pmc;
110 u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
112 if (tmp & AT91_PMC_OSCBYPASS)
115 if (!(tmp & AT91_PMC_MOSCEN))
118 tmp &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN);
119 pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
122 static int clk_main_osc_is_prepared(struct clk_hw *hw)
124 struct clk_main_osc *osc = to_clk_main_osc(hw);
125 struct at91_pmc *pmc = osc->pmc;
126 u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
128 if (tmp & AT91_PMC_OSCBYPASS)
131 return !!((pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS) &&
132 (pmc_read(pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCEN));
135 static const struct clk_ops main_osc_ops = {
136 .prepare = clk_main_osc_prepare,
137 .unprepare = clk_main_osc_unprepare,
138 .is_prepared = clk_main_osc_is_prepared,
141 static struct clk * __init
142 at91_clk_register_main_osc(struct at91_pmc *pmc,
145 const char *parent_name,
149 struct clk_main_osc *osc;
150 struct clk *clk = NULL;
151 struct clk_init_data init;
153 if (!pmc || !irq || !name || !parent_name)
154 return ERR_PTR(-EINVAL);
156 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
158 return ERR_PTR(-ENOMEM);
161 init.ops = &main_osc_ops;
162 init.parent_names = &parent_name;
163 init.num_parents = 1;
164 init.flags = CLK_IGNORE_UNUSED;
166 osc->hw.init = &init;
170 init_waitqueue_head(&osc->wait);
171 irq_set_status_flags(osc->irq, IRQ_NOAUTOEN);
172 ret = request_irq(osc->irq, clk_main_osc_irq_handler,
173 IRQF_TRIGGER_HIGH, name, osc);
178 pmc_write(pmc, AT91_CKGR_MOR,
179 (pmc_read(pmc, AT91_CKGR_MOR) &
180 ~(MOR_KEY_MASK | AT91_PMC_MOSCEN)) |
181 AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
183 clk = clk_register(NULL, &osc->hw);
192 void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np,
193 struct at91_pmc *pmc)
197 const char *name = np->name;
198 const char *parent_name;
201 of_property_read_string(np, "clock-output-names", &name);
202 bypass = of_property_read_bool(np, "atmel,osc-bypass");
203 parent_name = of_clk_get_parent_name(np, 0);
205 irq = irq_of_parse_and_map(np, 0);
209 clk = at91_clk_register_main_osc(pmc, irq, name, parent_name, bypass);
213 of_clk_add_provider(np, of_clk_src_simple_get, clk);
216 static irqreturn_t clk_main_rc_osc_irq_handler(int irq, void *dev_id)
218 struct clk_main_rc_osc *osc = dev_id;
221 disable_irq_nosync(osc->irq);
226 static int clk_main_rc_osc_prepare(struct clk_hw *hw)
228 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
229 struct at91_pmc *pmc = osc->pmc;
232 tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
234 if (!(tmp & AT91_PMC_MOSCRCEN)) {
235 tmp |= AT91_PMC_MOSCRCEN | AT91_PMC_KEY;
236 pmc_write(pmc, AT91_CKGR_MOR, tmp);
239 while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS)) {
240 enable_irq(osc->irq);
241 wait_event(osc->wait,
242 pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS);
248 static void clk_main_rc_osc_unprepare(struct clk_hw *hw)
250 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
251 struct at91_pmc *pmc = osc->pmc;
252 u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
254 if (!(tmp & AT91_PMC_MOSCRCEN))
257 tmp &= ~(MOR_KEY_MASK | AT91_PMC_MOSCRCEN);
258 pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
261 static int clk_main_rc_osc_is_prepared(struct clk_hw *hw)
263 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
264 struct at91_pmc *pmc = osc->pmc;
266 return !!((pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS) &&
267 (pmc_read(pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCRCEN));
270 static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw,
271 unsigned long parent_rate)
273 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
275 return osc->frequency;
278 static unsigned long clk_main_rc_osc_recalc_accuracy(struct clk_hw *hw,
279 unsigned long parent_acc)
281 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
283 return osc->accuracy;
286 static const struct clk_ops main_rc_osc_ops = {
287 .prepare = clk_main_rc_osc_prepare,
288 .unprepare = clk_main_rc_osc_unprepare,
289 .is_prepared = clk_main_rc_osc_is_prepared,
290 .recalc_rate = clk_main_rc_osc_recalc_rate,
291 .recalc_accuracy = clk_main_rc_osc_recalc_accuracy,
294 static struct clk * __init
295 at91_clk_register_main_rc_osc(struct at91_pmc *pmc,
298 u32 frequency, u32 accuracy)
301 struct clk_main_rc_osc *osc;
302 struct clk *clk = NULL;
303 struct clk_init_data init;
305 if (!pmc || !irq || !name || !frequency)
306 return ERR_PTR(-EINVAL);
308 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
310 return ERR_PTR(-ENOMEM);
313 init.ops = &main_rc_osc_ops;
314 init.parent_names = NULL;
315 init.num_parents = 0;
316 init.flags = CLK_IS_ROOT | CLK_IGNORE_UNUSED;
318 osc->hw.init = &init;
321 osc->frequency = frequency;
322 osc->accuracy = accuracy;
324 init_waitqueue_head(&osc->wait);
325 irq_set_status_flags(osc->irq, IRQ_NOAUTOEN);
326 ret = request_irq(osc->irq, clk_main_rc_osc_irq_handler,
327 IRQF_TRIGGER_HIGH, name, osc);
331 clk = clk_register(NULL, &osc->hw);
340 void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np,
341 struct at91_pmc *pmc)
347 const char *name = np->name;
349 of_property_read_string(np, "clock-output-names", &name);
350 of_property_read_u32(np, "clock-frequency", &frequency);
351 of_property_read_u32(np, "clock-accuracy", &accuracy);
353 irq = irq_of_parse_and_map(np, 0);
357 clk = at91_clk_register_main_rc_osc(pmc, irq, name, frequency,
362 of_clk_add_provider(np, of_clk_src_simple_get, clk);
366 static int clk_main_probe_frequency(struct at91_pmc *pmc)
368 unsigned long prep_time, timeout;
371 timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT);
374 tmp = pmc_read(pmc, AT91_CKGR_MCFR);
375 if (tmp & AT91_PMC_MAINRDY)
377 usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT);
378 } while (time_before(prep_time, timeout));
383 static unsigned long clk_main_recalc_rate(struct at91_pmc *pmc,
384 unsigned long parent_rate)
391 tmp = pmc_read(pmc, AT91_CKGR_MCFR);
392 if (!(tmp & AT91_PMC_MAINRDY))
395 return ((tmp & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
398 static int clk_rm9200_main_prepare(struct clk_hw *hw)
400 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
402 return clk_main_probe_frequency(clkmain->pmc);
405 static int clk_rm9200_main_is_prepared(struct clk_hw *hw)
407 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
409 return !!(pmc_read(clkmain->pmc, AT91_CKGR_MCFR) & AT91_PMC_MAINRDY);
412 static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
413 unsigned long parent_rate)
415 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
417 return clk_main_recalc_rate(clkmain->pmc, parent_rate);
420 static const struct clk_ops rm9200_main_ops = {
421 .prepare = clk_rm9200_main_prepare,
422 .is_prepared = clk_rm9200_main_is_prepared,
423 .recalc_rate = clk_rm9200_main_recalc_rate,
426 static struct clk * __init
427 at91_clk_register_rm9200_main(struct at91_pmc *pmc,
429 const char *parent_name)
431 struct clk_rm9200_main *clkmain;
432 struct clk *clk = NULL;
433 struct clk_init_data init;
436 return ERR_PTR(-EINVAL);
439 return ERR_PTR(-EINVAL);
441 clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
443 return ERR_PTR(-ENOMEM);
446 init.ops = &rm9200_main_ops;
447 init.parent_names = &parent_name;
448 init.num_parents = 1;
451 clkmain->hw.init = &init;
454 clk = clk_register(NULL, &clkmain->hw);
461 void __init of_at91rm9200_clk_main_setup(struct device_node *np,
462 struct at91_pmc *pmc)
465 const char *parent_name;
466 const char *name = np->name;
468 parent_name = of_clk_get_parent_name(np, 0);
469 of_property_read_string(np, "clock-output-names", &name);
471 clk = at91_clk_register_rm9200_main(pmc, name, parent_name);
475 of_clk_add_provider(np, of_clk_src_simple_get, clk);
478 static irqreturn_t clk_sam9x5_main_irq_handler(int irq, void *dev_id)
480 struct clk_sam9x5_main *clkmain = dev_id;
482 wake_up(&clkmain->wait);
483 disable_irq_nosync(clkmain->irq);
488 static int clk_sam9x5_main_prepare(struct clk_hw *hw)
490 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
491 struct at91_pmc *pmc = clkmain->pmc;
493 while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS)) {
494 enable_irq(clkmain->irq);
495 wait_event(clkmain->wait,
496 pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
499 return clk_main_probe_frequency(pmc);
502 static int clk_sam9x5_main_is_prepared(struct clk_hw *hw)
504 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
506 return !!(pmc_read(clkmain->pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
509 static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
510 unsigned long parent_rate)
512 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
514 return clk_main_recalc_rate(clkmain->pmc, parent_rate);
517 static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
519 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
520 struct at91_pmc *pmc = clkmain->pmc;
526 tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
528 if (index && !(tmp & AT91_PMC_MOSCSEL))
529 pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
530 else if (!index && (tmp & AT91_PMC_MOSCSEL))
531 pmc_write(pmc, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
533 while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS)) {
534 enable_irq(clkmain->irq);
535 wait_event(clkmain->wait,
536 pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
542 static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw)
544 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
546 return !!(pmc_read(clkmain->pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCEN);
549 static const struct clk_ops sam9x5_main_ops = {
550 .prepare = clk_sam9x5_main_prepare,
551 .is_prepared = clk_sam9x5_main_is_prepared,
552 .recalc_rate = clk_sam9x5_main_recalc_rate,
553 .set_parent = clk_sam9x5_main_set_parent,
554 .get_parent = clk_sam9x5_main_get_parent,
557 static struct clk * __init
558 at91_clk_register_sam9x5_main(struct at91_pmc *pmc,
561 const char **parent_names,
565 struct clk_sam9x5_main *clkmain;
566 struct clk *clk = NULL;
567 struct clk_init_data init;
569 if (!pmc || !irq || !name)
570 return ERR_PTR(-EINVAL);
572 if (!parent_names || !num_parents)
573 return ERR_PTR(-EINVAL);
575 clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
577 return ERR_PTR(-ENOMEM);
580 init.ops = &sam9x5_main_ops;
581 init.parent_names = parent_names;
582 init.num_parents = num_parents;
583 init.flags = CLK_SET_PARENT_GATE;
585 clkmain->hw.init = &init;
588 clkmain->parent = !!(pmc_read(clkmain->pmc, AT91_CKGR_MOR) &
590 init_waitqueue_head(&clkmain->wait);
591 irq_set_status_flags(clkmain->irq, IRQ_NOAUTOEN);
592 ret = request_irq(clkmain->irq, clk_sam9x5_main_irq_handler,
593 IRQF_TRIGGER_HIGH, name, clkmain);
597 clk = clk_register(NULL, &clkmain->hw);
599 free_irq(clkmain->irq, clkmain);
606 void __init of_at91sam9x5_clk_main_setup(struct device_node *np,
607 struct at91_pmc *pmc)
610 const char *parent_names[2];
613 const char *name = np->name;
616 num_parents = of_count_phandle_with_args(np, "clocks", "#clock-cells");
617 if (num_parents <= 0 || num_parents > 2)
620 for (i = 0; i < num_parents; ++i) {
621 parent_names[i] = of_clk_get_parent_name(np, i);
622 if (!parent_names[i])
626 of_property_read_string(np, "clock-output-names", &name);
628 irq = irq_of_parse_and_map(np, 0);
632 clk = at91_clk_register_sam9x5_main(pmc, irq, name, parent_names,
637 of_clk_add_provider(np, of_clk_src_simple_get, clk);